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 enable below define if you don't need
10 * SSI interrupt status debug message when debugging
11 * see rsnd_dbg_irq_status()
13 * #define RSND_DEBUG_NO_IRQ_STATUS 1
18 #define SRC_NAME "src"
20 /* SCU_SYSTEM_STATUS0/1 */
21 #define OUF_SRC(id) ((1 << (id + 16)) | (1 << id))
26 struct rsnd_kctrl_cfg_s sen; /* sync convert enable */
27 struct rsnd_kctrl_cfg_s sync; /* sync convert */
31 #define RSND_SRC_NAME_SIZE 16
33 #define rsnd_src_get(priv, id) ((struct rsnd_src *)(priv->src) + id)
34 #define rsnd_src_nr(priv) ((priv)->src_nr)
35 #define rsnd_src_sync_is_enabled(mod) (rsnd_mod_to_src(mod)->sen.val)
37 #define rsnd_mod_to_src(_mod) \
38 container_of((_mod), struct rsnd_src, mod)
40 #define for_each_rsnd_src(pos, priv, i) \
42 ((i) < rsnd_src_nr(priv)) && \
43 ((pos) = (struct rsnd_src *)(priv)->src + i); \
48 * image of SRC (Sampling Rate Converter)
50 * 96kHz <-> +-----+ 48kHz +-----+ 48kHz +-------+
51 * 48kHz <-> | SRC | <------> | SSI | <-----> | codec |
52 * 44.1kHz <-> +-----+ +-----+ +-------+
57 static void rsnd_src_activation(struct rsnd_mod *mod)
59 rsnd_mod_write(mod, SRC_SWRSR, 0);
60 rsnd_mod_write(mod, SRC_SWRSR, 1);
63 static void rsnd_src_halt(struct rsnd_mod *mod)
65 rsnd_mod_write(mod, SRC_SRCIR, 1);
66 rsnd_mod_write(mod, SRC_SWRSR, 0);
69 static struct dma_chan *rsnd_src_dma_req(struct rsnd_dai_stream *io,
72 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
73 int is_play = rsnd_io_is_play(io);
75 return rsnd_dma_request_channel(rsnd_src_of_node(priv),
77 is_play ? "rx" : "tx");
80 static u32 rsnd_src_convert_rate(struct rsnd_dai_stream *io,
83 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
84 struct rsnd_src *src = rsnd_mod_to_src(mod);
90 if (!rsnd_src_sync_is_enabled(mod))
91 return rsnd_io_converted_rate(io);
93 convert_rate = src->sync.val;
96 convert_rate = rsnd_io_converted_rate(io);
99 convert_rate = runtime->rate;
104 unsigned int rsnd_src_get_rate(struct rsnd_priv *priv,
105 struct rsnd_dai_stream *io,
108 struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
109 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
110 unsigned int rate = 0;
111 int is_play = rsnd_io_is_play(io);
115 * runtime_rate -> [SRC] -> convert_rate
118 * convert_rate -> [SRC] -> runtime_rate
121 if (is_play == is_in)
122 return runtime->rate;
125 * return convert rate if SRC is used,
126 * otherwise, return runtime->rate as usual
129 rate = rsnd_src_convert_rate(io, src_mod);
132 rate = runtime->rate;
137 static const u32 bsdsr_table_pattern1[] = {
138 0x01800000, /* 6 - 1/6 */
139 0x01000000, /* 6 - 1/4 */
140 0x00c00000, /* 6 - 1/3 */
141 0x00800000, /* 6 - 1/2 */
142 0x00600000, /* 6 - 2/3 */
143 0x00400000, /* 6 - 1 */
146 static const u32 bsdsr_table_pattern2[] = {
147 0x02400000, /* 6 - 1/6 */
148 0x01800000, /* 6 - 1/4 */
149 0x01200000, /* 6 - 1/3 */
150 0x00c00000, /* 6 - 1/2 */
151 0x00900000, /* 6 - 2/3 */
152 0x00600000, /* 6 - 1 */
155 static const u32 bsisr_table[] = {
156 0x00100060, /* 6 - 1/6 */
157 0x00100040, /* 6 - 1/4 */
158 0x00100030, /* 6 - 1/3 */
159 0x00100020, /* 6 - 1/2 */
160 0x00100020, /* 6 - 2/3 */
161 0x00100020, /* 6 - 1 */
164 static const u32 chan288888[] = {
165 0x00000006, /* 1 to 2 */
166 0x000001fe, /* 1 to 8 */
167 0x000001fe, /* 1 to 8 */
168 0x000001fe, /* 1 to 8 */
169 0x000001fe, /* 1 to 8 */
170 0x000001fe, /* 1 to 8 */
173 static const u32 chan244888[] = {
174 0x00000006, /* 1 to 2 */
175 0x0000001e, /* 1 to 4 */
176 0x0000001e, /* 1 to 4 */
177 0x000001fe, /* 1 to 8 */
178 0x000001fe, /* 1 to 8 */
179 0x000001fe, /* 1 to 8 */
182 static const u32 chan222222[] = {
183 0x00000006, /* 1 to 2 */
184 0x00000006, /* 1 to 2 */
185 0x00000006, /* 1 to 2 */
186 0x00000006, /* 1 to 2 */
187 0x00000006, /* 1 to 2 */
188 0x00000006, /* 1 to 2 */
191 static void rsnd_src_set_convert_rate(struct rsnd_dai_stream *io,
192 struct rsnd_mod *mod)
194 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
195 struct device *dev = rsnd_priv_to_dev(priv);
196 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
197 int is_play = rsnd_io_is_play(io);
200 u32 ifscr, fsrate, adinr;
202 u32 i_busif, o_busif, tmp;
203 const u32 *bsdsr_table;
212 fin = rsnd_src_get_in_rate(priv, io);
213 fout = rsnd_src_get_out_rate(priv, io);
215 chan = rsnd_runtime_channel_original(io);
217 /* 6 - 1/6 are very enough ratio for SRC_BSDSR */
221 ratio = 100 * fin / fout;
223 ratio = 100 * fout / fin;
226 dev_err(dev, "FSO/FSI ratio error\n");
230 use_src = (fin != fout) | rsnd_src_sync_is_enabled(mod);
235 adinr = rsnd_get_adinr_bit(mod, io) | chan;
238 * SRC_IFSCR / SRC_IFSVR
246 n = (u64)0x0400000 * fin;
252 * SRC_SRCCR / SRC_ROUTE_MODE0
259 if (rsnd_src_sync_is_enabled(mod)) {
261 route |= rsnd_io_is_play(io) ?
262 (0x1 << 24) : (0x1 << 25);
267 * SRC_BSDSR / SRC_BSISR
270 * Combination of Register Setting Related to
271 * FSO/FSI Ratio and Channel, Latency
273 switch (rsnd_mod_id(mod)) {
276 bsdsr_table = bsdsr_table_pattern1;
282 bsdsr_table = bsdsr_table_pattern1;
287 bsdsr_table = bsdsr_table_pattern1;
294 bsdsr_table = bsdsr_table_pattern2;
297 goto convert_rate_err;
301 * E3 need to overwrite
303 if (rsnd_is_e3(priv))
304 switch (rsnd_mod_id(mod)) {
310 for (idx = 0; idx < ARRAY_SIZE(chan222222); idx++)
311 if (chptn[idx] & (1 << chan))
315 idx >= ARRAY_SIZE(chan222222))
316 goto convert_rate_err;
319 tmp = rsnd_get_busif_shift(io, mod);
320 i_busif = ( is_play ? tmp : 0) | 1;
321 o_busif = (!is_play ? tmp : 0) | 1;
323 rsnd_mod_write(mod, SRC_ROUTE_MODE0, route);
325 rsnd_mod_write(mod, SRC_SRCIR, 1); /* initialize */
326 rsnd_mod_write(mod, SRC_ADINR, adinr);
327 rsnd_mod_write(mod, SRC_IFSCR, ifscr);
328 rsnd_mod_write(mod, SRC_IFSVR, fsrate);
329 rsnd_mod_write(mod, SRC_SRCCR, cr);
330 rsnd_mod_write(mod, SRC_BSDSR, bsdsr_table[idx]);
331 rsnd_mod_write(mod, SRC_BSISR, bsisr_table[idx]);
332 rsnd_mod_write(mod, SRC_SRCIR, 0); /* cancel initialize */
334 rsnd_mod_write(mod, SRC_I_BUSIF_MODE, i_busif);
335 rsnd_mod_write(mod, SRC_O_BUSIF_MODE, o_busif);
337 rsnd_mod_write(mod, SRC_BUSIF_DALIGN, rsnd_get_dalign(mod, io));
339 rsnd_adg_set_src_timesel_gen2(mod, io, fin, fout);
344 dev_err(dev, "unknown BSDSR/BSDIR settings\n");
347 static int rsnd_src_irq(struct rsnd_mod *mod,
348 struct rsnd_dai_stream *io,
349 struct rsnd_priv *priv,
352 struct rsnd_src *src = rsnd_mod_to_src(mod);
353 u32 sys_int_val, int_val, sys_int_mask;
355 int id = rsnd_mod_id(mod);
358 sys_int_mask = OUF_SRC(id);
362 * IRQ is not supported on non-DT
366 if ((irq <= 0) || !enable) {
374 * ignore over flow error when rsnd_src_sync_is_enabled()
376 if (rsnd_src_sync_is_enabled(mod))
377 sys_int_val = sys_int_val & 0xffff;
379 rsnd_mod_write(mod, SRC_INT_ENABLE0, int_val);
380 rsnd_mod_bset(mod, SCU_SYS_INT_EN0, sys_int_mask, sys_int_val);
381 rsnd_mod_bset(mod, SCU_SYS_INT_EN1, sys_int_mask, sys_int_val);
386 static void rsnd_src_status_clear(struct rsnd_mod *mod)
388 u32 val = OUF_SRC(rsnd_mod_id(mod));
390 rsnd_mod_write(mod, SCU_SYS_STATUS0, val);
391 rsnd_mod_write(mod, SCU_SYS_STATUS1, val);
394 static bool rsnd_src_error_occurred(struct rsnd_mod *mod)
396 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
397 struct device *dev = rsnd_priv_to_dev(priv);
399 u32 status0, status1;
402 val0 = val1 = OUF_SRC(rsnd_mod_id(mod));
407 * ignore over flow error when rsnd_src_sync_is_enabled()
409 if (rsnd_src_sync_is_enabled(mod))
410 val0 = val0 & 0xffff;
412 status0 = rsnd_mod_read(mod, SCU_SYS_STATUS0);
413 status1 = rsnd_mod_read(mod, SCU_SYS_STATUS1);
414 if ((status0 & val0) || (status1 & val1)) {
415 rsnd_dbg_irq_status(dev, "%s err status : 0x%08x, 0x%08x\n",
416 rsnd_mod_name(mod), status0, status1);
424 static int rsnd_src_start(struct rsnd_mod *mod,
425 struct rsnd_dai_stream *io,
426 struct rsnd_priv *priv)
433 * Enable SRC output if you want to use sync convert together with DVC
435 val = (rsnd_io_to_mod_dvc(io) && !rsnd_src_sync_is_enabled(mod)) ?
438 rsnd_mod_write(mod, SRC_CTRL, val);
443 static int rsnd_src_stop(struct rsnd_mod *mod,
444 struct rsnd_dai_stream *io,
445 struct rsnd_priv *priv)
447 rsnd_mod_write(mod, SRC_CTRL, 0);
452 static int rsnd_src_init(struct rsnd_mod *mod,
453 struct rsnd_dai_stream *io,
454 struct rsnd_priv *priv)
456 struct rsnd_src *src = rsnd_mod_to_src(mod);
458 /* reset sync convert_rate */
461 rsnd_mod_power_on(mod);
463 rsnd_src_activation(mod);
465 rsnd_src_set_convert_rate(io, mod);
467 rsnd_src_status_clear(mod);
472 static int rsnd_src_quit(struct rsnd_mod *mod,
473 struct rsnd_dai_stream *io,
474 struct rsnd_priv *priv)
476 struct rsnd_src *src = rsnd_mod_to_src(mod);
480 rsnd_mod_power_off(mod);
482 /* reset sync convert_rate */
488 static void __rsnd_src_interrupt(struct rsnd_mod *mod,
489 struct rsnd_dai_stream *io)
491 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
494 spin_lock(&priv->lock);
496 /* ignore all cases if not working */
497 if (!rsnd_io_is_working(io))
498 goto rsnd_src_interrupt_out;
500 if (rsnd_src_error_occurred(mod))
503 rsnd_src_status_clear(mod);
504 rsnd_src_interrupt_out:
506 spin_unlock(&priv->lock);
509 snd_pcm_stop_xrun(io->substream);
512 static irqreturn_t rsnd_src_interrupt(int irq, void *data)
514 struct rsnd_mod *mod = data;
516 rsnd_mod_interrupt(mod, __rsnd_src_interrupt);
521 static int rsnd_src_probe_(struct rsnd_mod *mod,
522 struct rsnd_dai_stream *io,
523 struct rsnd_priv *priv)
525 struct rsnd_src *src = rsnd_mod_to_src(mod);
526 struct device *dev = rsnd_priv_to_dev(priv);
532 * IRQ is not supported on non-DT
536 ret = devm_request_irq(dev, irq,
544 ret = rsnd_dma_attach(io, mod, &src->dma);
549 static int rsnd_src_pcm_new(struct rsnd_mod *mod,
550 struct rsnd_dai_stream *io,
551 struct snd_soc_pcm_runtime *rtd)
553 struct rsnd_src *src = rsnd_mod_to_src(mod);
557 * enable SRC sync convert if possible
561 * It can't use SRC Synchronous convert
562 * when Capture if it uses CMD
564 if (rsnd_io_to_mod_cmd(io) && !rsnd_io_is_play(io))
568 * enable sync convert
570 ret = rsnd_kctrl_new_s(mod, io, rtd,
571 rsnd_io_is_play(io) ?
572 "SRC Out Rate Switch" :
573 "SRC In Rate Switch",
574 rsnd_kctrl_accept_anytime,
575 rsnd_src_set_convert_rate,
580 ret = rsnd_kctrl_new_s(mod, io, rtd,
581 rsnd_io_is_play(io) ?
584 rsnd_kctrl_accept_runtime,
585 rsnd_src_set_convert_rate,
591 static struct rsnd_mod_ops rsnd_src_ops = {
593 .dma_req = rsnd_src_dma_req,
594 .probe = rsnd_src_probe_,
595 .init = rsnd_src_init,
596 .quit = rsnd_src_quit,
597 .start = rsnd_src_start,
598 .stop = rsnd_src_stop,
600 .pcm_new = rsnd_src_pcm_new,
601 .get_status = rsnd_mod_get_status,
604 struct rsnd_mod *rsnd_src_mod_get(struct rsnd_priv *priv, int id)
606 if (WARN_ON(id < 0 || id >= rsnd_src_nr(priv)))
609 return rsnd_mod_get(rsnd_src_get(priv, id));
612 int rsnd_src_probe(struct rsnd_priv *priv)
614 struct device_node *node;
615 struct device_node *np;
616 struct device *dev = rsnd_priv_to_dev(priv);
617 struct rsnd_src *src;
619 char name[RSND_SRC_NAME_SIZE];
622 /* This driver doesn't support Gen1 at this point */
623 if (rsnd_is_gen1(priv))
626 node = rsnd_src_of_node(priv);
628 return 0; /* not used is not error */
630 nr = of_get_child_count(node);
633 goto rsnd_src_probe_done;
636 src = devm_kcalloc(dev, nr, sizeof(*src), GFP_KERNEL);
639 goto rsnd_src_probe_done;
646 for_each_child_of_node(node, np) {
647 if (!of_device_is_available(np))
650 src = rsnd_src_get(priv, i);
652 snprintf(name, RSND_SRC_NAME_SIZE, "%s.%d",
655 src->irq = irq_of_parse_and_map(np, 0);
659 goto rsnd_src_probe_done;
662 clk = devm_clk_get(dev, name);
666 goto rsnd_src_probe_done;
669 ret = rsnd_mod_init(priv, rsnd_mod_get(src),
670 &rsnd_src_ops, clk, RSND_MOD_SRC, i);
673 goto rsnd_src_probe_done;
688 void rsnd_src_remove(struct rsnd_priv *priv)
690 struct rsnd_src *src;
693 for_each_rsnd_src(src, priv, i) {
694 rsnd_mod_quit(rsnd_mod_get(src));