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
94
95
96#include <linux/pm_runtime.h>
97#include "rsnd.h"
98
99#define RSND_RATES SNDRV_PCM_RATE_8000_96000
100#define RSND_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
101
102static const struct of_device_id rsnd_of_match[] = {
103 { .compatible = "renesas,rcar_sound-gen1", .data = (void *)RSND_GEN1 },
104 { .compatible = "renesas,rcar_sound-gen2", .data = (void *)RSND_GEN2 },
105 { .compatible = "renesas,rcar_sound-gen3", .data = (void *)RSND_GEN2 },
106 {},
107};
108MODULE_DEVICE_TABLE(of, rsnd_of_match);
109
110
111
112
113#ifdef DEBUG
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[%d] is not your expected module\n",
121 rsnd_mod_name(mod), rsnd_mod_id(mod));
122 }
123}
124#endif
125
126char *rsnd_mod_name(struct rsnd_mod *mod)
127{
128 if (!mod || !mod->ops)
129 return "unknown";
130
131 return mod->ops->name;
132}
133
134struct dma_chan *rsnd_mod_dma_req(struct rsnd_dai_stream *io,
135 struct rsnd_mod *mod)
136{
137 if (!mod || !mod->ops || !mod->ops->dma_req)
138 return NULL;
139
140 return mod->ops->dma_req(io, mod);
141}
142
143u32 *rsnd_mod_get_status(struct rsnd_dai_stream *io,
144 struct rsnd_mod *mod,
145 enum rsnd_mod_type type)
146{
147 return &mod->status;
148}
149
150int rsnd_mod_init(struct rsnd_priv *priv,
151 struct rsnd_mod *mod,
152 struct rsnd_mod_ops *ops,
153 struct clk *clk,
154 u32* (*get_status)(struct rsnd_dai_stream *io,
155 struct rsnd_mod *mod,
156 enum rsnd_mod_type type),
157 enum rsnd_mod_type type,
158 int id)
159{
160 int ret = clk_prepare(clk);
161
162 if (ret)
163 return ret;
164
165 mod->id = id;
166 mod->ops = ops;
167 mod->type = type;
168 mod->clk = clk;
169 mod->priv = priv;
170 mod->get_status = get_status;
171
172 return ret;
173}
174
175void rsnd_mod_quit(struct rsnd_mod *mod)
176{
177 if (mod->clk)
178 clk_unprepare(mod->clk);
179 mod->clk = NULL;
180}
181
182void rsnd_mod_interrupt(struct rsnd_mod *mod,
183 void (*callback)(struct rsnd_mod *mod,
184 struct rsnd_dai_stream *io))
185{
186 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
187 struct rsnd_dai_stream *io;
188 struct rsnd_dai *rdai;
189 int i;
190
191 for_each_rsnd_dai(rdai, priv, i) {
192 io = &rdai->playback;
193 if (mod == io->mod[mod->type])
194 callback(mod, io);
195
196 io = &rdai->capture;
197 if (mod == io->mod[mod->type])
198 callback(mod, io);
199 }
200}
201
202int rsnd_io_is_working(struct rsnd_dai_stream *io)
203{
204
205 return !!io->substream;
206}
207
208void rsnd_set_slot(struct rsnd_dai *rdai,
209 int slots, int num)
210{
211 rdai->slots = slots;
212 rdai->slots_num = num;
213}
214
215int rsnd_get_slot(struct rsnd_dai_stream *io)
216{
217 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
218
219 return rdai->slots;
220}
221
222int rsnd_get_slot_num(struct rsnd_dai_stream *io)
223{
224 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
225
226 return rdai->slots_num;
227}
228
229int rsnd_runtime_channel_original(struct rsnd_dai_stream *io)
230{
231 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
232
233 return runtime->channels;
234}
235
236int rsnd_runtime_channel_after_ctu(struct rsnd_dai_stream *io)
237{
238 int chan = rsnd_runtime_channel_original(io);
239 struct rsnd_mod *ctu_mod = rsnd_io_to_mod_ctu(io);
240
241 if (ctu_mod) {
242 u32 converted_chan = rsnd_ctu_converted_channel(ctu_mod);
243
244 if (converted_chan)
245 return converted_chan;
246 }
247
248 return chan;
249}
250
251int rsnd_runtime_channel_for_ssi(struct rsnd_dai_stream *io)
252{
253 int chan = rsnd_io_is_play(io) ?
254 rsnd_runtime_channel_after_ctu(io) :
255 rsnd_runtime_channel_original(io);
256
257
258 if (rsnd_runtime_is_ssi_multi(io))
259 chan /= rsnd_get_slot_num(io);
260
261
262 if (chan == 6)
263 chan = 8;
264
265 return chan;
266}
267
268int rsnd_runtime_is_ssi_multi(struct rsnd_dai_stream *io)
269{
270 int slots = rsnd_get_slot_num(io);
271 int chan = rsnd_io_is_play(io) ?
272 rsnd_runtime_channel_after_ctu(io) :
273 rsnd_runtime_channel_original(io);
274
275 return (chan >= 6) && (slots > 1);
276}
277
278int rsnd_runtime_is_ssi_tdm(struct rsnd_dai_stream *io)
279{
280 return rsnd_runtime_channel_for_ssi(io) >= 6;
281}
282
283
284
285
286u32 rsnd_get_adinr_bit(struct rsnd_mod *mod, struct rsnd_dai_stream *io)
287{
288 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
289 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
290 struct device *dev = rsnd_priv_to_dev(priv);
291
292 switch (runtime->sample_bits) {
293 case 16:
294 return 8 << 16;
295 case 32:
296 return 0 << 16;
297 }
298
299 dev_warn(dev, "not supported sample bits\n");
300
301 return 0;
302}
303
304
305
306
307u32 rsnd_get_dalign(struct rsnd_mod *mod, struct rsnd_dai_stream *io)
308{
309 struct rsnd_mod *ssi = rsnd_io_to_mod_ssi(io);
310 struct rsnd_mod *target;
311 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
312 u32 val = 0x76543210;
313 u32 mask = ~0;
314
315 if (rsnd_io_is_play(io)) {
316 struct rsnd_mod *src = rsnd_io_to_mod_src(io);
317
318 target = src ? src : ssi;
319 } else {
320 struct rsnd_mod *cmd = rsnd_io_to_mod_cmd(io);
321
322 target = cmd ? cmd : ssi;
323 }
324
325 mask <<= runtime->channels * 4;
326 val = val & mask;
327
328 switch (runtime->sample_bits) {
329 case 16:
330 val |= 0x67452301 & ~mask;
331 break;
332 case 32:
333 val |= 0x76543210 & ~mask;
334 break;
335 }
336
337
338
339
340
341
342 if (mod == target)
343 return val;
344 else
345 return 0x76543210;
346}
347
348
349
350
351#define rsnd_mod_call(idx, io, func, param...) \
352({ \
353 struct rsnd_priv *priv = rsnd_mod_to_priv(mod); \
354 struct rsnd_mod *mod = (io)->mod[idx]; \
355 struct device *dev = rsnd_priv_to_dev(priv); \
356 u32 *status = mod->get_status(io, mod, idx); \
357 u32 mask = 0xF << __rsnd_mod_shift_##func; \
358 u8 val = (*status >> __rsnd_mod_shift_##func) & 0xF; \
359 u8 add = ((val + __rsnd_mod_add_##func) & 0xF); \
360 int ret = 0; \
361 int call = (val == __rsnd_mod_call_##func) && (mod)->ops->func; \
362 if (add == 0xF) \
363 call = 0; \
364 else \
365 *status = (*status & ~mask) + \
366 (add << __rsnd_mod_shift_##func); \
367 dev_dbg(dev, "%s[%d]\t0x%08x %s\n", \
368 rsnd_mod_name(mod), rsnd_mod_id(mod), \
369 *status, call ? #func : ""); \
370 if (call) \
371 ret = (mod)->ops->func(mod, io, param); \
372 if (ret) \
373 dev_dbg(dev, "%s[%d] : rsnd_mod_call error %d\n", \
374 rsnd_mod_name(mod), rsnd_mod_id(mod), ret); \
375 ret; \
376})
377
378static enum rsnd_mod_type rsnd_mod_sequence[][RSND_MOD_MAX] = {
379 {
380
381 RSND_MOD_AUDMAPP,
382 RSND_MOD_AUDMA,
383 RSND_MOD_DVC,
384 RSND_MOD_MIX,
385 RSND_MOD_CTU,
386 RSND_MOD_CMD,
387 RSND_MOD_SRC,
388 RSND_MOD_SSIU,
389 RSND_MOD_SSIM3,
390 RSND_MOD_SSIM2,
391 RSND_MOD_SSIM1,
392 RSND_MOD_SSIP,
393 RSND_MOD_SSI,
394 }, {
395
396 RSND_MOD_AUDMAPP,
397 RSND_MOD_AUDMA,
398 RSND_MOD_SSIM3,
399 RSND_MOD_SSIM2,
400 RSND_MOD_SSIM1,
401 RSND_MOD_SSIP,
402 RSND_MOD_SSI,
403 RSND_MOD_SSIU,
404 RSND_MOD_DVC,
405 RSND_MOD_MIX,
406 RSND_MOD_CTU,
407 RSND_MOD_CMD,
408 RSND_MOD_SRC,
409 },
410};
411
412#define rsnd_dai_call(fn, io, param...) \
413({ \
414 struct rsnd_mod *mod; \
415 int type, is_play = rsnd_io_is_play(io); \
416 int ret = 0, i; \
417 for (i = 0; i < RSND_MOD_MAX; i++) { \
418 type = rsnd_mod_sequence[is_play][i]; \
419 mod = (io)->mod[type]; \
420 if (!mod) \
421 continue; \
422 ret |= rsnd_mod_call(type, io, fn, param); \
423 } \
424 ret; \
425})
426
427int rsnd_dai_connect(struct rsnd_mod *mod,
428 struct rsnd_dai_stream *io,
429 enum rsnd_mod_type type)
430{
431 struct rsnd_priv *priv;
432 struct device *dev;
433
434 if (!mod)
435 return -EIO;
436
437 if (io->mod[type] == mod)
438 return 0;
439
440 if (io->mod[type])
441 return -EINVAL;
442
443 priv = rsnd_mod_to_priv(mod);
444 dev = rsnd_priv_to_dev(priv);
445
446 io->mod[type] = mod;
447
448 dev_dbg(dev, "%s[%d] is connected to io (%s)\n",
449 rsnd_mod_name(mod), rsnd_mod_id(mod),
450 rsnd_io_is_play(io) ? "Playback" : "Capture");
451
452 return 0;
453}
454
455static void rsnd_dai_disconnect(struct rsnd_mod *mod,
456 struct rsnd_dai_stream *io,
457 enum rsnd_mod_type type)
458{
459 io->mod[type] = NULL;
460}
461
462struct rsnd_dai *rsnd_rdai_get(struct rsnd_priv *priv, int id)
463{
464 if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
465 return NULL;
466
467 return priv->rdai + id;
468}
469
470#define rsnd_dai_to_priv(dai) snd_soc_dai_get_drvdata(dai)
471static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai)
472{
473 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
474
475 return rsnd_rdai_get(priv, dai->id);
476}
477
478
479
480
481int rsnd_dai_pointer_offset(struct rsnd_dai_stream *io, int additional)
482{
483 struct snd_pcm_substream *substream = io->substream;
484 struct snd_pcm_runtime *runtime = substream->runtime;
485 int pos = io->byte_pos + additional;
486
487 pos %= (runtime->periods * io->byte_per_period);
488
489 return pos;
490}
491
492bool rsnd_dai_pointer_update(struct rsnd_dai_stream *io, int byte)
493{
494 io->byte_pos += byte;
495
496 if (io->byte_pos >= io->next_period_byte) {
497 struct snd_pcm_substream *substream = io->substream;
498 struct snd_pcm_runtime *runtime = substream->runtime;
499
500 io->period_pos++;
501 io->next_period_byte += io->byte_per_period;
502
503 if (io->period_pos >= runtime->periods) {
504 io->byte_pos = 0;
505 io->period_pos = 0;
506 io->next_period_byte = io->byte_per_period;
507 }
508
509 return true;
510 }
511
512 return false;
513}
514
515void rsnd_dai_period_elapsed(struct rsnd_dai_stream *io)
516{
517 struct snd_pcm_substream *substream = io->substream;
518
519
520
521
522
523
524
525
526 snd_pcm_period_elapsed(substream);
527}
528
529static void rsnd_dai_stream_init(struct rsnd_dai_stream *io,
530 struct snd_pcm_substream *substream)
531{
532 struct snd_pcm_runtime *runtime = substream->runtime;
533
534 io->substream = substream;
535 io->byte_pos = 0;
536 io->period_pos = 0;
537 io->byte_per_period = runtime->period_size *
538 runtime->channels *
539 samples_to_bytes(runtime, 1);
540 io->next_period_byte = io->byte_per_period;
541}
542
543static void rsnd_dai_stream_quit(struct rsnd_dai_stream *io)
544{
545 io->substream = NULL;
546}
547
548static
549struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream)
550{
551 struct snd_soc_pcm_runtime *rtd = substream->private_data;
552
553 return rtd->cpu_dai;
554}
555
556static
557struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai,
558 struct snd_pcm_substream *substream)
559{
560 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
561 return &rdai->playback;
562 else
563 return &rdai->capture;
564}
565
566static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd,
567 struct snd_soc_dai *dai)
568{
569 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
570 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
571 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
572 int ret;
573 unsigned long flags;
574
575 spin_lock_irqsave(&priv->lock, flags);
576
577 switch (cmd) {
578 case SNDRV_PCM_TRIGGER_START:
579 case SNDRV_PCM_TRIGGER_RESUME:
580 rsnd_dai_stream_init(io, substream);
581
582 ret = rsnd_dai_call(init, io, priv);
583 if (ret < 0)
584 goto dai_trigger_end;
585
586 ret = rsnd_dai_call(start, io, priv);
587 if (ret < 0)
588 goto dai_trigger_end;
589
590 ret = rsnd_dai_call(irq, io, priv, 1);
591 if (ret < 0)
592 goto dai_trigger_end;
593
594 break;
595 case SNDRV_PCM_TRIGGER_STOP:
596 case SNDRV_PCM_TRIGGER_SUSPEND:
597 ret = rsnd_dai_call(irq, io, priv, 0);
598
599 ret |= rsnd_dai_call(stop, io, priv);
600
601 ret |= rsnd_dai_call(quit, io, priv);
602
603 rsnd_dai_stream_quit(io);
604 break;
605 default:
606 ret = -EINVAL;
607 }
608
609dai_trigger_end:
610 spin_unlock_irqrestore(&priv->lock, flags);
611
612 return ret;
613}
614
615static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
616{
617 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
618
619
620 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
621 case SND_SOC_DAIFMT_CBM_CFM:
622 rdai->clk_master = 0;
623 break;
624 case SND_SOC_DAIFMT_CBS_CFS:
625 rdai->clk_master = 1;
626 break;
627 default:
628 return -EINVAL;
629 }
630
631
632 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
633 case SND_SOC_DAIFMT_I2S:
634 rdai->sys_delay = 0;
635 rdai->data_alignment = 0;
636 rdai->frm_clk_inv = 0;
637 break;
638 case SND_SOC_DAIFMT_LEFT_J:
639 rdai->sys_delay = 1;
640 rdai->data_alignment = 0;
641 rdai->frm_clk_inv = 1;
642 break;
643 case SND_SOC_DAIFMT_RIGHT_J:
644 rdai->sys_delay = 1;
645 rdai->data_alignment = 1;
646 rdai->frm_clk_inv = 1;
647 break;
648 }
649
650
651 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
652 case SND_SOC_DAIFMT_NB_IF:
653 rdai->bit_clk_inv = rdai->bit_clk_inv;
654 rdai->frm_clk_inv = !rdai->frm_clk_inv;
655 break;
656 case SND_SOC_DAIFMT_IB_NF:
657 rdai->bit_clk_inv = !rdai->bit_clk_inv;
658 rdai->frm_clk_inv = rdai->frm_clk_inv;
659 break;
660 case SND_SOC_DAIFMT_IB_IF:
661 rdai->bit_clk_inv = !rdai->bit_clk_inv;
662 rdai->frm_clk_inv = !rdai->frm_clk_inv;
663 break;
664 case SND_SOC_DAIFMT_NB_NF:
665 default:
666 break;
667 }
668
669 return 0;
670}
671
672static int rsnd_soc_set_dai_tdm_slot(struct snd_soc_dai *dai,
673 u32 tx_mask, u32 rx_mask,
674 int slots, int slot_width)
675{
676 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
677 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
678 struct device *dev = rsnd_priv_to_dev(priv);
679
680 switch (slots) {
681 case 6:
682
683 rsnd_set_slot(rdai, slots, 1);
684 break;
685 default:
686 dev_err(dev, "unsupported TDM slots (%d)\n", slots);
687 return -EINVAL;
688 }
689
690 return 0;
691}
692
693static const struct snd_soc_dai_ops rsnd_soc_dai_ops = {
694 .trigger = rsnd_soc_dai_trigger,
695 .set_fmt = rsnd_soc_dai_set_fmt,
696 .set_tdm_slot = rsnd_soc_set_dai_tdm_slot,
697};
698
699void rsnd_parse_connect_common(struct rsnd_dai *rdai,
700 struct rsnd_mod* (*mod_get)(struct rsnd_priv *priv, int id),
701 struct device_node *node,
702 struct device_node *playback,
703 struct device_node *capture)
704{
705 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
706 struct device_node *np;
707 struct rsnd_mod *mod;
708 int i;
709
710 if (!node)
711 return;
712
713 i = 0;
714 for_each_child_of_node(node, np) {
715 mod = mod_get(priv, i);
716 if (np == playback)
717 rsnd_dai_connect(mod, &rdai->playback, mod->type);
718 if (np == capture)
719 rsnd_dai_connect(mod, &rdai->capture, mod->type);
720 i++;
721 }
722
723 of_node_put(node);
724}
725
726static int rsnd_dai_probe(struct rsnd_priv *priv)
727{
728 struct device_node *dai_node;
729 struct device_node *dai_np;
730 struct device_node *playback, *capture;
731 struct rsnd_dai_stream *io_playback;
732 struct rsnd_dai_stream *io_capture;
733 struct snd_soc_dai_driver *rdrv, *drv;
734 struct rsnd_dai *rdai;
735 struct device *dev = rsnd_priv_to_dev(priv);
736 int nr, dai_i, io_i;
737 int ret;
738
739 dai_node = rsnd_dai_of_node(priv);
740 nr = of_get_child_count(dai_node);
741 if (!nr) {
742 ret = -EINVAL;
743 goto rsnd_dai_probe_done;
744 }
745
746 rdrv = devm_kzalloc(dev, sizeof(*rdrv) * nr, GFP_KERNEL);
747 rdai = devm_kzalloc(dev, sizeof(*rdai) * nr, GFP_KERNEL);
748 if (!rdrv || !rdai) {
749 ret = -ENOMEM;
750 goto rsnd_dai_probe_done;
751 }
752
753 priv->rdai_nr = nr;
754 priv->daidrv = rdrv;
755 priv->rdai = rdai;
756
757
758
759
760 dai_i = 0;
761 for_each_child_of_node(dai_node, dai_np) {
762 rdai = rsnd_rdai_get(priv, dai_i);
763 drv = rdrv + dai_i;
764 io_playback = &rdai->playback;
765 io_capture = &rdai->capture;
766
767 snprintf(rdai->name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", dai_i);
768
769 rdai->priv = priv;
770 drv->name = rdai->name;
771 drv->ops = &rsnd_soc_dai_ops;
772
773 snprintf(rdai->playback.name, RSND_DAI_NAME_SIZE,
774 "DAI%d Playback", dai_i);
775 drv->playback.rates = RSND_RATES;
776 drv->playback.formats = RSND_FMTS;
777 drv->playback.channels_min = 2;
778 drv->playback.channels_max = 6;
779 drv->playback.stream_name = rdai->playback.name;
780
781 snprintf(rdai->capture.name, RSND_DAI_NAME_SIZE,
782 "DAI%d Capture", dai_i);
783 drv->capture.rates = RSND_RATES;
784 drv->capture.formats = RSND_FMTS;
785 drv->capture.channels_min = 2;
786 drv->capture.channels_max = 6;
787 drv->capture.stream_name = rdai->capture.name;
788
789 rdai->playback.rdai = rdai;
790 rdai->capture.rdai = rdai;
791 rsnd_set_slot(rdai, 2, 1);
792
793 for (io_i = 0;; io_i++) {
794 playback = of_parse_phandle(dai_np, "playback", io_i);
795 capture = of_parse_phandle(dai_np, "capture", io_i);
796
797 if (!playback && !capture)
798 break;
799
800 rsnd_parse_connect_ssi(rdai, playback, capture);
801 rsnd_parse_connect_src(rdai, playback, capture);
802 rsnd_parse_connect_ctu(rdai, playback, capture);
803 rsnd_parse_connect_mix(rdai, playback, capture);
804 rsnd_parse_connect_dvc(rdai, playback, capture);
805
806 of_node_put(playback);
807 of_node_put(capture);
808 }
809
810 dai_i++;
811
812 dev_dbg(dev, "%s (%s/%s)\n", rdai->name,
813 rsnd_io_to_mod_ssi(io_playback) ? "play" : " -- ",
814 rsnd_io_to_mod_ssi(io_capture) ? "capture" : " -- ");
815 }
816
817 ret = 0;
818
819rsnd_dai_probe_done:
820 of_node_put(dai_node);
821
822 return ret;
823}
824
825
826
827
828static struct snd_pcm_hardware rsnd_pcm_hardware = {
829 .info = SNDRV_PCM_INFO_INTERLEAVED |
830 SNDRV_PCM_INFO_MMAP |
831 SNDRV_PCM_INFO_MMAP_VALID,
832 .buffer_bytes_max = 64 * 1024,
833 .period_bytes_min = 32,
834 .period_bytes_max = 8192,
835 .periods_min = 1,
836 .periods_max = 32,
837 .fifo_size = 256,
838};
839
840static int rsnd_pcm_open(struct snd_pcm_substream *substream)
841{
842 struct snd_pcm_runtime *runtime = substream->runtime;
843 int ret = 0;
844
845 snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware);
846
847 ret = snd_pcm_hw_constraint_integer(runtime,
848 SNDRV_PCM_HW_PARAM_PERIODS);
849
850 return ret;
851}
852
853static int rsnd_hw_params(struct snd_pcm_substream *substream,
854 struct snd_pcm_hw_params *hw_params)
855{
856 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
857 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
858 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
859 int ret;
860
861 ret = rsnd_dai_call(hw_params, io, substream, hw_params);
862 if (ret)
863 return ret;
864
865 return snd_pcm_lib_malloc_pages(substream,
866 params_buffer_bytes(hw_params));
867}
868
869static snd_pcm_uframes_t rsnd_pointer(struct snd_pcm_substream *substream)
870{
871 struct snd_pcm_runtime *runtime = substream->runtime;
872 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
873 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
874 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
875
876 return bytes_to_frames(runtime, io->byte_pos);
877}
878
879static struct snd_pcm_ops rsnd_pcm_ops = {
880 .open = rsnd_pcm_open,
881 .ioctl = snd_pcm_lib_ioctl,
882 .hw_params = rsnd_hw_params,
883 .hw_free = snd_pcm_lib_free_pages,
884 .pointer = rsnd_pointer,
885};
886
887
888
889
890#define kcontrol_to_cfg(kctrl) ((struct rsnd_kctrl_cfg *)kctrl->private_value)
891static int rsnd_kctrl_info(struct snd_kcontrol *kctrl,
892 struct snd_ctl_elem_info *uinfo)
893{
894 struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
895
896 if (cfg->texts) {
897 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
898 uinfo->count = cfg->size;
899 uinfo->value.enumerated.items = cfg->max;
900 if (uinfo->value.enumerated.item >= cfg->max)
901 uinfo->value.enumerated.item = cfg->max - 1;
902 strlcpy(uinfo->value.enumerated.name,
903 cfg->texts[uinfo->value.enumerated.item],
904 sizeof(uinfo->value.enumerated.name));
905 } else {
906 uinfo->count = cfg->size;
907 uinfo->value.integer.min = 0;
908 uinfo->value.integer.max = cfg->max;
909 uinfo->type = (cfg->max == 1) ?
910 SNDRV_CTL_ELEM_TYPE_BOOLEAN :
911 SNDRV_CTL_ELEM_TYPE_INTEGER;
912 }
913
914 return 0;
915}
916
917static int rsnd_kctrl_get(struct snd_kcontrol *kctrl,
918 struct snd_ctl_elem_value *uc)
919{
920 struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
921 int i;
922
923 for (i = 0; i < cfg->size; i++)
924 if (cfg->texts)
925 uc->value.enumerated.item[i] = cfg->val[i];
926 else
927 uc->value.integer.value[i] = cfg->val[i];
928
929 return 0;
930}
931
932static int rsnd_kctrl_put(struct snd_kcontrol *kctrl,
933 struct snd_ctl_elem_value *uc)
934{
935 struct rsnd_mod *mod = snd_kcontrol_chip(kctrl);
936 struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
937 int i, change = 0;
938
939 for (i = 0; i < cfg->size; i++) {
940 if (cfg->texts) {
941 change |= (uc->value.enumerated.item[i] != cfg->val[i]);
942 cfg->val[i] = uc->value.enumerated.item[i];
943 } else {
944 change |= (uc->value.integer.value[i] != cfg->val[i]);
945 cfg->val[i] = uc->value.integer.value[i];
946 }
947 }
948
949 if (change && cfg->update)
950 cfg->update(cfg->io, mod);
951
952 return change;
953}
954
955static int __rsnd_kctrl_new(struct rsnd_mod *mod,
956 struct rsnd_dai_stream *io,
957 struct snd_soc_pcm_runtime *rtd,
958 const unsigned char *name,
959 struct rsnd_kctrl_cfg *cfg,
960 void (*update)(struct rsnd_dai_stream *io,
961 struct rsnd_mod *mod))
962{
963 struct snd_card *card = rtd->card->snd_card;
964 struct snd_kcontrol *kctrl;
965 struct snd_kcontrol_new knew = {
966 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
967 .name = name,
968 .info = rsnd_kctrl_info,
969 .index = rtd->num,
970 .get = rsnd_kctrl_get,
971 .put = rsnd_kctrl_put,
972 .private_value = (unsigned long)cfg,
973 };
974 int ret;
975
976 kctrl = snd_ctl_new1(&knew, mod);
977 if (!kctrl)
978 return -ENOMEM;
979
980 ret = snd_ctl_add(card, kctrl);
981 if (ret < 0) {
982 snd_ctl_free_one(kctrl);
983 return ret;
984 }
985
986 cfg->update = update;
987 cfg->card = card;
988 cfg->kctrl = kctrl;
989 cfg->io = io;
990
991 return 0;
992}
993
994void _rsnd_kctrl_remove(struct rsnd_kctrl_cfg *cfg)
995{
996 snd_ctl_remove(cfg->card, cfg->kctrl);
997}
998
999int rsnd_kctrl_new_m(struct rsnd_mod *mod,
1000 struct rsnd_dai_stream *io,
1001 struct snd_soc_pcm_runtime *rtd,
1002 const unsigned char *name,
1003 void (*update)(struct rsnd_dai_stream *io,
1004 struct rsnd_mod *mod),
1005 struct rsnd_kctrl_cfg_m *_cfg,
1006 int ch_size,
1007 u32 max)
1008{
1009 if (ch_size > RSND_MAX_CHANNELS)
1010 return -EINVAL;
1011
1012 _cfg->cfg.max = max;
1013 _cfg->cfg.size = ch_size;
1014 _cfg->cfg.val = _cfg->val;
1015 return __rsnd_kctrl_new(mod, io, rtd, name, &_cfg->cfg, update);
1016}
1017
1018int rsnd_kctrl_new_s(struct rsnd_mod *mod,
1019 struct rsnd_dai_stream *io,
1020 struct snd_soc_pcm_runtime *rtd,
1021 const unsigned char *name,
1022 void (*update)(struct rsnd_dai_stream *io,
1023 struct rsnd_mod *mod),
1024 struct rsnd_kctrl_cfg_s *_cfg,
1025 u32 max)
1026{
1027 _cfg->cfg.max = max;
1028 _cfg->cfg.size = 1;
1029 _cfg->cfg.val = &_cfg->val;
1030 return __rsnd_kctrl_new(mod, io, rtd, name, &_cfg->cfg, update);
1031}
1032
1033int rsnd_kctrl_new_e(struct rsnd_mod *mod,
1034 struct rsnd_dai_stream *io,
1035 struct snd_soc_pcm_runtime *rtd,
1036 const unsigned char *name,
1037 struct rsnd_kctrl_cfg_s *_cfg,
1038 void (*update)(struct rsnd_dai_stream *io,
1039 struct rsnd_mod *mod),
1040 const char * const *texts,
1041 u32 max)
1042{
1043 _cfg->cfg.max = max;
1044 _cfg->cfg.size = 1;
1045 _cfg->cfg.val = &_cfg->val;
1046 _cfg->cfg.texts = texts;
1047 return __rsnd_kctrl_new(mod, io, rtd, name, &_cfg->cfg, update);
1048}
1049
1050
1051
1052
1053
1054#define PREALLOC_BUFFER (32 * 1024)
1055#define PREALLOC_BUFFER_MAX (32 * 1024)
1056
1057static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd)
1058{
1059 struct snd_soc_dai *dai = rtd->cpu_dai;
1060 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1061 int ret;
1062
1063 ret = rsnd_dai_call(pcm_new, &rdai->playback, rtd);
1064 if (ret)
1065 return ret;
1066
1067 ret = rsnd_dai_call(pcm_new, &rdai->capture, rtd);
1068 if (ret)
1069 return ret;
1070
1071 return snd_pcm_lib_preallocate_pages_for_all(
1072 rtd->pcm,
1073 SNDRV_DMA_TYPE_DEV,
1074 rtd->card->snd_card->dev,
1075 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
1076}
1077
1078static struct snd_soc_platform_driver rsnd_soc_platform = {
1079 .ops = &rsnd_pcm_ops,
1080 .pcm_new = rsnd_pcm_new,
1081};
1082
1083static const struct snd_soc_component_driver rsnd_soc_component = {
1084 .name = "rsnd",
1085};
1086
1087static int rsnd_rdai_continuance_probe(struct rsnd_priv *priv,
1088 struct rsnd_dai_stream *io)
1089{
1090 int ret;
1091
1092 ret = rsnd_dai_call(probe, io, priv);
1093 if (ret == -EAGAIN) {
1094 struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io);
1095 int i;
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108 rsnd_dai_call(remove, io, priv);
1109
1110
1111
1112
1113
1114 for (i = 0; i < RSND_MOD_MAX; i++)
1115 rsnd_dai_disconnect((io)->mod[i], io, i);
1116 rsnd_dai_connect(ssi_mod, io, RSND_MOD_SSI);
1117
1118
1119
1120
1121 rsnd_dai_call(fallback, io, priv);
1122
1123
1124
1125
1126
1127 ret = rsnd_dai_call(probe, io, priv);
1128 }
1129
1130 return ret;
1131}
1132
1133
1134
1135
1136static int rsnd_probe(struct platform_device *pdev)
1137{
1138 struct rsnd_priv *priv;
1139 struct device *dev = &pdev->dev;
1140 struct rsnd_dai *rdai;
1141 int (*probe_func[])(struct rsnd_priv *priv) = {
1142 rsnd_gen_probe,
1143 rsnd_dma_probe,
1144 rsnd_ssi_probe,
1145 rsnd_ssiu_probe,
1146 rsnd_src_probe,
1147 rsnd_ctu_probe,
1148 rsnd_mix_probe,
1149 rsnd_dvc_probe,
1150 rsnd_cmd_probe,
1151 rsnd_adg_probe,
1152 rsnd_dai_probe,
1153 };
1154 int ret, i;
1155
1156
1157
1158
1159 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1160 if (!priv) {
1161 dev_err(dev, "priv allocate failed\n");
1162 return -ENODEV;
1163 }
1164
1165 priv->pdev = pdev;
1166 priv->flags = (unsigned long)of_device_get_match_data(dev);
1167 spin_lock_init(&priv->lock);
1168
1169
1170
1171
1172 for (i = 0; i < ARRAY_SIZE(probe_func); i++) {
1173 ret = probe_func[i](priv);
1174 if (ret)
1175 return ret;
1176 }
1177
1178 for_each_rsnd_dai(rdai, priv, i) {
1179 ret = rsnd_rdai_continuance_probe(priv, &rdai->playback);
1180 if (ret)
1181 goto exit_snd_probe;
1182
1183 ret = rsnd_rdai_continuance_probe(priv, &rdai->capture);
1184 if (ret)
1185 goto exit_snd_probe;
1186 }
1187
1188 dev_set_drvdata(dev, priv);
1189
1190
1191
1192
1193 ret = snd_soc_register_platform(dev, &rsnd_soc_platform);
1194 if (ret < 0) {
1195 dev_err(dev, "cannot snd soc register\n");
1196 return ret;
1197 }
1198
1199 ret = snd_soc_register_component(dev, &rsnd_soc_component,
1200 priv->daidrv, rsnd_rdai_nr(priv));
1201 if (ret < 0) {
1202 dev_err(dev, "cannot snd dai register\n");
1203 goto exit_snd_soc;
1204 }
1205
1206 pm_runtime_enable(dev);
1207
1208 dev_info(dev, "probed\n");
1209 return ret;
1210
1211exit_snd_soc:
1212 snd_soc_unregister_platform(dev);
1213exit_snd_probe:
1214 for_each_rsnd_dai(rdai, priv, i) {
1215 rsnd_dai_call(remove, &rdai->playback, priv);
1216 rsnd_dai_call(remove, &rdai->capture, priv);
1217 }
1218
1219 return ret;
1220}
1221
1222static int rsnd_remove(struct platform_device *pdev)
1223{
1224 struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
1225 struct rsnd_dai *rdai;
1226 void (*remove_func[])(struct rsnd_priv *priv) = {
1227 rsnd_ssi_remove,
1228 rsnd_ssiu_remove,
1229 rsnd_src_remove,
1230 rsnd_ctu_remove,
1231 rsnd_mix_remove,
1232 rsnd_dvc_remove,
1233 rsnd_cmd_remove,
1234 rsnd_adg_remove,
1235 };
1236 int ret = 0, i;
1237
1238 pm_runtime_disable(&pdev->dev);
1239
1240 for_each_rsnd_dai(rdai, priv, i) {
1241 ret |= rsnd_dai_call(remove, &rdai->playback, priv);
1242 ret |= rsnd_dai_call(remove, &rdai->capture, priv);
1243 }
1244
1245 for (i = 0; i < ARRAY_SIZE(remove_func); i++)
1246 remove_func[i](priv);
1247
1248 snd_soc_unregister_component(&pdev->dev);
1249 snd_soc_unregister_platform(&pdev->dev);
1250
1251 return ret;
1252}
1253
1254static struct platform_driver rsnd_driver = {
1255 .driver = {
1256 .name = "rcar_sound",
1257 .of_match_table = rsnd_of_match,
1258 },
1259 .probe = rsnd_probe,
1260 .remove = rsnd_remove,
1261};
1262module_platform_driver(rsnd_driver);
1263
1264MODULE_LICENSE("GPL");
1265MODULE_DESCRIPTION("Renesas R-Car audio driver");
1266MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
1267MODULE_ALIAS("platform:rcar-pcm-audio");
1268