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_print_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 (1u << 31) /* Fixed */
28 #define DMEN (1u << 28) /* DMA Enable */
29 #define UIEN (1u << 27) /* Underflow Interrupt Enable */
30 #define OIEN (1u << 26) /* Overflow Interrupt Enable */
31 #define IIEN (1u << 25) /* Idle Mode Interrupt Enable */
32 #define DIEN (1u << 24) /* Data Interrupt Enable */
33 #define CHNL_4 (1u << 22) /* Channels */
34 #define CHNL_6 (2u << 22) /* Channels */
35 #define CHNL_8 (3u << 22) /* Channels */
36 #define DWL_MASK (7u << 19) /* Data Word Length mask */
37 #define DWL_8 (0u << 19) /* Data Word Length */
38 #define DWL_16 (1u << 19) /* Data Word Length */
39 #define DWL_18 (2u << 19) /* Data Word Length */
40 #define DWL_20 (3u << 19) /* Data Word Length */
41 #define DWL_22 (4u << 19) /* Data Word Length */
42 #define DWL_24 (5u << 19) /* Data Word Length */
43 #define DWL_32 (6u << 19) /* Data Word Length */
48 #define SWL_16 (1 << 16) /* R/W System Word Length */
49 #define SWL_24 (2 << 16) /* R/W System Word Length */
50 #define SWL_32 (3 << 16) /* R/W System Word Length */
52 #define SCKD (1 << 15) /* Serial Bit Clock Direction */
53 #define SWSD (1 << 14) /* Serial WS Direction */
54 #define SCKP (1 << 13) /* Serial Bit Clock Polarity */
55 #define SWSP (1 << 12) /* Serial WS Polarity */
56 #define SDTA (1 << 10) /* Serial Data Alignment */
57 #define PDTA (1 << 9) /* Parallel Data Alignment */
58 #define DEL (1 << 8) /* Serial Data Delay */
59 #define CKDV(v) (v << 4) /* Serial Clock Division Ratio */
60 #define TRMD (1 << 1) /* Transmit/Receive Mode Select */
61 #define EN (1 << 0) /* SSI Module Enable */
66 #define UIRQ (1 << 27) /* Underflow Error Interrupt Status */
67 #define OIRQ (1 << 26) /* Overflow Error Interrupt Status */
68 #define IIRQ (1 << 25) /* Idle Mode Interrupt Status */
69 #define DIRQ (1 << 24) /* Data Interrupt Status Flag */
74 #define CONT (1 << 8) /* WS Continue Function */
75 #define WS_MODE (1 << 0) /* WS Mode */
77 #define SSI_NAME "ssi"
100 #define RSND_SSI_CLK_PIN_SHARE (1 << 0)
101 #define RSND_SSI_NO_BUSIF (1 << 1) /* SSI+DMA without BUSIF */
102 #define RSND_SSI_PROBED (1 << 2)
104 #define for_each_rsnd_ssi(pos, priv, i) \
106 (i < rsnd_ssi_nr(priv)) && \
107 ((pos) = ((struct rsnd_ssi *)(priv)->ssi + i)); \
110 #define rsnd_ssi_get(priv, id) ((struct rsnd_ssi *)(priv->ssi) + id)
111 #define rsnd_ssi_nr(priv) ((priv)->ssi_nr)
112 #define rsnd_mod_to_ssi(_mod) container_of((_mod), struct rsnd_ssi, mod)
113 #define rsnd_ssi_is_parent(ssi, io) ((ssi) == rsnd_io_to_mod_ssip(io))
114 #define rsnd_ssi_is_multi_secondary(mod, io) \
115 (rsnd_ssi_multi_secondaries(io) & (1 << rsnd_mod_id(mod)))
116 #define rsnd_ssi_is_run_mods(mod, io) \
117 (rsnd_ssi_run_mods(io) & (1 << rsnd_mod_id(mod)))
118 #define rsnd_ssi_can_output_clk(mod) (!__rsnd_ssi_is_pin_sharing(mod))
120 int rsnd_ssi_use_busif(struct rsnd_dai_stream *io)
122 struct rsnd_mod *mod = rsnd_io_to_mod_ssi(io);
123 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
126 if (!rsnd_ssi_is_dma_mode(mod))
129 if (!(rsnd_flags_has(ssi, RSND_SSI_NO_BUSIF)))
131 if (rsnd_io_to_mod_src(io))
137 static void rsnd_ssi_status_clear(struct rsnd_mod *mod)
139 rsnd_mod_write(mod, SSISR, 0);
142 static u32 rsnd_ssi_status_get(struct rsnd_mod *mod)
144 return rsnd_mod_read(mod, SSISR);
147 static void rsnd_ssi_status_check(struct rsnd_mod *mod,
150 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
151 struct device *dev = rsnd_priv_to_dev(priv);
155 for (i = 0; i < 1024; i++) {
156 status = rsnd_ssi_status_get(mod);
163 dev_warn(dev, "%s status check failed\n", rsnd_mod_name(mod));
166 static u32 rsnd_ssi_multi_secondaries(struct rsnd_dai_stream *io)
168 static const enum rsnd_mod_type types[] = {
176 for (i = 0; i < ARRAY_SIZE(types); i++) {
177 struct rsnd_mod *mod = rsnd_io_to_mod(io, types[i]);
182 mask |= 1 << rsnd_mod_id(mod);
188 static u32 rsnd_ssi_run_mods(struct rsnd_dai_stream *io)
190 struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io);
191 struct rsnd_mod *ssi_parent_mod = rsnd_io_to_mod_ssip(io);
194 mods = rsnd_ssi_multi_secondaries_runtime(io) |
195 1 << rsnd_mod_id(ssi_mod);
198 mods |= 1 << rsnd_mod_id(ssi_parent_mod);
203 u32 rsnd_ssi_multi_secondaries_runtime(struct rsnd_dai_stream *io)
205 if (rsnd_runtime_is_multi_ssi(io))
206 return rsnd_ssi_multi_secondaries(io);
211 static u32 rsnd_rdai_width_to_swl(struct rsnd_dai *rdai)
213 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
214 struct device *dev = rsnd_priv_to_dev(priv);
215 int width = rsnd_rdai_width_get(rdai);
218 case 32: return SWL_32;
219 case 24: return SWL_24;
220 case 16: return SWL_16;
223 dev_err(dev, "unsupported slot width value: %d\n", width);
227 unsigned int rsnd_ssi_clk_query(struct rsnd_dai *rdai,
228 int param1, int param2, int *idx)
230 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
231 static const int ssi_clk_mul_table[] = {
232 1, 2, 4, 8, 16, 6, 12,
235 unsigned int main_rate;
236 int width = rsnd_rdai_width_get(rdai);
238 for (j = 0; j < ARRAY_SIZE(ssi_clk_mul_table); j++) {
241 * It will set SSIWSR.CONT here, but SSICR.CKDV = 000
242 * with it is not allowed. (SSIWSR.WS_MODE with
243 * SSICR.CKDV = 000 is not allowed either).
244 * Skip it. See SSICR.CKDV
249 main_rate = width * param1 * param2 * ssi_clk_mul_table[j];
251 ret = rsnd_adg_clk_query(priv, main_rate);
264 static int rsnd_ssi_master_clk_start(struct rsnd_mod *mod,
265 struct rsnd_dai_stream *io)
267 struct rsnd_priv *priv = rsnd_io_to_priv(io);
268 struct device *dev = rsnd_priv_to_dev(priv);
269 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
270 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
271 int chan = rsnd_runtime_channel_for_ssi(io);
273 unsigned int main_rate;
274 unsigned int rate = rsnd_io_is_play(io) ?
275 rsnd_src_get_out_rate(priv, io) :
276 rsnd_src_get_in_rate(priv, io);
278 if (!rsnd_rdai_is_clk_master(rdai))
281 if (!rsnd_ssi_can_output_clk(mod))
284 if (rsnd_ssi_is_multi_secondary(mod, io))
287 if (rsnd_runtime_is_tdm_split(io))
288 chan = rsnd_io_converted_chan(io);
290 chan = rsnd_channel_normalization(chan);
292 if (ssi->usrcnt > 0) {
293 if (ssi->rate != rate) {
294 dev_err(dev, "SSI parent/child should use same rate\n");
298 if (ssi->chan != chan) {
299 dev_err(dev, "SSI parent/child should use same chan\n");
306 main_rate = rsnd_ssi_clk_query(rdai, rate, chan, &idx);
308 dev_err(dev, "unsupported clock rate\n");
312 ret = rsnd_adg_ssi_clk_try_start(mod, main_rate);
317 * SSI clock will be output contiguously
319 * This means, rsnd_ssi_master_clk_start()
320 * and rsnd_ssi_register_setup() are necessary
323 * SSICR : FORCE, SCKD, SWSD
326 ssi->cr_clk = FORCE | rsnd_rdai_width_to_swl(rdai) |
327 SCKD | SWSD | CKDV(idx);
332 dev_dbg(dev, "%s outputs %d chan %u Hz\n",
333 rsnd_mod_name(mod), chan, rate);
338 static void rsnd_ssi_master_clk_stop(struct rsnd_mod *mod,
339 struct rsnd_dai_stream *io)
341 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
342 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
344 if (!rsnd_rdai_is_clk_master(rdai))
347 if (!rsnd_ssi_can_output_clk(mod))
357 rsnd_adg_ssi_clk_stop(mod);
360 static void rsnd_ssi_config_init(struct rsnd_mod *mod,
361 struct rsnd_dai_stream *io)
363 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
364 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
365 struct device *dev = rsnd_priv_to_dev(priv);
366 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
367 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
368 u32 cr_own = ssi->cr_own;
369 u32 cr_mode = ssi->cr_mode;
372 int is_tdm, is_tdm_split;
374 is_tdm = rsnd_runtime_is_tdm(io);
375 is_tdm_split = rsnd_runtime_is_tdm_split(io);
378 dev_dbg(dev, "TDM mode\n");
380 dev_dbg(dev, "TDM Split mode\n");
382 cr_own |= FORCE | rsnd_rdai_width_to_swl(rdai);
384 if (rdai->bit_clk_inv)
386 if (rdai->frm_clk_inv && !is_tdm)
388 if (rdai->data_alignment)
396 * rsnd_ssiu_init_gen2()
398 if (is_tdm || is_tdm_split) {
404 * We shouldn't exchange SWSP after running.
405 * This means, parent needs to care it.
407 if (rsnd_ssi_is_parent(mod, io))
410 if (rsnd_io_is_play(io))
414 width = snd_pcm_format_width(runtime->format);
417 * The SWL and DWL bits in SSICR should be fixed at 32-bit
418 * setting when TDM split mode.
420 * Operation :: TDM Format Split Function (TDM Split Mode)
440 if (rsnd_ssi_is_dma_mode(mod)) {
441 cr_mode = UIEN | OIEN | /* over/under run */
442 DMEN; /* DMA : enable DMA */
444 cr_mode = DIEN; /* PIO : enable Data interrupt */
448 ssi->cr_own = cr_own;
449 ssi->cr_mode = cr_mode;
453 static void rsnd_ssi_register_setup(struct rsnd_mod *mod)
455 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
457 rsnd_mod_write(mod, SSIWSR, ssi->wsr);
458 rsnd_mod_write(mod, SSICR, ssi->cr_own |
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 ret = rsnd_ssi_master_clk_start(mod, io);
483 ret = rsnd_mod_power_on(mod);
487 rsnd_ssi_config_init(mod, io);
489 rsnd_ssi_register_setup(mod);
491 /* clear error status */
492 rsnd_ssi_status_clear(mod);
497 static int rsnd_ssi_quit(struct rsnd_mod *mod,
498 struct rsnd_dai_stream *io,
499 struct rsnd_priv *priv)
501 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
502 struct device *dev = rsnd_priv_to_dev(priv);
504 if (!rsnd_ssi_is_run_mods(mod, io))
508 dev_err(dev, "%s usrcnt error\n", rsnd_mod_name(mod));
512 rsnd_ssi_master_clk_stop(mod, io);
514 rsnd_mod_power_off(mod);
527 static int rsnd_ssi_hw_params(struct rsnd_mod *mod,
528 struct rsnd_dai_stream *io,
529 struct snd_pcm_substream *substream,
530 struct snd_pcm_hw_params *params)
532 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
533 unsigned int fmt_width = snd_pcm_format_width(params_format(params));
535 if (fmt_width > rdai->chan_width) {
536 struct rsnd_priv *priv = rsnd_io_to_priv(io);
537 struct device *dev = rsnd_priv_to_dev(priv);
539 dev_err(dev, "invalid combination of slot-width and format-data-width\n");
546 static int rsnd_ssi_start(struct rsnd_mod *mod,
547 struct rsnd_dai_stream *io,
548 struct rsnd_priv *priv)
550 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
552 if (!rsnd_ssi_is_run_mods(mod, io))
556 * EN will be set via SSIU :: SSI_CONTROL
557 * if Multi channel mode
559 if (rsnd_ssi_multi_secondaries_runtime(io))
563 * EN is for data output.
564 * SSI parent EN is not needed.
566 if (rsnd_ssi_is_parent(mod, io))
571 rsnd_mod_write(mod, SSICR, ssi->cr_own |
579 static int rsnd_ssi_stop(struct rsnd_mod *mod,
580 struct rsnd_dai_stream *io,
581 struct rsnd_priv *priv)
583 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
586 if (!rsnd_ssi_is_run_mods(mod, io))
589 if (rsnd_ssi_is_parent(mod, io))
597 * Playback: Wait all data was sent
598 * Capture: It might not receave data. Do nothing
600 if (rsnd_io_is_play(io)) {
601 rsnd_mod_write(mod, SSICR, cr | ssi->cr_en);
602 rsnd_ssi_status_check(mod, DIRQ);
605 /* In multi-SSI mode, stop is performed by setting ssi0129 in
606 * SSI_CONTROL to 0 (in rsnd_ssio_stop_gen2). Do nothing here.
608 if (rsnd_ssi_multi_secondaries_runtime(io))
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,
629 int is_tdm, is_tdm_split;
630 int id = rsnd_mod_id(mod);
632 is_tdm = rsnd_runtime_is_tdm(io);
633 is_tdm_split = rsnd_runtime_is_tdm_split(io);
635 if (rsnd_is_gen1(priv))
638 if (rsnd_ssi_is_parent(mod, io))
641 if (!rsnd_ssi_is_run_mods(mod, io))
645 val = rsnd_ssi_is_dma_mode(mod) ? 0x0e000000 : 0x0f000000;
647 if (is_tdm || is_tdm_split) {
660 rsnd_mod_write(mod, SSI_INT_ENABLE, val);
665 static bool rsnd_ssi_pio_interrupt(struct rsnd_mod *mod,
666 struct rsnd_dai_stream *io);
667 static void __rsnd_ssi_interrupt(struct rsnd_mod *mod,
668 struct rsnd_dai_stream *io)
670 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
671 struct device *dev = rsnd_priv_to_dev(priv);
672 int is_dma = rsnd_ssi_is_dma_mode(mod);
674 bool elapsed = false;
677 spin_lock(&priv->lock);
679 /* ignore all cases if not working */
680 if (!rsnd_io_is_working(io))
681 goto rsnd_ssi_interrupt_out;
683 status = rsnd_ssi_status_get(mod);
686 if (!is_dma && (status & DIRQ))
687 elapsed = rsnd_ssi_pio_interrupt(mod, io);
690 if (is_dma && (status & (UIRQ | OIRQ))) {
691 rsnd_print_irq_status(dev, "%s err status : 0x%08x\n",
692 rsnd_mod_name(mod), status);
697 stop |= rsnd_ssiu_busif_err_status_clear(mod);
699 rsnd_ssi_status_clear(mod);
700 rsnd_ssi_interrupt_out:
701 spin_unlock(&priv->lock);
704 rsnd_dai_period_elapsed(io);
707 snd_pcm_stop_xrun(io->substream);
711 static irqreturn_t rsnd_ssi_interrupt(int irq, void *data)
713 struct rsnd_mod *mod = data;
715 rsnd_mod_interrupt(mod, __rsnd_ssi_interrupt);
720 static u32 *rsnd_ssi_get_status(struct rsnd_mod *mod,
721 struct rsnd_dai_stream *io,
722 enum rsnd_mod_type type)
725 * SSIP (= SSI parent) needs to be special, otherwise,
726 * 2nd SSI might doesn't start. see also rsnd_mod_call()
728 * We can't include parent SSI status on SSI, because we don't know
729 * how many SSI requests parent SSI. Thus, it is localed on "io" now.
732 * Capture : SSI1 (needs SSI0)
734 * 1) start Capture -> SSI0/SSI1 are started.
735 * 2) start Playback -> SSI0 doesn't work, because it is already
736 * marked as "started" on 1)
738 * OTOH, using each mod's status is good for MUX case.
739 * It doesn't need to start in 2nd start
741 * IO-0: SRC0 -> CTU1 -+-> MUX -> DVC -> SSIU -> SSI0
743 * IO-1: SRC1 -> CTU2 -+
745 * 1) start IO-0 -> start SSI0
746 * 2) start IO-1 -> SSI0 doesn't need to start, because it is
747 * already started on 1)
749 if (type == RSND_MOD_SSIP)
750 return &io->parent_ssi_status;
752 return rsnd_mod_get_status(mod, io, type);
758 static void rsnd_ssi_parent_attach(struct rsnd_mod *mod,
759 struct rsnd_dai_stream *io)
761 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
762 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
764 if (!__rsnd_ssi_is_pin_sharing(mod))
767 if (!rsnd_rdai_is_clk_master(rdai))
770 if (rsnd_ssi_is_multi_secondary(mod, io))
773 switch (rsnd_mod_id(mod)) {
777 rsnd_dai_connect(rsnd_ssi_mod_get(priv, 0), io, RSND_MOD_SSIP);
780 rsnd_dai_connect(rsnd_ssi_mod_get(priv, 3), io, RSND_MOD_SSIP);
783 rsnd_dai_connect(rsnd_ssi_mod_get(priv, 7), io, RSND_MOD_SSIP);
788 static int rsnd_ssi_pcm_new(struct rsnd_mod *mod,
789 struct rsnd_dai_stream *io,
790 struct snd_soc_pcm_runtime *rtd)
793 * rsnd_rdai_is_clk_master() will be enabled after set_fmt,
794 * and, pcm_new will be called after it.
795 * This function reuse pcm_new at this point.
797 rsnd_ssi_parent_attach(mod, io);
802 static int rsnd_ssi_common_probe(struct rsnd_mod *mod,
803 struct rsnd_dai_stream *io,
804 struct rsnd_priv *priv)
806 struct device *dev = rsnd_priv_to_dev(priv);
807 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
811 * SSIP/SSIU/IRQ are not needed on
812 * SSI Multi secondaries
814 if (rsnd_ssi_is_multi_secondary(mod, io))
818 * It can't judge ssi parent at this point
819 * see rsnd_ssi_pcm_new()
823 * SSI might be called again as PIO fallback
824 * It is easy to manual handling for IRQ request/free
826 * OTOH, this function might be called many times if platform is
827 * using MIX. It needs xxx_attach() many times on xxx_probe().
828 * Because of it, we can't control .probe/.remove calling count by
830 * But it don't need to call request_irq() many times.
831 * Let's control it by RSND_SSI_PROBED flag.
833 if (!rsnd_flags_has(ssi, RSND_SSI_PROBED)) {
834 ret = request_irq(ssi->irq,
839 rsnd_flags_set(ssi, RSND_SSI_PROBED);
845 static int rsnd_ssi_common_remove(struct rsnd_mod *mod,
846 struct rsnd_dai_stream *io,
847 struct rsnd_priv *priv)
849 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
850 struct rsnd_mod *pure_ssi_mod = rsnd_io_to_mod_ssi(io);
852 /* Do nothing if non SSI (= SSI parent, multi SSI) mod */
853 if (pure_ssi_mod != mod)
856 /* PIO will request IRQ again */
857 if (rsnd_flags_has(ssi, RSND_SSI_PROBED)) {
858 free_irq(ssi->irq, mod);
860 rsnd_flags_del(ssi, RSND_SSI_PROBED);
869 static bool rsnd_ssi_pio_interrupt(struct rsnd_mod *mod,
870 struct rsnd_dai_stream *io)
872 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
873 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
874 u32 *buf = (u32 *)(runtime->dma_area + ssi->byte_pos);
877 bool elapsed = false;
879 if (snd_pcm_format_width(runtime->format) == 24)
883 * 8/16/32 data can be assesse to TDR/RDR register
884 * directly as 32bit data
885 * see rsnd_ssi_init()
887 if (rsnd_io_is_play(io))
888 rsnd_mod_write(mod, SSITDR, (*buf) << shift);
890 *buf = (rsnd_mod_read(mod, SSIRDR) >> shift);
892 byte_pos = ssi->byte_pos + sizeof(*buf);
894 if (byte_pos >= ssi->next_period_byte) {
895 int period_pos = byte_pos / ssi->byte_per_period;
897 if (period_pos >= runtime->periods) {
902 ssi->next_period_byte = (period_pos + 1) * ssi->byte_per_period;
907 WRITE_ONCE(ssi->byte_pos, byte_pos);
912 static int rsnd_ssi_pio_init(struct rsnd_mod *mod,
913 struct rsnd_dai_stream *io,
914 struct rsnd_priv *priv)
916 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
917 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
919 if (!rsnd_ssi_is_parent(mod, io)) {
921 ssi->byte_per_period = runtime->period_size *
923 samples_to_bytes(runtime, 1);
924 ssi->next_period_byte = ssi->byte_per_period;
927 return rsnd_ssi_init(mod, io, priv);
930 static int rsnd_ssi_pio_pointer(struct rsnd_mod *mod,
931 struct rsnd_dai_stream *io,
932 snd_pcm_uframes_t *pointer)
934 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
935 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
937 *pointer = bytes_to_frames(runtime, READ_ONCE(ssi->byte_pos));
942 static struct rsnd_mod_ops rsnd_ssi_pio_ops = {
944 .probe = rsnd_ssi_common_probe,
945 .remove = rsnd_ssi_common_remove,
946 .init = rsnd_ssi_pio_init,
947 .quit = rsnd_ssi_quit,
948 .start = rsnd_ssi_start,
949 .stop = rsnd_ssi_stop,
951 .pointer = rsnd_ssi_pio_pointer,
952 .pcm_new = rsnd_ssi_pcm_new,
953 .hw_params = rsnd_ssi_hw_params,
954 .get_status = rsnd_ssi_get_status,
957 static int rsnd_ssi_dma_probe(struct rsnd_mod *mod,
958 struct rsnd_dai_stream *io,
959 struct rsnd_priv *priv)
964 * SSIP/SSIU/IRQ/DMA are not needed on
965 * SSI Multi secondaries
967 if (rsnd_ssi_is_multi_secondary(mod, io))
970 ret = rsnd_ssi_common_probe(mod, io, priv);
974 /* SSI probe might be called many times in MUX multi path */
975 ret = rsnd_dma_attach(io, mod, &io->dma);
980 static int rsnd_ssi_fallback(struct rsnd_mod *mod,
981 struct rsnd_dai_stream *io,
982 struct rsnd_priv *priv)
984 struct device *dev = rsnd_priv_to_dev(priv);
989 * SSI .probe might be called again.
991 * rsnd_rdai_continuance_probe()
993 mod->ops = &rsnd_ssi_pio_ops;
995 dev_info(dev, "%s fallback to PIO mode\n", rsnd_mod_name(mod));
1000 static struct dma_chan *rsnd_ssi_dma_req(struct rsnd_dai_stream *io,
1001 struct rsnd_mod *mod)
1003 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
1004 int is_play = rsnd_io_is_play(io);
1008 * It should use "rcar_sound,ssiu" on DT.
1009 * But, we need to keep compatibility for old version.
1011 * If it has "rcar_sound.ssiu", it will be used.
1012 * If not, "rcar_sound.ssi" will be used.
1014 * rsnd_ssiu_dma_req()
1015 * rsnd_dma_of_path()
1018 if (rsnd_ssi_use_busif(io))
1019 name = is_play ? "rxu" : "txu";
1021 name = is_play ? "rx" : "tx";
1023 return rsnd_dma_request_channel(rsnd_ssi_of_node(priv),
1024 SSI_NAME, mod, name);
1027 #ifdef CONFIG_DEBUG_FS
1028 static void rsnd_ssi_debug_info(struct seq_file *m,
1029 struct rsnd_dai_stream *io,
1030 struct rsnd_mod *mod)
1032 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
1033 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
1035 seq_printf(m, "clock: %s\n", rsnd_rdai_is_clk_master(rdai) ?
1036 "provider" : "consumer");
1037 seq_printf(m, "bit_clk_inv: %d\n", rdai->bit_clk_inv);
1038 seq_printf(m, "frm_clk_inv: %d\n", rdai->frm_clk_inv);
1039 seq_printf(m, "pin share: %d\n", __rsnd_ssi_is_pin_sharing(mod));
1040 seq_printf(m, "can out clk: %d\n", rsnd_ssi_can_output_clk(mod));
1041 seq_printf(m, "multi secondary: %d\n", rsnd_ssi_is_multi_secondary(mod, io));
1042 seq_printf(m, "tdm: %d, %d\n", rsnd_runtime_is_tdm(io),
1043 rsnd_runtime_is_tdm_split(io));
1044 seq_printf(m, "chan: %d\n", ssi->chan);
1045 seq_printf(m, "user: %d\n", ssi->usrcnt);
1047 rsnd_debugfs_mod_reg_show(m, mod, RSND_GEN2_SSI,
1048 rsnd_mod_id(mod) * 0x40, 0x40);
1050 #define DEBUG_INFO .debug_info = rsnd_ssi_debug_info
1055 static struct rsnd_mod_ops rsnd_ssi_dma_ops = {
1057 .dma_req = rsnd_ssi_dma_req,
1058 .probe = rsnd_ssi_dma_probe,
1059 .remove = rsnd_ssi_common_remove,
1060 .init = rsnd_ssi_init,
1061 .quit = rsnd_ssi_quit,
1062 .start = rsnd_ssi_start,
1063 .stop = rsnd_ssi_stop,
1064 .irq = rsnd_ssi_irq,
1065 .pcm_new = rsnd_ssi_pcm_new,
1066 .fallback = rsnd_ssi_fallback,
1067 .hw_params = rsnd_ssi_hw_params,
1068 .get_status = rsnd_ssi_get_status,
1072 int rsnd_ssi_is_dma_mode(struct rsnd_mod *mod)
1074 return mod->ops == &rsnd_ssi_dma_ops;
1080 static void rsnd_ssi_connect(struct rsnd_mod *mod,
1081 struct rsnd_dai_stream *io)
1083 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
1084 static const enum rsnd_mod_type types[] = {
1090 enum rsnd_mod_type type;
1093 /* try SSI -> SSIM1 -> SSIM2 -> SSIM3 */
1094 for (i = 0; i < ARRAY_SIZE(types); i++) {
1096 if (!rsnd_io_to_mod(io, type)) {
1097 rsnd_dai_connect(mod, io, type);
1098 rsnd_rdai_channels_set(rdai, (i + 1) * 2);
1099 rsnd_rdai_ssi_lane_set(rdai, (i + 1));
1105 void rsnd_parse_connect_ssi(struct rsnd_dai *rdai,
1106 struct device_node *playback,
1107 struct device_node *capture)
1109 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
1110 struct device *dev = rsnd_priv_to_dev(priv);
1111 struct device_node *node;
1112 struct device_node *np;
1115 node = rsnd_ssi_of_node(priv);
1120 for_each_child_of_node(node, np) {
1121 struct rsnd_mod *mod;
1123 i = rsnd_node_fixed_index(dev, np, SSI_NAME, i);
1129 mod = rsnd_ssi_mod_get(priv, i);
1132 rsnd_ssi_connect(mod, &rdai->playback);
1134 rsnd_ssi_connect(mod, &rdai->capture);
1141 struct rsnd_mod *rsnd_ssi_mod_get(struct rsnd_priv *priv, int id)
1143 if (WARN_ON(id < 0 || id >= rsnd_ssi_nr(priv)))
1146 return rsnd_mod_get(rsnd_ssi_get(priv, id));
1149 int __rsnd_ssi_is_pin_sharing(struct rsnd_mod *mod)
1154 return !!(rsnd_flags_has(rsnd_mod_to_ssi(mod), RSND_SSI_CLK_PIN_SHARE));
1157 int rsnd_ssi_probe(struct rsnd_priv *priv)
1159 struct device_node *node;
1160 struct device_node *np;
1161 struct device *dev = rsnd_priv_to_dev(priv);
1162 struct rsnd_mod_ops *ops;
1164 struct rsnd_ssi *ssi;
1165 char name[RSND_SSI_NAME_SIZE];
1168 node = rsnd_ssi_of_node(priv);
1172 nr = rsnd_node_count(priv, node, SSI_NAME);
1175 goto rsnd_ssi_probe_done;
1178 ssi = devm_kcalloc(dev, nr, sizeof(*ssi), GFP_KERNEL);
1181 goto rsnd_ssi_probe_done;
1188 for_each_child_of_node(node, np) {
1189 if (!of_device_is_available(np))
1192 i = rsnd_node_fixed_index(dev, np, SSI_NAME, i);
1196 goto rsnd_ssi_probe_done;
1199 ssi = rsnd_ssi_get(priv, i);
1201 snprintf(name, RSND_SSI_NAME_SIZE, "%s.%d",
1204 clk = devm_clk_get(dev, name);
1208 goto rsnd_ssi_probe_done;
1211 if (of_property_read_bool(np, "shared-pin"))
1212 rsnd_flags_set(ssi, RSND_SSI_CLK_PIN_SHARE);
1214 if (of_property_read_bool(np, "no-busif"))
1215 rsnd_flags_set(ssi, RSND_SSI_NO_BUSIF);
1217 ssi->irq = irq_of_parse_and_map(np, 0);
1221 goto rsnd_ssi_probe_done;
1224 if (of_property_read_bool(np, "pio-transfer"))
1225 ops = &rsnd_ssi_pio_ops;
1227 ops = &rsnd_ssi_dma_ops;
1229 ret = rsnd_mod_init(priv, rsnd_mod_get(ssi), ops, clk,
1233 goto rsnd_ssi_probe_done;
1241 rsnd_ssi_probe_done:
1247 void rsnd_ssi_remove(struct rsnd_priv *priv)
1249 struct rsnd_ssi *ssi;
1252 for_each_rsnd_ssi(ssi, priv, i) {
1253 rsnd_mod_quit(rsnd_mod_get(ssi));