WIP FPC-III support
[linux/fpc-iii.git] / sound / soc / sh / rcar / src.c
blob585ffba0244b9f568685ed52323d31f7f9fcb3b4
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Renesas R-Car SRC support
4 //
5 // Copyright (C) 2013 Renesas Solutions Corp.
6 // Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
8 /*
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
16 #include "rsnd.h"
18 #define SRC_NAME "src"
20 /* SCU_SYSTEM_STATUS0/1 */
21 #define OUF_SRC(id) ((1 << (id + 16)) | (1 << id))
23 struct rsnd_src {
24 struct rsnd_mod mod;
25 struct rsnd_mod *dma;
26 struct rsnd_kctrl_cfg_s sen; /* sync convert enable */
27 struct rsnd_kctrl_cfg_s sync; /* sync convert */
28 int irq;
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) \
41 for ((i) = 0; \
42 ((i) < rsnd_src_nr(priv)) && \
43 ((pos) = (struct rsnd_src *)(priv)->src + i); \
44 i++)
48 * image of SRC (Sampling Rate Converter)
50 * 96kHz <-> +-----+ 48kHz +-----+ 48kHz +-------+
51 * 48kHz <-> | SRC | <------> | SSI | <-----> | codec |
52 * 44.1kHz <-> +-----+ +-----+ +-------+
53 * ...
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,
70 struct rsnd_mod *mod)
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),
76 mod,
77 is_play ? "rx" : "tx");
80 static u32 rsnd_src_convert_rate(struct rsnd_dai_stream *io,
81 struct rsnd_mod *mod)
83 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
84 struct rsnd_src *src = rsnd_mod_to_src(mod);
85 u32 convert_rate;
87 if (!runtime)
88 return 0;
90 if (!rsnd_src_sync_is_enabled(mod))
91 return rsnd_io_converted_rate(io);
93 convert_rate = src->sync.val;
95 if (!convert_rate)
96 convert_rate = rsnd_io_converted_rate(io);
98 if (!convert_rate)
99 convert_rate = runtime->rate;
101 return convert_rate;
104 unsigned int rsnd_src_get_rate(struct rsnd_priv *priv,
105 struct rsnd_dai_stream *io,
106 int is_in)
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);
114 * Playback
115 * runtime_rate -> [SRC] -> convert_rate
117 * Capture
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
128 if (src_mod)
129 rate = rsnd_src_convert_rate(io, src_mod);
131 if (!rate)
132 rate = runtime->rate;
134 return 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);
198 int use_src = 0;
199 u32 fin, fout;
200 u32 ifscr, fsrate, adinr;
201 u32 cr, route;
202 u32 i_busif, o_busif, tmp;
203 const u32 *bsdsr_table;
204 const u32 *chptn;
205 uint ratio;
206 int chan;
207 int idx;
209 if (!runtime)
210 return;
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 */
218 if (fin == fout)
219 ratio = 0;
220 else if (fin > fout)
221 ratio = 100 * fin / fout;
222 else
223 ratio = 100 * fout / fin;
225 if (ratio > 600) {
226 dev_err(dev, "FSO/FSI ratio error\n");
227 return;
230 use_src = (fin != fout) | rsnd_src_sync_is_enabled(mod);
233 * SRC_ADINR
235 adinr = rsnd_get_adinr_bit(mod, io) | chan;
238 * SRC_IFSCR / SRC_IFSVR
240 ifscr = 0;
241 fsrate = 0;
242 if (use_src) {
243 u64 n;
245 ifscr = 1;
246 n = (u64)0x0400000 * fin;
247 do_div(n, fout);
248 fsrate = n;
252 * SRC_SRCCR / SRC_ROUTE_MODE0
254 cr = 0x00011110;
255 route = 0x0;
256 if (use_src) {
257 route = 0x1;
259 if (rsnd_src_sync_is_enabled(mod)) {
260 cr |= 0x1;
261 route |= rsnd_io_is_play(io) ?
262 (0x1 << 24) : (0x1 << 25);
267 * SRC_BSDSR / SRC_BSISR
269 * see
270 * Combination of Register Setting Related to
271 * FSO/FSI Ratio and Channel, Latency
273 switch (rsnd_mod_id(mod)) {
274 case 0:
275 chptn = chan288888;
276 bsdsr_table = bsdsr_table_pattern1;
277 break;
278 case 1:
279 case 3:
280 case 4:
281 chptn = chan244888;
282 bsdsr_table = bsdsr_table_pattern1;
283 break;
284 case 2:
285 case 9:
286 chptn = chan222222;
287 bsdsr_table = bsdsr_table_pattern1;
288 break;
289 case 5:
290 case 6:
291 case 7:
292 case 8:
293 chptn = chan222222;
294 bsdsr_table = bsdsr_table_pattern2;
295 break;
296 default:
297 goto convert_rate_err;
301 * E3 need to overwrite
303 if (rsnd_is_e3(priv))
304 switch (rsnd_mod_id(mod)) {
305 case 0:
306 case 4:
307 chptn = chan222222;
310 for (idx = 0; idx < ARRAY_SIZE(chan222222); idx++)
311 if (chptn[idx] & (1 << chan))
312 break;
314 if (chan > 8 ||
315 idx >= ARRAY_SIZE(chan222222))
316 goto convert_rate_err;
318 /* BUSIF_MODE */
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);
341 return;
343 convert_rate_err:
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,
350 int enable)
352 struct rsnd_src *src = rsnd_mod_to_src(mod);
353 u32 sys_int_val, int_val, sys_int_mask;
354 int irq = src->irq;
355 int id = rsnd_mod_id(mod);
357 sys_int_val =
358 sys_int_mask = OUF_SRC(id);
359 int_val = 0x3300;
362 * IRQ is not supported on non-DT
363 * see
364 * rsnd_src_probe_()
366 if ((irq <= 0) || !enable) {
367 sys_int_val = 0;
368 int_val = 0;
372 * WORKAROUND
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);
383 return 0;
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);
398 u32 val0, val1;
399 u32 status0, status1;
400 bool ret = false;
402 val0 = val1 = OUF_SRC(rsnd_mod_id(mod));
405 * WORKAROUND
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);
418 ret = true;
421 return ret;
424 static int rsnd_src_start(struct rsnd_mod *mod,
425 struct rsnd_dai_stream *io,
426 struct rsnd_priv *priv)
428 u32 val;
431 * WORKAROUND
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)) ?
436 0x01 : 0x11;
438 rsnd_mod_write(mod, SRC_CTRL, val);
440 return 0;
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);
449 return 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 */
459 src->sync.val = 0;
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);
469 return 0;
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);
478 rsnd_src_halt(mod);
480 rsnd_mod_power_off(mod);
482 /* reset sync convert_rate */
483 src->sync.val = 0;
485 return 0;
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);
492 bool stop = false;
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))
501 stop = true;
503 rsnd_src_status_clear(mod);
504 rsnd_src_interrupt_out:
506 spin_unlock(&priv->lock);
508 if (stop)
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);
518 return IRQ_HANDLED;
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);
527 int irq = src->irq;
528 int ret;
530 if (irq > 0) {
532 * IRQ is not supported on non-DT
533 * see
534 * rsnd_src_irq()
536 ret = devm_request_irq(dev, irq,
537 rsnd_src_interrupt,
538 IRQF_SHARED,
539 dev_name(dev), mod);
540 if (ret)
541 return ret;
544 ret = rsnd_dma_attach(io, mod, &src->dma);
546 return ret;
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);
554 int ret;
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))
565 return 0;
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,
576 &src->sen, 1);
577 if (ret < 0)
578 return ret;
580 ret = rsnd_kctrl_new_s(mod, io, rtd,
581 rsnd_io_is_play(io) ?
582 "SRC Out Rate" :
583 "SRC In Rate",
584 rsnd_kctrl_accept_runtime,
585 rsnd_src_set_convert_rate,
586 &src->sync, 192000);
588 return ret;
591 static struct rsnd_mod_ops rsnd_src_ops = {
592 .name = SRC_NAME,
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,
599 .irq = rsnd_src_irq,
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)))
607 id = 0;
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;
618 struct clk *clk;
619 char name[RSND_SRC_NAME_SIZE];
620 int i, nr, ret;
622 /* This driver doesn't support Gen1 at this point */
623 if (rsnd_is_gen1(priv))
624 return 0;
626 node = rsnd_src_of_node(priv);
627 if (!node)
628 return 0; /* not used is not error */
630 nr = of_get_child_count(node);
631 if (!nr) {
632 ret = -EINVAL;
633 goto rsnd_src_probe_done;
636 src = devm_kcalloc(dev, nr, sizeof(*src), GFP_KERNEL);
637 if (!src) {
638 ret = -ENOMEM;
639 goto rsnd_src_probe_done;
642 priv->src_nr = nr;
643 priv->src = src;
645 i = 0;
646 for_each_child_of_node(node, np) {
647 if (!of_device_is_available(np))
648 goto skip;
650 src = rsnd_src_get(priv, i);
652 snprintf(name, RSND_SRC_NAME_SIZE, "%s.%d",
653 SRC_NAME, i);
655 src->irq = irq_of_parse_and_map(np, 0);
656 if (!src->irq) {
657 ret = -EINVAL;
658 of_node_put(np);
659 goto rsnd_src_probe_done;
662 clk = devm_clk_get(dev, name);
663 if (IS_ERR(clk)) {
664 ret = PTR_ERR(clk);
665 of_node_put(np);
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);
671 if (ret) {
672 of_node_put(np);
673 goto rsnd_src_probe_done;
676 skip:
677 i++;
680 ret = 0;
682 rsnd_src_probe_done:
683 of_node_put(node);
685 return ret;
688 void rsnd_src_remove(struct rsnd_priv *priv)
690 struct rsnd_src *src;
691 int i;
693 for_each_rsnd_src(src, priv, i) {
694 rsnd_mod_quit(rsnd_mod_get(src));