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