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