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