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