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.
14 struct rsnd_src_platform_info *info; /* rcar_snd.h */
19 #define RSND_SRC_NAME_SIZE 16
24 #define OTBL_24 (0 << 16)
25 #define OTBL_22 (2 << 16)
26 #define OTBL_20 (4 << 16)
27 #define OTBL_18 (6 << 16)
28 #define OTBL_16 (8 << 16)
30 #define rsnd_src_mode_flags(p) ((p)->info->flags)
31 #define rsnd_src_convert_rate(p) ((p)->info->convert_rate)
32 #define rsnd_mod_to_src(_mod) \
33 container_of((_mod), struct rsnd_src, mod)
34 #define rsnd_src_hpbif_is_enable(src) \
35 (rsnd_src_mode_flags(src) & RSND_SCU_USE_HPBIF)
36 #define rsnd_src_dma_available(src) \
37 rsnd_dma_available(rsnd_mod_to_dma(&(src)->mod))
39 #define for_each_rsnd_src(pos, priv, i) \
41 ((i) < rsnd_src_nr(priv)) && \
42 ((pos) = (struct rsnd_src *)(priv)->src + i); \
47 * image of SRC (Sampling Rate Converter)
49 * 96kHz <-> +-----+ 48kHz +-----+ 48kHz +-------+
50 * 48kHz <-> | SRC | <------> | SSI | <-----> | codec |
51 * 44.1kHz <-> +-----+ +-----+ +-------+
61 * [mem] -> [SRU] -> [SSI]
66 * [mem] -> [SRC] -> [SSIU] -> [SSI]
71 * How to use SRC bypass mode for debugging
73 * SRC has bypass mode, and it is useful for debugging.
75 * SRCm_MODE controls whether SRC is used or not
76 * SSI_MODE0 controls whether SSIU which receives SRC data
78 * Both SRCm_MODE/SSI_MODE0 settings are needed if you use SRC,
79 * but SRC bypass mode needs SSI_MODE0 only.
82 * struct rsnd_src_platform_info {
87 * rsnd_src_hpbif_is_enable() will be true
88 * if flags had RSND_SRC_USE_HPBIF,
89 * and it controls whether SSIU is used or not.
91 * rsnd_src_convert_rate() indicates
92 * above convert_rate, and it controls
93 * whether SRC is used or not.
96 * struct rsnd_src_platform_info info = {
102 * struct rsnd_src_platform_info info = {
103 * .flags = RSND_SRC_USE_HPBIF,
104 * .convert_rate = 48000,
107 * ex) uses SRC bypass mode
108 * struct rsnd_src_platform_info info = {
109 * .flags = RSND_SRC_USE_HPBIF,
116 * Gen1/Gen2 common functions
118 int rsnd_src_ssi_mode_init(struct rsnd_mod *ssi_mod,
119 struct rsnd_dai *rdai,
120 struct rsnd_dai_stream *io)
122 struct rsnd_priv *priv = rsnd_mod_to_priv(ssi_mod);
123 struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
124 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
125 int ssi_id = rsnd_mod_id(ssi_mod);
131 if (info->dai_info) {
134 struct rsnd_src *src = rsnd_mod_to_src(src_mod);
135 has_src = rsnd_src_hpbif_is_enable(src);
138 rsnd_mod_bset(ssi_mod, SSI_MODE0, (1 << ssi_id),
139 has_src ? 0 : (1 << ssi_id));
144 if (rsnd_ssi_is_pin_sharing(ssi_mod)) {
159 rsnd_mod_bset(ssi_mod, SSI_MODE1,
161 rsnd_dai_is_clk_master(rdai) ?
162 0x2 << shift : 0x1 << shift);
168 int rsnd_src_enable_ssi_irq(struct rsnd_mod *ssi_mod,
169 struct rsnd_dai *rdai,
170 struct rsnd_dai_stream *io)
172 struct rsnd_priv *priv = rsnd_mod_to_priv(ssi_mod);
174 /* enable PIO interrupt if Gen2 */
175 if (rsnd_is_gen2(priv))
176 rsnd_mod_write(ssi_mod, INT_ENABLE, 0x0f000000);
181 unsigned int rsnd_src_get_ssi_rate(struct rsnd_priv *priv,
182 struct rsnd_dai_stream *io,
183 struct snd_pcm_runtime *runtime)
185 struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
186 struct rsnd_src *src;
187 unsigned int rate = 0;
190 src = rsnd_mod_to_src(src_mod);
193 * return convert rate if SRC is used,
194 * otherwise, return runtime->rate as usual
196 rate = rsnd_src_convert_rate(src);
200 rate = runtime->rate;
205 static int rsnd_src_set_convert_rate(struct rsnd_mod *mod,
206 struct rsnd_dai *rdai,
207 struct rsnd_dai_stream *io)
209 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
210 struct rsnd_src *src = rsnd_mod_to_src(mod);
211 u32 convert_rate = rsnd_src_convert_rate(src);
212 u32 adinr = runtime->channels;
216 fsrate = 0x0400000 / convert_rate * runtime->rate;
218 /* set/clear soft reset */
219 rsnd_mod_write(mod, SRC_SWRSR, 0);
220 rsnd_mod_write(mod, SRC_SWRSR, 1);
223 * Initialize the operation of the SRC internal circuits
224 * see rsnd_src_start()
226 rsnd_mod_write(mod, SRC_SRCIR, 1);
228 /* Set channel number and output bit length */
229 switch (runtime->sample_bits) {
239 rsnd_mod_write(mod, SRC_ADINR, adinr);
241 /* Enable the initial value of IFS */
243 rsnd_mod_write(mod, SRC_IFSCR, 1);
245 /* Set initial value of IFS */
246 rsnd_mod_write(mod, SRC_IFSVR, fsrate);
249 /* use DMA transfer */
250 rsnd_mod_write(mod, SRC_BUSIF_MODE, 1);
255 static int rsnd_src_init(struct rsnd_mod *mod,
256 struct rsnd_dai *rdai,
257 struct rsnd_dai_stream *io)
259 struct rsnd_src *src = rsnd_mod_to_src(mod);
261 clk_enable(src->clk);
266 static int rsnd_src_quit(struct rsnd_mod *mod,
267 struct rsnd_dai *rdai,
268 struct rsnd_dai_stream *io)
270 struct rsnd_src *src = rsnd_mod_to_src(mod);
272 clk_disable(src->clk);
277 static int rsnd_src_start(struct rsnd_mod *mod,
278 struct rsnd_dai *rdai,
279 struct rsnd_dai_stream *io)
281 struct rsnd_src *src = rsnd_mod_to_src(mod);
284 * Cancel the initialization and operate the SRC function
285 * see rsnd_src_set_convert_rate()
287 rsnd_mod_write(mod, SRC_SRCIR, 0);
289 if (rsnd_src_convert_rate(src))
290 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 1);
296 static int rsnd_src_stop(struct rsnd_mod *mod,
297 struct rsnd_dai *rdai,
298 struct rsnd_dai_stream *io)
300 struct rsnd_src *src = rsnd_mod_to_src(mod);
302 if (rsnd_src_convert_rate(src))
303 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 0);
308 static struct rsnd_mod_ops rsnd_src_non_ops = {
315 static int rsnd_src_set_route_gen1(struct rsnd_mod *mod,
316 struct rsnd_dai *rdai,
317 struct rsnd_dai_stream *io)
319 struct src_route_config {
326 { 0x7, 12, }, /* 3 */
327 { 0x7, 16, }, /* 4 */
328 { 0x7, 20, }, /* 5 */
329 { 0x7, 24, }, /* 6 */
330 { 0x3, 28, }, /* 7 */
331 { 0x3, 30, }, /* 8 */
337 id = rsnd_mod_id(mod);
338 if (id < 0 || id >= ARRAY_SIZE(routes))
344 val = rsnd_dai_is_play(rdai, io) ? 0x1 : 0x2;
345 val = val << routes[id].shift;
346 mask = routes[id].mask << routes[id].shift;
348 rsnd_mod_bset(mod, SRC_ROUTE_SEL, mask, val);
353 static int rsnd_src_set_convert_timing_gen1(struct rsnd_mod *mod,
354 struct rsnd_dai *rdai,
355 struct rsnd_dai_stream *io)
357 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
358 struct rsnd_src *src = rsnd_mod_to_src(mod);
359 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
360 u32 convert_rate = rsnd_src_convert_rate(src);
364 int id = rsnd_mod_id(mod);
370 shift = (id % 4) * 8;
371 mask = 0x1F << shift;
374 * ADG is used as source clock if SRC was used,
375 * then, SSI WS is used as destination clock.
376 * SSI WS is used as source clock if SRC is not used
377 * (when playback, source/destination become reverse when capture)
383 ret = rsnd_adg_set_convert_clk_gen1(priv, mod,
386 } else if (8 == id) {
387 /* use SSI WS, but SRU8 is special */
391 val = (id + 1) << shift;
399 rsnd_mod_bset(mod, SRC_TMG_SEL0, mask, val);
402 rsnd_mod_bset(mod, SRC_TMG_SEL1, mask, val);
405 rsnd_mod_bset(mod, SRC_TMG_SEL2, mask, val);
412 static int rsnd_src_set_convert_rate_gen1(struct rsnd_mod *mod,
413 struct rsnd_dai *rdai,
414 struct rsnd_dai_stream *io)
418 ret = rsnd_src_set_convert_rate(mod, rdai, io);
422 /* Select SRC mode (fixed value) */
423 rsnd_mod_write(mod, SRC_SRCCR, 0x00010110);
425 /* Set the restriction value of the FS ratio (98%) */
426 rsnd_mod_write(mod, SRC_MNFSR,
427 rsnd_mod_read(mod, SRC_IFSVR) / 100 * 98);
429 /* no SRC_BFSSR settings, since SRC_SRCCR::BUFMD is 0 */
434 static int rsnd_src_init_gen1(struct rsnd_mod *mod,
435 struct rsnd_dai *rdai,
436 struct rsnd_dai_stream *io)
440 ret = rsnd_src_init(mod, rdai, io);
444 ret = rsnd_src_set_route_gen1(mod, rdai, io);
448 ret = rsnd_src_set_convert_rate_gen1(mod, rdai, io);
452 ret = rsnd_src_set_convert_timing_gen1(mod, rdai, io);
459 static int rsnd_src_start_gen1(struct rsnd_mod *mod,
460 struct rsnd_dai *rdai,
461 struct rsnd_dai_stream *io)
463 int id = rsnd_mod_id(mod);
465 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), (1 << id));
467 return rsnd_src_start(mod, rdai, io);
470 static int rsnd_src_stop_gen1(struct rsnd_mod *mod,
471 struct rsnd_dai *rdai,
472 struct rsnd_dai_stream *io)
474 int id = rsnd_mod_id(mod);
476 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), 0);
478 return rsnd_src_stop(mod, rdai, io);
481 static struct rsnd_mod_ops rsnd_src_gen1_ops = {
482 .name = "sru (gen1)",
483 .init = rsnd_src_init_gen1,
484 .quit = rsnd_src_quit,
485 .start = rsnd_src_start_gen1,
486 .stop = rsnd_src_stop_gen1,
492 static int rsnd_src_set_convert_rate_gen2(struct rsnd_mod *mod,
493 struct rsnd_dai *rdai,
494 struct rsnd_dai_stream *io)
498 ret = rsnd_src_set_convert_rate(mod, rdai, io);
502 rsnd_mod_write(mod, SSI_BUSIF_ADINR, rsnd_mod_read(mod, SRC_ADINR));
503 rsnd_mod_write(mod, SSI_BUSIF_MODE, rsnd_mod_read(mod, SRC_BUSIF_MODE));
505 rsnd_mod_write(mod, SRC_SRCCR, 0x00011110);
507 rsnd_mod_write(mod, SRC_BSDSR, 0x01800000);
508 rsnd_mod_write(mod, SRC_BSISR, 0x00100060);
513 static int rsnd_src_set_convert_timing_gen2(struct rsnd_mod *mod,
514 struct rsnd_dai *rdai,
515 struct rsnd_dai_stream *io)
517 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
518 struct rsnd_src *src = rsnd_mod_to_src(mod);
519 u32 convert_rate = rsnd_src_convert_rate(src);
523 ret = rsnd_adg_set_convert_clk_gen2(mod, rdai, io,
527 ret = rsnd_adg_set_convert_timing_gen2(mod, rdai, io);
532 static int rsnd_src_probe_gen2(struct rsnd_mod *mod,
533 struct rsnd_dai *rdai,
534 struct rsnd_dai_stream *io)
536 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
537 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
538 struct rsnd_src *src = rsnd_mod_to_src(mod);
539 struct rsnd_mod *ssi = rsnd_ssi_mod_get(priv, rsnd_mod_id(mod));
540 struct device *dev = rsnd_priv_to_dev(priv);
545 is_play = rsnd_info_is_playback(priv, src);
547 is_play = rsnd_ssi_is_play(ssi);
549 ret = rsnd_dma_init(priv,
550 rsnd_mod_to_dma(mod),
554 dev_err(dev, "SRC DMA failed\n");
559 static int rsnd_src_remove_gen2(struct rsnd_mod *mod,
560 struct rsnd_dai *rdai,
561 struct rsnd_dai_stream *io)
563 rsnd_dma_quit(rsnd_mod_to_priv(mod), rsnd_mod_to_dma(mod));
568 static int rsnd_src_init_gen2(struct rsnd_mod *mod,
569 struct rsnd_dai *rdai,
570 struct rsnd_dai_stream *io)
574 ret = rsnd_src_init(mod, rdai, io);
578 ret = rsnd_src_set_convert_rate_gen2(mod, rdai, io);
582 ret = rsnd_src_set_convert_timing_gen2(mod, rdai, io);
589 static int rsnd_src_start_gen2(struct rsnd_mod *mod,
590 struct rsnd_dai *rdai,
591 struct rsnd_dai_stream *io)
593 struct rsnd_src *src = rsnd_mod_to_src(mod);
595 rsnd_dma_start(rsnd_mod_to_dma(&src->mod));
597 rsnd_mod_write(mod, SSI_CTRL, 0x1);
598 rsnd_mod_write(mod, SRC_CTRL, 0x11);
600 return rsnd_src_start(mod, rdai, io);
603 static int rsnd_src_stop_gen2(struct rsnd_mod *mod,
604 struct rsnd_dai *rdai,
605 struct rsnd_dai_stream *io)
607 struct rsnd_src *src = rsnd_mod_to_src(mod);
609 rsnd_mod_write(mod, SSI_CTRL, 0);
610 rsnd_mod_write(mod, SRC_CTRL, 0);
612 rsnd_dma_stop(rsnd_mod_to_dma(&src->mod));
614 return rsnd_src_stop(mod, rdai, io);
617 static struct rsnd_mod_ops rsnd_src_gen2_ops = {
618 .name = "src (gen2)",
619 .probe = rsnd_src_probe_gen2,
620 .remove = rsnd_src_remove_gen2,
621 .init = rsnd_src_init_gen2,
622 .quit = rsnd_src_quit,
623 .start = rsnd_src_start_gen2,
624 .stop = rsnd_src_stop_gen2,
627 struct rsnd_mod *rsnd_src_mod_get(struct rsnd_priv *priv, int id)
629 if (WARN_ON(id < 0 || id >= rsnd_src_nr(priv)))
632 return &((struct rsnd_src *)(priv->src) + id)->mod;
635 static void rsnd_of_parse_src(struct platform_device *pdev,
636 const struct rsnd_of_data *of_data,
637 struct rsnd_priv *priv)
639 struct device_node *src_node;
640 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
641 struct rsnd_src_platform_info *src_info;
642 struct device *dev = &pdev->dev;
648 src_node = of_get_child_by_name(dev->of_node, "rcar_sound,src");
652 nr = of_get_child_count(src_node);
656 src_info = devm_kzalloc(dev,
657 sizeof(struct rsnd_src_platform_info) * nr,
660 dev_err(dev, "src info allocation error\n");
664 info->src_info = src_info;
665 info->src_info_nr = nr;
668 int rsnd_src_probe(struct platform_device *pdev,
669 const struct rsnd_of_data *of_data,
670 struct rsnd_priv *priv)
672 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
673 struct device *dev = rsnd_priv_to_dev(priv);
674 struct rsnd_src *src;
675 struct rsnd_mod_ops *ops;
677 char name[RSND_SRC_NAME_SIZE];
680 rsnd_of_parse_src(pdev, of_data, priv);
685 nr = info->src_info_nr;
689 src = devm_kzalloc(dev, sizeof(*src) * nr, GFP_KERNEL);
691 dev_err(dev, "SRC allocate failed\n");
698 for_each_rsnd_src(src, priv, i) {
699 snprintf(name, RSND_SRC_NAME_SIZE, "src.%d", i);
701 clk = devm_clk_get(dev, name);
703 snprintf(name, RSND_SRC_NAME_SIZE, "scu.%d", i);
704 clk = devm_clk_get(dev, name);
710 src->info = &info->src_info[i];
713 ops = &rsnd_src_non_ops;
714 if (rsnd_src_hpbif_is_enable(src)) {
715 if (rsnd_is_gen1(priv))
716 ops = &rsnd_src_gen1_ops;
717 if (rsnd_is_gen2(priv))
718 ops = &rsnd_src_gen2_ops;
721 rsnd_mod_init(priv, &src->mod, ops, RSND_MOD_SRC, i);
723 dev_dbg(dev, "SRC%d probed\n", i);