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 of_device_id rsnd_of_match[] = {
103 { .compatible = "renesas,rcar_sound-gen1", .data = (void *)RSND_GEN1 },
104 { .compatible = "renesas,rcar_sound-gen2", .data = (void *)RSND_GEN2 },
105 { .compatible = "renesas,rcar_sound-gen3", .data = (void *)RSND_GEN2 }, /* gen2 compatible */
108 MODULE_DEVICE_TABLE(of, rsnd_of_match);
114 void rsnd_mod_make_sure(struct rsnd_mod *mod, enum rsnd_mod_type type)
116 if (mod->type != type) {
117 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
118 struct device *dev = rsnd_priv_to_dev(priv);
120 dev_warn(dev, "%s[%d] is not your expected module\n",
121 rsnd_mod_name(mod), rsnd_mod_id(mod));
126 char *rsnd_mod_name(struct rsnd_mod *mod)
128 if (!mod || !mod->ops)
131 return mod->ops->name;
134 struct dma_chan *rsnd_mod_dma_req(struct rsnd_dai_stream *io,
135 struct rsnd_mod *mod)
137 if (!mod || !mod->ops || !mod->ops->dma_req)
140 return mod->ops->dma_req(io, mod);
143 u32 *rsnd_mod_get_status(struct rsnd_dai_stream *io,
144 struct rsnd_mod *mod,
145 enum rsnd_mod_type type)
150 int rsnd_mod_init(struct rsnd_priv *priv,
151 struct rsnd_mod *mod,
152 struct rsnd_mod_ops *ops,
154 u32* (*get_status)(struct rsnd_dai_stream *io,
155 struct rsnd_mod *mod,
156 enum rsnd_mod_type type),
157 enum rsnd_mod_type type,
160 int ret = clk_prepare(clk);
170 mod->get_status = get_status;
175 void rsnd_mod_quit(struct rsnd_mod *mod)
178 clk_unprepare(mod->clk);
182 void rsnd_mod_interrupt(struct rsnd_mod *mod,
183 void (*callback)(struct rsnd_mod *mod,
184 struct rsnd_dai_stream *io))
186 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
187 struct rsnd_dai_stream *io;
188 struct rsnd_dai *rdai;
191 for_each_rsnd_dai(rdai, priv, i) {
192 io = &rdai->playback;
193 if (mod == io->mod[mod->type])
197 if (mod == io->mod[mod->type])
202 int rsnd_io_is_working(struct rsnd_dai_stream *io)
204 /* see rsnd_dai_stream_init/quit() */
205 return !!io->substream;
208 void rsnd_set_slot(struct rsnd_dai *rdai,
212 rdai->slots_num = num;
215 int rsnd_get_slot(struct rsnd_dai_stream *io)
217 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
222 int rsnd_get_slot_num(struct rsnd_dai_stream *io)
224 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
226 return rdai->slots_num;
229 int rsnd_runtime_channel_original(struct rsnd_dai_stream *io)
231 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
233 return runtime->channels;
236 int rsnd_runtime_channel_after_ctu(struct rsnd_dai_stream *io)
238 int chan = rsnd_runtime_channel_original(io);
239 struct rsnd_mod *ctu_mod = rsnd_io_to_mod_ctu(io);
242 u32 converted_chan = rsnd_ctu_converted_channel(ctu_mod);
245 return converted_chan;
251 int rsnd_runtime_channel_for_ssi(struct rsnd_dai_stream *io)
253 int chan = rsnd_io_is_play(io) ?
254 rsnd_runtime_channel_after_ctu(io) :
255 rsnd_runtime_channel_original(io);
258 if (rsnd_runtime_is_ssi_multi(io))
259 chan /= rsnd_get_slot_num(io);
261 /* TDM Extend Mode needs 8ch */
268 int rsnd_runtime_is_ssi_multi(struct rsnd_dai_stream *io)
270 int slots = rsnd_get_slot_num(io);
271 int chan = rsnd_io_is_play(io) ?
272 rsnd_runtime_channel_after_ctu(io) :
273 rsnd_runtime_channel_original(io);
275 return (chan >= 6) && (slots > 1);
278 int rsnd_runtime_is_ssi_tdm(struct rsnd_dai_stream *io)
280 return rsnd_runtime_channel_for_ssi(io) >= 6;
286 u32 rsnd_get_adinr_bit(struct rsnd_mod *mod, struct rsnd_dai_stream *io)
288 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
289 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
290 struct device *dev = rsnd_priv_to_dev(priv);
292 switch (runtime->sample_bits) {
299 dev_warn(dev, "not supported sample bits\n");
307 u32 rsnd_get_dalign(struct rsnd_mod *mod, struct rsnd_dai_stream *io)
309 struct rsnd_mod *ssi = rsnd_io_to_mod_ssi(io);
310 struct rsnd_mod *target;
311 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
312 u32 val = 0x76543210;
315 if (rsnd_io_is_play(io)) {
316 struct rsnd_mod *src = rsnd_io_to_mod_src(io);
318 target = src ? src : ssi;
320 struct rsnd_mod *cmd = rsnd_io_to_mod_cmd(io);
322 target = cmd ? cmd : ssi;
325 mask <<= runtime->channels * 4;
328 switch (runtime->sample_bits) {
330 val |= 0x67452301 & ~mask;
333 val |= 0x76543210 & ~mask;
338 * exchange channeles on SRC if possible,
339 * otherwise, R/L volume settings on DVC
340 * changes inverted channels
351 #define rsnd_mod_call(idx, io, func, param...) \
353 struct rsnd_priv *priv = rsnd_mod_to_priv(mod); \
354 struct rsnd_mod *mod = (io)->mod[idx]; \
355 struct device *dev = rsnd_priv_to_dev(priv); \
356 u32 *status = mod->get_status(io, mod, idx); \
357 u32 mask = 0xF << __rsnd_mod_shift_##func; \
358 u8 val = (*status >> __rsnd_mod_shift_##func) & 0xF; \
359 u8 add = ((val + __rsnd_mod_add_##func) & 0xF); \
361 int call = (val == __rsnd_mod_call_##func) && (mod)->ops->func; \
365 *status = (*status & ~mask) + \
366 (add << __rsnd_mod_shift_##func); \
367 dev_dbg(dev, "%s[%d]\t0x%08x %s\n", \
368 rsnd_mod_name(mod), rsnd_mod_id(mod), \
369 *status, call ? #func : ""); \
371 ret = (mod)->ops->func(mod, io, param); \
373 dev_dbg(dev, "%s[%d] : rsnd_mod_call error %d\n", \
374 rsnd_mod_name(mod), rsnd_mod_id(mod), ret); \
378 static enum rsnd_mod_type rsnd_mod_sequence[][RSND_MOD_MAX] = {
412 #define rsnd_dai_call(fn, io, param...) \
414 struct rsnd_mod *mod; \
415 int type, is_play = rsnd_io_is_play(io); \
417 for (i = 0; i < RSND_MOD_MAX; i++) { \
418 type = rsnd_mod_sequence[is_play][i]; \
419 mod = (io)->mod[type]; \
422 ret |= rsnd_mod_call(type, io, fn, param); \
427 int rsnd_dai_connect(struct rsnd_mod *mod,
428 struct rsnd_dai_stream *io,
429 enum rsnd_mod_type type)
431 struct rsnd_priv *priv;
437 if (io->mod[type] == mod)
443 priv = rsnd_mod_to_priv(mod);
444 dev = rsnd_priv_to_dev(priv);
448 dev_dbg(dev, "%s[%d] is connected to io (%s)\n",
449 rsnd_mod_name(mod), rsnd_mod_id(mod),
450 rsnd_io_is_play(io) ? "Playback" : "Capture");
455 static void rsnd_dai_disconnect(struct rsnd_mod *mod,
456 struct rsnd_dai_stream *io,
457 enum rsnd_mod_type type)
459 io->mod[type] = NULL;
462 struct rsnd_dai *rsnd_rdai_get(struct rsnd_priv *priv, int id)
464 if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
467 return priv->rdai + id;
470 #define rsnd_dai_to_priv(dai) snd_soc_dai_get_drvdata(dai)
471 static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai)
473 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
475 return rsnd_rdai_get(priv, dai->id);
479 * rsnd_soc_dai functions
481 int rsnd_dai_pointer_offset(struct rsnd_dai_stream *io, int additional)
483 struct snd_pcm_substream *substream = io->substream;
484 struct snd_pcm_runtime *runtime = substream->runtime;
485 int pos = io->byte_pos + additional;
487 pos %= (runtime->periods * io->byte_per_period);
492 bool rsnd_dai_pointer_update(struct rsnd_dai_stream *io, int byte)
494 io->byte_pos += byte;
496 if (io->byte_pos >= io->next_period_byte) {
497 struct snd_pcm_substream *substream = io->substream;
498 struct snd_pcm_runtime *runtime = substream->runtime;
501 io->next_period_byte += io->byte_per_period;
503 if (io->period_pos >= runtime->periods) {
506 io->next_period_byte = io->byte_per_period;
515 void rsnd_dai_period_elapsed(struct rsnd_dai_stream *io)
517 struct snd_pcm_substream *substream = io->substream;
520 * this function should be called...
522 * - if rsnd_dai_pointer_update() returns true
523 * - without spin lock
526 snd_pcm_period_elapsed(substream);
529 static void rsnd_dai_stream_init(struct rsnd_dai_stream *io,
530 struct snd_pcm_substream *substream)
532 struct snd_pcm_runtime *runtime = substream->runtime;
534 io->substream = substream;
537 io->byte_per_period = runtime->period_size *
539 samples_to_bytes(runtime, 1);
540 io->next_period_byte = io->byte_per_period;
543 static void rsnd_dai_stream_quit(struct rsnd_dai_stream *io)
545 io->substream = NULL;
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 = rsnd_dai_to_priv(dai);
570 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
571 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
575 spin_lock_irqsave(&priv->lock, flags);
578 case SNDRV_PCM_TRIGGER_START:
579 case SNDRV_PCM_TRIGGER_RESUME:
580 rsnd_dai_stream_init(io, substream);
582 ret = rsnd_dai_call(init, io, priv);
584 goto dai_trigger_end;
586 ret = rsnd_dai_call(start, io, priv);
588 goto dai_trigger_end;
590 ret = rsnd_dai_call(irq, io, priv, 1);
592 goto dai_trigger_end;
595 case SNDRV_PCM_TRIGGER_STOP:
596 case SNDRV_PCM_TRIGGER_SUSPEND:
597 ret = rsnd_dai_call(irq, io, priv, 0);
599 ret |= rsnd_dai_call(stop, io, priv);
601 ret |= rsnd_dai_call(quit, io, priv);
603 rsnd_dai_stream_quit(io);
610 spin_unlock_irqrestore(&priv->lock, flags);
615 static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
617 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
619 /* set master/slave audio interface */
620 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
621 case SND_SOC_DAIFMT_CBM_CFM:
622 rdai->clk_master = 0;
624 case SND_SOC_DAIFMT_CBS_CFS:
625 rdai->clk_master = 1; /* codec is slave, cpu is master */
632 rdai->bit_clk_inv = 0;
633 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
634 case SND_SOC_DAIFMT_I2S:
636 rdai->data_alignment = 0;
637 rdai->frm_clk_inv = 0;
639 case SND_SOC_DAIFMT_LEFT_J:
641 rdai->data_alignment = 0;
642 rdai->frm_clk_inv = 1;
644 case SND_SOC_DAIFMT_RIGHT_J:
646 rdai->data_alignment = 1;
647 rdai->frm_clk_inv = 1;
651 /* set clock inversion */
652 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
653 case SND_SOC_DAIFMT_NB_IF:
654 rdai->bit_clk_inv = rdai->bit_clk_inv;
655 rdai->frm_clk_inv = !rdai->frm_clk_inv;
657 case SND_SOC_DAIFMT_IB_NF:
658 rdai->bit_clk_inv = !rdai->bit_clk_inv;
659 rdai->frm_clk_inv = rdai->frm_clk_inv;
661 case SND_SOC_DAIFMT_IB_IF:
662 rdai->bit_clk_inv = !rdai->bit_clk_inv;
663 rdai->frm_clk_inv = !rdai->frm_clk_inv;
665 case SND_SOC_DAIFMT_NB_NF:
673 static int rsnd_soc_set_dai_tdm_slot(struct snd_soc_dai *dai,
674 u32 tx_mask, u32 rx_mask,
675 int slots, int slot_width)
677 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
678 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
679 struct device *dev = rsnd_priv_to_dev(priv);
683 /* TDM Extend Mode */
684 rsnd_set_slot(rdai, slots, 1);
687 dev_err(dev, "unsupported TDM slots (%d)\n", slots);
694 static const struct snd_soc_dai_ops rsnd_soc_dai_ops = {
695 .trigger = rsnd_soc_dai_trigger,
696 .set_fmt = rsnd_soc_dai_set_fmt,
697 .set_tdm_slot = rsnd_soc_set_dai_tdm_slot,
700 void rsnd_parse_connect_common(struct rsnd_dai *rdai,
701 struct rsnd_mod* (*mod_get)(struct rsnd_priv *priv, int id),
702 struct device_node *node,
703 struct device_node *playback,
704 struct device_node *capture)
706 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
707 struct device_node *np;
708 struct rsnd_mod *mod;
715 for_each_child_of_node(node, np) {
716 mod = mod_get(priv, i);
718 rsnd_dai_connect(mod, &rdai->playback, mod->type);
720 rsnd_dai_connect(mod, &rdai->capture, mod->type);
727 static int rsnd_dai_probe(struct rsnd_priv *priv)
729 struct device_node *dai_node;
730 struct device_node *dai_np;
731 struct device_node *playback, *capture;
732 struct rsnd_dai_stream *io_playback;
733 struct rsnd_dai_stream *io_capture;
734 struct snd_soc_dai_driver *rdrv, *drv;
735 struct rsnd_dai *rdai;
736 struct device *dev = rsnd_priv_to_dev(priv);
740 dai_node = rsnd_dai_of_node(priv);
741 nr = of_get_child_count(dai_node);
744 goto rsnd_dai_probe_done;
747 rdrv = devm_kzalloc(dev, sizeof(*rdrv) * nr, GFP_KERNEL);
748 rdai = devm_kzalloc(dev, sizeof(*rdai) * nr, GFP_KERNEL);
749 if (!rdrv || !rdai) {
751 goto rsnd_dai_probe_done;
762 for_each_child_of_node(dai_node, dai_np) {
763 rdai = rsnd_rdai_get(priv, dai_i);
765 io_playback = &rdai->playback;
766 io_capture = &rdai->capture;
768 snprintf(rdai->name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", dai_i);
771 drv->name = rdai->name;
772 drv->ops = &rsnd_soc_dai_ops;
774 snprintf(rdai->playback.name, RSND_DAI_NAME_SIZE,
775 "DAI%d Playback", dai_i);
776 drv->playback.rates = RSND_RATES;
777 drv->playback.formats = RSND_FMTS;
778 drv->playback.channels_min = 2;
779 drv->playback.channels_max = 6;
780 drv->playback.stream_name = rdai->playback.name;
782 snprintf(rdai->capture.name, RSND_DAI_NAME_SIZE,
783 "DAI%d Capture", dai_i);
784 drv->capture.rates = RSND_RATES;
785 drv->capture.formats = RSND_FMTS;
786 drv->capture.channels_min = 2;
787 drv->capture.channels_max = 6;
788 drv->capture.stream_name = rdai->capture.name;
790 rdai->playback.rdai = rdai;
791 rdai->capture.rdai = rdai;
792 rsnd_set_slot(rdai, 2, 1); /* default */
794 for (io_i = 0;; io_i++) {
795 playback = of_parse_phandle(dai_np, "playback", io_i);
796 capture = of_parse_phandle(dai_np, "capture", io_i);
798 if (!playback && !capture)
801 rsnd_parse_connect_ssi(rdai, playback, capture);
802 rsnd_parse_connect_src(rdai, playback, capture);
803 rsnd_parse_connect_ctu(rdai, playback, capture);
804 rsnd_parse_connect_mix(rdai, playback, capture);
805 rsnd_parse_connect_dvc(rdai, playback, capture);
807 of_node_put(playback);
808 of_node_put(capture);
813 dev_dbg(dev, "%s (%s/%s)\n", rdai->name,
814 rsnd_io_to_mod_ssi(io_playback) ? "play" : " -- ",
815 rsnd_io_to_mod_ssi(io_capture) ? "capture" : " -- ");
821 of_node_put(dai_node);
829 static struct snd_pcm_hardware rsnd_pcm_hardware = {
830 .info = SNDRV_PCM_INFO_INTERLEAVED |
831 SNDRV_PCM_INFO_MMAP |
832 SNDRV_PCM_INFO_MMAP_VALID,
833 .buffer_bytes_max = 64 * 1024,
834 .period_bytes_min = 32,
835 .period_bytes_max = 8192,
841 static int rsnd_pcm_open(struct snd_pcm_substream *substream)
843 struct snd_pcm_runtime *runtime = substream->runtime;
846 snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware);
848 ret = snd_pcm_hw_constraint_integer(runtime,
849 SNDRV_PCM_HW_PARAM_PERIODS);
854 static int rsnd_hw_params(struct snd_pcm_substream *substream,
855 struct snd_pcm_hw_params *hw_params)
857 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
858 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
859 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
862 ret = rsnd_dai_call(hw_params, io, substream, hw_params);
866 return snd_pcm_lib_malloc_pages(substream,
867 params_buffer_bytes(hw_params));
870 static snd_pcm_uframes_t rsnd_pointer(struct snd_pcm_substream *substream)
872 struct snd_pcm_runtime *runtime = substream->runtime;
873 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
874 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
875 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
877 return bytes_to_frames(runtime, io->byte_pos);
880 static struct snd_pcm_ops rsnd_pcm_ops = {
881 .open = rsnd_pcm_open,
882 .ioctl = snd_pcm_lib_ioctl,
883 .hw_params = rsnd_hw_params,
884 .hw_free = snd_pcm_lib_free_pages,
885 .pointer = rsnd_pointer,
891 #define kcontrol_to_cfg(kctrl) ((struct rsnd_kctrl_cfg *)kctrl->private_value)
892 static int rsnd_kctrl_info(struct snd_kcontrol *kctrl,
893 struct snd_ctl_elem_info *uinfo)
895 struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
898 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
899 uinfo->count = cfg->size;
900 uinfo->value.enumerated.items = cfg->max;
901 if (uinfo->value.enumerated.item >= cfg->max)
902 uinfo->value.enumerated.item = cfg->max - 1;
903 strlcpy(uinfo->value.enumerated.name,
904 cfg->texts[uinfo->value.enumerated.item],
905 sizeof(uinfo->value.enumerated.name));
907 uinfo->count = cfg->size;
908 uinfo->value.integer.min = 0;
909 uinfo->value.integer.max = cfg->max;
910 uinfo->type = (cfg->max == 1) ?
911 SNDRV_CTL_ELEM_TYPE_BOOLEAN :
912 SNDRV_CTL_ELEM_TYPE_INTEGER;
918 static int rsnd_kctrl_get(struct snd_kcontrol *kctrl,
919 struct snd_ctl_elem_value *uc)
921 struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
924 for (i = 0; i < cfg->size; i++)
926 uc->value.enumerated.item[i] = cfg->val[i];
928 uc->value.integer.value[i] = cfg->val[i];
933 static int rsnd_kctrl_put(struct snd_kcontrol *kctrl,
934 struct snd_ctl_elem_value *uc)
936 struct rsnd_mod *mod = snd_kcontrol_chip(kctrl);
937 struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
940 for (i = 0; i < cfg->size; i++) {
942 change |= (uc->value.enumerated.item[i] != cfg->val[i]);
943 cfg->val[i] = uc->value.enumerated.item[i];
945 change |= (uc->value.integer.value[i] != cfg->val[i]);
946 cfg->val[i] = uc->value.integer.value[i];
950 if (change && cfg->update)
951 cfg->update(cfg->io, mod);
956 static int __rsnd_kctrl_new(struct rsnd_mod *mod,
957 struct rsnd_dai_stream *io,
958 struct snd_soc_pcm_runtime *rtd,
959 const unsigned char *name,
960 struct rsnd_kctrl_cfg *cfg,
961 void (*update)(struct rsnd_dai_stream *io,
962 struct rsnd_mod *mod))
964 struct snd_card *card = rtd->card->snd_card;
965 struct snd_kcontrol *kctrl;
966 struct snd_kcontrol_new knew = {
967 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
969 .info = rsnd_kctrl_info,
971 .get = rsnd_kctrl_get,
972 .put = rsnd_kctrl_put,
973 .private_value = (unsigned long)cfg,
977 kctrl = snd_ctl_new1(&knew, mod);
981 ret = snd_ctl_add(card, kctrl);
985 cfg->update = update;
993 void _rsnd_kctrl_remove(struct rsnd_kctrl_cfg *cfg)
995 snd_ctl_remove(cfg->card, cfg->kctrl);
998 int rsnd_kctrl_new_m(struct rsnd_mod *mod,
999 struct rsnd_dai_stream *io,
1000 struct snd_soc_pcm_runtime *rtd,
1001 const unsigned char *name,
1002 void (*update)(struct rsnd_dai_stream *io,
1003 struct rsnd_mod *mod),
1004 struct rsnd_kctrl_cfg_m *_cfg,
1008 if (ch_size > RSND_MAX_CHANNELS)
1011 _cfg->cfg.max = max;
1012 _cfg->cfg.size = ch_size;
1013 _cfg->cfg.val = _cfg->val;
1014 return __rsnd_kctrl_new(mod, io, rtd, name, &_cfg->cfg, update);
1017 int rsnd_kctrl_new_s(struct rsnd_mod *mod,
1018 struct rsnd_dai_stream *io,
1019 struct snd_soc_pcm_runtime *rtd,
1020 const unsigned char *name,
1021 void (*update)(struct rsnd_dai_stream *io,
1022 struct rsnd_mod *mod),
1023 struct rsnd_kctrl_cfg_s *_cfg,
1026 _cfg->cfg.max = max;
1028 _cfg->cfg.val = &_cfg->val;
1029 return __rsnd_kctrl_new(mod, io, rtd, name, &_cfg->cfg, update);
1032 int rsnd_kctrl_new_e(struct rsnd_mod *mod,
1033 struct rsnd_dai_stream *io,
1034 struct snd_soc_pcm_runtime *rtd,
1035 const unsigned char *name,
1036 struct rsnd_kctrl_cfg_s *_cfg,
1037 void (*update)(struct rsnd_dai_stream *io,
1038 struct rsnd_mod *mod),
1039 const char * const *texts,
1042 _cfg->cfg.max = max;
1044 _cfg->cfg.val = &_cfg->val;
1045 _cfg->cfg.texts = texts;
1046 return __rsnd_kctrl_new(mod, io, rtd, name, &_cfg->cfg, update);
1053 #define PREALLOC_BUFFER (32 * 1024)
1054 #define PREALLOC_BUFFER_MAX (32 * 1024)
1056 static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd)
1058 struct snd_soc_dai *dai = rtd->cpu_dai;
1059 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1062 ret = rsnd_dai_call(pcm_new, &rdai->playback, rtd);
1066 ret = rsnd_dai_call(pcm_new, &rdai->capture, rtd);
1070 return snd_pcm_lib_preallocate_pages_for_all(
1073 rtd->card->snd_card->dev,
1074 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
1077 static struct snd_soc_platform_driver rsnd_soc_platform = {
1078 .ops = &rsnd_pcm_ops,
1079 .pcm_new = rsnd_pcm_new,
1082 static const struct snd_soc_component_driver rsnd_soc_component = {
1086 static int rsnd_rdai_continuance_probe(struct rsnd_priv *priv,
1087 struct rsnd_dai_stream *io)
1091 ret = rsnd_dai_call(probe, io, priv);
1092 if (ret == -EAGAIN) {
1093 struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io);
1097 * Fallback to PIO mode
1101 * call "remove" for SSI/SRC/DVC
1102 * SSI will be switch to PIO mode if it was DMA mode
1105 * rsnd_ssi_fallback()
1107 rsnd_dai_call(remove, io, priv);
1110 * remove all mod from io
1111 * and, re connect ssi
1113 for (i = 0; i < RSND_MOD_MAX; i++)
1114 rsnd_dai_disconnect((io)->mod[i], io, i);
1115 rsnd_dai_connect(ssi_mod, io, RSND_MOD_SSI);
1120 rsnd_dai_call(fallback, io, priv);
1124 * DAI has SSI which is PIO mode only now.
1126 ret = rsnd_dai_call(probe, io, priv);
1135 static int rsnd_probe(struct platform_device *pdev)
1137 struct rsnd_priv *priv;
1138 struct device *dev = &pdev->dev;
1139 struct rsnd_dai *rdai;
1140 int (*probe_func[])(struct rsnd_priv *priv) = {
1158 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1160 dev_err(dev, "priv allocate failed\n");
1165 priv->flags = (unsigned long)of_device_get_match_data(dev);
1166 spin_lock_init(&priv->lock);
1171 for (i = 0; i < ARRAY_SIZE(probe_func); i++) {
1172 ret = probe_func[i](priv);
1177 for_each_rsnd_dai(rdai, priv, i) {
1178 ret = rsnd_rdai_continuance_probe(priv, &rdai->playback);
1180 goto exit_snd_probe;
1182 ret = rsnd_rdai_continuance_probe(priv, &rdai->capture);
1184 goto exit_snd_probe;
1187 dev_set_drvdata(dev, priv);
1192 ret = snd_soc_register_platform(dev, &rsnd_soc_platform);
1194 dev_err(dev, "cannot snd soc register\n");
1198 ret = snd_soc_register_component(dev, &rsnd_soc_component,
1199 priv->daidrv, rsnd_rdai_nr(priv));
1201 dev_err(dev, "cannot snd dai register\n");
1205 pm_runtime_enable(dev);
1207 dev_info(dev, "probed\n");
1211 snd_soc_unregister_platform(dev);
1213 for_each_rsnd_dai(rdai, priv, i) {
1214 rsnd_dai_call(remove, &rdai->playback, priv);
1215 rsnd_dai_call(remove, &rdai->capture, priv);
1221 static int rsnd_remove(struct platform_device *pdev)
1223 struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
1224 struct rsnd_dai *rdai;
1225 void (*remove_func[])(struct rsnd_priv *priv) = {
1237 pm_runtime_disable(&pdev->dev);
1239 for_each_rsnd_dai(rdai, priv, i) {
1240 ret |= rsnd_dai_call(remove, &rdai->playback, priv);
1241 ret |= rsnd_dai_call(remove, &rdai->capture, priv);
1244 for (i = 0; i < ARRAY_SIZE(remove_func); i++)
1245 remove_func[i](priv);
1247 snd_soc_unregister_component(&pdev->dev);
1248 snd_soc_unregister_platform(&pdev->dev);
1253 static struct platform_driver rsnd_driver = {
1255 .name = "rcar_sound",
1256 .of_match_table = rsnd_of_match,
1258 .probe = rsnd_probe,
1259 .remove = rsnd_remove,
1261 module_platform_driver(rsnd_driver);
1263 MODULE_LICENSE("GPL");
1264 MODULE_DESCRIPTION("Renesas R-Car audio driver");
1265 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
1266 MODULE_ALIAS("platform:rcar-pcm-audio");