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 static struct dma_chan *rsnd_src_dma_req(struct rsnd_mod *mod)
119 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
120 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
121 int is_play = rsnd_io_is_play(io);
123 return rsnd_dma_request_channel(rsnd_src_of_node(priv),
125 is_play ? "rx" : "tx");
128 int rsnd_src_ssiu_start(struct rsnd_mod *ssi_mod,
131 struct rsnd_dai_stream *io = rsnd_mod_to_io(ssi_mod);
132 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
133 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
134 int ssi_id = rsnd_mod_id(ssi_mod);
139 rsnd_mod_bset(ssi_mod, SSI_MODE0, (1 << ssi_id),
140 !use_busif << ssi_id);
145 if (rsnd_ssi_is_pin_sharing(ssi_mod)) {
160 rsnd_mod_bset(ssi_mod, SSI_MODE1,
162 rsnd_rdai_is_clk_master(rdai) ?
163 0x2 << shift : 0x1 << shift);
167 * DMA settings for SSIU
170 u32 val = 0x76543210;
173 rsnd_mod_write(ssi_mod, SSI_BUSIF_ADINR,
174 rsnd_get_adinr(ssi_mod));
175 rsnd_mod_write(ssi_mod, SSI_BUSIF_MODE, 1);
176 rsnd_mod_write(ssi_mod, SSI_CTRL, 0x1);
178 mask <<= runtime->channels * 4;
181 switch (runtime->sample_bits) {
183 val |= 0x67452301 & ~mask;
186 val |= 0x76543210 & ~mask;
189 rsnd_mod_write(ssi_mod, BUSIF_DALIGN, val);
196 int rsnd_src_ssiu_stop(struct rsnd_mod *ssi_mod)
199 * DMA settings for SSIU
201 rsnd_mod_write(ssi_mod, SSI_CTRL, 0);
206 int rsnd_src_ssi_irq_enable(struct rsnd_mod *ssi_mod)
208 struct rsnd_priv *priv = rsnd_mod_to_priv(ssi_mod);
210 if (rsnd_is_gen1(priv))
213 /* enable SSI interrupt if Gen2 */
214 if (rsnd_ssi_is_dma_mode(ssi_mod))
215 rsnd_mod_write(ssi_mod, INT_ENABLE, 0x0e000000);
217 rsnd_mod_write(ssi_mod, INT_ENABLE, 0x0f000000);
222 int rsnd_src_ssi_irq_disable(struct rsnd_mod *ssi_mod)
224 struct rsnd_priv *priv = rsnd_mod_to_priv(ssi_mod);
226 if (rsnd_is_gen1(priv))
229 /* disable SSI interrupt if Gen2 */
230 rsnd_mod_write(ssi_mod, INT_ENABLE, 0x00000000);
235 unsigned int rsnd_src_get_ssi_rate(struct rsnd_priv *priv,
236 struct rsnd_dai_stream *io,
237 struct snd_pcm_runtime *runtime)
239 struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
240 struct rsnd_src *src;
241 unsigned int rate = 0;
244 src = rsnd_mod_to_src(src_mod);
247 * return convert rate if SRC is used,
248 * otherwise, return runtime->rate as usual
250 rate = rsnd_src_convert_rate(src);
254 rate = runtime->rate;
259 static int rsnd_src_set_convert_rate(struct rsnd_mod *mod)
261 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
262 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
263 struct rsnd_src *src = rsnd_mod_to_src(mod);
264 u32 convert_rate = rsnd_src_convert_rate(src);
268 fsrate = 0x0400000 / convert_rate * runtime->rate;
270 /* set/clear soft reset */
271 rsnd_mod_write(mod, SRC_SWRSR, 0);
272 rsnd_mod_write(mod, SRC_SWRSR, 1);
274 /* Set channel number and output bit length */
275 rsnd_mod_write(mod, SRC_ADINR, rsnd_get_adinr(mod));
277 /* Enable the initial value of IFS */
279 rsnd_mod_write(mod, SRC_IFSCR, 1);
281 /* Set initial value of IFS */
282 rsnd_mod_write(mod, SRC_IFSVR, fsrate);
285 /* use DMA transfer */
286 rsnd_mod_write(mod, SRC_BUSIF_MODE, 1);
291 static int rsnd_src_init(struct rsnd_mod *mod)
293 struct rsnd_src *src = rsnd_mod_to_src(mod);
295 rsnd_mod_hw_start(mod);
300 * Initialize the operation of the SRC internal circuits
301 * see rsnd_src_start()
303 rsnd_mod_write(mod, SRC_SRCIR, 1);
308 static int rsnd_src_quit(struct rsnd_mod *mod,
309 struct rsnd_priv *priv)
311 struct rsnd_src *src = rsnd_mod_to_src(mod);
312 struct device *dev = rsnd_priv_to_dev(priv);
314 rsnd_mod_hw_stop(mod);
317 dev_warn(dev, "%s[%d] under/over flow err = %d\n",
318 rsnd_mod_name(mod), rsnd_mod_id(mod), src->err);
323 static int rsnd_src_start(struct rsnd_mod *mod)
326 * Cancel the initialization and operate the SRC function
327 * see rsnd_src_init()
329 rsnd_mod_write(mod, SRC_SRCIR, 0);
334 static int rsnd_src_stop(struct rsnd_mod *mod)
343 static int rsnd_src_set_route_gen1(struct rsnd_mod *mod)
345 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
346 struct src_route_config {
353 { 0x7, 12, }, /* 3 */
354 { 0x7, 16, }, /* 4 */
355 { 0x7, 20, }, /* 5 */
356 { 0x7, 24, }, /* 6 */
357 { 0x3, 28, }, /* 7 */
358 { 0x3, 30, }, /* 8 */
364 id = rsnd_mod_id(mod);
365 if (id < 0 || id >= ARRAY_SIZE(routes))
371 val = rsnd_io_is_play(io) ? 0x1 : 0x2;
372 val = val << routes[id].shift;
373 mask = routes[id].mask << routes[id].shift;
375 rsnd_mod_bset(mod, SRC_ROUTE_SEL, mask, val);
380 static int rsnd_src_set_convert_timing_gen1(struct rsnd_mod *mod)
382 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
383 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
384 struct rsnd_src *src = rsnd_mod_to_src(mod);
385 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
386 u32 convert_rate = rsnd_src_convert_rate(src);
390 int id = rsnd_mod_id(mod);
396 shift = (id % 4) * 8;
397 mask = 0x1F << shift;
400 * ADG is used as source clock if SRC was used,
401 * then, SSI WS is used as destination clock.
402 * SSI WS is used as source clock if SRC is not used
403 * (when playback, source/destination become reverse when capture)
409 ret = rsnd_adg_set_convert_clk_gen1(priv, mod,
412 } else if (8 == id) {
413 /* use SSI WS, but SRU8 is special */
417 val = (id + 1) << shift;
425 rsnd_mod_bset(mod, SRC_TMG_SEL0, mask, val);
428 rsnd_mod_bset(mod, SRC_TMG_SEL1, mask, val);
431 rsnd_mod_bset(mod, SRC_TMG_SEL2, mask, val);
438 static int rsnd_src_set_convert_rate_gen1(struct rsnd_mod *mod)
440 struct rsnd_src *src = rsnd_mod_to_src(mod);
443 ret = rsnd_src_set_convert_rate(mod);
447 /* Select SRC mode (fixed value) */
448 rsnd_mod_write(mod, SRC_SRCCR, 0x00010110);
450 /* Set the restriction value of the FS ratio (98%) */
451 rsnd_mod_write(mod, SRC_MNFSR,
452 rsnd_mod_read(mod, SRC_IFSVR) / 100 * 98);
454 /* Gen1/Gen2 are not compatible */
455 if (rsnd_src_convert_rate(src))
456 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 1);
458 /* no SRC_BFSSR settings, since SRC_SRCCR::BUFMD is 0 */
463 static int rsnd_src_probe_gen1(struct rsnd_mod *mod,
464 struct rsnd_priv *priv)
466 struct device *dev = rsnd_priv_to_dev(priv);
468 dev_dbg(dev, "%s[%d] (Gen1) is probed\n",
469 rsnd_mod_name(mod), rsnd_mod_id(mod));
474 static int rsnd_src_init_gen1(struct rsnd_mod *mod,
475 struct rsnd_priv *priv)
479 ret = rsnd_src_init(mod);
483 ret = rsnd_src_set_route_gen1(mod);
487 ret = rsnd_src_set_convert_rate_gen1(mod);
491 ret = rsnd_src_set_convert_timing_gen1(mod);
498 static int rsnd_src_start_gen1(struct rsnd_mod *mod,
499 struct rsnd_priv *priv)
501 int id = rsnd_mod_id(mod);
503 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), (1 << id));
505 return rsnd_src_start(mod);
508 static int rsnd_src_stop_gen1(struct rsnd_mod *mod,
509 struct rsnd_priv *priv)
511 int id = rsnd_mod_id(mod);
513 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), 0);
515 return rsnd_src_stop(mod);
518 static struct rsnd_mod_ops rsnd_src_gen1_ops = {
520 .dma_req = rsnd_src_dma_req,
521 .probe = rsnd_src_probe_gen1,
522 .init = rsnd_src_init_gen1,
523 .quit = rsnd_src_quit,
524 .start = rsnd_src_start_gen1,
525 .stop = rsnd_src_stop_gen1,
531 #define rsnd_src_irq_enable_gen2(mod) rsnd_src_irq_ctrol_gen2(mod, 1)
532 #define rsnd_src_irq_disable_gen2(mod) rsnd_src_irq_ctrol_gen2(mod, 0)
533 static void rsnd_src_irq_ctrol_gen2(struct rsnd_mod *mod, int enable)
535 struct rsnd_src *src = rsnd_mod_to_src(mod);
536 u32 sys_int_val, int_val, sys_int_mask;
537 int irq = src->info->irq;
538 int id = rsnd_mod_id(mod);
541 sys_int_mask = OUF_SRC(id);
545 * IRQ is not supported on non-DT
547 * rsnd_src_probe_gen2()
549 if ((irq <= 0) || !enable) {
554 rsnd_mod_write(mod, SRC_INT_ENABLE0, int_val);
555 rsnd_mod_bset(mod, SCU_SYS_INT_EN0, sys_int_mask, sys_int_val);
556 rsnd_mod_bset(mod, SCU_SYS_INT_EN1, sys_int_mask, sys_int_val);
559 static void rsnd_src_error_clear_gen2(struct rsnd_mod *mod)
561 u32 val = OUF_SRC(rsnd_mod_id(mod));
563 rsnd_mod_bset(mod, SCU_SYS_STATUS0, val, val);
564 rsnd_mod_bset(mod, SCU_SYS_STATUS1, val, val);
567 static bool rsnd_src_error_record_gen2(struct rsnd_mod *mod)
569 u32 val = OUF_SRC(rsnd_mod_id(mod));
572 if ((rsnd_mod_read(mod, SCU_SYS_STATUS0) & val) ||
573 (rsnd_mod_read(mod, SCU_SYS_STATUS1) & val)) {
574 struct rsnd_src *src = rsnd_mod_to_src(mod);
580 /* clear error static */
581 rsnd_src_error_clear_gen2(mod);
586 static int _rsnd_src_start_gen2(struct rsnd_mod *mod)
588 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
589 u32 val = rsnd_io_to_mod_dvc(io) ? 0x01 : 0x11;
591 rsnd_mod_write(mod, SRC_CTRL, val);
593 rsnd_src_error_clear_gen2(mod);
597 rsnd_src_irq_enable_gen2(mod);
602 static int _rsnd_src_stop_gen2(struct rsnd_mod *mod)
604 rsnd_src_irq_disable_gen2(mod);
606 rsnd_mod_write(mod, SRC_CTRL, 0);
608 rsnd_src_error_record_gen2(mod);
610 return rsnd_src_stop(mod);
613 static irqreturn_t rsnd_src_interrupt_gen2(int irq, void *data)
615 struct rsnd_mod *mod = data;
616 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
621 if (rsnd_src_error_record_gen2(mod)) {
622 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
623 struct rsnd_src *src = rsnd_mod_to_src(mod);
624 struct device *dev = rsnd_priv_to_dev(priv);
626 dev_dbg(dev, "%s[%d] restart\n",
627 rsnd_mod_name(mod), rsnd_mod_id(mod));
629 _rsnd_src_stop_gen2(mod);
631 _rsnd_src_start_gen2(mod);
633 dev_warn(dev, "no more SRC restart\n");
639 static int rsnd_src_set_convert_rate_gen2(struct rsnd_mod *mod)
641 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
642 struct device *dev = rsnd_priv_to_dev(priv);
643 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
644 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
645 struct rsnd_src *src = rsnd_mod_to_src(mod);
646 u32 convert_rate = rsnd_src_convert_rate(src);
650 /* 6 - 1/6 are very enough ratio for SRC_BSDSR */
653 else if (convert_rate > runtime->rate)
654 ratio = 100 * convert_rate / runtime->rate;
656 ratio = 100 * runtime->rate / convert_rate;
659 dev_err(dev, "FSO/FSI ratio error\n");
663 ret = rsnd_src_set_convert_rate(mod);
667 rsnd_mod_write(mod, SRC_SRCCR, 0x00011110);
670 /* Gen1/Gen2 are not compatible */
671 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 1);
674 switch (rsnd_mod_id(mod)) {
679 rsnd_mod_write(mod, SRC_BSDSR, 0x02400000);
682 rsnd_mod_write(mod, SRC_BSDSR, 0x01800000);
686 rsnd_mod_write(mod, SRC_BSISR, 0x00100060);
691 static int rsnd_src_set_convert_timing_gen2(struct rsnd_mod *mod)
693 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
694 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
695 struct rsnd_src *src = rsnd_mod_to_src(mod);
696 u32 convert_rate = rsnd_src_convert_rate(src);
700 ret = rsnd_adg_set_convert_clk_gen2(mod, io,
704 ret = rsnd_adg_set_convert_timing_gen2(mod, io);
709 static int rsnd_src_probe_gen2(struct rsnd_mod *mod,
710 struct rsnd_priv *priv)
712 struct rsnd_src *src = rsnd_mod_to_src(mod);
713 struct device *dev = rsnd_priv_to_dev(priv);
714 int irq = src->info->irq;
719 * IRQ is not supported on non-DT
721 * rsnd_src_irq_enable_gen2()
723 ret = devm_request_irq(dev, irq,
724 rsnd_src_interrupt_gen2,
728 goto rsnd_src_probe_gen2_fail;
731 ret = rsnd_dma_init(priv,
732 rsnd_mod_to_dma(mod),
735 goto rsnd_src_probe_gen2_fail;
737 dev_dbg(dev, "%s[%d] (Gen2) is probed\n",
738 rsnd_mod_name(mod), rsnd_mod_id(mod));
742 rsnd_src_probe_gen2_fail:
743 dev_err(dev, "%s[%d] (Gen2) failed\n",
744 rsnd_mod_name(mod), rsnd_mod_id(mod));
749 static int rsnd_src_remove_gen2(struct rsnd_mod *mod,
750 struct rsnd_priv *priv)
752 rsnd_dma_quit(rsnd_mod_to_dma(mod));
757 static int rsnd_src_init_gen2(struct rsnd_mod *mod,
758 struct rsnd_priv *priv)
762 ret = rsnd_src_init(mod);
766 ret = rsnd_src_set_convert_rate_gen2(mod);
770 ret = rsnd_src_set_convert_timing_gen2(mod);
777 static int rsnd_src_start_gen2(struct rsnd_mod *mod,
778 struct rsnd_priv *priv)
780 rsnd_dma_start(rsnd_mod_to_dma(mod));
782 return _rsnd_src_start_gen2(mod);
785 static int rsnd_src_stop_gen2(struct rsnd_mod *mod,
786 struct rsnd_priv *priv)
790 ret = _rsnd_src_stop_gen2(mod);
792 rsnd_dma_stop(rsnd_mod_to_dma(mod));
797 static struct rsnd_mod_ops rsnd_src_gen2_ops = {
799 .dma_req = rsnd_src_dma_req,
800 .probe = rsnd_src_probe_gen2,
801 .remove = rsnd_src_remove_gen2,
802 .init = rsnd_src_init_gen2,
803 .quit = rsnd_src_quit,
804 .start = rsnd_src_start_gen2,
805 .stop = rsnd_src_stop_gen2,
808 struct rsnd_mod *rsnd_src_mod_get(struct rsnd_priv *priv, int id)
810 if (WARN_ON(id < 0 || id >= rsnd_src_nr(priv)))
813 return &((struct rsnd_src *)(priv->src) + id)->mod;
816 static void rsnd_of_parse_src(struct platform_device *pdev,
817 const struct rsnd_of_data *of_data,
818 struct rsnd_priv *priv)
820 struct device_node *src_node;
821 struct device_node *np;
822 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
823 struct rsnd_src_platform_info *src_info;
824 struct device *dev = &pdev->dev;
830 src_node = rsnd_src_of_node(priv);
834 nr = of_get_child_count(src_node);
836 goto rsnd_of_parse_src_end;
838 src_info = devm_kzalloc(dev,
839 sizeof(struct rsnd_src_platform_info) * nr,
842 dev_err(dev, "src info allocation error\n");
843 goto rsnd_of_parse_src_end;
846 info->src_info = src_info;
847 info->src_info_nr = nr;
850 for_each_child_of_node(src_node, np) {
851 src_info[i].irq = irq_of_parse_and_map(np, 0);
856 rsnd_of_parse_src_end:
857 of_node_put(src_node);
860 int rsnd_src_probe(struct platform_device *pdev,
861 const struct rsnd_of_data *of_data,
862 struct rsnd_priv *priv)
864 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
865 struct device *dev = rsnd_priv_to_dev(priv);
866 struct rsnd_src *src;
867 struct rsnd_mod_ops *ops;
869 char name[RSND_SRC_NAME_SIZE];
873 if (rsnd_is_gen1(priv))
874 ops = &rsnd_src_gen1_ops;
875 if (rsnd_is_gen2(priv))
876 ops = &rsnd_src_gen2_ops;
878 dev_err(dev, "unknown Generation\n");
882 rsnd_of_parse_src(pdev, of_data, priv);
887 nr = info->src_info_nr;
891 src = devm_kzalloc(dev, sizeof(*src) * nr, GFP_KERNEL);
893 dev_err(dev, "SRC allocate failed\n");
900 for_each_rsnd_src(src, priv, i) {
901 snprintf(name, RSND_SRC_NAME_SIZE, "%s.%d",
904 clk = devm_clk_get(dev, name);
908 src->info = &info->src_info[i];
910 ret = rsnd_mod_init(&src->mod, ops, clk, RSND_MOD_SRC, i);
914 dev_dbg(dev, "SRC%d probed\n", i);
920 void rsnd_src_remove(struct platform_device *pdev,
921 struct rsnd_priv *priv)
923 struct rsnd_src *src;
926 for_each_rsnd_src(src, priv, i) {
927 rsnd_mod_quit(&src->mod);