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"
88 #define RSND_SSI_CLK_PIN_SHARE (1 << 0)
89 #define RSND_SSI_NO_BUSIF (1 << 1) /* SSI+DMA without BUSIF */
90 #define RSND_SSI_HDMI0 (1 << 2) /* for HDMI0 */
91 #define RSND_SSI_HDMI1 (1 << 3) /* for HDMI1 */
92 #define RSND_SSI_PROBED (1 << 4)
94 #define for_each_rsnd_ssi(pos, priv, i) \
96 (i < rsnd_ssi_nr(priv)) && \
97 ((pos) = ((struct rsnd_ssi *)(priv)->ssi + i)); \
100 #define rsnd_ssi_get(priv, id) ((struct rsnd_ssi *)(priv->ssi) + id)
101 #define rsnd_ssi_nr(priv) ((priv)->ssi_nr)
102 #define rsnd_mod_to_ssi(_mod) container_of((_mod), struct rsnd_ssi, mod)
103 #define rsnd_ssi_flags_has(p, f) ((p)->flags & f)
104 #define rsnd_ssi_flags_set(p, f) ((p)->flags |= f)
105 #define rsnd_ssi_flags_del(p, f) ((p)->flags = ((p)->flags & ~f))
106 #define rsnd_ssi_is_parent(ssi, io) ((ssi) == rsnd_io_to_mod_ssip(io))
107 #define rsnd_ssi_is_multi_slave(mod, io) \
108 (rsnd_ssi_multi_slaves(io) & (1 << rsnd_mod_id(mod)))
109 #define rsnd_ssi_is_run_mods(mod, io) \
110 (rsnd_ssi_run_mods(io) & (1 << rsnd_mod_id(mod)))
111 #define rsnd_ssi_can_output_clk(mod) (!__rsnd_ssi_is_pin_sharing(mod))
113 int rsnd_ssi_hdmi_port(struct rsnd_dai_stream *io)
115 struct rsnd_mod *mod = rsnd_io_to_mod_ssi(io);
116 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
118 if (rsnd_ssi_flags_has(ssi, RSND_SSI_HDMI0))
119 return RSND_SSI_HDMI_PORT0;
121 if (rsnd_ssi_flags_has(ssi, RSND_SSI_HDMI1))
122 return RSND_SSI_HDMI_PORT1;
127 int rsnd_ssi_use_busif(struct rsnd_dai_stream *io)
129 struct rsnd_mod *mod = rsnd_io_to_mod_ssi(io);
130 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
133 if (!rsnd_ssi_is_dma_mode(mod))
136 if (!(rsnd_ssi_flags_has(ssi, RSND_SSI_NO_BUSIF)))
138 if (rsnd_io_to_mod_src(io))
144 static void rsnd_ssi_status_clear(struct rsnd_mod *mod)
146 rsnd_mod_write(mod, SSISR, 0);
149 static u32 rsnd_ssi_status_get(struct rsnd_mod *mod)
151 return rsnd_mod_read(mod, SSISR);
154 static void rsnd_ssi_status_check(struct rsnd_mod *mod,
157 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
158 struct device *dev = rsnd_priv_to_dev(priv);
162 for (i = 0; i < 1024; i++) {
163 status = rsnd_ssi_status_get(mod);
170 dev_warn(dev, "%s[%d] status check failed\n",
171 rsnd_mod_name(mod), rsnd_mod_id(mod));
174 static u32 rsnd_ssi_multi_slaves(struct rsnd_dai_stream *io)
176 struct rsnd_mod *mod;
177 enum rsnd_mod_type types[] = {
185 for (i = 0; i < ARRAY_SIZE(types); i++) {
186 mod = rsnd_io_to_mod(io, types[i]);
190 mask |= 1 << rsnd_mod_id(mod);
196 static u32 rsnd_ssi_run_mods(struct rsnd_dai_stream *io)
198 struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io);
199 struct rsnd_mod *ssi_parent_mod = rsnd_io_to_mod_ssip(io);
202 mods = rsnd_ssi_multi_slaves_runtime(io) |
203 1 << rsnd_mod_id(ssi_mod);
206 mods |= 1 << rsnd_mod_id(ssi_parent_mod);
211 u32 rsnd_ssi_multi_slaves_runtime(struct rsnd_dai_stream *io)
213 if (rsnd_runtime_is_ssi_multi(io))
214 return rsnd_ssi_multi_slaves(io);
219 unsigned int rsnd_ssi_clk_query(struct rsnd_priv *priv,
220 int param1, int param2, int *idx)
222 int ssi_clk_mul_table[] = {
223 1, 2, 4, 8, 16, 6, 12,
226 unsigned int main_rate;
228 for (j = 0; j < ARRAY_SIZE(ssi_clk_mul_table); j++) {
231 * It will set SSIWSR.CONT here, but SSICR.CKDV = 000
232 * with it is not allowed. (SSIWSR.WS_MODE with
233 * SSICR.CKDV = 000 is not allowed either).
234 * Skip it. See SSICR.CKDV
240 * this driver is assuming that
241 * system word is 32bit x chan
242 * see rsnd_ssi_init()
244 main_rate = 32 * param1 * param2 * ssi_clk_mul_table[j];
246 ret = rsnd_adg_clk_query(priv, main_rate);
259 static int rsnd_ssi_master_clk_start(struct rsnd_mod *mod,
260 struct rsnd_dai_stream *io)
262 struct rsnd_priv *priv = rsnd_io_to_priv(io);
263 struct device *dev = rsnd_priv_to_dev(priv);
264 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
265 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
266 int chan = rsnd_runtime_channel_for_ssi(io);
268 unsigned int main_rate;
269 unsigned int rate = rsnd_io_is_play(io) ?
270 rsnd_src_get_out_rate(priv, io) :
271 rsnd_src_get_in_rate(priv, io);
273 if (!rsnd_rdai_is_clk_master(rdai))
276 if (!rsnd_ssi_can_output_clk(mod))
279 if (rsnd_ssi_is_multi_slave(mod, io))
282 if (ssi->usrcnt > 0) {
283 if (ssi->rate != rate) {
284 dev_err(dev, "SSI parent/child should use same rate\n");
291 main_rate = rsnd_ssi_clk_query(priv, rate, chan, &idx);
293 dev_err(dev, "unsupported clock rate\n");
297 ret = rsnd_adg_ssi_clk_try_start(mod, main_rate);
302 * SSI clock will be output contiguously
304 * This means, rsnd_ssi_master_clk_start()
305 * and rsnd_ssi_register_setup() are necessary
308 * SSICR : FORCE, SCKD, SWSD
311 ssi->cr_clk = FORCE | SWL_32 | SCKD | SWSD | CKDV(idx);
315 dev_dbg(dev, "%s[%d] outputs %u Hz\n",
317 rsnd_mod_id(mod), rate);
322 static void rsnd_ssi_master_clk_stop(struct rsnd_mod *mod,
323 struct rsnd_dai_stream *io)
325 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
326 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
328 if (!rsnd_rdai_is_clk_master(rdai))
331 if (!rsnd_ssi_can_output_clk(mod))
340 rsnd_adg_ssi_clk_stop(mod);
343 static void rsnd_ssi_config_init(struct rsnd_mod *mod,
344 struct rsnd_dai_stream *io)
346 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
347 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
348 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
354 if (rsnd_ssi_is_parent(mod, io))
357 is_tdm = rsnd_runtime_is_ssi_tdm(io);
360 * always use 32bit system word.
361 * see also rsnd_ssi_master_clk_enable()
363 cr_own = FORCE | SWL_32;
365 if (rdai->bit_clk_inv)
367 if (rdai->frm_clk_inv ^ is_tdm)
369 if (rdai->data_alignment)
373 if (rsnd_io_is_play(io))
376 switch (runtime->sample_bits) {
385 if (rsnd_ssi_is_dma_mode(mod)) {
386 cr_mode = UIEN | OIEN | /* over/under run */
387 DMEN; /* DMA : enable DMA */
389 cr_mode = DIEN; /* PIO : enable Data interrupt */
395 * rsnd_ssiu_init_gen2()
403 ssi->cr_own = cr_own;
404 ssi->cr_mode = cr_mode;
408 static void rsnd_ssi_register_setup(struct rsnd_mod *mod)
410 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
412 rsnd_mod_write(mod, SSIWSR, ssi->wsr);
413 rsnd_mod_write(mod, SSICR, ssi->cr_own |
419 static void rsnd_ssi_pointer_init(struct rsnd_mod *mod,
420 struct rsnd_dai_stream *io)
422 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
423 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
427 ssi->byte_per_period = runtime->period_size *
429 samples_to_bytes(runtime, 1);
430 ssi->next_period_byte = ssi->byte_per_period;
433 static int rsnd_ssi_pointer_offset(struct rsnd_mod *mod,
434 struct rsnd_dai_stream *io,
437 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
438 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
439 int pos = ssi->byte_pos + additional;
441 pos %= (runtime->periods * ssi->byte_per_period);
446 static bool rsnd_ssi_pointer_update(struct rsnd_mod *mod,
447 struct rsnd_dai_stream *io,
450 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
454 byte_pos = ssi->byte_pos + byte;
456 if (byte_pos >= ssi->next_period_byte) {
457 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
460 ssi->next_period_byte += ssi->byte_per_period;
462 if (ssi->period_pos >= runtime->periods) {
465 ssi->next_period_byte = ssi->byte_per_period;
471 WRITE_ONCE(ssi->byte_pos, byte_pos);
477 * SSI mod common functions
479 static int rsnd_ssi_init(struct rsnd_mod *mod,
480 struct rsnd_dai_stream *io,
481 struct rsnd_priv *priv)
483 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
485 if (!rsnd_ssi_is_run_mods(mod, io))
488 rsnd_ssi_pointer_init(mod, io);
492 rsnd_mod_power_on(mod);
494 rsnd_ssi_config_init(mod, io);
496 rsnd_ssi_register_setup(mod);
498 /* clear error status */
499 rsnd_ssi_status_clear(mod);
504 static int rsnd_ssi_quit(struct rsnd_mod *mod,
505 struct rsnd_dai_stream *io,
506 struct rsnd_priv *priv)
508 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
509 struct device *dev = rsnd_priv_to_dev(priv);
511 if (!rsnd_ssi_is_run_mods(mod, io))
515 dev_err(dev, "%s[%d] usrcnt error\n",
516 rsnd_mod_name(mod), rsnd_mod_id(mod));
520 if (!rsnd_ssi_is_parent(mod, io))
523 rsnd_ssi_master_clk_stop(mod, io);
525 rsnd_mod_power_off(mod);
532 static int rsnd_ssi_hw_params(struct rsnd_mod *mod,
533 struct rsnd_dai_stream *io,
534 struct snd_pcm_substream *substream,
535 struct snd_pcm_hw_params *params)
537 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
538 int chan = params_channels(params);
541 * snd_pcm_ops::hw_params will be called *before*
542 * snd_soc_dai_ops::trigger. Thus, ssi->usrcnt is 0
548 * It will happen if SSI has parent/child connection.
549 * it is error if child <-> parent SSI uses
550 * different channels.
552 if (ssi->chan != chan)
561 static int rsnd_ssi_start(struct rsnd_mod *mod,
562 struct rsnd_dai_stream *io,
563 struct rsnd_priv *priv)
565 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
567 if (!rsnd_ssi_is_run_mods(mod, io))
571 * EN will be set via SSIU :: SSI_CONTROL
572 * if Multi channel mode
574 if (rsnd_ssi_multi_slaves_runtime(io))
578 * EN is for data output.
579 * SSI parent EN is not needed.
581 if (rsnd_ssi_is_parent(mod, io))
586 rsnd_mod_write(mod, SSICR, ssi->cr_own |
594 static int rsnd_ssi_stop(struct rsnd_mod *mod,
595 struct rsnd_dai_stream *io,
596 struct rsnd_priv *priv)
598 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
601 if (!rsnd_ssi_is_run_mods(mod, io))
604 if (rsnd_ssi_is_parent(mod, io))
609 * and, wait all data was sent
614 rsnd_mod_write(mod, SSICR, cr | EN);
615 rsnd_ssi_status_check(mod, DIRQ);
619 * and, wait idle state
621 rsnd_mod_write(mod, SSICR, cr); /* disabled all */
622 rsnd_ssi_status_check(mod, IIRQ);
629 static int rsnd_ssi_irq(struct rsnd_mod *mod,
630 struct rsnd_dai_stream *io,
631 struct rsnd_priv *priv,
636 if (rsnd_is_gen1(priv))
639 if (rsnd_ssi_is_parent(mod, io))
642 if (!rsnd_ssi_is_run_mods(mod, io))
646 val = rsnd_ssi_is_dma_mode(mod) ? 0x0e000000 : 0x0f000000;
648 rsnd_mod_write(mod, SSI_INT_ENABLE, val);
653 static void __rsnd_ssi_interrupt(struct rsnd_mod *mod,
654 struct rsnd_dai_stream *io)
656 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
657 int is_dma = rsnd_ssi_is_dma_mode(mod);
659 bool elapsed = false;
662 spin_lock(&priv->lock);
664 /* ignore all cases if not working */
665 if (!rsnd_io_is_working(io))
666 goto rsnd_ssi_interrupt_out;
668 status = rsnd_ssi_status_get(mod);
671 if (!is_dma && (status & DIRQ)) {
672 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
673 u32 *buf = (u32 *)(runtime->dma_area +
674 rsnd_ssi_pointer_offset(mod, io, 0));
677 switch (runtime->sample_bits) {
684 * 8/16/32 data can be assesse to TDR/RDR register
685 * directly as 32bit data
686 * see rsnd_ssi_init()
688 if (rsnd_io_is_play(io))
689 rsnd_mod_write(mod, SSITDR, (*buf) << shift);
691 *buf = (rsnd_mod_read(mod, SSIRDR) >> shift);
693 elapsed = rsnd_ssi_pointer_update(mod, io, sizeof(*buf));
697 if (is_dma && (status & (UIRQ | OIRQ)))
700 rsnd_ssi_status_clear(mod);
701 rsnd_ssi_interrupt_out:
702 spin_unlock(&priv->lock);
705 rsnd_dai_period_elapsed(io);
708 snd_pcm_stop_xrun(io->substream);
712 static irqreturn_t rsnd_ssi_interrupt(int irq, void *data)
714 struct rsnd_mod *mod = data;
716 rsnd_mod_interrupt(mod, __rsnd_ssi_interrupt);
724 static void rsnd_ssi_parent_attach(struct rsnd_mod *mod,
725 struct rsnd_dai_stream *io)
727 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
728 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
730 if (!__rsnd_ssi_is_pin_sharing(mod))
733 if (!rsnd_rdai_is_clk_master(rdai))
736 switch (rsnd_mod_id(mod)) {
739 rsnd_dai_connect(rsnd_ssi_mod_get(priv, 0), io, RSND_MOD_SSIP);
742 rsnd_dai_connect(rsnd_ssi_mod_get(priv, 3), io, RSND_MOD_SSIP);
745 rsnd_dai_connect(rsnd_ssi_mod_get(priv, 7), io, RSND_MOD_SSIP);
750 static int rsnd_ssi_pcm_new(struct rsnd_mod *mod,
751 struct rsnd_dai_stream *io,
752 struct snd_soc_pcm_runtime *rtd)
755 * rsnd_rdai_is_clk_master() will be enabled after set_fmt,
756 * and, pcm_new will be called after it.
757 * This function reuse pcm_new at this point.
759 rsnd_ssi_parent_attach(mod, io);
764 static int rsnd_ssi_common_probe(struct rsnd_mod *mod,
765 struct rsnd_dai_stream *io,
766 struct rsnd_priv *priv)
768 struct device *dev = rsnd_priv_to_dev(priv);
769 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
773 * SSIP/SSIU/IRQ are not needed on
776 if (rsnd_ssi_is_multi_slave(mod, io))
780 * It can't judge ssi parent at this point
781 * see rsnd_ssi_pcm_new()
784 ret = rsnd_ssiu_attach(io, mod);
789 * SSI might be called again as PIO fallback
790 * It is easy to manual handling for IRQ request/free
792 * OTOH, this function might be called many times if platform is
793 * using MIX. It needs xxx_attach() many times on xxx_probe().
794 * Because of it, we can't control .probe/.remove calling count by
796 * But it don't need to call request_irq() many times.
797 * Let's control it by RSND_SSI_PROBED flag.
799 if (!rsnd_ssi_flags_has(ssi, RSND_SSI_PROBED)) {
800 ret = request_irq(ssi->irq,
805 rsnd_ssi_flags_set(ssi, RSND_SSI_PROBED);
811 static int rsnd_ssi_common_remove(struct rsnd_mod *mod,
812 struct rsnd_dai_stream *io,
813 struct rsnd_priv *priv)
815 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
816 struct rsnd_mod *pure_ssi_mod = rsnd_io_to_mod_ssi(io);
818 /* Do nothing if non SSI (= SSI parent, multi SSI) mod */
819 if (pure_ssi_mod != mod)
822 /* PIO will request IRQ again */
823 if (rsnd_ssi_flags_has(ssi, RSND_SSI_PROBED)) {
824 free_irq(ssi->irq, mod);
826 rsnd_ssi_flags_del(ssi, RSND_SSI_PROBED);
832 static int rsnd_ssi_pointer(struct rsnd_mod *mod,
833 struct rsnd_dai_stream *io,
834 snd_pcm_uframes_t *pointer)
836 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
837 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
839 *pointer = bytes_to_frames(runtime, READ_ONCE(ssi->byte_pos));
844 static int rsnd_ssi_prepare(struct rsnd_mod *mod,
845 struct rsnd_dai_stream *io,
846 struct rsnd_priv *priv)
848 return rsnd_ssi_master_clk_start(mod, io);
851 static struct rsnd_mod_ops rsnd_ssi_pio_ops = {
853 .probe = rsnd_ssi_common_probe,
854 .remove = rsnd_ssi_common_remove,
855 .init = rsnd_ssi_init,
856 .quit = rsnd_ssi_quit,
857 .start = rsnd_ssi_start,
858 .stop = rsnd_ssi_stop,
860 .pointer= rsnd_ssi_pointer,
861 .pcm_new = rsnd_ssi_pcm_new,
862 .hw_params = rsnd_ssi_hw_params,
863 .prepare = rsnd_ssi_prepare,
866 static int rsnd_ssi_dma_probe(struct rsnd_mod *mod,
867 struct rsnd_dai_stream *io,
868 struct rsnd_priv *priv)
873 * SSIP/SSIU/IRQ/DMA are not needed on
876 if (rsnd_ssi_is_multi_slave(mod, io))
879 ret = rsnd_ssi_common_probe(mod, io, priv);
883 /* SSI probe might be called many times in MUX multi path */
884 ret = rsnd_dma_attach(io, mod, &io->dma);
889 static int rsnd_ssi_fallback(struct rsnd_mod *mod,
890 struct rsnd_dai_stream *io,
891 struct rsnd_priv *priv)
893 struct device *dev = rsnd_priv_to_dev(priv);
898 * SSI .probe might be called again.
900 * rsnd_rdai_continuance_probe()
902 mod->ops = &rsnd_ssi_pio_ops;
904 dev_info(dev, "%s[%d] fallback to PIO mode\n",
905 rsnd_mod_name(mod), rsnd_mod_id(mod));
910 static struct dma_chan *rsnd_ssi_dma_req(struct rsnd_dai_stream *io,
911 struct rsnd_mod *mod)
913 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
914 int is_play = rsnd_io_is_play(io);
917 if (rsnd_ssi_use_busif(io))
918 name = is_play ? "rxu" : "txu";
920 name = is_play ? "rx" : "tx";
922 return rsnd_dma_request_channel(rsnd_ssi_of_node(priv),
926 static struct rsnd_mod_ops rsnd_ssi_dma_ops = {
928 .dma_req = rsnd_ssi_dma_req,
929 .probe = rsnd_ssi_dma_probe,
930 .remove = rsnd_ssi_common_remove,
931 .init = rsnd_ssi_init,
932 .quit = rsnd_ssi_quit,
933 .start = rsnd_ssi_start,
934 .stop = rsnd_ssi_stop,
936 .pcm_new = rsnd_ssi_pcm_new,
937 .fallback = rsnd_ssi_fallback,
938 .hw_params = rsnd_ssi_hw_params,
939 .prepare = rsnd_ssi_prepare,
942 int rsnd_ssi_is_dma_mode(struct rsnd_mod *mod)
944 return mod->ops == &rsnd_ssi_dma_ops;
951 static void rsnd_ssi_connect(struct rsnd_mod *mod,
952 struct rsnd_dai_stream *io)
954 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
955 enum rsnd_mod_type types[] = {
961 enum rsnd_mod_type type;
964 /* try SSI -> SSIM1 -> SSIM2 -> SSIM3 */
965 for (i = 0; i < ARRAY_SIZE(types); i++) {
967 if (!rsnd_io_to_mod(io, type)) {
968 rsnd_dai_connect(mod, io, type);
969 rsnd_rdai_channels_set(rdai, (i + 1) * 2);
970 rsnd_rdai_ssi_lane_set(rdai, (i + 1));
976 void rsnd_parse_connect_ssi(struct rsnd_dai *rdai,
977 struct device_node *playback,
978 struct device_node *capture)
980 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
981 struct device_node *node;
982 struct device_node *np;
983 struct rsnd_mod *mod;
986 node = rsnd_ssi_of_node(priv);
991 for_each_child_of_node(node, np) {
992 mod = rsnd_ssi_mod_get(priv, i);
994 rsnd_ssi_connect(mod, &rdai->playback);
996 rsnd_ssi_connect(mod, &rdai->capture);
1003 static void __rsnd_ssi_parse_hdmi_connection(struct rsnd_priv *priv,
1004 struct rsnd_dai_stream *io,
1005 struct device_node *remote_ep)
1007 struct device *dev = rsnd_priv_to_dev(priv);
1008 struct rsnd_mod *mod = rsnd_io_to_mod_ssi(io);
1009 struct rsnd_ssi *ssi;
1014 ssi = rsnd_mod_to_ssi(mod);
1016 if (strstr(remote_ep->full_name, "hdmi0")) {
1017 rsnd_ssi_flags_set(ssi, RSND_SSI_HDMI0);
1018 dev_dbg(dev, "%s[%d] connected to HDMI0\n",
1019 rsnd_mod_name(mod), rsnd_mod_id(mod));
1022 if (strstr(remote_ep->full_name, "hdmi1")) {
1023 rsnd_ssi_flags_set(ssi, RSND_SSI_HDMI1);
1024 dev_dbg(dev, "%s[%d] connected to HDMI1\n",
1025 rsnd_mod_name(mod), rsnd_mod_id(mod));
1029 void rsnd_ssi_parse_hdmi_connection(struct rsnd_priv *priv,
1030 struct device_node *endpoint,
1033 struct rsnd_dai *rdai = rsnd_rdai_get(priv, dai_i);
1034 struct device_node *remote_ep;
1036 remote_ep = of_graph_get_remote_endpoint(endpoint);
1040 __rsnd_ssi_parse_hdmi_connection(priv, &rdai->playback, remote_ep);
1041 __rsnd_ssi_parse_hdmi_connection(priv, &rdai->capture, remote_ep);
1044 struct rsnd_mod *rsnd_ssi_mod_get(struct rsnd_priv *priv, int id)
1046 if (WARN_ON(id < 0 || id >= rsnd_ssi_nr(priv)))
1049 return rsnd_mod_get(rsnd_ssi_get(priv, id));
1052 int __rsnd_ssi_is_pin_sharing(struct rsnd_mod *mod)
1054 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
1056 return !!(rsnd_ssi_flags_has(ssi, RSND_SSI_CLK_PIN_SHARE));
1059 static u32 *rsnd_ssi_get_status(struct rsnd_dai_stream *io,
1060 struct rsnd_mod *mod,
1061 enum rsnd_mod_type type)
1064 * SSIP (= SSI parent) needs to be special, otherwise,
1065 * 2nd SSI might doesn't start. see also rsnd_mod_call()
1067 * We can't include parent SSI status on SSI, because we don't know
1068 * how many SSI requests parent SSI. Thus, it is localed on "io" now.
1071 * Capture : SSI1 (needs SSI0)
1073 * 1) start Capture -> SSI0/SSI1 are started.
1074 * 2) start Playback -> SSI0 doesn't work, because it is already
1075 * marked as "started" on 1)
1077 * OTOH, using each mod's status is good for MUX case.
1078 * It doesn't need to start in 2nd start
1080 * IO-0: SRC0 -> CTU1 -+-> MUX -> DVC -> SSIU -> SSI0
1082 * IO-1: SRC1 -> CTU2 -+
1084 * 1) start IO-0 -> start SSI0
1085 * 2) start IO-1 -> SSI0 doesn't need to start, because it is
1086 * already started on 1)
1088 if (type == RSND_MOD_SSIP)
1089 return &io->parent_ssi_status;
1091 return rsnd_mod_get_status(io, mod, type);
1094 int rsnd_ssi_probe(struct rsnd_priv *priv)
1096 struct device_node *node;
1097 struct device_node *np;
1098 struct device *dev = rsnd_priv_to_dev(priv);
1099 struct rsnd_mod_ops *ops;
1101 struct rsnd_ssi *ssi;
1102 char name[RSND_SSI_NAME_SIZE];
1105 node = rsnd_ssi_of_node(priv);
1109 nr = of_get_child_count(node);
1112 goto rsnd_ssi_probe_done;
1115 ssi = devm_kzalloc(dev, sizeof(*ssi) * nr, GFP_KERNEL);
1118 goto rsnd_ssi_probe_done;
1125 for_each_child_of_node(node, np) {
1126 ssi = rsnd_ssi_get(priv, i);
1128 snprintf(name, RSND_SSI_NAME_SIZE, "%s.%d",
1131 clk = devm_clk_get(dev, name);
1135 goto rsnd_ssi_probe_done;
1138 if (of_get_property(np, "shared-pin", NULL))
1139 rsnd_ssi_flags_set(ssi, RSND_SSI_CLK_PIN_SHARE);
1141 if (of_get_property(np, "no-busif", NULL))
1142 rsnd_ssi_flags_set(ssi, RSND_SSI_NO_BUSIF);
1144 ssi->irq = irq_of_parse_and_map(np, 0);
1148 goto rsnd_ssi_probe_done;
1151 if (of_property_read_bool(np, "pio-transfer"))
1152 ops = &rsnd_ssi_pio_ops;
1154 ops = &rsnd_ssi_dma_ops;
1156 ret = rsnd_mod_init(priv, rsnd_mod_get(ssi), ops, clk,
1157 rsnd_ssi_get_status, RSND_MOD_SSI, i);
1160 goto rsnd_ssi_probe_done;
1168 rsnd_ssi_probe_done:
1174 void rsnd_ssi_remove(struct rsnd_priv *priv)
1176 struct rsnd_ssi *ssi;
1179 for_each_rsnd_ssi(ssi, priv, i) {
1180 rsnd_mod_quit(rsnd_mod_get(ssi));