x86/speculation/mds: Fix documentation typo
[linux/fpc-iii.git] / sound / soc / sh / rcar / ssi.c
blob60cc550c5a4ca083a755057aade3f329caf019ea
1 /*
2 * Renesas R-Car SSIU/SSI support
4 * Copyright (C) 2013 Renesas Solutions Corp.
5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
7 * Based on fsi.c
8 * Kuninori Morimoto <morimoto.kuninori@renesas.com>
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.
14 #include <sound/simple_card_utils.h>
15 #include <linux/delay.h>
16 #include "rsnd.h"
17 #define RSND_SSI_NAME_SIZE 16
20 * SSICR
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 */
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 */
52 * SSISR
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 */
60 * SSIWSR
62 #define CONT (1 << 8) /* WS Continue Function */
63 #define WS_MODE (1 << 0) /* WS Mode */
65 #define SSI_NAME "ssi"
67 struct rsnd_ssi {
68 struct rsnd_mod mod;
69 struct rsnd_mod *dma;
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;
82 int byte_pos;
83 int period_pos;
84 int byte_per_period;
85 int next_period_byte;
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)
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++)
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))
114 int rsnd_ssi_hdmi_port(struct rsnd_dai_stream *io)
116 struct rsnd_mod *mod = rsnd_io_to_mod_ssi(io);
117 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
119 if (rsnd_ssi_flags_has(ssi, RSND_SSI_HDMI0))
120 return RSND_SSI_HDMI_PORT0;
122 if (rsnd_ssi_flags_has(ssi, RSND_SSI_HDMI1))
123 return RSND_SSI_HDMI_PORT1;
125 return 0;
128 int rsnd_ssi_use_busif(struct rsnd_dai_stream *io)
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;
134 if (!rsnd_ssi_is_dma_mode(mod))
135 return 0;
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;
142 return use_busif;
145 static void rsnd_ssi_status_clear(struct rsnd_mod *mod)
147 rsnd_mod_write(mod, SSISR, 0);
150 static u32 rsnd_ssi_status_get(struct rsnd_mod *mod)
152 return rsnd_mod_read(mod, SSISR);
155 static void rsnd_ssi_status_check(struct rsnd_mod *mod,
156 u32 bit)
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;
163 for (i = 0; i < 1024; i++) {
164 status = rsnd_ssi_status_get(mod);
165 if (status & bit)
166 return;
168 udelay(50);
171 dev_warn(dev, "%s[%d] status check failed\n",
172 rsnd_mod_name(mod), rsnd_mod_id(mod));
175 static u32 rsnd_ssi_multi_slaves(struct rsnd_dai_stream *io)
177 struct rsnd_mod *mod;
178 enum rsnd_mod_type types[] = {
179 RSND_MOD_SSIM1,
180 RSND_MOD_SSIM2,
181 RSND_MOD_SSIM3,
183 int i, mask;
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;
191 mask |= 1 << rsnd_mod_id(mod);
194 return mask;
197 static u32 rsnd_ssi_run_mods(struct rsnd_dai_stream *io)
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;
203 mods = rsnd_ssi_multi_slaves_runtime(io) |
204 1 << rsnd_mod_id(ssi_mod);
206 if (ssi_parent_mod)
207 mods |= 1 << rsnd_mod_id(ssi_parent_mod);
209 return mods;
212 u32 rsnd_ssi_multi_slaves_runtime(struct rsnd_dai_stream *io)
214 if (rsnd_runtime_is_ssi_multi(io))
215 return rsnd_ssi_multi_slaves(io);
217 return 0;
220 unsigned int rsnd_ssi_clk_query(struct rsnd_priv *priv,
221 int param1, int param2, int *idx)
223 int ssi_clk_mul_table[] = {
224 1, 2, 4, 8, 16, 6, 12,
226 int j, ret;
227 unsigned int main_rate;
229 for (j = 0; j < ARRAY_SIZE(ssi_clk_mul_table); j++) {
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
237 if (j == 0)
238 continue;
241 * this driver is assuming that
242 * system word is 32bit x chan
243 * see rsnd_ssi_init()
245 main_rate = 32 * param1 * param2 * ssi_clk_mul_table[j];
247 ret = rsnd_adg_clk_query(priv, main_rate);
248 if (ret < 0)
249 continue;
251 if (idx)
252 *idx = j;
254 return main_rate;
257 return 0;
260 static int rsnd_ssi_master_clk_start(struct rsnd_mod *mod,
261 struct rsnd_dai_stream *io)
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);
274 if (!rsnd_rdai_is_clk_master(rdai))
275 return 0;
277 if (!rsnd_ssi_can_output_clk(mod))
278 return 0;
280 if (rsnd_ssi_is_multi_slave(mod, io))
281 return 0;
283 if (ssi->usrcnt > 0) {
284 if (ssi->rate != rate) {
285 dev_err(dev, "SSI parent/child should use same rate\n");
286 return -EINVAL;
289 return 0;
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;
298 ret = rsnd_adg_ssi_clk_try_start(mod, main_rate);
299 if (ret < 0)
300 return ret;
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
309 * SSICR : FORCE, SCKD, SWSD
310 * SSIWSR : CONT
312 ssi->cr_clk = FORCE | SWL_32 | SCKD | SWSD | CKDV(idx);
313 ssi->wsr = CONT;
314 ssi->rate = rate;
316 dev_dbg(dev, "%s[%d] outputs %u Hz\n",
317 rsnd_mod_name(mod),
318 rsnd_mod_id(mod), rate);
320 return 0;
323 static void rsnd_ssi_master_clk_stop(struct rsnd_mod *mod,
324 struct rsnd_dai_stream *io)
326 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
327 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
329 if (!rsnd_rdai_is_clk_master(rdai))
330 return;
332 if (!rsnd_ssi_can_output_clk(mod))
333 return;
335 if (ssi->usrcnt > 1)
336 return;
338 ssi->cr_clk = 0;
339 ssi->rate = 0;
341 rsnd_adg_ssi_clk_stop(mod);
344 static void rsnd_ssi_config_init(struct rsnd_mod *mod,
345 struct rsnd_dai_stream *io)
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;
355 if (rsnd_ssi_is_parent(mod, io))
356 return;
358 is_tdm = rsnd_runtime_is_ssi_tdm(io);
361 * always use 32bit system word.
362 * see also rsnd_ssi_master_clk_enable()
364 cr_own = FORCE | SWL_32;
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;
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;
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 */
394 * TDM Extend Mode
395 * see
396 * rsnd_ssiu_init_gen2()
398 wsr = ssi->wsr;
399 if (is_tdm) {
400 wsr |= WS_MODE;
401 cr_own |= CHNL_8;
404 ssi->cr_own = cr_own;
405 ssi->cr_mode = cr_mode;
406 ssi->wsr = wsr;
409 static void rsnd_ssi_register_setup(struct rsnd_mod *mod)
411 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
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);
420 static void rsnd_ssi_pointer_init(struct rsnd_mod *mod,
421 struct rsnd_dai_stream *io)
423 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
424 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
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;
434 static int rsnd_ssi_pointer_offset(struct rsnd_mod *mod,
435 struct rsnd_dai_stream *io,
436 int additional)
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;
442 pos %= (runtime->periods * ssi->byte_per_period);
444 return pos;
447 static bool rsnd_ssi_pointer_update(struct rsnd_mod *mod,
448 struct rsnd_dai_stream *io,
449 int byte)
451 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
452 bool ret = false;
453 int byte_pos;
455 byte_pos = ssi->byte_pos + byte;
457 if (byte_pos >= ssi->next_period_byte) {
458 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
460 ssi->period_pos++;
461 ssi->next_period_byte += ssi->byte_per_period;
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;
469 ret = true;
472 WRITE_ONCE(ssi->byte_pos, byte_pos);
474 return ret;
478 * SSI mod common functions
480 static int rsnd_ssi_init(struct rsnd_mod *mod,
481 struct rsnd_dai_stream *io,
482 struct rsnd_priv *priv)
484 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
486 if (!rsnd_ssi_is_run_mods(mod, io))
487 return 0;
489 rsnd_ssi_pointer_init(mod, io);
491 ssi->usrcnt++;
493 rsnd_mod_power_on(mod);
495 rsnd_ssi_config_init(mod, io);
497 rsnd_ssi_register_setup(mod);
499 /* clear error status */
500 rsnd_ssi_status_clear(mod);
502 return 0;
505 static int rsnd_ssi_quit(struct rsnd_mod *mod,
506 struct rsnd_dai_stream *io,
507 struct rsnd_priv *priv)
509 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
510 struct device *dev = rsnd_priv_to_dev(priv);
512 if (!rsnd_ssi_is_run_mods(mod, io))
513 return 0;
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;
521 if (!rsnd_ssi_is_parent(mod, io))
522 ssi->cr_own = 0;
524 rsnd_ssi_master_clk_stop(mod, io);
526 rsnd_mod_power_off(mod);
528 ssi->usrcnt--;
530 return 0;
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)
538 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
539 int chan = params_channels(params);
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.
546 if (ssi->usrcnt) {
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.
553 if (ssi->chan != chan)
554 return -EIO;
557 ssi->chan = chan;
559 return 0;
562 static int rsnd_ssi_start(struct rsnd_mod *mod,
563 struct rsnd_dai_stream *io,
564 struct rsnd_priv *priv)
566 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
568 if (!rsnd_ssi_is_run_mods(mod, io))
569 return 0;
572 * EN will be set via SSIU :: SSI_CONTROL
573 * if Multi channel mode
575 if (rsnd_ssi_multi_slaves_runtime(io))
576 return 0;
579 * EN is for data output.
580 * SSI parent EN is not needed.
582 if (rsnd_ssi_is_parent(mod, io))
583 return 0;
585 ssi->cr_en = EN;
587 rsnd_mod_write(mod, SSICR, ssi->cr_own |
588 ssi->cr_clk |
589 ssi->cr_mode |
590 ssi->cr_en);
592 return 0;
595 static int rsnd_ssi_stop(struct rsnd_mod *mod,
596 struct rsnd_dai_stream *io,
597 struct rsnd_priv *priv)
599 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
600 u32 cr;
602 if (!rsnd_ssi_is_run_mods(mod, io))
603 return 0;
605 if (rsnd_ssi_is_parent(mod, io))
606 return 0;
609 * disable all IRQ,
610 * and, wait all data was sent
612 cr = ssi->cr_own |
613 ssi->cr_clk;
615 rsnd_mod_write(mod, SSICR, cr | EN);
616 rsnd_ssi_status_check(mod, DIRQ);
619 * disable SSI,
620 * and, wait idle state
622 rsnd_mod_write(mod, SSICR, cr); /* disabled all */
623 rsnd_ssi_status_check(mod, IIRQ);
625 ssi->cr_en = 0;
627 return 0;
630 static int rsnd_ssi_irq(struct rsnd_mod *mod,
631 struct rsnd_dai_stream *io,
632 struct rsnd_priv *priv,
633 int enable)
635 u32 val = 0;
637 if (rsnd_is_gen1(priv))
638 return 0;
640 if (rsnd_ssi_is_parent(mod, io))
641 return 0;
643 if (!rsnd_ssi_is_run_mods(mod, io))
644 return 0;
646 if (enable)
647 val = rsnd_ssi_is_dma_mode(mod) ? 0x0e000000 : 0x0f000000;
649 rsnd_mod_write(mod, SSI_INT_ENABLE, val);
651 return 0;
654 static void __rsnd_ssi_interrupt(struct rsnd_mod *mod,
655 struct rsnd_dai_stream *io)
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;
663 spin_lock(&priv->lock);
665 /* ignore all cases if not working */
666 if (!rsnd_io_is_working(io))
667 goto rsnd_ssi_interrupt_out;
669 status = rsnd_ssi_status_get(mod);
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;
678 switch (runtime->sample_bits) {
679 case 32:
680 shift = 8;
681 break;
685 * 8/16/32 data can be assesse to TDR/RDR register
686 * directly as 32bit data
687 * see rsnd_ssi_init()
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);
694 elapsed = rsnd_ssi_pointer_update(mod, io, sizeof(*buf));
697 /* DMA only */
698 if (is_dma && (status & (UIRQ | OIRQ)))
699 stop = true;
701 rsnd_ssi_status_clear(mod);
702 rsnd_ssi_interrupt_out:
703 spin_unlock(&priv->lock);
705 if (elapsed)
706 rsnd_dai_period_elapsed(io);
708 if (stop)
709 snd_pcm_stop_xrun(io->substream);
713 static irqreturn_t rsnd_ssi_interrupt(int irq, void *data)
715 struct rsnd_mod *mod = data;
717 rsnd_mod_interrupt(mod, __rsnd_ssi_interrupt);
719 return IRQ_HANDLED;
723 * SSI PIO
725 static void rsnd_ssi_parent_attach(struct rsnd_mod *mod,
726 struct rsnd_dai_stream *io)
728 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
729 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
731 if (!__rsnd_ssi_is_pin_sharing(mod))
732 return;
734 if (!rsnd_rdai_is_clk_master(rdai))
735 return;
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;
751 static int rsnd_ssi_pcm_new(struct rsnd_mod *mod,
752 struct rsnd_dai_stream *io,
753 struct snd_soc_pcm_runtime *rtd)
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.
760 rsnd_ssi_parent_attach(mod, io);
762 return 0;
765 static int rsnd_ssi_common_probe(struct rsnd_mod *mod,
766 struct rsnd_dai_stream *io,
767 struct rsnd_priv *priv)
769 struct device *dev = rsnd_priv_to_dev(priv);
770 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
771 int ret;
774 * SSIP/SSIU/IRQ are not needed on
775 * SSI Multi slaves
777 if (rsnd_ssi_is_multi_slave(mod, io))
778 return 0;
781 * It can't judge ssi parent at this point
782 * see rsnd_ssi_pcm_new()
785 ret = rsnd_ssiu_attach(io, mod);
786 if (ret < 0)
787 return ret;
790 * SSI might be called again as PIO fallback
791 * It is easy to manual handling for IRQ request/free
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.
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);
806 rsnd_ssi_flags_set(ssi, RSND_SSI_PROBED);
809 return ret;
812 static int rsnd_ssi_common_remove(struct rsnd_mod *mod,
813 struct rsnd_dai_stream *io,
814 struct rsnd_priv *priv)
816 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
817 struct rsnd_mod *pure_ssi_mod = rsnd_io_to_mod_ssi(io);
819 /* Do nothing if non SSI (= SSI parent, multi SSI) mod */
820 if (pure_ssi_mod != mod)
821 return 0;
823 /* PIO will request IRQ again */
824 if (rsnd_ssi_flags_has(ssi, RSND_SSI_PROBED)) {
825 free_irq(ssi->irq, mod);
827 rsnd_ssi_flags_del(ssi, RSND_SSI_PROBED);
830 return 0;
833 static int rsnd_ssi_pointer(struct rsnd_mod *mod,
834 struct rsnd_dai_stream *io,
835 snd_pcm_uframes_t *pointer)
837 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
838 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
840 *pointer = bytes_to_frames(runtime, READ_ONCE(ssi->byte_pos));
842 return 0;
845 static int rsnd_ssi_prepare(struct rsnd_mod *mod,
846 struct rsnd_dai_stream *io,
847 struct rsnd_priv *priv)
849 return rsnd_ssi_master_clk_start(mod, io);
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,
867 static int rsnd_ssi_dma_probe(struct rsnd_mod *mod,
868 struct rsnd_dai_stream *io,
869 struct rsnd_priv *priv)
871 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
872 int ret;
875 * SSIP/SSIU/IRQ/DMA are not needed on
876 * SSI Multi slaves
878 if (rsnd_ssi_is_multi_slave(mod, io))
879 return 0;
881 ret = rsnd_ssi_common_probe(mod, io, priv);
882 if (ret)
883 return ret;
885 /* SSI probe might be called many times in MUX multi path */
886 ret = rsnd_dma_attach(io, mod, &ssi->dma);
888 return ret;
891 static int rsnd_ssi_fallback(struct rsnd_mod *mod,
892 struct rsnd_dai_stream *io,
893 struct rsnd_priv *priv)
895 struct device *dev = rsnd_priv_to_dev(priv);
898 * fallback to PIO
900 * SSI .probe might be called again.
901 * see
902 * rsnd_rdai_continuance_probe()
904 mod->ops = &rsnd_ssi_pio_ops;
906 dev_info(dev, "%s[%d] fallback to PIO mode\n",
907 rsnd_mod_name(mod), rsnd_mod_id(mod));
909 return 0;
912 static struct dma_chan *rsnd_ssi_dma_req(struct rsnd_dai_stream *io,
913 struct rsnd_mod *mod)
915 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
916 int is_play = rsnd_io_is_play(io);
917 char *name;
919 if (rsnd_ssi_use_busif(io))
920 name = is_play ? "rxu" : "txu";
921 else
922 name = is_play ? "rx" : "tx";
924 return rsnd_dma_request_channel(rsnd_ssi_of_node(priv),
925 mod, name);
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,
944 int rsnd_ssi_is_dma_mode(struct rsnd_mod *mod)
946 return mod->ops == &rsnd_ssi_dma_ops;
951 * ssi mod function
953 static void rsnd_ssi_connect(struct rsnd_mod *mod,
954 struct rsnd_dai_stream *io)
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,
963 enum rsnd_mod_type type;
964 int i;
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;
978 void rsnd_parse_connect_ssi(struct rsnd_dai *rdai,
979 struct device_node *playback,
980 struct device_node *capture)
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;
988 node = rsnd_ssi_of_node(priv);
989 if (!node)
990 return;
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++;
1002 of_node_put(node);
1005 static void __rsnd_ssi_parse_hdmi_connection(struct rsnd_priv *priv,
1006 struct rsnd_dai_stream *io,
1007 struct device_node *remote_ep)
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;
1013 if (!mod)
1014 return;
1016 ssi = rsnd_mod_to_ssi(mod);
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));
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));
1031 void rsnd_ssi_parse_hdmi_connection(struct rsnd_priv *priv,
1032 struct device_node *endpoint,
1033 int dai_i)
1035 struct rsnd_dai *rdai = rsnd_rdai_get(priv, dai_i);
1036 struct device_node *remote_ep;
1038 remote_ep = of_graph_get_remote_endpoint(endpoint);
1039 if (!remote_ep)
1040 return;
1042 __rsnd_ssi_parse_hdmi_connection(priv, &rdai->playback, remote_ep);
1043 __rsnd_ssi_parse_hdmi_connection(priv, &rdai->capture, remote_ep);
1046 struct rsnd_mod *rsnd_ssi_mod_get(struct rsnd_priv *priv, int id)
1048 if (WARN_ON(id < 0 || id >= rsnd_ssi_nr(priv)))
1049 id = 0;
1051 return rsnd_mod_get(rsnd_ssi_get(priv, id));
1054 int __rsnd_ssi_is_pin_sharing(struct rsnd_mod *mod)
1056 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
1058 return !!(rsnd_ssi_flags_has(ssi, RSND_SSI_CLK_PIN_SHARE));
1061 static u32 *rsnd_ssi_get_status(struct rsnd_dai_stream *io,
1062 struct rsnd_mod *mod,
1063 enum rsnd_mod_type type)
1066 * SSIP (= SSI parent) needs to be special, otherwise,
1067 * 2nd SSI might doesn't start. see also rsnd_mod_call()
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)
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)
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
1084 * IO-1: SRC1 -> CTU2 -+
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)
1090 if (type == RSND_MOD_SSIP)
1091 return &io->parent_ssi_status;
1093 return rsnd_mod_get_status(io, mod, type);
1096 int rsnd_ssi_probe(struct rsnd_priv *priv)
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;
1107 node = rsnd_ssi_of_node(priv);
1108 if (!node)
1109 return -EINVAL;
1111 nr = of_get_child_count(node);
1112 if (!nr) {
1113 ret = -EINVAL;
1114 goto rsnd_ssi_probe_done;
1117 ssi = devm_kzalloc(dev, sizeof(*ssi) * nr, GFP_KERNEL);
1118 if (!ssi) {
1119 ret = -ENOMEM;
1120 goto rsnd_ssi_probe_done;
1123 priv->ssi = ssi;
1124 priv->ssi_nr = nr;
1126 i = 0;
1127 for_each_child_of_node(node, np) {
1128 ssi = rsnd_ssi_get(priv, i);
1130 snprintf(name, RSND_SSI_NAME_SIZE, "%s.%d",
1131 SSI_NAME, i);
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;
1140 if (of_get_property(np, "shared-pin", NULL))
1141 rsnd_ssi_flags_set(ssi, RSND_SSI_CLK_PIN_SHARE);
1143 if (of_get_property(np, "no-busif", NULL))
1144 rsnd_ssi_flags_set(ssi, RSND_SSI_NO_BUSIF);
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;
1153 if (of_property_read_bool(np, "pio-transfer"))
1154 ops = &rsnd_ssi_pio_ops;
1155 else
1156 ops = &rsnd_ssi_dma_ops;
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;
1165 i++;
1168 ret = 0;
1170 rsnd_ssi_probe_done:
1171 of_node_put(node);
1173 return ret;
1176 void rsnd_ssi_remove(struct rsnd_priv *priv)
1178 struct rsnd_ssi *ssi;
1179 int i;
1181 for_each_rsnd_ssi(ssi, priv, i) {
1182 rsnd_mod_quit(rsnd_mod_get(ssi));