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 *ssiu = rsnd_io_to_mod_ssiu(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 : ssiu;
320 struct rsnd_mod *cmd = rsnd_io_to_mod_cmd(io);
322 target = cmd ? cmd : ssiu;
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 struct rsnd_mod *rsnd_mod_next(int *iterator,
352 struct rsnd_dai_stream *io,
353 enum rsnd_mod_type *array,
356 struct rsnd_mod *mod;
357 enum rsnd_mod_type type;
358 int max = array ? array_size : RSND_MOD_MAX;
360 for (; *iterator < max; (*iterator)++) {
361 type = (array) ? array[*iterator] : *iterator;
372 static enum rsnd_mod_type rsnd_mod_sequence[][RSND_MOD_MAX] = {
406 static int rsnd_status_update(u32 *status,
407 int shift, int add, int timing)
409 u32 mask = 0xF << shift;
410 u8 val = (*status >> shift) & 0xF;
411 u8 next_val = (val + add) & 0xF;
412 int func_call = (val == timing);
414 if (next_val == 0xF) /* underflow case */
417 *status = (*status & ~mask) + (next_val << shift);
422 #define rsnd_dai_call(fn, io, param...) \
424 struct rsnd_priv *priv = rsnd_io_to_priv(io); \
425 struct device *dev = rsnd_priv_to_dev(priv); \
426 struct rsnd_mod *mod; \
427 int is_play = rsnd_io_is_play(io); \
429 enum rsnd_mod_type *types = rsnd_mod_sequence[is_play]; \
430 for_each_rsnd_mod_arrays(i, mod, io, types, RSND_MOD_MAX) { \
432 u32 *status = mod->get_status(io, mod, types[i]); \
433 int func_call = rsnd_status_update(status, \
434 __rsnd_mod_shift_##fn, \
435 __rsnd_mod_add_##fn, \
436 __rsnd_mod_call_##fn); \
437 dev_dbg(dev, "%s[%d]\t0x%08x %s\n", \
438 rsnd_mod_name(mod), rsnd_mod_id(mod), *status, \
439 (func_call && (mod)->ops->fn) ? #fn : ""); \
440 if (func_call && (mod)->ops->fn) \
441 tmp = (mod)->ops->fn(mod, io, param); \
443 dev_err(dev, "%s[%d] : %s error %d\n", \
444 rsnd_mod_name(mod), rsnd_mod_id(mod), \
451 int rsnd_dai_connect(struct rsnd_mod *mod,
452 struct rsnd_dai_stream *io,
453 enum rsnd_mod_type type)
455 struct rsnd_priv *priv;
461 if (io->mod[type] == mod)
467 priv = rsnd_mod_to_priv(mod);
468 dev = rsnd_priv_to_dev(priv);
472 dev_dbg(dev, "%s[%d] is connected to io (%s)\n",
473 rsnd_mod_name(mod), rsnd_mod_id(mod),
474 rsnd_io_is_play(io) ? "Playback" : "Capture");
479 static void rsnd_dai_disconnect(struct rsnd_mod *mod,
480 struct rsnd_dai_stream *io,
481 enum rsnd_mod_type type)
483 io->mod[type] = NULL;
486 struct rsnd_dai *rsnd_rdai_get(struct rsnd_priv *priv, int id)
488 if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
491 return priv->rdai + id;
494 #define rsnd_dai_to_priv(dai) snd_soc_dai_get_drvdata(dai)
495 static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai)
497 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
499 return rsnd_rdai_get(priv, dai->id);
503 * rsnd_soc_dai functions
505 int rsnd_dai_pointer_offset(struct rsnd_dai_stream *io, int additional)
507 struct snd_pcm_substream *substream = io->substream;
508 struct snd_pcm_runtime *runtime = substream->runtime;
509 int pos = io->byte_pos + additional;
511 pos %= (runtime->periods * io->byte_per_period);
516 bool rsnd_dai_pointer_update(struct rsnd_dai_stream *io, int byte)
518 io->byte_pos += byte;
520 if (io->byte_pos >= io->next_period_byte) {
521 struct snd_pcm_substream *substream = io->substream;
522 struct snd_pcm_runtime *runtime = substream->runtime;
525 io->next_period_byte += io->byte_per_period;
527 if (io->period_pos >= runtime->periods) {
530 io->next_period_byte = io->byte_per_period;
539 void rsnd_dai_period_elapsed(struct rsnd_dai_stream *io)
541 struct snd_pcm_substream *substream = io->substream;
544 * this function should be called...
546 * - if rsnd_dai_pointer_update() returns true
547 * - without spin lock
550 snd_pcm_period_elapsed(substream);
553 static void rsnd_dai_stream_init(struct rsnd_dai_stream *io,
554 struct snd_pcm_substream *substream)
556 struct snd_pcm_runtime *runtime = substream->runtime;
558 io->substream = substream;
561 io->byte_per_period = runtime->period_size *
563 samples_to_bytes(runtime, 1);
564 io->next_period_byte = io->byte_per_period;
567 static void rsnd_dai_stream_quit(struct rsnd_dai_stream *io)
569 io->substream = NULL;
573 struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream)
575 struct snd_soc_pcm_runtime *rtd = substream->private_data;
581 struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai,
582 struct snd_pcm_substream *substream)
584 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
585 return &rdai->playback;
587 return &rdai->capture;
590 static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd,
591 struct snd_soc_dai *dai)
593 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
594 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
595 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
599 spin_lock_irqsave(&priv->lock, flags);
602 case SNDRV_PCM_TRIGGER_START:
603 case SNDRV_PCM_TRIGGER_RESUME:
604 rsnd_dai_stream_init(io, substream);
606 ret = rsnd_dai_call(init, io, priv);
608 goto dai_trigger_end;
610 ret = rsnd_dai_call(start, io, priv);
612 goto dai_trigger_end;
614 ret = rsnd_dai_call(irq, io, priv, 1);
616 goto dai_trigger_end;
619 case SNDRV_PCM_TRIGGER_STOP:
620 case SNDRV_PCM_TRIGGER_SUSPEND:
621 ret = rsnd_dai_call(irq, io, priv, 0);
623 ret |= rsnd_dai_call(stop, io, priv);
625 ret |= rsnd_dai_call(quit, io, priv);
627 rsnd_dai_stream_quit(io);
634 spin_unlock_irqrestore(&priv->lock, flags);
639 static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
641 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
643 /* set master/slave audio interface */
644 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
645 case SND_SOC_DAIFMT_CBM_CFM:
646 rdai->clk_master = 0;
648 case SND_SOC_DAIFMT_CBS_CFS:
649 rdai->clk_master = 1; /* codec is slave, cpu is master */
656 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
657 case SND_SOC_DAIFMT_I2S:
659 rdai->data_alignment = 0;
660 rdai->frm_clk_inv = 0;
662 case SND_SOC_DAIFMT_LEFT_J:
664 rdai->data_alignment = 0;
665 rdai->frm_clk_inv = 1;
667 case SND_SOC_DAIFMT_RIGHT_J:
669 rdai->data_alignment = 1;
670 rdai->frm_clk_inv = 1;
674 /* set clock inversion */
675 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
676 case SND_SOC_DAIFMT_NB_IF:
677 rdai->bit_clk_inv = rdai->bit_clk_inv;
678 rdai->frm_clk_inv = !rdai->frm_clk_inv;
680 case SND_SOC_DAIFMT_IB_NF:
681 rdai->bit_clk_inv = !rdai->bit_clk_inv;
682 rdai->frm_clk_inv = rdai->frm_clk_inv;
684 case SND_SOC_DAIFMT_IB_IF:
685 rdai->bit_clk_inv = !rdai->bit_clk_inv;
686 rdai->frm_clk_inv = !rdai->frm_clk_inv;
688 case SND_SOC_DAIFMT_NB_NF:
696 static int rsnd_soc_set_dai_tdm_slot(struct snd_soc_dai *dai,
697 u32 tx_mask, u32 rx_mask,
698 int slots, int slot_width)
700 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
701 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
702 struct device *dev = rsnd_priv_to_dev(priv);
706 /* TDM Extend Mode */
707 rsnd_set_slot(rdai, slots, 1);
710 dev_err(dev, "unsupported TDM slots (%d)\n", slots);
717 static int rsnd_soc_dai_startup(struct snd_pcm_substream *substream,
718 struct snd_soc_dai *dai)
720 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
721 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
724 * call rsnd_dai_call without spinlock
726 return rsnd_dai_call(nolock_start, io, priv);
729 static void rsnd_soc_dai_shutdown(struct snd_pcm_substream *substream,
730 struct snd_soc_dai *dai)
732 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
733 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
736 * call rsnd_dai_call without spinlock
738 rsnd_dai_call(nolock_stop, io, priv);
741 static const struct snd_soc_dai_ops rsnd_soc_dai_ops = {
742 .startup = rsnd_soc_dai_startup,
743 .shutdown = rsnd_soc_dai_shutdown,
744 .trigger = rsnd_soc_dai_trigger,
745 .set_fmt = rsnd_soc_dai_set_fmt,
746 .set_tdm_slot = rsnd_soc_set_dai_tdm_slot,
749 void rsnd_parse_connect_common(struct rsnd_dai *rdai,
750 struct rsnd_mod* (*mod_get)(struct rsnd_priv *priv, int id),
751 struct device_node *node,
752 struct device_node *playback,
753 struct device_node *capture)
755 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
756 struct device_node *np;
757 struct rsnd_mod *mod;
764 for_each_child_of_node(node, np) {
765 mod = mod_get(priv, i);
767 rsnd_dai_connect(mod, &rdai->playback, mod->type);
769 rsnd_dai_connect(mod, &rdai->capture, mod->type);
776 static int rsnd_dai_probe(struct rsnd_priv *priv)
778 struct device_node *dai_node;
779 struct device_node *dai_np;
780 struct device_node *playback, *capture;
781 struct rsnd_dai_stream *io_playback;
782 struct rsnd_dai_stream *io_capture;
783 struct snd_soc_dai_driver *rdrv, *drv;
784 struct rsnd_dai *rdai;
785 struct device *dev = rsnd_priv_to_dev(priv);
789 dai_node = rsnd_dai_of_node(priv);
790 nr = of_get_child_count(dai_node);
793 goto rsnd_dai_probe_done;
796 rdrv = devm_kzalloc(dev, sizeof(*rdrv) * nr, GFP_KERNEL);
797 rdai = devm_kzalloc(dev, sizeof(*rdai) * nr, GFP_KERNEL);
798 if (!rdrv || !rdai) {
800 goto rsnd_dai_probe_done;
811 for_each_child_of_node(dai_node, dai_np) {
812 rdai = rsnd_rdai_get(priv, dai_i);
814 io_playback = &rdai->playback;
815 io_capture = &rdai->capture;
817 snprintf(rdai->name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", dai_i);
820 drv->name = rdai->name;
821 drv->ops = &rsnd_soc_dai_ops;
823 snprintf(rdai->playback.name, RSND_DAI_NAME_SIZE,
824 "DAI%d Playback", dai_i);
825 drv->playback.rates = RSND_RATES;
826 drv->playback.formats = RSND_FMTS;
827 drv->playback.channels_min = 2;
828 drv->playback.channels_max = 6;
829 drv->playback.stream_name = rdai->playback.name;
831 snprintf(rdai->capture.name, RSND_DAI_NAME_SIZE,
832 "DAI%d Capture", dai_i);
833 drv->capture.rates = RSND_RATES;
834 drv->capture.formats = RSND_FMTS;
835 drv->capture.channels_min = 2;
836 drv->capture.channels_max = 6;
837 drv->capture.stream_name = rdai->capture.name;
839 rdai->playback.rdai = rdai;
840 rdai->capture.rdai = rdai;
841 rsnd_set_slot(rdai, 2, 1); /* default */
843 for (io_i = 0;; io_i++) {
844 playback = of_parse_phandle(dai_np, "playback", io_i);
845 capture = of_parse_phandle(dai_np, "capture", io_i);
847 if (!playback && !capture)
850 rsnd_parse_connect_ssi(rdai, playback, capture);
851 rsnd_parse_connect_src(rdai, playback, capture);
852 rsnd_parse_connect_ctu(rdai, playback, capture);
853 rsnd_parse_connect_mix(rdai, playback, capture);
854 rsnd_parse_connect_dvc(rdai, playback, capture);
856 of_node_put(playback);
857 of_node_put(capture);
862 dev_dbg(dev, "%s (%s/%s)\n", rdai->name,
863 rsnd_io_to_mod_ssi(io_playback) ? "play" : " -- ",
864 rsnd_io_to_mod_ssi(io_capture) ? "capture" : " -- ");
870 of_node_put(dai_node);
878 static struct snd_pcm_hardware rsnd_pcm_hardware = {
879 .info = SNDRV_PCM_INFO_INTERLEAVED |
880 SNDRV_PCM_INFO_MMAP |
881 SNDRV_PCM_INFO_MMAP_VALID,
882 .buffer_bytes_max = 64 * 1024,
883 .period_bytes_min = 32,
884 .period_bytes_max = 8192,
890 static int rsnd_pcm_open(struct snd_pcm_substream *substream)
892 struct snd_pcm_runtime *runtime = substream->runtime;
895 snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware);
897 ret = snd_pcm_hw_constraint_integer(runtime,
898 SNDRV_PCM_HW_PARAM_PERIODS);
903 static int rsnd_hw_params(struct snd_pcm_substream *substream,
904 struct snd_pcm_hw_params *hw_params)
906 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
907 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
908 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
911 ret = rsnd_dai_call(hw_params, io, substream, hw_params);
915 return snd_pcm_lib_malloc_pages(substream,
916 params_buffer_bytes(hw_params));
919 static snd_pcm_uframes_t rsnd_pointer(struct snd_pcm_substream *substream)
921 struct snd_pcm_runtime *runtime = substream->runtime;
922 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
923 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
924 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
926 return bytes_to_frames(runtime, io->byte_pos);
929 static struct snd_pcm_ops rsnd_pcm_ops = {
930 .open = rsnd_pcm_open,
931 .ioctl = snd_pcm_lib_ioctl,
932 .hw_params = rsnd_hw_params,
933 .hw_free = snd_pcm_lib_free_pages,
934 .pointer = rsnd_pointer,
940 #define kcontrol_to_cfg(kctrl) ((struct rsnd_kctrl_cfg *)kctrl->private_value)
941 static int rsnd_kctrl_info(struct snd_kcontrol *kctrl,
942 struct snd_ctl_elem_info *uinfo)
944 struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
947 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
948 uinfo->count = cfg->size;
949 uinfo->value.enumerated.items = cfg->max;
950 if (uinfo->value.enumerated.item >= cfg->max)
951 uinfo->value.enumerated.item = cfg->max - 1;
952 strlcpy(uinfo->value.enumerated.name,
953 cfg->texts[uinfo->value.enumerated.item],
954 sizeof(uinfo->value.enumerated.name));
956 uinfo->count = cfg->size;
957 uinfo->value.integer.min = 0;
958 uinfo->value.integer.max = cfg->max;
959 uinfo->type = (cfg->max == 1) ?
960 SNDRV_CTL_ELEM_TYPE_BOOLEAN :
961 SNDRV_CTL_ELEM_TYPE_INTEGER;
967 static int rsnd_kctrl_get(struct snd_kcontrol *kctrl,
968 struct snd_ctl_elem_value *uc)
970 struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
973 for (i = 0; i < cfg->size; i++)
975 uc->value.enumerated.item[i] = cfg->val[i];
977 uc->value.integer.value[i] = cfg->val[i];
982 static int rsnd_kctrl_put(struct snd_kcontrol *kctrl,
983 struct snd_ctl_elem_value *uc)
985 struct rsnd_mod *mod = snd_kcontrol_chip(kctrl);
986 struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
989 for (i = 0; i < cfg->size; i++) {
991 change |= (uc->value.enumerated.item[i] != cfg->val[i]);
992 cfg->val[i] = uc->value.enumerated.item[i];
994 change |= (uc->value.integer.value[i] != cfg->val[i]);
995 cfg->val[i] = uc->value.integer.value[i];
999 if (change && cfg->update)
1000 cfg->update(cfg->io, mod);
1005 static int __rsnd_kctrl_new(struct rsnd_mod *mod,
1006 struct rsnd_dai_stream *io,
1007 struct snd_soc_pcm_runtime *rtd,
1008 const unsigned char *name,
1009 struct rsnd_kctrl_cfg *cfg,
1010 void (*update)(struct rsnd_dai_stream *io,
1011 struct rsnd_mod *mod))
1013 struct snd_card *card = rtd->card->snd_card;
1014 struct snd_kcontrol *kctrl;
1015 struct snd_kcontrol_new knew = {
1016 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1018 .info = rsnd_kctrl_info,
1020 .get = rsnd_kctrl_get,
1021 .put = rsnd_kctrl_put,
1022 .private_value = (unsigned long)cfg,
1026 kctrl = snd_ctl_new1(&knew, mod);
1030 ret = snd_ctl_add(card, kctrl);
1034 cfg->update = update;
1042 void _rsnd_kctrl_remove(struct rsnd_kctrl_cfg *cfg)
1044 if (cfg->card && cfg->kctrl)
1045 snd_ctl_remove(cfg->card, cfg->kctrl);
1051 int rsnd_kctrl_new_m(struct rsnd_mod *mod,
1052 struct rsnd_dai_stream *io,
1053 struct snd_soc_pcm_runtime *rtd,
1054 const unsigned char *name,
1055 void (*update)(struct rsnd_dai_stream *io,
1056 struct rsnd_mod *mod),
1057 struct rsnd_kctrl_cfg_m *_cfg,
1061 if (ch_size > RSND_MAX_CHANNELS)
1064 _cfg->cfg.max = max;
1065 _cfg->cfg.size = ch_size;
1066 _cfg->cfg.val = _cfg->val;
1067 return __rsnd_kctrl_new(mod, io, rtd, name, &_cfg->cfg, update);
1070 int rsnd_kctrl_new_s(struct rsnd_mod *mod,
1071 struct rsnd_dai_stream *io,
1072 struct snd_soc_pcm_runtime *rtd,
1073 const unsigned char *name,
1074 void (*update)(struct rsnd_dai_stream *io,
1075 struct rsnd_mod *mod),
1076 struct rsnd_kctrl_cfg_s *_cfg,
1079 _cfg->cfg.max = max;
1081 _cfg->cfg.val = &_cfg->val;
1082 return __rsnd_kctrl_new(mod, io, rtd, name, &_cfg->cfg, update);
1085 int rsnd_kctrl_new_e(struct rsnd_mod *mod,
1086 struct rsnd_dai_stream *io,
1087 struct snd_soc_pcm_runtime *rtd,
1088 const unsigned char *name,
1089 struct rsnd_kctrl_cfg_s *_cfg,
1090 void (*update)(struct rsnd_dai_stream *io,
1091 struct rsnd_mod *mod),
1092 const char * const *texts,
1095 _cfg->cfg.max = max;
1097 _cfg->cfg.val = &_cfg->val;
1098 _cfg->cfg.texts = texts;
1099 return __rsnd_kctrl_new(mod, io, rtd, name, &_cfg->cfg, update);
1106 #define PREALLOC_BUFFER (32 * 1024)
1107 #define PREALLOC_BUFFER_MAX (32 * 1024)
1109 static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd)
1111 struct snd_soc_dai *dai = rtd->cpu_dai;
1112 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1115 ret = rsnd_dai_call(pcm_new, &rdai->playback, rtd);
1119 ret = rsnd_dai_call(pcm_new, &rdai->capture, rtd);
1123 return snd_pcm_lib_preallocate_pages_for_all(
1125 SNDRV_DMA_TYPE_CONTINUOUS,
1126 snd_dma_continuous_data(GFP_KERNEL),
1127 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
1130 static struct snd_soc_platform_driver rsnd_soc_platform = {
1131 .ops = &rsnd_pcm_ops,
1132 .pcm_new = rsnd_pcm_new,
1135 static const struct snd_soc_component_driver rsnd_soc_component = {
1139 static int rsnd_rdai_continuance_probe(struct rsnd_priv *priv,
1140 struct rsnd_dai_stream *io)
1144 ret = rsnd_dai_call(probe, io, priv);
1145 if (ret == -EAGAIN) {
1146 struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io);
1147 struct rsnd_mod *mod;
1151 * Fallback to PIO mode
1155 * call "remove" for SSI/SRC/DVC
1156 * SSI will be switch to PIO mode if it was DMA mode
1159 * rsnd_ssi_fallback()
1161 rsnd_dai_call(remove, io, priv);
1164 * remove all mod from io
1165 * and, re connect ssi
1167 for_each_rsnd_mod(i, mod, io)
1168 rsnd_dai_disconnect(mod, io, i);
1169 rsnd_dai_connect(ssi_mod, io, RSND_MOD_SSI);
1174 rsnd_dai_call(fallback, io, priv);
1178 * DAI has SSI which is PIO mode only now.
1180 ret = rsnd_dai_call(probe, io, priv);
1189 static int rsnd_probe(struct platform_device *pdev)
1191 struct rsnd_priv *priv;
1192 struct device *dev = &pdev->dev;
1193 struct rsnd_dai *rdai;
1194 int (*probe_func[])(struct rsnd_priv *priv) = {
1212 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1214 dev_err(dev, "priv allocate failed\n");
1219 priv->flags = (unsigned long)of_device_get_match_data(dev);
1220 spin_lock_init(&priv->lock);
1225 for (i = 0; i < ARRAY_SIZE(probe_func); i++) {
1226 ret = probe_func[i](priv);
1231 for_each_rsnd_dai(rdai, priv, i) {
1232 ret = rsnd_rdai_continuance_probe(priv, &rdai->playback);
1234 goto exit_snd_probe;
1236 ret = rsnd_rdai_continuance_probe(priv, &rdai->capture);
1238 goto exit_snd_probe;
1241 dev_set_drvdata(dev, priv);
1246 ret = snd_soc_register_platform(dev, &rsnd_soc_platform);
1248 dev_err(dev, "cannot snd soc register\n");
1252 ret = snd_soc_register_component(dev, &rsnd_soc_component,
1253 priv->daidrv, rsnd_rdai_nr(priv));
1255 dev_err(dev, "cannot snd dai register\n");
1259 pm_runtime_enable(dev);
1261 dev_info(dev, "probed\n");
1265 snd_soc_unregister_platform(dev);
1267 for_each_rsnd_dai(rdai, priv, i) {
1268 rsnd_dai_call(remove, &rdai->playback, priv);
1269 rsnd_dai_call(remove, &rdai->capture, priv);
1275 static int rsnd_remove(struct platform_device *pdev)
1277 struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
1278 struct rsnd_dai *rdai;
1279 void (*remove_func[])(struct rsnd_priv *priv) = {
1291 pm_runtime_disable(&pdev->dev);
1293 for_each_rsnd_dai(rdai, priv, i) {
1294 ret |= rsnd_dai_call(remove, &rdai->playback, priv);
1295 ret |= rsnd_dai_call(remove, &rdai->capture, priv);
1298 for (i = 0; i < ARRAY_SIZE(remove_func); i++)
1299 remove_func[i](priv);
1301 snd_soc_unregister_component(&pdev->dev);
1302 snd_soc_unregister_platform(&pdev->dev);
1307 static int rsnd_suspend(struct device *dev)
1309 struct rsnd_priv *priv = dev_get_drvdata(dev);
1311 rsnd_adg_clk_disable(priv);
1316 static int rsnd_resume(struct device *dev)
1318 struct rsnd_priv *priv = dev_get_drvdata(dev);
1320 rsnd_adg_clk_enable(priv);
1325 static struct dev_pm_ops rsnd_pm_ops = {
1326 .suspend = rsnd_suspend,
1327 .resume = rsnd_resume,
1330 static struct platform_driver rsnd_driver = {
1332 .name = "rcar_sound",
1334 .of_match_table = rsnd_of_match,
1336 .probe = rsnd_probe,
1337 .remove = rsnd_remove,
1339 module_platform_driver(rsnd_driver);
1341 MODULE_LICENSE("GPL");
1342 MODULE_DESCRIPTION("Renesas R-Car audio driver");
1343 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
1344 MODULE_ALIAS("platform:rcar-pcm-audio");