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