]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - sound/soc/sh/rcar/src.c
ASoC: rsnd: add Multi channel support
[mirror_ubuntu-zesty-kernel.git] / sound / soc / sh / rcar / src.c
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
13 #define SRC_NAME "src"
14
15 /* SRCx_STATUS */
16 #define OUF_SRCO ((1 << 12) | (1 << 13))
17 #define OUF_SRCI ((1 << 9) | (1 << 8))
18
19 /* SCU_SYSTEM_STATUS0/1 */
20 #define OUF_SRC(id) ((1 << (id + 16)) | (1 << id))
21
22 struct rsnd_src {
23 struct rsnd_mod mod;
24 struct rsnd_mod *dma;
25 struct rsnd_kctrl_cfg_s sen; /* sync convert enable */
26 struct rsnd_kctrl_cfg_s sync; /* sync convert */
27 u32 convert_rate; /* sampling rate convert */
28 int err;
29 int irq;
30 };
31
32 #define RSND_SRC_NAME_SIZE 16
33
34 #define rsnd_src_get(priv, id) ((struct rsnd_src *)(priv->src) + id)
35 #define rsnd_src_to_dma(src) ((src)->dma)
36 #define rsnd_src_nr(priv) ((priv)->src_nr)
37 #define rsnd_enable_sync_convert(src) ((src)->sen.val)
38
39 #define rsnd_mod_to_src(_mod) \
40 container_of((_mod), struct rsnd_src, mod)
41
42 #define for_each_rsnd_src(pos, priv, i) \
43 for ((i) = 0; \
44 ((i) < rsnd_src_nr(priv)) && \
45 ((pos) = (struct rsnd_src *)(priv)->src + i); \
46 i++)
47
48
49 /*
50 * image of SRC (Sampling Rate Converter)
51 *
52 * 96kHz <-> +-----+ 48kHz +-----+ 48kHz +-------+
53 * 48kHz <-> | SRC | <------> | SSI | <-----> | codec |
54 * 44.1kHz <-> +-----+ +-----+ +-------+
55 * ...
56 *
57 */
58
59 /*
60 * src.c is caring...
61 *
62 * Gen1
63 *
64 * [mem] -> [SRU] -> [SSI]
65 * |--------|
66 *
67 * Gen2
68 *
69 * [mem] -> [SRC] -> [SSIU] -> [SSI]
70 * |-----------------|
71 */
72
73 static void rsnd_src_activation(struct rsnd_mod *mod)
74 {
75 rsnd_mod_write(mod, SRC_SWRSR, 0);
76 rsnd_mod_write(mod, SRC_SWRSR, 1);
77 }
78
79 static void rsnd_src_halt(struct rsnd_mod *mod)
80 {
81 rsnd_mod_write(mod, SRC_SRCIR, 1);
82 rsnd_mod_write(mod, SRC_SWRSR, 0);
83 }
84
85 static struct dma_chan *rsnd_src_dma_req(struct rsnd_dai_stream *io,
86 struct rsnd_mod *mod)
87 {
88 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
89 int is_play = rsnd_io_is_play(io);
90
91 return rsnd_dma_request_channel(rsnd_src_of_node(priv),
92 mod,
93 is_play ? "rx" : "tx");
94 }
95
96 static u32 rsnd_src_convert_rate(struct rsnd_dai_stream *io,
97 struct rsnd_src *src)
98 {
99 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
100 u32 convert_rate;
101
102 if (!runtime)
103 return 0;
104
105 if (!rsnd_enable_sync_convert(src))
106 return src->convert_rate;
107
108 convert_rate = src->sync.val;
109
110 if (!convert_rate)
111 convert_rate = src->convert_rate;
112
113 if (!convert_rate)
114 convert_rate = runtime->rate;
115
116 return convert_rate;
117 }
118
119 unsigned int rsnd_src_get_ssi_rate(struct rsnd_priv *priv,
120 struct rsnd_dai_stream *io,
121 struct snd_pcm_runtime *runtime)
122 {
123 struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
124 struct rsnd_src *src;
125 unsigned int rate = 0;
126
127 if (src_mod) {
128 src = rsnd_mod_to_src(src_mod);
129
130 /*
131 * return convert rate if SRC is used,
132 * otherwise, return runtime->rate as usual
133 */
134 rate = rsnd_src_convert_rate(io, src);
135 }
136
137 if (!rate)
138 rate = runtime->rate;
139
140 return rate;
141 }
142
143 static int rsnd_src_hw_params(struct rsnd_mod *mod,
144 struct rsnd_dai_stream *io,
145 struct snd_pcm_substream *substream,
146 struct snd_pcm_hw_params *fe_params)
147 {
148 struct rsnd_src *src = rsnd_mod_to_src(mod);
149 struct snd_soc_pcm_runtime *fe = substream->private_data;
150
151 /*
152 * SRC assumes that it is used under DPCM if user want to use
153 * sampling rate convert. Then, SRC should be FE.
154 * And then, this function will be called *after* BE settings.
155 * this means, each BE already has fixuped hw_params.
156 * see
157 * dpcm_fe_dai_hw_params()
158 * dpcm_be_dai_hw_params()
159 */
160 if (fe->dai_link->dynamic) {
161 int stream = substream->stream;
162 struct snd_soc_dpcm *dpcm;
163 struct snd_pcm_hw_params *be_params;
164
165 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
166 be_params = &dpcm->hw_params;
167
168 if (params_rate(fe_params) != params_rate(be_params))
169 src->convert_rate = params_rate(be_params);
170 }
171 }
172
173 return 0;
174 }
175
176 static void rsnd_src_set_convert_rate(struct rsnd_dai_stream *io,
177 struct rsnd_mod *mod)
178 {
179 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
180 struct device *dev = rsnd_priv_to_dev(priv);
181 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
182 struct rsnd_src *src = rsnd_mod_to_src(mod);
183 u32 convert_rate = rsnd_src_convert_rate(io, src);
184 u32 ifscr, fsrate, adinr;
185 u32 cr, route;
186 u32 bsdsr, bsisr;
187 uint ratio;
188
189 if (!runtime)
190 return;
191
192 /* 6 - 1/6 are very enough ratio for SRC_BSDSR */
193 if (!convert_rate)
194 ratio = 0;
195 else if (convert_rate > runtime->rate)
196 ratio = 100 * convert_rate / runtime->rate;
197 else
198 ratio = 100 * runtime->rate / convert_rate;
199
200 if (ratio > 600) {
201 dev_err(dev, "FSO/FSI ratio error\n");
202 return;
203 }
204
205 /*
206 * SRC_ADINR
207 */
208 adinr = rsnd_get_adinr_bit(mod, io) |
209 rsnd_get_adinr_chan(mod, io);
210
211 /*
212 * SRC_IFSCR / SRC_IFSVR
213 */
214 ifscr = 0;
215 fsrate = 0;
216 if (convert_rate) {
217 ifscr = 1;
218 fsrate = 0x0400000 / convert_rate * runtime->rate;
219 }
220
221 /*
222 * SRC_SRCCR / SRC_ROUTE_MODE0
223 */
224 cr = 0x00011110;
225 route = 0x0;
226 if (convert_rate) {
227 route = 0x1;
228
229 if (rsnd_enable_sync_convert(src)) {
230 cr |= 0x1;
231 route |= rsnd_io_is_play(io) ?
232 (0x1 << 24) : (0x1 << 25);
233 }
234 }
235
236 /*
237 * SRC_BSDSR / SRC_BSISR
238 */
239 switch (rsnd_mod_id(mod)) {
240 case 5:
241 case 6:
242 case 7:
243 case 8:
244 bsdsr = 0x02400000; /* 6 - 1/6 */
245 bsisr = 0x00100060; /* 6 - 1/6 */
246 break;
247 default:
248 bsdsr = 0x01800000; /* 6 - 1/6 */
249 bsisr = 0x00100060 ;/* 6 - 1/6 */
250 break;
251 }
252
253 rsnd_mod_write(mod, SRC_SRCIR, 1); /* initialize */
254 rsnd_mod_write(mod, SRC_ADINR, adinr);
255 rsnd_mod_write(mod, SRC_IFSCR, ifscr);
256 rsnd_mod_write(mod, SRC_IFSVR, fsrate);
257 rsnd_mod_write(mod, SRC_SRCCR, cr);
258 rsnd_mod_write(mod, SRC_BSDSR, bsdsr);
259 rsnd_mod_write(mod, SRC_BSISR, bsisr);
260 rsnd_mod_write(mod, SRC_SRCIR, 0); /* cancel initialize */
261
262 rsnd_mod_write(mod, SRC_ROUTE_MODE0, route);
263 rsnd_mod_write(mod, SRC_I_BUSIF_MODE, 1);
264 rsnd_mod_write(mod, SRC_O_BUSIF_MODE, 1);
265 rsnd_mod_write(mod, SRC_BUSIF_DALIGN, rsnd_get_dalign(mod, io));
266
267 if (convert_rate)
268 rsnd_adg_set_convert_clk_gen2(mod, io,
269 runtime->rate,
270 convert_rate);
271 else
272 rsnd_adg_set_convert_timing_gen2(mod, io);
273 }
274
275 #define rsnd_src_irq_enable(mod) rsnd_src_irq_ctrol(mod, 1)
276 #define rsnd_src_irq_disable(mod) rsnd_src_irq_ctrol(mod, 0)
277 static void rsnd_src_irq_ctrol(struct rsnd_mod *mod, int enable)
278 {
279 struct rsnd_src *src = rsnd_mod_to_src(mod);
280 u32 sys_int_val, int_val, sys_int_mask;
281 int irq = src->irq;
282 int id = rsnd_mod_id(mod);
283
284 sys_int_val =
285 sys_int_mask = OUF_SRC(id);
286 int_val = 0x3300;
287
288 /*
289 * IRQ is not supported on non-DT
290 * see
291 * rsnd_src_probe_()
292 */
293 if ((irq <= 0) || !enable) {
294 sys_int_val = 0;
295 int_val = 0;
296 }
297
298 /*
299 * WORKAROUND
300 *
301 * ignore over flow error when rsnd_enable_sync_convert()
302 */
303 if (rsnd_enable_sync_convert(src))
304 sys_int_val = sys_int_val & 0xffff;
305
306 rsnd_mod_write(mod, SRC_INT_ENABLE0, int_val);
307 rsnd_mod_bset(mod, SCU_SYS_INT_EN0, sys_int_mask, sys_int_val);
308 rsnd_mod_bset(mod, SCU_SYS_INT_EN1, sys_int_mask, sys_int_val);
309 }
310
311 static void rsnd_src_status_clear(struct rsnd_mod *mod)
312 {
313 u32 val = OUF_SRC(rsnd_mod_id(mod));
314
315 rsnd_mod_bset(mod, SCU_SYS_STATUS0, val, val);
316 rsnd_mod_bset(mod, SCU_SYS_STATUS1, val, val);
317 }
318
319 static bool rsnd_src_record_error(struct rsnd_mod *mod)
320 {
321 struct rsnd_src *src = rsnd_mod_to_src(mod);
322 u32 val0, val1;
323 bool ret = false;
324
325 val0 = val1 = OUF_SRC(rsnd_mod_id(mod));
326
327 /*
328 * WORKAROUND
329 *
330 * ignore over flow error when rsnd_enable_sync_convert()
331 */
332 if (rsnd_enable_sync_convert(src))
333 val0 = val0 & 0xffff;
334
335 if ((rsnd_mod_read(mod, SCU_SYS_STATUS0) & val0) ||
336 (rsnd_mod_read(mod, SCU_SYS_STATUS1) & val1)) {
337 struct rsnd_src *src = rsnd_mod_to_src(mod);
338
339 src->err++;
340 ret = true;
341 }
342
343 return ret;
344 }
345
346 static int rsnd_src_start(struct rsnd_mod *mod,
347 struct rsnd_dai_stream *io,
348 struct rsnd_priv *priv)
349 {
350 struct rsnd_src *src = rsnd_mod_to_src(mod);
351 u32 val;
352
353 /*
354 * WORKAROUND
355 *
356 * Enable SRC output if you want to use sync convert together with DVC
357 */
358 val = (rsnd_io_to_mod_dvc(io) && !rsnd_enable_sync_convert(src)) ?
359 0x01 : 0x11;
360
361 rsnd_mod_write(mod, SRC_CTRL, val);
362
363 return 0;
364 }
365
366 static int rsnd_src_stop(struct rsnd_mod *mod,
367 struct rsnd_dai_stream *io,
368 struct rsnd_priv *priv)
369 {
370 /*
371 * stop SRC output only
372 * see rsnd_src_quit
373 */
374 rsnd_mod_write(mod, SRC_CTRL, 0x01);
375
376 return 0;
377 }
378
379 static int rsnd_src_init(struct rsnd_mod *mod,
380 struct rsnd_dai_stream *io,
381 struct rsnd_priv *priv)
382 {
383 struct rsnd_src *src = rsnd_mod_to_src(mod);
384
385 rsnd_mod_power_on(mod);
386
387 rsnd_src_activation(mod);
388
389 rsnd_src_set_convert_rate(io, mod);
390
391 rsnd_src_status_clear(mod);
392
393 rsnd_src_irq_enable(mod);
394
395 src->err = 0;
396
397 /* reset sync convert_rate */
398 src->sync.val = 0;
399
400 return 0;
401 }
402
403 static int rsnd_src_quit(struct rsnd_mod *mod,
404 struct rsnd_dai_stream *io,
405 struct rsnd_priv *priv)
406 {
407 struct rsnd_src *src = rsnd_mod_to_src(mod);
408 struct device *dev = rsnd_priv_to_dev(priv);
409
410 rsnd_src_irq_disable(mod);
411
412 /* stop both out/in */
413 rsnd_mod_write(mod, SRC_CTRL, 0);
414
415 rsnd_src_halt(mod);
416
417 rsnd_mod_power_off(mod);
418
419 if (src->err)
420 dev_warn(dev, "%s[%d] under/over flow err = %d\n",
421 rsnd_mod_name(mod), rsnd_mod_id(mod), src->err);
422
423 src->convert_rate = 0;
424
425 /* reset sync convert_rate */
426 src->sync.val = 0;
427
428 return 0;
429 }
430
431 static void __rsnd_src_interrupt(struct rsnd_mod *mod,
432 struct rsnd_dai_stream *io)
433 {
434 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
435 struct rsnd_src *src = rsnd_mod_to_src(mod);
436 struct device *dev = rsnd_priv_to_dev(priv);
437
438 spin_lock(&priv->lock);
439
440 /* ignore all cases if not working */
441 if (!rsnd_io_is_working(io))
442 goto rsnd_src_interrupt_out;
443
444 if (rsnd_src_record_error(mod)) {
445
446 dev_dbg(dev, "%s[%d] restart\n",
447 rsnd_mod_name(mod), rsnd_mod_id(mod));
448
449 rsnd_src_stop(mod, io, priv);
450 rsnd_src_start(mod, io, priv);
451 }
452
453 if (src->err > 1024) {
454 rsnd_src_irq_disable(mod);
455
456 dev_warn(dev, "no more %s[%d] restart\n",
457 rsnd_mod_name(mod), rsnd_mod_id(mod));
458 }
459
460 rsnd_src_status_clear(mod);
461 rsnd_src_interrupt_out:
462
463 spin_unlock(&priv->lock);
464 }
465
466 static irqreturn_t rsnd_src_interrupt(int irq, void *data)
467 {
468 struct rsnd_mod *mod = data;
469
470 rsnd_mod_interrupt(mod, __rsnd_src_interrupt);
471
472 return IRQ_HANDLED;
473 }
474
475 static int rsnd_src_probe_(struct rsnd_mod *mod,
476 struct rsnd_dai_stream *io,
477 struct rsnd_priv *priv)
478 {
479 struct rsnd_src *src = rsnd_mod_to_src(mod);
480 struct device *dev = rsnd_priv_to_dev(priv);
481 int irq = src->irq;
482 int ret;
483
484 if (irq > 0) {
485 /*
486 * IRQ is not supported on non-DT
487 * see
488 * rsnd_src_irq_enable()
489 */
490 ret = devm_request_irq(dev, irq,
491 rsnd_src_interrupt,
492 IRQF_SHARED,
493 dev_name(dev), mod);
494 if (ret)
495 return ret;
496 }
497
498 src->dma = rsnd_dma_attach(io, mod, 0);
499 if (IS_ERR(src->dma))
500 return PTR_ERR(src->dma);
501
502 return ret;
503 }
504
505 static int rsnd_src_pcm_new(struct rsnd_mod *mod,
506 struct rsnd_dai_stream *io,
507 struct snd_soc_pcm_runtime *rtd)
508 {
509 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
510 struct rsnd_src *src = rsnd_mod_to_src(mod);
511 int ret;
512
513 /*
514 * enable SRC sync convert if possible
515 */
516
517 /*
518 * SRC sync convert needs clock master
519 */
520 if (!rsnd_rdai_is_clk_master(rdai))
521 return 0;
522
523 /*
524 * enable sync convert
525 */
526 ret = rsnd_kctrl_new_s(mod, io, rtd,
527 rsnd_io_is_play(io) ?
528 "SRC Out Rate Switch" :
529 "SRC In Rate Switch",
530 rsnd_src_set_convert_rate,
531 &src->sen, 1);
532 if (ret < 0)
533 return ret;
534
535 ret = rsnd_kctrl_new_s(mod, io, rtd,
536 rsnd_io_is_play(io) ?
537 "SRC Out Rate" :
538 "SRC In Rate",
539 rsnd_src_set_convert_rate,
540 &src->sync, 192000);
541
542 return ret;
543 }
544
545 static struct rsnd_mod_ops rsnd_src_ops = {
546 .name = SRC_NAME,
547 .dma_req = rsnd_src_dma_req,
548 .probe = rsnd_src_probe_,
549 .init = rsnd_src_init,
550 .quit = rsnd_src_quit,
551 .start = rsnd_src_start,
552 .stop = rsnd_src_stop,
553 .hw_params = rsnd_src_hw_params,
554 .pcm_new = rsnd_src_pcm_new,
555 };
556
557 struct rsnd_mod *rsnd_src_mod_get(struct rsnd_priv *priv, int id)
558 {
559 if (WARN_ON(id < 0 || id >= rsnd_src_nr(priv)))
560 id = 0;
561
562 return rsnd_mod_get(rsnd_src_get(priv, id));
563 }
564
565 int rsnd_src_probe(struct rsnd_priv *priv)
566 {
567 struct device_node *node;
568 struct device_node *np;
569 struct device *dev = rsnd_priv_to_dev(priv);
570 struct rsnd_src *src;
571 struct clk *clk;
572 char name[RSND_SRC_NAME_SIZE];
573 int i, nr, ret;
574
575 /* This driver doesn't support Gen1 at this point */
576 if (rsnd_is_gen1(priv))
577 return 0;
578
579 node = rsnd_src_of_node(priv);
580 if (!node)
581 return 0; /* not used is not error */
582
583 nr = of_get_child_count(node);
584 if (!nr) {
585 ret = -EINVAL;
586 goto rsnd_src_probe_done;
587 }
588
589 src = devm_kzalloc(dev, sizeof(*src) * nr, GFP_KERNEL);
590 if (!src) {
591 ret = -ENOMEM;
592 goto rsnd_src_probe_done;
593 }
594
595 priv->src_nr = nr;
596 priv->src = src;
597
598 i = 0;
599 for_each_child_of_node(node, np) {
600 src = rsnd_src_get(priv, i);
601
602 snprintf(name, RSND_SRC_NAME_SIZE, "%s.%d",
603 SRC_NAME, i);
604
605 src->irq = irq_of_parse_and_map(np, 0);
606 if (!src->irq) {
607 ret = -EINVAL;
608 goto rsnd_src_probe_done;
609 }
610
611 clk = devm_clk_get(dev, name);
612 if (IS_ERR(clk)) {
613 ret = PTR_ERR(clk);
614 goto rsnd_src_probe_done;
615 }
616
617 ret = rsnd_mod_init(priv, rsnd_mod_get(src),
618 &rsnd_src_ops, clk, RSND_MOD_SRC, i);
619 if (ret)
620 goto rsnd_src_probe_done;
621
622 i++;
623 }
624
625 ret = 0;
626
627 rsnd_src_probe_done:
628 of_node_put(node);
629
630 return ret;
631 }
632
633 void rsnd_src_remove(struct rsnd_priv *priv)
634 {
635 struct rsnd_src *src;
636 int i;
637
638 for_each_rsnd_src(src, priv, i) {
639 rsnd_mod_quit(rsnd_mod_get(src));
640 }
641 }