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