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