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 sdw_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 sdw_cols[SDW_FRAME_COLS] = {2, 4, 6, 8, 10, 12, 14, 16};
30 int sdw_find_col_index(int col)
34 for (i = 0; i < SDW_FRAME_COLS; i++) {
35 if (sdw_cols[i] == col)
39 pr_warn("Requested column not found, selecting lowest column no: 2\n");
42 EXPORT_SYMBOL(sdw_find_col_index);
44 int sdw_find_row_index(int row)
48 for (i = 0; i < SDW_FRAME_ROWS; i++) {
49 if (sdw_rows[i] == row)
53 pr_warn("Requested row not found, selecting lowest row no: 48\n");
56 EXPORT_SYMBOL(sdw_find_row_index);
58 static int _sdw_program_slave_port_params(struct sdw_bus *bus,
59 struct sdw_slave *slave,
60 struct sdw_transport_params *t_params,
61 enum sdw_dpn_type type)
63 u32 addr1, addr2, addr3, addr4;
67 if (bus->params.next_bank) {
68 addr1 = SDW_DPN_OFFSETCTRL2_B1(t_params->port_num);
69 addr2 = SDW_DPN_BLOCKCTRL3_B1(t_params->port_num);
70 addr3 = SDW_DPN_SAMPLECTRL2_B1(t_params->port_num);
71 addr4 = SDW_DPN_HCTRL_B1(t_params->port_num);
73 addr1 = SDW_DPN_OFFSETCTRL2_B0(t_params->port_num);
74 addr2 = SDW_DPN_BLOCKCTRL3_B0(t_params->port_num);
75 addr3 = SDW_DPN_SAMPLECTRL2_B0(t_params->port_num);
76 addr4 = SDW_DPN_HCTRL_B0(t_params->port_num);
79 /* Program DPN_OffsetCtrl2 registers */
80 ret = sdw_write(slave, addr1, t_params->offset2);
82 dev_err(bus->dev, "DPN_OffsetCtrl2 register write failed\n");
86 /* Program DPN_BlockCtrl3 register */
87 ret = sdw_write(slave, addr2, t_params->blk_pkg_mode);
89 dev_err(bus->dev, "DPN_BlockCtrl3 register write failed\n");
94 * Data ports are FULL, SIMPLE and REDUCED. This function handles
95 * FULL and REDUCED only and beyond this point only FULL is
96 * handled, so bail out if we are not FULL data port type
98 if (type != SDW_DPN_FULL)
101 /* Program DPN_SampleCtrl2 register */
102 wbuf = (t_params->sample_interval - 1);
103 wbuf &= SDW_DPN_SAMPLECTRL_HIGH;
104 wbuf >>= SDW_REG_SHIFT(SDW_DPN_SAMPLECTRL_HIGH);
106 ret = sdw_write(slave, addr3, wbuf);
108 dev_err(bus->dev, "DPN_SampleCtrl2 register write failed\n");
112 /* Program DPN_HCtrl register */
113 wbuf = t_params->hstart;
114 wbuf <<= SDW_REG_SHIFT(SDW_DPN_HCTRL_HSTART);
115 wbuf |= 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 dpn_prop = sdw_get_slave_dpn_prop(s_rt->slave,
142 addr1 = SDW_DPN_PORTCTRL(t_params->port_num);
143 addr2 = SDW_DPN_BLOCKCTRL1(t_params->port_num);
145 if (bus->params.next_bank) {
146 addr3 = SDW_DPN_SAMPLECTRL1_B1(t_params->port_num);
147 addr4 = SDW_DPN_OFFSETCTRL1_B1(t_params->port_num);
148 addr5 = SDW_DPN_BLOCKCTRL2_B1(t_params->port_num);
149 addr6 = SDW_DPN_LANECTRL_B1(t_params->port_num);
152 addr3 = SDW_DPN_SAMPLECTRL1_B0(t_params->port_num);
153 addr4 = SDW_DPN_OFFSETCTRL1_B0(t_params->port_num);
154 addr5 = SDW_DPN_BLOCKCTRL2_B0(t_params->port_num);
155 addr6 = SDW_DPN_LANECTRL_B0(t_params->port_num);
158 /* Program DPN_PortCtrl register */
159 wbuf = p_params->data_mode << SDW_REG_SHIFT(SDW_DPN_PORTCTRL_DATAMODE);
160 wbuf |= p_params->flow_mode;
162 ret = sdw_update(s_rt->slave, addr1, 0xF, wbuf);
164 dev_err(&s_rt->slave->dev,
165 "DPN_PortCtrl register write failed for port %d\n",
170 if (!dpn_prop->read_only_wordlength) {
171 /* Program DPN_BlockCtrl1 register */
172 ret = sdw_write(s_rt->slave, addr2, (p_params->bps - 1));
174 dev_err(&s_rt->slave->dev,
175 "DPN_BlockCtrl1 register write failed for port %d\n",
181 /* Program DPN_SampleCtrl1 register */
182 wbuf = (t_params->sample_interval - 1) & SDW_DPN_SAMPLECTRL_LOW;
183 ret = sdw_write(s_rt->slave, addr3, wbuf);
185 dev_err(&s_rt->slave->dev,
186 "DPN_SampleCtrl1 register write failed for port %d\n",
191 /* Program DPN_OffsetCtrl1 registers */
192 ret = sdw_write(s_rt->slave, addr4, t_params->offset1);
194 dev_err(&s_rt->slave->dev,
195 "DPN_OffsetCtrl1 register write failed for port %d\n",
200 /* Program DPN_BlockCtrl2 register*/
201 if (t_params->blk_grp_ctrl_valid) {
202 ret = sdw_write(s_rt->slave, addr5, t_params->blk_grp_ctrl);
204 dev_err(&s_rt->slave->dev,
205 "DPN_BlockCtrl2 reg write failed for port %d\n",
211 /* program DPN_LaneCtrl register */
212 if (slave_prop->lane_control_support) {
213 ret = sdw_write(s_rt->slave, addr6, t_params->lane_ctrl);
215 dev_err(&s_rt->slave->dev,
216 "DPN_LaneCtrl register write failed for port %d\n",
222 if (dpn_prop->type != SDW_DPN_SIMPLE) {
223 ret = _sdw_program_slave_port_params(bus, s_rt->slave,
224 t_params, dpn_prop->type);
226 dev_err(&s_rt->slave->dev,
227 "Transport reg write failed for port: %d\n",
234 static int sdw_program_master_port_params(struct sdw_bus *bus,
235 struct sdw_port_runtime *p_rt)
240 * we need to set transport and port parameters for the port.
241 * Transport parameters refers to the sample interval, offsets and
242 * hstart/stop etc of the data. Port parameters refers to word
243 * length, flow mode etc of the port
245 ret = bus->port_ops->dpn_set_port_transport_params(bus,
246 &p_rt->transport_params,
247 bus->params.next_bank);
251 return bus->port_ops->dpn_set_port_params(bus,
253 bus->params.next_bank);
257 * sdw_program_port_params() - Programs transport parameters of Master(s)
260 * @m_rt: Master stream runtime
262 static int sdw_program_port_params(struct sdw_master_runtime *m_rt)
264 struct sdw_slave_runtime *s_rt = NULL;
265 struct sdw_bus *bus = m_rt->bus;
266 struct sdw_port_runtime *p_rt;
269 /* Program transport & port parameters for Slave(s) */
270 list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) {
271 list_for_each_entry(p_rt, &s_rt->port_list, port_node) {
272 ret = sdw_program_slave_port_params(bus, s_rt, p_rt);
278 /* Program transport & port parameters for Master(s) */
279 list_for_each_entry(p_rt, &m_rt->port_list, port_node) {
280 ret = sdw_program_master_port_params(bus, p_rt);
289 * sdw_enable_disable_slave_ports: Enable/disable slave data port
292 * @s_rt: slave runtime
293 * @p_rt: port runtime
294 * @en: enable or disable operation
296 * This function only sets the enable/disable bits in the relevant bank, the
297 * actual enable/disable is done with a bank switch
299 static int sdw_enable_disable_slave_ports(struct sdw_bus *bus,
300 struct sdw_slave_runtime *s_rt,
301 struct sdw_port_runtime *p_rt,
304 struct sdw_transport_params *t_params = &p_rt->transport_params;
308 if (bus->params.next_bank)
309 addr = SDW_DPN_CHANNELEN_B1(p_rt->num);
311 addr = SDW_DPN_CHANNELEN_B0(p_rt->num);
314 * Since bus doesn't support sharing a port across two streams,
315 * it is safe to reset this register
318 ret = sdw_update(s_rt->slave, addr, 0xFF, p_rt->ch_mask);
320 ret = sdw_update(s_rt->slave, addr, 0xFF, 0x0);
323 dev_err(&s_rt->slave->dev,
324 "Slave chn_en reg write failed:%d port:%d\n",
325 ret, t_params->port_num);
330 static int sdw_enable_disable_master_ports(struct sdw_master_runtime *m_rt,
331 struct sdw_port_runtime *p_rt,
334 struct sdw_transport_params *t_params = &p_rt->transport_params;
335 struct sdw_bus *bus = m_rt->bus;
336 struct sdw_enable_ch enable_ch;
339 enable_ch.port_num = p_rt->num;
340 enable_ch.ch_mask = p_rt->ch_mask;
341 enable_ch.enable = en;
343 /* Perform Master port channel(s) enable/disable */
344 if (bus->port_ops->dpn_port_enable_ch) {
345 ret = bus->port_ops->dpn_port_enable_ch(bus,
347 bus->params.next_bank);
350 "Master chn_en write failed:%d port:%d\n",
351 ret, t_params->port_num);
356 "dpn_port_enable_ch not supported, %s failed\n",
357 en ? "enable" : "disable");
365 * sdw_enable_disable_ports() - Enable/disable port(s) for Master and
368 * @m_rt: Master stream runtime
369 * @en: mode (enable/disable)
371 static int sdw_enable_disable_ports(struct sdw_master_runtime *m_rt, bool en)
373 struct sdw_port_runtime *s_port, *m_port;
374 struct sdw_slave_runtime *s_rt;
377 /* Enable/Disable Slave port(s) */
378 list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) {
379 list_for_each_entry(s_port, &s_rt->port_list, port_node) {
380 ret = sdw_enable_disable_slave_ports(m_rt->bus, s_rt,
387 /* Enable/Disable Master port(s) */
388 list_for_each_entry(m_port, &m_rt->port_list, port_node) {
389 ret = sdw_enable_disable_master_ports(m_rt, m_port, en);
397 static int sdw_do_port_prep(struct sdw_slave_runtime *s_rt,
398 struct sdw_prepare_ch prep_ch,
399 enum sdw_port_prep_ops cmd)
401 const struct sdw_slave_ops *ops = s_rt->slave->ops;
404 if (ops->port_prep) {
405 ret = ops->port_prep(s_rt->slave, &prep_ch, cmd);
407 dev_err(&s_rt->slave->dev,
408 "Slave Port Prep cmd %d failed: %d\n",
417 static int sdw_prep_deprep_slave_ports(struct sdw_bus *bus,
418 struct sdw_slave_runtime *s_rt,
419 struct sdw_port_runtime *p_rt,
422 struct completion *port_ready;
423 struct sdw_dpn_prop *dpn_prop;
424 struct sdw_prepare_ch prep_ch;
425 unsigned int time_left;
430 prep_ch.num = p_rt->num;
431 prep_ch.ch_mask = p_rt->ch_mask;
433 dpn_prop = sdw_get_slave_dpn_prop(s_rt->slave,
438 "Slave Port:%d properties not found\n", prep_ch.num);
442 prep_ch.prepare = prep;
444 prep_ch.bank = bus->params.next_bank;
446 if (dpn_prop->imp_def_interrupts || !dpn_prop->simple_ch_prep_sm)
450 * Enable interrupt before Port prepare.
451 * For Port de-prepare, it is assumed that port
452 * was prepared earlier
455 ret = sdw_configure_dpn_intr(s_rt->slave, p_rt->num, prep,
456 dpn_prop->imp_def_interrupts);
461 /* Inform slave about the impending port prepare */
462 sdw_do_port_prep(s_rt, prep_ch, SDW_OPS_PORT_PRE_PREP);
464 /* Prepare Slave port implementing CP_SM */
465 if (!dpn_prop->simple_ch_prep_sm) {
466 addr = SDW_DPN_PREPARECTRL(p_rt->num);
469 ret = sdw_update(s_rt->slave, addr,
470 0xFF, p_rt->ch_mask);
472 ret = sdw_update(s_rt->slave, addr, 0xFF, 0x0);
475 dev_err(&s_rt->slave->dev,
476 "Slave prep_ctrl reg write failed\n");
480 /* Wait for completion on port ready */
481 port_ready = &s_rt->slave->port_ready[prep_ch.num];
482 time_left = wait_for_completion_timeout(port_ready,
483 msecs_to_jiffies(dpn_prop->ch_prep_timeout));
485 val = sdw_read(s_rt->slave, SDW_DPN_PREPARESTATUS(p_rt->num));
486 val &= p_rt->ch_mask;
487 if (!time_left || val) {
488 dev_err(&s_rt->slave->dev,
489 "Chn prep failed for port:%d\n", prep_ch.num);
494 /* Inform slaves about ports prepared */
495 sdw_do_port_prep(s_rt, prep_ch, SDW_OPS_PORT_POST_PREP);
497 /* Disable interrupt after Port de-prepare */
499 ret = sdw_configure_dpn_intr(s_rt->slave, p_rt->num, prep,
500 dpn_prop->imp_def_interrupts);
505 static int sdw_prep_deprep_master_ports(struct sdw_master_runtime *m_rt,
506 struct sdw_port_runtime *p_rt,
509 struct sdw_transport_params *t_params = &p_rt->transport_params;
510 struct sdw_bus *bus = m_rt->bus;
511 const struct sdw_master_port_ops *ops = bus->port_ops;
512 struct sdw_prepare_ch prep_ch;
515 prep_ch.num = p_rt->num;
516 prep_ch.ch_mask = p_rt->ch_mask;
517 prep_ch.prepare = prep; /* Prepare/De-prepare */
518 prep_ch.bank = bus->params.next_bank;
520 /* Pre-prepare/Pre-deprepare port(s) */
521 if (ops->dpn_port_prep) {
522 ret = ops->dpn_port_prep(bus, &prep_ch);
524 dev_err(bus->dev, "Port prepare failed for port:%d\n",
534 * sdw_prep_deprep_ports() - Prepare/De-prepare port(s) for Master(s) and
537 * @m_rt: Master runtime handle
538 * @prep: Prepare or De-prepare
540 static int sdw_prep_deprep_ports(struct sdw_master_runtime *m_rt, bool prep)
542 struct sdw_slave_runtime *s_rt;
543 struct sdw_port_runtime *p_rt;
546 /* Prepare/De-prepare Slave port(s) */
547 list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) {
548 list_for_each_entry(p_rt, &s_rt->port_list, port_node) {
549 ret = sdw_prep_deprep_slave_ports(m_rt->bus, s_rt,
556 /* Prepare/De-prepare Master port(s) */
557 list_for_each_entry(p_rt, &m_rt->port_list, port_node) {
558 ret = sdw_prep_deprep_master_ports(m_rt, p_rt, prep);
567 * sdw_notify_config() - Notify bus configuration
569 * @m_rt: Master runtime handle
571 * This function notifies the Master(s) and Slave(s) of the
572 * new bus configuration.
574 static int sdw_notify_config(struct sdw_master_runtime *m_rt)
576 struct sdw_slave_runtime *s_rt;
577 struct sdw_bus *bus = m_rt->bus;
578 struct sdw_slave *slave;
581 if (bus->ops->set_bus_conf) {
582 ret = bus->ops->set_bus_conf(bus, &bus->params);
587 list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) {
590 if (slave->ops->bus_config) {
591 ret = slave->ops->bus_config(slave, &bus->params);
593 dev_err(bus->dev, "Notify Slave: %d failed\n",
603 * sdw_program_params() - Program transport and port parameters for Master(s)
606 * @bus: SDW bus instance
608 static int sdw_program_params(struct sdw_bus *bus)
610 struct sdw_master_runtime *m_rt;
613 list_for_each_entry(m_rt, &bus->m_rt_list, bus_node) {
614 ret = sdw_program_port_params(m_rt);
617 "Program transport params failed: %d\n", ret);
621 ret = sdw_notify_config(m_rt);
624 "Notify bus config failed: %d\n", ret);
628 /* Enable port(s) on alternate bank for all active streams */
629 if (m_rt->stream->state != SDW_STREAM_ENABLED)
632 ret = sdw_enable_disable_ports(m_rt, true);
634 dev_err(bus->dev, "Enable channel failed: %d\n", ret);
642 static int sdw_bank_switch(struct sdw_bus *bus, int m_rt_count)
644 int col_index, row_index;
646 struct sdw_msg *wr_msg;
651 wr_msg = kzalloc(sizeof(*wr_msg), GFP_KERNEL);
655 bus->defer_msg.msg = wr_msg;
657 wbuf = kzalloc(sizeof(*wbuf), GFP_KERNEL);
663 /* Get row and column index to program register */
664 col_index = sdw_find_col_index(bus->params.col);
665 row_index = sdw_find_row_index(bus->params.row);
666 wbuf[0] = col_index | (row_index << 3);
668 if (bus->params.next_bank)
669 addr = SDW_SCP_FRAMECTRL_B1;
671 addr = SDW_SCP_FRAMECTRL_B0;
673 sdw_fill_msg(wr_msg, NULL, addr, 1, SDW_BROADCAST_DEV_NUM,
674 SDW_MSG_FLAG_WRITE, wbuf);
675 wr_msg->ssp_sync = true;
678 * Set the multi_link flag only when both the hardware supports
679 * and there is a stream handled by multiple masters
681 multi_link = bus->multi_link && (m_rt_count > 1);
684 ret = sdw_transfer_defer(bus, wr_msg, &bus->defer_msg);
686 ret = sdw_transfer(bus, wr_msg);
689 dev_err(bus->dev, "Slave frame_ctrl reg write failed\n");
696 bus->defer_msg.msg = NULL;
697 bus->params.curr_bank = !bus->params.curr_bank;
698 bus->params.next_bank = !bus->params.next_bank;
711 * sdw_ml_sync_bank_switch: Multilink register bank switch
713 * @bus: SDW bus instance
715 * Caller function should free the buffers on error
717 static int sdw_ml_sync_bank_switch(struct sdw_bus *bus)
719 unsigned long time_left;
721 if (!bus->multi_link)
724 /* Wait for completion of transfer */
725 time_left = wait_for_completion_timeout(&bus->defer_msg.complete,
726 bus->bank_switch_timeout);
729 dev_err(bus->dev, "Controller Timed out on bank switch\n");
733 bus->params.curr_bank = !bus->params.curr_bank;
734 bus->params.next_bank = !bus->params.next_bank;
736 if (bus->defer_msg.msg) {
737 kfree(bus->defer_msg.msg->buf);
738 kfree(bus->defer_msg.msg);
744 static int do_bank_switch(struct sdw_stream_runtime *stream)
746 struct sdw_master_runtime *m_rt;
747 const struct sdw_master_ops *ops;
749 bool multi_link = false;
752 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
756 if (bus->multi_link) {
758 mutex_lock(&bus->msg_lock);
761 /* Pre-bank switch */
762 if (ops->pre_bank_switch) {
763 ret = ops->pre_bank_switch(bus);
766 "Pre bank switch op failed: %d\n", ret);
772 * Perform Bank switch operation.
773 * For multi link cases, the actual bank switch is
774 * synchronized across all Masters and happens later as a
775 * part of post_bank_switch ops.
777 ret = sdw_bank_switch(bus, stream->m_rt_count);
779 dev_err(bus->dev, "Bank switch failed: %d\n", ret);
785 * For multi link cases, it is expected that the bank switch is
786 * triggered by the post_bank_switch for the first Master in the list
787 * and for the other Masters the post_bank_switch() should return doing
790 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
794 /* Post-bank switch */
795 if (ops->post_bank_switch) {
796 ret = ops->post_bank_switch(bus);
799 "Post bank switch op failed: %d\n",
803 } else if (bus->multi_link && stream->m_rt_count > 1) {
805 "Post bank switch ops not implemented\n");
809 /* Set the bank switch timeout to default, if not set */
810 if (!bus->bank_switch_timeout)
811 bus->bank_switch_timeout = DEFAULT_BANK_SWITCH_TIMEOUT;
813 /* Check if bank switch was successful */
814 ret = sdw_ml_sync_bank_switch(bus);
817 "multi link bank switch failed: %d\n", ret);
822 mutex_unlock(&bus->msg_lock);
828 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
831 kfree(bus->defer_msg.msg->buf);
832 kfree(bus->defer_msg.msg);
838 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
840 if (mutex_is_locked(&bus->msg_lock))
841 mutex_unlock(&bus->msg_lock);
849 * sdw_release_stream() - Free the assigned stream runtime
851 * @stream: SoundWire stream runtime
853 * sdw_release_stream should be called only once per stream
855 void sdw_release_stream(struct sdw_stream_runtime *stream)
859 EXPORT_SYMBOL(sdw_release_stream);
862 * sdw_alloc_stream() - Allocate and return stream runtime
864 * @stream_name: SoundWire stream name
866 * Allocates a SoundWire stream runtime instance.
867 * sdw_alloc_stream should be called only once per stream. Typically
868 * invoked from ALSA/ASoC machine/platform driver.
870 struct sdw_stream_runtime *sdw_alloc_stream(const char *stream_name)
872 struct sdw_stream_runtime *stream;
874 stream = kzalloc(sizeof(*stream), GFP_KERNEL);
878 stream->name = stream_name;
879 INIT_LIST_HEAD(&stream->master_list);
880 stream->state = SDW_STREAM_ALLOCATED;
881 stream->m_rt_count = 0;
885 EXPORT_SYMBOL(sdw_alloc_stream);
887 static struct sdw_master_runtime
888 *sdw_find_master_rt(struct sdw_bus *bus,
889 struct sdw_stream_runtime *stream)
891 struct sdw_master_runtime *m_rt;
893 /* Retrieve Bus handle if already available */
894 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
895 if (m_rt->bus == bus)
903 * sdw_alloc_master_rt() - Allocates and initialize Master runtime handle
905 * @bus: SDW bus instance
906 * @stream_config: Stream configuration
907 * @stream: Stream runtime handle.
909 * This function is to be called with bus_lock held.
911 static struct sdw_master_runtime
912 *sdw_alloc_master_rt(struct sdw_bus *bus,
913 struct sdw_stream_config *stream_config,
914 struct sdw_stream_runtime *stream)
916 struct sdw_master_runtime *m_rt;
919 * check if Master is already allocated (as a result of Slave adding
920 * it first), if so skip allocation and go to configure
922 m_rt = sdw_find_master_rt(bus, stream);
926 m_rt = kzalloc(sizeof(*m_rt), GFP_KERNEL);
930 /* Initialization of Master runtime handle */
931 INIT_LIST_HEAD(&m_rt->port_list);
932 INIT_LIST_HEAD(&m_rt->slave_rt_list);
933 list_add_tail(&m_rt->stream_node, &stream->master_list);
935 list_add_tail(&m_rt->bus_node, &bus->m_rt_list);
938 m_rt->ch_count = stream_config->ch_count;
940 m_rt->stream = stream;
941 m_rt->direction = stream_config->direction;
947 * sdw_alloc_slave_rt() - Allocate and initialize Slave runtime handle.
949 * @slave: Slave handle
950 * @stream_config: Stream configuration
951 * @stream: Stream runtime handle
953 * This function is to be called with bus_lock held.
955 static struct sdw_slave_runtime
956 *sdw_alloc_slave_rt(struct sdw_slave *slave,
957 struct sdw_stream_config *stream_config,
958 struct sdw_stream_runtime *stream)
960 struct sdw_slave_runtime *s_rt;
962 s_rt = kzalloc(sizeof(*s_rt), GFP_KERNEL);
966 INIT_LIST_HEAD(&s_rt->port_list);
967 s_rt->ch_count = stream_config->ch_count;
968 s_rt->direction = stream_config->direction;
974 static void sdw_master_port_release(struct sdw_bus *bus,
975 struct sdw_master_runtime *m_rt)
977 struct sdw_port_runtime *p_rt, *_p_rt;
979 list_for_each_entry_safe(p_rt, _p_rt, &m_rt->port_list, port_node) {
980 list_del(&p_rt->port_node);
985 static void sdw_slave_port_release(struct sdw_bus *bus,
986 struct sdw_slave *slave,
987 struct sdw_stream_runtime *stream)
989 struct sdw_port_runtime *p_rt, *_p_rt;
990 struct sdw_master_runtime *m_rt;
991 struct sdw_slave_runtime *s_rt;
993 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
994 list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) {
995 if (s_rt->slave != slave)
998 list_for_each_entry_safe(p_rt, _p_rt,
999 &s_rt->port_list, port_node) {
1000 list_del(&p_rt->port_node);
1008 * sdw_release_slave_stream() - Free Slave(s) runtime handle
1010 * @slave: Slave handle.
1011 * @stream: Stream runtime handle.
1013 * This function is to be called with bus_lock held.
1015 static void sdw_release_slave_stream(struct sdw_slave *slave,
1016 struct sdw_stream_runtime *stream)
1018 struct sdw_slave_runtime *s_rt, *_s_rt;
1019 struct sdw_master_runtime *m_rt;
1021 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
1022 /* Retrieve Slave runtime handle */
1023 list_for_each_entry_safe(s_rt, _s_rt,
1024 &m_rt->slave_rt_list, m_rt_node) {
1025 if (s_rt->slave == slave) {
1026 list_del(&s_rt->m_rt_node);
1035 * sdw_release_master_stream() - Free Master runtime handle
1037 * @m_rt: Master runtime node
1038 * @stream: Stream runtime handle.
1040 * This function is to be called with bus_lock held
1041 * It frees the Master runtime handle and associated Slave(s) runtime
1042 * handle. If this is called first then sdw_release_slave_stream() will have
1043 * no effect as Slave(s) runtime handle would already be freed up.
1045 static void sdw_release_master_stream(struct sdw_master_runtime *m_rt,
1046 struct sdw_stream_runtime *stream)
1048 struct sdw_slave_runtime *s_rt, *_s_rt;
1050 list_for_each_entry_safe(s_rt, _s_rt, &m_rt->slave_rt_list, m_rt_node) {
1051 sdw_slave_port_release(s_rt->slave->bus, s_rt->slave, stream);
1052 sdw_release_slave_stream(s_rt->slave, stream);
1055 list_del(&m_rt->stream_node);
1056 list_del(&m_rt->bus_node);
1061 * sdw_stream_remove_master() - Remove master from sdw_stream
1063 * @bus: SDW Bus instance
1064 * @stream: SoundWire stream
1066 * This removes and frees port_rt and master_rt from a stream
1068 int sdw_stream_remove_master(struct sdw_bus *bus,
1069 struct sdw_stream_runtime *stream)
1071 struct sdw_master_runtime *m_rt, *_m_rt;
1073 mutex_lock(&bus->bus_lock);
1075 list_for_each_entry_safe(m_rt, _m_rt,
1076 &stream->master_list, stream_node) {
1077 if (m_rt->bus != bus)
1080 sdw_master_port_release(bus, m_rt);
1081 sdw_release_master_stream(m_rt, stream);
1082 stream->m_rt_count--;
1085 if (list_empty(&stream->master_list))
1086 stream->state = SDW_STREAM_RELEASED;
1088 mutex_unlock(&bus->bus_lock);
1092 EXPORT_SYMBOL(sdw_stream_remove_master);
1095 * sdw_stream_remove_slave() - Remove slave from sdw_stream
1097 * @slave: SDW Slave instance
1098 * @stream: SoundWire stream
1100 * This removes and frees port_rt and slave_rt from a stream
1102 int sdw_stream_remove_slave(struct sdw_slave *slave,
1103 struct sdw_stream_runtime *stream)
1105 mutex_lock(&slave->bus->bus_lock);
1107 sdw_slave_port_release(slave->bus, slave, stream);
1108 sdw_release_slave_stream(slave, stream);
1110 mutex_unlock(&slave->bus->bus_lock);
1114 EXPORT_SYMBOL(sdw_stream_remove_slave);
1117 * sdw_config_stream() - Configure the allocated stream
1120 * @stream: SoundWire stream
1121 * @stream_config: Stream configuration for audio stream
1122 * @is_slave: is API called from Slave or Master
1124 * This function is to be called with bus_lock held.
1126 static int sdw_config_stream(struct device *dev,
1127 struct sdw_stream_runtime *stream,
1128 struct sdw_stream_config *stream_config,
1132 * Update the stream rate, channel and bps based on data
1133 * source. For more than one data source (multilink),
1134 * match the rate, bps, stream type and increment number of channels.
1136 * If rate/bps is zero, it means the values are not set, so skip
1137 * comparison and allow the value to be set and stored in stream
1139 if (stream->params.rate &&
1140 stream->params.rate != stream_config->frame_rate) {
1141 dev_err(dev, "rate not matching, stream:%s\n", stream->name);
1145 if (stream->params.bps &&
1146 stream->params.bps != stream_config->bps) {
1147 dev_err(dev, "bps not matching, stream:%s\n", stream->name);
1151 stream->type = stream_config->type;
1152 stream->params.rate = stream_config->frame_rate;
1153 stream->params.bps = stream_config->bps;
1155 /* TODO: Update this check during Device-device support */
1157 stream->params.ch_count += stream_config->ch_count;
1162 static int sdw_is_valid_port_range(struct device *dev,
1163 struct sdw_port_runtime *p_rt)
1165 if (!SDW_VALID_PORT_RANGE(p_rt->num)) {
1167 "SoundWire: Invalid port number :%d\n", p_rt->num);
1174 static struct sdw_port_runtime
1175 *sdw_port_alloc(struct device *dev,
1176 struct sdw_port_config *port_config,
1179 struct sdw_port_runtime *p_rt;
1181 p_rt = kzalloc(sizeof(*p_rt), GFP_KERNEL);
1185 p_rt->ch_mask = port_config[port_index].ch_mask;
1186 p_rt->num = port_config[port_index].num;
1191 static int sdw_master_port_config(struct sdw_bus *bus,
1192 struct sdw_master_runtime *m_rt,
1193 struct sdw_port_config *port_config,
1194 unsigned int num_ports)
1196 struct sdw_port_runtime *p_rt;
1199 /* Iterate for number of ports to perform initialization */
1200 for (i = 0; i < num_ports; i++) {
1201 p_rt = sdw_port_alloc(bus->dev, port_config, i);
1206 * TODO: Check port capabilities for requested
1207 * configuration (audio mode support)
1210 list_add_tail(&p_rt->port_node, &m_rt->port_list);
1216 static int sdw_slave_port_config(struct sdw_slave *slave,
1217 struct sdw_slave_runtime *s_rt,
1218 struct sdw_port_config *port_config,
1219 unsigned int num_config)
1221 struct sdw_port_runtime *p_rt;
1224 /* Iterate for number of ports to perform initialization */
1225 for (i = 0; i < num_config; i++) {
1226 p_rt = sdw_port_alloc(&slave->dev, port_config, i);
1231 * TODO: Check valid port range as defined by DisCo/
1234 ret = sdw_is_valid_port_range(&slave->dev, p_rt);
1241 * TODO: Check port capabilities for requested
1242 * configuration (audio mode support)
1245 list_add_tail(&p_rt->port_node, &s_rt->port_list);
1252 * sdw_stream_add_master() - Allocate and add master runtime to a stream
1254 * @bus: SDW Bus instance
1255 * @stream_config: Stream configuration for audio stream
1256 * @port_config: Port configuration for audio stream
1257 * @num_ports: Number of ports
1258 * @stream: SoundWire stream
1260 int sdw_stream_add_master(struct sdw_bus *bus,
1261 struct sdw_stream_config *stream_config,
1262 struct sdw_port_config *port_config,
1263 unsigned int num_ports,
1264 struct sdw_stream_runtime *stream)
1266 struct sdw_master_runtime *m_rt;
1269 mutex_lock(&bus->bus_lock);
1272 * For multi link streams, add the second master only if
1273 * the bus supports it.
1274 * Check if bus->multi_link is set
1276 if (!bus->multi_link && stream->m_rt_count > 0) {
1278 "Multilink not supported, link %d\n", bus->link_id);
1283 m_rt = sdw_alloc_master_rt(bus, stream_config, stream);
1286 "Master runtime config failed for stream:%s\n",
1292 ret = sdw_config_stream(bus->dev, stream, stream_config, false);
1296 ret = sdw_master_port_config(bus, m_rt, port_config, num_ports);
1300 stream->m_rt_count++;
1305 sdw_release_master_stream(m_rt, stream);
1307 mutex_unlock(&bus->bus_lock);
1310 EXPORT_SYMBOL(sdw_stream_add_master);
1313 * sdw_stream_add_slave() - Allocate and add master/slave runtime to a stream
1315 * @slave: SDW Slave instance
1316 * @stream_config: Stream configuration for audio stream
1317 * @stream: SoundWire stream
1318 * @port_config: Port configuration for audio stream
1319 * @num_ports: Number of ports
1321 * It is expected that Slave is added before adding Master
1325 int sdw_stream_add_slave(struct sdw_slave *slave,
1326 struct sdw_stream_config *stream_config,
1327 struct sdw_port_config *port_config,
1328 unsigned int num_ports,
1329 struct sdw_stream_runtime *stream)
1331 struct sdw_slave_runtime *s_rt;
1332 struct sdw_master_runtime *m_rt;
1335 mutex_lock(&slave->bus->bus_lock);
1338 * If this API is invoked by Slave first then m_rt is not valid.
1339 * So, allocate m_rt and add Slave to it.
1341 m_rt = sdw_alloc_master_rt(slave->bus, stream_config, stream);
1343 dev_err(&slave->dev,
1344 "alloc master runtime failed for stream:%s\n",
1350 s_rt = sdw_alloc_slave_rt(slave, stream_config, stream);
1352 dev_err(&slave->dev,
1353 "Slave runtime config failed for stream:%s\n",
1359 ret = sdw_config_stream(&slave->dev, stream, stream_config, true);
1363 list_add_tail(&s_rt->m_rt_node, &m_rt->slave_rt_list);
1365 ret = sdw_slave_port_config(slave, s_rt, port_config, num_ports);
1370 * Change stream state to CONFIGURED on first Slave add.
1371 * Bus is not aware of number of Slave(s) in a stream at this
1372 * point so cannot depend on all Slave(s) to be added in order to
1373 * change stream state to CONFIGURED.
1375 stream->state = SDW_STREAM_CONFIGURED;
1380 * we hit error so cleanup the stream, release all Slave(s) and
1383 sdw_release_master_stream(m_rt, stream);
1385 mutex_unlock(&slave->bus->bus_lock);
1388 EXPORT_SYMBOL(sdw_stream_add_slave);
1391 * sdw_get_slave_dpn_prop() - Get Slave port capabilities
1393 * @slave: Slave handle
1394 * @direction: Data direction.
1395 * @port_num: Port number
1397 struct sdw_dpn_prop *sdw_get_slave_dpn_prop(struct sdw_slave *slave,
1398 enum sdw_data_direction direction,
1399 unsigned int port_num)
1401 struct sdw_dpn_prop *dpn_prop;
1405 if (direction == SDW_DATA_DIR_TX) {
1406 num_ports = hweight32(slave->prop.source_ports);
1407 dpn_prop = slave->prop.src_dpn_prop;
1409 num_ports = hweight32(slave->prop.sink_ports);
1410 dpn_prop = slave->prop.sink_dpn_prop;
1413 for (i = 0; i < num_ports; i++) {
1414 if (dpn_prop[i].num == port_num)
1415 return &dpn_prop[i];
1422 * sdw_acquire_bus_lock: Acquire bus lock for all Master runtime(s)
1424 * @stream: SoundWire stream
1426 * Acquire bus_lock for each of the master runtime(m_rt) part of this
1427 * stream to reconfigure the bus.
1428 * NOTE: This function is called from SoundWire stream ops and is
1429 * expected that a global lock is held before acquiring bus_lock.
1431 static void sdw_acquire_bus_lock(struct sdw_stream_runtime *stream)
1433 struct sdw_master_runtime *m_rt;
1434 struct sdw_bus *bus = NULL;
1436 /* Iterate for all Master(s) in Master list */
1437 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
1440 mutex_lock(&bus->bus_lock);
1445 * sdw_release_bus_lock: Release bus lock for all Master runtime(s)
1447 * @stream: SoundWire stream
1449 * Release the previously held bus_lock after reconfiguring the bus.
1450 * NOTE: This function is called from SoundWire stream ops and is
1451 * expected that a global lock is held before releasing bus_lock.
1453 static void sdw_release_bus_lock(struct sdw_stream_runtime *stream)
1455 struct sdw_master_runtime *m_rt = NULL;
1456 struct sdw_bus *bus = NULL;
1458 /* Iterate for all Master(s) in Master list */
1459 list_for_each_entry_reverse(m_rt, &stream->master_list, stream_node) {
1461 mutex_unlock(&bus->bus_lock);
1465 static int _sdw_prepare_stream(struct sdw_stream_runtime *stream)
1467 struct sdw_master_runtime *m_rt;
1468 struct sdw_bus *bus = NULL;
1469 struct sdw_master_prop *prop;
1470 struct sdw_bus_params params;
1473 /* Prepare Master(s) and Slave(s) port(s) associated with stream */
1474 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
1477 memcpy(¶ms, &bus->params, sizeof(params));
1479 /* TODO: Support Asynchronous mode */
1480 if ((prop->max_clk_freq % stream->params.rate) != 0) {
1481 dev_err(bus->dev, "Async mode not supported\n");
1485 /* Increment cumulative bus bandwidth */
1486 /* TODO: Update this during Device-Device support */
1487 bus->params.bandwidth += m_rt->stream->params.rate *
1488 m_rt->ch_count * m_rt->stream->params.bps;
1490 /* Compute params */
1491 if (bus->compute_params) {
1492 ret = bus->compute_params(bus);
1494 dev_err(bus->dev, "Compute params failed: %d",
1500 /* Program params */
1501 ret = sdw_program_params(bus);
1503 dev_err(bus->dev, "Program params failed: %d\n", ret);
1504 goto restore_params;
1509 pr_err("Configuration error in %s\n", __func__);
1513 ret = do_bank_switch(stream);
1515 dev_err(bus->dev, "Bank switch failed: %d\n", ret);
1516 goto restore_params;
1519 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
1522 /* Prepare port(s) on the new clock configuration */
1523 ret = sdw_prep_deprep_ports(m_rt, true);
1525 dev_err(bus->dev, "Prepare port(s) failed ret = %d\n",
1531 stream->state = SDW_STREAM_PREPARED;
1536 memcpy(&bus->params, ¶ms, sizeof(params));
1541 * sdw_prepare_stream() - Prepare SoundWire stream
1543 * @stream: Soundwire stream
1545 * Documentation/driver-api/soundwire/stream.rst explains this API in detail
1547 int sdw_prepare_stream(struct sdw_stream_runtime *stream)
1552 pr_err("SoundWire: Handle not found for stream\n");
1556 sdw_acquire_bus_lock(stream);
1558 ret = _sdw_prepare_stream(stream);
1560 sdw_release_bus_lock(stream);
1563 EXPORT_SYMBOL(sdw_prepare_stream);
1565 static int _sdw_enable_stream(struct sdw_stream_runtime *stream)
1567 struct sdw_master_runtime *m_rt;
1568 struct sdw_bus *bus = NULL;
1571 /* Enable Master(s) and Slave(s) port(s) associated with stream */
1572 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
1575 /* Program params */
1576 ret = sdw_program_params(bus);
1578 dev_err(bus->dev, "Program params failed: %d\n", ret);
1582 /* Enable port(s) */
1583 ret = sdw_enable_disable_ports(m_rt, true);
1586 "Enable port(s) failed ret: %d\n", ret);
1592 pr_err("Configuration error in %s\n", __func__);
1596 ret = do_bank_switch(stream);
1598 dev_err(bus->dev, "Bank switch failed: %d\n", ret);
1602 stream->state = SDW_STREAM_ENABLED;
1607 * sdw_enable_stream() - Enable SoundWire stream
1609 * @stream: Soundwire stream
1611 * Documentation/driver-api/soundwire/stream.rst explains this API in detail
1613 int sdw_enable_stream(struct sdw_stream_runtime *stream)
1618 pr_err("SoundWire: Handle not found for stream\n");
1622 sdw_acquire_bus_lock(stream);
1624 ret = _sdw_enable_stream(stream);
1626 sdw_release_bus_lock(stream);
1629 EXPORT_SYMBOL(sdw_enable_stream);
1631 static int _sdw_disable_stream(struct sdw_stream_runtime *stream)
1633 struct sdw_master_runtime *m_rt;
1636 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
1637 struct sdw_bus *bus = m_rt->bus;
1639 /* Disable port(s) */
1640 ret = sdw_enable_disable_ports(m_rt, false);
1642 dev_err(bus->dev, "Disable port(s) failed: %d\n", ret);
1646 stream->state = SDW_STREAM_DISABLED;
1648 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
1649 struct sdw_bus *bus = m_rt->bus;
1651 /* Program params */
1652 ret = sdw_program_params(bus);
1654 dev_err(bus->dev, "Program params failed: %d\n", ret);
1659 ret = do_bank_switch(stream);
1661 pr_err("Bank switch failed: %d\n", ret);
1665 /* make sure alternate bank (previous current) is also disabled */
1666 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
1667 struct sdw_bus *bus = m_rt->bus;
1669 /* Disable port(s) */
1670 ret = sdw_enable_disable_ports(m_rt, false);
1672 dev_err(bus->dev, "Disable port(s) failed: %d\n", ret);
1681 * sdw_disable_stream() - Disable SoundWire stream
1683 * @stream: Soundwire stream
1685 * Documentation/driver-api/soundwire/stream.rst explains this API in detail
1687 int sdw_disable_stream(struct sdw_stream_runtime *stream)
1692 pr_err("SoundWire: Handle not found for stream\n");
1696 sdw_acquire_bus_lock(stream);
1698 ret = _sdw_disable_stream(stream);
1700 sdw_release_bus_lock(stream);
1703 EXPORT_SYMBOL(sdw_disable_stream);
1705 static int _sdw_deprepare_stream(struct sdw_stream_runtime *stream)
1707 struct sdw_master_runtime *m_rt;
1708 struct sdw_bus *bus;
1711 list_for_each_entry(m_rt, &stream->master_list, stream_node) {
1713 /* De-prepare port(s) */
1714 ret = sdw_prep_deprep_ports(m_rt, false);
1717 "De-prepare port(s) failed: %d\n", ret);
1721 /* TODO: Update this during Device-Device support */
1722 bus->params.bandwidth -= m_rt->stream->params.rate *
1723 m_rt->ch_count * m_rt->stream->params.bps;
1725 /* Program params */
1726 ret = sdw_program_params(bus);
1728 dev_err(bus->dev, "Program params failed: %d\n", ret);
1733 stream->state = SDW_STREAM_DEPREPARED;
1734 return do_bank_switch(stream);
1738 * sdw_deprepare_stream() - Deprepare SoundWire stream
1740 * @stream: Soundwire stream
1742 * Documentation/driver-api/soundwire/stream.rst explains this API in detail
1744 int sdw_deprepare_stream(struct sdw_stream_runtime *stream)
1749 pr_err("SoundWire: Handle not found for stream\n");
1753 sdw_acquire_bus_lock(stream);
1754 ret = _sdw_deprepare_stream(stream);
1756 sdw_release_bus_lock(stream);
1759 EXPORT_SYMBOL(sdw_deprepare_stream);