ASoC: rsnd: use mod base common method on DMA phase3
[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_mod *dma;
26 struct rsnd_kctrl_cfg_s sen; /* sync convert enable */
27 struct rsnd_kctrl_cfg_s sync; /* sync convert */
28 u32 convert_rate; /* sampling rate convert */
29 int err;
30 };
31
32 #define RSND_SRC_NAME_SIZE 16
33
34 #define rsnd_src_to_dma(src) ((src)->dma)
35 #define rsnd_src_nr(priv) ((priv)->src_nr)
36 #define rsnd_enable_sync_convert(src) ((src)->sen.val)
37 #define rsnd_src_of_node(priv) \
38 of_get_child_by_name(rsnd_priv_to_dev(priv)->of_node, "rcar_sound,src")
39
40 #define rsnd_mod_to_src(_mod) \
41 container_of((_mod), struct rsnd_src, mod)
42
43 #define for_each_rsnd_src(pos, priv, i) \
44 for ((i) = 0; \
45 ((i) < rsnd_src_nr(priv)) && \
46 ((pos) = (struct rsnd_src *)(priv)->src + i); \
47 i++)
48
49
50 /*
51 * image of SRC (Sampling Rate Converter)
52 *
53 * 96kHz <-> +-----+ 48kHz +-----+ 48kHz +-------+
54 * 48kHz <-> | SRC | <------> | SSI | <-----> | codec |
55 * 44.1kHz <-> +-----+ +-----+ +-------+
56 * ...
57 *
58 */
59
60 /*
61 * src.c is caring...
62 *
63 * Gen1
64 *
65 * [mem] -> [SRU] -> [SSI]
66 * |--------|
67 *
68 * Gen2
69 *
70 * [mem] -> [SRC] -> [SSIU] -> [SSI]
71 * |-----------------|
72 */
73
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
86 * struct rsnd_src_platform_info {
87 * u32 convert_rate;
88 * int dma_id;
89 * }
90 *
91 * rsnd_src_convert_rate() indicates
92 * above convert_rate, and it controls
93 * whether SRC is used or not.
94 *
95 * ex) doesn't use SRC
96 * static struct rsnd_dai_platform_info rsnd_dai = {
97 * .playback = { .ssi = &rsnd_ssi[0], },
98 * };
99 *
100 * ex) uses SRC
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] },
107 * };
108 *
109 * ex) uses SRC bypass mode
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] },
116 * };
117 *
118 */
119
120 /*
121 * Gen1/Gen2 common functions
122 */
123 static 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
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)
132 static void __rsnd_src_initialize_lock(struct rsnd_mod *mod, u32 enable)
133 {
134 rsnd_mod_write(mod, SRC_SRCIR, enable);
135 }
136
137 static struct dma_chan *rsnd_src_dma_req(struct rsnd_dai_stream *io,
138 struct rsnd_mod *mod)
139 {
140 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
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
148 int rsnd_src_ssiu_start(struct rsnd_mod *ssi_mod,
149 struct rsnd_dai_stream *io,
150 int use_busif)
151 {
152 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
153 int ssi_id = rsnd_mod_id(ssi_mod);
154
155 /*
156 * SSI_MODE0
157 */
158 rsnd_mod_bset(ssi_mod, SSI_MODE0, (1 << ssi_id),
159 !use_busif << ssi_id);
160
161 /*
162 * SSI_MODE1
163 */
164 if (rsnd_ssi_is_pin_sharing(io)) {
165 int shift = -1;
166 switch (ssi_id) {
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)
179 rsnd_mod_bset(ssi_mod, SSI_MODE1,
180 0x3 << shift,
181 rsnd_rdai_is_clk_master(rdai) ?
182 0x2 << shift : 0x1 << shift);
183 }
184
185 /*
186 * DMA settings for SSIU
187 */
188 if (use_busif) {
189 u32 val = rsnd_get_dalign(ssi_mod, io);
190
191 rsnd_mod_write(ssi_mod, SSI_BUSIF_ADINR,
192 rsnd_get_adinr_bit(ssi_mod, io));
193 rsnd_mod_write(ssi_mod, SSI_BUSIF_MODE, 1);
194 rsnd_mod_write(ssi_mod, SSI_CTRL, 0x1);
195
196 rsnd_mod_write(ssi_mod, SSI_BUSIF_DALIGN, val);
197 }
198
199 return 0;
200 }
201
202 int rsnd_src_ssiu_stop(struct rsnd_mod *ssi_mod,
203 struct rsnd_dai_stream *io)
204 {
205 /*
206 * DMA settings for SSIU
207 */
208 rsnd_mod_write(ssi_mod, SSI_CTRL, 0);
209
210 return 0;
211 }
212
213 static u32 rsnd_src_convert_rate(struct rsnd_dai_stream *io,
214 struct rsnd_src *src)
215 {
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
236 unsigned int rsnd_src_get_ssi_rate(struct rsnd_priv *priv,
237 struct rsnd_dai_stream *io,
238 struct snd_pcm_runtime *runtime)
239 {
240 struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
241 struct rsnd_src *src;
242 unsigned int rate = 0;
243
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 */
251 rate = rsnd_src_convert_rate(io, src);
252 }
253
254 if (!rate)
255 rate = runtime->rate;
256
257 return rate;
258 }
259
260 static int rsnd_src_set_convert_rate(struct rsnd_mod *mod,
261 struct rsnd_dai_stream *io)
262 {
263 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
264 struct rsnd_src *src = rsnd_mod_to_src(mod);
265 u32 convert_rate = rsnd_src_convert_rate(io, src);
266 u32 fsrate = 0;
267
268 if (convert_rate)
269 fsrate = 0x0400000 / convert_rate * runtime->rate;
270
271 /* Set channel number and output bit length */
272 rsnd_mod_write(mod, SRC_ADINR, rsnd_get_adinr_bit(mod, io));
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
288 static int rsnd_src_hw_params(struct rsnd_mod *mod,
289 struct rsnd_dai_stream *io,
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
324 static int rsnd_src_init(struct rsnd_mod *mod,
325 struct rsnd_priv *priv)
326 {
327 struct rsnd_src *src = rsnd_mod_to_src(mod);
328
329 rsnd_mod_power_on(mod);
330
331 rsnd_src_soft_reset(mod);
332
333 rsnd_src_initialize_lock(mod);
334
335 src->err = 0;
336
337 /* reset sync convert_rate */
338 src->sync.val = 0;
339
340 return 0;
341 }
342
343 static int rsnd_src_quit(struct rsnd_mod *mod,
344 struct rsnd_dai_stream *io,
345 struct rsnd_priv *priv)
346 {
347 struct rsnd_src *src = rsnd_mod_to_src(mod);
348 struct device *dev = rsnd_priv_to_dev(priv);
349
350 rsnd_mod_power_off(mod);
351
352 if (src->err)
353 dev_warn(dev, "%s[%d] under/over flow err = %d\n",
354 rsnd_mod_name(mod), rsnd_mod_id(mod), src->err);
355
356 src->convert_rate = 0;
357
358 /* reset sync convert_rate */
359 src->sync.val = 0;
360
361 return 0;
362 }
363
364 static int rsnd_src_start(struct rsnd_mod *mod)
365 {
366 rsnd_src_initialize_unlock(mod);
367
368 return 0;
369 }
370
371 static int rsnd_src_stop(struct rsnd_mod *mod)
372 {
373 /* nothing to do */
374 return 0;
375 }
376
377 /*
378 * Gen1 functions
379 */
380 static int rsnd_src_set_route_gen1(struct rsnd_dai_stream *io,
381 struct rsnd_mod *mod)
382 {
383 struct src_route_config {
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 };
397 u32 mask;
398 u32 val;
399 int id;
400
401 id = rsnd_mod_id(mod);
402 if (id < 0 || id >= ARRAY_SIZE(routes))
403 return -EIO;
404
405 /*
406 * SRC_ROUTE_SELECT
407 */
408 val = rsnd_io_is_play(io) ? 0x1 : 0x2;
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
414 return 0;
415 }
416
417 static int rsnd_src_set_convert_timing_gen1(struct rsnd_dai_stream *io,
418 struct rsnd_mod *mod)
419 {
420 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
421 struct rsnd_src *src = rsnd_mod_to_src(mod);
422 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
423 u32 convert_rate = rsnd_src_convert_rate(io, src);
424 u32 mask;
425 u32 val;
426 int shift;
427 int id = rsnd_mod_id(mod);
428 int ret;
429
430 /*
431 * SRC_TIMING_SELECT
432 */
433 shift = (id % 4) * 8;
434 mask = 0x1F << shift;
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 */
442 ret = 0;
443 if (convert_rate) {
444 /* use ADG */
445 val = 0;
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 */
451 val = id << shift;
452 } else {
453 /* use SSI WS */
454 val = (id + 1) << shift;
455 }
456
457 if (ret < 0)
458 return ret;
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
475 static int rsnd_src_set_convert_rate_gen1(struct rsnd_mod *mod,
476 struct rsnd_dai_stream *io)
477 {
478 struct rsnd_src *src = rsnd_mod_to_src(mod);
479 int ret;
480
481 ret = rsnd_src_set_convert_rate(mod, io);
482 if (ret < 0)
483 return ret;
484
485 /* Select SRC mode (fixed value) */
486 rsnd_mod_write(mod, SRC_SRCCR, 0x00010110);
487
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);
491
492 /* Gen1/Gen2 are not compatible */
493 if (rsnd_src_convert_rate(io, src))
494 rsnd_mod_write(mod, SRC_ROUTE_MODE0, 1);
495
496 /* no SRC_BFSSR settings, since SRC_SRCCR::BUFMD is 0 */
497
498 return 0;
499 }
500
501 static int rsnd_src_init_gen1(struct rsnd_mod *mod,
502 struct rsnd_dai_stream *io,
503 struct rsnd_priv *priv)
504 {
505 int ret;
506
507 ret = rsnd_src_init(mod, priv);
508 if (ret < 0)
509 return ret;
510
511 ret = rsnd_src_set_route_gen1(io, mod);
512 if (ret < 0)
513 return ret;
514
515 ret = rsnd_src_set_convert_rate_gen1(mod, io);
516 if (ret < 0)
517 return ret;
518
519 ret = rsnd_src_set_convert_timing_gen1(io, mod);
520 if (ret < 0)
521 return ret;
522
523 return 0;
524 }
525
526 static int rsnd_src_start_gen1(struct rsnd_mod *mod,
527 struct rsnd_dai_stream *io,
528 struct rsnd_priv *priv)
529 {
530 int id = rsnd_mod_id(mod);
531
532 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), (1 << id));
533
534 return rsnd_src_start(mod);
535 }
536
537 static int rsnd_src_stop_gen1(struct rsnd_mod *mod,
538 struct rsnd_dai_stream *io,
539 struct rsnd_priv *priv)
540 {
541 int id = rsnd_mod_id(mod);
542
543 rsnd_mod_bset(mod, SRC_ROUTE_CTRL, (1 << id), 0);
544
545 return rsnd_src_stop(mod);
546 }
547
548 static struct rsnd_mod_ops rsnd_src_gen1_ops = {
549 .name = SRC_NAME,
550 .dma_req = rsnd_src_dma_req,
551 .init = rsnd_src_init_gen1,
552 .quit = rsnd_src_quit,
553 .start = rsnd_src_start_gen1,
554 .stop = rsnd_src_stop_gen1,
555 .hw_params = rsnd_src_hw_params,
556 };
557
558 /*
559 * Gen2 functions
560 */
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)
563 static 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
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
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
597 static 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
605 static bool rsnd_src_error_record_gen2(struct rsnd_mod *mod)
606 {
607 struct rsnd_src *src = rsnd_mod_to_src(mod);
608 u32 val0, val1;
609 bool ret = false;
610
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)) {
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
635 static int rsnd_src_start_gen2(struct rsnd_mod *mod,
636 struct rsnd_dai_stream *io,
637 struct rsnd_priv *priv)
638 {
639 struct rsnd_src *src = rsnd_mod_to_src(mod);
640 u32 val;
641
642 val = rsnd_get_dalign(mod, io);
643
644 rsnd_mod_write(mod, SRC_BUSIF_DALIGN, val);
645
646 /*
647 * WORKAROUND
648 *
649 * Enable SRC output if you want to use sync convert together with DVC
650 */
651 val = (rsnd_io_to_mod_dvc(io) && !rsnd_enable_sync_convert(src)) ?
652 0x01 : 0x11;
653
654 rsnd_mod_write(mod, SRC_CTRL, val);
655
656 rsnd_src_error_clear_gen2(mod);
657
658 rsnd_src_start(mod);
659
660 rsnd_src_irq_enable_gen2(mod);
661
662 return 0;
663 }
664
665 static int rsnd_src_stop_gen2(struct rsnd_mod *mod,
666 struct rsnd_dai_stream *io,
667 struct rsnd_priv *priv)
668 {
669 rsnd_src_irq_disable_gen2(mod);
670
671 /*
672 * stop SRC output only
673 * see rsnd_src_quit_gen2
674 */
675 rsnd_mod_write(mod, SRC_CTRL, 0x01);
676
677 rsnd_src_error_record_gen2(mod);
678
679 return rsnd_src_stop(mod);
680 }
681
682 static int rsnd_src_quit_gen2(struct rsnd_mod *mod,
683 struct rsnd_dai_stream *io,
684 struct rsnd_priv *priv)
685 {
686 /* stop both out/in */
687 rsnd_mod_write(mod, SRC_CTRL, 0);
688
689 return 0;
690 }
691
692 static void __rsnd_src_interrupt_gen2(struct rsnd_mod *mod,
693 struct rsnd_dai_stream *io)
694 {
695 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
696 struct rsnd_src *src = rsnd_mod_to_src(mod);
697 struct device *dev = rsnd_priv_to_dev(priv);
698
699 spin_lock(&priv->lock);
700
701 /* ignore all cases if not working */
702 if (!rsnd_io_is_working(io))
703 goto rsnd_src_interrupt_gen2_out;
704
705 if (rsnd_src_error_record_gen2(mod)) {
706
707 dev_dbg(dev, "%s[%d] restart\n",
708 rsnd_mod_name(mod), rsnd_mod_id(mod));
709
710 rsnd_src_stop_gen2(mod, io, priv);
711 rsnd_src_start_gen2(mod, io, priv);
712 }
713
714 if (src->err > 1024) {
715 rsnd_src_irq_disable_gen2(mod);
716
717 dev_warn(dev, "no more %s[%d] restart\n",
718 rsnd_mod_name(mod), rsnd_mod_id(mod));
719 }
720
721 rsnd_src_interrupt_gen2_out:
722 spin_unlock(&priv->lock);
723 }
724
725 static irqreturn_t rsnd_src_interrupt_gen2(int irq, void *data)
726 {
727 struct rsnd_mod *mod = data;
728
729 rsnd_mod_interrupt(mod, __rsnd_src_interrupt_gen2);
730
731 return IRQ_HANDLED;
732 }
733
734 static int rsnd_src_set_convert_rate_gen2(struct rsnd_mod *mod,
735 struct rsnd_dai_stream *io)
736 {
737 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
738 struct device *dev = rsnd_priv_to_dev(priv);
739 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
740 struct rsnd_src *src = rsnd_mod_to_src(mod);
741 u32 convert_rate = rsnd_src_convert_rate(io, src);
742 u32 cr, route;
743 uint ratio;
744 int ret;
745
746 /* 6 - 1/6 are very enough ratio for SRC_BSDSR */
747 if (!convert_rate)
748 ratio = 0;
749 else if (convert_rate > runtime->rate)
750 ratio = 100 * convert_rate / runtime->rate;
751 else
752 ratio = 100 * runtime->rate / convert_rate;
753
754 if (ratio > 600) {
755 dev_err(dev, "FSO/FSI ratio error\n");
756 return -EINVAL;
757 }
758
759 ret = rsnd_src_set_convert_rate(mod, io);
760 if (ret < 0)
761 return ret;
762
763 cr = 0x00011110;
764 route = 0x0;
765 if (convert_rate) {
766 route = 0x1;
767
768 if (rsnd_enable_sync_convert(src)) {
769 cr |= 0x1;
770 route |= rsnd_io_is_play(io) ?
771 (0x1 << 24) : (0x1 << 25);
772 }
773 }
774
775 rsnd_mod_write(mod, SRC_SRCCR, cr);
776 rsnd_mod_write(mod, SRC_ROUTE_MODE0, route);
777
778 switch (rsnd_mod_id(mod)) {
779 case 5:
780 case 6:
781 case 7:
782 case 8:
783 rsnd_mod_write(mod, SRC_BSDSR, 0x02400000);
784 break;
785 default:
786 rsnd_mod_write(mod, SRC_BSDSR, 0x01800000);
787 break;
788 }
789
790 rsnd_mod_write(mod, SRC_BSISR, 0x00100060);
791
792 return 0;
793 }
794
795 static int rsnd_src_set_convert_timing_gen2(struct rsnd_dai_stream *io,
796 struct rsnd_mod *mod)
797 {
798 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
799 struct rsnd_src *src = rsnd_mod_to_src(mod);
800 u32 convert_rate = rsnd_src_convert_rate(io, src);
801 int ret;
802
803 if (convert_rate)
804 ret = rsnd_adg_set_convert_clk_gen2(mod, io,
805 runtime->rate,
806 convert_rate);
807 else
808 ret = rsnd_adg_set_convert_timing_gen2(mod, io);
809
810 return ret;
811 }
812
813 static int rsnd_src_probe_gen2(struct rsnd_mod *mod,
814 struct rsnd_dai_stream *io,
815 struct rsnd_priv *priv)
816 {
817 struct rsnd_src *src = rsnd_mod_to_src(mod);
818 struct device *dev = rsnd_priv_to_dev(priv);
819 int irq = src->info->irq;
820 int ret;
821
822 if (irq > 0) {
823 /*
824 * IRQ is not supported on non-DT
825 * see
826 * rsnd_src_irq_enable_gen2()
827 */
828 ret = devm_request_irq(dev, irq,
829 rsnd_src_interrupt_gen2,
830 IRQF_SHARED,
831 dev_name(dev), mod);
832 if (ret)
833 return ret;
834 }
835
836 src->dma = rsnd_dma_attach(io, mod, src->info->dma_id);
837 if (IS_ERR(src->dma))
838 return PTR_ERR(src->dma);
839
840 return ret;
841 }
842
843 static int rsnd_src_init_gen2(struct rsnd_mod *mod,
844 struct rsnd_dai_stream *io,
845 struct rsnd_priv *priv)
846 {
847 int ret;
848
849 ret = rsnd_src_init(mod, priv);
850 if (ret < 0)
851 return ret;
852
853 ret = rsnd_src_set_convert_rate_gen2(mod, io);
854 if (ret < 0)
855 return ret;
856
857 ret = rsnd_src_set_convert_timing_gen2(io, mod);
858 if (ret < 0)
859 return ret;
860
861 return 0;
862 }
863
864 static void rsnd_src_reconvert_update(struct rsnd_dai_stream *io,
865 struct rsnd_mod *mod)
866 {
867 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
868 struct rsnd_src *src = rsnd_mod_to_src(mod);
869 u32 convert_rate = rsnd_src_convert_rate(io, src);
870 u32 fsrate;
871
872 if (!runtime)
873 return;
874
875 if (!convert_rate)
876 convert_rate = runtime->rate;
877
878 fsrate = 0x0400000 / convert_rate * runtime->rate;
879
880 /* update IFS */
881 rsnd_mod_write(mod, SRC_IFSVR, fsrate);
882 }
883
884 static int rsnd_src_pcm_new_gen2(struct rsnd_mod *mod,
885 struct rsnd_dai_stream *io,
886 struct snd_soc_pcm_runtime *rtd)
887 {
888 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
889 struct rsnd_src *src = rsnd_mod_to_src(mod);
890 int ret;
891
892 /*
893 * enable SRC sync convert if possible
894 */
895
896 /*
897 * SRC sync convert needs clock master
898 */
899 if (!rsnd_rdai_is_clk_master(rdai))
900 return 0;
901
902 /*
903 * enable sync convert
904 */
905 ret = rsnd_kctrl_new_s(mod, io, rtd,
906 rsnd_io_is_play(io) ?
907 "SRC Out Rate Switch" :
908 "SRC In Rate Switch",
909 rsnd_src_reconvert_update,
910 &src->sen, 1);
911 if (ret < 0)
912 return ret;
913
914 ret = rsnd_kctrl_new_s(mod, io, rtd,
915 rsnd_io_is_play(io) ?
916 "SRC Out Rate" :
917 "SRC In Rate",
918 rsnd_src_reconvert_update,
919 &src->sync, 192000);
920
921 return ret;
922 }
923
924 static struct rsnd_mod_ops rsnd_src_gen2_ops = {
925 .name = SRC_NAME,
926 .dma_req = rsnd_src_dma_req,
927 .probe = rsnd_src_probe_gen2,
928 .init = rsnd_src_init_gen2,
929 .quit = rsnd_src_quit_gen2,
930 .start = rsnd_src_start_gen2,
931 .stop = rsnd_src_stop_gen2,
932 .hw_params = rsnd_src_hw_params,
933 .pcm_new = rsnd_src_pcm_new_gen2,
934 };
935
936 struct rsnd_mod *rsnd_src_mod_get(struct rsnd_priv *priv, int id)
937 {
938 if (WARN_ON(id < 0 || id >= rsnd_src_nr(priv)))
939 id = 0;
940
941 return rsnd_mod_get((struct rsnd_src *)(priv->src) + id);
942 }
943
944 static void rsnd_of_parse_src(struct platform_device *pdev,
945 const struct rsnd_of_data *of_data,
946 struct rsnd_priv *priv)
947 {
948 struct device_node *src_node;
949 struct device_node *np;
950 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
951 struct rsnd_src_platform_info *src_info;
952 struct device *dev = &pdev->dev;
953 int nr, i;
954
955 if (!of_data)
956 return;
957
958 src_node = rsnd_src_of_node(priv);
959 if (!src_node)
960 return;
961
962 nr = of_get_child_count(src_node);
963 if (!nr)
964 goto rsnd_of_parse_src_end;
965
966 src_info = devm_kzalloc(dev,
967 sizeof(struct rsnd_src_platform_info) * nr,
968 GFP_KERNEL);
969 if (!src_info) {
970 dev_err(dev, "src info allocation error\n");
971 goto rsnd_of_parse_src_end;
972 }
973
974 info->src_info = src_info;
975 info->src_info_nr = nr;
976
977 i = 0;
978 for_each_child_of_node(src_node, np) {
979 src_info[i].irq = irq_of_parse_and_map(np, 0);
980
981 i++;
982 }
983
984 rsnd_of_parse_src_end:
985 of_node_put(src_node);
986 }
987
988 int rsnd_src_probe(struct platform_device *pdev,
989 const struct rsnd_of_data *of_data,
990 struct rsnd_priv *priv)
991 {
992 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
993 struct device *dev = rsnd_priv_to_dev(priv);
994 struct rsnd_src *src;
995 struct rsnd_mod_ops *ops;
996 struct clk *clk;
997 char name[RSND_SRC_NAME_SIZE];
998 int i, nr, ret;
999
1000 ops = NULL;
1001 if (rsnd_is_gen1(priv)) {
1002 ops = &rsnd_src_gen1_ops;
1003 dev_warn(dev, "Gen1 support will be removed soon\n");
1004 }
1005 if (rsnd_is_gen2(priv))
1006 ops = &rsnd_src_gen2_ops;
1007 if (!ops) {
1008 dev_err(dev, "unknown Generation\n");
1009 return -EIO;
1010 }
1011
1012 rsnd_of_parse_src(pdev, of_data, priv);
1013
1014 /*
1015 * init SRC
1016 */
1017 nr = info->src_info_nr;
1018 if (!nr)
1019 return 0;
1020
1021 src = devm_kzalloc(dev, sizeof(*src) * nr, GFP_KERNEL);
1022 if (!src)
1023 return -ENOMEM;
1024
1025 priv->src_nr = nr;
1026 priv->src = src;
1027
1028 for_each_rsnd_src(src, priv, i) {
1029 snprintf(name, RSND_SRC_NAME_SIZE, "%s.%d",
1030 SRC_NAME, i);
1031
1032 clk = devm_clk_get(dev, name);
1033 if (IS_ERR(clk))
1034 return PTR_ERR(clk);
1035
1036 src->info = &info->src_info[i];
1037
1038 ret = rsnd_mod_init(priv, rsnd_mod_get(src), ops, clk, RSND_MOD_SRC, i);
1039 if (ret)
1040 return ret;
1041 }
1042
1043 return 0;
1044 }
1045
1046 void rsnd_src_remove(struct platform_device *pdev,
1047 struct rsnd_priv *priv)
1048 {
1049 struct rsnd_src *src;
1050 int i;
1051
1052 for_each_rsnd_src(src, priv, i) {
1053 rsnd_mod_quit(rsnd_mod_get(src));
1054 }
1055 }
This page took 0.168079 seconds and 5 git commands to generate.