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