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