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>
97 #include <linux/shdma-base.h>
100 #define RSND_RATES SNDRV_PCM_RATE_8000_96000
101 #define RSND_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
103 static struct rsnd_of_data rsnd_of_data_gen1 = {
107 static struct rsnd_of_data rsnd_of_data_gen2 = {
111 static struct of_device_id rsnd_of_match[] = {
112 { .compatible = "renesas,rcar_sound-gen1", .data = &rsnd_of_data_gen1 },
113 { .compatible = "renesas,rcar_sound-gen2", .data = &rsnd_of_data_gen2 },
116 MODULE_DEVICE_TABLE(of, rsnd_of_match);
119 * rsnd_platform functions
121 #define rsnd_platform_call(priv, dai, func, param...) \
122 (!(priv->info->func) ? 0 : \
123 priv->info->func(param))
125 #define rsnd_is_enable_path(io, name) \
126 ((io)->info ? (io)->info->name : NULL)
127 #define rsnd_info_id(priv, io, name) \
128 ((io)->info->name - priv->info->name##_info)
133 char *rsnd_mod_name(struct rsnd_mod *mod)
135 if (!mod || !mod->ops)
138 return mod->ops->name;
141 char *rsnd_mod_dma_name(struct rsnd_mod *mod)
143 if (!mod || !mod->ops)
146 if (!mod->ops->dma_name)
147 return mod->ops->name;
149 return mod->ops->dma_name(mod);
152 void rsnd_mod_init(struct rsnd_priv *priv,
153 struct rsnd_mod *mod,
154 struct rsnd_mod_ops *ops,
155 enum rsnd_mod_type type,
167 void rsnd_dma_stop(struct rsnd_dma *dma)
169 dmaengine_terminate_all(dma->chan);
172 static void rsnd_dma_complete(void *data)
174 struct rsnd_dma *dma = (struct rsnd_dma *)data;
175 struct rsnd_mod *mod = rsnd_dma_to_mod(dma);
176 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
179 * Renesas sound Gen1 needs 1 DMAC,
181 * In Gen2 case, it are Audio-DMAC, and Audio-DMAC-peri-peri.
182 * But, Audio-DMAC-peri-peri doesn't have interrupt,
183 * and this driver is assuming that here.
185 * If Audio-DMAC-peri-peri has interrpt,
186 * rsnd_dai_pointer_update() will be called twice,
187 * ant it will breaks io->byte_pos
190 rsnd_dai_pointer_update(io, io->byte_per_period);
193 void rsnd_dma_start(struct rsnd_dma *dma)
195 struct rsnd_mod *mod = rsnd_dma_to_mod(dma);
196 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
197 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
198 struct snd_pcm_substream *substream = io->substream;
199 struct device *dev = rsnd_priv_to_dev(priv);
200 struct dma_async_tx_descriptor *desc;
202 desc = dmaengine_prep_dma_cyclic(dma->chan,
203 (dma->addr) ? dma->addr :
204 substream->runtime->dma_addr,
205 snd_pcm_lib_buffer_bytes(substream),
206 snd_pcm_lib_period_bytes(substream),
208 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
211 dev_err(dev, "dmaengine_prep_slave_sg() fail\n");
215 desc->callback = rsnd_dma_complete;
216 desc->callback_param = dma;
218 if (dmaengine_submit(desc) < 0) {
219 dev_err(dev, "dmaengine_submit() fail\n");
223 dma_async_issue_pending(dma->chan);
226 int rsnd_dma_available(struct rsnd_dma *dma)
231 #define DMA_NAME_SIZE 16
232 #define MOD_MAX 4 /* MEM/SSI/SRC/DVC */
233 static int _rsnd_dma_of_name(char *dma_name, struct rsnd_mod *mod)
236 return snprintf(dma_name, DMA_NAME_SIZE / 2, "%s%d",
237 rsnd_mod_dma_name(mod), rsnd_mod_id(mod));
239 return snprintf(dma_name, DMA_NAME_SIZE / 2, "mem");
243 static void rsnd_dma_of_name(struct rsnd_mod *mod_from,
244 struct rsnd_mod *mod_to,
249 index = _rsnd_dma_of_name(dma_name + index, mod_from);
250 *(dma_name + index++) = '_';
251 index = _rsnd_dma_of_name(dma_name + index, mod_to);
254 static void rsnd_dma_of_path(struct rsnd_dma *dma,
256 struct rsnd_mod **mod_from,
257 struct rsnd_mod **mod_to)
259 struct rsnd_mod *this = rsnd_dma_to_mod(dma);
260 struct rsnd_dai_stream *io = rsnd_mod_to_io(this);
261 struct rsnd_mod *ssi = rsnd_io_to_mod_ssi(io);
262 struct rsnd_mod *src = rsnd_io_to_mod_src(io);
263 struct rsnd_mod *dvc = rsnd_io_to_mod_dvc(io);
264 struct rsnd_mod *mod[MOD_MAX];
268 for (i = 0; i < MOD_MAX; i++)
278 * mem -> SRC -> DVC -> SSI
280 mod[0] = NULL; /* for "mem" */
282 for (i = 1; i < MOD_MAX; i++) {
289 if ((!is_play) && (this == src))
292 mod[i] = (is_play) ? src : dvc;
294 mod[i] = (is_play) ? dvc : src;
307 *mod_from = mod[index - 1];
308 *mod_to = mod[index];
310 *mod_from = mod[index];
311 *mod_to = mod[index - 1];
315 int rsnd_dma_init(struct rsnd_priv *priv, struct rsnd_dma *dma,
318 struct device *dev = rsnd_priv_to_dev(priv);
319 struct dma_slave_config cfg;
320 struct rsnd_mod *mod_from;
321 struct rsnd_mod *mod_to;
322 char dma_name[DMA_NAME_SIZE];
327 dev_err(dev, "it already has dma channel\n");
332 dma_cap_set(DMA_SLAVE, mask);
334 rsnd_dma_of_path(dma, is_play, &mod_from, &mod_to);
335 rsnd_dma_of_name(mod_from, mod_to, dma_name);
338 cfg.direction = is_play ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM;
339 cfg.src_addr = rsnd_gen_dma_addr(priv, mod_from, is_play, 1);
340 cfg.dst_addr = rsnd_gen_dma_addr(priv, mod_to, is_play, 0);
341 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
342 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
344 dev_dbg(dev, "dma : %s %pad -> %pad\n",
345 dma_name, &cfg.src_addr, &cfg.dst_addr);
347 dma->chan = dma_request_slave_channel_compat(mask, shdma_chan_filter,
351 dev_err(dev, "can't get dma channel\n");
352 goto rsnd_dma_channel_err;
355 ret = dmaengine_slave_config(dma->chan, &cfg);
357 goto rsnd_dma_init_err;
359 dma->addr = is_play ? cfg.src_addr : cfg.dst_addr;
360 dma->dir = is_play ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM;
365 rsnd_dma_quit(priv, dma);
366 rsnd_dma_channel_err:
369 * DMA failed. try to PIO mode
371 * rsnd_ssi_dma_remove()
372 * rsnd_rdai_continuance_probe()
377 void rsnd_dma_quit(struct rsnd_priv *priv,
378 struct rsnd_dma *dma)
381 dma_release_channel(dma->chan);
389 u32 rsnd_get_adinr(struct rsnd_mod *mod)
391 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
392 struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
393 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
394 struct device *dev = rsnd_priv_to_dev(priv);
395 u32 adinr = runtime->channels;
397 switch (runtime->sample_bits) {
405 dev_warn(dev, "not supported sample bits\n");
415 #define __rsnd_mod_call(mod, func, rdai...) \
417 struct rsnd_priv *priv = rsnd_mod_to_priv(mod); \
418 struct device *dev = rsnd_priv_to_dev(priv); \
419 dev_dbg(dev, "%s[%d] %s\n", \
420 rsnd_mod_name(mod), rsnd_mod_id(mod), #func); \
421 (mod)->ops->func(mod, rdai); \
424 #define rsnd_mod_call(mod, func, rdai...) \
425 (!(mod) ? -ENODEV : \
426 !((mod)->ops->func) ? 0 : \
427 __rsnd_mod_call(mod, func, rdai))
429 #define rsnd_dai_call(fn, io, rdai...) \
431 struct rsnd_mod *mod; \
433 for (i = 0; i < RSND_MOD_MAX; i++) { \
434 mod = (io)->mod[i]; \
437 ret = rsnd_mod_call(mod, fn, rdai); \
444 static int rsnd_dai_connect(struct rsnd_mod *mod,
445 struct rsnd_dai_stream *io)
450 if (io->mod[mod->type]) {
451 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
452 struct device *dev = rsnd_priv_to_dev(priv);
454 dev_err(dev, "%s%d is not empty\n",
460 io->mod[mod->type] = mod;
466 static void rsnd_dai_disconnect(struct rsnd_mod *mod,
467 struct rsnd_dai_stream *io)
470 io->mod[mod->type] = NULL;
473 int rsnd_dai_id(struct rsnd_priv *priv, struct rsnd_dai *rdai)
475 int id = rdai - priv->rdai;
477 if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
483 struct rsnd_dai *rsnd_dai_get(struct rsnd_priv *priv, int id)
485 if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
488 return priv->rdai + id;
491 static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai)
493 struct rsnd_priv *priv = snd_soc_dai_get_drvdata(dai);
495 return rsnd_dai_get(priv, dai->id);
498 int rsnd_dai_is_play(struct rsnd_dai *rdai, struct rsnd_dai_stream *io)
500 return &rdai->playback == io;
504 * rsnd_soc_dai functions
506 int rsnd_dai_pointer_offset(struct rsnd_dai_stream *io, int additional)
508 struct snd_pcm_substream *substream = io->substream;
509 struct snd_pcm_runtime *runtime = substream->runtime;
510 int pos = io->byte_pos + additional;
512 pos %= (runtime->periods * io->byte_per_period);
517 void rsnd_dai_pointer_update(struct rsnd_dai_stream *io, int byte)
519 io->byte_pos += byte;
521 if (io->byte_pos >= io->next_period_byte) {
522 struct snd_pcm_substream *substream = io->substream;
523 struct snd_pcm_runtime *runtime = substream->runtime;
526 io->next_period_byte += io->byte_per_period;
528 if (io->period_pos >= runtime->periods) {
531 io->next_period_byte = io->byte_per_period;
534 snd_pcm_period_elapsed(substream);
538 static int rsnd_dai_stream_init(struct rsnd_dai_stream *io,
539 struct snd_pcm_substream *substream)
541 struct snd_pcm_runtime *runtime = substream->runtime;
543 io->substream = substream;
546 io->byte_per_period = runtime->period_size *
548 samples_to_bytes(runtime, 1);
549 io->next_period_byte = io->byte_per_period;
555 struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream)
557 struct snd_soc_pcm_runtime *rtd = substream->private_data;
563 struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai,
564 struct snd_pcm_substream *substream)
566 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
567 return &rdai->playback;
569 return &rdai->capture;
572 static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd,
573 struct snd_soc_dai *dai)
575 struct rsnd_priv *priv = snd_soc_dai_get_drvdata(dai);
576 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
577 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
578 int ssi_id = rsnd_mod_id(rsnd_io_to_mod_ssi(io));
582 rsnd_lock(priv, flags);
585 case SNDRV_PCM_TRIGGER_START:
586 ret = rsnd_dai_stream_init(io, substream);
588 goto dai_trigger_end;
590 ret = rsnd_platform_call(priv, dai, start, ssi_id);
592 goto dai_trigger_end;
594 ret = rsnd_dai_call(init, io, rdai);
596 goto dai_trigger_end;
598 ret = rsnd_dai_call(start, io, rdai);
600 goto dai_trigger_end;
602 case SNDRV_PCM_TRIGGER_STOP:
603 ret = rsnd_dai_call(stop, io, rdai);
605 goto dai_trigger_end;
607 ret = rsnd_dai_call(quit, io, rdai);
609 goto dai_trigger_end;
611 ret = rsnd_platform_call(priv, dai, stop, ssi_id);
613 goto dai_trigger_end;
620 rsnd_unlock(priv, flags);
625 static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
627 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
629 /* set master/slave audio interface */
630 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
631 case SND_SOC_DAIFMT_CBM_CFM:
632 rdai->clk_master = 0;
634 case SND_SOC_DAIFMT_CBS_CFS:
635 rdai->clk_master = 1; /* codec is slave, cpu is master */
642 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
643 case SND_SOC_DAIFMT_I2S:
645 rdai->data_alignment = 0;
646 rdai->frm_clk_inv = 0;
648 case SND_SOC_DAIFMT_LEFT_J:
650 rdai->data_alignment = 0;
651 rdai->frm_clk_inv = 1;
653 case SND_SOC_DAIFMT_RIGHT_J:
655 rdai->data_alignment = 1;
656 rdai->frm_clk_inv = 1;
660 /* set clock inversion */
661 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
662 case SND_SOC_DAIFMT_NB_IF:
663 rdai->bit_clk_inv = rdai->bit_clk_inv;
664 rdai->frm_clk_inv = !rdai->frm_clk_inv;
666 case SND_SOC_DAIFMT_IB_NF:
667 rdai->bit_clk_inv = !rdai->bit_clk_inv;
668 rdai->frm_clk_inv = rdai->frm_clk_inv;
670 case SND_SOC_DAIFMT_IB_IF:
671 rdai->bit_clk_inv = !rdai->bit_clk_inv;
672 rdai->frm_clk_inv = !rdai->frm_clk_inv;
674 case SND_SOC_DAIFMT_NB_NF:
682 static const struct snd_soc_dai_ops rsnd_soc_dai_ops = {
683 .trigger = rsnd_soc_dai_trigger,
684 .set_fmt = rsnd_soc_dai_set_fmt,
687 #define rsnd_path_parse(priv, io, type) \
689 struct rsnd_mod *mod; \
693 if (rsnd_is_enable_path(io, type)) { \
694 id = rsnd_info_id(priv, io, type); \
696 mod = rsnd_##type##_mod_get(priv, id); \
697 ret = rsnd_dai_connect(mod, io); \
703 #define rsnd_path_break(priv, io, type) \
705 struct rsnd_mod *mod; \
708 if (rsnd_is_enable_path(io, type)) { \
709 id = rsnd_info_id(priv, io, type); \
711 mod = rsnd_##type##_mod_get(priv, id); \
712 rsnd_dai_disconnect(mod, io); \
717 static int rsnd_path_init(struct rsnd_priv *priv,
718 struct rsnd_dai *rdai,
719 struct rsnd_dai_stream *io)
724 * Gen1 is created by SRU/SSI, and this SRU is base module of
725 * Gen2's SCU/SSIU/SSI. (Gen2 SCU/SSIU came from SRU)
728 * Gen1 SRU = Gen2 SCU + SSIU + etc
730 * Gen2 SCU path is very flexible, but, Gen1 SRU (SCU parts) is
735 ret = rsnd_path_parse(priv, io, src);
740 ret = rsnd_path_parse(priv, io, ssi);
745 ret = rsnd_path_parse(priv, io, dvc);
752 static void rsnd_of_parse_dai(struct platform_device *pdev,
753 const struct rsnd_of_data *of_data,
754 struct rsnd_priv *priv)
756 struct device_node *dai_node, *dai_np;
757 struct device_node *ssi_node, *ssi_np;
758 struct device_node *src_node, *src_np;
759 struct device_node *dvc_node, *dvc_np;
760 struct device_node *playback, *capture;
761 struct rsnd_dai_platform_info *dai_info;
762 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
763 struct device *dev = &pdev->dev;
765 int dai_i, ssi_i, src_i, dvc_i;
770 dai_node = of_get_child_by_name(dev->of_node, "rcar_sound,dai");
774 nr = of_get_child_count(dai_node);
778 dai_info = devm_kzalloc(dev,
779 sizeof(struct rsnd_dai_platform_info) * nr,
782 dev_err(dev, "dai info allocation error\n");
786 info->dai_info_nr = nr;
787 info->dai_info = dai_info;
789 ssi_node = of_get_child_by_name(dev->of_node, "rcar_sound,ssi");
790 src_node = of_get_child_by_name(dev->of_node, "rcar_sound,src");
791 dvc_node = of_get_child_by_name(dev->of_node, "rcar_sound,dvc");
793 #define mod_parse(name) \
795 struct rsnd_##name##_platform_info *name##_info; \
798 for_each_child_of_node(name##_node, name##_np) { \
799 name##_info = info->name##_info + name##_i; \
801 if (name##_np == playback) \
802 dai_info->playback.name = name##_info; \
803 if (name##_np == capture) \
804 dai_info->capture.name = name##_info; \
814 for_each_child_of_node(dai_node, dai_np) {
815 dai_info = info->dai_info + dai_i;
819 playback = of_parse_phandle(dai_np, "playback", i);
820 capture = of_parse_phandle(dai_np, "capture", i);
822 if (!playback && !capture)
829 of_node_put(playback);
830 of_node_put(capture);
837 static int rsnd_dai_probe(struct platform_device *pdev,
838 const struct rsnd_of_data *of_data,
839 struct rsnd_priv *priv)
841 struct snd_soc_dai_driver *drv;
842 struct rcar_snd_info *info = rsnd_priv_to_info(priv);
843 struct rsnd_dai *rdai;
844 struct rsnd_ssi_platform_info *pmod, *cmod;
845 struct device *dev = rsnd_priv_to_dev(priv);
849 rsnd_of_parse_dai(pdev, of_data, priv);
851 dai_nr = info->dai_info_nr;
853 dev_err(dev, "no dai\n");
857 drv = devm_kzalloc(dev, sizeof(*drv) * dai_nr, GFP_KERNEL);
858 rdai = devm_kzalloc(dev, sizeof(*rdai) * dai_nr, GFP_KERNEL);
860 dev_err(dev, "dai allocate failed\n");
864 priv->rdai_nr = dai_nr;
868 for (i = 0; i < dai_nr; i++) {
869 rdai[i].info = &info->dai_info[i];
871 pmod = rdai[i].info->playback.ssi;
872 cmod = rdai[i].info->capture.ssi;
877 snprintf(rdai[i].name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", i);
880 * init snd_soc_dai_driver
882 drv[i].name = rdai[i].name;
883 drv[i].ops = &rsnd_soc_dai_ops;
885 drv[i].playback.rates = RSND_RATES;
886 drv[i].playback.formats = RSND_FMTS;
887 drv[i].playback.channels_min = 2;
888 drv[i].playback.channels_max = 2;
890 rdai[i].playback.info = &info->dai_info[i].playback;
891 rsnd_path_init(priv, &rdai[i], &rdai[i].playback);
894 drv[i].capture.rates = RSND_RATES;
895 drv[i].capture.formats = RSND_FMTS;
896 drv[i].capture.channels_min = 2;
897 drv[i].capture.channels_max = 2;
899 rdai[i].capture.info = &info->dai_info[i].capture;
900 rsnd_path_init(priv, &rdai[i], &rdai[i].capture);
903 dev_dbg(dev, "%s (%s/%s)\n", rdai[i].name,
904 pmod ? "play" : " -- ",
905 cmod ? "capture" : " -- ");
914 static struct snd_pcm_hardware rsnd_pcm_hardware = {
915 .info = SNDRV_PCM_INFO_INTERLEAVED |
916 SNDRV_PCM_INFO_MMAP |
917 SNDRV_PCM_INFO_MMAP_VALID,
918 .buffer_bytes_max = 64 * 1024,
919 .period_bytes_min = 32,
920 .period_bytes_max = 8192,
926 static int rsnd_pcm_open(struct snd_pcm_substream *substream)
928 struct snd_pcm_runtime *runtime = substream->runtime;
931 snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware);
933 ret = snd_pcm_hw_constraint_integer(runtime,
934 SNDRV_PCM_HW_PARAM_PERIODS);
939 static int rsnd_hw_params(struct snd_pcm_substream *substream,
940 struct snd_pcm_hw_params *hw_params)
942 return snd_pcm_lib_malloc_pages(substream,
943 params_buffer_bytes(hw_params));
946 static snd_pcm_uframes_t rsnd_pointer(struct snd_pcm_substream *substream)
948 struct snd_pcm_runtime *runtime = substream->runtime;
949 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
950 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
951 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
953 return bytes_to_frames(runtime, io->byte_pos);
956 static struct snd_pcm_ops rsnd_pcm_ops = {
957 .open = rsnd_pcm_open,
958 .ioctl = snd_pcm_lib_ioctl,
959 .hw_params = rsnd_hw_params,
960 .hw_free = snd_pcm_lib_free_pages,
961 .pointer = rsnd_pointer,
968 #define PREALLOC_BUFFER (32 * 1024)
969 #define PREALLOC_BUFFER_MAX (32 * 1024)
971 static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd)
973 struct snd_soc_dai *dai = rtd->cpu_dai;
974 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
977 ret = rsnd_dai_call(pcm_new, &rdai->playback, rdai, rtd);
981 ret = rsnd_dai_call(pcm_new, &rdai->capture, rdai, rtd);
985 return snd_pcm_lib_preallocate_pages_for_all(
988 rtd->card->snd_card->dev,
989 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
992 static void rsnd_pcm_free(struct snd_pcm *pcm)
994 snd_pcm_lib_preallocate_free_for_all(pcm);
997 static struct snd_soc_platform_driver rsnd_soc_platform = {
998 .ops = &rsnd_pcm_ops,
999 .pcm_new = rsnd_pcm_new,
1000 .pcm_free = rsnd_pcm_free,
1003 static const struct snd_soc_component_driver rsnd_soc_component = {
1007 static int rsnd_rdai_continuance_probe(struct rsnd_priv *priv,
1008 struct rsnd_dai *rdai,
1011 struct rsnd_dai_stream *io = is_play ? &rdai->playback : &rdai->capture;
1014 ret = rsnd_dai_call(probe, io, rdai);
1015 if (ret == -EAGAIN) {
1017 * Fallback to PIO mode
1021 * call "remove" for SSI/SRC/DVC
1022 * SSI will be switch to PIO mode if it was DMA mode
1025 * rsnd_ssi_dma_remove()
1027 rsnd_dai_call(remove, io, rdai);
1030 * remove SRC/DVC from DAI,
1032 rsnd_path_break(priv, io, src);
1033 rsnd_path_break(priv, io, dvc);
1037 * DAI has SSI which is PIO mode only now.
1039 ret = rsnd_dai_call(probe, io, rdai);
1048 static int rsnd_probe(struct platform_device *pdev)
1050 struct rcar_snd_info *info;
1051 struct rsnd_priv *priv;
1052 struct device *dev = &pdev->dev;
1053 struct rsnd_dai *rdai;
1054 const struct of_device_id *of_id = of_match_device(rsnd_of_match, dev);
1055 const struct rsnd_of_data *of_data;
1056 int (*probe_func[])(struct platform_device *pdev,
1057 const struct rsnd_of_data *of_data,
1058 struct rsnd_priv *priv) = {
1071 info = devm_kzalloc(&pdev->dev,
1072 sizeof(struct rcar_snd_info), GFP_KERNEL);
1073 of_data = of_id->data;
1075 info = pdev->dev.platform_data;
1079 dev_err(dev, "driver needs R-Car sound information\n");
1086 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1088 dev_err(dev, "priv allocate failed\n");
1094 spin_lock_init(&priv->lock);
1099 for (i = 0; i < ARRAY_SIZE(probe_func); i++) {
1100 ret = probe_func[i](pdev, of_data, priv);
1105 for_each_rsnd_dai(rdai, priv, i) {
1106 ret = rsnd_rdai_continuance_probe(priv, rdai, 1);
1108 goto exit_snd_probe;
1110 ret = rsnd_rdai_continuance_probe(priv, rdai, 0);
1112 goto exit_snd_probe;
1118 ret = snd_soc_register_platform(dev, &rsnd_soc_platform);
1120 dev_err(dev, "cannot snd soc register\n");
1124 ret = snd_soc_register_component(dev, &rsnd_soc_component,
1125 priv->daidrv, rsnd_rdai_nr(priv));
1127 dev_err(dev, "cannot snd dai register\n");
1131 dev_set_drvdata(dev, priv);
1133 pm_runtime_enable(dev);
1135 dev_info(dev, "probed\n");
1139 snd_soc_unregister_platform(dev);
1141 for_each_rsnd_dai(rdai, priv, i) {
1142 rsnd_dai_call(remove, &rdai->playback, rdai);
1143 rsnd_dai_call(remove, &rdai->capture, rdai);
1149 static int rsnd_remove(struct platform_device *pdev)
1151 struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
1152 struct rsnd_dai *rdai;
1155 pm_runtime_disable(&pdev->dev);
1157 for_each_rsnd_dai(rdai, priv, i) {
1158 ret |= rsnd_dai_call(remove, &rdai->playback, rdai);
1159 ret |= rsnd_dai_call(remove, &rdai->capture, rdai);
1165 static struct platform_driver rsnd_driver = {
1167 .name = "rcar_sound",
1168 .of_match_table = rsnd_of_match,
1170 .probe = rsnd_probe,
1171 .remove = rsnd_remove,
1173 module_platform_driver(rsnd_driver);
1175 MODULE_LICENSE("GPL");
1176 MODULE_DESCRIPTION("Renesas R-Car audio driver");
1177 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
1178 MODULE_ALIAS("platform:rcar-pcm-audio");