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_src *src;
188 src = rsnd_mod_to_src(rsnd_io_to_mod_src(io));
191 * return convert rate if SRC is used,
192 * otherwise, return runtime->rate as usual
194 rate = rsnd_src_convert_rate(src);
196 rate = runtime->rate;
201 static int rsnd_src_set_convert_rate(struct rsnd_mod *mod,
202 struct rsnd_dai *rdai,
203 struct rsnd_dai_stream *io)
205 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
206 struct rsnd_src *src = rsnd_mod_to_src(mod);
207 u32 convert_rate = rsnd_src_convert_rate(src);
208 u32 adinr = runtime->channels;
212 fsrate = 0x0400000 / convert_rate * runtime->rate;
214 /* set/clear soft reset */
215 rsnd_mod_write(mod, SRC_SWRSR, 0);
216 rsnd_mod_write(mod, SRC_SWRSR, 1);
219 * Initialize the operation of the SRC internal circuits
220 * see rsnd_src_start()
222 rsnd_mod_write(mod, SRC_SRCIR, 1);
224 /* Set channel number and output bit length */
225 switch (runtime->sample_bits) {
235 rsnd_mod_write(mod, SRC_ADINR, adinr);
237 /* Enable the initial value of IFS */
239 rsnd_mod_write(mod, SRC_IFSCR, 1);
241 /* Set initial value of IFS */
242 rsnd_mod_write(mod, SRC_IFSVR, fsrate);
245 /* use DMA transfer */
246 rsnd_mod_write(mod, SRC_BUSIF_MODE, 1);
251 static int rsnd_src_init(struct rsnd_mod *mod,
252 struct rsnd_dai *rdai,
253 struct rsnd_dai_stream *io)
255 struct rsnd_src *src = rsnd_mod_to_src(mod);
257 clk_enable(src->clk);
262 static int rsnd_src_quit(struct rsnd_mod *mod,
263 struct rsnd_dai *rdai,
264 struct rsnd_dai_stream *io)
266 struct rsnd_src *src = rsnd_mod_to_src(mod);
268 clk_disable(src->clk);
273 static int rsnd_src_start(struct rsnd_mod *mod,
274 struct rsnd_dai *rdai,
275 struct rsnd_dai_stream *io)
277 struct rsnd_src *src = rsnd_mod_to_src(mod);
280 * Cancel the initialization and operate the SRC function
281 * see rsnd_src_set_convert_rate()
283 rsnd_mod_write(mod, SRC_SRCIR, 0);
285 if (rsnd_src_convert_rate(src))
286 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 1);
292 static int rsnd_src_stop(struct rsnd_mod *mod,
293 struct rsnd_dai *rdai,
294 struct rsnd_dai_stream *io)
296 struct rsnd_src *src = rsnd_mod_to_src(mod);
298 if (rsnd_src_convert_rate(src))
299 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 0);
304 static struct rsnd_mod_ops rsnd_src_non_ops = {
311 static int rsnd_src_set_route_gen1(struct rsnd_mod *mod,
312 struct rsnd_dai *rdai,
313 struct rsnd_dai_stream *io)
315 struct src_route_config {
322 { 0x7, 12, }, /* 3 */
323 { 0x7, 16, }, /* 4 */
324 { 0x7, 20, }, /* 5 */
325 { 0x7, 24, }, /* 6 */
326 { 0x3, 28, }, /* 7 */
327 { 0x3, 30, }, /* 8 */
333 id = rsnd_mod_id(mod);
334 if (id < 0 || id >= ARRAY_SIZE(routes))
340 val = rsnd_dai_is_play(rdai, io) ? 0x1 : 0x2;
341 val = val << routes[id].shift;
342 mask = routes[id].mask << routes[id].shift;
344 rsnd_mod_bset(mod, SRC_ROUTE_SEL, mask, val);
349 static int rsnd_src_set_convert_timing_gen1(struct rsnd_mod *mod,
350 struct rsnd_dai *rdai,
351 struct rsnd_dai_stream *io)
353 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
354 struct rsnd_src *src = rsnd_mod_to_src(mod);
355 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
356 u32 convert_rate = rsnd_src_convert_rate(src);
360 int id = rsnd_mod_id(mod);
366 shift = (id % 4) * 8;
367 mask = 0x1F << shift;
370 * ADG is used as source clock if SRC was used,
371 * then, SSI WS is used as destination clock.
372 * SSI WS is used as source clock if SRC is not used
373 * (when playback, source/destination become reverse when capture)
379 ret = rsnd_adg_set_convert_clk_gen1(priv, mod,
382 } else if (8 == id) {
383 /* use SSI WS, but SRU8 is special */
387 val = (id + 1) << shift;
395 rsnd_mod_bset(mod, SRC_TMG_SEL0, mask, val);
398 rsnd_mod_bset(mod, SRC_TMG_SEL1, mask, val);
401 rsnd_mod_bset(mod, SRC_TMG_SEL2, mask, val);
408 static int rsnd_src_set_convert_rate_gen1(struct rsnd_mod *mod,
409 struct rsnd_dai *rdai,
410 struct rsnd_dai_stream *io)
414 ret = rsnd_src_set_convert_rate(mod, rdai, io);
418 /* Select SRC mode (fixed value) */
419 rsnd_mod_write(mod, SRC_SRCCR, 0x00010110);
421 /* Set the restriction value of the FS ratio (98%) */
422 rsnd_mod_write(mod, SRC_MNFSR,
423 rsnd_mod_read(mod, SRC_IFSVR) / 100 * 98);
425 /* no SRC_BFSSR settings, since SRC_SRCCR::BUFMD is 0 */
430 static int rsnd_src_init_gen1(struct rsnd_mod *mod,
431 struct rsnd_dai *rdai,
432 struct rsnd_dai_stream *io)
436 ret = rsnd_src_init(mod, rdai, io);
440 ret = rsnd_src_set_route_gen1(mod, rdai, io);
444 ret = rsnd_src_set_convert_rate_gen1(mod, rdai, io);
448 ret = rsnd_src_set_convert_timing_gen1(mod, rdai, io);
455 static int rsnd_src_start_gen1(struct rsnd_mod *mod,
456 struct rsnd_dai *rdai,
457 struct rsnd_dai_stream *io)
459 int id = rsnd_mod_id(mod);
461 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), (1 << id));
463 return rsnd_src_start(mod, rdai, io);
466 static int rsnd_src_stop_gen1(struct rsnd_mod *mod,
467 struct rsnd_dai *rdai,
468 struct rsnd_dai_stream *io)
470 int id = rsnd_mod_id(mod);
472 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), 0);
474 return rsnd_src_stop(mod, rdai, io);
477 static struct rsnd_mod_ops rsnd_src_gen1_ops = {
478 .name = "sru (gen1)",
479 .init = rsnd_src_init_gen1,
480 .quit = rsnd_src_quit,
481 .start = rsnd_src_start_gen1,
482 .stop = rsnd_src_stop_gen1,
488 static int rsnd_src_set_convert_rate_gen2(struct rsnd_mod *mod,
489 struct rsnd_dai *rdai,
490 struct rsnd_dai_stream *io)
494 ret = rsnd_src_set_convert_rate(mod, rdai, io);
498 rsnd_mod_write(mod, SSI_BUSIF_ADINR, rsnd_mod_read(mod, SRC_ADINR));
499 rsnd_mod_write(mod, SSI_BUSIF_MODE, rsnd_mod_read(mod, SRC_BUSIF_MODE));
501 rsnd_mod_write(mod, SRC_SRCCR, 0x00011110);
503 rsnd_mod_write(mod, SRC_BSDSR, 0x01800000);
504 rsnd_mod_write(mod, SRC_BSISR, 0x00100060);
509 static int rsnd_src_set_convert_timing_gen2(struct rsnd_mod *mod,
510 struct rsnd_dai *rdai,
511 struct rsnd_dai_stream *io)
513 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
514 struct rsnd_src *src = rsnd_mod_to_src(mod);
515 u32 convert_rate = rsnd_src_convert_rate(src);
519 ret = rsnd_adg_set_convert_clk_gen2(mod, rdai, io,
523 ret = rsnd_adg_set_convert_timing_gen2(mod, rdai, io);
528 static int rsnd_src_probe_gen2(struct rsnd_mod *mod,
529 struct rsnd_dai *rdai,
530 struct rsnd_dai_stream *io)
532 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
533 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
534 struct rsnd_src *src = rsnd_mod_to_src(mod);
535 struct rsnd_mod *ssi = rsnd_ssi_mod_get(priv, rsnd_mod_id(mod));
536 struct device *dev = rsnd_priv_to_dev(priv);
541 is_play = rsnd_info_is_playback(priv, src);
543 is_play = rsnd_ssi_is_play(ssi);
545 ret = rsnd_dma_init(priv,
546 rsnd_mod_to_dma(mod),
550 dev_err(dev, "SRC DMA failed\n");
555 static int rsnd_src_remove_gen2(struct rsnd_mod *mod,
556 struct rsnd_dai *rdai,
557 struct rsnd_dai_stream *io)
559 rsnd_dma_quit(rsnd_mod_to_priv(mod), rsnd_mod_to_dma(mod));
564 static int rsnd_src_init_gen2(struct rsnd_mod *mod,
565 struct rsnd_dai *rdai,
566 struct rsnd_dai_stream *io)
570 ret = rsnd_src_init(mod, rdai, io);
574 ret = rsnd_src_set_convert_rate_gen2(mod, rdai, io);
578 ret = rsnd_src_set_convert_timing_gen2(mod, rdai, io);
585 static int rsnd_src_start_gen2(struct rsnd_mod *mod,
586 struct rsnd_dai *rdai,
587 struct rsnd_dai_stream *io)
589 struct rsnd_src *src = rsnd_mod_to_src(mod);
591 rsnd_dma_start(rsnd_mod_to_dma(&src->mod));
593 rsnd_mod_write(mod, SSI_CTRL, 0x1);
594 rsnd_mod_write(mod, SRC_CTRL, 0x11);
596 return rsnd_src_start(mod, rdai, io);
599 static int rsnd_src_stop_gen2(struct rsnd_mod *mod,
600 struct rsnd_dai *rdai,
601 struct rsnd_dai_stream *io)
603 struct rsnd_src *src = rsnd_mod_to_src(mod);
605 rsnd_mod_write(mod, SSI_CTRL, 0);
606 rsnd_mod_write(mod, SRC_CTRL, 0);
608 rsnd_dma_stop(rsnd_mod_to_dma(&src->mod));
610 return rsnd_src_stop(mod, rdai, io);
613 static struct rsnd_mod_ops rsnd_src_gen2_ops = {
614 .name = "src (gen2)",
615 .probe = rsnd_src_probe_gen2,
616 .remove = rsnd_src_remove_gen2,
617 .init = rsnd_src_init_gen2,
618 .quit = rsnd_src_quit,
619 .start = rsnd_src_start_gen2,
620 .stop = rsnd_src_stop_gen2,
623 struct rsnd_mod *rsnd_src_mod_get(struct rsnd_priv *priv, int id)
625 if (WARN_ON(id < 0 || id >= rsnd_src_nr(priv)))
628 return &((struct rsnd_src *)(priv->src) + id)->mod;
631 int rsnd_src_probe(struct platform_device *pdev,
632 struct rsnd_priv *priv)
634 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
635 struct device *dev = rsnd_priv_to_dev(priv);
636 struct rsnd_src *src;
637 struct rsnd_mod_ops *ops;
639 char name[RSND_SRC_NAME_SIZE];
645 nr = info->src_info_nr;
649 src = devm_kzalloc(dev, sizeof(*src) * nr, GFP_KERNEL);
651 dev_err(dev, "SRC allocate failed\n");
658 for_each_rsnd_src(src, priv, i) {
659 snprintf(name, RSND_SRC_NAME_SIZE, "src.%d", i);
661 clk = devm_clk_get(dev, name);
663 snprintf(name, RSND_SRC_NAME_SIZE, "scu.%d", i);
664 clk = devm_clk_get(dev, name);
670 src->info = &info->src_info[i];
673 ops = &rsnd_src_non_ops;
674 if (rsnd_src_hpbif_is_enable(src)) {
675 if (rsnd_is_gen1(priv))
676 ops = &rsnd_src_gen1_ops;
677 if (rsnd_is_gen2(priv))
678 ops = &rsnd_src_gen2_ops;
681 rsnd_mod_init(priv, &src->mod, ops, RSND_MOD_SRC, i);
683 dev_dbg(dev, "SRC%d probed\n", i);