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