1 // SPDX-License-Identifier: GPL-2.0
3 // Renesas R-Car SRC support
5 // Copyright (C) 2013 Renesas Solutions Corp.
6 // Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
9 * You can use Synchronous Sampling Rate Convert (if no DVC)
11 * amixer set "SRC Out Rate" on
13 * amixer set "SRC Out Rate" 96000 // convert rate to 96000Hz
14 * amixer set "SRC Out Rate" 22050 // convert rate to 22050Hz
18 * you can enable below define if you don't need
19 * SSI interrupt status debug message when debugging
20 * see rsnd_print_irq_status()
22 * #define RSND_DEBUG_NO_IRQ_STATUS 1
25 #include <linux/of_irq.h>
28 #define SRC_NAME "src"
30 /* SCU_SYSTEM_STATUS0/1 */
31 #define OUF_SRC(id) ((1 << (id + 16)) | (1 << id))
36 struct rsnd_kctrl_cfg_s sen; /* sync convert enable */
37 struct rsnd_kctrl_cfg_s sync; /* sync convert */
41 #define RSND_SRC_NAME_SIZE 16
43 #define rsnd_src_get(priv, id) ((struct rsnd_src *)(priv->src) + id)
44 #define rsnd_src_nr(priv) ((priv)->src_nr)
45 #define rsnd_src_sync_is_enabled(mod) (rsnd_mod_to_src(mod)->sen.val)
47 #define rsnd_mod_to_src(_mod) \
48 container_of((_mod), struct rsnd_src, mod)
50 #define for_each_rsnd_src(pos, priv, i) \
52 ((i) < rsnd_src_nr(priv)) && \
53 ((pos) = (struct rsnd_src *)(priv)->src + i); \
58 * image of SRC (Sampling Rate Converter)
60 * 96kHz <-> +-----+ 48kHz +-----+ 48kHz +-------+
61 * 48kHz <-> | SRC | <------> | SSI | <-----> | codec |
62 * 44.1kHz <-> +-----+ +-----+ +-------+
67 static void rsnd_src_activation(struct rsnd_mod *mod)
69 rsnd_mod_write(mod, SRC_SWRSR, 0);
70 rsnd_mod_write(mod, SRC_SWRSR, 1);
73 static void rsnd_src_halt(struct rsnd_mod *mod)
75 rsnd_mod_write(mod, SRC_SRCIR, 1);
76 rsnd_mod_write(mod, SRC_SWRSR, 0);
79 static struct dma_chan *rsnd_src_dma_req(struct rsnd_dai_stream *io,
82 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
83 int is_play = rsnd_io_is_play(io);
85 return rsnd_dma_request_channel(rsnd_src_of_node(priv),
87 is_play ? "rx" : "tx");
90 static u32 rsnd_src_convert_rate(struct rsnd_dai_stream *io,
93 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
94 struct rsnd_src *src = rsnd_mod_to_src(mod);
100 if (!rsnd_src_sync_is_enabled(mod))
101 return rsnd_io_converted_rate(io);
103 convert_rate = src->sync.val;
106 convert_rate = rsnd_io_converted_rate(io);
109 convert_rate = runtime->rate;
114 unsigned int rsnd_src_get_rate(struct rsnd_priv *priv,
115 struct rsnd_dai_stream *io,
118 struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
119 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
120 unsigned int rate = 0;
121 int is_play = rsnd_io_is_play(io);
125 * runtime_rate -> [SRC] -> convert_rate
128 * convert_rate -> [SRC] -> runtime_rate
131 if (is_play == is_in)
132 return runtime->rate;
135 * return convert rate if SRC is used,
136 * otherwise, return runtime->rate as usual
139 rate = rsnd_src_convert_rate(io, src_mod);
142 rate = runtime->rate;
147 static const u32 bsdsr_table_pattern1[] = {
148 0x01800000, /* 6 - 1/6 */
149 0x01000000, /* 6 - 1/4 */
150 0x00c00000, /* 6 - 1/3 */
151 0x00800000, /* 6 - 1/2 */
152 0x00600000, /* 6 - 2/3 */
153 0x00400000, /* 6 - 1 */
156 static const u32 bsdsr_table_pattern2[] = {
157 0x02400000, /* 6 - 1/6 */
158 0x01800000, /* 6 - 1/4 */
159 0x01200000, /* 6 - 1/3 */
160 0x00c00000, /* 6 - 1/2 */
161 0x00900000, /* 6 - 2/3 */
162 0x00600000, /* 6 - 1 */
165 static const u32 bsisr_table[] = {
166 0x00100060, /* 6 - 1/6 */
167 0x00100040, /* 6 - 1/4 */
168 0x00100030, /* 6 - 1/3 */
169 0x00100020, /* 6 - 1/2 */
170 0x00100020, /* 6 - 2/3 */
171 0x00100020, /* 6 - 1 */
174 static const u32 chan288888[] = {
175 0x00000006, /* 1 to 2 */
176 0x000001fe, /* 1 to 8 */
177 0x000001fe, /* 1 to 8 */
178 0x000001fe, /* 1 to 8 */
179 0x000001fe, /* 1 to 8 */
180 0x000001fe, /* 1 to 8 */
183 static const u32 chan244888[] = {
184 0x00000006, /* 1 to 2 */
185 0x0000001e, /* 1 to 4 */
186 0x0000001e, /* 1 to 4 */
187 0x000001fe, /* 1 to 8 */
188 0x000001fe, /* 1 to 8 */
189 0x000001fe, /* 1 to 8 */
192 static const u32 chan222222[] = {
193 0x00000006, /* 1 to 2 */
194 0x00000006, /* 1 to 2 */
195 0x00000006, /* 1 to 2 */
196 0x00000006, /* 1 to 2 */
197 0x00000006, /* 1 to 2 */
198 0x00000006, /* 1 to 2 */
201 static void rsnd_src_set_convert_rate(struct rsnd_dai_stream *io,
202 struct rsnd_mod *mod)
204 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
205 struct device *dev = rsnd_priv_to_dev(priv);
206 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
207 int is_play = rsnd_io_is_play(io);
210 u32 ifscr, fsrate, adinr;
212 u32 i_busif, o_busif, tmp;
213 const u32 *bsdsr_table;
222 fin = rsnd_src_get_in_rate(priv, io);
223 fout = rsnd_src_get_out_rate(priv, io);
225 chan = rsnd_runtime_channel_original(io);
227 /* 6 - 1/6 are very enough ratio for SRC_BSDSR */
231 ratio = 100 * fin / fout;
233 ratio = 100 * fout / fin;
236 dev_err(dev, "FSO/FSI ratio error\n");
240 use_src = (fin != fout) | rsnd_src_sync_is_enabled(mod);
245 adinr = rsnd_get_adinr_bit(mod, io) | chan;
248 * SRC_IFSCR / SRC_IFSVR
256 n = (u64)0x0400000 * fin;
262 * SRC_SRCCR / SRC_ROUTE_MODE0
269 if (rsnd_src_sync_is_enabled(mod)) {
271 route |= rsnd_io_is_play(io) ?
272 (0x1 << 24) : (0x1 << 25);
277 * SRC_BSDSR / SRC_BSISR
280 * Combination of Register Setting Related to
281 * FSO/FSI Ratio and Channel, Latency
283 switch (rsnd_mod_id(mod)) {
286 bsdsr_table = bsdsr_table_pattern1;
292 bsdsr_table = bsdsr_table_pattern1;
297 bsdsr_table = bsdsr_table_pattern1;
304 bsdsr_table = bsdsr_table_pattern2;
307 goto convert_rate_err;
311 * E3 need to overwrite
313 if (rsnd_is_e3(priv))
314 switch (rsnd_mod_id(mod)) {
320 for (idx = 0; idx < ARRAY_SIZE(chan222222); idx++)
321 if (chptn[idx] & (1 << chan))
325 idx >= ARRAY_SIZE(chan222222))
326 goto convert_rate_err;
329 tmp = rsnd_get_busif_shift(io, mod);
330 i_busif = ( is_play ? tmp : 0) | 1;
331 o_busif = (!is_play ? tmp : 0) | 1;
333 rsnd_mod_write(mod, SRC_ROUTE_MODE0, route);
335 rsnd_mod_write(mod, SRC_SRCIR, 1); /* initialize */
336 rsnd_mod_write(mod, SRC_ADINR, adinr);
337 rsnd_mod_write(mod, SRC_IFSCR, ifscr);
338 rsnd_mod_write(mod, SRC_IFSVR, fsrate);
339 rsnd_mod_write(mod, SRC_SRCCR, cr);
340 rsnd_mod_write(mod, SRC_BSDSR, bsdsr_table[idx]);
341 rsnd_mod_write(mod, SRC_BSISR, bsisr_table[idx]);
342 rsnd_mod_write(mod, SRC_SRCIR, 0); /* cancel initialize */
344 rsnd_mod_write(mod, SRC_I_BUSIF_MODE, i_busif);
345 rsnd_mod_write(mod, SRC_O_BUSIF_MODE, o_busif);
347 rsnd_mod_write(mod, SRC_BUSIF_DALIGN, rsnd_get_dalign(mod, io));
349 rsnd_adg_set_src_timesel_gen2(mod, io, fin, fout);
354 dev_err(dev, "unknown BSDSR/BSDIR settings\n");
357 static int rsnd_src_irq(struct rsnd_mod *mod,
358 struct rsnd_dai_stream *io,
359 struct rsnd_priv *priv,
362 struct rsnd_src *src = rsnd_mod_to_src(mod);
363 u32 sys_int_val, int_val, sys_int_mask;
365 int id = rsnd_mod_id(mod);
368 sys_int_mask = OUF_SRC(id);
372 * IRQ is not supported on non-DT
376 if ((irq <= 0) || !enable) {
384 * ignore over flow error when rsnd_src_sync_is_enabled()
386 if (rsnd_src_sync_is_enabled(mod))
387 sys_int_val = sys_int_val & 0xffff;
389 rsnd_mod_write(mod, SRC_INT_ENABLE0, int_val);
390 rsnd_mod_bset(mod, SCU_SYS_INT_EN0, sys_int_mask, sys_int_val);
391 rsnd_mod_bset(mod, SCU_SYS_INT_EN1, sys_int_mask, sys_int_val);
396 static void rsnd_src_status_clear(struct rsnd_mod *mod)
398 u32 val = OUF_SRC(rsnd_mod_id(mod));
400 rsnd_mod_write(mod, SCU_SYS_STATUS0, val);
401 rsnd_mod_write(mod, SCU_SYS_STATUS1, val);
404 static bool rsnd_src_error_occurred(struct rsnd_mod *mod)
406 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
407 struct device *dev = rsnd_priv_to_dev(priv);
409 u32 status0, status1;
412 val0 = val1 = OUF_SRC(rsnd_mod_id(mod));
417 * ignore over flow error when rsnd_src_sync_is_enabled()
419 if (rsnd_src_sync_is_enabled(mod))
420 val0 = val0 & 0xffff;
422 status0 = rsnd_mod_read(mod, SCU_SYS_STATUS0);
423 status1 = rsnd_mod_read(mod, SCU_SYS_STATUS1);
424 if ((status0 & val0) || (status1 & val1)) {
425 rsnd_print_irq_status(dev, "%s err status : 0x%08x, 0x%08x\n",
426 rsnd_mod_name(mod), status0, status1);
434 static int rsnd_src_start(struct rsnd_mod *mod,
435 struct rsnd_dai_stream *io,
436 struct rsnd_priv *priv)
443 * Enable SRC output if you want to use sync convert together with DVC
445 val = (rsnd_io_to_mod_dvc(io) && !rsnd_src_sync_is_enabled(mod)) ?
448 rsnd_mod_write(mod, SRC_CTRL, val);
453 static int rsnd_src_stop(struct rsnd_mod *mod,
454 struct rsnd_dai_stream *io,
455 struct rsnd_priv *priv)
457 rsnd_mod_write(mod, SRC_CTRL, 0);
462 static int rsnd_src_init(struct rsnd_mod *mod,
463 struct rsnd_dai_stream *io,
464 struct rsnd_priv *priv)
466 struct rsnd_src *src = rsnd_mod_to_src(mod);
469 /* reset sync convert_rate */
472 ret = rsnd_mod_power_on(mod);
476 rsnd_src_activation(mod);
478 rsnd_src_set_convert_rate(io, mod);
480 rsnd_src_status_clear(mod);
485 static int rsnd_src_quit(struct rsnd_mod *mod,
486 struct rsnd_dai_stream *io,
487 struct rsnd_priv *priv)
489 struct rsnd_src *src = rsnd_mod_to_src(mod);
493 rsnd_mod_power_off(mod);
495 /* reset sync convert_rate */
501 static void __rsnd_src_interrupt(struct rsnd_mod *mod,
502 struct rsnd_dai_stream *io)
504 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
507 spin_lock(&priv->lock);
509 /* ignore all cases if not working */
510 if (!rsnd_io_is_working(io))
511 goto rsnd_src_interrupt_out;
513 if (rsnd_src_error_occurred(mod))
516 rsnd_src_status_clear(mod);
517 rsnd_src_interrupt_out:
519 spin_unlock(&priv->lock);
522 snd_pcm_stop_xrun(io->substream);
525 static irqreturn_t rsnd_src_interrupt(int irq, void *data)
527 struct rsnd_mod *mod = data;
529 rsnd_mod_interrupt(mod, __rsnd_src_interrupt);
534 static int rsnd_src_probe_(struct rsnd_mod *mod,
535 struct rsnd_dai_stream *io,
536 struct rsnd_priv *priv)
538 struct rsnd_src *src = rsnd_mod_to_src(mod);
539 struct device *dev = rsnd_priv_to_dev(priv);
545 * IRQ is not supported on non-DT
549 ret = devm_request_irq(dev, irq,
557 ret = rsnd_dma_attach(io, mod, &src->dma);
562 static int rsnd_src_pcm_new(struct rsnd_mod *mod,
563 struct rsnd_dai_stream *io,
564 struct snd_soc_pcm_runtime *rtd)
566 struct rsnd_src *src = rsnd_mod_to_src(mod);
570 * enable SRC sync convert if possible
574 * It can't use SRC Synchronous convert
575 * when Capture if it uses CMD
577 if (rsnd_io_to_mod_cmd(io) && !rsnd_io_is_play(io))
581 * enable sync convert
583 ret = rsnd_kctrl_new_s(mod, io, rtd,
584 rsnd_io_is_play(io) ?
585 "SRC Out Rate Switch" :
586 "SRC In Rate Switch",
587 rsnd_kctrl_accept_anytime,
588 rsnd_src_set_convert_rate,
593 ret = rsnd_kctrl_new_s(mod, io, rtd,
594 rsnd_io_is_play(io) ?
597 rsnd_kctrl_accept_runtime,
598 rsnd_src_set_convert_rate,
604 #ifdef CONFIG_DEBUG_FS
605 static void rsnd_src_debug_info(struct seq_file *m,
606 struct rsnd_dai_stream *io,
607 struct rsnd_mod *mod)
609 rsnd_debugfs_mod_reg_show(m, mod, RSND_GEN2_SCU,
610 rsnd_mod_id(mod) * 0x20, 0x20);
612 rsnd_debugfs_mod_reg_show(m, mod, RSND_GEN2_SCU,
615 rsnd_debugfs_mod_reg_show(m, mod, RSND_GEN2_SCU,
616 0x200 + rsnd_mod_id(mod) * 0x40, 0x40);
618 #define DEBUG_INFO .debug_info = rsnd_src_debug_info
623 static struct rsnd_mod_ops rsnd_src_ops = {
625 .dma_req = rsnd_src_dma_req,
626 .probe = rsnd_src_probe_,
627 .init = rsnd_src_init,
628 .quit = rsnd_src_quit,
629 .start = rsnd_src_start,
630 .stop = rsnd_src_stop,
632 .pcm_new = rsnd_src_pcm_new,
633 .get_status = rsnd_mod_get_status,
637 struct rsnd_mod *rsnd_src_mod_get(struct rsnd_priv *priv, int id)
639 if (WARN_ON(id < 0 || id >= rsnd_src_nr(priv)))
642 return rsnd_mod_get(rsnd_src_get(priv, id));
645 int rsnd_src_probe(struct rsnd_priv *priv)
647 struct device_node *node;
648 struct device_node *np;
649 struct device *dev = rsnd_priv_to_dev(priv);
650 struct rsnd_src *src;
652 char name[RSND_SRC_NAME_SIZE];
655 /* This driver doesn't support Gen1 at this point */
656 if (rsnd_is_gen1(priv))
659 node = rsnd_src_of_node(priv);
661 return 0; /* not used is not error */
663 nr = rsnd_node_count(priv, node, SRC_NAME);
666 goto rsnd_src_probe_done;
669 src = devm_kcalloc(dev, nr, sizeof(*src), GFP_KERNEL);
672 goto rsnd_src_probe_done;
679 for_each_child_of_node(node, np) {
680 if (!of_device_is_available(np))
683 i = rsnd_node_fixed_index(dev, np, SRC_NAME, i);
687 goto rsnd_src_probe_done;
690 src = rsnd_src_get(priv, i);
692 snprintf(name, RSND_SRC_NAME_SIZE, "%s.%d",
695 src->irq = irq_of_parse_and_map(np, 0);
699 goto rsnd_src_probe_done;
702 clk = devm_clk_get(dev, name);
706 goto rsnd_src_probe_done;
709 ret = rsnd_mod_init(priv, rsnd_mod_get(src),
710 &rsnd_src_ops, clk, RSND_MOD_SRC, i);
713 goto rsnd_src_probe_done;
728 void rsnd_src_remove(struct rsnd_priv *priv)
730 struct rsnd_src *src;
733 for_each_rsnd_src(src, priv, i) {
734 rsnd_mod_quit(rsnd_mod_get(src));