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