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_src_of_node(priv) \
32 of_get_child_by_name(rsnd_priv_to_dev(priv)->of_node, "rcar_sound,src")
34 #define rsnd_mod_to_src(_mod) \
35 container_of((_mod), struct rsnd_src, mod)
37 #define for_each_rsnd_src(pos, priv, i) \
39 ((i) < rsnd_src_nr(priv)) && \
40 ((pos) = (struct rsnd_src *)(priv)->src + i); \
45 * image of SRC (Sampling Rate Converter)
47 * 96kHz <-> +-----+ 48kHz +-----+ 48kHz +-------+
48 * 48kHz <-> | SRC | <------> | SSI | <-----> | codec |
49 * 44.1kHz <-> +-----+ +-----+ +-------+
59 * [mem] -> [SRU] -> [SSI]
64 * [mem] -> [SRC] -> [SSIU] -> [SSI]
69 * How to use SRC bypass mode for debugging
71 * SRC has bypass mode, and it is useful for debugging.
73 * SRCm_MODE controls whether SRC is used or not
74 * SSI_MODE0 controls whether SSIU which receives SRC data
76 * Both SRCm_MODE/SSI_MODE0 settings are needed if you use SRC,
77 * but SRC bypass mode needs SSI_MODE0 only.
80 * struct rsnd_src_platform_info {
85 * rsnd_src_convert_rate() indicates
86 * above convert_rate, and it controls
87 * whether SRC is used or not.
90 * static struct rsnd_dai_platform_info rsnd_dai = {
91 * .playback = { .ssi = &rsnd_ssi[0], },
95 * static struct rsnd_src_platform_info rsnd_src[] = {
99 * static struct rsnd_dai_platform_info rsnd_dai = {
100 * .playback = { .ssi = &rsnd_ssi[0], .src = &rsnd_src[0] },
103 * ex) uses SRC bypass mode
104 * static struct rsnd_src_platform_info rsnd_src[] = {
108 * static struct rsnd_dai_platform_info rsnd_dai = {
109 * .playback = { .ssi = &rsnd_ssi[0], .src = &rsnd_src[0] },
115 * Gen1/Gen2 common functions
117 int rsnd_src_ssiu_start(struct rsnd_mod
*ssi_mod
,
120 struct rsnd_dai_stream
*io
= rsnd_mod_to_io(ssi_mod
);
121 struct rsnd_dai
*rdai
= rsnd_io_to_rdai(io
);
122 struct snd_pcm_runtime
*runtime
= rsnd_io_to_runtime(io
);
123 int ssi_id
= rsnd_mod_id(ssi_mod
);
128 rsnd_mod_bset(ssi_mod
, SSI_MODE0
, (1 << ssi_id
),
129 !use_busif
<< ssi_id
);
134 if (rsnd_ssi_is_pin_sharing(ssi_mod
)) {
149 rsnd_mod_bset(ssi_mod
, SSI_MODE1
,
151 rsnd_rdai_is_clk_master(rdai
) ?
152 0x2 << shift
: 0x1 << shift
);
156 * DMA settings for SSIU
159 u32 val
= 0x76543210;
162 rsnd_mod_write(ssi_mod
, SSI_BUSIF_ADINR
,
163 rsnd_get_adinr(ssi_mod
));
164 rsnd_mod_write(ssi_mod
, SSI_BUSIF_MODE
, 1);
165 rsnd_mod_write(ssi_mod
, SSI_CTRL
, 0x1);
167 mask
<<= runtime
->channels
* 4;
170 switch (runtime
->sample_bits
) {
172 val
|= 0x67452301 & ~mask
;
175 val
|= 0x76543210 & ~mask
;
178 rsnd_mod_write(ssi_mod
, BUSIF_DALIGN
, val
);
185 int rsnd_src_ssiu_stop(struct rsnd_mod
*ssi_mod
)
188 * DMA settings for SSIU
190 rsnd_mod_write(ssi_mod
, SSI_CTRL
, 0);
195 int rsnd_src_ssi_irq_enable(struct rsnd_mod
*ssi_mod
)
197 struct rsnd_priv
*priv
= rsnd_mod_to_priv(ssi_mod
);
199 if (rsnd_is_gen1(priv
))
202 /* enable SSI interrupt if Gen2 */
203 if (rsnd_ssi_is_dma_mode(ssi_mod
))
204 rsnd_mod_write(ssi_mod
, INT_ENABLE
, 0x0e000000);
206 rsnd_mod_write(ssi_mod
, INT_ENABLE
, 0x0f000000);
211 int rsnd_src_ssi_irq_disable(struct rsnd_mod
*ssi_mod
)
213 struct rsnd_priv
*priv
= rsnd_mod_to_priv(ssi_mod
);
215 if (rsnd_is_gen1(priv
))
218 /* disable SSI interrupt if Gen2 */
219 rsnd_mod_write(ssi_mod
, INT_ENABLE
, 0x00000000);
224 unsigned int rsnd_src_get_ssi_rate(struct rsnd_priv
*priv
,
225 struct rsnd_dai_stream
*io
,
226 struct snd_pcm_runtime
*runtime
)
228 struct rsnd_mod
*src_mod
= rsnd_io_to_mod_src(io
);
229 struct rsnd_src
*src
;
230 unsigned int rate
= 0;
233 src
= rsnd_mod_to_src(src_mod
);
236 * return convert rate if SRC is used,
237 * otherwise, return runtime->rate as usual
239 rate
= rsnd_src_convert_rate(src
);
243 rate
= runtime
->rate
;
248 static int rsnd_src_set_convert_rate(struct rsnd_mod
*mod
)
250 struct rsnd_dai_stream
*io
= rsnd_mod_to_io(mod
);
251 struct snd_pcm_runtime
*runtime
= rsnd_io_to_runtime(io
);
252 struct rsnd_src
*src
= rsnd_mod_to_src(mod
);
253 u32 convert_rate
= rsnd_src_convert_rate(src
);
257 fsrate
= 0x0400000 / convert_rate
* runtime
->rate
;
259 /* set/clear soft reset */
260 rsnd_mod_write(mod
, SRC_SWRSR
, 0);
261 rsnd_mod_write(mod
, SRC_SWRSR
, 1);
263 /* Set channel number and output bit length */
264 rsnd_mod_write(mod
, SRC_ADINR
, rsnd_get_adinr(mod
));
266 /* Enable the initial value of IFS */
268 rsnd_mod_write(mod
, SRC_IFSCR
, 1);
270 /* Set initial value of IFS */
271 rsnd_mod_write(mod
, SRC_IFSVR
, fsrate
);
274 /* use DMA transfer */
275 rsnd_mod_write(mod
, SRC_BUSIF_MODE
, 1);
280 static int rsnd_src_init(struct rsnd_mod
*mod
)
282 struct rsnd_src
*src
= rsnd_mod_to_src(mod
);
284 rsnd_mod_hw_start(mod
);
289 * Initialize the operation of the SRC internal circuits
290 * see rsnd_src_start()
292 rsnd_mod_write(mod
, SRC_SRCIR
, 1);
297 static int rsnd_src_quit(struct rsnd_mod
*mod
,
298 struct rsnd_priv
*priv
)
300 struct rsnd_src
*src
= rsnd_mod_to_src(mod
);
301 struct device
*dev
= rsnd_priv_to_dev(priv
);
303 rsnd_mod_hw_stop(mod
);
306 dev_warn(dev
, "%s[%d] under/over flow err = %d\n",
307 rsnd_mod_name(mod
), rsnd_mod_id(mod
), src
->err
);
312 static int rsnd_src_start(struct rsnd_mod
*mod
)
315 * Cancel the initialization and operate the SRC function
316 * see rsnd_src_init()
318 rsnd_mod_write(mod
, SRC_SRCIR
, 0);
323 static int rsnd_src_stop(struct rsnd_mod
*mod
)
332 static int rsnd_src_set_route_gen1(struct rsnd_mod
*mod
)
334 struct rsnd_dai_stream
*io
= rsnd_mod_to_io(mod
);
335 struct src_route_config
{
342 { 0x7, 12, }, /* 3 */
343 { 0x7, 16, }, /* 4 */
344 { 0x7, 20, }, /* 5 */
345 { 0x7, 24, }, /* 6 */
346 { 0x3, 28, }, /* 7 */
347 { 0x3, 30, }, /* 8 */
353 id
= rsnd_mod_id(mod
);
354 if (id
< 0 || id
>= ARRAY_SIZE(routes
))
360 val
= rsnd_io_is_play(io
) ? 0x1 : 0x2;
361 val
= val
<< routes
[id
].shift
;
362 mask
= routes
[id
].mask
<< routes
[id
].shift
;
364 rsnd_mod_bset(mod
, SRC_ROUTE_SEL
, mask
, val
);
369 static int rsnd_src_set_convert_timing_gen1(struct rsnd_mod
*mod
)
371 struct rsnd_dai_stream
*io
= rsnd_mod_to_io(mod
);
372 struct rsnd_priv
*priv
= rsnd_mod_to_priv(mod
);
373 struct rsnd_src
*src
= rsnd_mod_to_src(mod
);
374 struct snd_pcm_runtime
*runtime
= rsnd_io_to_runtime(io
);
375 u32 convert_rate
= rsnd_src_convert_rate(src
);
379 int id
= rsnd_mod_id(mod
);
385 shift
= (id
% 4) * 8;
386 mask
= 0x1F << shift
;
389 * ADG is used as source clock if SRC was used,
390 * then, SSI WS is used as destination clock.
391 * SSI WS is used as source clock if SRC is not used
392 * (when playback, source/destination become reverse when capture)
398 ret
= rsnd_adg_set_convert_clk_gen1(priv
, mod
,
401 } else if (8 == id
) {
402 /* use SSI WS, but SRU8 is special */
406 val
= (id
+ 1) << shift
;
414 rsnd_mod_bset(mod
, SRC_TMG_SEL0
, mask
, val
);
417 rsnd_mod_bset(mod
, SRC_TMG_SEL1
, mask
, val
);
420 rsnd_mod_bset(mod
, SRC_TMG_SEL2
, mask
, val
);
427 static int rsnd_src_set_convert_rate_gen1(struct rsnd_mod
*mod
)
429 struct rsnd_src
*src
= rsnd_mod_to_src(mod
);
432 ret
= rsnd_src_set_convert_rate(mod
);
436 /* Select SRC mode (fixed value) */
437 rsnd_mod_write(mod
, SRC_SRCCR
, 0x00010110);
439 /* Set the restriction value of the FS ratio (98%) */
440 rsnd_mod_write(mod
, SRC_MNFSR
,
441 rsnd_mod_read(mod
, SRC_IFSVR
) / 100 * 98);
443 /* Gen1/Gen2 are not compatible */
444 if (rsnd_src_convert_rate(src
))
445 rsnd_mod_write(mod
, SRC_ROUTE_MODE0
, 1);
447 /* no SRC_BFSSR settings, since SRC_SRCCR::BUFMD is 0 */
452 static int rsnd_src_probe_gen1(struct rsnd_mod
*mod
,
453 struct rsnd_priv
*priv
)
455 struct device
*dev
= rsnd_priv_to_dev(priv
);
457 dev_dbg(dev
, "%s[%d] (Gen1) is probed\n",
458 rsnd_mod_name(mod
), rsnd_mod_id(mod
));
463 static int rsnd_src_init_gen1(struct rsnd_mod
*mod
,
464 struct rsnd_priv
*priv
)
468 ret
= rsnd_src_init(mod
);
472 ret
= rsnd_src_set_route_gen1(mod
);
476 ret
= rsnd_src_set_convert_rate_gen1(mod
);
480 ret
= rsnd_src_set_convert_timing_gen1(mod
);
487 static int rsnd_src_start_gen1(struct rsnd_mod
*mod
,
488 struct rsnd_priv
*priv
)
490 int id
= rsnd_mod_id(mod
);
492 rsnd_mod_bset(mod
, SRC_ROUTE_CTRL
, (1 << id
), (1 << id
));
494 return rsnd_src_start(mod
);
497 static int rsnd_src_stop_gen1(struct rsnd_mod
*mod
,
498 struct rsnd_priv
*priv
)
500 int id
= rsnd_mod_id(mod
);
502 rsnd_mod_bset(mod
, SRC_ROUTE_CTRL
, (1 << id
), 0);
504 return rsnd_src_stop(mod
);
507 static struct rsnd_mod_ops rsnd_src_gen1_ops
= {
509 .probe
= rsnd_src_probe_gen1
,
510 .init
= rsnd_src_init_gen1
,
511 .quit
= rsnd_src_quit
,
512 .start
= rsnd_src_start_gen1
,
513 .stop
= rsnd_src_stop_gen1
,
519 #define rsnd_src_irq_enable_gen2(mod) rsnd_src_irq_ctrol_gen2(mod, 1)
520 #define rsnd_src_irq_disable_gen2(mod) rsnd_src_irq_ctrol_gen2(mod, 0)
521 static void rsnd_src_irq_ctrol_gen2(struct rsnd_mod
*mod
, int enable
)
523 struct rsnd_src
*src
= rsnd_mod_to_src(mod
);
524 u32 sys_int_val
, int_val
, sys_int_mask
;
525 int irq
= src
->info
->irq
;
526 int id
= rsnd_mod_id(mod
);
529 sys_int_mask
= OUF_SRC(id
);
533 * IRQ is not supported on non-DT
535 * rsnd_src_probe_gen2()
537 if ((irq
<= 0) || !enable
) {
542 rsnd_mod_write(mod
, SRC_INT_ENABLE0
, int_val
);
543 rsnd_mod_bset(mod
, SCU_SYS_INT_EN0
, sys_int_mask
, sys_int_val
);
544 rsnd_mod_bset(mod
, SCU_SYS_INT_EN1
, sys_int_mask
, sys_int_val
);
547 static void rsnd_src_error_clear_gen2(struct rsnd_mod
*mod
)
549 u32 val
= OUF_SRC(rsnd_mod_id(mod
));
551 rsnd_mod_bset(mod
, SCU_SYS_STATUS0
, val
, val
);
552 rsnd_mod_bset(mod
, SCU_SYS_STATUS1
, val
, val
);
555 static bool rsnd_src_error_record_gen2(struct rsnd_mod
*mod
)
557 u32 val
= OUF_SRC(rsnd_mod_id(mod
));
560 if ((rsnd_mod_read(mod
, SCU_SYS_STATUS0
) & val
) ||
561 (rsnd_mod_read(mod
, SCU_SYS_STATUS1
) & val
)) {
562 struct rsnd_src
*src
= rsnd_mod_to_src(mod
);
568 /* clear error static */
569 rsnd_src_error_clear_gen2(mod
);
574 static int _rsnd_src_start_gen2(struct rsnd_mod
*mod
)
576 struct rsnd_dai_stream
*io
= rsnd_mod_to_io(mod
);
577 u32 val
= rsnd_io_to_mod_dvc(io
) ? 0x01 : 0x11;
579 rsnd_mod_write(mod
, SRC_CTRL
, val
);
581 rsnd_src_error_clear_gen2(mod
);
585 rsnd_src_irq_enable_gen2(mod
);
590 static int _rsnd_src_stop_gen2(struct rsnd_mod
*mod
)
592 rsnd_src_irq_disable_gen2(mod
);
594 rsnd_mod_write(mod
, SRC_CTRL
, 0);
596 rsnd_src_error_record_gen2(mod
);
598 return rsnd_src_stop(mod
);
601 static irqreturn_t
rsnd_src_interrupt_gen2(int irq
, void *data
)
603 struct rsnd_mod
*mod
= data
;
604 struct rsnd_dai_stream
*io
= rsnd_mod_to_io(mod
);
609 if (rsnd_src_error_record_gen2(mod
)) {
610 struct rsnd_priv
*priv
= rsnd_mod_to_priv(mod
);
611 struct device
*dev
= rsnd_priv_to_dev(priv
);
613 _rsnd_src_stop_gen2(mod
);
614 _rsnd_src_start_gen2(mod
);
616 dev_dbg(dev
, "%s[%d] restart\n",
617 rsnd_mod_name(mod
), rsnd_mod_id(mod
));
623 static int rsnd_src_set_convert_rate_gen2(struct rsnd_mod
*mod
)
625 struct rsnd_priv
*priv
= rsnd_mod_to_priv(mod
);
626 struct device
*dev
= rsnd_priv_to_dev(priv
);
627 struct rsnd_dai_stream
*io
= rsnd_mod_to_io(mod
);
628 struct snd_pcm_runtime
*runtime
= rsnd_io_to_runtime(io
);
629 struct rsnd_src
*src
= rsnd_mod_to_src(mod
);
630 u32 convert_rate
= rsnd_src_convert_rate(src
);
634 /* 6 - 1/6 are very enough ratio for SRC_BSDSR */
637 else if (convert_rate
> runtime
->rate
)
638 ratio
= 100 * convert_rate
/ runtime
->rate
;
640 ratio
= 100 * runtime
->rate
/ convert_rate
;
643 dev_err(dev
, "FSO/FSI ratio error\n");
647 ret
= rsnd_src_set_convert_rate(mod
);
651 rsnd_mod_write(mod
, SRC_SRCCR
, 0x00011110);
654 /* Gen1/Gen2 are not compatible */
655 rsnd_mod_write(mod
, SRC_ROUTE_MODE0
, 1);
658 switch (rsnd_mod_id(mod
)) {
663 rsnd_mod_write(mod
, SRC_BSDSR
, 0x02400000);
666 rsnd_mod_write(mod
, SRC_BSDSR
, 0x01800000);
670 rsnd_mod_write(mod
, SRC_BSISR
, 0x00100060);
675 static int rsnd_src_set_convert_timing_gen2(struct rsnd_mod
*mod
)
677 struct rsnd_dai_stream
*io
= rsnd_mod_to_io(mod
);
678 struct snd_pcm_runtime
*runtime
= rsnd_io_to_runtime(io
);
679 struct rsnd_src
*src
= rsnd_mod_to_src(mod
);
680 u32 convert_rate
= rsnd_src_convert_rate(src
);
684 ret
= rsnd_adg_set_convert_clk_gen2(mod
, io
,
688 ret
= rsnd_adg_set_convert_timing_gen2(mod
, io
);
693 static int rsnd_src_probe_gen2(struct rsnd_mod
*mod
,
694 struct rsnd_priv
*priv
)
696 struct rsnd_src
*src
= rsnd_mod_to_src(mod
);
697 struct device
*dev
= rsnd_priv_to_dev(priv
);
698 int irq
= src
->info
->irq
;
703 * IRQ is not supported on non-DT
705 * rsnd_src_irq_enable_gen2()
707 ret
= devm_request_irq(dev
, irq
,
708 rsnd_src_interrupt_gen2
,
712 goto rsnd_src_probe_gen2_fail
;
715 ret
= rsnd_dma_init(priv
,
716 rsnd_mod_to_dma(mod
),
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(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
= rsnd_src_of_node(priv
);
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 rsnd_mod_init(&src
->mod
, ops
, clk
, RSND_MOD_SRC
, i
);
895 dev_dbg(dev
, "SRC%d probed\n", i
);