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>
19 * Array of supported rows and columns as per MIPI SoundWire Specification 1.1
21 * The rows are arranged as per the array index value programmed
22 * in register. The index 15 has dummy value 0 in order to fill hole.
24 int rows[SDW_FRAME_ROWS] = {48, 50, 60, 64, 75, 80, 125, 147,
25 96, 100, 120, 128, 150, 160, 250, 0,
26 192, 200, 240, 256, 72, 144, 90, 180};
28 int cols[SDW_FRAME_COLS] = {2, 4, 6, 8, 10, 12, 14, 16};
30 static int sdw_find_col_index(int col)
34 for (i = 0; i < SDW_FRAME_COLS; i++) {
39 pr_warn("Requested column not found, selecting lowest column no: 2\n");
43 static int sdw_find_row_index(int row)
47 for (i = 0; i < SDW_FRAME_ROWS; i++) {
52 pr_warn("Requested row not found, selecting lowest row no: 48\n");
56 static int _sdw_program_slave_port_params(struct sdw_bus *bus,
57 struct sdw_slave *slave,
58 struct sdw_transport_params *t_params,
59 enum sdw_dpn_type type)
61 u32 addr1, addr2, addr3, addr4;
65 if (bus->params.next_bank) {
66 addr1 = SDW_DPN_OFFSETCTRL2_B1(t_params->port_num);
67 addr2 = SDW_DPN_BLOCKCTRL3_B1(t_params->port_num);
68 addr3 = SDW_DPN_SAMPLECTRL2_B1(t_params->port_num);
69 addr4 = SDW_DPN_HCTRL_B1(t_params->port_num);
71 addr1 = SDW_DPN_OFFSETCTRL2_B0(t_params->port_num);
72 addr2 = SDW_DPN_BLOCKCTRL3_B0(t_params->port_num);
73 addr3 = SDW_DPN_SAMPLECTRL2_B0(t_params->port_num);
74 addr4 = SDW_DPN_HCTRL_B0(t_params->port_num);
77 /* Program DPN_OffsetCtrl2 registers */
78 ret = sdw_write(slave, addr1, t_params->offset2);
80 dev_err(bus->dev, "DPN_OffsetCtrl2 register write failed\n");
84 /* Program DPN_BlockCtrl3 register */
85 ret = sdw_write(slave, addr2, t_params->blk_pkg_mode);
87 dev_err(bus->dev, "DPN_BlockCtrl3 register write failed\n");
92 * Data ports are FULL, SIMPLE and REDUCED. This function handles
93 * FULL and REDUCED only and beyond this point only FULL is
94 * handled, so bail out if we are not FULL data port type
96 if (type != SDW_DPN_FULL)
99 /* Program DPN_SampleCtrl2 register */
100 wbuf = (t_params->sample_interval - 1);
101 wbuf &= SDW_DPN_SAMPLECTRL_HIGH;
102 wbuf >>= SDW_REG_SHIFT(SDW_DPN_SAMPLECTRL_HIGH);
104 ret = sdw_write(slave, addr3, wbuf);
106 dev_err(bus->dev, "DPN_SampleCtrl2 register write failed\n");
110 /* Program DPN_HCtrl register */
111 wbuf = t_params->hstart;
112 wbuf <<= SDW_REG_SHIFT(SDW_DPN_HCTRL_HSTART);
113 wbuf |= t_params->hstop;
115 ret = sdw_write(slave, addr4, wbuf);
117 dev_err(bus->dev, "DPN_HCtrl register write failed\n");
122 static int sdw_program_slave_port_params(struct sdw_bus *bus,
123 struct sdw_slave_runtime *s_rt,
124 struct sdw_port_runtime *p_rt)
126 struct sdw_transport_params *t_params = &p_rt->transport_params;
127 struct sdw_port_params *p_params = &p_rt->port_params;
128 struct sdw_slave_prop *slave_prop = &s_rt->slave->prop;
129 u32 addr1, addr2, addr3, addr4, addr5, addr6;
130 struct sdw_dpn_prop *dpn_prop;
134 dpn_prop = sdw_get_slave_dpn_prop(s_rt->slave,
140 addr1 = SDW_DPN_PORTCTRL(t_params->port_num);
141 addr2 = SDW_DPN_BLOCKCTRL1(t_params->port_num);
143 if (bus->params.next_bank) {
144 addr3 = SDW_DPN_SAMPLECTRL1_B1(t_params->port_num);
145 addr4 = SDW_DPN_OFFSETCTRL1_B1(t_params->port_num);
146 addr5 = SDW_DPN_BLOCKCTRL2_B1(t_params->port_num);
147 addr6 = SDW_DPN_LANECTRL_B1(t_params->port_num);
150 addr3 = SDW_DPN_SAMPLECTRL1_B0(t_params->port_num);
151 addr4 = SDW_DPN_OFFSETCTRL1_B0(t_params->port_num);
152 addr5 = SDW_DPN_BLOCKCTRL2_B0(t_params->port_num);
153 addr6 = SDW_DPN_LANECTRL_B0(t_params->port_num);
156 /* Program DPN_PortCtrl register */
157 wbuf = p_params->data_mode << SDW_REG_SHIFT(SDW_DPN_PORTCTRL_DATAMODE);
158 wbuf |= p_params->flow_mode;
160 ret = sdw_update(s_rt->slave, addr1, 0xF, wbuf);
162 dev_err(&s_rt->slave->dev,
163 "DPN_PortCtrl register write failed for port %d\n",
168 /* Program DPN_BlockCtrl1 register */
169 ret = sdw_write(s_rt->slave, addr2, (p_params->bps - 1));
171 dev_err(&s_rt->slave->dev,
172 "DPN_BlockCtrl1 register write failed for port %d\n",
177 /* Program DPN_SampleCtrl1 register */
178 wbuf = (t_params->sample_interval - 1) & SDW_DPN_SAMPLECTRL_LOW;
179 ret = sdw_write(s_rt->slave, addr3, wbuf);
181 dev_err(&s_rt->slave->dev,
182 "DPN_SampleCtrl1 register write failed for port %d\n",
187 /* Program DPN_OffsetCtrl1 registers */
188 ret = sdw_write(s_rt->slave, addr4, t_params->offset1);
190 dev_err(&s_rt->slave->dev,
191 "DPN_OffsetCtrl1 register write failed for port %d\n",
196 /* Program DPN_BlockCtrl2 register*/
197 if (t_params->blk_grp_ctrl_valid) {
198 ret = sdw_write(s_rt->slave, addr5, t_params->blk_grp_ctrl);
200 dev_err(&s_rt->slave->dev,
201 "DPN_BlockCtrl2 reg write failed for port %d\n",
207 /* program DPN_LaneCtrl register */
208 if (slave_prop->lane_control_support) {
209 ret = sdw_write(s_rt->slave, addr6, t_params->lane_ctrl);
211 dev_err(&s_rt->slave->dev,
212 "DPN_LaneCtrl register write failed for port %d\n",
218 if (dpn_prop->type != SDW_DPN_SIMPLE) {
219 ret = _sdw_program_slave_port_params(bus, s_rt->slave,
220 t_params, dpn_prop->type);
222 dev_err(&s_rt->slave->dev,
223 "Transport reg write failed for port: %d\n",
230 static int sdw_program_master_port_params(struct sdw_bus *bus,
231 struct sdw_port_runtime *p_rt)
236 * we need to set transport and port parameters for the port.
237 * Transport parameters refers to the sample interval, offsets and
238 * hstart/stop etc of the data. Port parameters refers to word
239 * length, flow mode etc of the port
241 ret = bus->port_ops->dpn_set_port_transport_params(bus,
242 &p_rt->transport_params,
243 bus->params.next_bank);
247 return bus->port_ops->dpn_set_port_params(bus,
249 bus->params.next_bank);
253 * sdw_program_port_params() - Programs transport parameters of Master(s)
256 * @m_rt: Master stream runtime
258 static int sdw_program_port_params(struct sdw_master_runtime *m_rt)
260 struct sdw_slave_runtime *s_rt = NULL;
261 struct sdw_bus *bus = m_rt->bus;
262 struct sdw_port_runtime *p_rt;
265 /* Program transport & port parameters for Slave(s) */
266 list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) {
267 list_for_each_entry(p_rt, &s_rt->port_list, port_node) {
268 ret = sdw_program_slave_port_params(bus, s_rt, p_rt);
274 /* Program transport & port parameters for Master(s) */
275 list_for_each_entry(p_rt, &m_rt->port_list, port_node) {
276 ret = sdw_program_master_port_params(bus, p_rt);
285 * sdw_enable_disable_slave_ports: Enable/disable slave data port
288 * @s_rt: slave runtime
289 * @p_rt: port runtime
290 * @en: enable or disable operation
292 * This function only sets the enable/disable bits in the relevant bank, the
293 * actual enable/disable is done with a bank switch
295 static int sdw_enable_disable_slave_ports(struct sdw_bus *bus,
296 struct sdw_slave_runtime *s_rt,
297 struct sdw_port_runtime *p_rt,
300 struct sdw_transport_params *t_params = &p_rt->transport_params;
304 if (bus->params.next_bank)
305 addr = SDW_DPN_CHANNELEN_B1(p_rt->num);
307 addr = SDW_DPN_CHANNELEN_B0(p_rt->num);
310 * Since bus doesn't support sharing a port across two streams,
311 * it is safe to reset this register
314 ret = sdw_update(s_rt->slave, addr, 0xFF, p_rt->ch_mask);
316 ret = sdw_update(s_rt->slave, addr, 0xFF, 0x0);
319 dev_err(&s_rt->slave->dev,
320 "Slave chn_en reg write failed:%d port:%d\n",
321 ret, t_params->port_num);
326 static int sdw_enable_disable_master_ports(struct sdw_master_runtime *m_rt,
327 struct sdw_port_runtime *p_rt,
330 struct sdw_transport_params *t_params = &p_rt->transport_params;
331 struct sdw_bus *bus = m_rt->bus;
332 struct sdw_enable_ch enable_ch;
335 enable_ch.port_num = p_rt->num;
336 enable_ch.ch_mask = p_rt->ch_mask;
337 enable_ch.enable = en;
339 /* Perform Master port channel(s) enable/disable */
340 if (bus->port_ops->dpn_port_enable_ch) {
341 ret = bus->port_ops->dpn_port_enable_ch(bus,
343 bus->params.next_bank);
346 "Master chn_en write failed:%d port:%d\n",
347 ret, t_params->port_num);
352 "dpn_port_enable_ch not supported, %s failed\n",
353 en ? "enable" : "disable");
361 * sdw_enable_disable_ports() - Enable/disable port(s) for Master and
364 * @m_rt: Master stream runtime
365 * @en: mode (enable/disable)
367 static int sdw_enable_disable_ports(struct sdw_master_runtime *m_rt, bool en)
369 struct sdw_port_runtime *s_port, *m_port;
370 struct sdw_slave_runtime *s_rt = NULL;
373 /* Enable/Disable Slave port(s) */
374 list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) {
375 list_for_each_entry(s_port, &s_rt->port_list, port_node) {
376 ret = sdw_enable_disable_slave_ports(m_rt->bus, s_rt,
383 /* Enable/Disable Master port(s) */
384 list_for_each_entry(m_port, &m_rt->port_list, port_node) {
385 ret = sdw_enable_disable_master_ports(m_rt, m_port, en);
393 static int sdw_do_port_prep(struct sdw_slave_runtime *s_rt,
394 struct sdw_prepare_ch prep_ch,
395 enum sdw_port_prep_ops cmd)
397 const struct sdw_slave_ops *ops = s_rt->slave->ops;
400 if (ops->port_prep) {
401 ret = ops->port_prep(s_rt->slave, &prep_ch, cmd);
403 dev_err(&s_rt->slave->dev,
404 "Slave Port Prep cmd %d failed: %d\n",
413 static int sdw_prep_deprep_slave_ports(struct sdw_bus *bus,
414 struct sdw_slave_runtime *s_rt,
415 struct sdw_port_runtime *p_rt,
418 struct completion *port_ready = NULL;
419 struct sdw_dpn_prop *dpn_prop;
420 struct sdw_prepare_ch prep_ch;
421 unsigned int time_left;
426 prep_ch.num = p_rt->num;
427 prep_ch.ch_mask = p_rt->ch_mask;
429 dpn_prop = sdw_get_slave_dpn_prop(s_rt->slave,
434 "Slave Port:%d properties not found\n", prep_ch.num);
438 prep_ch.prepare = prep;
440 prep_ch.bank = bus->params.next_bank;
442 if (dpn_prop->device_interrupts || !dpn_prop->simple_ch_prep_sm)
446 * Enable interrupt before Port prepare.
447 * For Port de-prepare, it is assumed that port
448 * was prepared earlier
451 ret = sdw_configure_dpn_intr(s_rt->slave, p_rt->num, prep,
452 dpn_prop->device_interrupts);
457 /* Inform slave about the impending port prepare */
458 sdw_do_port_prep(s_rt, prep_ch, SDW_OPS_PORT_PRE_PREP);
460 /* Prepare Slave port implementing CP_SM */
461 if (!dpn_prop->simple_ch_prep_sm) {
462 addr = SDW_DPN_PREPARECTRL(p_rt->num);
465 ret = sdw_update(s_rt->slave, addr,
466 0xFF, p_rt->ch_mask);
468 ret = sdw_update(s_rt->slave, addr, 0xFF, 0x0);
471 dev_err(&s_rt->slave->dev,
472 "Slave prep_ctrl reg write failed\n");
476 /* Wait for completion on port ready */
477 port_ready = &s_rt->slave->port_ready[prep_ch.num];
478 time_left = wait_for_completion_timeout(port_ready,
479 msecs_to_jiffies(dpn_prop->ch_prep_timeout));
481 val = sdw_read(s_rt->slave, SDW_DPN_PREPARESTATUS(p_rt->num));
482 val &= p_rt->ch_mask;
483 if (!time_left || val) {
484 dev_err(&s_rt->slave->dev,
485 "Chn prep failed for port:%d\n", prep_ch.num);
490 /* Inform slaves about ports prepared */
491 sdw_do_port_prep(s_rt, prep_ch, SDW_OPS_PORT_POST_PREP);
493 /* Disable interrupt after Port de-prepare */
495 ret = sdw_configure_dpn_intr(s_rt->slave, p_rt->num, prep,
496 dpn_prop->device_interrupts);
501 static int sdw_prep_deprep_master_ports(struct sdw_master_runtime *m_rt,
502 struct sdw_port_runtime *p_rt,
505 struct sdw_transport_params *t_params = &p_rt->transport_params;
506 struct sdw_bus *bus = m_rt->bus;
507 const struct sdw_master_port_ops *ops = bus->port_ops;
508 struct sdw_prepare_ch prep_ch;
511 prep_ch.num = p_rt->num;
512 prep_ch.ch_mask = p_rt->ch_mask;
513 prep_ch.prepare = prep; /* Prepare/De-prepare */
514 prep_ch.bank = bus->params.next_bank;
516 /* Pre-prepare/Pre-deprepare port(s) */
517 if (ops->dpn_port_prep) {
518 ret = ops->dpn_port_prep(bus, &prep_ch);
520 dev_err(bus->dev, "Port prepare failed for port:%d\n",
530 * sdw_prep_deprep_ports() - Prepare/De-prepare port(s) for Master(s) and
533 * @m_rt: Master runtime handle
534 * @prep: Prepare or De-prepare
536 static int sdw_prep_deprep_ports(struct sdw_master_runtime *m_rt, bool prep)
538 struct sdw_slave_runtime *s_rt = NULL;
539 struct sdw_port_runtime *p_rt;
542 /* Prepare/De-prepare Slave port(s) */
543 list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) {
544 list_for_each_entry(p_rt, &s_rt->port_list, port_node) {
545 ret = sdw_prep_deprep_slave_ports(m_rt->bus, s_rt,
552 /* Prepare/De-prepare Master port(s) */
553 list_for_each_entry(p_rt, &m_rt->port_list, port_node) {
554 ret = sdw_prep_deprep_master_ports(m_rt, p_rt, prep);
563 * sdw_notify_config() - Notify bus configuration
565 * @m_rt: Master runtime handle
567 * This function notifies the Master(s) and Slave(s) of the
568 * new bus configuration.
570 static int sdw_notify_config(struct sdw_master_runtime *m_rt)
572 struct sdw_slave_runtime *s_rt;
573 struct sdw_bus *bus = m_rt->bus;
574 struct sdw_slave *slave;
577 if (bus->ops->set_bus_conf) {
578 ret = bus->ops->set_bus_conf(bus, &bus->params);
583 list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) {
586 if (slave->ops->bus_config) {
587 ret = slave->ops->bus_config(slave, &bus->params);
589 dev_err(bus->dev, "Notify Slave: %d failed\n",
599 * sdw_program_params() - Program transport and port parameters for Master(s)
602 * @bus: SDW bus instance
604 static int sdw_program_params(struct sdw_bus *bus)
606 struct sdw_master_runtime *m_rt = NULL;
609 list_for_each_entry(m_rt, &bus->m_rt_list, bus_node) {
610 ret = sdw_program_port_params(m_rt);
613 "Program transport params failed: %d\n", ret);
617 ret = sdw_notify_config(m_rt);
620 "Notify bus config failed: %d\n", ret);
624 /* Enable port(s) on alternate bank for all active streams */
625 if (m_rt->stream->state != SDW_STREAM_ENABLED)
628 ret = sdw_enable_disable_ports(m_rt, true);
630 dev_err(bus->dev, "Enable channel failed: %d\n", ret);
638 static int sdw_bank_switch(struct sdw_bus *bus, int m_rt_count)
640 int col_index, row_index;
642 struct sdw_msg *wr_msg;
647 wr_msg = kzalloc(sizeof(*wr_msg), GFP_KERNEL);
651 bus->defer_msg.msg = wr_msg;
653 wbuf = kzalloc(sizeof(*wbuf), GFP_KERNEL);
659 /* Get row and column index to program register */
660 col_index = sdw_find_col_index(bus->params.col);
661 row_index = sdw_find_row_index(bus->params.row);
662 wbuf[0] = col_index | (row_index << 3);
664 if (bus->params.next_bank)
665 addr = SDW_SCP_FRAMECTRL_B1;
667 addr = SDW_SCP_FRAMECTRL_B0;
669 sdw_fill_msg(wr_msg, NULL, addr, 1, SDW_BROADCAST_DEV_NUM,
670 SDW_MSG_FLAG_WRITE, wbuf);
671 wr_msg->ssp_sync = true;
674 * Set the multi_link flag only when both the hardware supports
675 * and there is a stream handled by multiple masters
677 multi_link = bus->multi_link && (m_rt_count > 1);
680 ret = sdw_transfer_defer(bus, wr_msg, &bus->defer_msg);
682 ret = sdw_transfer(bus, wr_msg);
685 dev_err(bus->dev, "Slave frame_ctrl reg write failed\n");
692 bus->defer_msg.msg = NULL;
693 bus->params.curr_bank = !bus->params.curr_bank;
694 bus->params.next_bank = !bus->params.next_bank;
707 * sdw_ml_sync_bank_switch: Multilink register bank switch
709 * @bus: SDW bus instance
711 * Caller function should free the buffers on error
713 static int sdw_ml_sync_bank_switch(struct sdw_bus *bus)
715 unsigned long time_left;
717 if (!bus->multi_link)
720 /* Wait for completion of transfer */
721 time_left = wait_for_completion_timeout(&bus->defer_msg.complete,
722 bus->bank_switch_timeout);
725 dev_err(bus->dev, "Controller Timed out on bank switch\n");
729 bus->params.curr_bank = !bus->params.curr_bank;
730 bus->params.next_bank = !bus->params.next_bank;
732 if (bus->defer_msg.msg) {
733 kfree(bus->defer_msg.msg->buf);
734 kfree(bus->defer_msg.msg);
740 static int do_bank_switch(struct sdw_stream_runtime *stream)
742 struct sdw_master_runtime *m_rt = NULL;
743 const struct sdw_master_ops *ops;
744 struct sdw_bus *bus = NULL;
745 bool multi_link = false;
748 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
752 if (bus->multi_link) {
754 mutex_lock(&bus->msg_lock);
757 /* Pre-bank switch */
758 if (ops->pre_bank_switch) {
759 ret = ops->pre_bank_switch(bus);
762 "Pre bank switch op failed: %d\n", ret);
768 * Perform Bank switch operation.
769 * For multi link cases, the actual bank switch is
770 * synchronized across all Masters and happens later as a
771 * part of post_bank_switch ops.
773 ret = sdw_bank_switch(bus, stream->m_rt_count);
775 dev_err(bus->dev, "Bank switch failed: %d\n", ret);
781 * For multi link cases, it is expected that the bank switch is
782 * triggered by the post_bank_switch for the first Master in the list
783 * and for the other Masters the post_bank_switch() should return doing
786 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
790 /* Post-bank switch */
791 if (ops->post_bank_switch) {
792 ret = ops->post_bank_switch(bus);
795 "Post bank switch op failed: %d\n",
799 } else if (bus->multi_link && stream->m_rt_count > 1) {
801 "Post bank switch ops not implemented\n");
805 /* Set the bank switch timeout to default, if not set */
806 if (!bus->bank_switch_timeout)
807 bus->bank_switch_timeout = DEFAULT_BANK_SWITCH_TIMEOUT;
809 /* Check if bank switch was successful */
810 ret = sdw_ml_sync_bank_switch(bus);
813 "multi link bank switch failed: %d\n", ret);
817 mutex_unlock(&bus->msg_lock);
823 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
826 kfree(bus->defer_msg.msg->buf);
827 kfree(bus->defer_msg.msg);
833 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
835 if (mutex_is_locked(&bus->msg_lock))
836 mutex_unlock(&bus->msg_lock);
844 * sdw_release_stream() - Free the assigned stream runtime
846 * @stream: SoundWire stream runtime
848 * sdw_release_stream should be called only once per stream
850 void sdw_release_stream(struct sdw_stream_runtime *stream)
854 EXPORT_SYMBOL(sdw_release_stream);
857 * sdw_alloc_stream() - Allocate and return stream runtime
859 * @stream_name: SoundWire stream name
861 * Allocates a SoundWire stream runtime instance.
862 * sdw_alloc_stream should be called only once per stream. Typically
863 * invoked from ALSA/ASoC machine/platform driver.
865 struct sdw_stream_runtime *sdw_alloc_stream(char *stream_name)
867 struct sdw_stream_runtime *stream;
869 stream = kzalloc(sizeof(*stream), GFP_KERNEL);
873 stream->name = stream_name;
874 INIT_LIST_HEAD(&stream->master_list);
875 stream->state = SDW_STREAM_ALLOCATED;
876 stream->m_rt_count = 0;
880 EXPORT_SYMBOL(sdw_alloc_stream);
882 static struct sdw_master_runtime
883 *sdw_find_master_rt(struct sdw_bus *bus,
884 struct sdw_stream_runtime *stream)
886 struct sdw_master_runtime *m_rt = NULL;
888 /* Retrieve Bus handle if already available */
889 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
890 if (m_rt->bus == bus)
898 * sdw_alloc_master_rt() - Allocates and initialize Master runtime handle
900 * @bus: SDW bus instance
901 * @stream_config: Stream configuration
902 * @stream: Stream runtime handle.
904 * This function is to be called with bus_lock held.
906 static struct sdw_master_runtime
907 *sdw_alloc_master_rt(struct sdw_bus *bus,
908 struct sdw_stream_config *stream_config,
909 struct sdw_stream_runtime *stream)
911 struct sdw_master_runtime *m_rt;
914 * check if Master is already allocated (as a result of Slave adding
915 * it first), if so skip allocation and go to configure
917 m_rt = sdw_find_master_rt(bus, stream);
921 m_rt = kzalloc(sizeof(*m_rt), GFP_KERNEL);
925 /* Initialization of Master runtime handle */
926 INIT_LIST_HEAD(&m_rt->port_list);
927 INIT_LIST_HEAD(&m_rt->slave_rt_list);
928 list_add_tail(&m_rt->stream_node, &stream->master_list);
930 list_add_tail(&m_rt->bus_node, &bus->m_rt_list);
933 m_rt->ch_count = stream_config->ch_count;
935 m_rt->stream = stream;
936 m_rt->direction = stream_config->direction;
942 * sdw_alloc_slave_rt() - Allocate and initialize Slave runtime handle.
944 * @slave: Slave handle
945 * @stream_config: Stream configuration
946 * @stream: Stream runtime handle
948 * This function is to be called with bus_lock held.
950 static struct sdw_slave_runtime
951 *sdw_alloc_slave_rt(struct sdw_slave *slave,
952 struct sdw_stream_config *stream_config,
953 struct sdw_stream_runtime *stream)
955 struct sdw_slave_runtime *s_rt = NULL;
957 s_rt = kzalloc(sizeof(*s_rt), GFP_KERNEL);
961 INIT_LIST_HEAD(&s_rt->port_list);
962 s_rt->ch_count = stream_config->ch_count;
963 s_rt->direction = stream_config->direction;
969 static void sdw_master_port_release(struct sdw_bus *bus,
970 struct sdw_master_runtime *m_rt)
972 struct sdw_port_runtime *p_rt, *_p_rt;
974 list_for_each_entry_safe(p_rt, _p_rt, &m_rt->port_list, port_node) {
975 list_del(&p_rt->port_node);
980 static void sdw_slave_port_release(struct sdw_bus *bus,
981 struct sdw_slave *slave,
982 struct sdw_stream_runtime *stream)
984 struct sdw_port_runtime *p_rt, *_p_rt;
985 struct sdw_master_runtime *m_rt;
986 struct sdw_slave_runtime *s_rt;
988 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
989 list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) {
990 if (s_rt->slave != slave)
993 list_for_each_entry_safe(p_rt, _p_rt,
994 &s_rt->port_list, port_node) {
995 list_del(&p_rt->port_node);
1003 * sdw_release_slave_stream() - Free Slave(s) runtime handle
1005 * @slave: Slave handle.
1006 * @stream: Stream runtime handle.
1008 * This function is to be called with bus_lock held.
1010 static void sdw_release_slave_stream(struct sdw_slave *slave,
1011 struct sdw_stream_runtime *stream)
1013 struct sdw_slave_runtime *s_rt, *_s_rt;
1014 struct sdw_master_runtime *m_rt;
1016 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
1017 /* Retrieve Slave runtime handle */
1018 list_for_each_entry_safe(s_rt, _s_rt,
1019 &m_rt->slave_rt_list, m_rt_node) {
1020 if (s_rt->slave == slave) {
1021 list_del(&s_rt->m_rt_node);
1030 * sdw_release_master_stream() - Free Master runtime handle
1032 * @m_rt: Master runtime node
1033 * @stream: Stream runtime handle.
1035 * This function is to be called with bus_lock held
1036 * It frees the Master runtime handle and associated Slave(s) runtime
1037 * handle. If this is called first then sdw_release_slave_stream() will have
1038 * no effect as Slave(s) runtime handle would already be freed up.
1040 static void sdw_release_master_stream(struct sdw_master_runtime *m_rt,
1041 struct sdw_stream_runtime *stream)
1043 struct sdw_slave_runtime *s_rt, *_s_rt;
1045 list_for_each_entry_safe(s_rt, _s_rt, &m_rt->slave_rt_list, m_rt_node) {
1046 sdw_slave_port_release(s_rt->slave->bus, s_rt->slave, stream);
1047 sdw_release_slave_stream(s_rt->slave, stream);
1050 list_del(&m_rt->stream_node);
1051 list_del(&m_rt->bus_node);
1056 * sdw_stream_remove_master() - Remove master from sdw_stream
1058 * @bus: SDW Bus instance
1059 * @stream: SoundWire stream
1061 * This removes and frees port_rt and master_rt from a stream
1063 int sdw_stream_remove_master(struct sdw_bus *bus,
1064 struct sdw_stream_runtime *stream)
1066 struct sdw_master_runtime *m_rt, *_m_rt;
1068 mutex_lock(&bus->bus_lock);
1070 list_for_each_entry_safe(m_rt, _m_rt,
1071 &stream->master_list, stream_node) {
1072 if (m_rt->bus != bus)
1075 sdw_master_port_release(bus, m_rt);
1076 sdw_release_master_stream(m_rt, stream);
1077 stream->m_rt_count--;
1080 if (list_empty(&stream->master_list))
1081 stream->state = SDW_STREAM_RELEASED;
1083 mutex_unlock(&bus->bus_lock);
1087 EXPORT_SYMBOL(sdw_stream_remove_master);
1090 * sdw_stream_remove_slave() - Remove slave from sdw_stream
1092 * @slave: SDW Slave instance
1093 * @stream: SoundWire stream
1095 * This removes and frees port_rt and slave_rt from a stream
1097 int sdw_stream_remove_slave(struct sdw_slave *slave,
1098 struct sdw_stream_runtime *stream)
1100 mutex_lock(&slave->bus->bus_lock);
1102 sdw_slave_port_release(slave->bus, slave, stream);
1103 sdw_release_slave_stream(slave, stream);
1105 mutex_unlock(&slave->bus->bus_lock);
1109 EXPORT_SYMBOL(sdw_stream_remove_slave);
1112 * sdw_config_stream() - Configure the allocated stream
1115 * @stream: SoundWire stream
1116 * @stream_config: Stream configuration for audio stream
1117 * @is_slave: is API called from Slave or Master
1119 * This function is to be called with bus_lock held.
1121 static int sdw_config_stream(struct device *dev,
1122 struct sdw_stream_runtime *stream,
1123 struct sdw_stream_config *stream_config,
1127 * Update the stream rate, channel and bps based on data
1128 * source. For more than one data source (multilink),
1129 * match the rate, bps, stream type and increment number of channels.
1131 * If rate/bps is zero, it means the values are not set, so skip
1132 * comparison and allow the value to be set and stored in stream
1134 if (stream->params.rate &&
1135 stream->params.rate != stream_config->frame_rate) {
1136 dev_err(dev, "rate not matching, stream:%s\n", stream->name);
1140 if (stream->params.bps &&
1141 stream->params.bps != stream_config->bps) {
1142 dev_err(dev, "bps not matching, stream:%s\n", stream->name);
1146 stream->type = stream_config->type;
1147 stream->params.rate = stream_config->frame_rate;
1148 stream->params.bps = stream_config->bps;
1150 /* TODO: Update this check during Device-device support */
1152 stream->params.ch_count += stream_config->ch_count;
1157 static int sdw_is_valid_port_range(struct device *dev,
1158 struct sdw_port_runtime *p_rt)
1160 if (!SDW_VALID_PORT_RANGE(p_rt->num)) {
1162 "SoundWire: Invalid port number :%d\n", p_rt->num);
1169 static struct sdw_port_runtime
1170 *sdw_port_alloc(struct device *dev,
1171 struct sdw_port_config *port_config,
1174 struct sdw_port_runtime *p_rt;
1176 p_rt = kzalloc(sizeof(*p_rt), GFP_KERNEL);
1180 p_rt->ch_mask = port_config[port_index].ch_mask;
1181 p_rt->num = port_config[port_index].num;
1186 static int sdw_master_port_config(struct sdw_bus *bus,
1187 struct sdw_master_runtime *m_rt,
1188 struct sdw_port_config *port_config,
1189 unsigned int num_ports)
1191 struct sdw_port_runtime *p_rt;
1194 /* Iterate for number of ports to perform initialization */
1195 for (i = 0; i < num_ports; i++) {
1196 p_rt = sdw_port_alloc(bus->dev, port_config, i);
1201 * TODO: Check port capabilities for requested
1202 * configuration (audio mode support)
1205 list_add_tail(&p_rt->port_node, &m_rt->port_list);
1211 static int sdw_slave_port_config(struct sdw_slave *slave,
1212 struct sdw_slave_runtime *s_rt,
1213 struct sdw_port_config *port_config,
1214 unsigned int num_config)
1216 struct sdw_port_runtime *p_rt;
1219 /* Iterate for number of ports to perform initialization */
1220 for (i = 0; i < num_config; i++) {
1221 p_rt = sdw_port_alloc(&slave->dev, port_config, i);
1226 * TODO: Check valid port range as defined by DisCo/
1229 ret = sdw_is_valid_port_range(&slave->dev, p_rt);
1236 * TODO: Check port capabilities for requested
1237 * configuration (audio mode support)
1240 list_add_tail(&p_rt->port_node, &s_rt->port_list);
1247 * sdw_stream_add_master() - Allocate and add master runtime to a stream
1249 * @bus: SDW Bus instance
1250 * @stream_config: Stream configuration for audio stream
1251 * @port_config: Port configuration for audio stream
1252 * @num_ports: Number of ports
1253 * @stream: SoundWire stream
1255 int sdw_stream_add_master(struct sdw_bus *bus,
1256 struct sdw_stream_config *stream_config,
1257 struct sdw_port_config *port_config,
1258 unsigned int num_ports,
1259 struct sdw_stream_runtime *stream)
1261 struct sdw_master_runtime *m_rt = NULL;
1264 mutex_lock(&bus->bus_lock);
1267 * For multi link streams, add the second master only if
1268 * the bus supports it.
1269 * Check if bus->multi_link is set
1271 if (!bus->multi_link && stream->m_rt_count > 0) {
1273 "Multilink not supported, link %d\n", bus->link_id);
1278 m_rt = sdw_alloc_master_rt(bus, stream_config, stream);
1281 "Master runtime config failed for stream:%s\n",
1287 ret = sdw_config_stream(bus->dev, stream, stream_config, false);
1291 ret = sdw_master_port_config(bus, m_rt, port_config, num_ports);
1295 stream->m_rt_count++;
1300 sdw_release_master_stream(m_rt, stream);
1302 mutex_unlock(&bus->bus_lock);
1305 EXPORT_SYMBOL(sdw_stream_add_master);
1308 * sdw_stream_add_slave() - Allocate and add master/slave runtime to a stream
1310 * @slave: SDW Slave instance
1311 * @stream_config: Stream configuration for audio stream
1312 * @stream: SoundWire stream
1313 * @port_config: Port configuration for audio stream
1314 * @num_ports: Number of ports
1316 * It is expected that Slave is added before adding Master
1320 int sdw_stream_add_slave(struct sdw_slave *slave,
1321 struct sdw_stream_config *stream_config,
1322 struct sdw_port_config *port_config,
1323 unsigned int num_ports,
1324 struct sdw_stream_runtime *stream)
1326 struct sdw_slave_runtime *s_rt;
1327 struct sdw_master_runtime *m_rt;
1330 mutex_lock(&slave->bus->bus_lock);
1333 * If this API is invoked by Slave first then m_rt is not valid.
1334 * So, allocate m_rt and add Slave to it.
1336 m_rt = sdw_alloc_master_rt(slave->bus, stream_config, stream);
1338 dev_err(&slave->dev,
1339 "alloc master runtime failed for stream:%s\n",
1345 s_rt = sdw_alloc_slave_rt(slave, stream_config, stream);
1347 dev_err(&slave->dev,
1348 "Slave runtime config failed for stream:%s\n",
1354 ret = sdw_config_stream(&slave->dev, stream, stream_config, true);
1358 list_add_tail(&s_rt->m_rt_node, &m_rt->slave_rt_list);
1360 ret = sdw_slave_port_config(slave, s_rt, port_config, num_ports);
1365 * Change stream state to CONFIGURED on first Slave add.
1366 * Bus is not aware of number of Slave(s) in a stream at this
1367 * point so cannot depend on all Slave(s) to be added in order to
1368 * change stream state to CONFIGURED.
1370 stream->state = SDW_STREAM_CONFIGURED;
1375 * we hit error so cleanup the stream, release all Slave(s) and
1378 sdw_release_master_stream(m_rt, stream);
1380 mutex_unlock(&slave->bus->bus_lock);
1383 EXPORT_SYMBOL(sdw_stream_add_slave);
1386 * sdw_get_slave_dpn_prop() - Get Slave port capabilities
1388 * @slave: Slave handle
1389 * @direction: Data direction.
1390 * @port_num: Port number
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)
1396 struct sdw_dpn_prop *dpn_prop;
1400 if (direction == SDW_DATA_DIR_TX) {
1401 num_ports = hweight32(slave->prop.source_ports);
1402 dpn_prop = slave->prop.src_dpn_prop;
1404 num_ports = hweight32(slave->prop.sink_ports);
1405 dpn_prop = slave->prop.sink_dpn_prop;
1408 for (i = 0; i < num_ports; i++) {
1409 dpn_prop = &dpn_prop[i];
1411 if (dpn_prop->num == port_num)
1412 return &dpn_prop[i];
1419 * sdw_acquire_bus_lock: Acquire bus lock for all Master runtime(s)
1421 * @stream: SoundWire stream
1423 * Acquire bus_lock for each of the master runtime(m_rt) part of this
1424 * stream to reconfigure the bus.
1425 * NOTE: This function is called from SoundWire stream ops and is
1426 * expected that a global lock is held before acquiring bus_lock.
1428 static void sdw_acquire_bus_lock(struct sdw_stream_runtime *stream)
1430 struct sdw_master_runtime *m_rt = NULL;
1431 struct sdw_bus *bus = NULL;
1433 /* Iterate for all Master(s) in Master list */
1434 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
1437 mutex_lock(&bus->bus_lock);
1442 * sdw_release_bus_lock: Release bus lock for all Master runtime(s)
1444 * @stream: SoundWire stream
1446 * Release the previously held bus_lock after reconfiguring the bus.
1447 * NOTE: This function is called from SoundWire stream ops and is
1448 * expected that a global lock is held before releasing bus_lock.
1450 static void sdw_release_bus_lock(struct sdw_stream_runtime *stream)
1452 struct sdw_master_runtime *m_rt = NULL;
1453 struct sdw_bus *bus = NULL;
1455 /* Iterate for all Master(s) in Master list */
1456 list_for_each_entry_reverse(m_rt, &stream->master_list, stream_node) {
1458 mutex_unlock(&bus->bus_lock);
1462 static int _sdw_prepare_stream(struct sdw_stream_runtime *stream)
1464 struct sdw_master_runtime *m_rt = NULL;
1465 struct sdw_bus *bus = NULL;
1466 struct sdw_master_prop *prop = NULL;
1467 struct sdw_bus_params params;
1470 /* Prepare Master(s) and Slave(s) port(s) associated with stream */
1471 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
1474 memcpy(¶ms, &bus->params, sizeof(params));
1476 /* TODO: Support Asynchronous mode */
1477 if ((prop->max_freq % stream->params.rate) != 0) {
1478 dev_err(bus->dev, "Async mode not supported\n");
1482 /* Increment cumulative bus bandwidth */
1483 /* TODO: Update this during Device-Device support */
1484 bus->params.bandwidth += m_rt->stream->params.rate *
1485 m_rt->ch_count * m_rt->stream->params.bps;
1487 /* Program params */
1488 ret = sdw_program_params(bus);
1490 dev_err(bus->dev, "Program params failed: %d\n", ret);
1491 goto restore_params;
1495 ret = do_bank_switch(stream);
1497 dev_err(bus->dev, "Bank switch failed: %d\n", ret);
1498 goto restore_params;
1501 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
1504 /* Prepare port(s) on the new clock configuration */
1505 ret = sdw_prep_deprep_ports(m_rt, true);
1507 dev_err(bus->dev, "Prepare port(s) failed ret = %d\n",
1513 stream->state = SDW_STREAM_PREPARED;
1518 memcpy(&bus->params, ¶ms, sizeof(params));
1523 * sdw_prepare_stream() - Prepare SoundWire stream
1525 * @stream: Soundwire stream
1527 * Documentation/driver-api/soundwire/stream.rst explains this API in detail
1529 int sdw_prepare_stream(struct sdw_stream_runtime *stream)
1534 pr_err("SoundWire: Handle not found for stream\n");
1538 sdw_acquire_bus_lock(stream);
1540 ret = _sdw_prepare_stream(stream);
1542 pr_err("Prepare for stream:%s failed: %d\n", stream->name, ret);
1544 sdw_release_bus_lock(stream);
1547 EXPORT_SYMBOL(sdw_prepare_stream);
1549 static int _sdw_enable_stream(struct sdw_stream_runtime *stream)
1551 struct sdw_master_runtime *m_rt = NULL;
1552 struct sdw_bus *bus = NULL;
1555 /* Enable Master(s) and Slave(s) port(s) associated with stream */
1556 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
1559 /* Program params */
1560 ret = sdw_program_params(bus);
1562 dev_err(bus->dev, "Program params failed: %d\n", ret);
1566 /* Enable port(s) */
1567 ret = sdw_enable_disable_ports(m_rt, true);
1570 "Enable port(s) failed ret: %d\n", ret);
1575 ret = do_bank_switch(stream);
1577 dev_err(bus->dev, "Bank switch failed: %d\n", ret);
1581 stream->state = SDW_STREAM_ENABLED;
1586 * sdw_enable_stream() - Enable SoundWire stream
1588 * @stream: Soundwire stream
1590 * Documentation/driver-api/soundwire/stream.rst explains this API in detail
1592 int sdw_enable_stream(struct sdw_stream_runtime *stream)
1597 pr_err("SoundWire: Handle not found for stream\n");
1601 sdw_acquire_bus_lock(stream);
1603 ret = _sdw_enable_stream(stream);
1605 pr_err("Enable for stream:%s failed: %d\n", stream->name, ret);
1607 sdw_release_bus_lock(stream);
1610 EXPORT_SYMBOL(sdw_enable_stream);
1612 static int _sdw_disable_stream(struct sdw_stream_runtime *stream)
1614 struct sdw_master_runtime *m_rt = NULL;
1615 struct sdw_bus *bus = NULL;
1618 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
1620 /* Disable port(s) */
1621 ret = sdw_enable_disable_ports(m_rt, false);
1623 dev_err(bus->dev, "Disable port(s) failed: %d\n", ret);
1627 stream->state = SDW_STREAM_DISABLED;
1629 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
1631 /* Program params */
1632 ret = sdw_program_params(bus);
1634 dev_err(bus->dev, "Program params failed: %d\n", ret);
1639 return do_bank_switch(stream);
1643 * sdw_disable_stream() - Disable SoundWire stream
1645 * @stream: Soundwire stream
1647 * Documentation/driver-api/soundwire/stream.rst explains this API in detail
1649 int sdw_disable_stream(struct sdw_stream_runtime *stream)
1654 pr_err("SoundWire: Handle not found for stream\n");
1658 sdw_acquire_bus_lock(stream);
1660 ret = _sdw_disable_stream(stream);
1662 pr_err("Disable for stream:%s failed: %d\n", stream->name, ret);
1664 sdw_release_bus_lock(stream);
1667 EXPORT_SYMBOL(sdw_disable_stream);
1669 static int _sdw_deprepare_stream(struct sdw_stream_runtime *stream)
1671 struct sdw_master_runtime *m_rt = NULL;
1672 struct sdw_bus *bus = NULL;
1675 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
1677 /* De-prepare port(s) */
1678 ret = sdw_prep_deprep_ports(m_rt, false);
1681 "De-prepare port(s) failed: %d\n", ret);
1685 /* TODO: Update this during Device-Device support */
1686 bus->params.bandwidth -= m_rt->stream->params.rate *
1687 m_rt->ch_count * m_rt->stream->params.bps;
1689 /* Program params */
1690 ret = sdw_program_params(bus);
1692 dev_err(bus->dev, "Program params failed: %d\n", ret);
1697 stream->state = SDW_STREAM_DEPREPARED;
1698 return do_bank_switch(stream);
1702 * sdw_deprepare_stream() - Deprepare SoundWire stream
1704 * @stream: Soundwire stream
1706 * Documentation/driver-api/soundwire/stream.rst explains this API in detail
1708 int sdw_deprepare_stream(struct sdw_stream_runtime *stream)
1713 pr_err("SoundWire: Handle not found for stream\n");
1717 sdw_acquire_bus_lock(stream);
1718 ret = _sdw_deprepare_stream(stream);
1720 pr_err("De-prepare for stream:%d failed: %d\n", ret, ret);
1722 sdw_release_bus_lock(stream);
1725 EXPORT_SYMBOL(sdw_deprepare_stream);