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