aboutsummaryrefslogtreecommitdiffstats
path: root/sound/soc/sh/rcar/src.c
diff options
context:
space:
mode:
Diffstat (limited to 'sound/soc/sh/rcar/src.c')
-rw-r--r--sound/soc/sh/rcar/src.c687
1 files changed, 687 insertions, 0 deletions
diff --git a/sound/soc/sh/rcar/src.c b/sound/soc/sh/rcar/src.c
new file mode 100644
index 000000000000..ea6a214985d0
--- /dev/null
+++ b/sound/soc/sh/rcar/src.c
@@ -0,0 +1,687 @@
1/*
2 * Renesas R-Car SRC support
3 *
4 * Copyright (C) 2013 Renesas Solutions Corp.
5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
6 *
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.
10 */
11#include "rsnd.h"
12
13struct rsnd_src {
14 struct rsnd_src_platform_info *info; /* rcar_snd.h */
15 struct rsnd_mod mod;
16 struct clk *clk;
17};
18
19#define RSND_SRC_NAME_SIZE 16
20
21/*
22 * ADINR
23 */
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)
29
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))
38
39#define for_each_rsnd_src(pos, priv, i) \
40 for ((i) = 0; \
41 ((i) < rsnd_src_nr(priv)) && \
42 ((pos) = (struct rsnd_src *)(priv)->src + i); \
43 i++)
44
45
46/*
47 * image of SRC (Sampling Rate Converter)
48 *
49 * 96kHz <-> +-----+ 48kHz +-----+ 48kHz +-------+
50 * 48kHz <-> | SRC | <------> | SSI | <-----> | codec |
51 * 44.1kHz <-> +-----+ +-----+ +-------+
52 * ...
53 *
54 */
55
56/*
57 * src.c is caring...
58 *
59 * Gen1
60 *
61 * [mem] -> [SRU] -> [SSI]
62 * |--------|
63 *
64 * Gen2
65 *
66 * [mem] -> [SRC] -> [SSIU] -> [SSI]
67 * |-----------------|
68 */
69
70/*
71 * How to use SRC bypass mode for debugging
72 *
73 * SRC has bypass mode, and it is useful for debugging.
74 * In Gen2 case,
75 * SRCm_MODE controls whether SRC is used or not
76 * SSI_MODE0 controls whether SSIU which receives SRC data
77 * is used or not.
78 * Both SRCm_MODE/SSI_MODE0 settings are needed if you use SRC,
79 * but SRC bypass mode needs SSI_MODE0 only.
80 *
81 * This driver request
82 * struct rsnd_src_platform_info {
83 * u32 flags;
84 * u32 convert_rate;
85 * }
86 *
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.
90 *
91 * rsnd_src_convert_rate() indicates
92 * above convert_rate, and it controls
93 * whether SRC is used or not.
94 *
95 * ex) doesn't use SRC
96 * struct rsnd_src_platform_info info = {
97 * .flags = 0,
98 * .convert_rate = 0,
99 * };
100 *
101 * ex) uses SRC
102 * struct rsnd_src_platform_info info = {
103 * .flags = RSND_SRC_USE_HPBIF,
104 * .convert_rate = 48000,
105 * };
106 *
107 * ex) uses SRC bypass mode
108 * struct rsnd_src_platform_info info = {
109 * .flags = RSND_SRC_USE_HPBIF,
110 * .convert_rate = 0,
111 * };
112 *
113 */
114
115/*
116 * Gen1/Gen2 common functions
117 */
118int rsnd_src_ssi_mode_init(struct rsnd_mod *ssi_mod,
119 struct rsnd_dai *rdai,
120 struct rsnd_dai_stream *io)
121{
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);
126 int has_src = 0;
127
128 /*
129 * SSI_MODE0
130 */
131 if (info->dai_info) {
132 has_src = !!src_mod;
133 } else {
134 struct rsnd_src *src = rsnd_mod_to_src(src_mod);
135 has_src = rsnd_src_hpbif_is_enable(src);
136 }
137
138 rsnd_mod_bset(ssi_mod, SSI_MODE0, (1 << ssi_id),
139 has_src ? 0 : (1 << ssi_id));
140
141 /*
142 * SSI_MODE1
143 */
144 if (rsnd_ssi_is_pin_sharing(ssi_mod)) {
145 int shift = -1;
146 switch (ssi_id) {
147 case 1:
148 shift = 0;
149 break;
150 case 2:
151 shift = 2;
152 break;
153 case 4:
154 shift = 16;
155 break;
156 }
157
158 if (shift >= 0)
159 rsnd_mod_bset(ssi_mod, SSI_MODE1,
160 0x3 << shift,
161 rsnd_dai_is_clk_master(rdai) ?
162 0x2 << shift : 0x1 << shift);
163 }
164
165 return 0;
166}
167
168int rsnd_src_enable_ssi_irq(struct rsnd_mod *ssi_mod,
169 struct rsnd_dai *rdai,
170 struct rsnd_dai_stream *io)
171{
172 struct rsnd_priv *priv = rsnd_mod_to_priv(ssi_mod);
173
174 /* enable PIO interrupt if Gen2 */
175 if (rsnd_is_gen2(priv))
176 rsnd_mod_write(ssi_mod, INT_ENABLE, 0x0f000000);
177
178 return 0;
179}
180
181unsigned int rsnd_src_get_ssi_rate(struct rsnd_priv *priv,
182 struct rsnd_dai_stream *io,
183 struct snd_pcm_runtime *runtime)
184{
185 struct rsnd_src *src;
186 unsigned int rate;
187
188 src = rsnd_mod_to_src(rsnd_io_to_mod_src(io));
189
190 /*
191 * return convert rate if SRC is used,
192 * otherwise, return runtime->rate as usual
193 */
194 rate = rsnd_src_convert_rate(src);
195 if (!rate)
196 rate = runtime->rate;
197
198 return rate;
199}
200
201static int rsnd_src_set_convert_rate(struct rsnd_mod *mod,
202 struct rsnd_dai *rdai,
203 struct rsnd_dai_stream *io)
204{
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;
209 u32 fsrate = 0;
210
211 if (convert_rate)
212 fsrate = 0x0400000 / convert_rate * runtime->rate;
213
214 /* set/clear soft reset */
215 rsnd_mod_write(mod, SRC_SWRSR, 0);
216 rsnd_mod_write(mod, SRC_SWRSR, 1);
217
218 /*
219 * Initialize the operation of the SRC internal circuits
220 * see rsnd_src_start()
221 */
222 rsnd_mod_write(mod, SRC_SRCIR, 1);
223
224 /* Set channel number and output bit length */
225 switch (runtime->sample_bits) {
226 case 16:
227 adinr |= OTBL_16;
228 break;
229 case 32:
230 adinr |= OTBL_24;
231 break;
232 default:
233 return -EIO;
234 }
235 rsnd_mod_write(mod, SRC_ADINR, adinr);
236
237 /* Enable the initial value of IFS */
238 if (fsrate) {
239 rsnd_mod_write(mod, SRC_IFSCR, 1);
240
241 /* Set initial value of IFS */
242 rsnd_mod_write(mod, SRC_IFSVR, fsrate);
243 }
244
245 /* use DMA transfer */
246 rsnd_mod_write(mod, SRC_BUSIF_MODE, 1);
247
248 return 0;
249}
250
251static int rsnd_src_init(struct rsnd_mod *mod,
252 struct rsnd_dai *rdai,
253 struct rsnd_dai_stream *io)
254{
255 struct rsnd_src *src = rsnd_mod_to_src(mod);
256
257 clk_enable(src->clk);
258
259 return 0;
260}
261
262static int rsnd_src_quit(struct rsnd_mod *mod,
263 struct rsnd_dai *rdai,
264 struct rsnd_dai_stream *io)
265{
266 struct rsnd_src *src = rsnd_mod_to_src(mod);
267
268 clk_disable(src->clk);
269
270 return 0;
271}
272
273static int rsnd_src_start(struct rsnd_mod *mod,
274 struct rsnd_dai *rdai,
275 struct rsnd_dai_stream *io)
276{
277 struct rsnd_src *src = rsnd_mod_to_src(mod);
278
279 /*
280 * Cancel the initialization and operate the SRC function
281 * see rsnd_src_set_convert_rate()
282 */
283 rsnd_mod_write(mod, SRC_SRCIR, 0);
284
285 if (rsnd_src_convert_rate(src))
286 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 1);
287
288 return 0;
289}
290
291
292static int rsnd_src_stop(struct rsnd_mod *mod,
293 struct rsnd_dai *rdai,
294 struct rsnd_dai_stream *io)
295{
296 struct rsnd_src *src = rsnd_mod_to_src(mod);
297
298 if (rsnd_src_convert_rate(src))
299 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 0);
300
301 return 0;
302}
303
304static struct rsnd_mod_ops rsnd_src_non_ops = {
305 .name = "src (non)",
306};
307
308/*
309 * Gen1 functions
310 */
311static int rsnd_src_set_route_gen1(struct rsnd_mod *mod,
312 struct rsnd_dai *rdai,
313 struct rsnd_dai_stream *io)
314{
315 struct src_route_config {
316 u32 mask;
317 int shift;
318 } routes[] = {
319 { 0xF, 0, }, /* 0 */
320 { 0xF, 4, }, /* 1 */
321 { 0xF, 8, }, /* 2 */
322 { 0x7, 12, }, /* 3 */
323 { 0x7, 16, }, /* 4 */
324 { 0x7, 20, }, /* 5 */
325 { 0x7, 24, }, /* 6 */
326 { 0x3, 28, }, /* 7 */
327 { 0x3, 30, }, /* 8 */
328 };
329 u32 mask;
330 u32 val;
331 int id;
332
333 id = rsnd_mod_id(mod);
334 if (id < 0 || id >= ARRAY_SIZE(routes))
335 return -EIO;
336
337 /*
338 * SRC_ROUTE_SELECT
339 */
340 val = rsnd_dai_is_play(rdai, io) ? 0x1 : 0x2;
341 val = val << routes[id].shift;
342 mask = routes[id].mask << routes[id].shift;
343
344 rsnd_mod_bset(mod, SRC_ROUTE_SEL, mask, val);
345
346 return 0;
347}
348
349static int rsnd_src_set_convert_timing_gen1(struct rsnd_mod *mod,
350 struct rsnd_dai *rdai,
351 struct rsnd_dai_stream *io)
352{
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);
357 u32 mask;
358 u32 val;
359 int shift;
360 int id = rsnd_mod_id(mod);
361 int ret;
362
363 /*
364 * SRC_TIMING_SELECT
365 */
366 shift = (id % 4) * 8;
367 mask = 0x1F << shift;
368
369 /*
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)
374 */
375 ret = 0;
376 if (convert_rate) {
377 /* use ADG */
378 val = 0;
379 ret = rsnd_adg_set_convert_clk_gen1(priv, mod,
380 runtime->rate,
381 convert_rate);
382 } else if (8 == id) {
383 /* use SSI WS, but SRU8 is special */
384 val = id << shift;
385 } else {
386 /* use SSI WS */
387 val = (id + 1) << shift;
388 }
389
390 if (ret < 0)
391 return ret;
392
393 switch (id / 4) {
394 case 0:
395 rsnd_mod_bset(mod, SRC_TMG_SEL0, mask, val);
396 break;
397 case 1:
398 rsnd_mod_bset(mod, SRC_TMG_SEL1, mask, val);
399 break;
400 case 2:
401 rsnd_mod_bset(mod, SRC_TMG_SEL2, mask, val);
402 break;
403 }
404
405 return 0;
406}
407
408static int rsnd_src_set_convert_rate_gen1(struct rsnd_mod *mod,
409 struct rsnd_dai *rdai,
410 struct rsnd_dai_stream *io)
411{
412 int ret;
413
414 ret = rsnd_src_set_convert_rate(mod, rdai, io);
415 if (ret < 0)
416 return ret;
417
418 /* Select SRC mode (fixed value) */
419 rsnd_mod_write(mod, SRC_SRCCR, 0x00010110);
420
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);
424
425 /* no SRC_BFSSR settings, since SRC_SRCCR::BUFMD is 0 */
426
427 return 0;
428}
429
430static int rsnd_src_init_gen1(struct rsnd_mod *mod,
431 struct rsnd_dai *rdai,
432 struct rsnd_dai_stream *io)
433{
434 int ret;
435
436 ret = rsnd_src_init(mod, rdai, io);
437 if (ret < 0)
438 return ret;
439
440 ret = rsnd_src_set_route_gen1(mod, rdai, io);
441 if (ret < 0)
442 return ret;
443
444 ret = rsnd_src_set_convert_rate_gen1(mod, rdai, io);
445 if (ret < 0)
446 return ret;
447
448 ret = rsnd_src_set_convert_timing_gen1(mod, rdai, io);
449 if (ret < 0)
450 return ret;
451
452 return 0;
453}
454
455static int rsnd_src_start_gen1(struct rsnd_mod *mod,
456 struct rsnd_dai *rdai,
457 struct rsnd_dai_stream *io)
458{
459 int id = rsnd_mod_id(mod);
460
461 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), (1 << id));
462
463 return rsnd_src_start(mod, rdai, io);
464}
465
466static int rsnd_src_stop_gen1(struct rsnd_mod *mod,
467 struct rsnd_dai *rdai,
468 struct rsnd_dai_stream *io)
469{
470 int id = rsnd_mod_id(mod);
471
472 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), 0);
473
474 return rsnd_src_stop(mod, rdai, io);
475}
476
477static 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,
483};
484
485/*
486 * Gen2 functions
487 */
488static int rsnd_src_set_convert_rate_gen2(struct rsnd_mod *mod,
489 struct rsnd_dai *rdai,
490 struct rsnd_dai_stream *io)
491{
492 int ret;
493
494 ret = rsnd_src_set_convert_rate(mod, rdai, io);
495 if (ret < 0)
496 return ret;
497
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));
500
501 rsnd_mod_write(mod, SRC_SRCCR, 0x00011110);
502
503 rsnd_mod_write(mod, SRC_BSDSR, 0x01800000);
504 rsnd_mod_write(mod, SRC_BSISR, 0x00100060);
505
506 return 0;
507}
508
509static int rsnd_src_set_convert_timing_gen2(struct rsnd_mod *mod,
510 struct rsnd_dai *rdai,
511 struct rsnd_dai_stream *io)
512{
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);
516 int ret;
517
518 if (convert_rate)
519 ret = rsnd_adg_set_convert_clk_gen2(mod, rdai, io,
520 runtime->rate,
521 convert_rate);
522 else
523 ret = rsnd_adg_set_convert_timing_gen2(mod, rdai, io);
524
525 return ret;
526}
527
528static int rsnd_src_probe_gen2(struct rsnd_mod *mod,
529 struct rsnd_dai *rdai,
530 struct rsnd_dai_stream *io)
531{
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);
537 int ret;
538 int is_play;
539
540 if (info->dai_info)
541 is_play = rsnd_info_is_playback(priv, src);
542 else
543 is_play = rsnd_ssi_is_play(ssi);
544
545 ret = rsnd_dma_init(priv,
546 rsnd_mod_to_dma(mod),
547 is_play,
548 src->info->dma_id);
549 if (ret < 0)
550 dev_err(dev, "SRC DMA failed\n");
551
552 return ret;
553}
554
555static int rsnd_src_remove_gen2(struct rsnd_mod *mod,
556 struct rsnd_dai *rdai,
557 struct rsnd_dai_stream *io)
558{
559 rsnd_dma_quit(rsnd_mod_to_priv(mod), rsnd_mod_to_dma(mod));
560
561 return 0;
562}
563
564static int rsnd_src_init_gen2(struct rsnd_mod *mod,
565 struct rsnd_dai *rdai,
566 struct rsnd_dai_stream *io)
567{
568 int ret;
569
570 ret = rsnd_src_init(mod, rdai, io);
571 if (ret < 0)
572 return ret;
573
574 ret = rsnd_src_set_convert_rate_gen2(mod, rdai, io);
575 if (ret < 0)
576 return ret;
577
578 ret = rsnd_src_set_convert_timing_gen2(mod, rdai, io);
579 if (ret < 0)
580 return ret;
581
582 return 0;
583}
584
585static int rsnd_src_start_gen2(struct rsnd_mod *mod,
586 struct rsnd_dai *rdai,
587 struct rsnd_dai_stream *io)
588{
589 struct rsnd_src *src = rsnd_mod_to_src(mod);
590
591 rsnd_dma_start(rsnd_mod_to_dma(&src->mod));
592
593 rsnd_mod_write(mod, SSI_CTRL, 0x1);
594 rsnd_mod_write(mod, SRC_CTRL, 0x11);
595
596 return rsnd_src_start(mod, rdai, io);
597}
598
599static int rsnd_src_stop_gen2(struct rsnd_mod *mod,
600 struct rsnd_dai *rdai,
601 struct rsnd_dai_stream *io)
602{
603 struct rsnd_src *src = rsnd_mod_to_src(mod);
604
605 rsnd_mod_write(mod, SSI_CTRL, 0);
606 rsnd_mod_write(mod, SRC_CTRL, 0);
607
608 rsnd_dma_stop(rsnd_mod_to_dma(&src->mod));
609
610 return rsnd_src_stop(mod, rdai, io);
611}
612
613static 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,
621};
622
623struct rsnd_mod *rsnd_src_mod_get(struct rsnd_priv *priv, int id)
624{
625 if (WARN_ON(id < 0 || id >= rsnd_src_nr(priv)))
626 id = 0;
627
628 return &((struct rsnd_src *)(priv->src) + id)->mod;
629}
630
631int rsnd_src_probe(struct platform_device *pdev,
632 struct rsnd_priv *priv)
633{
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;
638 struct clk *clk;
639 char name[RSND_SRC_NAME_SIZE];
640 int i, nr;
641
642 /*
643 * init SRC
644 */
645 nr = info->src_info_nr;
646 if (!nr)
647 return 0;
648
649 src = devm_kzalloc(dev, sizeof(*src) * nr, GFP_KERNEL);
650 if (!src) {
651 dev_err(dev, "SRC allocate failed\n");
652 return -ENOMEM;
653 }
654
655 priv->src_nr = nr;
656 priv->src = src;
657
658 for_each_rsnd_src(src, priv, i) {
659 snprintf(name, RSND_SRC_NAME_SIZE, "src.%d", i);
660
661 clk = devm_clk_get(dev, name);
662 if (IS_ERR(clk)) {
663 snprintf(name, RSND_SRC_NAME_SIZE, "scu.%d", i);
664 clk = devm_clk_get(dev, name);
665 }
666
667 if (IS_ERR(clk))
668 return PTR_ERR(clk);
669
670 src->info = &info->src_info[i];
671 src->clk = clk;
672
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;
679 }
680
681 rsnd_mod_init(priv, &src->mod, ops, RSND_MOD_SRC, i);
682
683 dev_dbg(dev, "SRC%d probed\n", i);
684 }
685
686 return 0;
687}