ASoC: rsnd: src: make sure SRC soft reset
[deliverable/linux.git] / sound / soc / sh / rcar / src.c
1 /*
2 * Renesas R-Car SRC support
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
13 #define SRC_NAME "src"
14
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
22 struct rsnd_src {
23 struct rsnd_src_platform_info *info; /* rcar_snd.h */
24 struct rsnd_mod mod;
25 struct rsnd_kctrl_cfg_s sen; /* sync convert enable */
26 struct rsnd_kctrl_cfg_s sync; /* sync convert */
27 u32 convert_rate; /* sampling rate convert */
28 int err;
29 };
30
31 #define RSND_SRC_NAME_SIZE 16
32
33 #define rsnd_src_nr(priv) ((priv)->src_nr)
34 #define rsnd_enable_sync_convert(src) ((src)->sen.val)
35 #define rsnd_src_of_node(priv) \
36 of_get_child_by_name(rsnd_priv_to_dev(priv)->of_node, "rcar_sound,src")
37
38 #define rsnd_mod_to_src(_mod) \
39 container_of((_mod), struct rsnd_src, mod)
40
41 #define for_each_rsnd_src(pos, priv, i) \
42 for ((i) = 0; \
43 ((i) < rsnd_src_nr(priv)) && \
44 ((pos) = (struct rsnd_src *)(priv)->src + i); \
45 i++)
46
47
48 /*
49 * image of SRC (Sampling Rate Converter)
50 *
51 * 96kHz <-> +-----+ 48kHz +-----+ 48kHz +-------+
52 * 48kHz <-> | SRC | <------> | SSI | <-----> | codec |
53 * 44.1kHz <-> +-----+ +-----+ +-------+
54 * ...
55 *
56 */
57
58 /*
59 * src.c is caring...
60 *
61 * Gen1
62 *
63 * [mem] -> [SRU] -> [SSI]
64 * |--------|
65 *
66 * Gen2
67 *
68 * [mem] -> [SRC] -> [SSIU] -> [SSI]
69 * |-----------------|
70 */
71
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
84 * struct rsnd_src_platform_info {
85 * u32 convert_rate;
86 * int dma_id;
87 * }
88 *
89 * rsnd_src_convert_rate() indicates
90 * above convert_rate, and it controls
91 * whether SRC is used or not.
92 *
93 * ex) doesn't use SRC
94 * static struct rsnd_dai_platform_info rsnd_dai = {
95 * .playback = { .ssi = &rsnd_ssi[0], },
96 * };
97 *
98 * ex) uses SRC
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] },
105 * };
106 *
107 * ex) uses SRC bypass mode
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] },
114 * };
115 *
116 */
117
118 /*
119 * Gen1/Gen2 common functions
120 */
121 static 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
127 static struct dma_chan *rsnd_src_dma_req(struct rsnd_dai_stream *io,
128 struct rsnd_mod *mod)
129 {
130 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
131 int is_play = rsnd_io_is_play(io);
132
133 return rsnd_dma_request_channel(rsnd_src_of_node(priv),
134 mod,
135 is_play ? "rx" : "tx");
136 }
137
138 int rsnd_src_ssiu_start(struct rsnd_mod *ssi_mod,
139 struct rsnd_dai_stream *io,
140 int use_busif)
141 {
142 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
143 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
144 int ssi_id = rsnd_mod_id(ssi_mod);
145
146 /*
147 * SSI_MODE0
148 */
149 rsnd_mod_bset(ssi_mod, SSI_MODE0, (1 << ssi_id),
150 !use_busif << ssi_id);
151
152 /*
153 * SSI_MODE1
154 */
155 if (rsnd_ssi_is_pin_sharing(ssi_mod)) {
156 int shift = -1;
157 switch (ssi_id) {
158 case 1:
159 shift = 0;
160 break;
161 case 2:
162 shift = 2;
163 break;
164 case 4:
165 shift = 16;
166 break;
167 }
168
169 if (shift >= 0)
170 rsnd_mod_bset(ssi_mod, SSI_MODE1,
171 0x3 << shift,
172 rsnd_rdai_is_clk_master(rdai) ?
173 0x2 << shift : 0x1 << shift);
174 }
175
176 /*
177 * DMA settings for SSIU
178 */
179 if (use_busif) {
180 u32 val = 0x76543210;
181 u32 mask = ~0;
182
183 rsnd_mod_write(ssi_mod, SSI_BUSIF_ADINR,
184 rsnd_get_adinr(ssi_mod, io));
185 rsnd_mod_write(ssi_mod, SSI_BUSIF_MODE, 1);
186 rsnd_mod_write(ssi_mod, SSI_CTRL, 0x1);
187
188 mask <<= runtime->channels * 4;
189 val = val & mask;
190
191 switch (runtime->sample_bits) {
192 case 16:
193 val |= 0x67452301 & ~mask;
194 break;
195 case 32:
196 val |= 0x76543210 & ~mask;
197 break;
198 }
199 rsnd_mod_write(ssi_mod, SSI_BUSIF_DALIGN, val);
200
201 }
202
203 return 0;
204 }
205
206 int rsnd_src_ssiu_stop(struct rsnd_mod *ssi_mod,
207 struct rsnd_dai_stream *io)
208 {
209 /*
210 * DMA settings for SSIU
211 */
212 rsnd_mod_write(ssi_mod, SSI_CTRL, 0);
213
214 return 0;
215 }
216
217 int rsnd_src_ssi_irq_enable(struct rsnd_mod *ssi_mod)
218 {
219 struct rsnd_priv *priv = rsnd_mod_to_priv(ssi_mod);
220
221 if (rsnd_is_gen1(priv))
222 return 0;
223
224 /* enable SSI interrupt if Gen2 */
225 rsnd_mod_write(ssi_mod, SSI_INT_ENABLE,
226 rsnd_ssi_is_dma_mode(ssi_mod) ?
227 0x0e000000 : 0x0f000000);
228
229 return 0;
230 }
231
232 int rsnd_src_ssi_irq_disable(struct rsnd_mod *ssi_mod)
233 {
234 struct rsnd_priv *priv = rsnd_mod_to_priv(ssi_mod);
235
236 if (rsnd_is_gen1(priv))
237 return 0;
238
239 /* disable SSI interrupt if Gen2 */
240 rsnd_mod_write(ssi_mod, SSI_INT_ENABLE, 0x00000000);
241
242 return 0;
243 }
244
245 static u32 rsnd_src_convert_rate(struct rsnd_dai_stream *io,
246 struct rsnd_src *src)
247 {
248 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
249 u32 convert_rate;
250
251 if (!runtime)
252 return 0;
253
254 if (!rsnd_enable_sync_convert(src))
255 return src->convert_rate;
256
257 convert_rate = src->sync.val;
258
259 if (!convert_rate)
260 convert_rate = src->convert_rate;
261
262 if (!convert_rate)
263 convert_rate = runtime->rate;
264
265 return convert_rate;
266 }
267
268 unsigned int rsnd_src_get_ssi_rate(struct rsnd_priv *priv,
269 struct rsnd_dai_stream *io,
270 struct snd_pcm_runtime *runtime)
271 {
272 struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
273 struct rsnd_src *src;
274 unsigned int rate = 0;
275
276 if (src_mod) {
277 src = rsnd_mod_to_src(src_mod);
278
279 /*
280 * return convert rate if SRC is used,
281 * otherwise, return runtime->rate as usual
282 */
283 rate = rsnd_src_convert_rate(io, src);
284 }
285
286 if (!rate)
287 rate = runtime->rate;
288
289 return rate;
290 }
291
292 static int rsnd_src_set_convert_rate(struct rsnd_mod *mod,
293 struct rsnd_dai_stream *io)
294 {
295 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
296 struct rsnd_src *src = rsnd_mod_to_src(mod);
297 u32 convert_rate = rsnd_src_convert_rate(io, src);
298 u32 fsrate = 0;
299
300 if (convert_rate)
301 fsrate = 0x0400000 / convert_rate * runtime->rate;
302
303 /* Set channel number and output bit length */
304 rsnd_mod_write(mod, SRC_ADINR, rsnd_get_adinr(mod, io));
305
306 /* Enable the initial value of IFS */
307 if (fsrate) {
308 rsnd_mod_write(mod, SRC_IFSCR, 1);
309
310 /* Set initial value of IFS */
311 rsnd_mod_write(mod, SRC_IFSVR, fsrate);
312 }
313
314 /* use DMA transfer */
315 rsnd_mod_write(mod, SRC_BUSIF_MODE, 1);
316
317 return 0;
318 }
319
320 static int rsnd_src_hw_params(struct rsnd_mod *mod,
321 struct rsnd_dai_stream *io,
322 struct snd_pcm_substream *substream,
323 struct snd_pcm_hw_params *fe_params)
324 {
325 struct rsnd_src *src = rsnd_mod_to_src(mod);
326 struct snd_soc_pcm_runtime *fe = substream->private_data;
327
328 /* default value (mainly for non-DT) */
329 src->convert_rate = src->info->convert_rate;
330
331 /*
332 * SRC assumes that it is used under DPCM if user want to use
333 * sampling rate convert. Then, SRC should be FE.
334 * And then, this function will be called *after* BE settings.
335 * this means, each BE already has fixuped hw_params.
336 * see
337 * dpcm_fe_dai_hw_params()
338 * dpcm_be_dai_hw_params()
339 */
340 if (fe->dai_link->dynamic) {
341 int stream = substream->stream;
342 struct snd_soc_dpcm *dpcm;
343 struct snd_pcm_hw_params *be_params;
344
345 list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) {
346 be_params = &dpcm->hw_params;
347
348 if (params_rate(fe_params) != params_rate(be_params))
349 src->convert_rate = params_rate(be_params);
350 }
351 }
352
353 return 0;
354 }
355
356 static int rsnd_src_init(struct rsnd_mod *mod,
357 struct rsnd_priv *priv)
358 {
359 struct rsnd_src *src = rsnd_mod_to_src(mod);
360
361 rsnd_mod_hw_start(mod);
362
363 rsnd_src_soft_reset(mod);
364
365 src->err = 0;
366
367 /* reset sync convert_rate */
368 src->sync.val = 0;
369
370 /*
371 * Initialize the operation of the SRC internal circuits
372 * see rsnd_src_start()
373 */
374 rsnd_mod_write(mod, SRC_SRCIR, 1);
375
376 return 0;
377 }
378
379 static int rsnd_src_quit(struct rsnd_mod *mod,
380 struct rsnd_dai_stream *io,
381 struct rsnd_priv *priv)
382 {
383 struct rsnd_src *src = rsnd_mod_to_src(mod);
384 struct device *dev = rsnd_priv_to_dev(priv);
385
386 rsnd_mod_hw_stop(mod);
387
388 if (src->err)
389 dev_warn(dev, "%s[%d] under/over flow err = %d\n",
390 rsnd_mod_name(mod), rsnd_mod_id(mod), src->err);
391
392 src->convert_rate = 0;
393
394 /* reset sync convert_rate */
395 src->sync.val = 0;
396
397 return 0;
398 }
399
400 static int rsnd_src_start(struct rsnd_mod *mod)
401 {
402 /*
403 * Cancel the initialization and operate the SRC function
404 * see rsnd_src_init()
405 */
406 rsnd_mod_write(mod, SRC_SRCIR, 0);
407
408 return 0;
409 }
410
411 static int rsnd_src_stop(struct rsnd_mod *mod)
412 {
413 /* nothing to do */
414 return 0;
415 }
416
417 /*
418 * Gen1 functions
419 */
420 static int rsnd_src_set_route_gen1(struct rsnd_dai_stream *io,
421 struct rsnd_mod *mod)
422 {
423 struct src_route_config {
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 };
437 u32 mask;
438 u32 val;
439 int id;
440
441 id = rsnd_mod_id(mod);
442 if (id < 0 || id >= ARRAY_SIZE(routes))
443 return -EIO;
444
445 /*
446 * SRC_ROUTE_SELECT
447 */
448 val = rsnd_io_is_play(io) ? 0x1 : 0x2;
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
454 return 0;
455 }
456
457 static int rsnd_src_set_convert_timing_gen1(struct rsnd_dai_stream *io,
458 struct rsnd_mod *mod)
459 {
460 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
461 struct rsnd_src *src = rsnd_mod_to_src(mod);
462 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
463 u32 convert_rate = rsnd_src_convert_rate(io, src);
464 u32 mask;
465 u32 val;
466 int shift;
467 int id = rsnd_mod_id(mod);
468 int ret;
469
470 /*
471 * SRC_TIMING_SELECT
472 */
473 shift = (id % 4) * 8;
474 mask = 0x1F << shift;
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 */
482 ret = 0;
483 if (convert_rate) {
484 /* use ADG */
485 val = 0;
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 */
491 val = id << shift;
492 } else {
493 /* use SSI WS */
494 val = (id + 1) << shift;
495 }
496
497 if (ret < 0)
498 return ret;
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
515 static int rsnd_src_set_convert_rate_gen1(struct rsnd_mod *mod,
516 struct rsnd_dai_stream *io)
517 {
518 struct rsnd_src *src = rsnd_mod_to_src(mod);
519 int ret;
520
521 ret = rsnd_src_set_convert_rate(mod, io);
522 if (ret < 0)
523 return ret;
524
525 /* Select SRC mode (fixed value) */
526 rsnd_mod_write(mod, SRC_SRCCR, 0x00010110);
527
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);
531
532 /* Gen1/Gen2 are not compatible */
533 if (rsnd_src_convert_rate(io, src))
534 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 1);
535
536 /* no SRC_BFSSR settings, since SRC_SRCCR::BUFMD is 0 */
537
538 return 0;
539 }
540
541 static int rsnd_src_init_gen1(struct rsnd_mod *mod,
542 struct rsnd_dai_stream *io,
543 struct rsnd_priv *priv)
544 {
545 int ret;
546
547 ret = rsnd_src_init(mod, priv);
548 if (ret < 0)
549 return ret;
550
551 ret = rsnd_src_set_route_gen1(io, mod);
552 if (ret < 0)
553 return ret;
554
555 ret = rsnd_src_set_convert_rate_gen1(mod, io);
556 if (ret < 0)
557 return ret;
558
559 ret = rsnd_src_set_convert_timing_gen1(io, mod);
560 if (ret < 0)
561 return ret;
562
563 return 0;
564 }
565
566 static int rsnd_src_start_gen1(struct rsnd_mod *mod,
567 struct rsnd_dai_stream *io,
568 struct rsnd_priv *priv)
569 {
570 int id = rsnd_mod_id(mod);
571
572 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), (1 << id));
573
574 return rsnd_src_start(mod);
575 }
576
577 static int rsnd_src_stop_gen1(struct rsnd_mod *mod,
578 struct rsnd_dai_stream *io,
579 struct rsnd_priv *priv)
580 {
581 int id = rsnd_mod_id(mod);
582
583 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), 0);
584
585 return rsnd_src_stop(mod);
586 }
587
588 static struct rsnd_mod_ops rsnd_src_gen1_ops = {
589 .name = SRC_NAME,
590 .dma_req = rsnd_src_dma_req,
591 .init = rsnd_src_init_gen1,
592 .quit = rsnd_src_quit,
593 .start = rsnd_src_start_gen1,
594 .stop = rsnd_src_stop_gen1,
595 .hw_params = rsnd_src_hw_params,
596 };
597
598 /*
599 * Gen2 functions
600 */
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)
603 static 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
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
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
637 static 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
645 static bool rsnd_src_error_record_gen2(struct rsnd_mod *mod)
646 {
647 struct rsnd_src *src = rsnd_mod_to_src(mod);
648 u32 val0, val1;
649 bool ret = false;
650
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)) {
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
675 static int _rsnd_src_start_gen2(struct rsnd_mod *mod,
676 struct rsnd_dai_stream *io)
677 {
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;
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
700 static 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
711 static void __rsnd_src_interrupt_gen2(struct rsnd_mod *mod,
712 struct rsnd_dai_stream *io)
713 {
714 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
715
716 spin_lock(&priv->lock);
717
718 /* ignore all cases if not working */
719 if (!rsnd_io_is_working(io))
720 goto rsnd_src_interrupt_gen2_out;
721
722 if (rsnd_src_error_record_gen2(mod)) {
723 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
724 struct rsnd_src *src = rsnd_mod_to_src(mod);
725 struct device *dev = rsnd_priv_to_dev(priv);
726
727 dev_dbg(dev, "%s[%d] restart\n",
728 rsnd_mod_name(mod), rsnd_mod_id(mod));
729
730 _rsnd_src_stop_gen2(mod);
731 if (src->err < 1024)
732 _rsnd_src_start_gen2(mod, io);
733 else
734 dev_warn(dev, "no more SRC restart\n");
735 }
736
737 rsnd_src_interrupt_gen2_out:
738 spin_unlock(&priv->lock);
739 }
740
741 static 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);
746
747 return IRQ_HANDLED;
748 }
749
750 static int rsnd_src_set_convert_rate_gen2(struct rsnd_mod *mod,
751 struct rsnd_dai_stream *io)
752 {
753 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
754 struct device *dev = rsnd_priv_to_dev(priv);
755 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
756 struct rsnd_src *src = rsnd_mod_to_src(mod);
757 u32 convert_rate = rsnd_src_convert_rate(io, src);
758 u32 cr, route;
759 uint ratio;
760 int ret;
761
762 /* 6 - 1/6 are very enough ratio for SRC_BSDSR */
763 if (!convert_rate)
764 ratio = 0;
765 else if (convert_rate > runtime->rate)
766 ratio = 100 * convert_rate / runtime->rate;
767 else
768 ratio = 100 * runtime->rate / convert_rate;
769
770 if (ratio > 600) {
771 dev_err(dev, "FSO/FSI ratio error\n");
772 return -EINVAL;
773 }
774
775 ret = rsnd_src_set_convert_rate(mod, io);
776 if (ret < 0)
777 return ret;
778
779 cr = 0x00011110;
780 route = 0x0;
781 if (convert_rate) {
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 }
789 }
790
791 rsnd_mod_write(mod, SRC_SRCCR, cr);
792 rsnd_mod_write(mod, SRC_ROUTE_MODE0, route);
793
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
806 rsnd_mod_write(mod, SRC_BSISR, 0x00100060);
807
808 return 0;
809 }
810
811 static int rsnd_src_set_convert_timing_gen2(struct rsnd_dai_stream *io,
812 struct rsnd_mod *mod)
813 {
814 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
815 struct rsnd_src *src = rsnd_mod_to_src(mod);
816 u32 convert_rate = rsnd_src_convert_rate(io, src);
817 int ret;
818
819 if (convert_rate)
820 ret = rsnd_adg_set_convert_clk_gen2(mod, io,
821 runtime->rate,
822 convert_rate);
823 else
824 ret = rsnd_adg_set_convert_timing_gen2(mod, io);
825
826 return ret;
827 }
828
829 static int rsnd_src_probe_gen2(struct rsnd_mod *mod,
830 struct rsnd_dai_stream *io,
831 struct rsnd_priv *priv)
832 {
833 struct rsnd_src *src = rsnd_mod_to_src(mod);
834 struct device *dev = rsnd_priv_to_dev(priv);
835 int irq = src->info->irq;
836 int ret;
837
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)
849 return ret;
850 }
851
852 ret = rsnd_dma_init(io,
853 rsnd_mod_to_dma(mod),
854 src->info->dma_id);
855
856 return ret;
857 }
858
859 static int rsnd_src_remove_gen2(struct rsnd_mod *mod,
860 struct rsnd_dai_stream *io,
861 struct rsnd_priv *priv)
862 {
863 rsnd_dma_quit(io, rsnd_mod_to_dma(mod));
864
865 return 0;
866 }
867
868 static int rsnd_src_init_gen2(struct rsnd_mod *mod,
869 struct rsnd_dai_stream *io,
870 struct rsnd_priv *priv)
871 {
872 int ret;
873
874 ret = rsnd_src_init(mod, priv);
875 if (ret < 0)
876 return ret;
877
878 ret = rsnd_src_set_convert_rate_gen2(mod, io);
879 if (ret < 0)
880 return ret;
881
882 ret = rsnd_src_set_convert_timing_gen2(io, mod);
883 if (ret < 0)
884 return ret;
885
886 return 0;
887 }
888
889 static int rsnd_src_start_gen2(struct rsnd_mod *mod,
890 struct rsnd_dai_stream *io,
891 struct rsnd_priv *priv)
892 {
893 rsnd_dma_start(io, rsnd_mod_to_dma(mod));
894
895 return _rsnd_src_start_gen2(mod, io);
896 }
897
898 static int rsnd_src_stop_gen2(struct rsnd_mod *mod,
899 struct rsnd_dai_stream *io,
900 struct rsnd_priv *priv)
901 {
902 int ret;
903
904 ret = _rsnd_src_stop_gen2(mod);
905
906 rsnd_dma_stop(io, rsnd_mod_to_dma(mod));
907
908 return ret;
909 }
910
911 static void rsnd_src_reconvert_update(struct rsnd_dai_stream *io,
912 struct rsnd_mod *mod)
913 {
914 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
915 struct rsnd_src *src = rsnd_mod_to_src(mod);
916 u32 convert_rate = rsnd_src_convert_rate(io, src);
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
931 static int rsnd_src_pcm_new(struct rsnd_mod *mod,
932 struct rsnd_dai_stream *io,
933 struct snd_soc_pcm_runtime *rtd)
934 {
935 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
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
956 /*
957 * enable sync convert
958 */
959 ret = rsnd_kctrl_new_s(mod, io, rtd,
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
968 ret = rsnd_kctrl_new_s(mod, io, rtd,
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
978 static struct rsnd_mod_ops rsnd_src_gen2_ops = {
979 .name = SRC_NAME,
980 .dma_req = rsnd_src_dma_req,
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,
987 .hw_params = rsnd_src_hw_params,
988 .pcm_new = rsnd_src_pcm_new,
989 };
990
991 struct rsnd_mod *rsnd_src_mod_get(struct rsnd_priv *priv, int id)
992 {
993 if (WARN_ON(id < 0 || id >= rsnd_src_nr(priv)))
994 id = 0;
995
996 return &((struct rsnd_src *)(priv->src) + id)->mod;
997 }
998
999 static 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;
1004 struct device_node *np;
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;
1008 int nr, i;
1009
1010 if (!of_data)
1011 return;
1012
1013 src_node = rsnd_src_of_node(priv);
1014 if (!src_node)
1015 return;
1016
1017 nr = of_get_child_count(src_node);
1018 if (!nr)
1019 goto rsnd_of_parse_src_end;
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");
1026 goto rsnd_of_parse_src_end;
1027 }
1028
1029 info->src_info = src_info;
1030 info->src_info_nr = nr;
1031
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
1039 rsnd_of_parse_src_end:
1040 of_node_put(src_node);
1041 }
1042
1043 int rsnd_src_probe(struct platform_device *pdev,
1044 const struct rsnd_of_data *of_data,
1045 struct rsnd_priv *priv)
1046 {
1047 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
1048 struct device *dev = rsnd_priv_to_dev(priv);
1049 struct rsnd_src *src;
1050 struct rsnd_mod_ops *ops;
1051 struct clk *clk;
1052 char name[RSND_SRC_NAME_SIZE];
1053 int i, nr, ret;
1054
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
1065 rsnd_of_parse_src(pdev, of_data, priv);
1066
1067 /*
1068 * init SRC
1069 */
1070 nr = info->src_info_nr;
1071 if (!nr)
1072 return 0;
1073
1074 src = devm_kzalloc(dev, sizeof(*src) * nr, GFP_KERNEL);
1075 if (!src)
1076 return -ENOMEM;
1077
1078 priv->src_nr = nr;
1079 priv->src = src;
1080
1081 for_each_rsnd_src(src, priv, i) {
1082 snprintf(name, RSND_SRC_NAME_SIZE, "%s.%d",
1083 SRC_NAME, i);
1084
1085 clk = devm_clk_get(dev, name);
1086 if (IS_ERR(clk))
1087 return PTR_ERR(clk);
1088
1089 src->info = &info->src_info[i];
1090
1091 ret = rsnd_mod_init(priv, &src->mod, ops, clk, RSND_MOD_SRC, i);
1092 if (ret)
1093 return ret;
1094 }
1095
1096 return 0;
1097 }
1098
1099 void 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.072297 seconds and 5 git commands to generate.