ASoC: rsnd: add common interrupt handler for SSI/SRC/DMA
[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
KM
22struct rsnd_src {
23 struct rsnd_src_platform_info *info; /* rcar_snd.h */
07539c1d 24 struct rsnd_mod mod;
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 */
cfcefe01 28 int err;
07539c1d
KM
29};
30
ba9c949f 31#define RSND_SRC_NAME_SIZE 16
374a5281 32
43cb6954 33#define rsnd_enable_sync_convert(src) ((src)->sen.val)
82e76ed3
KM
34#define rsnd_src_of_node(priv) \
35 of_get_child_by_name(rsnd_priv_to_dev(priv)->of_node, "rcar_sound,src")
36
ba9c949f
KM
37#define rsnd_mod_to_src(_mod) \
38 container_of((_mod), struct rsnd_src, mod)
39cf3c40 39
ba9c949f 40#define for_each_rsnd_src(pos, priv, i) \
39cf3c40 41 for ((i) = 0; \
ba9c949f
KM
42 ((i) < rsnd_src_nr(priv)) && \
43 ((pos) = (struct rsnd_src *)(priv)->src + i); \
39cf3c40
KM
44 i++)
45
46
ef749400
KM
47/*
48 * image of SRC (Sampling Rate Converter)
49 *
50 * 96kHz <-> +-----+ 48kHz +-----+ 48kHz +-------+
51 * 48kHz <-> | SRC | <------> | SSI | <-----> | codec |
52 * 44.1kHz <-> +-----+ +-----+ +-------+
53 * ...
54 *
55 */
374a5281 56
c926b746 57/*
ba9c949f 58 * src.c is caring...
c926b746
KM
59 *
60 * Gen1
61 *
62 * [mem] -> [SRU] -> [SSI]
63 * |--------|
64 *
65 * Gen2
66 *
ba9c949f 67 * [mem] -> [SRC] -> [SSIU] -> [SSI]
c926b746
KM
68 * |-----------------|
69 */
70
41c6221c
KM
71/*
72 * How to use SRC bypass mode for debugging
73 *
74 * SRC has bypass mode, and it is useful for debugging.
75 * In Gen2 case,
76 * SRCm_MODE controls whether SRC is used or not
77 * SSI_MODE0 controls whether SSIU which receives SRC data
78 * is used or not.
79 * Both SRCm_MODE/SSI_MODE0 settings are needed if you use SRC,
80 * but SRC bypass mode needs SSI_MODE0 only.
81 *
82 * This driver request
ba9c949f 83 * struct rsnd_src_platform_info {
41c6221c 84 * u32 convert_rate;
29e69fd2 85 * int dma_id;
41c6221c
KM
86 * }
87 *
ba9c949f 88 * rsnd_src_convert_rate() indicates
41c6221c
KM
89 * above convert_rate, and it controls
90 * whether SRC is used or not.
91 *
92 * ex) doesn't use SRC
29e69fd2
KM
93 * static struct rsnd_dai_platform_info rsnd_dai = {
94 * .playback = { .ssi = &rsnd_ssi[0], },
41c6221c
KM
95 * };
96 *
97 * ex) uses SRC
29e69fd2
KM
98 * static struct rsnd_src_platform_info rsnd_src[] = {
99 * RSND_SCU(48000, 0),
100 * ...
101 * };
102 * static struct rsnd_dai_platform_info rsnd_dai = {
103 * .playback = { .ssi = &rsnd_ssi[0], .src = &rsnd_src[0] },
41c6221c
KM
104 * };
105 *
106 * ex) uses SRC bypass mode
29e69fd2
KM
107 * static struct rsnd_src_platform_info rsnd_src[] = {
108 * RSND_SCU(0, 0),
109 * ...
110 * };
111 * static struct rsnd_dai_platform_info rsnd_dai = {
112 * .playback = { .ssi = &rsnd_ssi[0], .src = &rsnd_src[0] },
41c6221c
KM
113 * };
114 *
115 */
116
1b7b08ef
KM
117/*
118 * Gen1/Gen2 common functions
119 */
72adc61f
KM
120static struct dma_chan *rsnd_src_dma_req(struct rsnd_mod *mod)
121{
122 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
123 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
124 int is_play = rsnd_io_is_play(io);
125
126 return rsnd_dma_request_channel(rsnd_src_of_node(priv),
127 mod,
128 is_play ? "rx" : "tx");
129}
130
d9288d0b 131int rsnd_src_ssiu_start(struct rsnd_mod *ssi_mod,
d9288d0b 132 int use_busif)
7b5ce975 133{
1cc71959 134 struct rsnd_dai_stream *io = rsnd_mod_to_io(ssi_mod);
f708d944 135 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
1cc71959 136 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
374e5426 137 int ssi_id = rsnd_mod_id(ssi_mod);
7b5ce975
KM
138
139 /*
140 * SSI_MODE0
141 */
221bf523 142 rsnd_mod_bset(ssi_mod, SSI_MODE0, (1 << ssi_id),
d9288d0b 143 !use_busif << ssi_id);
7b5ce975
KM
144
145 /*
146 * SSI_MODE1
147 */
374e5426 148 if (rsnd_ssi_is_pin_sharing(ssi_mod)) {
7b5ce975 149 int shift = -1;
374e5426 150 switch (ssi_id) {
7b5ce975
KM
151 case 1:
152 shift = 0;
153 break;
154 case 2:
155 shift = 2;
156 break;
157 case 4:
158 shift = 16;
159 break;
160 }
161
162 if (shift >= 0)
221bf523 163 rsnd_mod_bset(ssi_mod, SSI_MODE1,
7b5ce975 164 0x3 << shift,
3ed6448c 165 rsnd_rdai_is_clk_master(rdai) ?
7b5ce975
KM
166 0x2 << shift : 0x1 << shift);
167 }
168
d9288d0b
KM
169 /*
170 * DMA settings for SSIU
171 */
172 if (use_busif) {
1cc71959
KM
173 u32 val = 0x76543210;
174 u32 mask = ~0;
175
d9288d0b
KM
176 rsnd_mod_write(ssi_mod, SSI_BUSIF_ADINR,
177 rsnd_get_adinr(ssi_mod));
178 rsnd_mod_write(ssi_mod, SSI_BUSIF_MODE, 1);
179 rsnd_mod_write(ssi_mod, SSI_CTRL, 0x1);
1cc71959
KM
180
181 mask <<= runtime->channels * 4;
182 val = val & mask;
183
184 switch (runtime->sample_bits) {
185 case 16:
186 val |= 0x67452301 & ~mask;
187 break;
188 case 32:
189 val |= 0x76543210 & ~mask;
190 break;
191 }
192 rsnd_mod_write(ssi_mod, BUSIF_DALIGN, val);
193
d9288d0b
KM
194 }
195
196 return 0;
197}
198
f708d944 199int rsnd_src_ssiu_stop(struct rsnd_mod *ssi_mod)
d9288d0b
KM
200{
201 /*
202 * DMA settings for SSIU
203 */
660cdce2 204 rsnd_mod_write(ssi_mod, SSI_CTRL, 0);
d9288d0b 205
7b5ce975
KM
206 return 0;
207}
208
f708d944 209int rsnd_src_ssi_irq_enable(struct rsnd_mod *ssi_mod)
b8cc41e9
KM
210{
211 struct rsnd_priv *priv = rsnd_mod_to_priv(ssi_mod);
212
c17dba8b
KM
213 if (rsnd_is_gen1(priv))
214 return 0;
215
216 /* enable SSI interrupt if Gen2 */
217 if (rsnd_ssi_is_dma_mode(ssi_mod))
218 rsnd_mod_write(ssi_mod, INT_ENABLE, 0x0e000000);
219 else
b8cc41e9
KM
220 rsnd_mod_write(ssi_mod, INT_ENABLE, 0x0f000000);
221
222 return 0;
223}
224
f708d944 225int rsnd_src_ssi_irq_disable(struct rsnd_mod *ssi_mod)
c17dba8b
KM
226{
227 struct rsnd_priv *priv = rsnd_mod_to_priv(ssi_mod);
228
229 if (rsnd_is_gen1(priv))
230 return 0;
231
232 /* disable SSI interrupt if Gen2 */
233 rsnd_mod_write(ssi_mod, INT_ENABLE, 0x00000000);
234
235 return 0;
236}
237
43cb6954
KM
238static u32 rsnd_src_convert_rate(struct rsnd_src *src)
239{
240 struct rsnd_mod *mod = &src->mod;
241 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
242 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
243 u32 convert_rate;
244
245 if (!runtime)
246 return 0;
247
248 if (!rsnd_enable_sync_convert(src))
249 return src->convert_rate;
250
251 convert_rate = src->sync.val;
252
253 if (!convert_rate)
254 convert_rate = src->convert_rate;
255
256 if (!convert_rate)
257 convert_rate = runtime->rate;
258
259 return convert_rate;
260}
261
ba9c949f 262unsigned int rsnd_src_get_ssi_rate(struct rsnd_priv *priv,
374e5426 263 struct rsnd_dai_stream *io,
1b7b08ef
KM
264 struct snd_pcm_runtime *runtime)
265{
b1eac430 266 struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
ba9c949f 267 struct rsnd_src *src;
b1eac430 268 unsigned int rate = 0;
1b7b08ef 269
b1eac430
KM
270 if (src_mod) {
271 src = rsnd_mod_to_src(src_mod);
272
273 /*
274 * return convert rate if SRC is used,
275 * otherwise, return runtime->rate as usual
276 */
277 rate = rsnd_src_convert_rate(src);
278 }
1b7b08ef 279
1b7b08ef
KM
280 if (!rate)
281 rate = runtime->rate;
282
283 return rate;
284}
285
f708d944 286static int rsnd_src_set_convert_rate(struct rsnd_mod *mod)
1b7b08ef 287{
b42fccf6 288 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
1b7b08ef 289 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
ba9c949f
KM
290 struct rsnd_src *src = rsnd_mod_to_src(mod);
291 u32 convert_rate = rsnd_src_convert_rate(src);
1b7b08ef
KM
292 u32 fsrate = 0;
293
294 if (convert_rate)
295 fsrate = 0x0400000 / convert_rate * runtime->rate;
296
297 /* set/clear soft reset */
298 rsnd_mod_write(mod, SRC_SWRSR, 0);
299 rsnd_mod_write(mod, SRC_SWRSR, 1);
300
1b7b08ef 301 /* Set channel number and output bit length */
d7bdbc5d 302 rsnd_mod_write(mod, SRC_ADINR, rsnd_get_adinr(mod));
1b7b08ef
KM
303
304 /* Enable the initial value of IFS */
305 if (fsrate) {
306 rsnd_mod_write(mod, SRC_IFSCR, 1);
307
308 /* Set initial value of IFS */
309 rsnd_mod_write(mod, SRC_IFSVR, fsrate);
310 }
311
312 /* use DMA transfer */
313 rsnd_mod_write(mod, SRC_BUSIF_MODE, 1);
314
315 return 0;
316}
317
3b7843ff 318static int rsnd_src_hw_params(struct rsnd_mod *mod,
2c0fac19 319 struct rsnd_dai_stream *io,
3b7843ff
KM
320 struct snd_pcm_substream *substream,
321 struct snd_pcm_hw_params *fe_params)
322{
323 struct rsnd_src *src = rsnd_mod_to_src(mod);
324 struct snd_soc_pcm_runtime *fe = substream->private_data;
325
326 /* default value (mainly for non-DT) */
327 src->convert_rate = src->info->convert_rate;
328
329 /*
330 * SRC assumes that it is used under DPCM if user want to use
331 * sampling rate convert. Then, SRC should be FE.
332 * And then, this function will be called *after* BE settings.
333 * this means, each BE already has fixuped hw_params.
334 * see
335 * dpcm_fe_dai_hw_params()
336 * dpcm_be_dai_hw_params()
337 */
338 if (fe->dai_link->dynamic) {
339 int stream = substream->stream;
340 struct snd_soc_dpcm *dpcm;
341 struct snd_pcm_hw_params *be_params;
342
343 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
344 be_params = &dpcm->hw_params;
345
346 if (params_rate(fe_params) != params_rate(be_params))
347 src->convert_rate = params_rate(be_params);
348 }
349 }
350
351 return 0;
352}
353
354static int rsnd_src_init(struct rsnd_mod *mod,
355 struct rsnd_priv *priv)
1b7b08ef 356{
ba9c949f 357 struct rsnd_src *src = rsnd_mod_to_src(mod);
1b7b08ef 358
85642952 359 rsnd_mod_hw_start(mod);
1b7b08ef 360
cfcefe01
KM
361 src->err = 0;
362
43cb6954
KM
363 /* reset sync convert_rate */
364 src->sync.val = 0;
365
603cefa5
KM
366 /*
367 * Initialize the operation of the SRC internal circuits
368 * see rsnd_src_start()
369 */
370 rsnd_mod_write(mod, SRC_SRCIR, 1);
371
1b7b08ef
KM
372 return 0;
373}
374
ba9c949f 375static int rsnd_src_quit(struct rsnd_mod *mod,
2c0fac19 376 struct rsnd_dai_stream *io,
690602fc 377 struct rsnd_priv *priv)
1b7b08ef 378{
ba9c949f 379 struct rsnd_src *src = rsnd_mod_to_src(mod);
cfcefe01 380 struct device *dev = rsnd_priv_to_dev(priv);
1b7b08ef 381
85642952 382 rsnd_mod_hw_stop(mod);
1b7b08ef 383
cfcefe01 384 if (src->err)
337b0b4c
KM
385 dev_warn(dev, "%s[%d] under/over flow err = %d\n",
386 rsnd_mod_name(mod), rsnd_mod_id(mod), src->err);
cfcefe01 387
3b7843ff
KM
388 src->convert_rate = 0;
389
43cb6954
KM
390 /* reset sync convert_rate */
391 src->sync.val = 0;
392
1b7b08ef
KM
393 return 0;
394}
395
f0ef0cb8 396static int rsnd_src_start(struct rsnd_mod *mod)
1b7b08ef 397{
1b7b08ef
KM
398 /*
399 * Cancel the initialization and operate the SRC function
603cefa5 400 * see rsnd_src_init()
1b7b08ef
KM
401 */
402 rsnd_mod_write(mod, SRC_SRCIR, 0);
403
1b7b08ef
KM
404 return 0;
405}
406
f0ef0cb8 407static int rsnd_src_stop(struct rsnd_mod *mod)
1b7b08ef 408{
933cc8cb 409 /* nothing to do */
1b7b08ef
KM
410 return 0;
411}
412
1b7b08ef
KM
413/*
414 * Gen1 functions
415 */
f708d944 416static int rsnd_src_set_route_gen1(struct rsnd_mod *mod)
374a5281 417{
b42fccf6 418 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
ba9c949f 419 struct src_route_config {
374a5281
KM
420 u32 mask;
421 int shift;
422 } routes[] = {
423 { 0xF, 0, }, /* 0 */
424 { 0xF, 4, }, /* 1 */
425 { 0xF, 8, }, /* 2 */
426 { 0x7, 12, }, /* 3 */
427 { 0x7, 16, }, /* 4 */
428 { 0x7, 20, }, /* 5 */
429 { 0x7, 24, }, /* 6 */
430 { 0x3, 28, }, /* 7 */
431 { 0x3, 30, }, /* 8 */
432 };
374a5281
KM
433 u32 mask;
434 u32 val;
374a5281
KM
435 int id;
436
374a5281 437 id = rsnd_mod_id(mod);
b5f3d7af 438 if (id < 0 || id >= ARRAY_SIZE(routes))
374a5281
KM
439 return -EIO;
440
441 /*
442 * SRC_ROUTE_SELECT
443 */
985a4f6e 444 val = rsnd_io_is_play(io) ? 0x1 : 0x2;
374a5281
KM
445 val = val << routes[id].shift;
446 mask = routes[id].mask << routes[id].shift;
447
448 rsnd_mod_bset(mod, SRC_ROUTE_SEL, mask, val);
449
28dc4b63
KM
450 return 0;
451}
452
f708d944 453static int rsnd_src_set_convert_timing_gen1(struct rsnd_mod *mod)
28dc4b63 454{
b42fccf6 455 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
28dc4b63 456 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
ba9c949f 457 struct rsnd_src *src = rsnd_mod_to_src(mod);
28dc4b63 458 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
ba9c949f 459 u32 convert_rate = rsnd_src_convert_rate(src);
28dc4b63
KM
460 u32 mask;
461 u32 val;
462 int shift;
463 int id = rsnd_mod_id(mod);
464 int ret;
465
374a5281
KM
466 /*
467 * SRC_TIMING_SELECT
468 */
469 shift = (id % 4) * 8;
470 mask = 0x1F << shift;
ef749400
KM
471
472 /*
473 * ADG is used as source clock if SRC was used,
474 * then, SSI WS is used as destination clock.
475 * SSI WS is used as source clock if SRC is not used
476 * (when playback, source/destination become reverse when capture)
477 */
28dc4b63
KM
478 ret = 0;
479 if (convert_rate) {
480 /* use ADG */
ef749400 481 val = 0;
28dc4b63
KM
482 ret = rsnd_adg_set_convert_clk_gen1(priv, mod,
483 runtime->rate,
484 convert_rate);
485 } else if (8 == id) {
486 /* use SSI WS, but SRU8 is special */
374a5281 487 val = id << shift;
28dc4b63
KM
488 } else {
489 /* use SSI WS */
374a5281 490 val = (id + 1) << shift;
28dc4b63
KM
491 }
492
493 if (ret < 0)
494 return ret;
374a5281
KM
495
496 switch (id / 4) {
497 case 0:
498 rsnd_mod_bset(mod, SRC_TMG_SEL0, mask, val);
499 break;
500 case 1:
501 rsnd_mod_bset(mod, SRC_TMG_SEL1, mask, val);
502 break;
503 case 2:
504 rsnd_mod_bset(mod, SRC_TMG_SEL2, mask, val);
505 break;
506 }
507
508 return 0;
509}
510
f708d944 511static int rsnd_src_set_convert_rate_gen1(struct rsnd_mod *mod)
374a5281 512{
933cc8cb 513 struct rsnd_src *src = rsnd_mod_to_src(mod);
1b7b08ef 514 int ret;
ef749400 515
f708d944 516 ret = rsnd_src_set_convert_rate(mod);
1b7b08ef
KM
517 if (ret < 0)
518 return ret;
ef749400 519
1b7b08ef
KM
520 /* Select SRC mode (fixed value) */
521 rsnd_mod_write(mod, SRC_SRCCR, 0x00010110);
ef749400 522
1b7b08ef
KM
523 /* Set the restriction value of the FS ratio (98%) */
524 rsnd_mod_write(mod, SRC_MNFSR,
525 rsnd_mod_read(mod, SRC_IFSVR) / 100 * 98);
ef749400 526
933cc8cb
KM
527 /* Gen1/Gen2 are not compatible */
528 if (rsnd_src_convert_rate(src))
529 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 1);
530
1b7b08ef 531 /* no SRC_BFSSR settings, since SRC_SRCCR::BUFMD is 0 */
ef749400 532
374a5281
KM
533 return 0;
534}
535
ba9c949f 536static int rsnd_src_init_gen1(struct rsnd_mod *mod,
2c0fac19 537 struct rsnd_dai_stream *io,
690602fc 538 struct rsnd_priv *priv)
07539c1d 539{
374a5281
KM
540 int ret;
541
3b7843ff 542 ret = rsnd_src_init(mod, priv);
7b5ce975
KM
543 if (ret < 0)
544 return ret;
545
f708d944 546 ret = rsnd_src_set_route_gen1(mod);
374a5281
KM
547 if (ret < 0)
548 return ret;
549
f708d944 550 ret = rsnd_src_set_convert_rate_gen1(mod);
374a5281
KM
551 if (ret < 0)
552 return ret;
07539c1d 553
f708d944 554 ret = rsnd_src_set_convert_timing_gen1(mod);
28dc4b63
KM
555 if (ret < 0)
556 return ret;
557
a204d90c
KM
558 return 0;
559}
560
ba9c949f 561static int rsnd_src_start_gen1(struct rsnd_mod *mod,
2c0fac19 562 struct rsnd_dai_stream *io,
690602fc 563 struct rsnd_priv *priv)
a204d90c 564{
1b7b08ef 565 int id = rsnd_mod_id(mod);
a204d90c 566
1b7b08ef 567 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), (1 << id));
374a5281 568
f0ef0cb8 569 return rsnd_src_start(mod);
07539c1d
KM
570}
571
ba9c949f 572static int rsnd_src_stop_gen1(struct rsnd_mod *mod,
2c0fac19 573 struct rsnd_dai_stream *io,
690602fc 574 struct rsnd_priv *priv)
a204d90c 575{
e7ce74ea 576 int id = rsnd_mod_id(mod);
a204d90c 577
1b7b08ef 578 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), 0);
e7ce74ea 579
f0ef0cb8 580 return rsnd_src_stop(mod);
a204d90c
KM
581}
582
ba9c949f 583static struct rsnd_mod_ops rsnd_src_gen1_ops = {
8aefda50 584 .name = SRC_NAME,
72adc61f 585 .dma_req = rsnd_src_dma_req,
ba9c949f
KM
586 .init = rsnd_src_init_gen1,
587 .quit = rsnd_src_quit,
588 .start = rsnd_src_start_gen1,
589 .stop = rsnd_src_stop_gen1,
3b7843ff 590 .hw_params = rsnd_src_hw_params,
1b7b08ef 591};
e7ce74ea 592
1b7b08ef
KM
593/*
594 * Gen2 functions
595 */
cfcefe01
KM
596#define rsnd_src_irq_enable_gen2(mod) rsnd_src_irq_ctrol_gen2(mod, 1)
597#define rsnd_src_irq_disable_gen2(mod) rsnd_src_irq_ctrol_gen2(mod, 0)
598static void rsnd_src_irq_ctrol_gen2(struct rsnd_mod *mod, int enable)
599{
600 struct rsnd_src *src = rsnd_mod_to_src(mod);
601 u32 sys_int_val, int_val, sys_int_mask;
602 int irq = src->info->irq;
603 int id = rsnd_mod_id(mod);
604
605 sys_int_val =
606 sys_int_mask = OUF_SRC(id);
607 int_val = 0x3300;
608
609 /*
610 * IRQ is not supported on non-DT
611 * see
612 * rsnd_src_probe_gen2()
613 */
614 if ((irq <= 0) || !enable) {
615 sys_int_val = 0;
616 int_val = 0;
617 }
618
619 rsnd_mod_write(mod, SRC_INT_ENABLE0, int_val);
620 rsnd_mod_bset(mod, SCU_SYS_INT_EN0, sys_int_mask, sys_int_val);
621 rsnd_mod_bset(mod, SCU_SYS_INT_EN1, sys_int_mask, sys_int_val);
622}
623
624static void rsnd_src_error_clear_gen2(struct rsnd_mod *mod)
625{
626 u32 val = OUF_SRC(rsnd_mod_id(mod));
627
628 rsnd_mod_bset(mod, SCU_SYS_STATUS0, val, val);
629 rsnd_mod_bset(mod, SCU_SYS_STATUS1, val, val);
630}
631
632static bool rsnd_src_error_record_gen2(struct rsnd_mod *mod)
633{
634 u32 val = OUF_SRC(rsnd_mod_id(mod));
635 bool ret = false;
636
637 if ((rsnd_mod_read(mod, SCU_SYS_STATUS0) & val) ||
638 (rsnd_mod_read(mod, SCU_SYS_STATUS1) & val)) {
639 struct rsnd_src *src = rsnd_mod_to_src(mod);
640
641 src->err++;
642 ret = true;
643 }
644
645 /* clear error static */
646 rsnd_src_error_clear_gen2(mod);
647
648 return ret;
649}
650
651static int _rsnd_src_start_gen2(struct rsnd_mod *mod)
652{
653 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
654 u32 val = rsnd_io_to_mod_dvc(io) ? 0x01 : 0x11;
655
656 rsnd_mod_write(mod, SRC_CTRL, val);
657
658 rsnd_src_error_clear_gen2(mod);
659
660 rsnd_src_start(mod);
661
662 rsnd_src_irq_enable_gen2(mod);
663
664 return 0;
665}
666
667static int _rsnd_src_stop_gen2(struct rsnd_mod *mod)
668{
669 rsnd_src_irq_disable_gen2(mod);
670
671 rsnd_mod_write(mod, SRC_CTRL, 0);
672
673 rsnd_src_error_record_gen2(mod);
674
675 return rsnd_src_stop(mod);
676}
677
678static irqreturn_t rsnd_src_interrupt_gen2(int irq, void *data)
679{
680 struct rsnd_mod *mod = data;
02299d98
KM
681 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
682
683 spin_lock(&priv->lock);
cfcefe01 684
02299d98
KM
685 /* ignore all cases if not working */
686 if (!rsnd_mod_is_working(mod))
687 goto rsnd_src_interrupt_gen2_out;
cfcefe01
KM
688
689 if (rsnd_src_error_record_gen2(mod)) {
690 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
639b231f 691 struct rsnd_src *src = rsnd_mod_to_src(mod);
cfcefe01
KM
692 struct device *dev = rsnd_priv_to_dev(priv);
693
cfcefe01
KM
694 dev_dbg(dev, "%s[%d] restart\n",
695 rsnd_mod_name(mod), rsnd_mod_id(mod));
639b231f
KM
696
697 _rsnd_src_stop_gen2(mod);
698 if (src->err < 1024)
699 _rsnd_src_start_gen2(mod);
700 else
701 dev_warn(dev, "no more SRC restart\n");
cfcefe01 702 }
02299d98
KM
703rsnd_src_interrupt_gen2_out:
704 spin_unlock(&priv->lock);
cfcefe01
KM
705
706 return IRQ_HANDLED;
707}
708
f708d944 709static int rsnd_src_set_convert_rate_gen2(struct rsnd_mod *mod)
629509c5 710{
054cd7f4
KM
711 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
712 struct device *dev = rsnd_priv_to_dev(priv);
713 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
714 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
715 struct rsnd_src *src = rsnd_mod_to_src(mod);
b167a578 716 u32 convert_rate = rsnd_src_convert_rate(src);
43cb6954 717 u32 cr, route;
054cd7f4 718 uint ratio;
629509c5
KM
719 int ret;
720
054cd7f4 721 /* 6 - 1/6 are very enough ratio for SRC_BSDSR */
b167a578 722 if (!convert_rate)
054cd7f4 723 ratio = 0;
b167a578
KM
724 else if (convert_rate > runtime->rate)
725 ratio = 100 * convert_rate / runtime->rate;
054cd7f4 726 else
b167a578 727 ratio = 100 * runtime->rate / convert_rate;
054cd7f4
KM
728
729 if (ratio > 600) {
730 dev_err(dev, "FSO/FSI ratio error\n");
731 return -EINVAL;
732 }
733
f708d944 734 ret = rsnd_src_set_convert_rate(mod);
629509c5
KM
735 if (ret < 0)
736 return ret;
737
43cb6954
KM
738 cr = 0x00011110;
739 route = 0x0;
933cc8cb 740 if (convert_rate) {
43cb6954
KM
741 route = 0x1;
742
743 if (rsnd_enable_sync_convert(src)) {
744 cr |= 0x1;
745 route |= rsnd_io_is_play(io) ?
746 (0x1 << 24) : (0x1 << 25);
747 }
933cc8cb
KM
748 }
749
43cb6954
KM
750 rsnd_mod_write(mod, SRC_SRCCR, cr);
751 rsnd_mod_write(mod, SRC_ROUTE_MODE0, route);
752
054cd7f4
KM
753 switch (rsnd_mod_id(mod)) {
754 case 5:
755 case 6:
756 case 7:
757 case 8:
758 rsnd_mod_write(mod, SRC_BSDSR, 0x02400000);
759 break;
760 default:
761 rsnd_mod_write(mod, SRC_BSDSR, 0x01800000);
762 break;
763 }
764
629509c5
KM
765 rsnd_mod_write(mod, SRC_BSISR, 0x00100060);
766
767 return 0;
768}
769
f708d944 770static int rsnd_src_set_convert_timing_gen2(struct rsnd_mod *mod)
629509c5 771{
b42fccf6 772 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
629509c5 773 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
ba9c949f
KM
774 struct rsnd_src *src = rsnd_mod_to_src(mod);
775 u32 convert_rate = rsnd_src_convert_rate(src);
629509c5
KM
776 int ret;
777
778 if (convert_rate)
f708d944 779 ret = rsnd_adg_set_convert_clk_gen2(mod, io,
629509c5
KM
780 runtime->rate,
781 convert_rate);
782 else
f708d944 783 ret = rsnd_adg_set_convert_timing_gen2(mod, io);
629509c5
KM
784
785 return ret;
786}
787
ba9c949f 788static int rsnd_src_probe_gen2(struct rsnd_mod *mod,
2c0fac19 789 struct rsnd_dai_stream *io,
690602fc 790 struct rsnd_priv *priv)
76c6fb5c 791{
ba9c949f 792 struct rsnd_src *src = rsnd_mod_to_src(mod);
76c6fb5c 793 struct device *dev = rsnd_priv_to_dev(priv);
cfcefe01 794 int irq = src->info->irq;
76c6fb5c 795 int ret;
76c6fb5c 796
cfcefe01
KM
797 if (irq > 0) {
798 /*
799 * IRQ is not supported on non-DT
800 * see
801 * rsnd_src_irq_enable_gen2()
802 */
803 ret = devm_request_irq(dev, irq,
804 rsnd_src_interrupt_gen2,
805 IRQF_SHARED,
806 dev_name(dev), mod);
807 if (ret)
b543b52a 808 return ret;
cfcefe01
KM
809 }
810
76c6fb5c
KM
811 ret = rsnd_dma_init(priv,
812 rsnd_mod_to_dma(mod),
ba9c949f 813 src->info->dma_id);
8aefda50 814
76c6fb5c
KM
815 return ret;
816}
817
ba9c949f 818static int rsnd_src_remove_gen2(struct rsnd_mod *mod,
2c0fac19 819 struct rsnd_dai_stream *io,
690602fc 820 struct rsnd_priv *priv)
76c6fb5c 821{
8a2ff426 822 rsnd_dma_quit(rsnd_mod_to_dma(mod));
76c6fb5c
KM
823
824 return 0;
825}
826
ba9c949f 827static int rsnd_src_init_gen2(struct rsnd_mod *mod,
2c0fac19 828 struct rsnd_dai_stream *io,
690602fc 829 struct rsnd_priv *priv)
629509c5
KM
830{
831 int ret;
832
3b7843ff 833 ret = rsnd_src_init(mod, priv);
629509c5
KM
834 if (ret < 0)
835 return ret;
836
f708d944 837 ret = rsnd_src_set_convert_rate_gen2(mod);
629509c5
KM
838 if (ret < 0)
839 return ret;
840
f708d944 841 ret = rsnd_src_set_convert_timing_gen2(mod);
629509c5
KM
842 if (ret < 0)
843 return ret;
844
845 return 0;
846}
847
ba9c949f 848static int rsnd_src_start_gen2(struct rsnd_mod *mod,
2c0fac19 849 struct rsnd_dai_stream *io,
690602fc 850 struct rsnd_priv *priv)
629509c5 851{
cfcefe01 852 rsnd_dma_start(rsnd_mod_to_dma(mod));
629509c5 853
cfcefe01 854 return _rsnd_src_start_gen2(mod);
629509c5
KM
855}
856
ba9c949f 857static int rsnd_src_stop_gen2(struct rsnd_mod *mod,
2c0fac19 858 struct rsnd_dai_stream *io,
690602fc 859 struct rsnd_priv *priv)
629509c5 860{
cfcefe01 861 int ret;
629509c5 862
cfcefe01 863 ret = _rsnd_src_stop_gen2(mod);
629509c5 864
cfcefe01 865 rsnd_dma_stop(rsnd_mod_to_dma(mod));
629509c5 866
cfcefe01 867 return ret;
629509c5
KM
868}
869
43cb6954
KM
870static void rsnd_src_reconvert_update(struct rsnd_mod *mod)
871{
872 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
873 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
874 struct rsnd_src *src = rsnd_mod_to_src(mod);
875 u32 convert_rate = rsnd_src_convert_rate(src);
876 u32 fsrate;
877
878 if (!runtime)
879 return;
880
881 if (!convert_rate)
882 convert_rate = runtime->rate;
883
884 fsrate = 0x0400000 / convert_rate * runtime->rate;
885
886 /* update IFS */
887 rsnd_mod_write(mod, SRC_IFSVR, fsrate);
888}
889
890static int rsnd_src_pcm_new(struct rsnd_mod *mod,
2c0fac19 891 struct rsnd_dai_stream *io,
43cb6954
KM
892 struct snd_soc_pcm_runtime *rtd)
893{
894 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
43cb6954
KM
895 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
896 struct rsnd_src *src = rsnd_mod_to_src(mod);
897 int ret;
898
899 /*
900 * enable SRC sync convert if possible
901 */
902
903 /*
904 * Gen1 is not supported
905 */
906 if (rsnd_is_gen1(priv))
907 return 0;
908
909 /*
910 * SRC sync convert needs clock master
911 */
912 if (!rsnd_rdai_is_clk_master(rdai))
913 return 0;
914
915 /*
916 * We can't use SRC sync convert
917 * if it has DVC
918 */
919 if (rsnd_io_to_mod_dvc(io))
920 return 0;
921
922 /*
923 * enable sync convert
924 */
925 ret = rsnd_kctrl_new_s(mod, rtd,
926 rsnd_io_is_play(io) ?
927 "SRC Out Rate Switch" :
928 "SRC In Rate Switch",
929 rsnd_src_reconvert_update,
930 &src->sen, 1);
931 if (ret < 0)
932 return ret;
933
934 ret = rsnd_kctrl_new_s(mod, rtd,
935 rsnd_io_is_play(io) ?
936 "SRC Out Rate" :
937 "SRC In Rate",
938 rsnd_src_reconvert_update,
939 &src->sync, 192000);
940
941 return ret;
942}
943
ba9c949f 944static struct rsnd_mod_ops rsnd_src_gen2_ops = {
8aefda50 945 .name = SRC_NAME,
72adc61f 946 .dma_req = rsnd_src_dma_req,
ba9c949f
KM
947 .probe = rsnd_src_probe_gen2,
948 .remove = rsnd_src_remove_gen2,
949 .init = rsnd_src_init_gen2,
950 .quit = rsnd_src_quit,
951 .start = rsnd_src_start_gen2,
952 .stop = rsnd_src_stop_gen2,
3b7843ff 953 .hw_params = rsnd_src_hw_params,
43cb6954 954 .pcm_new = rsnd_src_pcm_new,
629509c5
KM
955};
956
ba9c949f 957struct rsnd_mod *rsnd_src_mod_get(struct rsnd_priv *priv, int id)
07539c1d 958{
ba9c949f 959 if (WARN_ON(id < 0 || id >= rsnd_src_nr(priv)))
8b14719b 960 id = 0;
07539c1d 961
ba9c949f 962 return &((struct rsnd_src *)(priv->src) + id)->mod;
07539c1d
KM
963}
964
90e8e50f
KM
965static void rsnd_of_parse_src(struct platform_device *pdev,
966 const struct rsnd_of_data *of_data,
967 struct rsnd_priv *priv)
968{
969 struct device_node *src_node;
cfcefe01 970 struct device_node *np;
90e8e50f
KM
971 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
972 struct rsnd_src_platform_info *src_info;
973 struct device *dev = &pdev->dev;
cfcefe01 974 int nr, i;
90e8e50f
KM
975
976 if (!of_data)
977 return;
978
82e76ed3 979 src_node = rsnd_src_of_node(priv);
90e8e50f
KM
980 if (!src_node)
981 return;
982
983 nr = of_get_child_count(src_node);
984 if (!nr)
f451e48d 985 goto rsnd_of_parse_src_end;
90e8e50f
KM
986
987 src_info = devm_kzalloc(dev,
988 sizeof(struct rsnd_src_platform_info) * nr,
989 GFP_KERNEL);
990 if (!src_info) {
991 dev_err(dev, "src info allocation error\n");
f451e48d 992 goto rsnd_of_parse_src_end;
90e8e50f
KM
993 }
994
995 info->src_info = src_info;
996 info->src_info_nr = nr;
f451e48d 997
cfcefe01
KM
998 i = 0;
999 for_each_child_of_node(src_node, np) {
1000 src_info[i].irq = irq_of_parse_and_map(np, 0);
1001
1002 i++;
1003 }
1004
f451e48d
KM
1005rsnd_of_parse_src_end:
1006 of_node_put(src_node);
90e8e50f
KM
1007}
1008
ba9c949f 1009int rsnd_src_probe(struct platform_device *pdev,
90e8e50f 1010 const struct rsnd_of_data *of_data,
07539c1d
KM
1011 struct rsnd_priv *priv)
1012{
5da39cf3 1013 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
07539c1d 1014 struct device *dev = rsnd_priv_to_dev(priv);
ba9c949f 1015 struct rsnd_src *src;
013f38fe 1016 struct rsnd_mod_ops *ops;
ef749400 1017 struct clk *clk;
ba9c949f 1018 char name[RSND_SRC_NAME_SIZE];
2f78dd7f 1019 int i, nr, ret;
07539c1d 1020
033e7ed8
KM
1021 ops = NULL;
1022 if (rsnd_is_gen1(priv))
1023 ops = &rsnd_src_gen1_ops;
1024 if (rsnd_is_gen2(priv))
1025 ops = &rsnd_src_gen2_ops;
1026 if (!ops) {
1027 dev_err(dev, "unknown Generation\n");
1028 return -EIO;
1029 }
1030
90e8e50f
KM
1031 rsnd_of_parse_src(pdev, of_data, priv);
1032
07539c1d 1033 /*
ba9c949f 1034 * init SRC
07539c1d 1035 */
ba9c949f 1036 nr = info->src_info_nr;
389933d9
KM
1037 if (!nr)
1038 return 0;
1039
ba9c949f
KM
1040 src = devm_kzalloc(dev, sizeof(*src) * nr, GFP_KERNEL);
1041 if (!src) {
1042 dev_err(dev, "SRC allocate failed\n");
07539c1d
KM
1043 return -ENOMEM;
1044 }
1045
ba9c949f
KM
1046 priv->src_nr = nr;
1047 priv->src = src;
07539c1d 1048
ba9c949f 1049 for_each_rsnd_src(src, priv, i) {
8aefda50
KM
1050 snprintf(name, RSND_SRC_NAME_SIZE, "%s.%d",
1051 SRC_NAME, i);
ef749400
KM
1052
1053 clk = devm_clk_get(dev, name);
1054 if (IS_ERR(clk))
1055 return PTR_ERR(clk);
1056
ba9c949f 1057 src->info = &info->src_info[i];
07539c1d 1058
2099bc8e 1059 ret = rsnd_mod_init(priv, &src->mod, ops, clk, RSND_MOD_SRC, i);
2f78dd7f
KM
1060 if (ret)
1061 return ret;
374a5281 1062 }
07539c1d
KM
1063
1064 return 0;
1065}
2f78dd7f
KM
1066
1067void rsnd_src_remove(struct platform_device *pdev,
1068 struct rsnd_priv *priv)
1069{
1070 struct rsnd_src *src;
1071 int i;
1072
1073 for_each_rsnd_src(src, priv, i) {
1074 rsnd_mod_quit(&src->mod);
1075 }
1076}
This page took 0.342527 seconds and 5 git commands to generate.