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);
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 int rsnd_mod_init(struct rsnd_priv *priv,
142 struct rsnd_mod *mod,
143 struct rsnd_mod_ops *ops,
145 enum rsnd_mod_type type,
148 int ret = clk_prepare(clk);
162 void rsnd_mod_quit(struct rsnd_mod *mod)
165 clk_unprepare(mod->clk);
168 void rsnd_mod_interrupt(struct rsnd_mod *mod,
169 void (*callback)(struct rsnd_mod *mod,
170 struct rsnd_dai_stream *io))
172 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
173 struct rsnd_dai_stream *io;
174 struct rsnd_dai *rdai;
177 for_each_rsnd_dai(rdai, priv, i) {
178 io = &rdai->playback;
179 if (mod == io->mod[mod->type])
183 if (mod == io->mod[mod->type])
188 int rsnd_io_is_working(struct rsnd_dai_stream *io)
190 /* see rsnd_dai_stream_init/quit() */
191 return !!io->substream;
194 void rsnd_set_slot(struct rsnd_dai *rdai,
198 rdai->slots_num = num;
201 int rsnd_get_slot(struct rsnd_dai_stream *io)
203 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
208 int rsnd_get_slot_num(struct rsnd_dai_stream *io)
210 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
212 return rdai->slots_num;
215 int rsnd_get_slot_width(struct rsnd_dai_stream *io)
217 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
218 int chan = runtime->channels;
220 /* Multi channel Mode */
221 if (rsnd_ssi_multi_slaves(io))
222 chan /= rsnd_get_slot_num(io);
224 /* TDM Extend Mode needs 8ch */
234 u32 rsnd_get_adinr_bit(struct rsnd_mod *mod, struct rsnd_dai_stream *io)
236 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
237 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
238 struct device *dev = rsnd_priv_to_dev(priv);
240 switch (runtime->sample_bits) {
247 dev_warn(dev, "not supported sample bits\n");
252 u32 rsnd_get_adinr_chan(struct rsnd_mod *mod, struct rsnd_dai_stream *io)
254 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
255 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
256 struct device *dev = rsnd_priv_to_dev(priv);
257 u32 chan = runtime->channels;
267 dev_warn(dev, "not supported channel\n");
278 u32 rsnd_get_dalign(struct rsnd_mod *mod, struct rsnd_dai_stream *io)
280 struct rsnd_mod *ssi = rsnd_io_to_mod_ssi(io);
281 struct rsnd_mod *target;
282 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
283 u32 val = 0x76543210;
286 if (rsnd_io_is_play(io)) {
287 struct rsnd_mod *src = rsnd_io_to_mod_src(io);
289 target = src ? src : ssi;
291 struct rsnd_mod *cmd = rsnd_io_to_mod_cmd(io);
293 target = cmd ? cmd : ssi;
296 mask <<= runtime->channels * 4;
299 switch (runtime->sample_bits) {
301 val |= 0x67452301 & ~mask;
304 val |= 0x76543210 & ~mask;
309 * exchange channeles on SRC if possible,
310 * otherwise, R/L volume settings on DVC
311 * changes inverted channels
322 #define rsnd_mod_call(idx, io, func, param...) \
324 struct rsnd_priv *priv = rsnd_mod_to_priv(mod); \
325 struct rsnd_mod *mod = (io)->mod[idx]; \
326 struct device *dev = rsnd_priv_to_dev(priv); \
327 u32 *status = (io)->mod_status + idx; \
328 u32 mask = 0xF << __rsnd_mod_shift_##func; \
329 u8 val = (*status >> __rsnd_mod_shift_##func) & 0xF; \
330 u8 add = ((val + __rsnd_mod_add_##func) & 0xF); \
332 int call = (val == __rsnd_mod_call_##func) && (mod)->ops->func; \
333 *status = (*status & ~mask) + \
334 (add << __rsnd_mod_shift_##func); \
335 dev_dbg(dev, "%s[%d]\t0x%08x %s\n", \
336 rsnd_mod_name(mod), rsnd_mod_id(mod), \
337 *status, call ? #func : ""); \
339 ret = (mod)->ops->func(mod, io, param); \
343 #define rsnd_dai_call(fn, io, param...) \
345 struct rsnd_mod *mod; \
347 for (i = 0; i < RSND_MOD_MAX; i++) { \
348 mod = (io)->mod[i]; \
351 ret |= rsnd_mod_call(i, io, fn, param); \
356 int rsnd_dai_connect(struct rsnd_mod *mod,
357 struct rsnd_dai_stream *io,
358 enum rsnd_mod_type type)
360 struct rsnd_priv *priv;
369 priv = rsnd_mod_to_priv(mod);
370 dev = rsnd_priv_to_dev(priv);
374 dev_dbg(dev, "%s[%d] is connected to io (%s)\n",
375 rsnd_mod_name(mod), rsnd_mod_id(mod),
376 rsnd_io_is_play(io) ? "Playback" : "Capture");
381 static void rsnd_dai_disconnect(struct rsnd_mod *mod,
382 struct rsnd_dai_stream *io,
383 enum rsnd_mod_type type)
385 io->mod[type] = NULL;
388 struct rsnd_dai *rsnd_rdai_get(struct rsnd_priv *priv, int id)
390 if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
393 return priv->rdai + id;
396 #define rsnd_dai_to_priv(dai) snd_soc_dai_get_drvdata(dai)
397 static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai)
399 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
401 return rsnd_rdai_get(priv, dai->id);
405 * rsnd_soc_dai functions
407 int rsnd_dai_pointer_offset(struct rsnd_dai_stream *io, int additional)
409 struct snd_pcm_substream *substream = io->substream;
410 struct snd_pcm_runtime *runtime = substream->runtime;
411 int pos = io->byte_pos + additional;
413 pos %= (runtime->periods * io->byte_per_period);
418 bool rsnd_dai_pointer_update(struct rsnd_dai_stream *io, int byte)
420 io->byte_pos += byte;
422 if (io->byte_pos >= io->next_period_byte) {
423 struct snd_pcm_substream *substream = io->substream;
424 struct snd_pcm_runtime *runtime = substream->runtime;
427 io->next_period_byte += io->byte_per_period;
429 if (io->period_pos >= runtime->periods) {
432 io->next_period_byte = io->byte_per_period;
441 void rsnd_dai_period_elapsed(struct rsnd_dai_stream *io)
443 struct snd_pcm_substream *substream = io->substream;
446 * this function should be called...
448 * - if rsnd_dai_pointer_update() returns true
449 * - without spin lock
452 snd_pcm_period_elapsed(substream);
455 static void rsnd_dai_stream_init(struct rsnd_dai_stream *io,
456 struct snd_pcm_substream *substream)
458 struct snd_pcm_runtime *runtime = substream->runtime;
460 io->substream = substream;
463 io->byte_per_period = runtime->period_size *
465 samples_to_bytes(runtime, 1);
466 io->next_period_byte = io->byte_per_period;
469 static void rsnd_dai_stream_quit(struct rsnd_dai_stream *io)
471 io->substream = NULL;
475 struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream)
477 struct snd_soc_pcm_runtime *rtd = substream->private_data;
483 struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai,
484 struct snd_pcm_substream *substream)
486 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
487 return &rdai->playback;
489 return &rdai->capture;
492 static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd,
493 struct snd_soc_dai *dai)
495 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
496 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
497 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
501 spin_lock_irqsave(&priv->lock, flags);
504 case SNDRV_PCM_TRIGGER_START:
505 rsnd_dai_stream_init(io, substream);
507 ret = rsnd_dai_call(init, io, priv);
509 goto dai_trigger_end;
511 ret = rsnd_dai_call(start, io, priv);
513 goto dai_trigger_end;
515 case SNDRV_PCM_TRIGGER_STOP:
516 ret = rsnd_dai_call(stop, io, priv);
518 ret |= rsnd_dai_call(quit, io, priv);
520 rsnd_dai_stream_quit(io);
527 spin_unlock_irqrestore(&priv->lock, flags);
532 static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
534 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
536 /* set master/slave audio interface */
537 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
538 case SND_SOC_DAIFMT_CBM_CFM:
539 rdai->clk_master = 0;
541 case SND_SOC_DAIFMT_CBS_CFS:
542 rdai->clk_master = 1; /* codec is slave, cpu is master */
549 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
550 case SND_SOC_DAIFMT_I2S:
552 rdai->data_alignment = 0;
553 rdai->frm_clk_inv = 0;
555 case SND_SOC_DAIFMT_LEFT_J:
557 rdai->data_alignment = 0;
558 rdai->frm_clk_inv = 1;
560 case SND_SOC_DAIFMT_RIGHT_J:
562 rdai->data_alignment = 1;
563 rdai->frm_clk_inv = 1;
567 /* set clock inversion */
568 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
569 case SND_SOC_DAIFMT_NB_IF:
570 rdai->bit_clk_inv = rdai->bit_clk_inv;
571 rdai->frm_clk_inv = !rdai->frm_clk_inv;
573 case SND_SOC_DAIFMT_IB_NF:
574 rdai->bit_clk_inv = !rdai->bit_clk_inv;
575 rdai->frm_clk_inv = rdai->frm_clk_inv;
577 case SND_SOC_DAIFMT_IB_IF:
578 rdai->bit_clk_inv = !rdai->bit_clk_inv;
579 rdai->frm_clk_inv = !rdai->frm_clk_inv;
581 case SND_SOC_DAIFMT_NB_NF:
589 static int rsnd_soc_set_dai_tdm_slot(struct snd_soc_dai *dai,
590 u32 tx_mask, u32 rx_mask,
591 int slots, int slot_width)
593 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
594 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
595 struct device *dev = rsnd_priv_to_dev(priv);
599 /* TDM Extend Mode */
600 rsnd_set_slot(rdai, slots, 1);
603 dev_err(dev, "unsupported TDM slots (%d)\n", slots);
610 static const struct snd_soc_dai_ops rsnd_soc_dai_ops = {
611 .trigger = rsnd_soc_dai_trigger,
612 .set_fmt = rsnd_soc_dai_set_fmt,
613 .set_tdm_slot = rsnd_soc_set_dai_tdm_slot,
616 void rsnd_parse_connect_common(struct rsnd_dai *rdai,
617 struct rsnd_mod* (*mod_get)(struct rsnd_priv *priv, int id),
618 struct device_node *node,
619 struct device_node *playback,
620 struct device_node *capture)
622 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
623 struct device_node *np;
624 struct rsnd_mod *mod;
631 for_each_child_of_node(node, np) {
632 mod = mod_get(priv, i);
634 rsnd_dai_connect(mod, &rdai->playback, mod->type);
636 rsnd_dai_connect(mod, &rdai->capture, mod->type);
643 static int rsnd_dai_probe(struct rsnd_priv *priv)
645 struct device_node *dai_node;
646 struct device_node *dai_np;
647 struct device_node *playback, *capture;
648 struct rsnd_dai_stream *io_playback;
649 struct rsnd_dai_stream *io_capture;
650 struct snd_soc_dai_driver *rdrv, *drv;
651 struct rsnd_dai *rdai;
652 struct device *dev = rsnd_priv_to_dev(priv);
656 dai_node = rsnd_dai_of_node(priv);
657 nr = of_get_child_count(dai_node);
660 goto rsnd_dai_probe_done;
663 rdrv = devm_kzalloc(dev, sizeof(*rdrv) * nr, GFP_KERNEL);
664 rdai = devm_kzalloc(dev, sizeof(*rdai) * nr, GFP_KERNEL);
665 if (!rdrv || !rdai) {
667 goto rsnd_dai_probe_done;
678 for_each_child_of_node(dai_node, dai_np) {
679 rdai = rsnd_rdai_get(priv, dai_i);
681 io_playback = &rdai->playback;
682 io_capture = &rdai->capture;
684 snprintf(rdai->name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", dai_i);
687 drv->name = rdai->name;
688 drv->ops = &rsnd_soc_dai_ops;
690 snprintf(rdai->playback.name, RSND_DAI_NAME_SIZE,
691 "DAI%d Playback", dai_i);
692 drv->playback.rates = RSND_RATES;
693 drv->playback.formats = RSND_FMTS;
694 drv->playback.channels_min = 2;
695 drv->playback.channels_max = 6;
696 drv->playback.stream_name = rdai->playback.name;
698 snprintf(rdai->capture.name, RSND_DAI_NAME_SIZE,
699 "DAI%d Capture", dai_i);
700 drv->capture.rates = RSND_RATES;
701 drv->capture.formats = RSND_FMTS;
702 drv->capture.channels_min = 2;
703 drv->capture.channels_max = 6;
704 drv->capture.stream_name = rdai->capture.name;
706 rdai->playback.rdai = rdai;
707 rdai->capture.rdai = rdai;
708 rsnd_set_slot(rdai, 2, 1); /* default */
710 for (io_i = 0;; io_i++) {
711 playback = of_parse_phandle(dai_np, "playback", io_i);
712 capture = of_parse_phandle(dai_np, "capture", io_i);
714 if (!playback && !capture)
717 rsnd_parse_connect_ssi(rdai, playback, capture);
718 rsnd_parse_connect_src(rdai, playback, capture);
719 rsnd_parse_connect_ctu(rdai, playback, capture);
720 rsnd_parse_connect_mix(rdai, playback, capture);
721 rsnd_parse_connect_dvc(rdai, playback, capture);
723 of_node_put(playback);
724 of_node_put(capture);
729 dev_dbg(dev, "%s (%s/%s)\n", rdai->name,
730 rsnd_io_to_mod_ssi(io_playback) ? "play" : " -- ",
731 rsnd_io_to_mod_ssi(io_capture) ? "capture" : " -- ");
737 of_node_put(dai_node);
745 static struct snd_pcm_hardware rsnd_pcm_hardware = {
746 .info = SNDRV_PCM_INFO_INTERLEAVED |
747 SNDRV_PCM_INFO_MMAP |
748 SNDRV_PCM_INFO_MMAP_VALID,
749 .buffer_bytes_max = 64 * 1024,
750 .period_bytes_min = 32,
751 .period_bytes_max = 8192,
757 static int rsnd_pcm_open(struct snd_pcm_substream *substream)
759 struct snd_pcm_runtime *runtime = substream->runtime;
762 snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware);
764 ret = snd_pcm_hw_constraint_integer(runtime,
765 SNDRV_PCM_HW_PARAM_PERIODS);
770 static int rsnd_hw_params(struct snd_pcm_substream *substream,
771 struct snd_pcm_hw_params *hw_params)
773 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
774 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
775 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
778 ret = rsnd_dai_call(hw_params, io, substream, hw_params);
782 return snd_pcm_lib_malloc_pages(substream,
783 params_buffer_bytes(hw_params));
786 static snd_pcm_uframes_t rsnd_pointer(struct snd_pcm_substream *substream)
788 struct snd_pcm_runtime *runtime = substream->runtime;
789 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
790 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
791 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
793 return bytes_to_frames(runtime, io->byte_pos);
796 static struct snd_pcm_ops rsnd_pcm_ops = {
797 .open = rsnd_pcm_open,
798 .ioctl = snd_pcm_lib_ioctl,
799 .hw_params = rsnd_hw_params,
800 .hw_free = snd_pcm_lib_free_pages,
801 .pointer = rsnd_pointer,
807 #define kcontrol_to_cfg(kctrl) ((struct rsnd_kctrl_cfg *)kctrl->private_value)
808 static int rsnd_kctrl_info(struct snd_kcontrol *kctrl,
809 struct snd_ctl_elem_info *uinfo)
811 struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
814 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
815 uinfo->count = cfg->size;
816 uinfo->value.enumerated.items = cfg->max;
817 if (uinfo->value.enumerated.item >= cfg->max)
818 uinfo->value.enumerated.item = cfg->max - 1;
819 strlcpy(uinfo->value.enumerated.name,
820 cfg->texts[uinfo->value.enumerated.item],
821 sizeof(uinfo->value.enumerated.name));
823 uinfo->count = cfg->size;
824 uinfo->value.integer.min = 0;
825 uinfo->value.integer.max = cfg->max;
826 uinfo->type = (cfg->max == 1) ?
827 SNDRV_CTL_ELEM_TYPE_BOOLEAN :
828 SNDRV_CTL_ELEM_TYPE_INTEGER;
834 static int rsnd_kctrl_get(struct snd_kcontrol *kctrl,
835 struct snd_ctl_elem_value *uc)
837 struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
840 for (i = 0; i < cfg->size; i++)
842 uc->value.enumerated.item[i] = cfg->val[i];
844 uc->value.integer.value[i] = cfg->val[i];
849 static int rsnd_kctrl_put(struct snd_kcontrol *kctrl,
850 struct snd_ctl_elem_value *uc)
852 struct rsnd_mod *mod = snd_kcontrol_chip(kctrl);
853 struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
856 for (i = 0; i < cfg->size; i++) {
858 change |= (uc->value.enumerated.item[i] != cfg->val[i]);
859 cfg->val[i] = uc->value.enumerated.item[i];
861 change |= (uc->value.integer.value[i] != cfg->val[i]);
862 cfg->val[i] = uc->value.integer.value[i];
867 cfg->update(cfg->io, mod);
872 static int __rsnd_kctrl_new(struct rsnd_mod *mod,
873 struct rsnd_dai_stream *io,
874 struct snd_soc_pcm_runtime *rtd,
875 const unsigned char *name,
876 struct rsnd_kctrl_cfg *cfg,
877 void (*update)(struct rsnd_dai_stream *io,
878 struct rsnd_mod *mod))
880 struct snd_card *card = rtd->card->snd_card;
881 struct snd_kcontrol *kctrl;
882 struct snd_kcontrol_new knew = {
883 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
885 .info = rsnd_kctrl_info,
887 .get = rsnd_kctrl_get,
888 .put = rsnd_kctrl_put,
889 .private_value = (unsigned long)cfg,
893 kctrl = snd_ctl_new1(&knew, mod);
897 ret = snd_ctl_add(card, kctrl);
899 snd_ctl_free_one(kctrl);
903 cfg->update = update;
911 void _rsnd_kctrl_remove(struct rsnd_kctrl_cfg *cfg)
913 snd_ctl_remove(cfg->card, cfg->kctrl);
916 int rsnd_kctrl_new_m(struct rsnd_mod *mod,
917 struct rsnd_dai_stream *io,
918 struct snd_soc_pcm_runtime *rtd,
919 const unsigned char *name,
920 void (*update)(struct rsnd_dai_stream *io,
921 struct rsnd_mod *mod),
922 struct rsnd_kctrl_cfg_m *_cfg,
926 if (ch_size > RSND_DVC_CHANNELS)
930 _cfg->cfg.size = ch_size;
931 _cfg->cfg.val = _cfg->val;
932 return __rsnd_kctrl_new(mod, io, rtd, name, &_cfg->cfg, update);
935 int rsnd_kctrl_new_s(struct rsnd_mod *mod,
936 struct rsnd_dai_stream *io,
937 struct snd_soc_pcm_runtime *rtd,
938 const unsigned char *name,
939 void (*update)(struct rsnd_dai_stream *io,
940 struct rsnd_mod *mod),
941 struct rsnd_kctrl_cfg_s *_cfg,
946 _cfg->cfg.val = &_cfg->val;
947 return __rsnd_kctrl_new(mod, io, rtd, name, &_cfg->cfg, update);
950 int rsnd_kctrl_new_e(struct rsnd_mod *mod,
951 struct rsnd_dai_stream *io,
952 struct snd_soc_pcm_runtime *rtd,
953 const unsigned char *name,
954 struct rsnd_kctrl_cfg_s *_cfg,
955 void (*update)(struct rsnd_dai_stream *io,
956 struct rsnd_mod *mod),
957 const char * const *texts,
962 _cfg->cfg.val = &_cfg->val;
963 _cfg->cfg.texts = texts;
964 return __rsnd_kctrl_new(mod, io, rtd, name, &_cfg->cfg, update);
971 #define PREALLOC_BUFFER (32 * 1024)
972 #define PREALLOC_BUFFER_MAX (32 * 1024)
974 static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd)
976 struct snd_soc_dai *dai = rtd->cpu_dai;
977 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
980 ret = rsnd_dai_call(pcm_new, &rdai->playback, rtd);
984 ret = rsnd_dai_call(pcm_new, &rdai->capture, rtd);
988 return snd_pcm_lib_preallocate_pages_for_all(
991 rtd->card->snd_card->dev,
992 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
995 static struct snd_soc_platform_driver rsnd_soc_platform = {
996 .ops = &rsnd_pcm_ops,
997 .pcm_new = rsnd_pcm_new,
1000 static const struct snd_soc_component_driver rsnd_soc_component = {
1004 static int rsnd_rdai_continuance_probe(struct rsnd_priv *priv,
1005 struct rsnd_dai_stream *io)
1009 ret = rsnd_dai_call(probe, io, priv);
1010 if (ret == -EAGAIN) {
1011 struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io);
1015 * Fallback to PIO mode
1019 * call "remove" for SSI/SRC/DVC
1020 * SSI will be switch to PIO mode if it was DMA mode
1023 * rsnd_ssi_fallback()
1025 rsnd_dai_call(remove, io, priv);
1028 * remove all mod from io
1029 * and, re connect ssi
1031 for (i = 0; i < RSND_MOD_MAX; i++)
1032 rsnd_dai_disconnect((io)->mod[i], io, i);
1033 rsnd_dai_connect(ssi_mod, io, RSND_MOD_SSI);
1038 rsnd_dai_call(fallback, io, priv);
1042 * DAI has SSI which is PIO mode only now.
1044 ret = rsnd_dai_call(probe, io, priv);
1053 static int rsnd_probe(struct platform_device *pdev)
1055 struct rsnd_priv *priv;
1056 struct device *dev = &pdev->dev;
1057 struct rsnd_dai *rdai;
1058 const struct of_device_id *of_id = of_match_device(rsnd_of_match, dev);
1059 int (*probe_func[])(struct rsnd_priv *priv) = {
1077 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1079 dev_err(dev, "priv allocate failed\n");
1084 priv->flags = (unsigned long)of_id->data;
1085 spin_lock_init(&priv->lock);
1090 for (i = 0; i < ARRAY_SIZE(probe_func); i++) {
1091 ret = probe_func[i](priv);
1096 for_each_rsnd_dai(rdai, priv, i) {
1097 ret = rsnd_rdai_continuance_probe(priv, &rdai->playback);
1099 goto exit_snd_probe;
1101 ret = rsnd_rdai_continuance_probe(priv, &rdai->capture);
1103 goto exit_snd_probe;
1106 dev_set_drvdata(dev, priv);
1111 ret = snd_soc_register_platform(dev, &rsnd_soc_platform);
1113 dev_err(dev, "cannot snd soc register\n");
1117 ret = snd_soc_register_component(dev, &rsnd_soc_component,
1118 priv->daidrv, rsnd_rdai_nr(priv));
1120 dev_err(dev, "cannot snd dai register\n");
1124 pm_runtime_enable(dev);
1126 dev_info(dev, "probed\n");
1130 snd_soc_unregister_platform(dev);
1132 for_each_rsnd_dai(rdai, priv, i) {
1133 rsnd_dai_call(remove, &rdai->playback, priv);
1134 rsnd_dai_call(remove, &rdai->capture, priv);
1140 static int rsnd_remove(struct platform_device *pdev)
1142 struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
1143 struct rsnd_dai *rdai;
1144 void (*remove_func[])(struct rsnd_priv *priv) = {
1156 pm_runtime_disable(&pdev->dev);
1158 for_each_rsnd_dai(rdai, priv, i) {
1159 ret |= rsnd_dai_call(remove, &rdai->playback, priv);
1160 ret |= rsnd_dai_call(remove, &rdai->capture, priv);
1163 for (i = 0; i < ARRAY_SIZE(remove_func); i++)
1164 remove_func[i](priv);
1166 snd_soc_unregister_component(&pdev->dev);
1167 snd_soc_unregister_platform(&pdev->dev);
1172 static struct platform_driver rsnd_driver = {
1174 .name = "rcar_sound",
1175 .of_match_table = rsnd_of_match,
1177 .probe = rsnd_probe,
1178 .remove = rsnd_remove,
1180 module_platform_driver(rsnd_driver);
1182 MODULE_LICENSE("GPL");
1183 MODULE_DESCRIPTION("Renesas R-Car audio driver");
1184 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
1185 MODULE_ALIAS("platform:rcar-pcm-audio");