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