2 * Renesas R-Car SCU 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_scu_platform_info *info; /* rcar_snd.h */
19 #define RSND_SCU_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_scu_mode_flags(p) ((p)->info->flags)
31 #define rsnd_scu_convert_rate(p) ((p)->info->convert_rate)
32 #define rsnd_mod_to_scu(_mod) \
33 container_of((_mod), struct rsnd_scu, mod)
34 #define rsnd_scu_hpbif_is_enable(scu) \
35 (rsnd_scu_mode_flags(scu) & RSND_SCU_USE_HPBIF)
36 #define rsnd_scu_dma_available(scu) \
37 rsnd_dma_available(rsnd_mod_to_dma(&(scu)->mod))
39 #define for_each_rsnd_scu(pos, priv, i) \
41 ((i) < rsnd_scu_nr(priv)) && \
42 ((pos) = (struct rsnd_scu *)(priv)->scu + 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] -> [SCU] -> [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_scu_platform_info {
87 * rsnd_scu_hpbif_is_enable() will be true
88 * if flags had RSND_SCU_USE_HPBIF,
89 * and it controls whether SSIU is used or not.
91 * rsnd_scu_convert_rate() indicates
92 * above convert_rate, and it controls
93 * whether SRC is used or not.
96 * struct rsnd_scu_platform_info info = {
102 * struct rsnd_scu_platform_info info = {
103 * .flags = RSND_SCU_USE_HPBIF,
104 * .convert_rate = 48000,
107 * ex) uses SRC bypass mode
108 * struct rsnd_scu_platform_info info = {
109 * .flags = RSND_SCU_USE_HPBIF,
116 * Gen1/Gen2 common functions
118 static int rsnd_scu_ssi_mode_init(struct rsnd_mod *mod,
119 struct rsnd_dai *rdai,
120 struct rsnd_dai_stream *io)
122 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
123 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
124 struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io);
125 int ssi_id = rsnd_mod_id(ssi_mod);
126 u32 convert_rate = rsnd_scu_convert_rate(scu);
128 if (convert_rate && !rsnd_dai_is_clk_master(rdai)) {
129 struct device *dev = rsnd_priv_to_dev(priv);
131 dev_err(dev, "rsnd should be clk master when you rate convert\n");
138 rsnd_mod_bset(mod, SSI_MODE0, (1 << ssi_id),
139 rsnd_scu_hpbif_is_enable(scu) ? 0 : (1 << ssi_id));
144 if (rsnd_ssi_is_pin_sharing(ssi_mod)) {
159 rsnd_mod_bset(mod, SSI_MODE1,
161 rsnd_dai_is_clk_master(rdai) ?
162 0x2 << shift : 0x1 << shift);
168 unsigned int rsnd_scu_get_ssi_rate(struct rsnd_priv *priv,
169 struct rsnd_dai_stream *io,
170 struct snd_pcm_runtime *runtime)
172 struct rsnd_scu *scu;
175 scu = rsnd_mod_to_scu(rsnd_io_to_mod_scu(io));
178 * return convert rate if SRC is used,
179 * otherwise, return runtime->rate as usual
181 rate = rsnd_scu_convert_rate(scu);
183 rate = runtime->rate;
188 static int rsnd_scu_set_convert_rate(struct rsnd_mod *mod,
189 struct rsnd_dai *rdai,
190 struct rsnd_dai_stream *io)
192 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
193 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
194 u32 convert_rate = rsnd_scu_convert_rate(scu);
195 u32 adinr = runtime->channels;
199 fsrate = 0x0400000 / convert_rate * runtime->rate;
201 /* set/clear soft reset */
202 rsnd_mod_write(mod, SRC_SWRSR, 0);
203 rsnd_mod_write(mod, SRC_SWRSR, 1);
206 * Initialize the operation of the SRC internal circuits
207 * see rsnd_scu_start()
209 rsnd_mod_write(mod, SRC_SRCIR, 1);
211 /* Set channel number and output bit length */
212 switch (runtime->sample_bits) {
222 rsnd_mod_write(mod, SRC_ADINR, adinr);
224 /* Enable the initial value of IFS */
226 rsnd_mod_write(mod, SRC_IFSCR, 1);
228 /* Set initial value of IFS */
229 rsnd_mod_write(mod, SRC_IFSVR, fsrate);
232 /* use DMA transfer */
233 rsnd_mod_write(mod, SRC_BUSIF_MODE, 1);
238 static int rsnd_scu_init(struct rsnd_mod *mod,
239 struct rsnd_dai *rdai,
240 struct rsnd_dai_stream *io)
242 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
245 clk_enable(scu->clk);
247 ret = rsnd_scu_ssi_mode_init(mod, rdai, io);
254 static int rsnd_scu_quit(struct rsnd_mod *mod,
255 struct rsnd_dai *rdai,
256 struct rsnd_dai_stream *io)
258 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
260 clk_disable(scu->clk);
265 static int rsnd_scu_start(struct rsnd_mod *mod,
266 struct rsnd_dai *rdai,
267 struct rsnd_dai_stream *io)
269 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
272 * Cancel the initialization and operate the SRC function
273 * see rsnd_scu_set_convert_rate()
275 rsnd_mod_write(mod, SRC_SRCIR, 0);
277 if (rsnd_scu_convert_rate(scu))
278 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 1);
284 static int rsnd_scu_stop(struct rsnd_mod *mod,
285 struct rsnd_dai *rdai,
286 struct rsnd_dai_stream *io)
288 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
290 if (rsnd_scu_convert_rate(scu))
291 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 0);
296 static struct rsnd_mod_ops rsnd_scu_non_ops = {
303 static int rsnd_src_set_route_gen1(struct rsnd_mod *mod,
304 struct rsnd_dai *rdai,
305 struct rsnd_dai_stream *io)
307 struct scu_route_config {
314 { 0x7, 12, }, /* 3 */
315 { 0x7, 16, }, /* 4 */
316 { 0x7, 20, }, /* 5 */
317 { 0x7, 24, }, /* 6 */
318 { 0x3, 28, }, /* 7 */
319 { 0x3, 30, }, /* 8 */
325 id = rsnd_mod_id(mod);
326 if (id < 0 || id >= ARRAY_SIZE(routes))
332 val = rsnd_dai_is_play(rdai, io) ? 0x1 : 0x2;
333 val = val << routes[id].shift;
334 mask = routes[id].mask << routes[id].shift;
336 rsnd_mod_bset(mod, SRC_ROUTE_SEL, mask, val);
341 static int rsnd_scu_set_convert_timing_gen1(struct rsnd_mod *mod,
342 struct rsnd_dai *rdai,
343 struct rsnd_dai_stream *io)
345 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
346 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
347 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
348 u32 convert_rate = rsnd_scu_convert_rate(scu);
352 int id = rsnd_mod_id(mod);
358 shift = (id % 4) * 8;
359 mask = 0x1F << shift;
362 * ADG is used as source clock if SRC was used,
363 * then, SSI WS is used as destination clock.
364 * SSI WS is used as source clock if SRC is not used
365 * (when playback, source/destination become reverse when capture)
371 ret = rsnd_adg_set_convert_clk_gen1(priv, mod,
374 } else if (8 == id) {
375 /* use SSI WS, but SRU8 is special */
379 val = (id + 1) << shift;
387 rsnd_mod_bset(mod, SRC_TMG_SEL0, mask, val);
390 rsnd_mod_bset(mod, SRC_TMG_SEL1, mask, val);
393 rsnd_mod_bset(mod, SRC_TMG_SEL2, mask, val);
400 static int rsnd_scu_set_convert_rate_gen1(struct rsnd_mod *mod,
401 struct rsnd_dai *rdai,
402 struct rsnd_dai_stream *io)
406 ret = rsnd_scu_set_convert_rate(mod, rdai, io);
410 /* Select SRC mode (fixed value) */
411 rsnd_mod_write(mod, SRC_SRCCR, 0x00010110);
413 /* Set the restriction value of the FS ratio (98%) */
414 rsnd_mod_write(mod, SRC_MNFSR,
415 rsnd_mod_read(mod, SRC_IFSVR) / 100 * 98);
417 /* no SRC_BFSSR settings, since SRC_SRCCR::BUFMD is 0 */
422 static int rsnd_scu_init_gen1(struct rsnd_mod *mod,
423 struct rsnd_dai *rdai,
424 struct rsnd_dai_stream *io)
428 ret = rsnd_scu_init(mod, rdai, io);
432 ret = rsnd_src_set_route_gen1(mod, rdai, io);
436 ret = rsnd_scu_set_convert_rate_gen1(mod, rdai, io);
440 ret = rsnd_scu_set_convert_timing_gen1(mod, rdai, io);
447 static int rsnd_scu_start_gen1(struct rsnd_mod *mod,
448 struct rsnd_dai *rdai,
449 struct rsnd_dai_stream *io)
451 int id = rsnd_mod_id(mod);
453 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), (1 << id));
455 return rsnd_scu_start(mod, rdai, io);
458 static int rsnd_scu_stop_gen1(struct rsnd_mod *mod,
459 struct rsnd_dai *rdai,
460 struct rsnd_dai_stream *io)
462 int id = rsnd_mod_id(mod);
464 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), 0);
466 return rsnd_scu_stop(mod, rdai, io);
469 static struct rsnd_mod_ops rsnd_scu_gen1_ops = {
470 .name = "sru (gen1)",
471 .init = rsnd_scu_init_gen1,
472 .quit = rsnd_scu_quit,
473 .start = rsnd_scu_start_gen1,
474 .stop = rsnd_scu_stop_gen1,
477 static struct rsnd_mod_ops rsnd_scu_non_gen1_ops = {
478 .name = "non-sru (gen1)",
479 .init = rsnd_scu_ssi_mode_init,
485 static int rsnd_scu_set_convert_rate_gen2(struct rsnd_mod *mod,
486 struct rsnd_dai *rdai,
487 struct rsnd_dai_stream *io)
491 ret = rsnd_scu_set_convert_rate(mod, rdai, io);
495 rsnd_mod_write(mod, SSI_BUSIF_ADINR, rsnd_mod_read(mod, SRC_ADINR));
496 rsnd_mod_write(mod, SSI_BUSIF_MODE, rsnd_mod_read(mod, SRC_BUSIF_MODE));
498 rsnd_mod_write(mod, SRC_SRCCR, 0x00011110);
500 rsnd_mod_write(mod, SRC_BSDSR, 0x01800000);
501 rsnd_mod_write(mod, SRC_BSISR, 0x00100060);
506 static int rsnd_scu_set_convert_timing_gen2(struct rsnd_mod *mod,
507 struct rsnd_dai *rdai,
508 struct rsnd_dai_stream *io)
510 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
511 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
512 u32 convert_rate = rsnd_scu_convert_rate(scu);
516 ret = rsnd_adg_set_convert_clk_gen2(mod, rdai, io,
520 ret = rsnd_adg_set_convert_timing_gen2(mod, rdai, io);
525 static int rsnd_scu_init_gen2(struct rsnd_mod *mod,
526 struct rsnd_dai *rdai,
527 struct rsnd_dai_stream *io)
531 ret = rsnd_scu_init(mod, rdai, io);
535 ret = rsnd_scu_set_convert_rate_gen2(mod, rdai, io);
539 ret = rsnd_scu_set_convert_timing_gen2(mod, rdai, io);
546 static int rsnd_scu_start_gen2(struct rsnd_mod *mod,
547 struct rsnd_dai *rdai,
548 struct rsnd_dai_stream *io)
550 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
552 rsnd_dma_start(rsnd_mod_to_dma(&scu->mod));
554 rsnd_mod_write(mod, SSI_CTRL, 0x1);
555 rsnd_mod_write(mod, SRC_CTRL, 0x11);
557 return rsnd_scu_start(mod, rdai, io);
560 static int rsnd_scu_stop_gen2(struct rsnd_mod *mod,
561 struct rsnd_dai *rdai,
562 struct rsnd_dai_stream *io)
564 struct rsnd_scu *scu = rsnd_mod_to_scu(mod);
566 rsnd_mod_write(mod, SSI_CTRL, 0);
567 rsnd_mod_write(mod, SRC_CTRL, 0);
569 rsnd_dma_stop(rsnd_mod_to_dma(&scu->mod));
571 return rsnd_scu_stop(mod, rdai, io);
574 static struct rsnd_mod_ops rsnd_scu_gen2_ops = {
575 .name = "scu (gen2)",
576 .init = rsnd_scu_init_gen2,
577 .quit = rsnd_scu_quit,
578 .start = rsnd_scu_start_gen2,
579 .stop = rsnd_scu_stop_gen2,
582 static int rsnd_scu_start_non_gen2(struct rsnd_mod *mod,
583 struct rsnd_dai *rdai,
584 struct rsnd_dai_stream *io)
586 struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io);
588 /* enable PIO interrupt */
589 rsnd_mod_write(ssi_mod, INT_ENABLE, 0x0f000000);
594 static struct rsnd_mod_ops rsnd_scu_non_gen2_ops = {
595 .name = "non-scu (gen2)",
596 .init = rsnd_scu_ssi_mode_init,
597 .start = rsnd_scu_start_non_gen2,
600 struct rsnd_mod *rsnd_scu_mod_get(struct rsnd_priv *priv, int id)
602 if (WARN_ON(id < 0 || id >= rsnd_scu_nr(priv)))
605 return &((struct rsnd_scu *)(priv->scu) + id)->mod;
608 int rsnd_scu_probe(struct platform_device *pdev,
609 struct rsnd_priv *priv)
611 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
612 struct device *dev = rsnd_priv_to_dev(priv);
613 struct rsnd_scu *scu;
614 struct rsnd_mod_ops *ops;
616 char name[RSND_SCU_NAME_SIZE];
622 nr = info->scu_info_nr;
623 scu = devm_kzalloc(dev, sizeof(*scu) * nr, GFP_KERNEL);
625 dev_err(dev, "SCU allocate failed\n");
632 for_each_rsnd_scu(scu, priv, i) {
633 snprintf(name, RSND_SCU_NAME_SIZE, "scu.%d", i);
635 clk = devm_clk_get(dev, name);
639 scu->info = &info->scu_info[i];
642 ops = &rsnd_scu_non_ops;
643 if (rsnd_scu_hpbif_is_enable(scu)) {
644 if (rsnd_is_gen1(priv))
645 ops = &rsnd_scu_gen1_ops;
646 if (rsnd_is_gen2(priv)) {
647 struct rsnd_mod *ssi = rsnd_ssi_mod_get(priv, i);
648 int ret = rsnd_dma_init(priv,
649 rsnd_mod_to_dma(&scu->mod),
650 rsnd_ssi_is_play(ssi),
655 ops = &rsnd_scu_gen2_ops;
658 if (rsnd_is_gen1(priv))
659 ops = &rsnd_scu_non_gen1_ops;
660 if (rsnd_is_gen2(priv))
661 ops = &rsnd_scu_non_gen2_ops;
664 rsnd_mod_init(priv, &scu->mod, ops, RSND_MOD_SCU, i);
666 dev_dbg(dev, "SCU%d probed\n", i);
672 void rsnd_scu_remove(struct platform_device *pdev,
673 struct rsnd_priv *priv)
675 struct rsnd_scu *scu;
678 for_each_rsnd_scu(scu, priv, i) {
679 if (rsnd_scu_dma_available(scu))
680 rsnd_dma_quit(priv, rsnd_mod_to_dma(&scu->mod));