1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Renesas R-Car SSIU/SSI support
4 //
5 // Copyright (C) 2013 Renesas Solutions Corp.
6 // Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
7 //
8 // Based on fsi.c
9 // Kuninori Morimoto <morimoto.kuninori@renesas.com>
10
11 /*
12 * you can enable below define if you don't need
13 * SSI interrupt status debug message when debugging
14 * see rsnd_dbg_irq_status()
15 *
16 * #define RSND_DEBUG_NO_IRQ_STATUS 1
17 */
18
19 #include <sound/simple_card_utils.h>
20 #include <linux/delay.h>
21 #include "rsnd.h"
22 #define RSND_SSI_NAME_SIZE 16
23
24 /*
25 * SSICR
26 */
27 #define FORCE (1 << 31) /* Fixed */
28 #define DMEN (1 << 28) /* DMA Enable */
29 #define UIEN (1 << 27) /* Underflow Interrupt Enable */
30 #define OIEN (1 << 26) /* Overflow Interrupt Enable */
31 #define IIEN (1 << 25) /* Idle Mode Interrupt Enable */
32 #define DIEN (1 << 24) /* Data Interrupt Enable */
33 #define CHNL_4 (1 << 22) /* Channels */
34 #define CHNL_6 (2 << 22) /* Channels */
35 #define CHNL_8 (3 << 22) /* Channels */
36 #define DWL_MASK (7 << 19) /* Data Word Length mask */
37 #define DWL_8 (0 << 19) /* Data Word Length */
38 #define DWL_16 (1 << 19) /* Data Word Length */
39 #define DWL_18 (2 << 19) /* Data Word Length */
40 #define DWL_20 (3 << 19) /* Data Word Length */
41 #define DWL_22 (4 << 19) /* Data Word Length */
42 #define DWL_24 (5 << 19) /* Data Word Length */
43 #define DWL_32 (6 << 19) /* Data Word Length */
44
45 #define SWL_32 (3 << 16) /* R/W System Word Length */
46 #define SCKD (1 << 15) /* Serial Bit Clock Direction */
47 #define SWSD (1 << 14) /* Serial WS Direction */
48 #define SCKP (1 << 13) /* Serial Bit Clock Polarity */
49 #define SWSP (1 << 12) /* Serial WS Polarity */
50 #define SDTA (1 << 10) /* Serial Data Alignment */
51 #define PDTA (1 << 9) /* Parallel Data Alignment */
52 #define DEL (1 << 8) /* Serial Data Delay */
53 #define CKDV(v) (v << 4) /* Serial Clock Division Ratio */
54 #define TRMD (1 << 1) /* Transmit/Receive Mode Select */
55 #define EN (1 << 0) /* SSI Module Enable */
56
57 /*
58 * SSISR
59 */
60 #define UIRQ (1 << 27) /* Underflow Error Interrupt Status */
61 #define OIRQ (1 << 26) /* Overflow Error Interrupt Status */
62 #define IIRQ (1 << 25) /* Idle Mode Interrupt Status */
63 #define DIRQ (1 << 24) /* Data Interrupt Status Flag */
64
65 /*
66 * SSIWSR
67 */
68 #define CONT (1 << 8) /* WS Continue Function */
69 #define WS_MODE (1 << 0) /* WS Mode */
70
71 #define SSI_NAME "ssi"
72
73 struct rsnd_ssi {
74 struct rsnd_mod mod;
75 struct rsnd_mod *dma;
76
77 u32 flags;
78 u32 cr_own;
79 u32 cr_clk;
80 u32 cr_mode;
81 u32 cr_en;
82 u32 wsr;
83 int chan;
84 int rate;
85 int irq;
86 unsigned int usrcnt;
87
88 /* for PIO */
89 int byte_pos;
90 int byte_per_period;
91 int next_period_byte;
92 };
93
94 /* flags */
95 #define RSND_SSI_CLK_PIN_SHARE (1 << 0)
96 #define RSND_SSI_NO_BUSIF (1 << 1) /* SSI+DMA without BUSIF */
97 #define RSND_SSI_HDMI0 (1 << 2) /* for HDMI0 */
98 #define RSND_SSI_HDMI1 (1 << 3) /* for HDMI1 */
99 #define RSND_SSI_PROBED (1 << 4)
100
101 #define for_each_rsnd_ssi(pos, priv, i) \
102 for (i = 0; \
103 (i < rsnd_ssi_nr(priv)) && \
104 ((pos) = ((struct rsnd_ssi *)(priv)->ssi + i)); \
105 i++)
106
107 #define rsnd_ssi_get(priv, id) ((struct rsnd_ssi *)(priv->ssi) + id)
108 #define rsnd_ssi_nr(priv) ((priv)->ssi_nr)
109 #define rsnd_mod_to_ssi(_mod) container_of((_mod), struct rsnd_ssi, mod)
110 #define rsnd_ssi_is_parent(ssi, io) ((ssi) == rsnd_io_to_mod_ssip(io))
111 #define rsnd_ssi_is_multi_slave(mod, io) \
112 (rsnd_ssi_multi_slaves(io) & (1 << rsnd_mod_id(mod)))
113 #define rsnd_ssi_is_run_mods(mod, io) \
114 (rsnd_ssi_run_mods(io) & (1 << rsnd_mod_id(mod)))
115 #define rsnd_ssi_can_output_clk(mod) (!__rsnd_ssi_is_pin_sharing(mod))
116
rsnd_ssi_hdmi_port(struct rsnd_dai_stream * io)117 int rsnd_ssi_hdmi_port(struct rsnd_dai_stream *io)
118 {
119 struct rsnd_mod *mod = rsnd_io_to_mod_ssi(io);
120 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
121
122 if (rsnd_flags_has(ssi, RSND_SSI_HDMI0))
123 return RSND_SSI_HDMI_PORT0;
124
125 if (rsnd_flags_has(ssi, RSND_SSI_HDMI1))
126 return RSND_SSI_HDMI_PORT1;
127
128 return 0;
129 }
130
rsnd_ssi_use_busif(struct rsnd_dai_stream * io)131 int rsnd_ssi_use_busif(struct rsnd_dai_stream *io)
132 {
133 struct rsnd_mod *mod = rsnd_io_to_mod_ssi(io);
134 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
135 int use_busif = 0;
136
137 if (!rsnd_ssi_is_dma_mode(mod))
138 return 0;
139
140 if (!(rsnd_flags_has(ssi, RSND_SSI_NO_BUSIF)))
141 use_busif = 1;
142 if (rsnd_io_to_mod_src(io))
143 use_busif = 1;
144
145 return use_busif;
146 }
147
rsnd_ssi_status_clear(struct rsnd_mod * mod)148 static void rsnd_ssi_status_clear(struct rsnd_mod *mod)
149 {
150 rsnd_mod_write(mod, SSISR, 0);
151 }
152
rsnd_ssi_status_get(struct rsnd_mod * mod)153 static u32 rsnd_ssi_status_get(struct rsnd_mod *mod)
154 {
155 return rsnd_mod_read(mod, SSISR);
156 }
157
rsnd_ssi_status_check(struct rsnd_mod * mod,u32 bit)158 static void rsnd_ssi_status_check(struct rsnd_mod *mod,
159 u32 bit)
160 {
161 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
162 struct device *dev = rsnd_priv_to_dev(priv);
163 u32 status;
164 int i;
165
166 for (i = 0; i < 1024; i++) {
167 status = rsnd_ssi_status_get(mod);
168 if (status & bit)
169 return;
170
171 udelay(5);
172 }
173
174 dev_warn(dev, "%s[%d] status check failed\n",
175 rsnd_mod_name(mod), rsnd_mod_id(mod));
176 }
177
rsnd_ssi_multi_slaves(struct rsnd_dai_stream * io)178 static u32 rsnd_ssi_multi_slaves(struct rsnd_dai_stream *io)
179 {
180 struct rsnd_mod *mod;
181 enum rsnd_mod_type types[] = {
182 RSND_MOD_SSIM1,
183 RSND_MOD_SSIM2,
184 RSND_MOD_SSIM3,
185 };
186 int i, mask;
187
188 mask = 0;
189 for (i = 0; i < ARRAY_SIZE(types); i++) {
190 mod = rsnd_io_to_mod(io, types[i]);
191 if (!mod)
192 continue;
193
194 mask |= 1 << rsnd_mod_id(mod);
195 }
196
197 return mask;
198 }
199
rsnd_ssi_run_mods(struct rsnd_dai_stream * io)200 static u32 rsnd_ssi_run_mods(struct rsnd_dai_stream *io)
201 {
202 struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io);
203 struct rsnd_mod *ssi_parent_mod = rsnd_io_to_mod_ssip(io);
204 u32 mods;
205
206 mods = rsnd_ssi_multi_slaves_runtime(io) |
207 1 << rsnd_mod_id(ssi_mod);
208
209 if (ssi_parent_mod)
210 mods |= 1 << rsnd_mod_id(ssi_parent_mod);
211
212 return mods;
213 }
214
rsnd_ssi_multi_slaves_runtime(struct rsnd_dai_stream * io)215 u32 rsnd_ssi_multi_slaves_runtime(struct rsnd_dai_stream *io)
216 {
217 if (rsnd_runtime_is_ssi_multi(io))
218 return rsnd_ssi_multi_slaves(io);
219
220 return 0;
221 }
222
rsnd_ssi_clk_query(struct rsnd_priv * priv,int param1,int param2,int * idx)223 unsigned int rsnd_ssi_clk_query(struct rsnd_priv *priv,
224 int param1, int param2, int *idx)
225 {
226 int ssi_clk_mul_table[] = {
227 1, 2, 4, 8, 16, 6, 12,
228 };
229 int j, ret;
230 unsigned int main_rate;
231
232 for (j = 0; j < ARRAY_SIZE(ssi_clk_mul_table); j++) {
233
234 /*
235 * It will set SSIWSR.CONT here, but SSICR.CKDV = 000
236 * with it is not allowed. (SSIWSR.WS_MODE with
237 * SSICR.CKDV = 000 is not allowed either).
238 * Skip it. See SSICR.CKDV
239 */
240 if (j == 0)
241 continue;
242
243 /*
244 * this driver is assuming that
245 * system word is 32bit x chan
246 * see rsnd_ssi_init()
247 */
248 main_rate = 32 * param1 * param2 * ssi_clk_mul_table[j];
249
250 ret = rsnd_adg_clk_query(priv, main_rate);
251 if (ret < 0)
252 continue;
253
254 if (idx)
255 *idx = j;
256
257 return main_rate;
258 }
259
260 return 0;
261 }
262
rsnd_ssi_master_clk_start(struct rsnd_mod * mod,struct rsnd_dai_stream * io)263 static int rsnd_ssi_master_clk_start(struct rsnd_mod *mod,
264 struct rsnd_dai_stream *io)
265 {
266 struct rsnd_priv *priv = rsnd_io_to_priv(io);
267 struct device *dev = rsnd_priv_to_dev(priv);
268 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
269 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
270 int chan = rsnd_runtime_channel_for_ssi(io);
271 int idx, ret;
272 unsigned int main_rate;
273 unsigned int rate = rsnd_io_is_play(io) ?
274 rsnd_src_get_out_rate(priv, io) :
275 rsnd_src_get_in_rate(priv, io);
276
277 if (!rsnd_rdai_is_clk_master(rdai))
278 return 0;
279
280 if (!rsnd_ssi_can_output_clk(mod))
281 return 0;
282
283 if (rsnd_ssi_is_multi_slave(mod, io))
284 return 0;
285
286 if (ssi->rate) {
287 if (ssi->rate != rate) {
288 dev_err(dev, "SSI parent/child should use same rate\n");
289 return -EINVAL;
290 }
291
292 return 0;
293 }
294
295 main_rate = rsnd_ssi_clk_query(priv, rate, chan, &idx);
296 if (!main_rate) {
297 dev_err(dev, "unsupported clock rate\n");
298 return -EIO;
299 }
300
301 ret = rsnd_adg_ssi_clk_try_start(mod, main_rate);
302 if (ret < 0)
303 return ret;
304
305 /*
306 * SSI clock will be output contiguously
307 * by below settings.
308 * This means, rsnd_ssi_master_clk_start()
309 * and rsnd_ssi_register_setup() are necessary
310 * for SSI parent
311 *
312 * SSICR : FORCE, SCKD, SWSD
313 * SSIWSR : CONT
314 */
315 ssi->cr_clk = FORCE | SWL_32 | SCKD | SWSD | CKDV(idx);
316 ssi->wsr = CONT;
317 ssi->rate = rate;
318
319 dev_dbg(dev, "%s[%d] outputs %u Hz\n",
320 rsnd_mod_name(mod),
321 rsnd_mod_id(mod), rate);
322
323 return 0;
324 }
325
rsnd_ssi_master_clk_stop(struct rsnd_mod * mod,struct rsnd_dai_stream * io)326 static void rsnd_ssi_master_clk_stop(struct rsnd_mod *mod,
327 struct rsnd_dai_stream *io)
328 {
329 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
330 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
331
332 if (!rsnd_rdai_is_clk_master(rdai))
333 return;
334
335 if (!rsnd_ssi_can_output_clk(mod))
336 return;
337
338 if (ssi->usrcnt > 1)
339 return;
340
341 ssi->cr_clk = 0;
342 ssi->rate = 0;
343
344 rsnd_adg_ssi_clk_stop(mod);
345 }
346
rsnd_ssi_config_init(struct rsnd_mod * mod,struct rsnd_dai_stream * io)347 static void rsnd_ssi_config_init(struct rsnd_mod *mod,
348 struct rsnd_dai_stream *io)
349 {
350 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
351 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
352 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
353 u32 cr_own = ssi->cr_own;
354 u32 cr_mode = ssi->cr_mode;
355 u32 wsr = ssi->wsr;
356 int is_tdm;
357
358 is_tdm = rsnd_runtime_is_ssi_tdm(io);
359
360 /*
361 * always use 32bit system word.
362 * see also rsnd_ssi_master_clk_enable()
363 */
364 cr_own |= FORCE | SWL_32;
365
366 if (rdai->bit_clk_inv)
367 cr_own |= SCKP;
368 if (rdai->frm_clk_inv ^ is_tdm)
369 cr_own |= SWSP;
370 if (rdai->data_alignment)
371 cr_own |= SDTA;
372 if (rdai->sys_delay)
373 cr_own |= DEL;
374
375 /*
376 * We shouldn't exchange SWSP after running.
377 * This means, parent needs to care it.
378 */
379 if (rsnd_ssi_is_parent(mod, io))
380 goto init_end;
381
382 if (rsnd_io_is_play(io))
383 cr_own |= TRMD;
384
385 cr_own &= ~DWL_MASK;
386 switch (snd_pcm_format_width(runtime->format)) {
387 case 16:
388 cr_own |= DWL_16;
389 break;
390 case 24:
391 cr_own |= DWL_24;
392 break;
393 }
394
395 if (rsnd_ssi_is_dma_mode(mod)) {
396 cr_mode = UIEN | OIEN | /* over/under run */
397 DMEN; /* DMA : enable DMA */
398 } else {
399 cr_mode = DIEN; /* PIO : enable Data interrupt */
400 }
401
402 /*
403 * TDM Extend Mode
404 * see
405 * rsnd_ssiu_init_gen2()
406 */
407 wsr = ssi->wsr;
408 if (is_tdm) {
409 wsr |= WS_MODE;
410 cr_own |= CHNL_8;
411 }
412 init_end:
413 ssi->cr_own = cr_own;
414 ssi->cr_mode = cr_mode;
415 ssi->wsr = wsr;
416 }
417
rsnd_ssi_register_setup(struct rsnd_mod * mod)418 static void rsnd_ssi_register_setup(struct rsnd_mod *mod)
419 {
420 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
421
422 rsnd_mod_write(mod, SSIWSR, ssi->wsr);
423 rsnd_mod_write(mod, SSICR, ssi->cr_own |
424 ssi->cr_clk |
425 ssi->cr_mode |
426 ssi->cr_en);
427 }
428
429 /*
430 * SSI mod common functions
431 */
rsnd_ssi_init(struct rsnd_mod * mod,struct rsnd_dai_stream * io,struct rsnd_priv * priv)432 static int rsnd_ssi_init(struct rsnd_mod *mod,
433 struct rsnd_dai_stream *io,
434 struct rsnd_priv *priv)
435 {
436 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
437
438 if (!rsnd_ssi_is_run_mods(mod, io))
439 return 0;
440
441 ssi->usrcnt++;
442
443 rsnd_mod_power_on(mod);
444
445 rsnd_ssi_config_init(mod, io);
446
447 rsnd_ssi_register_setup(mod);
448
449 /* clear error status */
450 rsnd_ssi_status_clear(mod);
451
452 return 0;
453 }
454
rsnd_ssi_quit(struct rsnd_mod * mod,struct rsnd_dai_stream * io,struct rsnd_priv * priv)455 static int rsnd_ssi_quit(struct rsnd_mod *mod,
456 struct rsnd_dai_stream *io,
457 struct rsnd_priv *priv)
458 {
459 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
460 struct device *dev = rsnd_priv_to_dev(priv);
461
462 if (!rsnd_ssi_is_run_mods(mod, io))
463 return 0;
464
465 if (!ssi->usrcnt) {
466 dev_err(dev, "%s[%d] usrcnt error\n",
467 rsnd_mod_name(mod), rsnd_mod_id(mod));
468 return -EIO;
469 }
470
471 rsnd_ssi_master_clk_stop(mod, io);
472
473 rsnd_mod_power_off(mod);
474
475 ssi->usrcnt--;
476
477 if (!ssi->usrcnt) {
478 ssi->cr_own = 0;
479 ssi->cr_mode = 0;
480 ssi->wsr = 0;
481 }
482
483 return 0;
484 }
485
rsnd_ssi_hw_params(struct rsnd_mod * mod,struct rsnd_dai_stream * io,struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)486 static int rsnd_ssi_hw_params(struct rsnd_mod *mod,
487 struct rsnd_dai_stream *io,
488 struct snd_pcm_substream *substream,
489 struct snd_pcm_hw_params *params)
490 {
491 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
492 int chan = params_channels(params);
493
494 /*
495 * snd_pcm_ops::hw_params will be called *before*
496 * snd_soc_dai_ops::trigger. Thus, ssi->usrcnt is 0
497 * in 1st call.
498 */
499 if (ssi->usrcnt) {
500 /*
501 * Already working.
502 * It will happen if SSI has parent/child connection.
503 * it is error if child <-> parent SSI uses
504 * different channels.
505 */
506 if (ssi->chan != chan)
507 return -EIO;
508 }
509
510 ssi->chan = chan;
511
512 return 0;
513 }
514
rsnd_ssi_start(struct rsnd_mod * mod,struct rsnd_dai_stream * io,struct rsnd_priv * priv)515 static int rsnd_ssi_start(struct rsnd_mod *mod,
516 struct rsnd_dai_stream *io,
517 struct rsnd_priv *priv)
518 {
519 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
520
521 if (!rsnd_ssi_is_run_mods(mod, io))
522 return 0;
523
524 /*
525 * EN will be set via SSIU :: SSI_CONTROL
526 * if Multi channel mode
527 */
528 if (rsnd_ssi_multi_slaves_runtime(io))
529 return 0;
530
531 /*
532 * EN is for data output.
533 * SSI parent EN is not needed.
534 */
535 if (rsnd_ssi_is_parent(mod, io))
536 return 0;
537
538 ssi->cr_en = EN;
539
540 rsnd_mod_write(mod, SSICR, ssi->cr_own |
541 ssi->cr_clk |
542 ssi->cr_mode |
543 ssi->cr_en);
544
545 return 0;
546 }
547
rsnd_ssi_stop(struct rsnd_mod * mod,struct rsnd_dai_stream * io,struct rsnd_priv * priv)548 static int rsnd_ssi_stop(struct rsnd_mod *mod,
549 struct rsnd_dai_stream *io,
550 struct rsnd_priv *priv)
551 {
552 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
553 u32 cr;
554
555 if (!rsnd_ssi_is_run_mods(mod, io))
556 return 0;
557
558 if (rsnd_ssi_is_parent(mod, io))
559 return 0;
560
561 cr = ssi->cr_own |
562 ssi->cr_clk;
563
564 /*
565 * disable all IRQ,
566 * Playback: Wait all data was sent
567 * Capture: It might not receave data. Do nothing
568 */
569 if (rsnd_io_is_play(io)) {
570 rsnd_mod_write(mod, SSICR, cr | EN);
571 rsnd_ssi_status_check(mod, DIRQ);
572 }
573
574 /*
575 * disable SSI,
576 * and, wait idle state
577 */
578 rsnd_mod_write(mod, SSICR, cr); /* disabled all */
579 rsnd_ssi_status_check(mod, IIRQ);
580
581 ssi->cr_en = 0;
582
583 return 0;
584 }
585
rsnd_ssi_irq(struct rsnd_mod * mod,struct rsnd_dai_stream * io,struct rsnd_priv * priv,int enable)586 static int rsnd_ssi_irq(struct rsnd_mod *mod,
587 struct rsnd_dai_stream *io,
588 struct rsnd_priv *priv,
589 int enable)
590 {
591 u32 val = 0;
592
593 if (rsnd_is_gen1(priv))
594 return 0;
595
596 if (rsnd_ssi_is_parent(mod, io))
597 return 0;
598
599 if (!rsnd_ssi_is_run_mods(mod, io))
600 return 0;
601
602 if (enable)
603 val = rsnd_ssi_is_dma_mode(mod) ? 0x0e000000 : 0x0f000000;
604
605 rsnd_mod_write(mod, SSI_INT_ENABLE, val);
606
607 return 0;
608 }
609
610 static bool rsnd_ssi_pio_interrupt(struct rsnd_mod *mod,
611 struct rsnd_dai_stream *io);
__rsnd_ssi_interrupt(struct rsnd_mod * mod,struct rsnd_dai_stream * io)612 static void __rsnd_ssi_interrupt(struct rsnd_mod *mod,
613 struct rsnd_dai_stream *io)
614 {
615 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
616 struct device *dev = rsnd_priv_to_dev(priv);
617 int is_dma = rsnd_ssi_is_dma_mode(mod);
618 u32 status;
619 bool elapsed = false;
620 bool stop = false;
621
622 spin_lock(&priv->lock);
623
624 /* ignore all cases if not working */
625 if (!rsnd_io_is_working(io))
626 goto rsnd_ssi_interrupt_out;
627
628 status = rsnd_ssi_status_get(mod);
629
630 /* PIO only */
631 if (!is_dma && (status & DIRQ))
632 elapsed = rsnd_ssi_pio_interrupt(mod, io);
633
634 /* DMA only */
635 if (is_dma && (status & (UIRQ | OIRQ))) {
636 rsnd_dbg_irq_status(dev, "%s[%d] err status : 0x%08x\n",
637 rsnd_mod_name(mod), rsnd_mod_id(mod), status);
638
639 stop = true;
640 }
641
642 rsnd_ssi_status_clear(mod);
643 rsnd_ssi_interrupt_out:
644 spin_unlock(&priv->lock);
645
646 if (elapsed)
647 rsnd_dai_period_elapsed(io);
648
649 if (stop)
650 snd_pcm_stop_xrun(io->substream);
651
652 }
653
rsnd_ssi_interrupt(int irq,void * data)654 static irqreturn_t rsnd_ssi_interrupt(int irq, void *data)
655 {
656 struct rsnd_mod *mod = data;
657
658 rsnd_mod_interrupt(mod, __rsnd_ssi_interrupt);
659
660 return IRQ_HANDLED;
661 }
662
663 /*
664 * SSI PIO
665 */
rsnd_ssi_parent_attach(struct rsnd_mod * mod,struct rsnd_dai_stream * io)666 static void rsnd_ssi_parent_attach(struct rsnd_mod *mod,
667 struct rsnd_dai_stream *io)
668 {
669 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
670 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
671
672 if (!__rsnd_ssi_is_pin_sharing(mod))
673 return;
674
675 if (!rsnd_rdai_is_clk_master(rdai))
676 return;
677
678 switch (rsnd_mod_id(mod)) {
679 case 1:
680 case 2:
681 rsnd_dai_connect(rsnd_ssi_mod_get(priv, 0), io, RSND_MOD_SSIP);
682 break;
683 case 4:
684 rsnd_dai_connect(rsnd_ssi_mod_get(priv, 3), io, RSND_MOD_SSIP);
685 break;
686 case 8:
687 rsnd_dai_connect(rsnd_ssi_mod_get(priv, 7), io, RSND_MOD_SSIP);
688 break;
689 }
690 }
691
rsnd_ssi_pcm_new(struct rsnd_mod * mod,struct rsnd_dai_stream * io,struct snd_soc_pcm_runtime * rtd)692 static int rsnd_ssi_pcm_new(struct rsnd_mod *mod,
693 struct rsnd_dai_stream *io,
694 struct snd_soc_pcm_runtime *rtd)
695 {
696 /*
697 * rsnd_rdai_is_clk_master() will be enabled after set_fmt,
698 * and, pcm_new will be called after it.
699 * This function reuse pcm_new at this point.
700 */
701 rsnd_ssi_parent_attach(mod, io);
702
703 return 0;
704 }
705
rsnd_ssi_common_probe(struct rsnd_mod * mod,struct rsnd_dai_stream * io,struct rsnd_priv * priv)706 static int rsnd_ssi_common_probe(struct rsnd_mod *mod,
707 struct rsnd_dai_stream *io,
708 struct rsnd_priv *priv)
709 {
710 struct device *dev = rsnd_priv_to_dev(priv);
711 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
712 int ret;
713
714 /*
715 * SSIP/SSIU/IRQ are not needed on
716 * SSI Multi slaves
717 */
718 if (rsnd_ssi_is_multi_slave(mod, io))
719 return 0;
720
721 /*
722 * It can't judge ssi parent at this point
723 * see rsnd_ssi_pcm_new()
724 */
725
726 ret = rsnd_ssiu_attach(io, mod);
727 if (ret < 0)
728 return ret;
729
730 /*
731 * SSI might be called again as PIO fallback
732 * It is easy to manual handling for IRQ request/free
733 *
734 * OTOH, this function might be called many times if platform is
735 * using MIX. It needs xxx_attach() many times on xxx_probe().
736 * Because of it, we can't control .probe/.remove calling count by
737 * mod->status.
738 * But it don't need to call request_irq() many times.
739 * Let's control it by RSND_SSI_PROBED flag.
740 */
741 if (!rsnd_flags_has(ssi, RSND_SSI_PROBED)) {
742 ret = request_irq(ssi->irq,
743 rsnd_ssi_interrupt,
744 IRQF_SHARED,
745 dev_name(dev), mod);
746
747 rsnd_flags_set(ssi, RSND_SSI_PROBED);
748 }
749
750 return ret;
751 }
752
rsnd_ssi_common_remove(struct rsnd_mod * mod,struct rsnd_dai_stream * io,struct rsnd_priv * priv)753 static int rsnd_ssi_common_remove(struct rsnd_mod *mod,
754 struct rsnd_dai_stream *io,
755 struct rsnd_priv *priv)
756 {
757 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
758 struct rsnd_mod *pure_ssi_mod = rsnd_io_to_mod_ssi(io);
759
760 /* Do nothing if non SSI (= SSI parent, multi SSI) mod */
761 if (pure_ssi_mod != mod)
762 return 0;
763
764 /* PIO will request IRQ again */
765 if (rsnd_flags_has(ssi, RSND_SSI_PROBED)) {
766 free_irq(ssi->irq, mod);
767
768 rsnd_flags_del(ssi, RSND_SSI_PROBED);
769 }
770
771 return 0;
772 }
773
774 /*
775 * SSI PIO functions
776 */
rsnd_ssi_pio_interrupt(struct rsnd_mod * mod,struct rsnd_dai_stream * io)777 static bool rsnd_ssi_pio_interrupt(struct rsnd_mod *mod,
778 struct rsnd_dai_stream *io)
779 {
780 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
781 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
782 u32 *buf = (u32 *)(runtime->dma_area + ssi->byte_pos);
783 int shift = 0;
784 int byte_pos;
785 bool elapsed = false;
786
787 if (snd_pcm_format_width(runtime->format) == 24)
788 shift = 8;
789
790 /*
791 * 8/16/32 data can be assesse to TDR/RDR register
792 * directly as 32bit data
793 * see rsnd_ssi_init()
794 */
795 if (rsnd_io_is_play(io))
796 rsnd_mod_write(mod, SSITDR, (*buf) << shift);
797 else
798 *buf = (rsnd_mod_read(mod, SSIRDR) >> shift);
799
800 byte_pos = ssi->byte_pos + sizeof(*buf);
801
802 if (byte_pos >= ssi->next_period_byte) {
803 int period_pos = byte_pos / ssi->byte_per_period;
804
805 if (period_pos >= runtime->periods) {
806 byte_pos = 0;
807 period_pos = 0;
808 }
809
810 ssi->next_period_byte = (period_pos + 1) * ssi->byte_per_period;
811
812 elapsed = true;
813 }
814
815 WRITE_ONCE(ssi->byte_pos, byte_pos);
816
817 return elapsed;
818 }
819
rsnd_ssi_pio_init(struct rsnd_mod * mod,struct rsnd_dai_stream * io,struct rsnd_priv * priv)820 static int rsnd_ssi_pio_init(struct rsnd_mod *mod,
821 struct rsnd_dai_stream *io,
822 struct rsnd_priv *priv)
823 {
824 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
825 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
826
827 if (!rsnd_ssi_is_parent(mod, io)) {
828 ssi->byte_pos = 0;
829 ssi->byte_per_period = runtime->period_size *
830 runtime->channels *
831 samples_to_bytes(runtime, 1);
832 ssi->next_period_byte = ssi->byte_per_period;
833 }
834
835 return rsnd_ssi_init(mod, io, priv);
836 }
837
rsnd_ssi_pio_pointer(struct rsnd_mod * mod,struct rsnd_dai_stream * io,snd_pcm_uframes_t * pointer)838 static int rsnd_ssi_pio_pointer(struct rsnd_mod *mod,
839 struct rsnd_dai_stream *io,
840 snd_pcm_uframes_t *pointer)
841 {
842 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
843 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
844
845 *pointer = bytes_to_frames(runtime, READ_ONCE(ssi->byte_pos));
846
847 return 0;
848 }
849
rsnd_ssi_prepare(struct rsnd_mod * mod,struct rsnd_dai_stream * io,struct rsnd_priv * priv)850 static int rsnd_ssi_prepare(struct rsnd_mod *mod,
851 struct rsnd_dai_stream *io,
852 struct rsnd_priv *priv)
853 {
854 return rsnd_ssi_master_clk_start(mod, io);
855 }
856
857 static struct rsnd_mod_ops rsnd_ssi_pio_ops = {
858 .name = SSI_NAME,
859 .probe = rsnd_ssi_common_probe,
860 .remove = rsnd_ssi_common_remove,
861 .init = rsnd_ssi_pio_init,
862 .quit = rsnd_ssi_quit,
863 .start = rsnd_ssi_start,
864 .stop = rsnd_ssi_stop,
865 .irq = rsnd_ssi_irq,
866 .pointer = rsnd_ssi_pio_pointer,
867 .pcm_new = rsnd_ssi_pcm_new,
868 .hw_params = rsnd_ssi_hw_params,
869 .prepare = rsnd_ssi_prepare,
870 };
871
rsnd_ssi_dma_probe(struct rsnd_mod * mod,struct rsnd_dai_stream * io,struct rsnd_priv * priv)872 static int rsnd_ssi_dma_probe(struct rsnd_mod *mod,
873 struct rsnd_dai_stream *io,
874 struct rsnd_priv *priv)
875 {
876 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
877 int ret;
878
879 /*
880 * SSIP/SSIU/IRQ/DMA are not needed on
881 * SSI Multi slaves
882 */
883 if (rsnd_ssi_is_multi_slave(mod, io))
884 return 0;
885
886 ret = rsnd_ssi_common_probe(mod, io, priv);
887 if (ret)
888 return ret;
889
890 /* SSI probe might be called many times in MUX multi path */
891 ret = rsnd_dma_attach(io, mod, &ssi->dma);
892
893 return ret;
894 }
895
rsnd_ssi_fallback(struct rsnd_mod * mod,struct rsnd_dai_stream * io,struct rsnd_priv * priv)896 static int rsnd_ssi_fallback(struct rsnd_mod *mod,
897 struct rsnd_dai_stream *io,
898 struct rsnd_priv *priv)
899 {
900 struct device *dev = rsnd_priv_to_dev(priv);
901
902 /*
903 * fallback to PIO
904 *
905 * SSI .probe might be called again.
906 * see
907 * rsnd_rdai_continuance_probe()
908 */
909 mod->ops = &rsnd_ssi_pio_ops;
910
911 dev_info(dev, "%s[%d] fallback to PIO mode\n",
912 rsnd_mod_name(mod), rsnd_mod_id(mod));
913
914 return 0;
915 }
916
rsnd_ssi_dma_req(struct rsnd_dai_stream * io,struct rsnd_mod * mod)917 static struct dma_chan *rsnd_ssi_dma_req(struct rsnd_dai_stream *io,
918 struct rsnd_mod *mod)
919 {
920 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
921 int is_play = rsnd_io_is_play(io);
922 char *name;
923
924 if (rsnd_ssi_use_busif(io))
925 name = is_play ? "rxu" : "txu";
926 else
927 name = is_play ? "rx" : "tx";
928
929 return rsnd_dma_request_channel(rsnd_ssi_of_node(priv),
930 mod, name);
931 }
932
933 static struct rsnd_mod_ops rsnd_ssi_dma_ops = {
934 .name = SSI_NAME,
935 .dma_req = rsnd_ssi_dma_req,
936 .probe = rsnd_ssi_dma_probe,
937 .remove = rsnd_ssi_common_remove,
938 .init = rsnd_ssi_init,
939 .quit = rsnd_ssi_quit,
940 .start = rsnd_ssi_start,
941 .stop = rsnd_ssi_stop,
942 .irq = rsnd_ssi_irq,
943 .pcm_new = rsnd_ssi_pcm_new,
944 .fallback = rsnd_ssi_fallback,
945 .hw_params = rsnd_ssi_hw_params,
946 .prepare = rsnd_ssi_prepare,
947 };
948
rsnd_ssi_is_dma_mode(struct rsnd_mod * mod)949 int rsnd_ssi_is_dma_mode(struct rsnd_mod *mod)
950 {
951 return mod->ops == &rsnd_ssi_dma_ops;
952 }
953
954
955 /*
956 * ssi mod function
957 */
rsnd_ssi_connect(struct rsnd_mod * mod,struct rsnd_dai_stream * io)958 static void rsnd_ssi_connect(struct rsnd_mod *mod,
959 struct rsnd_dai_stream *io)
960 {
961 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
962 enum rsnd_mod_type types[] = {
963 RSND_MOD_SSI,
964 RSND_MOD_SSIM1,
965 RSND_MOD_SSIM2,
966 RSND_MOD_SSIM3,
967 };
968 enum rsnd_mod_type type;
969 int i;
970
971 /* try SSI -> SSIM1 -> SSIM2 -> SSIM3 */
972 for (i = 0; i < ARRAY_SIZE(types); i++) {
973 type = types[i];
974 if (!rsnd_io_to_mod(io, type)) {
975 rsnd_dai_connect(mod, io, type);
976 rsnd_rdai_channels_set(rdai, (i + 1) * 2);
977 rsnd_rdai_ssi_lane_set(rdai, (i + 1));
978 return;
979 }
980 }
981 }
982
rsnd_parse_connect_ssi(struct rsnd_dai * rdai,struct device_node * playback,struct device_node * capture)983 void rsnd_parse_connect_ssi(struct rsnd_dai *rdai,
984 struct device_node *playback,
985 struct device_node *capture)
986 {
987 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
988 struct device_node *node;
989 struct device_node *np;
990 struct rsnd_mod *mod;
991 int i;
992
993 node = rsnd_ssi_of_node(priv);
994 if (!node)
995 return;
996
997 i = 0;
998 for_each_child_of_node(node, np) {
999 mod = rsnd_ssi_mod_get(priv, i);
1000 if (np == playback)
1001 rsnd_ssi_connect(mod, &rdai->playback);
1002 if (np == capture)
1003 rsnd_ssi_connect(mod, &rdai->capture);
1004 i++;
1005 }
1006
1007 of_node_put(node);
1008 }
1009
__rsnd_ssi_parse_hdmi_connection(struct rsnd_priv * priv,struct rsnd_dai_stream * io,struct device_node * remote_ep)1010 static void __rsnd_ssi_parse_hdmi_connection(struct rsnd_priv *priv,
1011 struct rsnd_dai_stream *io,
1012 struct device_node *remote_ep)
1013 {
1014 struct device *dev = rsnd_priv_to_dev(priv);
1015 struct rsnd_mod *mod = rsnd_io_to_mod_ssi(io);
1016 struct rsnd_ssi *ssi;
1017 struct device_node *remote_node = of_graph_get_port_parent(remote_ep);
1018
1019 /* support Gen3 only */
1020 if (!rsnd_is_gen3(priv))
1021 return;
1022
1023 if (!mod)
1024 return;
1025
1026 ssi = rsnd_mod_to_ssi(mod);
1027
1028 /* HDMI0 */
1029 if (strstr(remote_node->full_name, "hdmi@fead0000")) {
1030 rsnd_flags_set(ssi, RSND_SSI_HDMI0);
1031 dev_dbg(dev, "%s[%d] connected to HDMI0\n",
1032 rsnd_mod_name(mod), rsnd_mod_id(mod));
1033 }
1034
1035 /* HDMI1 */
1036 if (strstr(remote_node->full_name, "hdmi@feae0000")) {
1037 rsnd_flags_set(ssi, RSND_SSI_HDMI1);
1038 dev_dbg(dev, "%s[%d] connected to HDMI1\n",
1039 rsnd_mod_name(mod), rsnd_mod_id(mod));
1040 }
1041 }
1042
rsnd_ssi_parse_hdmi_connection(struct rsnd_priv * priv,struct device_node * endpoint,int dai_i)1043 void rsnd_ssi_parse_hdmi_connection(struct rsnd_priv *priv,
1044 struct device_node *endpoint,
1045 int dai_i)
1046 {
1047 struct rsnd_dai *rdai = rsnd_rdai_get(priv, dai_i);
1048 struct device_node *remote_ep;
1049
1050 remote_ep = of_graph_get_remote_endpoint(endpoint);
1051 if (!remote_ep)
1052 return;
1053
1054 __rsnd_ssi_parse_hdmi_connection(priv, &rdai->playback, remote_ep);
1055 __rsnd_ssi_parse_hdmi_connection(priv, &rdai->capture, remote_ep);
1056 }
1057
rsnd_ssi_mod_get(struct rsnd_priv * priv,int id)1058 struct rsnd_mod *rsnd_ssi_mod_get(struct rsnd_priv *priv, int id)
1059 {
1060 if (WARN_ON(id < 0 || id >= rsnd_ssi_nr(priv)))
1061 id = 0;
1062
1063 return rsnd_mod_get(rsnd_ssi_get(priv, id));
1064 }
1065
__rsnd_ssi_is_pin_sharing(struct rsnd_mod * mod)1066 int __rsnd_ssi_is_pin_sharing(struct rsnd_mod *mod)
1067 {
1068 if (!mod)
1069 return 0;
1070
1071 return !!(rsnd_flags_has(rsnd_mod_to_ssi(mod), RSND_SSI_CLK_PIN_SHARE));
1072 }
1073
rsnd_ssi_get_status(struct rsnd_dai_stream * io,struct rsnd_mod * mod,enum rsnd_mod_type type)1074 static u32 *rsnd_ssi_get_status(struct rsnd_dai_stream *io,
1075 struct rsnd_mod *mod,
1076 enum rsnd_mod_type type)
1077 {
1078 /*
1079 * SSIP (= SSI parent) needs to be special, otherwise,
1080 * 2nd SSI might doesn't start. see also rsnd_mod_call()
1081 *
1082 * We can't include parent SSI status on SSI, because we don't know
1083 * how many SSI requests parent SSI. Thus, it is localed on "io" now.
1084 * ex) trouble case
1085 * Playback: SSI0
1086 * Capture : SSI1 (needs SSI0)
1087 *
1088 * 1) start Capture -> SSI0/SSI1 are started.
1089 * 2) start Playback -> SSI0 doesn't work, because it is already
1090 * marked as "started" on 1)
1091 *
1092 * OTOH, using each mod's status is good for MUX case.
1093 * It doesn't need to start in 2nd start
1094 * ex)
1095 * IO-0: SRC0 -> CTU1 -+-> MUX -> DVC -> SSIU -> SSI0
1096 * |
1097 * IO-1: SRC1 -> CTU2 -+
1098 *
1099 * 1) start IO-0 -> start SSI0
1100 * 2) start IO-1 -> SSI0 doesn't need to start, because it is
1101 * already started on 1)
1102 */
1103 if (type == RSND_MOD_SSIP)
1104 return &io->parent_ssi_status;
1105
1106 return rsnd_mod_get_status(io, mod, type);
1107 }
1108
rsnd_ssi_probe(struct rsnd_priv * priv)1109 int rsnd_ssi_probe(struct rsnd_priv *priv)
1110 {
1111 struct device_node *node;
1112 struct device_node *np;
1113 struct device *dev = rsnd_priv_to_dev(priv);
1114 struct rsnd_mod_ops *ops;
1115 struct clk *clk;
1116 struct rsnd_ssi *ssi;
1117 char name[RSND_SSI_NAME_SIZE];
1118 int i, nr, ret;
1119
1120 node = rsnd_ssi_of_node(priv);
1121 if (!node)
1122 return -EINVAL;
1123
1124 nr = of_get_child_count(node);
1125 if (!nr) {
1126 ret = -EINVAL;
1127 goto rsnd_ssi_probe_done;
1128 }
1129
1130 ssi = devm_kcalloc(dev, nr, sizeof(*ssi), GFP_KERNEL);
1131 if (!ssi) {
1132 ret = -ENOMEM;
1133 goto rsnd_ssi_probe_done;
1134 }
1135
1136 priv->ssi = ssi;
1137 priv->ssi_nr = nr;
1138
1139 i = 0;
1140 for_each_child_of_node(node, np) {
1141 if (!of_device_is_available(np))
1142 goto skip;
1143
1144 ssi = rsnd_ssi_get(priv, i);
1145
1146 snprintf(name, RSND_SSI_NAME_SIZE, "%s.%d",
1147 SSI_NAME, i);
1148
1149 clk = devm_clk_get(dev, name);
1150 if (IS_ERR(clk)) {
1151 ret = PTR_ERR(clk);
1152 of_node_put(np);
1153 goto rsnd_ssi_probe_done;
1154 }
1155
1156 if (of_get_property(np, "shared-pin", NULL))
1157 rsnd_flags_set(ssi, RSND_SSI_CLK_PIN_SHARE);
1158
1159 if (of_get_property(np, "no-busif", NULL))
1160 rsnd_flags_set(ssi, RSND_SSI_NO_BUSIF);
1161
1162 ssi->irq = irq_of_parse_and_map(np, 0);
1163 if (!ssi->irq) {
1164 ret = -EINVAL;
1165 of_node_put(np);
1166 goto rsnd_ssi_probe_done;
1167 }
1168
1169 if (of_property_read_bool(np, "pio-transfer"))
1170 ops = &rsnd_ssi_pio_ops;
1171 else
1172 ops = &rsnd_ssi_dma_ops;
1173
1174 ret = rsnd_mod_init(priv, rsnd_mod_get(ssi), ops, clk,
1175 rsnd_ssi_get_status, RSND_MOD_SSI, i);
1176 if (ret) {
1177 of_node_put(np);
1178 goto rsnd_ssi_probe_done;
1179 }
1180 skip:
1181 i++;
1182 }
1183
1184 ret = 0;
1185
1186 rsnd_ssi_probe_done:
1187 of_node_put(node);
1188
1189 return ret;
1190 }
1191
rsnd_ssi_remove(struct rsnd_priv * priv)1192 void rsnd_ssi_remove(struct rsnd_priv *priv)
1193 {
1194 struct rsnd_ssi *ssi;
1195 int i;
1196
1197 for_each_rsnd_ssi(ssi, priv, i) {
1198 rsnd_mod_quit(rsnd_mod_get(ssi));
1199 }
1200 }
1201