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 int rsnd_mod_init(struct rsnd_mod *mod,
153 struct rsnd_mod_ops *ops,
155 enum rsnd_mod_type type,
158 int ret = clk_prepare(clk);
171 void rsnd_mod_quit(struct rsnd_mod *mod)
174 clk_unprepare(mod->clk);
180 void rsnd_dma_stop(struct rsnd_dma *dma)
182 dmaengine_terminate_all(dma->chan);
185 static void rsnd_dma_complete(void *data)
187 struct rsnd_dma *dma = (struct rsnd_dma *)data;
188 struct rsnd_mod *mod = rsnd_dma_to_mod(dma);
189 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
192 * Renesas sound Gen1 needs 1 DMAC,
194 * In Gen2 case, it are Audio-DMAC, and Audio-DMAC-peri-peri.
195 * But, Audio-DMAC-peri-peri doesn't have interrupt,
196 * and this driver is assuming that here.
198 * If Audio-DMAC-peri-peri has interrpt,
199 * rsnd_dai_pointer_update() will be called twice,
200 * ant it will breaks io->byte_pos
203 rsnd_dai_pointer_update(io, io->byte_per_period);
206 void rsnd_dma_start(struct rsnd_dma *dma)
208 struct rsnd_mod *mod = rsnd_dma_to_mod(dma);
209 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
210 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
211 struct snd_pcm_substream *substream = io->substream;
212 struct device *dev = rsnd_priv_to_dev(priv);
213 struct dma_async_tx_descriptor *desc;
215 desc = dmaengine_prep_dma_cyclic(dma->chan,
216 (dma->addr) ? dma->addr :
217 substream->runtime->dma_addr,
218 snd_pcm_lib_buffer_bytes(substream),
219 snd_pcm_lib_period_bytes(substream),
221 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
224 dev_err(dev, "dmaengine_prep_slave_sg() fail\n");
228 desc->callback = rsnd_dma_complete;
229 desc->callback_param = dma;
231 if (dmaengine_submit(desc) < 0) {
232 dev_err(dev, "dmaengine_submit() fail\n");
236 dma_async_issue_pending(dma->chan);
239 int rsnd_dma_available(struct rsnd_dma *dma)
244 #define DMA_NAME_SIZE 16
245 #define MOD_MAX 4 /* MEM/SSI/SRC/DVC */
246 static int _rsnd_dma_of_name(char *dma_name, struct rsnd_mod *mod)
249 return snprintf(dma_name, DMA_NAME_SIZE / 2, "%s%d",
250 rsnd_mod_dma_name(mod), rsnd_mod_id(mod));
252 return snprintf(dma_name, DMA_NAME_SIZE / 2, "mem");
256 static void rsnd_dma_of_name(struct rsnd_mod *mod_from,
257 struct rsnd_mod *mod_to,
262 index = _rsnd_dma_of_name(dma_name + index, mod_from);
263 *(dma_name + index++) = '_';
264 index = _rsnd_dma_of_name(dma_name + index, mod_to);
267 static void rsnd_dma_of_path(struct rsnd_dma *dma,
269 struct rsnd_mod **mod_from,
270 struct rsnd_mod **mod_to)
272 struct rsnd_mod *this = rsnd_dma_to_mod(dma);
273 struct rsnd_dai_stream *io = rsnd_mod_to_io(this);
274 struct rsnd_mod *ssi = rsnd_io_to_mod_ssi(io);
275 struct rsnd_mod *src = rsnd_io_to_mod_src(io);
276 struct rsnd_mod *dvc = rsnd_io_to_mod_dvc(io);
277 struct rsnd_mod *mod[MOD_MAX];
281 for (i = 0; i < MOD_MAX; i++)
291 * mem -> SRC -> DVC -> SSI
293 mod[0] = NULL; /* for "mem" */
295 for (i = 1; i < MOD_MAX; i++) {
302 if ((!is_play) && (this == src))
305 mod[i] = (is_play) ? src : dvc;
307 mod[i] = (is_play) ? dvc : src;
320 *mod_from = mod[index - 1];
321 *mod_to = mod[index];
323 *mod_from = mod[index];
324 *mod_to = mod[index - 1];
328 int rsnd_dma_init(struct rsnd_priv *priv, struct rsnd_dma *dma,
331 struct device *dev = rsnd_priv_to_dev(priv);
332 struct dma_slave_config cfg;
333 struct rsnd_mod *mod_from;
334 struct rsnd_mod *mod_to;
335 char dma_name[DMA_NAME_SIZE];
340 dev_err(dev, "it already has dma channel\n");
345 dma_cap_set(DMA_SLAVE, mask);
347 rsnd_dma_of_path(dma, is_play, &mod_from, &mod_to);
348 rsnd_dma_of_name(mod_from, mod_to, dma_name);
351 cfg.direction = is_play ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM;
352 cfg.src_addr = rsnd_gen_dma_addr(priv, mod_from, is_play, 1);
353 cfg.dst_addr = rsnd_gen_dma_addr(priv, mod_to, is_play, 0);
354 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
355 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
357 dev_dbg(dev, "dma : %s %pad -> %pad\n",
358 dma_name, &cfg.src_addr, &cfg.dst_addr);
360 dma->chan = dma_request_slave_channel_compat(mask, shdma_chan_filter,
364 dev_err(dev, "can't get dma channel\n");
365 goto rsnd_dma_channel_err;
368 ret = dmaengine_slave_config(dma->chan, &cfg);
370 goto rsnd_dma_init_err;
372 dma->addr = is_play ? cfg.src_addr : cfg.dst_addr;
373 dma->dir = is_play ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM;
378 rsnd_dma_quit(priv, dma);
379 rsnd_dma_channel_err:
382 * DMA failed. try to PIO mode
384 * rsnd_ssi_fallback()
385 * rsnd_rdai_continuance_probe()
390 void rsnd_dma_quit(struct rsnd_priv *priv,
391 struct rsnd_dma *dma)
394 dma_release_channel(dma->chan);
402 u32 rsnd_get_adinr(struct rsnd_mod *mod)
404 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
405 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
406 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
407 struct device *dev = rsnd_priv_to_dev(priv);
408 u32 adinr = runtime->channels;
410 switch (runtime->sample_bits) {
418 dev_warn(dev, "not supported sample bits\n");
428 #define __rsnd_mod_call(mod, func, param...) \
430 struct rsnd_priv *priv = rsnd_mod_to_priv(mod); \
431 struct device *dev = rsnd_priv_to_dev(priv); \
432 u32 mask = 1 << __rsnd_mod_shift_##func; \
433 u32 call = __rsnd_mod_call_##func << __rsnd_mod_shift_##func; \
435 if ((mod->status & mask) == call) { \
436 dev_dbg(dev, "%s[%d] %s\n", \
437 rsnd_mod_name(mod), rsnd_mod_id(mod), #func); \
438 ret = (mod)->ops->func(mod, param); \
439 mod->status = (mod->status & ~mask) | (~call & mask); \
444 #define rsnd_mod_call(mod, func, param...) \
445 (!(mod) ? -ENODEV : \
446 !((mod)->ops->func) ? 0 : \
447 __rsnd_mod_call(mod, func, param))
449 #define rsnd_dai_call(fn, io, param...) \
451 struct rsnd_mod *mod; \
453 for (i = 0; i < RSND_MOD_MAX; i++) { \
454 mod = (io)->mod[i]; \
457 ret = rsnd_mod_call(mod, fn, param); \
464 static int rsnd_dai_connect(struct rsnd_mod *mod,
465 struct rsnd_dai_stream *io)
470 if (io->mod[mod->type]) {
471 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
472 struct device *dev = rsnd_priv_to_dev(priv);
474 dev_err(dev, "%s%d is not empty\n",
480 io->mod[mod->type] = mod;
486 static void rsnd_dai_disconnect(struct rsnd_mod *mod,
487 struct rsnd_dai_stream *io)
490 io->mod[mod->type] = NULL;
493 struct rsnd_dai *rsnd_rdai_get(struct rsnd_priv *priv, int id)
495 if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
498 return priv->rdai + id;
501 static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai)
503 struct rsnd_priv *priv = snd_soc_dai_get_drvdata(dai);
505 return rsnd_rdai_get(priv, dai->id);
509 * rsnd_soc_dai functions
511 int rsnd_dai_pointer_offset(struct rsnd_dai_stream *io, int additional)
513 struct snd_pcm_substream *substream = io->substream;
514 struct snd_pcm_runtime *runtime = substream->runtime;
515 int pos = io->byte_pos + additional;
517 pos %= (runtime->periods * io->byte_per_period);
522 void rsnd_dai_pointer_update(struct rsnd_dai_stream *io, int byte)
524 io->byte_pos += byte;
526 if (io->byte_pos >= io->next_period_byte) {
527 struct snd_pcm_substream *substream = io->substream;
528 struct snd_pcm_runtime *runtime = substream->runtime;
531 io->next_period_byte += io->byte_per_period;
533 if (io->period_pos >= runtime->periods) {
536 io->next_period_byte = io->byte_per_period;
539 snd_pcm_period_elapsed(substream);
543 static int rsnd_dai_stream_init(struct rsnd_dai_stream *io,
544 struct snd_pcm_substream *substream)
546 struct snd_pcm_runtime *runtime = substream->runtime;
548 io->substream = substream;
551 io->byte_per_period = runtime->period_size *
553 samples_to_bytes(runtime, 1);
554 io->next_period_byte = io->byte_per_period;
560 struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream)
562 struct snd_soc_pcm_runtime *rtd = substream->private_data;
568 struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai,
569 struct snd_pcm_substream *substream)
571 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
572 return &rdai->playback;
574 return &rdai->capture;
577 static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd,
578 struct snd_soc_dai *dai)
580 struct rsnd_priv *priv = snd_soc_dai_get_drvdata(dai);
581 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
582 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
583 int ssi_id = rsnd_mod_id(rsnd_io_to_mod_ssi(io));
587 rsnd_lock(priv, flags);
590 case SNDRV_PCM_TRIGGER_START:
591 ret = rsnd_dai_stream_init(io, substream);
593 goto dai_trigger_end;
595 ret = rsnd_platform_call(priv, dai, start, ssi_id);
597 goto dai_trigger_end;
599 ret = rsnd_dai_call(init, io, priv);
601 goto dai_trigger_end;
603 ret = rsnd_dai_call(start, io, priv);
605 goto dai_trigger_end;
607 case SNDRV_PCM_TRIGGER_STOP:
608 ret = rsnd_dai_call(stop, io, priv);
610 goto dai_trigger_end;
612 ret = rsnd_dai_call(quit, io, priv);
614 goto dai_trigger_end;
616 ret = rsnd_platform_call(priv, dai, stop, ssi_id);
618 goto dai_trigger_end;
625 rsnd_unlock(priv, flags);
630 static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
632 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
634 /* set master/slave audio interface */
635 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
636 case SND_SOC_DAIFMT_CBM_CFM:
637 rdai->clk_master = 0;
639 case SND_SOC_DAIFMT_CBS_CFS:
640 rdai->clk_master = 1; /* codec is slave, cpu is master */
647 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
648 case SND_SOC_DAIFMT_I2S:
650 rdai->data_alignment = 0;
651 rdai->frm_clk_inv = 0;
653 case SND_SOC_DAIFMT_LEFT_J:
655 rdai->data_alignment = 0;
656 rdai->frm_clk_inv = 1;
658 case SND_SOC_DAIFMT_RIGHT_J:
660 rdai->data_alignment = 1;
661 rdai->frm_clk_inv = 1;
665 /* set clock inversion */
666 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
667 case SND_SOC_DAIFMT_NB_IF:
668 rdai->bit_clk_inv = rdai->bit_clk_inv;
669 rdai->frm_clk_inv = !rdai->frm_clk_inv;
671 case SND_SOC_DAIFMT_IB_NF:
672 rdai->bit_clk_inv = !rdai->bit_clk_inv;
673 rdai->frm_clk_inv = rdai->frm_clk_inv;
675 case SND_SOC_DAIFMT_IB_IF:
676 rdai->bit_clk_inv = !rdai->bit_clk_inv;
677 rdai->frm_clk_inv = !rdai->frm_clk_inv;
679 case SND_SOC_DAIFMT_NB_NF:
687 static const struct snd_soc_dai_ops rsnd_soc_dai_ops = {
688 .trigger = rsnd_soc_dai_trigger,
689 .set_fmt = rsnd_soc_dai_set_fmt,
692 #define rsnd_path_parse(priv, io, type) \
694 struct rsnd_mod *mod; \
698 if (rsnd_is_enable_path(io, type)) { \
699 id = rsnd_info_id(priv, io, type); \
701 mod = rsnd_##type##_mod_get(priv, id); \
702 ret = rsnd_dai_connect(mod, io); \
708 #define rsnd_path_break(priv, io, type) \
710 struct rsnd_mod *mod; \
713 if (rsnd_is_enable_path(io, type)) { \
714 id = rsnd_info_id(priv, io, type); \
716 mod = rsnd_##type##_mod_get(priv, id); \
717 rsnd_dai_disconnect(mod, io); \
722 static int rsnd_path_init(struct rsnd_priv *priv,
723 struct rsnd_dai *rdai,
724 struct rsnd_dai_stream *io)
729 * Gen1 is created by SRU/SSI, and this SRU is base module of
730 * Gen2's SCU/SSIU/SSI. (Gen2 SCU/SSIU came from SRU)
733 * Gen1 SRU = Gen2 SCU + SSIU + etc
735 * Gen2 SCU path is very flexible, but, Gen1 SRU (SCU parts) is
740 ret = rsnd_path_parse(priv, io, src);
745 ret = rsnd_path_parse(priv, io, ssi);
750 ret = rsnd_path_parse(priv, io, dvc);
757 static void rsnd_of_parse_dai(struct platform_device *pdev,
758 const struct rsnd_of_data *of_data,
759 struct rsnd_priv *priv)
761 struct device_node *dai_node, *dai_np;
762 struct device_node *ssi_node, *ssi_np;
763 struct device_node *src_node, *src_np;
764 struct device_node *dvc_node, *dvc_np;
765 struct device_node *playback, *capture;
766 struct rsnd_dai_platform_info *dai_info;
767 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
768 struct device *dev = &pdev->dev;
770 int dai_i, ssi_i, src_i, dvc_i;
775 dai_node = of_get_child_by_name(dev->of_node, "rcar_sound,dai");
779 nr = of_get_child_count(dai_node);
783 dai_info = devm_kzalloc(dev,
784 sizeof(struct rsnd_dai_platform_info) * nr,
787 dev_err(dev, "dai info allocation error\n");
791 info->dai_info_nr = nr;
792 info->dai_info = dai_info;
794 ssi_node = of_get_child_by_name(dev->of_node, "rcar_sound,ssi");
795 src_node = of_get_child_by_name(dev->of_node, "rcar_sound,src");
796 dvc_node = of_get_child_by_name(dev->of_node, "rcar_sound,dvc");
798 #define mod_parse(name) \
800 struct rsnd_##name##_platform_info *name##_info; \
803 for_each_child_of_node(name##_node, name##_np) { \
804 name##_info = info->name##_info + name##_i; \
806 if (name##_np == playback) \
807 dai_info->playback.name = name##_info; \
808 if (name##_np == capture) \
809 dai_info->capture.name = name##_info; \
819 for_each_child_of_node(dai_node, dai_np) {
820 dai_info = info->dai_info + dai_i;
824 playback = of_parse_phandle(dai_np, "playback", i);
825 capture = of_parse_phandle(dai_np, "capture", i);
827 if (!playback && !capture)
834 of_node_put(playback);
835 of_node_put(capture);
842 static int rsnd_dai_probe(struct platform_device *pdev,
843 const struct rsnd_of_data *of_data,
844 struct rsnd_priv *priv)
846 struct snd_soc_dai_driver *drv;
847 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
848 struct rsnd_dai *rdai;
849 struct rsnd_ssi_platform_info *pmod, *cmod;
850 struct device *dev = rsnd_priv_to_dev(priv);
854 rsnd_of_parse_dai(pdev, of_data, priv);
856 dai_nr = info->dai_info_nr;
858 dev_err(dev, "no dai\n");
862 drv = devm_kzalloc(dev, sizeof(*drv) * dai_nr, GFP_KERNEL);
863 rdai = devm_kzalloc(dev, sizeof(*rdai) * dai_nr, GFP_KERNEL);
865 dev_err(dev, "dai allocate failed\n");
869 priv->rdai_nr = dai_nr;
873 for (i = 0; i < dai_nr; i++) {
875 pmod = info->dai_info[i].playback.ssi;
876 cmod = info->dai_info[i].capture.ssi;
881 snprintf(rdai[i].name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", i);
885 * init snd_soc_dai_driver
887 drv[i].name = rdai[i].name;
888 drv[i].ops = &rsnd_soc_dai_ops;
890 drv[i].playback.rates = RSND_RATES;
891 drv[i].playback.formats = RSND_FMTS;
892 drv[i].playback.channels_min = 2;
893 drv[i].playback.channels_max = 2;
895 rdai[i].playback.info = &info->dai_info[i].playback;
896 rdai[i].playback.rdai = rdai + i;
897 rsnd_path_init(priv, &rdai[i], &rdai[i].playback);
900 drv[i].capture.rates = RSND_RATES;
901 drv[i].capture.formats = RSND_FMTS;
902 drv[i].capture.channels_min = 2;
903 drv[i].capture.channels_max = 2;
905 rdai[i].capture.info = &info->dai_info[i].capture;
906 rdai[i].capture.rdai = rdai + i;
907 rsnd_path_init(priv, &rdai[i], &rdai[i].capture);
910 dev_dbg(dev, "%s (%s/%s)\n", rdai[i].name,
911 pmod ? "play" : " -- ",
912 cmod ? "capture" : " -- ");
921 static struct snd_pcm_hardware rsnd_pcm_hardware = {
922 .info = SNDRV_PCM_INFO_INTERLEAVED |
923 SNDRV_PCM_INFO_MMAP |
924 SNDRV_PCM_INFO_MMAP_VALID,
925 .buffer_bytes_max = 64 * 1024,
926 .period_bytes_min = 32,
927 .period_bytes_max = 8192,
933 static int rsnd_pcm_open(struct snd_pcm_substream *substream)
935 struct snd_pcm_runtime *runtime = substream->runtime;
938 snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware);
940 ret = snd_pcm_hw_constraint_integer(runtime,
941 SNDRV_PCM_HW_PARAM_PERIODS);
946 static int rsnd_hw_params(struct snd_pcm_substream *substream,
947 struct snd_pcm_hw_params *hw_params)
949 return snd_pcm_lib_malloc_pages(substream,
950 params_buffer_bytes(hw_params));
953 static snd_pcm_uframes_t rsnd_pointer(struct snd_pcm_substream *substream)
955 struct snd_pcm_runtime *runtime = substream->runtime;
956 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
957 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
958 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
960 return bytes_to_frames(runtime, io->byte_pos);
963 static struct snd_pcm_ops rsnd_pcm_ops = {
964 .open = rsnd_pcm_open,
965 .ioctl = snd_pcm_lib_ioctl,
966 .hw_params = rsnd_hw_params,
967 .hw_free = snd_pcm_lib_free_pages,
968 .pointer = rsnd_pointer,
974 #define kcontrol_to_cfg(kctrl) ((struct rsnd_kctrl_cfg *)kctrl->private_value)
975 static int rsnd_kctrl_info(struct snd_kcontrol *kctrl,
976 struct snd_ctl_elem_info *uinfo)
978 struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
981 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
982 uinfo->count = cfg->size;
983 uinfo->value.enumerated.items = cfg->max;
984 if (uinfo->value.enumerated.item >= cfg->max)
985 uinfo->value.enumerated.item = cfg->max - 1;
986 strlcpy(uinfo->value.enumerated.name,
987 cfg->texts[uinfo->value.enumerated.item],
988 sizeof(uinfo->value.enumerated.name));
990 uinfo->count = cfg->size;
991 uinfo->value.integer.min = 0;
992 uinfo->value.integer.max = cfg->max;
993 uinfo->type = (cfg->max == 1) ?
994 SNDRV_CTL_ELEM_TYPE_BOOLEAN :
995 SNDRV_CTL_ELEM_TYPE_INTEGER;
1001 static int rsnd_kctrl_get(struct snd_kcontrol *kctrl,
1002 struct snd_ctl_elem_value *uc)
1004 struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
1007 for (i = 0; i < cfg->size; i++)
1009 uc->value.enumerated.item[i] = cfg->val[i];
1011 uc->value.integer.value[i] = cfg->val[i];
1016 static int rsnd_kctrl_put(struct snd_kcontrol *kctrl,
1017 struct snd_ctl_elem_value *uc)
1019 struct rsnd_mod *mod = snd_kcontrol_chip(kctrl);
1020 struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
1023 for (i = 0; i < cfg->size; i++) {
1025 change |= (uc->value.enumerated.item[i] != cfg->val[i]);
1026 cfg->val[i] = uc->value.enumerated.item[i];
1028 change |= (uc->value.integer.value[i] != cfg->val[i]);
1029 cfg->val[i] = uc->value.integer.value[i];
1039 static int __rsnd_kctrl_new(struct rsnd_mod *mod,
1040 struct snd_soc_pcm_runtime *rtd,
1041 const unsigned char *name,
1042 struct rsnd_kctrl_cfg *cfg,
1043 void (*update)(struct rsnd_mod *mod))
1045 struct snd_card *card = rtd->card->snd_card;
1046 struct snd_kcontrol *kctrl;
1047 struct snd_kcontrol_new knew = {
1048 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1050 .info = rsnd_kctrl_info,
1051 .get = rsnd_kctrl_get,
1052 .put = rsnd_kctrl_put,
1053 .private_value = (unsigned long)cfg,
1057 kctrl = snd_ctl_new1(&knew, mod);
1061 ret = snd_ctl_add(card, kctrl);
1063 snd_ctl_free_one(kctrl);
1067 cfg->update = update;
1074 void _rsnd_kctrl_remove(struct rsnd_kctrl_cfg *cfg)
1076 snd_ctl_remove(cfg->card, cfg->kctrl);
1079 int rsnd_kctrl_new_m(struct rsnd_mod *mod,
1080 struct snd_soc_pcm_runtime *rtd,
1081 const unsigned char *name,
1082 void (*update)(struct rsnd_mod *mod),
1083 struct rsnd_kctrl_cfg_m *_cfg,
1086 _cfg->cfg.max = max;
1087 _cfg->cfg.size = RSND_DVC_CHANNELS;
1088 _cfg->cfg.val = _cfg->val;
1089 return __rsnd_kctrl_new(mod, rtd, name, &_cfg->cfg, update);
1092 int rsnd_kctrl_new_s(struct rsnd_mod *mod,
1093 struct snd_soc_pcm_runtime *rtd,
1094 const unsigned char *name,
1095 void (*update)(struct rsnd_mod *mod),
1096 struct rsnd_kctrl_cfg_s *_cfg,
1099 _cfg->cfg.max = max;
1101 _cfg->cfg.val = &_cfg->val;
1102 return __rsnd_kctrl_new(mod, rtd, name, &_cfg->cfg, update);
1105 int rsnd_kctrl_new_e(struct rsnd_mod *mod,
1106 struct snd_soc_pcm_runtime *rtd,
1107 const unsigned char *name,
1108 struct rsnd_kctrl_cfg_s *_cfg,
1109 void (*update)(struct rsnd_mod *mod),
1110 const char * const *texts,
1113 _cfg->cfg.max = max;
1115 _cfg->cfg.val = &_cfg->val;
1116 _cfg->cfg.texts = texts;
1117 return __rsnd_kctrl_new(mod, rtd, name, &_cfg->cfg, update);
1124 #define PREALLOC_BUFFER (32 * 1024)
1125 #define PREALLOC_BUFFER_MAX (32 * 1024)
1127 static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd)
1129 struct snd_soc_dai *dai = rtd->cpu_dai;
1130 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1133 ret = rsnd_dai_call(pcm_new, &rdai->playback, rtd);
1137 ret = rsnd_dai_call(pcm_new, &rdai->capture, rtd);
1141 return snd_pcm_lib_preallocate_pages_for_all(
1144 rtd->card->snd_card->dev,
1145 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
1148 static struct snd_soc_platform_driver rsnd_soc_platform = {
1149 .ops = &rsnd_pcm_ops,
1150 .pcm_new = rsnd_pcm_new,
1153 static const struct snd_soc_component_driver rsnd_soc_component = {
1157 static int rsnd_rdai_continuance_probe(struct rsnd_priv *priv,
1158 struct rsnd_dai_stream *io)
1162 ret = rsnd_dai_call(probe, io, priv);
1163 if (ret == -EAGAIN) {
1165 * Fallback to PIO mode
1169 * call "remove" for SSI/SRC/DVC
1170 * SSI will be switch to PIO mode if it was DMA mode
1173 * rsnd_ssi_fallback()
1175 rsnd_dai_call(remove, io, priv);
1178 * remove SRC/DVC from DAI,
1180 rsnd_path_break(priv, io, src);
1181 rsnd_path_break(priv, io, dvc);
1186 rsnd_dai_call(fallback, io, priv);
1190 * DAI has SSI which is PIO mode only now.
1192 ret = rsnd_dai_call(probe, io, priv);
1201 static int rsnd_probe(struct platform_device *pdev)
1203 struct rcar_snd_info *info;
1204 struct rsnd_priv *priv;
1205 struct device *dev = &pdev->dev;
1206 struct rsnd_dai *rdai;
1207 const struct of_device_id *of_id = of_match_device(rsnd_of_match, dev);
1208 const struct rsnd_of_data *of_data;
1209 int (*probe_func[])(struct platform_device *pdev,
1210 const struct rsnd_of_data *of_data,
1211 struct rsnd_priv *priv) = {
1224 info = devm_kzalloc(&pdev->dev,
1225 sizeof(struct rcar_snd_info), GFP_KERNEL);
1226 of_data = of_id->data;
1228 info = pdev->dev.platform_data;
1232 dev_err(dev, "driver needs R-Car sound information\n");
1239 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1241 dev_err(dev, "priv allocate failed\n");
1247 spin_lock_init(&priv->lock);
1252 for (i = 0; i < ARRAY_SIZE(probe_func); i++) {
1253 ret = probe_func[i](pdev, of_data, priv);
1258 for_each_rsnd_dai(rdai, priv, i) {
1259 ret = rsnd_rdai_continuance_probe(priv, &rdai->playback);
1261 goto exit_snd_probe;
1263 ret = rsnd_rdai_continuance_probe(priv, &rdai->capture);
1265 goto exit_snd_probe;
1268 dev_set_drvdata(dev, priv);
1273 ret = snd_soc_register_platform(dev, &rsnd_soc_platform);
1275 dev_err(dev, "cannot snd soc register\n");
1279 ret = snd_soc_register_component(dev, &rsnd_soc_component,
1280 priv->daidrv, rsnd_rdai_nr(priv));
1282 dev_err(dev, "cannot snd dai register\n");
1286 pm_runtime_enable(dev);
1288 dev_info(dev, "probed\n");
1292 snd_soc_unregister_platform(dev);
1294 for_each_rsnd_dai(rdai, priv, i) {
1295 rsnd_dai_call(remove, &rdai->playback, priv);
1296 rsnd_dai_call(remove, &rdai->capture, priv);
1302 static int rsnd_remove(struct platform_device *pdev)
1304 struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
1305 struct rsnd_dai *rdai;
1306 void (*remove_func[])(struct platform_device *pdev,
1307 struct rsnd_priv *priv) = {
1314 pm_runtime_disable(&pdev->dev);
1316 for_each_rsnd_dai(rdai, priv, i) {
1317 ret |= rsnd_dai_call(remove, &rdai->playback, priv);
1318 ret |= rsnd_dai_call(remove, &rdai->capture, priv);
1321 for (i = 0; i < ARRAY_SIZE(remove_func); i++)
1322 remove_func[i](pdev, priv);
1324 snd_soc_unregister_component(&pdev->dev);
1325 snd_soc_unregister_platform(&pdev->dev);
1330 static struct platform_driver rsnd_driver = {
1332 .name = "rcar_sound",
1333 .of_match_table = rsnd_of_match,
1335 .probe = rsnd_probe,
1336 .remove = rsnd_remove,
1338 module_platform_driver(rsnd_driver);
1340 MODULE_LICENSE("GPL");
1341 MODULE_DESCRIPTION("Renesas R-Car audio driver");
1342 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
1343 MODULE_ALIAS("platform:rcar-pcm-audio");