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 <sound/soc.h>
20 * Array of supported rows and columns as per MIPI SoundWire Specification 1.1
22 * The rows are arranged as per the array index value programmed
23 * in register. The index 15 has dummy value 0 in order to fill hole.
25 int sdw_rows[SDW_FRAME_ROWS] = {48, 50, 60, 64, 75, 80, 125, 147,
26 96, 100, 120, 128, 150, 160, 250, 0,
27 192, 200, 240, 256, 72, 144, 90, 180};
28 EXPORT_SYMBOL(sdw_rows);
30 int sdw_cols[SDW_FRAME_COLS] = {2, 4, 6, 8, 10, 12, 14, 16};
31 EXPORT_SYMBOL(sdw_cols);
33 int sdw_find_col_index(int col)
37 for (i = 0; i < SDW_FRAME_COLS; i++) {
38 if (sdw_cols[i] == col)
42 pr_warn("Requested column not found, selecting lowest column no: 2\n");
45 EXPORT_SYMBOL(sdw_find_col_index);
47 int sdw_find_row_index(int row)
51 for (i = 0; i < SDW_FRAME_ROWS; i++) {
52 if (sdw_rows[i] == row)
56 pr_warn("Requested row not found, selecting lowest row no: 48\n");
59 EXPORT_SYMBOL(sdw_find_row_index);
61 static int _sdw_program_slave_port_params(struct sdw_bus *bus,
62 struct sdw_slave *slave,
63 struct sdw_transport_params *t_params,
64 enum sdw_dpn_type type)
66 u32 addr1, addr2, addr3, addr4;
70 if (bus->params.next_bank) {
71 addr1 = SDW_DPN_OFFSETCTRL2_B1(t_params->port_num);
72 addr2 = SDW_DPN_BLOCKCTRL3_B1(t_params->port_num);
73 addr3 = SDW_DPN_SAMPLECTRL2_B1(t_params->port_num);
74 addr4 = SDW_DPN_HCTRL_B1(t_params->port_num);
76 addr1 = SDW_DPN_OFFSETCTRL2_B0(t_params->port_num);
77 addr2 = SDW_DPN_BLOCKCTRL3_B0(t_params->port_num);
78 addr3 = SDW_DPN_SAMPLECTRL2_B0(t_params->port_num);
79 addr4 = SDW_DPN_HCTRL_B0(t_params->port_num);
82 /* Program DPN_OffsetCtrl2 registers */
83 ret = sdw_write(slave, addr1, t_params->offset2);
85 dev_err(bus->dev, "DPN_OffsetCtrl2 register write failed\n");
89 /* Program DPN_BlockCtrl3 register */
90 ret = sdw_write(slave, addr2, t_params->blk_pkg_mode);
92 dev_err(bus->dev, "DPN_BlockCtrl3 register write failed\n");
97 * Data ports are FULL, SIMPLE and REDUCED. This function handles
98 * FULL and REDUCED only and beyond this point only FULL is
99 * handled, so bail out if we are not FULL data port type
101 if (type != SDW_DPN_FULL)
104 /* Program DPN_SampleCtrl2 register */
105 wbuf = FIELD_GET(SDW_DPN_SAMPLECTRL_HIGH, t_params->sample_interval - 1);
107 ret = sdw_write(slave, addr3, wbuf);
109 dev_err(bus->dev, "DPN_SampleCtrl2 register write failed\n");
113 /* Program DPN_HCtrl register */
114 wbuf = FIELD_PREP(SDW_DPN_HCTRL_HSTART, t_params->hstart);
115 wbuf |= FIELD_PREP(SDW_DPN_HCTRL_HSTOP, t_params->hstop);
117 ret = sdw_write(slave, addr4, wbuf);
119 dev_err(bus->dev, "DPN_HCtrl register write failed\n");
124 static int sdw_program_slave_port_params(struct sdw_bus *bus,
125 struct sdw_slave_runtime *s_rt,
126 struct sdw_port_runtime *p_rt)
128 struct sdw_transport_params *t_params = &p_rt->transport_params;
129 struct sdw_port_params *p_params = &p_rt->port_params;
130 struct sdw_slave_prop *slave_prop = &s_rt->slave->prop;
131 u32 addr1, addr2, addr3, addr4, addr5, addr6;
132 struct sdw_dpn_prop *dpn_prop;
136 if (s_rt->slave->is_mockup_device)
139 dpn_prop = sdw_get_slave_dpn_prop(s_rt->slave,
145 addr1 = SDW_DPN_PORTCTRL(t_params->port_num);
146 addr2 = SDW_DPN_BLOCKCTRL1(t_params->port_num);
148 if (bus->params.next_bank) {
149 addr3 = SDW_DPN_SAMPLECTRL1_B1(t_params->port_num);
150 addr4 = SDW_DPN_OFFSETCTRL1_B1(t_params->port_num);
151 addr5 = SDW_DPN_BLOCKCTRL2_B1(t_params->port_num);
152 addr6 = SDW_DPN_LANECTRL_B1(t_params->port_num);
155 addr3 = SDW_DPN_SAMPLECTRL1_B0(t_params->port_num);
156 addr4 = SDW_DPN_OFFSETCTRL1_B0(t_params->port_num);
157 addr5 = SDW_DPN_BLOCKCTRL2_B0(t_params->port_num);
158 addr6 = SDW_DPN_LANECTRL_B0(t_params->port_num);
161 /* Program DPN_PortCtrl register */
162 wbuf = FIELD_PREP(SDW_DPN_PORTCTRL_DATAMODE, p_params->data_mode);
163 wbuf |= FIELD_PREP(SDW_DPN_PORTCTRL_FLOWMODE, p_params->flow_mode);
165 ret = sdw_update(s_rt->slave, addr1, 0xF, wbuf);
167 dev_err(&s_rt->slave->dev,
168 "DPN_PortCtrl register write failed for port %d\n",
173 if (!dpn_prop->read_only_wordlength) {
174 /* Program DPN_BlockCtrl1 register */
175 ret = sdw_write(s_rt->slave, addr2, (p_params->bps - 1));
177 dev_err(&s_rt->slave->dev,
178 "DPN_BlockCtrl1 register write failed for port %d\n",
184 /* Program DPN_SampleCtrl1 register */
185 wbuf = (t_params->sample_interval - 1) & SDW_DPN_SAMPLECTRL_LOW;
186 ret = sdw_write(s_rt->slave, addr3, wbuf);
188 dev_err(&s_rt->slave->dev,
189 "DPN_SampleCtrl1 register write failed for port %d\n",
194 /* Program DPN_OffsetCtrl1 registers */
195 ret = sdw_write(s_rt->slave, addr4, t_params->offset1);
197 dev_err(&s_rt->slave->dev,
198 "DPN_OffsetCtrl1 register write failed for port %d\n",
203 /* Program DPN_BlockCtrl2 register*/
204 if (t_params->blk_grp_ctrl_valid) {
205 ret = sdw_write(s_rt->slave, addr5, t_params->blk_grp_ctrl);
207 dev_err(&s_rt->slave->dev,
208 "DPN_BlockCtrl2 reg write failed for port %d\n",
214 /* program DPN_LaneCtrl register */
215 if (slave_prop->lane_control_support) {
216 ret = sdw_write(s_rt->slave, addr6, t_params->lane_ctrl);
218 dev_err(&s_rt->slave->dev,
219 "DPN_LaneCtrl register write failed for port %d\n",
225 if (dpn_prop->type != SDW_DPN_SIMPLE) {
226 ret = _sdw_program_slave_port_params(bus, s_rt->slave,
227 t_params, dpn_prop->type);
229 dev_err(&s_rt->slave->dev,
230 "Transport reg write failed for port: %d\n",
237 static int sdw_program_master_port_params(struct sdw_bus *bus,
238 struct sdw_port_runtime *p_rt)
243 * we need to set transport and port parameters for the port.
244 * Transport parameters refers to the sample interval, offsets and
245 * hstart/stop etc of the data. Port parameters refers to word
246 * length, flow mode etc of the port
248 ret = bus->port_ops->dpn_set_port_transport_params(bus,
249 &p_rt->transport_params,
250 bus->params.next_bank);
254 return bus->port_ops->dpn_set_port_params(bus,
256 bus->params.next_bank);
260 * sdw_program_port_params() - Programs transport parameters of Master(s)
263 * @m_rt: Master stream runtime
265 static int sdw_program_port_params(struct sdw_master_runtime *m_rt)
267 struct sdw_slave_runtime *s_rt;
268 struct sdw_bus *bus = m_rt->bus;
269 struct sdw_port_runtime *p_rt;
272 /* Program transport & port parameters for Slave(s) */
273 list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) {
274 list_for_each_entry(p_rt, &s_rt->port_list, port_node) {
275 ret = sdw_program_slave_port_params(bus, s_rt, p_rt);
281 /* Program transport & port parameters for Master(s) */
282 list_for_each_entry(p_rt, &m_rt->port_list, port_node) {
283 ret = sdw_program_master_port_params(bus, p_rt);
292 * sdw_enable_disable_slave_ports: Enable/disable slave data port
295 * @s_rt: slave runtime
296 * @p_rt: port runtime
297 * @en: enable or disable operation
299 * This function only sets the enable/disable bits in the relevant bank, the
300 * actual enable/disable is done with a bank switch
302 static int sdw_enable_disable_slave_ports(struct sdw_bus *bus,
303 struct sdw_slave_runtime *s_rt,
304 struct sdw_port_runtime *p_rt,
307 struct sdw_transport_params *t_params = &p_rt->transport_params;
311 if (bus->params.next_bank)
312 addr = SDW_DPN_CHANNELEN_B1(p_rt->num);
314 addr = SDW_DPN_CHANNELEN_B0(p_rt->num);
317 * Since bus doesn't support sharing a port across two streams,
318 * it is safe to reset this register
321 ret = sdw_write(s_rt->slave, addr, p_rt->ch_mask);
323 ret = sdw_write(s_rt->slave, addr, 0x0);
326 dev_err(&s_rt->slave->dev,
327 "Slave chn_en reg write failed:%d port:%d\n",
328 ret, t_params->port_num);
333 static int sdw_enable_disable_master_ports(struct sdw_master_runtime *m_rt,
334 struct sdw_port_runtime *p_rt,
337 struct sdw_transport_params *t_params = &p_rt->transport_params;
338 struct sdw_bus *bus = m_rt->bus;
339 struct sdw_enable_ch enable_ch;
342 enable_ch.port_num = p_rt->num;
343 enable_ch.ch_mask = p_rt->ch_mask;
344 enable_ch.enable = en;
346 /* Perform Master port channel(s) enable/disable */
347 if (bus->port_ops->dpn_port_enable_ch) {
348 ret = bus->port_ops->dpn_port_enable_ch(bus,
350 bus->params.next_bank);
353 "Master chn_en write failed:%d port:%d\n",
354 ret, t_params->port_num);
359 "dpn_port_enable_ch not supported, %s failed\n",
360 en ? "enable" : "disable");
368 * sdw_enable_disable_ports() - Enable/disable port(s) for Master and
371 * @m_rt: Master stream runtime
372 * @en: mode (enable/disable)
374 static int sdw_enable_disable_ports(struct sdw_master_runtime *m_rt, bool en)
376 struct sdw_port_runtime *s_port, *m_port;
377 struct sdw_slave_runtime *s_rt;
380 /* Enable/Disable Slave port(s) */
381 list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) {
382 list_for_each_entry(s_port, &s_rt->port_list, port_node) {
383 ret = sdw_enable_disable_slave_ports(m_rt->bus, s_rt,
390 /* Enable/Disable Master port(s) */
391 list_for_each_entry(m_port, &m_rt->port_list, port_node) {
392 ret = sdw_enable_disable_master_ports(m_rt, m_port, en);
400 static int sdw_do_port_prep(struct sdw_slave_runtime *s_rt,
401 struct sdw_prepare_ch prep_ch,
402 enum sdw_port_prep_ops cmd)
404 const struct sdw_slave_ops *ops = s_rt->slave->ops;
407 if (ops->port_prep) {
408 ret = ops->port_prep(s_rt->slave, &prep_ch, cmd);
410 dev_err(&s_rt->slave->dev,
411 "Slave Port Prep cmd %d failed: %d\n",
420 static int sdw_prep_deprep_slave_ports(struct sdw_bus *bus,
421 struct sdw_slave_runtime *s_rt,
422 struct sdw_port_runtime *p_rt,
425 struct completion *port_ready;
426 struct sdw_dpn_prop *dpn_prop;
427 struct sdw_prepare_ch prep_ch;
432 prep_ch.num = p_rt->num;
433 prep_ch.ch_mask = p_rt->ch_mask;
435 dpn_prop = sdw_get_slave_dpn_prop(s_rt->slave,
440 "Slave Port:%d properties not found\n", prep_ch.num);
444 prep_ch.prepare = prep;
446 prep_ch.bank = bus->params.next_bank;
448 if (dpn_prop->imp_def_interrupts || !dpn_prop->simple_ch_prep_sm ||
449 bus->params.s_data_mode != SDW_PORT_DATA_MODE_NORMAL)
453 * Enable interrupt before Port prepare.
454 * For Port de-prepare, it is assumed that port
455 * was prepared earlier
458 ret = sdw_configure_dpn_intr(s_rt->slave, p_rt->num, prep,
459 dpn_prop->imp_def_interrupts);
464 /* Inform slave about the impending port prepare */
465 sdw_do_port_prep(s_rt, prep_ch, SDW_OPS_PORT_PRE_PREP);
467 /* Prepare Slave port implementing CP_SM */
468 if (!dpn_prop->simple_ch_prep_sm) {
469 addr = SDW_DPN_PREPARECTRL(p_rt->num);
472 ret = sdw_write(s_rt->slave, addr, p_rt->ch_mask);
474 ret = sdw_write(s_rt->slave, addr, 0x0);
477 dev_err(&s_rt->slave->dev,
478 "Slave prep_ctrl reg write failed\n");
482 /* Wait for completion on port ready */
483 port_ready = &s_rt->slave->port_ready[prep_ch.num];
484 wait_for_completion_timeout(port_ready,
485 msecs_to_jiffies(dpn_prop->ch_prep_timeout));
487 val = sdw_read(s_rt->slave, SDW_DPN_PREPARESTATUS(p_rt->num));
488 if ((val < 0) || (val & p_rt->ch_mask)) {
489 ret = (val < 0) ? val : -ETIMEDOUT;
490 dev_err(&s_rt->slave->dev,
491 "Chn prep failed for port %d: %d\n", prep_ch.num, ret);
496 /* Inform slaves about ports prepared */
497 sdw_do_port_prep(s_rt, prep_ch, SDW_OPS_PORT_POST_PREP);
499 /* Disable interrupt after Port de-prepare */
501 ret = sdw_configure_dpn_intr(s_rt->slave, p_rt->num, prep,
502 dpn_prop->imp_def_interrupts);
507 static int sdw_prep_deprep_master_ports(struct sdw_master_runtime *m_rt,
508 struct sdw_port_runtime *p_rt,
511 struct sdw_transport_params *t_params = &p_rt->transport_params;
512 struct sdw_bus *bus = m_rt->bus;
513 const struct sdw_master_port_ops *ops = bus->port_ops;
514 struct sdw_prepare_ch prep_ch;
517 prep_ch.num = p_rt->num;
518 prep_ch.ch_mask = p_rt->ch_mask;
519 prep_ch.prepare = prep; /* Prepare/De-prepare */
520 prep_ch.bank = bus->params.next_bank;
522 /* Pre-prepare/Pre-deprepare port(s) */
523 if (ops->dpn_port_prep) {
524 ret = ops->dpn_port_prep(bus, &prep_ch);
526 dev_err(bus->dev, "Port prepare failed for port:%d\n",
536 * sdw_prep_deprep_ports() - Prepare/De-prepare port(s) for Master(s) and
539 * @m_rt: Master runtime handle
540 * @prep: Prepare or De-prepare
542 static int sdw_prep_deprep_ports(struct sdw_master_runtime *m_rt, bool prep)
544 struct sdw_slave_runtime *s_rt;
545 struct sdw_port_runtime *p_rt;
548 /* Prepare/De-prepare Slave port(s) */
549 list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) {
550 list_for_each_entry(p_rt, &s_rt->port_list, port_node) {
551 ret = sdw_prep_deprep_slave_ports(m_rt->bus, s_rt,
558 /* Prepare/De-prepare Master port(s) */
559 list_for_each_entry(p_rt, &m_rt->port_list, port_node) {
560 ret = sdw_prep_deprep_master_ports(m_rt, p_rt, prep);
569 * sdw_notify_config() - Notify bus configuration
571 * @m_rt: Master runtime handle
573 * This function notifies the Master(s) and Slave(s) of the
574 * new bus configuration.
576 static int sdw_notify_config(struct sdw_master_runtime *m_rt)
578 struct sdw_slave_runtime *s_rt;
579 struct sdw_bus *bus = m_rt->bus;
580 struct sdw_slave *slave;
583 if (bus->ops->set_bus_conf) {
584 ret = bus->ops->set_bus_conf(bus, &bus->params);
589 list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) {
592 if (slave->ops->bus_config) {
593 ret = slave->ops->bus_config(slave, &bus->params);
595 dev_err(bus->dev, "Notify Slave: %d failed\n",
606 * sdw_program_params() - Program transport and port parameters for Master(s)
609 * @bus: SDW bus instance
610 * @prepare: true if sdw_program_params() is called by _prepare.
612 static int sdw_program_params(struct sdw_bus *bus, bool prepare)
614 struct sdw_master_runtime *m_rt;
617 list_for_each_entry(m_rt, &bus->m_rt_list, bus_node) {
620 * this loop walks through all master runtimes for a
621 * bus, but the ports can only be configured while
622 * explicitly preparing a stream or handling an
623 * already-prepared stream otherwise.
626 m_rt->stream->state == SDW_STREAM_CONFIGURED)
629 ret = sdw_program_port_params(m_rt);
632 "Program transport params failed: %d\n", ret);
636 ret = sdw_notify_config(m_rt);
639 "Notify bus config failed: %d\n", ret);
643 /* Enable port(s) on alternate bank for all active streams */
644 if (m_rt->stream->state != SDW_STREAM_ENABLED)
647 ret = sdw_enable_disable_ports(m_rt, true);
649 dev_err(bus->dev, "Enable channel failed: %d\n", ret);
657 static int sdw_bank_switch(struct sdw_bus *bus, int m_rt_count)
659 int col_index, row_index;
661 struct sdw_msg *wr_msg;
666 wr_msg = kzalloc(sizeof(*wr_msg), GFP_KERNEL);
670 bus->defer_msg.msg = wr_msg;
672 wbuf = kzalloc(sizeof(*wbuf), GFP_KERNEL);
678 /* Get row and column index to program register */
679 col_index = sdw_find_col_index(bus->params.col);
680 row_index = sdw_find_row_index(bus->params.row);
681 wbuf[0] = col_index | (row_index << 3);
683 if (bus->params.next_bank)
684 addr = SDW_SCP_FRAMECTRL_B1;
686 addr = SDW_SCP_FRAMECTRL_B0;
688 sdw_fill_msg(wr_msg, NULL, addr, 1, SDW_BROADCAST_DEV_NUM,
689 SDW_MSG_FLAG_WRITE, wbuf);
690 wr_msg->ssp_sync = true;
693 * Set the multi_link flag only when both the hardware supports
694 * and hardware-based sync is required
696 multi_link = bus->multi_link && (m_rt_count >= bus->hw_sync_min_links);
699 ret = sdw_transfer_defer(bus, wr_msg, &bus->defer_msg);
701 ret = sdw_transfer(bus, wr_msg);
703 if (ret < 0 && ret != -ENODATA) {
704 dev_err(bus->dev, "Slave frame_ctrl reg write failed\n");
711 bus->defer_msg.msg = NULL;
712 bus->params.curr_bank = !bus->params.curr_bank;
713 bus->params.next_bank = !bus->params.next_bank;
722 bus->defer_msg.msg = NULL;
727 * sdw_ml_sync_bank_switch: Multilink register bank switch
729 * @bus: SDW bus instance
731 * Caller function should free the buffers on error
733 static int sdw_ml_sync_bank_switch(struct sdw_bus *bus)
735 unsigned long time_left;
737 if (!bus->multi_link)
740 /* Wait for completion of transfer */
741 time_left = wait_for_completion_timeout(&bus->defer_msg.complete,
742 bus->bank_switch_timeout);
745 dev_err(bus->dev, "Controller Timed out on bank switch\n");
749 bus->params.curr_bank = !bus->params.curr_bank;
750 bus->params.next_bank = !bus->params.next_bank;
752 if (bus->defer_msg.msg) {
753 kfree(bus->defer_msg.msg->buf);
754 kfree(bus->defer_msg.msg);
760 static int do_bank_switch(struct sdw_stream_runtime *stream)
762 struct sdw_master_runtime *m_rt;
763 const struct sdw_master_ops *ops;
765 bool multi_link = false;
769 m_rt_count = stream->m_rt_count;
771 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
775 if (bus->multi_link && m_rt_count >= bus->hw_sync_min_links) {
777 mutex_lock(&bus->msg_lock);
780 /* Pre-bank switch */
781 if (ops->pre_bank_switch) {
782 ret = ops->pre_bank_switch(bus);
785 "Pre bank switch op failed: %d\n", ret);
791 * Perform Bank switch operation.
792 * For multi link cases, the actual bank switch is
793 * synchronized across all Masters and happens later as a
794 * part of post_bank_switch ops.
796 ret = sdw_bank_switch(bus, m_rt_count);
798 dev_err(bus->dev, "Bank switch failed: %d\n", ret);
804 * For multi link cases, it is expected that the bank switch is
805 * triggered by the post_bank_switch for the first Master in the list
806 * and for the other Masters the post_bank_switch() should return doing
809 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
813 /* Post-bank switch */
814 if (ops->post_bank_switch) {
815 ret = ops->post_bank_switch(bus);
818 "Post bank switch op failed: %d\n",
822 } else if (multi_link) {
824 "Post bank switch ops not implemented\n");
829 /* Set the bank switch timeout to default, if not set */
830 if (!bus->bank_switch_timeout)
831 bus->bank_switch_timeout = DEFAULT_BANK_SWITCH_TIMEOUT;
833 /* Check if bank switch was successful */
834 ret = sdw_ml_sync_bank_switch(bus);
837 "multi link bank switch failed: %d\n", ret);
842 mutex_unlock(&bus->msg_lock);
848 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
850 if (bus->defer_msg.msg) {
851 kfree(bus->defer_msg.msg->buf);
852 kfree(bus->defer_msg.msg);
859 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
861 if (mutex_is_locked(&bus->msg_lock))
862 mutex_unlock(&bus->msg_lock);
869 static struct sdw_port_runtime *sdw_port_alloc(struct list_head *port_list)
871 struct sdw_port_runtime *p_rt;
873 p_rt = kzalloc(sizeof(*p_rt), GFP_KERNEL);
877 list_add_tail(&p_rt->port_node, port_list);
882 static int sdw_port_config(struct sdw_port_runtime *p_rt,
883 struct sdw_port_config *port_config,
886 p_rt->ch_mask = port_config[port_index].ch_mask;
887 p_rt->num = port_config[port_index].num;
890 * TODO: Check port capabilities for requested configuration
896 static void sdw_port_free(struct sdw_port_runtime *p_rt)
898 list_del(&p_rt->port_node);
902 static bool sdw_slave_port_allocated(struct sdw_slave_runtime *s_rt)
904 return !list_empty(&s_rt->port_list);
907 static void sdw_slave_port_free(struct sdw_slave *slave,
908 struct sdw_stream_runtime *stream)
910 struct sdw_port_runtime *p_rt, *_p_rt;
911 struct sdw_master_runtime *m_rt;
912 struct sdw_slave_runtime *s_rt;
914 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
915 list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) {
916 if (s_rt->slave != slave)
919 list_for_each_entry_safe(p_rt, _p_rt,
920 &s_rt->port_list, port_node) {
927 static int sdw_slave_port_alloc(struct sdw_slave *slave,
928 struct sdw_slave_runtime *s_rt,
929 unsigned int num_config)
931 struct sdw_port_runtime *p_rt;
934 /* Iterate for number of ports to perform initialization */
935 for (i = 0; i < num_config; i++) {
936 p_rt = sdw_port_alloc(&s_rt->port_list);
944 static int sdw_slave_port_is_valid_range(struct device *dev, int num)
946 if (!SDW_VALID_PORT_RANGE(num)) {
947 dev_err(dev, "SoundWire: Invalid port number :%d\n", num);
954 static int sdw_slave_port_config(struct sdw_slave *slave,
955 struct sdw_slave_runtime *s_rt,
956 struct sdw_port_config *port_config)
958 struct sdw_port_runtime *p_rt;
963 list_for_each_entry(p_rt, &s_rt->port_list, port_node) {
965 * TODO: Check valid port range as defined by DisCo/
968 ret = sdw_slave_port_is_valid_range(&slave->dev, port_config[i].num);
972 ret = sdw_port_config(p_rt, port_config, i);
981 static bool sdw_master_port_allocated(struct sdw_master_runtime *m_rt)
983 return !list_empty(&m_rt->port_list);
986 static void sdw_master_port_free(struct sdw_master_runtime *m_rt)
988 struct sdw_port_runtime *p_rt, *_p_rt;
990 list_for_each_entry_safe(p_rt, _p_rt, &m_rt->port_list, port_node) {
995 static int sdw_master_port_alloc(struct sdw_master_runtime *m_rt,
996 unsigned int num_ports)
998 struct sdw_port_runtime *p_rt;
1001 /* Iterate for number of ports to perform initialization */
1002 for (i = 0; i < num_ports; i++) {
1003 p_rt = sdw_port_alloc(&m_rt->port_list);
1011 static int sdw_master_port_config(struct sdw_master_runtime *m_rt,
1012 struct sdw_port_config *port_config)
1014 struct sdw_port_runtime *p_rt;
1019 list_for_each_entry(p_rt, &m_rt->port_list, port_node) {
1020 ret = sdw_port_config(p_rt, port_config, i);
1030 * sdw_slave_rt_alloc() - Allocate a Slave runtime handle.
1032 * @slave: Slave handle
1033 * @m_rt: Master runtime handle
1035 * This function is to be called with bus_lock held.
1037 static struct sdw_slave_runtime
1038 *sdw_slave_rt_alloc(struct sdw_slave *slave,
1039 struct sdw_master_runtime *m_rt)
1041 struct sdw_slave_runtime *s_rt;
1043 s_rt = kzalloc(sizeof(*s_rt), GFP_KERNEL);
1047 INIT_LIST_HEAD(&s_rt->port_list);
1048 s_rt->slave = slave;
1050 list_add_tail(&s_rt->m_rt_node, &m_rt->slave_rt_list);
1056 * sdw_slave_rt_config() - Configure a Slave runtime handle.
1058 * @s_rt: Slave runtime handle
1059 * @stream_config: Stream configuration
1061 * This function is to be called with bus_lock held.
1063 static int sdw_slave_rt_config(struct sdw_slave_runtime *s_rt,
1064 struct sdw_stream_config *stream_config)
1066 s_rt->ch_count = stream_config->ch_count;
1067 s_rt->direction = stream_config->direction;
1072 static struct sdw_slave_runtime *sdw_slave_rt_find(struct sdw_slave *slave,
1073 struct sdw_stream_runtime *stream)
1075 struct sdw_slave_runtime *s_rt, *_s_rt;
1076 struct sdw_master_runtime *m_rt;
1078 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
1079 /* Retrieve Slave runtime handle */
1080 list_for_each_entry_safe(s_rt, _s_rt,
1081 &m_rt->slave_rt_list, m_rt_node) {
1082 if (s_rt->slave == slave)
1090 * sdw_slave_rt_free() - Free Slave(s) runtime handle
1092 * @slave: Slave handle.
1093 * @stream: Stream runtime handle.
1095 * This function is to be called with bus_lock held.
1097 static void sdw_slave_rt_free(struct sdw_slave *slave,
1098 struct sdw_stream_runtime *stream)
1100 struct sdw_slave_runtime *s_rt;
1102 s_rt = sdw_slave_rt_find(slave, stream);
1104 list_del(&s_rt->m_rt_node);
1109 static struct sdw_master_runtime
1110 *sdw_master_rt_find(struct sdw_bus *bus,
1111 struct sdw_stream_runtime *stream)
1113 struct sdw_master_runtime *m_rt;
1115 /* Retrieve Bus handle if already available */
1116 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
1117 if (m_rt->bus == bus)
1125 * sdw_master_rt_alloc() - Allocates a Master runtime handle
1127 * @bus: SDW bus instance
1128 * @stream: Stream runtime handle.
1130 * This function is to be called with bus_lock held.
1132 static struct sdw_master_runtime
1133 *sdw_master_rt_alloc(struct sdw_bus *bus,
1134 struct sdw_stream_runtime *stream)
1136 struct sdw_master_runtime *m_rt;
1138 m_rt = kzalloc(sizeof(*m_rt), GFP_KERNEL);
1142 /* Initialization of Master runtime handle */
1143 INIT_LIST_HEAD(&m_rt->port_list);
1144 INIT_LIST_HEAD(&m_rt->slave_rt_list);
1145 list_add_tail(&m_rt->stream_node, &stream->master_list);
1147 list_add_tail(&m_rt->bus_node, &bus->m_rt_list);
1150 m_rt->stream = stream;
1156 * sdw_master_rt_config() - Configure Master runtime handle
1158 * @m_rt: Master runtime handle
1159 * @stream_config: Stream configuration
1161 * This function is to be called with bus_lock held.
1164 static int sdw_master_rt_config(struct sdw_master_runtime *m_rt,
1165 struct sdw_stream_config *stream_config)
1167 m_rt->ch_count = stream_config->ch_count;
1168 m_rt->direction = stream_config->direction;
1174 * sdw_master_rt_free() - Free Master runtime handle
1176 * @m_rt: Master runtime node
1177 * @stream: Stream runtime handle.
1179 * This function is to be called with bus_lock held
1180 * It frees the Master runtime handle and associated Slave(s) runtime
1181 * handle. If this is called first then sdw_slave_rt_free() will have
1182 * no effect as Slave(s) runtime handle would already be freed up.
1184 static void sdw_master_rt_free(struct sdw_master_runtime *m_rt,
1185 struct sdw_stream_runtime *stream)
1187 struct sdw_slave_runtime *s_rt, *_s_rt;
1189 list_for_each_entry_safe(s_rt, _s_rt, &m_rt->slave_rt_list, m_rt_node) {
1190 sdw_slave_port_free(s_rt->slave, stream);
1191 sdw_slave_rt_free(s_rt->slave, stream);
1194 list_del(&m_rt->stream_node);
1195 list_del(&m_rt->bus_node);
1200 * sdw_config_stream() - Configure the allocated stream
1203 * @stream: SoundWire stream
1204 * @stream_config: Stream configuration for audio stream
1205 * @is_slave: is API called from Slave or Master
1207 * This function is to be called with bus_lock held.
1209 static int sdw_config_stream(struct device *dev,
1210 struct sdw_stream_runtime *stream,
1211 struct sdw_stream_config *stream_config,
1215 * Update the stream rate, channel and bps based on data
1216 * source. For more than one data source (multilink),
1217 * match the rate, bps, stream type and increment number of channels.
1219 * If rate/bps is zero, it means the values are not set, so skip
1220 * comparison and allow the value to be set and stored in stream
1222 if (stream->params.rate &&
1223 stream->params.rate != stream_config->frame_rate) {
1224 dev_err(dev, "rate not matching, stream:%s\n", stream->name);
1228 if (stream->params.bps &&
1229 stream->params.bps != stream_config->bps) {
1230 dev_err(dev, "bps not matching, stream:%s\n", stream->name);
1234 stream->type = stream_config->type;
1235 stream->params.rate = stream_config->frame_rate;
1236 stream->params.bps = stream_config->bps;
1238 /* TODO: Update this check during Device-device support */
1240 stream->params.ch_count += stream_config->ch_count;
1246 * sdw_get_slave_dpn_prop() - Get Slave port capabilities
1248 * @slave: Slave handle
1249 * @direction: Data direction.
1250 * @port_num: Port number
1252 struct sdw_dpn_prop *sdw_get_slave_dpn_prop(struct sdw_slave *slave,
1253 enum sdw_data_direction direction,
1254 unsigned int port_num)
1256 struct sdw_dpn_prop *dpn_prop;
1260 if (direction == SDW_DATA_DIR_TX) {
1261 num_ports = hweight32(slave->prop.source_ports);
1262 dpn_prop = slave->prop.src_dpn_prop;
1264 num_ports = hweight32(slave->prop.sink_ports);
1265 dpn_prop = slave->prop.sink_dpn_prop;
1268 for (i = 0; i < num_ports; i++) {
1269 if (dpn_prop[i].num == port_num)
1270 return &dpn_prop[i];
1277 * sdw_acquire_bus_lock: Acquire bus lock for all Master runtime(s)
1279 * @stream: SoundWire stream
1281 * Acquire bus_lock for each of the master runtime(m_rt) part of this
1282 * stream to reconfigure the bus.
1283 * NOTE: This function is called from SoundWire stream ops and is
1284 * expected that a global lock is held before acquiring bus_lock.
1286 static void sdw_acquire_bus_lock(struct sdw_stream_runtime *stream)
1288 struct sdw_master_runtime *m_rt;
1289 struct sdw_bus *bus;
1291 /* Iterate for all Master(s) in Master list */
1292 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
1295 mutex_lock(&bus->bus_lock);
1300 * sdw_release_bus_lock: Release bus lock for all Master runtime(s)
1302 * @stream: SoundWire stream
1304 * Release the previously held bus_lock after reconfiguring the bus.
1305 * NOTE: This function is called from SoundWire stream ops and is
1306 * expected that a global lock is held before releasing bus_lock.
1308 static void sdw_release_bus_lock(struct sdw_stream_runtime *stream)
1310 struct sdw_master_runtime *m_rt;
1311 struct sdw_bus *bus;
1313 /* Iterate for all Master(s) in Master list */
1314 list_for_each_entry_reverse(m_rt, &stream->master_list, stream_node) {
1316 mutex_unlock(&bus->bus_lock);
1320 static int _sdw_prepare_stream(struct sdw_stream_runtime *stream,
1323 struct sdw_master_runtime *m_rt;
1324 struct sdw_bus *bus = NULL;
1325 struct sdw_master_prop *prop;
1326 struct sdw_bus_params params;
1329 /* Prepare Master(s) and Slave(s) port(s) associated with stream */
1330 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
1333 memcpy(¶ms, &bus->params, sizeof(params));
1335 /* TODO: Support Asynchronous mode */
1336 if ((prop->max_clk_freq % stream->params.rate) != 0) {
1337 dev_err(bus->dev, "Async mode not supported\n");
1342 goto program_params;
1344 /* Increment cumulative bus bandwidth */
1345 /* TODO: Update this during Device-Device support */
1346 bus->params.bandwidth += m_rt->stream->params.rate *
1347 m_rt->ch_count * m_rt->stream->params.bps;
1349 /* Compute params */
1350 if (bus->compute_params) {
1351 ret = bus->compute_params(bus);
1353 dev_err(bus->dev, "Compute params failed: %d\n",
1360 /* Program params */
1361 ret = sdw_program_params(bus, true);
1363 dev_err(bus->dev, "Program params failed: %d\n", ret);
1364 goto restore_params;
1369 pr_err("Configuration error in %s\n", __func__);
1373 ret = do_bank_switch(stream);
1375 dev_err(bus->dev, "Bank switch failed: %d\n", ret);
1376 goto restore_params;
1379 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
1382 /* Prepare port(s) on the new clock configuration */
1383 ret = sdw_prep_deprep_ports(m_rt, true);
1385 dev_err(bus->dev, "Prepare port(s) failed ret = %d\n",
1391 stream->state = SDW_STREAM_PREPARED;
1396 memcpy(&bus->params, ¶ms, sizeof(params));
1401 * sdw_prepare_stream() - Prepare SoundWire stream
1403 * @stream: Soundwire stream
1405 * Documentation/driver-api/soundwire/stream.rst explains this API in detail
1407 int sdw_prepare_stream(struct sdw_stream_runtime *stream)
1409 bool update_params = true;
1413 pr_err("SoundWire: Handle not found for stream\n");
1417 sdw_acquire_bus_lock(stream);
1419 if (stream->state == SDW_STREAM_PREPARED) {
1424 if (stream->state != SDW_STREAM_CONFIGURED &&
1425 stream->state != SDW_STREAM_DEPREPARED &&
1426 stream->state != SDW_STREAM_DISABLED) {
1427 pr_err("%s: %s: inconsistent state state %d\n",
1428 __func__, stream->name, stream->state);
1434 * when the stream is DISABLED, this means sdw_prepare_stream()
1435 * is called as a result of an underflow or a resume operation.
1436 * In this case, the bus parameters shall not be recomputed, but
1437 * still need to be re-applied
1439 if (stream->state == SDW_STREAM_DISABLED)
1440 update_params = false;
1442 ret = _sdw_prepare_stream(stream, update_params);
1445 sdw_release_bus_lock(stream);
1448 EXPORT_SYMBOL(sdw_prepare_stream);
1450 static int _sdw_enable_stream(struct sdw_stream_runtime *stream)
1452 struct sdw_master_runtime *m_rt;
1453 struct sdw_bus *bus = NULL;
1456 /* Enable Master(s) and Slave(s) port(s) associated with stream */
1457 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
1460 /* Program params */
1461 ret = sdw_program_params(bus, false);
1463 dev_err(bus->dev, "Program params failed: %d\n", ret);
1467 /* Enable port(s) */
1468 ret = sdw_enable_disable_ports(m_rt, true);
1471 "Enable port(s) failed ret: %d\n", ret);
1477 pr_err("Configuration error in %s\n", __func__);
1481 ret = do_bank_switch(stream);
1483 dev_err(bus->dev, "Bank switch failed: %d\n", ret);
1487 stream->state = SDW_STREAM_ENABLED;
1492 * sdw_enable_stream() - Enable SoundWire stream
1494 * @stream: Soundwire stream
1496 * Documentation/driver-api/soundwire/stream.rst explains this API in detail
1498 int sdw_enable_stream(struct sdw_stream_runtime *stream)
1503 pr_err("SoundWire: Handle not found for stream\n");
1507 sdw_acquire_bus_lock(stream);
1509 if (stream->state == SDW_STREAM_ENABLED) {
1514 if (stream->state != SDW_STREAM_PREPARED &&
1515 stream->state != SDW_STREAM_DISABLED) {
1516 pr_err("%s: %s: inconsistent state state %d\n",
1517 __func__, stream->name, stream->state);
1522 ret = _sdw_enable_stream(stream);
1525 sdw_release_bus_lock(stream);
1528 EXPORT_SYMBOL(sdw_enable_stream);
1530 static int _sdw_disable_stream(struct sdw_stream_runtime *stream)
1532 struct sdw_master_runtime *m_rt;
1535 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
1536 struct sdw_bus *bus = m_rt->bus;
1538 /* Disable port(s) */
1539 ret = sdw_enable_disable_ports(m_rt, false);
1541 dev_err(bus->dev, "Disable port(s) failed: %d\n", ret);
1545 stream->state = SDW_STREAM_DISABLED;
1547 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
1548 struct sdw_bus *bus = m_rt->bus;
1550 /* Program params */
1551 ret = sdw_program_params(bus, false);
1553 dev_err(bus->dev, "Program params failed: %d\n", ret);
1558 ret = do_bank_switch(stream);
1560 pr_err("Bank switch failed: %d\n", ret);
1564 /* make sure alternate bank (previous current) is also disabled */
1565 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
1566 struct sdw_bus *bus = m_rt->bus;
1568 /* Disable port(s) */
1569 ret = sdw_enable_disable_ports(m_rt, false);
1571 dev_err(bus->dev, "Disable port(s) failed: %d\n", ret);
1580 * sdw_disable_stream() - Disable SoundWire stream
1582 * @stream: Soundwire stream
1584 * Documentation/driver-api/soundwire/stream.rst explains this API in detail
1586 int sdw_disable_stream(struct sdw_stream_runtime *stream)
1591 pr_err("SoundWire: Handle not found for stream\n");
1595 sdw_acquire_bus_lock(stream);
1597 if (stream->state == SDW_STREAM_DISABLED) {
1602 if (stream->state != SDW_STREAM_ENABLED) {
1603 pr_err("%s: %s: inconsistent state state %d\n",
1604 __func__, stream->name, stream->state);
1609 ret = _sdw_disable_stream(stream);
1612 sdw_release_bus_lock(stream);
1615 EXPORT_SYMBOL(sdw_disable_stream);
1617 static int _sdw_deprepare_stream(struct sdw_stream_runtime *stream)
1619 struct sdw_master_runtime *m_rt;
1620 struct sdw_bus *bus;
1623 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
1625 /* De-prepare port(s) */
1626 ret = sdw_prep_deprep_ports(m_rt, false);
1629 "De-prepare port(s) failed: %d\n", ret);
1633 /* TODO: Update this during Device-Device support */
1634 bus->params.bandwidth -= m_rt->stream->params.rate *
1635 m_rt->ch_count * m_rt->stream->params.bps;
1637 /* Compute params */
1638 if (bus->compute_params) {
1639 ret = bus->compute_params(bus);
1641 dev_err(bus->dev, "Compute params failed: %d\n",
1647 /* Program params */
1648 ret = sdw_program_params(bus, false);
1650 dev_err(bus->dev, "Program params failed: %d\n", ret);
1655 stream->state = SDW_STREAM_DEPREPARED;
1656 return do_bank_switch(stream);
1660 * sdw_deprepare_stream() - Deprepare SoundWire stream
1662 * @stream: Soundwire stream
1664 * Documentation/driver-api/soundwire/stream.rst explains this API in detail
1666 int sdw_deprepare_stream(struct sdw_stream_runtime *stream)
1671 pr_err("SoundWire: Handle not found for stream\n");
1675 sdw_acquire_bus_lock(stream);
1677 if (stream->state == SDW_STREAM_DEPREPARED) {
1682 if (stream->state != SDW_STREAM_PREPARED &&
1683 stream->state != SDW_STREAM_DISABLED) {
1684 pr_err("%s: %s: inconsistent state state %d\n",
1685 __func__, stream->name, stream->state);
1690 ret = _sdw_deprepare_stream(stream);
1693 sdw_release_bus_lock(stream);
1696 EXPORT_SYMBOL(sdw_deprepare_stream);
1698 static int set_stream(struct snd_pcm_substream *substream,
1699 struct sdw_stream_runtime *sdw_stream)
1701 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1702 struct snd_soc_dai *dai;
1706 /* Set stream pointer on all DAIs */
1707 for_each_rtd_dais(rtd, i, dai) {
1708 ret = snd_soc_dai_set_stream(dai, sdw_stream, substream->stream);
1710 dev_err(rtd->dev, "failed to set stream pointer on dai %s\n", dai->name);
1719 * sdw_alloc_stream() - Allocate and return stream runtime
1721 * @stream_name: SoundWire stream name
1723 * Allocates a SoundWire stream runtime instance.
1724 * sdw_alloc_stream should be called only once per stream. Typically
1725 * invoked from ALSA/ASoC machine/platform driver.
1727 struct sdw_stream_runtime *sdw_alloc_stream(const char *stream_name)
1729 struct sdw_stream_runtime *stream;
1731 stream = kzalloc(sizeof(*stream), GFP_KERNEL);
1735 stream->name = stream_name;
1736 INIT_LIST_HEAD(&stream->master_list);
1737 stream->state = SDW_STREAM_ALLOCATED;
1738 stream->m_rt_count = 0;
1742 EXPORT_SYMBOL(sdw_alloc_stream);
1745 * sdw_startup_stream() - Startup SoundWire stream
1747 * @sdw_substream: Soundwire stream
1749 * Documentation/driver-api/soundwire/stream.rst explains this API in detail
1751 int sdw_startup_stream(void *sdw_substream)
1753 struct snd_pcm_substream *substream = sdw_substream;
1754 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1755 struct sdw_stream_runtime *sdw_stream;
1759 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1760 name = kasprintf(GFP_KERNEL, "%s-Playback", substream->name);
1762 name = kasprintf(GFP_KERNEL, "%s-Capture", substream->name);
1767 sdw_stream = sdw_alloc_stream(name);
1769 dev_err(rtd->dev, "alloc stream failed for substream DAI %s\n", substream->name);
1774 ret = set_stream(substream, sdw_stream);
1776 goto release_stream;
1780 sdw_release_stream(sdw_stream);
1781 set_stream(substream, NULL);
1786 EXPORT_SYMBOL(sdw_startup_stream);
1789 * sdw_shutdown_stream() - Shutdown SoundWire stream
1791 * @sdw_substream: Soundwire stream
1793 * Documentation/driver-api/soundwire/stream.rst explains this API in detail
1795 void sdw_shutdown_stream(void *sdw_substream)
1797 struct snd_pcm_substream *substream = sdw_substream;
1798 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1799 struct sdw_stream_runtime *sdw_stream;
1800 struct snd_soc_dai *dai;
1802 /* Find stream from first CPU DAI */
1803 dai = asoc_rtd_to_cpu(rtd, 0);
1805 sdw_stream = snd_soc_dai_get_stream(dai, substream->stream);
1807 if (IS_ERR(sdw_stream)) {
1808 dev_err(rtd->dev, "no stream found for DAI %s\n", dai->name);
1812 /* release memory */
1813 kfree(sdw_stream->name);
1814 sdw_release_stream(sdw_stream);
1816 /* clear DAI data */
1817 set_stream(substream, NULL);
1819 EXPORT_SYMBOL(sdw_shutdown_stream);
1822 * sdw_release_stream() - Free the assigned stream runtime
1824 * @stream: SoundWire stream runtime
1826 * sdw_release_stream should be called only once per stream
1828 void sdw_release_stream(struct sdw_stream_runtime *stream)
1832 EXPORT_SYMBOL(sdw_release_stream);
1835 * sdw_stream_add_master() - Allocate and add master runtime to a stream
1837 * @bus: SDW Bus instance
1838 * @stream_config: Stream configuration for audio stream
1839 * @port_config: Port configuration for audio stream
1840 * @num_ports: Number of ports
1841 * @stream: SoundWire stream
1843 int sdw_stream_add_master(struct sdw_bus *bus,
1844 struct sdw_stream_config *stream_config,
1845 struct sdw_port_config *port_config,
1846 unsigned int num_ports,
1847 struct sdw_stream_runtime *stream)
1849 struct sdw_master_runtime *m_rt;
1850 bool alloc_master_rt = true;
1853 mutex_lock(&bus->bus_lock);
1856 * For multi link streams, add the second master only if
1857 * the bus supports it.
1858 * Check if bus->multi_link is set
1860 if (!bus->multi_link && stream->m_rt_count > 0) {
1862 "Multilink not supported, link %d\n", bus->link_id);
1868 * check if Master is already allocated (e.g. as a result of Slave adding
1869 * it first), if so skip allocation and go to configuration
1871 m_rt = sdw_master_rt_find(bus, stream);
1873 alloc_master_rt = false;
1874 goto skip_alloc_master_rt;
1877 m_rt = sdw_master_rt_alloc(bus, stream);
1879 dev_err(bus->dev, "Master runtime alloc failed for stream:%s\n", stream->name);
1883 skip_alloc_master_rt:
1885 if (sdw_master_port_allocated(m_rt))
1886 goto skip_alloc_master_port;
1888 ret = sdw_master_port_alloc(m_rt, num_ports);
1892 stream->m_rt_count++;
1894 skip_alloc_master_port:
1896 ret = sdw_master_rt_config(m_rt, stream_config);
1900 ret = sdw_config_stream(bus->dev, stream, stream_config, false);
1904 ret = sdw_master_port_config(m_rt, port_config);
1910 * we only cleanup what was allocated in this routine
1912 if (alloc_master_rt)
1913 sdw_master_rt_free(m_rt, stream);
1915 mutex_unlock(&bus->bus_lock);
1918 EXPORT_SYMBOL(sdw_stream_add_master);
1921 * sdw_stream_remove_master() - Remove master from sdw_stream
1923 * @bus: SDW Bus instance
1924 * @stream: SoundWire stream
1926 * This removes and frees port_rt and master_rt from a stream
1928 int sdw_stream_remove_master(struct sdw_bus *bus,
1929 struct sdw_stream_runtime *stream)
1931 struct sdw_master_runtime *m_rt, *_m_rt;
1933 mutex_lock(&bus->bus_lock);
1935 list_for_each_entry_safe(m_rt, _m_rt,
1936 &stream->master_list, stream_node) {
1937 if (m_rt->bus != bus)
1940 sdw_master_port_free(m_rt);
1941 sdw_master_rt_free(m_rt, stream);
1942 stream->m_rt_count--;
1945 if (list_empty(&stream->master_list))
1946 stream->state = SDW_STREAM_RELEASED;
1948 mutex_unlock(&bus->bus_lock);
1952 EXPORT_SYMBOL(sdw_stream_remove_master);
1955 * sdw_stream_add_slave() - Allocate and add master/slave runtime to a stream
1957 * @slave: SDW Slave instance
1958 * @stream_config: Stream configuration for audio stream
1959 * @stream: SoundWire stream
1960 * @port_config: Port configuration for audio stream
1961 * @num_ports: Number of ports
1963 * It is expected that Slave is added before adding Master
1967 int sdw_stream_add_slave(struct sdw_slave *slave,
1968 struct sdw_stream_config *stream_config,
1969 struct sdw_port_config *port_config,
1970 unsigned int num_ports,
1971 struct sdw_stream_runtime *stream)
1973 struct sdw_slave_runtime *s_rt;
1974 struct sdw_master_runtime *m_rt;
1975 bool alloc_master_rt = true;
1976 bool alloc_slave_rt = true;
1980 mutex_lock(&slave->bus->bus_lock);
1983 * check if Master is already allocated, if so skip allocation
1984 * and go to configuration
1986 m_rt = sdw_master_rt_find(slave->bus, stream);
1988 alloc_master_rt = false;
1989 goto skip_alloc_master_rt;
1993 * If this API is invoked by Slave first then m_rt is not valid.
1994 * So, allocate m_rt and add Slave to it.
1996 m_rt = sdw_master_rt_alloc(slave->bus, stream);
1998 dev_err(&slave->dev, "Master runtime alloc failed for stream:%s\n", stream->name);
2003 skip_alloc_master_rt:
2004 s_rt = sdw_slave_rt_find(slave, stream);
2006 goto skip_alloc_slave_rt;
2008 s_rt = sdw_slave_rt_alloc(slave, m_rt);
2010 dev_err(&slave->dev, "Slave runtime alloc failed for stream:%s\n", stream->name);
2011 alloc_slave_rt = false;
2016 skip_alloc_slave_rt:
2017 if (sdw_slave_port_allocated(s_rt))
2018 goto skip_port_alloc;
2020 ret = sdw_slave_port_alloc(slave, s_rt, num_ports);
2025 ret = sdw_master_rt_config(m_rt, stream_config);
2029 ret = sdw_slave_rt_config(s_rt, stream_config);
2033 ret = sdw_config_stream(&slave->dev, stream, stream_config, true);
2037 ret = sdw_slave_port_config(slave, s_rt, port_config);
2042 * Change stream state to CONFIGURED on first Slave add.
2043 * Bus is not aware of number of Slave(s) in a stream at this
2044 * point so cannot depend on all Slave(s) to be added in order to
2045 * change stream state to CONFIGURED.
2047 stream->state = SDW_STREAM_CONFIGURED;
2052 * we only cleanup what was allocated in this routine. The 'else if'
2053 * is intentional, the 'master_rt_free' will call sdw_slave_rt_free()
2056 if (alloc_master_rt)
2057 sdw_master_rt_free(m_rt, stream);
2058 else if (alloc_slave_rt)
2059 sdw_slave_rt_free(slave, stream);
2061 mutex_unlock(&slave->bus->bus_lock);
2064 EXPORT_SYMBOL(sdw_stream_add_slave);
2067 * sdw_stream_remove_slave() - Remove slave from sdw_stream
2069 * @slave: SDW Slave instance
2070 * @stream: SoundWire stream
2072 * This removes and frees port_rt and slave_rt from a stream
2074 int sdw_stream_remove_slave(struct sdw_slave *slave,
2075 struct sdw_stream_runtime *stream)
2077 mutex_lock(&slave->bus->bus_lock);
2079 sdw_slave_port_free(slave, stream);
2080 sdw_slave_rt_free(slave, stream);
2082 mutex_unlock(&slave->bus->bus_lock);
2086 EXPORT_SYMBOL(sdw_stream_remove_slave);