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
27 #define SRC_NAME "src"
29 /* SCU_SYSTEM_STATUS0/1 */
30 #define OUF_SRC(id) ((1 << (id + 16)) | (1 << id))
35 struct rsnd_kctrl_cfg_s sen; /* sync convert enable */
36 struct rsnd_kctrl_cfg_s sync; /* sync convert */
40 #define RSND_SRC_NAME_SIZE 16
42 #define rsnd_src_get(priv, id) ((struct rsnd_src *)(priv->src) + id)
43 #define rsnd_src_nr(priv) ((priv)->src_nr)
44 #define rsnd_src_sync_is_enabled(mod) (rsnd_mod_to_src(mod)->sen.val)
46 #define rsnd_mod_to_src(_mod) \
47 container_of((_mod), struct rsnd_src, mod)
49 #define for_each_rsnd_src(pos, priv, i) \
51 ((i) < rsnd_src_nr(priv)) && \
52 ((pos) = (struct rsnd_src *)(priv)->src + i); \
57 * image of SRC (Sampling Rate Converter)
59 * 96kHz <-> +-----+ 48kHz +-----+ 48kHz +-------+
60 * 48kHz <-> | SRC | <------> | SSI | <-----> | codec |
61 * 44.1kHz <-> +-----+ +-----+ +-------+
66 static void rsnd_src_activation(struct rsnd_mod *mod)
68 rsnd_mod_write(mod, SRC_SWRSR, 0);
69 rsnd_mod_write(mod, SRC_SWRSR, 1);
72 static void rsnd_src_halt(struct rsnd_mod *mod)
74 rsnd_mod_write(mod, SRC_SRCIR, 1);
75 rsnd_mod_write(mod, SRC_SWRSR, 0);
78 static struct dma_chan *rsnd_src_dma_req(struct rsnd_dai_stream *io,
81 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
82 int is_play = rsnd_io_is_play(io);
84 return rsnd_dma_request_channel(rsnd_src_of_node(priv),
86 is_play ? "rx" : "tx");
89 static u32 rsnd_src_convert_rate(struct rsnd_dai_stream *io,
92 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
93 struct rsnd_src *src = rsnd_mod_to_src(mod);
99 if (!rsnd_src_sync_is_enabled(mod))
100 return rsnd_io_converted_rate(io);
102 convert_rate = src->sync.val;
105 convert_rate = rsnd_io_converted_rate(io);
108 convert_rate = runtime->rate;
113 unsigned int rsnd_src_get_rate(struct rsnd_priv *priv,
114 struct rsnd_dai_stream *io,
117 struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
118 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
119 unsigned int rate = 0;
120 int is_play = rsnd_io_is_play(io);
124 * runtime_rate -> [SRC] -> convert_rate
127 * convert_rate -> [SRC] -> runtime_rate
130 if (is_play == is_in)
131 return runtime->rate;
134 * return convert rate if SRC is used,
135 * otherwise, return runtime->rate as usual
138 rate = rsnd_src_convert_rate(io, src_mod);
141 rate = runtime->rate;
146 static const u32 bsdsr_table_pattern1[] = {
147 0x01800000, /* 6 - 1/6 */
148 0x01000000, /* 6 - 1/4 */
149 0x00c00000, /* 6 - 1/3 */
150 0x00800000, /* 6 - 1/2 */
151 0x00600000, /* 6 - 2/3 */
152 0x00400000, /* 6 - 1 */
155 static const u32 bsdsr_table_pattern2[] = {
156 0x02400000, /* 6 - 1/6 */
157 0x01800000, /* 6 - 1/4 */
158 0x01200000, /* 6 - 1/3 */
159 0x00c00000, /* 6 - 1/2 */
160 0x00900000, /* 6 - 2/3 */
161 0x00600000, /* 6 - 1 */
164 static const u32 bsisr_table[] = {
165 0x00100060, /* 6 - 1/6 */
166 0x00100040, /* 6 - 1/4 */
167 0x00100030, /* 6 - 1/3 */
168 0x00100020, /* 6 - 1/2 */
169 0x00100020, /* 6 - 2/3 */
170 0x00100020, /* 6 - 1 */
173 static const u32 chan288888[] = {
174 0x00000006, /* 1 to 2 */
175 0x000001fe, /* 1 to 8 */
176 0x000001fe, /* 1 to 8 */
177 0x000001fe, /* 1 to 8 */
178 0x000001fe, /* 1 to 8 */
179 0x000001fe, /* 1 to 8 */
182 static const u32 chan244888[] = {
183 0x00000006, /* 1 to 2 */
184 0x0000001e, /* 1 to 4 */
185 0x0000001e, /* 1 to 4 */
186 0x000001fe, /* 1 to 8 */
187 0x000001fe, /* 1 to 8 */
188 0x000001fe, /* 1 to 8 */
191 static const u32 chan222222[] = {
192 0x00000006, /* 1 to 2 */
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 */
200 static void rsnd_src_set_convert_rate(struct rsnd_dai_stream *io,
201 struct rsnd_mod *mod)
203 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
204 struct device *dev = rsnd_priv_to_dev(priv);
205 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
206 int is_play = rsnd_io_is_play(io);
209 u32 ifscr, fsrate, adinr;
211 u32 i_busif, o_busif, tmp;
212 const u32 *bsdsr_table;
221 fin = rsnd_src_get_in_rate(priv, io);
222 fout = rsnd_src_get_out_rate(priv, io);
224 chan = rsnd_runtime_channel_original(io);
226 /* 6 - 1/6 are very enough ratio for SRC_BSDSR */
230 ratio = 100 * fin / fout;
232 ratio = 100 * fout / fin;
235 dev_err(dev, "FSO/FSI ratio error\n");
239 use_src = (fin != fout) | rsnd_src_sync_is_enabled(mod);
244 adinr = rsnd_get_adinr_bit(mod, io) | chan;
247 * SRC_IFSCR / SRC_IFSVR
255 n = (u64)0x0400000 * fin;
261 * SRC_SRCCR / SRC_ROUTE_MODE0
268 if (rsnd_src_sync_is_enabled(mod)) {
270 route |= rsnd_io_is_play(io) ?
271 (0x1 << 24) : (0x1 << 25);
276 * SRC_BSDSR / SRC_BSISR
279 * Combination of Register Setting Related to
280 * FSO/FSI Ratio and Channel, Latency
282 switch (rsnd_mod_id(mod)) {
285 bsdsr_table = bsdsr_table_pattern1;
291 bsdsr_table = bsdsr_table_pattern1;
296 bsdsr_table = bsdsr_table_pattern1;
303 bsdsr_table = bsdsr_table_pattern2;
306 goto convert_rate_err;
310 * E3 need to overwrite
312 if (rsnd_is_e3(priv))
313 switch (rsnd_mod_id(mod)) {
319 for (idx = 0; idx < ARRAY_SIZE(chan222222); idx++)
320 if (chptn[idx] & (1 << chan))
324 idx >= ARRAY_SIZE(chan222222))
325 goto convert_rate_err;
328 tmp = rsnd_get_busif_shift(io, mod);
329 i_busif = ( is_play ? tmp : 0) | 1;
330 o_busif = (!is_play ? tmp : 0) | 1;
332 rsnd_mod_write(mod, SRC_ROUTE_MODE0, route);
334 rsnd_mod_write(mod, SRC_SRCIR, 1); /* initialize */
335 rsnd_mod_write(mod, SRC_ADINR, adinr);
336 rsnd_mod_write(mod, SRC_IFSCR, ifscr);
337 rsnd_mod_write(mod, SRC_IFSVR, fsrate);
338 rsnd_mod_write(mod, SRC_SRCCR, cr);
339 rsnd_mod_write(mod, SRC_BSDSR, bsdsr_table[idx]);
340 rsnd_mod_write(mod, SRC_BSISR, bsisr_table[idx]);
341 rsnd_mod_write(mod, SRC_SRCIR, 0); /* cancel initialize */
343 rsnd_mod_write(mod, SRC_I_BUSIF_MODE, i_busif);
344 rsnd_mod_write(mod, SRC_O_BUSIF_MODE, o_busif);
346 rsnd_mod_write(mod, SRC_BUSIF_DALIGN, rsnd_get_dalign(mod, io));
348 rsnd_adg_set_src_timesel_gen2(mod, io, fin, fout);
353 dev_err(dev, "unknown BSDSR/BSDIR settings\n");
356 static int rsnd_src_irq(struct rsnd_mod *mod,
357 struct rsnd_dai_stream *io,
358 struct rsnd_priv *priv,
361 struct rsnd_src *src = rsnd_mod_to_src(mod);
362 u32 sys_int_val, int_val, sys_int_mask;
364 int id = rsnd_mod_id(mod);
367 sys_int_mask = OUF_SRC(id);
371 * IRQ is not supported on non-DT
375 if ((irq <= 0) || !enable) {
383 * ignore over flow error when rsnd_src_sync_is_enabled()
385 if (rsnd_src_sync_is_enabled(mod))
386 sys_int_val = sys_int_val & 0xffff;
388 rsnd_mod_write(mod, SRC_INT_ENABLE0, int_val);
389 rsnd_mod_bset(mod, SCU_SYS_INT_EN0, sys_int_mask, sys_int_val);
390 rsnd_mod_bset(mod, SCU_SYS_INT_EN1, sys_int_mask, sys_int_val);
395 static void rsnd_src_status_clear(struct rsnd_mod *mod)
397 u32 val = OUF_SRC(rsnd_mod_id(mod));
399 rsnd_mod_write(mod, SCU_SYS_STATUS0, val);
400 rsnd_mod_write(mod, SCU_SYS_STATUS1, val);
403 static bool rsnd_src_error_occurred(struct rsnd_mod *mod)
405 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
406 struct device *dev = rsnd_priv_to_dev(priv);
408 u32 status0, status1;
411 val0 = val1 = OUF_SRC(rsnd_mod_id(mod));
416 * ignore over flow error when rsnd_src_sync_is_enabled()
418 if (rsnd_src_sync_is_enabled(mod))
419 val0 = val0 & 0xffff;
421 status0 = rsnd_mod_read(mod, SCU_SYS_STATUS0);
422 status1 = rsnd_mod_read(mod, SCU_SYS_STATUS1);
423 if ((status0 & val0) || (status1 & val1)) {
424 rsnd_print_irq_status(dev, "%s err status : 0x%08x, 0x%08x\n",
425 rsnd_mod_name(mod), status0, status1);
433 static int rsnd_src_start(struct rsnd_mod *mod,
434 struct rsnd_dai_stream *io,
435 struct rsnd_priv *priv)
442 * Enable SRC output if you want to use sync convert together with DVC
444 val = (rsnd_io_to_mod_dvc(io) && !rsnd_src_sync_is_enabled(mod)) ?
447 rsnd_mod_write(mod, SRC_CTRL, val);
452 static int rsnd_src_stop(struct rsnd_mod *mod,
453 struct rsnd_dai_stream *io,
454 struct rsnd_priv *priv)
456 rsnd_mod_write(mod, SRC_CTRL, 0);
461 static int rsnd_src_init(struct rsnd_mod *mod,
462 struct rsnd_dai_stream *io,
463 struct rsnd_priv *priv)
465 struct rsnd_src *src = rsnd_mod_to_src(mod);
468 /* reset sync convert_rate */
471 ret = rsnd_mod_power_on(mod);
475 rsnd_src_activation(mod);
477 rsnd_src_set_convert_rate(io, mod);
479 rsnd_src_status_clear(mod);
484 static int rsnd_src_quit(struct rsnd_mod *mod,
485 struct rsnd_dai_stream *io,
486 struct rsnd_priv *priv)
488 struct rsnd_src *src = rsnd_mod_to_src(mod);
492 rsnd_mod_power_off(mod);
494 /* reset sync convert_rate */
500 static void __rsnd_src_interrupt(struct rsnd_mod *mod,
501 struct rsnd_dai_stream *io)
503 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
506 spin_lock(&priv->lock);
508 /* ignore all cases if not working */
509 if (!rsnd_io_is_working(io))
510 goto rsnd_src_interrupt_out;
512 if (rsnd_src_error_occurred(mod))
515 rsnd_src_status_clear(mod);
516 rsnd_src_interrupt_out:
518 spin_unlock(&priv->lock);
521 snd_pcm_stop_xrun(io->substream);
524 static irqreturn_t rsnd_src_interrupt(int irq, void *data)
526 struct rsnd_mod *mod = data;
528 rsnd_mod_interrupt(mod, __rsnd_src_interrupt);
533 static int rsnd_src_probe_(struct rsnd_mod *mod,
534 struct rsnd_dai_stream *io,
535 struct rsnd_priv *priv)
537 struct rsnd_src *src = rsnd_mod_to_src(mod);
538 struct device *dev = rsnd_priv_to_dev(priv);
544 * IRQ is not supported on non-DT
548 ret = devm_request_irq(dev, irq,
556 ret = rsnd_dma_attach(io, mod, &src->dma);
561 static int rsnd_src_pcm_new(struct rsnd_mod *mod,
562 struct rsnd_dai_stream *io,
563 struct snd_soc_pcm_runtime *rtd)
565 struct rsnd_src *src = rsnd_mod_to_src(mod);
569 * enable SRC sync convert if possible
573 * It can't use SRC Synchronous convert
574 * when Capture if it uses CMD
576 if (rsnd_io_to_mod_cmd(io) && !rsnd_io_is_play(io))
580 * enable sync convert
582 ret = rsnd_kctrl_new_s(mod, io, rtd,
583 rsnd_io_is_play(io) ?
584 "SRC Out Rate Switch" :
585 "SRC In Rate Switch",
586 rsnd_kctrl_accept_anytime,
587 rsnd_src_set_convert_rate,
592 ret = rsnd_kctrl_new_s(mod, io, rtd,
593 rsnd_io_is_play(io) ?
596 rsnd_kctrl_accept_runtime,
597 rsnd_src_set_convert_rate,
603 #ifdef CONFIG_DEBUG_FS
604 static void rsnd_src_debug_info(struct seq_file *m,
605 struct rsnd_dai_stream *io,
606 struct rsnd_mod *mod)
608 rsnd_debugfs_mod_reg_show(m, mod, RSND_GEN2_SCU,
609 rsnd_mod_id(mod) * 0x20, 0x20);
611 rsnd_debugfs_mod_reg_show(m, mod, RSND_GEN2_SCU,
614 rsnd_debugfs_mod_reg_show(m, mod, RSND_GEN2_SCU,
615 0x200 + rsnd_mod_id(mod) * 0x40, 0x40);
617 #define DEBUG_INFO .debug_info = rsnd_src_debug_info
622 static struct rsnd_mod_ops rsnd_src_ops = {
624 .dma_req = rsnd_src_dma_req,
625 .probe = rsnd_src_probe_,
626 .init = rsnd_src_init,
627 .quit = rsnd_src_quit,
628 .start = rsnd_src_start,
629 .stop = rsnd_src_stop,
631 .pcm_new = rsnd_src_pcm_new,
632 .get_status = rsnd_mod_get_status,
636 struct rsnd_mod *rsnd_src_mod_get(struct rsnd_priv *priv, int id)
638 if (WARN_ON(id < 0 || id >= rsnd_src_nr(priv)))
641 return rsnd_mod_get(rsnd_src_get(priv, id));
644 int rsnd_src_probe(struct rsnd_priv *priv)
646 struct device_node *node;
647 struct device_node *np;
648 struct device *dev = rsnd_priv_to_dev(priv);
649 struct rsnd_src *src;
651 char name[RSND_SRC_NAME_SIZE];
654 /* This driver doesn't support Gen1 at this point */
655 if (rsnd_is_gen1(priv))
658 node = rsnd_src_of_node(priv);
660 return 0; /* not used is not error */
662 nr = rsnd_node_count(priv, node, SRC_NAME);
665 goto rsnd_src_probe_done;
668 src = devm_kcalloc(dev, nr, sizeof(*src), GFP_KERNEL);
671 goto rsnd_src_probe_done;
678 for_each_child_of_node(node, np) {
679 if (!of_device_is_available(np))
682 i = rsnd_node_fixed_index(dev, np, SRC_NAME, i);
686 goto rsnd_src_probe_done;
689 src = rsnd_src_get(priv, i);
691 snprintf(name, RSND_SRC_NAME_SIZE, "%s.%d",
694 src->irq = irq_of_parse_and_map(np, 0);
698 goto rsnd_src_probe_done;
701 clk = devm_clk_get(dev, name);
705 goto rsnd_src_probe_done;
708 ret = rsnd_mod_init(priv, rsnd_mod_get(src),
709 &rsnd_src_ops, clk, RSND_MOD_SRC, i);
712 goto rsnd_src_probe_done;
727 void rsnd_src_remove(struct rsnd_priv *priv)
729 struct rsnd_src *src;
732 for_each_rsnd_src(src, priv, i) {
733 rsnd_mod_quit(rsnd_mod_get(src));