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_192000
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);
113 void rsnd_mod_make_sure(struct rsnd_mod *mod, enum rsnd_mod_type type)
115 if (mod->type != type) {
116 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
117 struct device *dev = rsnd_priv_to_dev(priv);
119 dev_warn(dev, "%s[%d] is not your expected module\n",
120 rsnd_mod_name(mod), rsnd_mod_id(mod));
124 char *rsnd_mod_name(struct rsnd_mod *mod)
126 if (!mod || !mod->ops)
129 return mod->ops->name;
132 struct dma_chan *rsnd_mod_dma_req(struct rsnd_dai_stream *io,
133 struct rsnd_mod *mod)
135 if (!mod || !mod->ops || !mod->ops->dma_req)
138 return mod->ops->dma_req(io, mod);
141 u32 *rsnd_mod_get_status(struct rsnd_dai_stream *io,
142 struct rsnd_mod *mod,
143 enum rsnd_mod_type type)
148 int rsnd_mod_init(struct rsnd_priv *priv,
149 struct rsnd_mod *mod,
150 struct rsnd_mod_ops *ops,
152 u32* (*get_status)(struct rsnd_dai_stream *io,
153 struct rsnd_mod *mod,
154 enum rsnd_mod_type type),
155 enum rsnd_mod_type type,
158 int ret = clk_prepare(clk);
168 mod->get_status = get_status;
173 void rsnd_mod_quit(struct rsnd_mod *mod)
176 clk_unprepare(mod->clk);
180 void rsnd_mod_interrupt(struct rsnd_mod *mod,
181 void (*callback)(struct rsnd_mod *mod,
182 struct rsnd_dai_stream *io))
184 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
185 struct rsnd_dai_stream *io;
186 struct rsnd_dai *rdai;
189 for_each_rsnd_dai(rdai, priv, i) {
190 io = &rdai->playback;
191 if (mod == io->mod[mod->type])
195 if (mod == io->mod[mod->type])
200 int rsnd_io_is_working(struct rsnd_dai_stream *io)
202 /* see rsnd_dai_stream_init/quit() */
203 return !!io->substream;
206 void rsnd_set_slot(struct rsnd_dai *rdai,
210 rdai->slots_num = num;
213 int rsnd_get_slot(struct rsnd_dai_stream *io)
215 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
220 int rsnd_get_slot_num(struct rsnd_dai_stream *io)
222 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
224 return rdai->slots_num;
227 int rsnd_runtime_channel_original(struct rsnd_dai_stream *io)
229 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
231 return runtime->channels;
234 int rsnd_runtime_channel_after_ctu(struct rsnd_dai_stream *io)
236 int chan = rsnd_runtime_channel_original(io);
237 struct rsnd_mod *ctu_mod = rsnd_io_to_mod_ctu(io);
240 u32 converted_chan = rsnd_ctu_converted_channel(ctu_mod);
243 return converted_chan;
249 int rsnd_runtime_channel_for_ssi(struct rsnd_dai_stream *io)
251 int chan = rsnd_io_is_play(io) ?
252 rsnd_runtime_channel_after_ctu(io) :
253 rsnd_runtime_channel_original(io);
256 if (rsnd_runtime_is_ssi_multi(io))
257 chan /= rsnd_get_slot_num(io);
259 /* TDM Extend Mode needs 8ch */
266 int rsnd_runtime_is_ssi_multi(struct rsnd_dai_stream *io)
268 int slots = rsnd_get_slot_num(io);
269 int chan = rsnd_io_is_play(io) ?
270 rsnd_runtime_channel_after_ctu(io) :
271 rsnd_runtime_channel_original(io);
273 return (chan >= 6) && (slots > 1);
276 int rsnd_runtime_is_ssi_tdm(struct rsnd_dai_stream *io)
278 return rsnd_runtime_channel_for_ssi(io) >= 6;
284 u32 rsnd_get_adinr_bit(struct rsnd_mod *mod, struct rsnd_dai_stream *io)
286 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
287 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
288 struct device *dev = rsnd_priv_to_dev(priv);
290 switch (runtime->sample_bits) {
297 dev_warn(dev, "not supported sample bits\n");
305 u32 rsnd_get_dalign(struct rsnd_mod *mod, struct rsnd_dai_stream *io)
307 struct rsnd_mod *ssiu = rsnd_io_to_mod_ssiu(io);
308 struct rsnd_mod *target;
309 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
310 u32 val = 0x76543210;
313 if (rsnd_io_is_play(io)) {
314 struct rsnd_mod *src = rsnd_io_to_mod_src(io);
316 target = src ? src : ssiu;
318 struct rsnd_mod *cmd = rsnd_io_to_mod_cmd(io);
320 target = cmd ? cmd : ssiu;
323 mask <<= runtime->channels * 4;
326 switch (runtime->sample_bits) {
328 val |= 0x67452301 & ~mask;
331 val |= 0x76543210 & ~mask;
336 * exchange channeles on SRC if possible,
337 * otherwise, R/L volume settings on DVC
338 * changes inverted channels
346 u32 rsnd_get_busif_shift(struct rsnd_dai_stream *io, struct rsnd_mod *mod)
348 enum rsnd_mod_type playback_mods[] = {
353 enum rsnd_mod_type capture_mods[] = {
358 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
359 struct rsnd_mod *tmod = NULL;
360 enum rsnd_mod_type *mods =
361 rsnd_io_is_play(io) ?
362 playback_mods : capture_mods;
366 * This is needed for 24bit data
367 * We need to shift 8bit
369 * Linux 24bit data is located as 0x00******
370 * HW 24bit data is located as 0x******00
373 switch (runtime->sample_bits) {
380 for (i = 0; i < ARRAY_SIZE(playback_mods); i++) {
381 tmod = rsnd_io_to_mod(io, mods[i]);
389 if (rsnd_io_is_play(io))
390 return (0 << 20) | /* shift to Left */
391 (8 << 16); /* 8bit */
393 return (1 << 20) | /* shift to Right */
394 (8 << 16); /* 8bit */
400 struct rsnd_mod *rsnd_mod_next(int *iterator,
401 struct rsnd_dai_stream *io,
402 enum rsnd_mod_type *array,
405 struct rsnd_mod *mod;
406 enum rsnd_mod_type type;
407 int max = array ? array_size : RSND_MOD_MAX;
409 for (; *iterator < max; (*iterator)++) {
410 type = (array) ? array[*iterator] : *iterator;
421 static enum rsnd_mod_type rsnd_mod_sequence[][RSND_MOD_MAX] = {
455 static int rsnd_status_update(u32 *status,
456 int shift, int add, int timing)
458 u32 mask = 0xF << shift;
459 u8 val = (*status >> shift) & 0xF;
460 u8 next_val = (val + add) & 0xF;
461 int func_call = (val == timing);
463 if (next_val == 0xF) /* underflow case */
466 *status = (*status & ~mask) + (next_val << shift);
471 #define rsnd_dai_call(fn, io, param...) \
473 struct rsnd_priv *priv = rsnd_io_to_priv(io); \
474 struct device *dev = rsnd_priv_to_dev(priv); \
475 struct rsnd_mod *mod; \
476 int is_play = rsnd_io_is_play(io); \
478 enum rsnd_mod_type *types = rsnd_mod_sequence[is_play]; \
479 for_each_rsnd_mod_arrays(i, mod, io, types, RSND_MOD_MAX) { \
481 u32 *status = mod->get_status(io, mod, types[i]); \
482 int func_call = rsnd_status_update(status, \
483 __rsnd_mod_shift_##fn, \
484 __rsnd_mod_add_##fn, \
485 __rsnd_mod_call_##fn); \
486 dev_dbg(dev, "%s[%d]\t0x%08x %s\n", \
487 rsnd_mod_name(mod), rsnd_mod_id(mod), *status, \
488 (func_call && (mod)->ops->fn) ? #fn : ""); \
489 if (func_call && (mod)->ops->fn) \
490 tmp = (mod)->ops->fn(mod, io, param); \
492 dev_err(dev, "%s[%d] : %s error %d\n", \
493 rsnd_mod_name(mod), rsnd_mod_id(mod), \
500 int rsnd_dai_connect(struct rsnd_mod *mod,
501 struct rsnd_dai_stream *io,
502 enum rsnd_mod_type type)
504 struct rsnd_priv *priv;
510 if (io->mod[type] == mod)
516 priv = rsnd_mod_to_priv(mod);
517 dev = rsnd_priv_to_dev(priv);
521 dev_dbg(dev, "%s[%d] is connected to io (%s)\n",
522 rsnd_mod_name(mod), rsnd_mod_id(mod),
523 rsnd_io_is_play(io) ? "Playback" : "Capture");
528 static void rsnd_dai_disconnect(struct rsnd_mod *mod,
529 struct rsnd_dai_stream *io,
530 enum rsnd_mod_type type)
532 io->mod[type] = NULL;
535 struct rsnd_dai *rsnd_rdai_get(struct rsnd_priv *priv, int id)
537 if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
540 return priv->rdai + id;
543 #define rsnd_dai_to_priv(dai) snd_soc_dai_get_drvdata(dai)
544 static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai)
546 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
548 return rsnd_rdai_get(priv, dai->id);
552 * rsnd_soc_dai functions
554 int rsnd_dai_pointer_offset(struct rsnd_dai_stream *io, int additional)
556 struct snd_pcm_substream *substream = io->substream;
557 struct snd_pcm_runtime *runtime = substream->runtime;
558 int pos = io->byte_pos + additional;
560 pos %= (runtime->periods * io->byte_per_period);
565 bool rsnd_dai_pointer_update(struct rsnd_dai_stream *io, int byte)
567 io->byte_pos += byte;
569 if (io->byte_pos >= io->next_period_byte) {
570 struct snd_pcm_substream *substream = io->substream;
571 struct snd_pcm_runtime *runtime = substream->runtime;
574 io->next_period_byte += io->byte_per_period;
576 if (io->period_pos >= runtime->periods) {
579 io->next_period_byte = io->byte_per_period;
588 void rsnd_dai_period_elapsed(struct rsnd_dai_stream *io)
590 struct snd_pcm_substream *substream = io->substream;
593 * this function should be called...
595 * - if rsnd_dai_pointer_update() returns true
596 * - without spin lock
599 snd_pcm_period_elapsed(substream);
602 static void rsnd_dai_stream_init(struct rsnd_dai_stream *io,
603 struct snd_pcm_substream *substream)
605 struct snd_pcm_runtime *runtime = substream->runtime;
607 io->substream = substream;
610 io->byte_per_period = runtime->period_size *
612 samples_to_bytes(runtime, 1);
613 io->next_period_byte = io->byte_per_period;
616 static void rsnd_dai_stream_quit(struct rsnd_dai_stream *io)
618 io->substream = NULL;
622 struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream)
624 struct snd_soc_pcm_runtime *rtd = substream->private_data;
630 struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai,
631 struct snd_pcm_substream *substream)
633 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
634 return &rdai->playback;
636 return &rdai->capture;
639 static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd,
640 struct snd_soc_dai *dai)
642 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
643 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
644 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
648 spin_lock_irqsave(&priv->lock, flags);
651 case SNDRV_PCM_TRIGGER_START:
652 case SNDRV_PCM_TRIGGER_RESUME:
653 rsnd_dai_stream_init(io, substream);
655 ret = rsnd_dai_call(init, io, priv);
657 goto dai_trigger_end;
659 ret = rsnd_dai_call(start, io, priv);
661 goto dai_trigger_end;
663 ret = rsnd_dai_call(irq, io, priv, 1);
665 goto dai_trigger_end;
668 case SNDRV_PCM_TRIGGER_STOP:
669 case SNDRV_PCM_TRIGGER_SUSPEND:
670 ret = rsnd_dai_call(irq, io, priv, 0);
672 ret |= rsnd_dai_call(stop, io, priv);
674 ret |= rsnd_dai_call(quit, io, priv);
676 rsnd_dai_stream_quit(io);
683 spin_unlock_irqrestore(&priv->lock, flags);
688 static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
690 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
692 /* set master/slave audio interface */
693 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
694 case SND_SOC_DAIFMT_CBM_CFM:
695 rdai->clk_master = 0;
697 case SND_SOC_DAIFMT_CBS_CFS:
698 rdai->clk_master = 1; /* codec is slave, cpu is master */
705 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
706 case SND_SOC_DAIFMT_I2S:
708 rdai->data_alignment = 0;
709 rdai->frm_clk_inv = 0;
711 case SND_SOC_DAIFMT_LEFT_J:
713 rdai->data_alignment = 0;
714 rdai->frm_clk_inv = 1;
716 case SND_SOC_DAIFMT_RIGHT_J:
718 rdai->data_alignment = 1;
719 rdai->frm_clk_inv = 1;
723 /* set clock inversion */
724 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
725 case SND_SOC_DAIFMT_NB_IF:
726 rdai->frm_clk_inv = !rdai->frm_clk_inv;
728 case SND_SOC_DAIFMT_IB_NF:
729 rdai->bit_clk_inv = !rdai->bit_clk_inv;
731 case SND_SOC_DAIFMT_IB_IF:
732 rdai->bit_clk_inv = !rdai->bit_clk_inv;
733 rdai->frm_clk_inv = !rdai->frm_clk_inv;
735 case SND_SOC_DAIFMT_NB_NF:
743 static int rsnd_soc_set_dai_tdm_slot(struct snd_soc_dai *dai,
744 u32 tx_mask, u32 rx_mask,
745 int slots, int slot_width)
747 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
748 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
749 struct device *dev = rsnd_priv_to_dev(priv);
753 /* TDM Extend Mode */
754 rsnd_set_slot(rdai, slots, 1);
757 dev_err(dev, "unsupported TDM slots (%d)\n", slots);
764 static int rsnd_soc_dai_startup(struct snd_pcm_substream *substream,
765 struct snd_soc_dai *dai)
767 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
768 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
771 * call rsnd_dai_call without spinlock
773 return rsnd_dai_call(nolock_start, io, priv);
776 static void rsnd_soc_dai_shutdown(struct snd_pcm_substream *substream,
777 struct snd_soc_dai *dai)
779 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
780 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
783 * call rsnd_dai_call without spinlock
785 rsnd_dai_call(nolock_stop, io, priv);
788 static const struct snd_soc_dai_ops rsnd_soc_dai_ops = {
789 .startup = rsnd_soc_dai_startup,
790 .shutdown = rsnd_soc_dai_shutdown,
791 .trigger = rsnd_soc_dai_trigger,
792 .set_fmt = rsnd_soc_dai_set_fmt,
793 .set_tdm_slot = rsnd_soc_set_dai_tdm_slot,
796 void rsnd_parse_connect_common(struct rsnd_dai *rdai,
797 struct rsnd_mod* (*mod_get)(struct rsnd_priv *priv, int id),
798 struct device_node *node,
799 struct device_node *playback,
800 struct device_node *capture)
802 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
803 struct device_node *np;
804 struct rsnd_mod *mod;
811 for_each_child_of_node(node, np) {
812 mod = mod_get(priv, i);
814 rsnd_dai_connect(mod, &rdai->playback, mod->type);
816 rsnd_dai_connect(mod, &rdai->capture, mod->type);
823 static int rsnd_dai_probe(struct rsnd_priv *priv)
825 struct device_node *dai_node;
826 struct device_node *dai_np;
827 struct device_node *playback, *capture;
828 struct rsnd_dai_stream *io_playback;
829 struct rsnd_dai_stream *io_capture;
830 struct snd_soc_dai_driver *rdrv, *drv;
831 struct rsnd_dai *rdai;
832 struct device *dev = rsnd_priv_to_dev(priv);
836 dai_node = rsnd_dai_of_node(priv);
837 nr = of_get_child_count(dai_node);
840 goto rsnd_dai_probe_done;
843 rdrv = devm_kzalloc(dev, sizeof(*rdrv) * nr, GFP_KERNEL);
844 rdai = devm_kzalloc(dev, sizeof(*rdai) * nr, GFP_KERNEL);
845 if (!rdrv || !rdai) {
847 goto rsnd_dai_probe_done;
858 for_each_child_of_node(dai_node, dai_np) {
859 rdai = rsnd_rdai_get(priv, dai_i);
861 io_playback = &rdai->playback;
862 io_capture = &rdai->capture;
864 snprintf(rdai->name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", dai_i);
867 drv->name = rdai->name;
868 drv->ops = &rsnd_soc_dai_ops;
870 snprintf(rdai->playback.name, RSND_DAI_NAME_SIZE,
871 "DAI%d Playback", dai_i);
872 drv->playback.rates = RSND_RATES;
873 drv->playback.formats = RSND_FMTS;
874 drv->playback.channels_min = 2;
875 drv->playback.channels_max = 6;
876 drv->playback.stream_name = rdai->playback.name;
878 snprintf(rdai->capture.name, RSND_DAI_NAME_SIZE,
879 "DAI%d Capture", dai_i);
880 drv->capture.rates = RSND_RATES;
881 drv->capture.formats = RSND_FMTS;
882 drv->capture.channels_min = 2;
883 drv->capture.channels_max = 6;
884 drv->capture.stream_name = rdai->capture.name;
886 rdai->playback.rdai = rdai;
887 rdai->capture.rdai = rdai;
888 rsnd_set_slot(rdai, 2, 1); /* default */
890 for (io_i = 0;; io_i++) {
891 playback = of_parse_phandle(dai_np, "playback", io_i);
892 capture = of_parse_phandle(dai_np, "capture", io_i);
894 if (!playback && !capture)
897 rsnd_parse_connect_ssi(rdai, playback, capture);
898 rsnd_parse_connect_src(rdai, playback, capture);
899 rsnd_parse_connect_ctu(rdai, playback, capture);
900 rsnd_parse_connect_mix(rdai, playback, capture);
901 rsnd_parse_connect_dvc(rdai, playback, capture);
903 of_node_put(playback);
904 of_node_put(capture);
909 dev_dbg(dev, "%s (%s/%s)\n", rdai->name,
910 rsnd_io_to_mod_ssi(io_playback) ? "play" : " -- ",
911 rsnd_io_to_mod_ssi(io_capture) ? "capture" : " -- ");
917 of_node_put(dai_node);
925 static struct snd_pcm_hardware rsnd_pcm_hardware = {
926 .info = SNDRV_PCM_INFO_INTERLEAVED |
927 SNDRV_PCM_INFO_MMAP |
928 SNDRV_PCM_INFO_MMAP_VALID,
929 .buffer_bytes_max = 64 * 1024,
930 .period_bytes_min = 32,
931 .period_bytes_max = 8192,
937 static int rsnd_pcm_open(struct snd_pcm_substream *substream)
939 struct snd_pcm_runtime *runtime = substream->runtime;
942 snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware);
944 ret = snd_pcm_hw_constraint_integer(runtime,
945 SNDRV_PCM_HW_PARAM_PERIODS);
950 static int rsnd_hw_params(struct snd_pcm_substream *substream,
951 struct snd_pcm_hw_params *hw_params)
953 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
954 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
955 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
958 ret = rsnd_dai_call(hw_params, io, substream, hw_params);
962 return snd_pcm_lib_malloc_pages(substream,
963 params_buffer_bytes(hw_params));
966 static snd_pcm_uframes_t rsnd_pointer(struct snd_pcm_substream *substream)
968 struct snd_pcm_runtime *runtime = substream->runtime;
969 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
970 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
971 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
973 return bytes_to_frames(runtime, io->byte_pos);
976 static struct snd_pcm_ops rsnd_pcm_ops = {
977 .open = rsnd_pcm_open,
978 .ioctl = snd_pcm_lib_ioctl,
979 .hw_params = rsnd_hw_params,
980 .hw_free = snd_pcm_lib_free_pages,
981 .pointer = rsnd_pointer,
987 #define kcontrol_to_cfg(kctrl) ((struct rsnd_kctrl_cfg *)kctrl->private_value)
988 static int rsnd_kctrl_info(struct snd_kcontrol *kctrl,
989 struct snd_ctl_elem_info *uinfo)
991 struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
994 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
995 uinfo->count = cfg->size;
996 uinfo->value.enumerated.items = cfg->max;
997 if (uinfo->value.enumerated.item >= cfg->max)
998 uinfo->value.enumerated.item = cfg->max - 1;
999 strlcpy(uinfo->value.enumerated.name,
1000 cfg->texts[uinfo->value.enumerated.item],
1001 sizeof(uinfo->value.enumerated.name));
1003 uinfo->count = cfg->size;
1004 uinfo->value.integer.min = 0;
1005 uinfo->value.integer.max = cfg->max;
1006 uinfo->type = (cfg->max == 1) ?
1007 SNDRV_CTL_ELEM_TYPE_BOOLEAN :
1008 SNDRV_CTL_ELEM_TYPE_INTEGER;
1014 static int rsnd_kctrl_get(struct snd_kcontrol *kctrl,
1015 struct snd_ctl_elem_value *uc)
1017 struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
1020 for (i = 0; i < cfg->size; i++)
1022 uc->value.enumerated.item[i] = cfg->val[i];
1024 uc->value.integer.value[i] = cfg->val[i];
1029 static int rsnd_kctrl_put(struct snd_kcontrol *kctrl,
1030 struct snd_ctl_elem_value *uc)
1032 struct rsnd_mod *mod = snd_kcontrol_chip(kctrl);
1033 struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
1036 for (i = 0; i < cfg->size; i++) {
1038 change |= (uc->value.enumerated.item[i] != cfg->val[i]);
1039 cfg->val[i] = uc->value.enumerated.item[i];
1041 change |= (uc->value.integer.value[i] != cfg->val[i]);
1042 cfg->val[i] = uc->value.integer.value[i];
1046 if (change && cfg->update)
1047 cfg->update(cfg->io, mod);
1052 struct rsnd_kctrl_cfg *rsnd_kctrl_init_m(struct rsnd_kctrl_cfg_m *cfg)
1054 cfg->cfg.val = cfg->val;
1059 struct rsnd_kctrl_cfg *rsnd_kctrl_init_s(struct rsnd_kctrl_cfg_s *cfg)
1061 cfg->cfg.val = &cfg->val;
1066 int rsnd_kctrl_new(struct rsnd_mod *mod,
1067 struct rsnd_dai_stream *io,
1068 struct snd_soc_pcm_runtime *rtd,
1069 const unsigned char *name,
1070 void (*update)(struct rsnd_dai_stream *io,
1071 struct rsnd_mod *mod),
1072 struct rsnd_kctrl_cfg *cfg,
1073 const char * const *texts,
1077 struct snd_card *card = rtd->card->snd_card;
1078 struct snd_kcontrol *kctrl;
1079 struct snd_kcontrol_new knew = {
1080 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1082 .info = rsnd_kctrl_info,
1084 .get = rsnd_kctrl_get,
1085 .put = rsnd_kctrl_put,
1086 .private_value = (unsigned long)cfg,
1090 if (size > RSND_MAX_CHANNELS)
1093 kctrl = snd_ctl_new1(&knew, mod);
1097 ret = snd_ctl_add(card, kctrl);
1104 cfg->update = update;
1116 #define PREALLOC_BUFFER (32 * 1024)
1117 #define PREALLOC_BUFFER_MAX (32 * 1024)
1119 static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd)
1121 struct snd_soc_dai *dai = rtd->cpu_dai;
1122 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1125 ret = rsnd_dai_call(pcm_new, &rdai->playback, rtd);
1129 ret = rsnd_dai_call(pcm_new, &rdai->capture, rtd);
1133 return snd_pcm_lib_preallocate_pages_for_all(
1135 SNDRV_DMA_TYPE_CONTINUOUS,
1136 snd_dma_continuous_data(GFP_KERNEL),
1137 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
1140 static struct snd_soc_platform_driver rsnd_soc_platform = {
1141 .ops = &rsnd_pcm_ops,
1142 .pcm_new = rsnd_pcm_new,
1145 static const struct snd_soc_component_driver rsnd_soc_component = {
1149 static int rsnd_rdai_continuance_probe(struct rsnd_priv *priv,
1150 struct rsnd_dai_stream *io)
1154 ret = rsnd_dai_call(probe, io, priv);
1155 if (ret == -EAGAIN) {
1156 struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io);
1157 struct rsnd_mod *mod;
1161 * Fallback to PIO mode
1165 * call "remove" for SSI/SRC/DVC
1166 * SSI will be switch to PIO mode if it was DMA mode
1169 * rsnd_ssi_fallback()
1171 rsnd_dai_call(remove, io, priv);
1174 * remove all mod from io
1175 * and, re connect ssi
1177 for_each_rsnd_mod(i, mod, io)
1178 rsnd_dai_disconnect(mod, io, i);
1179 rsnd_dai_connect(ssi_mod, io, RSND_MOD_SSI);
1184 rsnd_dai_call(fallback, io, priv);
1188 * DAI has SSI which is PIO mode only now.
1190 ret = rsnd_dai_call(probe, io, priv);
1199 static int rsnd_probe(struct platform_device *pdev)
1201 struct rsnd_priv *priv;
1202 struct device *dev = &pdev->dev;
1203 struct rsnd_dai *rdai;
1204 int (*probe_func[])(struct rsnd_priv *priv) = {
1222 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1224 dev_err(dev, "priv allocate failed\n");
1229 priv->flags = (unsigned long)of_device_get_match_data(dev);
1230 spin_lock_init(&priv->lock);
1235 for (i = 0; i < ARRAY_SIZE(probe_func); i++) {
1236 ret = probe_func[i](priv);
1241 for_each_rsnd_dai(rdai, priv, i) {
1242 ret = rsnd_rdai_continuance_probe(priv, &rdai->playback);
1244 goto exit_snd_probe;
1246 ret = rsnd_rdai_continuance_probe(priv, &rdai->capture);
1248 goto exit_snd_probe;
1251 dev_set_drvdata(dev, priv);
1256 ret = snd_soc_register_platform(dev, &rsnd_soc_platform);
1258 dev_err(dev, "cannot snd soc register\n");
1262 ret = snd_soc_register_component(dev, &rsnd_soc_component,
1263 priv->daidrv, rsnd_rdai_nr(priv));
1265 dev_err(dev, "cannot snd dai register\n");
1269 pm_runtime_enable(dev);
1271 dev_info(dev, "probed\n");
1275 snd_soc_unregister_platform(dev);
1277 for_each_rsnd_dai(rdai, priv, i) {
1278 rsnd_dai_call(remove, &rdai->playback, priv);
1279 rsnd_dai_call(remove, &rdai->capture, priv);
1285 static int rsnd_remove(struct platform_device *pdev)
1287 struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
1288 struct rsnd_dai *rdai;
1289 void (*remove_func[])(struct rsnd_priv *priv) = {
1301 pm_runtime_disable(&pdev->dev);
1303 for_each_rsnd_dai(rdai, priv, i) {
1304 ret |= rsnd_dai_call(remove, &rdai->playback, priv);
1305 ret |= rsnd_dai_call(remove, &rdai->capture, priv);
1308 for (i = 0; i < ARRAY_SIZE(remove_func); i++)
1309 remove_func[i](priv);
1311 snd_soc_unregister_component(&pdev->dev);
1312 snd_soc_unregister_platform(&pdev->dev);
1317 static int rsnd_suspend(struct device *dev)
1319 struct rsnd_priv *priv = dev_get_drvdata(dev);
1321 rsnd_adg_clk_disable(priv);
1326 static int rsnd_resume(struct device *dev)
1328 struct rsnd_priv *priv = dev_get_drvdata(dev);
1330 rsnd_adg_clk_enable(priv);
1335 static struct dev_pm_ops rsnd_pm_ops = {
1336 .suspend = rsnd_suspend,
1337 .resume = rsnd_resume,
1340 static struct platform_driver rsnd_driver = {
1342 .name = "rcar_sound",
1344 .of_match_table = rsnd_of_match,
1346 .probe = rsnd_probe,
1347 .remove = rsnd_remove,
1349 module_platform_driver(rsnd_driver);
1351 MODULE_LICENSE("GPL");
1352 MODULE_DESCRIPTION("Renesas R-Car audio driver");
1353 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
1354 MODULE_ALIAS("platform:rcar-pcm-audio");