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