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 */
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_to_dma(mod) ((ssi)->dma)
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_mode_flags(p) ((p)->flags)
105 #define rsnd_ssi_is_parent(ssi, io) ((ssi) == rsnd_io_to_mod_ssip(io))
106 #define rsnd_ssi_is_multi_slave(mod, io) \
107 (rsnd_ssi_multi_slaves(io) & (1 << rsnd_mod_id(mod)))
108 #define rsnd_ssi_is_run_mods(mod, io) \
109 (rsnd_ssi_run_mods(io) & (1 << rsnd_mod_id(mod)))
111 int rsnd_ssi_hdmi_port(struct rsnd_dai_stream *io)
113 struct rsnd_mod *mod = rsnd_io_to_mod_ssi(io);
114 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
116 if (rsnd_ssi_mode_flags(ssi) & RSND_SSI_HDMI0)
117 return RSND_SSI_HDMI_PORT0;
119 if (rsnd_ssi_mode_flags(ssi) & RSND_SSI_HDMI1)
120 return RSND_SSI_HDMI_PORT1;
125 int rsnd_ssi_use_busif(struct rsnd_dai_stream *io)
127 struct rsnd_mod *mod = rsnd_io_to_mod_ssi(io);
128 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
131 if (!rsnd_ssi_is_dma_mode(mod))
134 if (!(rsnd_ssi_mode_flags(ssi) & RSND_SSI_NO_BUSIF))
136 if (rsnd_io_to_mod_src(io))
142 static void rsnd_ssi_status_clear(struct rsnd_mod *mod)
144 rsnd_mod_write(mod, SSISR, 0);
147 static u32 rsnd_ssi_status_get(struct rsnd_mod *mod)
149 return rsnd_mod_read(mod, SSISR);
152 static void rsnd_ssi_status_check(struct rsnd_mod *mod,
155 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
156 struct device *dev = rsnd_priv_to_dev(priv);
160 for (i = 0; i < 1024; i++) {
161 status = rsnd_ssi_status_get(mod);
168 dev_warn(dev, "%s[%d] status check failed\n",
169 rsnd_mod_name(mod), rsnd_mod_id(mod));
172 static u32 rsnd_ssi_multi_slaves(struct rsnd_dai_stream *io)
174 struct rsnd_mod *mod;
175 enum rsnd_mod_type types[] = {
183 for (i = 0; i < ARRAY_SIZE(types); i++) {
184 mod = rsnd_io_to_mod(io, types[i]);
188 mask |= 1 << rsnd_mod_id(mod);
194 static u32 rsnd_ssi_run_mods(struct rsnd_dai_stream *io)
196 struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io);
197 struct rsnd_mod *ssi_parent_mod = rsnd_io_to_mod_ssip(io);
199 return rsnd_ssi_multi_slaves_runtime(io) |
200 1 << rsnd_mod_id(ssi_mod) |
201 1 << rsnd_mod_id(ssi_parent_mod);
204 u32 rsnd_ssi_multi_slaves_runtime(struct rsnd_dai_stream *io)
206 if (rsnd_runtime_is_ssi_multi(io))
207 return rsnd_ssi_multi_slaves(io);
212 unsigned int rsnd_ssi_clk_query(struct rsnd_priv *priv,
213 int param1, int param2, int *idx)
215 int ssi_clk_mul_table[] = {
216 1, 2, 4, 8, 16, 6, 12,
219 unsigned int main_rate;
221 for (j = 0; j < ARRAY_SIZE(ssi_clk_mul_table); j++) {
224 * It will set SSIWSR.CONT here, but SSICR.CKDV = 000
225 * with it is not allowed. (SSIWSR.WS_MODE with
226 * SSICR.CKDV = 000 is not allowed either).
227 * Skip it. See SSICR.CKDV
233 * this driver is assuming that
234 * system word is 32bit x chan
235 * see rsnd_ssi_init()
237 main_rate = 32 * param1 * param2 * ssi_clk_mul_table[j];
239 ret = rsnd_adg_clk_query(priv, main_rate);
252 static int rsnd_ssi_master_clk_start(struct rsnd_mod *mod,
253 struct rsnd_dai_stream *io)
255 struct rsnd_priv *priv = rsnd_io_to_priv(io);
256 struct device *dev = rsnd_priv_to_dev(priv);
257 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
258 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
259 struct rsnd_mod *ssi_parent_mod = rsnd_io_to_mod_ssip(io);
260 int chan = rsnd_runtime_channel_for_ssi(io);
262 unsigned int main_rate;
263 unsigned int rate = rsnd_io_is_play(io) ?
264 rsnd_src_get_out_rate(priv, io) :
265 rsnd_src_get_in_rate(priv, io);
267 if (!rsnd_rdai_is_clk_master(rdai))
270 if (ssi_parent_mod && !rsnd_ssi_is_parent(mod, io))
273 if (rsnd_ssi_is_multi_slave(mod, io))
276 if (ssi->usrcnt > 1) {
277 if (ssi->rate != rate) {
278 dev_err(dev, "SSI parent/child should use same rate\n");
285 main_rate = rsnd_ssi_clk_query(priv, rate, chan, &idx);
287 dev_err(dev, "unsupported clock rate\n");
291 ret = rsnd_adg_ssi_clk_try_start(mod, main_rate);
296 * SSI clock will be output contiguously
298 * This means, rsnd_ssi_master_clk_start()
299 * and rsnd_ssi_register_setup() are necessary
302 * SSICR : FORCE, SCKD, SWSD
305 ssi->cr_clk = FORCE | SWL_32 | SCKD | SWSD | CKDV(idx);
309 dev_dbg(dev, "%s[%d] outputs %u Hz\n",
311 rsnd_mod_id(mod), rate);
316 static void rsnd_ssi_master_clk_stop(struct rsnd_mod *mod,
317 struct rsnd_dai_stream *io)
319 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
320 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
321 struct rsnd_mod *ssi_parent_mod = rsnd_io_to_mod_ssip(io);
323 if (!rsnd_rdai_is_clk_master(rdai))
326 if (ssi_parent_mod && !rsnd_ssi_is_parent(mod, io))
335 rsnd_adg_ssi_clk_stop(mod);
338 static void rsnd_ssi_config_init(struct rsnd_mod *mod,
339 struct rsnd_dai_stream *io)
341 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
342 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
343 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
349 is_tdm = rsnd_runtime_is_ssi_tdm(io);
352 * always use 32bit system word.
353 * see also rsnd_ssi_master_clk_enable()
355 cr_own = FORCE | SWL_32;
357 if (rdai->bit_clk_inv)
359 if (rdai->frm_clk_inv ^ is_tdm)
361 if (rdai->data_alignment)
365 if (rsnd_io_is_play(io))
368 switch (runtime->sample_bits) {
377 if (rsnd_ssi_is_dma_mode(mod)) {
378 cr_mode = UIEN | OIEN | /* over/under run */
379 DMEN; /* DMA : enable DMA */
381 cr_mode = DIEN; /* PIO : enable Data interrupt */
387 * rsnd_ssiu_init_gen2()
395 ssi->cr_own = cr_own;
396 ssi->cr_mode = cr_mode;
400 static void rsnd_ssi_register_setup(struct rsnd_mod *mod)
402 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
404 rsnd_mod_write(mod, SSIWSR, ssi->wsr);
405 rsnd_mod_write(mod, SSICR, ssi->cr_own |
411 static void rsnd_ssi_pointer_init(struct rsnd_mod *mod,
412 struct rsnd_dai_stream *io)
414 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
415 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
419 ssi->byte_per_period = runtime->period_size *
421 samples_to_bytes(runtime, 1);
422 ssi->next_period_byte = ssi->byte_per_period;
425 static int rsnd_ssi_pointer_offset(struct rsnd_mod *mod,
426 struct rsnd_dai_stream *io,
429 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
430 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
431 int pos = ssi->byte_pos + additional;
433 pos %= (runtime->periods * ssi->byte_per_period);
438 static bool rsnd_ssi_pointer_update(struct rsnd_mod *mod,
439 struct rsnd_dai_stream *io,
442 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
444 ssi->byte_pos += byte;
446 if (ssi->byte_pos >= ssi->next_period_byte) {
447 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
450 ssi->next_period_byte += ssi->byte_per_period;
452 if (ssi->period_pos >= runtime->periods) {
455 ssi->next_period_byte = ssi->byte_per_period;
465 * SSI mod common functions
467 static int rsnd_ssi_init(struct rsnd_mod *mod,
468 struct rsnd_dai_stream *io,
469 struct rsnd_priv *priv)
471 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
474 if (!rsnd_ssi_is_run_mods(mod, io))
477 rsnd_ssi_pointer_init(mod, io);
481 rsnd_mod_power_on(mod);
483 ret = rsnd_ssi_master_clk_start(mod, io);
487 if (!rsnd_ssi_is_parent(mod, io))
488 rsnd_ssi_config_init(mod, io);
490 rsnd_ssi_register_setup(mod);
492 /* clear error status */
493 rsnd_ssi_status_clear(mod);
498 static int rsnd_ssi_quit(struct rsnd_mod *mod,
499 struct rsnd_dai_stream *io,
500 struct rsnd_priv *priv)
502 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
503 struct device *dev = rsnd_priv_to_dev(priv);
505 if (!rsnd_ssi_is_run_mods(mod, io))
509 dev_err(dev, "%s[%d] usrcnt error\n",
510 rsnd_mod_name(mod), rsnd_mod_id(mod));
514 if (!rsnd_ssi_is_parent(mod, io))
517 rsnd_ssi_master_clk_stop(mod, io);
519 rsnd_mod_power_off(mod);
526 static int rsnd_ssi_hw_params(struct rsnd_mod *mod,
527 struct rsnd_dai_stream *io,
528 struct snd_pcm_substream *substream,
529 struct snd_pcm_hw_params *params)
531 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
532 int chan = params_channels(params);
535 * snd_pcm_ops::hw_params will be called *before*
536 * snd_soc_dai_ops::trigger. Thus, ssi->usrcnt is 0
542 * It will happen if SSI has parent/child connection.
543 * it is error if child <-> parent SSI uses
544 * different channels.
546 if (ssi->chan != chan)
555 static int rsnd_ssi_start(struct rsnd_mod *mod,
556 struct rsnd_dai_stream *io,
557 struct rsnd_priv *priv)
559 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
561 if (!rsnd_ssi_is_run_mods(mod, io))
565 * EN will be set via SSIU :: SSI_CONTROL
566 * if Multi channel mode
568 if (rsnd_ssi_multi_slaves_runtime(io))
572 * EN is for data output.
573 * SSI parent EN is not needed.
575 if (rsnd_ssi_is_parent(mod, io))
580 rsnd_mod_write(mod, SSICR, ssi->cr_own |
588 static int rsnd_ssi_stop(struct rsnd_mod *mod,
589 struct rsnd_dai_stream *io,
590 struct rsnd_priv *priv)
592 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
595 if (!rsnd_ssi_is_run_mods(mod, io))
598 if (rsnd_ssi_is_parent(mod, io))
603 * and, wait all data was sent
608 rsnd_mod_write(mod, SSICR, cr | EN);
609 rsnd_ssi_status_check(mod, DIRQ);
613 * and, wait idle state
615 rsnd_mod_write(mod, SSICR, cr); /* disabled all */
616 rsnd_ssi_status_check(mod, IIRQ);
623 static int rsnd_ssi_irq(struct rsnd_mod *mod,
624 struct rsnd_dai_stream *io,
625 struct rsnd_priv *priv,
630 if (rsnd_is_gen1(priv))
633 if (rsnd_ssi_is_parent(mod, io))
636 if (!rsnd_ssi_is_run_mods(mod, io))
640 val = rsnd_ssi_is_dma_mode(mod) ? 0x0e000000 : 0x0f000000;
642 rsnd_mod_write(mod, SSI_INT_ENABLE, val);
647 static void __rsnd_ssi_interrupt(struct rsnd_mod *mod,
648 struct rsnd_dai_stream *io)
650 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
651 int is_dma = rsnd_ssi_is_dma_mode(mod);
653 bool elapsed = false;
656 spin_lock(&priv->lock);
658 /* ignore all cases if not working */
659 if (!rsnd_io_is_working(io))
660 goto rsnd_ssi_interrupt_out;
662 status = rsnd_ssi_status_get(mod);
665 if (!is_dma && (status & DIRQ)) {
666 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
667 u32 *buf = (u32 *)(runtime->dma_area +
668 rsnd_ssi_pointer_offset(mod, io, 0));
671 switch (runtime->sample_bits) {
678 * 8/16/32 data can be assesse to TDR/RDR register
679 * directly as 32bit data
680 * see rsnd_ssi_init()
682 if (rsnd_io_is_play(io))
683 rsnd_mod_write(mod, SSITDR, (*buf) << shift);
685 *buf = (rsnd_mod_read(mod, SSIRDR) >> shift);
687 elapsed = rsnd_ssi_pointer_update(mod, io, sizeof(*buf));
691 if (is_dma && (status & (UIRQ | OIRQ)))
694 rsnd_ssi_status_clear(mod);
695 rsnd_ssi_interrupt_out:
696 spin_unlock(&priv->lock);
699 rsnd_dai_period_elapsed(io);
702 snd_pcm_stop_xrun(io->substream);
706 static irqreturn_t rsnd_ssi_interrupt(int irq, void *data)
708 struct rsnd_mod *mod = data;
710 rsnd_mod_interrupt(mod, __rsnd_ssi_interrupt);
718 static void rsnd_ssi_parent_attach(struct rsnd_mod *mod,
719 struct rsnd_dai_stream *io)
721 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
722 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
724 if (!__rsnd_ssi_is_pin_sharing(mod))
727 if (!rsnd_rdai_is_clk_master(rdai))
730 switch (rsnd_mod_id(mod)) {
733 rsnd_dai_connect(rsnd_ssi_mod_get(priv, 0), io, RSND_MOD_SSIP);
736 rsnd_dai_connect(rsnd_ssi_mod_get(priv, 3), io, RSND_MOD_SSIP);
739 rsnd_dai_connect(rsnd_ssi_mod_get(priv, 7), io, RSND_MOD_SSIP);
744 static int rsnd_ssi_pcm_new(struct rsnd_mod *mod,
745 struct rsnd_dai_stream *io,
746 struct snd_soc_pcm_runtime *rtd)
749 * rsnd_rdai_is_clk_master() will be enabled after set_fmt,
750 * and, pcm_new will be called after it.
751 * This function reuse pcm_new at this point.
753 rsnd_ssi_parent_attach(mod, io);
758 static int rsnd_ssi_common_probe(struct rsnd_mod *mod,
759 struct rsnd_dai_stream *io,
760 struct rsnd_priv *priv)
762 struct device *dev = rsnd_priv_to_dev(priv);
763 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
767 * SSIP/SSIU/IRQ are not needed on
770 if (rsnd_ssi_is_multi_slave(mod, io))
774 * It can't judge ssi parent at this point
775 * see rsnd_ssi_pcm_new()
778 ret = rsnd_ssiu_attach(io, mod);
783 * SSI might be called again as PIO fallback
784 * It is easy to manual handling for IRQ request/free
786 ret = request_irq(ssi->irq,
794 static int rsnd_ssi_pointer(struct rsnd_mod *mod,
795 struct rsnd_dai_stream *io,
796 snd_pcm_uframes_t *pointer)
798 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
799 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
801 *pointer = bytes_to_frames(runtime, ssi->byte_pos);
806 static struct rsnd_mod_ops rsnd_ssi_pio_ops = {
808 .probe = rsnd_ssi_common_probe,
809 .init = rsnd_ssi_init,
810 .quit = rsnd_ssi_quit,
811 .start = rsnd_ssi_start,
812 .stop = rsnd_ssi_stop,
814 .pointer= rsnd_ssi_pointer,
815 .pcm_new = rsnd_ssi_pcm_new,
816 .hw_params = rsnd_ssi_hw_params,
819 static int rsnd_ssi_dma_probe(struct rsnd_mod *mod,
820 struct rsnd_dai_stream *io,
821 struct rsnd_priv *priv)
823 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
827 * SSIP/SSIU/IRQ/DMA are not needed on
830 if (rsnd_ssi_is_multi_slave(mod, io))
833 ret = rsnd_ssi_common_probe(mod, io, priv);
837 /* SSI probe might be called many times in MUX multi path */
838 ret = rsnd_dma_attach(io, mod, &ssi->dma);
843 static int rsnd_ssi_dma_remove(struct rsnd_mod *mod,
844 struct rsnd_dai_stream *io,
845 struct rsnd_priv *priv)
847 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
848 struct rsnd_mod *pure_ssi_mod = rsnd_io_to_mod_ssi(io);
850 /* Do nothing if non SSI (= SSI parent, multi SSI) mod */
851 if (pure_ssi_mod != mod)
854 /* PIO will request IRQ again */
855 free_irq(ssi->irq, mod);
860 static int rsnd_ssi_fallback(struct rsnd_mod *mod,
861 struct rsnd_dai_stream *io,
862 struct rsnd_priv *priv)
864 struct device *dev = rsnd_priv_to_dev(priv);
869 * SSI .probe might be called again.
871 * rsnd_rdai_continuance_probe()
873 mod->ops = &rsnd_ssi_pio_ops;
875 dev_info(dev, "%s[%d] fallback to PIO mode\n",
876 rsnd_mod_name(mod), rsnd_mod_id(mod));
881 static struct dma_chan *rsnd_ssi_dma_req(struct rsnd_dai_stream *io,
882 struct rsnd_mod *mod)
884 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
885 int is_play = rsnd_io_is_play(io);
888 if (rsnd_ssi_use_busif(io))
889 name = is_play ? "rxu" : "txu";
891 name = is_play ? "rx" : "tx";
893 return rsnd_dma_request_channel(rsnd_ssi_of_node(priv),
897 static struct rsnd_mod_ops rsnd_ssi_dma_ops = {
899 .dma_req = rsnd_ssi_dma_req,
900 .probe = rsnd_ssi_dma_probe,
901 .remove = rsnd_ssi_dma_remove,
902 .init = rsnd_ssi_init,
903 .quit = rsnd_ssi_quit,
904 .start = rsnd_ssi_start,
905 .stop = rsnd_ssi_stop,
907 .pcm_new = rsnd_ssi_pcm_new,
908 .fallback = rsnd_ssi_fallback,
909 .hw_params = rsnd_ssi_hw_params,
912 int rsnd_ssi_is_dma_mode(struct rsnd_mod *mod)
914 return mod->ops == &rsnd_ssi_dma_ops;
921 static void rsnd_ssi_connect(struct rsnd_mod *mod,
922 struct rsnd_dai_stream *io)
924 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
925 enum rsnd_mod_type types[] = {
931 enum rsnd_mod_type type;
934 /* try SSI -> SSIM1 -> SSIM2 -> SSIM3 */
935 for (i = 0; i < ARRAY_SIZE(types); i++) {
937 if (!rsnd_io_to_mod(io, type)) {
938 rsnd_dai_connect(mod, io, type);
939 rsnd_rdai_channels_set(rdai, (i + 1) * 2);
940 rsnd_rdai_ssi_lane_set(rdai, (i + 1));
946 void rsnd_parse_connect_ssi(struct rsnd_dai *rdai,
947 struct device_node *playback,
948 struct device_node *capture)
950 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
951 struct device_node *node;
952 struct device_node *np;
953 struct rsnd_mod *mod;
956 node = rsnd_ssi_of_node(priv);
961 for_each_child_of_node(node, np) {
962 mod = rsnd_ssi_mod_get(priv, i);
964 rsnd_ssi_connect(mod, &rdai->playback);
966 rsnd_ssi_connect(mod, &rdai->capture);
973 static void __rsnd_ssi_parse_hdmi_connection(struct rsnd_priv *priv,
974 struct rsnd_dai_stream *io,
975 struct device_node *remote_ep)
977 struct device *dev = rsnd_priv_to_dev(priv);
978 struct rsnd_mod *mod = rsnd_io_to_mod_ssi(io);
979 struct rsnd_ssi *ssi;
984 ssi = rsnd_mod_to_ssi(mod);
986 if (strstr(remote_ep->full_name, "hdmi0")) {
987 ssi->flags |= RSND_SSI_HDMI0;
988 dev_dbg(dev, "%s[%d] connected to HDMI0\n",
989 rsnd_mod_name(mod), rsnd_mod_id(mod));
992 if (strstr(remote_ep->full_name, "hdmi1")) {
993 ssi->flags |= RSND_SSI_HDMI1;
994 dev_dbg(dev, "%s[%d] connected to HDMI1\n",
995 rsnd_mod_name(mod), rsnd_mod_id(mod));
999 void rsnd_ssi_parse_hdmi_connection(struct rsnd_priv *priv,
1000 struct device_node *endpoint,
1003 struct rsnd_dai *rdai = rsnd_rdai_get(priv, dai_i);
1004 struct device_node *remote_ep;
1006 remote_ep = of_graph_get_remote_endpoint(endpoint);
1010 __rsnd_ssi_parse_hdmi_connection(priv, &rdai->playback, remote_ep);
1011 __rsnd_ssi_parse_hdmi_connection(priv, &rdai->capture, remote_ep);
1014 struct rsnd_mod *rsnd_ssi_mod_get(struct rsnd_priv *priv, int id)
1016 if (WARN_ON(id < 0 || id >= rsnd_ssi_nr(priv)))
1019 return rsnd_mod_get(rsnd_ssi_get(priv, id));
1022 int __rsnd_ssi_is_pin_sharing(struct rsnd_mod *mod)
1024 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
1026 return !!(rsnd_ssi_mode_flags(ssi) & RSND_SSI_CLK_PIN_SHARE);
1029 static u32 *rsnd_ssi_get_status(struct rsnd_dai_stream *io,
1030 struct rsnd_mod *mod,
1031 enum rsnd_mod_type type)
1034 * SSIP (= SSI parent) needs to be special, otherwise,
1035 * 2nd SSI might doesn't start. see also rsnd_mod_call()
1037 * We can't include parent SSI status on SSI, because we don't know
1038 * how many SSI requests parent SSI. Thus, it is localed on "io" now.
1041 * Capture : SSI1 (needs SSI0)
1043 * 1) start Capture -> SSI0/SSI1 are started.
1044 * 2) start Playback -> SSI0 doesn't work, because it is already
1045 * marked as "started" on 1)
1047 * OTOH, using each mod's status is good for MUX case.
1048 * It doesn't need to start in 2nd start
1050 * IO-0: SRC0 -> CTU1 -+-> MUX -> DVC -> SSIU -> SSI0
1052 * IO-1: SRC1 -> CTU2 -+
1054 * 1) start IO-0 -> start SSI0
1055 * 2) start IO-1 -> SSI0 doesn't need to start, because it is
1056 * already started on 1)
1058 if (type == RSND_MOD_SSIP)
1059 return &io->parent_ssi_status;
1061 return rsnd_mod_get_status(io, mod, type);
1064 int rsnd_ssi_probe(struct rsnd_priv *priv)
1066 struct device_node *node;
1067 struct device_node *np;
1068 struct device *dev = rsnd_priv_to_dev(priv);
1069 struct rsnd_mod_ops *ops;
1071 struct rsnd_ssi *ssi;
1072 char name[RSND_SSI_NAME_SIZE];
1075 node = rsnd_ssi_of_node(priv);
1079 nr = of_get_child_count(node);
1082 goto rsnd_ssi_probe_done;
1085 ssi = devm_kzalloc(dev, sizeof(*ssi) * nr, GFP_KERNEL);
1088 goto rsnd_ssi_probe_done;
1095 for_each_child_of_node(node, np) {
1096 ssi = rsnd_ssi_get(priv, i);
1098 snprintf(name, RSND_SSI_NAME_SIZE, "%s.%d",
1101 clk = devm_clk_get(dev, name);
1104 goto rsnd_ssi_probe_done;
1107 if (of_get_property(np, "shared-pin", NULL))
1108 ssi->flags |= RSND_SSI_CLK_PIN_SHARE;
1110 if (of_get_property(np, "no-busif", NULL))
1111 ssi->flags |= RSND_SSI_NO_BUSIF;
1113 ssi->irq = irq_of_parse_and_map(np, 0);
1116 goto rsnd_ssi_probe_done;
1119 if (of_property_read_bool(np, "pio-transfer"))
1120 ops = &rsnd_ssi_pio_ops;
1122 ops = &rsnd_ssi_dma_ops;
1124 ret = rsnd_mod_init(priv, rsnd_mod_get(ssi), ops, clk,
1125 rsnd_ssi_get_status, RSND_MOD_SSI, i);
1127 goto rsnd_ssi_probe_done;
1134 rsnd_ssi_probe_done:
1140 void rsnd_ssi_remove(struct rsnd_priv *priv)
1142 struct rsnd_ssi *ssi;
1145 for_each_rsnd_ssi(ssi, priv, i) {
1146 rsnd_mod_quit(rsnd_mod_get(ssi));