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_is_parent(ssi, io) ((ssi) == rsnd_io_to_mod_ssip(io))
105 #define rsnd_ssi_is_multi_slave(mod, io) \
106 (rsnd_ssi_multi_slaves(io) & (1 << rsnd_mod_id(mod)))
107 #define rsnd_ssi_is_run_mods(mod, io) \
108 (rsnd_ssi_run_mods(io) & (1 << rsnd_mod_id(mod)))
109 #define rsnd_ssi_can_output_clk(mod) (!__rsnd_ssi_is_pin_sharing(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_flags_has(ssi
, RSND_SSI_HDMI0
))
117 return RSND_SSI_HDMI_PORT0
;
119 if (rsnd_flags_has(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_flags_has(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
);
200 mods
= rsnd_ssi_multi_slaves_runtime(io
) |
201 1 << rsnd_mod_id(ssi_mod
);
204 mods
|= 1 << rsnd_mod_id(ssi_parent_mod
);
209 u32
rsnd_ssi_multi_slaves_runtime(struct rsnd_dai_stream
*io
)
211 if (rsnd_runtime_is_ssi_multi(io
))
212 return rsnd_ssi_multi_slaves(io
);
217 unsigned int rsnd_ssi_clk_query(struct rsnd_priv
*priv
,
218 int param1
, int param2
, int *idx
)
220 int ssi_clk_mul_table
[] = {
221 1, 2, 4, 8, 16, 6, 12,
224 unsigned int main_rate
;
226 for (j
= 0; j
< ARRAY_SIZE(ssi_clk_mul_table
); j
++) {
229 * It will set SSIWSR.CONT here, but SSICR.CKDV = 000
230 * with it is not allowed. (SSIWSR.WS_MODE with
231 * SSICR.CKDV = 000 is not allowed either).
232 * Skip it. See SSICR.CKDV
238 * this driver is assuming that
239 * system word is 32bit x chan
240 * see rsnd_ssi_init()
242 main_rate
= 32 * param1
* param2
* ssi_clk_mul_table
[j
];
244 ret
= rsnd_adg_clk_query(priv
, main_rate
);
257 static int rsnd_ssi_master_clk_start(struct rsnd_mod
*mod
,
258 struct rsnd_dai_stream
*io
)
260 struct rsnd_priv
*priv
= rsnd_io_to_priv(io
);
261 struct device
*dev
= rsnd_priv_to_dev(priv
);
262 struct rsnd_dai
*rdai
= rsnd_io_to_rdai(io
);
263 struct rsnd_ssi
*ssi
= rsnd_mod_to_ssi(mod
);
264 int chan
= rsnd_runtime_channel_for_ssi(io
);
266 unsigned int main_rate
;
267 unsigned int rate
= rsnd_io_is_play(io
) ?
268 rsnd_src_get_out_rate(priv
, io
) :
269 rsnd_src_get_in_rate(priv
, io
);
271 if (!rsnd_rdai_is_clk_master(rdai
))
274 if (!rsnd_ssi_can_output_clk(mod
))
277 if (rsnd_ssi_is_multi_slave(mod
, io
))
280 if (ssi
->usrcnt
> 1) {
281 if (ssi
->rate
!= rate
) {
282 dev_err(dev
, "SSI parent/child should use same rate\n");
289 main_rate
= rsnd_ssi_clk_query(priv
, rate
, chan
, &idx
);
291 dev_err(dev
, "unsupported clock rate\n");
295 ret
= rsnd_adg_ssi_clk_try_start(mod
, main_rate
);
300 * SSI clock will be output contiguously
302 * This means, rsnd_ssi_master_clk_start()
303 * and rsnd_ssi_register_setup() are necessary
306 * SSICR : FORCE, SCKD, SWSD
309 ssi
->cr_clk
= FORCE
| SWL_32
| SCKD
| SWSD
| CKDV(idx
);
313 dev_dbg(dev
, "%s[%d] outputs %u Hz\n",
315 rsnd_mod_id(mod
), rate
);
320 static void rsnd_ssi_master_clk_stop(struct rsnd_mod
*mod
,
321 struct rsnd_dai_stream
*io
)
323 struct rsnd_dai
*rdai
= rsnd_io_to_rdai(io
);
324 struct rsnd_ssi
*ssi
= rsnd_mod_to_ssi(mod
);
326 if (!rsnd_rdai_is_clk_master(rdai
))
329 if (!rsnd_ssi_can_output_clk(mod
))
338 rsnd_adg_ssi_clk_stop(mod
);
341 static void rsnd_ssi_config_init(struct rsnd_mod
*mod
,
342 struct rsnd_dai_stream
*io
)
344 struct rsnd_dai
*rdai
= rsnd_io_to_rdai(io
);
345 struct snd_pcm_runtime
*runtime
= rsnd_io_to_runtime(io
);
346 struct rsnd_ssi
*ssi
= rsnd_mod_to_ssi(mod
);
352 if (rsnd_ssi_is_parent(mod
, io
))
355 is_tdm
= rsnd_runtime_is_ssi_tdm(io
);
358 * always use 32bit system word.
359 * see also rsnd_ssi_master_clk_enable()
361 cr_own
= FORCE
| SWL_32
;
363 if (rdai
->bit_clk_inv
)
365 if (rdai
->frm_clk_inv
^ is_tdm
)
367 if (rdai
->data_alignment
)
371 if (rsnd_io_is_play(io
))
374 switch (runtime
->sample_bits
) {
383 if (rsnd_ssi_is_dma_mode(mod
)) {
384 cr_mode
= UIEN
| OIEN
| /* over/under run */
385 DMEN
; /* DMA : enable DMA */
387 cr_mode
= DIEN
; /* PIO : enable Data interrupt */
393 * rsnd_ssiu_init_gen2()
401 ssi
->cr_own
= cr_own
;
402 ssi
->cr_mode
= cr_mode
;
406 static void rsnd_ssi_register_setup(struct rsnd_mod
*mod
)
408 struct rsnd_ssi
*ssi
= rsnd_mod_to_ssi(mod
);
410 rsnd_mod_write(mod
, SSIWSR
, ssi
->wsr
);
411 rsnd_mod_write(mod
, SSICR
, ssi
->cr_own
|
417 static void rsnd_ssi_pointer_init(struct rsnd_mod
*mod
,
418 struct rsnd_dai_stream
*io
)
420 struct rsnd_ssi
*ssi
= rsnd_mod_to_ssi(mod
);
421 struct snd_pcm_runtime
*runtime
= rsnd_io_to_runtime(io
);
425 ssi
->byte_per_period
= runtime
->period_size
*
427 samples_to_bytes(runtime
, 1);
428 ssi
->next_period_byte
= ssi
->byte_per_period
;
431 static int rsnd_ssi_pointer_offset(struct rsnd_mod
*mod
,
432 struct rsnd_dai_stream
*io
,
435 struct rsnd_ssi
*ssi
= rsnd_mod_to_ssi(mod
);
436 struct snd_pcm_runtime
*runtime
= rsnd_io_to_runtime(io
);
437 int pos
= ssi
->byte_pos
+ additional
;
439 pos
%= (runtime
->periods
* ssi
->byte_per_period
);
444 static bool rsnd_ssi_pointer_update(struct rsnd_mod
*mod
,
445 struct rsnd_dai_stream
*io
,
448 struct rsnd_ssi
*ssi
= rsnd_mod_to_ssi(mod
);
452 byte_pos
= ssi
->byte_pos
+ byte
;
454 if (byte_pos
>= ssi
->next_period_byte
) {
455 struct snd_pcm_runtime
*runtime
= rsnd_io_to_runtime(io
);
458 ssi
->next_period_byte
+= ssi
->byte_per_period
;
460 if (ssi
->period_pos
>= runtime
->periods
) {
463 ssi
->next_period_byte
= ssi
->byte_per_period
;
469 WRITE_ONCE(ssi
->byte_pos
, byte_pos
);
475 * SSI mod common functions
477 static int rsnd_ssi_init(struct rsnd_mod
*mod
,
478 struct rsnd_dai_stream
*io
,
479 struct rsnd_priv
*priv
)
481 struct rsnd_ssi
*ssi
= rsnd_mod_to_ssi(mod
);
484 if (!rsnd_ssi_is_run_mods(mod
, io
))
487 rsnd_ssi_pointer_init(mod
, io
);
491 rsnd_mod_power_on(mod
);
493 ret
= rsnd_ssi_master_clk_start(mod
, io
);
497 rsnd_ssi_config_init(mod
, io
);
499 rsnd_ssi_register_setup(mod
);
501 /* clear error status */
502 rsnd_ssi_status_clear(mod
);
507 static int rsnd_ssi_quit(struct rsnd_mod
*mod
,
508 struct rsnd_dai_stream
*io
,
509 struct rsnd_priv
*priv
)
511 struct rsnd_ssi
*ssi
= rsnd_mod_to_ssi(mod
);
512 struct device
*dev
= rsnd_priv_to_dev(priv
);
514 if (!rsnd_ssi_is_run_mods(mod
, io
))
518 dev_err(dev
, "%s[%d] usrcnt error\n",
519 rsnd_mod_name(mod
), rsnd_mod_id(mod
));
523 if (!rsnd_ssi_is_parent(mod
, io
))
526 rsnd_ssi_master_clk_stop(mod
, io
);
528 rsnd_mod_power_off(mod
);
535 static int rsnd_ssi_hw_params(struct rsnd_mod
*mod
,
536 struct rsnd_dai_stream
*io
,
537 struct snd_pcm_substream
*substream
,
538 struct snd_pcm_hw_params
*params
)
540 struct rsnd_ssi
*ssi
= rsnd_mod_to_ssi(mod
);
541 int chan
= params_channels(params
);
544 * snd_pcm_ops::hw_params will be called *before*
545 * snd_soc_dai_ops::trigger. Thus, ssi->usrcnt is 0
551 * It will happen if SSI has parent/child connection.
552 * it is error if child <-> parent SSI uses
553 * different channels.
555 if (ssi
->chan
!= chan
)
564 static int rsnd_ssi_start(struct rsnd_mod
*mod
,
565 struct rsnd_dai_stream
*io
,
566 struct rsnd_priv
*priv
)
568 struct rsnd_ssi
*ssi
= rsnd_mod_to_ssi(mod
);
570 if (!rsnd_ssi_is_run_mods(mod
, io
))
574 * EN will be set via SSIU :: SSI_CONTROL
575 * if Multi channel mode
577 if (rsnd_ssi_multi_slaves_runtime(io
))
581 * EN is for data output.
582 * SSI parent EN is not needed.
584 if (rsnd_ssi_is_parent(mod
, io
))
589 rsnd_mod_write(mod
, SSICR
, ssi
->cr_own
|
597 static int rsnd_ssi_stop(struct rsnd_mod
*mod
,
598 struct rsnd_dai_stream
*io
,
599 struct rsnd_priv
*priv
)
601 struct rsnd_ssi
*ssi
= rsnd_mod_to_ssi(mod
);
604 if (!rsnd_ssi_is_run_mods(mod
, io
))
607 if (rsnd_ssi_is_parent(mod
, io
))
615 * Playback: Wait all data was sent
616 * Capture: It might not receave data. Do nothing
618 if (rsnd_io_is_play(io
)) {
619 rsnd_mod_write(mod
, SSICR
, cr
| EN
);
620 rsnd_ssi_status_check(mod
, DIRQ
);
625 * and, wait idle state
627 rsnd_mod_write(mod
, SSICR
, cr
); /* disabled all */
628 rsnd_ssi_status_check(mod
, IIRQ
);
635 static int rsnd_ssi_irq(struct rsnd_mod
*mod
,
636 struct rsnd_dai_stream
*io
,
637 struct rsnd_priv
*priv
,
642 if (rsnd_is_gen1(priv
))
645 if (rsnd_ssi_is_parent(mod
, io
))
648 if (!rsnd_ssi_is_run_mods(mod
, io
))
652 val
= rsnd_ssi_is_dma_mode(mod
) ? 0x0e000000 : 0x0f000000;
654 rsnd_mod_write(mod
, SSI_INT_ENABLE
, val
);
659 static void __rsnd_ssi_interrupt(struct rsnd_mod
*mod
,
660 struct rsnd_dai_stream
*io
)
662 struct rsnd_priv
*priv
= rsnd_mod_to_priv(mod
);
663 int is_dma
= rsnd_ssi_is_dma_mode(mod
);
665 bool elapsed
= false;
668 spin_lock(&priv
->lock
);
670 /* ignore all cases if not working */
671 if (!rsnd_io_is_working(io
))
672 goto rsnd_ssi_interrupt_out
;
674 status
= rsnd_ssi_status_get(mod
);
677 if (!is_dma
&& (status
& DIRQ
)) {
678 struct snd_pcm_runtime
*runtime
= rsnd_io_to_runtime(io
);
679 u32
*buf
= (u32
*)(runtime
->dma_area
+
680 rsnd_ssi_pointer_offset(mod
, io
, 0));
683 switch (runtime
->sample_bits
) {
690 * 8/16/32 data can be assesse to TDR/RDR register
691 * directly as 32bit data
692 * see rsnd_ssi_init()
694 if (rsnd_io_is_play(io
))
695 rsnd_mod_write(mod
, SSITDR
, (*buf
) << shift
);
697 *buf
= (rsnd_mod_read(mod
, SSIRDR
) >> shift
);
699 elapsed
= rsnd_ssi_pointer_update(mod
, io
, sizeof(*buf
));
703 if (is_dma
&& (status
& (UIRQ
| OIRQ
)))
706 rsnd_ssi_status_clear(mod
);
707 rsnd_ssi_interrupt_out
:
708 spin_unlock(&priv
->lock
);
711 rsnd_dai_period_elapsed(io
);
714 snd_pcm_stop_xrun(io
->substream
);
718 static irqreturn_t
rsnd_ssi_interrupt(int irq
, void *data
)
720 struct rsnd_mod
*mod
= data
;
722 rsnd_mod_interrupt(mod
, __rsnd_ssi_interrupt
);
730 static void rsnd_ssi_parent_attach(struct rsnd_mod
*mod
,
731 struct rsnd_dai_stream
*io
)
733 struct rsnd_dai
*rdai
= rsnd_io_to_rdai(io
);
734 struct rsnd_priv
*priv
= rsnd_mod_to_priv(mod
);
736 if (!__rsnd_ssi_is_pin_sharing(mod
))
739 if (!rsnd_rdai_is_clk_master(rdai
))
742 switch (rsnd_mod_id(mod
)) {
745 rsnd_dai_connect(rsnd_ssi_mod_get(priv
, 0), io
, RSND_MOD_SSIP
);
748 rsnd_dai_connect(rsnd_ssi_mod_get(priv
, 3), io
, RSND_MOD_SSIP
);
751 rsnd_dai_connect(rsnd_ssi_mod_get(priv
, 7), io
, RSND_MOD_SSIP
);
756 static int rsnd_ssi_pcm_new(struct rsnd_mod
*mod
,
757 struct rsnd_dai_stream
*io
,
758 struct snd_soc_pcm_runtime
*rtd
)
761 * rsnd_rdai_is_clk_master() will be enabled after set_fmt,
762 * and, pcm_new will be called after it.
763 * This function reuse pcm_new at this point.
765 rsnd_ssi_parent_attach(mod
, io
);
770 static int rsnd_ssi_common_probe(struct rsnd_mod
*mod
,
771 struct rsnd_dai_stream
*io
,
772 struct rsnd_priv
*priv
)
774 struct device
*dev
= rsnd_priv_to_dev(priv
);
775 struct rsnd_ssi
*ssi
= rsnd_mod_to_ssi(mod
);
779 * SSIP/SSIU/IRQ are not needed on
782 if (rsnd_ssi_is_multi_slave(mod
, io
))
786 * It can't judge ssi parent at this point
787 * see rsnd_ssi_pcm_new()
790 ret
= rsnd_ssiu_attach(io
, mod
);
795 * SSI might be called again as PIO fallback
796 * It is easy to manual handling for IRQ request/free
798 * OTOH, this function might be called many times if platform is
799 * using MIX. It needs xxx_attach() many times on xxx_probe().
800 * Because of it, we can't control .probe/.remove calling count by
802 * But it don't need to call request_irq() many times.
803 * Let's control it by RSND_SSI_PROBED flag.
805 if (!rsnd_flags_has(ssi
, RSND_SSI_PROBED
)) {
806 ret
= request_irq(ssi
->irq
,
811 rsnd_flags_set(ssi
, RSND_SSI_PROBED
);
817 static int rsnd_ssi_common_remove(struct rsnd_mod
*mod
,
818 struct rsnd_dai_stream
*io
,
819 struct rsnd_priv
*priv
)
821 struct rsnd_ssi
*ssi
= rsnd_mod_to_ssi(mod
);
822 struct rsnd_mod
*pure_ssi_mod
= rsnd_io_to_mod_ssi(io
);
824 /* Do nothing if non SSI (= SSI parent, multi SSI) mod */
825 if (pure_ssi_mod
!= mod
)
828 /* PIO will request IRQ again */
829 if (rsnd_flags_has(ssi
, RSND_SSI_PROBED
)) {
830 free_irq(ssi
->irq
, mod
);
832 rsnd_flags_del(ssi
, RSND_SSI_PROBED
);
838 static int rsnd_ssi_pointer(struct rsnd_mod
*mod
,
839 struct rsnd_dai_stream
*io
,
840 snd_pcm_uframes_t
*pointer
)
842 struct rsnd_ssi
*ssi
= rsnd_mod_to_ssi(mod
);
843 struct snd_pcm_runtime
*runtime
= rsnd_io_to_runtime(io
);
845 *pointer
= bytes_to_frames(runtime
, READ_ONCE(ssi
->byte_pos
));
850 static struct rsnd_mod_ops rsnd_ssi_pio_ops
= {
852 .probe
= rsnd_ssi_common_probe
,
853 .remove
= rsnd_ssi_common_remove
,
854 .init
= rsnd_ssi_init
,
855 .quit
= rsnd_ssi_quit
,
856 .start
= rsnd_ssi_start
,
857 .stop
= rsnd_ssi_stop
,
859 .pointer
= rsnd_ssi_pointer
,
860 .pcm_new
= rsnd_ssi_pcm_new
,
861 .hw_params
= rsnd_ssi_hw_params
,
864 static int rsnd_ssi_dma_probe(struct rsnd_mod
*mod
,
865 struct rsnd_dai_stream
*io
,
866 struct rsnd_priv
*priv
)
868 struct rsnd_ssi
*ssi
= rsnd_mod_to_ssi(mod
);
872 * SSIP/SSIU/IRQ/DMA are not needed on
875 if (rsnd_ssi_is_multi_slave(mod
, io
))
878 ret
= rsnd_ssi_common_probe(mod
, io
, priv
);
882 /* SSI probe might be called many times in MUX multi path */
883 ret
= rsnd_dma_attach(io
, mod
, &ssi
->dma
);
888 static int rsnd_ssi_fallback(struct rsnd_mod
*mod
,
889 struct rsnd_dai_stream
*io
,
890 struct rsnd_priv
*priv
)
892 struct device
*dev
= rsnd_priv_to_dev(priv
);
897 * SSI .probe might be called again.
899 * rsnd_rdai_continuance_probe()
901 mod
->ops
= &rsnd_ssi_pio_ops
;
903 dev_info(dev
, "%s[%d] fallback to PIO mode\n",
904 rsnd_mod_name(mod
), rsnd_mod_id(mod
));
909 static struct dma_chan
*rsnd_ssi_dma_req(struct rsnd_dai_stream
*io
,
910 struct rsnd_mod
*mod
)
912 struct rsnd_priv
*priv
= rsnd_mod_to_priv(mod
);
913 int is_play
= rsnd_io_is_play(io
);
916 if (rsnd_ssi_use_busif(io
))
917 name
= is_play
? "rxu" : "txu";
919 name
= is_play
? "rx" : "tx";
921 return rsnd_dma_request_channel(rsnd_ssi_of_node(priv
),
925 static struct rsnd_mod_ops rsnd_ssi_dma_ops
= {
927 .dma_req
= rsnd_ssi_dma_req
,
928 .probe
= rsnd_ssi_dma_probe
,
929 .remove
= rsnd_ssi_common_remove
,
930 .init
= rsnd_ssi_init
,
931 .quit
= rsnd_ssi_quit
,
932 .start
= rsnd_ssi_start
,
933 .stop
= rsnd_ssi_stop
,
935 .pcm_new
= rsnd_ssi_pcm_new
,
936 .fallback
= rsnd_ssi_fallback
,
937 .hw_params
= rsnd_ssi_hw_params
,
940 int rsnd_ssi_is_dma_mode(struct rsnd_mod
*mod
)
942 return mod
->ops
== &rsnd_ssi_dma_ops
;
949 static void rsnd_ssi_connect(struct rsnd_mod
*mod
,
950 struct rsnd_dai_stream
*io
)
952 struct rsnd_dai
*rdai
= rsnd_io_to_rdai(io
);
953 enum rsnd_mod_type types
[] = {
959 enum rsnd_mod_type type
;
962 /* try SSI -> SSIM1 -> SSIM2 -> SSIM3 */
963 for (i
= 0; i
< ARRAY_SIZE(types
); i
++) {
965 if (!rsnd_io_to_mod(io
, type
)) {
966 rsnd_dai_connect(mod
, io
, type
);
967 rsnd_rdai_channels_set(rdai
, (i
+ 1) * 2);
968 rsnd_rdai_ssi_lane_set(rdai
, (i
+ 1));
974 void rsnd_parse_connect_ssi(struct rsnd_dai
*rdai
,
975 struct device_node
*playback
,
976 struct device_node
*capture
)
978 struct rsnd_priv
*priv
= rsnd_rdai_to_priv(rdai
);
979 struct device_node
*node
;
980 struct device_node
*np
;
981 struct rsnd_mod
*mod
;
984 node
= rsnd_ssi_of_node(priv
);
989 for_each_child_of_node(node
, np
) {
990 mod
= rsnd_ssi_mod_get(priv
, i
);
992 rsnd_ssi_connect(mod
, &rdai
->playback
);
994 rsnd_ssi_connect(mod
, &rdai
->capture
);
1001 static void __rsnd_ssi_parse_hdmi_connection(struct rsnd_priv
*priv
,
1002 struct rsnd_dai_stream
*io
,
1003 struct device_node
*remote_ep
)
1005 struct device
*dev
= rsnd_priv_to_dev(priv
);
1006 struct rsnd_mod
*mod
= rsnd_io_to_mod_ssi(io
);
1007 struct rsnd_ssi
*ssi
;
1012 ssi
= rsnd_mod_to_ssi(mod
);
1014 if (strstr(remote_ep
->full_name
, "hdmi0")) {
1015 rsnd_flags_set(ssi
, RSND_SSI_HDMI0
);
1016 dev_dbg(dev
, "%s[%d] connected to HDMI0\n",
1017 rsnd_mod_name(mod
), rsnd_mod_id(mod
));
1020 if (strstr(remote_ep
->full_name
, "hdmi1")) {
1021 rsnd_flags_set(ssi
, RSND_SSI_HDMI1
);
1022 dev_dbg(dev
, "%s[%d] connected to HDMI1\n",
1023 rsnd_mod_name(mod
), rsnd_mod_id(mod
));
1027 void rsnd_ssi_parse_hdmi_connection(struct rsnd_priv
*priv
,
1028 struct device_node
*endpoint
,
1031 struct rsnd_dai
*rdai
= rsnd_rdai_get(priv
, dai_i
);
1032 struct device_node
*remote_ep
;
1034 remote_ep
= of_graph_get_remote_endpoint(endpoint
);
1038 __rsnd_ssi_parse_hdmi_connection(priv
, &rdai
->playback
, remote_ep
);
1039 __rsnd_ssi_parse_hdmi_connection(priv
, &rdai
->capture
, remote_ep
);
1042 struct rsnd_mod
*rsnd_ssi_mod_get(struct rsnd_priv
*priv
, int id
)
1044 if (WARN_ON(id
< 0 || id
>= rsnd_ssi_nr(priv
)))
1047 return rsnd_mod_get(rsnd_ssi_get(priv
, id
));
1050 int __rsnd_ssi_is_pin_sharing(struct rsnd_mod
*mod
)
1052 struct rsnd_ssi
*ssi
= rsnd_mod_to_ssi(mod
);
1054 return !!(rsnd_flags_has(ssi
, RSND_SSI_CLK_PIN_SHARE
));
1057 static u32
*rsnd_ssi_get_status(struct rsnd_dai_stream
*io
,
1058 struct rsnd_mod
*mod
,
1059 enum rsnd_mod_type type
)
1062 * SSIP (= SSI parent) needs to be special, otherwise,
1063 * 2nd SSI might doesn't start. see also rsnd_mod_call()
1065 * We can't include parent SSI status on SSI, because we don't know
1066 * how many SSI requests parent SSI. Thus, it is localed on "io" now.
1069 * Capture : SSI1 (needs SSI0)
1071 * 1) start Capture -> SSI0/SSI1 are started.
1072 * 2) start Playback -> SSI0 doesn't work, because it is already
1073 * marked as "started" on 1)
1075 * OTOH, using each mod's status is good for MUX case.
1076 * It doesn't need to start in 2nd start
1078 * IO-0: SRC0 -> CTU1 -+-> MUX -> DVC -> SSIU -> SSI0
1080 * IO-1: SRC1 -> CTU2 -+
1082 * 1) start IO-0 -> start SSI0
1083 * 2) start IO-1 -> SSI0 doesn't need to start, because it is
1084 * already started on 1)
1086 if (type
== RSND_MOD_SSIP
)
1087 return &io
->parent_ssi_status
;
1089 return rsnd_mod_get_status(io
, mod
, type
);
1092 int rsnd_ssi_probe(struct rsnd_priv
*priv
)
1094 struct device_node
*node
;
1095 struct device_node
*np
;
1096 struct device
*dev
= rsnd_priv_to_dev(priv
);
1097 struct rsnd_mod_ops
*ops
;
1099 struct rsnd_ssi
*ssi
;
1100 char name
[RSND_SSI_NAME_SIZE
];
1103 node
= rsnd_ssi_of_node(priv
);
1107 nr
= of_get_child_count(node
);
1110 goto rsnd_ssi_probe_done
;
1113 ssi
= devm_kzalloc(dev
, sizeof(*ssi
) * nr
, GFP_KERNEL
);
1116 goto rsnd_ssi_probe_done
;
1123 for_each_child_of_node(node
, np
) {
1124 if (!of_device_is_available(np
))
1127 ssi
= rsnd_ssi_get(priv
, i
);
1129 snprintf(name
, RSND_SSI_NAME_SIZE
, "%s.%d",
1132 clk
= devm_clk_get(dev
, name
);
1136 goto rsnd_ssi_probe_done
;
1139 if (of_get_property(np
, "shared-pin", NULL
))
1140 rsnd_flags_set(ssi
, RSND_SSI_CLK_PIN_SHARE
);
1142 if (of_get_property(np
, "no-busif", NULL
))
1143 rsnd_flags_set(ssi
, RSND_SSI_NO_BUSIF
);
1145 ssi
->irq
= irq_of_parse_and_map(np
, 0);
1149 goto rsnd_ssi_probe_done
;
1152 if (of_property_read_bool(np
, "pio-transfer"))
1153 ops
= &rsnd_ssi_pio_ops
;
1155 ops
= &rsnd_ssi_dma_ops
;
1157 ret
= rsnd_mod_init(priv
, rsnd_mod_get(ssi
), ops
, clk
,
1158 rsnd_ssi_get_status
, RSND_MOD_SSI
, i
);
1161 goto rsnd_ssi_probe_done
;
1169 rsnd_ssi_probe_done
:
1175 void rsnd_ssi_remove(struct rsnd_priv
*priv
)
1177 struct rsnd_ssi
*ssi
;
1180 for_each_rsnd_ssi(ssi
, priv
, i
) {
1181 rsnd_mod_quit(rsnd_mod_get(ssi
));