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