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 struct rsnd_src_platform_info *info; /* rcar_snd.h */
21 #define RSND_SRC_NAME_SIZE 16
23 #define rsnd_src_convert_rate(p) ((p)->info->convert_rate)
24 #define rsnd_mod_to_src(_mod) \
25 container_of((_mod), struct rsnd_src, mod)
26 #define rsnd_src_dma_available(src) \
27 rsnd_dma_available(rsnd_mod_to_dma(&(src)->mod))
29 #define for_each_rsnd_src(pos, priv, i) \
31 ((i) < rsnd_src_nr(priv)) && \
32 ((pos) = (struct rsnd_src *)(priv)->src + i); \
37 * image of SRC (Sampling Rate Converter)
39 * 96kHz <-> +-----+ 48kHz +-----+ 48kHz +-------+
40 * 48kHz <-> | SRC | <------> | SSI | <-----> | codec |
41 * 44.1kHz <-> +-----+ +-----+ +-------+
51 * [mem] -> [SRU] -> [SSI]
56 * [mem] -> [SRC] -> [SSIU] -> [SSI]
61 * How to use SRC bypass mode for debugging
63 * SRC has bypass mode, and it is useful for debugging.
65 * SRCm_MODE controls whether SRC is used or not
66 * SSI_MODE0 controls whether SSIU which receives SRC data
68 * Both SRCm_MODE/SSI_MODE0 settings are needed if you use SRC,
69 * but SRC bypass mode needs SSI_MODE0 only.
72 * struct rsnd_src_platform_info {
77 * rsnd_src_convert_rate() indicates
78 * above convert_rate, and it controls
79 * whether SRC is used or not.
82 * static struct rsnd_dai_platform_info rsnd_dai = {
83 * .playback = { .ssi = &rsnd_ssi[0], },
87 * static struct rsnd_src_platform_info rsnd_src[] = {
91 * static struct rsnd_dai_platform_info rsnd_dai = {
92 * .playback = { .ssi = &rsnd_ssi[0], .src = &rsnd_src[0] },
95 * ex) uses SRC bypass mode
96 * static struct rsnd_src_platform_info rsnd_src[] = {
100 * static struct rsnd_dai_platform_info rsnd_dai = {
101 * .playback = { .ssi = &rsnd_ssi[0], .src = &rsnd_src[0] },
107 * Gen1/Gen2 common functions
109 int rsnd_src_ssiu_start(struct rsnd_mod *ssi_mod,
110 struct rsnd_dai *rdai,
113 struct rsnd_dai_stream *io = rsnd_mod_to_io(ssi_mod);
114 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
115 int ssi_id = rsnd_mod_id(ssi_mod);
120 rsnd_mod_bset(ssi_mod, SSI_MODE0, (1 << ssi_id),
121 !use_busif << ssi_id);
126 if (rsnd_ssi_is_pin_sharing(ssi_mod)) {
141 rsnd_mod_bset(ssi_mod, SSI_MODE1,
143 rsnd_dai_is_clk_master(rdai) ?
144 0x2 << shift : 0x1 << shift);
148 * DMA settings for SSIU
151 u32 val = 0x76543210;
154 rsnd_mod_write(ssi_mod, SSI_BUSIF_ADINR,
155 rsnd_get_adinr(ssi_mod));
156 rsnd_mod_write(ssi_mod, SSI_BUSIF_MODE, 1);
157 rsnd_mod_write(ssi_mod, SSI_CTRL, 0x1);
159 mask <<= runtime->channels * 4;
162 switch (runtime->sample_bits) {
164 val |= 0x67452301 & ~mask;
167 val |= 0x76543210 & ~mask;
170 rsnd_mod_write(ssi_mod, BUSIF_DALIGN, val);
177 int rsnd_src_ssiu_stop(struct rsnd_mod *ssi_mod,
178 struct rsnd_dai *rdai,
182 * DMA settings for SSIU
185 rsnd_mod_write(ssi_mod, SSI_CTRL, 0);
190 int rsnd_src_enable_ssi_irq(struct rsnd_mod *ssi_mod,
191 struct rsnd_dai *rdai)
193 struct rsnd_priv *priv = rsnd_mod_to_priv(ssi_mod);
195 /* enable PIO interrupt if Gen2 */
196 if (rsnd_is_gen2(priv))
197 rsnd_mod_write(ssi_mod, INT_ENABLE, 0x0f000000);
202 unsigned int rsnd_src_get_ssi_rate(struct rsnd_priv *priv,
203 struct rsnd_dai_stream *io,
204 struct snd_pcm_runtime *runtime)
206 struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
207 struct rsnd_src *src;
208 unsigned int rate = 0;
211 src = rsnd_mod_to_src(src_mod);
214 * return convert rate if SRC is used,
215 * otherwise, return runtime->rate as usual
217 rate = rsnd_src_convert_rate(src);
221 rate = runtime->rate;
226 static int rsnd_src_set_convert_rate(struct rsnd_mod *mod,
227 struct rsnd_dai *rdai)
229 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
230 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
231 struct rsnd_src *src = rsnd_mod_to_src(mod);
232 u32 convert_rate = rsnd_src_convert_rate(src);
236 fsrate = 0x0400000 / convert_rate * runtime->rate;
238 /* set/clear soft reset */
239 rsnd_mod_write(mod, SRC_SWRSR, 0);
240 rsnd_mod_write(mod, SRC_SWRSR, 1);
243 * Initialize the operation of the SRC internal circuits
244 * see rsnd_src_start()
246 rsnd_mod_write(mod, SRC_SRCIR, 1);
248 /* Set channel number and output bit length */
249 rsnd_mod_write(mod, SRC_ADINR, rsnd_get_adinr(mod));
251 /* Enable the initial value of IFS */
253 rsnd_mod_write(mod, SRC_IFSCR, 1);
255 /* Set initial value of IFS */
256 rsnd_mod_write(mod, SRC_IFSVR, fsrate);
259 /* use DMA transfer */
260 rsnd_mod_write(mod, SRC_BUSIF_MODE, 1);
265 static int rsnd_src_init(struct rsnd_mod *mod,
266 struct rsnd_dai *rdai)
268 struct rsnd_src *src = rsnd_mod_to_src(mod);
270 clk_prepare_enable(src->clk);
275 static int rsnd_src_quit(struct rsnd_mod *mod,
276 struct rsnd_dai *rdai)
278 struct rsnd_src *src = rsnd_mod_to_src(mod);
280 clk_disable_unprepare(src->clk);
285 static int rsnd_src_start(struct rsnd_mod *mod,
286 struct rsnd_dai *rdai)
288 struct rsnd_src *src = rsnd_mod_to_src(mod);
291 * Cancel the initialization and operate the SRC function
292 * see rsnd_src_set_convert_rate()
294 rsnd_mod_write(mod, SRC_SRCIR, 0);
296 if (rsnd_src_convert_rate(src))
297 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 1);
303 static int rsnd_src_stop(struct rsnd_mod *mod,
304 struct rsnd_dai *rdai)
306 struct rsnd_src *src = rsnd_mod_to_src(mod);
308 if (rsnd_src_convert_rate(src))
309 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 0);
317 static int rsnd_src_set_route_gen1(struct rsnd_mod *mod,
318 struct rsnd_dai *rdai)
320 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
321 struct src_route_config {
328 { 0x7, 12, }, /* 3 */
329 { 0x7, 16, }, /* 4 */
330 { 0x7, 20, }, /* 5 */
331 { 0x7, 24, }, /* 6 */
332 { 0x3, 28, }, /* 7 */
333 { 0x3, 30, }, /* 8 */
339 id = rsnd_mod_id(mod);
340 if (id < 0 || id >= ARRAY_SIZE(routes))
346 val = rsnd_dai_is_play(rdai, io) ? 0x1 : 0x2;
347 val = val << routes[id].shift;
348 mask = routes[id].mask << routes[id].shift;
350 rsnd_mod_bset(mod, SRC_ROUTE_SEL, mask, val);
355 static int rsnd_src_set_convert_timing_gen1(struct rsnd_mod *mod,
356 struct rsnd_dai *rdai)
358 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
359 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
360 struct rsnd_src *src = rsnd_mod_to_src(mod);
361 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
362 u32 convert_rate = rsnd_src_convert_rate(src);
366 int id = rsnd_mod_id(mod);
372 shift = (id % 4) * 8;
373 mask = 0x1F << shift;
376 * ADG is used as source clock if SRC was used,
377 * then, SSI WS is used as destination clock.
378 * SSI WS is used as source clock if SRC is not used
379 * (when playback, source/destination become reverse when capture)
385 ret = rsnd_adg_set_convert_clk_gen1(priv, mod,
388 } else if (8 == id) {
389 /* use SSI WS, but SRU8 is special */
393 val = (id + 1) << shift;
401 rsnd_mod_bset(mod, SRC_TMG_SEL0, mask, val);
404 rsnd_mod_bset(mod, SRC_TMG_SEL1, mask, val);
407 rsnd_mod_bset(mod, SRC_TMG_SEL2, mask, val);
414 static int rsnd_src_set_convert_rate_gen1(struct rsnd_mod *mod,
415 struct rsnd_dai *rdai)
419 ret = rsnd_src_set_convert_rate(mod, rdai);
423 /* Select SRC mode (fixed value) */
424 rsnd_mod_write(mod, SRC_SRCCR, 0x00010110);
426 /* Set the restriction value of the FS ratio (98%) */
427 rsnd_mod_write(mod, SRC_MNFSR,
428 rsnd_mod_read(mod, SRC_IFSVR) / 100 * 98);
430 /* no SRC_BFSSR settings, since SRC_SRCCR::BUFMD is 0 */
435 static int rsnd_src_probe_gen1(struct rsnd_mod *mod,
436 struct rsnd_dai *rdai)
438 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
439 struct device *dev = rsnd_priv_to_dev(priv);
441 dev_dbg(dev, "%s (Gen1) is probed\n", rsnd_mod_name(mod));
446 static int rsnd_src_init_gen1(struct rsnd_mod *mod,
447 struct rsnd_dai *rdai)
451 ret = rsnd_src_init(mod, rdai);
455 ret = rsnd_src_set_route_gen1(mod, rdai);
459 ret = rsnd_src_set_convert_rate_gen1(mod, rdai);
463 ret = rsnd_src_set_convert_timing_gen1(mod, rdai);
470 static int rsnd_src_start_gen1(struct rsnd_mod *mod,
471 struct rsnd_dai *rdai)
473 int id = rsnd_mod_id(mod);
475 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), (1 << id));
477 return rsnd_src_start(mod, rdai);
480 static int rsnd_src_stop_gen1(struct rsnd_mod *mod,
481 struct rsnd_dai *rdai)
483 int id = rsnd_mod_id(mod);
485 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), 0);
487 return rsnd_src_stop(mod, rdai);
490 static struct rsnd_mod_ops rsnd_src_gen1_ops = {
492 .probe = rsnd_src_probe_gen1,
493 .init = rsnd_src_init_gen1,
494 .quit = rsnd_src_quit,
495 .start = rsnd_src_start_gen1,
496 .stop = rsnd_src_stop_gen1,
502 static int rsnd_src_set_convert_rate_gen2(struct rsnd_mod *mod,
503 struct rsnd_dai *rdai)
505 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
506 struct device *dev = rsnd_priv_to_dev(priv);
507 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
508 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
509 struct rsnd_src *src = rsnd_mod_to_src(mod);
513 /* 6 - 1/6 are very enough ratio for SRC_BSDSR */
514 if (!rsnd_src_convert_rate(src))
516 else if (rsnd_src_convert_rate(src) > runtime->rate)
517 ratio = 100 * rsnd_src_convert_rate(src) / runtime->rate;
519 ratio = 100 * runtime->rate / rsnd_src_convert_rate(src);
522 dev_err(dev, "FSO/FSI ratio error\n");
526 ret = rsnd_src_set_convert_rate(mod, rdai);
530 rsnd_mod_write(mod, SRC_SRCCR, 0x00011110);
532 switch (rsnd_mod_id(mod)) {
537 rsnd_mod_write(mod, SRC_BSDSR, 0x02400000);
540 rsnd_mod_write(mod, SRC_BSDSR, 0x01800000);
544 rsnd_mod_write(mod, SRC_BSISR, 0x00100060);
549 static int rsnd_src_set_convert_timing_gen2(struct rsnd_mod *mod,
550 struct rsnd_dai *rdai)
552 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
553 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
554 struct rsnd_src *src = rsnd_mod_to_src(mod);
555 u32 convert_rate = rsnd_src_convert_rate(src);
559 ret = rsnd_adg_set_convert_clk_gen2(mod, rdai, io,
563 ret = rsnd_adg_set_convert_timing_gen2(mod, rdai, io);
568 static int rsnd_src_probe_gen2(struct rsnd_mod *mod,
569 struct rsnd_dai *rdai)
571 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
572 struct rsnd_src *src = rsnd_mod_to_src(mod);
573 struct device *dev = rsnd_priv_to_dev(priv);
576 ret = rsnd_dma_init(priv,
577 rsnd_mod_to_dma(mod),
578 rsnd_info_is_playback(priv, src),
581 dev_err(dev, "SRC DMA failed\n");
583 dev_dbg(dev, "%s (Gen2) is probed\n", rsnd_mod_name(mod));
588 static int rsnd_src_remove_gen2(struct rsnd_mod *mod,
589 struct rsnd_dai *rdai)
591 rsnd_dma_quit(rsnd_mod_to_priv(mod), rsnd_mod_to_dma(mod));
596 static int rsnd_src_init_gen2(struct rsnd_mod *mod,
597 struct rsnd_dai *rdai)
601 ret = rsnd_src_init(mod, rdai);
605 ret = rsnd_src_set_convert_rate_gen2(mod, rdai);
609 ret = rsnd_src_set_convert_timing_gen2(mod, rdai);
616 static int rsnd_src_start_gen2(struct rsnd_mod *mod,
617 struct rsnd_dai *rdai)
619 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
620 struct rsnd_src *src = rsnd_mod_to_src(mod);
621 u32 val = rsnd_io_to_mod_dvc(io) ? 0x01 : 0x11;
623 rsnd_dma_start(rsnd_mod_to_dma(&src->mod));
625 rsnd_mod_write(mod, SRC_CTRL, val);
627 return rsnd_src_start(mod, rdai);
630 static int rsnd_src_stop_gen2(struct rsnd_mod *mod,
631 struct rsnd_dai *rdai)
633 struct rsnd_src *src = rsnd_mod_to_src(mod);
635 rsnd_mod_write(mod, SRC_CTRL, 0);
637 rsnd_dma_stop(rsnd_mod_to_dma(&src->mod));
639 return rsnd_src_stop(mod, rdai);
642 static struct rsnd_mod_ops rsnd_src_gen2_ops = {
644 .probe = rsnd_src_probe_gen2,
645 .remove = rsnd_src_remove_gen2,
646 .init = rsnd_src_init_gen2,
647 .quit = rsnd_src_quit,
648 .start = rsnd_src_start_gen2,
649 .stop = rsnd_src_stop_gen2,
652 struct rsnd_mod *rsnd_src_mod_get(struct rsnd_priv *priv, int id)
654 if (WARN_ON(id < 0 || id >= rsnd_src_nr(priv)))
657 return &((struct rsnd_src *)(priv->src) + id)->mod;
660 static void rsnd_of_parse_src(struct platform_device *pdev,
661 const struct rsnd_of_data *of_data,
662 struct rsnd_priv *priv)
664 struct device_node *src_node;
665 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
666 struct rsnd_src_platform_info *src_info;
667 struct device *dev = &pdev->dev;
673 src_node = of_get_child_by_name(dev->of_node, "rcar_sound,src");
677 nr = of_get_child_count(src_node);
679 goto rsnd_of_parse_src_end;
681 src_info = devm_kzalloc(dev,
682 sizeof(struct rsnd_src_platform_info) * nr,
685 dev_err(dev, "src info allocation error\n");
686 goto rsnd_of_parse_src_end;
689 info->src_info = src_info;
690 info->src_info_nr = nr;
692 rsnd_of_parse_src_end:
693 of_node_put(src_node);
696 int rsnd_src_probe(struct platform_device *pdev,
697 const struct rsnd_of_data *of_data,
698 struct rsnd_priv *priv)
700 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
701 struct device *dev = rsnd_priv_to_dev(priv);
702 struct rsnd_src *src;
703 struct rsnd_mod_ops *ops;
705 char name[RSND_SRC_NAME_SIZE];
709 if (rsnd_is_gen1(priv))
710 ops = &rsnd_src_gen1_ops;
711 if (rsnd_is_gen2(priv))
712 ops = &rsnd_src_gen2_ops;
714 dev_err(dev, "unknown Generation\n");
718 rsnd_of_parse_src(pdev, of_data, priv);
723 nr = info->src_info_nr;
727 src = devm_kzalloc(dev, sizeof(*src) * nr, GFP_KERNEL);
729 dev_err(dev, "SRC allocate failed\n");
736 for_each_rsnd_src(src, priv, i) {
737 snprintf(name, RSND_SRC_NAME_SIZE, "%s.%d",
740 clk = devm_clk_get(dev, name);
744 src->info = &info->src_info[i];
747 rsnd_mod_init(priv, &src->mod, ops, RSND_MOD_SRC, i);
749 dev_dbg(dev, "SRC%d probed\n", i);