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