1 // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) 2 // Copyright(c) 2015-18 Intel Corporation. 3 4 /* 5 * stream.c - SoundWire Bus stream operations. 6 */ 7 8 #include <linux/delay.h> 9 #include <linux/device.h> 10 #include <linux/init.h> 11 #include <linux/module.h> 12 #include <linux/mod_devicetable.h> 13 #include <linux/slab.h> 14 #include <linux/soundwire/sdw_registers.h> 15 #include <linux/soundwire/sdw.h> 16 #include <linux/soundwire/sdw_type.h> 17 #include <sound/soc.h> 18 #include "bus.h" 19 20 /* 21 * Array of supported rows and columns as per MIPI SoundWire Specification 1.1 22 * 23 * The rows are arranged as per the array index value programmed 24 * in register. The index 15 has dummy value 0 in order to fill hole. 25 */ 26 int sdw_rows[SDW_FRAME_ROWS] = {48, 50, 60, 64, 75, 80, 125, 147, 27 96, 100, 120, 128, 150, 160, 250, 0, 28 192, 200, 240, 256, 72, 144, 90, 180}; 29 EXPORT_SYMBOL(sdw_rows); 30 31 int sdw_cols[SDW_FRAME_COLS] = {2, 4, 6, 8, 10, 12, 14, 16}; 32 EXPORT_SYMBOL(sdw_cols); 33 34 int sdw_find_col_index(int col) 35 { 36 int i; 37 38 for (i = 0; i < SDW_FRAME_COLS; i++) { 39 if (sdw_cols[i] == col) 40 return i; 41 } 42 43 pr_warn("Requested column not found, selecting lowest column no: 2\n"); 44 return 0; 45 } 46 EXPORT_SYMBOL(sdw_find_col_index); 47 48 int sdw_find_row_index(int row) 49 { 50 int i; 51 52 for (i = 0; i < SDW_FRAME_ROWS; i++) { 53 if (sdw_rows[i] == row) 54 return i; 55 } 56 57 pr_warn("Requested row not found, selecting lowest row no: 48\n"); 58 return 0; 59 } 60 EXPORT_SYMBOL(sdw_find_row_index); 61 62 static int _sdw_program_slave_port_params(struct sdw_bus *bus, 63 struct sdw_slave *slave, 64 struct sdw_transport_params *t_params, 65 enum sdw_dpn_type type) 66 { 67 u32 addr1, addr2, addr3, addr4; 68 int ret; 69 u16 wbuf; 70 71 if (bus->params.next_bank) { 72 addr1 = SDW_DPN_OFFSETCTRL2_B1(t_params->port_num); 73 addr2 = SDW_DPN_BLOCKCTRL3_B1(t_params->port_num); 74 addr3 = SDW_DPN_SAMPLECTRL2_B1(t_params->port_num); 75 addr4 = SDW_DPN_HCTRL_B1(t_params->port_num); 76 } else { 77 addr1 = SDW_DPN_OFFSETCTRL2_B0(t_params->port_num); 78 addr2 = SDW_DPN_BLOCKCTRL3_B0(t_params->port_num); 79 addr3 = SDW_DPN_SAMPLECTRL2_B0(t_params->port_num); 80 addr4 = SDW_DPN_HCTRL_B0(t_params->port_num); 81 } 82 83 /* Program DPN_OffsetCtrl2 registers */ 84 ret = sdw_write_no_pm(slave, addr1, t_params->offset2); 85 if (ret < 0) { 86 dev_err(bus->dev, "DPN_OffsetCtrl2 register write failed\n"); 87 return ret; 88 } 89 90 /* Program DPN_BlockCtrl3 register */ 91 ret = sdw_write_no_pm(slave, addr2, t_params->blk_pkg_mode); 92 if (ret < 0) { 93 dev_err(bus->dev, "DPN_BlockCtrl3 register write failed\n"); 94 return ret; 95 } 96 97 /* 98 * Data ports are FULL, SIMPLE and REDUCED. This function handles 99 * FULL and REDUCED only and beyond this point only FULL is 100 * handled, so bail out if we are not FULL data port type 101 */ 102 if (type != SDW_DPN_FULL) 103 return ret; 104 105 /* Program DPN_SampleCtrl2 register */ 106 wbuf = FIELD_GET(SDW_DPN_SAMPLECTRL_HIGH, t_params->sample_interval - 1); 107 108 ret = sdw_write_no_pm(slave, addr3, wbuf); 109 if (ret < 0) { 110 dev_err(bus->dev, "DPN_SampleCtrl2 register write failed\n"); 111 return ret; 112 } 113 114 /* Program DPN_HCtrl register */ 115 wbuf = FIELD_PREP(SDW_DPN_HCTRL_HSTART, t_params->hstart); 116 wbuf |= FIELD_PREP(SDW_DPN_HCTRL_HSTOP, t_params->hstop); 117 118 ret = sdw_write_no_pm(slave, addr4, wbuf); 119 if (ret < 0) 120 dev_err(bus->dev, "DPN_HCtrl register write failed\n"); 121 122 return ret; 123 } 124 125 static int sdw_program_slave_port_params(struct sdw_bus *bus, 126 struct sdw_slave_runtime *s_rt, 127 struct sdw_port_runtime *p_rt) 128 { 129 struct sdw_transport_params *t_params = &p_rt->transport_params; 130 struct sdw_port_params *p_params = &p_rt->port_params; 131 struct sdw_slave_prop *slave_prop = &s_rt->slave->prop; 132 u32 addr1, addr2, addr3, addr4, addr5, addr6; 133 struct sdw_dpn_prop *dpn_prop; 134 int ret; 135 u8 wbuf; 136 137 if (s_rt->slave->is_mockup_device) 138 return 0; 139 140 dpn_prop = sdw_get_slave_dpn_prop(s_rt->slave, 141 s_rt->direction, 142 t_params->port_num); 143 if (!dpn_prop) 144 return -EINVAL; 145 146 addr1 = SDW_DPN_PORTCTRL(t_params->port_num); 147 addr2 = SDW_DPN_BLOCKCTRL1(t_params->port_num); 148 149 if (bus->params.next_bank) { 150 addr3 = SDW_DPN_SAMPLECTRL1_B1(t_params->port_num); 151 addr4 = SDW_DPN_OFFSETCTRL1_B1(t_params->port_num); 152 addr5 = SDW_DPN_BLOCKCTRL2_B1(t_params->port_num); 153 addr6 = SDW_DPN_LANECTRL_B1(t_params->port_num); 154 155 } else { 156 addr3 = SDW_DPN_SAMPLECTRL1_B0(t_params->port_num); 157 addr4 = SDW_DPN_OFFSETCTRL1_B0(t_params->port_num); 158 addr5 = SDW_DPN_BLOCKCTRL2_B0(t_params->port_num); 159 addr6 = SDW_DPN_LANECTRL_B0(t_params->port_num); 160 } 161 162 /* Program DPN_PortCtrl register */ 163 wbuf = FIELD_PREP(SDW_DPN_PORTCTRL_DATAMODE, p_params->data_mode); 164 wbuf |= FIELD_PREP(SDW_DPN_PORTCTRL_FLOWMODE, p_params->flow_mode); 165 166 ret = sdw_update_no_pm(s_rt->slave, addr1, 0xF, wbuf); 167 if (ret < 0) { 168 dev_err(&s_rt->slave->dev, 169 "DPN_PortCtrl register write failed for port %d\n", 170 t_params->port_num); 171 return ret; 172 } 173 174 if (!dpn_prop->read_only_wordlength) { 175 /* Program DPN_BlockCtrl1 register */ 176 ret = sdw_write_no_pm(s_rt->slave, addr2, (p_params->bps - 1)); 177 if (ret < 0) { 178 dev_err(&s_rt->slave->dev, 179 "DPN_BlockCtrl1 register write failed for port %d\n", 180 t_params->port_num); 181 return ret; 182 } 183 } 184 185 /* Program DPN_SampleCtrl1 register */ 186 wbuf = (t_params->sample_interval - 1) & SDW_DPN_SAMPLECTRL_LOW; 187 ret = sdw_write_no_pm(s_rt->slave, addr3, wbuf); 188 if (ret < 0) { 189 dev_err(&s_rt->slave->dev, 190 "DPN_SampleCtrl1 register write failed for port %d\n", 191 t_params->port_num); 192 return ret; 193 } 194 195 /* Program DPN_OffsetCtrl1 registers */ 196 ret = sdw_write_no_pm(s_rt->slave, addr4, t_params->offset1); 197 if (ret < 0) { 198 dev_err(&s_rt->slave->dev, 199 "DPN_OffsetCtrl1 register write failed for port %d\n", 200 t_params->port_num); 201 return ret; 202 } 203 204 /* Program DPN_BlockCtrl2 register*/ 205 if (t_params->blk_grp_ctrl_valid) { 206 ret = sdw_write_no_pm(s_rt->slave, addr5, t_params->blk_grp_ctrl); 207 if (ret < 0) { 208 dev_err(&s_rt->slave->dev, 209 "DPN_BlockCtrl2 reg write failed for port %d\n", 210 t_params->port_num); 211 return ret; 212 } 213 } 214 215 /* program DPN_LaneCtrl register */ 216 if (slave_prop->lane_control_support) { 217 ret = sdw_write_no_pm(s_rt->slave, addr6, t_params->lane_ctrl); 218 if (ret < 0) { 219 dev_err(&s_rt->slave->dev, 220 "DPN_LaneCtrl register write failed for port %d\n", 221 t_params->port_num); 222 return ret; 223 } 224 } 225 226 if (dpn_prop->type != SDW_DPN_SIMPLE) { 227 ret = _sdw_program_slave_port_params(bus, s_rt->slave, 228 t_params, dpn_prop->type); 229 if (ret < 0) 230 dev_err(&s_rt->slave->dev, 231 "Transport reg write failed for port: %d\n", 232 t_params->port_num); 233 } 234 235 return ret; 236 } 237 238 static int sdw_program_master_port_params(struct sdw_bus *bus, 239 struct sdw_port_runtime *p_rt) 240 { 241 int ret; 242 243 /* 244 * we need to set transport and port parameters for the port. 245 * Transport parameters refers to the sample interval, offsets and 246 * hstart/stop etc of the data. Port parameters refers to word 247 * length, flow mode etc of the port 248 */ 249 ret = bus->port_ops->dpn_set_port_transport_params(bus, 250 &p_rt->transport_params, 251 bus->params.next_bank); 252 if (ret < 0) 253 return ret; 254 255 return bus->port_ops->dpn_set_port_params(bus, 256 &p_rt->port_params, 257 bus->params.next_bank); 258 } 259 260 /** 261 * sdw_program_port_params() - Programs transport parameters of Master(s) 262 * and Slave(s) 263 * 264 * @m_rt: Master stream runtime 265 */ 266 static int sdw_program_port_params(struct sdw_master_runtime *m_rt) 267 { 268 struct sdw_slave_runtime *s_rt; 269 struct sdw_bus *bus = m_rt->bus; 270 struct sdw_port_runtime *p_rt; 271 int ret = 0; 272 273 /* Program transport & port parameters for Slave(s) */ 274 list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) { 275 list_for_each_entry(p_rt, &s_rt->port_list, port_node) { 276 ret = sdw_program_slave_port_params(bus, s_rt, p_rt); 277 if (ret < 0) 278 return ret; 279 } 280 } 281 282 /* Program transport & port parameters for Master(s) */ 283 list_for_each_entry(p_rt, &m_rt->port_list, port_node) { 284 ret = sdw_program_master_port_params(bus, p_rt); 285 if (ret < 0) 286 return ret; 287 } 288 289 return 0; 290 } 291 292 /** 293 * sdw_enable_disable_slave_ports: Enable/disable slave data port 294 * 295 * @bus: bus instance 296 * @s_rt: slave runtime 297 * @p_rt: port runtime 298 * @en: enable or disable operation 299 * 300 * This function only sets the enable/disable bits in the relevant bank, the 301 * actual enable/disable is done with a bank switch 302 */ 303 static int sdw_enable_disable_slave_ports(struct sdw_bus *bus, 304 struct sdw_slave_runtime *s_rt, 305 struct sdw_port_runtime *p_rt, 306 bool en) 307 { 308 struct sdw_transport_params *t_params = &p_rt->transport_params; 309 u32 addr; 310 int ret; 311 312 if (bus->params.next_bank) 313 addr = SDW_DPN_CHANNELEN_B1(p_rt->num); 314 else 315 addr = SDW_DPN_CHANNELEN_B0(p_rt->num); 316 317 /* 318 * Since bus doesn't support sharing a port across two streams, 319 * it is safe to reset this register 320 */ 321 if (en) 322 ret = sdw_write_no_pm(s_rt->slave, addr, p_rt->ch_mask); 323 else 324 ret = sdw_write_no_pm(s_rt->slave, addr, 0x0); 325 326 if (ret < 0) 327 dev_err(&s_rt->slave->dev, 328 "Slave chn_en reg write failed:%d port:%d\n", 329 ret, t_params->port_num); 330 331 return ret; 332 } 333 334 static int sdw_enable_disable_master_ports(struct sdw_master_runtime *m_rt, 335 struct sdw_port_runtime *p_rt, 336 bool en) 337 { 338 struct sdw_transport_params *t_params = &p_rt->transport_params; 339 struct sdw_bus *bus = m_rt->bus; 340 struct sdw_enable_ch enable_ch; 341 int ret; 342 343 enable_ch.port_num = p_rt->num; 344 enable_ch.ch_mask = p_rt->ch_mask; 345 enable_ch.enable = en; 346 347 /* Perform Master port channel(s) enable/disable */ 348 if (bus->port_ops->dpn_port_enable_ch) { 349 ret = bus->port_ops->dpn_port_enable_ch(bus, 350 &enable_ch, 351 bus->params.next_bank); 352 if (ret < 0) { 353 dev_err(bus->dev, 354 "Master chn_en write failed:%d port:%d\n", 355 ret, t_params->port_num); 356 return ret; 357 } 358 } else { 359 dev_err(bus->dev, 360 "dpn_port_enable_ch not supported, %s failed\n", 361 en ? "enable" : "disable"); 362 return -EINVAL; 363 } 364 365 return 0; 366 } 367 368 /** 369 * sdw_enable_disable_ports() - Enable/disable port(s) for Master and 370 * Slave(s) 371 * 372 * @m_rt: Master stream runtime 373 * @en: mode (enable/disable) 374 */ 375 static int sdw_enable_disable_ports(struct sdw_master_runtime *m_rt, bool en) 376 { 377 struct sdw_port_runtime *s_port, *m_port; 378 struct sdw_slave_runtime *s_rt; 379 int ret = 0; 380 381 /* Enable/Disable Slave port(s) */ 382 list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) { 383 list_for_each_entry(s_port, &s_rt->port_list, port_node) { 384 ret = sdw_enable_disable_slave_ports(m_rt->bus, s_rt, 385 s_port, en); 386 if (ret < 0) 387 return ret; 388 } 389 } 390 391 /* Enable/Disable Master port(s) */ 392 list_for_each_entry(m_port, &m_rt->port_list, port_node) { 393 ret = sdw_enable_disable_master_ports(m_rt, m_port, en); 394 if (ret < 0) 395 return ret; 396 } 397 398 return 0; 399 } 400 401 static int sdw_do_port_prep(struct sdw_slave_runtime *s_rt, 402 struct sdw_prepare_ch prep_ch, 403 enum sdw_port_prep_ops cmd) 404 { 405 int ret = 0; 406 struct sdw_slave *slave = s_rt->slave; 407 408 mutex_lock(&slave->sdw_dev_lock); 409 410 if (slave->probed) { 411 struct device *dev = &slave->dev; 412 struct sdw_driver *drv = drv_to_sdw_driver(dev->driver); 413 414 if (drv->ops && drv->ops->port_prep) { 415 ret = drv->ops->port_prep(slave, &prep_ch, cmd); 416 if (ret < 0) 417 dev_err(dev, "Slave Port Prep cmd %d failed: %d\n", 418 cmd, ret); 419 } 420 } 421 422 mutex_unlock(&slave->sdw_dev_lock); 423 424 return ret; 425 } 426 427 static int sdw_prep_deprep_slave_ports(struct sdw_bus *bus, 428 struct sdw_slave_runtime *s_rt, 429 struct sdw_port_runtime *p_rt, 430 bool prep) 431 { 432 struct completion *port_ready; 433 struct sdw_dpn_prop *dpn_prop; 434 struct sdw_prepare_ch prep_ch; 435 bool intr = false; 436 int ret = 0, val; 437 u32 addr; 438 439 prep_ch.num = p_rt->num; 440 prep_ch.ch_mask = p_rt->ch_mask; 441 442 dpn_prop = sdw_get_slave_dpn_prop(s_rt->slave, 443 s_rt->direction, 444 prep_ch.num); 445 if (!dpn_prop) { 446 dev_err(bus->dev, 447 "Slave Port:%d properties not found\n", prep_ch.num); 448 return -EINVAL; 449 } 450 451 prep_ch.prepare = prep; 452 453 prep_ch.bank = bus->params.next_bank; 454 455 if (dpn_prop->imp_def_interrupts || !dpn_prop->simple_ch_prep_sm || 456 bus->params.s_data_mode != SDW_PORT_DATA_MODE_NORMAL) 457 intr = true; 458 459 /* 460 * Enable interrupt before Port prepare. 461 * For Port de-prepare, it is assumed that port 462 * was prepared earlier 463 */ 464 if (prep && intr) { 465 ret = sdw_configure_dpn_intr(s_rt->slave, p_rt->num, prep, 466 dpn_prop->imp_def_interrupts); 467 if (ret < 0) 468 return ret; 469 } 470 471 /* Inform slave about the impending port prepare */ 472 sdw_do_port_prep(s_rt, prep_ch, prep ? SDW_OPS_PORT_PRE_PREP : SDW_OPS_PORT_PRE_DEPREP); 473 474 /* Prepare Slave port implementing CP_SM */ 475 if (!dpn_prop->simple_ch_prep_sm) { 476 addr = SDW_DPN_PREPARECTRL(p_rt->num); 477 478 if (prep) 479 ret = sdw_write_no_pm(s_rt->slave, addr, p_rt->ch_mask); 480 else 481 ret = sdw_write_no_pm(s_rt->slave, addr, 0x0); 482 483 if (ret < 0) { 484 dev_err(&s_rt->slave->dev, 485 "Slave prep_ctrl reg write failed\n"); 486 return ret; 487 } 488 489 /* Wait for completion on port ready */ 490 port_ready = &s_rt->slave->port_ready[prep_ch.num]; 491 wait_for_completion_timeout(port_ready, 492 msecs_to_jiffies(dpn_prop->ch_prep_timeout)); 493 494 val = sdw_read_no_pm(s_rt->slave, SDW_DPN_PREPARESTATUS(p_rt->num)); 495 if ((val < 0) || (val & p_rt->ch_mask)) { 496 ret = (val < 0) ? val : -ETIMEDOUT; 497 dev_err(&s_rt->slave->dev, 498 "Chn prep failed for port %d: %d\n", prep_ch.num, ret); 499 return ret; 500 } 501 } 502 503 /* Inform slaves about ports prepared */ 504 sdw_do_port_prep(s_rt, prep_ch, prep ? SDW_OPS_PORT_POST_PREP : SDW_OPS_PORT_POST_DEPREP); 505 506 /* Disable interrupt after Port de-prepare */ 507 if (!prep && intr) 508 ret = sdw_configure_dpn_intr(s_rt->slave, p_rt->num, prep, 509 dpn_prop->imp_def_interrupts); 510 511 return ret; 512 } 513 514 static int sdw_prep_deprep_master_ports(struct sdw_master_runtime *m_rt, 515 struct sdw_port_runtime *p_rt, 516 bool prep) 517 { 518 struct sdw_transport_params *t_params = &p_rt->transport_params; 519 struct sdw_bus *bus = m_rt->bus; 520 const struct sdw_master_port_ops *ops = bus->port_ops; 521 struct sdw_prepare_ch prep_ch; 522 int ret = 0; 523 524 prep_ch.num = p_rt->num; 525 prep_ch.ch_mask = p_rt->ch_mask; 526 prep_ch.prepare = prep; /* Prepare/De-prepare */ 527 prep_ch.bank = bus->params.next_bank; 528 529 /* Pre-prepare/Pre-deprepare port(s) */ 530 if (ops->dpn_port_prep) { 531 ret = ops->dpn_port_prep(bus, &prep_ch); 532 if (ret < 0) { 533 dev_err(bus->dev, "Port prepare failed for port:%d\n", 534 t_params->port_num); 535 return ret; 536 } 537 } 538 539 return ret; 540 } 541 542 /** 543 * sdw_prep_deprep_ports() - Prepare/De-prepare port(s) for Master(s) and 544 * Slave(s) 545 * 546 * @m_rt: Master runtime handle 547 * @prep: Prepare or De-prepare 548 */ 549 static int sdw_prep_deprep_ports(struct sdw_master_runtime *m_rt, bool prep) 550 { 551 struct sdw_slave_runtime *s_rt; 552 struct sdw_port_runtime *p_rt; 553 int ret = 0; 554 555 /* Prepare/De-prepare Slave port(s) */ 556 list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) { 557 list_for_each_entry(p_rt, &s_rt->port_list, port_node) { 558 ret = sdw_prep_deprep_slave_ports(m_rt->bus, s_rt, 559 p_rt, prep); 560 if (ret < 0) 561 return ret; 562 } 563 } 564 565 /* Prepare/De-prepare Master port(s) */ 566 list_for_each_entry(p_rt, &m_rt->port_list, port_node) { 567 ret = sdw_prep_deprep_master_ports(m_rt, p_rt, prep); 568 if (ret < 0) 569 return ret; 570 } 571 572 return ret; 573 } 574 575 /** 576 * sdw_notify_config() - Notify bus configuration 577 * 578 * @m_rt: Master runtime handle 579 * 580 * This function notifies the Master(s) and Slave(s) of the 581 * new bus configuration. 582 */ 583 static int sdw_notify_config(struct sdw_master_runtime *m_rt) 584 { 585 struct sdw_slave_runtime *s_rt; 586 struct sdw_bus *bus = m_rt->bus; 587 struct sdw_slave *slave; 588 int ret; 589 590 if (bus->ops->set_bus_conf) { 591 ret = bus->ops->set_bus_conf(bus, &bus->params); 592 if (ret < 0) 593 return ret; 594 } 595 596 list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) { 597 slave = s_rt->slave; 598 599 mutex_lock(&slave->sdw_dev_lock); 600 601 if (slave->probed) { 602 struct device *dev = &slave->dev; 603 struct sdw_driver *drv = drv_to_sdw_driver(dev->driver); 604 605 if (drv->ops && drv->ops->bus_config) { 606 ret = drv->ops->bus_config(slave, &bus->params); 607 if (ret < 0) { 608 dev_err(dev, "Notify Slave: %d failed\n", 609 slave->dev_num); 610 mutex_unlock(&slave->sdw_dev_lock); 611 return ret; 612 } 613 } 614 } 615 616 mutex_unlock(&slave->sdw_dev_lock); 617 } 618 619 return 0; 620 } 621 622 /** 623 * sdw_program_params() - Program transport and port parameters for Master(s) 624 * and Slave(s) 625 * 626 * @bus: SDW bus instance 627 * @prepare: true if sdw_program_params() is called by _prepare. 628 */ 629 static int sdw_program_params(struct sdw_bus *bus, bool prepare) 630 { 631 struct sdw_master_runtime *m_rt; 632 int ret = 0; 633 634 list_for_each_entry(m_rt, &bus->m_rt_list, bus_node) { 635 636 /* 637 * this loop walks through all master runtimes for a 638 * bus, but the ports can only be configured while 639 * explicitly preparing a stream or handling an 640 * already-prepared stream otherwise. 641 */ 642 if (!prepare && 643 m_rt->stream->state == SDW_STREAM_CONFIGURED) 644 continue; 645 646 ret = sdw_program_port_params(m_rt); 647 if (ret < 0) { 648 dev_err(bus->dev, 649 "Program transport params failed: %d\n", ret); 650 return ret; 651 } 652 653 ret = sdw_notify_config(m_rt); 654 if (ret < 0) { 655 dev_err(bus->dev, 656 "Notify bus config failed: %d\n", ret); 657 return ret; 658 } 659 660 /* Enable port(s) on alternate bank for all active streams */ 661 if (m_rt->stream->state != SDW_STREAM_ENABLED) 662 continue; 663 664 ret = sdw_enable_disable_ports(m_rt, true); 665 if (ret < 0) { 666 dev_err(bus->dev, "Enable channel failed: %d\n", ret); 667 return ret; 668 } 669 } 670 671 return ret; 672 } 673 674 static int sdw_bank_switch(struct sdw_bus *bus, int m_rt_count) 675 { 676 int col_index, row_index; 677 bool multi_link; 678 struct sdw_msg *wr_msg; 679 u8 *wbuf; 680 int ret; 681 u16 addr; 682 683 wr_msg = kzalloc(sizeof(*wr_msg), GFP_KERNEL); 684 if (!wr_msg) 685 return -ENOMEM; 686 687 wbuf = kzalloc(sizeof(*wbuf), GFP_KERNEL); 688 if (!wbuf) { 689 ret = -ENOMEM; 690 goto error_1; 691 } 692 693 /* Get row and column index to program register */ 694 col_index = sdw_find_col_index(bus->params.col); 695 row_index = sdw_find_row_index(bus->params.row); 696 wbuf[0] = col_index | (row_index << 3); 697 698 if (bus->params.next_bank) 699 addr = SDW_SCP_FRAMECTRL_B1; 700 else 701 addr = SDW_SCP_FRAMECTRL_B0; 702 703 sdw_fill_msg(wr_msg, NULL, addr, 1, SDW_BROADCAST_DEV_NUM, 704 SDW_MSG_FLAG_WRITE, wbuf); 705 wr_msg->ssp_sync = true; 706 707 /* 708 * Set the multi_link flag only when both the hardware supports 709 * and hardware-based sync is required 710 */ 711 multi_link = bus->multi_link && (m_rt_count >= bus->hw_sync_min_links); 712 713 if (multi_link) 714 ret = sdw_transfer_defer(bus, wr_msg); 715 else 716 ret = sdw_transfer(bus, wr_msg); 717 718 if (ret < 0 && ret != -ENODATA) { 719 dev_err(bus->dev, "Slave frame_ctrl reg write failed\n"); 720 goto error; 721 } 722 723 if (!multi_link) { 724 kfree(wbuf); 725 kfree(wr_msg); 726 bus->defer_msg.msg = NULL; 727 bus->params.curr_bank = !bus->params.curr_bank; 728 bus->params.next_bank = !bus->params.next_bank; 729 } 730 731 return 0; 732 733 error: 734 kfree(wbuf); 735 error_1: 736 kfree(wr_msg); 737 bus->defer_msg.msg = NULL; 738 return ret; 739 } 740 741 /** 742 * sdw_ml_sync_bank_switch: Multilink register bank switch 743 * 744 * @bus: SDW bus instance 745 * @multi_link: whether this is a multi-link stream with hardware-based sync 746 * 747 * Caller function should free the buffers on error 748 */ 749 static int sdw_ml_sync_bank_switch(struct sdw_bus *bus, bool multi_link) 750 { 751 unsigned long time_left; 752 753 if (!multi_link) 754 return 0; 755 756 /* Wait for completion of transfer */ 757 time_left = wait_for_completion_timeout(&bus->defer_msg.complete, 758 bus->bank_switch_timeout); 759 760 if (!time_left) { 761 dev_err(bus->dev, "Controller Timed out on bank switch\n"); 762 return -ETIMEDOUT; 763 } 764 765 bus->params.curr_bank = !bus->params.curr_bank; 766 bus->params.next_bank = !bus->params.next_bank; 767 768 if (bus->defer_msg.msg) { 769 kfree(bus->defer_msg.msg->buf); 770 kfree(bus->defer_msg.msg); 771 bus->defer_msg.msg = NULL; 772 } 773 774 return 0; 775 } 776 777 static int do_bank_switch(struct sdw_stream_runtime *stream) 778 { 779 struct sdw_master_runtime *m_rt; 780 const struct sdw_master_ops *ops; 781 struct sdw_bus *bus; 782 bool multi_link = false; 783 int m_rt_count; 784 int ret = 0; 785 786 m_rt_count = stream->m_rt_count; 787 788 list_for_each_entry(m_rt, &stream->master_list, stream_node) { 789 bus = m_rt->bus; 790 ops = bus->ops; 791 792 if (bus->multi_link && m_rt_count >= bus->hw_sync_min_links) { 793 multi_link = true; 794 mutex_lock(&bus->msg_lock); 795 } 796 797 /* Pre-bank switch */ 798 if (ops->pre_bank_switch) { 799 ret = ops->pre_bank_switch(bus); 800 if (ret < 0) { 801 dev_err(bus->dev, 802 "Pre bank switch op failed: %d\n", ret); 803 goto msg_unlock; 804 } 805 } 806 807 /* 808 * Perform Bank switch operation. 809 * For multi link cases, the actual bank switch is 810 * synchronized across all Masters and happens later as a 811 * part of post_bank_switch ops. 812 */ 813 ret = sdw_bank_switch(bus, m_rt_count); 814 if (ret < 0) { 815 dev_err(bus->dev, "Bank switch failed: %d\n", ret); 816 goto error; 817 } 818 } 819 820 /* 821 * For multi link cases, it is expected that the bank switch is 822 * triggered by the post_bank_switch for the first Master in the list 823 * and for the other Masters the post_bank_switch() should return doing 824 * nothing. 825 */ 826 list_for_each_entry(m_rt, &stream->master_list, stream_node) { 827 bus = m_rt->bus; 828 ops = bus->ops; 829 830 /* Post-bank switch */ 831 if (ops->post_bank_switch) { 832 ret = ops->post_bank_switch(bus); 833 if (ret < 0) { 834 dev_err(bus->dev, 835 "Post bank switch op failed: %d\n", 836 ret); 837 goto error; 838 } 839 } else if (multi_link) { 840 dev_err(bus->dev, 841 "Post bank switch ops not implemented\n"); 842 ret = -EINVAL; 843 goto error; 844 } 845 846 /* Set the bank switch timeout to default, if not set */ 847 if (!bus->bank_switch_timeout) 848 bus->bank_switch_timeout = DEFAULT_BANK_SWITCH_TIMEOUT; 849 850 /* Check if bank switch was successful */ 851 ret = sdw_ml_sync_bank_switch(bus, multi_link); 852 if (ret < 0) { 853 dev_err(bus->dev, 854 "multi link bank switch failed: %d\n", ret); 855 goto error; 856 } 857 858 if (multi_link) 859 mutex_unlock(&bus->msg_lock); 860 } 861 862 return ret; 863 864 error: 865 list_for_each_entry(m_rt, &stream->master_list, stream_node) { 866 bus = m_rt->bus; 867 if (bus->defer_msg.msg) { 868 kfree(bus->defer_msg.msg->buf); 869 kfree(bus->defer_msg.msg); 870 bus->defer_msg.msg = NULL; 871 } 872 } 873 874 msg_unlock: 875 876 if (multi_link) { 877 list_for_each_entry(m_rt, &stream->master_list, stream_node) { 878 bus = m_rt->bus; 879 if (mutex_is_locked(&bus->msg_lock)) 880 mutex_unlock(&bus->msg_lock); 881 } 882 } 883 884 return ret; 885 } 886 887 static struct sdw_port_runtime *sdw_port_alloc(struct list_head *port_list) 888 { 889 struct sdw_port_runtime *p_rt; 890 891 p_rt = kzalloc(sizeof(*p_rt), GFP_KERNEL); 892 if (!p_rt) 893 return NULL; 894 895 list_add_tail(&p_rt->port_node, port_list); 896 897 return p_rt; 898 } 899 900 static int sdw_port_config(struct sdw_port_runtime *p_rt, 901 const struct sdw_port_config *port_config, 902 int port_index) 903 { 904 p_rt->ch_mask = port_config[port_index].ch_mask; 905 p_rt->num = port_config[port_index].num; 906 907 /* 908 * TODO: Check port capabilities for requested configuration 909 */ 910 911 return 0; 912 } 913 914 static void sdw_port_free(struct sdw_port_runtime *p_rt) 915 { 916 list_del(&p_rt->port_node); 917 kfree(p_rt); 918 } 919 920 static bool sdw_slave_port_allocated(struct sdw_slave_runtime *s_rt) 921 { 922 return !list_empty(&s_rt->port_list); 923 } 924 925 static void sdw_slave_port_free(struct sdw_slave *slave, 926 struct sdw_stream_runtime *stream) 927 { 928 struct sdw_port_runtime *p_rt, *_p_rt; 929 struct sdw_master_runtime *m_rt; 930 struct sdw_slave_runtime *s_rt; 931 932 list_for_each_entry(m_rt, &stream->master_list, stream_node) { 933 list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) { 934 if (s_rt->slave != slave) 935 continue; 936 937 list_for_each_entry_safe(p_rt, _p_rt, 938 &s_rt->port_list, port_node) { 939 sdw_port_free(p_rt); 940 } 941 } 942 } 943 } 944 945 static int sdw_slave_port_alloc(struct sdw_slave *slave, 946 struct sdw_slave_runtime *s_rt, 947 unsigned int num_config) 948 { 949 struct sdw_port_runtime *p_rt; 950 int i; 951 952 /* Iterate for number of ports to perform initialization */ 953 for (i = 0; i < num_config; i++) { 954 p_rt = sdw_port_alloc(&s_rt->port_list); 955 if (!p_rt) 956 return -ENOMEM; 957 } 958 959 return 0; 960 } 961 962 static int sdw_slave_port_is_valid_range(struct device *dev, int num) 963 { 964 if (!SDW_VALID_PORT_RANGE(num)) { 965 dev_err(dev, "SoundWire: Invalid port number :%d\n", num); 966 return -EINVAL; 967 } 968 969 return 0; 970 } 971 972 static int sdw_slave_port_config(struct sdw_slave *slave, 973 struct sdw_slave_runtime *s_rt, 974 const struct sdw_port_config *port_config) 975 { 976 struct sdw_port_runtime *p_rt; 977 int ret; 978 int i; 979 980 i = 0; 981 list_for_each_entry(p_rt, &s_rt->port_list, port_node) { 982 /* 983 * TODO: Check valid port range as defined by DisCo/ 984 * slave 985 */ 986 ret = sdw_slave_port_is_valid_range(&slave->dev, port_config[i].num); 987 if (ret < 0) 988 return ret; 989 990 ret = sdw_port_config(p_rt, port_config, i); 991 if (ret < 0) 992 return ret; 993 i++; 994 } 995 996 return 0; 997 } 998 999 static bool sdw_master_port_allocated(struct sdw_master_runtime *m_rt) 1000 { 1001 return !list_empty(&m_rt->port_list); 1002 } 1003 1004 static void sdw_master_port_free(struct sdw_master_runtime *m_rt) 1005 { 1006 struct sdw_port_runtime *p_rt, *_p_rt; 1007 1008 list_for_each_entry_safe(p_rt, _p_rt, &m_rt->port_list, port_node) { 1009 sdw_port_free(p_rt); 1010 } 1011 } 1012 1013 static int sdw_master_port_alloc(struct sdw_master_runtime *m_rt, 1014 unsigned int num_ports) 1015 { 1016 struct sdw_port_runtime *p_rt; 1017 int i; 1018 1019 /* Iterate for number of ports to perform initialization */ 1020 for (i = 0; i < num_ports; i++) { 1021 p_rt = sdw_port_alloc(&m_rt->port_list); 1022 if (!p_rt) 1023 return -ENOMEM; 1024 } 1025 1026 return 0; 1027 } 1028 1029 static int sdw_master_port_config(struct sdw_master_runtime *m_rt, 1030 const struct sdw_port_config *port_config) 1031 { 1032 struct sdw_port_runtime *p_rt; 1033 int ret; 1034 int i; 1035 1036 i = 0; 1037 list_for_each_entry(p_rt, &m_rt->port_list, port_node) { 1038 ret = sdw_port_config(p_rt, port_config, i); 1039 if (ret < 0) 1040 return ret; 1041 i++; 1042 } 1043 1044 return 0; 1045 } 1046 1047 /** 1048 * sdw_slave_rt_alloc() - Allocate a Slave runtime handle. 1049 * 1050 * @slave: Slave handle 1051 * @m_rt: Master runtime handle 1052 * 1053 * This function is to be called with bus_lock held. 1054 */ 1055 static struct sdw_slave_runtime 1056 *sdw_slave_rt_alloc(struct sdw_slave *slave, 1057 struct sdw_master_runtime *m_rt) 1058 { 1059 struct sdw_slave_runtime *s_rt; 1060 1061 s_rt = kzalloc(sizeof(*s_rt), GFP_KERNEL); 1062 if (!s_rt) 1063 return NULL; 1064 1065 INIT_LIST_HEAD(&s_rt->port_list); 1066 s_rt->slave = slave; 1067 1068 list_add_tail(&s_rt->m_rt_node, &m_rt->slave_rt_list); 1069 1070 return s_rt; 1071 } 1072 1073 /** 1074 * sdw_slave_rt_config() - Configure a Slave runtime handle. 1075 * 1076 * @s_rt: Slave runtime handle 1077 * @stream_config: Stream configuration 1078 * 1079 * This function is to be called with bus_lock held. 1080 */ 1081 static int sdw_slave_rt_config(struct sdw_slave_runtime *s_rt, 1082 struct sdw_stream_config *stream_config) 1083 { 1084 s_rt->ch_count = stream_config->ch_count; 1085 s_rt->direction = stream_config->direction; 1086 1087 return 0; 1088 } 1089 1090 static struct sdw_slave_runtime *sdw_slave_rt_find(struct sdw_slave *slave, 1091 struct sdw_stream_runtime *stream) 1092 { 1093 struct sdw_slave_runtime *s_rt, *_s_rt; 1094 struct sdw_master_runtime *m_rt; 1095 1096 list_for_each_entry(m_rt, &stream->master_list, stream_node) { 1097 /* Retrieve Slave runtime handle */ 1098 list_for_each_entry_safe(s_rt, _s_rt, 1099 &m_rt->slave_rt_list, m_rt_node) { 1100 if (s_rt->slave == slave) 1101 return s_rt; 1102 } 1103 } 1104 return NULL; 1105 } 1106 1107 /** 1108 * sdw_slave_rt_free() - Free Slave(s) runtime handle 1109 * 1110 * @slave: Slave handle. 1111 * @stream: Stream runtime handle. 1112 * 1113 * This function is to be called with bus_lock held. 1114 */ 1115 static void sdw_slave_rt_free(struct sdw_slave *slave, 1116 struct sdw_stream_runtime *stream) 1117 { 1118 struct sdw_slave_runtime *s_rt; 1119 1120 s_rt = sdw_slave_rt_find(slave, stream); 1121 if (s_rt) { 1122 list_del(&s_rt->m_rt_node); 1123 kfree(s_rt); 1124 } 1125 } 1126 1127 static struct sdw_master_runtime 1128 *sdw_master_rt_find(struct sdw_bus *bus, 1129 struct sdw_stream_runtime *stream) 1130 { 1131 struct sdw_master_runtime *m_rt; 1132 1133 /* Retrieve Bus handle if already available */ 1134 list_for_each_entry(m_rt, &stream->master_list, stream_node) { 1135 if (m_rt->bus == bus) 1136 return m_rt; 1137 } 1138 1139 return NULL; 1140 } 1141 1142 /** 1143 * sdw_master_rt_alloc() - Allocates a Master runtime handle 1144 * 1145 * @bus: SDW bus instance 1146 * @stream: Stream runtime handle. 1147 * 1148 * This function is to be called with bus_lock held. 1149 */ 1150 static struct sdw_master_runtime 1151 *sdw_master_rt_alloc(struct sdw_bus *bus, 1152 struct sdw_stream_runtime *stream) 1153 { 1154 struct sdw_master_runtime *m_rt, *walk_m_rt; 1155 struct list_head *insert_after; 1156 1157 m_rt = kzalloc(sizeof(*m_rt), GFP_KERNEL); 1158 if (!m_rt) 1159 return NULL; 1160 1161 /* Initialization of Master runtime handle */ 1162 INIT_LIST_HEAD(&m_rt->port_list); 1163 INIT_LIST_HEAD(&m_rt->slave_rt_list); 1164 1165 /* 1166 * Add in order of bus id so that when taking the bus_lock 1167 * of multiple buses they will always be taken in the same 1168 * order to prevent a mutex deadlock. 1169 */ 1170 insert_after = &stream->master_list; 1171 list_for_each_entry_reverse(walk_m_rt, &stream->master_list, stream_node) { 1172 if (walk_m_rt->bus->id < bus->id) { 1173 insert_after = &walk_m_rt->stream_node; 1174 break; 1175 } 1176 } 1177 list_add(&m_rt->stream_node, insert_after); 1178 1179 list_add_tail(&m_rt->bus_node, &bus->m_rt_list); 1180 1181 m_rt->bus = bus; 1182 m_rt->stream = stream; 1183 1184 return m_rt; 1185 } 1186 1187 /** 1188 * sdw_master_rt_config() - Configure Master runtime handle 1189 * 1190 * @m_rt: Master runtime handle 1191 * @stream_config: Stream configuration 1192 * 1193 * This function is to be called with bus_lock held. 1194 */ 1195 1196 static int sdw_master_rt_config(struct sdw_master_runtime *m_rt, 1197 struct sdw_stream_config *stream_config) 1198 { 1199 m_rt->ch_count = stream_config->ch_count; 1200 m_rt->direction = stream_config->direction; 1201 1202 return 0; 1203 } 1204 1205 /** 1206 * sdw_master_rt_free() - Free Master runtime handle 1207 * 1208 * @m_rt: Master runtime node 1209 * @stream: Stream runtime handle. 1210 * 1211 * This function is to be called with bus_lock held 1212 * It frees the Master runtime handle and associated Slave(s) runtime 1213 * handle. If this is called first then sdw_slave_rt_free() will have 1214 * no effect as Slave(s) runtime handle would already be freed up. 1215 */ 1216 static void sdw_master_rt_free(struct sdw_master_runtime *m_rt, 1217 struct sdw_stream_runtime *stream) 1218 { 1219 struct sdw_slave_runtime *s_rt, *_s_rt; 1220 1221 list_for_each_entry_safe(s_rt, _s_rt, &m_rt->slave_rt_list, m_rt_node) { 1222 sdw_slave_port_free(s_rt->slave, stream); 1223 sdw_slave_rt_free(s_rt->slave, stream); 1224 } 1225 1226 list_del(&m_rt->stream_node); 1227 list_del(&m_rt->bus_node); 1228 kfree(m_rt); 1229 } 1230 1231 /** 1232 * sdw_config_stream() - Configure the allocated stream 1233 * 1234 * @dev: SDW device 1235 * @stream: SoundWire stream 1236 * @stream_config: Stream configuration for audio stream 1237 * @is_slave: is API called from Slave or Master 1238 * 1239 * This function is to be called with bus_lock held. 1240 */ 1241 static int sdw_config_stream(struct device *dev, 1242 struct sdw_stream_runtime *stream, 1243 struct sdw_stream_config *stream_config, 1244 bool is_slave) 1245 { 1246 /* 1247 * Update the stream rate, channel and bps based on data 1248 * source. For more than one data source (multilink), 1249 * match the rate, bps, stream type and increment number of channels. 1250 * 1251 * If rate/bps is zero, it means the values are not set, so skip 1252 * comparison and allow the value to be set and stored in stream 1253 */ 1254 if (stream->params.rate && 1255 stream->params.rate != stream_config->frame_rate) { 1256 dev_err(dev, "rate not matching, stream:%s\n", stream->name); 1257 return -EINVAL; 1258 } 1259 1260 if (stream->params.bps && 1261 stream->params.bps != stream_config->bps) { 1262 dev_err(dev, "bps not matching, stream:%s\n", stream->name); 1263 return -EINVAL; 1264 } 1265 1266 stream->type = stream_config->type; 1267 stream->params.rate = stream_config->frame_rate; 1268 stream->params.bps = stream_config->bps; 1269 1270 /* TODO: Update this check during Device-device support */ 1271 if (is_slave) 1272 stream->params.ch_count += stream_config->ch_count; 1273 1274 return 0; 1275 } 1276 1277 /** 1278 * sdw_get_slave_dpn_prop() - Get Slave port capabilities 1279 * 1280 * @slave: Slave handle 1281 * @direction: Data direction. 1282 * @port_num: Port number 1283 */ 1284 struct sdw_dpn_prop *sdw_get_slave_dpn_prop(struct sdw_slave *slave, 1285 enum sdw_data_direction direction, 1286 unsigned int port_num) 1287 { 1288 struct sdw_dpn_prop *dpn_prop; 1289 u8 num_ports; 1290 int i; 1291 1292 if (direction == SDW_DATA_DIR_TX) { 1293 num_ports = hweight32(slave->prop.source_ports); 1294 dpn_prop = slave->prop.src_dpn_prop; 1295 } else { 1296 num_ports = hweight32(slave->prop.sink_ports); 1297 dpn_prop = slave->prop.sink_dpn_prop; 1298 } 1299 1300 for (i = 0; i < num_ports; i++) { 1301 if (dpn_prop[i].num == port_num) 1302 return &dpn_prop[i]; 1303 } 1304 1305 return NULL; 1306 } 1307 1308 /** 1309 * sdw_acquire_bus_lock: Acquire bus lock for all Master runtime(s) 1310 * 1311 * @stream: SoundWire stream 1312 * 1313 * Acquire bus_lock for each of the master runtime(m_rt) part of this 1314 * stream to reconfigure the bus. 1315 * NOTE: This function is called from SoundWire stream ops and is 1316 * expected that a global lock is held before acquiring bus_lock. 1317 */ 1318 static void sdw_acquire_bus_lock(struct sdw_stream_runtime *stream) 1319 { 1320 struct sdw_master_runtime *m_rt; 1321 struct sdw_bus *bus; 1322 1323 /* Iterate for all Master(s) in Master list */ 1324 list_for_each_entry(m_rt, &stream->master_list, stream_node) { 1325 bus = m_rt->bus; 1326 1327 mutex_lock(&bus->bus_lock); 1328 } 1329 } 1330 1331 /** 1332 * sdw_release_bus_lock: Release bus lock for all Master runtime(s) 1333 * 1334 * @stream: SoundWire stream 1335 * 1336 * Release the previously held bus_lock after reconfiguring the bus. 1337 * NOTE: This function is called from SoundWire stream ops and is 1338 * expected that a global lock is held before releasing bus_lock. 1339 */ 1340 static void sdw_release_bus_lock(struct sdw_stream_runtime *stream) 1341 { 1342 struct sdw_master_runtime *m_rt; 1343 struct sdw_bus *bus; 1344 1345 /* Iterate for all Master(s) in Master list */ 1346 list_for_each_entry_reverse(m_rt, &stream->master_list, stream_node) { 1347 bus = m_rt->bus; 1348 mutex_unlock(&bus->bus_lock); 1349 } 1350 } 1351 1352 static int _sdw_prepare_stream(struct sdw_stream_runtime *stream, 1353 bool update_params) 1354 { 1355 struct sdw_master_runtime *m_rt; 1356 struct sdw_bus *bus; 1357 struct sdw_master_prop *prop; 1358 struct sdw_bus_params params; 1359 int ret; 1360 1361 /* Prepare Master(s) and Slave(s) port(s) associated with stream */ 1362 list_for_each_entry(m_rt, &stream->master_list, stream_node) { 1363 bus = m_rt->bus; 1364 prop = &bus->prop; 1365 memcpy(¶ms, &bus->params, sizeof(params)); 1366 1367 /* TODO: Support Asynchronous mode */ 1368 if ((prop->max_clk_freq % stream->params.rate) != 0) { 1369 dev_err(bus->dev, "Async mode not supported\n"); 1370 return -EINVAL; 1371 } 1372 1373 if (update_params) { 1374 /* Increment cumulative bus bandwidth */ 1375 /* TODO: Update this during Device-Device support */ 1376 bus->params.bandwidth += m_rt->stream->params.rate * 1377 m_rt->ch_count * m_rt->stream->params.bps; 1378 1379 /* Compute params */ 1380 if (bus->compute_params) { 1381 ret = bus->compute_params(bus); 1382 if (ret < 0) { 1383 dev_err(bus->dev, "Compute params failed: %d\n", 1384 ret); 1385 goto restore_params; 1386 } 1387 } 1388 } 1389 1390 /* Program params */ 1391 ret = sdw_program_params(bus, true); 1392 if (ret < 0) { 1393 dev_err(bus->dev, "Program params failed: %d\n", ret); 1394 goto restore_params; 1395 } 1396 } 1397 1398 ret = do_bank_switch(stream); 1399 if (ret < 0) { 1400 pr_err("%s: do_bank_switch failed: %d\n", __func__, ret); 1401 goto restore_params; 1402 } 1403 1404 list_for_each_entry(m_rt, &stream->master_list, stream_node) { 1405 bus = m_rt->bus; 1406 1407 /* Prepare port(s) on the new clock configuration */ 1408 ret = sdw_prep_deprep_ports(m_rt, true); 1409 if (ret < 0) { 1410 dev_err(bus->dev, "Prepare port(s) failed ret = %d\n", 1411 ret); 1412 return ret; 1413 } 1414 } 1415 1416 stream->state = SDW_STREAM_PREPARED; 1417 1418 return ret; 1419 1420 restore_params: 1421 memcpy(&bus->params, ¶ms, sizeof(params)); 1422 return ret; 1423 } 1424 1425 /** 1426 * sdw_prepare_stream() - Prepare SoundWire stream 1427 * 1428 * @stream: Soundwire stream 1429 * 1430 * Documentation/driver-api/soundwire/stream.rst explains this API in detail 1431 */ 1432 int sdw_prepare_stream(struct sdw_stream_runtime *stream) 1433 { 1434 bool update_params = true; 1435 int ret; 1436 1437 if (!stream) { 1438 pr_err("SoundWire: Handle not found for stream\n"); 1439 return -EINVAL; 1440 } 1441 1442 sdw_acquire_bus_lock(stream); 1443 1444 if (stream->state == SDW_STREAM_PREPARED) { 1445 ret = 0; 1446 goto state_err; 1447 } 1448 1449 if (stream->state != SDW_STREAM_CONFIGURED && 1450 stream->state != SDW_STREAM_DEPREPARED && 1451 stream->state != SDW_STREAM_DISABLED) { 1452 pr_err("%s: %s: inconsistent state state %d\n", 1453 __func__, stream->name, stream->state); 1454 ret = -EINVAL; 1455 goto state_err; 1456 } 1457 1458 /* 1459 * when the stream is DISABLED, this means sdw_prepare_stream() 1460 * is called as a result of an underflow or a resume operation. 1461 * In this case, the bus parameters shall not be recomputed, but 1462 * still need to be re-applied 1463 */ 1464 if (stream->state == SDW_STREAM_DISABLED) 1465 update_params = false; 1466 1467 ret = _sdw_prepare_stream(stream, update_params); 1468 1469 state_err: 1470 sdw_release_bus_lock(stream); 1471 return ret; 1472 } 1473 EXPORT_SYMBOL(sdw_prepare_stream); 1474 1475 static int _sdw_enable_stream(struct sdw_stream_runtime *stream) 1476 { 1477 struct sdw_master_runtime *m_rt; 1478 struct sdw_bus *bus; 1479 int ret; 1480 1481 /* Enable Master(s) and Slave(s) port(s) associated with stream */ 1482 list_for_each_entry(m_rt, &stream->master_list, stream_node) { 1483 bus = m_rt->bus; 1484 1485 /* Program params */ 1486 ret = sdw_program_params(bus, false); 1487 if (ret < 0) { 1488 dev_err(bus->dev, "%s: Program params failed: %d\n", __func__, ret); 1489 return ret; 1490 } 1491 1492 /* Enable port(s) */ 1493 ret = sdw_enable_disable_ports(m_rt, true); 1494 if (ret < 0) { 1495 dev_err(bus->dev, 1496 "Enable port(s) failed ret: %d\n", ret); 1497 return ret; 1498 } 1499 } 1500 1501 ret = do_bank_switch(stream); 1502 if (ret < 0) { 1503 pr_err("%s: do_bank_switch failed: %d\n", __func__, ret); 1504 return ret; 1505 } 1506 1507 stream->state = SDW_STREAM_ENABLED; 1508 return 0; 1509 } 1510 1511 /** 1512 * sdw_enable_stream() - Enable SoundWire stream 1513 * 1514 * @stream: Soundwire stream 1515 * 1516 * Documentation/driver-api/soundwire/stream.rst explains this API in detail 1517 */ 1518 int sdw_enable_stream(struct sdw_stream_runtime *stream) 1519 { 1520 int ret; 1521 1522 if (!stream) { 1523 pr_err("SoundWire: Handle not found for stream\n"); 1524 return -EINVAL; 1525 } 1526 1527 sdw_acquire_bus_lock(stream); 1528 1529 if (stream->state == SDW_STREAM_ENABLED) { 1530 ret = 0; 1531 goto state_err; 1532 } 1533 1534 if (stream->state != SDW_STREAM_PREPARED && 1535 stream->state != SDW_STREAM_DISABLED) { 1536 pr_err("%s: %s: inconsistent state state %d\n", 1537 __func__, stream->name, stream->state); 1538 ret = -EINVAL; 1539 goto state_err; 1540 } 1541 1542 ret = _sdw_enable_stream(stream); 1543 1544 state_err: 1545 sdw_release_bus_lock(stream); 1546 return ret; 1547 } 1548 EXPORT_SYMBOL(sdw_enable_stream); 1549 1550 static int _sdw_disable_stream(struct sdw_stream_runtime *stream) 1551 { 1552 struct sdw_master_runtime *m_rt; 1553 int ret; 1554 1555 list_for_each_entry(m_rt, &stream->master_list, stream_node) { 1556 struct sdw_bus *bus = m_rt->bus; 1557 1558 /* Disable port(s) */ 1559 ret = sdw_enable_disable_ports(m_rt, false); 1560 if (ret < 0) { 1561 dev_err(bus->dev, "Disable port(s) failed: %d\n", ret); 1562 return ret; 1563 } 1564 } 1565 stream->state = SDW_STREAM_DISABLED; 1566 1567 list_for_each_entry(m_rt, &stream->master_list, stream_node) { 1568 struct sdw_bus *bus = m_rt->bus; 1569 1570 /* Program params */ 1571 ret = sdw_program_params(bus, false); 1572 if (ret < 0) { 1573 dev_err(bus->dev, "%s: Program params failed: %d\n", __func__, ret); 1574 return ret; 1575 } 1576 } 1577 1578 ret = do_bank_switch(stream); 1579 if (ret < 0) { 1580 pr_err("%s: do_bank_switch failed: %d\n", __func__, ret); 1581 return ret; 1582 } 1583 1584 /* make sure alternate bank (previous current) is also disabled */ 1585 list_for_each_entry(m_rt, &stream->master_list, stream_node) { 1586 struct sdw_bus *bus = m_rt->bus; 1587 1588 /* Disable port(s) */ 1589 ret = sdw_enable_disable_ports(m_rt, false); 1590 if (ret < 0) { 1591 dev_err(bus->dev, "Disable port(s) failed: %d\n", ret); 1592 return ret; 1593 } 1594 } 1595 1596 return 0; 1597 } 1598 1599 /** 1600 * sdw_disable_stream() - Disable SoundWire stream 1601 * 1602 * @stream: Soundwire stream 1603 * 1604 * Documentation/driver-api/soundwire/stream.rst explains this API in detail 1605 */ 1606 int sdw_disable_stream(struct sdw_stream_runtime *stream) 1607 { 1608 int ret; 1609 1610 if (!stream) { 1611 pr_err("SoundWire: Handle not found for stream\n"); 1612 return -EINVAL; 1613 } 1614 1615 sdw_acquire_bus_lock(stream); 1616 1617 if (stream->state == SDW_STREAM_DISABLED) { 1618 ret = 0; 1619 goto state_err; 1620 } 1621 1622 if (stream->state != SDW_STREAM_ENABLED) { 1623 pr_err("%s: %s: inconsistent state state %d\n", 1624 __func__, stream->name, stream->state); 1625 ret = -EINVAL; 1626 goto state_err; 1627 } 1628 1629 ret = _sdw_disable_stream(stream); 1630 1631 state_err: 1632 sdw_release_bus_lock(stream); 1633 return ret; 1634 } 1635 EXPORT_SYMBOL(sdw_disable_stream); 1636 1637 static int _sdw_deprepare_stream(struct sdw_stream_runtime *stream) 1638 { 1639 struct sdw_master_runtime *m_rt; 1640 struct sdw_bus *bus; 1641 int ret = 0; 1642 1643 list_for_each_entry(m_rt, &stream->master_list, stream_node) { 1644 bus = m_rt->bus; 1645 /* De-prepare port(s) */ 1646 ret = sdw_prep_deprep_ports(m_rt, false); 1647 if (ret < 0) { 1648 dev_err(bus->dev, 1649 "De-prepare port(s) failed: %d\n", ret); 1650 return ret; 1651 } 1652 1653 /* TODO: Update this during Device-Device support */ 1654 bus->params.bandwidth -= m_rt->stream->params.rate * 1655 m_rt->ch_count * m_rt->stream->params.bps; 1656 1657 /* Compute params */ 1658 if (bus->compute_params) { 1659 ret = bus->compute_params(bus); 1660 if (ret < 0) { 1661 dev_err(bus->dev, "Compute params failed: %d\n", 1662 ret); 1663 return ret; 1664 } 1665 } 1666 1667 /* Program params */ 1668 ret = sdw_program_params(bus, false); 1669 if (ret < 0) { 1670 dev_err(bus->dev, "%s: Program params failed: %d\n", __func__, ret); 1671 return ret; 1672 } 1673 } 1674 1675 stream->state = SDW_STREAM_DEPREPARED; 1676 return do_bank_switch(stream); 1677 } 1678 1679 /** 1680 * sdw_deprepare_stream() - Deprepare SoundWire stream 1681 * 1682 * @stream: Soundwire stream 1683 * 1684 * Documentation/driver-api/soundwire/stream.rst explains this API in detail 1685 */ 1686 int sdw_deprepare_stream(struct sdw_stream_runtime *stream) 1687 { 1688 int ret; 1689 1690 if (!stream) { 1691 pr_err("SoundWire: Handle not found for stream\n"); 1692 return -EINVAL; 1693 } 1694 1695 sdw_acquire_bus_lock(stream); 1696 1697 if (stream->state == SDW_STREAM_DEPREPARED) { 1698 ret = 0; 1699 goto state_err; 1700 } 1701 1702 if (stream->state != SDW_STREAM_PREPARED && 1703 stream->state != SDW_STREAM_DISABLED) { 1704 pr_err("%s: %s: inconsistent state state %d\n", 1705 __func__, stream->name, stream->state); 1706 ret = -EINVAL; 1707 goto state_err; 1708 } 1709 1710 ret = _sdw_deprepare_stream(stream); 1711 1712 state_err: 1713 sdw_release_bus_lock(stream); 1714 return ret; 1715 } 1716 EXPORT_SYMBOL(sdw_deprepare_stream); 1717 1718 static int set_stream(struct snd_pcm_substream *substream, 1719 struct sdw_stream_runtime *sdw_stream) 1720 { 1721 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 1722 struct snd_soc_dai *dai; 1723 int ret = 0; 1724 int i; 1725 1726 /* Set stream pointer on all DAIs */ 1727 for_each_rtd_dais(rtd, i, dai) { 1728 ret = snd_soc_dai_set_stream(dai, sdw_stream, substream->stream); 1729 if (ret < 0) { 1730 dev_err(rtd->dev, "failed to set stream pointer on dai %s\n", dai->name); 1731 break; 1732 } 1733 } 1734 1735 return ret; 1736 } 1737 1738 /** 1739 * sdw_alloc_stream() - Allocate and return stream runtime 1740 * 1741 * @stream_name: SoundWire stream name 1742 * 1743 * Allocates a SoundWire stream runtime instance. 1744 * sdw_alloc_stream should be called only once per stream. Typically 1745 * invoked from ALSA/ASoC machine/platform driver. 1746 */ 1747 struct sdw_stream_runtime *sdw_alloc_stream(const char *stream_name) 1748 { 1749 struct sdw_stream_runtime *stream; 1750 1751 stream = kzalloc(sizeof(*stream), GFP_KERNEL); 1752 if (!stream) 1753 return NULL; 1754 1755 stream->name = stream_name; 1756 INIT_LIST_HEAD(&stream->master_list); 1757 stream->state = SDW_STREAM_ALLOCATED; 1758 stream->m_rt_count = 0; 1759 1760 return stream; 1761 } 1762 EXPORT_SYMBOL(sdw_alloc_stream); 1763 1764 /** 1765 * sdw_startup_stream() - Startup SoundWire stream 1766 * 1767 * @sdw_substream: Soundwire stream 1768 * 1769 * Documentation/driver-api/soundwire/stream.rst explains this API in detail 1770 */ 1771 int sdw_startup_stream(void *sdw_substream) 1772 { 1773 struct snd_pcm_substream *substream = sdw_substream; 1774 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 1775 struct sdw_stream_runtime *sdw_stream; 1776 char *name; 1777 int ret; 1778 1779 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 1780 name = kasprintf(GFP_KERNEL, "%s-Playback", substream->name); 1781 else 1782 name = kasprintf(GFP_KERNEL, "%s-Capture", substream->name); 1783 1784 if (!name) 1785 return -ENOMEM; 1786 1787 sdw_stream = sdw_alloc_stream(name); 1788 if (!sdw_stream) { 1789 dev_err(rtd->dev, "alloc stream failed for substream DAI %s\n", substream->name); 1790 ret = -ENOMEM; 1791 goto error; 1792 } 1793 1794 ret = set_stream(substream, sdw_stream); 1795 if (ret < 0) 1796 goto release_stream; 1797 return 0; 1798 1799 release_stream: 1800 sdw_release_stream(sdw_stream); 1801 set_stream(substream, NULL); 1802 error: 1803 kfree(name); 1804 return ret; 1805 } 1806 EXPORT_SYMBOL(sdw_startup_stream); 1807 1808 /** 1809 * sdw_shutdown_stream() - Shutdown SoundWire stream 1810 * 1811 * @sdw_substream: Soundwire stream 1812 * 1813 * Documentation/driver-api/soundwire/stream.rst explains this API in detail 1814 */ 1815 void sdw_shutdown_stream(void *sdw_substream) 1816 { 1817 struct snd_pcm_substream *substream = sdw_substream; 1818 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 1819 struct sdw_stream_runtime *sdw_stream; 1820 struct snd_soc_dai *dai; 1821 1822 /* Find stream from first CPU DAI */ 1823 dai = snd_soc_rtd_to_cpu(rtd, 0); 1824 1825 sdw_stream = snd_soc_dai_get_stream(dai, substream->stream); 1826 1827 if (IS_ERR(sdw_stream)) { 1828 dev_err(rtd->dev, "no stream found for DAI %s\n", dai->name); 1829 return; 1830 } 1831 1832 /* release memory */ 1833 kfree(sdw_stream->name); 1834 sdw_release_stream(sdw_stream); 1835 1836 /* clear DAI data */ 1837 set_stream(substream, NULL); 1838 } 1839 EXPORT_SYMBOL(sdw_shutdown_stream); 1840 1841 /** 1842 * sdw_release_stream() - Free the assigned stream runtime 1843 * 1844 * @stream: SoundWire stream runtime 1845 * 1846 * sdw_release_stream should be called only once per stream 1847 */ 1848 void sdw_release_stream(struct sdw_stream_runtime *stream) 1849 { 1850 kfree(stream); 1851 } 1852 EXPORT_SYMBOL(sdw_release_stream); 1853 1854 /** 1855 * sdw_stream_add_master() - Allocate and add master runtime to a stream 1856 * 1857 * @bus: SDW Bus instance 1858 * @stream_config: Stream configuration for audio stream 1859 * @port_config: Port configuration for audio stream 1860 * @num_ports: Number of ports 1861 * @stream: SoundWire stream 1862 */ 1863 int sdw_stream_add_master(struct sdw_bus *bus, 1864 struct sdw_stream_config *stream_config, 1865 const struct sdw_port_config *port_config, 1866 unsigned int num_ports, 1867 struct sdw_stream_runtime *stream) 1868 { 1869 struct sdw_master_runtime *m_rt; 1870 bool alloc_master_rt = false; 1871 int ret; 1872 1873 mutex_lock(&bus->bus_lock); 1874 1875 /* 1876 * For multi link streams, add the second master only if 1877 * the bus supports it. 1878 * Check if bus->multi_link is set 1879 */ 1880 if (!bus->multi_link && stream->m_rt_count > 0) { 1881 dev_err(bus->dev, 1882 "Multilink not supported, link %d\n", bus->link_id); 1883 ret = -EINVAL; 1884 goto unlock; 1885 } 1886 1887 /* 1888 * check if Master is already allocated (e.g. as a result of Slave adding 1889 * it first), if so skip allocation and go to configuration 1890 */ 1891 m_rt = sdw_master_rt_find(bus, stream); 1892 if (!m_rt) { 1893 m_rt = sdw_master_rt_alloc(bus, stream); 1894 if (!m_rt) { 1895 dev_err(bus->dev, "%s: Master runtime alloc failed for stream:%s\n", 1896 __func__, stream->name); 1897 ret = -ENOMEM; 1898 goto unlock; 1899 } 1900 1901 alloc_master_rt = true; 1902 } 1903 1904 if (!sdw_master_port_allocated(m_rt)) { 1905 ret = sdw_master_port_alloc(m_rt, num_ports); 1906 if (ret) 1907 goto alloc_error; 1908 1909 stream->m_rt_count++; 1910 } 1911 1912 ret = sdw_master_rt_config(m_rt, stream_config); 1913 if (ret < 0) 1914 goto unlock; 1915 1916 ret = sdw_config_stream(bus->dev, stream, stream_config, false); 1917 if (ret) 1918 goto unlock; 1919 1920 ret = sdw_master_port_config(m_rt, port_config); 1921 1922 goto unlock; 1923 1924 alloc_error: 1925 /* 1926 * we only cleanup what was allocated in this routine 1927 */ 1928 if (alloc_master_rt) 1929 sdw_master_rt_free(m_rt, stream); 1930 unlock: 1931 mutex_unlock(&bus->bus_lock); 1932 return ret; 1933 } 1934 EXPORT_SYMBOL(sdw_stream_add_master); 1935 1936 /** 1937 * sdw_stream_remove_master() - Remove master from sdw_stream 1938 * 1939 * @bus: SDW Bus instance 1940 * @stream: SoundWire stream 1941 * 1942 * This removes and frees port_rt and master_rt from a stream 1943 */ 1944 int sdw_stream_remove_master(struct sdw_bus *bus, 1945 struct sdw_stream_runtime *stream) 1946 { 1947 struct sdw_master_runtime *m_rt, *_m_rt; 1948 1949 mutex_lock(&bus->bus_lock); 1950 1951 list_for_each_entry_safe(m_rt, _m_rt, 1952 &stream->master_list, stream_node) { 1953 if (m_rt->bus != bus) 1954 continue; 1955 1956 sdw_master_port_free(m_rt); 1957 sdw_master_rt_free(m_rt, stream); 1958 stream->m_rt_count--; 1959 } 1960 1961 if (list_empty(&stream->master_list)) 1962 stream->state = SDW_STREAM_RELEASED; 1963 1964 mutex_unlock(&bus->bus_lock); 1965 1966 return 0; 1967 } 1968 EXPORT_SYMBOL(sdw_stream_remove_master); 1969 1970 /** 1971 * sdw_stream_add_slave() - Allocate and add master/slave runtime to a stream 1972 * 1973 * @slave: SDW Slave instance 1974 * @stream_config: Stream configuration for audio stream 1975 * @stream: SoundWire stream 1976 * @port_config: Port configuration for audio stream 1977 * @num_ports: Number of ports 1978 * 1979 * It is expected that Slave is added before adding Master 1980 * to the Stream. 1981 * 1982 */ 1983 int sdw_stream_add_slave(struct sdw_slave *slave, 1984 struct sdw_stream_config *stream_config, 1985 const struct sdw_port_config *port_config, 1986 unsigned int num_ports, 1987 struct sdw_stream_runtime *stream) 1988 { 1989 struct sdw_slave_runtime *s_rt; 1990 struct sdw_master_runtime *m_rt; 1991 bool alloc_master_rt = false; 1992 bool alloc_slave_rt = false; 1993 1994 int ret; 1995 1996 mutex_lock(&slave->bus->bus_lock); 1997 1998 /* 1999 * check if Master is already allocated, if so skip allocation 2000 * and go to configuration 2001 */ 2002 m_rt = sdw_master_rt_find(slave->bus, stream); 2003 if (!m_rt) { 2004 /* 2005 * If this API is invoked by Slave first then m_rt is not valid. 2006 * So, allocate m_rt and add Slave to it. 2007 */ 2008 m_rt = sdw_master_rt_alloc(slave->bus, stream); 2009 if (!m_rt) { 2010 dev_err(&slave->dev, "%s: Master runtime alloc failed for stream:%s\n", 2011 __func__, stream->name); 2012 ret = -ENOMEM; 2013 goto unlock; 2014 } 2015 2016 alloc_master_rt = true; 2017 } 2018 2019 s_rt = sdw_slave_rt_find(slave, stream); 2020 if (!s_rt) { 2021 s_rt = sdw_slave_rt_alloc(slave, m_rt); 2022 if (!s_rt) { 2023 dev_err(&slave->dev, "Slave runtime alloc failed for stream:%s\n", 2024 stream->name); 2025 ret = -ENOMEM; 2026 goto alloc_error; 2027 } 2028 2029 alloc_slave_rt = true; 2030 } 2031 2032 if (!sdw_slave_port_allocated(s_rt)) { 2033 ret = sdw_slave_port_alloc(slave, s_rt, num_ports); 2034 if (ret) 2035 goto alloc_error; 2036 } 2037 2038 ret = sdw_master_rt_config(m_rt, stream_config); 2039 if (ret) 2040 goto unlock; 2041 2042 ret = sdw_slave_rt_config(s_rt, stream_config); 2043 if (ret) 2044 goto unlock; 2045 2046 ret = sdw_config_stream(&slave->dev, stream, stream_config, true); 2047 if (ret) 2048 goto unlock; 2049 2050 ret = sdw_slave_port_config(slave, s_rt, port_config); 2051 if (ret) 2052 goto unlock; 2053 2054 /* 2055 * Change stream state to CONFIGURED on first Slave add. 2056 * Bus is not aware of number of Slave(s) in a stream at this 2057 * point so cannot depend on all Slave(s) to be added in order to 2058 * change stream state to CONFIGURED. 2059 */ 2060 stream->state = SDW_STREAM_CONFIGURED; 2061 goto unlock; 2062 2063 alloc_error: 2064 /* 2065 * we only cleanup what was allocated in this routine. The 'else if' 2066 * is intentional, the 'master_rt_free' will call sdw_slave_rt_free() 2067 * internally. 2068 */ 2069 if (alloc_master_rt) 2070 sdw_master_rt_free(m_rt, stream); 2071 else if (alloc_slave_rt) 2072 sdw_slave_rt_free(slave, stream); 2073 unlock: 2074 mutex_unlock(&slave->bus->bus_lock); 2075 return ret; 2076 } 2077 EXPORT_SYMBOL(sdw_stream_add_slave); 2078 2079 /** 2080 * sdw_stream_remove_slave() - Remove slave from sdw_stream 2081 * 2082 * @slave: SDW Slave instance 2083 * @stream: SoundWire stream 2084 * 2085 * This removes and frees port_rt and slave_rt from a stream 2086 */ 2087 int sdw_stream_remove_slave(struct sdw_slave *slave, 2088 struct sdw_stream_runtime *stream) 2089 { 2090 mutex_lock(&slave->bus->bus_lock); 2091 2092 sdw_slave_port_free(slave, stream); 2093 sdw_slave_rt_free(slave, stream); 2094 2095 mutex_unlock(&slave->bus->bus_lock); 2096 2097 return 0; 2098 } 2099 EXPORT_SYMBOL(sdw_stream_remove_slave); 2100