1 // SPDX-License-Identifier: GPL-2.0
3 // Renesas R-Car SSIU/SSI support
5 // Copyright (C) 2013 Renesas Solutions Corp.
6 // Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
9 // Kuninori Morimoto <morimoto.kuninori@renesas.com>
12 * you can enable below define if you don't need
13 * SSI interrupt status debug message when debugging
14 * see rsnd_dbg_irq_status()
16 * #define RSND_DEBUG_NO_IRQ_STATUS 1
19 #include <sound/simple_card_utils.h>
20 #include <linux/delay.h>
22 #define RSND_SSI_NAME_SIZE 16
27 #define FORCE (1 << 31) /* Fixed */
28 #define DMEN (1 << 28) /* DMA Enable */
29 #define UIEN (1 << 27) /* Underflow Interrupt Enable */
30 #define OIEN (1 << 26) /* Overflow Interrupt Enable */
31 #define IIEN (1 << 25) /* Idle Mode Interrupt Enable */
32 #define DIEN (1 << 24) /* Data Interrupt Enable */
33 #define CHNL_4 (1 << 22) /* Channels */
34 #define CHNL_6 (2 << 22) /* Channels */
35 #define CHNL_8 (3 << 22) /* Channels */
36 #define DWL_MASK (7 << 19) /* Data Word Length mask */
37 #define DWL_8 (0 << 19) /* Data Word Length */
38 #define DWL_16 (1 << 19) /* Data Word Length */
39 #define DWL_18 (2 << 19) /* Data Word Length */
40 #define DWL_20 (3 << 19) /* Data Word Length */
41 #define DWL_22 (4 << 19) /* Data Word Length */
42 #define DWL_24 (5 << 19) /* Data Word Length */
43 #define DWL_32 (6 << 19) /* Data Word Length */
45 #define SWL_32 (3 << 16) /* R/W System Word Length */
46 #define SCKD (1 << 15) /* Serial Bit Clock Direction */
47 #define SWSD (1 << 14) /* Serial WS Direction */
48 #define SCKP (1 << 13) /* Serial Bit Clock Polarity */
49 #define SWSP (1 << 12) /* Serial WS Polarity */
50 #define SDTA (1 << 10) /* Serial Data Alignment */
51 #define PDTA (1 << 9) /* Parallel Data Alignment */
52 #define DEL (1 << 8) /* Serial Data Delay */
53 #define CKDV(v) (v << 4) /* Serial Clock Division Ratio */
54 #define TRMD (1 << 1) /* Transmit/Receive Mode Select */
55 #define EN (1 << 0) /* SSI Module Enable */
60 #define UIRQ (1 << 27) /* Underflow Error Interrupt Status */
61 #define OIRQ (1 << 26) /* Overflow Error Interrupt Status */
62 #define IIRQ (1 << 25) /* Idle Mode Interrupt Status */
63 #define DIRQ (1 << 24) /* Data Interrupt Status Flag */
68 #define CONT (1 << 8) /* WS Continue Function */
69 #define WS_MODE (1 << 0) /* WS Mode */
71 #define SSI_NAME "ssi"
94 #define RSND_SSI_CLK_PIN_SHARE (1 << 0)
95 #define RSND_SSI_NO_BUSIF (1 << 1) /* SSI+DMA without BUSIF */
96 #define RSND_SSI_HDMI0 (1 << 2) /* for HDMI0 */
97 #define RSND_SSI_HDMI1 (1 << 3) /* for HDMI1 */
98 #define RSND_SSI_PROBED (1 << 4)
100 #define for_each_rsnd_ssi(pos, priv, i) \
102 (i < rsnd_ssi_nr(priv)) && \
103 ((pos) = ((struct rsnd_ssi *)(priv)->ssi + i)); \
106 #define rsnd_ssi_get(priv, id) ((struct rsnd_ssi *)(priv->ssi) + id)
107 #define rsnd_ssi_nr(priv) ((priv)->ssi_nr)
108 #define rsnd_mod_to_ssi(_mod) container_of((_mod), struct rsnd_ssi, mod)
109 #define rsnd_ssi_is_parent(ssi, io) ((ssi) == rsnd_io_to_mod_ssip(io))
110 #define rsnd_ssi_is_multi_slave(mod, io) \
111 (rsnd_ssi_multi_slaves(io) & (1 << rsnd_mod_id(mod)))
112 #define rsnd_ssi_is_run_mods(mod, io) \
113 (rsnd_ssi_run_mods(io) & (1 << rsnd_mod_id(mod)))
114 #define rsnd_ssi_can_output_clk(mod) (!__rsnd_ssi_is_pin_sharing(mod))
116 int rsnd_ssi_hdmi_port(struct rsnd_dai_stream *io)
118 struct rsnd_mod *mod = rsnd_io_to_mod_ssi(io);
119 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
121 if (rsnd_flags_has(ssi, RSND_SSI_HDMI0))
122 return RSND_SSI_HDMI_PORT0;
124 if (rsnd_flags_has(ssi, RSND_SSI_HDMI1))
125 return RSND_SSI_HDMI_PORT1;
130 int rsnd_ssi_use_busif(struct rsnd_dai_stream *io)
132 struct rsnd_mod *mod = rsnd_io_to_mod_ssi(io);
133 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
136 if (!rsnd_ssi_is_dma_mode(mod))
139 if (!(rsnd_flags_has(ssi, RSND_SSI_NO_BUSIF)))
141 if (rsnd_io_to_mod_src(io))
147 static void rsnd_ssi_status_clear(struct rsnd_mod *mod)
149 rsnd_mod_write(mod, SSISR, 0);
152 static u32 rsnd_ssi_status_get(struct rsnd_mod *mod)
154 return rsnd_mod_read(mod, SSISR);
157 static void rsnd_ssi_status_check(struct rsnd_mod *mod,
160 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
161 struct device *dev = rsnd_priv_to_dev(priv);
165 for (i = 0; i < 1024; i++) {
166 status = rsnd_ssi_status_get(mod);
173 dev_warn(dev, "%s[%d] status check failed\n",
174 rsnd_mod_name(mod), rsnd_mod_id(mod));
177 static u32 rsnd_ssi_multi_slaves(struct rsnd_dai_stream *io)
179 struct rsnd_mod *mod;
180 enum rsnd_mod_type types[] = {
188 for (i = 0; i < ARRAY_SIZE(types); i++) {
189 mod = rsnd_io_to_mod(io, types[i]);
193 mask |= 1 << rsnd_mod_id(mod);
199 static u32 rsnd_ssi_run_mods(struct rsnd_dai_stream *io)
201 struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io);
202 struct rsnd_mod *ssi_parent_mod = rsnd_io_to_mod_ssip(io);
205 mods = rsnd_ssi_multi_slaves_runtime(io) |
206 1 << rsnd_mod_id(ssi_mod);
209 mods |= 1 << rsnd_mod_id(ssi_parent_mod);
214 u32 rsnd_ssi_multi_slaves_runtime(struct rsnd_dai_stream *io)
216 if (rsnd_runtime_is_ssi_multi(io))
217 return rsnd_ssi_multi_slaves(io);
222 unsigned int rsnd_ssi_clk_query(struct rsnd_priv *priv,
223 int param1, int param2, int *idx)
225 int ssi_clk_mul_table[] = {
226 1, 2, 4, 8, 16, 6, 12,
229 unsigned int main_rate;
231 for (j = 0; j < ARRAY_SIZE(ssi_clk_mul_table); j++) {
234 * It will set SSIWSR.CONT here, but SSICR.CKDV = 000
235 * with it is not allowed. (SSIWSR.WS_MODE with
236 * SSICR.CKDV = 000 is not allowed either).
237 * Skip it. See SSICR.CKDV
243 * this driver is assuming that
244 * system word is 32bit x chan
245 * see rsnd_ssi_init()
247 main_rate = 32 * param1 * param2 * ssi_clk_mul_table[j];
249 ret = rsnd_adg_clk_query(priv, main_rate);
262 static int rsnd_ssi_master_clk_start(struct rsnd_mod *mod,
263 struct rsnd_dai_stream *io)
265 struct rsnd_priv *priv = rsnd_io_to_priv(io);
266 struct device *dev = rsnd_priv_to_dev(priv);
267 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
268 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
269 int chan = rsnd_runtime_channel_for_ssi(io);
271 unsigned int main_rate;
272 unsigned int rate = rsnd_io_is_play(io) ?
273 rsnd_src_get_out_rate(priv, io) :
274 rsnd_src_get_in_rate(priv, io);
276 if (!rsnd_rdai_is_clk_master(rdai))
279 if (!rsnd_ssi_can_output_clk(mod))
282 if (rsnd_ssi_is_multi_slave(mod, io))
285 if (ssi->usrcnt > 0) {
286 if (ssi->rate != rate) {
287 dev_err(dev, "SSI parent/child should use same rate\n");
294 main_rate = rsnd_ssi_clk_query(priv, rate, chan, &idx);
296 dev_err(dev, "unsupported clock rate\n");
300 ret = rsnd_adg_ssi_clk_try_start(mod, main_rate);
305 * SSI clock will be output contiguously
307 * This means, rsnd_ssi_master_clk_start()
308 * and rsnd_ssi_register_setup() are necessary
311 * SSICR : FORCE, SCKD, SWSD
314 ssi->cr_clk = FORCE | SWL_32 | SCKD | SWSD | CKDV(idx);
318 dev_dbg(dev, "%s[%d] outputs %u Hz\n",
320 rsnd_mod_id(mod), rate);
325 static void rsnd_ssi_master_clk_stop(struct rsnd_mod *mod,
326 struct rsnd_dai_stream *io)
328 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
329 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
331 if (!rsnd_rdai_is_clk_master(rdai))
334 if (!rsnd_ssi_can_output_clk(mod))
343 rsnd_adg_ssi_clk_stop(mod);
346 static void rsnd_ssi_config_init(struct rsnd_mod *mod,
347 struct rsnd_dai_stream *io)
349 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
350 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
351 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
352 u32 cr_own = ssi->cr_own;
353 u32 cr_mode = ssi->cr_mode;
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)
375 * We shouldn't exchange SWSP after running.
376 * This means, parent needs to care it.
378 if (rsnd_ssi_is_parent(mod, io))
381 if (rsnd_io_is_play(io))
385 switch (snd_pcm_format_width(runtime->format)) {
394 if (rsnd_ssi_is_dma_mode(mod)) {
395 cr_mode = UIEN | OIEN | /* over/under run */
396 DMEN; /* DMA : enable DMA */
398 cr_mode = DIEN; /* PIO : enable Data interrupt */
404 * rsnd_ssiu_init_gen2()
412 ssi->cr_own = cr_own;
413 ssi->cr_mode = cr_mode;
417 static void rsnd_ssi_register_setup(struct rsnd_mod *mod)
419 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
421 rsnd_mod_write(mod, SSIWSR, ssi->wsr);
422 rsnd_mod_write(mod, SSICR, ssi->cr_own |
429 * SSI mod common functions
431 static int rsnd_ssi_init(struct rsnd_mod *mod,
432 struct rsnd_dai_stream *io,
433 struct rsnd_priv *priv)
435 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
437 if (!rsnd_ssi_is_run_mods(mod, io))
442 rsnd_mod_power_on(mod);
444 rsnd_ssi_config_init(mod, io);
446 rsnd_ssi_register_setup(mod);
448 /* clear error status */
449 rsnd_ssi_status_clear(mod);
454 static int rsnd_ssi_quit(struct rsnd_mod *mod,
455 struct rsnd_dai_stream *io,
456 struct rsnd_priv *priv)
458 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
459 struct device *dev = rsnd_priv_to_dev(priv);
461 if (!rsnd_ssi_is_run_mods(mod, io))
465 dev_err(dev, "%s[%d] usrcnt error\n",
466 rsnd_mod_name(mod), rsnd_mod_id(mod));
470 rsnd_ssi_master_clk_stop(mod, io);
472 rsnd_mod_power_off(mod);
485 static int rsnd_ssi_hw_params(struct rsnd_mod *mod,
486 struct rsnd_dai_stream *io,
487 struct snd_pcm_substream *substream,
488 struct snd_pcm_hw_params *params)
490 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
491 int chan = params_channels(params);
494 * snd_pcm_ops::hw_params will be called *before*
495 * snd_soc_dai_ops::trigger. Thus, ssi->usrcnt is 0
501 * It will happen if SSI has parent/child connection.
502 * it is error if child <-> parent SSI uses
503 * different channels.
505 if (ssi->chan != chan)
514 static int rsnd_ssi_start(struct rsnd_mod *mod,
515 struct rsnd_dai_stream *io,
516 struct rsnd_priv *priv)
518 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
520 if (!rsnd_ssi_is_run_mods(mod, io))
524 * EN will be set via SSIU :: SSI_CONTROL
525 * if Multi channel mode
527 if (rsnd_ssi_multi_slaves_runtime(io))
531 * EN is for data output.
532 * SSI parent EN is not needed.
534 if (rsnd_ssi_is_parent(mod, io))
539 rsnd_mod_write(mod, SSICR, ssi->cr_own |
547 static int rsnd_ssi_stop(struct rsnd_mod *mod,
548 struct rsnd_dai_stream *io,
549 struct rsnd_priv *priv)
551 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
554 if (!rsnd_ssi_is_run_mods(mod, io))
557 if (rsnd_ssi_is_parent(mod, io))
565 * Playback: Wait all data was sent
566 * Capture: It might not receave data. Do nothing
568 if (rsnd_io_is_play(io)) {
569 rsnd_mod_write(mod, SSICR, cr | ssi->cr_en);
570 rsnd_ssi_status_check(mod, DIRQ);
573 /* In multi-SSI mode, stop is performed by setting ssi0129 in
574 * SSI_CONTROL to 0 (in rsnd_ssio_stop_gen2). Do nothing here.
576 if (rsnd_ssi_multi_slaves_runtime(io))
581 * and, wait idle state
583 rsnd_mod_write(mod, SSICR, cr); /* disabled all */
584 rsnd_ssi_status_check(mod, IIRQ);
591 static int rsnd_ssi_irq(struct rsnd_mod *mod,
592 struct rsnd_dai_stream *io,
593 struct rsnd_priv *priv,
598 if (rsnd_is_gen1(priv))
601 if (rsnd_ssi_is_parent(mod, io))
604 if (!rsnd_ssi_is_run_mods(mod, io))
608 val = rsnd_ssi_is_dma_mode(mod) ? 0x0e000000 : 0x0f000000;
610 rsnd_mod_write(mod, SSI_INT_ENABLE, val);
615 static bool rsnd_ssi_pio_interrupt(struct rsnd_mod *mod,
616 struct rsnd_dai_stream *io);
617 static void __rsnd_ssi_interrupt(struct rsnd_mod *mod,
618 struct rsnd_dai_stream *io)
620 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
621 struct device *dev = rsnd_priv_to_dev(priv);
622 int is_dma = rsnd_ssi_is_dma_mode(mod);
624 bool elapsed = false;
627 spin_lock(&priv->lock);
629 /* ignore all cases if not working */
630 if (!rsnd_io_is_working(io))
631 goto rsnd_ssi_interrupt_out;
633 status = rsnd_ssi_status_get(mod);
636 if (!is_dma && (status & DIRQ))
637 elapsed = rsnd_ssi_pio_interrupt(mod, io);
640 if (is_dma && (status & (UIRQ | OIRQ))) {
641 rsnd_dbg_irq_status(dev, "%s[%d] err status : 0x%08x\n",
642 rsnd_mod_name(mod), rsnd_mod_id(mod), status);
647 rsnd_ssi_status_clear(mod);
648 rsnd_ssi_interrupt_out:
649 spin_unlock(&priv->lock);
652 rsnd_dai_period_elapsed(io);
655 snd_pcm_stop_xrun(io->substream);
659 static irqreturn_t rsnd_ssi_interrupt(int irq, void *data)
661 struct rsnd_mod *mod = data;
663 rsnd_mod_interrupt(mod, __rsnd_ssi_interrupt);
671 static void rsnd_ssi_parent_attach(struct rsnd_mod *mod,
672 struct rsnd_dai_stream *io)
674 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
675 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
677 if (!__rsnd_ssi_is_pin_sharing(mod))
680 if (!rsnd_rdai_is_clk_master(rdai))
683 if (rsnd_ssi_is_multi_slave(mod, io))
686 switch (rsnd_mod_id(mod)) {
689 rsnd_dai_connect(rsnd_ssi_mod_get(priv, 0), io, RSND_MOD_SSIP);
692 rsnd_dai_connect(rsnd_ssi_mod_get(priv, 3), io, RSND_MOD_SSIP);
695 rsnd_dai_connect(rsnd_ssi_mod_get(priv, 7), io, RSND_MOD_SSIP);
700 static int rsnd_ssi_pcm_new(struct rsnd_mod *mod,
701 struct rsnd_dai_stream *io,
702 struct snd_soc_pcm_runtime *rtd)
705 * rsnd_rdai_is_clk_master() will be enabled after set_fmt,
706 * and, pcm_new will be called after it.
707 * This function reuse pcm_new at this point.
709 rsnd_ssi_parent_attach(mod, io);
714 static int rsnd_ssi_common_probe(struct rsnd_mod *mod,
715 struct rsnd_dai_stream *io,
716 struct rsnd_priv *priv)
718 struct device *dev = rsnd_priv_to_dev(priv);
719 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
723 * SSIP/SSIU/IRQ are not needed on
726 if (rsnd_ssi_is_multi_slave(mod, io))
730 * It can't judge ssi parent at this point
731 * see rsnd_ssi_pcm_new()
734 ret = rsnd_ssiu_attach(io, mod);
739 * SSI might be called again as PIO fallback
740 * It is easy to manual handling for IRQ request/free
742 * OTOH, this function might be called many times if platform is
743 * using MIX. It needs xxx_attach() many times on xxx_probe().
744 * Because of it, we can't control .probe/.remove calling count by
746 * But it don't need to call request_irq() many times.
747 * Let's control it by RSND_SSI_PROBED flag.
749 if (!rsnd_flags_has(ssi, RSND_SSI_PROBED)) {
750 ret = request_irq(ssi->irq,
755 rsnd_flags_set(ssi, RSND_SSI_PROBED);
761 static int rsnd_ssi_common_remove(struct rsnd_mod *mod,
762 struct rsnd_dai_stream *io,
763 struct rsnd_priv *priv)
765 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
766 struct rsnd_mod *pure_ssi_mod = rsnd_io_to_mod_ssi(io);
768 /* Do nothing if non SSI (= SSI parent, multi SSI) mod */
769 if (pure_ssi_mod != mod)
772 /* PIO will request IRQ again */
773 if (rsnd_flags_has(ssi, RSND_SSI_PROBED)) {
774 free_irq(ssi->irq, mod);
776 rsnd_flags_del(ssi, RSND_SSI_PROBED);
785 static bool rsnd_ssi_pio_interrupt(struct rsnd_mod *mod,
786 struct rsnd_dai_stream *io)
788 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
789 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
790 u32 *buf = (u32 *)(runtime->dma_area + ssi->byte_pos);
793 bool elapsed = false;
795 if (snd_pcm_format_width(runtime->format) == 24)
799 * 8/16/32 data can be assesse to TDR/RDR register
800 * directly as 32bit data
801 * see rsnd_ssi_init()
803 if (rsnd_io_is_play(io))
804 rsnd_mod_write(mod, SSITDR, (*buf) << shift);
806 *buf = (rsnd_mod_read(mod, SSIRDR) >> shift);
808 byte_pos = ssi->byte_pos + sizeof(*buf);
810 if (byte_pos >= ssi->next_period_byte) {
811 int period_pos = byte_pos / ssi->byte_per_period;
813 if (period_pos >= runtime->periods) {
818 ssi->next_period_byte = (period_pos + 1) * ssi->byte_per_period;
823 WRITE_ONCE(ssi->byte_pos, byte_pos);
828 static int rsnd_ssi_pio_init(struct rsnd_mod *mod,
829 struct rsnd_dai_stream *io,
830 struct rsnd_priv *priv)
832 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
833 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
835 if (!rsnd_ssi_is_parent(mod, io)) {
837 ssi->byte_per_period = runtime->period_size *
839 samples_to_bytes(runtime, 1);
840 ssi->next_period_byte = ssi->byte_per_period;
843 return rsnd_ssi_init(mod, io, priv);
846 static int rsnd_ssi_pio_pointer(struct rsnd_mod *mod,
847 struct rsnd_dai_stream *io,
848 snd_pcm_uframes_t *pointer)
850 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
851 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
853 *pointer = bytes_to_frames(runtime, READ_ONCE(ssi->byte_pos));
858 static int rsnd_ssi_prepare(struct rsnd_mod *mod,
859 struct rsnd_dai_stream *io,
860 struct rsnd_priv *priv)
862 return rsnd_ssi_master_clk_start(mod, io);
865 static struct rsnd_mod_ops rsnd_ssi_pio_ops = {
867 .probe = rsnd_ssi_common_probe,
868 .remove = rsnd_ssi_common_remove,
869 .init = rsnd_ssi_pio_init,
870 .quit = rsnd_ssi_quit,
871 .start = rsnd_ssi_start,
872 .stop = rsnd_ssi_stop,
874 .pointer = rsnd_ssi_pio_pointer,
875 .pcm_new = rsnd_ssi_pcm_new,
876 .hw_params = rsnd_ssi_hw_params,
877 .prepare = rsnd_ssi_prepare,
880 static int rsnd_ssi_dma_probe(struct rsnd_mod *mod,
881 struct rsnd_dai_stream *io,
882 struct rsnd_priv *priv)
887 * SSIP/SSIU/IRQ/DMA are not needed on
890 if (rsnd_ssi_is_multi_slave(mod, io))
893 ret = rsnd_ssi_common_probe(mod, io, priv);
897 /* SSI probe might be called many times in MUX multi path */
898 ret = rsnd_dma_attach(io, mod, &io->dma);
903 static int rsnd_ssi_fallback(struct rsnd_mod *mod,
904 struct rsnd_dai_stream *io,
905 struct rsnd_priv *priv)
907 struct device *dev = rsnd_priv_to_dev(priv);
912 * SSI .probe might be called again.
914 * rsnd_rdai_continuance_probe()
916 mod->ops = &rsnd_ssi_pio_ops;
918 dev_info(dev, "%s[%d] fallback to PIO mode\n",
919 rsnd_mod_name(mod), rsnd_mod_id(mod));
924 static struct dma_chan *rsnd_ssi_dma_req(struct rsnd_dai_stream *io,
925 struct rsnd_mod *mod)
927 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
928 int is_play = rsnd_io_is_play(io);
931 if (rsnd_ssi_use_busif(io))
932 name = is_play ? "rxu" : "txu";
934 name = is_play ? "rx" : "tx";
936 return rsnd_dma_request_channel(rsnd_ssi_of_node(priv),
940 static struct rsnd_mod_ops rsnd_ssi_dma_ops = {
942 .dma_req = rsnd_ssi_dma_req,
943 .probe = rsnd_ssi_dma_probe,
944 .remove = rsnd_ssi_common_remove,
945 .init = rsnd_ssi_init,
946 .quit = rsnd_ssi_quit,
947 .start = rsnd_ssi_start,
948 .stop = rsnd_ssi_stop,
950 .pcm_new = rsnd_ssi_pcm_new,
951 .fallback = rsnd_ssi_fallback,
952 .hw_params = rsnd_ssi_hw_params,
953 .prepare = rsnd_ssi_prepare,
956 int rsnd_ssi_is_dma_mode(struct rsnd_mod *mod)
958 return mod->ops == &rsnd_ssi_dma_ops;
965 static void rsnd_ssi_connect(struct rsnd_mod *mod,
966 struct rsnd_dai_stream *io)
968 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
969 enum rsnd_mod_type types[] = {
975 enum rsnd_mod_type type;
978 /* try SSI -> SSIM1 -> SSIM2 -> SSIM3 */
979 for (i = 0; i < ARRAY_SIZE(types); i++) {
981 if (!rsnd_io_to_mod(io, type)) {
982 rsnd_dai_connect(mod, io, type);
983 rsnd_rdai_channels_set(rdai, (i + 1) * 2);
984 rsnd_rdai_ssi_lane_set(rdai, (i + 1));
990 void rsnd_parse_connect_ssi(struct rsnd_dai *rdai,
991 struct device_node *playback,
992 struct device_node *capture)
994 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
995 struct device_node *node;
996 struct device_node *np;
997 struct rsnd_mod *mod;
1000 node = rsnd_ssi_of_node(priv);
1005 for_each_child_of_node(node, np) {
1006 mod = rsnd_ssi_mod_get(priv, i);
1008 rsnd_ssi_connect(mod, &rdai->playback);
1010 rsnd_ssi_connect(mod, &rdai->capture);
1017 static void __rsnd_ssi_parse_hdmi_connection(struct rsnd_priv *priv,
1018 struct rsnd_dai_stream *io,
1019 struct device_node *remote_ep)
1021 struct device *dev = rsnd_priv_to_dev(priv);
1022 struct rsnd_mod *mod = rsnd_io_to_mod_ssi(io);
1023 struct rsnd_ssi *ssi;
1024 struct device_node *remote_node = of_graph_get_port_parent(remote_ep);
1026 /* support Gen3 only */
1027 if (!rsnd_is_gen3(priv))
1033 ssi = rsnd_mod_to_ssi(mod);
1036 if (strstr(remote_node->full_name, "hdmi@fead0000")) {
1037 rsnd_flags_set(ssi, RSND_SSI_HDMI0);
1038 dev_dbg(dev, "%s[%d] connected to HDMI0\n",
1039 rsnd_mod_name(mod), rsnd_mod_id(mod));
1043 if (strstr(remote_node->full_name, "hdmi@feae0000")) {
1044 rsnd_flags_set(ssi, RSND_SSI_HDMI1);
1045 dev_dbg(dev, "%s[%d] connected to HDMI1\n",
1046 rsnd_mod_name(mod), rsnd_mod_id(mod));
1050 void rsnd_ssi_parse_hdmi_connection(struct rsnd_priv *priv,
1051 struct device_node *endpoint,
1054 struct rsnd_dai *rdai = rsnd_rdai_get(priv, dai_i);
1055 struct device_node *remote_ep;
1057 remote_ep = of_graph_get_remote_endpoint(endpoint);
1061 __rsnd_ssi_parse_hdmi_connection(priv, &rdai->playback, remote_ep);
1062 __rsnd_ssi_parse_hdmi_connection(priv, &rdai->capture, remote_ep);
1065 struct rsnd_mod *rsnd_ssi_mod_get(struct rsnd_priv *priv, int id)
1067 if (WARN_ON(id < 0 || id >= rsnd_ssi_nr(priv)))
1070 return rsnd_mod_get(rsnd_ssi_get(priv, id));
1073 int __rsnd_ssi_is_pin_sharing(struct rsnd_mod *mod)
1078 return !!(rsnd_flags_has(rsnd_mod_to_ssi(mod), RSND_SSI_CLK_PIN_SHARE));
1081 static u32 *rsnd_ssi_get_status(struct rsnd_dai_stream *io,
1082 struct rsnd_mod *mod,
1083 enum rsnd_mod_type type)
1086 * SSIP (= SSI parent) needs to be special, otherwise,
1087 * 2nd SSI might doesn't start. see also rsnd_mod_call()
1089 * We can't include parent SSI status on SSI, because we don't know
1090 * how many SSI requests parent SSI. Thus, it is localed on "io" now.
1093 * Capture : SSI1 (needs SSI0)
1095 * 1) start Capture -> SSI0/SSI1 are started.
1096 * 2) start Playback -> SSI0 doesn't work, because it is already
1097 * marked as "started" on 1)
1099 * OTOH, using each mod's status is good for MUX case.
1100 * It doesn't need to start in 2nd start
1102 * IO-0: SRC0 -> CTU1 -+-> MUX -> DVC -> SSIU -> SSI0
1104 * IO-1: SRC1 -> CTU2 -+
1106 * 1) start IO-0 -> start SSI0
1107 * 2) start IO-1 -> SSI0 doesn't need to start, because it is
1108 * already started on 1)
1110 if (type == RSND_MOD_SSIP)
1111 return &io->parent_ssi_status;
1113 return rsnd_mod_get_status(io, mod, type);
1116 int rsnd_ssi_probe(struct rsnd_priv *priv)
1118 struct device_node *node;
1119 struct device_node *np;
1120 struct device *dev = rsnd_priv_to_dev(priv);
1121 struct rsnd_mod_ops *ops;
1123 struct rsnd_ssi *ssi;
1124 char name[RSND_SSI_NAME_SIZE];
1127 node = rsnd_ssi_of_node(priv);
1131 nr = of_get_child_count(node);
1134 goto rsnd_ssi_probe_done;
1137 ssi = devm_kcalloc(dev, nr, sizeof(*ssi), GFP_KERNEL);
1140 goto rsnd_ssi_probe_done;
1147 for_each_child_of_node(node, np) {
1148 if (!of_device_is_available(np))
1151 ssi = rsnd_ssi_get(priv, i);
1153 snprintf(name, RSND_SSI_NAME_SIZE, "%s.%d",
1156 clk = devm_clk_get(dev, name);
1160 goto rsnd_ssi_probe_done;
1163 if (of_get_property(np, "shared-pin", NULL))
1164 rsnd_flags_set(ssi, RSND_SSI_CLK_PIN_SHARE);
1166 if (of_get_property(np, "no-busif", NULL))
1167 rsnd_flags_set(ssi, RSND_SSI_NO_BUSIF);
1169 ssi->irq = irq_of_parse_and_map(np, 0);
1173 goto rsnd_ssi_probe_done;
1176 if (of_property_read_bool(np, "pio-transfer"))
1177 ops = &rsnd_ssi_pio_ops;
1179 ops = &rsnd_ssi_dma_ops;
1181 ret = rsnd_mod_init(priv, rsnd_mod_get(ssi), ops, clk,
1182 rsnd_ssi_get_status, RSND_MOD_SSI, i);
1185 goto rsnd_ssi_probe_done;
1193 rsnd_ssi_probe_done:
1199 void rsnd_ssi_remove(struct rsnd_priv *priv)
1201 struct rsnd_ssi *ssi;
1204 for_each_rsnd_ssi(ssi, priv, i) {
1205 rsnd_mod_quit(rsnd_mod_get(ssi));