2 * Renesas R-Car SRC support
4 * Copyright (C) 2013 Renesas Solutions Corp.
5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
13 #define SRC_NAME "src"
16 #define OUF_SRCO ((1 << 12) | (1 << 13))
17 #define OUF_SRCI ((1 << 9) | (1 << 8))
19 /* SCU_SYSTEM_STATUS0/1 */
20 #define OUF_SRC(id) ((1 << (id + 16)) | (1 << id))
23 struct rsnd_src_platform_info
*info
; /* rcar_snd.h */
28 #define RSND_SRC_NAME_SIZE 16
30 #define rsnd_src_convert_rate(p) ((p)->info->convert_rate)
31 #define rsnd_mod_to_src(_mod) \
32 container_of((_mod), struct rsnd_src, mod)
33 #define rsnd_src_dma_available(src) \
34 rsnd_dma_available(rsnd_mod_to_dma(&(src)->mod))
36 #define for_each_rsnd_src(pos, priv, i) \
38 ((i) < rsnd_src_nr(priv)) && \
39 ((pos) = (struct rsnd_src *)(priv)->src + i); \
44 * image of SRC (Sampling Rate Converter)
46 * 96kHz <-> +-----+ 48kHz +-----+ 48kHz +-------+
47 * 48kHz <-> | SRC | <------> | SSI | <-----> | codec |
48 * 44.1kHz <-> +-----+ +-----+ +-------+
58 * [mem] -> [SRU] -> [SSI]
63 * [mem] -> [SRC] -> [SSIU] -> [SSI]
68 * How to use SRC bypass mode for debugging
70 * SRC has bypass mode, and it is useful for debugging.
72 * SRCm_MODE controls whether SRC is used or not
73 * SSI_MODE0 controls whether SSIU which receives SRC data
75 * Both SRCm_MODE/SSI_MODE0 settings are needed if you use SRC,
76 * but SRC bypass mode needs SSI_MODE0 only.
79 * struct rsnd_src_platform_info {
84 * rsnd_src_convert_rate() indicates
85 * above convert_rate, and it controls
86 * whether SRC is used or not.
89 * static struct rsnd_dai_platform_info rsnd_dai = {
90 * .playback = { .ssi = &rsnd_ssi[0], },
94 * static struct rsnd_src_platform_info rsnd_src[] = {
98 * static struct rsnd_dai_platform_info rsnd_dai = {
99 * .playback = { .ssi = &rsnd_ssi[0], .src = &rsnd_src[0] },
102 * ex) uses SRC bypass mode
103 * static struct rsnd_src_platform_info rsnd_src[] = {
107 * static struct rsnd_dai_platform_info rsnd_dai = {
108 * .playback = { .ssi = &rsnd_ssi[0], .src = &rsnd_src[0] },
114 * Gen1/Gen2 common functions
116 int rsnd_src_ssiu_start(struct rsnd_mod
*ssi_mod
,
119 struct rsnd_dai_stream
*io
= rsnd_mod_to_io(ssi_mod
);
120 struct rsnd_dai
*rdai
= rsnd_io_to_rdai(io
);
121 struct snd_pcm_runtime
*runtime
= rsnd_io_to_runtime(io
);
122 int ssi_id
= rsnd_mod_id(ssi_mod
);
127 rsnd_mod_bset(ssi_mod
, SSI_MODE0
, (1 << ssi_id
),
128 !use_busif
<< ssi_id
);
133 if (rsnd_ssi_is_pin_sharing(ssi_mod
)) {
148 rsnd_mod_bset(ssi_mod
, SSI_MODE1
,
150 rsnd_rdai_is_clk_master(rdai
) ?
151 0x2 << shift
: 0x1 << shift
);
155 * DMA settings for SSIU
158 u32 val
= 0x76543210;
161 rsnd_mod_write(ssi_mod
, SSI_BUSIF_ADINR
,
162 rsnd_get_adinr(ssi_mod
));
163 rsnd_mod_write(ssi_mod
, SSI_BUSIF_MODE
, 1);
164 rsnd_mod_write(ssi_mod
, SSI_CTRL
, 0x1);
166 mask
<<= runtime
->channels
* 4;
169 switch (runtime
->sample_bits
) {
171 val
|= 0x67452301 & ~mask
;
174 val
|= 0x76543210 & ~mask
;
177 rsnd_mod_write(ssi_mod
, BUSIF_DALIGN
, val
);
184 int rsnd_src_ssiu_stop(struct rsnd_mod
*ssi_mod
)
187 * DMA settings for SSIU
189 rsnd_mod_write(ssi_mod
, SSI_CTRL
, 0);
194 int rsnd_src_ssi_irq_enable(struct rsnd_mod
*ssi_mod
)
196 struct rsnd_priv
*priv
= rsnd_mod_to_priv(ssi_mod
);
198 if (rsnd_is_gen1(priv
))
201 /* enable SSI interrupt if Gen2 */
202 if (rsnd_ssi_is_dma_mode(ssi_mod
))
203 rsnd_mod_write(ssi_mod
, INT_ENABLE
, 0x0e000000);
205 rsnd_mod_write(ssi_mod
, INT_ENABLE
, 0x0f000000);
210 int rsnd_src_ssi_irq_disable(struct rsnd_mod
*ssi_mod
)
212 struct rsnd_priv
*priv
= rsnd_mod_to_priv(ssi_mod
);
214 if (rsnd_is_gen1(priv
))
217 /* disable SSI interrupt if Gen2 */
218 rsnd_mod_write(ssi_mod
, INT_ENABLE
, 0x00000000);
223 unsigned int rsnd_src_get_ssi_rate(struct rsnd_priv
*priv
,
224 struct rsnd_dai_stream
*io
,
225 struct snd_pcm_runtime
*runtime
)
227 struct rsnd_mod
*src_mod
= rsnd_io_to_mod_src(io
);
228 struct rsnd_src
*src
;
229 unsigned int rate
= 0;
232 src
= rsnd_mod_to_src(src_mod
);
235 * return convert rate if SRC is used,
236 * otherwise, return runtime->rate as usual
238 rate
= rsnd_src_convert_rate(src
);
242 rate
= runtime
->rate
;
247 static int rsnd_src_set_convert_rate(struct rsnd_mod
*mod
)
249 struct rsnd_dai_stream
*io
= rsnd_mod_to_io(mod
);
250 struct snd_pcm_runtime
*runtime
= rsnd_io_to_runtime(io
);
251 struct rsnd_src
*src
= rsnd_mod_to_src(mod
);
252 u32 convert_rate
= rsnd_src_convert_rate(src
);
256 fsrate
= 0x0400000 / convert_rate
* runtime
->rate
;
258 /* set/clear soft reset */
259 rsnd_mod_write(mod
, SRC_SWRSR
, 0);
260 rsnd_mod_write(mod
, SRC_SWRSR
, 1);
262 /* Set channel number and output bit length */
263 rsnd_mod_write(mod
, SRC_ADINR
, rsnd_get_adinr(mod
));
265 /* Enable the initial value of IFS */
267 rsnd_mod_write(mod
, SRC_IFSCR
, 1);
269 /* Set initial value of IFS */
270 rsnd_mod_write(mod
, SRC_IFSVR
, fsrate
);
273 /* use DMA transfer */
274 rsnd_mod_write(mod
, SRC_BUSIF_MODE
, 1);
279 static int rsnd_src_init(struct rsnd_mod
*mod
)
281 struct rsnd_src
*src
= rsnd_mod_to_src(mod
);
283 rsnd_mod_hw_start(mod
);
288 * Initialize the operation of the SRC internal circuits
289 * see rsnd_src_start()
291 rsnd_mod_write(mod
, SRC_SRCIR
, 1);
296 static int rsnd_src_quit(struct rsnd_mod
*mod
,
297 struct rsnd_priv
*priv
)
299 struct rsnd_src
*src
= rsnd_mod_to_src(mod
);
300 struct device
*dev
= rsnd_priv_to_dev(priv
);
302 rsnd_mod_hw_stop(mod
);
305 dev_warn(dev
, "%s[%d] under/over flow err = %d\n",
306 rsnd_mod_name(mod
), rsnd_mod_id(mod
), src
->err
);
311 static int rsnd_src_start(struct rsnd_mod
*mod
)
314 * Cancel the initialization and operate the SRC function
315 * see rsnd_src_init()
317 rsnd_mod_write(mod
, SRC_SRCIR
, 0);
322 static int rsnd_src_stop(struct rsnd_mod
*mod
)
331 static int rsnd_src_set_route_gen1(struct rsnd_mod
*mod
)
333 struct rsnd_dai_stream
*io
= rsnd_mod_to_io(mod
);
334 struct src_route_config
{
341 { 0x7, 12, }, /* 3 */
342 { 0x7, 16, }, /* 4 */
343 { 0x7, 20, }, /* 5 */
344 { 0x7, 24, }, /* 6 */
345 { 0x3, 28, }, /* 7 */
346 { 0x3, 30, }, /* 8 */
352 id
= rsnd_mod_id(mod
);
353 if (id
< 0 || id
>= ARRAY_SIZE(routes
))
359 val
= rsnd_io_is_play(io
) ? 0x1 : 0x2;
360 val
= val
<< routes
[id
].shift
;
361 mask
= routes
[id
].mask
<< routes
[id
].shift
;
363 rsnd_mod_bset(mod
, SRC_ROUTE_SEL
, mask
, val
);
368 static int rsnd_src_set_convert_timing_gen1(struct rsnd_mod
*mod
)
370 struct rsnd_dai_stream
*io
= rsnd_mod_to_io(mod
);
371 struct rsnd_priv
*priv
= rsnd_mod_to_priv(mod
);
372 struct rsnd_src
*src
= rsnd_mod_to_src(mod
);
373 struct snd_pcm_runtime
*runtime
= rsnd_io_to_runtime(io
);
374 u32 convert_rate
= rsnd_src_convert_rate(src
);
378 int id
= rsnd_mod_id(mod
);
384 shift
= (id
% 4) * 8;
385 mask
= 0x1F << shift
;
388 * ADG is used as source clock if SRC was used,
389 * then, SSI WS is used as destination clock.
390 * SSI WS is used as source clock if SRC is not used
391 * (when playback, source/destination become reverse when capture)
397 ret
= rsnd_adg_set_convert_clk_gen1(priv
, mod
,
400 } else if (8 == id
) {
401 /* use SSI WS, but SRU8 is special */
405 val
= (id
+ 1) << shift
;
413 rsnd_mod_bset(mod
, SRC_TMG_SEL0
, mask
, val
);
416 rsnd_mod_bset(mod
, SRC_TMG_SEL1
, mask
, val
);
419 rsnd_mod_bset(mod
, SRC_TMG_SEL2
, mask
, val
);
426 static int rsnd_src_set_convert_rate_gen1(struct rsnd_mod
*mod
)
428 struct rsnd_src
*src
= rsnd_mod_to_src(mod
);
431 ret
= rsnd_src_set_convert_rate(mod
);
435 /* Select SRC mode (fixed value) */
436 rsnd_mod_write(mod
, SRC_SRCCR
, 0x00010110);
438 /* Set the restriction value of the FS ratio (98%) */
439 rsnd_mod_write(mod
, SRC_MNFSR
,
440 rsnd_mod_read(mod
, SRC_IFSVR
) / 100 * 98);
442 /* Gen1/Gen2 are not compatible */
443 if (rsnd_src_convert_rate(src
))
444 rsnd_mod_write(mod
, SRC_ROUTE_MODE0
, 1);
446 /* no SRC_BFSSR settings, since SRC_SRCCR::BUFMD is 0 */
451 static int rsnd_src_probe_gen1(struct rsnd_mod
*mod
,
452 struct rsnd_priv
*priv
)
454 struct device
*dev
= rsnd_priv_to_dev(priv
);
456 dev_dbg(dev
, "%s[%d] (Gen1) is probed\n",
457 rsnd_mod_name(mod
), rsnd_mod_id(mod
));
462 static int rsnd_src_init_gen1(struct rsnd_mod
*mod
,
463 struct rsnd_priv
*priv
)
467 ret
= rsnd_src_init(mod
);
471 ret
= rsnd_src_set_route_gen1(mod
);
475 ret
= rsnd_src_set_convert_rate_gen1(mod
);
479 ret
= rsnd_src_set_convert_timing_gen1(mod
);
486 static int rsnd_src_start_gen1(struct rsnd_mod
*mod
,
487 struct rsnd_priv
*priv
)
489 int id
= rsnd_mod_id(mod
);
491 rsnd_mod_bset(mod
, SRC_ROUTE_CTRL
, (1 << id
), (1 << id
));
493 return rsnd_src_start(mod
);
496 static int rsnd_src_stop_gen1(struct rsnd_mod
*mod
,
497 struct rsnd_priv
*priv
)
499 int id
= rsnd_mod_id(mod
);
501 rsnd_mod_bset(mod
, SRC_ROUTE_CTRL
, (1 << id
), 0);
503 return rsnd_src_stop(mod
);
506 static struct rsnd_mod_ops rsnd_src_gen1_ops
= {
508 .probe
= rsnd_src_probe_gen1
,
509 .init
= rsnd_src_init_gen1
,
510 .quit
= rsnd_src_quit
,
511 .start
= rsnd_src_start_gen1
,
512 .stop
= rsnd_src_stop_gen1
,
518 #define rsnd_src_irq_enable_gen2(mod) rsnd_src_irq_ctrol_gen2(mod, 1)
519 #define rsnd_src_irq_disable_gen2(mod) rsnd_src_irq_ctrol_gen2(mod, 0)
520 static void rsnd_src_irq_ctrol_gen2(struct rsnd_mod
*mod
, int enable
)
522 struct rsnd_src
*src
= rsnd_mod_to_src(mod
);
523 u32 sys_int_val
, int_val
, sys_int_mask
;
524 int irq
= src
->info
->irq
;
525 int id
= rsnd_mod_id(mod
);
528 sys_int_mask
= OUF_SRC(id
);
532 * IRQ is not supported on non-DT
534 * rsnd_src_probe_gen2()
536 if ((irq
<= 0) || !enable
) {
541 rsnd_mod_write(mod
, SRC_INT_ENABLE0
, int_val
);
542 rsnd_mod_bset(mod
, SCU_SYS_INT_EN0
, sys_int_mask
, sys_int_val
);
543 rsnd_mod_bset(mod
, SCU_SYS_INT_EN1
, sys_int_mask
, sys_int_val
);
546 static void rsnd_src_error_clear_gen2(struct rsnd_mod
*mod
)
548 u32 val
= OUF_SRC(rsnd_mod_id(mod
));
550 rsnd_mod_bset(mod
, SCU_SYS_STATUS0
, val
, val
);
551 rsnd_mod_bset(mod
, SCU_SYS_STATUS1
, val
, val
);
554 static bool rsnd_src_error_record_gen2(struct rsnd_mod
*mod
)
556 u32 val
= OUF_SRC(rsnd_mod_id(mod
));
559 if ((rsnd_mod_read(mod
, SCU_SYS_STATUS0
) & val
) ||
560 (rsnd_mod_read(mod
, SCU_SYS_STATUS1
) & val
)) {
561 struct rsnd_src
*src
= rsnd_mod_to_src(mod
);
567 /* clear error static */
568 rsnd_src_error_clear_gen2(mod
);
573 static int _rsnd_src_start_gen2(struct rsnd_mod
*mod
)
575 struct rsnd_dai_stream
*io
= rsnd_mod_to_io(mod
);
576 u32 val
= rsnd_io_to_mod_dvc(io
) ? 0x01 : 0x11;
578 rsnd_mod_write(mod
, SRC_CTRL
, val
);
580 rsnd_src_error_clear_gen2(mod
);
584 rsnd_src_irq_enable_gen2(mod
);
589 static int _rsnd_src_stop_gen2(struct rsnd_mod
*mod
)
591 rsnd_src_irq_disable_gen2(mod
);
593 rsnd_mod_write(mod
, SRC_CTRL
, 0);
595 rsnd_src_error_record_gen2(mod
);
597 return rsnd_src_stop(mod
);
600 static irqreturn_t
rsnd_src_interrupt_gen2(int irq
, void *data
)
602 struct rsnd_mod
*mod
= data
;
603 struct rsnd_dai_stream
*io
= rsnd_mod_to_io(mod
);
608 if (rsnd_src_error_record_gen2(mod
)) {
609 struct rsnd_priv
*priv
= rsnd_mod_to_priv(mod
);
610 struct device
*dev
= rsnd_priv_to_dev(priv
);
612 _rsnd_src_stop_gen2(mod
);
613 _rsnd_src_start_gen2(mod
);
615 dev_dbg(dev
, "%s[%d] restart\n",
616 rsnd_mod_name(mod
), rsnd_mod_id(mod
));
622 static int rsnd_src_set_convert_rate_gen2(struct rsnd_mod
*mod
)
624 struct rsnd_priv
*priv
= rsnd_mod_to_priv(mod
);
625 struct device
*dev
= rsnd_priv_to_dev(priv
);
626 struct rsnd_dai_stream
*io
= rsnd_mod_to_io(mod
);
627 struct snd_pcm_runtime
*runtime
= rsnd_io_to_runtime(io
);
628 struct rsnd_src
*src
= rsnd_mod_to_src(mod
);
629 u32 convert_rate
= rsnd_src_convert_rate(src
);
633 /* 6 - 1/6 are very enough ratio for SRC_BSDSR */
636 else if (convert_rate
> runtime
->rate
)
637 ratio
= 100 * convert_rate
/ runtime
->rate
;
639 ratio
= 100 * runtime
->rate
/ convert_rate
;
642 dev_err(dev
, "FSO/FSI ratio error\n");
646 ret
= rsnd_src_set_convert_rate(mod
);
650 rsnd_mod_write(mod
, SRC_SRCCR
, 0x00011110);
653 /* Gen1/Gen2 are not compatible */
654 rsnd_mod_write(mod
, SRC_ROUTE_MODE0
, 1);
657 switch (rsnd_mod_id(mod
)) {
662 rsnd_mod_write(mod
, SRC_BSDSR
, 0x02400000);
665 rsnd_mod_write(mod
, SRC_BSDSR
, 0x01800000);
669 rsnd_mod_write(mod
, SRC_BSISR
, 0x00100060);
674 static int rsnd_src_set_convert_timing_gen2(struct rsnd_mod
*mod
)
676 struct rsnd_dai_stream
*io
= rsnd_mod_to_io(mod
);
677 struct snd_pcm_runtime
*runtime
= rsnd_io_to_runtime(io
);
678 struct rsnd_src
*src
= rsnd_mod_to_src(mod
);
679 u32 convert_rate
= rsnd_src_convert_rate(src
);
683 ret
= rsnd_adg_set_convert_clk_gen2(mod
, io
,
687 ret
= rsnd_adg_set_convert_timing_gen2(mod
, io
);
692 static int rsnd_src_probe_gen2(struct rsnd_mod
*mod
,
693 struct rsnd_priv
*priv
)
695 struct rsnd_src
*src
= rsnd_mod_to_src(mod
);
696 struct device
*dev
= rsnd_priv_to_dev(priv
);
697 int irq
= src
->info
->irq
;
702 * IRQ is not supported on non-DT
704 * rsnd_src_irq_enable_gen2()
706 ret
= devm_request_irq(dev
, irq
,
707 rsnd_src_interrupt_gen2
,
711 goto rsnd_src_probe_gen2_fail
;
714 ret
= rsnd_dma_init(priv
,
715 rsnd_mod_to_dma(mod
),
716 rsnd_info_is_playback(priv
, src
),
719 goto rsnd_src_probe_gen2_fail
;
721 dev_dbg(dev
, "%s[%d] (Gen2) is probed\n",
722 rsnd_mod_name(mod
), rsnd_mod_id(mod
));
726 rsnd_src_probe_gen2_fail
:
727 dev_err(dev
, "%s[%d] (Gen2) failed\n",
728 rsnd_mod_name(mod
), rsnd_mod_id(mod
));
733 static int rsnd_src_remove_gen2(struct rsnd_mod
*mod
,
734 struct rsnd_priv
*priv
)
736 rsnd_dma_quit(priv
, rsnd_mod_to_dma(mod
));
741 static int rsnd_src_init_gen2(struct rsnd_mod
*mod
,
742 struct rsnd_priv
*priv
)
746 ret
= rsnd_src_init(mod
);
750 ret
= rsnd_src_set_convert_rate_gen2(mod
);
754 ret
= rsnd_src_set_convert_timing_gen2(mod
);
761 static int rsnd_src_start_gen2(struct rsnd_mod
*mod
,
762 struct rsnd_priv
*priv
)
764 rsnd_dma_start(rsnd_mod_to_dma(mod
));
766 return _rsnd_src_start_gen2(mod
);
769 static int rsnd_src_stop_gen2(struct rsnd_mod
*mod
,
770 struct rsnd_priv
*priv
)
774 ret
= _rsnd_src_stop_gen2(mod
);
776 rsnd_dma_stop(rsnd_mod_to_dma(mod
));
781 static struct rsnd_mod_ops rsnd_src_gen2_ops
= {
783 .probe
= rsnd_src_probe_gen2
,
784 .remove
= rsnd_src_remove_gen2
,
785 .init
= rsnd_src_init_gen2
,
786 .quit
= rsnd_src_quit
,
787 .start
= rsnd_src_start_gen2
,
788 .stop
= rsnd_src_stop_gen2
,
791 struct rsnd_mod
*rsnd_src_mod_get(struct rsnd_priv
*priv
, int id
)
793 if (WARN_ON(id
< 0 || id
>= rsnd_src_nr(priv
)))
796 return &((struct rsnd_src
*)(priv
->src
) + id
)->mod
;
799 static void rsnd_of_parse_src(struct platform_device
*pdev
,
800 const struct rsnd_of_data
*of_data
,
801 struct rsnd_priv
*priv
)
803 struct device_node
*src_node
;
804 struct device_node
*np
;
805 struct rcar_snd_info
*info
= rsnd_priv_to_info(priv
);
806 struct rsnd_src_platform_info
*src_info
;
807 struct device
*dev
= &pdev
->dev
;
813 src_node
= of_get_child_by_name(dev
->of_node
, "rcar_sound,src");
817 nr
= of_get_child_count(src_node
);
819 goto rsnd_of_parse_src_end
;
821 src_info
= devm_kzalloc(dev
,
822 sizeof(struct rsnd_src_platform_info
) * nr
,
825 dev_err(dev
, "src info allocation error\n");
826 goto rsnd_of_parse_src_end
;
829 info
->src_info
= src_info
;
830 info
->src_info_nr
= nr
;
833 for_each_child_of_node(src_node
, np
) {
834 src_info
[i
].irq
= irq_of_parse_and_map(np
, 0);
839 rsnd_of_parse_src_end
:
840 of_node_put(src_node
);
843 int rsnd_src_probe(struct platform_device
*pdev
,
844 const struct rsnd_of_data
*of_data
,
845 struct rsnd_priv
*priv
)
847 struct rcar_snd_info
*info
= rsnd_priv_to_info(priv
);
848 struct device
*dev
= rsnd_priv_to_dev(priv
);
849 struct rsnd_src
*src
;
850 struct rsnd_mod_ops
*ops
;
852 char name
[RSND_SRC_NAME_SIZE
];
856 if (rsnd_is_gen1(priv
))
857 ops
= &rsnd_src_gen1_ops
;
858 if (rsnd_is_gen2(priv
))
859 ops
= &rsnd_src_gen2_ops
;
861 dev_err(dev
, "unknown Generation\n");
865 rsnd_of_parse_src(pdev
, of_data
, priv
);
870 nr
= info
->src_info_nr
;
874 src
= devm_kzalloc(dev
, sizeof(*src
) * nr
, GFP_KERNEL
);
876 dev_err(dev
, "SRC allocate failed\n");
883 for_each_rsnd_src(src
, priv
, i
) {
884 snprintf(name
, RSND_SRC_NAME_SIZE
, "%s.%d",
887 clk
= devm_clk_get(dev
, name
);
891 src
->info
= &info
->src_info
[i
];
893 ret
= rsnd_mod_init(&src
->mod
, ops
, clk
, RSND_MOD_SRC
, i
);
897 dev_dbg(dev
, "SRC%d probed\n", i
);
903 void rsnd_src_remove(struct platform_device
*pdev
,
904 struct rsnd_priv
*priv
)
906 struct rsnd_src
*src
;
909 for_each_rsnd_src(src
, priv
, i
) {
910 rsnd_mod_quit(&src
->mod
);