1 // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2 // Copyright(c) 2015-18 Intel Corporation.
5 * stream.c - SoundWire Bus stream operations.
8 #include <linux/delay.h>
9 #include <linux/device.h>
10 #include <linux/init.h>
11 #include <linux/module.h>
12 #include <linux/mod_devicetable.h>
13 #include <linux/slab.h>
14 #include <linux/soundwire/sdw_registers.h>
15 #include <linux/soundwire/sdw.h>
16 #include <linux/soundwire/sdw_type.h>
17 #include <sound/soc.h>
21 * Array of supported rows and columns as per MIPI SoundWire Specification 1.1
23 * The rows are arranged as per the array index value programmed
24 * in register. The index 15 has dummy value 0 in order to fill hole.
26 int sdw_rows[SDW_FRAME_ROWS] = {48, 50, 60, 64, 75, 80, 125, 147,
27 96, 100, 120, 128, 150, 160, 250, 0,
28 192, 200, 240, 256, 72, 144, 90, 180};
29 EXPORT_SYMBOL(sdw_rows);
31 int sdw_cols[SDW_FRAME_COLS] = {2, 4, 6, 8, 10, 12, 14, 16};
32 EXPORT_SYMBOL(sdw_cols);
34 int sdw_find_col_index(int col)
38 for (i = 0; i < SDW_FRAME_COLS; i++) {
39 if (sdw_cols[i] == col)
43 pr_warn("Requested column not found, selecting lowest column no: 2\n");
46 EXPORT_SYMBOL(sdw_find_col_index);
48 int sdw_find_row_index(int row)
52 for (i = 0; i < SDW_FRAME_ROWS; i++) {
53 if (sdw_rows[i] == row)
57 pr_warn("Requested row not found, selecting lowest row no: 48\n");
60 EXPORT_SYMBOL(sdw_find_row_index);
62 static int _sdw_program_slave_port_params(struct sdw_bus *bus,
63 struct sdw_slave *slave,
64 struct sdw_transport_params *t_params,
65 enum sdw_dpn_type type)
67 u32 addr1, addr2, addr3, addr4;
71 if (bus->params.next_bank) {
72 addr1 = SDW_DPN_OFFSETCTRL2_B1(t_params->port_num);
73 addr2 = SDW_DPN_BLOCKCTRL3_B1(t_params->port_num);
74 addr3 = SDW_DPN_SAMPLECTRL2_B1(t_params->port_num);
75 addr4 = SDW_DPN_HCTRL_B1(t_params->port_num);
77 addr1 = SDW_DPN_OFFSETCTRL2_B0(t_params->port_num);
78 addr2 = SDW_DPN_BLOCKCTRL3_B0(t_params->port_num);
79 addr3 = SDW_DPN_SAMPLECTRL2_B0(t_params->port_num);
80 addr4 = SDW_DPN_HCTRL_B0(t_params->port_num);
83 /* Program DPN_OffsetCtrl2 registers */
84 ret = sdw_write_no_pm(slave, addr1, t_params->offset2);
86 dev_err(bus->dev, "DPN_OffsetCtrl2 register write failed\n");
90 /* Program DPN_BlockCtrl3 register */
91 ret = sdw_write_no_pm(slave, addr2, t_params->blk_pkg_mode);
93 dev_err(bus->dev, "DPN_BlockCtrl3 register write failed\n");
98 * Data ports are FULL, SIMPLE and REDUCED. This function handles
99 * FULL and REDUCED only and beyond this point only FULL is
100 * handled, so bail out if we are not FULL data port type
102 if (type != SDW_DPN_FULL)
105 /* Program DPN_SampleCtrl2 register */
106 wbuf = FIELD_GET(SDW_DPN_SAMPLECTRL_HIGH, t_params->sample_interval - 1);
108 ret = sdw_write_no_pm(slave, addr3, wbuf);
110 dev_err(bus->dev, "DPN_SampleCtrl2 register write failed\n");
114 /* Program DPN_HCtrl register */
115 wbuf = FIELD_PREP(SDW_DPN_HCTRL_HSTART, t_params->hstart);
116 wbuf |= FIELD_PREP(SDW_DPN_HCTRL_HSTOP, t_params->hstop);
118 ret = sdw_write_no_pm(slave, addr4, wbuf);
120 dev_err(bus->dev, "DPN_HCtrl register write failed\n");
125 static int sdw_program_slave_port_params(struct sdw_bus *bus,
126 struct sdw_slave_runtime *s_rt,
127 struct sdw_port_runtime *p_rt)
129 struct sdw_transport_params *t_params = &p_rt->transport_params;
130 struct sdw_port_params *p_params = &p_rt->port_params;
131 struct sdw_slave_prop *slave_prop = &s_rt->slave->prop;
132 u32 addr1, addr2, addr3, addr4, addr5, addr6;
133 struct sdw_dpn_prop *dpn_prop;
137 if (s_rt->slave->is_mockup_device)
140 dpn_prop = sdw_get_slave_dpn_prop(s_rt->slave,
146 addr1 = SDW_DPN_PORTCTRL(t_params->port_num);
147 addr2 = SDW_DPN_BLOCKCTRL1(t_params->port_num);
149 if (bus->params.next_bank) {
150 addr3 = SDW_DPN_SAMPLECTRL1_B1(t_params->port_num);
151 addr4 = SDW_DPN_OFFSETCTRL1_B1(t_params->port_num);
152 addr5 = SDW_DPN_BLOCKCTRL2_B1(t_params->port_num);
153 addr6 = SDW_DPN_LANECTRL_B1(t_params->port_num);
156 addr3 = SDW_DPN_SAMPLECTRL1_B0(t_params->port_num);
157 addr4 = SDW_DPN_OFFSETCTRL1_B0(t_params->port_num);
158 addr5 = SDW_DPN_BLOCKCTRL2_B0(t_params->port_num);
159 addr6 = SDW_DPN_LANECTRL_B0(t_params->port_num);
162 /* Program DPN_PortCtrl register */
163 wbuf = FIELD_PREP(SDW_DPN_PORTCTRL_DATAMODE, p_params->data_mode);
164 wbuf |= FIELD_PREP(SDW_DPN_PORTCTRL_FLOWMODE, p_params->flow_mode);
166 ret = sdw_update_no_pm(s_rt->slave, addr1, 0xF, wbuf);
168 dev_err(&s_rt->slave->dev,
169 "DPN_PortCtrl register write failed for port %d\n",
174 if (!dpn_prop->read_only_wordlength) {
175 /* Program DPN_BlockCtrl1 register */
176 ret = sdw_write_no_pm(s_rt->slave, addr2, (p_params->bps - 1));
178 dev_err(&s_rt->slave->dev,
179 "DPN_BlockCtrl1 register write failed for port %d\n",
185 /* Program DPN_SampleCtrl1 register */
186 wbuf = (t_params->sample_interval - 1) & SDW_DPN_SAMPLECTRL_LOW;
187 ret = sdw_write_no_pm(s_rt->slave, addr3, wbuf);
189 dev_err(&s_rt->slave->dev,
190 "DPN_SampleCtrl1 register write failed for port %d\n",
195 /* Program DPN_OffsetCtrl1 registers */
196 ret = sdw_write_no_pm(s_rt->slave, addr4, t_params->offset1);
198 dev_err(&s_rt->slave->dev,
199 "DPN_OffsetCtrl1 register write failed for port %d\n",
204 /* Program DPN_BlockCtrl2 register*/
205 if (t_params->blk_grp_ctrl_valid) {
206 ret = sdw_write_no_pm(s_rt->slave, addr5, t_params->blk_grp_ctrl);
208 dev_err(&s_rt->slave->dev,
209 "DPN_BlockCtrl2 reg write failed for port %d\n",
215 /* program DPN_LaneCtrl register */
216 if (slave_prop->lane_control_support) {
217 ret = sdw_write_no_pm(s_rt->slave, addr6, t_params->lane_ctrl);
219 dev_err(&s_rt->slave->dev,
220 "DPN_LaneCtrl register write failed for port %d\n",
226 if (dpn_prop->type != SDW_DPN_SIMPLE) {
227 ret = _sdw_program_slave_port_params(bus, s_rt->slave,
228 t_params, dpn_prop->type);
230 dev_err(&s_rt->slave->dev,
231 "Transport reg write failed for port: %d\n",
238 static int sdw_program_master_port_params(struct sdw_bus *bus,
239 struct sdw_port_runtime *p_rt)
244 * we need to set transport and port parameters for the port.
245 * Transport parameters refers to the sample interval, offsets and
246 * hstart/stop etc of the data. Port parameters refers to word
247 * length, flow mode etc of the port
249 ret = bus->port_ops->dpn_set_port_transport_params(bus,
250 &p_rt->transport_params,
251 bus->params.next_bank);
255 return bus->port_ops->dpn_set_port_params(bus,
257 bus->params.next_bank);
261 * sdw_program_port_params() - Programs transport parameters of Master(s)
264 * @m_rt: Master stream runtime
266 static int sdw_program_port_params(struct sdw_master_runtime *m_rt)
268 struct sdw_slave_runtime *s_rt;
269 struct sdw_bus *bus = m_rt->bus;
270 struct sdw_port_runtime *p_rt;
273 /* Program transport & port parameters for Slave(s) */
274 list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) {
275 list_for_each_entry(p_rt, &s_rt->port_list, port_node) {
276 ret = sdw_program_slave_port_params(bus, s_rt, p_rt);
282 /* Program transport & port parameters for Master(s) */
283 list_for_each_entry(p_rt, &m_rt->port_list, port_node) {
284 ret = sdw_program_master_port_params(bus, p_rt);
293 * sdw_enable_disable_slave_ports: Enable/disable slave data port
296 * @s_rt: slave runtime
297 * @p_rt: port runtime
298 * @en: enable or disable operation
300 * This function only sets the enable/disable bits in the relevant bank, the
301 * actual enable/disable is done with a bank switch
303 static int sdw_enable_disable_slave_ports(struct sdw_bus *bus,
304 struct sdw_slave_runtime *s_rt,
305 struct sdw_port_runtime *p_rt,
308 struct sdw_transport_params *t_params = &p_rt->transport_params;
312 if (bus->params.next_bank)
313 addr = SDW_DPN_CHANNELEN_B1(p_rt->num);
315 addr = SDW_DPN_CHANNELEN_B0(p_rt->num);
318 * Since bus doesn't support sharing a port across two streams,
319 * it is safe to reset this register
322 ret = sdw_write_no_pm(s_rt->slave, addr, p_rt->ch_mask);
324 ret = sdw_write_no_pm(s_rt->slave, addr, 0x0);
327 dev_err(&s_rt->slave->dev,
328 "Slave chn_en reg write failed:%d port:%d\n",
329 ret, t_params->port_num);
334 static int sdw_enable_disable_master_ports(struct sdw_master_runtime *m_rt,
335 struct sdw_port_runtime *p_rt,
338 struct sdw_transport_params *t_params = &p_rt->transport_params;
339 struct sdw_bus *bus = m_rt->bus;
340 struct sdw_enable_ch enable_ch;
343 enable_ch.port_num = p_rt->num;
344 enable_ch.ch_mask = p_rt->ch_mask;
345 enable_ch.enable = en;
347 /* Perform Master port channel(s) enable/disable */
348 if (bus->port_ops->dpn_port_enable_ch) {
349 ret = bus->port_ops->dpn_port_enable_ch(bus,
351 bus->params.next_bank);
354 "Master chn_en write failed:%d port:%d\n",
355 ret, t_params->port_num);
360 "dpn_port_enable_ch not supported, %s failed\n",
361 en ? "enable" : "disable");
369 * sdw_enable_disable_ports() - Enable/disable port(s) for Master and
372 * @m_rt: Master stream runtime
373 * @en: mode (enable/disable)
375 static int sdw_enable_disable_ports(struct sdw_master_runtime *m_rt, bool en)
377 struct sdw_port_runtime *s_port, *m_port;
378 struct sdw_slave_runtime *s_rt;
381 /* Enable/Disable Slave port(s) */
382 list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) {
383 list_for_each_entry(s_port, &s_rt->port_list, port_node) {
384 ret = sdw_enable_disable_slave_ports(m_rt->bus, s_rt,
391 /* Enable/Disable Master port(s) */
392 list_for_each_entry(m_port, &m_rt->port_list, port_node) {
393 ret = sdw_enable_disable_master_ports(m_rt, m_port, en);
401 static int sdw_do_port_prep(struct sdw_slave_runtime *s_rt,
402 struct sdw_prepare_ch prep_ch,
403 enum sdw_port_prep_ops cmd)
406 struct sdw_slave *slave = s_rt->slave;
408 mutex_lock(&slave->sdw_dev_lock);
411 struct device *dev = &slave->dev;
412 struct sdw_driver *drv = drv_to_sdw_driver(dev->driver);
414 if (drv->ops && drv->ops->port_prep) {
415 ret = drv->ops->port_prep(slave, &prep_ch, cmd);
417 dev_err(dev, "Slave Port Prep cmd %d failed: %d\n",
422 mutex_unlock(&slave->sdw_dev_lock);
427 static int sdw_prep_deprep_slave_ports(struct sdw_bus *bus,
428 struct sdw_slave_runtime *s_rt,
429 struct sdw_port_runtime *p_rt,
432 struct completion *port_ready;
433 struct sdw_dpn_prop *dpn_prop;
434 struct sdw_prepare_ch prep_ch;
439 prep_ch.num = p_rt->num;
440 prep_ch.ch_mask = p_rt->ch_mask;
442 dpn_prop = sdw_get_slave_dpn_prop(s_rt->slave,
447 "Slave Port:%d properties not found\n", prep_ch.num);
451 prep_ch.prepare = prep;
453 prep_ch.bank = bus->params.next_bank;
455 if (dpn_prop->imp_def_interrupts || !dpn_prop->simple_ch_prep_sm ||
456 bus->params.s_data_mode != SDW_PORT_DATA_MODE_NORMAL)
460 * Enable interrupt before Port prepare.
461 * For Port de-prepare, it is assumed that port
462 * was prepared earlier
465 ret = sdw_configure_dpn_intr(s_rt->slave, p_rt->num, prep,
466 dpn_prop->imp_def_interrupts);
471 /* Inform slave about the impending port prepare */
472 sdw_do_port_prep(s_rt, prep_ch, prep ? SDW_OPS_PORT_PRE_PREP : SDW_OPS_PORT_PRE_DEPREP);
474 /* Prepare Slave port implementing CP_SM */
475 if (!dpn_prop->simple_ch_prep_sm) {
476 addr = SDW_DPN_PREPARECTRL(p_rt->num);
479 ret = sdw_write_no_pm(s_rt->slave, addr, p_rt->ch_mask);
481 ret = sdw_write_no_pm(s_rt->slave, addr, 0x0);
484 dev_err(&s_rt->slave->dev,
485 "Slave prep_ctrl reg write failed\n");
489 /* Wait for completion on port ready */
490 port_ready = &s_rt->slave->port_ready[prep_ch.num];
491 wait_for_completion_timeout(port_ready,
492 msecs_to_jiffies(dpn_prop->ch_prep_timeout));
494 val = sdw_read_no_pm(s_rt->slave, SDW_DPN_PREPARESTATUS(p_rt->num));
495 if ((val < 0) || (val & p_rt->ch_mask)) {
496 ret = (val < 0) ? val : -ETIMEDOUT;
497 dev_err(&s_rt->slave->dev,
498 "Chn prep failed for port %d: %d\n", prep_ch.num, ret);
503 /* Inform slaves about ports prepared */
504 sdw_do_port_prep(s_rt, prep_ch, prep ? SDW_OPS_PORT_POST_PREP : SDW_OPS_PORT_POST_DEPREP);
506 /* Disable interrupt after Port de-prepare */
508 ret = sdw_configure_dpn_intr(s_rt->slave, p_rt->num, prep,
509 dpn_prop->imp_def_interrupts);
514 static int sdw_prep_deprep_master_ports(struct sdw_master_runtime *m_rt,
515 struct sdw_port_runtime *p_rt,
518 struct sdw_transport_params *t_params = &p_rt->transport_params;
519 struct sdw_bus *bus = m_rt->bus;
520 const struct sdw_master_port_ops *ops = bus->port_ops;
521 struct sdw_prepare_ch prep_ch;
524 prep_ch.num = p_rt->num;
525 prep_ch.ch_mask = p_rt->ch_mask;
526 prep_ch.prepare = prep; /* Prepare/De-prepare */
527 prep_ch.bank = bus->params.next_bank;
529 /* Pre-prepare/Pre-deprepare port(s) */
530 if (ops->dpn_port_prep) {
531 ret = ops->dpn_port_prep(bus, &prep_ch);
533 dev_err(bus->dev, "Port prepare failed for port:%d\n",
543 * sdw_prep_deprep_ports() - Prepare/De-prepare port(s) for Master(s) and
546 * @m_rt: Master runtime handle
547 * @prep: Prepare or De-prepare
549 static int sdw_prep_deprep_ports(struct sdw_master_runtime *m_rt, bool prep)
551 struct sdw_slave_runtime *s_rt;
552 struct sdw_port_runtime *p_rt;
555 /* Prepare/De-prepare Slave port(s) */
556 list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) {
557 list_for_each_entry(p_rt, &s_rt->port_list, port_node) {
558 ret = sdw_prep_deprep_slave_ports(m_rt->bus, s_rt,
565 /* Prepare/De-prepare Master port(s) */
566 list_for_each_entry(p_rt, &m_rt->port_list, port_node) {
567 ret = sdw_prep_deprep_master_ports(m_rt, p_rt, prep);
576 * sdw_notify_config() - Notify bus configuration
578 * @m_rt: Master runtime handle
580 * This function notifies the Master(s) and Slave(s) of the
581 * new bus configuration.
583 static int sdw_notify_config(struct sdw_master_runtime *m_rt)
585 struct sdw_slave_runtime *s_rt;
586 struct sdw_bus *bus = m_rt->bus;
587 struct sdw_slave *slave;
590 if (bus->ops->set_bus_conf) {
591 ret = bus->ops->set_bus_conf(bus, &bus->params);
596 list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) {
599 mutex_lock(&slave->sdw_dev_lock);
602 struct device *dev = &slave->dev;
603 struct sdw_driver *drv = drv_to_sdw_driver(dev->driver);
605 if (drv->ops && drv->ops->bus_config) {
606 ret = drv->ops->bus_config(slave, &bus->params);
608 dev_err(dev, "Notify Slave: %d failed\n",
610 mutex_unlock(&slave->sdw_dev_lock);
616 mutex_unlock(&slave->sdw_dev_lock);
623 * sdw_program_params() - Program transport and port parameters for Master(s)
626 * @bus: SDW bus instance
627 * @prepare: true if sdw_program_params() is called by _prepare.
629 static int sdw_program_params(struct sdw_bus *bus, bool prepare)
631 struct sdw_master_runtime *m_rt;
634 list_for_each_entry(m_rt, &bus->m_rt_list, bus_node) {
637 * this loop walks through all master runtimes for a
638 * bus, but the ports can only be configured while
639 * explicitly preparing a stream or handling an
640 * already-prepared stream otherwise.
643 m_rt->stream->state == SDW_STREAM_CONFIGURED)
646 ret = sdw_program_port_params(m_rt);
649 "Program transport params failed: %d\n", ret);
653 ret = sdw_notify_config(m_rt);
656 "Notify bus config failed: %d\n", ret);
660 /* Enable port(s) on alternate bank for all active streams */
661 if (m_rt->stream->state != SDW_STREAM_ENABLED)
664 ret = sdw_enable_disable_ports(m_rt, true);
666 dev_err(bus->dev, "Enable channel failed: %d\n", ret);
674 static int sdw_bank_switch(struct sdw_bus *bus, int m_rt_count)
676 int col_index, row_index;
678 struct sdw_msg *wr_msg;
683 wr_msg = kzalloc(sizeof(*wr_msg), GFP_KERNEL);
687 wbuf = kzalloc(sizeof(*wbuf), GFP_KERNEL);
693 /* Get row and column index to program register */
694 col_index = sdw_find_col_index(bus->params.col);
695 row_index = sdw_find_row_index(bus->params.row);
696 wbuf[0] = col_index | (row_index << 3);
698 if (bus->params.next_bank)
699 addr = SDW_SCP_FRAMECTRL_B1;
701 addr = SDW_SCP_FRAMECTRL_B0;
703 sdw_fill_msg(wr_msg, NULL, addr, 1, SDW_BROADCAST_DEV_NUM,
704 SDW_MSG_FLAG_WRITE, wbuf);
705 wr_msg->ssp_sync = true;
708 * Set the multi_link flag only when both the hardware supports
709 * and hardware-based sync is required
711 multi_link = bus->multi_link && (m_rt_count >= bus->hw_sync_min_links);
714 ret = sdw_transfer_defer(bus, wr_msg);
716 ret = sdw_transfer(bus, wr_msg);
718 if (ret < 0 && ret != -ENODATA) {
719 dev_err(bus->dev, "Slave frame_ctrl reg write failed\n");
726 bus->defer_msg.msg = NULL;
727 bus->params.curr_bank = !bus->params.curr_bank;
728 bus->params.next_bank = !bus->params.next_bank;
737 bus->defer_msg.msg = NULL;
742 * sdw_ml_sync_bank_switch: Multilink register bank switch
744 * @bus: SDW bus instance
746 * Caller function should free the buffers on error
748 static int sdw_ml_sync_bank_switch(struct sdw_bus *bus)
750 unsigned long time_left;
752 if (!bus->multi_link)
755 /* Wait for completion of transfer */
756 time_left = wait_for_completion_timeout(&bus->defer_msg.complete,
757 bus->bank_switch_timeout);
760 dev_err(bus->dev, "Controller Timed out on bank switch\n");
764 bus->params.curr_bank = !bus->params.curr_bank;
765 bus->params.next_bank = !bus->params.next_bank;
767 if (bus->defer_msg.msg) {
768 kfree(bus->defer_msg.msg->buf);
769 kfree(bus->defer_msg.msg);
770 bus->defer_msg.msg = NULL;
776 static int do_bank_switch(struct sdw_stream_runtime *stream)
778 struct sdw_master_runtime *m_rt;
779 const struct sdw_master_ops *ops;
781 bool multi_link = false;
785 m_rt_count = stream->m_rt_count;
787 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
791 if (bus->multi_link && m_rt_count >= bus->hw_sync_min_links) {
793 mutex_lock(&bus->msg_lock);
796 /* Pre-bank switch */
797 if (ops->pre_bank_switch) {
798 ret = ops->pre_bank_switch(bus);
801 "Pre bank switch op failed: %d\n", ret);
807 * Perform Bank switch operation.
808 * For multi link cases, the actual bank switch is
809 * synchronized across all Masters and happens later as a
810 * part of post_bank_switch ops.
812 ret = sdw_bank_switch(bus, m_rt_count);
814 dev_err(bus->dev, "Bank switch failed: %d\n", ret);
820 * For multi link cases, it is expected that the bank switch is
821 * triggered by the post_bank_switch for the first Master in the list
822 * and for the other Masters the post_bank_switch() should return doing
825 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
829 /* Post-bank switch */
830 if (ops->post_bank_switch) {
831 ret = ops->post_bank_switch(bus);
834 "Post bank switch op failed: %d\n",
838 } else if (multi_link) {
840 "Post bank switch ops not implemented\n");
845 /* Set the bank switch timeout to default, if not set */
846 if (!bus->bank_switch_timeout)
847 bus->bank_switch_timeout = DEFAULT_BANK_SWITCH_TIMEOUT;
849 /* Check if bank switch was successful */
850 ret = sdw_ml_sync_bank_switch(bus);
853 "multi link bank switch failed: %d\n", ret);
858 mutex_unlock(&bus->msg_lock);
864 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
866 if (bus->defer_msg.msg) {
867 kfree(bus->defer_msg.msg->buf);
868 kfree(bus->defer_msg.msg);
869 bus->defer_msg.msg = NULL;
876 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
878 if (mutex_is_locked(&bus->msg_lock))
879 mutex_unlock(&bus->msg_lock);
886 static struct sdw_port_runtime *sdw_port_alloc(struct list_head *port_list)
888 struct sdw_port_runtime *p_rt;
890 p_rt = kzalloc(sizeof(*p_rt), GFP_KERNEL);
894 list_add_tail(&p_rt->port_node, port_list);
899 static int sdw_port_config(struct sdw_port_runtime *p_rt,
900 struct sdw_port_config *port_config,
903 p_rt->ch_mask = port_config[port_index].ch_mask;
904 p_rt->num = port_config[port_index].num;
907 * TODO: Check port capabilities for requested configuration
913 static void sdw_port_free(struct sdw_port_runtime *p_rt)
915 list_del(&p_rt->port_node);
919 static bool sdw_slave_port_allocated(struct sdw_slave_runtime *s_rt)
921 return !list_empty(&s_rt->port_list);
924 static void sdw_slave_port_free(struct sdw_slave *slave,
925 struct sdw_stream_runtime *stream)
927 struct sdw_port_runtime *p_rt, *_p_rt;
928 struct sdw_master_runtime *m_rt;
929 struct sdw_slave_runtime *s_rt;
931 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
932 list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) {
933 if (s_rt->slave != slave)
936 list_for_each_entry_safe(p_rt, _p_rt,
937 &s_rt->port_list, port_node) {
944 static int sdw_slave_port_alloc(struct sdw_slave *slave,
945 struct sdw_slave_runtime *s_rt,
946 unsigned int num_config)
948 struct sdw_port_runtime *p_rt;
951 /* Iterate for number of ports to perform initialization */
952 for (i = 0; i < num_config; i++) {
953 p_rt = sdw_port_alloc(&s_rt->port_list);
961 static int sdw_slave_port_is_valid_range(struct device *dev, int num)
963 if (!SDW_VALID_PORT_RANGE(num)) {
964 dev_err(dev, "SoundWire: Invalid port number :%d\n", num);
971 static int sdw_slave_port_config(struct sdw_slave *slave,
972 struct sdw_slave_runtime *s_rt,
973 struct sdw_port_config *port_config)
975 struct sdw_port_runtime *p_rt;
980 list_for_each_entry(p_rt, &s_rt->port_list, port_node) {
982 * TODO: Check valid port range as defined by DisCo/
985 ret = sdw_slave_port_is_valid_range(&slave->dev, port_config[i].num);
989 ret = sdw_port_config(p_rt, port_config, i);
998 static bool sdw_master_port_allocated(struct sdw_master_runtime *m_rt)
1000 return !list_empty(&m_rt->port_list);
1003 static void sdw_master_port_free(struct sdw_master_runtime *m_rt)
1005 struct sdw_port_runtime *p_rt, *_p_rt;
1007 list_for_each_entry_safe(p_rt, _p_rt, &m_rt->port_list, port_node) {
1008 sdw_port_free(p_rt);
1012 static int sdw_master_port_alloc(struct sdw_master_runtime *m_rt,
1013 unsigned int num_ports)
1015 struct sdw_port_runtime *p_rt;
1018 /* Iterate for number of ports to perform initialization */
1019 for (i = 0; i < num_ports; i++) {
1020 p_rt = sdw_port_alloc(&m_rt->port_list);
1028 static int sdw_master_port_config(struct sdw_master_runtime *m_rt,
1029 struct sdw_port_config *port_config)
1031 struct sdw_port_runtime *p_rt;
1036 list_for_each_entry(p_rt, &m_rt->port_list, port_node) {
1037 ret = sdw_port_config(p_rt, port_config, i);
1047 * sdw_slave_rt_alloc() - Allocate a Slave runtime handle.
1049 * @slave: Slave handle
1050 * @m_rt: Master runtime handle
1052 * This function is to be called with bus_lock held.
1054 static struct sdw_slave_runtime
1055 *sdw_slave_rt_alloc(struct sdw_slave *slave,
1056 struct sdw_master_runtime *m_rt)
1058 struct sdw_slave_runtime *s_rt;
1060 s_rt = kzalloc(sizeof(*s_rt), GFP_KERNEL);
1064 INIT_LIST_HEAD(&s_rt->port_list);
1065 s_rt->slave = slave;
1067 list_add_tail(&s_rt->m_rt_node, &m_rt->slave_rt_list);
1073 * sdw_slave_rt_config() - Configure a Slave runtime handle.
1075 * @s_rt: Slave runtime handle
1076 * @stream_config: Stream configuration
1078 * This function is to be called with bus_lock held.
1080 static int sdw_slave_rt_config(struct sdw_slave_runtime *s_rt,
1081 struct sdw_stream_config *stream_config)
1083 s_rt->ch_count = stream_config->ch_count;
1084 s_rt->direction = stream_config->direction;
1089 static struct sdw_slave_runtime *sdw_slave_rt_find(struct sdw_slave *slave,
1090 struct sdw_stream_runtime *stream)
1092 struct sdw_slave_runtime *s_rt, *_s_rt;
1093 struct sdw_master_runtime *m_rt;
1095 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
1096 /* Retrieve Slave runtime handle */
1097 list_for_each_entry_safe(s_rt, _s_rt,
1098 &m_rt->slave_rt_list, m_rt_node) {
1099 if (s_rt->slave == slave)
1107 * sdw_slave_rt_free() - Free Slave(s) runtime handle
1109 * @slave: Slave handle.
1110 * @stream: Stream runtime handle.
1112 * This function is to be called with bus_lock held.
1114 static void sdw_slave_rt_free(struct sdw_slave *slave,
1115 struct sdw_stream_runtime *stream)
1117 struct sdw_slave_runtime *s_rt;
1119 s_rt = sdw_slave_rt_find(slave, stream);
1121 list_del(&s_rt->m_rt_node);
1126 static struct sdw_master_runtime
1127 *sdw_master_rt_find(struct sdw_bus *bus,
1128 struct sdw_stream_runtime *stream)
1130 struct sdw_master_runtime *m_rt;
1132 /* Retrieve Bus handle if already available */
1133 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
1134 if (m_rt->bus == bus)
1142 * sdw_master_rt_alloc() - Allocates a Master runtime handle
1144 * @bus: SDW bus instance
1145 * @stream: Stream runtime handle.
1147 * This function is to be called with bus_lock held.
1149 static struct sdw_master_runtime
1150 *sdw_master_rt_alloc(struct sdw_bus *bus,
1151 struct sdw_stream_runtime *stream)
1153 struct sdw_master_runtime *m_rt, *walk_m_rt;
1154 struct list_head *insert_after;
1156 m_rt = kzalloc(sizeof(*m_rt), GFP_KERNEL);
1160 /* Initialization of Master runtime handle */
1161 INIT_LIST_HEAD(&m_rt->port_list);
1162 INIT_LIST_HEAD(&m_rt->slave_rt_list);
1165 * Add in order of bus id so that when taking the bus_lock
1166 * of multiple buses they will always be taken in the same
1167 * order to prevent a mutex deadlock.
1169 insert_after = &stream->master_list;
1170 list_for_each_entry_reverse(walk_m_rt, &stream->master_list, stream_node) {
1171 if (walk_m_rt->bus->id < bus->id) {
1172 insert_after = &walk_m_rt->stream_node;
1176 list_add(&m_rt->stream_node, insert_after);
1178 list_add_tail(&m_rt->bus_node, &bus->m_rt_list);
1181 m_rt->stream = stream;
1187 * sdw_master_rt_config() - Configure Master runtime handle
1189 * @m_rt: Master runtime handle
1190 * @stream_config: Stream configuration
1192 * This function is to be called with bus_lock held.
1195 static int sdw_master_rt_config(struct sdw_master_runtime *m_rt,
1196 struct sdw_stream_config *stream_config)
1198 m_rt->ch_count = stream_config->ch_count;
1199 m_rt->direction = stream_config->direction;
1205 * sdw_master_rt_free() - Free Master runtime handle
1207 * @m_rt: Master runtime node
1208 * @stream: Stream runtime handle.
1210 * This function is to be called with bus_lock held
1211 * It frees the Master runtime handle and associated Slave(s) runtime
1212 * handle. If this is called first then sdw_slave_rt_free() will have
1213 * no effect as Slave(s) runtime handle would already be freed up.
1215 static void sdw_master_rt_free(struct sdw_master_runtime *m_rt,
1216 struct sdw_stream_runtime *stream)
1218 struct sdw_slave_runtime *s_rt, *_s_rt;
1220 list_for_each_entry_safe(s_rt, _s_rt, &m_rt->slave_rt_list, m_rt_node) {
1221 sdw_slave_port_free(s_rt->slave, stream);
1222 sdw_slave_rt_free(s_rt->slave, stream);
1225 list_del(&m_rt->stream_node);
1226 list_del(&m_rt->bus_node);
1231 * sdw_config_stream() - Configure the allocated stream
1234 * @stream: SoundWire stream
1235 * @stream_config: Stream configuration for audio stream
1236 * @is_slave: is API called from Slave or Master
1238 * This function is to be called with bus_lock held.
1240 static int sdw_config_stream(struct device *dev,
1241 struct sdw_stream_runtime *stream,
1242 struct sdw_stream_config *stream_config,
1246 * Update the stream rate, channel and bps based on data
1247 * source. For more than one data source (multilink),
1248 * match the rate, bps, stream type and increment number of channels.
1250 * If rate/bps is zero, it means the values are not set, so skip
1251 * comparison and allow the value to be set and stored in stream
1253 if (stream->params.rate &&
1254 stream->params.rate != stream_config->frame_rate) {
1255 dev_err(dev, "rate not matching, stream:%s\n", stream->name);
1259 if (stream->params.bps &&
1260 stream->params.bps != stream_config->bps) {
1261 dev_err(dev, "bps not matching, stream:%s\n", stream->name);
1265 stream->type = stream_config->type;
1266 stream->params.rate = stream_config->frame_rate;
1267 stream->params.bps = stream_config->bps;
1269 /* TODO: Update this check during Device-device support */
1271 stream->params.ch_count += stream_config->ch_count;
1277 * sdw_get_slave_dpn_prop() - Get Slave port capabilities
1279 * @slave: Slave handle
1280 * @direction: Data direction.
1281 * @port_num: Port number
1283 struct sdw_dpn_prop *sdw_get_slave_dpn_prop(struct sdw_slave *slave,
1284 enum sdw_data_direction direction,
1285 unsigned int port_num)
1287 struct sdw_dpn_prop *dpn_prop;
1291 if (direction == SDW_DATA_DIR_TX) {
1292 num_ports = hweight32(slave->prop.source_ports);
1293 dpn_prop = slave->prop.src_dpn_prop;
1295 num_ports = hweight32(slave->prop.sink_ports);
1296 dpn_prop = slave->prop.sink_dpn_prop;
1299 for (i = 0; i < num_ports; i++) {
1300 if (dpn_prop[i].num == port_num)
1301 return &dpn_prop[i];
1308 * sdw_acquire_bus_lock: Acquire bus lock for all Master runtime(s)
1310 * @stream: SoundWire stream
1312 * Acquire bus_lock for each of the master runtime(m_rt) part of this
1313 * stream to reconfigure the bus.
1314 * NOTE: This function is called from SoundWire stream ops and is
1315 * expected that a global lock is held before acquiring bus_lock.
1317 static void sdw_acquire_bus_lock(struct sdw_stream_runtime *stream)
1319 struct sdw_master_runtime *m_rt;
1320 struct sdw_bus *bus;
1322 /* Iterate for all Master(s) in Master list */
1323 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
1326 mutex_lock(&bus->bus_lock);
1331 * sdw_release_bus_lock: Release bus lock for all Master runtime(s)
1333 * @stream: SoundWire stream
1335 * Release the previously held bus_lock after reconfiguring the bus.
1336 * NOTE: This function is called from SoundWire stream ops and is
1337 * expected that a global lock is held before releasing bus_lock.
1339 static void sdw_release_bus_lock(struct sdw_stream_runtime *stream)
1341 struct sdw_master_runtime *m_rt;
1342 struct sdw_bus *bus;
1344 /* Iterate for all Master(s) in Master list */
1345 list_for_each_entry_reverse(m_rt, &stream->master_list, stream_node) {
1347 mutex_unlock(&bus->bus_lock);
1351 static int _sdw_prepare_stream(struct sdw_stream_runtime *stream,
1354 struct sdw_master_runtime *m_rt;
1355 struct sdw_bus *bus;
1356 struct sdw_master_prop *prop;
1357 struct sdw_bus_params params;
1360 /* Prepare Master(s) and Slave(s) port(s) associated with stream */
1361 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
1364 memcpy(¶ms, &bus->params, sizeof(params));
1366 /* TODO: Support Asynchronous mode */
1367 if ((prop->max_clk_freq % stream->params.rate) != 0) {
1368 dev_err(bus->dev, "Async mode not supported\n");
1372 if (update_params) {
1373 /* Increment cumulative bus bandwidth */
1374 /* TODO: Update this during Device-Device support */
1375 bus->params.bandwidth += m_rt->stream->params.rate *
1376 m_rt->ch_count * m_rt->stream->params.bps;
1378 /* Compute params */
1379 if (bus->compute_params) {
1380 ret = bus->compute_params(bus);
1382 dev_err(bus->dev, "Compute params failed: %d\n",
1384 goto restore_params;
1389 /* Program params */
1390 ret = sdw_program_params(bus, true);
1392 dev_err(bus->dev, "Program params failed: %d\n", ret);
1393 goto restore_params;
1397 ret = do_bank_switch(stream);
1399 pr_err("%s: do_bank_switch failed: %d\n", __func__, ret);
1400 goto restore_params;
1403 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
1406 /* Prepare port(s) on the new clock configuration */
1407 ret = sdw_prep_deprep_ports(m_rt, true);
1409 dev_err(bus->dev, "Prepare port(s) failed ret = %d\n",
1415 stream->state = SDW_STREAM_PREPARED;
1420 memcpy(&bus->params, ¶ms, sizeof(params));
1425 * sdw_prepare_stream() - Prepare SoundWire stream
1427 * @stream: Soundwire stream
1429 * Documentation/driver-api/soundwire/stream.rst explains this API in detail
1431 int sdw_prepare_stream(struct sdw_stream_runtime *stream)
1433 bool update_params = true;
1437 pr_err("SoundWire: Handle not found for stream\n");
1441 sdw_acquire_bus_lock(stream);
1443 if (stream->state == SDW_STREAM_PREPARED) {
1448 if (stream->state != SDW_STREAM_CONFIGURED &&
1449 stream->state != SDW_STREAM_DEPREPARED &&
1450 stream->state != SDW_STREAM_DISABLED) {
1451 pr_err("%s: %s: inconsistent state state %d\n",
1452 __func__, stream->name, stream->state);
1458 * when the stream is DISABLED, this means sdw_prepare_stream()
1459 * is called as a result of an underflow or a resume operation.
1460 * In this case, the bus parameters shall not be recomputed, but
1461 * still need to be re-applied
1463 if (stream->state == SDW_STREAM_DISABLED)
1464 update_params = false;
1466 ret = _sdw_prepare_stream(stream, update_params);
1469 sdw_release_bus_lock(stream);
1472 EXPORT_SYMBOL(sdw_prepare_stream);
1474 static int _sdw_enable_stream(struct sdw_stream_runtime *stream)
1476 struct sdw_master_runtime *m_rt;
1477 struct sdw_bus *bus;
1480 /* Enable Master(s) and Slave(s) port(s) associated with stream */
1481 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
1484 /* Program params */
1485 ret = sdw_program_params(bus, false);
1487 dev_err(bus->dev, "%s: Program params failed: %d\n", __func__, ret);
1491 /* Enable port(s) */
1492 ret = sdw_enable_disable_ports(m_rt, true);
1495 "Enable port(s) failed ret: %d\n", ret);
1500 ret = do_bank_switch(stream);
1502 pr_err("%s: do_bank_switch failed: %d\n", __func__, ret);
1506 stream->state = SDW_STREAM_ENABLED;
1511 * sdw_enable_stream() - Enable SoundWire stream
1513 * @stream: Soundwire stream
1515 * Documentation/driver-api/soundwire/stream.rst explains this API in detail
1517 int sdw_enable_stream(struct sdw_stream_runtime *stream)
1522 pr_err("SoundWire: Handle not found for stream\n");
1526 sdw_acquire_bus_lock(stream);
1528 if (stream->state == SDW_STREAM_ENABLED) {
1533 if (stream->state != SDW_STREAM_PREPARED &&
1534 stream->state != SDW_STREAM_DISABLED) {
1535 pr_err("%s: %s: inconsistent state state %d\n",
1536 __func__, stream->name, stream->state);
1541 ret = _sdw_enable_stream(stream);
1544 sdw_release_bus_lock(stream);
1547 EXPORT_SYMBOL(sdw_enable_stream);
1549 static int _sdw_disable_stream(struct sdw_stream_runtime *stream)
1551 struct sdw_master_runtime *m_rt;
1554 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
1555 struct sdw_bus *bus = m_rt->bus;
1557 /* Disable port(s) */
1558 ret = sdw_enable_disable_ports(m_rt, false);
1560 dev_err(bus->dev, "Disable port(s) failed: %d\n", ret);
1564 stream->state = SDW_STREAM_DISABLED;
1566 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
1567 struct sdw_bus *bus = m_rt->bus;
1569 /* Program params */
1570 ret = sdw_program_params(bus, false);
1572 dev_err(bus->dev, "%s: Program params failed: %d\n", __func__, ret);
1577 ret = do_bank_switch(stream);
1579 pr_err("%s: do_bank_switch failed: %d\n", __func__, ret);
1583 /* make sure alternate bank (previous current) is also disabled */
1584 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
1585 struct sdw_bus *bus = m_rt->bus;
1587 /* Disable port(s) */
1588 ret = sdw_enable_disable_ports(m_rt, false);
1590 dev_err(bus->dev, "Disable port(s) failed: %d\n", ret);
1599 * sdw_disable_stream() - Disable SoundWire stream
1601 * @stream: Soundwire stream
1603 * Documentation/driver-api/soundwire/stream.rst explains this API in detail
1605 int sdw_disable_stream(struct sdw_stream_runtime *stream)
1610 pr_err("SoundWire: Handle not found for stream\n");
1614 sdw_acquire_bus_lock(stream);
1616 if (stream->state == SDW_STREAM_DISABLED) {
1621 if (stream->state != SDW_STREAM_ENABLED) {
1622 pr_err("%s: %s: inconsistent state state %d\n",
1623 __func__, stream->name, stream->state);
1628 ret = _sdw_disable_stream(stream);
1631 sdw_release_bus_lock(stream);
1634 EXPORT_SYMBOL(sdw_disable_stream);
1636 static int _sdw_deprepare_stream(struct sdw_stream_runtime *stream)
1638 struct sdw_master_runtime *m_rt;
1639 struct sdw_bus *bus;
1642 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
1644 /* De-prepare port(s) */
1645 ret = sdw_prep_deprep_ports(m_rt, false);
1648 "De-prepare port(s) failed: %d\n", ret);
1652 /* TODO: Update this during Device-Device support */
1653 bus->params.bandwidth -= m_rt->stream->params.rate *
1654 m_rt->ch_count * m_rt->stream->params.bps;
1656 /* Compute params */
1657 if (bus->compute_params) {
1658 ret = bus->compute_params(bus);
1660 dev_err(bus->dev, "Compute params failed: %d\n",
1666 /* Program params */
1667 ret = sdw_program_params(bus, false);
1669 dev_err(bus->dev, "%s: Program params failed: %d\n", __func__, ret);
1674 stream->state = SDW_STREAM_DEPREPARED;
1675 return do_bank_switch(stream);
1679 * sdw_deprepare_stream() - Deprepare SoundWire stream
1681 * @stream: Soundwire stream
1683 * Documentation/driver-api/soundwire/stream.rst explains this API in detail
1685 int sdw_deprepare_stream(struct sdw_stream_runtime *stream)
1690 pr_err("SoundWire: Handle not found for stream\n");
1694 sdw_acquire_bus_lock(stream);
1696 if (stream->state == SDW_STREAM_DEPREPARED) {
1701 if (stream->state != SDW_STREAM_PREPARED &&
1702 stream->state != SDW_STREAM_DISABLED) {
1703 pr_err("%s: %s: inconsistent state state %d\n",
1704 __func__, stream->name, stream->state);
1709 ret = _sdw_deprepare_stream(stream);
1712 sdw_release_bus_lock(stream);
1715 EXPORT_SYMBOL(sdw_deprepare_stream);
1717 static int set_stream(struct snd_pcm_substream *substream,
1718 struct sdw_stream_runtime *sdw_stream)
1720 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1721 struct snd_soc_dai *dai;
1725 /* Set stream pointer on all DAIs */
1726 for_each_rtd_dais(rtd, i, dai) {
1727 ret = snd_soc_dai_set_stream(dai, sdw_stream, substream->stream);
1729 dev_err(rtd->dev, "failed to set stream pointer on dai %s\n", dai->name);
1738 * sdw_alloc_stream() - Allocate and return stream runtime
1740 * @stream_name: SoundWire stream name
1742 * Allocates a SoundWire stream runtime instance.
1743 * sdw_alloc_stream should be called only once per stream. Typically
1744 * invoked from ALSA/ASoC machine/platform driver.
1746 struct sdw_stream_runtime *sdw_alloc_stream(const char *stream_name)
1748 struct sdw_stream_runtime *stream;
1750 stream = kzalloc(sizeof(*stream), GFP_KERNEL);
1754 stream->name = stream_name;
1755 INIT_LIST_HEAD(&stream->master_list);
1756 stream->state = SDW_STREAM_ALLOCATED;
1757 stream->m_rt_count = 0;
1761 EXPORT_SYMBOL(sdw_alloc_stream);
1764 * sdw_startup_stream() - Startup SoundWire stream
1766 * @sdw_substream: Soundwire stream
1768 * Documentation/driver-api/soundwire/stream.rst explains this API in detail
1770 int sdw_startup_stream(void *sdw_substream)
1772 struct snd_pcm_substream *substream = sdw_substream;
1773 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1774 struct sdw_stream_runtime *sdw_stream;
1778 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1779 name = kasprintf(GFP_KERNEL, "%s-Playback", substream->name);
1781 name = kasprintf(GFP_KERNEL, "%s-Capture", substream->name);
1786 sdw_stream = sdw_alloc_stream(name);
1788 dev_err(rtd->dev, "alloc stream failed for substream DAI %s\n", substream->name);
1793 ret = set_stream(substream, sdw_stream);
1795 goto release_stream;
1799 sdw_release_stream(sdw_stream);
1800 set_stream(substream, NULL);
1805 EXPORT_SYMBOL(sdw_startup_stream);
1808 * sdw_shutdown_stream() - Shutdown SoundWire stream
1810 * @sdw_substream: Soundwire stream
1812 * Documentation/driver-api/soundwire/stream.rst explains this API in detail
1814 void sdw_shutdown_stream(void *sdw_substream)
1816 struct snd_pcm_substream *substream = sdw_substream;
1817 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1818 struct sdw_stream_runtime *sdw_stream;
1819 struct snd_soc_dai *dai;
1821 /* Find stream from first CPU DAI */
1822 dai = asoc_rtd_to_cpu(rtd, 0);
1824 sdw_stream = snd_soc_dai_get_stream(dai, substream->stream);
1826 if (IS_ERR(sdw_stream)) {
1827 dev_err(rtd->dev, "no stream found for DAI %s\n", dai->name);
1831 /* release memory */
1832 kfree(sdw_stream->name);
1833 sdw_release_stream(sdw_stream);
1835 /* clear DAI data */
1836 set_stream(substream, NULL);
1838 EXPORT_SYMBOL(sdw_shutdown_stream);
1841 * sdw_release_stream() - Free the assigned stream runtime
1843 * @stream: SoundWire stream runtime
1845 * sdw_release_stream should be called only once per stream
1847 void sdw_release_stream(struct sdw_stream_runtime *stream)
1851 EXPORT_SYMBOL(sdw_release_stream);
1854 * sdw_stream_add_master() - Allocate and add master runtime to a stream
1856 * @bus: SDW Bus instance
1857 * @stream_config: Stream configuration for audio stream
1858 * @port_config: Port configuration for audio stream
1859 * @num_ports: Number of ports
1860 * @stream: SoundWire stream
1862 int sdw_stream_add_master(struct sdw_bus *bus,
1863 struct sdw_stream_config *stream_config,
1864 struct sdw_port_config *port_config,
1865 unsigned int num_ports,
1866 struct sdw_stream_runtime *stream)
1868 struct sdw_master_runtime *m_rt;
1869 bool alloc_master_rt = false;
1872 mutex_lock(&bus->bus_lock);
1875 * For multi link streams, add the second master only if
1876 * the bus supports it.
1877 * Check if bus->multi_link is set
1879 if (!bus->multi_link && stream->m_rt_count > 0) {
1881 "Multilink not supported, link %d\n", bus->link_id);
1887 * check if Master is already allocated (e.g. as a result of Slave adding
1888 * it first), if so skip allocation and go to configuration
1890 m_rt = sdw_master_rt_find(bus, stream);
1892 m_rt = sdw_master_rt_alloc(bus, stream);
1894 dev_err(bus->dev, "%s: Master runtime alloc failed for stream:%s\n",
1895 __func__, stream->name);
1900 alloc_master_rt = true;
1903 if (!sdw_master_port_allocated(m_rt)) {
1904 ret = sdw_master_port_alloc(m_rt, num_ports);
1908 stream->m_rt_count++;
1911 ret = sdw_master_rt_config(m_rt, stream_config);
1915 ret = sdw_config_stream(bus->dev, stream, stream_config, false);
1919 ret = sdw_master_port_config(m_rt, port_config);
1925 * we only cleanup what was allocated in this routine
1927 if (alloc_master_rt)
1928 sdw_master_rt_free(m_rt, stream);
1930 mutex_unlock(&bus->bus_lock);
1933 EXPORT_SYMBOL(sdw_stream_add_master);
1936 * sdw_stream_remove_master() - Remove master from sdw_stream
1938 * @bus: SDW Bus instance
1939 * @stream: SoundWire stream
1941 * This removes and frees port_rt and master_rt from a stream
1943 int sdw_stream_remove_master(struct sdw_bus *bus,
1944 struct sdw_stream_runtime *stream)
1946 struct sdw_master_runtime *m_rt, *_m_rt;
1948 mutex_lock(&bus->bus_lock);
1950 list_for_each_entry_safe(m_rt, _m_rt,
1951 &stream->master_list, stream_node) {
1952 if (m_rt->bus != bus)
1955 sdw_master_port_free(m_rt);
1956 sdw_master_rt_free(m_rt, stream);
1957 stream->m_rt_count--;
1960 if (list_empty(&stream->master_list))
1961 stream->state = SDW_STREAM_RELEASED;
1963 mutex_unlock(&bus->bus_lock);
1967 EXPORT_SYMBOL(sdw_stream_remove_master);
1970 * sdw_stream_add_slave() - Allocate and add master/slave runtime to a stream
1972 * @slave: SDW Slave instance
1973 * @stream_config: Stream configuration for audio stream
1974 * @stream: SoundWire stream
1975 * @port_config: Port configuration for audio stream
1976 * @num_ports: Number of ports
1978 * It is expected that Slave is added before adding Master
1982 int sdw_stream_add_slave(struct sdw_slave *slave,
1983 struct sdw_stream_config *stream_config,
1984 struct sdw_port_config *port_config,
1985 unsigned int num_ports,
1986 struct sdw_stream_runtime *stream)
1988 struct sdw_slave_runtime *s_rt;
1989 struct sdw_master_runtime *m_rt;
1990 bool alloc_master_rt = false;
1991 bool alloc_slave_rt = false;
1995 mutex_lock(&slave->bus->bus_lock);
1998 * check if Master is already allocated, if so skip allocation
1999 * and go to configuration
2001 m_rt = sdw_master_rt_find(slave->bus, stream);
2004 * If this API is invoked by Slave first then m_rt is not valid.
2005 * So, allocate m_rt and add Slave to it.
2007 m_rt = sdw_master_rt_alloc(slave->bus, stream);
2009 dev_err(&slave->dev, "%s: Master runtime alloc failed for stream:%s\n",
2010 __func__, stream->name);
2015 alloc_master_rt = true;
2018 s_rt = sdw_slave_rt_find(slave, stream);
2020 s_rt = sdw_slave_rt_alloc(slave, m_rt);
2022 dev_err(&slave->dev, "Slave runtime alloc failed for stream:%s\n",
2028 alloc_slave_rt = true;
2031 if (!sdw_slave_port_allocated(s_rt)) {
2032 ret = sdw_slave_port_alloc(slave, s_rt, num_ports);
2037 ret = sdw_master_rt_config(m_rt, stream_config);
2041 ret = sdw_slave_rt_config(s_rt, stream_config);
2045 ret = sdw_config_stream(&slave->dev, stream, stream_config, true);
2049 ret = sdw_slave_port_config(slave, s_rt, port_config);
2054 * Change stream state to CONFIGURED on first Slave add.
2055 * Bus is not aware of number of Slave(s) in a stream at this
2056 * point so cannot depend on all Slave(s) to be added in order to
2057 * change stream state to CONFIGURED.
2059 stream->state = SDW_STREAM_CONFIGURED;
2064 * we only cleanup what was allocated in this routine. The 'else if'
2065 * is intentional, the 'master_rt_free' will call sdw_slave_rt_free()
2068 if (alloc_master_rt)
2069 sdw_master_rt_free(m_rt, stream);
2070 else if (alloc_slave_rt)
2071 sdw_slave_rt_free(slave, stream);
2073 mutex_unlock(&slave->bus->bus_lock);
2076 EXPORT_SYMBOL(sdw_stream_add_slave);
2079 * sdw_stream_remove_slave() - Remove slave from sdw_stream
2081 * @slave: SDW Slave instance
2082 * @stream: SoundWire stream
2084 * This removes and frees port_rt and slave_rt from a stream
2086 int sdw_stream_remove_slave(struct sdw_slave *slave,
2087 struct sdw_stream_runtime *stream)
2089 mutex_lock(&slave->bus->bus_lock);
2091 sdw_slave_port_free(slave, stream);
2092 sdw_slave_rt_free(slave, stream);
2094 mutex_unlock(&slave->bus->bus_lock);
2098 EXPORT_SYMBOL(sdw_stream_remove_slave);