1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include "rsnd.h"
26
27#define SRC_NAME "src"
28
29
30#define OUF_SRC(id) ((1 << (id + 16)) | (1 << id))
31
32struct rsnd_src {
33 struct rsnd_mod mod;
34 struct rsnd_mod *dma;
35 struct rsnd_kctrl_cfg_s sen;
36 struct rsnd_kctrl_cfg_s sync;
37 int irq;
38};
39
40#define RSND_SRC_NAME_SIZE 16
41
42#define rsnd_src_get(priv, id) ((struct rsnd_src *)(priv->src) + id)
43#define rsnd_src_nr(priv) ((priv)->src_nr)
44#define rsnd_src_sync_is_enabled(mod) (rsnd_mod_to_src(mod)->sen.val)
45
46#define rsnd_mod_to_src(_mod) \
47 container_of((_mod), struct rsnd_src, mod)
48
49#define for_each_rsnd_src(pos, priv, i) \
50 for ((i) = 0; \
51 ((i) < rsnd_src_nr(priv)) && \
52 ((pos) = (struct rsnd_src *)(priv)->src + i); \
53 i++)
54
55
56
57
58
59
60
61
62
63
64
65
66static void rsnd_src_activation(struct rsnd_mod *mod)
67{
68 rsnd_mod_write(mod, SRC_SWRSR, 0);
69 rsnd_mod_write(mod, SRC_SWRSR, 1);
70}
71
72static void rsnd_src_halt(struct rsnd_mod *mod)
73{
74 rsnd_mod_write(mod, SRC_SRCIR, 1);
75 rsnd_mod_write(mod, SRC_SWRSR, 0);
76}
77
78static struct dma_chan *rsnd_src_dma_req(struct rsnd_dai_stream *io,
79 struct rsnd_mod *mod)
80{
81 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
82 int is_play = rsnd_io_is_play(io);
83
84 return rsnd_dma_request_channel(rsnd_src_of_node(priv),
85 mod,
86 is_play ? "rx" : "tx");
87}
88
89static u32 rsnd_src_convert_rate(struct rsnd_dai_stream *io,
90 struct rsnd_mod *mod)
91{
92 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
93 struct rsnd_src *src = rsnd_mod_to_src(mod);
94 u32 convert_rate;
95
96 if (!runtime)
97 return 0;
98
99 if (!rsnd_src_sync_is_enabled(mod))
100 return rsnd_io_converted_rate(io);
101
102 convert_rate = src->sync.val;
103
104 if (!convert_rate)
105 convert_rate = rsnd_io_converted_rate(io);
106
107 if (!convert_rate)
108 convert_rate = runtime->rate;
109
110 return convert_rate;
111}
112
113unsigned int rsnd_src_get_rate(struct rsnd_priv *priv,
114 struct rsnd_dai_stream *io,
115 int is_in)
116{
117 struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
118 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
119 unsigned int rate = 0;
120 int is_play = rsnd_io_is_play(io);
121
122
123
124
125
126
127
128
129
130 if (is_play == is_in)
131 return runtime->rate;
132
133
134
135
136
137 if (src_mod)
138 rate = rsnd_src_convert_rate(io, src_mod);
139
140 if (!rate)
141 rate = runtime->rate;
142
143 return rate;
144}
145
146static const u32 bsdsr_table_pattern1[] = {
147 0x01800000,
148 0x01000000,
149 0x00c00000,
150 0x00800000,
151 0x00600000,
152 0x00400000,
153};
154
155static const u32 bsdsr_table_pattern2[] = {
156 0x02400000,
157 0x01800000,
158 0x01200000,
159 0x00c00000,
160 0x00900000,
161 0x00600000,
162};
163
164static const u32 bsisr_table[] = {
165 0x00100060,
166 0x00100040,
167 0x00100030,
168 0x00100020,
169 0x00100020,
170 0x00100020,
171};
172
173static const u32 chan288888[] = {
174 0x00000006,
175 0x000001fe,
176 0x000001fe,
177 0x000001fe,
178 0x000001fe,
179 0x000001fe,
180};
181
182static const u32 chan244888[] = {
183 0x00000006,
184 0x0000001e,
185 0x0000001e,
186 0x000001fe,
187 0x000001fe,
188 0x000001fe,
189};
190
191static const u32 chan222222[] = {
192 0x00000006,
193 0x00000006,
194 0x00000006,
195 0x00000006,
196 0x00000006,
197 0x00000006,
198};
199
200static void rsnd_src_set_convert_rate(struct rsnd_dai_stream *io,
201 struct rsnd_mod *mod)
202{
203 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
204 struct device *dev = rsnd_priv_to_dev(priv);
205 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
206 int is_play = rsnd_io_is_play(io);
207 int use_src = 0;
208 u32 fin, fout;
209 u32 ifscr, fsrate, adinr;
210 u32 cr, route;
211 u32 i_busif, o_busif, tmp;
212 const u32 *bsdsr_table;
213 const u32 *chptn;
214 uint ratio;
215 int chan;
216 int idx;
217
218 if (!runtime)
219 return;
220
221 fin = rsnd_src_get_in_rate(priv, io);
222 fout = rsnd_src_get_out_rate(priv, io);
223
224 chan = rsnd_runtime_channel_original(io);
225
226
227 if (fin == fout)
228 ratio = 0;
229 else if (fin > fout)
230 ratio = 100 * fin / fout;
231 else
232 ratio = 100 * fout / fin;
233
234 if (ratio > 600) {
235 dev_err(dev, "FSO/FSI ratio error\n");
236 return;
237 }
238
239 use_src = (fin != fout) | rsnd_src_sync_is_enabled(mod);
240
241
242
243
244 adinr = rsnd_get_adinr_bit(mod, io) | chan;
245
246
247
248
249 ifscr = 0;
250 fsrate = 0;
251 if (use_src) {
252 u64 n;
253
254 ifscr = 1;
255 n = (u64)0x0400000 * fin;
256 do_div(n, fout);
257 fsrate = n;
258 }
259
260
261
262
263 cr = 0x00011110;
264 route = 0x0;
265 if (use_src) {
266 route = 0x1;
267
268 if (rsnd_src_sync_is_enabled(mod)) {
269 cr |= 0x1;
270 route |= rsnd_io_is_play(io) ?
271 (0x1 << 24) : (0x1 << 25);
272 }
273 }
274
275
276
277
278
279
280
281
282 switch (rsnd_mod_id(mod)) {
283 case 0:
284 chptn = chan288888;
285 bsdsr_table = bsdsr_table_pattern1;
286 break;
287 case 1:
288 case 3:
289 case 4:
290 chptn = chan244888;
291 bsdsr_table = bsdsr_table_pattern1;
292 break;
293 case 2:
294 case 9:
295 chptn = chan222222;
296 bsdsr_table = bsdsr_table_pattern1;
297 break;
298 case 5:
299 case 6:
300 case 7:
301 case 8:
302 chptn = chan222222;
303 bsdsr_table = bsdsr_table_pattern2;
304 break;
305 default:
306 goto convert_rate_err;
307 }
308
309
310
311
312 if (rsnd_is_e3(priv))
313 switch (rsnd_mod_id(mod)) {
314 case 0:
315 case 4:
316 chptn = chan222222;
317 }
318
319 for (idx = 0; idx < ARRAY_SIZE(chan222222); idx++)
320 if (chptn[idx] & (1 << chan))
321 break;
322
323 if (chan > 8 ||
324 idx >= ARRAY_SIZE(chan222222))
325 goto convert_rate_err;
326
327
328 tmp = rsnd_get_busif_shift(io, mod);
329 i_busif = ( is_play ? tmp : 0) | 1;
330 o_busif = (!is_play ? tmp : 0) | 1;
331
332 rsnd_mod_write(mod, SRC_ROUTE_MODE0, route);
333
334 rsnd_mod_write(mod, SRC_SRCIR, 1);
335 rsnd_mod_write(mod, SRC_ADINR, adinr);
336 rsnd_mod_write(mod, SRC_IFSCR, ifscr);
337 rsnd_mod_write(mod, SRC_IFSVR, fsrate);
338 rsnd_mod_write(mod, SRC_SRCCR, cr);
339 rsnd_mod_write(mod, SRC_BSDSR, bsdsr_table[idx]);
340 rsnd_mod_write(mod, SRC_BSISR, bsisr_table[idx]);
341 rsnd_mod_write(mod, SRC_SRCIR, 0);
342
343 rsnd_mod_write(mod, SRC_I_BUSIF_MODE, i_busif);
344 rsnd_mod_write(mod, SRC_O_BUSIF_MODE, o_busif);
345
346 rsnd_mod_write(mod, SRC_BUSIF_DALIGN, rsnd_get_dalign(mod, io));
347
348 rsnd_adg_set_src_timesel_gen2(mod, io, fin, fout);
349
350 return;
351
352convert_rate_err:
353 dev_err(dev, "unknown BSDSR/BSDIR settings\n");
354}
355
356static int rsnd_src_irq(struct rsnd_mod *mod,
357 struct rsnd_dai_stream *io,
358 struct rsnd_priv *priv,
359 int enable)
360{
361 struct rsnd_src *src = rsnd_mod_to_src(mod);
362 u32 sys_int_val, int_val, sys_int_mask;
363 int irq = src->irq;
364 int id = rsnd_mod_id(mod);
365
366 sys_int_val =
367 sys_int_mask = OUF_SRC(id);
368 int_val = 0x3300;
369
370
371
372
373
374
375 if ((irq <= 0) || !enable) {
376 sys_int_val = 0;
377 int_val = 0;
378 }
379
380
381
382
383
384
385 if (rsnd_src_sync_is_enabled(mod))
386 sys_int_val = sys_int_val & 0xffff;
387
388 rsnd_mod_write(mod, SRC_INT_ENABLE0, int_val);
389 rsnd_mod_bset(mod, SCU_SYS_INT_EN0, sys_int_mask, sys_int_val);
390 rsnd_mod_bset(mod, SCU_SYS_INT_EN1, sys_int_mask, sys_int_val);
391
392 return 0;
393}
394
395static void rsnd_src_status_clear(struct rsnd_mod *mod)
396{
397 u32 val = OUF_SRC(rsnd_mod_id(mod));
398
399 rsnd_mod_write(mod, SCU_SYS_STATUS0, val);
400 rsnd_mod_write(mod, SCU_SYS_STATUS1, val);
401}
402
403static bool rsnd_src_error_occurred(struct rsnd_mod *mod)
404{
405 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
406 struct device *dev = rsnd_priv_to_dev(priv);
407 u32 val0, val1;
408 u32 status0, status1;
409 bool ret = false;
410
411 val0 = val1 = OUF_SRC(rsnd_mod_id(mod));
412
413
414
415
416
417
418 if (rsnd_src_sync_is_enabled(mod))
419 val0 = val0 & 0xffff;
420
421 status0 = rsnd_mod_read(mod, SCU_SYS_STATUS0);
422 status1 = rsnd_mod_read(mod, SCU_SYS_STATUS1);
423 if ((status0 & val0) || (status1 & val1)) {
424 rsnd_dbg_irq_status(dev, "%s err status : 0x%08x, 0x%08x\n",
425 rsnd_mod_name(mod), status0, status1);
426
427 ret = true;
428 }
429
430 return ret;
431}
432
433static int rsnd_src_start(struct rsnd_mod *mod,
434 struct rsnd_dai_stream *io,
435 struct rsnd_priv *priv)
436{
437 u32 val;
438
439
440
441
442
443
444 val = (rsnd_io_to_mod_dvc(io) && !rsnd_src_sync_is_enabled(mod)) ?
445 0x01 : 0x11;
446
447 rsnd_mod_write(mod, SRC_CTRL, val);
448
449 return 0;
450}
451
452static int rsnd_src_stop(struct rsnd_mod *mod,
453 struct rsnd_dai_stream *io,
454 struct rsnd_priv *priv)
455{
456 rsnd_mod_write(mod, SRC_CTRL, 0);
457
458 return 0;
459}
460
461static int rsnd_src_init(struct rsnd_mod *mod,
462 struct rsnd_dai_stream *io,
463 struct rsnd_priv *priv)
464{
465 struct rsnd_src *src = rsnd_mod_to_src(mod);
466
467
468 src->sync.val = 0;
469
470 rsnd_mod_power_on(mod);
471
472 rsnd_src_activation(mod);
473
474 rsnd_src_set_convert_rate(io, mod);
475
476 rsnd_src_status_clear(mod);
477
478 return 0;
479}
480
481static int rsnd_src_quit(struct rsnd_mod *mod,
482 struct rsnd_dai_stream *io,
483 struct rsnd_priv *priv)
484{
485 struct rsnd_src *src = rsnd_mod_to_src(mod);
486
487 rsnd_src_halt(mod);
488
489 rsnd_mod_power_off(mod);
490
491
492 src->sync.val = 0;
493
494 return 0;
495}
496
497static void __rsnd_src_interrupt(struct rsnd_mod *mod,
498 struct rsnd_dai_stream *io)
499{
500 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
501 bool stop = false;
502
503 spin_lock(&priv->lock);
504
505
506 if (!rsnd_io_is_working(io))
507 goto rsnd_src_interrupt_out;
508
509 if (rsnd_src_error_occurred(mod))
510 stop = true;
511
512 rsnd_src_status_clear(mod);
513rsnd_src_interrupt_out:
514
515 spin_unlock(&priv->lock);
516
517 if (stop)
518 snd_pcm_stop_xrun(io->substream);
519}
520
521static irqreturn_t rsnd_src_interrupt(int irq, void *data)
522{
523 struct rsnd_mod *mod = data;
524
525 rsnd_mod_interrupt(mod, __rsnd_src_interrupt);
526
527 return IRQ_HANDLED;
528}
529
530static int rsnd_src_probe_(struct rsnd_mod *mod,
531 struct rsnd_dai_stream *io,
532 struct rsnd_priv *priv)
533{
534 struct rsnd_src *src = rsnd_mod_to_src(mod);
535 struct device *dev = rsnd_priv_to_dev(priv);
536 int irq = src->irq;
537 int ret;
538
539 if (irq > 0) {
540
541
542
543
544
545 ret = devm_request_irq(dev, irq,
546 rsnd_src_interrupt,
547 IRQF_SHARED,
548 dev_name(dev), mod);
549 if (ret)
550 return ret;
551 }
552
553 ret = rsnd_dma_attach(io, mod, &src->dma);
554
555 return ret;
556}
557
558static int rsnd_src_pcm_new(struct rsnd_mod *mod,
559 struct rsnd_dai_stream *io,
560 struct snd_soc_pcm_runtime *rtd)
561{
562 struct rsnd_src *src = rsnd_mod_to_src(mod);
563 int ret;
564
565
566
567
568
569
570
571
572
573 if (rsnd_io_to_mod_cmd(io) && !rsnd_io_is_play(io))
574 return 0;
575
576
577
578
579 ret = rsnd_kctrl_new_s(mod, io, rtd,
580 rsnd_io_is_play(io) ?
581 "SRC Out Rate Switch" :
582 "SRC In Rate Switch",
583 rsnd_kctrl_accept_anytime,
584 rsnd_src_set_convert_rate,
585 &src->sen, 1);
586 if (ret < 0)
587 return ret;
588
589 ret = rsnd_kctrl_new_s(mod, io, rtd,
590 rsnd_io_is_play(io) ?
591 "SRC Out Rate" :
592 "SRC In Rate",
593 rsnd_kctrl_accept_runtime,
594 rsnd_src_set_convert_rate,
595 &src->sync, 192000);
596
597 return ret;
598}
599
600static struct rsnd_mod_ops rsnd_src_ops = {
601 .name = SRC_NAME,
602 .dma_req = rsnd_src_dma_req,
603 .probe = rsnd_src_probe_,
604 .init = rsnd_src_init,
605 .quit = rsnd_src_quit,
606 .start = rsnd_src_start,
607 .stop = rsnd_src_stop,
608 .irq = rsnd_src_irq,
609 .pcm_new = rsnd_src_pcm_new,
610 .get_status = rsnd_mod_get_status,
611};
612
613struct rsnd_mod *rsnd_src_mod_get(struct rsnd_priv *priv, int id)
614{
615 if (WARN_ON(id < 0 || id >= rsnd_src_nr(priv)))
616 id = 0;
617
618 return rsnd_mod_get(rsnd_src_get(priv, id));
619}
620
621int rsnd_src_probe(struct rsnd_priv *priv)
622{
623 struct device_node *node;
624 struct device_node *np;
625 struct device *dev = rsnd_priv_to_dev(priv);
626 struct rsnd_src *src;
627 struct clk *clk;
628 char name[RSND_SRC_NAME_SIZE];
629 int i, nr, ret;
630
631
632 if (rsnd_is_gen1(priv))
633 return 0;
634
635 node = rsnd_src_of_node(priv);
636 if (!node)
637 return 0;
638
639 nr = of_get_child_count(node);
640 if (!nr) {
641 ret = -EINVAL;
642 goto rsnd_src_probe_done;
643 }
644
645 src = devm_kcalloc(dev, nr, sizeof(*src), GFP_KERNEL);
646 if (!src) {
647 ret = -ENOMEM;
648 goto rsnd_src_probe_done;
649 }
650
651 priv->src_nr = nr;
652 priv->src = src;
653
654 i = 0;
655 for_each_child_of_node(node, np) {
656 if (!of_device_is_available(np))
657 goto skip;
658
659 src = rsnd_src_get(priv, i);
660
661 snprintf(name, RSND_SRC_NAME_SIZE, "%s.%d",
662 SRC_NAME, i);
663
664 src->irq = irq_of_parse_and_map(np, 0);
665 if (!src->irq) {
666 ret = -EINVAL;
667 of_node_put(np);
668 goto rsnd_src_probe_done;
669 }
670
671 clk = devm_clk_get(dev, name);
672 if (IS_ERR(clk)) {
673 ret = PTR_ERR(clk);
674 of_node_put(np);
675 goto rsnd_src_probe_done;
676 }
677
678 ret = rsnd_mod_init(priv, rsnd_mod_get(src),
679 &rsnd_src_ops, clk, RSND_MOD_SRC, i);
680 if (ret) {
681 of_node_put(np);
682 goto rsnd_src_probe_done;
683 }
684
685skip:
686 i++;
687 }
688
689 ret = 0;
690
691rsnd_src_probe_done:
692 of_node_put(node);
693
694 return ret;
695}
696
697void rsnd_src_remove(struct rsnd_priv *priv)
698{
699 struct rsnd_src *src;
700 int i;
701
702 for_each_rsnd_src(src, priv, i) {
703 rsnd_mod_quit(rsnd_mod_get(src));
704 }
705}
706