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