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,
117 struct rsnd_dai *rdai,
120 struct rsnd_dai_stream *io = rsnd_mod_to_io(ssi_mod);
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,
185 struct rsnd_dai *rdai)
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,
196 struct rsnd_dai *rdai)
198 struct rsnd_priv *priv = rsnd_mod_to_priv(ssi_mod);
200 if (rsnd_is_gen1(priv))
203 /* enable SSI interrupt if Gen2 */
204 if (rsnd_ssi_is_dma_mode(ssi_mod))
205 rsnd_mod_write(ssi_mod, INT_ENABLE, 0x0e000000);
207 rsnd_mod_write(ssi_mod, INT_ENABLE, 0x0f000000);
212 int rsnd_src_ssi_irq_disable(struct rsnd_mod *ssi_mod,
213 struct rsnd_dai *rdai)
215 struct rsnd_priv *priv = rsnd_mod_to_priv(ssi_mod);
217 if (rsnd_is_gen1(priv))
220 /* disable SSI interrupt if Gen2 */
221 rsnd_mod_write(ssi_mod, INT_ENABLE, 0x00000000);
226 unsigned int rsnd_src_get_ssi_rate(struct rsnd_priv *priv,
227 struct rsnd_dai_stream *io,
228 struct snd_pcm_runtime *runtime)
230 struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
231 struct rsnd_src *src;
232 unsigned int rate = 0;
235 src = rsnd_mod_to_src(src_mod);
238 * return convert rate if SRC is used,
239 * otherwise, return runtime->rate as usual
241 rate = rsnd_src_convert_rate(src);
245 rate = runtime->rate;
250 static int rsnd_src_set_convert_rate(struct rsnd_mod *mod,
251 struct rsnd_dai *rdai)
253 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
254 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
255 struct rsnd_src *src = rsnd_mod_to_src(mod);
256 u32 convert_rate = rsnd_src_convert_rate(src);
260 fsrate = 0x0400000 / convert_rate * runtime->rate;
262 /* set/clear soft reset */
263 rsnd_mod_write(mod, SRC_SWRSR, 0);
264 rsnd_mod_write(mod, SRC_SWRSR, 1);
266 /* Set channel number and output bit length */
267 rsnd_mod_write(mod, SRC_ADINR, rsnd_get_adinr(mod));
269 /* Enable the initial value of IFS */
271 rsnd_mod_write(mod, SRC_IFSCR, 1);
273 /* Set initial value of IFS */
274 rsnd_mod_write(mod, SRC_IFSVR, fsrate);
277 /* use DMA transfer */
278 rsnd_mod_write(mod, SRC_BUSIF_MODE, 1);
283 static int rsnd_src_init(struct rsnd_mod *mod,
284 struct rsnd_dai *rdai)
286 struct rsnd_src *src = rsnd_mod_to_src(mod);
288 rsnd_mod_hw_start(mod);
293 * Initialize the operation of the SRC internal circuits
294 * see rsnd_src_start()
296 rsnd_mod_write(mod, SRC_SRCIR, 1);
301 static int rsnd_src_quit(struct rsnd_mod *mod,
302 struct rsnd_dai *rdai)
304 struct rsnd_src *src = rsnd_mod_to_src(mod);
305 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
306 struct device *dev = rsnd_priv_to_dev(priv);
308 rsnd_mod_hw_stop(mod);
311 dev_warn(dev, "src under/over flow err = %d\n", src->err);
316 static int rsnd_src_start(struct rsnd_mod *mod)
319 * Cancel the initialization and operate the SRC function
320 * see rsnd_src_init()
322 rsnd_mod_write(mod, SRC_SRCIR, 0);
327 static int rsnd_src_stop(struct rsnd_mod *mod)
336 static int rsnd_src_set_route_gen1(struct rsnd_mod *mod,
337 struct rsnd_dai *rdai)
339 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
340 struct src_route_config {
347 { 0x7, 12, }, /* 3 */
348 { 0x7, 16, }, /* 4 */
349 { 0x7, 20, }, /* 5 */
350 { 0x7, 24, }, /* 6 */
351 { 0x3, 28, }, /* 7 */
352 { 0x3, 30, }, /* 8 */
358 id = rsnd_mod_id(mod);
359 if (id < 0 || id >= ARRAY_SIZE(routes))
365 val = rsnd_io_is_play(io) ? 0x1 : 0x2;
366 val = val << routes[id].shift;
367 mask = routes[id].mask << routes[id].shift;
369 rsnd_mod_bset(mod, SRC_ROUTE_SEL, mask, val);
374 static int rsnd_src_set_convert_timing_gen1(struct rsnd_mod *mod,
375 struct rsnd_dai *rdai)
377 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
378 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
379 struct rsnd_src *src = rsnd_mod_to_src(mod);
380 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
381 u32 convert_rate = rsnd_src_convert_rate(src);
385 int id = rsnd_mod_id(mod);
391 shift = (id % 4) * 8;
392 mask = 0x1F << shift;
395 * ADG is used as source clock if SRC was used,
396 * then, SSI WS is used as destination clock.
397 * SSI WS is used as source clock if SRC is not used
398 * (when playback, source/destination become reverse when capture)
404 ret = rsnd_adg_set_convert_clk_gen1(priv, mod,
407 } else if (8 == id) {
408 /* use SSI WS, but SRU8 is special */
412 val = (id + 1) << shift;
420 rsnd_mod_bset(mod, SRC_TMG_SEL0, mask, val);
423 rsnd_mod_bset(mod, SRC_TMG_SEL1, mask, val);
426 rsnd_mod_bset(mod, SRC_TMG_SEL2, mask, val);
433 static int rsnd_src_set_convert_rate_gen1(struct rsnd_mod *mod,
434 struct rsnd_dai *rdai)
436 struct rsnd_src *src = rsnd_mod_to_src(mod);
439 ret = rsnd_src_set_convert_rate(mod, rdai);
443 /* Select SRC mode (fixed value) */
444 rsnd_mod_write(mod, SRC_SRCCR, 0x00010110);
446 /* Set the restriction value of the FS ratio (98%) */
447 rsnd_mod_write(mod, SRC_MNFSR,
448 rsnd_mod_read(mod, SRC_IFSVR) / 100 * 98);
450 /* Gen1/Gen2 are not compatible */
451 if (rsnd_src_convert_rate(src))
452 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 1);
454 /* no SRC_BFSSR settings, since SRC_SRCCR::BUFMD is 0 */
459 static int rsnd_src_probe_gen1(struct rsnd_mod *mod,
460 struct rsnd_dai *rdai)
462 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
463 struct device *dev = rsnd_priv_to_dev(priv);
465 dev_dbg(dev, "%s[%d] (Gen1) is probed\n",
466 rsnd_mod_name(mod), rsnd_mod_id(mod));
471 static int rsnd_src_init_gen1(struct rsnd_mod *mod,
472 struct rsnd_dai *rdai)
476 ret = rsnd_src_init(mod, rdai);
480 ret = rsnd_src_set_route_gen1(mod, rdai);
484 ret = rsnd_src_set_convert_rate_gen1(mod, rdai);
488 ret = rsnd_src_set_convert_timing_gen1(mod, rdai);
495 static int rsnd_src_start_gen1(struct rsnd_mod *mod,
496 struct rsnd_dai *rdai)
498 int id = rsnd_mod_id(mod);
500 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), (1 << id));
502 return rsnd_src_start(mod);
505 static int rsnd_src_stop_gen1(struct rsnd_mod *mod,
506 struct rsnd_dai *rdai)
508 int id = rsnd_mod_id(mod);
510 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), 0);
512 return rsnd_src_stop(mod);
515 static struct rsnd_mod_ops rsnd_src_gen1_ops = {
517 .probe = rsnd_src_probe_gen1,
518 .init = rsnd_src_init_gen1,
519 .quit = rsnd_src_quit,
520 .start = rsnd_src_start_gen1,
521 .stop = rsnd_src_stop_gen1,
527 #define rsnd_src_irq_enable_gen2(mod) rsnd_src_irq_ctrol_gen2(mod, 1)
528 #define rsnd_src_irq_disable_gen2(mod) rsnd_src_irq_ctrol_gen2(mod, 0)
529 static void rsnd_src_irq_ctrol_gen2(struct rsnd_mod *mod, int enable)
531 struct rsnd_src *src = rsnd_mod_to_src(mod);
532 u32 sys_int_val, int_val, sys_int_mask;
533 int irq = src->info->irq;
534 int id = rsnd_mod_id(mod);
537 sys_int_mask = OUF_SRC(id);
541 * IRQ is not supported on non-DT
543 * rsnd_src_probe_gen2()
545 if ((irq <= 0) || !enable) {
550 rsnd_mod_write(mod, SRC_INT_ENABLE0, int_val);
551 rsnd_mod_bset(mod, SCU_SYS_INT_EN0, sys_int_mask, sys_int_val);
552 rsnd_mod_bset(mod, SCU_SYS_INT_EN1, sys_int_mask, sys_int_val);
555 static void rsnd_src_error_clear_gen2(struct rsnd_mod *mod)
557 u32 val = OUF_SRC(rsnd_mod_id(mod));
559 rsnd_mod_bset(mod, SCU_SYS_STATUS0, val, val);
560 rsnd_mod_bset(mod, SCU_SYS_STATUS1, val, val);
563 static bool rsnd_src_error_record_gen2(struct rsnd_mod *mod)
565 u32 val = OUF_SRC(rsnd_mod_id(mod));
568 if ((rsnd_mod_read(mod, SCU_SYS_STATUS0) & val) ||
569 (rsnd_mod_read(mod, SCU_SYS_STATUS1) & val)) {
570 struct rsnd_src *src = rsnd_mod_to_src(mod);
576 /* clear error static */
577 rsnd_src_error_clear_gen2(mod);
582 static int _rsnd_src_start_gen2(struct rsnd_mod *mod)
584 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
585 u32 val = rsnd_io_to_mod_dvc(io) ? 0x01 : 0x11;
587 rsnd_mod_write(mod, SRC_CTRL, val);
589 rsnd_src_error_clear_gen2(mod);
593 rsnd_src_irq_enable_gen2(mod);
598 static int _rsnd_src_stop_gen2(struct rsnd_mod *mod)
600 rsnd_src_irq_disable_gen2(mod);
602 rsnd_mod_write(mod, SRC_CTRL, 0);
604 rsnd_src_error_record_gen2(mod);
606 return rsnd_src_stop(mod);
609 static irqreturn_t rsnd_src_interrupt_gen2(int irq, void *data)
611 struct rsnd_mod *mod = data;
612 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
617 if (rsnd_src_error_record_gen2(mod)) {
618 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
619 struct device *dev = rsnd_priv_to_dev(priv);
621 _rsnd_src_stop_gen2(mod);
622 _rsnd_src_start_gen2(mod);
624 dev_dbg(dev, "%s[%d] restart\n",
625 rsnd_mod_name(mod), rsnd_mod_id(mod));
631 static int rsnd_src_set_convert_rate_gen2(struct rsnd_mod *mod,
632 struct rsnd_dai *rdai)
634 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
635 struct device *dev = rsnd_priv_to_dev(priv);
636 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
637 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
638 struct rsnd_src *src = rsnd_mod_to_src(mod);
639 u32 convert_rate = rsnd_src_convert_rate(src);
643 /* 6 - 1/6 are very enough ratio for SRC_BSDSR */
646 else if (convert_rate > runtime->rate)
647 ratio = 100 * convert_rate / runtime->rate;
649 ratio = 100 * runtime->rate / convert_rate;
652 dev_err(dev, "FSO/FSI ratio error\n");
656 ret = rsnd_src_set_convert_rate(mod, rdai);
660 rsnd_mod_write(mod, SRC_SRCCR, 0x00011110);
663 /* Gen1/Gen2 are not compatible */
664 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 1);
667 switch (rsnd_mod_id(mod)) {
672 rsnd_mod_write(mod, SRC_BSDSR, 0x02400000);
675 rsnd_mod_write(mod, SRC_BSDSR, 0x01800000);
679 rsnd_mod_write(mod, SRC_BSISR, 0x00100060);
684 static int rsnd_src_set_convert_timing_gen2(struct rsnd_mod *mod,
685 struct rsnd_dai *rdai)
687 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
688 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
689 struct rsnd_src *src = rsnd_mod_to_src(mod);
690 u32 convert_rate = rsnd_src_convert_rate(src);
694 ret = rsnd_adg_set_convert_clk_gen2(mod, rdai, io,
698 ret = rsnd_adg_set_convert_timing_gen2(mod, rdai, io);
703 static int rsnd_src_probe_gen2(struct rsnd_mod *mod,
704 struct rsnd_dai *rdai)
706 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
707 struct rsnd_src *src = rsnd_mod_to_src(mod);
708 struct device *dev = rsnd_priv_to_dev(priv);
709 int irq = src->info->irq;
714 * IRQ is not supported on non-DT
716 * rsnd_src_irq_enable_gen2()
718 ret = devm_request_irq(dev, irq,
719 rsnd_src_interrupt_gen2,
723 goto rsnd_src_probe_gen2_fail;
726 ret = rsnd_dma_init(priv,
727 rsnd_mod_to_dma(mod),
728 rsnd_info_is_playback(priv, src),
731 goto rsnd_src_probe_gen2_fail;
733 dev_dbg(dev, "%s[%d] (Gen2) is probed\n",
734 rsnd_mod_name(mod), rsnd_mod_id(mod));
738 rsnd_src_probe_gen2_fail:
739 dev_err(dev, "%s[%d] (Gen2) failed\n",
740 rsnd_mod_name(mod), rsnd_mod_id(mod));
745 static int rsnd_src_remove_gen2(struct rsnd_mod *mod,
746 struct rsnd_dai *rdai)
748 rsnd_dma_quit(rsnd_mod_to_priv(mod), rsnd_mod_to_dma(mod));
753 static int rsnd_src_init_gen2(struct rsnd_mod *mod,
754 struct rsnd_dai *rdai)
758 ret = rsnd_src_init(mod, rdai);
762 ret = rsnd_src_set_convert_rate_gen2(mod, rdai);
766 ret = rsnd_src_set_convert_timing_gen2(mod, rdai);
773 static int rsnd_src_start_gen2(struct rsnd_mod *mod,
774 struct rsnd_dai *rdai)
776 rsnd_dma_start(rsnd_mod_to_dma(mod));
778 return _rsnd_src_start_gen2(mod);
781 static int rsnd_src_stop_gen2(struct rsnd_mod *mod,
782 struct rsnd_dai *rdai)
786 ret = _rsnd_src_stop_gen2(mod);
788 rsnd_dma_stop(rsnd_mod_to_dma(mod));
793 static struct rsnd_mod_ops rsnd_src_gen2_ops = {
795 .probe = rsnd_src_probe_gen2,
796 .remove = rsnd_src_remove_gen2,
797 .init = rsnd_src_init_gen2,
798 .quit = rsnd_src_quit,
799 .start = rsnd_src_start_gen2,
800 .stop = rsnd_src_stop_gen2,
803 struct rsnd_mod *rsnd_src_mod_get(struct rsnd_priv *priv, int id)
805 if (WARN_ON(id < 0 || id >= rsnd_src_nr(priv)))
808 return &((struct rsnd_src *)(priv->src) + id)->mod;
811 static void rsnd_of_parse_src(struct platform_device *pdev,
812 const struct rsnd_of_data *of_data,
813 struct rsnd_priv *priv)
815 struct device_node *src_node;
816 struct device_node *np;
817 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
818 struct rsnd_src_platform_info *src_info;
819 struct device *dev = &pdev->dev;
825 src_node = of_get_child_by_name(dev->of_node, "rcar_sound,src");
829 nr = of_get_child_count(src_node);
831 goto rsnd_of_parse_src_end;
833 src_info = devm_kzalloc(dev,
834 sizeof(struct rsnd_src_platform_info) * nr,
837 dev_err(dev, "src info allocation error\n");
838 goto rsnd_of_parse_src_end;
841 info->src_info = src_info;
842 info->src_info_nr = nr;
845 for_each_child_of_node(src_node, np) {
846 src_info[i].irq = irq_of_parse_and_map(np, 0);
851 rsnd_of_parse_src_end:
852 of_node_put(src_node);
855 int rsnd_src_probe(struct platform_device *pdev,
856 const struct rsnd_of_data *of_data,
857 struct rsnd_priv *priv)
859 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
860 struct device *dev = rsnd_priv_to_dev(priv);
861 struct rsnd_src *src;
862 struct rsnd_mod_ops *ops;
864 char name[RSND_SRC_NAME_SIZE];
868 if (rsnd_is_gen1(priv))
869 ops = &rsnd_src_gen1_ops;
870 if (rsnd_is_gen2(priv))
871 ops = &rsnd_src_gen2_ops;
873 dev_err(dev, "unknown Generation\n");
877 rsnd_of_parse_src(pdev, of_data, priv);
882 nr = info->src_info_nr;
886 src = devm_kzalloc(dev, sizeof(*src) * nr, GFP_KERNEL);
888 dev_err(dev, "SRC allocate failed\n");
895 for_each_rsnd_src(src, priv, i) {
896 snprintf(name, RSND_SRC_NAME_SIZE, "%s.%d",
899 clk = devm_clk_get(dev, name);
903 src->info = &info->src_info[i];
905 rsnd_mod_init(priv, &src->mod, ops, clk, RSND_MOD_SRC, i);
907 dev_dbg(dev, "SRC%d probed\n", i);