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