2 * Renesas R-Car SRU/SCU/SSIU/SSI support
4 * Copyright (C) 2013 Renesas Solutions Corp.
5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
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
20 * SRU : Sound Routing Unit
21 * - SRC : Sampling Rate Converter
23 * - CTU : Channel Count Conversion Unit
25 * - DVC : Digital Volume and Mute Function
26 * - SSI : Serial Sound Interface
30 * SCU : Sampling Rate Converter Unit
31 * - SRC : Sampling Rate Converter
33 * - CTU : Channel Count Conversion Unit
35 * - DVC : Digital Volume and Mute Function
36 * SSIU : Serial Sound Interface Unit
37 * - SSI : Serial Sound Interface
45 * | ** this depends on Gen1/Gen2
49 * | ** these depend on data path
50 * | ** gen and platform data control it
54 * | +- playback -> [mod] -> [mod] -> [mod] -> ...
57 * | +- capture -> [mod] -> [mod] -> [mod] -> ...
61 * | +- playback -> [mod] -> [mod] -> [mod] -> ...
64 * | +- capture -> [mod] -> [mod] -> [mod] -> ...
67 * | ** these control ssi
76 * | ** these control src
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 <linux/shdma-base.h>
100 #define RSND_RATES SNDRV_PCM_RATE_8000_96000
101 #define RSND_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
103 static struct rsnd_of_data rsnd_of_data_gen1 = {
107 static struct rsnd_of_data rsnd_of_data_gen2 = {
111 static struct of_device_id rsnd_of_match[] = {
112 { .compatible = "renesas,rcar_sound-gen1", .data = &rsnd_of_data_gen1 },
113 { .compatible = "renesas,rcar_sound-gen2", .data = &rsnd_of_data_gen2 },
116 MODULE_DEVICE_TABLE(of, rsnd_of_match);
119 * rsnd_platform functions
121 #define rsnd_platform_call(priv, dai, func, param...) \
122 (!(priv->info->func) ? 0 : \
123 priv->info->func(param))
125 #define rsnd_is_enable_path(io, name) \
126 ((io)->info ? (io)->info->name : NULL)
127 #define rsnd_info_id(priv, io, name) \
128 ((io)->info->name - priv->info->name##_info)
133 char *rsnd_mod_name(struct rsnd_mod *mod)
135 if (!mod || !mod->ops)
138 return mod->ops->name;
141 char *rsnd_mod_dma_name(struct rsnd_mod *mod)
143 if (!mod || !mod->ops)
146 if (!mod->ops->dma_name)
147 return mod->ops->name;
149 return mod->ops->dma_name(mod);
152 void rsnd_mod_init(struct rsnd_priv *priv,
153 struct rsnd_mod *mod,
154 struct rsnd_mod_ops *ops,
156 enum rsnd_mod_type type,
169 void rsnd_dma_stop(struct rsnd_dma *dma)
171 dmaengine_terminate_all(dma->chan);
174 static void rsnd_dma_complete(void *data)
176 struct rsnd_dma *dma = (struct rsnd_dma *)data;
177 struct rsnd_mod *mod = rsnd_dma_to_mod(dma);
178 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
181 * Renesas sound Gen1 needs 1 DMAC,
183 * In Gen2 case, it are Audio-DMAC, and Audio-DMAC-peri-peri.
184 * But, Audio-DMAC-peri-peri doesn't have interrupt,
185 * and this driver is assuming that here.
187 * If Audio-DMAC-peri-peri has interrpt,
188 * rsnd_dai_pointer_update() will be called twice,
189 * ant it will breaks io->byte_pos
192 rsnd_dai_pointer_update(io, io->byte_per_period);
195 void rsnd_dma_start(struct rsnd_dma *dma)
197 struct rsnd_mod *mod = rsnd_dma_to_mod(dma);
198 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
199 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
200 struct snd_pcm_substream *substream = io->substream;
201 struct device *dev = rsnd_priv_to_dev(priv);
202 struct dma_async_tx_descriptor *desc;
204 desc = dmaengine_prep_dma_cyclic(dma->chan,
205 (dma->addr) ? dma->addr :
206 substream->runtime->dma_addr,
207 snd_pcm_lib_buffer_bytes(substream),
208 snd_pcm_lib_period_bytes(substream),
210 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
213 dev_err(dev, "dmaengine_prep_slave_sg() fail\n");
217 desc->callback = rsnd_dma_complete;
218 desc->callback_param = dma;
220 if (dmaengine_submit(desc) < 0) {
221 dev_err(dev, "dmaengine_submit() fail\n");
225 dma_async_issue_pending(dma->chan);
228 int rsnd_dma_available(struct rsnd_dma *dma)
233 #define DMA_NAME_SIZE 16
234 #define MOD_MAX 4 /* MEM/SSI/SRC/DVC */
235 static int _rsnd_dma_of_name(char *dma_name, struct rsnd_mod *mod)
238 return snprintf(dma_name, DMA_NAME_SIZE / 2, "%s%d",
239 rsnd_mod_dma_name(mod), rsnd_mod_id(mod));
241 return snprintf(dma_name, DMA_NAME_SIZE / 2, "mem");
245 static void rsnd_dma_of_name(struct rsnd_mod *mod_from,
246 struct rsnd_mod *mod_to,
251 index = _rsnd_dma_of_name(dma_name + index, mod_from);
252 *(dma_name + index++) = '_';
253 index = _rsnd_dma_of_name(dma_name + index, mod_to);
256 static void rsnd_dma_of_path(struct rsnd_dma *dma,
258 struct rsnd_mod **mod_from,
259 struct rsnd_mod **mod_to)
261 struct rsnd_mod *this = rsnd_dma_to_mod(dma);
262 struct rsnd_dai_stream *io = rsnd_mod_to_io(this);
263 struct rsnd_mod *ssi = rsnd_io_to_mod_ssi(io);
264 struct rsnd_mod *src = rsnd_io_to_mod_src(io);
265 struct rsnd_mod *dvc = rsnd_io_to_mod_dvc(io);
266 struct rsnd_mod *mod[MOD_MAX];
270 for (i = 0; i < MOD_MAX; i++)
280 * mem -> SRC -> DVC -> SSI
282 mod[0] = NULL; /* for "mem" */
284 for (i = 1; i < MOD_MAX; i++) {
291 if ((!is_play) && (this == src))
294 mod[i] = (is_play) ? src : dvc;
296 mod[i] = (is_play) ? dvc : src;
309 *mod_from = mod[index - 1];
310 *mod_to = mod[index];
312 *mod_from = mod[index];
313 *mod_to = mod[index - 1];
317 int rsnd_dma_init(struct rsnd_priv *priv, struct rsnd_dma *dma,
320 struct device *dev = rsnd_priv_to_dev(priv);
321 struct dma_slave_config cfg;
322 struct rsnd_mod *mod_from;
323 struct rsnd_mod *mod_to;
324 char dma_name[DMA_NAME_SIZE];
329 dev_err(dev, "it already has dma channel\n");
334 dma_cap_set(DMA_SLAVE, mask);
336 rsnd_dma_of_path(dma, is_play, &mod_from, &mod_to);
337 rsnd_dma_of_name(mod_from, mod_to, dma_name);
340 cfg.direction = is_play ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM;
341 cfg.src_addr = rsnd_gen_dma_addr(priv, mod_from, is_play, 1);
342 cfg.dst_addr = rsnd_gen_dma_addr(priv, mod_to, is_play, 0);
343 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
344 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
346 dev_dbg(dev, "dma : %s %pad -> %pad\n",
347 dma_name, &cfg.src_addr, &cfg.dst_addr);
349 dma->chan = dma_request_slave_channel_compat(mask, shdma_chan_filter,
353 dev_err(dev, "can't get dma channel\n");
354 goto rsnd_dma_channel_err;
357 ret = dmaengine_slave_config(dma->chan, &cfg);
359 goto rsnd_dma_init_err;
361 dma->addr = is_play ? cfg.src_addr : cfg.dst_addr;
362 dma->dir = is_play ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM;
367 rsnd_dma_quit(priv, dma);
368 rsnd_dma_channel_err:
371 * DMA failed. try to PIO mode
373 * rsnd_ssi_fallback()
374 * rsnd_rdai_continuance_probe()
379 void rsnd_dma_quit(struct rsnd_priv *priv,
380 struct rsnd_dma *dma)
383 dma_release_channel(dma->chan);
391 u32 rsnd_get_adinr(struct rsnd_mod *mod)
393 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
394 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
395 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
396 struct device *dev = rsnd_priv_to_dev(priv);
397 u32 adinr = runtime->channels;
399 switch (runtime->sample_bits) {
407 dev_warn(dev, "not supported sample bits\n");
417 #define __rsnd_mod_call(mod, func, rdai...) \
419 struct rsnd_priv *priv = rsnd_mod_to_priv(mod); \
420 struct device *dev = rsnd_priv_to_dev(priv); \
421 u32 mask = 1 << __rsnd_mod_shift_##func; \
422 u32 call = __rsnd_mod_call_##func << __rsnd_mod_shift_##func; \
424 if ((mod->status & mask) == call) { \
425 dev_dbg(dev, "%s[%d] %s\n", \
426 rsnd_mod_name(mod), rsnd_mod_id(mod), #func); \
427 ret = (mod)->ops->func(mod, rdai); \
428 mod->status = (mod->status & ~mask) | (~call & mask); \
433 #define rsnd_mod_call(mod, func, rdai...) \
434 (!(mod) ? -ENODEV : \
435 !((mod)->ops->func) ? 0 : \
436 __rsnd_mod_call(mod, func, rdai))
438 #define rsnd_dai_call(fn, io, rdai...) \
440 struct rsnd_mod *mod; \
442 for (i = 0; i < RSND_MOD_MAX; i++) { \
443 mod = (io)->mod[i]; \
446 ret = rsnd_mod_call(mod, fn, rdai); \
453 static int rsnd_dai_connect(struct rsnd_mod *mod,
454 struct rsnd_dai_stream *io)
459 if (io->mod[mod->type]) {
460 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
461 struct device *dev = rsnd_priv_to_dev(priv);
463 dev_err(dev, "%s%d is not empty\n",
469 io->mod[mod->type] = mod;
475 static void rsnd_dai_disconnect(struct rsnd_mod *mod,
476 struct rsnd_dai_stream *io)
479 io->mod[mod->type] = NULL;
482 struct rsnd_dai *rsnd_rdai_get(struct rsnd_priv *priv, int id)
484 if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
487 return priv->rdai + id;
490 static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai)
492 struct rsnd_priv *priv = snd_soc_dai_get_drvdata(dai);
494 return rsnd_rdai_get(priv, dai->id);
498 * rsnd_soc_dai functions
500 int rsnd_dai_pointer_offset(struct rsnd_dai_stream *io, int additional)
502 struct snd_pcm_substream *substream = io->substream;
503 struct snd_pcm_runtime *runtime = substream->runtime;
504 int pos = io->byte_pos + additional;
506 pos %= (runtime->periods * io->byte_per_period);
511 void rsnd_dai_pointer_update(struct rsnd_dai_stream *io, int byte)
513 io->byte_pos += byte;
515 if (io->byte_pos >= io->next_period_byte) {
516 struct snd_pcm_substream *substream = io->substream;
517 struct snd_pcm_runtime *runtime = substream->runtime;
520 io->next_period_byte += io->byte_per_period;
522 if (io->period_pos >= runtime->periods) {
525 io->next_period_byte = io->byte_per_period;
528 snd_pcm_period_elapsed(substream);
532 static int rsnd_dai_stream_init(struct rsnd_dai_stream *io,
533 struct snd_pcm_substream *substream)
535 struct snd_pcm_runtime *runtime = substream->runtime;
537 io->substream = substream;
540 io->byte_per_period = runtime->period_size *
542 samples_to_bytes(runtime, 1);
543 io->next_period_byte = io->byte_per_period;
549 struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream)
551 struct snd_soc_pcm_runtime *rtd = substream->private_data;
557 struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai,
558 struct snd_pcm_substream *substream)
560 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
561 return &rdai->playback;
563 return &rdai->capture;
566 static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd,
567 struct snd_soc_dai *dai)
569 struct rsnd_priv *priv = snd_soc_dai_get_drvdata(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 ssi_id = rsnd_mod_id(rsnd_io_to_mod_ssi(io));
576 rsnd_lock(priv, flags);
579 case SNDRV_PCM_TRIGGER_START:
580 ret = rsnd_dai_stream_init(io, substream);
582 goto dai_trigger_end;
584 ret = rsnd_platform_call(priv, dai, start, ssi_id);
586 goto dai_trigger_end;
588 ret = rsnd_dai_call(init, io, rdai);
590 goto dai_trigger_end;
592 ret = rsnd_dai_call(start, io, rdai);
594 goto dai_trigger_end;
596 case SNDRV_PCM_TRIGGER_STOP:
597 ret = rsnd_dai_call(stop, io, rdai);
599 goto dai_trigger_end;
601 ret = rsnd_dai_call(quit, io, rdai);
603 goto dai_trigger_end;
605 ret = rsnd_platform_call(priv, dai, stop, ssi_id);
607 goto dai_trigger_end;
614 rsnd_unlock(priv, flags);
619 static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
621 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
623 /* set master/slave audio interface */
624 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
625 case SND_SOC_DAIFMT_CBM_CFM:
626 rdai->clk_master = 0;
628 case SND_SOC_DAIFMT_CBS_CFS:
629 rdai->clk_master = 1; /* codec is slave, cpu is master */
636 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
637 case SND_SOC_DAIFMT_I2S:
639 rdai->data_alignment = 0;
640 rdai->frm_clk_inv = 0;
642 case SND_SOC_DAIFMT_LEFT_J:
644 rdai->data_alignment = 0;
645 rdai->frm_clk_inv = 1;
647 case SND_SOC_DAIFMT_RIGHT_J:
649 rdai->data_alignment = 1;
650 rdai->frm_clk_inv = 1;
654 /* set clock inversion */
655 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
656 case SND_SOC_DAIFMT_NB_IF:
657 rdai->bit_clk_inv = rdai->bit_clk_inv;
658 rdai->frm_clk_inv = !rdai->frm_clk_inv;
660 case SND_SOC_DAIFMT_IB_NF:
661 rdai->bit_clk_inv = !rdai->bit_clk_inv;
662 rdai->frm_clk_inv = rdai->frm_clk_inv;
664 case SND_SOC_DAIFMT_IB_IF:
665 rdai->bit_clk_inv = !rdai->bit_clk_inv;
666 rdai->frm_clk_inv = !rdai->frm_clk_inv;
668 case SND_SOC_DAIFMT_NB_NF:
676 static const struct snd_soc_dai_ops rsnd_soc_dai_ops = {
677 .trigger = rsnd_soc_dai_trigger,
678 .set_fmt = rsnd_soc_dai_set_fmt,
681 #define rsnd_path_parse(priv, io, type) \
683 struct rsnd_mod *mod; \
687 if (rsnd_is_enable_path(io, type)) { \
688 id = rsnd_info_id(priv, io, type); \
690 mod = rsnd_##type##_mod_get(priv, id); \
691 ret = rsnd_dai_connect(mod, io); \
697 #define rsnd_path_break(priv, io, type) \
699 struct rsnd_mod *mod; \
702 if (rsnd_is_enable_path(io, type)) { \
703 id = rsnd_info_id(priv, io, type); \
705 mod = rsnd_##type##_mod_get(priv, id); \
706 rsnd_dai_disconnect(mod, io); \
711 static int rsnd_path_init(struct rsnd_priv *priv,
712 struct rsnd_dai *rdai,
713 struct rsnd_dai_stream *io)
718 * Gen1 is created by SRU/SSI, and this SRU is base module of
719 * Gen2's SCU/SSIU/SSI. (Gen2 SCU/SSIU came from SRU)
722 * Gen1 SRU = Gen2 SCU + SSIU + etc
724 * Gen2 SCU path is very flexible, but, Gen1 SRU (SCU parts) is
729 ret = rsnd_path_parse(priv, io, src);
734 ret = rsnd_path_parse(priv, io, ssi);
739 ret = rsnd_path_parse(priv, io, dvc);
746 static void rsnd_of_parse_dai(struct platform_device *pdev,
747 const struct rsnd_of_data *of_data,
748 struct rsnd_priv *priv)
750 struct device_node *dai_node, *dai_np;
751 struct device_node *ssi_node, *ssi_np;
752 struct device_node *src_node, *src_np;
753 struct device_node *dvc_node, *dvc_np;
754 struct device_node *playback, *capture;
755 struct rsnd_dai_platform_info *dai_info;
756 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
757 struct device *dev = &pdev->dev;
759 int dai_i, ssi_i, src_i, dvc_i;
764 dai_node = of_get_child_by_name(dev->of_node, "rcar_sound,dai");
768 nr = of_get_child_count(dai_node);
772 dai_info = devm_kzalloc(dev,
773 sizeof(struct rsnd_dai_platform_info) * nr,
776 dev_err(dev, "dai info allocation error\n");
780 info->dai_info_nr = nr;
781 info->dai_info = dai_info;
783 ssi_node = of_get_child_by_name(dev->of_node, "rcar_sound,ssi");
784 src_node = of_get_child_by_name(dev->of_node, "rcar_sound,src");
785 dvc_node = of_get_child_by_name(dev->of_node, "rcar_sound,dvc");
787 #define mod_parse(name) \
789 struct rsnd_##name##_platform_info *name##_info; \
792 for_each_child_of_node(name##_node, name##_np) { \
793 name##_info = info->name##_info + name##_i; \
795 if (name##_np == playback) \
796 dai_info->playback.name = name##_info; \
797 if (name##_np == capture) \
798 dai_info->capture.name = name##_info; \
808 for_each_child_of_node(dai_node, dai_np) {
809 dai_info = info->dai_info + dai_i;
813 playback = of_parse_phandle(dai_np, "playback", i);
814 capture = of_parse_phandle(dai_np, "capture", i);
816 if (!playback && !capture)
823 of_node_put(playback);
824 of_node_put(capture);
831 static int rsnd_dai_probe(struct platform_device *pdev,
832 const struct rsnd_of_data *of_data,
833 struct rsnd_priv *priv)
835 struct snd_soc_dai_driver *drv;
836 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
837 struct rsnd_dai *rdai;
838 struct rsnd_ssi_platform_info *pmod, *cmod;
839 struct device *dev = rsnd_priv_to_dev(priv);
843 rsnd_of_parse_dai(pdev, of_data, priv);
845 dai_nr = info->dai_info_nr;
847 dev_err(dev, "no dai\n");
851 drv = devm_kzalloc(dev, sizeof(*drv) * dai_nr, GFP_KERNEL);
852 rdai = devm_kzalloc(dev, sizeof(*rdai) * dai_nr, GFP_KERNEL);
854 dev_err(dev, "dai allocate failed\n");
858 priv->rdai_nr = dai_nr;
862 for (i = 0; i < dai_nr; i++) {
864 pmod = info->dai_info[i].playback.ssi;
865 cmod = info->dai_info[i].capture.ssi;
870 snprintf(rdai[i].name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", i);
873 * init snd_soc_dai_driver
875 drv[i].name = rdai[i].name;
876 drv[i].ops = &rsnd_soc_dai_ops;
878 drv[i].playback.rates = RSND_RATES;
879 drv[i].playback.formats = RSND_FMTS;
880 drv[i].playback.channels_min = 2;
881 drv[i].playback.channels_max = 2;
883 rdai[i].playback.info = &info->dai_info[i].playback;
884 rdai[i].playback.rdai = rdai + i;
885 rsnd_path_init(priv, &rdai[i], &rdai[i].playback);
888 drv[i].capture.rates = RSND_RATES;
889 drv[i].capture.formats = RSND_FMTS;
890 drv[i].capture.channels_min = 2;
891 drv[i].capture.channels_max = 2;
893 rdai[i].capture.info = &info->dai_info[i].capture;
894 rdai[i].capture.rdai = rdai + i;
895 rsnd_path_init(priv, &rdai[i], &rdai[i].capture);
898 dev_dbg(dev, "%s (%s/%s)\n", rdai[i].name,
899 pmod ? "play" : " -- ",
900 cmod ? "capture" : " -- ");
909 static struct snd_pcm_hardware rsnd_pcm_hardware = {
910 .info = SNDRV_PCM_INFO_INTERLEAVED |
911 SNDRV_PCM_INFO_MMAP |
912 SNDRV_PCM_INFO_MMAP_VALID,
913 .buffer_bytes_max = 64 * 1024,
914 .period_bytes_min = 32,
915 .period_bytes_max = 8192,
921 static int rsnd_pcm_open(struct snd_pcm_substream *substream)
923 struct snd_pcm_runtime *runtime = substream->runtime;
926 snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware);
928 ret = snd_pcm_hw_constraint_integer(runtime,
929 SNDRV_PCM_HW_PARAM_PERIODS);
934 static int rsnd_hw_params(struct snd_pcm_substream *substream,
935 struct snd_pcm_hw_params *hw_params)
937 return snd_pcm_lib_malloc_pages(substream,
938 params_buffer_bytes(hw_params));
941 static snd_pcm_uframes_t rsnd_pointer(struct snd_pcm_substream *substream)
943 struct snd_pcm_runtime *runtime = substream->runtime;
944 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
945 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
946 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
948 return bytes_to_frames(runtime, io->byte_pos);
951 static struct snd_pcm_ops rsnd_pcm_ops = {
952 .open = rsnd_pcm_open,
953 .ioctl = snd_pcm_lib_ioctl,
954 .hw_params = rsnd_hw_params,
955 .hw_free = snd_pcm_lib_free_pages,
956 .pointer = rsnd_pointer,
962 #define kcontrol_to_cfg(kctrl) ((struct rsnd_kctrl_cfg *)kctrl->private_value)
963 static int rsnd_kctrl_info(struct snd_kcontrol *kctrl,
964 struct snd_ctl_elem_info *uinfo)
966 struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
969 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
970 uinfo->count = cfg->size;
971 uinfo->value.enumerated.items = cfg->max;
972 if (uinfo->value.enumerated.item >= cfg->max)
973 uinfo->value.enumerated.item = cfg->max - 1;
974 strlcpy(uinfo->value.enumerated.name,
975 cfg->texts[uinfo->value.enumerated.item],
976 sizeof(uinfo->value.enumerated.name));
978 uinfo->count = cfg->size;
979 uinfo->value.integer.min = 0;
980 uinfo->value.integer.max = cfg->max;
981 uinfo->type = (cfg->max == 1) ?
982 SNDRV_CTL_ELEM_TYPE_BOOLEAN :
983 SNDRV_CTL_ELEM_TYPE_INTEGER;
989 static int rsnd_kctrl_get(struct snd_kcontrol *kctrl,
990 struct snd_ctl_elem_value *uc)
992 struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
995 for (i = 0; i < cfg->size; i++)
997 uc->value.enumerated.item[i] = cfg->val[i];
999 uc->value.integer.value[i] = cfg->val[i];
1004 static int rsnd_kctrl_put(struct snd_kcontrol *kctrl,
1005 struct snd_ctl_elem_value *uc)
1007 struct rsnd_mod *mod = snd_kcontrol_chip(kctrl);
1008 struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
1011 for (i = 0; i < cfg->size; i++) {
1013 change |= (uc->value.enumerated.item[i] != cfg->val[i]);
1014 cfg->val[i] = uc->value.enumerated.item[i];
1016 change |= (uc->value.integer.value[i] != cfg->val[i]);
1017 cfg->val[i] = uc->value.integer.value[i];
1027 static int __rsnd_kctrl_new(struct rsnd_mod *mod,
1028 struct rsnd_dai *rdai,
1029 struct snd_soc_pcm_runtime *rtd,
1030 const unsigned char *name,
1031 struct rsnd_kctrl_cfg *cfg,
1032 void (*update)(struct rsnd_mod *mod))
1034 struct snd_card *card = rtd->card->snd_card;
1035 struct snd_kcontrol *kctrl;
1036 struct snd_kcontrol_new knew = {
1037 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1039 .info = rsnd_kctrl_info,
1040 .get = rsnd_kctrl_get,
1041 .put = rsnd_kctrl_put,
1042 .private_value = (unsigned long)cfg,
1046 kctrl = snd_ctl_new1(&knew, mod);
1050 ret = snd_ctl_add(card, kctrl);
1054 cfg->update = update;
1059 int rsnd_kctrl_new_m(struct rsnd_mod *mod,
1060 struct rsnd_dai *rdai,
1061 struct snd_soc_pcm_runtime *rtd,
1062 const unsigned char *name,
1063 void (*update)(struct rsnd_mod *mod),
1064 struct rsnd_kctrl_cfg_m *_cfg,
1067 _cfg->cfg.max = max;
1068 _cfg->cfg.size = RSND_DVC_CHANNELS;
1069 _cfg->cfg.val = _cfg->val;
1070 return __rsnd_kctrl_new(mod, rdai, rtd, name, &_cfg->cfg, update);
1073 int rsnd_kctrl_new_s(struct rsnd_mod *mod,
1074 struct rsnd_dai *rdai,
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,
1081 _cfg->cfg.max = max;
1083 _cfg->cfg.val = &_cfg->val;
1084 return __rsnd_kctrl_new(mod, rdai, rtd, name, &_cfg->cfg, update);
1087 int rsnd_kctrl_new_e(struct rsnd_mod *mod,
1088 struct rsnd_dai *rdai,
1089 struct snd_soc_pcm_runtime *rtd,
1090 const unsigned char *name,
1091 struct rsnd_kctrl_cfg_s *_cfg,
1092 void (*update)(struct rsnd_mod *mod),
1093 const char * const *texts,
1096 _cfg->cfg.max = max;
1098 _cfg->cfg.val = &_cfg->val;
1099 _cfg->cfg.texts = texts;
1100 return __rsnd_kctrl_new(mod, rdai, rtd, name, &_cfg->cfg, update);
1107 #define PREALLOC_BUFFER (32 * 1024)
1108 #define PREALLOC_BUFFER_MAX (32 * 1024)
1110 static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd)
1112 struct snd_soc_dai *dai = rtd->cpu_dai;
1113 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1116 ret = rsnd_dai_call(pcm_new, &rdai->playback, rdai, rtd);
1120 ret = rsnd_dai_call(pcm_new, &rdai->capture, rdai, rtd);
1124 return snd_pcm_lib_preallocate_pages_for_all(
1127 rtd->card->snd_card->dev,
1128 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
1131 static struct snd_soc_platform_driver rsnd_soc_platform = {
1132 .ops = &rsnd_pcm_ops,
1133 .pcm_new = rsnd_pcm_new,
1136 static const struct snd_soc_component_driver rsnd_soc_component = {
1140 static int rsnd_rdai_continuance_probe(struct rsnd_priv *priv,
1141 struct rsnd_dai *rdai,
1144 struct rsnd_dai_stream *io = is_play ? &rdai->playback : &rdai->capture;
1147 ret = rsnd_dai_call(probe, io, rdai);
1148 if (ret == -EAGAIN) {
1150 * Fallback to PIO mode
1154 * call "remove" for SSI/SRC/DVC
1155 * SSI will be switch to PIO mode if it was DMA mode
1158 * rsnd_ssi_fallback()
1160 rsnd_dai_call(remove, io, rdai);
1163 * remove SRC/DVC from DAI,
1165 rsnd_path_break(priv, io, src);
1166 rsnd_path_break(priv, io, dvc);
1171 rsnd_dai_call(fallback, io, rdai);
1175 * DAI has SSI which is PIO mode only now.
1177 ret = rsnd_dai_call(probe, io, rdai);
1186 static int rsnd_probe(struct platform_device *pdev)
1188 struct rcar_snd_info *info;
1189 struct rsnd_priv *priv;
1190 struct device *dev = &pdev->dev;
1191 struct rsnd_dai *rdai;
1192 const struct of_device_id *of_id = of_match_device(rsnd_of_match, dev);
1193 const struct rsnd_of_data *of_data;
1194 int (*probe_func[])(struct platform_device *pdev,
1195 const struct rsnd_of_data *of_data,
1196 struct rsnd_priv *priv) = {
1209 info = devm_kzalloc(&pdev->dev,
1210 sizeof(struct rcar_snd_info), GFP_KERNEL);
1211 of_data = of_id->data;
1213 info = pdev->dev.platform_data;
1217 dev_err(dev, "driver needs R-Car sound information\n");
1224 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1226 dev_err(dev, "priv allocate failed\n");
1232 spin_lock_init(&priv->lock);
1237 for (i = 0; i < ARRAY_SIZE(probe_func); i++) {
1238 ret = probe_func[i](pdev, of_data, priv);
1243 for_each_rsnd_dai(rdai, priv, i) {
1244 ret = rsnd_rdai_continuance_probe(priv, rdai, 1);
1246 goto exit_snd_probe;
1248 ret = rsnd_rdai_continuance_probe(priv, rdai, 0);
1250 goto exit_snd_probe;
1256 ret = snd_soc_register_platform(dev, &rsnd_soc_platform);
1258 dev_err(dev, "cannot snd soc register\n");
1262 ret = snd_soc_register_component(dev, &rsnd_soc_component,
1263 priv->daidrv, rsnd_rdai_nr(priv));
1265 dev_err(dev, "cannot snd dai register\n");
1269 dev_set_drvdata(dev, priv);
1271 pm_runtime_enable(dev);
1273 dev_info(dev, "probed\n");
1277 snd_soc_unregister_platform(dev);
1279 for_each_rsnd_dai(rdai, priv, i) {
1280 rsnd_dai_call(remove, &rdai->playback, rdai);
1281 rsnd_dai_call(remove, &rdai->capture, rdai);
1287 static int rsnd_remove(struct platform_device *pdev)
1289 struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
1290 struct rsnd_dai *rdai;
1293 pm_runtime_disable(&pdev->dev);
1295 for_each_rsnd_dai(rdai, priv, i) {
1296 ret |= rsnd_dai_call(remove, &rdai->playback, rdai);
1297 ret |= rsnd_dai_call(remove, &rdai->capture, rdai);
1303 static struct platform_driver rsnd_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");