xref: /OK3568_Linux_fs/kernel/drivers/soundwire/stream.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2*4882a593Smuzhiyun // Copyright(c) 2015-18 Intel Corporation.
3*4882a593Smuzhiyun 
4*4882a593Smuzhiyun /*
5*4882a593Smuzhiyun  *  stream.c - SoundWire Bus stream operations.
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #include <linux/delay.h>
9*4882a593Smuzhiyun #include <linux/device.h>
10*4882a593Smuzhiyun #include <linux/init.h>
11*4882a593Smuzhiyun #include <linux/module.h>
12*4882a593Smuzhiyun #include <linux/mod_devicetable.h>
13*4882a593Smuzhiyun #include <linux/slab.h>
14*4882a593Smuzhiyun #include <linux/soundwire/sdw_registers.h>
15*4882a593Smuzhiyun #include <linux/soundwire/sdw.h>
16*4882a593Smuzhiyun #include <sound/soc.h>
17*4882a593Smuzhiyun #include "bus.h"
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun /*
20*4882a593Smuzhiyun  * Array of supported rows and columns as per MIPI SoundWire Specification 1.1
21*4882a593Smuzhiyun  *
22*4882a593Smuzhiyun  * The rows are arranged as per the array index value programmed
23*4882a593Smuzhiyun  * in register. The index 15 has dummy value 0 in order to fill hole.
24*4882a593Smuzhiyun  */
25*4882a593Smuzhiyun int sdw_rows[SDW_FRAME_ROWS] = {48, 50, 60, 64, 75, 80, 125, 147,
26*4882a593Smuzhiyun 			96, 100, 120, 128, 150, 160, 250, 0,
27*4882a593Smuzhiyun 			192, 200, 240, 256, 72, 144, 90, 180};
28*4882a593Smuzhiyun EXPORT_SYMBOL(sdw_rows);
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun int sdw_cols[SDW_FRAME_COLS] = {2, 4, 6, 8, 10, 12, 14, 16};
31*4882a593Smuzhiyun EXPORT_SYMBOL(sdw_cols);
32*4882a593Smuzhiyun 
sdw_find_col_index(int col)33*4882a593Smuzhiyun int sdw_find_col_index(int col)
34*4882a593Smuzhiyun {
35*4882a593Smuzhiyun 	int i;
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun 	for (i = 0; i < SDW_FRAME_COLS; i++) {
38*4882a593Smuzhiyun 		if (sdw_cols[i] == col)
39*4882a593Smuzhiyun 			return i;
40*4882a593Smuzhiyun 	}
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun 	pr_warn("Requested column not found, selecting lowest column no: 2\n");
43*4882a593Smuzhiyun 	return 0;
44*4882a593Smuzhiyun }
45*4882a593Smuzhiyun EXPORT_SYMBOL(sdw_find_col_index);
46*4882a593Smuzhiyun 
sdw_find_row_index(int row)47*4882a593Smuzhiyun int sdw_find_row_index(int row)
48*4882a593Smuzhiyun {
49*4882a593Smuzhiyun 	int i;
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun 	for (i = 0; i < SDW_FRAME_ROWS; i++) {
52*4882a593Smuzhiyun 		if (sdw_rows[i] == row)
53*4882a593Smuzhiyun 			return i;
54*4882a593Smuzhiyun 	}
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun 	pr_warn("Requested row not found, selecting lowest row no: 48\n");
57*4882a593Smuzhiyun 	return 0;
58*4882a593Smuzhiyun }
59*4882a593Smuzhiyun EXPORT_SYMBOL(sdw_find_row_index);
60*4882a593Smuzhiyun 
_sdw_program_slave_port_params(struct sdw_bus * bus,struct sdw_slave * slave,struct sdw_transport_params * t_params,enum sdw_dpn_type type)61*4882a593Smuzhiyun static int _sdw_program_slave_port_params(struct sdw_bus *bus,
62*4882a593Smuzhiyun 					  struct sdw_slave *slave,
63*4882a593Smuzhiyun 					  struct sdw_transport_params *t_params,
64*4882a593Smuzhiyun 					  enum sdw_dpn_type type)
65*4882a593Smuzhiyun {
66*4882a593Smuzhiyun 	u32 addr1, addr2, addr3, addr4;
67*4882a593Smuzhiyun 	int ret;
68*4882a593Smuzhiyun 	u16 wbuf;
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun 	if (bus->params.next_bank) {
71*4882a593Smuzhiyun 		addr1 = SDW_DPN_OFFSETCTRL2_B1(t_params->port_num);
72*4882a593Smuzhiyun 		addr2 = SDW_DPN_BLOCKCTRL3_B1(t_params->port_num);
73*4882a593Smuzhiyun 		addr3 = SDW_DPN_SAMPLECTRL2_B1(t_params->port_num);
74*4882a593Smuzhiyun 		addr4 = SDW_DPN_HCTRL_B1(t_params->port_num);
75*4882a593Smuzhiyun 	} else {
76*4882a593Smuzhiyun 		addr1 = SDW_DPN_OFFSETCTRL2_B0(t_params->port_num);
77*4882a593Smuzhiyun 		addr2 = SDW_DPN_BLOCKCTRL3_B0(t_params->port_num);
78*4882a593Smuzhiyun 		addr3 = SDW_DPN_SAMPLECTRL2_B0(t_params->port_num);
79*4882a593Smuzhiyun 		addr4 = SDW_DPN_HCTRL_B0(t_params->port_num);
80*4882a593Smuzhiyun 	}
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun 	/* Program DPN_OffsetCtrl2 registers */
83*4882a593Smuzhiyun 	ret = sdw_write(slave, addr1, t_params->offset2);
84*4882a593Smuzhiyun 	if (ret < 0) {
85*4882a593Smuzhiyun 		dev_err(bus->dev, "DPN_OffsetCtrl2 register write failed\n");
86*4882a593Smuzhiyun 		return ret;
87*4882a593Smuzhiyun 	}
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun 	/* Program DPN_BlockCtrl3 register */
90*4882a593Smuzhiyun 	ret = sdw_write(slave, addr2, t_params->blk_pkg_mode);
91*4882a593Smuzhiyun 	if (ret < 0) {
92*4882a593Smuzhiyun 		dev_err(bus->dev, "DPN_BlockCtrl3 register write failed\n");
93*4882a593Smuzhiyun 		return ret;
94*4882a593Smuzhiyun 	}
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun 	/*
97*4882a593Smuzhiyun 	 * Data ports are FULL, SIMPLE and REDUCED. This function handles
98*4882a593Smuzhiyun 	 * FULL and REDUCED only and beyond this point only FULL is
99*4882a593Smuzhiyun 	 * handled, so bail out if we are not FULL data port type
100*4882a593Smuzhiyun 	 */
101*4882a593Smuzhiyun 	if (type != SDW_DPN_FULL)
102*4882a593Smuzhiyun 		return ret;
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun 	/* Program DPN_SampleCtrl2 register */
105*4882a593Smuzhiyun 	wbuf = FIELD_GET(SDW_DPN_SAMPLECTRL_HIGH, t_params->sample_interval - 1);
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun 	ret = sdw_write(slave, addr3, wbuf);
108*4882a593Smuzhiyun 	if (ret < 0) {
109*4882a593Smuzhiyun 		dev_err(bus->dev, "DPN_SampleCtrl2 register write failed\n");
110*4882a593Smuzhiyun 		return ret;
111*4882a593Smuzhiyun 	}
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun 	/* Program DPN_HCtrl register */
114*4882a593Smuzhiyun 	wbuf = FIELD_PREP(SDW_DPN_HCTRL_HSTART, t_params->hstart);
115*4882a593Smuzhiyun 	wbuf |= FIELD_PREP(SDW_DPN_HCTRL_HSTOP, t_params->hstop);
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun 	ret = sdw_write(slave, addr4, wbuf);
118*4882a593Smuzhiyun 	if (ret < 0)
119*4882a593Smuzhiyun 		dev_err(bus->dev, "DPN_HCtrl register write failed\n");
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 	return ret;
122*4882a593Smuzhiyun }
123*4882a593Smuzhiyun 
sdw_program_slave_port_params(struct sdw_bus * bus,struct sdw_slave_runtime * s_rt,struct sdw_port_runtime * p_rt)124*4882a593Smuzhiyun static int sdw_program_slave_port_params(struct sdw_bus *bus,
125*4882a593Smuzhiyun 					 struct sdw_slave_runtime *s_rt,
126*4882a593Smuzhiyun 					 struct sdw_port_runtime *p_rt)
127*4882a593Smuzhiyun {
128*4882a593Smuzhiyun 	struct sdw_transport_params *t_params = &p_rt->transport_params;
129*4882a593Smuzhiyun 	struct sdw_port_params *p_params = &p_rt->port_params;
130*4882a593Smuzhiyun 	struct sdw_slave_prop *slave_prop = &s_rt->slave->prop;
131*4882a593Smuzhiyun 	u32 addr1, addr2, addr3, addr4, addr5, addr6;
132*4882a593Smuzhiyun 	struct sdw_dpn_prop *dpn_prop;
133*4882a593Smuzhiyun 	int ret;
134*4882a593Smuzhiyun 	u8 wbuf;
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun 	dpn_prop = sdw_get_slave_dpn_prop(s_rt->slave,
137*4882a593Smuzhiyun 					  s_rt->direction,
138*4882a593Smuzhiyun 					  t_params->port_num);
139*4882a593Smuzhiyun 	if (!dpn_prop)
140*4882a593Smuzhiyun 		return -EINVAL;
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 	addr1 = SDW_DPN_PORTCTRL(t_params->port_num);
143*4882a593Smuzhiyun 	addr2 = SDW_DPN_BLOCKCTRL1(t_params->port_num);
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun 	if (bus->params.next_bank) {
146*4882a593Smuzhiyun 		addr3 = SDW_DPN_SAMPLECTRL1_B1(t_params->port_num);
147*4882a593Smuzhiyun 		addr4 = SDW_DPN_OFFSETCTRL1_B1(t_params->port_num);
148*4882a593Smuzhiyun 		addr5 = SDW_DPN_BLOCKCTRL2_B1(t_params->port_num);
149*4882a593Smuzhiyun 		addr6 = SDW_DPN_LANECTRL_B1(t_params->port_num);
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 	} else {
152*4882a593Smuzhiyun 		addr3 = SDW_DPN_SAMPLECTRL1_B0(t_params->port_num);
153*4882a593Smuzhiyun 		addr4 = SDW_DPN_OFFSETCTRL1_B0(t_params->port_num);
154*4882a593Smuzhiyun 		addr5 = SDW_DPN_BLOCKCTRL2_B0(t_params->port_num);
155*4882a593Smuzhiyun 		addr6 = SDW_DPN_LANECTRL_B0(t_params->port_num);
156*4882a593Smuzhiyun 	}
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun 	/* Program DPN_PortCtrl register */
159*4882a593Smuzhiyun 	wbuf = FIELD_PREP(SDW_DPN_PORTCTRL_DATAMODE, p_params->data_mode);
160*4882a593Smuzhiyun 	wbuf |= FIELD_PREP(SDW_DPN_PORTCTRL_FLOWMODE, p_params->flow_mode);
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun 	ret = sdw_update(s_rt->slave, addr1, 0xF, wbuf);
163*4882a593Smuzhiyun 	if (ret < 0) {
164*4882a593Smuzhiyun 		dev_err(&s_rt->slave->dev,
165*4882a593Smuzhiyun 			"DPN_PortCtrl register write failed for port %d\n",
166*4882a593Smuzhiyun 			t_params->port_num);
167*4882a593Smuzhiyun 		return ret;
168*4882a593Smuzhiyun 	}
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun 	if (!dpn_prop->read_only_wordlength) {
171*4882a593Smuzhiyun 		/* Program DPN_BlockCtrl1 register */
172*4882a593Smuzhiyun 		ret = sdw_write(s_rt->slave, addr2, (p_params->bps - 1));
173*4882a593Smuzhiyun 		if (ret < 0) {
174*4882a593Smuzhiyun 			dev_err(&s_rt->slave->dev,
175*4882a593Smuzhiyun 				"DPN_BlockCtrl1 register write failed for port %d\n",
176*4882a593Smuzhiyun 				t_params->port_num);
177*4882a593Smuzhiyun 			return ret;
178*4882a593Smuzhiyun 		}
179*4882a593Smuzhiyun 	}
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun 	/* Program DPN_SampleCtrl1 register */
182*4882a593Smuzhiyun 	wbuf = (t_params->sample_interval - 1) & SDW_DPN_SAMPLECTRL_LOW;
183*4882a593Smuzhiyun 	ret = sdw_write(s_rt->slave, addr3, wbuf);
184*4882a593Smuzhiyun 	if (ret < 0) {
185*4882a593Smuzhiyun 		dev_err(&s_rt->slave->dev,
186*4882a593Smuzhiyun 			"DPN_SampleCtrl1 register write failed for port %d\n",
187*4882a593Smuzhiyun 			t_params->port_num);
188*4882a593Smuzhiyun 		return ret;
189*4882a593Smuzhiyun 	}
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun 	/* Program DPN_OffsetCtrl1 registers */
192*4882a593Smuzhiyun 	ret = sdw_write(s_rt->slave, addr4, t_params->offset1);
193*4882a593Smuzhiyun 	if (ret < 0) {
194*4882a593Smuzhiyun 		dev_err(&s_rt->slave->dev,
195*4882a593Smuzhiyun 			"DPN_OffsetCtrl1 register write failed for port %d\n",
196*4882a593Smuzhiyun 			t_params->port_num);
197*4882a593Smuzhiyun 		return ret;
198*4882a593Smuzhiyun 	}
199*4882a593Smuzhiyun 
200*4882a593Smuzhiyun 	/* Program DPN_BlockCtrl2 register*/
201*4882a593Smuzhiyun 	if (t_params->blk_grp_ctrl_valid) {
202*4882a593Smuzhiyun 		ret = sdw_write(s_rt->slave, addr5, t_params->blk_grp_ctrl);
203*4882a593Smuzhiyun 		if (ret < 0) {
204*4882a593Smuzhiyun 			dev_err(&s_rt->slave->dev,
205*4882a593Smuzhiyun 				"DPN_BlockCtrl2 reg write failed for port %d\n",
206*4882a593Smuzhiyun 				t_params->port_num);
207*4882a593Smuzhiyun 			return ret;
208*4882a593Smuzhiyun 		}
209*4882a593Smuzhiyun 	}
210*4882a593Smuzhiyun 
211*4882a593Smuzhiyun 	/* program DPN_LaneCtrl register */
212*4882a593Smuzhiyun 	if (slave_prop->lane_control_support) {
213*4882a593Smuzhiyun 		ret = sdw_write(s_rt->slave, addr6, t_params->lane_ctrl);
214*4882a593Smuzhiyun 		if (ret < 0) {
215*4882a593Smuzhiyun 			dev_err(&s_rt->slave->dev,
216*4882a593Smuzhiyun 				"DPN_LaneCtrl register write failed for port %d\n",
217*4882a593Smuzhiyun 				t_params->port_num);
218*4882a593Smuzhiyun 			return ret;
219*4882a593Smuzhiyun 		}
220*4882a593Smuzhiyun 	}
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun 	if (dpn_prop->type != SDW_DPN_SIMPLE) {
223*4882a593Smuzhiyun 		ret = _sdw_program_slave_port_params(bus, s_rt->slave,
224*4882a593Smuzhiyun 						     t_params, dpn_prop->type);
225*4882a593Smuzhiyun 		if (ret < 0)
226*4882a593Smuzhiyun 			dev_err(&s_rt->slave->dev,
227*4882a593Smuzhiyun 				"Transport reg write failed for port: %d\n",
228*4882a593Smuzhiyun 				t_params->port_num);
229*4882a593Smuzhiyun 	}
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun 	return ret;
232*4882a593Smuzhiyun }
233*4882a593Smuzhiyun 
sdw_program_master_port_params(struct sdw_bus * bus,struct sdw_port_runtime * p_rt)234*4882a593Smuzhiyun static int sdw_program_master_port_params(struct sdw_bus *bus,
235*4882a593Smuzhiyun 					  struct sdw_port_runtime *p_rt)
236*4882a593Smuzhiyun {
237*4882a593Smuzhiyun 	int ret;
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun 	/*
240*4882a593Smuzhiyun 	 * we need to set transport and port parameters for the port.
241*4882a593Smuzhiyun 	 * Transport parameters refers to the sample interval, offsets and
242*4882a593Smuzhiyun 	 * hstart/stop etc of the data. Port parameters refers to word
243*4882a593Smuzhiyun 	 * length, flow mode etc of the port
244*4882a593Smuzhiyun 	 */
245*4882a593Smuzhiyun 	ret = bus->port_ops->dpn_set_port_transport_params(bus,
246*4882a593Smuzhiyun 					&p_rt->transport_params,
247*4882a593Smuzhiyun 					bus->params.next_bank);
248*4882a593Smuzhiyun 	if (ret < 0)
249*4882a593Smuzhiyun 		return ret;
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun 	return bus->port_ops->dpn_set_port_params(bus,
252*4882a593Smuzhiyun 						  &p_rt->port_params,
253*4882a593Smuzhiyun 						  bus->params.next_bank);
254*4882a593Smuzhiyun }
255*4882a593Smuzhiyun 
256*4882a593Smuzhiyun /**
257*4882a593Smuzhiyun  * sdw_program_port_params() - Programs transport parameters of Master(s)
258*4882a593Smuzhiyun  * and Slave(s)
259*4882a593Smuzhiyun  *
260*4882a593Smuzhiyun  * @m_rt: Master stream runtime
261*4882a593Smuzhiyun  */
sdw_program_port_params(struct sdw_master_runtime * m_rt)262*4882a593Smuzhiyun static int sdw_program_port_params(struct sdw_master_runtime *m_rt)
263*4882a593Smuzhiyun {
264*4882a593Smuzhiyun 	struct sdw_slave_runtime *s_rt = NULL;
265*4882a593Smuzhiyun 	struct sdw_bus *bus = m_rt->bus;
266*4882a593Smuzhiyun 	struct sdw_port_runtime *p_rt;
267*4882a593Smuzhiyun 	int ret = 0;
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun 	/* Program transport & port parameters for Slave(s) */
270*4882a593Smuzhiyun 	list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) {
271*4882a593Smuzhiyun 		list_for_each_entry(p_rt, &s_rt->port_list, port_node) {
272*4882a593Smuzhiyun 			ret = sdw_program_slave_port_params(bus, s_rt, p_rt);
273*4882a593Smuzhiyun 			if (ret < 0)
274*4882a593Smuzhiyun 				return ret;
275*4882a593Smuzhiyun 		}
276*4882a593Smuzhiyun 	}
277*4882a593Smuzhiyun 
278*4882a593Smuzhiyun 	/* Program transport & port parameters for Master(s) */
279*4882a593Smuzhiyun 	list_for_each_entry(p_rt, &m_rt->port_list, port_node) {
280*4882a593Smuzhiyun 		ret = sdw_program_master_port_params(bus, p_rt);
281*4882a593Smuzhiyun 		if (ret < 0)
282*4882a593Smuzhiyun 			return ret;
283*4882a593Smuzhiyun 	}
284*4882a593Smuzhiyun 
285*4882a593Smuzhiyun 	return 0;
286*4882a593Smuzhiyun }
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun /**
289*4882a593Smuzhiyun  * sdw_enable_disable_slave_ports: Enable/disable slave data port
290*4882a593Smuzhiyun  *
291*4882a593Smuzhiyun  * @bus: bus instance
292*4882a593Smuzhiyun  * @s_rt: slave runtime
293*4882a593Smuzhiyun  * @p_rt: port runtime
294*4882a593Smuzhiyun  * @en: enable or disable operation
295*4882a593Smuzhiyun  *
296*4882a593Smuzhiyun  * This function only sets the enable/disable bits in the relevant bank, the
297*4882a593Smuzhiyun  * actual enable/disable is done with a bank switch
298*4882a593Smuzhiyun  */
sdw_enable_disable_slave_ports(struct sdw_bus * bus,struct sdw_slave_runtime * s_rt,struct sdw_port_runtime * p_rt,bool en)299*4882a593Smuzhiyun static int sdw_enable_disable_slave_ports(struct sdw_bus *bus,
300*4882a593Smuzhiyun 					  struct sdw_slave_runtime *s_rt,
301*4882a593Smuzhiyun 					  struct sdw_port_runtime *p_rt,
302*4882a593Smuzhiyun 					  bool en)
303*4882a593Smuzhiyun {
304*4882a593Smuzhiyun 	struct sdw_transport_params *t_params = &p_rt->transport_params;
305*4882a593Smuzhiyun 	u32 addr;
306*4882a593Smuzhiyun 	int ret;
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun 	if (bus->params.next_bank)
309*4882a593Smuzhiyun 		addr = SDW_DPN_CHANNELEN_B1(p_rt->num);
310*4882a593Smuzhiyun 	else
311*4882a593Smuzhiyun 		addr = SDW_DPN_CHANNELEN_B0(p_rt->num);
312*4882a593Smuzhiyun 
313*4882a593Smuzhiyun 	/*
314*4882a593Smuzhiyun 	 * Since bus doesn't support sharing a port across two streams,
315*4882a593Smuzhiyun 	 * it is safe to reset this register
316*4882a593Smuzhiyun 	 */
317*4882a593Smuzhiyun 	if (en)
318*4882a593Smuzhiyun 		ret = sdw_write(s_rt->slave, addr, p_rt->ch_mask);
319*4882a593Smuzhiyun 	else
320*4882a593Smuzhiyun 		ret = sdw_write(s_rt->slave, addr, 0x0);
321*4882a593Smuzhiyun 
322*4882a593Smuzhiyun 	if (ret < 0)
323*4882a593Smuzhiyun 		dev_err(&s_rt->slave->dev,
324*4882a593Smuzhiyun 			"Slave chn_en reg write failed:%d port:%d\n",
325*4882a593Smuzhiyun 			ret, t_params->port_num);
326*4882a593Smuzhiyun 
327*4882a593Smuzhiyun 	return ret;
328*4882a593Smuzhiyun }
329*4882a593Smuzhiyun 
sdw_enable_disable_master_ports(struct sdw_master_runtime * m_rt,struct sdw_port_runtime * p_rt,bool en)330*4882a593Smuzhiyun static int sdw_enable_disable_master_ports(struct sdw_master_runtime *m_rt,
331*4882a593Smuzhiyun 					   struct sdw_port_runtime *p_rt,
332*4882a593Smuzhiyun 					   bool en)
333*4882a593Smuzhiyun {
334*4882a593Smuzhiyun 	struct sdw_transport_params *t_params = &p_rt->transport_params;
335*4882a593Smuzhiyun 	struct sdw_bus *bus = m_rt->bus;
336*4882a593Smuzhiyun 	struct sdw_enable_ch enable_ch;
337*4882a593Smuzhiyun 	int ret;
338*4882a593Smuzhiyun 
339*4882a593Smuzhiyun 	enable_ch.port_num = p_rt->num;
340*4882a593Smuzhiyun 	enable_ch.ch_mask = p_rt->ch_mask;
341*4882a593Smuzhiyun 	enable_ch.enable = en;
342*4882a593Smuzhiyun 
343*4882a593Smuzhiyun 	/* Perform Master port channel(s) enable/disable */
344*4882a593Smuzhiyun 	if (bus->port_ops->dpn_port_enable_ch) {
345*4882a593Smuzhiyun 		ret = bus->port_ops->dpn_port_enable_ch(bus,
346*4882a593Smuzhiyun 							&enable_ch,
347*4882a593Smuzhiyun 							bus->params.next_bank);
348*4882a593Smuzhiyun 		if (ret < 0) {
349*4882a593Smuzhiyun 			dev_err(bus->dev,
350*4882a593Smuzhiyun 				"Master chn_en write failed:%d port:%d\n",
351*4882a593Smuzhiyun 				ret, t_params->port_num);
352*4882a593Smuzhiyun 			return ret;
353*4882a593Smuzhiyun 		}
354*4882a593Smuzhiyun 	} else {
355*4882a593Smuzhiyun 		dev_err(bus->dev,
356*4882a593Smuzhiyun 			"dpn_port_enable_ch not supported, %s failed\n",
357*4882a593Smuzhiyun 			en ? "enable" : "disable");
358*4882a593Smuzhiyun 		return -EINVAL;
359*4882a593Smuzhiyun 	}
360*4882a593Smuzhiyun 
361*4882a593Smuzhiyun 	return 0;
362*4882a593Smuzhiyun }
363*4882a593Smuzhiyun 
364*4882a593Smuzhiyun /**
365*4882a593Smuzhiyun  * sdw_enable_disable_ports() - Enable/disable port(s) for Master and
366*4882a593Smuzhiyun  * Slave(s)
367*4882a593Smuzhiyun  *
368*4882a593Smuzhiyun  * @m_rt: Master stream runtime
369*4882a593Smuzhiyun  * @en: mode (enable/disable)
370*4882a593Smuzhiyun  */
sdw_enable_disable_ports(struct sdw_master_runtime * m_rt,bool en)371*4882a593Smuzhiyun static int sdw_enable_disable_ports(struct sdw_master_runtime *m_rt, bool en)
372*4882a593Smuzhiyun {
373*4882a593Smuzhiyun 	struct sdw_port_runtime *s_port, *m_port;
374*4882a593Smuzhiyun 	struct sdw_slave_runtime *s_rt;
375*4882a593Smuzhiyun 	int ret = 0;
376*4882a593Smuzhiyun 
377*4882a593Smuzhiyun 	/* Enable/Disable Slave port(s) */
378*4882a593Smuzhiyun 	list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) {
379*4882a593Smuzhiyun 		list_for_each_entry(s_port, &s_rt->port_list, port_node) {
380*4882a593Smuzhiyun 			ret = sdw_enable_disable_slave_ports(m_rt->bus, s_rt,
381*4882a593Smuzhiyun 							     s_port, en);
382*4882a593Smuzhiyun 			if (ret < 0)
383*4882a593Smuzhiyun 				return ret;
384*4882a593Smuzhiyun 		}
385*4882a593Smuzhiyun 	}
386*4882a593Smuzhiyun 
387*4882a593Smuzhiyun 	/* Enable/Disable Master port(s) */
388*4882a593Smuzhiyun 	list_for_each_entry(m_port, &m_rt->port_list, port_node) {
389*4882a593Smuzhiyun 		ret = sdw_enable_disable_master_ports(m_rt, m_port, en);
390*4882a593Smuzhiyun 		if (ret < 0)
391*4882a593Smuzhiyun 			return ret;
392*4882a593Smuzhiyun 	}
393*4882a593Smuzhiyun 
394*4882a593Smuzhiyun 	return 0;
395*4882a593Smuzhiyun }
396*4882a593Smuzhiyun 
sdw_do_port_prep(struct sdw_slave_runtime * s_rt,struct sdw_prepare_ch prep_ch,enum sdw_port_prep_ops cmd)397*4882a593Smuzhiyun static int sdw_do_port_prep(struct sdw_slave_runtime *s_rt,
398*4882a593Smuzhiyun 			    struct sdw_prepare_ch prep_ch,
399*4882a593Smuzhiyun 			    enum sdw_port_prep_ops cmd)
400*4882a593Smuzhiyun {
401*4882a593Smuzhiyun 	const struct sdw_slave_ops *ops = s_rt->slave->ops;
402*4882a593Smuzhiyun 	int ret;
403*4882a593Smuzhiyun 
404*4882a593Smuzhiyun 	if (ops->port_prep) {
405*4882a593Smuzhiyun 		ret = ops->port_prep(s_rt->slave, &prep_ch, cmd);
406*4882a593Smuzhiyun 		if (ret < 0) {
407*4882a593Smuzhiyun 			dev_err(&s_rt->slave->dev,
408*4882a593Smuzhiyun 				"Slave Port Prep cmd %d failed: %d\n",
409*4882a593Smuzhiyun 				cmd, ret);
410*4882a593Smuzhiyun 			return ret;
411*4882a593Smuzhiyun 		}
412*4882a593Smuzhiyun 	}
413*4882a593Smuzhiyun 
414*4882a593Smuzhiyun 	return 0;
415*4882a593Smuzhiyun }
416*4882a593Smuzhiyun 
sdw_prep_deprep_slave_ports(struct sdw_bus * bus,struct sdw_slave_runtime * s_rt,struct sdw_port_runtime * p_rt,bool prep)417*4882a593Smuzhiyun static int sdw_prep_deprep_slave_ports(struct sdw_bus *bus,
418*4882a593Smuzhiyun 				       struct sdw_slave_runtime *s_rt,
419*4882a593Smuzhiyun 				       struct sdw_port_runtime *p_rt,
420*4882a593Smuzhiyun 				       bool prep)
421*4882a593Smuzhiyun {
422*4882a593Smuzhiyun 	struct completion *port_ready;
423*4882a593Smuzhiyun 	struct sdw_dpn_prop *dpn_prop;
424*4882a593Smuzhiyun 	struct sdw_prepare_ch prep_ch;
425*4882a593Smuzhiyun 	bool intr = false;
426*4882a593Smuzhiyun 	int ret = 0, val;
427*4882a593Smuzhiyun 	u32 addr;
428*4882a593Smuzhiyun 
429*4882a593Smuzhiyun 	prep_ch.num = p_rt->num;
430*4882a593Smuzhiyun 	prep_ch.ch_mask = p_rt->ch_mask;
431*4882a593Smuzhiyun 
432*4882a593Smuzhiyun 	dpn_prop = sdw_get_slave_dpn_prop(s_rt->slave,
433*4882a593Smuzhiyun 					  s_rt->direction,
434*4882a593Smuzhiyun 					  prep_ch.num);
435*4882a593Smuzhiyun 	if (!dpn_prop) {
436*4882a593Smuzhiyun 		dev_err(bus->dev,
437*4882a593Smuzhiyun 			"Slave Port:%d properties not found\n", prep_ch.num);
438*4882a593Smuzhiyun 		return -EINVAL;
439*4882a593Smuzhiyun 	}
440*4882a593Smuzhiyun 
441*4882a593Smuzhiyun 	prep_ch.prepare = prep;
442*4882a593Smuzhiyun 
443*4882a593Smuzhiyun 	prep_ch.bank = bus->params.next_bank;
444*4882a593Smuzhiyun 
445*4882a593Smuzhiyun 	if (dpn_prop->imp_def_interrupts || !dpn_prop->simple_ch_prep_sm ||
446*4882a593Smuzhiyun 	    bus->params.s_data_mode != SDW_PORT_DATA_MODE_NORMAL)
447*4882a593Smuzhiyun 		intr = true;
448*4882a593Smuzhiyun 
449*4882a593Smuzhiyun 	/*
450*4882a593Smuzhiyun 	 * Enable interrupt before Port prepare.
451*4882a593Smuzhiyun 	 * For Port de-prepare, it is assumed that port
452*4882a593Smuzhiyun 	 * was prepared earlier
453*4882a593Smuzhiyun 	 */
454*4882a593Smuzhiyun 	if (prep && intr) {
455*4882a593Smuzhiyun 		ret = sdw_configure_dpn_intr(s_rt->slave, p_rt->num, prep,
456*4882a593Smuzhiyun 					     dpn_prop->imp_def_interrupts);
457*4882a593Smuzhiyun 		if (ret < 0)
458*4882a593Smuzhiyun 			return ret;
459*4882a593Smuzhiyun 	}
460*4882a593Smuzhiyun 
461*4882a593Smuzhiyun 	/* Inform slave about the impending port prepare */
462*4882a593Smuzhiyun 	sdw_do_port_prep(s_rt, prep_ch, SDW_OPS_PORT_PRE_PREP);
463*4882a593Smuzhiyun 
464*4882a593Smuzhiyun 	/* Prepare Slave port implementing CP_SM */
465*4882a593Smuzhiyun 	if (!dpn_prop->simple_ch_prep_sm) {
466*4882a593Smuzhiyun 		addr = SDW_DPN_PREPARECTRL(p_rt->num);
467*4882a593Smuzhiyun 
468*4882a593Smuzhiyun 		if (prep)
469*4882a593Smuzhiyun 			ret = sdw_write(s_rt->slave, addr, p_rt->ch_mask);
470*4882a593Smuzhiyun 		else
471*4882a593Smuzhiyun 			ret = sdw_write(s_rt->slave, addr, 0x0);
472*4882a593Smuzhiyun 
473*4882a593Smuzhiyun 		if (ret < 0) {
474*4882a593Smuzhiyun 			dev_err(&s_rt->slave->dev,
475*4882a593Smuzhiyun 				"Slave prep_ctrl reg write failed\n");
476*4882a593Smuzhiyun 			return ret;
477*4882a593Smuzhiyun 		}
478*4882a593Smuzhiyun 
479*4882a593Smuzhiyun 		/* Wait for completion on port ready */
480*4882a593Smuzhiyun 		port_ready = &s_rt->slave->port_ready[prep_ch.num];
481*4882a593Smuzhiyun 		wait_for_completion_timeout(port_ready,
482*4882a593Smuzhiyun 			msecs_to_jiffies(dpn_prop->ch_prep_timeout));
483*4882a593Smuzhiyun 
484*4882a593Smuzhiyun 		val = sdw_read(s_rt->slave, SDW_DPN_PREPARESTATUS(p_rt->num));
485*4882a593Smuzhiyun 		if ((val < 0) || (val & p_rt->ch_mask)) {
486*4882a593Smuzhiyun 			ret = (val < 0) ? val : -ETIMEDOUT;
487*4882a593Smuzhiyun 			dev_err(&s_rt->slave->dev,
488*4882a593Smuzhiyun 				"Chn prep failed for port %d: %d\n", prep_ch.num, ret);
489*4882a593Smuzhiyun 			return ret;
490*4882a593Smuzhiyun 		}
491*4882a593Smuzhiyun 	}
492*4882a593Smuzhiyun 
493*4882a593Smuzhiyun 	/* Inform slaves about ports prepared */
494*4882a593Smuzhiyun 	sdw_do_port_prep(s_rt, prep_ch, SDW_OPS_PORT_POST_PREP);
495*4882a593Smuzhiyun 
496*4882a593Smuzhiyun 	/* Disable interrupt after Port de-prepare */
497*4882a593Smuzhiyun 	if (!prep && intr)
498*4882a593Smuzhiyun 		ret = sdw_configure_dpn_intr(s_rt->slave, p_rt->num, prep,
499*4882a593Smuzhiyun 					     dpn_prop->imp_def_interrupts);
500*4882a593Smuzhiyun 
501*4882a593Smuzhiyun 	return ret;
502*4882a593Smuzhiyun }
503*4882a593Smuzhiyun 
sdw_prep_deprep_master_ports(struct sdw_master_runtime * m_rt,struct sdw_port_runtime * p_rt,bool prep)504*4882a593Smuzhiyun static int sdw_prep_deprep_master_ports(struct sdw_master_runtime *m_rt,
505*4882a593Smuzhiyun 					struct sdw_port_runtime *p_rt,
506*4882a593Smuzhiyun 					bool prep)
507*4882a593Smuzhiyun {
508*4882a593Smuzhiyun 	struct sdw_transport_params *t_params = &p_rt->transport_params;
509*4882a593Smuzhiyun 	struct sdw_bus *bus = m_rt->bus;
510*4882a593Smuzhiyun 	const struct sdw_master_port_ops *ops = bus->port_ops;
511*4882a593Smuzhiyun 	struct sdw_prepare_ch prep_ch;
512*4882a593Smuzhiyun 	int ret = 0;
513*4882a593Smuzhiyun 
514*4882a593Smuzhiyun 	prep_ch.num = p_rt->num;
515*4882a593Smuzhiyun 	prep_ch.ch_mask = p_rt->ch_mask;
516*4882a593Smuzhiyun 	prep_ch.prepare = prep; /* Prepare/De-prepare */
517*4882a593Smuzhiyun 	prep_ch.bank = bus->params.next_bank;
518*4882a593Smuzhiyun 
519*4882a593Smuzhiyun 	/* Pre-prepare/Pre-deprepare port(s) */
520*4882a593Smuzhiyun 	if (ops->dpn_port_prep) {
521*4882a593Smuzhiyun 		ret = ops->dpn_port_prep(bus, &prep_ch);
522*4882a593Smuzhiyun 		if (ret < 0) {
523*4882a593Smuzhiyun 			dev_err(bus->dev, "Port prepare failed for port:%d\n",
524*4882a593Smuzhiyun 				t_params->port_num);
525*4882a593Smuzhiyun 			return ret;
526*4882a593Smuzhiyun 		}
527*4882a593Smuzhiyun 	}
528*4882a593Smuzhiyun 
529*4882a593Smuzhiyun 	return ret;
530*4882a593Smuzhiyun }
531*4882a593Smuzhiyun 
532*4882a593Smuzhiyun /**
533*4882a593Smuzhiyun  * sdw_prep_deprep_ports() - Prepare/De-prepare port(s) for Master(s) and
534*4882a593Smuzhiyun  * Slave(s)
535*4882a593Smuzhiyun  *
536*4882a593Smuzhiyun  * @m_rt: Master runtime handle
537*4882a593Smuzhiyun  * @prep: Prepare or De-prepare
538*4882a593Smuzhiyun  */
sdw_prep_deprep_ports(struct sdw_master_runtime * m_rt,bool prep)539*4882a593Smuzhiyun static int sdw_prep_deprep_ports(struct sdw_master_runtime *m_rt, bool prep)
540*4882a593Smuzhiyun {
541*4882a593Smuzhiyun 	struct sdw_slave_runtime *s_rt;
542*4882a593Smuzhiyun 	struct sdw_port_runtime *p_rt;
543*4882a593Smuzhiyun 	int ret = 0;
544*4882a593Smuzhiyun 
545*4882a593Smuzhiyun 	/* Prepare/De-prepare Slave port(s) */
546*4882a593Smuzhiyun 	list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) {
547*4882a593Smuzhiyun 		list_for_each_entry(p_rt, &s_rt->port_list, port_node) {
548*4882a593Smuzhiyun 			ret = sdw_prep_deprep_slave_ports(m_rt->bus, s_rt,
549*4882a593Smuzhiyun 							  p_rt, prep);
550*4882a593Smuzhiyun 			if (ret < 0)
551*4882a593Smuzhiyun 				return ret;
552*4882a593Smuzhiyun 		}
553*4882a593Smuzhiyun 	}
554*4882a593Smuzhiyun 
555*4882a593Smuzhiyun 	/* Prepare/De-prepare Master port(s) */
556*4882a593Smuzhiyun 	list_for_each_entry(p_rt, &m_rt->port_list, port_node) {
557*4882a593Smuzhiyun 		ret = sdw_prep_deprep_master_ports(m_rt, p_rt, prep);
558*4882a593Smuzhiyun 		if (ret < 0)
559*4882a593Smuzhiyun 			return ret;
560*4882a593Smuzhiyun 	}
561*4882a593Smuzhiyun 
562*4882a593Smuzhiyun 	return ret;
563*4882a593Smuzhiyun }
564*4882a593Smuzhiyun 
565*4882a593Smuzhiyun /**
566*4882a593Smuzhiyun  * sdw_notify_config() - Notify bus configuration
567*4882a593Smuzhiyun  *
568*4882a593Smuzhiyun  * @m_rt: Master runtime handle
569*4882a593Smuzhiyun  *
570*4882a593Smuzhiyun  * This function notifies the Master(s) and Slave(s) of the
571*4882a593Smuzhiyun  * new bus configuration.
572*4882a593Smuzhiyun  */
sdw_notify_config(struct sdw_master_runtime * m_rt)573*4882a593Smuzhiyun static int sdw_notify_config(struct sdw_master_runtime *m_rt)
574*4882a593Smuzhiyun {
575*4882a593Smuzhiyun 	struct sdw_slave_runtime *s_rt;
576*4882a593Smuzhiyun 	struct sdw_bus *bus = m_rt->bus;
577*4882a593Smuzhiyun 	struct sdw_slave *slave;
578*4882a593Smuzhiyun 	int ret = 0;
579*4882a593Smuzhiyun 
580*4882a593Smuzhiyun 	if (bus->ops->set_bus_conf) {
581*4882a593Smuzhiyun 		ret = bus->ops->set_bus_conf(bus, &bus->params);
582*4882a593Smuzhiyun 		if (ret < 0)
583*4882a593Smuzhiyun 			return ret;
584*4882a593Smuzhiyun 	}
585*4882a593Smuzhiyun 
586*4882a593Smuzhiyun 	list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) {
587*4882a593Smuzhiyun 		slave = s_rt->slave;
588*4882a593Smuzhiyun 
589*4882a593Smuzhiyun 		if (slave->ops->bus_config) {
590*4882a593Smuzhiyun 			ret = slave->ops->bus_config(slave, &bus->params);
591*4882a593Smuzhiyun 			if (ret < 0) {
592*4882a593Smuzhiyun 				dev_err(bus->dev, "Notify Slave: %d failed\n",
593*4882a593Smuzhiyun 					slave->dev_num);
594*4882a593Smuzhiyun 				return ret;
595*4882a593Smuzhiyun 			}
596*4882a593Smuzhiyun 		}
597*4882a593Smuzhiyun 	}
598*4882a593Smuzhiyun 
599*4882a593Smuzhiyun 	return ret;
600*4882a593Smuzhiyun }
601*4882a593Smuzhiyun 
602*4882a593Smuzhiyun /**
603*4882a593Smuzhiyun  * sdw_program_params() - Program transport and port parameters for Master(s)
604*4882a593Smuzhiyun  * and Slave(s)
605*4882a593Smuzhiyun  *
606*4882a593Smuzhiyun  * @bus: SDW bus instance
607*4882a593Smuzhiyun  * @prepare: true if sdw_program_params() is called by _prepare.
608*4882a593Smuzhiyun  */
sdw_program_params(struct sdw_bus * bus,bool prepare)609*4882a593Smuzhiyun static int sdw_program_params(struct sdw_bus *bus, bool prepare)
610*4882a593Smuzhiyun {
611*4882a593Smuzhiyun 	struct sdw_master_runtime *m_rt;
612*4882a593Smuzhiyun 	int ret = 0;
613*4882a593Smuzhiyun 
614*4882a593Smuzhiyun 	list_for_each_entry(m_rt, &bus->m_rt_list, bus_node) {
615*4882a593Smuzhiyun 
616*4882a593Smuzhiyun 		/*
617*4882a593Smuzhiyun 		 * this loop walks through all master runtimes for a
618*4882a593Smuzhiyun 		 * bus, but the ports can only be configured while
619*4882a593Smuzhiyun 		 * explicitly preparing a stream or handling an
620*4882a593Smuzhiyun 		 * already-prepared stream otherwise.
621*4882a593Smuzhiyun 		 */
622*4882a593Smuzhiyun 		if (!prepare &&
623*4882a593Smuzhiyun 		    m_rt->stream->state == SDW_STREAM_CONFIGURED)
624*4882a593Smuzhiyun 			continue;
625*4882a593Smuzhiyun 
626*4882a593Smuzhiyun 		ret = sdw_program_port_params(m_rt);
627*4882a593Smuzhiyun 		if (ret < 0) {
628*4882a593Smuzhiyun 			dev_err(bus->dev,
629*4882a593Smuzhiyun 				"Program transport params failed: %d\n", ret);
630*4882a593Smuzhiyun 			return ret;
631*4882a593Smuzhiyun 		}
632*4882a593Smuzhiyun 
633*4882a593Smuzhiyun 		ret = sdw_notify_config(m_rt);
634*4882a593Smuzhiyun 		if (ret < 0) {
635*4882a593Smuzhiyun 			dev_err(bus->dev,
636*4882a593Smuzhiyun 				"Notify bus config failed: %d\n", ret);
637*4882a593Smuzhiyun 			return ret;
638*4882a593Smuzhiyun 		}
639*4882a593Smuzhiyun 
640*4882a593Smuzhiyun 		/* Enable port(s) on alternate bank for all active streams */
641*4882a593Smuzhiyun 		if (m_rt->stream->state != SDW_STREAM_ENABLED)
642*4882a593Smuzhiyun 			continue;
643*4882a593Smuzhiyun 
644*4882a593Smuzhiyun 		ret = sdw_enable_disable_ports(m_rt, true);
645*4882a593Smuzhiyun 		if (ret < 0) {
646*4882a593Smuzhiyun 			dev_err(bus->dev, "Enable channel failed: %d\n", ret);
647*4882a593Smuzhiyun 			return ret;
648*4882a593Smuzhiyun 		}
649*4882a593Smuzhiyun 	}
650*4882a593Smuzhiyun 
651*4882a593Smuzhiyun 	return ret;
652*4882a593Smuzhiyun }
653*4882a593Smuzhiyun 
sdw_bank_switch(struct sdw_bus * bus,int m_rt_count)654*4882a593Smuzhiyun static int sdw_bank_switch(struct sdw_bus *bus, int m_rt_count)
655*4882a593Smuzhiyun {
656*4882a593Smuzhiyun 	int col_index, row_index;
657*4882a593Smuzhiyun 	bool multi_link;
658*4882a593Smuzhiyun 	struct sdw_msg *wr_msg;
659*4882a593Smuzhiyun 	u8 *wbuf;
660*4882a593Smuzhiyun 	int ret;
661*4882a593Smuzhiyun 	u16 addr;
662*4882a593Smuzhiyun 
663*4882a593Smuzhiyun 	wr_msg = kzalloc(sizeof(*wr_msg), GFP_KERNEL);
664*4882a593Smuzhiyun 	if (!wr_msg)
665*4882a593Smuzhiyun 		return -ENOMEM;
666*4882a593Smuzhiyun 
667*4882a593Smuzhiyun 	bus->defer_msg.msg = wr_msg;
668*4882a593Smuzhiyun 
669*4882a593Smuzhiyun 	wbuf = kzalloc(sizeof(*wbuf), GFP_KERNEL);
670*4882a593Smuzhiyun 	if (!wbuf) {
671*4882a593Smuzhiyun 		ret = -ENOMEM;
672*4882a593Smuzhiyun 		goto error_1;
673*4882a593Smuzhiyun 	}
674*4882a593Smuzhiyun 
675*4882a593Smuzhiyun 	/* Get row and column index to program register */
676*4882a593Smuzhiyun 	col_index = sdw_find_col_index(bus->params.col);
677*4882a593Smuzhiyun 	row_index = sdw_find_row_index(bus->params.row);
678*4882a593Smuzhiyun 	wbuf[0] = col_index | (row_index << 3);
679*4882a593Smuzhiyun 
680*4882a593Smuzhiyun 	if (bus->params.next_bank)
681*4882a593Smuzhiyun 		addr = SDW_SCP_FRAMECTRL_B1;
682*4882a593Smuzhiyun 	else
683*4882a593Smuzhiyun 		addr = SDW_SCP_FRAMECTRL_B0;
684*4882a593Smuzhiyun 
685*4882a593Smuzhiyun 	sdw_fill_msg(wr_msg, NULL, addr, 1, SDW_BROADCAST_DEV_NUM,
686*4882a593Smuzhiyun 		     SDW_MSG_FLAG_WRITE, wbuf);
687*4882a593Smuzhiyun 	wr_msg->ssp_sync = true;
688*4882a593Smuzhiyun 
689*4882a593Smuzhiyun 	/*
690*4882a593Smuzhiyun 	 * Set the multi_link flag only when both the hardware supports
691*4882a593Smuzhiyun 	 * and hardware-based sync is required
692*4882a593Smuzhiyun 	 */
693*4882a593Smuzhiyun 	multi_link = bus->multi_link && (m_rt_count >= bus->hw_sync_min_links);
694*4882a593Smuzhiyun 
695*4882a593Smuzhiyun 	if (multi_link)
696*4882a593Smuzhiyun 		ret = sdw_transfer_defer(bus, wr_msg, &bus->defer_msg);
697*4882a593Smuzhiyun 	else
698*4882a593Smuzhiyun 		ret = sdw_transfer(bus, wr_msg);
699*4882a593Smuzhiyun 
700*4882a593Smuzhiyun 	if (ret < 0) {
701*4882a593Smuzhiyun 		dev_err(bus->dev, "Slave frame_ctrl reg write failed\n");
702*4882a593Smuzhiyun 		goto error;
703*4882a593Smuzhiyun 	}
704*4882a593Smuzhiyun 
705*4882a593Smuzhiyun 	if (!multi_link) {
706*4882a593Smuzhiyun 		kfree(wr_msg);
707*4882a593Smuzhiyun 		kfree(wbuf);
708*4882a593Smuzhiyun 		bus->defer_msg.msg = NULL;
709*4882a593Smuzhiyun 		bus->params.curr_bank = !bus->params.curr_bank;
710*4882a593Smuzhiyun 		bus->params.next_bank = !bus->params.next_bank;
711*4882a593Smuzhiyun 	}
712*4882a593Smuzhiyun 
713*4882a593Smuzhiyun 	return 0;
714*4882a593Smuzhiyun 
715*4882a593Smuzhiyun error:
716*4882a593Smuzhiyun 	kfree(wbuf);
717*4882a593Smuzhiyun error_1:
718*4882a593Smuzhiyun 	kfree(wr_msg);
719*4882a593Smuzhiyun 	bus->defer_msg.msg = NULL;
720*4882a593Smuzhiyun 	return ret;
721*4882a593Smuzhiyun }
722*4882a593Smuzhiyun 
723*4882a593Smuzhiyun /**
724*4882a593Smuzhiyun  * sdw_ml_sync_bank_switch: Multilink register bank switch
725*4882a593Smuzhiyun  *
726*4882a593Smuzhiyun  * @bus: SDW bus instance
727*4882a593Smuzhiyun  *
728*4882a593Smuzhiyun  * Caller function should free the buffers on error
729*4882a593Smuzhiyun  */
sdw_ml_sync_bank_switch(struct sdw_bus * bus)730*4882a593Smuzhiyun static int sdw_ml_sync_bank_switch(struct sdw_bus *bus)
731*4882a593Smuzhiyun {
732*4882a593Smuzhiyun 	unsigned long time_left;
733*4882a593Smuzhiyun 
734*4882a593Smuzhiyun 	if (!bus->multi_link)
735*4882a593Smuzhiyun 		return 0;
736*4882a593Smuzhiyun 
737*4882a593Smuzhiyun 	/* Wait for completion of transfer */
738*4882a593Smuzhiyun 	time_left = wait_for_completion_timeout(&bus->defer_msg.complete,
739*4882a593Smuzhiyun 						bus->bank_switch_timeout);
740*4882a593Smuzhiyun 
741*4882a593Smuzhiyun 	if (!time_left) {
742*4882a593Smuzhiyun 		dev_err(bus->dev, "Controller Timed out on bank switch\n");
743*4882a593Smuzhiyun 		return -ETIMEDOUT;
744*4882a593Smuzhiyun 	}
745*4882a593Smuzhiyun 
746*4882a593Smuzhiyun 	bus->params.curr_bank = !bus->params.curr_bank;
747*4882a593Smuzhiyun 	bus->params.next_bank = !bus->params.next_bank;
748*4882a593Smuzhiyun 
749*4882a593Smuzhiyun 	if (bus->defer_msg.msg) {
750*4882a593Smuzhiyun 		kfree(bus->defer_msg.msg->buf);
751*4882a593Smuzhiyun 		kfree(bus->defer_msg.msg);
752*4882a593Smuzhiyun 	}
753*4882a593Smuzhiyun 
754*4882a593Smuzhiyun 	return 0;
755*4882a593Smuzhiyun }
756*4882a593Smuzhiyun 
do_bank_switch(struct sdw_stream_runtime * stream)757*4882a593Smuzhiyun static int do_bank_switch(struct sdw_stream_runtime *stream)
758*4882a593Smuzhiyun {
759*4882a593Smuzhiyun 	struct sdw_master_runtime *m_rt;
760*4882a593Smuzhiyun 	const struct sdw_master_ops *ops;
761*4882a593Smuzhiyun 	struct sdw_bus *bus;
762*4882a593Smuzhiyun 	bool multi_link = false;
763*4882a593Smuzhiyun 	int m_rt_count;
764*4882a593Smuzhiyun 	int ret = 0;
765*4882a593Smuzhiyun 
766*4882a593Smuzhiyun 	m_rt_count = stream->m_rt_count;
767*4882a593Smuzhiyun 
768*4882a593Smuzhiyun 	list_for_each_entry(m_rt, &stream->master_list, stream_node) {
769*4882a593Smuzhiyun 		bus = m_rt->bus;
770*4882a593Smuzhiyun 		ops = bus->ops;
771*4882a593Smuzhiyun 
772*4882a593Smuzhiyun 		if (bus->multi_link && m_rt_count >= bus->hw_sync_min_links) {
773*4882a593Smuzhiyun 			multi_link = true;
774*4882a593Smuzhiyun 			mutex_lock(&bus->msg_lock);
775*4882a593Smuzhiyun 		}
776*4882a593Smuzhiyun 
777*4882a593Smuzhiyun 		/* Pre-bank switch */
778*4882a593Smuzhiyun 		if (ops->pre_bank_switch) {
779*4882a593Smuzhiyun 			ret = ops->pre_bank_switch(bus);
780*4882a593Smuzhiyun 			if (ret < 0) {
781*4882a593Smuzhiyun 				dev_err(bus->dev,
782*4882a593Smuzhiyun 					"Pre bank switch op failed: %d\n", ret);
783*4882a593Smuzhiyun 				goto msg_unlock;
784*4882a593Smuzhiyun 			}
785*4882a593Smuzhiyun 		}
786*4882a593Smuzhiyun 
787*4882a593Smuzhiyun 		/*
788*4882a593Smuzhiyun 		 * Perform Bank switch operation.
789*4882a593Smuzhiyun 		 * For multi link cases, the actual bank switch is
790*4882a593Smuzhiyun 		 * synchronized across all Masters and happens later as a
791*4882a593Smuzhiyun 		 * part of post_bank_switch ops.
792*4882a593Smuzhiyun 		 */
793*4882a593Smuzhiyun 		ret = sdw_bank_switch(bus, m_rt_count);
794*4882a593Smuzhiyun 		if (ret < 0) {
795*4882a593Smuzhiyun 			dev_err(bus->dev, "Bank switch failed: %d\n", ret);
796*4882a593Smuzhiyun 			goto error;
797*4882a593Smuzhiyun 		}
798*4882a593Smuzhiyun 	}
799*4882a593Smuzhiyun 
800*4882a593Smuzhiyun 	/*
801*4882a593Smuzhiyun 	 * For multi link cases, it is expected that the bank switch is
802*4882a593Smuzhiyun 	 * triggered by the post_bank_switch for the first Master in the list
803*4882a593Smuzhiyun 	 * and for the other Masters the post_bank_switch() should return doing
804*4882a593Smuzhiyun 	 * nothing.
805*4882a593Smuzhiyun 	 */
806*4882a593Smuzhiyun 	list_for_each_entry(m_rt, &stream->master_list, stream_node) {
807*4882a593Smuzhiyun 		bus = m_rt->bus;
808*4882a593Smuzhiyun 		ops = bus->ops;
809*4882a593Smuzhiyun 
810*4882a593Smuzhiyun 		/* Post-bank switch */
811*4882a593Smuzhiyun 		if (ops->post_bank_switch) {
812*4882a593Smuzhiyun 			ret = ops->post_bank_switch(bus);
813*4882a593Smuzhiyun 			if (ret < 0) {
814*4882a593Smuzhiyun 				dev_err(bus->dev,
815*4882a593Smuzhiyun 					"Post bank switch op failed: %d\n",
816*4882a593Smuzhiyun 					ret);
817*4882a593Smuzhiyun 				goto error;
818*4882a593Smuzhiyun 			}
819*4882a593Smuzhiyun 		} else if (multi_link) {
820*4882a593Smuzhiyun 			dev_err(bus->dev,
821*4882a593Smuzhiyun 				"Post bank switch ops not implemented\n");
822*4882a593Smuzhiyun 			goto error;
823*4882a593Smuzhiyun 		}
824*4882a593Smuzhiyun 
825*4882a593Smuzhiyun 		/* Set the bank switch timeout to default, if not set */
826*4882a593Smuzhiyun 		if (!bus->bank_switch_timeout)
827*4882a593Smuzhiyun 			bus->bank_switch_timeout = DEFAULT_BANK_SWITCH_TIMEOUT;
828*4882a593Smuzhiyun 
829*4882a593Smuzhiyun 		/* Check if bank switch was successful */
830*4882a593Smuzhiyun 		ret = sdw_ml_sync_bank_switch(bus);
831*4882a593Smuzhiyun 		if (ret < 0) {
832*4882a593Smuzhiyun 			dev_err(bus->dev,
833*4882a593Smuzhiyun 				"multi link bank switch failed: %d\n", ret);
834*4882a593Smuzhiyun 			goto error;
835*4882a593Smuzhiyun 		}
836*4882a593Smuzhiyun 
837*4882a593Smuzhiyun 		if (multi_link)
838*4882a593Smuzhiyun 			mutex_unlock(&bus->msg_lock);
839*4882a593Smuzhiyun 	}
840*4882a593Smuzhiyun 
841*4882a593Smuzhiyun 	return ret;
842*4882a593Smuzhiyun 
843*4882a593Smuzhiyun error:
844*4882a593Smuzhiyun 	list_for_each_entry(m_rt, &stream->master_list, stream_node) {
845*4882a593Smuzhiyun 		bus = m_rt->bus;
846*4882a593Smuzhiyun 		if (bus->defer_msg.msg) {
847*4882a593Smuzhiyun 			kfree(bus->defer_msg.msg->buf);
848*4882a593Smuzhiyun 			kfree(bus->defer_msg.msg);
849*4882a593Smuzhiyun 		}
850*4882a593Smuzhiyun 	}
851*4882a593Smuzhiyun 
852*4882a593Smuzhiyun msg_unlock:
853*4882a593Smuzhiyun 
854*4882a593Smuzhiyun 	if (multi_link) {
855*4882a593Smuzhiyun 		list_for_each_entry(m_rt, &stream->master_list, stream_node) {
856*4882a593Smuzhiyun 			bus = m_rt->bus;
857*4882a593Smuzhiyun 			if (mutex_is_locked(&bus->msg_lock))
858*4882a593Smuzhiyun 				mutex_unlock(&bus->msg_lock);
859*4882a593Smuzhiyun 		}
860*4882a593Smuzhiyun 	}
861*4882a593Smuzhiyun 
862*4882a593Smuzhiyun 	return ret;
863*4882a593Smuzhiyun }
864*4882a593Smuzhiyun 
865*4882a593Smuzhiyun /**
866*4882a593Smuzhiyun  * sdw_release_stream() - Free the assigned stream runtime
867*4882a593Smuzhiyun  *
868*4882a593Smuzhiyun  * @stream: SoundWire stream runtime
869*4882a593Smuzhiyun  *
870*4882a593Smuzhiyun  * sdw_release_stream should be called only once per stream
871*4882a593Smuzhiyun  */
sdw_release_stream(struct sdw_stream_runtime * stream)872*4882a593Smuzhiyun void sdw_release_stream(struct sdw_stream_runtime *stream)
873*4882a593Smuzhiyun {
874*4882a593Smuzhiyun 	kfree(stream);
875*4882a593Smuzhiyun }
876*4882a593Smuzhiyun EXPORT_SYMBOL(sdw_release_stream);
877*4882a593Smuzhiyun 
878*4882a593Smuzhiyun /**
879*4882a593Smuzhiyun  * sdw_alloc_stream() - Allocate and return stream runtime
880*4882a593Smuzhiyun  *
881*4882a593Smuzhiyun  * @stream_name: SoundWire stream name
882*4882a593Smuzhiyun  *
883*4882a593Smuzhiyun  * Allocates a SoundWire stream runtime instance.
884*4882a593Smuzhiyun  * sdw_alloc_stream should be called only once per stream. Typically
885*4882a593Smuzhiyun  * invoked from ALSA/ASoC machine/platform driver.
886*4882a593Smuzhiyun  */
sdw_alloc_stream(const char * stream_name)887*4882a593Smuzhiyun struct sdw_stream_runtime *sdw_alloc_stream(const char *stream_name)
888*4882a593Smuzhiyun {
889*4882a593Smuzhiyun 	struct sdw_stream_runtime *stream;
890*4882a593Smuzhiyun 
891*4882a593Smuzhiyun 	stream = kzalloc(sizeof(*stream), GFP_KERNEL);
892*4882a593Smuzhiyun 	if (!stream)
893*4882a593Smuzhiyun 		return NULL;
894*4882a593Smuzhiyun 
895*4882a593Smuzhiyun 	stream->name = stream_name;
896*4882a593Smuzhiyun 	INIT_LIST_HEAD(&stream->master_list);
897*4882a593Smuzhiyun 	stream->state = SDW_STREAM_ALLOCATED;
898*4882a593Smuzhiyun 	stream->m_rt_count = 0;
899*4882a593Smuzhiyun 
900*4882a593Smuzhiyun 	return stream;
901*4882a593Smuzhiyun }
902*4882a593Smuzhiyun EXPORT_SYMBOL(sdw_alloc_stream);
903*4882a593Smuzhiyun 
904*4882a593Smuzhiyun static struct sdw_master_runtime
sdw_find_master_rt(struct sdw_bus * bus,struct sdw_stream_runtime * stream)905*4882a593Smuzhiyun *sdw_find_master_rt(struct sdw_bus *bus,
906*4882a593Smuzhiyun 		    struct sdw_stream_runtime *stream)
907*4882a593Smuzhiyun {
908*4882a593Smuzhiyun 	struct sdw_master_runtime *m_rt;
909*4882a593Smuzhiyun 
910*4882a593Smuzhiyun 	/* Retrieve Bus handle if already available */
911*4882a593Smuzhiyun 	list_for_each_entry(m_rt, &stream->master_list, stream_node) {
912*4882a593Smuzhiyun 		if (m_rt->bus == bus)
913*4882a593Smuzhiyun 			return m_rt;
914*4882a593Smuzhiyun 	}
915*4882a593Smuzhiyun 
916*4882a593Smuzhiyun 	return NULL;
917*4882a593Smuzhiyun }
918*4882a593Smuzhiyun 
919*4882a593Smuzhiyun /**
920*4882a593Smuzhiyun  * sdw_alloc_master_rt() - Allocates and initialize Master runtime handle
921*4882a593Smuzhiyun  *
922*4882a593Smuzhiyun  * @bus: SDW bus instance
923*4882a593Smuzhiyun  * @stream_config: Stream configuration
924*4882a593Smuzhiyun  * @stream: Stream runtime handle.
925*4882a593Smuzhiyun  *
926*4882a593Smuzhiyun  * This function is to be called with bus_lock held.
927*4882a593Smuzhiyun  */
928*4882a593Smuzhiyun static struct sdw_master_runtime
sdw_alloc_master_rt(struct sdw_bus * bus,struct sdw_stream_config * stream_config,struct sdw_stream_runtime * stream)929*4882a593Smuzhiyun *sdw_alloc_master_rt(struct sdw_bus *bus,
930*4882a593Smuzhiyun 		     struct sdw_stream_config *stream_config,
931*4882a593Smuzhiyun 		     struct sdw_stream_runtime *stream)
932*4882a593Smuzhiyun {
933*4882a593Smuzhiyun 	struct sdw_master_runtime *m_rt;
934*4882a593Smuzhiyun 
935*4882a593Smuzhiyun 	/*
936*4882a593Smuzhiyun 	 * check if Master is already allocated (as a result of Slave adding
937*4882a593Smuzhiyun 	 * it first), if so skip allocation and go to configure
938*4882a593Smuzhiyun 	 */
939*4882a593Smuzhiyun 	m_rt = sdw_find_master_rt(bus, stream);
940*4882a593Smuzhiyun 	if (m_rt)
941*4882a593Smuzhiyun 		goto stream_config;
942*4882a593Smuzhiyun 
943*4882a593Smuzhiyun 	m_rt = kzalloc(sizeof(*m_rt), GFP_KERNEL);
944*4882a593Smuzhiyun 	if (!m_rt)
945*4882a593Smuzhiyun 		return NULL;
946*4882a593Smuzhiyun 
947*4882a593Smuzhiyun 	/* Initialization of Master runtime handle */
948*4882a593Smuzhiyun 	INIT_LIST_HEAD(&m_rt->port_list);
949*4882a593Smuzhiyun 	INIT_LIST_HEAD(&m_rt->slave_rt_list);
950*4882a593Smuzhiyun 	list_add_tail(&m_rt->stream_node, &stream->master_list);
951*4882a593Smuzhiyun 
952*4882a593Smuzhiyun 	list_add_tail(&m_rt->bus_node, &bus->m_rt_list);
953*4882a593Smuzhiyun 
954*4882a593Smuzhiyun stream_config:
955*4882a593Smuzhiyun 	m_rt->ch_count = stream_config->ch_count;
956*4882a593Smuzhiyun 	m_rt->bus = bus;
957*4882a593Smuzhiyun 	m_rt->stream = stream;
958*4882a593Smuzhiyun 	m_rt->direction = stream_config->direction;
959*4882a593Smuzhiyun 
960*4882a593Smuzhiyun 	return m_rt;
961*4882a593Smuzhiyun }
962*4882a593Smuzhiyun 
963*4882a593Smuzhiyun /**
964*4882a593Smuzhiyun  * sdw_alloc_slave_rt() - Allocate and initialize Slave runtime handle.
965*4882a593Smuzhiyun  *
966*4882a593Smuzhiyun  * @slave: Slave handle
967*4882a593Smuzhiyun  * @stream_config: Stream configuration
968*4882a593Smuzhiyun  * @stream: Stream runtime handle
969*4882a593Smuzhiyun  *
970*4882a593Smuzhiyun  * This function is to be called with bus_lock held.
971*4882a593Smuzhiyun  */
972*4882a593Smuzhiyun static struct sdw_slave_runtime
sdw_alloc_slave_rt(struct sdw_slave * slave,struct sdw_stream_config * stream_config,struct sdw_stream_runtime * stream)973*4882a593Smuzhiyun *sdw_alloc_slave_rt(struct sdw_slave *slave,
974*4882a593Smuzhiyun 		    struct sdw_stream_config *stream_config,
975*4882a593Smuzhiyun 		    struct sdw_stream_runtime *stream)
976*4882a593Smuzhiyun {
977*4882a593Smuzhiyun 	struct sdw_slave_runtime *s_rt;
978*4882a593Smuzhiyun 
979*4882a593Smuzhiyun 	s_rt = kzalloc(sizeof(*s_rt), GFP_KERNEL);
980*4882a593Smuzhiyun 	if (!s_rt)
981*4882a593Smuzhiyun 		return NULL;
982*4882a593Smuzhiyun 
983*4882a593Smuzhiyun 	INIT_LIST_HEAD(&s_rt->port_list);
984*4882a593Smuzhiyun 	s_rt->ch_count = stream_config->ch_count;
985*4882a593Smuzhiyun 	s_rt->direction = stream_config->direction;
986*4882a593Smuzhiyun 	s_rt->slave = slave;
987*4882a593Smuzhiyun 
988*4882a593Smuzhiyun 	return s_rt;
989*4882a593Smuzhiyun }
990*4882a593Smuzhiyun 
sdw_master_port_release(struct sdw_bus * bus,struct sdw_master_runtime * m_rt)991*4882a593Smuzhiyun static void sdw_master_port_release(struct sdw_bus *bus,
992*4882a593Smuzhiyun 				    struct sdw_master_runtime *m_rt)
993*4882a593Smuzhiyun {
994*4882a593Smuzhiyun 	struct sdw_port_runtime *p_rt, *_p_rt;
995*4882a593Smuzhiyun 
996*4882a593Smuzhiyun 	list_for_each_entry_safe(p_rt, _p_rt, &m_rt->port_list, port_node) {
997*4882a593Smuzhiyun 		list_del(&p_rt->port_node);
998*4882a593Smuzhiyun 		kfree(p_rt);
999*4882a593Smuzhiyun 	}
1000*4882a593Smuzhiyun }
1001*4882a593Smuzhiyun 
sdw_slave_port_release(struct sdw_bus * bus,struct sdw_slave * slave,struct sdw_stream_runtime * stream)1002*4882a593Smuzhiyun static void sdw_slave_port_release(struct sdw_bus *bus,
1003*4882a593Smuzhiyun 				   struct sdw_slave *slave,
1004*4882a593Smuzhiyun 				   struct sdw_stream_runtime *stream)
1005*4882a593Smuzhiyun {
1006*4882a593Smuzhiyun 	struct sdw_port_runtime *p_rt, *_p_rt;
1007*4882a593Smuzhiyun 	struct sdw_master_runtime *m_rt;
1008*4882a593Smuzhiyun 	struct sdw_slave_runtime *s_rt;
1009*4882a593Smuzhiyun 
1010*4882a593Smuzhiyun 	list_for_each_entry(m_rt, &stream->master_list, stream_node) {
1011*4882a593Smuzhiyun 		list_for_each_entry(s_rt, &m_rt->slave_rt_list, m_rt_node) {
1012*4882a593Smuzhiyun 			if (s_rt->slave != slave)
1013*4882a593Smuzhiyun 				continue;
1014*4882a593Smuzhiyun 
1015*4882a593Smuzhiyun 			list_for_each_entry_safe(p_rt, _p_rt,
1016*4882a593Smuzhiyun 						 &s_rt->port_list, port_node) {
1017*4882a593Smuzhiyun 				list_del(&p_rt->port_node);
1018*4882a593Smuzhiyun 				kfree(p_rt);
1019*4882a593Smuzhiyun 			}
1020*4882a593Smuzhiyun 		}
1021*4882a593Smuzhiyun 	}
1022*4882a593Smuzhiyun }
1023*4882a593Smuzhiyun 
1024*4882a593Smuzhiyun /**
1025*4882a593Smuzhiyun  * sdw_release_slave_stream() - Free Slave(s) runtime handle
1026*4882a593Smuzhiyun  *
1027*4882a593Smuzhiyun  * @slave: Slave handle.
1028*4882a593Smuzhiyun  * @stream: Stream runtime handle.
1029*4882a593Smuzhiyun  *
1030*4882a593Smuzhiyun  * This function is to be called with bus_lock held.
1031*4882a593Smuzhiyun  */
sdw_release_slave_stream(struct sdw_slave * slave,struct sdw_stream_runtime * stream)1032*4882a593Smuzhiyun static void sdw_release_slave_stream(struct sdw_slave *slave,
1033*4882a593Smuzhiyun 				     struct sdw_stream_runtime *stream)
1034*4882a593Smuzhiyun {
1035*4882a593Smuzhiyun 	struct sdw_slave_runtime *s_rt, *_s_rt;
1036*4882a593Smuzhiyun 	struct sdw_master_runtime *m_rt;
1037*4882a593Smuzhiyun 
1038*4882a593Smuzhiyun 	list_for_each_entry(m_rt, &stream->master_list, stream_node) {
1039*4882a593Smuzhiyun 		/* Retrieve Slave runtime handle */
1040*4882a593Smuzhiyun 		list_for_each_entry_safe(s_rt, _s_rt,
1041*4882a593Smuzhiyun 					 &m_rt->slave_rt_list, m_rt_node) {
1042*4882a593Smuzhiyun 			if (s_rt->slave == slave) {
1043*4882a593Smuzhiyun 				list_del(&s_rt->m_rt_node);
1044*4882a593Smuzhiyun 				kfree(s_rt);
1045*4882a593Smuzhiyun 				return;
1046*4882a593Smuzhiyun 			}
1047*4882a593Smuzhiyun 		}
1048*4882a593Smuzhiyun 	}
1049*4882a593Smuzhiyun }
1050*4882a593Smuzhiyun 
1051*4882a593Smuzhiyun /**
1052*4882a593Smuzhiyun  * sdw_release_master_stream() - Free Master runtime handle
1053*4882a593Smuzhiyun  *
1054*4882a593Smuzhiyun  * @m_rt: Master runtime node
1055*4882a593Smuzhiyun  * @stream: Stream runtime handle.
1056*4882a593Smuzhiyun  *
1057*4882a593Smuzhiyun  * This function is to be called with bus_lock held
1058*4882a593Smuzhiyun  * It frees the Master runtime handle and associated Slave(s) runtime
1059*4882a593Smuzhiyun  * handle. If this is called first then sdw_release_slave_stream() will have
1060*4882a593Smuzhiyun  * no effect as Slave(s) runtime handle would already be freed up.
1061*4882a593Smuzhiyun  */
sdw_release_master_stream(struct sdw_master_runtime * m_rt,struct sdw_stream_runtime * stream)1062*4882a593Smuzhiyun static void sdw_release_master_stream(struct sdw_master_runtime *m_rt,
1063*4882a593Smuzhiyun 				      struct sdw_stream_runtime *stream)
1064*4882a593Smuzhiyun {
1065*4882a593Smuzhiyun 	struct sdw_slave_runtime *s_rt, *_s_rt;
1066*4882a593Smuzhiyun 
1067*4882a593Smuzhiyun 	list_for_each_entry_safe(s_rt, _s_rt, &m_rt->slave_rt_list, m_rt_node) {
1068*4882a593Smuzhiyun 		sdw_slave_port_release(s_rt->slave->bus, s_rt->slave, stream);
1069*4882a593Smuzhiyun 		sdw_release_slave_stream(s_rt->slave, stream);
1070*4882a593Smuzhiyun 	}
1071*4882a593Smuzhiyun 
1072*4882a593Smuzhiyun 	list_del(&m_rt->stream_node);
1073*4882a593Smuzhiyun 	list_del(&m_rt->bus_node);
1074*4882a593Smuzhiyun 	kfree(m_rt);
1075*4882a593Smuzhiyun }
1076*4882a593Smuzhiyun 
1077*4882a593Smuzhiyun /**
1078*4882a593Smuzhiyun  * sdw_stream_remove_master() - Remove master from sdw_stream
1079*4882a593Smuzhiyun  *
1080*4882a593Smuzhiyun  * @bus: SDW Bus instance
1081*4882a593Smuzhiyun  * @stream: SoundWire stream
1082*4882a593Smuzhiyun  *
1083*4882a593Smuzhiyun  * This removes and frees port_rt and master_rt from a stream
1084*4882a593Smuzhiyun  */
sdw_stream_remove_master(struct sdw_bus * bus,struct sdw_stream_runtime * stream)1085*4882a593Smuzhiyun int sdw_stream_remove_master(struct sdw_bus *bus,
1086*4882a593Smuzhiyun 			     struct sdw_stream_runtime *stream)
1087*4882a593Smuzhiyun {
1088*4882a593Smuzhiyun 	struct sdw_master_runtime *m_rt, *_m_rt;
1089*4882a593Smuzhiyun 
1090*4882a593Smuzhiyun 	mutex_lock(&bus->bus_lock);
1091*4882a593Smuzhiyun 
1092*4882a593Smuzhiyun 	list_for_each_entry_safe(m_rt, _m_rt,
1093*4882a593Smuzhiyun 				 &stream->master_list, stream_node) {
1094*4882a593Smuzhiyun 		if (m_rt->bus != bus)
1095*4882a593Smuzhiyun 			continue;
1096*4882a593Smuzhiyun 
1097*4882a593Smuzhiyun 		sdw_master_port_release(bus, m_rt);
1098*4882a593Smuzhiyun 		sdw_release_master_stream(m_rt, stream);
1099*4882a593Smuzhiyun 		stream->m_rt_count--;
1100*4882a593Smuzhiyun 	}
1101*4882a593Smuzhiyun 
1102*4882a593Smuzhiyun 	if (list_empty(&stream->master_list))
1103*4882a593Smuzhiyun 		stream->state = SDW_STREAM_RELEASED;
1104*4882a593Smuzhiyun 
1105*4882a593Smuzhiyun 	mutex_unlock(&bus->bus_lock);
1106*4882a593Smuzhiyun 
1107*4882a593Smuzhiyun 	return 0;
1108*4882a593Smuzhiyun }
1109*4882a593Smuzhiyun EXPORT_SYMBOL(sdw_stream_remove_master);
1110*4882a593Smuzhiyun 
1111*4882a593Smuzhiyun /**
1112*4882a593Smuzhiyun  * sdw_stream_remove_slave() - Remove slave from sdw_stream
1113*4882a593Smuzhiyun  *
1114*4882a593Smuzhiyun  * @slave: SDW Slave instance
1115*4882a593Smuzhiyun  * @stream: SoundWire stream
1116*4882a593Smuzhiyun  *
1117*4882a593Smuzhiyun  * This removes and frees port_rt and slave_rt from a stream
1118*4882a593Smuzhiyun  */
sdw_stream_remove_slave(struct sdw_slave * slave,struct sdw_stream_runtime * stream)1119*4882a593Smuzhiyun int sdw_stream_remove_slave(struct sdw_slave *slave,
1120*4882a593Smuzhiyun 			    struct sdw_stream_runtime *stream)
1121*4882a593Smuzhiyun {
1122*4882a593Smuzhiyun 	mutex_lock(&slave->bus->bus_lock);
1123*4882a593Smuzhiyun 
1124*4882a593Smuzhiyun 	sdw_slave_port_release(slave->bus, slave, stream);
1125*4882a593Smuzhiyun 	sdw_release_slave_stream(slave, stream);
1126*4882a593Smuzhiyun 
1127*4882a593Smuzhiyun 	mutex_unlock(&slave->bus->bus_lock);
1128*4882a593Smuzhiyun 
1129*4882a593Smuzhiyun 	return 0;
1130*4882a593Smuzhiyun }
1131*4882a593Smuzhiyun EXPORT_SYMBOL(sdw_stream_remove_slave);
1132*4882a593Smuzhiyun 
1133*4882a593Smuzhiyun /**
1134*4882a593Smuzhiyun  * sdw_config_stream() - Configure the allocated stream
1135*4882a593Smuzhiyun  *
1136*4882a593Smuzhiyun  * @dev: SDW device
1137*4882a593Smuzhiyun  * @stream: SoundWire stream
1138*4882a593Smuzhiyun  * @stream_config: Stream configuration for audio stream
1139*4882a593Smuzhiyun  * @is_slave: is API called from Slave or Master
1140*4882a593Smuzhiyun  *
1141*4882a593Smuzhiyun  * This function is to be called with bus_lock held.
1142*4882a593Smuzhiyun  */
sdw_config_stream(struct device * dev,struct sdw_stream_runtime * stream,struct sdw_stream_config * stream_config,bool is_slave)1143*4882a593Smuzhiyun static int sdw_config_stream(struct device *dev,
1144*4882a593Smuzhiyun 			     struct sdw_stream_runtime *stream,
1145*4882a593Smuzhiyun 			     struct sdw_stream_config *stream_config,
1146*4882a593Smuzhiyun 			     bool is_slave)
1147*4882a593Smuzhiyun {
1148*4882a593Smuzhiyun 	/*
1149*4882a593Smuzhiyun 	 * Update the stream rate, channel and bps based on data
1150*4882a593Smuzhiyun 	 * source. For more than one data source (multilink),
1151*4882a593Smuzhiyun 	 * match the rate, bps, stream type and increment number of channels.
1152*4882a593Smuzhiyun 	 *
1153*4882a593Smuzhiyun 	 * If rate/bps is zero, it means the values are not set, so skip
1154*4882a593Smuzhiyun 	 * comparison and allow the value to be set and stored in stream
1155*4882a593Smuzhiyun 	 */
1156*4882a593Smuzhiyun 	if (stream->params.rate &&
1157*4882a593Smuzhiyun 	    stream->params.rate != stream_config->frame_rate) {
1158*4882a593Smuzhiyun 		dev_err(dev, "rate not matching, stream:%s\n", stream->name);
1159*4882a593Smuzhiyun 		return -EINVAL;
1160*4882a593Smuzhiyun 	}
1161*4882a593Smuzhiyun 
1162*4882a593Smuzhiyun 	if (stream->params.bps &&
1163*4882a593Smuzhiyun 	    stream->params.bps != stream_config->bps) {
1164*4882a593Smuzhiyun 		dev_err(dev, "bps not matching, stream:%s\n", stream->name);
1165*4882a593Smuzhiyun 		return -EINVAL;
1166*4882a593Smuzhiyun 	}
1167*4882a593Smuzhiyun 
1168*4882a593Smuzhiyun 	stream->type = stream_config->type;
1169*4882a593Smuzhiyun 	stream->params.rate = stream_config->frame_rate;
1170*4882a593Smuzhiyun 	stream->params.bps = stream_config->bps;
1171*4882a593Smuzhiyun 
1172*4882a593Smuzhiyun 	/* TODO: Update this check during Device-device support */
1173*4882a593Smuzhiyun 	if (is_slave)
1174*4882a593Smuzhiyun 		stream->params.ch_count += stream_config->ch_count;
1175*4882a593Smuzhiyun 
1176*4882a593Smuzhiyun 	return 0;
1177*4882a593Smuzhiyun }
1178*4882a593Smuzhiyun 
sdw_is_valid_port_range(struct device * dev,struct sdw_port_runtime * p_rt)1179*4882a593Smuzhiyun static int sdw_is_valid_port_range(struct device *dev,
1180*4882a593Smuzhiyun 				   struct sdw_port_runtime *p_rt)
1181*4882a593Smuzhiyun {
1182*4882a593Smuzhiyun 	if (!SDW_VALID_PORT_RANGE(p_rt->num)) {
1183*4882a593Smuzhiyun 		dev_err(dev,
1184*4882a593Smuzhiyun 			"SoundWire: Invalid port number :%d\n", p_rt->num);
1185*4882a593Smuzhiyun 		return -EINVAL;
1186*4882a593Smuzhiyun 	}
1187*4882a593Smuzhiyun 
1188*4882a593Smuzhiyun 	return 0;
1189*4882a593Smuzhiyun }
1190*4882a593Smuzhiyun 
1191*4882a593Smuzhiyun static struct sdw_port_runtime
sdw_port_alloc(struct device * dev,struct sdw_port_config * port_config,int port_index)1192*4882a593Smuzhiyun *sdw_port_alloc(struct device *dev,
1193*4882a593Smuzhiyun 		struct sdw_port_config *port_config,
1194*4882a593Smuzhiyun 		int port_index)
1195*4882a593Smuzhiyun {
1196*4882a593Smuzhiyun 	struct sdw_port_runtime *p_rt;
1197*4882a593Smuzhiyun 
1198*4882a593Smuzhiyun 	p_rt = kzalloc(sizeof(*p_rt), GFP_KERNEL);
1199*4882a593Smuzhiyun 	if (!p_rt)
1200*4882a593Smuzhiyun 		return NULL;
1201*4882a593Smuzhiyun 
1202*4882a593Smuzhiyun 	p_rt->ch_mask = port_config[port_index].ch_mask;
1203*4882a593Smuzhiyun 	p_rt->num = port_config[port_index].num;
1204*4882a593Smuzhiyun 
1205*4882a593Smuzhiyun 	return p_rt;
1206*4882a593Smuzhiyun }
1207*4882a593Smuzhiyun 
sdw_master_port_config(struct sdw_bus * bus,struct sdw_master_runtime * m_rt,struct sdw_port_config * port_config,unsigned int num_ports)1208*4882a593Smuzhiyun static int sdw_master_port_config(struct sdw_bus *bus,
1209*4882a593Smuzhiyun 				  struct sdw_master_runtime *m_rt,
1210*4882a593Smuzhiyun 				  struct sdw_port_config *port_config,
1211*4882a593Smuzhiyun 				  unsigned int num_ports)
1212*4882a593Smuzhiyun {
1213*4882a593Smuzhiyun 	struct sdw_port_runtime *p_rt;
1214*4882a593Smuzhiyun 	int i;
1215*4882a593Smuzhiyun 
1216*4882a593Smuzhiyun 	/* Iterate for number of ports to perform initialization */
1217*4882a593Smuzhiyun 	for (i = 0; i < num_ports; i++) {
1218*4882a593Smuzhiyun 		p_rt = sdw_port_alloc(bus->dev, port_config, i);
1219*4882a593Smuzhiyun 		if (!p_rt)
1220*4882a593Smuzhiyun 			return -ENOMEM;
1221*4882a593Smuzhiyun 
1222*4882a593Smuzhiyun 		/*
1223*4882a593Smuzhiyun 		 * TODO: Check port capabilities for requested
1224*4882a593Smuzhiyun 		 * configuration (audio mode support)
1225*4882a593Smuzhiyun 		 */
1226*4882a593Smuzhiyun 
1227*4882a593Smuzhiyun 		list_add_tail(&p_rt->port_node, &m_rt->port_list);
1228*4882a593Smuzhiyun 	}
1229*4882a593Smuzhiyun 
1230*4882a593Smuzhiyun 	return 0;
1231*4882a593Smuzhiyun }
1232*4882a593Smuzhiyun 
sdw_slave_port_config(struct sdw_slave * slave,struct sdw_slave_runtime * s_rt,struct sdw_port_config * port_config,unsigned int num_config)1233*4882a593Smuzhiyun static int sdw_slave_port_config(struct sdw_slave *slave,
1234*4882a593Smuzhiyun 				 struct sdw_slave_runtime *s_rt,
1235*4882a593Smuzhiyun 				 struct sdw_port_config *port_config,
1236*4882a593Smuzhiyun 				 unsigned int num_config)
1237*4882a593Smuzhiyun {
1238*4882a593Smuzhiyun 	struct sdw_port_runtime *p_rt;
1239*4882a593Smuzhiyun 	int i, ret;
1240*4882a593Smuzhiyun 
1241*4882a593Smuzhiyun 	/* Iterate for number of ports to perform initialization */
1242*4882a593Smuzhiyun 	for (i = 0; i < num_config; i++) {
1243*4882a593Smuzhiyun 		p_rt = sdw_port_alloc(&slave->dev, port_config, i);
1244*4882a593Smuzhiyun 		if (!p_rt)
1245*4882a593Smuzhiyun 			return -ENOMEM;
1246*4882a593Smuzhiyun 
1247*4882a593Smuzhiyun 		/*
1248*4882a593Smuzhiyun 		 * TODO: Check valid port range as defined by DisCo/
1249*4882a593Smuzhiyun 		 * slave
1250*4882a593Smuzhiyun 		 */
1251*4882a593Smuzhiyun 		ret = sdw_is_valid_port_range(&slave->dev, p_rt);
1252*4882a593Smuzhiyun 		if (ret < 0) {
1253*4882a593Smuzhiyun 			kfree(p_rt);
1254*4882a593Smuzhiyun 			return ret;
1255*4882a593Smuzhiyun 		}
1256*4882a593Smuzhiyun 
1257*4882a593Smuzhiyun 		/*
1258*4882a593Smuzhiyun 		 * TODO: Check port capabilities for requested
1259*4882a593Smuzhiyun 		 * configuration (audio mode support)
1260*4882a593Smuzhiyun 		 */
1261*4882a593Smuzhiyun 
1262*4882a593Smuzhiyun 		list_add_tail(&p_rt->port_node, &s_rt->port_list);
1263*4882a593Smuzhiyun 	}
1264*4882a593Smuzhiyun 
1265*4882a593Smuzhiyun 	return 0;
1266*4882a593Smuzhiyun }
1267*4882a593Smuzhiyun 
1268*4882a593Smuzhiyun /**
1269*4882a593Smuzhiyun  * sdw_stream_add_master() - Allocate and add master runtime to a stream
1270*4882a593Smuzhiyun  *
1271*4882a593Smuzhiyun  * @bus: SDW Bus instance
1272*4882a593Smuzhiyun  * @stream_config: Stream configuration for audio stream
1273*4882a593Smuzhiyun  * @port_config: Port configuration for audio stream
1274*4882a593Smuzhiyun  * @num_ports: Number of ports
1275*4882a593Smuzhiyun  * @stream: SoundWire stream
1276*4882a593Smuzhiyun  */
sdw_stream_add_master(struct sdw_bus * bus,struct sdw_stream_config * stream_config,struct sdw_port_config * port_config,unsigned int num_ports,struct sdw_stream_runtime * stream)1277*4882a593Smuzhiyun int sdw_stream_add_master(struct sdw_bus *bus,
1278*4882a593Smuzhiyun 			  struct sdw_stream_config *stream_config,
1279*4882a593Smuzhiyun 			  struct sdw_port_config *port_config,
1280*4882a593Smuzhiyun 			  unsigned int num_ports,
1281*4882a593Smuzhiyun 			  struct sdw_stream_runtime *stream)
1282*4882a593Smuzhiyun {
1283*4882a593Smuzhiyun 	struct sdw_master_runtime *m_rt;
1284*4882a593Smuzhiyun 	int ret;
1285*4882a593Smuzhiyun 
1286*4882a593Smuzhiyun 	mutex_lock(&bus->bus_lock);
1287*4882a593Smuzhiyun 
1288*4882a593Smuzhiyun 	/*
1289*4882a593Smuzhiyun 	 * For multi link streams, add the second master only if
1290*4882a593Smuzhiyun 	 * the bus supports it.
1291*4882a593Smuzhiyun 	 * Check if bus->multi_link is set
1292*4882a593Smuzhiyun 	 */
1293*4882a593Smuzhiyun 	if (!bus->multi_link && stream->m_rt_count > 0) {
1294*4882a593Smuzhiyun 		dev_err(bus->dev,
1295*4882a593Smuzhiyun 			"Multilink not supported, link %d\n", bus->link_id);
1296*4882a593Smuzhiyun 		ret = -EINVAL;
1297*4882a593Smuzhiyun 		goto unlock;
1298*4882a593Smuzhiyun 	}
1299*4882a593Smuzhiyun 
1300*4882a593Smuzhiyun 	m_rt = sdw_alloc_master_rt(bus, stream_config, stream);
1301*4882a593Smuzhiyun 	if (!m_rt) {
1302*4882a593Smuzhiyun 		dev_err(bus->dev,
1303*4882a593Smuzhiyun 			"Master runtime config failed for stream:%s\n",
1304*4882a593Smuzhiyun 			stream->name);
1305*4882a593Smuzhiyun 		ret = -ENOMEM;
1306*4882a593Smuzhiyun 		goto unlock;
1307*4882a593Smuzhiyun 	}
1308*4882a593Smuzhiyun 
1309*4882a593Smuzhiyun 	ret = sdw_config_stream(bus->dev, stream, stream_config, false);
1310*4882a593Smuzhiyun 	if (ret)
1311*4882a593Smuzhiyun 		goto stream_error;
1312*4882a593Smuzhiyun 
1313*4882a593Smuzhiyun 	ret = sdw_master_port_config(bus, m_rt, port_config, num_ports);
1314*4882a593Smuzhiyun 	if (ret)
1315*4882a593Smuzhiyun 		goto stream_error;
1316*4882a593Smuzhiyun 
1317*4882a593Smuzhiyun 	stream->m_rt_count++;
1318*4882a593Smuzhiyun 
1319*4882a593Smuzhiyun 	goto unlock;
1320*4882a593Smuzhiyun 
1321*4882a593Smuzhiyun stream_error:
1322*4882a593Smuzhiyun 	sdw_release_master_stream(m_rt, stream);
1323*4882a593Smuzhiyun unlock:
1324*4882a593Smuzhiyun 	mutex_unlock(&bus->bus_lock);
1325*4882a593Smuzhiyun 	return ret;
1326*4882a593Smuzhiyun }
1327*4882a593Smuzhiyun EXPORT_SYMBOL(sdw_stream_add_master);
1328*4882a593Smuzhiyun 
1329*4882a593Smuzhiyun /**
1330*4882a593Smuzhiyun  * sdw_stream_add_slave() - Allocate and add master/slave runtime to a stream
1331*4882a593Smuzhiyun  *
1332*4882a593Smuzhiyun  * @slave: SDW Slave instance
1333*4882a593Smuzhiyun  * @stream_config: Stream configuration for audio stream
1334*4882a593Smuzhiyun  * @stream: SoundWire stream
1335*4882a593Smuzhiyun  * @port_config: Port configuration for audio stream
1336*4882a593Smuzhiyun  * @num_ports: Number of ports
1337*4882a593Smuzhiyun  *
1338*4882a593Smuzhiyun  * It is expected that Slave is added before adding Master
1339*4882a593Smuzhiyun  * to the Stream.
1340*4882a593Smuzhiyun  *
1341*4882a593Smuzhiyun  */
sdw_stream_add_slave(struct sdw_slave * slave,struct sdw_stream_config * stream_config,struct sdw_port_config * port_config,unsigned int num_ports,struct sdw_stream_runtime * stream)1342*4882a593Smuzhiyun int sdw_stream_add_slave(struct sdw_slave *slave,
1343*4882a593Smuzhiyun 			 struct sdw_stream_config *stream_config,
1344*4882a593Smuzhiyun 			 struct sdw_port_config *port_config,
1345*4882a593Smuzhiyun 			 unsigned int num_ports,
1346*4882a593Smuzhiyun 			 struct sdw_stream_runtime *stream)
1347*4882a593Smuzhiyun {
1348*4882a593Smuzhiyun 	struct sdw_slave_runtime *s_rt;
1349*4882a593Smuzhiyun 	struct sdw_master_runtime *m_rt;
1350*4882a593Smuzhiyun 	int ret;
1351*4882a593Smuzhiyun 
1352*4882a593Smuzhiyun 	mutex_lock(&slave->bus->bus_lock);
1353*4882a593Smuzhiyun 
1354*4882a593Smuzhiyun 	/*
1355*4882a593Smuzhiyun 	 * If this API is invoked by Slave first then m_rt is not valid.
1356*4882a593Smuzhiyun 	 * So, allocate m_rt and add Slave to it.
1357*4882a593Smuzhiyun 	 */
1358*4882a593Smuzhiyun 	m_rt = sdw_alloc_master_rt(slave->bus, stream_config, stream);
1359*4882a593Smuzhiyun 	if (!m_rt) {
1360*4882a593Smuzhiyun 		dev_err(&slave->dev,
1361*4882a593Smuzhiyun 			"alloc master runtime failed for stream:%s\n",
1362*4882a593Smuzhiyun 			stream->name);
1363*4882a593Smuzhiyun 		ret = -ENOMEM;
1364*4882a593Smuzhiyun 		goto error;
1365*4882a593Smuzhiyun 	}
1366*4882a593Smuzhiyun 
1367*4882a593Smuzhiyun 	s_rt = sdw_alloc_slave_rt(slave, stream_config, stream);
1368*4882a593Smuzhiyun 	if (!s_rt) {
1369*4882a593Smuzhiyun 		dev_err(&slave->dev,
1370*4882a593Smuzhiyun 			"Slave runtime config failed for stream:%s\n",
1371*4882a593Smuzhiyun 			stream->name);
1372*4882a593Smuzhiyun 		ret = -ENOMEM;
1373*4882a593Smuzhiyun 		goto stream_error;
1374*4882a593Smuzhiyun 	}
1375*4882a593Smuzhiyun 
1376*4882a593Smuzhiyun 	ret = sdw_config_stream(&slave->dev, stream, stream_config, true);
1377*4882a593Smuzhiyun 	if (ret) {
1378*4882a593Smuzhiyun 		/*
1379*4882a593Smuzhiyun 		 * sdw_release_master_stream will release s_rt in slave_rt_list in
1380*4882a593Smuzhiyun 		 * stream_error case, but s_rt is only added to slave_rt_list
1381*4882a593Smuzhiyun 		 * when sdw_config_stream is successful, so free s_rt explicitly
1382*4882a593Smuzhiyun 		 * when sdw_config_stream is failed.
1383*4882a593Smuzhiyun 		 */
1384*4882a593Smuzhiyun 		kfree(s_rt);
1385*4882a593Smuzhiyun 		goto stream_error;
1386*4882a593Smuzhiyun 	}
1387*4882a593Smuzhiyun 
1388*4882a593Smuzhiyun 	list_add_tail(&s_rt->m_rt_node, &m_rt->slave_rt_list);
1389*4882a593Smuzhiyun 
1390*4882a593Smuzhiyun 	ret = sdw_slave_port_config(slave, s_rt, port_config, num_ports);
1391*4882a593Smuzhiyun 	if (ret)
1392*4882a593Smuzhiyun 		goto stream_error;
1393*4882a593Smuzhiyun 
1394*4882a593Smuzhiyun 	/*
1395*4882a593Smuzhiyun 	 * Change stream state to CONFIGURED on first Slave add.
1396*4882a593Smuzhiyun 	 * Bus is not aware of number of Slave(s) in a stream at this
1397*4882a593Smuzhiyun 	 * point so cannot depend on all Slave(s) to be added in order to
1398*4882a593Smuzhiyun 	 * change stream state to CONFIGURED.
1399*4882a593Smuzhiyun 	 */
1400*4882a593Smuzhiyun 	stream->state = SDW_STREAM_CONFIGURED;
1401*4882a593Smuzhiyun 	goto error;
1402*4882a593Smuzhiyun 
1403*4882a593Smuzhiyun stream_error:
1404*4882a593Smuzhiyun 	/*
1405*4882a593Smuzhiyun 	 * we hit error so cleanup the stream, release all Slave(s) and
1406*4882a593Smuzhiyun 	 * Master runtime
1407*4882a593Smuzhiyun 	 */
1408*4882a593Smuzhiyun 	sdw_release_master_stream(m_rt, stream);
1409*4882a593Smuzhiyun error:
1410*4882a593Smuzhiyun 	mutex_unlock(&slave->bus->bus_lock);
1411*4882a593Smuzhiyun 	return ret;
1412*4882a593Smuzhiyun }
1413*4882a593Smuzhiyun EXPORT_SYMBOL(sdw_stream_add_slave);
1414*4882a593Smuzhiyun 
1415*4882a593Smuzhiyun /**
1416*4882a593Smuzhiyun  * sdw_get_slave_dpn_prop() - Get Slave port capabilities
1417*4882a593Smuzhiyun  *
1418*4882a593Smuzhiyun  * @slave: Slave handle
1419*4882a593Smuzhiyun  * @direction: Data direction.
1420*4882a593Smuzhiyun  * @port_num: Port number
1421*4882a593Smuzhiyun  */
sdw_get_slave_dpn_prop(struct sdw_slave * slave,enum sdw_data_direction direction,unsigned int port_num)1422*4882a593Smuzhiyun struct sdw_dpn_prop *sdw_get_slave_dpn_prop(struct sdw_slave *slave,
1423*4882a593Smuzhiyun 					    enum sdw_data_direction direction,
1424*4882a593Smuzhiyun 					    unsigned int port_num)
1425*4882a593Smuzhiyun {
1426*4882a593Smuzhiyun 	struct sdw_dpn_prop *dpn_prop;
1427*4882a593Smuzhiyun 	u8 num_ports;
1428*4882a593Smuzhiyun 	int i;
1429*4882a593Smuzhiyun 
1430*4882a593Smuzhiyun 	if (direction == SDW_DATA_DIR_TX) {
1431*4882a593Smuzhiyun 		num_ports = hweight32(slave->prop.source_ports);
1432*4882a593Smuzhiyun 		dpn_prop = slave->prop.src_dpn_prop;
1433*4882a593Smuzhiyun 	} else {
1434*4882a593Smuzhiyun 		num_ports = hweight32(slave->prop.sink_ports);
1435*4882a593Smuzhiyun 		dpn_prop = slave->prop.sink_dpn_prop;
1436*4882a593Smuzhiyun 	}
1437*4882a593Smuzhiyun 
1438*4882a593Smuzhiyun 	for (i = 0; i < num_ports; i++) {
1439*4882a593Smuzhiyun 		if (dpn_prop[i].num == port_num)
1440*4882a593Smuzhiyun 			return &dpn_prop[i];
1441*4882a593Smuzhiyun 	}
1442*4882a593Smuzhiyun 
1443*4882a593Smuzhiyun 	return NULL;
1444*4882a593Smuzhiyun }
1445*4882a593Smuzhiyun 
1446*4882a593Smuzhiyun /**
1447*4882a593Smuzhiyun  * sdw_acquire_bus_lock: Acquire bus lock for all Master runtime(s)
1448*4882a593Smuzhiyun  *
1449*4882a593Smuzhiyun  * @stream: SoundWire stream
1450*4882a593Smuzhiyun  *
1451*4882a593Smuzhiyun  * Acquire bus_lock for each of the master runtime(m_rt) part of this
1452*4882a593Smuzhiyun  * stream to reconfigure the bus.
1453*4882a593Smuzhiyun  * NOTE: This function is called from SoundWire stream ops and is
1454*4882a593Smuzhiyun  * expected that a global lock is held before acquiring bus_lock.
1455*4882a593Smuzhiyun  */
sdw_acquire_bus_lock(struct sdw_stream_runtime * stream)1456*4882a593Smuzhiyun static void sdw_acquire_bus_lock(struct sdw_stream_runtime *stream)
1457*4882a593Smuzhiyun {
1458*4882a593Smuzhiyun 	struct sdw_master_runtime *m_rt;
1459*4882a593Smuzhiyun 	struct sdw_bus *bus = NULL;
1460*4882a593Smuzhiyun 
1461*4882a593Smuzhiyun 	/* Iterate for all Master(s) in Master list */
1462*4882a593Smuzhiyun 	list_for_each_entry(m_rt, &stream->master_list, stream_node) {
1463*4882a593Smuzhiyun 		bus = m_rt->bus;
1464*4882a593Smuzhiyun 
1465*4882a593Smuzhiyun 		mutex_lock(&bus->bus_lock);
1466*4882a593Smuzhiyun 	}
1467*4882a593Smuzhiyun }
1468*4882a593Smuzhiyun 
1469*4882a593Smuzhiyun /**
1470*4882a593Smuzhiyun  * sdw_release_bus_lock: Release bus lock for all Master runtime(s)
1471*4882a593Smuzhiyun  *
1472*4882a593Smuzhiyun  * @stream: SoundWire stream
1473*4882a593Smuzhiyun  *
1474*4882a593Smuzhiyun  * Release the previously held bus_lock after reconfiguring the bus.
1475*4882a593Smuzhiyun  * NOTE: This function is called from SoundWire stream ops and is
1476*4882a593Smuzhiyun  * expected that a global lock is held before releasing bus_lock.
1477*4882a593Smuzhiyun  */
sdw_release_bus_lock(struct sdw_stream_runtime * stream)1478*4882a593Smuzhiyun static void sdw_release_bus_lock(struct sdw_stream_runtime *stream)
1479*4882a593Smuzhiyun {
1480*4882a593Smuzhiyun 	struct sdw_master_runtime *m_rt = NULL;
1481*4882a593Smuzhiyun 	struct sdw_bus *bus = NULL;
1482*4882a593Smuzhiyun 
1483*4882a593Smuzhiyun 	/* Iterate for all Master(s) in Master list */
1484*4882a593Smuzhiyun 	list_for_each_entry_reverse(m_rt, &stream->master_list, stream_node) {
1485*4882a593Smuzhiyun 		bus = m_rt->bus;
1486*4882a593Smuzhiyun 		mutex_unlock(&bus->bus_lock);
1487*4882a593Smuzhiyun 	}
1488*4882a593Smuzhiyun }
1489*4882a593Smuzhiyun 
_sdw_prepare_stream(struct sdw_stream_runtime * stream,bool update_params)1490*4882a593Smuzhiyun static int _sdw_prepare_stream(struct sdw_stream_runtime *stream,
1491*4882a593Smuzhiyun 			       bool update_params)
1492*4882a593Smuzhiyun {
1493*4882a593Smuzhiyun 	struct sdw_master_runtime *m_rt;
1494*4882a593Smuzhiyun 	struct sdw_bus *bus = NULL;
1495*4882a593Smuzhiyun 	struct sdw_master_prop *prop;
1496*4882a593Smuzhiyun 	struct sdw_bus_params params;
1497*4882a593Smuzhiyun 	int ret;
1498*4882a593Smuzhiyun 
1499*4882a593Smuzhiyun 	/* Prepare  Master(s) and Slave(s) port(s) associated with stream */
1500*4882a593Smuzhiyun 	list_for_each_entry(m_rt, &stream->master_list, stream_node) {
1501*4882a593Smuzhiyun 		bus = m_rt->bus;
1502*4882a593Smuzhiyun 		prop = &bus->prop;
1503*4882a593Smuzhiyun 		memcpy(&params, &bus->params, sizeof(params));
1504*4882a593Smuzhiyun 
1505*4882a593Smuzhiyun 		/* TODO: Support Asynchronous mode */
1506*4882a593Smuzhiyun 		if ((prop->max_clk_freq % stream->params.rate) != 0) {
1507*4882a593Smuzhiyun 			dev_err(bus->dev, "Async mode not supported\n");
1508*4882a593Smuzhiyun 			return -EINVAL;
1509*4882a593Smuzhiyun 		}
1510*4882a593Smuzhiyun 
1511*4882a593Smuzhiyun 		if (!update_params)
1512*4882a593Smuzhiyun 			goto program_params;
1513*4882a593Smuzhiyun 
1514*4882a593Smuzhiyun 		/* Increment cumulative bus bandwidth */
1515*4882a593Smuzhiyun 		/* TODO: Update this during Device-Device support */
1516*4882a593Smuzhiyun 		bus->params.bandwidth += m_rt->stream->params.rate *
1517*4882a593Smuzhiyun 			m_rt->ch_count * m_rt->stream->params.bps;
1518*4882a593Smuzhiyun 
1519*4882a593Smuzhiyun 		/* Compute params */
1520*4882a593Smuzhiyun 		if (bus->compute_params) {
1521*4882a593Smuzhiyun 			ret = bus->compute_params(bus);
1522*4882a593Smuzhiyun 			if (ret < 0) {
1523*4882a593Smuzhiyun 				dev_err(bus->dev, "Compute params failed: %d",
1524*4882a593Smuzhiyun 					ret);
1525*4882a593Smuzhiyun 				return ret;
1526*4882a593Smuzhiyun 			}
1527*4882a593Smuzhiyun 		}
1528*4882a593Smuzhiyun 
1529*4882a593Smuzhiyun program_params:
1530*4882a593Smuzhiyun 		/* Program params */
1531*4882a593Smuzhiyun 		ret = sdw_program_params(bus, true);
1532*4882a593Smuzhiyun 		if (ret < 0) {
1533*4882a593Smuzhiyun 			dev_err(bus->dev, "Program params failed: %d\n", ret);
1534*4882a593Smuzhiyun 			goto restore_params;
1535*4882a593Smuzhiyun 		}
1536*4882a593Smuzhiyun 	}
1537*4882a593Smuzhiyun 
1538*4882a593Smuzhiyun 	if (!bus) {
1539*4882a593Smuzhiyun 		pr_err("Configuration error in %s\n", __func__);
1540*4882a593Smuzhiyun 		return -EINVAL;
1541*4882a593Smuzhiyun 	}
1542*4882a593Smuzhiyun 
1543*4882a593Smuzhiyun 	ret = do_bank_switch(stream);
1544*4882a593Smuzhiyun 	if (ret < 0) {
1545*4882a593Smuzhiyun 		dev_err(bus->dev, "Bank switch failed: %d\n", ret);
1546*4882a593Smuzhiyun 		goto restore_params;
1547*4882a593Smuzhiyun 	}
1548*4882a593Smuzhiyun 
1549*4882a593Smuzhiyun 	list_for_each_entry(m_rt, &stream->master_list, stream_node) {
1550*4882a593Smuzhiyun 		bus = m_rt->bus;
1551*4882a593Smuzhiyun 
1552*4882a593Smuzhiyun 		/* Prepare port(s) on the new clock configuration */
1553*4882a593Smuzhiyun 		ret = sdw_prep_deprep_ports(m_rt, true);
1554*4882a593Smuzhiyun 		if (ret < 0) {
1555*4882a593Smuzhiyun 			dev_err(bus->dev, "Prepare port(s) failed ret = %d\n",
1556*4882a593Smuzhiyun 				ret);
1557*4882a593Smuzhiyun 			return ret;
1558*4882a593Smuzhiyun 		}
1559*4882a593Smuzhiyun 	}
1560*4882a593Smuzhiyun 
1561*4882a593Smuzhiyun 	stream->state = SDW_STREAM_PREPARED;
1562*4882a593Smuzhiyun 
1563*4882a593Smuzhiyun 	return ret;
1564*4882a593Smuzhiyun 
1565*4882a593Smuzhiyun restore_params:
1566*4882a593Smuzhiyun 	memcpy(&bus->params, &params, sizeof(params));
1567*4882a593Smuzhiyun 	return ret;
1568*4882a593Smuzhiyun }
1569*4882a593Smuzhiyun 
1570*4882a593Smuzhiyun /**
1571*4882a593Smuzhiyun  * sdw_prepare_stream() - Prepare SoundWire stream
1572*4882a593Smuzhiyun  *
1573*4882a593Smuzhiyun  * @stream: Soundwire stream
1574*4882a593Smuzhiyun  *
1575*4882a593Smuzhiyun  * Documentation/driver-api/soundwire/stream.rst explains this API in detail
1576*4882a593Smuzhiyun  */
sdw_prepare_stream(struct sdw_stream_runtime * stream)1577*4882a593Smuzhiyun int sdw_prepare_stream(struct sdw_stream_runtime *stream)
1578*4882a593Smuzhiyun {
1579*4882a593Smuzhiyun 	bool update_params = true;
1580*4882a593Smuzhiyun 	int ret;
1581*4882a593Smuzhiyun 
1582*4882a593Smuzhiyun 	if (!stream) {
1583*4882a593Smuzhiyun 		pr_err("SoundWire: Handle not found for stream\n");
1584*4882a593Smuzhiyun 		return -EINVAL;
1585*4882a593Smuzhiyun 	}
1586*4882a593Smuzhiyun 
1587*4882a593Smuzhiyun 	sdw_acquire_bus_lock(stream);
1588*4882a593Smuzhiyun 
1589*4882a593Smuzhiyun 	if (stream->state == SDW_STREAM_PREPARED) {
1590*4882a593Smuzhiyun 		ret = 0;
1591*4882a593Smuzhiyun 		goto state_err;
1592*4882a593Smuzhiyun 	}
1593*4882a593Smuzhiyun 
1594*4882a593Smuzhiyun 	if (stream->state != SDW_STREAM_CONFIGURED &&
1595*4882a593Smuzhiyun 	    stream->state != SDW_STREAM_DEPREPARED &&
1596*4882a593Smuzhiyun 	    stream->state != SDW_STREAM_DISABLED) {
1597*4882a593Smuzhiyun 		pr_err("%s: %s: inconsistent state state %d\n",
1598*4882a593Smuzhiyun 		       __func__, stream->name, stream->state);
1599*4882a593Smuzhiyun 		ret = -EINVAL;
1600*4882a593Smuzhiyun 		goto state_err;
1601*4882a593Smuzhiyun 	}
1602*4882a593Smuzhiyun 
1603*4882a593Smuzhiyun 	/*
1604*4882a593Smuzhiyun 	 * when the stream is DISABLED, this means sdw_prepare_stream()
1605*4882a593Smuzhiyun 	 * is called as a result of an underflow or a resume operation.
1606*4882a593Smuzhiyun 	 * In this case, the bus parameters shall not be recomputed, but
1607*4882a593Smuzhiyun 	 * still need to be re-applied
1608*4882a593Smuzhiyun 	 */
1609*4882a593Smuzhiyun 	if (stream->state == SDW_STREAM_DISABLED)
1610*4882a593Smuzhiyun 		update_params = false;
1611*4882a593Smuzhiyun 
1612*4882a593Smuzhiyun 	ret = _sdw_prepare_stream(stream, update_params);
1613*4882a593Smuzhiyun 
1614*4882a593Smuzhiyun state_err:
1615*4882a593Smuzhiyun 	sdw_release_bus_lock(stream);
1616*4882a593Smuzhiyun 	return ret;
1617*4882a593Smuzhiyun }
1618*4882a593Smuzhiyun EXPORT_SYMBOL(sdw_prepare_stream);
1619*4882a593Smuzhiyun 
_sdw_enable_stream(struct sdw_stream_runtime * stream)1620*4882a593Smuzhiyun static int _sdw_enable_stream(struct sdw_stream_runtime *stream)
1621*4882a593Smuzhiyun {
1622*4882a593Smuzhiyun 	struct sdw_master_runtime *m_rt;
1623*4882a593Smuzhiyun 	struct sdw_bus *bus = NULL;
1624*4882a593Smuzhiyun 	int ret;
1625*4882a593Smuzhiyun 
1626*4882a593Smuzhiyun 	/* Enable Master(s) and Slave(s) port(s) associated with stream */
1627*4882a593Smuzhiyun 	list_for_each_entry(m_rt, &stream->master_list, stream_node) {
1628*4882a593Smuzhiyun 		bus = m_rt->bus;
1629*4882a593Smuzhiyun 
1630*4882a593Smuzhiyun 		/* Program params */
1631*4882a593Smuzhiyun 		ret = sdw_program_params(bus, false);
1632*4882a593Smuzhiyun 		if (ret < 0) {
1633*4882a593Smuzhiyun 			dev_err(bus->dev, "Program params failed: %d\n", ret);
1634*4882a593Smuzhiyun 			return ret;
1635*4882a593Smuzhiyun 		}
1636*4882a593Smuzhiyun 
1637*4882a593Smuzhiyun 		/* Enable port(s) */
1638*4882a593Smuzhiyun 		ret = sdw_enable_disable_ports(m_rt, true);
1639*4882a593Smuzhiyun 		if (ret < 0) {
1640*4882a593Smuzhiyun 			dev_err(bus->dev,
1641*4882a593Smuzhiyun 				"Enable port(s) failed ret: %d\n", ret);
1642*4882a593Smuzhiyun 			return ret;
1643*4882a593Smuzhiyun 		}
1644*4882a593Smuzhiyun 	}
1645*4882a593Smuzhiyun 
1646*4882a593Smuzhiyun 	if (!bus) {
1647*4882a593Smuzhiyun 		pr_err("Configuration error in %s\n", __func__);
1648*4882a593Smuzhiyun 		return -EINVAL;
1649*4882a593Smuzhiyun 	}
1650*4882a593Smuzhiyun 
1651*4882a593Smuzhiyun 	ret = do_bank_switch(stream);
1652*4882a593Smuzhiyun 	if (ret < 0) {
1653*4882a593Smuzhiyun 		dev_err(bus->dev, "Bank switch failed: %d\n", ret);
1654*4882a593Smuzhiyun 		return ret;
1655*4882a593Smuzhiyun 	}
1656*4882a593Smuzhiyun 
1657*4882a593Smuzhiyun 	stream->state = SDW_STREAM_ENABLED;
1658*4882a593Smuzhiyun 	return 0;
1659*4882a593Smuzhiyun }
1660*4882a593Smuzhiyun 
1661*4882a593Smuzhiyun /**
1662*4882a593Smuzhiyun  * sdw_enable_stream() - Enable SoundWire stream
1663*4882a593Smuzhiyun  *
1664*4882a593Smuzhiyun  * @stream: Soundwire stream
1665*4882a593Smuzhiyun  *
1666*4882a593Smuzhiyun  * Documentation/driver-api/soundwire/stream.rst explains this API in detail
1667*4882a593Smuzhiyun  */
sdw_enable_stream(struct sdw_stream_runtime * stream)1668*4882a593Smuzhiyun int sdw_enable_stream(struct sdw_stream_runtime *stream)
1669*4882a593Smuzhiyun {
1670*4882a593Smuzhiyun 	int ret;
1671*4882a593Smuzhiyun 
1672*4882a593Smuzhiyun 	if (!stream) {
1673*4882a593Smuzhiyun 		pr_err("SoundWire: Handle not found for stream\n");
1674*4882a593Smuzhiyun 		return -EINVAL;
1675*4882a593Smuzhiyun 	}
1676*4882a593Smuzhiyun 
1677*4882a593Smuzhiyun 	sdw_acquire_bus_lock(stream);
1678*4882a593Smuzhiyun 
1679*4882a593Smuzhiyun 	if (stream->state != SDW_STREAM_PREPARED &&
1680*4882a593Smuzhiyun 	    stream->state != SDW_STREAM_DISABLED) {
1681*4882a593Smuzhiyun 		pr_err("%s: %s: inconsistent state state %d\n",
1682*4882a593Smuzhiyun 		       __func__, stream->name, stream->state);
1683*4882a593Smuzhiyun 		ret = -EINVAL;
1684*4882a593Smuzhiyun 		goto state_err;
1685*4882a593Smuzhiyun 	}
1686*4882a593Smuzhiyun 
1687*4882a593Smuzhiyun 	ret = _sdw_enable_stream(stream);
1688*4882a593Smuzhiyun 
1689*4882a593Smuzhiyun state_err:
1690*4882a593Smuzhiyun 	sdw_release_bus_lock(stream);
1691*4882a593Smuzhiyun 	return ret;
1692*4882a593Smuzhiyun }
1693*4882a593Smuzhiyun EXPORT_SYMBOL(sdw_enable_stream);
1694*4882a593Smuzhiyun 
_sdw_disable_stream(struct sdw_stream_runtime * stream)1695*4882a593Smuzhiyun static int _sdw_disable_stream(struct sdw_stream_runtime *stream)
1696*4882a593Smuzhiyun {
1697*4882a593Smuzhiyun 	struct sdw_master_runtime *m_rt;
1698*4882a593Smuzhiyun 	int ret;
1699*4882a593Smuzhiyun 
1700*4882a593Smuzhiyun 	list_for_each_entry(m_rt, &stream->master_list, stream_node) {
1701*4882a593Smuzhiyun 		struct sdw_bus *bus = m_rt->bus;
1702*4882a593Smuzhiyun 
1703*4882a593Smuzhiyun 		/* Disable port(s) */
1704*4882a593Smuzhiyun 		ret = sdw_enable_disable_ports(m_rt, false);
1705*4882a593Smuzhiyun 		if (ret < 0) {
1706*4882a593Smuzhiyun 			dev_err(bus->dev, "Disable port(s) failed: %d\n", ret);
1707*4882a593Smuzhiyun 			return ret;
1708*4882a593Smuzhiyun 		}
1709*4882a593Smuzhiyun 	}
1710*4882a593Smuzhiyun 	stream->state = SDW_STREAM_DISABLED;
1711*4882a593Smuzhiyun 
1712*4882a593Smuzhiyun 	list_for_each_entry(m_rt, &stream->master_list, stream_node) {
1713*4882a593Smuzhiyun 		struct sdw_bus *bus = m_rt->bus;
1714*4882a593Smuzhiyun 
1715*4882a593Smuzhiyun 		/* Program params */
1716*4882a593Smuzhiyun 		ret = sdw_program_params(bus, false);
1717*4882a593Smuzhiyun 		if (ret < 0) {
1718*4882a593Smuzhiyun 			dev_err(bus->dev, "Program params failed: %d\n", ret);
1719*4882a593Smuzhiyun 			return ret;
1720*4882a593Smuzhiyun 		}
1721*4882a593Smuzhiyun 	}
1722*4882a593Smuzhiyun 
1723*4882a593Smuzhiyun 	ret = do_bank_switch(stream);
1724*4882a593Smuzhiyun 	if (ret < 0) {
1725*4882a593Smuzhiyun 		pr_err("Bank switch failed: %d\n", ret);
1726*4882a593Smuzhiyun 		return ret;
1727*4882a593Smuzhiyun 	}
1728*4882a593Smuzhiyun 
1729*4882a593Smuzhiyun 	/* make sure alternate bank (previous current) is also disabled */
1730*4882a593Smuzhiyun 	list_for_each_entry(m_rt, &stream->master_list, stream_node) {
1731*4882a593Smuzhiyun 		struct sdw_bus *bus = m_rt->bus;
1732*4882a593Smuzhiyun 
1733*4882a593Smuzhiyun 		/* Disable port(s) */
1734*4882a593Smuzhiyun 		ret = sdw_enable_disable_ports(m_rt, false);
1735*4882a593Smuzhiyun 		if (ret < 0) {
1736*4882a593Smuzhiyun 			dev_err(bus->dev, "Disable port(s) failed: %d\n", ret);
1737*4882a593Smuzhiyun 			return ret;
1738*4882a593Smuzhiyun 		}
1739*4882a593Smuzhiyun 	}
1740*4882a593Smuzhiyun 
1741*4882a593Smuzhiyun 	return 0;
1742*4882a593Smuzhiyun }
1743*4882a593Smuzhiyun 
1744*4882a593Smuzhiyun /**
1745*4882a593Smuzhiyun  * sdw_disable_stream() - Disable SoundWire stream
1746*4882a593Smuzhiyun  *
1747*4882a593Smuzhiyun  * @stream: Soundwire stream
1748*4882a593Smuzhiyun  *
1749*4882a593Smuzhiyun  * Documentation/driver-api/soundwire/stream.rst explains this API in detail
1750*4882a593Smuzhiyun  */
sdw_disable_stream(struct sdw_stream_runtime * stream)1751*4882a593Smuzhiyun int sdw_disable_stream(struct sdw_stream_runtime *stream)
1752*4882a593Smuzhiyun {
1753*4882a593Smuzhiyun 	int ret;
1754*4882a593Smuzhiyun 
1755*4882a593Smuzhiyun 	if (!stream) {
1756*4882a593Smuzhiyun 		pr_err("SoundWire: Handle not found for stream\n");
1757*4882a593Smuzhiyun 		return -EINVAL;
1758*4882a593Smuzhiyun 	}
1759*4882a593Smuzhiyun 
1760*4882a593Smuzhiyun 	sdw_acquire_bus_lock(stream);
1761*4882a593Smuzhiyun 
1762*4882a593Smuzhiyun 	if (stream->state != SDW_STREAM_ENABLED) {
1763*4882a593Smuzhiyun 		pr_err("%s: %s: inconsistent state state %d\n",
1764*4882a593Smuzhiyun 		       __func__, stream->name, stream->state);
1765*4882a593Smuzhiyun 		ret = -EINVAL;
1766*4882a593Smuzhiyun 		goto state_err;
1767*4882a593Smuzhiyun 	}
1768*4882a593Smuzhiyun 
1769*4882a593Smuzhiyun 	ret = _sdw_disable_stream(stream);
1770*4882a593Smuzhiyun 
1771*4882a593Smuzhiyun state_err:
1772*4882a593Smuzhiyun 	sdw_release_bus_lock(stream);
1773*4882a593Smuzhiyun 	return ret;
1774*4882a593Smuzhiyun }
1775*4882a593Smuzhiyun EXPORT_SYMBOL(sdw_disable_stream);
1776*4882a593Smuzhiyun 
_sdw_deprepare_stream(struct sdw_stream_runtime * stream)1777*4882a593Smuzhiyun static int _sdw_deprepare_stream(struct sdw_stream_runtime *stream)
1778*4882a593Smuzhiyun {
1779*4882a593Smuzhiyun 	struct sdw_master_runtime *m_rt;
1780*4882a593Smuzhiyun 	struct sdw_bus *bus;
1781*4882a593Smuzhiyun 	int ret = 0;
1782*4882a593Smuzhiyun 
1783*4882a593Smuzhiyun 	list_for_each_entry(m_rt, &stream->master_list, stream_node) {
1784*4882a593Smuzhiyun 		bus = m_rt->bus;
1785*4882a593Smuzhiyun 		/* De-prepare port(s) */
1786*4882a593Smuzhiyun 		ret = sdw_prep_deprep_ports(m_rt, false);
1787*4882a593Smuzhiyun 		if (ret < 0) {
1788*4882a593Smuzhiyun 			dev_err(bus->dev,
1789*4882a593Smuzhiyun 				"De-prepare port(s) failed: %d\n", ret);
1790*4882a593Smuzhiyun 			return ret;
1791*4882a593Smuzhiyun 		}
1792*4882a593Smuzhiyun 
1793*4882a593Smuzhiyun 		/* TODO: Update this during Device-Device support */
1794*4882a593Smuzhiyun 		bus->params.bandwidth -= m_rt->stream->params.rate *
1795*4882a593Smuzhiyun 			m_rt->ch_count * m_rt->stream->params.bps;
1796*4882a593Smuzhiyun 
1797*4882a593Smuzhiyun 		/* Compute params */
1798*4882a593Smuzhiyun 		if (bus->compute_params) {
1799*4882a593Smuzhiyun 			ret = bus->compute_params(bus);
1800*4882a593Smuzhiyun 			if (ret < 0) {
1801*4882a593Smuzhiyun 				dev_err(bus->dev, "Compute params failed: %d",
1802*4882a593Smuzhiyun 					ret);
1803*4882a593Smuzhiyun 				return ret;
1804*4882a593Smuzhiyun 			}
1805*4882a593Smuzhiyun 		}
1806*4882a593Smuzhiyun 
1807*4882a593Smuzhiyun 		/* Program params */
1808*4882a593Smuzhiyun 		ret = sdw_program_params(bus, false);
1809*4882a593Smuzhiyun 		if (ret < 0) {
1810*4882a593Smuzhiyun 			dev_err(bus->dev, "Program params failed: %d\n", ret);
1811*4882a593Smuzhiyun 			return ret;
1812*4882a593Smuzhiyun 		}
1813*4882a593Smuzhiyun 	}
1814*4882a593Smuzhiyun 
1815*4882a593Smuzhiyun 	stream->state = SDW_STREAM_DEPREPARED;
1816*4882a593Smuzhiyun 	return do_bank_switch(stream);
1817*4882a593Smuzhiyun }
1818*4882a593Smuzhiyun 
1819*4882a593Smuzhiyun /**
1820*4882a593Smuzhiyun  * sdw_deprepare_stream() - Deprepare SoundWire stream
1821*4882a593Smuzhiyun  *
1822*4882a593Smuzhiyun  * @stream: Soundwire stream
1823*4882a593Smuzhiyun  *
1824*4882a593Smuzhiyun  * Documentation/driver-api/soundwire/stream.rst explains this API in detail
1825*4882a593Smuzhiyun  */
sdw_deprepare_stream(struct sdw_stream_runtime * stream)1826*4882a593Smuzhiyun int sdw_deprepare_stream(struct sdw_stream_runtime *stream)
1827*4882a593Smuzhiyun {
1828*4882a593Smuzhiyun 	int ret;
1829*4882a593Smuzhiyun 
1830*4882a593Smuzhiyun 	if (!stream) {
1831*4882a593Smuzhiyun 		pr_err("SoundWire: Handle not found for stream\n");
1832*4882a593Smuzhiyun 		return -EINVAL;
1833*4882a593Smuzhiyun 	}
1834*4882a593Smuzhiyun 
1835*4882a593Smuzhiyun 	sdw_acquire_bus_lock(stream);
1836*4882a593Smuzhiyun 
1837*4882a593Smuzhiyun 	if (stream->state != SDW_STREAM_PREPARED &&
1838*4882a593Smuzhiyun 	    stream->state != SDW_STREAM_DISABLED) {
1839*4882a593Smuzhiyun 		pr_err("%s: %s: inconsistent state state %d\n",
1840*4882a593Smuzhiyun 		       __func__, stream->name, stream->state);
1841*4882a593Smuzhiyun 		ret = -EINVAL;
1842*4882a593Smuzhiyun 		goto state_err;
1843*4882a593Smuzhiyun 	}
1844*4882a593Smuzhiyun 
1845*4882a593Smuzhiyun 	ret = _sdw_deprepare_stream(stream);
1846*4882a593Smuzhiyun 
1847*4882a593Smuzhiyun state_err:
1848*4882a593Smuzhiyun 	sdw_release_bus_lock(stream);
1849*4882a593Smuzhiyun 	return ret;
1850*4882a593Smuzhiyun }
1851*4882a593Smuzhiyun EXPORT_SYMBOL(sdw_deprepare_stream);
1852*4882a593Smuzhiyun 
set_stream(struct snd_pcm_substream * substream,struct sdw_stream_runtime * sdw_stream)1853*4882a593Smuzhiyun static int set_stream(struct snd_pcm_substream *substream,
1854*4882a593Smuzhiyun 		      struct sdw_stream_runtime *sdw_stream)
1855*4882a593Smuzhiyun {
1856*4882a593Smuzhiyun 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
1857*4882a593Smuzhiyun 	struct snd_soc_dai *dai;
1858*4882a593Smuzhiyun 	int ret = 0;
1859*4882a593Smuzhiyun 	int i;
1860*4882a593Smuzhiyun 
1861*4882a593Smuzhiyun 	/* Set stream pointer on all DAIs */
1862*4882a593Smuzhiyun 	for_each_rtd_dais(rtd, i, dai) {
1863*4882a593Smuzhiyun 		ret = snd_soc_dai_set_sdw_stream(dai, sdw_stream, substream->stream);
1864*4882a593Smuzhiyun 		if (ret < 0) {
1865*4882a593Smuzhiyun 			dev_err(rtd->dev, "failed to set stream pointer on dai %s", dai->name);
1866*4882a593Smuzhiyun 			break;
1867*4882a593Smuzhiyun 		}
1868*4882a593Smuzhiyun 	}
1869*4882a593Smuzhiyun 
1870*4882a593Smuzhiyun 	return ret;
1871*4882a593Smuzhiyun }
1872*4882a593Smuzhiyun 
1873*4882a593Smuzhiyun /**
1874*4882a593Smuzhiyun  * sdw_startup_stream() - Startup SoundWire stream
1875*4882a593Smuzhiyun  *
1876*4882a593Smuzhiyun  * @sdw_substream: Soundwire stream
1877*4882a593Smuzhiyun  *
1878*4882a593Smuzhiyun  * Documentation/driver-api/soundwire/stream.rst explains this API in detail
1879*4882a593Smuzhiyun  */
sdw_startup_stream(void * sdw_substream)1880*4882a593Smuzhiyun int sdw_startup_stream(void *sdw_substream)
1881*4882a593Smuzhiyun {
1882*4882a593Smuzhiyun 	struct snd_pcm_substream *substream = sdw_substream;
1883*4882a593Smuzhiyun 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
1884*4882a593Smuzhiyun 	struct sdw_stream_runtime *sdw_stream;
1885*4882a593Smuzhiyun 	char *name;
1886*4882a593Smuzhiyun 	int ret;
1887*4882a593Smuzhiyun 
1888*4882a593Smuzhiyun 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1889*4882a593Smuzhiyun 		name = kasprintf(GFP_KERNEL, "%s-Playback", substream->name);
1890*4882a593Smuzhiyun 	else
1891*4882a593Smuzhiyun 		name = kasprintf(GFP_KERNEL, "%s-Capture", substream->name);
1892*4882a593Smuzhiyun 
1893*4882a593Smuzhiyun 	if (!name)
1894*4882a593Smuzhiyun 		return -ENOMEM;
1895*4882a593Smuzhiyun 
1896*4882a593Smuzhiyun 	sdw_stream = sdw_alloc_stream(name);
1897*4882a593Smuzhiyun 	if (!sdw_stream) {
1898*4882a593Smuzhiyun 		dev_err(rtd->dev, "alloc stream failed for substream DAI %s", substream->name);
1899*4882a593Smuzhiyun 		ret = -ENOMEM;
1900*4882a593Smuzhiyun 		goto error;
1901*4882a593Smuzhiyun 	}
1902*4882a593Smuzhiyun 
1903*4882a593Smuzhiyun 	ret = set_stream(substream, sdw_stream);
1904*4882a593Smuzhiyun 	if (ret < 0)
1905*4882a593Smuzhiyun 		goto release_stream;
1906*4882a593Smuzhiyun 	return 0;
1907*4882a593Smuzhiyun 
1908*4882a593Smuzhiyun release_stream:
1909*4882a593Smuzhiyun 	sdw_release_stream(sdw_stream);
1910*4882a593Smuzhiyun 	set_stream(substream, NULL);
1911*4882a593Smuzhiyun error:
1912*4882a593Smuzhiyun 	kfree(name);
1913*4882a593Smuzhiyun 	return ret;
1914*4882a593Smuzhiyun }
1915*4882a593Smuzhiyun EXPORT_SYMBOL(sdw_startup_stream);
1916*4882a593Smuzhiyun 
1917*4882a593Smuzhiyun /**
1918*4882a593Smuzhiyun  * sdw_shutdown_stream() - Shutdown SoundWire stream
1919*4882a593Smuzhiyun  *
1920*4882a593Smuzhiyun  * @sdw_substream: Soundwire stream
1921*4882a593Smuzhiyun  *
1922*4882a593Smuzhiyun  * Documentation/driver-api/soundwire/stream.rst explains this API in detail
1923*4882a593Smuzhiyun  */
sdw_shutdown_stream(void * sdw_substream)1924*4882a593Smuzhiyun void sdw_shutdown_stream(void *sdw_substream)
1925*4882a593Smuzhiyun {
1926*4882a593Smuzhiyun 	struct snd_pcm_substream *substream = sdw_substream;
1927*4882a593Smuzhiyun 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
1928*4882a593Smuzhiyun 	struct sdw_stream_runtime *sdw_stream;
1929*4882a593Smuzhiyun 	struct snd_soc_dai *dai;
1930*4882a593Smuzhiyun 
1931*4882a593Smuzhiyun 	/* Find stream from first CPU DAI */
1932*4882a593Smuzhiyun 	dai = asoc_rtd_to_cpu(rtd, 0);
1933*4882a593Smuzhiyun 
1934*4882a593Smuzhiyun 	sdw_stream = snd_soc_dai_get_sdw_stream(dai, substream->stream);
1935*4882a593Smuzhiyun 
1936*4882a593Smuzhiyun 	if (IS_ERR(sdw_stream)) {
1937*4882a593Smuzhiyun 		dev_err(rtd->dev, "no stream found for DAI %s", dai->name);
1938*4882a593Smuzhiyun 		return;
1939*4882a593Smuzhiyun 	}
1940*4882a593Smuzhiyun 
1941*4882a593Smuzhiyun 	/* release memory */
1942*4882a593Smuzhiyun 	kfree(sdw_stream->name);
1943*4882a593Smuzhiyun 	sdw_release_stream(sdw_stream);
1944*4882a593Smuzhiyun 
1945*4882a593Smuzhiyun 	/* clear DAI data */
1946*4882a593Smuzhiyun 	set_stream(substream, NULL);
1947*4882a593Smuzhiyun }
1948*4882a593Smuzhiyun EXPORT_SYMBOL(sdw_shutdown_stream);
1949