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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93#include <linux/pm_runtime.h>
94#include "rsnd.h"
95
96#define RSND_RATES SNDRV_PCM_RATE_8000_192000
97#define RSND_FMTS (SNDRV_PCM_FMTBIT_S8 |\
98 SNDRV_PCM_FMTBIT_S16_LE |\
99 SNDRV_PCM_FMTBIT_S24_LE)
100
101static const struct of_device_id rsnd_of_match[] = {
102 { .compatible = "renesas,rcar_sound-gen1", .data = (void *)RSND_GEN1 },
103 { .compatible = "renesas,rcar_sound-gen2", .data = (void *)RSND_GEN2 },
104 { .compatible = "renesas,rcar_sound-gen3", .data = (void *)RSND_GEN3 },
105
106 { .compatible = "renesas,rcar_sound-r8a77990", .data = (void *)(RSND_GEN3 | RSND_SOC_E) },
107 {},
108};
109MODULE_DEVICE_TABLE(of, rsnd_of_match);
110
111
112
113
114void rsnd_mod_make_sure(struct rsnd_mod *mod, enum rsnd_mod_type type)
115{
116 if (mod->type != type) {
117 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
118 struct device *dev = rsnd_priv_to_dev(priv);
119
120 dev_warn(dev, "%s is not your expected module\n",
121 rsnd_mod_name(mod));
122 }
123}
124
125struct dma_chan *rsnd_mod_dma_req(struct rsnd_dai_stream *io,
126 struct rsnd_mod *mod)
127{
128 if (!mod || !mod->ops || !mod->ops->dma_req)
129 return NULL;
130
131 return mod->ops->dma_req(io, mod);
132}
133
134#define MOD_NAME_NUM 5
135#define MOD_NAME_SIZE 16
136char *rsnd_mod_name(struct rsnd_mod *mod)
137{
138 static char names[MOD_NAME_NUM][MOD_NAME_SIZE];
139 static int num;
140 char *name = names[num];
141
142 num++;
143 if (num >= MOD_NAME_NUM)
144 num = 0;
145
146
147
148
149
150
151 if ((mod)->ops->id_sub) {
152 snprintf(name, MOD_NAME_SIZE, "%s[%d%d]",
153 mod->ops->name,
154 rsnd_mod_id(mod),
155 rsnd_mod_id_sub(mod));
156 } else {
157 snprintf(name, MOD_NAME_SIZE, "%s[%d]",
158 mod->ops->name,
159 rsnd_mod_id(mod));
160 }
161
162 return name;
163}
164
165u32 *rsnd_mod_get_status(struct rsnd_mod *mod,
166 struct rsnd_dai_stream *io,
167 enum rsnd_mod_type type)
168{
169 return &mod->status;
170}
171
172int rsnd_mod_id_raw(struct rsnd_mod *mod)
173{
174 return mod->id;
175}
176
177int rsnd_mod_id(struct rsnd_mod *mod)
178{
179 if ((mod)->ops->id)
180 return (mod)->ops->id(mod);
181
182 return rsnd_mod_id_raw(mod);
183}
184
185int rsnd_mod_id_sub(struct rsnd_mod *mod)
186{
187 if ((mod)->ops->id_sub)
188 return (mod)->ops->id_sub(mod);
189
190 return 0;
191}
192
193int rsnd_mod_init(struct rsnd_priv *priv,
194 struct rsnd_mod *mod,
195 struct rsnd_mod_ops *ops,
196 struct clk *clk,
197 enum rsnd_mod_type type,
198 int id)
199{
200 int ret = clk_prepare(clk);
201
202 if (ret)
203 return ret;
204
205 mod->id = id;
206 mod->ops = ops;
207 mod->type = type;
208 mod->clk = clk;
209 mod->priv = priv;
210
211 return 0;
212}
213
214void rsnd_mod_quit(struct rsnd_mod *mod)
215{
216 clk_unprepare(mod->clk);
217 mod->clk = NULL;
218}
219
220void rsnd_mod_interrupt(struct rsnd_mod *mod,
221 void (*callback)(struct rsnd_mod *mod,
222 struct rsnd_dai_stream *io))
223{
224 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
225 struct rsnd_dai *rdai;
226 int i;
227
228 for_each_rsnd_dai(rdai, priv, i) {
229 struct rsnd_dai_stream *io = &rdai->playback;
230
231 if (mod == io->mod[mod->type])
232 callback(mod, io);
233
234 io = &rdai->capture;
235 if (mod == io->mod[mod->type])
236 callback(mod, io);
237 }
238}
239
240int rsnd_io_is_working(struct rsnd_dai_stream *io)
241{
242
243 if (io->substream)
244 return snd_pcm_running(io->substream);
245
246 return 0;
247}
248
249int rsnd_runtime_channel_original_with_params(struct rsnd_dai_stream *io,
250 struct snd_pcm_hw_params *params)
251{
252 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
253
254
255
256
257
258
259
260 if (params)
261 return params_channels(params);
262 else if (runtime)
263 return runtime->channels;
264 return 0;
265}
266
267int rsnd_runtime_channel_after_ctu_with_params(struct rsnd_dai_stream *io,
268 struct snd_pcm_hw_params *params)
269{
270 int chan = rsnd_runtime_channel_original_with_params(io, params);
271 struct rsnd_mod *ctu_mod = rsnd_io_to_mod_ctu(io);
272
273 if (ctu_mod) {
274 u32 converted_chan = rsnd_io_converted_chan(io);
275
276
277
278
279
280
281
282
283 if (rsnd_runtime_is_tdm_split(io)) {
284 struct device *dev = rsnd_priv_to_dev(rsnd_io_to_priv(io));
285
286 dev_err(dev, "CTU and TDM Split should be used\n");
287 }
288
289 if (converted_chan)
290 return converted_chan;
291 }
292
293 return chan;
294}
295
296int rsnd_channel_normalization(int chan)
297{
298 if (WARN_ON((chan > 8) || (chan < 0)))
299 return 0;
300
301
302 if (chan == 6)
303 chan = 8;
304
305 return chan;
306}
307
308int rsnd_runtime_channel_for_ssi_with_params(struct rsnd_dai_stream *io,
309 struct snd_pcm_hw_params *params)
310{
311 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
312 int chan = rsnd_io_is_play(io) ?
313 rsnd_runtime_channel_after_ctu_with_params(io, params) :
314 rsnd_runtime_channel_original_with_params(io, params);
315
316
317 if (rsnd_runtime_is_multi_ssi(io))
318 chan /= rsnd_rdai_ssi_lane_get(rdai);
319
320 return rsnd_channel_normalization(chan);
321}
322
323int rsnd_runtime_is_multi_ssi(struct rsnd_dai_stream *io)
324{
325 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
326 int lane = rsnd_rdai_ssi_lane_get(rdai);
327 int chan = rsnd_io_is_play(io) ?
328 rsnd_runtime_channel_after_ctu(io) :
329 rsnd_runtime_channel_original(io);
330
331 return (chan > 2) && (lane > 1);
332}
333
334int rsnd_runtime_is_tdm(struct rsnd_dai_stream *io)
335{
336 return rsnd_runtime_channel_for_ssi(io) >= 6;
337}
338
339int rsnd_runtime_is_tdm_split(struct rsnd_dai_stream *io)
340{
341 return !!rsnd_flags_has(io, RSND_STREAM_TDM_SPLIT);
342}
343
344
345
346
347u32 rsnd_get_adinr_bit(struct rsnd_mod *mod, struct rsnd_dai_stream *io)
348{
349 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
350 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
351 struct device *dev = rsnd_priv_to_dev(priv);
352
353 switch (snd_pcm_format_width(runtime->format)) {
354 case 8:
355 return 16 << 16;
356 case 16:
357 return 8 << 16;
358 case 24:
359 return 0 << 16;
360 }
361
362 dev_warn(dev, "not supported sample bits\n");
363
364 return 0;
365}
366
367
368
369
370u32 rsnd_get_dalign(struct rsnd_mod *mod, struct rsnd_dai_stream *io)
371{
372 static const u32 dalign_values[8] = {
373 0x76543210, 0x00000032, 0x00007654, 0x00000076,
374 0xfedcba98, 0x000000ba, 0x0000fedc, 0x000000fe,
375 };
376 int id = 0;
377 struct rsnd_mod *ssiu = rsnd_io_to_mod_ssiu(io);
378 struct rsnd_mod *target;
379 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
380 u32 dalign;
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403 if (rsnd_io_is_play(io)) {
404 struct rsnd_mod *src = rsnd_io_to_mod_src(io);
405
406 target = src ? src : ssiu;
407 } else {
408 struct rsnd_mod *cmd = rsnd_io_to_mod_cmd(io);
409
410 target = cmd ? cmd : ssiu;
411 }
412
413 if (mod == ssiu)
414 id = rsnd_mod_id_sub(mod);
415
416 dalign = dalign_values[id];
417
418 if (mod == target && snd_pcm_format_width(runtime->format) == 16) {
419
420 dalign = (dalign & 0xf0f0f0f0) >> 4 |
421 (dalign & 0x0f0f0f0f) << 4;
422 }
423
424 return dalign;
425}
426
427u32 rsnd_get_busif_shift(struct rsnd_dai_stream *io, struct rsnd_mod *mod)
428{
429 enum rsnd_mod_type playback_mods[] = {
430 RSND_MOD_SRC,
431 RSND_MOD_CMD,
432 RSND_MOD_SSIU,
433 };
434 enum rsnd_mod_type capture_mods[] = {
435 RSND_MOD_CMD,
436 RSND_MOD_SRC,
437 RSND_MOD_SSIU,
438 };
439 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
440 struct rsnd_mod *tmod = NULL;
441 enum rsnd_mod_type *mods =
442 rsnd_io_is_play(io) ?
443 playback_mods : capture_mods;
444 int i;
445
446
447
448
449
450
451
452
453
454 if (snd_pcm_format_width(runtime->format) != 24)
455 return 0;
456
457 for (i = 0; i < ARRAY_SIZE(playback_mods); i++) {
458 tmod = rsnd_io_to_mod(io, mods[i]);
459 if (tmod)
460 break;
461 }
462
463 if (tmod != mod)
464 return 0;
465
466 if (rsnd_io_is_play(io))
467 return (0 << 20) |
468 (8 << 16);
469 else
470 return (1 << 20) |
471 (8 << 16);
472}
473
474
475
476
477struct rsnd_mod *rsnd_mod_next(int *iterator,
478 struct rsnd_dai_stream *io,
479 enum rsnd_mod_type *array,
480 int array_size)
481{
482 int max = array ? array_size : RSND_MOD_MAX;
483
484 for (; *iterator < max; (*iterator)++) {
485 enum rsnd_mod_type type = (array) ? array[*iterator] : *iterator;
486 struct rsnd_mod *mod = rsnd_io_to_mod(io, type);
487
488 if (mod)
489 return mod;
490 }
491
492 return NULL;
493}
494
495static enum rsnd_mod_type rsnd_mod_sequence[][RSND_MOD_MAX] = {
496 {
497
498 RSND_MOD_AUDMAPP,
499 RSND_MOD_AUDMA,
500 RSND_MOD_DVC,
501 RSND_MOD_MIX,
502 RSND_MOD_CTU,
503 RSND_MOD_CMD,
504 RSND_MOD_SRC,
505 RSND_MOD_SSIU,
506 RSND_MOD_SSIM3,
507 RSND_MOD_SSIM2,
508 RSND_MOD_SSIM1,
509 RSND_MOD_SSIP,
510 RSND_MOD_SSI,
511 }, {
512
513 RSND_MOD_AUDMAPP,
514 RSND_MOD_AUDMA,
515 RSND_MOD_SSIM3,
516 RSND_MOD_SSIM2,
517 RSND_MOD_SSIM1,
518 RSND_MOD_SSIP,
519 RSND_MOD_SSI,
520 RSND_MOD_SSIU,
521 RSND_MOD_DVC,
522 RSND_MOD_MIX,
523 RSND_MOD_CTU,
524 RSND_MOD_CMD,
525 RSND_MOD_SRC,
526 },
527};
528
529static int rsnd_status_update(struct rsnd_dai_stream *io,
530 struct rsnd_mod *mod, enum rsnd_mod_type type,
531 int shift, int add, int timing)
532{
533 u32 *status = mod->ops->get_status(mod, io, type);
534 u32 mask = 0xF << shift;
535 u8 val = (*status >> shift) & 0xF;
536 u8 next_val = (val + add) & 0xF;
537 int func_call = (val == timing);
538
539
540 if (add == 0 || shift == 28)
541 return 1;
542
543 if (next_val == 0xF)
544 func_call = -1;
545 else
546 *status = (*status & ~mask) + (next_val << shift);
547
548 return func_call;
549}
550
551#define rsnd_dai_call(fn, io, param...) \
552({ \
553 struct device *dev = rsnd_priv_to_dev(rsnd_io_to_priv(io)); \
554 struct rsnd_mod *mod; \
555 int is_play = rsnd_io_is_play(io); \
556 int ret = 0, i; \
557 enum rsnd_mod_type *types = rsnd_mod_sequence[is_play]; \
558 for_each_rsnd_mod_arrays(i, mod, io, types, RSND_MOD_MAX) { \
559 int tmp = 0; \
560 int func_call = rsnd_status_update(io, mod, types[i], \
561 __rsnd_mod_shift_##fn, \
562 __rsnd_mod_add_##fn, \
563 __rsnd_mod_call_##fn); \
564 if (func_call > 0 && (mod)->ops->fn) \
565 tmp = (mod)->ops->fn(mod, io, param); \
566 if (unlikely(func_call < 0) || \
567 unlikely(tmp && (tmp != -EPROBE_DEFER))) \
568 dev_err(dev, "%s : %s error (%d, %d)\n", \
569 rsnd_mod_name(mod), #fn, tmp, func_call);\
570 ret |= tmp; \
571 } \
572 ret; \
573})
574
575int rsnd_dai_connect(struct rsnd_mod *mod,
576 struct rsnd_dai_stream *io,
577 enum rsnd_mod_type type)
578{
579 struct rsnd_priv *priv;
580 struct device *dev;
581
582 if (!mod)
583 return -EIO;
584
585 if (io->mod[type] == mod)
586 return 0;
587
588 if (io->mod[type])
589 return -EINVAL;
590
591 priv = rsnd_mod_to_priv(mod);
592 dev = rsnd_priv_to_dev(priv);
593
594 io->mod[type] = mod;
595
596 dev_dbg(dev, "%s is connected to io (%s)\n",
597 rsnd_mod_name(mod),
598 rsnd_io_is_play(io) ? "Playback" : "Capture");
599
600 return 0;
601}
602
603static void rsnd_dai_disconnect(struct rsnd_mod *mod,
604 struct rsnd_dai_stream *io,
605 enum rsnd_mod_type type)
606{
607 io->mod[type] = NULL;
608}
609
610int rsnd_rdai_channels_ctrl(struct rsnd_dai *rdai,
611 int max_channels)
612{
613 if (max_channels > 0)
614 rdai->max_channels = max_channels;
615
616 return rdai->max_channels;
617}
618
619int rsnd_rdai_ssi_lane_ctrl(struct rsnd_dai *rdai,
620 int ssi_lane)
621{
622 if (ssi_lane > 0)
623 rdai->ssi_lane = ssi_lane;
624
625 return rdai->ssi_lane;
626}
627
628int rsnd_rdai_width_ctrl(struct rsnd_dai *rdai, int width)
629{
630 if (width > 0)
631 rdai->chan_width = width;
632
633 return rdai->chan_width;
634}
635
636struct rsnd_dai *rsnd_rdai_get(struct rsnd_priv *priv, int id)
637{
638 if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
639 return NULL;
640
641 return priv->rdai + id;
642}
643
644static struct snd_soc_dai_driver
645*rsnd_daidrv_get(struct rsnd_priv *priv, int id)
646{
647 if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
648 return NULL;
649
650 return priv->daidrv + id;
651}
652
653#define rsnd_dai_to_priv(dai) snd_soc_dai_get_drvdata(dai)
654static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai)
655{
656 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
657
658 return rsnd_rdai_get(priv, dai->id);
659}
660
661
662
663
664void rsnd_dai_period_elapsed(struct rsnd_dai_stream *io)
665{
666 struct snd_pcm_substream *substream = io->substream;
667
668
669
670
671
672
673
674
675 snd_pcm_period_elapsed(substream);
676}
677
678static void rsnd_dai_stream_init(struct rsnd_dai_stream *io,
679 struct snd_pcm_substream *substream)
680{
681 io->substream = substream;
682}
683
684static void rsnd_dai_stream_quit(struct rsnd_dai_stream *io)
685{
686 io->substream = NULL;
687}
688
689static
690struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream)
691{
692 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
693
694 return asoc_rtd_to_cpu(rtd, 0);
695}
696
697static
698struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai,
699 struct snd_pcm_substream *substream)
700{
701 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
702 return &rdai->playback;
703 else
704 return &rdai->capture;
705}
706
707static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd,
708 struct snd_soc_dai *dai)
709{
710 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
711 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
712 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
713 int ret;
714 unsigned long flags;
715
716 spin_lock_irqsave(&priv->lock, flags);
717
718 switch (cmd) {
719 case SNDRV_PCM_TRIGGER_START:
720 case SNDRV_PCM_TRIGGER_RESUME:
721 ret = rsnd_dai_call(init, io, priv);
722 if (ret < 0)
723 goto dai_trigger_end;
724
725 ret = rsnd_dai_call(start, io, priv);
726 if (ret < 0)
727 goto dai_trigger_end;
728
729 ret = rsnd_dai_call(irq, io, priv, 1);
730 if (ret < 0)
731 goto dai_trigger_end;
732
733 break;
734 case SNDRV_PCM_TRIGGER_STOP:
735 case SNDRV_PCM_TRIGGER_SUSPEND:
736 ret = rsnd_dai_call(irq, io, priv, 0);
737
738 ret |= rsnd_dai_call(stop, io, priv);
739
740 ret |= rsnd_dai_call(quit, io, priv);
741
742 break;
743 default:
744 ret = -EINVAL;
745 }
746
747dai_trigger_end:
748 spin_unlock_irqrestore(&priv->lock, flags);
749
750 return ret;
751}
752
753static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
754{
755 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
756
757
758 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
759 case SND_SOC_DAIFMT_CBP_CFP:
760 rdai->clk_master = 0;
761 break;
762 case SND_SOC_DAIFMT_CBC_CFC:
763 rdai->clk_master = 1;
764 break;
765 default:
766 return -EINVAL;
767 }
768
769
770 rdai->bit_clk_inv = 0;
771 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
772 case SND_SOC_DAIFMT_I2S:
773 rdai->sys_delay = 0;
774 rdai->data_alignment = 0;
775 rdai->frm_clk_inv = 0;
776 break;
777 case SND_SOC_DAIFMT_LEFT_J:
778 case SND_SOC_DAIFMT_DSP_B:
779 rdai->sys_delay = 1;
780 rdai->data_alignment = 0;
781 rdai->frm_clk_inv = 1;
782 break;
783 case SND_SOC_DAIFMT_RIGHT_J:
784 rdai->sys_delay = 1;
785 rdai->data_alignment = 1;
786 rdai->frm_clk_inv = 1;
787 break;
788 case SND_SOC_DAIFMT_DSP_A:
789 rdai->sys_delay = 0;
790 rdai->data_alignment = 0;
791 rdai->frm_clk_inv = 1;
792 break;
793 }
794
795
796 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
797 case SND_SOC_DAIFMT_NB_IF:
798 rdai->frm_clk_inv = !rdai->frm_clk_inv;
799 break;
800 case SND_SOC_DAIFMT_IB_NF:
801 rdai->bit_clk_inv = !rdai->bit_clk_inv;
802 break;
803 case SND_SOC_DAIFMT_IB_IF:
804 rdai->bit_clk_inv = !rdai->bit_clk_inv;
805 rdai->frm_clk_inv = !rdai->frm_clk_inv;
806 break;
807 case SND_SOC_DAIFMT_NB_NF:
808 default:
809 break;
810 }
811
812 return 0;
813}
814
815static int rsnd_soc_set_dai_tdm_slot(struct snd_soc_dai *dai,
816 u32 tx_mask, u32 rx_mask,
817 int slots, int slot_width)
818{
819 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
820 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
821 struct device *dev = rsnd_priv_to_dev(priv);
822
823 switch (slot_width) {
824 case 16:
825 case 24:
826 case 32:
827 break;
828 default:
829
830 slot_width = 32;
831 }
832
833 switch (slots) {
834 case 2:
835
836 case 6:
837 case 8:
838
839 rsnd_rdai_channels_set(rdai, slots);
840 rsnd_rdai_ssi_lane_set(rdai, 1);
841 rsnd_rdai_width_set(rdai, slot_width);
842 break;
843 default:
844 dev_err(dev, "unsupported TDM slots (%d)\n", slots);
845 return -EINVAL;
846 }
847
848 return 0;
849}
850
851static unsigned int rsnd_soc_hw_channels_list[] = {
852 2, 6, 8,
853};
854
855static unsigned int rsnd_soc_hw_rate_list[] = {
856 8000,
857 11025,
858 16000,
859 22050,
860 32000,
861 44100,
862 48000,
863 64000,
864 88200,
865 96000,
866 176400,
867 192000,
868};
869
870static int rsnd_soc_hw_rule(struct rsnd_dai *rdai,
871 unsigned int *list, int list_num,
872 struct snd_interval *baseline, struct snd_interval *iv)
873{
874 struct snd_interval p;
875 unsigned int rate;
876 int i;
877
878 snd_interval_any(&p);
879 p.min = UINT_MAX;
880 p.max = 0;
881
882 for (i = 0; i < list_num; i++) {
883
884 if (!snd_interval_test(iv, list[i]))
885 continue;
886
887 rate = rsnd_ssi_clk_query(rdai,
888 baseline->min, list[i], NULL);
889 if (rate > 0) {
890 p.min = min(p.min, list[i]);
891 p.max = max(p.max, list[i]);
892 }
893
894 rate = rsnd_ssi_clk_query(rdai,
895 baseline->max, list[i], NULL);
896 if (rate > 0) {
897 p.min = min(p.min, list[i]);
898 p.max = max(p.max, list[i]);
899 }
900 }
901
902 return snd_interval_refine(iv, &p);
903}
904
905static int rsnd_soc_hw_rule_rate(struct snd_pcm_hw_params *params,
906 struct snd_pcm_hw_rule *rule)
907{
908 struct snd_interval *ic_ = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
909 struct snd_interval *ir = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
910 struct snd_interval ic;
911 struct rsnd_dai_stream *io = rule->private;
912 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
913
914
915
916
917
918
919 ic = *ic_;
920 ic.min =
921 ic.max = rsnd_runtime_channel_for_ssi_with_params(io, params);
922
923 return rsnd_soc_hw_rule(rdai, rsnd_soc_hw_rate_list,
924 ARRAY_SIZE(rsnd_soc_hw_rate_list),
925 &ic, ir);
926}
927
928static int rsnd_soc_hw_rule_channels(struct snd_pcm_hw_params *params,
929 struct snd_pcm_hw_rule *rule)
930{
931 struct snd_interval *ic_ = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
932 struct snd_interval *ir = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
933 struct snd_interval ic;
934 struct rsnd_dai_stream *io = rule->private;
935 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
936
937
938
939
940
941
942 ic = *ic_;
943 ic.min =
944 ic.max = rsnd_runtime_channel_for_ssi_with_params(io, params);
945
946 return rsnd_soc_hw_rule(rdai, rsnd_soc_hw_channels_list,
947 ARRAY_SIZE(rsnd_soc_hw_channels_list),
948 ir, &ic);
949}
950
951static const struct snd_pcm_hardware rsnd_pcm_hardware = {
952 .info = SNDRV_PCM_INFO_INTERLEAVED |
953 SNDRV_PCM_INFO_MMAP |
954 SNDRV_PCM_INFO_MMAP_VALID,
955 .buffer_bytes_max = 64 * 1024,
956 .period_bytes_min = 32,
957 .period_bytes_max = 8192,
958 .periods_min = 1,
959 .periods_max = 32,
960 .fifo_size = 256,
961};
962
963static int rsnd_soc_dai_startup(struct snd_pcm_substream *substream,
964 struct snd_soc_dai *dai)
965{
966 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
967 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
968 struct snd_pcm_hw_constraint_list *constraint = &rdai->constraint;
969 struct snd_pcm_runtime *runtime = substream->runtime;
970 unsigned int max_channels = rsnd_rdai_channels_get(rdai);
971 int i;
972
973 rsnd_dai_stream_init(io, substream);
974
975
976
977
978
979 constraint->list = rsnd_soc_hw_channels_list;
980 constraint->count = 0;
981 constraint->mask = 0;
982
983 for (i = 0; i < ARRAY_SIZE(rsnd_soc_hw_channels_list); i++) {
984 if (rsnd_soc_hw_channels_list[i] > max_channels)
985 break;
986 constraint->count = i + 1;
987 }
988
989 snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware);
990
991 snd_pcm_hw_constraint_list(runtime, 0,
992 SNDRV_PCM_HW_PARAM_CHANNELS, constraint);
993
994 snd_pcm_hw_constraint_integer(runtime,
995 SNDRV_PCM_HW_PARAM_PERIODS);
996
997
998
999
1000
1001 if (rsnd_rdai_is_clk_master(rdai)) {
1002 int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1003
1004 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1005 rsnd_soc_hw_rule_rate,
1006 is_play ? &rdai->playback : &rdai->capture,
1007 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1008 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1009 rsnd_soc_hw_rule_channels,
1010 is_play ? &rdai->playback : &rdai->capture,
1011 SNDRV_PCM_HW_PARAM_RATE, -1);
1012 }
1013
1014 return 0;
1015}
1016
1017static void rsnd_soc_dai_shutdown(struct snd_pcm_substream *substream,
1018 struct snd_soc_dai *dai)
1019{
1020 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1021 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
1022 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
1023
1024
1025
1026
1027 rsnd_dai_call(cleanup, io, priv);
1028
1029 rsnd_dai_stream_quit(io);
1030}
1031
1032static int rsnd_soc_dai_prepare(struct snd_pcm_substream *substream,
1033 struct snd_soc_dai *dai)
1034{
1035 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
1036 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1037 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
1038
1039 return rsnd_dai_call(prepare, io, priv);
1040}
1041
1042static u64 rsnd_soc_dai_formats[] = {
1043
1044
1045
1046
1047
1048
1049
1050
1051 SND_SOC_POSSIBLE_DAIFMT_I2S |
1052 SND_SOC_POSSIBLE_DAIFMT_RIGHT_J |
1053 SND_SOC_POSSIBLE_DAIFMT_LEFT_J |
1054 SND_SOC_POSSIBLE_DAIFMT_NB_NF |
1055 SND_SOC_POSSIBLE_DAIFMT_NB_IF |
1056 SND_SOC_POSSIBLE_DAIFMT_IB_NF |
1057 SND_SOC_POSSIBLE_DAIFMT_IB_IF,
1058
1059
1060
1061
1062
1063 SND_SOC_POSSIBLE_DAIFMT_DSP_A |
1064 SND_SOC_POSSIBLE_DAIFMT_DSP_B,
1065};
1066
1067static const struct snd_soc_dai_ops rsnd_soc_dai_ops = {
1068 .startup = rsnd_soc_dai_startup,
1069 .shutdown = rsnd_soc_dai_shutdown,
1070 .trigger = rsnd_soc_dai_trigger,
1071 .set_fmt = rsnd_soc_dai_set_fmt,
1072 .set_tdm_slot = rsnd_soc_set_dai_tdm_slot,
1073 .prepare = rsnd_soc_dai_prepare,
1074 .auto_selectable_formats = rsnd_soc_dai_formats,
1075 .num_auto_selectable_formats = ARRAY_SIZE(rsnd_soc_dai_formats),
1076};
1077
1078static void rsnd_parse_tdm_split_mode(struct rsnd_priv *priv,
1079 struct rsnd_dai_stream *io,
1080 struct device_node *dai_np)
1081{
1082 struct device *dev = rsnd_priv_to_dev(priv);
1083 struct device_node *ssiu_np = rsnd_ssiu_of_node(priv);
1084 struct device_node *np;
1085 int is_play = rsnd_io_is_play(io);
1086 int i;
1087
1088 if (!ssiu_np)
1089 return;
1090
1091
1092
1093
1094
1095 for (i = 0;; i++) {
1096 struct device_node *node = is_play ?
1097 of_parse_phandle(dai_np, "playback", i) :
1098 of_parse_phandle(dai_np, "capture", i);
1099
1100 if (!node)
1101 break;
1102
1103 for_each_child_of_node(ssiu_np, np) {
1104 if (np == node) {
1105 rsnd_flags_set(io, RSND_STREAM_TDM_SPLIT);
1106 dev_dbg(dev, "%s is part of TDM Split\n", io->name);
1107 }
1108 }
1109
1110 of_node_put(node);
1111 }
1112
1113 of_node_put(ssiu_np);
1114}
1115
1116static void rsnd_parse_connect_simple(struct rsnd_priv *priv,
1117 struct rsnd_dai_stream *io,
1118 struct device_node *dai_np)
1119{
1120 if (!rsnd_io_to_mod_ssi(io))
1121 return;
1122
1123 rsnd_parse_tdm_split_mode(priv, io, dai_np);
1124}
1125
1126static void rsnd_parse_connect_graph(struct rsnd_priv *priv,
1127 struct rsnd_dai_stream *io,
1128 struct device_node *endpoint)
1129{
1130 struct device *dev = rsnd_priv_to_dev(priv);
1131 struct device_node *remote_node;
1132
1133 if (!rsnd_io_to_mod_ssi(io))
1134 return;
1135
1136 remote_node = of_graph_get_remote_port_parent(endpoint);
1137
1138
1139 if (strstr(remote_node->full_name, "hdmi@fead0000")) {
1140 rsnd_flags_set(io, RSND_STREAM_HDMI0);
1141 dev_dbg(dev, "%s connected to HDMI0\n", io->name);
1142 }
1143
1144
1145 if (strstr(remote_node->full_name, "hdmi@feae0000")) {
1146 rsnd_flags_set(io, RSND_STREAM_HDMI1);
1147 dev_dbg(dev, "%s connected to HDMI1\n", io->name);
1148 }
1149
1150 rsnd_parse_tdm_split_mode(priv, io, endpoint);
1151
1152 of_node_put(remote_node);
1153}
1154
1155void rsnd_parse_connect_common(struct rsnd_dai *rdai, char *name,
1156 struct rsnd_mod* (*mod_get)(struct rsnd_priv *priv, int id),
1157 struct device_node *node,
1158 struct device_node *playback,
1159 struct device_node *capture)
1160{
1161 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
1162 struct device_node *np;
1163 int i;
1164
1165 if (!node)
1166 return;
1167
1168 i = 0;
1169 for_each_child_of_node(node, np) {
1170 struct rsnd_mod *mod;
1171
1172 i = rsnd_node_fixed_index(np, name, i);
1173
1174 mod = mod_get(priv, i);
1175
1176 if (np == playback)
1177 rsnd_dai_connect(mod, &rdai->playback, mod->type);
1178 if (np == capture)
1179 rsnd_dai_connect(mod, &rdai->capture, mod->type);
1180 i++;
1181 }
1182
1183 of_node_put(node);
1184}
1185
1186int rsnd_node_fixed_index(struct device_node *node, char *name, int idx)
1187{
1188 char node_name[16];
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206 for (; idx < 64; idx++) {
1207 snprintf(node_name, sizeof(node_name), "%s-%d", name, idx);
1208
1209 if (strncmp(node_name, of_node_full_name(node), sizeof(node_name)) == 0)
1210 return idx;
1211 }
1212
1213 return -EINVAL;
1214}
1215
1216int rsnd_node_count(struct rsnd_priv *priv, struct device_node *node, char *name)
1217{
1218 struct device *dev = rsnd_priv_to_dev(priv);
1219 struct device_node *np;
1220 int i;
1221
1222 i = 0;
1223 for_each_child_of_node(node, np) {
1224 i = rsnd_node_fixed_index(np, name, i);
1225 if (i < 0) {
1226 dev_err(dev, "strange node numbering (%s)",
1227 of_node_full_name(node));
1228 return 0;
1229 }
1230 i++;
1231 }
1232
1233 return i;
1234}
1235
1236static struct device_node *rsnd_dai_of_node(struct rsnd_priv *priv,
1237 int *is_graph)
1238{
1239 struct device *dev = rsnd_priv_to_dev(priv);
1240 struct device_node *np = dev->of_node;
1241 struct device_node *dai_node;
1242 struct device_node *ret;
1243
1244 *is_graph = 0;
1245
1246
1247
1248
1249
1250 dai_node = of_get_child_by_name(np, RSND_NODE_DAI);
1251 if (dai_node) {
1252 ret = dai_node;
1253 goto of_node_compatible;
1254 }
1255
1256 ret = np;
1257
1258 dai_node = of_graph_get_next_endpoint(np, NULL);
1259 if (dai_node)
1260 goto of_node_graph;
1261
1262 return NULL;
1263
1264of_node_graph:
1265 *is_graph = 1;
1266of_node_compatible:
1267 of_node_put(dai_node);
1268
1269 return ret;
1270}
1271
1272
1273#define PREALLOC_BUFFER (32 * 1024)
1274#define PREALLOC_BUFFER_MAX (32 * 1024)
1275
1276static int rsnd_preallocate_pages(struct snd_soc_pcm_runtime *rtd,
1277 struct rsnd_dai_stream *io,
1278 int stream)
1279{
1280 struct rsnd_priv *priv = rsnd_io_to_priv(io);
1281 struct device *dev = rsnd_priv_to_dev(priv);
1282 struct snd_pcm_substream *substream;
1283
1284
1285
1286
1287
1288
1289 if (io->dmac_dev)
1290 dev = io->dmac_dev;
1291
1292 for (substream = rtd->pcm->streams[stream].substream;
1293 substream;
1294 substream = substream->next) {
1295 snd_pcm_set_managed_buffer(substream,
1296 SNDRV_DMA_TYPE_DEV,
1297 dev,
1298 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
1299 }
1300
1301 return 0;
1302}
1303
1304static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd,
1305 struct snd_soc_dai *dai)
1306{
1307 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1308 int ret;
1309
1310 ret = rsnd_dai_call(pcm_new, &rdai->playback, rtd);
1311 if (ret)
1312 return ret;
1313
1314 ret = rsnd_dai_call(pcm_new, &rdai->capture, rtd);
1315 if (ret)
1316 return ret;
1317
1318 ret = rsnd_preallocate_pages(rtd, &rdai->playback,
1319 SNDRV_PCM_STREAM_PLAYBACK);
1320 if (ret)
1321 return ret;
1322
1323 ret = rsnd_preallocate_pages(rtd, &rdai->capture,
1324 SNDRV_PCM_STREAM_CAPTURE);
1325 if (ret)
1326 return ret;
1327
1328 return 0;
1329}
1330
1331static void __rsnd_dai_probe(struct rsnd_priv *priv,
1332 struct device_node *dai_np,
1333 int dai_i)
1334{
1335 struct rsnd_dai_stream *io_playback;
1336 struct rsnd_dai_stream *io_capture;
1337 struct snd_soc_dai_driver *drv;
1338 struct rsnd_dai *rdai;
1339 struct device *dev = rsnd_priv_to_dev(priv);
1340 int io_i;
1341
1342 rdai = rsnd_rdai_get(priv, dai_i);
1343 drv = rsnd_daidrv_get(priv, dai_i);
1344 io_playback = &rdai->playback;
1345 io_capture = &rdai->capture;
1346
1347 snprintf(rdai->name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", dai_i);
1348
1349 rdai->priv = priv;
1350 drv->name = rdai->name;
1351 drv->ops = &rsnd_soc_dai_ops;
1352 drv->pcm_new = rsnd_pcm_new;
1353
1354 snprintf(io_playback->name, RSND_DAI_NAME_SIZE,
1355 "DAI%d Playback", dai_i);
1356 drv->playback.rates = RSND_RATES;
1357 drv->playback.formats = RSND_FMTS;
1358 drv->playback.channels_min = 2;
1359 drv->playback.channels_max = 8;
1360 drv->playback.stream_name = io_playback->name;
1361
1362 snprintf(io_capture->name, RSND_DAI_NAME_SIZE,
1363 "DAI%d Capture", dai_i);
1364 drv->capture.rates = RSND_RATES;
1365 drv->capture.formats = RSND_FMTS;
1366 drv->capture.channels_min = 2;
1367 drv->capture.channels_max = 8;
1368 drv->capture.stream_name = io_capture->name;
1369
1370 io_playback->rdai = rdai;
1371 io_capture->rdai = rdai;
1372 rsnd_rdai_channels_set(rdai, 2);
1373 rsnd_rdai_ssi_lane_set(rdai, 1);
1374 rsnd_rdai_width_set(rdai, 32);
1375
1376 for (io_i = 0;; io_i++) {
1377 struct device_node *playback = of_parse_phandle(dai_np, "playback", io_i);
1378 struct device_node *capture = of_parse_phandle(dai_np, "capture", io_i);
1379
1380 if (!playback && !capture)
1381 break;
1382
1383 rsnd_parse_connect_ssi(rdai, playback, capture);
1384 rsnd_parse_connect_ssiu(rdai, playback, capture);
1385 rsnd_parse_connect_src(rdai, playback, capture);
1386 rsnd_parse_connect_ctu(rdai, playback, capture);
1387 rsnd_parse_connect_mix(rdai, playback, capture);
1388 rsnd_parse_connect_dvc(rdai, playback, capture);
1389
1390 of_node_put(playback);
1391 of_node_put(capture);
1392 }
1393
1394 if (rsnd_ssi_is_pin_sharing(io_capture) ||
1395 rsnd_ssi_is_pin_sharing(io_playback)) {
1396
1397 drv->symmetric_rate = 1;
1398 }
1399
1400 dev_dbg(dev, "%s (%s/%s)\n", rdai->name,
1401 rsnd_io_to_mod_ssi(io_playback) ? "play" : " -- ",
1402 rsnd_io_to_mod_ssi(io_capture) ? "capture" : " -- ");
1403}
1404
1405static int rsnd_dai_probe(struct rsnd_priv *priv)
1406{
1407 struct device_node *dai_node;
1408 struct device_node *dai_np;
1409 struct snd_soc_dai_driver *rdrv;
1410 struct device *dev = rsnd_priv_to_dev(priv);
1411 struct rsnd_dai *rdai;
1412 int nr;
1413 int is_graph;
1414 int dai_i;
1415
1416 dai_node = rsnd_dai_of_node(priv, &is_graph);
1417 if (is_graph)
1418 nr = of_graph_get_endpoint_count(dai_node);
1419 else
1420 nr = of_get_child_count(dai_node);
1421
1422 if (!nr)
1423 return -EINVAL;
1424
1425 rdrv = devm_kcalloc(dev, nr, sizeof(*rdrv), GFP_KERNEL);
1426 rdai = devm_kcalloc(dev, nr, sizeof(*rdai), GFP_KERNEL);
1427 if (!rdrv || !rdai)
1428 return -ENOMEM;
1429
1430 priv->rdai_nr = nr;
1431 priv->daidrv = rdrv;
1432 priv->rdai = rdai;
1433
1434
1435
1436
1437 dai_i = 0;
1438 if (is_graph) {
1439 for_each_endpoint_of_node(dai_node, dai_np) {
1440 __rsnd_dai_probe(priv, dai_np, dai_i);
1441 if (rsnd_is_gen3(priv)) {
1442 rdai = rsnd_rdai_get(priv, dai_i);
1443
1444 rsnd_parse_connect_graph(priv, &rdai->playback, dai_np);
1445 rsnd_parse_connect_graph(priv, &rdai->capture, dai_np);
1446 }
1447 dai_i++;
1448 }
1449 } else {
1450 for_each_child_of_node(dai_node, dai_np) {
1451 __rsnd_dai_probe(priv, dai_np, dai_i);
1452 if (rsnd_is_gen3(priv)) {
1453 rdai = rsnd_rdai_get(priv, dai_i);
1454
1455 rsnd_parse_connect_simple(priv, &rdai->playback, dai_np);
1456 rsnd_parse_connect_simple(priv, &rdai->capture, dai_np);
1457 }
1458 dai_i++;
1459 }
1460 }
1461
1462 return 0;
1463}
1464
1465
1466
1467
1468static int rsnd_hw_update(struct snd_pcm_substream *substream,
1469 struct snd_pcm_hw_params *hw_params)
1470{
1471 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
1472 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1473 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
1474 struct rsnd_priv *priv = rsnd_io_to_priv(io);
1475 unsigned long flags;
1476 int ret;
1477
1478 spin_lock_irqsave(&priv->lock, flags);
1479 if (hw_params)
1480 ret = rsnd_dai_call(hw_params, io, substream, hw_params);
1481 else
1482 ret = rsnd_dai_call(hw_free, io, substream);
1483 spin_unlock_irqrestore(&priv->lock, flags);
1484
1485 return ret;
1486}
1487
1488static int rsnd_hw_params(struct snd_soc_component *component,
1489 struct snd_pcm_substream *substream,
1490 struct snd_pcm_hw_params *hw_params)
1491{
1492 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
1493 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1494 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
1495 struct snd_soc_pcm_runtime *fe = asoc_substream_to_rtd(substream);
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506 io->converted_rate = 0;
1507 io->converted_chan = 0;
1508 if (fe->dai_link->dynamic) {
1509 struct rsnd_priv *priv = rsnd_io_to_priv(io);
1510 struct device *dev = rsnd_priv_to_dev(priv);
1511 struct snd_soc_dpcm *dpcm;
1512 int stream = substream->stream;
1513
1514 for_each_dpcm_be(fe, stream, dpcm) {
1515 struct snd_pcm_hw_params *be_params = &dpcm->hw_params;
1516
1517 if (params_channels(hw_params) != params_channels(be_params))
1518 io->converted_chan = params_channels(be_params);
1519 if (params_rate(hw_params) != params_rate(be_params))
1520 io->converted_rate = params_rate(be_params);
1521 }
1522 if (io->converted_chan)
1523 dev_dbg(dev, "convert channels = %d\n", io->converted_chan);
1524 if (io->converted_rate) {
1525
1526
1527
1528
1529
1530
1531
1532
1533 int k_up = 6;
1534 int k_down = 6;
1535 int channel;
1536 struct rsnd_mod *src_mod = rsnd_io_to_mod_src(io);
1537
1538 dev_dbg(dev, "convert rate = %d\n", io->converted_rate);
1539
1540 channel = io->converted_chan ? io->converted_chan :
1541 params_channels(hw_params);
1542
1543 switch (rsnd_mod_id(src_mod)) {
1544
1545
1546
1547
1548
1549
1550
1551 case 1:
1552 case 3:
1553 case 4:
1554 if (channel > 4) {
1555 k_down = 2;
1556 break;
1557 }
1558 fallthrough;
1559 case 0:
1560 if (channel > 2)
1561 k_down = 4;
1562 break;
1563
1564
1565 default:
1566 if (channel > 2)
1567 return -EINVAL;
1568 }
1569
1570 if (params_rate(hw_params) > io->converted_rate * k_down) {
1571 hw_param_interval(hw_params, SNDRV_PCM_HW_PARAM_RATE)->min =
1572 io->converted_rate * k_down;
1573 hw_param_interval(hw_params, SNDRV_PCM_HW_PARAM_RATE)->max =
1574 io->converted_rate * k_down;
1575 hw_params->cmask |= SNDRV_PCM_HW_PARAM_RATE;
1576 } else if (params_rate(hw_params) * k_up < io->converted_rate) {
1577 hw_param_interval(hw_params, SNDRV_PCM_HW_PARAM_RATE)->min =
1578 (io->converted_rate + k_up - 1) / k_up;
1579 hw_param_interval(hw_params, SNDRV_PCM_HW_PARAM_RATE)->max =
1580 (io->converted_rate + k_up - 1) / k_up;
1581 hw_params->cmask |= SNDRV_PCM_HW_PARAM_RATE;
1582 }
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592 }
1593 }
1594
1595 return rsnd_hw_update(substream, hw_params);
1596}
1597
1598static int rsnd_hw_free(struct snd_soc_component *component,
1599 struct snd_pcm_substream *substream)
1600{
1601 return rsnd_hw_update(substream, NULL);
1602}
1603
1604static snd_pcm_uframes_t rsnd_pointer(struct snd_soc_component *component,
1605 struct snd_pcm_substream *substream)
1606{
1607 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
1608 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1609 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
1610 snd_pcm_uframes_t pointer = 0;
1611
1612 rsnd_dai_call(pointer, io, &pointer);
1613
1614 return pointer;
1615}
1616
1617
1618
1619
1620static int rsnd_kctrl_info(struct snd_kcontrol *kctrl,
1621 struct snd_ctl_elem_info *uinfo)
1622{
1623 struct rsnd_kctrl_cfg *cfg = snd_kcontrol_chip(kctrl);
1624
1625 if (cfg->texts) {
1626 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1627 uinfo->count = cfg->size;
1628 uinfo->value.enumerated.items = cfg->max;
1629 if (uinfo->value.enumerated.item >= cfg->max)
1630 uinfo->value.enumerated.item = cfg->max - 1;
1631 strscpy(uinfo->value.enumerated.name,
1632 cfg->texts[uinfo->value.enumerated.item],
1633 sizeof(uinfo->value.enumerated.name));
1634 } else {
1635 uinfo->count = cfg->size;
1636 uinfo->value.integer.min = 0;
1637 uinfo->value.integer.max = cfg->max;
1638 uinfo->type = (cfg->max == 1) ?
1639 SNDRV_CTL_ELEM_TYPE_BOOLEAN :
1640 SNDRV_CTL_ELEM_TYPE_INTEGER;
1641 }
1642
1643 return 0;
1644}
1645
1646static int rsnd_kctrl_get(struct snd_kcontrol *kctrl,
1647 struct snd_ctl_elem_value *uc)
1648{
1649 struct rsnd_kctrl_cfg *cfg = snd_kcontrol_chip(kctrl);
1650 int i;
1651
1652 for (i = 0; i < cfg->size; i++)
1653 if (cfg->texts)
1654 uc->value.enumerated.item[i] = cfg->val[i];
1655 else
1656 uc->value.integer.value[i] = cfg->val[i];
1657
1658 return 0;
1659}
1660
1661static int rsnd_kctrl_put(struct snd_kcontrol *kctrl,
1662 struct snd_ctl_elem_value *uc)
1663{
1664 struct rsnd_kctrl_cfg *cfg = snd_kcontrol_chip(kctrl);
1665 int i, change = 0;
1666
1667 if (!cfg->accept(cfg->io))
1668 return 0;
1669
1670 for (i = 0; i < cfg->size; i++) {
1671 if (cfg->texts) {
1672 change |= (uc->value.enumerated.item[i] != cfg->val[i]);
1673 cfg->val[i] = uc->value.enumerated.item[i];
1674 } else {
1675 change |= (uc->value.integer.value[i] != cfg->val[i]);
1676 cfg->val[i] = uc->value.integer.value[i];
1677 }
1678 }
1679
1680 if (change && cfg->update)
1681 cfg->update(cfg->io, cfg->mod);
1682
1683 return change;
1684}
1685
1686int rsnd_kctrl_accept_anytime(struct rsnd_dai_stream *io)
1687{
1688 return 1;
1689}
1690
1691int rsnd_kctrl_accept_runtime(struct rsnd_dai_stream *io)
1692{
1693 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
1694 struct rsnd_priv *priv = rsnd_io_to_priv(io);
1695 struct device *dev = rsnd_priv_to_dev(priv);
1696
1697 if (!runtime) {
1698 dev_warn(dev, "Can't update kctrl when idle\n");
1699 return 0;
1700 }
1701
1702 return 1;
1703}
1704
1705struct rsnd_kctrl_cfg *rsnd_kctrl_init_m(struct rsnd_kctrl_cfg_m *cfg)
1706{
1707 cfg->cfg.val = cfg->val;
1708
1709 return &cfg->cfg;
1710}
1711
1712struct rsnd_kctrl_cfg *rsnd_kctrl_init_s(struct rsnd_kctrl_cfg_s *cfg)
1713{
1714 cfg->cfg.val = &cfg->val;
1715
1716 return &cfg->cfg;
1717}
1718
1719const char * const volume_ramp_rate[] = {
1720 "128 dB/1 step",
1721 "64 dB/1 step",
1722 "32 dB/1 step",
1723 "16 dB/1 step",
1724 "8 dB/1 step",
1725 "4 dB/1 step",
1726 "2 dB/1 step",
1727 "1 dB/1 step",
1728 "0.5 dB/1 step",
1729 "0.25 dB/1 step",
1730 "0.125 dB/1 step",
1731 "0.125 dB/2 steps",
1732 "0.125 dB/4 steps",
1733 "0.125 dB/8 steps",
1734 "0.125 dB/16 steps",
1735 "0.125 dB/32 steps",
1736 "0.125 dB/64 steps",
1737 "0.125 dB/128 steps",
1738 "0.125 dB/256 steps",
1739 "0.125 dB/512 steps",
1740 "0.125 dB/1024 steps",
1741 "0.125 dB/2048 steps",
1742 "0.125 dB/4096 steps",
1743 "0.125 dB/8192 steps",
1744};
1745
1746int rsnd_kctrl_new(struct rsnd_mod *mod,
1747 struct rsnd_dai_stream *io,
1748 struct snd_soc_pcm_runtime *rtd,
1749 const unsigned char *name,
1750 int (*accept)(struct rsnd_dai_stream *io),
1751 void (*update)(struct rsnd_dai_stream *io,
1752 struct rsnd_mod *mod),
1753 struct rsnd_kctrl_cfg *cfg,
1754 const char * const *texts,
1755 int size,
1756 u32 max)
1757{
1758 struct snd_card *card = rtd->card->snd_card;
1759 struct snd_kcontrol *kctrl;
1760 struct snd_kcontrol_new knew = {
1761 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1762 .name = name,
1763 .info = rsnd_kctrl_info,
1764 .index = rtd->num,
1765 .get = rsnd_kctrl_get,
1766 .put = rsnd_kctrl_put,
1767 };
1768 int ret;
1769
1770
1771
1772
1773
1774
1775 list_for_each_entry(kctrl, &card->controls, list) {
1776 struct rsnd_kctrl_cfg *c = kctrl->private_data;
1777
1778 if (c == cfg)
1779 return 0;
1780 }
1781
1782 if (size > RSND_MAX_CHANNELS)
1783 return -EINVAL;
1784
1785 kctrl = snd_ctl_new1(&knew, cfg);
1786 if (!kctrl)
1787 return -ENOMEM;
1788
1789 ret = snd_ctl_add(card, kctrl);
1790 if (ret < 0)
1791 return ret;
1792
1793 cfg->texts = texts;
1794 cfg->max = max;
1795 cfg->size = size;
1796 cfg->accept = accept;
1797 cfg->update = update;
1798 cfg->card = card;
1799 cfg->kctrl = kctrl;
1800 cfg->io = io;
1801 cfg->mod = mod;
1802
1803 return 0;
1804}
1805
1806
1807
1808
1809static const struct snd_soc_component_driver rsnd_soc_component = {
1810 .name = "rsnd",
1811 .probe = rsnd_debugfs_probe,
1812 .hw_params = rsnd_hw_params,
1813 .hw_free = rsnd_hw_free,
1814 .pointer = rsnd_pointer,
1815};
1816
1817static int rsnd_rdai_continuance_probe(struct rsnd_priv *priv,
1818 struct rsnd_dai_stream *io)
1819{
1820 int ret;
1821
1822 ret = rsnd_dai_call(probe, io, priv);
1823 if (ret == -EAGAIN) {
1824 struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io);
1825 struct rsnd_mod *mod;
1826 int i;
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839 rsnd_dai_call(remove, io, priv);
1840
1841
1842
1843
1844
1845 for_each_rsnd_mod(i, mod, io)
1846 rsnd_dai_disconnect(mod, io, i);
1847 rsnd_dai_connect(ssi_mod, io, RSND_MOD_SSI);
1848
1849
1850
1851
1852 rsnd_dai_call(fallback, io, priv);
1853
1854
1855
1856
1857
1858 ret = rsnd_dai_call(probe, io, priv);
1859 }
1860
1861 return ret;
1862}
1863
1864
1865
1866
1867static int rsnd_probe(struct platform_device *pdev)
1868{
1869 struct rsnd_priv *priv;
1870 struct device *dev = &pdev->dev;
1871 struct rsnd_dai *rdai;
1872 int (*probe_func[])(struct rsnd_priv *priv) = {
1873 rsnd_gen_probe,
1874 rsnd_dma_probe,
1875 rsnd_ssi_probe,
1876 rsnd_ssiu_probe,
1877 rsnd_src_probe,
1878 rsnd_ctu_probe,
1879 rsnd_mix_probe,
1880 rsnd_dvc_probe,
1881 rsnd_cmd_probe,
1882 rsnd_adg_probe,
1883 rsnd_dai_probe,
1884 };
1885 int ret, i;
1886
1887
1888
1889
1890 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1891 if (!priv)
1892 return -ENODEV;
1893
1894 priv->pdev = pdev;
1895 priv->flags = (unsigned long)of_device_get_match_data(dev);
1896 spin_lock_init(&priv->lock);
1897
1898
1899
1900
1901 for (i = 0; i < ARRAY_SIZE(probe_func); i++) {
1902 ret = probe_func[i](priv);
1903 if (ret)
1904 return ret;
1905 }
1906
1907 for_each_rsnd_dai(rdai, priv, i) {
1908 ret = rsnd_rdai_continuance_probe(priv, &rdai->playback);
1909 if (ret)
1910 goto exit_snd_probe;
1911
1912 ret = rsnd_rdai_continuance_probe(priv, &rdai->capture);
1913 if (ret)
1914 goto exit_snd_probe;
1915 }
1916
1917 dev_set_drvdata(dev, priv);
1918
1919
1920
1921
1922 ret = devm_snd_soc_register_component(dev, &rsnd_soc_component,
1923 priv->daidrv, rsnd_rdai_nr(priv));
1924 if (ret < 0) {
1925 dev_err(dev, "cannot snd dai register\n");
1926 goto exit_snd_probe;
1927 }
1928
1929 pm_runtime_enable(dev);
1930
1931 dev_info(dev, "probed\n");
1932 return ret;
1933
1934exit_snd_probe:
1935 for_each_rsnd_dai(rdai, priv, i) {
1936 rsnd_dai_call(remove, &rdai->playback, priv);
1937 rsnd_dai_call(remove, &rdai->capture, priv);
1938 }
1939
1940
1941
1942
1943
1944
1945
1946 rsnd_adg_remove(priv);
1947
1948 return ret;
1949}
1950
1951static int rsnd_remove(struct platform_device *pdev)
1952{
1953 struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
1954 struct rsnd_dai *rdai;
1955 void (*remove_func[])(struct rsnd_priv *priv) = {
1956 rsnd_ssi_remove,
1957 rsnd_ssiu_remove,
1958 rsnd_src_remove,
1959 rsnd_ctu_remove,
1960 rsnd_mix_remove,
1961 rsnd_dvc_remove,
1962 rsnd_cmd_remove,
1963 rsnd_adg_remove,
1964 };
1965 int ret = 0, i;
1966
1967 pm_runtime_disable(&pdev->dev);
1968
1969 for_each_rsnd_dai(rdai, priv, i) {
1970 ret |= rsnd_dai_call(remove, &rdai->playback, priv);
1971 ret |= rsnd_dai_call(remove, &rdai->capture, priv);
1972 }
1973
1974 for (i = 0; i < ARRAY_SIZE(remove_func); i++)
1975 remove_func[i](priv);
1976
1977 return ret;
1978}
1979
1980static int __maybe_unused rsnd_suspend(struct device *dev)
1981{
1982 struct rsnd_priv *priv = dev_get_drvdata(dev);
1983
1984 rsnd_adg_clk_disable(priv);
1985
1986 return 0;
1987}
1988
1989static int __maybe_unused rsnd_resume(struct device *dev)
1990{
1991 struct rsnd_priv *priv = dev_get_drvdata(dev);
1992
1993 rsnd_adg_clk_enable(priv);
1994
1995 return 0;
1996}
1997
1998static const struct dev_pm_ops rsnd_pm_ops = {
1999 SET_SYSTEM_SLEEP_PM_OPS(rsnd_suspend, rsnd_resume)
2000};
2001
2002static struct platform_driver rsnd_driver = {
2003 .driver = {
2004 .name = "rcar_sound",
2005 .pm = &rsnd_pm_ops,
2006 .of_match_table = rsnd_of_match,
2007 },
2008 .probe = rsnd_probe,
2009 .remove = rsnd_remove,
2010};
2011module_platform_driver(rsnd_driver);
2012
2013MODULE_LICENSE("GPL v2");
2014MODULE_DESCRIPTION("Renesas R-Car audio driver");
2015MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
2016MODULE_ALIAS("platform:rcar-pcm-audio");
2017