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 struct sdw_slave *slave; 633 int ret = 0; 634 u32 addr1; 635 636 /* Check if all Peripherals comply with SDCA */ 637 list_for_each_entry(slave, &bus->slaves, node) { 638 if (!slave->dev_num_sticky) 639 continue; 640 if (!is_clock_scaling_supported_by_slave(slave)) { 641 dev_dbg(&slave->dev, "The Peripheral doesn't comply with SDCA\n"); 642 goto manager_runtime; 643 } 644 } 645 646 if (bus->params.next_bank) 647 addr1 = SDW_SCP_BUSCLOCK_SCALE_B1; 648 else 649 addr1 = SDW_SCP_BUSCLOCK_SCALE_B0; 650 651 /* Program SDW_SCP_BUSCLOCK_SCALE if all Peripherals comply with SDCA */ 652 list_for_each_entry(slave, &bus->slaves, node) { 653 int scale_index; 654 u8 base; 655 656 if (!slave->dev_num_sticky) 657 continue; 658 scale_index = sdw_slave_get_scale_index(slave, &base); 659 if (scale_index < 0) 660 return scale_index; 661 662 ret = sdw_write_no_pm(slave, addr1, scale_index); 663 if (ret < 0) { 664 dev_err(&slave->dev, "SDW_SCP_BUSCLOCK_SCALE register write failed\n"); 665 return ret; 666 } 667 } 668 669 manager_runtime: 670 list_for_each_entry(m_rt, &bus->m_rt_list, bus_node) { 671 672 /* 673 * this loop walks through all master runtimes for a 674 * bus, but the ports can only be configured while 675 * explicitly preparing a stream or handling an 676 * already-prepared stream otherwise. 677 */ 678 if (!prepare && 679 m_rt->stream->state == SDW_STREAM_CONFIGURED) 680 continue; 681 682 ret = sdw_program_port_params(m_rt); 683 if (ret < 0) { 684 dev_err(bus->dev, 685 "Program transport params failed: %d\n", ret); 686 return ret; 687 } 688 689 ret = sdw_notify_config(m_rt); 690 if (ret < 0) { 691 dev_err(bus->dev, 692 "Notify bus config failed: %d\n", ret); 693 return ret; 694 } 695 696 /* Enable port(s) on alternate bank for all active streams */ 697 if (m_rt->stream->state != SDW_STREAM_ENABLED) 698 continue; 699 700 ret = sdw_enable_disable_ports(m_rt, true); 701 if (ret < 0) { 702 dev_err(bus->dev, "Enable channel failed: %d\n", ret); 703 return ret; 704 } 705 } 706 707 return ret; 708 } 709 710 static int sdw_bank_switch(struct sdw_bus *bus, int m_rt_count) 711 { 712 int col_index, row_index; 713 bool multi_link; 714 struct sdw_msg *wr_msg; 715 u8 *wbuf; 716 int ret; 717 u16 addr; 718 719 wr_msg = kzalloc(sizeof(*wr_msg), GFP_KERNEL); 720 if (!wr_msg) 721 return -ENOMEM; 722 723 wbuf = kzalloc(sizeof(*wbuf), GFP_KERNEL); 724 if (!wbuf) { 725 ret = -ENOMEM; 726 goto error_1; 727 } 728 729 /* Get row and column index to program register */ 730 col_index = sdw_find_col_index(bus->params.col); 731 row_index = sdw_find_row_index(bus->params.row); 732 wbuf[0] = col_index | (row_index << 3); 733 734 if (bus->params.next_bank) 735 addr = SDW_SCP_FRAMECTRL_B1; 736 else 737 addr = SDW_SCP_FRAMECTRL_B0; 738 739 sdw_fill_msg(wr_msg, NULL, addr, 1, SDW_BROADCAST_DEV_NUM, 740 SDW_MSG_FLAG_WRITE, wbuf); 741 wr_msg->ssp_sync = true; 742 743 /* 744 * Set the multi_link flag only when both the hardware supports 745 * and hardware-based sync is required 746 */ 747 multi_link = bus->multi_link && (m_rt_count >= bus->hw_sync_min_links); 748 749 if (multi_link) 750 ret = sdw_transfer_defer(bus, wr_msg); 751 else 752 ret = sdw_transfer(bus, wr_msg); 753 754 if (ret < 0 && ret != -ENODATA) { 755 dev_err(bus->dev, "Slave frame_ctrl reg write failed\n"); 756 goto error; 757 } 758 759 if (!multi_link) { 760 kfree(wbuf); 761 kfree(wr_msg); 762 bus->defer_msg.msg = NULL; 763 bus->params.curr_bank = !bus->params.curr_bank; 764 bus->params.next_bank = !bus->params.next_bank; 765 } 766 767 return 0; 768 769 error: 770 kfree(wbuf); 771 error_1: 772 kfree(wr_msg); 773 bus->defer_msg.msg = NULL; 774 return ret; 775 } 776 777 /** 778 * sdw_ml_sync_bank_switch: Multilink register bank switch 779 * 780 * @bus: SDW bus instance 781 * @multi_link: whether this is a multi-link stream with hardware-based sync 782 * 783 * Caller function should free the buffers on error 784 */ 785 static int sdw_ml_sync_bank_switch(struct sdw_bus *bus, bool multi_link) 786 { 787 unsigned long time_left; 788 789 if (!multi_link) 790 return 0; 791 792 /* Wait for completion of transfer */ 793 time_left = wait_for_completion_timeout(&bus->defer_msg.complete, 794 bus->bank_switch_timeout); 795 796 if (!time_left) { 797 dev_err(bus->dev, "Controller Timed out on bank switch\n"); 798 return -ETIMEDOUT; 799 } 800 801 bus->params.curr_bank = !bus->params.curr_bank; 802 bus->params.next_bank = !bus->params.next_bank; 803 804 if (bus->defer_msg.msg) { 805 kfree(bus->defer_msg.msg->buf); 806 kfree(bus->defer_msg.msg); 807 bus->defer_msg.msg = NULL; 808 } 809 810 return 0; 811 } 812 813 static int do_bank_switch(struct sdw_stream_runtime *stream) 814 { 815 struct sdw_master_runtime *m_rt; 816 const struct sdw_master_ops *ops; 817 struct sdw_bus *bus; 818 bool multi_link = false; 819 int m_rt_count; 820 int ret = 0; 821 822 m_rt_count = stream->m_rt_count; 823 824 list_for_each_entry(m_rt, &stream->master_list, stream_node) { 825 bus = m_rt->bus; 826 ops = bus->ops; 827 828 if (bus->multi_link && m_rt_count >= bus->hw_sync_min_links) { 829 multi_link = true; 830 mutex_lock(&bus->msg_lock); 831 } 832 833 /* Pre-bank switch */ 834 if (ops->pre_bank_switch) { 835 ret = ops->pre_bank_switch(bus); 836 if (ret < 0) { 837 dev_err(bus->dev, 838 "Pre bank switch op failed: %d\n", ret); 839 goto msg_unlock; 840 } 841 } 842 843 /* 844 * Perform Bank switch operation. 845 * For multi link cases, the actual bank switch is 846 * synchronized across all Masters and happens later as a 847 * part of post_bank_switch ops. 848 */ 849 ret = sdw_bank_switch(bus, m_rt_count); 850 if (ret < 0) { 851 dev_err(bus->dev, "Bank switch failed: %d\n", ret); 852 goto error; 853 } 854 } 855 856 /* 857 * For multi link cases, it is expected that the bank switch is 858 * triggered by the post_bank_switch for the first Master in the list 859 * and for the other Masters the post_bank_switch() should return doing 860 * nothing. 861 */ 862 list_for_each_entry(m_rt, &stream->master_list, stream_node) { 863 bus = m_rt->bus; 864 ops = bus->ops; 865 866 /* Post-bank switch */ 867 if (ops->post_bank_switch) { 868 ret = ops->post_bank_switch(bus); 869 if (ret < 0) { 870 dev_err(bus->dev, 871 "Post bank switch op failed: %d\n", 872 ret); 873 goto error; 874 } 875 } else if (multi_link) { 876 dev_err(bus->dev, 877 "Post bank switch ops not implemented\n"); 878 ret = -EINVAL; 879 goto error; 880 } 881 882 /* Set the bank switch timeout to default, if not set */ 883 if (!bus->bank_switch_timeout) 884 bus->bank_switch_timeout = DEFAULT_BANK_SWITCH_TIMEOUT; 885 886 /* Check if bank switch was successful */ 887 ret = sdw_ml_sync_bank_switch(bus, multi_link); 888 if (ret < 0) { 889 dev_err(bus->dev, 890 "multi link bank switch failed: %d\n", ret); 891 goto error; 892 } 893 894 if (multi_link) 895 mutex_unlock(&bus->msg_lock); 896 } 897 898 return ret; 899 900 error: 901 list_for_each_entry(m_rt, &stream->master_list, stream_node) { 902 bus = m_rt->bus; 903 if (bus->defer_msg.msg) { 904 kfree(bus->defer_msg.msg->buf); 905 kfree(bus->defer_msg.msg); 906 bus->defer_msg.msg = NULL; 907 } 908 } 909 910 msg_unlock: 911 912 if (multi_link) { 913 list_for_each_entry(m_rt, &stream->master_list, stream_node) { 914 bus = m_rt->bus; 915 if (mutex_is_locked(&bus->msg_lock)) 916 mutex_unlock(&bus->msg_lock); 917 } 918 } 919 920 return ret; 921 } 922 923 static struct sdw_port_runtime *sdw_port_alloc(struct list_head *port_list) 924 { 925 struct sdw_port_runtime *p_rt; 926 927 p_rt = kzalloc(sizeof(*p_rt), GFP_KERNEL); 928 if (!p_rt) 929 return NULL; 930 931 list_add_tail(&p_rt->port_node, port_list); 932 933 return p_rt; 934 } 935 936 static int sdw_port_config(struct sdw_port_runtime *p_rt, 937 const struct sdw_port_config *port_config, 938 int port_index) 939 { 940 p_rt->ch_mask = port_config[port_index].ch_mask; 941 p_rt->num = port_config[port_index].num; 942 943 /* 944 * TODO: Check port capabilities for requested configuration 945 */ 946 947 return 0; 948 } 949 950 static void sdw_port_free(struct sdw_port_runtime *p_rt) 951 { 952 list_del(&p_rt->port_node); 953 kfree(p_rt); 954 } 955 956 static bool sdw_slave_port_allocated(struct sdw_slave_runtime *s_rt) 957 { 958 return !list_empty(&s_rt->port_list); 959 } 960 961 static void sdw_slave_port_free(struct sdw_slave *slave, 962 struct sdw_stream_runtime *stream) 963 { 964 struct sdw_port_runtime *p_rt, *_p_rt; 965 struct sdw_master_runtime *m_rt; 966 struct sdw_slave_runtime *s_rt; 967 968 list_for_each_entry(m_rt, &stream->master_list, stream_node) { 969 list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) { 970 if (s_rt->slave != slave) 971 continue; 972 973 list_for_each_entry_safe(p_rt, _p_rt, 974 &s_rt->port_list, port_node) { 975 sdw_port_free(p_rt); 976 } 977 } 978 } 979 } 980 981 static int sdw_slave_port_alloc(struct sdw_slave *slave, 982 struct sdw_slave_runtime *s_rt, 983 unsigned int num_config) 984 { 985 struct sdw_port_runtime *p_rt; 986 int i; 987 988 /* Iterate for number of ports to perform initialization */ 989 for (i = 0; i < num_config; i++) { 990 p_rt = sdw_port_alloc(&s_rt->port_list); 991 if (!p_rt) 992 return -ENOMEM; 993 } 994 995 return 0; 996 } 997 998 static int sdw_slave_port_is_valid_range(struct device *dev, int num) 999 { 1000 if (!SDW_VALID_PORT_RANGE(num)) { 1001 dev_err(dev, "SoundWire: Invalid port number :%d\n", num); 1002 return -EINVAL; 1003 } 1004 1005 return 0; 1006 } 1007 1008 static int sdw_slave_port_config(struct sdw_slave *slave, 1009 struct sdw_slave_runtime *s_rt, 1010 const struct sdw_port_config *port_config) 1011 { 1012 struct sdw_port_runtime *p_rt; 1013 int ret; 1014 int i; 1015 1016 i = 0; 1017 list_for_each_entry(p_rt, &s_rt->port_list, port_node) { 1018 /* 1019 * TODO: Check valid port range as defined by DisCo/ 1020 * slave 1021 */ 1022 ret = sdw_slave_port_is_valid_range(&slave->dev, port_config[i].num); 1023 if (ret < 0) 1024 return ret; 1025 1026 ret = sdw_port_config(p_rt, port_config, i); 1027 if (ret < 0) 1028 return ret; 1029 i++; 1030 } 1031 1032 return 0; 1033 } 1034 1035 static bool sdw_master_port_allocated(struct sdw_master_runtime *m_rt) 1036 { 1037 return !list_empty(&m_rt->port_list); 1038 } 1039 1040 static void sdw_master_port_free(struct sdw_master_runtime *m_rt) 1041 { 1042 struct sdw_port_runtime *p_rt, *_p_rt; 1043 1044 list_for_each_entry_safe(p_rt, _p_rt, &m_rt->port_list, port_node) { 1045 sdw_port_free(p_rt); 1046 } 1047 } 1048 1049 static int sdw_master_port_alloc(struct sdw_master_runtime *m_rt, 1050 unsigned int num_ports) 1051 { 1052 struct sdw_port_runtime *p_rt; 1053 int i; 1054 1055 /* Iterate for number of ports to perform initialization */ 1056 for (i = 0; i < num_ports; i++) { 1057 p_rt = sdw_port_alloc(&m_rt->port_list); 1058 if (!p_rt) 1059 return -ENOMEM; 1060 } 1061 1062 return 0; 1063 } 1064 1065 static int sdw_master_port_config(struct sdw_master_runtime *m_rt, 1066 const struct sdw_port_config *port_config) 1067 { 1068 struct sdw_port_runtime *p_rt; 1069 int ret; 1070 int i; 1071 1072 i = 0; 1073 list_for_each_entry(p_rt, &m_rt->port_list, port_node) { 1074 ret = sdw_port_config(p_rt, port_config, i); 1075 if (ret < 0) 1076 return ret; 1077 i++; 1078 } 1079 1080 return 0; 1081 } 1082 1083 /** 1084 * sdw_slave_rt_alloc() - Allocate a Slave runtime handle. 1085 * 1086 * @slave: Slave handle 1087 * @m_rt: Master runtime handle 1088 * 1089 * This function is to be called with bus_lock held. 1090 */ 1091 static struct sdw_slave_runtime 1092 *sdw_slave_rt_alloc(struct sdw_slave *slave, 1093 struct sdw_master_runtime *m_rt) 1094 { 1095 struct sdw_slave_runtime *s_rt; 1096 1097 s_rt = kzalloc(sizeof(*s_rt), GFP_KERNEL); 1098 if (!s_rt) 1099 return NULL; 1100 1101 INIT_LIST_HEAD(&s_rt->port_list); 1102 s_rt->slave = slave; 1103 1104 list_add_tail(&s_rt->m_rt_node, &m_rt->slave_rt_list); 1105 1106 return s_rt; 1107 } 1108 1109 /** 1110 * sdw_slave_rt_config() - Configure a Slave runtime handle. 1111 * 1112 * @s_rt: Slave runtime handle 1113 * @stream_config: Stream configuration 1114 * 1115 * This function is to be called with bus_lock held. 1116 */ 1117 static int sdw_slave_rt_config(struct sdw_slave_runtime *s_rt, 1118 struct sdw_stream_config *stream_config) 1119 { 1120 s_rt->ch_count = stream_config->ch_count; 1121 s_rt->direction = stream_config->direction; 1122 1123 return 0; 1124 } 1125 1126 static struct sdw_slave_runtime *sdw_slave_rt_find(struct sdw_slave *slave, 1127 struct sdw_stream_runtime *stream) 1128 { 1129 struct sdw_slave_runtime *s_rt, *_s_rt; 1130 struct sdw_master_runtime *m_rt; 1131 1132 list_for_each_entry(m_rt, &stream->master_list, stream_node) { 1133 /* Retrieve Slave runtime handle */ 1134 list_for_each_entry_safe(s_rt, _s_rt, 1135 &m_rt->slave_rt_list, m_rt_node) { 1136 if (s_rt->slave == slave) 1137 return s_rt; 1138 } 1139 } 1140 return NULL; 1141 } 1142 1143 /** 1144 * sdw_slave_rt_free() - Free Slave(s) runtime handle 1145 * 1146 * @slave: Slave handle. 1147 * @stream: Stream runtime handle. 1148 * 1149 * This function is to be called with bus_lock held. 1150 */ 1151 static void sdw_slave_rt_free(struct sdw_slave *slave, 1152 struct sdw_stream_runtime *stream) 1153 { 1154 struct sdw_slave_runtime *s_rt; 1155 1156 s_rt = sdw_slave_rt_find(slave, stream); 1157 if (s_rt) { 1158 list_del(&s_rt->m_rt_node); 1159 kfree(s_rt); 1160 } 1161 } 1162 1163 static struct sdw_master_runtime 1164 *sdw_master_rt_find(struct sdw_bus *bus, 1165 struct sdw_stream_runtime *stream) 1166 { 1167 struct sdw_master_runtime *m_rt; 1168 1169 /* Retrieve Bus handle if already available */ 1170 list_for_each_entry(m_rt, &stream->master_list, stream_node) { 1171 if (m_rt->bus == bus) 1172 return m_rt; 1173 } 1174 1175 return NULL; 1176 } 1177 1178 /** 1179 * sdw_master_rt_alloc() - Allocates a Master runtime handle 1180 * 1181 * @bus: SDW bus instance 1182 * @stream: Stream runtime handle. 1183 * 1184 * This function is to be called with bus_lock held. 1185 */ 1186 static struct sdw_master_runtime 1187 *sdw_master_rt_alloc(struct sdw_bus *bus, 1188 struct sdw_stream_runtime *stream) 1189 { 1190 struct sdw_master_runtime *m_rt, *walk_m_rt; 1191 struct list_head *insert_after; 1192 1193 m_rt = kzalloc(sizeof(*m_rt), GFP_KERNEL); 1194 if (!m_rt) 1195 return NULL; 1196 1197 /* Initialization of Master runtime handle */ 1198 INIT_LIST_HEAD(&m_rt->port_list); 1199 INIT_LIST_HEAD(&m_rt->slave_rt_list); 1200 1201 /* 1202 * Add in order of bus id so that when taking the bus_lock 1203 * of multiple buses they will always be taken in the same 1204 * order to prevent a mutex deadlock. 1205 */ 1206 insert_after = &stream->master_list; 1207 list_for_each_entry_reverse(walk_m_rt, &stream->master_list, stream_node) { 1208 if (walk_m_rt->bus->id < bus->id) { 1209 insert_after = &walk_m_rt->stream_node; 1210 break; 1211 } 1212 } 1213 list_add(&m_rt->stream_node, insert_after); 1214 1215 list_add_tail(&m_rt->bus_node, &bus->m_rt_list); 1216 1217 m_rt->bus = bus; 1218 m_rt->stream = stream; 1219 1220 bus->stream_refcount++; 1221 1222 return m_rt; 1223 } 1224 1225 /** 1226 * sdw_master_rt_config() - Configure Master runtime handle 1227 * 1228 * @m_rt: Master runtime handle 1229 * @stream_config: Stream configuration 1230 * 1231 * This function is to be called with bus_lock held. 1232 */ 1233 1234 static int sdw_master_rt_config(struct sdw_master_runtime *m_rt, 1235 struct sdw_stream_config *stream_config) 1236 { 1237 m_rt->ch_count = stream_config->ch_count; 1238 m_rt->direction = stream_config->direction; 1239 1240 return 0; 1241 } 1242 1243 /** 1244 * sdw_master_rt_free() - Free Master runtime handle 1245 * 1246 * @m_rt: Master runtime node 1247 * @stream: Stream runtime handle. 1248 * 1249 * This function is to be called with bus_lock held 1250 * It frees the Master runtime handle and associated Slave(s) runtime 1251 * handle. If this is called first then sdw_slave_rt_free() will have 1252 * no effect as Slave(s) runtime handle would already be freed up. 1253 */ 1254 static void sdw_master_rt_free(struct sdw_master_runtime *m_rt, 1255 struct sdw_stream_runtime *stream) 1256 { 1257 struct sdw_slave_runtime *s_rt, *_s_rt; 1258 struct sdw_bus *bus = m_rt->bus; 1259 1260 list_for_each_entry_safe(s_rt, _s_rt, &m_rt->slave_rt_list, m_rt_node) { 1261 sdw_slave_port_free(s_rt->slave, stream); 1262 sdw_slave_rt_free(s_rt->slave, stream); 1263 } 1264 1265 list_del(&m_rt->stream_node); 1266 list_del(&m_rt->bus_node); 1267 kfree(m_rt); 1268 1269 bus->stream_refcount--; 1270 } 1271 1272 /** 1273 * sdw_config_stream() - Configure the allocated stream 1274 * 1275 * @dev: SDW device 1276 * @stream: SoundWire stream 1277 * @stream_config: Stream configuration for audio stream 1278 * @is_slave: is API called from Slave or Master 1279 * 1280 * This function is to be called with bus_lock held. 1281 */ 1282 static int sdw_config_stream(struct device *dev, 1283 struct sdw_stream_runtime *stream, 1284 struct sdw_stream_config *stream_config, 1285 bool is_slave) 1286 { 1287 /* 1288 * Update the stream rate, channel and bps based on data 1289 * source. For more than one data source (multilink), 1290 * match the rate, bps, stream type and increment number of channels. 1291 * 1292 * If rate/bps is zero, it means the values are not set, so skip 1293 * comparison and allow the value to be set and stored in stream 1294 */ 1295 if (stream->params.rate && 1296 stream->params.rate != stream_config->frame_rate) { 1297 dev_err(dev, "rate not matching, stream:%s\n", stream->name); 1298 return -EINVAL; 1299 } 1300 1301 if (stream->params.bps && 1302 stream->params.bps != stream_config->bps) { 1303 dev_err(dev, "bps not matching, stream:%s\n", stream->name); 1304 return -EINVAL; 1305 } 1306 1307 stream->type = stream_config->type; 1308 stream->params.rate = stream_config->frame_rate; 1309 stream->params.bps = stream_config->bps; 1310 1311 /* TODO: Update this check during Device-device support */ 1312 if (is_slave) 1313 stream->params.ch_count += stream_config->ch_count; 1314 1315 return 0; 1316 } 1317 1318 /** 1319 * sdw_get_slave_dpn_prop() - Get Slave port capabilities 1320 * 1321 * @slave: Slave handle 1322 * @direction: Data direction. 1323 * @port_num: Port number 1324 */ 1325 struct sdw_dpn_prop *sdw_get_slave_dpn_prop(struct sdw_slave *slave, 1326 enum sdw_data_direction direction, 1327 unsigned int port_num) 1328 { 1329 struct sdw_dpn_prop *dpn_prop; 1330 u8 num_ports; 1331 int i; 1332 1333 if (direction == SDW_DATA_DIR_TX) { 1334 num_ports = hweight32(slave->prop.source_ports); 1335 dpn_prop = slave->prop.src_dpn_prop; 1336 } else { 1337 num_ports = hweight32(slave->prop.sink_ports); 1338 dpn_prop = slave->prop.sink_dpn_prop; 1339 } 1340 1341 for (i = 0; i < num_ports; i++) { 1342 if (dpn_prop[i].num == port_num) 1343 return &dpn_prop[i]; 1344 } 1345 1346 return NULL; 1347 } 1348 1349 /** 1350 * sdw_acquire_bus_lock: Acquire bus lock for all Master runtime(s) 1351 * 1352 * @stream: SoundWire stream 1353 * 1354 * Acquire bus_lock for each of the master runtime(m_rt) part of this 1355 * stream to reconfigure the bus. 1356 * NOTE: This function is called from SoundWire stream ops and is 1357 * expected that a global lock is held before acquiring bus_lock. 1358 */ 1359 static void sdw_acquire_bus_lock(struct sdw_stream_runtime *stream) 1360 { 1361 struct sdw_master_runtime *m_rt; 1362 struct sdw_bus *bus; 1363 1364 /* Iterate for all Master(s) in Master list */ 1365 list_for_each_entry(m_rt, &stream->master_list, stream_node) { 1366 bus = m_rt->bus; 1367 1368 mutex_lock(&bus->bus_lock); 1369 } 1370 } 1371 1372 /** 1373 * sdw_release_bus_lock: Release bus lock for all Master runtime(s) 1374 * 1375 * @stream: SoundWire stream 1376 * 1377 * Release the previously held bus_lock after reconfiguring the bus. 1378 * NOTE: This function is called from SoundWire stream ops and is 1379 * expected that a global lock is held before releasing bus_lock. 1380 */ 1381 static void sdw_release_bus_lock(struct sdw_stream_runtime *stream) 1382 { 1383 struct sdw_master_runtime *m_rt; 1384 struct sdw_bus *bus; 1385 1386 /* Iterate for all Master(s) in Master list */ 1387 list_for_each_entry_reverse(m_rt, &stream->master_list, stream_node) { 1388 bus = m_rt->bus; 1389 mutex_unlock(&bus->bus_lock); 1390 } 1391 } 1392 1393 static int _sdw_prepare_stream(struct sdw_stream_runtime *stream, 1394 bool update_params) 1395 { 1396 struct sdw_master_runtime *m_rt; 1397 struct sdw_bus *bus; 1398 struct sdw_master_prop *prop; 1399 struct sdw_bus_params params; 1400 int ret; 1401 1402 /* Prepare Master(s) and Slave(s) port(s) associated with stream */ 1403 list_for_each_entry(m_rt, &stream->master_list, stream_node) { 1404 bus = m_rt->bus; 1405 prop = &bus->prop; 1406 memcpy(¶ms, &bus->params, sizeof(params)); 1407 1408 /* TODO: Support Asynchronous mode */ 1409 if ((prop->max_clk_freq % stream->params.rate) != 0) { 1410 dev_err(bus->dev, "Async mode not supported\n"); 1411 return -EINVAL; 1412 } 1413 1414 if (update_params) { 1415 /* Increment cumulative bus bandwidth */ 1416 /* TODO: Update this during Device-Device support */ 1417 bus->params.bandwidth += m_rt->stream->params.rate * 1418 m_rt->ch_count * m_rt->stream->params.bps; 1419 1420 /* Compute params */ 1421 if (bus->compute_params) { 1422 ret = bus->compute_params(bus, stream); 1423 if (ret < 0) { 1424 dev_err(bus->dev, "Compute params failed: %d\n", 1425 ret); 1426 goto restore_params; 1427 } 1428 } 1429 } 1430 1431 /* Program params */ 1432 ret = sdw_program_params(bus, true); 1433 if (ret < 0) { 1434 dev_err(bus->dev, "Program params failed: %d\n", ret); 1435 goto restore_params; 1436 } 1437 } 1438 1439 ret = do_bank_switch(stream); 1440 if (ret < 0) { 1441 pr_err("%s: do_bank_switch failed: %d\n", __func__, ret); 1442 goto restore_params; 1443 } 1444 1445 list_for_each_entry(m_rt, &stream->master_list, stream_node) { 1446 bus = m_rt->bus; 1447 1448 /* Prepare port(s) on the new clock configuration */ 1449 ret = sdw_prep_deprep_ports(m_rt, true); 1450 if (ret < 0) { 1451 dev_err(bus->dev, "Prepare port(s) failed ret = %d\n", 1452 ret); 1453 return ret; 1454 } 1455 } 1456 1457 stream->state = SDW_STREAM_PREPARED; 1458 1459 return ret; 1460 1461 restore_params: 1462 memcpy(&bus->params, ¶ms, sizeof(params)); 1463 return ret; 1464 } 1465 1466 /** 1467 * sdw_prepare_stream() - Prepare SoundWire stream 1468 * 1469 * @stream: Soundwire stream 1470 * 1471 * Documentation/driver-api/soundwire/stream.rst explains this API in detail 1472 */ 1473 int sdw_prepare_stream(struct sdw_stream_runtime *stream) 1474 { 1475 bool update_params = true; 1476 int ret; 1477 1478 if (!stream) { 1479 pr_err("SoundWire: Handle not found for stream\n"); 1480 return -EINVAL; 1481 } 1482 1483 sdw_acquire_bus_lock(stream); 1484 1485 if (stream->state == SDW_STREAM_PREPARED) { 1486 ret = 0; 1487 goto state_err; 1488 } 1489 1490 if (stream->state != SDW_STREAM_CONFIGURED && 1491 stream->state != SDW_STREAM_DEPREPARED && 1492 stream->state != SDW_STREAM_DISABLED) { 1493 pr_err("%s: %s: inconsistent state state %d\n", 1494 __func__, stream->name, stream->state); 1495 ret = -EINVAL; 1496 goto state_err; 1497 } 1498 1499 /* 1500 * when the stream is DISABLED, this means sdw_prepare_stream() 1501 * is called as a result of an underflow or a resume operation. 1502 * In this case, the bus parameters shall not be recomputed, but 1503 * still need to be re-applied 1504 */ 1505 if (stream->state == SDW_STREAM_DISABLED) 1506 update_params = false; 1507 1508 ret = _sdw_prepare_stream(stream, update_params); 1509 1510 state_err: 1511 sdw_release_bus_lock(stream); 1512 return ret; 1513 } 1514 EXPORT_SYMBOL(sdw_prepare_stream); 1515 1516 static int _sdw_enable_stream(struct sdw_stream_runtime *stream) 1517 { 1518 struct sdw_master_runtime *m_rt; 1519 struct sdw_bus *bus; 1520 int ret; 1521 1522 /* Enable Master(s) and Slave(s) port(s) associated with stream */ 1523 list_for_each_entry(m_rt, &stream->master_list, stream_node) { 1524 bus = m_rt->bus; 1525 1526 /* Program params */ 1527 ret = sdw_program_params(bus, false); 1528 if (ret < 0) { 1529 dev_err(bus->dev, "%s: Program params failed: %d\n", __func__, ret); 1530 return ret; 1531 } 1532 1533 /* Enable port(s) */ 1534 ret = sdw_enable_disable_ports(m_rt, true); 1535 if (ret < 0) { 1536 dev_err(bus->dev, 1537 "Enable port(s) failed ret: %d\n", ret); 1538 return ret; 1539 } 1540 } 1541 1542 ret = do_bank_switch(stream); 1543 if (ret < 0) { 1544 pr_err("%s: do_bank_switch failed: %d\n", __func__, ret); 1545 return ret; 1546 } 1547 1548 stream->state = SDW_STREAM_ENABLED; 1549 return 0; 1550 } 1551 1552 /** 1553 * sdw_enable_stream() - Enable SoundWire stream 1554 * 1555 * @stream: Soundwire stream 1556 * 1557 * Documentation/driver-api/soundwire/stream.rst explains this API in detail 1558 */ 1559 int sdw_enable_stream(struct sdw_stream_runtime *stream) 1560 { 1561 int ret; 1562 1563 if (!stream) { 1564 pr_err("SoundWire: Handle not found for stream\n"); 1565 return -EINVAL; 1566 } 1567 1568 sdw_acquire_bus_lock(stream); 1569 1570 if (stream->state == SDW_STREAM_ENABLED) { 1571 ret = 0; 1572 goto state_err; 1573 } 1574 1575 if (stream->state != SDW_STREAM_PREPARED && 1576 stream->state != SDW_STREAM_DISABLED) { 1577 pr_err("%s: %s: inconsistent state state %d\n", 1578 __func__, stream->name, stream->state); 1579 ret = -EINVAL; 1580 goto state_err; 1581 } 1582 1583 ret = _sdw_enable_stream(stream); 1584 1585 state_err: 1586 sdw_release_bus_lock(stream); 1587 return ret; 1588 } 1589 EXPORT_SYMBOL(sdw_enable_stream); 1590 1591 static int _sdw_disable_stream(struct sdw_stream_runtime *stream) 1592 { 1593 struct sdw_master_runtime *m_rt; 1594 int ret; 1595 1596 list_for_each_entry(m_rt, &stream->master_list, stream_node) { 1597 struct sdw_bus *bus = m_rt->bus; 1598 1599 /* Disable port(s) */ 1600 ret = sdw_enable_disable_ports(m_rt, false); 1601 if (ret < 0) { 1602 dev_err(bus->dev, "Disable port(s) failed: %d\n", ret); 1603 return ret; 1604 } 1605 } 1606 stream->state = SDW_STREAM_DISABLED; 1607 1608 list_for_each_entry(m_rt, &stream->master_list, stream_node) { 1609 struct sdw_bus *bus = m_rt->bus; 1610 1611 /* Program params */ 1612 ret = sdw_program_params(bus, false); 1613 if (ret < 0) { 1614 dev_err(bus->dev, "%s: Program params failed: %d\n", __func__, ret); 1615 return ret; 1616 } 1617 } 1618 1619 ret = do_bank_switch(stream); 1620 if (ret < 0) { 1621 pr_err("%s: do_bank_switch failed: %d\n", __func__, ret); 1622 return ret; 1623 } 1624 1625 /* make sure alternate bank (previous current) is also disabled */ 1626 list_for_each_entry(m_rt, &stream->master_list, stream_node) { 1627 struct sdw_bus *bus = m_rt->bus; 1628 1629 /* Disable port(s) */ 1630 ret = sdw_enable_disable_ports(m_rt, false); 1631 if (ret < 0) { 1632 dev_err(bus->dev, "Disable port(s) failed: %d\n", ret); 1633 return ret; 1634 } 1635 } 1636 1637 return 0; 1638 } 1639 1640 /** 1641 * sdw_disable_stream() - Disable SoundWire stream 1642 * 1643 * @stream: Soundwire stream 1644 * 1645 * Documentation/driver-api/soundwire/stream.rst explains this API in detail 1646 */ 1647 int sdw_disable_stream(struct sdw_stream_runtime *stream) 1648 { 1649 int ret; 1650 1651 if (!stream) { 1652 pr_err("SoundWire: Handle not found for stream\n"); 1653 return -EINVAL; 1654 } 1655 1656 sdw_acquire_bus_lock(stream); 1657 1658 if (stream->state == SDW_STREAM_DISABLED) { 1659 ret = 0; 1660 goto state_err; 1661 } 1662 1663 if (stream->state != SDW_STREAM_ENABLED) { 1664 pr_err("%s: %s: inconsistent state state %d\n", 1665 __func__, stream->name, stream->state); 1666 ret = -EINVAL; 1667 goto state_err; 1668 } 1669 1670 ret = _sdw_disable_stream(stream); 1671 1672 state_err: 1673 sdw_release_bus_lock(stream); 1674 return ret; 1675 } 1676 EXPORT_SYMBOL(sdw_disable_stream); 1677 1678 static int _sdw_deprepare_stream(struct sdw_stream_runtime *stream) 1679 { 1680 struct sdw_master_runtime *m_rt; 1681 struct sdw_port_runtime *p_rt; 1682 unsigned int multi_lane_bandwidth; 1683 unsigned int bandwidth; 1684 struct sdw_bus *bus; 1685 int state = stream->state; 1686 int ret = 0; 1687 1688 /* 1689 * first mark the state as DEPREPARED so that it is not taken into account 1690 * for bit allocation 1691 */ 1692 stream->state = SDW_STREAM_DEPREPARED; 1693 1694 list_for_each_entry(m_rt, &stream->master_list, stream_node) { 1695 bus = m_rt->bus; 1696 /* De-prepare port(s) */ 1697 ret = sdw_prep_deprep_ports(m_rt, false); 1698 if (ret < 0) { 1699 dev_err(bus->dev, 1700 "De-prepare port(s) failed: %d\n", ret); 1701 stream->state = state; 1702 return ret; 1703 } 1704 1705 multi_lane_bandwidth = 0; 1706 1707 list_for_each_entry(p_rt, &m_rt->port_list, port_node) { 1708 if (!p_rt->lane) 1709 continue; 1710 1711 bandwidth = m_rt->stream->params.rate * hweight32(p_rt->ch_mask) * 1712 m_rt->stream->params.bps; 1713 multi_lane_bandwidth += bandwidth; 1714 bus->lane_used_bandwidth[p_rt->lane] -= bandwidth; 1715 if (!bus->lane_used_bandwidth[p_rt->lane]) 1716 p_rt->lane = 0; 1717 } 1718 /* TODO: Update this during Device-Device support */ 1719 bandwidth = m_rt->stream->params.rate * m_rt->ch_count * m_rt->stream->params.bps; 1720 bus->params.bandwidth -= bandwidth - multi_lane_bandwidth; 1721 1722 /* Compute params */ 1723 if (bus->compute_params) { 1724 ret = bus->compute_params(bus, stream); 1725 if (ret < 0) { 1726 dev_err(bus->dev, "Compute params failed: %d\n", 1727 ret); 1728 stream->state = state; 1729 return ret; 1730 } 1731 } 1732 1733 /* Program params */ 1734 ret = sdw_program_params(bus, false); 1735 if (ret < 0) { 1736 dev_err(bus->dev, "%s: Program params failed: %d\n", __func__, ret); 1737 stream->state = state; 1738 return ret; 1739 } 1740 } 1741 1742 return do_bank_switch(stream); 1743 } 1744 1745 /** 1746 * sdw_deprepare_stream() - Deprepare SoundWire stream 1747 * 1748 * @stream: Soundwire stream 1749 * 1750 * Documentation/driver-api/soundwire/stream.rst explains this API in detail 1751 */ 1752 int sdw_deprepare_stream(struct sdw_stream_runtime *stream) 1753 { 1754 int ret; 1755 1756 if (!stream) { 1757 pr_err("SoundWire: Handle not found for stream\n"); 1758 return -EINVAL; 1759 } 1760 1761 sdw_acquire_bus_lock(stream); 1762 1763 if (stream->state == SDW_STREAM_DEPREPARED) { 1764 ret = 0; 1765 goto state_err; 1766 } 1767 1768 if (stream->state != SDW_STREAM_PREPARED && 1769 stream->state != SDW_STREAM_DISABLED) { 1770 pr_err("%s: %s: inconsistent state state %d\n", 1771 __func__, stream->name, stream->state); 1772 ret = -EINVAL; 1773 goto state_err; 1774 } 1775 1776 ret = _sdw_deprepare_stream(stream); 1777 1778 state_err: 1779 sdw_release_bus_lock(stream); 1780 return ret; 1781 } 1782 EXPORT_SYMBOL(sdw_deprepare_stream); 1783 1784 static int set_stream(struct snd_pcm_substream *substream, 1785 struct sdw_stream_runtime *sdw_stream) 1786 { 1787 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 1788 struct snd_soc_dai *dai; 1789 int ret = 0; 1790 int i; 1791 1792 /* Set stream pointer on all DAIs */ 1793 for_each_rtd_dais(rtd, i, dai) { 1794 ret = snd_soc_dai_set_stream(dai, sdw_stream, substream->stream); 1795 if (ret < 0) { 1796 dev_err(rtd->dev, "failed to set stream pointer on dai %s\n", dai->name); 1797 break; 1798 } 1799 } 1800 1801 return ret; 1802 } 1803 1804 /** 1805 * sdw_alloc_stream() - Allocate and return stream runtime 1806 * 1807 * @stream_name: SoundWire stream name 1808 * 1809 * Allocates a SoundWire stream runtime instance. 1810 * sdw_alloc_stream should be called only once per stream. Typically 1811 * invoked from ALSA/ASoC machine/platform driver. 1812 */ 1813 struct sdw_stream_runtime *sdw_alloc_stream(const char *stream_name) 1814 { 1815 struct sdw_stream_runtime *stream; 1816 1817 stream = kzalloc(sizeof(*stream), GFP_KERNEL); 1818 if (!stream) 1819 return NULL; 1820 1821 stream->name = stream_name; 1822 INIT_LIST_HEAD(&stream->master_list); 1823 stream->state = SDW_STREAM_ALLOCATED; 1824 stream->m_rt_count = 0; 1825 1826 return stream; 1827 } 1828 EXPORT_SYMBOL(sdw_alloc_stream); 1829 1830 /** 1831 * sdw_startup_stream() - Startup SoundWire stream 1832 * 1833 * @sdw_substream: Soundwire stream 1834 * 1835 * Documentation/driver-api/soundwire/stream.rst explains this API in detail 1836 */ 1837 int sdw_startup_stream(void *sdw_substream) 1838 { 1839 struct snd_pcm_substream *substream = sdw_substream; 1840 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 1841 struct sdw_stream_runtime *sdw_stream; 1842 char *name; 1843 int ret; 1844 1845 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 1846 name = kasprintf(GFP_KERNEL, "%s-Playback", substream->name); 1847 else 1848 name = kasprintf(GFP_KERNEL, "%s-Capture", substream->name); 1849 1850 if (!name) 1851 return -ENOMEM; 1852 1853 sdw_stream = sdw_alloc_stream(name); 1854 if (!sdw_stream) { 1855 dev_err(rtd->dev, "alloc stream failed for substream DAI %s\n", substream->name); 1856 ret = -ENOMEM; 1857 goto error; 1858 } 1859 1860 ret = set_stream(substream, sdw_stream); 1861 if (ret < 0) 1862 goto release_stream; 1863 return 0; 1864 1865 release_stream: 1866 sdw_release_stream(sdw_stream); 1867 set_stream(substream, NULL); 1868 error: 1869 kfree(name); 1870 return ret; 1871 } 1872 EXPORT_SYMBOL(sdw_startup_stream); 1873 1874 /** 1875 * sdw_shutdown_stream() - Shutdown SoundWire stream 1876 * 1877 * @sdw_substream: Soundwire stream 1878 * 1879 * Documentation/driver-api/soundwire/stream.rst explains this API in detail 1880 */ 1881 void sdw_shutdown_stream(void *sdw_substream) 1882 { 1883 struct snd_pcm_substream *substream = sdw_substream; 1884 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 1885 struct sdw_stream_runtime *sdw_stream; 1886 struct snd_soc_dai *dai; 1887 1888 /* Find stream from first CPU DAI */ 1889 dai = snd_soc_rtd_to_cpu(rtd, 0); 1890 1891 sdw_stream = snd_soc_dai_get_stream(dai, substream->stream); 1892 1893 if (IS_ERR(sdw_stream)) { 1894 dev_err(rtd->dev, "no stream found for DAI %s\n", dai->name); 1895 return; 1896 } 1897 1898 /* release memory */ 1899 kfree(sdw_stream->name); 1900 sdw_release_stream(sdw_stream); 1901 1902 /* clear DAI data */ 1903 set_stream(substream, NULL); 1904 } 1905 EXPORT_SYMBOL(sdw_shutdown_stream); 1906 1907 /** 1908 * sdw_release_stream() - Free the assigned stream runtime 1909 * 1910 * @stream: SoundWire stream runtime 1911 * 1912 * sdw_release_stream should be called only once per stream 1913 */ 1914 void sdw_release_stream(struct sdw_stream_runtime *stream) 1915 { 1916 kfree(stream); 1917 } 1918 EXPORT_SYMBOL(sdw_release_stream); 1919 1920 /** 1921 * sdw_stream_add_master() - Allocate and add master runtime to a stream 1922 * 1923 * @bus: SDW Bus instance 1924 * @stream_config: Stream configuration for audio stream 1925 * @port_config: Port configuration for audio stream 1926 * @num_ports: Number of ports 1927 * @stream: SoundWire stream 1928 */ 1929 int sdw_stream_add_master(struct sdw_bus *bus, 1930 struct sdw_stream_config *stream_config, 1931 const struct sdw_port_config *port_config, 1932 unsigned int num_ports, 1933 struct sdw_stream_runtime *stream) 1934 { 1935 struct sdw_master_runtime *m_rt; 1936 bool alloc_master_rt = false; 1937 int ret; 1938 1939 mutex_lock(&bus->bus_lock); 1940 1941 /* 1942 * For multi link streams, add the second master only if 1943 * the bus supports it. 1944 * Check if bus->multi_link is set 1945 */ 1946 if (!bus->multi_link && stream->m_rt_count > 0) { 1947 dev_err(bus->dev, 1948 "Multilink not supported, link %d\n", bus->link_id); 1949 ret = -EINVAL; 1950 goto unlock; 1951 } 1952 1953 /* 1954 * check if Master is already allocated (e.g. as a result of Slave adding 1955 * it first), if so skip allocation and go to configuration 1956 */ 1957 m_rt = sdw_master_rt_find(bus, stream); 1958 if (!m_rt) { 1959 m_rt = sdw_master_rt_alloc(bus, stream); 1960 if (!m_rt) { 1961 dev_err(bus->dev, "%s: Master runtime alloc failed for stream:%s\n", 1962 __func__, stream->name); 1963 ret = -ENOMEM; 1964 goto unlock; 1965 } 1966 1967 alloc_master_rt = true; 1968 } 1969 1970 if (!sdw_master_port_allocated(m_rt)) { 1971 ret = sdw_master_port_alloc(m_rt, num_ports); 1972 if (ret) 1973 goto alloc_error; 1974 1975 stream->m_rt_count++; 1976 } 1977 1978 ret = sdw_master_rt_config(m_rt, stream_config); 1979 if (ret < 0) 1980 goto unlock; 1981 1982 ret = sdw_config_stream(bus->dev, stream, stream_config, false); 1983 if (ret) 1984 goto unlock; 1985 1986 ret = sdw_master_port_config(m_rt, port_config); 1987 1988 goto unlock; 1989 1990 alloc_error: 1991 /* 1992 * we only cleanup what was allocated in this routine 1993 */ 1994 if (alloc_master_rt) 1995 sdw_master_rt_free(m_rt, stream); 1996 unlock: 1997 mutex_unlock(&bus->bus_lock); 1998 return ret; 1999 } 2000 EXPORT_SYMBOL(sdw_stream_add_master); 2001 2002 /** 2003 * sdw_stream_remove_master() - Remove master from sdw_stream 2004 * 2005 * @bus: SDW Bus instance 2006 * @stream: SoundWire stream 2007 * 2008 * This removes and frees port_rt and master_rt from a stream 2009 */ 2010 int sdw_stream_remove_master(struct sdw_bus *bus, 2011 struct sdw_stream_runtime *stream) 2012 { 2013 struct sdw_master_runtime *m_rt, *_m_rt; 2014 2015 mutex_lock(&bus->bus_lock); 2016 2017 list_for_each_entry_safe(m_rt, _m_rt, 2018 &stream->master_list, stream_node) { 2019 if (m_rt->bus != bus) 2020 continue; 2021 2022 sdw_master_port_free(m_rt); 2023 sdw_master_rt_free(m_rt, stream); 2024 stream->m_rt_count--; 2025 } 2026 2027 if (list_empty(&stream->master_list)) 2028 stream->state = SDW_STREAM_RELEASED; 2029 2030 mutex_unlock(&bus->bus_lock); 2031 2032 return 0; 2033 } 2034 EXPORT_SYMBOL(sdw_stream_remove_master); 2035 2036 /** 2037 * sdw_stream_add_slave() - Allocate and add master/slave runtime to a stream 2038 * 2039 * @slave: SDW Slave instance 2040 * @stream_config: Stream configuration for audio stream 2041 * @stream: SoundWire stream 2042 * @port_config: Port configuration for audio stream 2043 * @num_ports: Number of ports 2044 * 2045 * It is expected that Slave is added before adding Master 2046 * to the Stream. 2047 * 2048 */ 2049 int sdw_stream_add_slave(struct sdw_slave *slave, 2050 struct sdw_stream_config *stream_config, 2051 const struct sdw_port_config *port_config, 2052 unsigned int num_ports, 2053 struct sdw_stream_runtime *stream) 2054 { 2055 struct sdw_slave_runtime *s_rt; 2056 struct sdw_master_runtime *m_rt; 2057 bool alloc_master_rt = false; 2058 bool alloc_slave_rt = false; 2059 2060 int ret; 2061 2062 mutex_lock(&slave->bus->bus_lock); 2063 2064 /* 2065 * check if Master is already allocated, if so skip allocation 2066 * and go to configuration 2067 */ 2068 m_rt = sdw_master_rt_find(slave->bus, stream); 2069 if (!m_rt) { 2070 /* 2071 * If this API is invoked by Slave first then m_rt is not valid. 2072 * So, allocate m_rt and add Slave to it. 2073 */ 2074 m_rt = sdw_master_rt_alloc(slave->bus, stream); 2075 if (!m_rt) { 2076 dev_err(&slave->dev, "%s: Master runtime alloc failed for stream:%s\n", 2077 __func__, stream->name); 2078 ret = -ENOMEM; 2079 goto unlock; 2080 } 2081 2082 alloc_master_rt = true; 2083 } 2084 2085 s_rt = sdw_slave_rt_find(slave, stream); 2086 if (!s_rt) { 2087 s_rt = sdw_slave_rt_alloc(slave, m_rt); 2088 if (!s_rt) { 2089 dev_err(&slave->dev, "Slave runtime alloc failed for stream:%s\n", 2090 stream->name); 2091 ret = -ENOMEM; 2092 goto alloc_error; 2093 } 2094 2095 alloc_slave_rt = true; 2096 } 2097 2098 if (!sdw_slave_port_allocated(s_rt)) { 2099 ret = sdw_slave_port_alloc(slave, s_rt, num_ports); 2100 if (ret) 2101 goto alloc_error; 2102 } 2103 2104 ret = sdw_master_rt_config(m_rt, stream_config); 2105 if (ret) 2106 goto unlock; 2107 2108 ret = sdw_slave_rt_config(s_rt, stream_config); 2109 if (ret) 2110 goto unlock; 2111 2112 ret = sdw_config_stream(&slave->dev, stream, stream_config, true); 2113 if (ret) 2114 goto unlock; 2115 2116 ret = sdw_slave_port_config(slave, s_rt, port_config); 2117 if (ret) 2118 goto unlock; 2119 2120 /* 2121 * Change stream state to CONFIGURED on first Slave add. 2122 * Bus is not aware of number of Slave(s) in a stream at this 2123 * point so cannot depend on all Slave(s) to be added in order to 2124 * change stream state to CONFIGURED. 2125 */ 2126 stream->state = SDW_STREAM_CONFIGURED; 2127 goto unlock; 2128 2129 alloc_error: 2130 /* 2131 * we only cleanup what was allocated in this routine. The 'else if' 2132 * is intentional, the 'master_rt_free' will call sdw_slave_rt_free() 2133 * internally. 2134 */ 2135 if (alloc_master_rt) 2136 sdw_master_rt_free(m_rt, stream); 2137 else if (alloc_slave_rt) 2138 sdw_slave_rt_free(slave, stream); 2139 unlock: 2140 mutex_unlock(&slave->bus->bus_lock); 2141 return ret; 2142 } 2143 EXPORT_SYMBOL(sdw_stream_add_slave); 2144 2145 /** 2146 * sdw_stream_remove_slave() - Remove slave from sdw_stream 2147 * 2148 * @slave: SDW Slave instance 2149 * @stream: SoundWire stream 2150 * 2151 * This removes and frees port_rt and slave_rt from a stream 2152 */ 2153 int sdw_stream_remove_slave(struct sdw_slave *slave, 2154 struct sdw_stream_runtime *stream) 2155 { 2156 mutex_lock(&slave->bus->bus_lock); 2157 2158 sdw_slave_port_free(slave, stream); 2159 sdw_slave_rt_free(slave, stream); 2160 2161 mutex_unlock(&slave->bus->bus_lock); 2162 2163 return 0; 2164 } 2165 EXPORT_SYMBOL(sdw_stream_remove_slave); 2166