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>
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 const struct rsnd_of_data rsnd_of_data_gen1 = {
106 static const struct rsnd_of_data rsnd_of_data_gen2 = {
110 static const 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 },
113 { .compatible = "renesas,rcar_sound-gen3", .data = &rsnd_of_data_gen2 }, /* gen2 compatible */
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)
130 void rsnd_mod_make_sure(struct rsnd_mod *mod, enum rsnd_mod_type type)
132 if (mod->type != type) {
133 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
134 struct device *dev = rsnd_priv_to_dev(priv);
136 dev_warn(dev, "%s[%d] is not your expected module\n",
137 rsnd_mod_name(mod), rsnd_mod_id(mod));
144 char *rsnd_mod_name(struct rsnd_mod *mod)
146 if (!mod || !mod->ops)
149 return mod->ops->name;
152 struct dma_chan *rsnd_mod_dma_req(struct rsnd_dai_stream *io,
153 struct rsnd_mod *mod)
155 if (!mod || !mod->ops || !mod->ops->dma_req)
158 return mod->ops->dma_req(io, mod);
161 int rsnd_mod_init(struct rsnd_priv *priv,
162 struct rsnd_mod *mod,
163 struct rsnd_mod_ops *ops,
165 enum rsnd_mod_type type,
168 int ret = clk_prepare(clk);
182 void rsnd_mod_quit(struct rsnd_mod *mod)
185 clk_unprepare(mod->clk);
188 void rsnd_mod_interrupt(struct rsnd_mod *mod,
189 void (*callback)(struct rsnd_mod *mod,
190 struct rsnd_dai_stream *io))
192 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
193 struct rsnd_dai_stream *io;
194 struct rsnd_dai *rdai;
197 for_each_rsnd_dai(rdai, priv, i) {
198 io = &rdai->playback;
199 if (mod == io->mod[mod->type])
203 if (mod == io->mod[mod->type])
208 int rsnd_io_is_working(struct rsnd_dai_stream *io)
210 /* see rsnd_dai_stream_init/quit() */
211 return !!io->substream;
217 u32 rsnd_get_adinr_bit(struct rsnd_mod *mod, struct rsnd_dai_stream *io)
219 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
220 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
221 struct device *dev = rsnd_priv_to_dev(priv);
222 u32 adinr = runtime->channels;
224 switch (runtime->sample_bits) {
232 dev_warn(dev, "not supported sample bits\n");
239 u32 rsnd_get_adinr_chan(struct rsnd_mod *mod, struct rsnd_dai_stream *io)
241 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
242 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
243 struct device *dev = rsnd_priv_to_dev(priv);
244 u32 chan = runtime->channels;
254 dev_warn(dev, "not supported channel\n");
265 u32 rsnd_get_dalign(struct rsnd_mod *mod, struct rsnd_dai_stream *io)
267 struct rsnd_mod *src = rsnd_io_to_mod_src(io);
268 struct rsnd_mod *ssi = rsnd_io_to_mod_ssi(io);
269 struct rsnd_mod *target = src ? src : ssi;
270 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
271 u32 val = 0x76543210;
274 mask <<= runtime->channels * 4;
277 switch (runtime->sample_bits) {
279 val |= 0x67452301 & ~mask;
282 val |= 0x76543210 & ~mask;
287 * exchange channeles on SRC if possible,
288 * otherwise, R/L volume settings on DVC
289 * changes inverted channels
300 #define rsnd_mod_call(idx, io, func, param...) \
302 struct rsnd_priv *priv = rsnd_mod_to_priv(mod); \
303 struct rsnd_mod *mod = (io)->mod[idx]; \
304 struct device *dev = rsnd_priv_to_dev(priv); \
305 u32 *status = (io)->mod_status + idx; \
306 u32 mask = 0xF << __rsnd_mod_shift_##func; \
307 u8 val = (*status >> __rsnd_mod_shift_##func) & 0xF; \
308 u8 add = ((val + __rsnd_mod_add_##func) & 0xF); \
310 int call = (val == __rsnd_mod_call_##func) && (mod)->ops->func; \
311 *status = (*status & ~mask) + \
312 (add << __rsnd_mod_shift_##func); \
313 dev_dbg(dev, "%s[%d]\t0x%08x %s\n", \
314 rsnd_mod_name(mod), rsnd_mod_id(mod), \
315 *status, call ? #func : ""); \
317 ret = (mod)->ops->func(mod, io, param); \
321 #define rsnd_dai_call(fn, io, param...) \
323 struct rsnd_mod *mod; \
325 for (i = 0; i < RSND_MOD_MAX; i++) { \
326 mod = (io)->mod[i]; \
329 ret |= rsnd_mod_call(i, io, fn, param); \
334 int rsnd_dai_connect(struct rsnd_mod *mod,
335 struct rsnd_dai_stream *io,
336 enum rsnd_mod_type type)
338 struct rsnd_priv *priv;
344 priv = rsnd_mod_to_priv(mod);
345 dev = rsnd_priv_to_dev(priv);
349 dev_dbg(dev, "%s[%d] is connected to io (%s)\n",
350 rsnd_mod_name(mod), rsnd_mod_id(mod),
351 rsnd_io_is_play(io) ? "Playback" : "Capture");
356 static void rsnd_dai_disconnect(struct rsnd_mod *mod,
357 struct rsnd_dai_stream *io,
358 enum rsnd_mod_type type)
360 io->mod[type] = NULL;
363 struct rsnd_dai *rsnd_rdai_get(struct rsnd_priv *priv, int id)
365 if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
368 return priv->rdai + id;
371 #define rsnd_dai_to_priv(dai) snd_soc_dai_get_drvdata(dai)
372 static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai)
374 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
376 return rsnd_rdai_get(priv, dai->id);
380 * rsnd_soc_dai functions
382 int rsnd_dai_pointer_offset(struct rsnd_dai_stream *io, int additional)
384 struct snd_pcm_substream *substream = io->substream;
385 struct snd_pcm_runtime *runtime = substream->runtime;
386 int pos = io->byte_pos + additional;
388 pos %= (runtime->periods * io->byte_per_period);
393 bool rsnd_dai_pointer_update(struct rsnd_dai_stream *io, int byte)
395 io->byte_pos += byte;
397 if (io->byte_pos >= io->next_period_byte) {
398 struct snd_pcm_substream *substream = io->substream;
399 struct snd_pcm_runtime *runtime = substream->runtime;
402 io->next_period_byte += io->byte_per_period;
404 if (io->period_pos >= runtime->periods) {
407 io->next_period_byte = io->byte_per_period;
416 void rsnd_dai_period_elapsed(struct rsnd_dai_stream *io)
418 struct snd_pcm_substream *substream = io->substream;
421 * this function should be called...
423 * - if rsnd_dai_pointer_update() returns true
424 * - without spin lock
427 snd_pcm_period_elapsed(substream);
430 static void rsnd_dai_stream_init(struct rsnd_dai_stream *io,
431 struct snd_pcm_substream *substream)
433 struct snd_pcm_runtime *runtime = substream->runtime;
435 io->substream = substream;
438 io->byte_per_period = runtime->period_size *
440 samples_to_bytes(runtime, 1);
441 io->next_period_byte = io->byte_per_period;
444 static void rsnd_dai_stream_quit(struct rsnd_dai_stream *io)
446 io->substream = NULL;
450 struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream)
452 struct snd_soc_pcm_runtime *rtd = substream->private_data;
458 struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai,
459 struct snd_pcm_substream *substream)
461 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
462 return &rdai->playback;
464 return &rdai->capture;
467 static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd,
468 struct snd_soc_dai *dai)
470 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
471 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
472 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
473 int ssi_id = rsnd_mod_id(rsnd_io_to_mod_ssi(io));
477 spin_lock_irqsave(&priv->lock, flags);
480 case SNDRV_PCM_TRIGGER_START:
481 rsnd_dai_stream_init(io, substream);
483 ret = rsnd_platform_call(priv, dai, start, ssi_id);
485 goto dai_trigger_end;
487 ret = rsnd_dai_call(init, io, priv);
489 goto dai_trigger_end;
491 ret = rsnd_dai_call(start, io, priv);
493 goto dai_trigger_end;
495 case SNDRV_PCM_TRIGGER_STOP:
496 ret = rsnd_dai_call(stop, io, priv);
498 ret |= rsnd_dai_call(quit, io, priv);
500 ret |= rsnd_platform_call(priv, dai, stop, ssi_id);
502 rsnd_dai_stream_quit(io);
509 spin_unlock_irqrestore(&priv->lock, flags);
514 static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
516 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
518 /* set master/slave audio interface */
519 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
520 case SND_SOC_DAIFMT_CBM_CFM:
521 rdai->clk_master = 0;
523 case SND_SOC_DAIFMT_CBS_CFS:
524 rdai->clk_master = 1; /* codec is slave, cpu is master */
531 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
532 case SND_SOC_DAIFMT_I2S:
534 rdai->data_alignment = 0;
535 rdai->frm_clk_inv = 0;
537 case SND_SOC_DAIFMT_LEFT_J:
539 rdai->data_alignment = 0;
540 rdai->frm_clk_inv = 1;
542 case SND_SOC_DAIFMT_RIGHT_J:
544 rdai->data_alignment = 1;
545 rdai->frm_clk_inv = 1;
549 /* set clock inversion */
550 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
551 case SND_SOC_DAIFMT_NB_IF:
552 rdai->bit_clk_inv = rdai->bit_clk_inv;
553 rdai->frm_clk_inv = !rdai->frm_clk_inv;
555 case SND_SOC_DAIFMT_IB_NF:
556 rdai->bit_clk_inv = !rdai->bit_clk_inv;
557 rdai->frm_clk_inv = rdai->frm_clk_inv;
559 case SND_SOC_DAIFMT_IB_IF:
560 rdai->bit_clk_inv = !rdai->bit_clk_inv;
561 rdai->frm_clk_inv = !rdai->frm_clk_inv;
563 case SND_SOC_DAIFMT_NB_NF:
571 static const struct snd_soc_dai_ops rsnd_soc_dai_ops = {
572 .trigger = rsnd_soc_dai_trigger,
573 .set_fmt = rsnd_soc_dai_set_fmt,
576 #define rsnd_path_add(priv, io, _type) \
578 struct rsnd_mod *mod; \
582 if (rsnd_is_enable_path(io, _type)) { \
583 id = rsnd_info_id(priv, io, _type); \
585 mod = rsnd_##_type##_mod_get(priv, id); \
586 ret = rsnd_dai_connect(mod, io, mod->type);\
592 void rsnd_path_parse(struct rsnd_priv *priv,
593 struct rsnd_dai_stream *io)
595 struct rsnd_mod *dvc = rsnd_io_to_mod_dvc(io);
596 struct rsnd_mod *mix = rsnd_io_to_mod_mix(io);
597 struct rsnd_mod *src = rsnd_io_to_mod_src(io);
598 struct rsnd_mod *cmd;
599 struct device *dev = rsnd_priv_to_dev(priv);
602 /* Gen1 is not supported */
603 if (rsnd_is_gen1(priv))
610 struct rsnd_dai *rdai;
622 * it is assuming that integrater is well understanding about
623 * data path. Here doesn't check impossible connection,
627 for_each_rsnd_dai(rdai, priv, i) {
628 io = &rdai->playback;
629 if (mix == rsnd_io_to_mod_mix(io))
630 data |= path[rsnd_mod_id(src)];
633 if (mix == rsnd_io_to_mod_mix(io))
634 data |= path[rsnd_mod_id(src)];
638 * We can't use ctu = rsnd_io_ctu() here.
639 * Since, ID of dvc/mix are 0 or 1 (= same as CMD number)
640 * but ctu IDs are 0 - 7 (= CTU00 - CTU13)
653 data = path[rsnd_mod_id(src)];
658 dev_dbg(dev, "ctu/mix path = 0x%08x", data);
660 rsnd_mod_write(cmd, CMD_ROUTE_SLCT, data);
662 rsnd_mod_write(cmd, CMD_CTRL, 0x10);
665 static int rsnd_path_init(struct rsnd_priv *priv,
666 struct rsnd_dai *rdai,
667 struct rsnd_dai_stream *io)
672 * Gen1 is created by SRU/SSI, and this SRU is base module of
673 * Gen2's SCU/SSIU/SSI. (Gen2 SCU/SSIU came from SRU)
676 * Gen1 SRU = Gen2 SCU + SSIU + etc
678 * Gen2 SCU path is very flexible, but, Gen1 SRU (SCU parts) is
683 ret = rsnd_path_add(priv, io, ssi);
688 ret = rsnd_path_add(priv, io, src);
693 ret = rsnd_path_add(priv, io, ctu);
698 ret = rsnd_path_add(priv, io, mix);
703 ret = rsnd_path_add(priv, io, dvc);
710 static void rsnd_of_parse_dai(struct platform_device *pdev,
711 const struct rsnd_of_data *of_data,
712 struct rsnd_priv *priv)
714 struct device_node *dai_node, *dai_np;
715 struct device_node *ssi_node, *ssi_np;
716 struct device_node *src_node, *src_np;
717 struct device_node *ctu_node, *ctu_np;
718 struct device_node *mix_node, *mix_np;
719 struct device_node *dvc_node, *dvc_np;
720 struct device_node *playback, *capture;
721 struct rsnd_dai_platform_info *dai_info;
722 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
723 struct device *dev = &pdev->dev;
725 int dai_i, ssi_i, src_i, ctu_i, mix_i, dvc_i;
730 dai_node = of_get_child_by_name(dev->of_node, "rcar_sound,dai");
734 nr = of_get_child_count(dai_node);
738 dai_info = devm_kzalloc(dev,
739 sizeof(struct rsnd_dai_platform_info) * nr,
742 dev_err(dev, "dai info allocation error\n");
746 info->dai_info_nr = nr;
747 info->dai_info = dai_info;
749 ssi_node = of_get_child_by_name(dev->of_node, "rcar_sound,ssi");
750 src_node = of_get_child_by_name(dev->of_node, "rcar_sound,src");
751 ctu_node = of_get_child_by_name(dev->of_node, "rcar_sound,ctu");
752 mix_node = of_get_child_by_name(dev->of_node, "rcar_sound,mix");
753 dvc_node = of_get_child_by_name(dev->of_node, "rcar_sound,dvc");
755 #define mod_parse(name) \
757 struct rsnd_##name##_platform_info *name##_info; \
760 for_each_child_of_node(name##_node, name##_np) { \
761 name##_info = info->name##_info + name##_i; \
763 if (name##_np == playback) \
764 dai_info->playback.name = name##_info; \
765 if (name##_np == capture) \
766 dai_info->capture.name = name##_info; \
776 for_each_child_of_node(dai_node, dai_np) {
777 dai_info = info->dai_info + dai_i;
781 playback = of_parse_phandle(dai_np, "playback", i);
782 capture = of_parse_phandle(dai_np, "capture", i);
784 if (!playback && !capture)
793 of_node_put(playback);
794 of_node_put(capture);
801 static int rsnd_dai_probe(struct platform_device *pdev,
802 const struct rsnd_of_data *of_data,
803 struct rsnd_priv *priv)
805 struct snd_soc_dai_driver *drv;
806 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
807 struct rsnd_dai *rdai;
808 struct rsnd_ssi_platform_info *pmod, *cmod;
809 struct device *dev = rsnd_priv_to_dev(priv);
813 rsnd_of_parse_dai(pdev, of_data, priv);
815 dai_nr = info->dai_info_nr;
817 dev_err(dev, "no dai\n");
821 drv = devm_kzalloc(dev, sizeof(*drv) * dai_nr, GFP_KERNEL);
822 rdai = devm_kzalloc(dev, sizeof(*rdai) * dai_nr, GFP_KERNEL);
824 dev_err(dev, "dai allocate failed\n");
828 priv->rdai_nr = dai_nr;
832 for (i = 0; i < dai_nr; i++) {
834 pmod = info->dai_info[i].playback.ssi;
835 cmod = info->dai_info[i].capture.ssi;
840 snprintf(rdai[i].name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", i);
844 * init snd_soc_dai_driver
846 drv[i].name = rdai[i].name;
847 drv[i].ops = &rsnd_soc_dai_ops;
849 snprintf(rdai[i].playback.name, RSND_DAI_NAME_SIZE,
850 "DAI%d Playback", i);
852 drv[i].playback.rates = RSND_RATES;
853 drv[i].playback.formats = RSND_FMTS;
854 drv[i].playback.channels_min = 2;
855 drv[i].playback.channels_max = 2;
856 drv[i].playback.stream_name = rdai[i].playback.name;
858 rdai[i].playback.info = &info->dai_info[i].playback;
859 rdai[i].playback.rdai = rdai + i;
860 rsnd_path_init(priv, &rdai[i], &rdai[i].playback);
863 snprintf(rdai[i].capture.name, RSND_DAI_NAME_SIZE,
866 drv[i].capture.rates = RSND_RATES;
867 drv[i].capture.formats = RSND_FMTS;
868 drv[i].capture.channels_min = 2;
869 drv[i].capture.channels_max = 2;
870 drv[i].capture.stream_name = rdai[i].capture.name;
872 rdai[i].capture.info = &info->dai_info[i].capture;
873 rdai[i].capture.rdai = rdai + i;
874 rsnd_path_init(priv, &rdai[i], &rdai[i].capture);
877 dev_dbg(dev, "%s (%s/%s)\n", rdai[i].name,
878 pmod ? "play" : " -- ",
879 cmod ? "capture" : " -- ");
888 static struct snd_pcm_hardware rsnd_pcm_hardware = {
889 .info = SNDRV_PCM_INFO_INTERLEAVED |
890 SNDRV_PCM_INFO_MMAP |
891 SNDRV_PCM_INFO_MMAP_VALID,
892 .buffer_bytes_max = 64 * 1024,
893 .period_bytes_min = 32,
894 .period_bytes_max = 8192,
900 static int rsnd_pcm_open(struct snd_pcm_substream *substream)
902 struct snd_pcm_runtime *runtime = substream->runtime;
905 snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware);
907 ret = snd_pcm_hw_constraint_integer(runtime,
908 SNDRV_PCM_HW_PARAM_PERIODS);
913 static int rsnd_hw_params(struct snd_pcm_substream *substream,
914 struct snd_pcm_hw_params *hw_params)
916 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
917 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
918 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
921 ret = rsnd_dai_call(hw_params, io, substream, hw_params);
925 return snd_pcm_lib_malloc_pages(substream,
926 params_buffer_bytes(hw_params));
929 static snd_pcm_uframes_t rsnd_pointer(struct snd_pcm_substream *substream)
931 struct snd_pcm_runtime *runtime = substream->runtime;
932 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
933 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
934 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
936 return bytes_to_frames(runtime, io->byte_pos);
939 static struct snd_pcm_ops rsnd_pcm_ops = {
940 .open = rsnd_pcm_open,
941 .ioctl = snd_pcm_lib_ioctl,
942 .hw_params = rsnd_hw_params,
943 .hw_free = snd_pcm_lib_free_pages,
944 .pointer = rsnd_pointer,
950 #define kcontrol_to_cfg(kctrl) ((struct rsnd_kctrl_cfg *)kctrl->private_value)
951 static int rsnd_kctrl_info(struct snd_kcontrol *kctrl,
952 struct snd_ctl_elem_info *uinfo)
954 struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
957 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
958 uinfo->count = cfg->size;
959 uinfo->value.enumerated.items = cfg->max;
960 if (uinfo->value.enumerated.item >= cfg->max)
961 uinfo->value.enumerated.item = cfg->max - 1;
962 strlcpy(uinfo->value.enumerated.name,
963 cfg->texts[uinfo->value.enumerated.item],
964 sizeof(uinfo->value.enumerated.name));
966 uinfo->count = cfg->size;
967 uinfo->value.integer.min = 0;
968 uinfo->value.integer.max = cfg->max;
969 uinfo->type = (cfg->max == 1) ?
970 SNDRV_CTL_ELEM_TYPE_BOOLEAN :
971 SNDRV_CTL_ELEM_TYPE_INTEGER;
977 static int rsnd_kctrl_get(struct snd_kcontrol *kctrl,
978 struct snd_ctl_elem_value *uc)
980 struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
983 for (i = 0; i < cfg->size; i++)
985 uc->value.enumerated.item[i] = cfg->val[i];
987 uc->value.integer.value[i] = cfg->val[i];
992 static int rsnd_kctrl_put(struct snd_kcontrol *kctrl,
993 struct snd_ctl_elem_value *uc)
995 struct rsnd_mod *mod = snd_kcontrol_chip(kctrl);
996 struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
999 for (i = 0; i < cfg->size; i++) {
1001 change |= (uc->value.enumerated.item[i] != cfg->val[i]);
1002 cfg->val[i] = uc->value.enumerated.item[i];
1004 change |= (uc->value.integer.value[i] != cfg->val[i]);
1005 cfg->val[i] = uc->value.integer.value[i];
1010 cfg->update(cfg->io, mod);
1015 static 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 struct rsnd_kctrl_cfg *cfg,
1020 void (*update)(struct rsnd_dai_stream *io,
1021 struct rsnd_mod *mod))
1023 struct snd_soc_card *soc_card = rtd->card;
1024 struct snd_card *card = rtd->card->snd_card;
1025 struct snd_kcontrol *kctrl;
1026 struct snd_kcontrol_new knew = {
1027 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1029 .info = rsnd_kctrl_info,
1030 .index = rtd - soc_card->rtd,
1031 .get = rsnd_kctrl_get,
1032 .put = rsnd_kctrl_put,
1033 .private_value = (unsigned long)cfg,
1037 kctrl = snd_ctl_new1(&knew, mod);
1041 ret = snd_ctl_add(card, kctrl);
1043 snd_ctl_free_one(kctrl);
1047 cfg->update = update;
1055 void _rsnd_kctrl_remove(struct rsnd_kctrl_cfg *cfg)
1057 snd_ctl_remove(cfg->card, cfg->kctrl);
1060 int rsnd_kctrl_new_m(struct rsnd_mod *mod,
1061 struct rsnd_dai_stream *io,
1062 struct snd_soc_pcm_runtime *rtd,
1063 const unsigned char *name,
1064 void (*update)(struct rsnd_dai_stream *io,
1065 struct rsnd_mod *mod),
1066 struct rsnd_kctrl_cfg_m *_cfg,
1069 _cfg->cfg.max = max;
1070 _cfg->cfg.size = RSND_DVC_CHANNELS;
1071 _cfg->cfg.val = _cfg->val;
1072 return __rsnd_kctrl_new(mod, io, rtd, name, &_cfg->cfg, update);
1075 int rsnd_kctrl_new_s(struct rsnd_mod *mod,
1076 struct rsnd_dai_stream *io,
1077 struct snd_soc_pcm_runtime *rtd,
1078 const unsigned char *name,
1079 void (*update)(struct rsnd_dai_stream *io,
1080 struct rsnd_mod *mod),
1081 struct rsnd_kctrl_cfg_s *_cfg,
1084 _cfg->cfg.max = max;
1086 _cfg->cfg.val = &_cfg->val;
1087 return __rsnd_kctrl_new(mod, io, rtd, name, &_cfg->cfg, update);
1090 int rsnd_kctrl_new_e(struct rsnd_mod *mod,
1091 struct rsnd_dai_stream *io,
1092 struct snd_soc_pcm_runtime *rtd,
1093 const unsigned char *name,
1094 struct rsnd_kctrl_cfg_s *_cfg,
1095 void (*update)(struct rsnd_dai_stream *io,
1096 struct rsnd_mod *mod),
1097 const char * const *texts,
1100 _cfg->cfg.max = max;
1102 _cfg->cfg.val = &_cfg->val;
1103 _cfg->cfg.texts = texts;
1104 return __rsnd_kctrl_new(mod, io, rtd, name, &_cfg->cfg, update);
1111 #define PREALLOC_BUFFER (32 * 1024)
1112 #define PREALLOC_BUFFER_MAX (32 * 1024)
1114 static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd)
1116 struct snd_soc_dai *dai = rtd->cpu_dai;
1117 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1120 ret = rsnd_dai_call(pcm_new, &rdai->playback, rtd);
1124 ret = rsnd_dai_call(pcm_new, &rdai->capture, rtd);
1128 return snd_pcm_lib_preallocate_pages_for_all(
1131 rtd->card->snd_card->dev,
1132 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
1135 static struct snd_soc_platform_driver rsnd_soc_platform = {
1136 .ops = &rsnd_pcm_ops,
1137 .pcm_new = rsnd_pcm_new,
1140 static const struct snd_soc_component_driver rsnd_soc_component = {
1144 static int rsnd_rdai_continuance_probe(struct rsnd_priv *priv,
1145 struct rsnd_dai_stream *io)
1149 ret = rsnd_dai_call(probe, io, priv);
1150 if (ret == -EAGAIN) {
1151 struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io);
1155 * Fallback to PIO mode
1159 * call "remove" for SSI/SRC/DVC
1160 * SSI will be switch to PIO mode if it was DMA mode
1163 * rsnd_ssi_fallback()
1165 rsnd_dai_call(remove, io, priv);
1168 * remove all mod from io
1169 * and, re connect ssi
1171 for (i = 0; i < RSND_MOD_MAX; i++)
1172 rsnd_dai_disconnect((io)->mod[i], io, i);
1173 rsnd_dai_connect(ssi_mod, io, RSND_MOD_SSI);
1178 rsnd_dai_call(fallback, io, priv);
1182 * DAI has SSI which is PIO mode only now.
1184 ret = rsnd_dai_call(probe, io, priv);
1193 static int rsnd_probe(struct platform_device *pdev)
1195 struct rcar_snd_info *info;
1196 struct rsnd_priv *priv;
1197 struct device *dev = &pdev->dev;
1198 struct rsnd_dai *rdai;
1199 const struct of_device_id *of_id = of_match_device(rsnd_of_match, dev);
1200 const struct rsnd_of_data *of_data;
1201 int (*probe_func[])(struct platform_device *pdev,
1202 const struct rsnd_of_data *of_data,
1203 struct rsnd_priv *priv) = {
1216 info = devm_kzalloc(&pdev->dev, sizeof(struct rcar_snd_info),
1220 of_data = of_id->data;
1225 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1227 dev_err(dev, "priv allocate failed\n");
1233 spin_lock_init(&priv->lock);
1238 for (i = 0; i < ARRAY_SIZE(probe_func); i++) {
1239 ret = probe_func[i](pdev, of_data, priv);
1244 for_each_rsnd_dai(rdai, priv, i) {
1245 ret = rsnd_rdai_continuance_probe(priv, &rdai->playback);
1247 goto exit_snd_probe;
1249 ret = rsnd_rdai_continuance_probe(priv, &rdai->capture);
1251 goto exit_snd_probe;
1254 dev_set_drvdata(dev, priv);
1259 ret = snd_soc_register_platform(dev, &rsnd_soc_platform);
1261 dev_err(dev, "cannot snd soc register\n");
1265 ret = snd_soc_register_component(dev, &rsnd_soc_component,
1266 priv->daidrv, rsnd_rdai_nr(priv));
1268 dev_err(dev, "cannot snd dai register\n");
1272 pm_runtime_enable(dev);
1274 dev_info(dev, "probed\n");
1278 snd_soc_unregister_platform(dev);
1280 for_each_rsnd_dai(rdai, priv, i) {
1281 rsnd_dai_call(remove, &rdai->playback, priv);
1282 rsnd_dai_call(remove, &rdai->capture, priv);
1288 static int rsnd_remove(struct platform_device *pdev)
1290 struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
1291 struct rsnd_dai *rdai;
1292 void (*remove_func[])(struct platform_device *pdev,
1293 struct rsnd_priv *priv) = {
1302 pm_runtime_disable(&pdev->dev);
1304 for_each_rsnd_dai(rdai, priv, i) {
1305 ret |= rsnd_dai_call(remove, &rdai->playback, priv);
1306 ret |= rsnd_dai_call(remove, &rdai->capture, priv);
1309 for (i = 0; i < ARRAY_SIZE(remove_func); i++)
1310 remove_func[i](pdev, priv);
1312 snd_soc_unregister_component(&pdev->dev);
1313 snd_soc_unregister_platform(&pdev->dev);
1318 static struct platform_driver rsnd_driver = {
1320 .name = "rcar_sound",
1321 .of_match_table = rsnd_of_match,
1323 .probe = rsnd_probe,
1324 .remove = rsnd_remove,
1326 module_platform_driver(rsnd_driver);
1328 MODULE_LICENSE("GPL");
1329 MODULE_DESCRIPTION("Renesas R-Car audio driver");
1330 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
1331 MODULE_ALIAS("platform:rcar-pcm-audio");