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