2 * Renesas R-Car SSIU/SSI support
4 * Copyright (C) 2013 Renesas Solutions Corp.
5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
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>
17 #define RSND_SSI_NAME_SIZE 16
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 */
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 */
62 #define CONT (1 << 8) /* WS Continue Function */
63 #define WS_MODE (1 << 0) /* WS Mode */
65 #define SSI_NAME "ssi"
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) \
97 (i < rsnd_ssi_nr(priv)) && \
98 ((pos) = ((struct rsnd_ssi *)(priv)->ssi + 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;
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);
134 if (!rsnd_ssi_is_dma_mode(mod))
137 if (!(rsnd_ssi_flags_has(ssi, RSND_SSI_NO_BUSIF)))
139 if (rsnd_io_to_mod_src(io))
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,
158 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
159 struct device *dev = rsnd_priv_to_dev(priv);
163 for (i = 0; i < 1024; i++) {
164 status = rsnd_ssi_status_get(mod);
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[] = {
186 for (i = 0; i < ARRAY_SIZE(types); i++) {
187 mod = rsnd_io_to_mod(io, types[i]);
191 mask |= 1 << rsnd_mod_id(mod);
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);
202 return rsnd_ssi_multi_slaves_runtime(io) |
203 1 << rsnd_mod_id(ssi_mod) |
204 1 << rsnd_mod_id(ssi_parent_mod);
207 u32 rsnd_ssi_multi_slaves_runtime(struct rsnd_dai_stream *io)
209 if (rsnd_runtime_is_ssi_multi(io))
210 return rsnd_ssi_multi_slaves(io);
215 unsigned int rsnd_ssi_clk_query(struct rsnd_priv *priv,
216 int param1, int param2, int *idx)
218 int ssi_clk_mul_table[] = {
219 1, 2, 4, 8, 16, 6, 12,
222 unsigned int main_rate;
224 for (j = 0; j < ARRAY_SIZE(ssi_clk_mul_table); j++) {
227 * It will set SSIWSR.CONT here, but SSICR.CKDV = 000
228 * with it is not allowed. (SSIWSR.WS_MODE with
229 * SSICR.CKDV = 000 is not allowed either).
230 * Skip it. See SSICR.CKDV
236 * this driver is assuming that
237 * system word is 32bit x chan
238 * see rsnd_ssi_init()
240 main_rate = 32 * param1 * param2 * ssi_clk_mul_table[j];
242 ret = rsnd_adg_clk_query(priv, main_rate);
255 static int rsnd_ssi_master_clk_start(struct rsnd_mod *mod,
256 struct rsnd_dai_stream *io)
258 struct rsnd_priv *priv = rsnd_io_to_priv(io);
259 struct device *dev = rsnd_priv_to_dev(priv);
260 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
261 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
262 int chan = rsnd_runtime_channel_for_ssi(io);
264 unsigned int main_rate;
265 unsigned int rate = rsnd_io_is_play(io) ?
266 rsnd_src_get_out_rate(priv, io) :
267 rsnd_src_get_in_rate(priv, io);
269 if (!rsnd_rdai_is_clk_master(rdai))
272 if (!rsnd_ssi_can_output_clk(mod))
275 if (rsnd_ssi_is_multi_slave(mod, io))
278 if (ssi->usrcnt > 1) {
279 if (ssi->rate != rate) {
280 dev_err(dev, "SSI parent/child should use same rate\n");
287 main_rate = rsnd_ssi_clk_query(priv, rate, chan, &idx);
289 dev_err(dev, "unsupported clock rate\n");
293 ret = rsnd_adg_ssi_clk_try_start(mod, main_rate);
298 * SSI clock will be output contiguously
300 * This means, rsnd_ssi_master_clk_start()
301 * and rsnd_ssi_register_setup() are necessary
304 * SSICR : FORCE, SCKD, SWSD
307 ssi->cr_clk = FORCE | SWL_32 | SCKD | SWSD | CKDV(idx);
311 dev_dbg(dev, "%s[%d] outputs %u Hz\n",
313 rsnd_mod_id(mod), rate);
318 static void rsnd_ssi_master_clk_stop(struct rsnd_mod *mod,
319 struct rsnd_dai_stream *io)
321 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
322 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
324 if (!rsnd_rdai_is_clk_master(rdai))
327 if (!rsnd_ssi_can_output_clk(mod))
336 rsnd_adg_ssi_clk_stop(mod);
339 static void rsnd_ssi_config_init(struct rsnd_mod *mod,
340 struct rsnd_dai_stream *io)
342 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
343 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
344 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
350 if (rsnd_ssi_is_parent(mod, io))
353 is_tdm = rsnd_runtime_is_ssi_tdm(io);
356 * always use 32bit system word.
357 * see also rsnd_ssi_master_clk_enable()
359 cr_own = FORCE | SWL_32;
361 if (rdai->bit_clk_inv)
363 if (rdai->frm_clk_inv ^ is_tdm)
365 if (rdai->data_alignment)
369 if (rsnd_io_is_play(io))
372 switch (runtime->sample_bits) {
381 if (rsnd_ssi_is_dma_mode(mod)) {
382 cr_mode = UIEN | OIEN | /* over/under run */
383 DMEN; /* DMA : enable DMA */
385 cr_mode = DIEN; /* PIO : enable Data interrupt */
391 * rsnd_ssiu_init_gen2()
399 ssi->cr_own = cr_own;
400 ssi->cr_mode = cr_mode;
404 static void rsnd_ssi_register_setup(struct rsnd_mod *mod)
406 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
408 rsnd_mod_write(mod, SSIWSR, ssi->wsr);
409 rsnd_mod_write(mod, SSICR, ssi->cr_own |
415 static void rsnd_ssi_pointer_init(struct rsnd_mod *mod,
416 struct rsnd_dai_stream *io)
418 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
419 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
423 ssi->byte_per_period = runtime->period_size *
425 samples_to_bytes(runtime, 1);
426 ssi->next_period_byte = ssi->byte_per_period;
429 static int rsnd_ssi_pointer_offset(struct rsnd_mod *mod,
430 struct rsnd_dai_stream *io,
433 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
434 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
435 int pos = ssi->byte_pos + additional;
437 pos %= (runtime->periods * ssi->byte_per_period);
442 static bool rsnd_ssi_pointer_update(struct rsnd_mod *mod,
443 struct rsnd_dai_stream *io,
446 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
448 ssi->byte_pos += byte;
450 if (ssi->byte_pos >= ssi->next_period_byte) {
451 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
454 ssi->next_period_byte += ssi->byte_per_period;
456 if (ssi->period_pos >= runtime->periods) {
459 ssi->next_period_byte = ssi->byte_per_period;
469 * SSI mod common functions
471 static int rsnd_ssi_init(struct rsnd_mod *mod,
472 struct rsnd_dai_stream *io,
473 struct rsnd_priv *priv)
475 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
478 if (!rsnd_ssi_is_run_mods(mod, io))
481 rsnd_ssi_pointer_init(mod, io);
485 rsnd_mod_power_on(mod);
487 ret = rsnd_ssi_master_clk_start(mod, io);
491 rsnd_ssi_config_init(mod, io);
493 rsnd_ssi_register_setup(mod);
495 /* clear error status */
496 rsnd_ssi_status_clear(mod);
501 static int rsnd_ssi_quit(struct rsnd_mod *mod,
502 struct rsnd_dai_stream *io,
503 struct rsnd_priv *priv)
505 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
506 struct device *dev = rsnd_priv_to_dev(priv);
508 if (!rsnd_ssi_is_run_mods(mod, io))
512 dev_err(dev, "%s[%d] usrcnt error\n",
513 rsnd_mod_name(mod), rsnd_mod_id(mod));
517 if (!rsnd_ssi_is_parent(mod, io))
520 rsnd_ssi_master_clk_stop(mod, io);
522 rsnd_mod_power_off(mod);
529 static int rsnd_ssi_hw_params(struct rsnd_mod *mod,
530 struct rsnd_dai_stream *io,
531 struct snd_pcm_substream *substream,
532 struct snd_pcm_hw_params *params)
534 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
535 int chan = params_channels(params);
538 * snd_pcm_ops::hw_params will be called *before*
539 * snd_soc_dai_ops::trigger. Thus, ssi->usrcnt is 0
545 * It will happen if SSI has parent/child connection.
546 * it is error if child <-> parent SSI uses
547 * different channels.
549 if (ssi->chan != chan)
558 static int rsnd_ssi_start(struct rsnd_mod *mod,
559 struct rsnd_dai_stream *io,
560 struct rsnd_priv *priv)
562 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
564 if (!rsnd_ssi_is_run_mods(mod, io))
568 * EN will be set via SSIU :: SSI_CONTROL
569 * if Multi channel mode
571 if (rsnd_ssi_multi_slaves_runtime(io))
575 * EN is for data output.
576 * SSI parent EN is not needed.
578 if (rsnd_ssi_is_parent(mod, io))
583 rsnd_mod_write(mod, SSICR, ssi->cr_own |
591 static int rsnd_ssi_stop(struct rsnd_mod *mod,
592 struct rsnd_dai_stream *io,
593 struct rsnd_priv *priv)
595 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
598 if (!rsnd_ssi_is_run_mods(mod, io))
601 if (rsnd_ssi_is_parent(mod, io))
606 * and, wait all data was sent
611 rsnd_mod_write(mod, SSICR, cr | EN);
612 rsnd_ssi_status_check(mod, DIRQ);
616 * and, wait idle state
618 rsnd_mod_write(mod, SSICR, cr); /* disabled all */
619 rsnd_ssi_status_check(mod, IIRQ);
626 static int rsnd_ssi_irq(struct rsnd_mod *mod,
627 struct rsnd_dai_stream *io,
628 struct rsnd_priv *priv,
633 if (rsnd_is_gen1(priv))
636 if (rsnd_ssi_is_parent(mod, io))
639 if (!rsnd_ssi_is_run_mods(mod, io))
643 val = rsnd_ssi_is_dma_mode(mod) ? 0x0e000000 : 0x0f000000;
645 rsnd_mod_write(mod, SSI_INT_ENABLE, val);
650 static void __rsnd_ssi_interrupt(struct rsnd_mod *mod,
651 struct rsnd_dai_stream *io)
653 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
654 int is_dma = rsnd_ssi_is_dma_mode(mod);
656 bool elapsed = false;
659 spin_lock(&priv->lock);
661 /* ignore all cases if not working */
662 if (!rsnd_io_is_working(io))
663 goto rsnd_ssi_interrupt_out;
665 status = rsnd_ssi_status_get(mod);
668 if (!is_dma && (status & DIRQ)) {
669 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
670 u32 *buf = (u32 *)(runtime->dma_area +
671 rsnd_ssi_pointer_offset(mod, io, 0));
674 switch (runtime->sample_bits) {
681 * 8/16/32 data can be assesse to TDR/RDR register
682 * directly as 32bit data
683 * see rsnd_ssi_init()
685 if (rsnd_io_is_play(io))
686 rsnd_mod_write(mod, SSITDR, (*buf) << shift);
688 *buf = (rsnd_mod_read(mod, SSIRDR) >> shift);
690 elapsed = rsnd_ssi_pointer_update(mod, io, sizeof(*buf));
694 if (is_dma && (status & (UIRQ | OIRQ)))
697 rsnd_ssi_status_clear(mod);
698 rsnd_ssi_interrupt_out:
699 spin_unlock(&priv->lock);
702 rsnd_dai_period_elapsed(io);
705 snd_pcm_stop_xrun(io->substream);
709 static irqreturn_t rsnd_ssi_interrupt(int irq, void *data)
711 struct rsnd_mod *mod = data;
713 rsnd_mod_interrupt(mod, __rsnd_ssi_interrupt);
721 static void rsnd_ssi_parent_attach(struct rsnd_mod *mod,
722 struct rsnd_dai_stream *io)
724 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
725 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
727 if (!__rsnd_ssi_is_pin_sharing(mod))
730 if (!rsnd_rdai_is_clk_master(rdai))
733 switch (rsnd_mod_id(mod)) {
736 rsnd_dai_connect(rsnd_ssi_mod_get(priv, 0), io, RSND_MOD_SSIP);
739 rsnd_dai_connect(rsnd_ssi_mod_get(priv, 3), io, RSND_MOD_SSIP);
742 rsnd_dai_connect(rsnd_ssi_mod_get(priv, 7), io, RSND_MOD_SSIP);
747 static int rsnd_ssi_pcm_new(struct rsnd_mod *mod,
748 struct rsnd_dai_stream *io,
749 struct snd_soc_pcm_runtime *rtd)
752 * rsnd_rdai_is_clk_master() will be enabled after set_fmt,
753 * and, pcm_new will be called after it.
754 * This function reuse pcm_new at this point.
756 rsnd_ssi_parent_attach(mod, io);
761 static int rsnd_ssi_common_probe(struct rsnd_mod *mod,
762 struct rsnd_dai_stream *io,
763 struct rsnd_priv *priv)
765 struct device *dev = rsnd_priv_to_dev(priv);
766 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
770 * SSIP/SSIU/IRQ are not needed on
773 if (rsnd_ssi_is_multi_slave(mod, io))
777 * It can't judge ssi parent at this point
778 * see rsnd_ssi_pcm_new()
781 ret = rsnd_ssiu_attach(io, mod);
786 * SSI might be called again as PIO fallback
787 * It is easy to manual handling for IRQ request/free
789 * OTOH, this function might be called many times if platform is
790 * using MIX. It needs xxx_attach() many times on xxx_probe().
791 * Because of it, we can't control .probe/.remove calling count by
793 * But it don't need to call request_irq() many times.
794 * Let's control it by RSND_SSI_PROBED flag.
796 if (!rsnd_ssi_flags_has(ssi, RSND_SSI_PROBED)) {
797 ret = request_irq(ssi->irq,
802 rsnd_ssi_flags_set(ssi, RSND_SSI_PROBED);
808 static int rsnd_ssi_common_remove(struct rsnd_mod *mod,
809 struct rsnd_dai_stream *io,
810 struct rsnd_priv *priv)
812 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
813 struct rsnd_mod *pure_ssi_mod = rsnd_io_to_mod_ssi(io);
815 /* Do nothing if non SSI (= SSI parent, multi SSI) mod */
816 if (pure_ssi_mod != mod)
819 /* PIO will request IRQ again */
820 if (rsnd_ssi_flags_has(ssi, RSND_SSI_PROBED)) {
821 free_irq(ssi->irq, mod);
823 rsnd_ssi_flags_del(ssi, RSND_SSI_PROBED);
829 static int rsnd_ssi_pointer(struct rsnd_mod *mod,
830 struct rsnd_dai_stream *io,
831 snd_pcm_uframes_t *pointer)
833 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
834 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
836 *pointer = bytes_to_frames(runtime, ssi->byte_pos);
841 static struct rsnd_mod_ops rsnd_ssi_pio_ops = {
843 .probe = rsnd_ssi_common_probe,
844 .remove = rsnd_ssi_common_remove,
845 .init = rsnd_ssi_init,
846 .quit = rsnd_ssi_quit,
847 .start = rsnd_ssi_start,
848 .stop = rsnd_ssi_stop,
850 .pointer= rsnd_ssi_pointer,
851 .pcm_new = rsnd_ssi_pcm_new,
852 .hw_params = rsnd_ssi_hw_params,
855 static int rsnd_ssi_dma_probe(struct rsnd_mod *mod,
856 struct rsnd_dai_stream *io,
857 struct rsnd_priv *priv)
859 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
863 * SSIP/SSIU/IRQ/DMA are not needed on
866 if (rsnd_ssi_is_multi_slave(mod, io))
869 ret = rsnd_ssi_common_probe(mod, io, priv);
873 /* SSI probe might be called many times in MUX multi path */
874 ret = rsnd_dma_attach(io, mod, &ssi->dma);
879 static int rsnd_ssi_fallback(struct rsnd_mod *mod,
880 struct rsnd_dai_stream *io,
881 struct rsnd_priv *priv)
883 struct device *dev = rsnd_priv_to_dev(priv);
888 * SSI .probe might be called again.
890 * rsnd_rdai_continuance_probe()
892 mod->ops = &rsnd_ssi_pio_ops;
894 dev_info(dev, "%s[%d] fallback to PIO mode\n",
895 rsnd_mod_name(mod), rsnd_mod_id(mod));
900 static struct dma_chan *rsnd_ssi_dma_req(struct rsnd_dai_stream *io,
901 struct rsnd_mod *mod)
903 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
904 int is_play = rsnd_io_is_play(io);
907 if (rsnd_ssi_use_busif(io))
908 name = is_play ? "rxu" : "txu";
910 name = is_play ? "rx" : "tx";
912 return rsnd_dma_request_channel(rsnd_ssi_of_node(priv),
916 static struct rsnd_mod_ops rsnd_ssi_dma_ops = {
918 .dma_req = rsnd_ssi_dma_req,
919 .probe = rsnd_ssi_dma_probe,
920 .remove = rsnd_ssi_common_remove,
921 .init = rsnd_ssi_init,
922 .quit = rsnd_ssi_quit,
923 .start = rsnd_ssi_start,
924 .stop = rsnd_ssi_stop,
926 .pcm_new = rsnd_ssi_pcm_new,
927 .fallback = rsnd_ssi_fallback,
928 .hw_params = rsnd_ssi_hw_params,
931 int rsnd_ssi_is_dma_mode(struct rsnd_mod *mod)
933 return mod->ops == &rsnd_ssi_dma_ops;
940 static void rsnd_ssi_connect(struct rsnd_mod *mod,
941 struct rsnd_dai_stream *io)
943 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
944 enum rsnd_mod_type types[] = {
950 enum rsnd_mod_type type;
953 /* try SSI -> SSIM1 -> SSIM2 -> SSIM3 */
954 for (i = 0; i < ARRAY_SIZE(types); i++) {
956 if (!rsnd_io_to_mod(io, type)) {
957 rsnd_dai_connect(mod, io, type);
958 rsnd_rdai_channels_set(rdai, (i + 1) * 2);
959 rsnd_rdai_ssi_lane_set(rdai, (i + 1));
965 void rsnd_parse_connect_ssi(struct rsnd_dai *rdai,
966 struct device_node *playback,
967 struct device_node *capture)
969 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
970 struct device_node *node;
971 struct device_node *np;
972 struct rsnd_mod *mod;
975 node = rsnd_ssi_of_node(priv);
980 for_each_child_of_node(node, np) {
981 mod = rsnd_ssi_mod_get(priv, i);
983 rsnd_ssi_connect(mod, &rdai->playback);
985 rsnd_ssi_connect(mod, &rdai->capture);
992 static void __rsnd_ssi_parse_hdmi_connection(struct rsnd_priv *priv,
993 struct rsnd_dai_stream *io,
994 struct device_node *remote_ep)
996 struct device *dev = rsnd_priv_to_dev(priv);
997 struct rsnd_mod *mod = rsnd_io_to_mod_ssi(io);
998 struct rsnd_ssi *ssi;
1003 ssi = rsnd_mod_to_ssi(mod);
1005 if (strstr(remote_ep->full_name, "hdmi0")) {
1006 rsnd_ssi_flags_set(ssi, RSND_SSI_HDMI0);
1007 dev_dbg(dev, "%s[%d] connected to HDMI0\n",
1008 rsnd_mod_name(mod), rsnd_mod_id(mod));
1011 if (strstr(remote_ep->full_name, "hdmi1")) {
1012 rsnd_ssi_flags_set(ssi, RSND_SSI_HDMI1);
1013 dev_dbg(dev, "%s[%d] connected to HDMI1\n",
1014 rsnd_mod_name(mod), rsnd_mod_id(mod));
1018 void rsnd_ssi_parse_hdmi_connection(struct rsnd_priv *priv,
1019 struct device_node *endpoint,
1022 struct rsnd_dai *rdai = rsnd_rdai_get(priv, dai_i);
1023 struct device_node *remote_ep;
1025 remote_ep = of_graph_get_remote_endpoint(endpoint);
1029 __rsnd_ssi_parse_hdmi_connection(priv, &rdai->playback, remote_ep);
1030 __rsnd_ssi_parse_hdmi_connection(priv, &rdai->capture, remote_ep);
1033 struct rsnd_mod *rsnd_ssi_mod_get(struct rsnd_priv *priv, int id)
1035 if (WARN_ON(id < 0 || id >= rsnd_ssi_nr(priv)))
1038 return rsnd_mod_get(rsnd_ssi_get(priv, id));
1041 int __rsnd_ssi_is_pin_sharing(struct rsnd_mod *mod)
1043 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
1045 return !!(rsnd_ssi_flags_has(ssi, RSND_SSI_CLK_PIN_SHARE));
1048 static u32 *rsnd_ssi_get_status(struct rsnd_dai_stream *io,
1049 struct rsnd_mod *mod,
1050 enum rsnd_mod_type type)
1053 * SSIP (= SSI parent) needs to be special, otherwise,
1054 * 2nd SSI might doesn't start. see also rsnd_mod_call()
1056 * We can't include parent SSI status on SSI, because we don't know
1057 * how many SSI requests parent SSI. Thus, it is localed on "io" now.
1060 * Capture : SSI1 (needs SSI0)
1062 * 1) start Capture -> SSI0/SSI1 are started.
1063 * 2) start Playback -> SSI0 doesn't work, because it is already
1064 * marked as "started" on 1)
1066 * OTOH, using each mod's status is good for MUX case.
1067 * It doesn't need to start in 2nd start
1069 * IO-0: SRC0 -> CTU1 -+-> MUX -> DVC -> SSIU -> SSI0
1071 * IO-1: SRC1 -> CTU2 -+
1073 * 1) start IO-0 -> start SSI0
1074 * 2) start IO-1 -> SSI0 doesn't need to start, because it is
1075 * already started on 1)
1077 if (type == RSND_MOD_SSIP)
1078 return &io->parent_ssi_status;
1080 return rsnd_mod_get_status(io, mod, type);
1083 int rsnd_ssi_probe(struct rsnd_priv *priv)
1085 struct device_node *node;
1086 struct device_node *np;
1087 struct device *dev = rsnd_priv_to_dev(priv);
1088 struct rsnd_mod_ops *ops;
1090 struct rsnd_ssi *ssi;
1091 char name[RSND_SSI_NAME_SIZE];
1094 node = rsnd_ssi_of_node(priv);
1098 nr = of_get_child_count(node);
1101 goto rsnd_ssi_probe_done;
1104 ssi = devm_kzalloc(dev, sizeof(*ssi) * nr, GFP_KERNEL);
1107 goto rsnd_ssi_probe_done;
1114 for_each_child_of_node(node, np) {
1115 ssi = rsnd_ssi_get(priv, i);
1117 snprintf(name, RSND_SSI_NAME_SIZE, "%s.%d",
1120 clk = devm_clk_get(dev, name);
1124 goto rsnd_ssi_probe_done;
1127 if (of_get_property(np, "shared-pin", NULL))
1128 rsnd_ssi_flags_set(ssi, RSND_SSI_CLK_PIN_SHARE);
1130 if (of_get_property(np, "no-busif", NULL))
1131 rsnd_ssi_flags_set(ssi, RSND_SSI_NO_BUSIF);
1133 ssi->irq = irq_of_parse_and_map(np, 0);
1137 goto rsnd_ssi_probe_done;
1140 if (of_property_read_bool(np, "pio-transfer"))
1141 ops = &rsnd_ssi_pio_ops;
1143 ops = &rsnd_ssi_dma_ops;
1145 ret = rsnd_mod_init(priv, rsnd_mod_get(ssi), ops, clk,
1146 rsnd_ssi_get_status, RSND_MOD_SSI, i);
1149 goto rsnd_ssi_probe_done;
1157 rsnd_ssi_probe_done:
1163 void rsnd_ssi_remove(struct rsnd_priv *priv)
1165 struct rsnd_ssi *ssi;
1168 for_each_rsnd_ssi(ssi, priv, i) {
1169 rsnd_mod_quit(rsnd_mod_get(ssi));