ASoC: rsnd: remove un-needed parameter from rsnd_dma_quit()
[linux-2.6/btrfs-unstable.git] / sound / soc / sh / rcar / core.c
blob7db686d0cbd8053215e9cd801ac0800679fc4920
1 /*
2 * Renesas R-Car SRU/SCU/SSIU/SSI support
4 * Copyright (C) 2013 Renesas Solutions Corp.
5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
7 * Based on fsi.c
8 * Kuninori Morimoto <morimoto.kuninori@renesas.com>
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.
16 * Renesas R-Car sound device structure
18 * Gen1
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
28 * Gen2
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
41 * driver data Image
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 * ...
86 * for_each_rsnd_dai(xx, priv, xx)
87 * rdai[0] => rdai[1] => rdai[2] => ...
89 * for_each_rsnd_mod(xx, rdai, xx)
90 * [mod] => [mod] => [mod] => ...
92 * rsnd_dai_call(xxx, fn )
93 * [mod]->fn() -> [mod]->fn() -> [mod]->fn()...
96 #include <linux/pm_runtime.h>
97 #include "rsnd.h"
99 #define RSND_RATES SNDRV_PCM_RATE_8000_96000
100 #define RSND_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
102 static struct rsnd_of_data rsnd_of_data_gen1 = {
103 .flags = RSND_GEN1,
106 static struct rsnd_of_data rsnd_of_data_gen2 = {
107 .flags = RSND_GEN2,
110 static struct of_device_id rsnd_of_match[] = {
111 { .compatible = "renesas,rcar_sound-gen1", .data = &rsnd_of_data_gen1 },
112 { .compatible = "renesas,rcar_sound-gen2", .data = &rsnd_of_data_gen2 },
115 MODULE_DEVICE_TABLE(of, rsnd_of_match);
118 * rsnd_platform functions
120 #define rsnd_platform_call(priv, dai, func, param...) \
121 (!(priv->info->func) ? 0 : \
122 priv->info->func(param))
124 #define rsnd_is_enable_path(io, name) \
125 ((io)->info ? (io)->info->name : NULL)
126 #define rsnd_info_id(priv, io, name) \
127 ((io)->info->name - priv->info->name##_info)
130 * rsnd_mod functions
132 char *rsnd_mod_name(struct rsnd_mod *mod)
134 if (!mod || !mod->ops)
135 return "unknown";
137 return mod->ops->name;
140 char *rsnd_mod_dma_name(struct rsnd_mod *mod)
142 if (!mod || !mod->ops)
143 return "unknown";
145 if (!mod->ops->dma_name)
146 return mod->ops->name;
148 return mod->ops->dma_name(mod);
151 void rsnd_mod_init(struct rsnd_mod *mod,
152 struct rsnd_mod_ops *ops,
153 struct clk *clk,
154 enum rsnd_mod_type type,
155 int id)
157 mod->id = id;
158 mod->ops = ops;
159 mod->type = type;
160 mod->clk = clk;
164 * rsnd_dma functions
166 void rsnd_dma_stop(struct rsnd_dma *dma)
168 dmaengine_terminate_all(dma->chan);
171 static void rsnd_dma_complete(void *data)
173 struct rsnd_dma *dma = (struct rsnd_dma *)data;
174 struct rsnd_mod *mod = rsnd_dma_to_mod(dma);
175 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
178 * Renesas sound Gen1 needs 1 DMAC,
179 * Gen2 needs 2 DMAC.
180 * In Gen2 case, it are Audio-DMAC, and Audio-DMAC-peri-peri.
181 * But, Audio-DMAC-peri-peri doesn't have interrupt,
182 * and this driver is assuming that here.
184 * If Audio-DMAC-peri-peri has interrpt,
185 * rsnd_dai_pointer_update() will be called twice,
186 * ant it will breaks io->byte_pos
189 rsnd_dai_pointer_update(io, io->byte_per_period);
192 void rsnd_dma_start(struct rsnd_dma *dma)
194 struct rsnd_mod *mod = rsnd_dma_to_mod(dma);
195 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
196 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
197 struct snd_pcm_substream *substream = io->substream;
198 struct device *dev = rsnd_priv_to_dev(priv);
199 struct dma_async_tx_descriptor *desc;
201 desc = dmaengine_prep_dma_cyclic(dma->chan,
202 (dma->addr) ? dma->addr :
203 substream->runtime->dma_addr,
204 snd_pcm_lib_buffer_bytes(substream),
205 snd_pcm_lib_period_bytes(substream),
206 dma->dir,
207 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
209 if (!desc) {
210 dev_err(dev, "dmaengine_prep_slave_sg() fail\n");
211 return;
214 desc->callback = rsnd_dma_complete;
215 desc->callback_param = dma;
217 if (dmaengine_submit(desc) < 0) {
218 dev_err(dev, "dmaengine_submit() fail\n");
219 return;
222 dma_async_issue_pending(dma->chan);
225 #define DMA_NAME_SIZE 16
226 #define MOD_MAX 4 /* MEM/SSI/SRC/DVC */
227 static int _rsnd_dma_of_name(char *dma_name, struct rsnd_mod *mod)
229 if (mod)
230 return snprintf(dma_name, DMA_NAME_SIZE / 2, "%s%d",
231 rsnd_mod_dma_name(mod), rsnd_mod_id(mod));
232 else
233 return snprintf(dma_name, DMA_NAME_SIZE / 2, "mem");
237 static void rsnd_dma_of_name(struct rsnd_mod *mod_from,
238 struct rsnd_mod *mod_to,
239 char *dma_name)
241 int index = 0;
243 index = _rsnd_dma_of_name(dma_name + index, mod_from);
244 *(dma_name + index++) = '_';
245 index = _rsnd_dma_of_name(dma_name + index, mod_to);
248 static void rsnd_dma_of_path(struct rsnd_dma *dma,
249 int is_play,
250 struct rsnd_mod **mod_from,
251 struct rsnd_mod **mod_to)
253 struct rsnd_mod *this = rsnd_dma_to_mod(dma);
254 struct rsnd_dai_stream *io = rsnd_mod_to_io(this);
255 struct rsnd_mod *ssi = rsnd_io_to_mod_ssi(io);
256 struct rsnd_mod *src = rsnd_io_to_mod_src(io);
257 struct rsnd_mod *dvc = rsnd_io_to_mod_dvc(io);
258 struct rsnd_mod *mod[MOD_MAX];
259 int i, index;
262 for (i = 0; i < MOD_MAX; i++)
263 mod[i] = NULL;
266 * in play case...
268 * src -> dst
270 * mem -> SSI
271 * mem -> SRC -> SSI
272 * mem -> SRC -> DVC -> SSI
274 mod[0] = NULL; /* for "mem" */
275 index = 1;
276 for (i = 1; i < MOD_MAX; i++) {
277 if (!src) {
278 mod[i] = ssi;
279 } else if (!dvc) {
280 mod[i] = src;
281 src = NULL;
282 } else {
283 if ((!is_play) && (this == src))
284 this = dvc;
286 mod[i] = (is_play) ? src : dvc;
287 i++;
288 mod[i] = (is_play) ? dvc : src;
289 src = NULL;
290 dvc = NULL;
293 if (mod[i] == this)
294 index = i;
296 if (mod[i] == ssi)
297 break;
300 if (is_play) {
301 *mod_from = mod[index - 1];
302 *mod_to = mod[index];
303 } else {
304 *mod_from = mod[index];
305 *mod_to = mod[index - 1];
309 int rsnd_dma_init(struct rsnd_priv *priv, struct rsnd_dma *dma, int id)
311 struct device *dev = rsnd_priv_to_dev(priv);
312 struct dma_slave_config cfg;
313 struct rsnd_mod *mod = rsnd_dma_to_mod(dma);
314 struct rsnd_mod *mod_from;
315 struct rsnd_mod *mod_to;
316 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
317 int is_play = rsnd_io_is_play(io);
318 char dma_name[DMA_NAME_SIZE];
319 dma_cap_mask_t mask;
320 int ret;
322 if (dma->chan) {
323 dev_err(dev, "it already has dma channel\n");
324 return -EIO;
327 dma_cap_zero(mask);
328 dma_cap_set(DMA_SLAVE, mask);
330 rsnd_dma_of_path(dma, is_play, &mod_from, &mod_to);
331 rsnd_dma_of_name(mod_from, mod_to, dma_name);
333 cfg.slave_id = id;
334 cfg.direction = is_play ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM;
335 cfg.src_addr = rsnd_gen_dma_addr(priv, mod_from, is_play, 1);
336 cfg.dst_addr = rsnd_gen_dma_addr(priv, mod_to, is_play, 0);
337 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
338 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
340 dev_dbg(dev, "dma : %s %pad -> %pad\n",
341 dma_name, &cfg.src_addr, &cfg.dst_addr);
343 dma->chan = dma_request_slave_channel_compat(mask, shdma_chan_filter,
344 (void *)id, dev,
345 dma_name);
346 if (!dma->chan) {
347 dev_err(dev, "can't get dma channel\n");
348 goto rsnd_dma_channel_err;
351 ret = dmaengine_slave_config(dma->chan, &cfg);
352 if (ret < 0)
353 goto rsnd_dma_init_err;
355 dma->addr = is_play ? cfg.src_addr : cfg.dst_addr;
356 dma->dir = is_play ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM;
358 return 0;
360 rsnd_dma_init_err:
361 rsnd_dma_quit(dma);
362 rsnd_dma_channel_err:
365 * DMA failed. try to PIO mode
366 * see
367 * rsnd_ssi_fallback()
368 * rsnd_rdai_continuance_probe()
370 return -EAGAIN;
373 void rsnd_dma_quit(struct rsnd_dma *dma)
375 if (dma->chan)
376 dma_release_channel(dma->chan);
378 dma->chan = NULL;
382 * settting function
384 u32 rsnd_get_adinr(struct rsnd_mod *mod)
386 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
387 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
388 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
389 struct device *dev = rsnd_priv_to_dev(priv);
390 u32 adinr = runtime->channels;
392 switch (runtime->sample_bits) {
393 case 16:
394 adinr |= (8 << 16);
395 break;
396 case 32:
397 adinr |= (0 << 16);
398 break;
399 default:
400 dev_warn(dev, "not supported sample bits\n");
401 return 0;
404 return adinr;
408 * rsnd_dai functions
410 #define __rsnd_mod_call(mod, func, param...) \
411 ({ \
412 struct rsnd_priv *priv = rsnd_mod_to_priv(mod); \
413 struct device *dev = rsnd_priv_to_dev(priv); \
414 u32 mask = 1 << __rsnd_mod_shift_##func; \
415 u32 call = __rsnd_mod_call_##func << __rsnd_mod_shift_##func; \
416 int ret = 0; \
417 if ((mod->status & mask) == call) { \
418 dev_dbg(dev, "%s[%d] %s\n", \
419 rsnd_mod_name(mod), rsnd_mod_id(mod), #func); \
420 ret = (mod)->ops->func(mod, param); \
421 mod->status = (mod->status & ~mask) | (~call & mask); \
423 ret; \
426 #define rsnd_mod_call(mod, func, param...) \
427 (!(mod) ? -ENODEV : \
428 !((mod)->ops->func) ? 0 : \
429 __rsnd_mod_call(mod, func, param))
431 #define rsnd_dai_call(fn, io, param...) \
432 ({ \
433 struct rsnd_mod *mod; \
434 int ret = 0, i; \
435 for (i = 0; i < RSND_MOD_MAX; i++) { \
436 mod = (io)->mod[i]; \
437 if (!mod) \
438 continue; \
439 ret = rsnd_mod_call(mod, fn, param); \
440 if (ret < 0) \
441 break; \
443 ret; \
446 static int rsnd_dai_connect(struct rsnd_mod *mod,
447 struct rsnd_dai_stream *io)
449 if (!mod)
450 return -EIO;
452 if (io->mod[mod->type]) {
453 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
454 struct device *dev = rsnd_priv_to_dev(priv);
456 dev_err(dev, "%s%d is not empty\n",
457 rsnd_mod_name(mod),
458 rsnd_mod_id(mod));
459 return -EIO;
462 io->mod[mod->type] = mod;
463 mod->io = io;
465 return 0;
468 static void rsnd_dai_disconnect(struct rsnd_mod *mod,
469 struct rsnd_dai_stream *io)
471 mod->io = NULL;
472 io->mod[mod->type] = NULL;
475 struct rsnd_dai *rsnd_rdai_get(struct rsnd_priv *priv, int id)
477 if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
478 return NULL;
480 return priv->rdai + id;
483 static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai)
485 struct rsnd_priv *priv = snd_soc_dai_get_drvdata(dai);
487 return rsnd_rdai_get(priv, dai->id);
491 * rsnd_soc_dai functions
493 int rsnd_dai_pointer_offset(struct rsnd_dai_stream *io, int additional)
495 struct snd_pcm_substream *substream = io->substream;
496 struct snd_pcm_runtime *runtime = substream->runtime;
497 int pos = io->byte_pos + additional;
499 pos %= (runtime->periods * io->byte_per_period);
501 return pos;
504 void rsnd_dai_pointer_update(struct rsnd_dai_stream *io, int byte)
506 io->byte_pos += byte;
508 if (io->byte_pos >= io->next_period_byte) {
509 struct snd_pcm_substream *substream = io->substream;
510 struct snd_pcm_runtime *runtime = substream->runtime;
512 io->period_pos++;
513 io->next_period_byte += io->byte_per_period;
515 if (io->period_pos >= runtime->periods) {
516 io->byte_pos = 0;
517 io->period_pos = 0;
518 io->next_period_byte = io->byte_per_period;
521 snd_pcm_period_elapsed(substream);
525 static int rsnd_dai_stream_init(struct rsnd_dai_stream *io,
526 struct snd_pcm_substream *substream)
528 struct snd_pcm_runtime *runtime = substream->runtime;
530 io->substream = substream;
531 io->byte_pos = 0;
532 io->period_pos = 0;
533 io->byte_per_period = runtime->period_size *
534 runtime->channels *
535 samples_to_bytes(runtime, 1);
536 io->next_period_byte = io->byte_per_period;
538 return 0;
541 static
542 struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream)
544 struct snd_soc_pcm_runtime *rtd = substream->private_data;
546 return rtd->cpu_dai;
549 static
550 struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai,
551 struct snd_pcm_substream *substream)
553 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
554 return &rdai->playback;
555 else
556 return &rdai->capture;
559 static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd,
560 struct snd_soc_dai *dai)
562 struct rsnd_priv *priv = snd_soc_dai_get_drvdata(dai);
563 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
564 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
565 int ssi_id = rsnd_mod_id(rsnd_io_to_mod_ssi(io));
566 int ret;
567 unsigned long flags;
569 rsnd_lock(priv, flags);
571 switch (cmd) {
572 case SNDRV_PCM_TRIGGER_START:
573 ret = rsnd_dai_stream_init(io, substream);
574 if (ret < 0)
575 goto dai_trigger_end;
577 ret = rsnd_platform_call(priv, dai, start, ssi_id);
578 if (ret < 0)
579 goto dai_trigger_end;
581 ret = rsnd_dai_call(init, io, priv);
582 if (ret < 0)
583 goto dai_trigger_end;
585 ret = rsnd_dai_call(start, io, priv);
586 if (ret < 0)
587 goto dai_trigger_end;
588 break;
589 case SNDRV_PCM_TRIGGER_STOP:
590 ret = rsnd_dai_call(stop, io, priv);
591 if (ret < 0)
592 goto dai_trigger_end;
594 ret = rsnd_dai_call(quit, io, priv);
595 if (ret < 0)
596 goto dai_trigger_end;
598 ret = rsnd_platform_call(priv, dai, stop, ssi_id);
599 if (ret < 0)
600 goto dai_trigger_end;
601 break;
602 default:
603 ret = -EINVAL;
606 dai_trigger_end:
607 rsnd_unlock(priv, flags);
609 return ret;
612 static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
614 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
616 /* set master/slave audio interface */
617 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
618 case SND_SOC_DAIFMT_CBM_CFM:
619 rdai->clk_master = 0;
620 break;
621 case SND_SOC_DAIFMT_CBS_CFS:
622 rdai->clk_master = 1; /* codec is slave, cpu is master */
623 break;
624 default:
625 return -EINVAL;
628 /* set format */
629 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
630 case SND_SOC_DAIFMT_I2S:
631 rdai->sys_delay = 0;
632 rdai->data_alignment = 0;
633 rdai->frm_clk_inv = 0;
634 break;
635 case SND_SOC_DAIFMT_LEFT_J:
636 rdai->sys_delay = 1;
637 rdai->data_alignment = 0;
638 rdai->frm_clk_inv = 1;
639 break;
640 case SND_SOC_DAIFMT_RIGHT_J:
641 rdai->sys_delay = 1;
642 rdai->data_alignment = 1;
643 rdai->frm_clk_inv = 1;
644 break;
647 /* set clock inversion */
648 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
649 case SND_SOC_DAIFMT_NB_IF:
650 rdai->bit_clk_inv = rdai->bit_clk_inv;
651 rdai->frm_clk_inv = !rdai->frm_clk_inv;
652 break;
653 case SND_SOC_DAIFMT_IB_NF:
654 rdai->bit_clk_inv = !rdai->bit_clk_inv;
655 rdai->frm_clk_inv = rdai->frm_clk_inv;
656 break;
657 case SND_SOC_DAIFMT_IB_IF:
658 rdai->bit_clk_inv = !rdai->bit_clk_inv;
659 rdai->frm_clk_inv = !rdai->frm_clk_inv;
660 break;
661 case SND_SOC_DAIFMT_NB_NF:
662 default:
663 break;
666 return 0;
669 static const struct snd_soc_dai_ops rsnd_soc_dai_ops = {
670 .trigger = rsnd_soc_dai_trigger,
671 .set_fmt = rsnd_soc_dai_set_fmt,
674 #define rsnd_path_parse(priv, io, type) \
675 ({ \
676 struct rsnd_mod *mod; \
677 int ret = 0; \
678 int id = -1; \
680 if (rsnd_is_enable_path(io, type)) { \
681 id = rsnd_info_id(priv, io, type); \
682 if (id >= 0) { \
683 mod = rsnd_##type##_mod_get(priv, id); \
684 ret = rsnd_dai_connect(mod, io); \
687 ret; \
690 #define rsnd_path_break(priv, io, type) \
692 struct rsnd_mod *mod; \
693 int id = -1; \
695 if (rsnd_is_enable_path(io, type)) { \
696 id = rsnd_info_id(priv, io, type); \
697 if (id >= 0) { \
698 mod = rsnd_##type##_mod_get(priv, id); \
699 rsnd_dai_disconnect(mod, io); \
704 static int rsnd_path_init(struct rsnd_priv *priv,
705 struct rsnd_dai *rdai,
706 struct rsnd_dai_stream *io)
708 int ret;
711 * Gen1 is created by SRU/SSI, and this SRU is base module of
712 * Gen2's SCU/SSIU/SSI. (Gen2 SCU/SSIU came from SRU)
714 * Easy image is..
715 * Gen1 SRU = Gen2 SCU + SSIU + etc
717 * Gen2 SCU path is very flexible, but, Gen1 SRU (SCU parts) is
718 * using fixed path.
721 /* SRC */
722 ret = rsnd_path_parse(priv, io, src);
723 if (ret < 0)
724 return ret;
726 /* SSI */
727 ret = rsnd_path_parse(priv, io, ssi);
728 if (ret < 0)
729 return ret;
731 /* DVC */
732 ret = rsnd_path_parse(priv, io, dvc);
733 if (ret < 0)
734 return ret;
736 return ret;
739 static void rsnd_of_parse_dai(struct platform_device *pdev,
740 const struct rsnd_of_data *of_data,
741 struct rsnd_priv *priv)
743 struct device_node *dai_node, *dai_np;
744 struct device_node *ssi_node, *ssi_np;
745 struct device_node *src_node, *src_np;
746 struct device_node *dvc_node, *dvc_np;
747 struct device_node *playback, *capture;
748 struct rsnd_dai_platform_info *dai_info;
749 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
750 struct device *dev = &pdev->dev;
751 int nr, i;
752 int dai_i, ssi_i, src_i, dvc_i;
754 if (!of_data)
755 return;
757 dai_node = of_get_child_by_name(dev->of_node, "rcar_sound,dai");
758 if (!dai_node)
759 return;
761 nr = of_get_child_count(dai_node);
762 if (!nr)
763 return;
765 dai_info = devm_kzalloc(dev,
766 sizeof(struct rsnd_dai_platform_info) * nr,
767 GFP_KERNEL);
768 if (!dai_info) {
769 dev_err(dev, "dai info allocation error\n");
770 return;
773 info->dai_info_nr = nr;
774 info->dai_info = dai_info;
776 ssi_node = of_get_child_by_name(dev->of_node, "rcar_sound,ssi");
777 src_node = of_get_child_by_name(dev->of_node, "rcar_sound,src");
778 dvc_node = of_get_child_by_name(dev->of_node, "rcar_sound,dvc");
780 #define mod_parse(name) \
781 if (name##_node) { \
782 struct rsnd_##name##_platform_info *name##_info; \
784 name##_i = 0; \
785 for_each_child_of_node(name##_node, name##_np) { \
786 name##_info = info->name##_info + name##_i; \
788 if (name##_np == playback) \
789 dai_info->playback.name = name##_info; \
790 if (name##_np == capture) \
791 dai_info->capture.name = name##_info; \
793 name##_i++; \
798 * parse all dai
800 dai_i = 0;
801 for_each_child_of_node(dai_node, dai_np) {
802 dai_info = info->dai_info + dai_i;
804 for (i = 0;; i++) {
806 playback = of_parse_phandle(dai_np, "playback", i);
807 capture = of_parse_phandle(dai_np, "capture", i);
809 if (!playback && !capture)
810 break;
812 mod_parse(ssi);
813 mod_parse(src);
814 mod_parse(dvc);
816 of_node_put(playback);
817 of_node_put(capture);
820 dai_i++;
824 static int rsnd_dai_probe(struct platform_device *pdev,
825 const struct rsnd_of_data *of_data,
826 struct rsnd_priv *priv)
828 struct snd_soc_dai_driver *drv;
829 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
830 struct rsnd_dai *rdai;
831 struct rsnd_ssi_platform_info *pmod, *cmod;
832 struct device *dev = rsnd_priv_to_dev(priv);
833 int dai_nr;
834 int i;
836 rsnd_of_parse_dai(pdev, of_data, priv);
838 dai_nr = info->dai_info_nr;
839 if (!dai_nr) {
840 dev_err(dev, "no dai\n");
841 return -EIO;
844 drv = devm_kzalloc(dev, sizeof(*drv) * dai_nr, GFP_KERNEL);
845 rdai = devm_kzalloc(dev, sizeof(*rdai) * dai_nr, GFP_KERNEL);
846 if (!drv || !rdai) {
847 dev_err(dev, "dai allocate failed\n");
848 return -ENOMEM;
851 priv->rdai_nr = dai_nr;
852 priv->daidrv = drv;
853 priv->rdai = rdai;
855 for (i = 0; i < dai_nr; i++) {
857 pmod = info->dai_info[i].playback.ssi;
858 cmod = info->dai_info[i].capture.ssi;
861 * init rsnd_dai
863 snprintf(rdai[i].name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", i);
864 rdai[i].priv = priv;
867 * init snd_soc_dai_driver
869 drv[i].name = rdai[i].name;
870 drv[i].ops = &rsnd_soc_dai_ops;
871 if (pmod) {
872 drv[i].playback.rates = RSND_RATES;
873 drv[i].playback.formats = RSND_FMTS;
874 drv[i].playback.channels_min = 2;
875 drv[i].playback.channels_max = 2;
877 rdai[i].playback.info = &info->dai_info[i].playback;
878 rdai[i].playback.rdai = rdai + i;
879 rsnd_path_init(priv, &rdai[i], &rdai[i].playback);
881 if (cmod) {
882 drv[i].capture.rates = RSND_RATES;
883 drv[i].capture.formats = RSND_FMTS;
884 drv[i].capture.channels_min = 2;
885 drv[i].capture.channels_max = 2;
887 rdai[i].capture.info = &info->dai_info[i].capture;
888 rdai[i].capture.rdai = rdai + i;
889 rsnd_path_init(priv, &rdai[i], &rdai[i].capture);
892 dev_dbg(dev, "%s (%s/%s)\n", rdai[i].name,
893 pmod ? "play" : " -- ",
894 cmod ? "capture" : " -- ");
897 return 0;
901 * pcm ops
903 static struct snd_pcm_hardware rsnd_pcm_hardware = {
904 .info = SNDRV_PCM_INFO_INTERLEAVED |
905 SNDRV_PCM_INFO_MMAP |
906 SNDRV_PCM_INFO_MMAP_VALID,
907 .buffer_bytes_max = 64 * 1024,
908 .period_bytes_min = 32,
909 .period_bytes_max = 8192,
910 .periods_min = 1,
911 .periods_max = 32,
912 .fifo_size = 256,
915 static int rsnd_pcm_open(struct snd_pcm_substream *substream)
917 struct snd_pcm_runtime *runtime = substream->runtime;
918 int ret = 0;
920 snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware);
922 ret = snd_pcm_hw_constraint_integer(runtime,
923 SNDRV_PCM_HW_PARAM_PERIODS);
925 return ret;
928 static int rsnd_hw_params(struct snd_pcm_substream *substream,
929 struct snd_pcm_hw_params *hw_params)
931 return snd_pcm_lib_malloc_pages(substream,
932 params_buffer_bytes(hw_params));
935 static snd_pcm_uframes_t rsnd_pointer(struct snd_pcm_substream *substream)
937 struct snd_pcm_runtime *runtime = substream->runtime;
938 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
939 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
940 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
942 return bytes_to_frames(runtime, io->byte_pos);
945 static struct snd_pcm_ops rsnd_pcm_ops = {
946 .open = rsnd_pcm_open,
947 .ioctl = snd_pcm_lib_ioctl,
948 .hw_params = rsnd_hw_params,
949 .hw_free = snd_pcm_lib_free_pages,
950 .pointer = rsnd_pointer,
954 * snd_kcontrol
956 #define kcontrol_to_cfg(kctrl) ((struct rsnd_kctrl_cfg *)kctrl->private_value)
957 static int rsnd_kctrl_info(struct snd_kcontrol *kctrl,
958 struct snd_ctl_elem_info *uinfo)
960 struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
962 if (cfg->texts) {
963 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
964 uinfo->count = cfg->size;
965 uinfo->value.enumerated.items = cfg->max;
966 if (uinfo->value.enumerated.item >= cfg->max)
967 uinfo->value.enumerated.item = cfg->max - 1;
968 strlcpy(uinfo->value.enumerated.name,
969 cfg->texts[uinfo->value.enumerated.item],
970 sizeof(uinfo->value.enumerated.name));
971 } else {
972 uinfo->count = cfg->size;
973 uinfo->value.integer.min = 0;
974 uinfo->value.integer.max = cfg->max;
975 uinfo->type = (cfg->max == 1) ?
976 SNDRV_CTL_ELEM_TYPE_BOOLEAN :
977 SNDRV_CTL_ELEM_TYPE_INTEGER;
980 return 0;
983 static int rsnd_kctrl_get(struct snd_kcontrol *kctrl,
984 struct snd_ctl_elem_value *uc)
986 struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
987 int i;
989 for (i = 0; i < cfg->size; i++)
990 if (cfg->texts)
991 uc->value.enumerated.item[i] = cfg->val[i];
992 else
993 uc->value.integer.value[i] = cfg->val[i];
995 return 0;
998 static int rsnd_kctrl_put(struct snd_kcontrol *kctrl,
999 struct snd_ctl_elem_value *uc)
1001 struct rsnd_mod *mod = snd_kcontrol_chip(kctrl);
1002 struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
1003 int i, change = 0;
1005 for (i = 0; i < cfg->size; i++) {
1006 if (cfg->texts) {
1007 change |= (uc->value.enumerated.item[i] != cfg->val[i]);
1008 cfg->val[i] = uc->value.enumerated.item[i];
1009 } else {
1010 change |= (uc->value.integer.value[i] != cfg->val[i]);
1011 cfg->val[i] = uc->value.integer.value[i];
1015 if (change)
1016 cfg->update(mod);
1018 return change;
1021 static int __rsnd_kctrl_new(struct rsnd_mod *mod,
1022 struct snd_soc_pcm_runtime *rtd,
1023 const unsigned char *name,
1024 struct rsnd_kctrl_cfg *cfg,
1025 void (*update)(struct rsnd_mod *mod))
1027 struct snd_card *card = rtd->card->snd_card;
1028 struct snd_kcontrol *kctrl;
1029 struct snd_kcontrol_new knew = {
1030 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1031 .name = name,
1032 .info = rsnd_kctrl_info,
1033 .get = rsnd_kctrl_get,
1034 .put = rsnd_kctrl_put,
1035 .private_value = (unsigned long)cfg,
1037 int ret;
1039 kctrl = snd_ctl_new1(&knew, mod);
1040 if (!kctrl)
1041 return -ENOMEM;
1043 ret = snd_ctl_add(card, kctrl);
1044 if (ret < 0) {
1045 snd_ctl_free_one(kctrl);
1046 return ret;
1049 cfg->update = update;
1050 cfg->card = card;
1051 cfg->kctrl = kctrl;
1053 return 0;
1056 void _rsnd_kctrl_remove(struct rsnd_kctrl_cfg *cfg)
1058 snd_ctl_remove(cfg->card, cfg->kctrl);
1061 int rsnd_kctrl_new_m(struct rsnd_mod *mod,
1062 struct snd_soc_pcm_runtime *rtd,
1063 const unsigned char *name,
1064 void (*update)(struct rsnd_mod *mod),
1065 struct rsnd_kctrl_cfg_m *_cfg,
1066 u32 max)
1068 _cfg->cfg.max = max;
1069 _cfg->cfg.size = RSND_DVC_CHANNELS;
1070 _cfg->cfg.val = _cfg->val;
1071 return __rsnd_kctrl_new(mod, rtd, name, &_cfg->cfg, update);
1074 int rsnd_kctrl_new_s(struct rsnd_mod *mod,
1075 struct snd_soc_pcm_runtime *rtd,
1076 const unsigned char *name,
1077 void (*update)(struct rsnd_mod *mod),
1078 struct rsnd_kctrl_cfg_s *_cfg,
1079 u32 max)
1081 _cfg->cfg.max = max;
1082 _cfg->cfg.size = 1;
1083 _cfg->cfg.val = &_cfg->val;
1084 return __rsnd_kctrl_new(mod, rtd, name, &_cfg->cfg, update);
1087 int rsnd_kctrl_new_e(struct rsnd_mod *mod,
1088 struct snd_soc_pcm_runtime *rtd,
1089 const unsigned char *name,
1090 struct rsnd_kctrl_cfg_s *_cfg,
1091 void (*update)(struct rsnd_mod *mod),
1092 const char * const *texts,
1093 u32 max)
1095 _cfg->cfg.max = max;
1096 _cfg->cfg.size = 1;
1097 _cfg->cfg.val = &_cfg->val;
1098 _cfg->cfg.texts = texts;
1099 return __rsnd_kctrl_new(mod, rtd, name, &_cfg->cfg, update);
1103 * snd_soc_platform
1106 #define PREALLOC_BUFFER (32 * 1024)
1107 #define PREALLOC_BUFFER_MAX (32 * 1024)
1109 static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd)
1111 struct snd_soc_dai *dai = rtd->cpu_dai;
1112 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1113 int ret;
1115 ret = rsnd_dai_call(pcm_new, &rdai->playback, rtd);
1116 if (ret)
1117 return ret;
1119 ret = rsnd_dai_call(pcm_new, &rdai->capture, rtd);
1120 if (ret)
1121 return ret;
1123 return snd_pcm_lib_preallocate_pages_for_all(
1124 rtd->pcm,
1125 SNDRV_DMA_TYPE_DEV,
1126 rtd->card->snd_card->dev,
1127 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
1130 static struct snd_soc_platform_driver rsnd_soc_platform = {
1131 .ops = &rsnd_pcm_ops,
1132 .pcm_new = rsnd_pcm_new,
1135 static const struct snd_soc_component_driver rsnd_soc_component = {
1136 .name = "rsnd",
1139 static int rsnd_rdai_continuance_probe(struct rsnd_priv *priv,
1140 struct rsnd_dai_stream *io)
1142 int ret;
1144 ret = rsnd_dai_call(probe, io, priv);
1145 if (ret == -EAGAIN) {
1147 * Fallback to PIO mode
1151 * call "remove" for SSI/SRC/DVC
1152 * SSI will be switch to PIO mode if it was DMA mode
1153 * see
1154 * rsnd_dma_init()
1155 * rsnd_ssi_fallback()
1157 rsnd_dai_call(remove, io, priv);
1160 * remove SRC/DVC from DAI,
1162 rsnd_path_break(priv, io, src);
1163 rsnd_path_break(priv, io, dvc);
1166 * fallback
1168 rsnd_dai_call(fallback, io, priv);
1171 * retry to "probe".
1172 * DAI has SSI which is PIO mode only now.
1174 ret = rsnd_dai_call(probe, io, priv);
1177 return ret;
1181 * rsnd probe
1183 static int rsnd_probe(struct platform_device *pdev)
1185 struct rcar_snd_info *info;
1186 struct rsnd_priv *priv;
1187 struct device *dev = &pdev->dev;
1188 struct rsnd_dai *rdai;
1189 const struct of_device_id *of_id = of_match_device(rsnd_of_match, dev);
1190 const struct rsnd_of_data *of_data;
1191 int (*probe_func[])(struct platform_device *pdev,
1192 const struct rsnd_of_data *of_data,
1193 struct rsnd_priv *priv) = {
1194 rsnd_gen_probe,
1195 rsnd_ssi_probe,
1196 rsnd_src_probe,
1197 rsnd_dvc_probe,
1198 rsnd_adg_probe,
1199 rsnd_dai_probe,
1201 int ret, i;
1203 info = NULL;
1204 of_data = NULL;
1205 if (of_id) {
1206 info = devm_kzalloc(&pdev->dev,
1207 sizeof(struct rcar_snd_info), GFP_KERNEL);
1208 of_data = of_id->data;
1209 } else {
1210 info = pdev->dev.platform_data;
1213 if (!info) {
1214 dev_err(dev, "driver needs R-Car sound information\n");
1215 return -ENODEV;
1219 * init priv data
1221 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1222 if (!priv) {
1223 dev_err(dev, "priv allocate failed\n");
1224 return -ENODEV;
1227 priv->pdev = pdev;
1228 priv->info = info;
1229 spin_lock_init(&priv->lock);
1232 * init each module
1234 for (i = 0; i < ARRAY_SIZE(probe_func); i++) {
1235 ret = probe_func[i](pdev, of_data, priv);
1236 if (ret)
1237 return ret;
1240 for_each_rsnd_dai(rdai, priv, i) {
1241 ret = rsnd_rdai_continuance_probe(priv, &rdai->playback);
1242 if (ret)
1243 goto exit_snd_probe;
1245 ret = rsnd_rdai_continuance_probe(priv, &rdai->capture);
1246 if (ret)
1247 goto exit_snd_probe;
1251 * asoc register
1253 ret = snd_soc_register_platform(dev, &rsnd_soc_platform);
1254 if (ret < 0) {
1255 dev_err(dev, "cannot snd soc register\n");
1256 return ret;
1259 ret = snd_soc_register_component(dev, &rsnd_soc_component,
1260 priv->daidrv, rsnd_rdai_nr(priv));
1261 if (ret < 0) {
1262 dev_err(dev, "cannot snd dai register\n");
1263 goto exit_snd_soc;
1266 dev_set_drvdata(dev, priv);
1268 pm_runtime_enable(dev);
1270 dev_info(dev, "probed\n");
1271 return ret;
1273 exit_snd_soc:
1274 snd_soc_unregister_platform(dev);
1275 exit_snd_probe:
1276 for_each_rsnd_dai(rdai, priv, i) {
1277 rsnd_dai_call(remove, &rdai->playback, priv);
1278 rsnd_dai_call(remove, &rdai->capture, priv);
1281 return ret;
1284 static int rsnd_remove(struct platform_device *pdev)
1286 struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
1287 struct rsnd_dai *rdai;
1288 int ret = 0, i;
1290 pm_runtime_disable(&pdev->dev);
1292 for_each_rsnd_dai(rdai, priv, i) {
1293 ret |= rsnd_dai_call(remove, &rdai->playback, priv);
1294 ret |= rsnd_dai_call(remove, &rdai->capture, priv);
1297 snd_soc_unregister_component(&pdev->dev);
1298 snd_soc_unregister_platform(&pdev->dev);
1300 return ret;
1303 static struct platform_driver rsnd_driver = {
1304 .driver = {
1305 .name = "rcar_sound",
1306 .of_match_table = rsnd_of_match,
1308 .probe = rsnd_probe,
1309 .remove = rsnd_remove,
1311 module_platform_driver(rsnd_driver);
1313 MODULE_LICENSE("GPL");
1314 MODULE_DESCRIPTION("Renesas R-Car audio driver");
1315 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
1316 MODULE_ALIAS("platform:rcar-pcm-audio");