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 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
= FIELD_PREP(SDW_DPN_PORTCTRL_DATAMODE
, p_params
->data_mode
);
160 wbuf
|= FIELD_PREP(SDW_DPN_PORTCTRL_FLOWMODE
, 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_write(s_rt
->slave
, addr
, p_rt
->ch_mask
);
320 ret
= sdw_write(s_rt
->slave
, addr
, 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
||
447 bus
->params
.s_data_mode
!= SDW_PORT_DATA_MODE_NORMAL
)
451 * Enable interrupt before Port prepare.
452 * For Port de-prepare, it is assumed that port
453 * was prepared earlier
456 ret
= sdw_configure_dpn_intr(s_rt
->slave
, p_rt
->num
, prep
,
457 dpn_prop
->imp_def_interrupts
);
462 /* Inform slave about the impending port prepare */
463 sdw_do_port_prep(s_rt
, prep_ch
, SDW_OPS_PORT_PRE_PREP
);
465 /* Prepare Slave port implementing CP_SM */
466 if (!dpn_prop
->simple_ch_prep_sm
) {
467 addr
= SDW_DPN_PREPARECTRL(p_rt
->num
);
470 ret
= sdw_write(s_rt
->slave
, addr
, p_rt
->ch_mask
);
472 ret
= sdw_write(s_rt
->slave
, addr
, 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",
604 * sdw_program_params() - Program transport and port parameters for Master(s)
607 * @bus: SDW bus instance
608 * @prepare: true if sdw_program_params() is called by _prepare.
610 static int sdw_program_params(struct sdw_bus
*bus
, bool prepare
)
612 struct sdw_master_runtime
*m_rt
;
615 list_for_each_entry(m_rt
, &bus
->m_rt_list
, bus_node
) {
618 * this loop walks through all master runtimes for a
619 * bus, but the ports can only be configured while
620 * explicitly preparing a stream or handling an
621 * already-prepared stream otherwise.
624 m_rt
->stream
->state
== SDW_STREAM_CONFIGURED
)
627 ret
= sdw_program_port_params(m_rt
);
630 "Program transport params failed: %d\n", ret
);
634 ret
= sdw_notify_config(m_rt
);
637 "Notify bus config failed: %d\n", ret
);
641 /* Enable port(s) on alternate bank for all active streams */
642 if (m_rt
->stream
->state
!= SDW_STREAM_ENABLED
)
645 ret
= sdw_enable_disable_ports(m_rt
, true);
647 dev_err(bus
->dev
, "Enable channel failed: %d\n", ret
);
655 static int sdw_bank_switch(struct sdw_bus
*bus
, int m_rt_count
)
657 int col_index
, row_index
;
659 struct sdw_msg
*wr_msg
;
664 wr_msg
= kzalloc(sizeof(*wr_msg
), GFP_KERNEL
);
668 bus
->defer_msg
.msg
= wr_msg
;
670 wbuf
= kzalloc(sizeof(*wbuf
), GFP_KERNEL
);
676 /* Get row and column index to program register */
677 col_index
= sdw_find_col_index(bus
->params
.col
);
678 row_index
= sdw_find_row_index(bus
->params
.row
);
679 wbuf
[0] = col_index
| (row_index
<< 3);
681 if (bus
->params
.next_bank
)
682 addr
= SDW_SCP_FRAMECTRL_B1
;
684 addr
= SDW_SCP_FRAMECTRL_B0
;
686 sdw_fill_msg(wr_msg
, NULL
, addr
, 1, SDW_BROADCAST_DEV_NUM
,
687 SDW_MSG_FLAG_WRITE
, wbuf
);
688 wr_msg
->ssp_sync
= true;
691 * Set the multi_link flag only when both the hardware supports
692 * and hardware-based sync is required
694 multi_link
= bus
->multi_link
&& (m_rt_count
>= bus
->hw_sync_min_links
);
697 ret
= sdw_transfer_defer(bus
, wr_msg
, &bus
->defer_msg
);
699 ret
= sdw_transfer(bus
, wr_msg
);
702 dev_err(bus
->dev
, "Slave frame_ctrl reg write failed\n");
709 bus
->defer_msg
.msg
= NULL
;
710 bus
->params
.curr_bank
= !bus
->params
.curr_bank
;
711 bus
->params
.next_bank
= !bus
->params
.next_bank
;
720 bus
->defer_msg
.msg
= NULL
;
725 * sdw_ml_sync_bank_switch: Multilink register bank switch
727 * @bus: SDW bus instance
729 * Caller function should free the buffers on error
731 static int sdw_ml_sync_bank_switch(struct sdw_bus
*bus
)
733 unsigned long time_left
;
735 if (!bus
->multi_link
)
738 /* Wait for completion of transfer */
739 time_left
= wait_for_completion_timeout(&bus
->defer_msg
.complete
,
740 bus
->bank_switch_timeout
);
743 dev_err(bus
->dev
, "Controller Timed out on bank switch\n");
747 bus
->params
.curr_bank
= !bus
->params
.curr_bank
;
748 bus
->params
.next_bank
= !bus
->params
.next_bank
;
750 if (bus
->defer_msg
.msg
) {
751 kfree(bus
->defer_msg
.msg
->buf
);
752 kfree(bus
->defer_msg
.msg
);
758 static int do_bank_switch(struct sdw_stream_runtime
*stream
)
760 struct sdw_master_runtime
*m_rt
;
761 const struct sdw_master_ops
*ops
;
763 bool multi_link
= false;
767 m_rt_count
= stream
->m_rt_count
;
769 list_for_each_entry(m_rt
, &stream
->master_list
, stream_node
) {
773 if (bus
->multi_link
&& m_rt_count
>= bus
->hw_sync_min_links
) {
775 mutex_lock(&bus
->msg_lock
);
778 /* Pre-bank switch */
779 if (ops
->pre_bank_switch
) {
780 ret
= ops
->pre_bank_switch(bus
);
783 "Pre bank switch op failed: %d\n", ret
);
789 * Perform Bank switch operation.
790 * For multi link cases, the actual bank switch is
791 * synchronized across all Masters and happens later as a
792 * part of post_bank_switch ops.
794 ret
= sdw_bank_switch(bus
, m_rt_count
);
796 dev_err(bus
->dev
, "Bank switch failed: %d\n", ret
);
802 * For multi link cases, it is expected that the bank switch is
803 * triggered by the post_bank_switch for the first Master in the list
804 * and for the other Masters the post_bank_switch() should return doing
807 list_for_each_entry(m_rt
, &stream
->master_list
, stream_node
) {
811 /* Post-bank switch */
812 if (ops
->post_bank_switch
) {
813 ret
= ops
->post_bank_switch(bus
);
816 "Post bank switch op failed: %d\n",
820 } else if (multi_link
) {
822 "Post bank switch ops not implemented\n");
826 /* Set the bank switch timeout to default, if not set */
827 if (!bus
->bank_switch_timeout
)
828 bus
->bank_switch_timeout
= DEFAULT_BANK_SWITCH_TIMEOUT
;
830 /* Check if bank switch was successful */
831 ret
= sdw_ml_sync_bank_switch(bus
);
834 "multi link bank switch failed: %d\n", ret
);
839 mutex_unlock(&bus
->msg_lock
);
845 list_for_each_entry(m_rt
, &stream
->master_list
, stream_node
) {
847 if (bus
->defer_msg
.msg
) {
848 kfree(bus
->defer_msg
.msg
->buf
);
849 kfree(bus
->defer_msg
.msg
);
856 list_for_each_entry(m_rt
, &stream
->master_list
, stream_node
) {
858 if (mutex_is_locked(&bus
->msg_lock
))
859 mutex_unlock(&bus
->msg_lock
);
867 * sdw_release_stream() - Free the assigned stream runtime
869 * @stream: SoundWire stream runtime
871 * sdw_release_stream should be called only once per stream
873 void sdw_release_stream(struct sdw_stream_runtime
*stream
)
877 EXPORT_SYMBOL(sdw_release_stream
);
880 * sdw_alloc_stream() - Allocate and return stream runtime
882 * @stream_name: SoundWire stream name
884 * Allocates a SoundWire stream runtime instance.
885 * sdw_alloc_stream should be called only once per stream. Typically
886 * invoked from ALSA/ASoC machine/platform driver.
888 struct sdw_stream_runtime
*sdw_alloc_stream(const char *stream_name
)
890 struct sdw_stream_runtime
*stream
;
892 stream
= kzalloc(sizeof(*stream
), GFP_KERNEL
);
896 stream
->name
= stream_name
;
897 INIT_LIST_HEAD(&stream
->master_list
);
898 stream
->state
= SDW_STREAM_ALLOCATED
;
899 stream
->m_rt_count
= 0;
903 EXPORT_SYMBOL(sdw_alloc_stream
);
905 static struct sdw_master_runtime
906 *sdw_find_master_rt(struct sdw_bus
*bus
,
907 struct sdw_stream_runtime
*stream
)
909 struct sdw_master_runtime
*m_rt
;
911 /* Retrieve Bus handle if already available */
912 list_for_each_entry(m_rt
, &stream
->master_list
, stream_node
) {
913 if (m_rt
->bus
== bus
)
921 * sdw_alloc_master_rt() - Allocates and initialize Master runtime handle
923 * @bus: SDW bus instance
924 * @stream_config: Stream configuration
925 * @stream: Stream runtime handle.
927 * This function is to be called with bus_lock held.
929 static struct sdw_master_runtime
930 *sdw_alloc_master_rt(struct sdw_bus
*bus
,
931 struct sdw_stream_config
*stream_config
,
932 struct sdw_stream_runtime
*stream
)
934 struct sdw_master_runtime
*m_rt
;
937 * check if Master is already allocated (as a result of Slave adding
938 * it first), if so skip allocation and go to configure
940 m_rt
= sdw_find_master_rt(bus
, stream
);
944 m_rt
= kzalloc(sizeof(*m_rt
), GFP_KERNEL
);
948 /* Initialization of Master runtime handle */
949 INIT_LIST_HEAD(&m_rt
->port_list
);
950 INIT_LIST_HEAD(&m_rt
->slave_rt_list
);
951 list_add_tail(&m_rt
->stream_node
, &stream
->master_list
);
953 list_add_tail(&m_rt
->bus_node
, &bus
->m_rt_list
);
956 m_rt
->ch_count
= stream_config
->ch_count
;
958 m_rt
->stream
= stream
;
959 m_rt
->direction
= stream_config
->direction
;
965 * sdw_alloc_slave_rt() - Allocate and initialize Slave runtime handle.
967 * @slave: Slave handle
968 * @stream_config: Stream configuration
969 * @stream: Stream runtime handle
971 * This function is to be called with bus_lock held.
973 static struct sdw_slave_runtime
974 *sdw_alloc_slave_rt(struct sdw_slave
*slave
,
975 struct sdw_stream_config
*stream_config
,
976 struct sdw_stream_runtime
*stream
)
978 struct sdw_slave_runtime
*s_rt
;
980 s_rt
= kzalloc(sizeof(*s_rt
), GFP_KERNEL
);
984 INIT_LIST_HEAD(&s_rt
->port_list
);
985 s_rt
->ch_count
= stream_config
->ch_count
;
986 s_rt
->direction
= stream_config
->direction
;
992 static void sdw_master_port_release(struct sdw_bus
*bus
,
993 struct sdw_master_runtime
*m_rt
)
995 struct sdw_port_runtime
*p_rt
, *_p_rt
;
997 list_for_each_entry_safe(p_rt
, _p_rt
, &m_rt
->port_list
, port_node
) {
998 list_del(&p_rt
->port_node
);
1003 static void sdw_slave_port_release(struct sdw_bus
*bus
,
1004 struct sdw_slave
*slave
,
1005 struct sdw_stream_runtime
*stream
)
1007 struct sdw_port_runtime
*p_rt
, *_p_rt
;
1008 struct sdw_master_runtime
*m_rt
;
1009 struct sdw_slave_runtime
*s_rt
;
1011 list_for_each_entry(m_rt
, &stream
->master_list
, stream_node
) {
1012 list_for_each_entry(s_rt
, &m_rt
->slave_rt_list
, m_rt_node
) {
1013 if (s_rt
->slave
!= slave
)
1016 list_for_each_entry_safe(p_rt
, _p_rt
,
1017 &s_rt
->port_list
, port_node
) {
1018 list_del(&p_rt
->port_node
);
1026 * sdw_release_slave_stream() - Free Slave(s) runtime handle
1028 * @slave: Slave handle.
1029 * @stream: Stream runtime handle.
1031 * This function is to be called with bus_lock held.
1033 static void sdw_release_slave_stream(struct sdw_slave
*slave
,
1034 struct sdw_stream_runtime
*stream
)
1036 struct sdw_slave_runtime
*s_rt
, *_s_rt
;
1037 struct sdw_master_runtime
*m_rt
;
1039 list_for_each_entry(m_rt
, &stream
->master_list
, stream_node
) {
1040 /* Retrieve Slave runtime handle */
1041 list_for_each_entry_safe(s_rt
, _s_rt
,
1042 &m_rt
->slave_rt_list
, m_rt_node
) {
1043 if (s_rt
->slave
== slave
) {
1044 list_del(&s_rt
->m_rt_node
);
1053 * sdw_release_master_stream() - Free Master runtime handle
1055 * @m_rt: Master runtime node
1056 * @stream: Stream runtime handle.
1058 * This function is to be called with bus_lock held
1059 * It frees the Master runtime handle and associated Slave(s) runtime
1060 * handle. If this is called first then sdw_release_slave_stream() will have
1061 * no effect as Slave(s) runtime handle would already be freed up.
1063 static void sdw_release_master_stream(struct sdw_master_runtime
*m_rt
,
1064 struct sdw_stream_runtime
*stream
)
1066 struct sdw_slave_runtime
*s_rt
, *_s_rt
;
1068 list_for_each_entry_safe(s_rt
, _s_rt
, &m_rt
->slave_rt_list
, m_rt_node
) {
1069 sdw_slave_port_release(s_rt
->slave
->bus
, s_rt
->slave
, stream
);
1070 sdw_release_slave_stream(s_rt
->slave
, stream
);
1073 list_del(&m_rt
->stream_node
);
1074 list_del(&m_rt
->bus_node
);
1079 * sdw_stream_remove_master() - Remove master from sdw_stream
1081 * @bus: SDW Bus instance
1082 * @stream: SoundWire stream
1084 * This removes and frees port_rt and master_rt from a stream
1086 int sdw_stream_remove_master(struct sdw_bus
*bus
,
1087 struct sdw_stream_runtime
*stream
)
1089 struct sdw_master_runtime
*m_rt
, *_m_rt
;
1091 mutex_lock(&bus
->bus_lock
);
1093 list_for_each_entry_safe(m_rt
, _m_rt
,
1094 &stream
->master_list
, stream_node
) {
1095 if (m_rt
->bus
!= bus
)
1098 sdw_master_port_release(bus
, m_rt
);
1099 sdw_release_master_stream(m_rt
, stream
);
1100 stream
->m_rt_count
--;
1103 if (list_empty(&stream
->master_list
))
1104 stream
->state
= SDW_STREAM_RELEASED
;
1106 mutex_unlock(&bus
->bus_lock
);
1110 EXPORT_SYMBOL(sdw_stream_remove_master
);
1113 * sdw_stream_remove_slave() - Remove slave from sdw_stream
1115 * @slave: SDW Slave instance
1116 * @stream: SoundWire stream
1118 * This removes and frees port_rt and slave_rt from a stream
1120 int sdw_stream_remove_slave(struct sdw_slave
*slave
,
1121 struct sdw_stream_runtime
*stream
)
1123 mutex_lock(&slave
->bus
->bus_lock
);
1125 sdw_slave_port_release(slave
->bus
, slave
, stream
);
1126 sdw_release_slave_stream(slave
, stream
);
1128 mutex_unlock(&slave
->bus
->bus_lock
);
1132 EXPORT_SYMBOL(sdw_stream_remove_slave
);
1135 * sdw_config_stream() - Configure the allocated stream
1138 * @stream: SoundWire stream
1139 * @stream_config: Stream configuration for audio stream
1140 * @is_slave: is API called from Slave or Master
1142 * This function is to be called with bus_lock held.
1144 static int sdw_config_stream(struct device
*dev
,
1145 struct sdw_stream_runtime
*stream
,
1146 struct sdw_stream_config
*stream_config
,
1150 * Update the stream rate, channel and bps based on data
1151 * source. For more than one data source (multilink),
1152 * match the rate, bps, stream type and increment number of channels.
1154 * If rate/bps is zero, it means the values are not set, so skip
1155 * comparison and allow the value to be set and stored in stream
1157 if (stream
->params
.rate
&&
1158 stream
->params
.rate
!= stream_config
->frame_rate
) {
1159 dev_err(dev
, "rate not matching, stream:%s\n", stream
->name
);
1163 if (stream
->params
.bps
&&
1164 stream
->params
.bps
!= stream_config
->bps
) {
1165 dev_err(dev
, "bps not matching, stream:%s\n", stream
->name
);
1169 stream
->type
= stream_config
->type
;
1170 stream
->params
.rate
= stream_config
->frame_rate
;
1171 stream
->params
.bps
= stream_config
->bps
;
1173 /* TODO: Update this check during Device-device support */
1175 stream
->params
.ch_count
+= stream_config
->ch_count
;
1180 static int sdw_is_valid_port_range(struct device
*dev
,
1181 struct sdw_port_runtime
*p_rt
)
1183 if (!SDW_VALID_PORT_RANGE(p_rt
->num
)) {
1185 "SoundWire: Invalid port number :%d\n", p_rt
->num
);
1192 static struct sdw_port_runtime
1193 *sdw_port_alloc(struct device
*dev
,
1194 struct sdw_port_config
*port_config
,
1197 struct sdw_port_runtime
*p_rt
;
1199 p_rt
= kzalloc(sizeof(*p_rt
), GFP_KERNEL
);
1203 p_rt
->ch_mask
= port_config
[port_index
].ch_mask
;
1204 p_rt
->num
= port_config
[port_index
].num
;
1209 static int sdw_master_port_config(struct sdw_bus
*bus
,
1210 struct sdw_master_runtime
*m_rt
,
1211 struct sdw_port_config
*port_config
,
1212 unsigned int num_ports
)
1214 struct sdw_port_runtime
*p_rt
;
1217 /* Iterate for number of ports to perform initialization */
1218 for (i
= 0; i
< num_ports
; i
++) {
1219 p_rt
= sdw_port_alloc(bus
->dev
, port_config
, i
);
1224 * TODO: Check port capabilities for requested
1225 * configuration (audio mode support)
1228 list_add_tail(&p_rt
->port_node
, &m_rt
->port_list
);
1234 static int sdw_slave_port_config(struct sdw_slave
*slave
,
1235 struct sdw_slave_runtime
*s_rt
,
1236 struct sdw_port_config
*port_config
,
1237 unsigned int num_config
)
1239 struct sdw_port_runtime
*p_rt
;
1242 /* Iterate for number of ports to perform initialization */
1243 for (i
= 0; i
< num_config
; i
++) {
1244 p_rt
= sdw_port_alloc(&slave
->dev
, port_config
, i
);
1249 * TODO: Check valid port range as defined by DisCo/
1252 ret
= sdw_is_valid_port_range(&slave
->dev
, p_rt
);
1259 * TODO: Check port capabilities for requested
1260 * configuration (audio mode support)
1263 list_add_tail(&p_rt
->port_node
, &s_rt
->port_list
);
1270 * sdw_stream_add_master() - Allocate and add master runtime to a stream
1272 * @bus: SDW Bus instance
1273 * @stream_config: Stream configuration for audio stream
1274 * @port_config: Port configuration for audio stream
1275 * @num_ports: Number of ports
1276 * @stream: SoundWire stream
1278 int sdw_stream_add_master(struct sdw_bus
*bus
,
1279 struct sdw_stream_config
*stream_config
,
1280 struct sdw_port_config
*port_config
,
1281 unsigned int num_ports
,
1282 struct sdw_stream_runtime
*stream
)
1284 struct sdw_master_runtime
*m_rt
;
1287 mutex_lock(&bus
->bus_lock
);
1290 * For multi link streams, add the second master only if
1291 * the bus supports it.
1292 * Check if bus->multi_link is set
1294 if (!bus
->multi_link
&& stream
->m_rt_count
> 0) {
1296 "Multilink not supported, link %d\n", bus
->link_id
);
1301 m_rt
= sdw_alloc_master_rt(bus
, stream_config
, stream
);
1304 "Master runtime config failed for stream:%s\n",
1310 ret
= sdw_config_stream(bus
->dev
, stream
, stream_config
, false);
1314 ret
= sdw_master_port_config(bus
, m_rt
, port_config
, num_ports
);
1318 stream
->m_rt_count
++;
1323 sdw_release_master_stream(m_rt
, stream
);
1325 mutex_unlock(&bus
->bus_lock
);
1328 EXPORT_SYMBOL(sdw_stream_add_master
);
1331 * sdw_stream_add_slave() - Allocate and add master/slave runtime to a stream
1333 * @slave: SDW Slave instance
1334 * @stream_config: Stream configuration for audio stream
1335 * @stream: SoundWire stream
1336 * @port_config: Port configuration for audio stream
1337 * @num_ports: Number of ports
1339 * It is expected that Slave is added before adding Master
1343 int sdw_stream_add_slave(struct sdw_slave
*slave
,
1344 struct sdw_stream_config
*stream_config
,
1345 struct sdw_port_config
*port_config
,
1346 unsigned int num_ports
,
1347 struct sdw_stream_runtime
*stream
)
1349 struct sdw_slave_runtime
*s_rt
;
1350 struct sdw_master_runtime
*m_rt
;
1353 mutex_lock(&slave
->bus
->bus_lock
);
1356 * If this API is invoked by Slave first then m_rt is not valid.
1357 * So, allocate m_rt and add Slave to it.
1359 m_rt
= sdw_alloc_master_rt(slave
->bus
, stream_config
, stream
);
1361 dev_err(&slave
->dev
,
1362 "alloc master runtime failed for stream:%s\n",
1368 s_rt
= sdw_alloc_slave_rt(slave
, stream_config
, stream
);
1370 dev_err(&slave
->dev
,
1371 "Slave runtime config failed for stream:%s\n",
1377 ret
= sdw_config_stream(&slave
->dev
, stream
, stream_config
, true);
1381 list_add_tail(&s_rt
->m_rt_node
, &m_rt
->slave_rt_list
);
1383 ret
= sdw_slave_port_config(slave
, s_rt
, port_config
, num_ports
);
1388 * Change stream state to CONFIGURED on first Slave add.
1389 * Bus is not aware of number of Slave(s) in a stream at this
1390 * point so cannot depend on all Slave(s) to be added in order to
1391 * change stream state to CONFIGURED.
1393 stream
->state
= SDW_STREAM_CONFIGURED
;
1398 * we hit error so cleanup the stream, release all Slave(s) and
1401 sdw_release_master_stream(m_rt
, stream
);
1403 mutex_unlock(&slave
->bus
->bus_lock
);
1406 EXPORT_SYMBOL(sdw_stream_add_slave
);
1409 * sdw_get_slave_dpn_prop() - Get Slave port capabilities
1411 * @slave: Slave handle
1412 * @direction: Data direction.
1413 * @port_num: Port number
1415 struct sdw_dpn_prop
*sdw_get_slave_dpn_prop(struct sdw_slave
*slave
,
1416 enum sdw_data_direction direction
,
1417 unsigned int port_num
)
1419 struct sdw_dpn_prop
*dpn_prop
;
1423 if (direction
== SDW_DATA_DIR_TX
) {
1424 num_ports
= hweight32(slave
->prop
.source_ports
);
1425 dpn_prop
= slave
->prop
.src_dpn_prop
;
1427 num_ports
= hweight32(slave
->prop
.sink_ports
);
1428 dpn_prop
= slave
->prop
.sink_dpn_prop
;
1431 for (i
= 0; i
< num_ports
; i
++) {
1432 if (dpn_prop
[i
].num
== port_num
)
1433 return &dpn_prop
[i
];
1440 * sdw_acquire_bus_lock: Acquire bus lock for all Master runtime(s)
1442 * @stream: SoundWire stream
1444 * Acquire bus_lock for each of the master runtime(m_rt) part of this
1445 * stream to reconfigure the bus.
1446 * NOTE: This function is called from SoundWire stream ops and is
1447 * expected that a global lock is held before acquiring bus_lock.
1449 static void sdw_acquire_bus_lock(struct sdw_stream_runtime
*stream
)
1451 struct sdw_master_runtime
*m_rt
;
1452 struct sdw_bus
*bus
= NULL
;
1454 /* Iterate for all Master(s) in Master list */
1455 list_for_each_entry(m_rt
, &stream
->master_list
, stream_node
) {
1458 mutex_lock(&bus
->bus_lock
);
1463 * sdw_release_bus_lock: Release bus lock for all Master runtime(s)
1465 * @stream: SoundWire stream
1467 * Release the previously held bus_lock after reconfiguring the bus.
1468 * NOTE: This function is called from SoundWire stream ops and is
1469 * expected that a global lock is held before releasing bus_lock.
1471 static void sdw_release_bus_lock(struct sdw_stream_runtime
*stream
)
1473 struct sdw_master_runtime
*m_rt
= NULL
;
1474 struct sdw_bus
*bus
= NULL
;
1476 /* Iterate for all Master(s) in Master list */
1477 list_for_each_entry_reverse(m_rt
, &stream
->master_list
, stream_node
) {
1479 mutex_unlock(&bus
->bus_lock
);
1483 static int _sdw_prepare_stream(struct sdw_stream_runtime
*stream
,
1486 struct sdw_master_runtime
*m_rt
;
1487 struct sdw_bus
*bus
= NULL
;
1488 struct sdw_master_prop
*prop
;
1489 struct sdw_bus_params params
;
1492 /* Prepare Master(s) and Slave(s) port(s) associated with stream */
1493 list_for_each_entry(m_rt
, &stream
->master_list
, stream_node
) {
1496 memcpy(¶ms
, &bus
->params
, sizeof(params
));
1498 /* TODO: Support Asynchronous mode */
1499 if ((prop
->max_clk_freq
% stream
->params
.rate
) != 0) {
1500 dev_err(bus
->dev
, "Async mode not supported\n");
1505 goto program_params
;
1507 /* Increment cumulative bus bandwidth */
1508 /* TODO: Update this during Device-Device support */
1509 bus
->params
.bandwidth
+= m_rt
->stream
->params
.rate
*
1510 m_rt
->ch_count
* m_rt
->stream
->params
.bps
;
1512 /* Compute params */
1513 if (bus
->compute_params
) {
1514 ret
= bus
->compute_params(bus
);
1516 dev_err(bus
->dev
, "Compute params failed: %d",
1523 /* Program params */
1524 ret
= sdw_program_params(bus
, true);
1526 dev_err(bus
->dev
, "Program params failed: %d\n", ret
);
1527 goto restore_params
;
1532 pr_err("Configuration error in %s\n", __func__
);
1536 ret
= do_bank_switch(stream
);
1538 dev_err(bus
->dev
, "Bank switch failed: %d\n", ret
);
1539 goto restore_params
;
1542 list_for_each_entry(m_rt
, &stream
->master_list
, stream_node
) {
1545 /* Prepare port(s) on the new clock configuration */
1546 ret
= sdw_prep_deprep_ports(m_rt
, true);
1548 dev_err(bus
->dev
, "Prepare port(s) failed ret = %d\n",
1554 stream
->state
= SDW_STREAM_PREPARED
;
1559 memcpy(&bus
->params
, ¶ms
, sizeof(params
));
1564 * sdw_prepare_stream() - Prepare SoundWire stream
1566 * @stream: Soundwire stream
1568 * Documentation/driver-api/soundwire/stream.rst explains this API in detail
1570 int sdw_prepare_stream(struct sdw_stream_runtime
*stream
)
1572 bool update_params
= true;
1576 pr_err("SoundWire: Handle not found for stream\n");
1580 sdw_acquire_bus_lock(stream
);
1582 if (stream
->state
== SDW_STREAM_PREPARED
) {
1587 if (stream
->state
!= SDW_STREAM_CONFIGURED
&&
1588 stream
->state
!= SDW_STREAM_DEPREPARED
&&
1589 stream
->state
!= SDW_STREAM_DISABLED
) {
1590 pr_err("%s: %s: inconsistent state state %d\n",
1591 __func__
, stream
->name
, stream
->state
);
1597 * when the stream is DISABLED, this means sdw_prepare_stream()
1598 * is called as a result of an underflow or a resume operation.
1599 * In this case, the bus parameters shall not be recomputed, but
1600 * still need to be re-applied
1602 if (stream
->state
== SDW_STREAM_DISABLED
)
1603 update_params
= false;
1605 ret
= _sdw_prepare_stream(stream
, update_params
);
1608 sdw_release_bus_lock(stream
);
1611 EXPORT_SYMBOL(sdw_prepare_stream
);
1613 static int _sdw_enable_stream(struct sdw_stream_runtime
*stream
)
1615 struct sdw_master_runtime
*m_rt
;
1616 struct sdw_bus
*bus
= NULL
;
1619 /* Enable Master(s) and Slave(s) port(s) associated with stream */
1620 list_for_each_entry(m_rt
, &stream
->master_list
, stream_node
) {
1623 /* Program params */
1624 ret
= sdw_program_params(bus
, false);
1626 dev_err(bus
->dev
, "Program params failed: %d\n", ret
);
1630 /* Enable port(s) */
1631 ret
= sdw_enable_disable_ports(m_rt
, true);
1634 "Enable port(s) failed ret: %d\n", ret
);
1640 pr_err("Configuration error in %s\n", __func__
);
1644 ret
= do_bank_switch(stream
);
1646 dev_err(bus
->dev
, "Bank switch failed: %d\n", ret
);
1650 stream
->state
= SDW_STREAM_ENABLED
;
1655 * sdw_enable_stream() - Enable SoundWire stream
1657 * @stream: Soundwire stream
1659 * Documentation/driver-api/soundwire/stream.rst explains this API in detail
1661 int sdw_enable_stream(struct sdw_stream_runtime
*stream
)
1666 pr_err("SoundWire: Handle not found for stream\n");
1670 sdw_acquire_bus_lock(stream
);
1672 if (stream
->state
!= SDW_STREAM_PREPARED
&&
1673 stream
->state
!= SDW_STREAM_DISABLED
) {
1674 pr_err("%s: %s: inconsistent state state %d\n",
1675 __func__
, stream
->name
, stream
->state
);
1680 ret
= _sdw_enable_stream(stream
);
1683 sdw_release_bus_lock(stream
);
1686 EXPORT_SYMBOL(sdw_enable_stream
);
1688 static int _sdw_disable_stream(struct sdw_stream_runtime
*stream
)
1690 struct sdw_master_runtime
*m_rt
;
1693 list_for_each_entry(m_rt
, &stream
->master_list
, stream_node
) {
1694 struct sdw_bus
*bus
= m_rt
->bus
;
1696 /* Disable port(s) */
1697 ret
= sdw_enable_disable_ports(m_rt
, false);
1699 dev_err(bus
->dev
, "Disable port(s) failed: %d\n", ret
);
1703 stream
->state
= SDW_STREAM_DISABLED
;
1705 list_for_each_entry(m_rt
, &stream
->master_list
, stream_node
) {
1706 struct sdw_bus
*bus
= m_rt
->bus
;
1708 /* Program params */
1709 ret
= sdw_program_params(bus
, false);
1711 dev_err(bus
->dev
, "Program params failed: %d\n", ret
);
1716 ret
= do_bank_switch(stream
);
1718 pr_err("Bank switch failed: %d\n", ret
);
1722 /* make sure alternate bank (previous current) is also disabled */
1723 list_for_each_entry(m_rt
, &stream
->master_list
, stream_node
) {
1724 struct sdw_bus
*bus
= m_rt
->bus
;
1726 /* Disable port(s) */
1727 ret
= sdw_enable_disable_ports(m_rt
, false);
1729 dev_err(bus
->dev
, "Disable port(s) failed: %d\n", ret
);
1738 * sdw_disable_stream() - Disable SoundWire stream
1740 * @stream: Soundwire stream
1742 * Documentation/driver-api/soundwire/stream.rst explains this API in detail
1744 int sdw_disable_stream(struct sdw_stream_runtime
*stream
)
1749 pr_err("SoundWire: Handle not found for stream\n");
1753 sdw_acquire_bus_lock(stream
);
1755 if (stream
->state
!= SDW_STREAM_ENABLED
) {
1756 pr_err("%s: %s: inconsistent state state %d\n",
1757 __func__
, stream
->name
, stream
->state
);
1762 ret
= _sdw_disable_stream(stream
);
1765 sdw_release_bus_lock(stream
);
1768 EXPORT_SYMBOL(sdw_disable_stream
);
1770 static int _sdw_deprepare_stream(struct sdw_stream_runtime
*stream
)
1772 struct sdw_master_runtime
*m_rt
;
1773 struct sdw_bus
*bus
;
1776 list_for_each_entry(m_rt
, &stream
->master_list
, stream_node
) {
1778 /* De-prepare port(s) */
1779 ret
= sdw_prep_deprep_ports(m_rt
, false);
1782 "De-prepare port(s) failed: %d\n", ret
);
1786 /* TODO: Update this during Device-Device support */
1787 bus
->params
.bandwidth
-= m_rt
->stream
->params
.rate
*
1788 m_rt
->ch_count
* m_rt
->stream
->params
.bps
;
1790 /* Compute params */
1791 if (bus
->compute_params
) {
1792 ret
= bus
->compute_params(bus
);
1794 dev_err(bus
->dev
, "Compute params failed: %d",
1800 /* Program params */
1801 ret
= sdw_program_params(bus
, false);
1803 dev_err(bus
->dev
, "Program params failed: %d\n", ret
);
1808 stream
->state
= SDW_STREAM_DEPREPARED
;
1809 return do_bank_switch(stream
);
1813 * sdw_deprepare_stream() - Deprepare SoundWire stream
1815 * @stream: Soundwire stream
1817 * Documentation/driver-api/soundwire/stream.rst explains this API in detail
1819 int sdw_deprepare_stream(struct sdw_stream_runtime
*stream
)
1824 pr_err("SoundWire: Handle not found for stream\n");
1828 sdw_acquire_bus_lock(stream
);
1830 if (stream
->state
!= SDW_STREAM_PREPARED
&&
1831 stream
->state
!= SDW_STREAM_DISABLED
) {
1832 pr_err("%s: %s: inconsistent state state %d\n",
1833 __func__
, stream
->name
, stream
->state
);
1838 ret
= _sdw_deprepare_stream(stream
);
1841 sdw_release_bus_lock(stream
);
1844 EXPORT_SYMBOL(sdw_deprepare_stream
);
1846 static int set_stream(struct snd_pcm_substream
*substream
,
1847 struct sdw_stream_runtime
*sdw_stream
)
1849 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
1850 struct snd_soc_dai
*dai
;
1854 /* Set stream pointer on all DAIs */
1855 for_each_rtd_dais(rtd
, i
, dai
) {
1856 ret
= snd_soc_dai_set_sdw_stream(dai
, sdw_stream
, substream
->stream
);
1858 dev_err(rtd
->dev
, "failed to set stream pointer on dai %s", dai
->name
);
1867 * sdw_startup_stream() - Startup SoundWire stream
1869 * @sdw_substream: Soundwire stream
1871 * Documentation/driver-api/soundwire/stream.rst explains this API in detail
1873 int sdw_startup_stream(void *sdw_substream
)
1875 struct snd_pcm_substream
*substream
= sdw_substream
;
1876 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
1877 struct sdw_stream_runtime
*sdw_stream
;
1881 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
1882 name
= kasprintf(GFP_KERNEL
, "%s-Playback", substream
->name
);
1884 name
= kasprintf(GFP_KERNEL
, "%s-Capture", substream
->name
);
1889 sdw_stream
= sdw_alloc_stream(name
);
1891 dev_err(rtd
->dev
, "alloc stream failed for substream DAI %s", substream
->name
);
1896 ret
= set_stream(substream
, sdw_stream
);
1898 goto release_stream
;
1902 sdw_release_stream(sdw_stream
);
1903 set_stream(substream
, NULL
);
1908 EXPORT_SYMBOL(sdw_startup_stream
);
1911 * sdw_shutdown_stream() - Shutdown SoundWire stream
1913 * @sdw_substream: Soundwire stream
1915 * Documentation/driver-api/soundwire/stream.rst explains this API in detail
1917 void sdw_shutdown_stream(void *sdw_substream
)
1919 struct snd_pcm_substream
*substream
= sdw_substream
;
1920 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
1921 struct sdw_stream_runtime
*sdw_stream
;
1922 struct snd_soc_dai
*dai
;
1924 /* Find stream from first CPU DAI */
1925 dai
= asoc_rtd_to_cpu(rtd
, 0);
1927 sdw_stream
= snd_soc_dai_get_sdw_stream(dai
, substream
->stream
);
1929 if (IS_ERR(sdw_stream
)) {
1930 dev_err(rtd
->dev
, "no stream found for DAI %s", dai
->name
);
1934 /* release memory */
1935 kfree(sdw_stream
->name
);
1936 sdw_release_stream(sdw_stream
);
1938 /* clear DAI data */
1939 set_stream(substream
, NULL
);
1941 EXPORT_SYMBOL(sdw_shutdown_stream
);