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 int rsnd_runtime_channel_original(struct rsnd_dai_stream *io)
208 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
210 return runtime->channels;
213 int rsnd_runtime_channel_after_ctu(struct rsnd_dai_stream *io)
215 int chan = rsnd_runtime_channel_original(io);
216 struct rsnd_mod *ctu_mod = rsnd_io_to_mod_ctu(io);
219 u32 converted_chan = rsnd_ctu_converted_channel(ctu_mod);
222 return converted_chan;
228 int rsnd_runtime_channel_for_ssi(struct rsnd_dai_stream *io)
230 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
231 int chan = rsnd_io_is_play(io) ?
232 rsnd_runtime_channel_after_ctu(io) :
233 rsnd_runtime_channel_original(io);
236 if (rsnd_runtime_is_ssi_multi(io))
237 chan /= rsnd_rdai_ssi_lane_get(rdai);
239 /* TDM Extend Mode needs 8ch */
246 int rsnd_runtime_is_ssi_multi(struct rsnd_dai_stream *io)
248 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
249 int lane = rsnd_rdai_ssi_lane_get(rdai);
250 int chan = rsnd_io_is_play(io) ?
251 rsnd_runtime_channel_after_ctu(io) :
252 rsnd_runtime_channel_original(io);
254 return (chan > 2) && (lane > 1);
257 int rsnd_runtime_is_ssi_tdm(struct rsnd_dai_stream *io)
259 return rsnd_runtime_channel_for_ssi(io) >= 6;
265 u32 rsnd_get_adinr_bit(struct rsnd_mod *mod, struct rsnd_dai_stream *io)
267 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
268 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
269 struct device *dev = rsnd_priv_to_dev(priv);
271 switch (runtime->sample_bits) {
278 dev_warn(dev, "not supported sample bits\n");
286 u32 rsnd_get_dalign(struct rsnd_mod *mod, struct rsnd_dai_stream *io)
288 struct rsnd_mod *ssiu = rsnd_io_to_mod_ssiu(io);
289 struct rsnd_mod *target;
290 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
291 u32 val = 0x76543210;
295 * *Hardware* L/R and *Software* L/R are inverted.
296 * We need to care about inversion timing to control
297 * Playback/Capture correctly.
298 * The point is [DVC] needs *Hardware* L/R, [MEM] needs *Software* L/R
300 * sL/R : software L/R
301 * hL/R : hardware L/R
302 * (*) : conversion timing
305 * sL/R (*) hL/R hL/R hL/R hL/R hL/R
306 * [MEM] -> [SRC] -> [DVC] -> [CMD] -> [SSIU] -> [SSI] -> codec
309 * hL/R hL/R hL/R hL/R hL/R (*) sL/R
310 * codec -> [SSI] -> [SSIU] -> [SRC] -> [DVC] -> [CMD] -> [MEM]
312 if (rsnd_io_is_play(io)) {
313 struct rsnd_mod *src = rsnd_io_to_mod_src(io);
315 target = src ? src : ssiu;
317 struct rsnd_mod *cmd = rsnd_io_to_mod_cmd(io);
319 target = cmd ? cmd : ssiu;
322 mask <<= runtime->channels * 4;
325 switch (runtime->sample_bits) {
327 val |= 0x67452301 & ~mask;
330 val |= 0x76543210 & ~mask;
335 * exchange channeles on SRC if possible,
336 * otherwise, R/L volume settings on DVC
337 * changes inverted channels
345 u32 rsnd_get_busif_shift(struct rsnd_dai_stream *io, struct rsnd_mod *mod)
347 enum rsnd_mod_type playback_mods[] = {
352 enum rsnd_mod_type capture_mods[] = {
357 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
358 struct rsnd_mod *tmod = NULL;
359 enum rsnd_mod_type *mods =
360 rsnd_io_is_play(io) ?
361 playback_mods : capture_mods;
365 * This is needed for 24bit data
366 * We need to shift 8bit
368 * Linux 24bit data is located as 0x00******
369 * HW 24bit data is located as 0x******00
372 switch (runtime->sample_bits) {
379 for (i = 0; i < ARRAY_SIZE(playback_mods); i++) {
380 tmod = rsnd_io_to_mod(io, mods[i]);
388 if (rsnd_io_is_play(io))
389 return (0 << 20) | /* shift to Left */
390 (8 << 16); /* 8bit */
392 return (1 << 20) | /* shift to Right */
393 (8 << 16); /* 8bit */
399 struct rsnd_mod *rsnd_mod_next(int *iterator,
400 struct rsnd_dai_stream *io,
401 enum rsnd_mod_type *array,
404 struct rsnd_mod *mod;
405 enum rsnd_mod_type type;
406 int max = array ? array_size : RSND_MOD_MAX;
408 for (; *iterator < max; (*iterator)++) {
409 type = (array) ? array[*iterator] : *iterator;
420 static enum rsnd_mod_type rsnd_mod_sequence[][RSND_MOD_MAX] = {
454 static int rsnd_status_update(u32 *status,
455 int shift, int add, int timing)
457 u32 mask = 0xF << shift;
458 u8 val = (*status >> shift) & 0xF;
459 u8 next_val = (val + add) & 0xF;
460 int func_call = (val == timing);
462 if (next_val == 0xF) /* underflow case */
465 *status = (*status & ~mask) + (next_val << shift);
470 #define rsnd_dai_call(fn, io, param...) \
472 struct device *dev = rsnd_priv_to_dev(rsnd_io_to_priv(io)); \
473 struct rsnd_mod *mod; \
474 int is_play = rsnd_io_is_play(io); \
476 enum rsnd_mod_type *types = rsnd_mod_sequence[is_play]; \
477 for_each_rsnd_mod_arrays(i, mod, io, types, RSND_MOD_MAX) { \
479 u32 *status = mod->get_status(io, mod, types[i]); \
480 int func_call = rsnd_status_update(status, \
481 __rsnd_mod_shift_##fn, \
482 __rsnd_mod_add_##fn, \
483 __rsnd_mod_call_##fn); \
484 dev_dbg(dev, "%s[%d]\t0x%08x %s\n", \
485 rsnd_mod_name(mod), rsnd_mod_id(mod), *status, \
486 (func_call && (mod)->ops->fn) ? #fn : ""); \
487 if (func_call && (mod)->ops->fn) \
488 tmp = (mod)->ops->fn(mod, io, param); \
490 dev_err(dev, "%s[%d] : %s error %d\n", \
491 rsnd_mod_name(mod), rsnd_mod_id(mod), \
498 int rsnd_dai_connect(struct rsnd_mod *mod,
499 struct rsnd_dai_stream *io,
500 enum rsnd_mod_type type)
502 struct rsnd_priv *priv;
508 if (io->mod[type] == mod)
514 priv = rsnd_mod_to_priv(mod);
515 dev = rsnd_priv_to_dev(priv);
519 dev_dbg(dev, "%s[%d] is connected to io (%s)\n",
520 rsnd_mod_name(mod), rsnd_mod_id(mod),
521 rsnd_io_is_play(io) ? "Playback" : "Capture");
526 static void rsnd_dai_disconnect(struct rsnd_mod *mod,
527 struct rsnd_dai_stream *io,
528 enum rsnd_mod_type type)
530 io->mod[type] = NULL;
533 int rsnd_rdai_channels_ctrl(struct rsnd_dai *rdai,
536 if (max_channels > 0)
537 rdai->max_channels = max_channels;
539 return rdai->max_channels;
542 int rsnd_rdai_ssi_lane_ctrl(struct rsnd_dai *rdai,
546 rdai->ssi_lane = ssi_lane;
548 return rdai->ssi_lane;
551 struct rsnd_dai *rsnd_rdai_get(struct rsnd_priv *priv, int id)
553 if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
556 return priv->rdai + id;
559 #define rsnd_dai_to_priv(dai) snd_soc_dai_get_drvdata(dai)
560 static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai)
562 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
564 return rsnd_rdai_get(priv, dai->id);
568 * rsnd_soc_dai functions
570 void rsnd_dai_period_elapsed(struct rsnd_dai_stream *io)
572 struct snd_pcm_substream *substream = io->substream;
575 * this function should be called...
577 * - if rsnd_dai_pointer_update() returns true
578 * - without spin lock
581 snd_pcm_period_elapsed(substream);
584 static void rsnd_dai_stream_init(struct rsnd_dai_stream *io,
585 struct snd_pcm_substream *substream)
587 io->substream = substream;
590 static void rsnd_dai_stream_quit(struct rsnd_dai_stream *io)
592 io->substream = NULL;
596 struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream)
598 struct snd_soc_pcm_runtime *rtd = substream->private_data;
604 struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai,
605 struct snd_pcm_substream *substream)
607 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
608 return &rdai->playback;
610 return &rdai->capture;
613 static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd,
614 struct snd_soc_dai *dai)
616 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
617 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
618 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
622 spin_lock_irqsave(&priv->lock, flags);
625 case SNDRV_PCM_TRIGGER_START:
626 case SNDRV_PCM_TRIGGER_RESUME:
627 rsnd_dai_stream_init(io, substream);
629 ret = rsnd_dai_call(init, io, priv);
631 goto dai_trigger_end;
633 ret = rsnd_dai_call(start, io, priv);
635 goto dai_trigger_end;
637 ret = rsnd_dai_call(irq, io, priv, 1);
639 goto dai_trigger_end;
642 case SNDRV_PCM_TRIGGER_STOP:
643 case SNDRV_PCM_TRIGGER_SUSPEND:
644 ret = rsnd_dai_call(irq, io, priv, 0);
646 ret |= rsnd_dai_call(stop, io, priv);
648 ret |= rsnd_dai_call(quit, io, priv);
650 rsnd_dai_stream_quit(io);
657 spin_unlock_irqrestore(&priv->lock, flags);
662 static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
664 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
666 /* set master/slave audio interface */
667 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
668 case SND_SOC_DAIFMT_CBM_CFM:
669 rdai->clk_master = 0;
671 case SND_SOC_DAIFMT_CBS_CFS:
672 rdai->clk_master = 1; /* codec is slave, cpu is master */
679 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
680 case SND_SOC_DAIFMT_I2S:
682 rdai->data_alignment = 0;
683 rdai->frm_clk_inv = 0;
685 case SND_SOC_DAIFMT_LEFT_J:
687 rdai->data_alignment = 0;
688 rdai->frm_clk_inv = 1;
690 case SND_SOC_DAIFMT_RIGHT_J:
692 rdai->data_alignment = 1;
693 rdai->frm_clk_inv = 1;
697 /* set clock inversion */
698 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
699 case SND_SOC_DAIFMT_NB_IF:
700 rdai->frm_clk_inv = !rdai->frm_clk_inv;
702 case SND_SOC_DAIFMT_IB_NF:
703 rdai->bit_clk_inv = !rdai->bit_clk_inv;
705 case SND_SOC_DAIFMT_IB_IF:
706 rdai->bit_clk_inv = !rdai->bit_clk_inv;
707 rdai->frm_clk_inv = !rdai->frm_clk_inv;
709 case SND_SOC_DAIFMT_NB_NF:
717 static int rsnd_soc_set_dai_tdm_slot(struct snd_soc_dai *dai,
718 u32 tx_mask, u32 rx_mask,
719 int slots, int slot_width)
721 struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
722 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
723 struct device *dev = rsnd_priv_to_dev(priv);
730 /* TDM Extend Mode */
731 rsnd_rdai_channels_set(rdai, slots);
732 rsnd_rdai_ssi_lane_set(rdai, 1);
735 dev_err(dev, "unsupported TDM slots (%d)\n", slots);
742 static unsigned int rsnd_soc_hw_channels_list[] = {
746 static unsigned int rsnd_soc_hw_rate_list[] = {
761 static int rsnd_soc_hw_rule(struct rsnd_priv *priv,
762 unsigned int *list, int list_num,
763 struct snd_interval *baseline, struct snd_interval *iv)
765 struct snd_interval p;
769 snd_interval_any(&p);
773 for (i = 0; i < list_num; i++) {
775 if (!snd_interval_test(iv, list[i]))
778 rate = rsnd_ssi_clk_query(priv,
779 baseline->min, list[i], NULL);
781 p.min = min(p.min, list[i]);
782 p.max = max(p.max, list[i]);
785 rate = rsnd_ssi_clk_query(priv,
786 baseline->max, list[i], NULL);
788 p.min = min(p.min, list[i]);
789 p.max = max(p.max, list[i]);
793 return snd_interval_refine(iv, &p);
796 static int rsnd_soc_hw_rule_rate(struct snd_pcm_hw_params *params,
797 struct snd_pcm_hw_rule *rule)
799 struct snd_interval *ic_ = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
800 struct snd_interval *ir = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
801 struct snd_interval ic;
802 struct snd_soc_dai *dai = rule->private;
803 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
804 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
807 * possible sampling rate limitation is same as
808 * 2ch if it supports multi ssi
811 if (1 < rsnd_rdai_ssi_lane_get(rdai)) {
816 return rsnd_soc_hw_rule(priv, rsnd_soc_hw_rate_list,
817 ARRAY_SIZE(rsnd_soc_hw_rate_list),
822 static int rsnd_soc_hw_rule_channels(struct snd_pcm_hw_params *params,
823 struct snd_pcm_hw_rule *rule)
825 struct snd_interval *ic_ = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
826 struct snd_interval *ir = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
827 struct snd_interval ic;
828 struct snd_soc_dai *dai = rule->private;
829 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
830 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
833 * possible sampling rate limitation is same as
834 * 2ch if it supports multi ssi
837 if (1 < rsnd_rdai_ssi_lane_get(rdai)) {
842 return rsnd_soc_hw_rule(priv, rsnd_soc_hw_channels_list,
843 ARRAY_SIZE(rsnd_soc_hw_channels_list),
847 static void rsnd_soc_hw_constraint(struct snd_pcm_runtime *runtime,
848 struct snd_soc_dai *dai)
850 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
851 struct snd_pcm_hw_constraint_list *constraint = &rdai->constraint;
852 unsigned int max_channels = rsnd_rdai_channels_get(rdai);
857 * It depends on Platform design
859 constraint->list = rsnd_soc_hw_channels_list;
860 constraint->count = 0;
861 constraint->mask = 0;
863 for (i = 0; i < ARRAY_SIZE(rsnd_soc_hw_channels_list); i++) {
864 if (rsnd_soc_hw_channels_list[i] > max_channels)
866 constraint->count = i + 1;
869 snd_pcm_hw_constraint_list(runtime, 0,
870 SNDRV_PCM_HW_PARAM_CHANNELS, constraint);
873 * Sampling Rate / Channel Limitation
874 * It depends on Clock Master Mode
876 if (!rsnd_rdai_is_clk_master(rdai))
879 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
880 rsnd_soc_hw_rule_rate, dai,
881 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
882 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
883 rsnd_soc_hw_rule_channels, dai,
884 SNDRV_PCM_HW_PARAM_RATE, -1);
887 static int rsnd_soc_dai_startup(struct snd_pcm_substream *substream,
888 struct snd_soc_dai *dai)
890 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
891 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
892 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
895 /* rsnd_io_to_runtime() is not yet enabled here */
896 rsnd_soc_hw_constraint(substream->runtime, dai);
899 * call rsnd_dai_call without spinlock
901 ret = rsnd_dai_call(nolock_start, io, priv);
903 rsnd_dai_call(nolock_stop, io, priv);
908 static void rsnd_soc_dai_shutdown(struct snd_pcm_substream *substream,
909 struct snd_soc_dai *dai)
911 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
912 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
913 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
916 * call rsnd_dai_call without spinlock
918 rsnd_dai_call(nolock_stop, io, priv);
921 static const struct snd_soc_dai_ops rsnd_soc_dai_ops = {
922 .startup = rsnd_soc_dai_startup,
923 .shutdown = rsnd_soc_dai_shutdown,
924 .trigger = rsnd_soc_dai_trigger,
925 .set_fmt = rsnd_soc_dai_set_fmt,
926 .set_tdm_slot = rsnd_soc_set_dai_tdm_slot,
929 void rsnd_parse_connect_common(struct rsnd_dai *rdai,
930 struct rsnd_mod* (*mod_get)(struct rsnd_priv *priv, int id),
931 struct device_node *node,
932 struct device_node *playback,
933 struct device_node *capture)
935 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
936 struct device_node *np;
937 struct rsnd_mod *mod;
944 for_each_child_of_node(node, np) {
945 mod = mod_get(priv, i);
947 rsnd_dai_connect(mod, &rdai->playback, mod->type);
949 rsnd_dai_connect(mod, &rdai->capture, mod->type);
956 static struct device_node *rsnd_dai_of_node(struct rsnd_priv *priv,
959 struct device *dev = rsnd_priv_to_dev(priv);
960 struct device_node *np = dev->of_node;
961 struct device_node *dai_node;
962 struct device_node *ret;
967 * parse both previous dai (= rcar_sound,dai), and
968 * graph dai (= ports/port)
970 dai_node = of_get_child_by_name(np, RSND_NODE_DAI);
973 goto of_node_compatible;
978 dai_node = of_graph_get_next_endpoint(np, NULL);
987 of_node_put(dai_node);
992 static void __rsnd_dai_probe(struct rsnd_priv *priv,
993 struct device_node *dai_np,
994 int dai_i, int is_graph)
996 struct device_node *playback, *capture;
997 struct rsnd_dai_stream *io_playback;
998 struct rsnd_dai_stream *io_capture;
999 struct snd_soc_dai_driver *drv;
1000 struct rsnd_dai *rdai;
1001 struct device *dev = rsnd_priv_to_dev(priv);
1004 rdai = rsnd_rdai_get(priv, dai_i);
1005 drv = priv->daidrv + dai_i;
1006 io_playback = &rdai->playback;
1007 io_capture = &rdai->capture;
1009 snprintf(rdai->name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", dai_i);
1012 drv->name = rdai->name;
1013 drv->ops = &rsnd_soc_dai_ops;
1015 snprintf(rdai->playback.name, RSND_DAI_NAME_SIZE,
1016 "DAI%d Playback", dai_i);
1017 drv->playback.rates = RSND_RATES;
1018 drv->playback.formats = RSND_FMTS;
1019 drv->playback.channels_min = 2;
1020 drv->playback.channels_max = 16;
1021 drv->playback.stream_name = rdai->playback.name;
1023 snprintf(rdai->capture.name, RSND_DAI_NAME_SIZE,
1024 "DAI%d Capture", dai_i);
1025 drv->capture.rates = RSND_RATES;
1026 drv->capture.formats = RSND_FMTS;
1027 drv->capture.channels_min = 2;
1028 drv->capture.channels_max = 16;
1029 drv->capture.stream_name = rdai->capture.name;
1031 rdai->playback.rdai = rdai;
1032 rdai->capture.rdai = rdai;
1033 rsnd_rdai_channels_set(rdai, 2); /* default 2ch */
1034 rsnd_rdai_ssi_lane_set(rdai, 1); /* default 1lane */
1036 for (io_i = 0;; io_i++) {
1037 playback = of_parse_phandle(dai_np, "playback", io_i);
1038 capture = of_parse_phandle(dai_np, "capture", io_i);
1040 if (!playback && !capture)
1043 rsnd_parse_connect_ssi(rdai, playback, capture);
1044 rsnd_parse_connect_src(rdai, playback, capture);
1045 rsnd_parse_connect_ctu(rdai, playback, capture);
1046 rsnd_parse_connect_mix(rdai, playback, capture);
1047 rsnd_parse_connect_dvc(rdai, playback, capture);
1049 of_node_put(playback);
1050 of_node_put(capture);
1053 dev_dbg(dev, "%s (%s/%s)\n", rdai->name,
1054 rsnd_io_to_mod_ssi(io_playback) ? "play" : " -- ",
1055 rsnd_io_to_mod_ssi(io_capture) ? "capture" : " -- ");
1058 static int rsnd_dai_probe(struct rsnd_priv *priv)
1060 struct device_node *dai_node;
1061 struct device_node *dai_np;
1062 struct snd_soc_dai_driver *rdrv;
1063 struct device *dev = rsnd_priv_to_dev(priv);
1064 struct rsnd_dai *rdai;
1069 dai_node = rsnd_dai_of_node(priv, &is_graph);
1071 nr = of_graph_get_endpoint_count(dai_node);
1073 nr = of_get_child_count(dai_node);
1078 rdrv = devm_kzalloc(dev, sizeof(*rdrv) * nr, GFP_KERNEL);
1079 rdai = devm_kzalloc(dev, sizeof(*rdai) * nr, GFP_KERNEL);
1084 priv->daidrv = rdrv;
1092 for_each_endpoint_of_node(dai_node, dai_np) {
1093 __rsnd_dai_probe(priv, dai_np, dai_i, is_graph);
1094 rsnd_ssi_parse_hdmi_connection(priv, dai_np, dai_i);
1098 for_each_child_of_node(dai_node, dai_np)
1099 __rsnd_dai_probe(priv, dai_np, dai_i++, is_graph);
1108 static struct snd_pcm_hardware rsnd_pcm_hardware = {
1109 .info = SNDRV_PCM_INFO_INTERLEAVED |
1110 SNDRV_PCM_INFO_MMAP |
1111 SNDRV_PCM_INFO_MMAP_VALID,
1112 .buffer_bytes_max = 64 * 1024,
1113 .period_bytes_min = 32,
1114 .period_bytes_max = 8192,
1120 static int rsnd_pcm_open(struct snd_pcm_substream *substream)
1122 struct snd_pcm_runtime *runtime = substream->runtime;
1125 snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware);
1127 ret = snd_pcm_hw_constraint_integer(runtime,
1128 SNDRV_PCM_HW_PARAM_PERIODS);
1133 static int rsnd_hw_params(struct snd_pcm_substream *substream,
1134 struct snd_pcm_hw_params *hw_params)
1136 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
1137 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1138 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
1141 ret = rsnd_dai_call(hw_params, io, substream, hw_params);
1145 return snd_pcm_lib_malloc_pages(substream,
1146 params_buffer_bytes(hw_params));
1149 static snd_pcm_uframes_t rsnd_pointer(struct snd_pcm_substream *substream)
1151 struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
1152 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1153 struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
1154 snd_pcm_uframes_t pointer = 0;
1156 rsnd_dai_call(pointer, io, &pointer);
1161 static struct snd_pcm_ops rsnd_pcm_ops = {
1162 .open = rsnd_pcm_open,
1163 .ioctl = snd_pcm_lib_ioctl,
1164 .hw_params = rsnd_hw_params,
1165 .hw_free = snd_pcm_lib_free_pages,
1166 .pointer = rsnd_pointer,
1172 #define kcontrol_to_cfg(kctrl) ((struct rsnd_kctrl_cfg *)kctrl->private_value)
1173 static int rsnd_kctrl_info(struct snd_kcontrol *kctrl,
1174 struct snd_ctl_elem_info *uinfo)
1176 struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
1179 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1180 uinfo->count = cfg->size;
1181 uinfo->value.enumerated.items = cfg->max;
1182 if (uinfo->value.enumerated.item >= cfg->max)
1183 uinfo->value.enumerated.item = cfg->max - 1;
1184 strlcpy(uinfo->value.enumerated.name,
1185 cfg->texts[uinfo->value.enumerated.item],
1186 sizeof(uinfo->value.enumerated.name));
1188 uinfo->count = cfg->size;
1189 uinfo->value.integer.min = 0;
1190 uinfo->value.integer.max = cfg->max;
1191 uinfo->type = (cfg->max == 1) ?
1192 SNDRV_CTL_ELEM_TYPE_BOOLEAN :
1193 SNDRV_CTL_ELEM_TYPE_INTEGER;
1199 static int rsnd_kctrl_get(struct snd_kcontrol *kctrl,
1200 struct snd_ctl_elem_value *uc)
1202 struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
1205 for (i = 0; i < cfg->size; i++)
1207 uc->value.enumerated.item[i] = cfg->val[i];
1209 uc->value.integer.value[i] = cfg->val[i];
1214 static int rsnd_kctrl_put(struct snd_kcontrol *kctrl,
1215 struct snd_ctl_elem_value *uc)
1217 struct rsnd_mod *mod = snd_kcontrol_chip(kctrl);
1218 struct rsnd_kctrl_cfg *cfg = kcontrol_to_cfg(kctrl);
1221 if (!cfg->accept(cfg->io))
1224 for (i = 0; i < cfg->size; i++) {
1226 change |= (uc->value.enumerated.item[i] != cfg->val[i]);
1227 cfg->val[i] = uc->value.enumerated.item[i];
1229 change |= (uc->value.integer.value[i] != cfg->val[i]);
1230 cfg->val[i] = uc->value.integer.value[i];
1234 if (change && cfg->update)
1235 cfg->update(cfg->io, mod);
1240 int rsnd_kctrl_accept_anytime(struct rsnd_dai_stream *io)
1245 int rsnd_kctrl_accept_runtime(struct rsnd_dai_stream *io)
1247 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
1252 struct rsnd_kctrl_cfg *rsnd_kctrl_init_m(struct rsnd_kctrl_cfg_m *cfg)
1254 cfg->cfg.val = cfg->val;
1259 struct rsnd_kctrl_cfg *rsnd_kctrl_init_s(struct rsnd_kctrl_cfg_s *cfg)
1261 cfg->cfg.val = &cfg->val;
1266 int rsnd_kctrl_new(struct rsnd_mod *mod,
1267 struct rsnd_dai_stream *io,
1268 struct snd_soc_pcm_runtime *rtd,
1269 const unsigned char *name,
1270 int (*accept)(struct rsnd_dai_stream *io),
1271 void (*update)(struct rsnd_dai_stream *io,
1272 struct rsnd_mod *mod),
1273 struct rsnd_kctrl_cfg *cfg,
1274 const char * const *texts,
1278 struct snd_card *card = rtd->card->snd_card;
1279 struct snd_kcontrol *kctrl;
1280 struct snd_kcontrol_new knew = {
1281 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1283 .info = rsnd_kctrl_info,
1285 .get = rsnd_kctrl_get,
1286 .put = rsnd_kctrl_put,
1287 .private_value = (unsigned long)cfg,
1291 if (size > RSND_MAX_CHANNELS)
1294 kctrl = snd_ctl_new1(&knew, mod);
1298 ret = snd_ctl_add(card, kctrl);
1305 cfg->accept = accept;
1306 cfg->update = update;
1318 #define PREALLOC_BUFFER (32 * 1024)
1319 #define PREALLOC_BUFFER_MAX (32 * 1024)
1321 static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd)
1323 struct snd_soc_dai *dai = rtd->cpu_dai;
1324 struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1327 ret = rsnd_dai_call(pcm_new, &rdai->playback, rtd);
1331 ret = rsnd_dai_call(pcm_new, &rdai->capture, rtd);
1335 return snd_pcm_lib_preallocate_pages_for_all(
1337 SNDRV_DMA_TYPE_CONTINUOUS,
1338 snd_dma_continuous_data(GFP_KERNEL),
1339 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
1342 static struct snd_soc_platform_driver rsnd_soc_platform = {
1343 .ops = &rsnd_pcm_ops,
1344 .pcm_new = rsnd_pcm_new,
1347 static const struct snd_soc_component_driver rsnd_soc_component = {
1351 static int rsnd_rdai_continuance_probe(struct rsnd_priv *priv,
1352 struct rsnd_dai_stream *io)
1356 ret = rsnd_dai_call(probe, io, priv);
1357 if (ret == -EAGAIN) {
1358 struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io);
1359 struct rsnd_mod *mod;
1363 * Fallback to PIO mode
1367 * call "remove" for SSI/SRC/DVC
1368 * SSI will be switch to PIO mode if it was DMA mode
1371 * rsnd_ssi_fallback()
1373 rsnd_dai_call(remove, io, priv);
1376 * remove all mod from io
1377 * and, re connect ssi
1379 for_each_rsnd_mod(i, mod, io)
1380 rsnd_dai_disconnect(mod, io, i);
1381 rsnd_dai_connect(ssi_mod, io, RSND_MOD_SSI);
1386 rsnd_dai_call(fallback, io, priv);
1390 * DAI has SSI which is PIO mode only now.
1392 ret = rsnd_dai_call(probe, io, priv);
1401 static int rsnd_probe(struct platform_device *pdev)
1403 struct rsnd_priv *priv;
1404 struct device *dev = &pdev->dev;
1405 struct rsnd_dai *rdai;
1406 int (*probe_func[])(struct rsnd_priv *priv) = {
1424 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1426 dev_err(dev, "priv allocate failed\n");
1431 priv->flags = (unsigned long)of_device_get_match_data(dev);
1432 spin_lock_init(&priv->lock);
1437 for (i = 0; i < ARRAY_SIZE(probe_func); i++) {
1438 ret = probe_func[i](priv);
1443 for_each_rsnd_dai(rdai, priv, i) {
1444 ret = rsnd_rdai_continuance_probe(priv, &rdai->playback);
1446 goto exit_snd_probe;
1448 ret = rsnd_rdai_continuance_probe(priv, &rdai->capture);
1450 goto exit_snd_probe;
1453 dev_set_drvdata(dev, priv);
1458 ret = snd_soc_register_platform(dev, &rsnd_soc_platform);
1460 dev_err(dev, "cannot snd soc register\n");
1464 ret = snd_soc_register_component(dev, &rsnd_soc_component,
1465 priv->daidrv, rsnd_rdai_nr(priv));
1467 dev_err(dev, "cannot snd dai register\n");
1471 pm_runtime_enable(dev);
1473 dev_info(dev, "probed\n");
1477 snd_soc_unregister_platform(dev);
1479 for_each_rsnd_dai(rdai, priv, i) {
1480 rsnd_dai_call(remove, &rdai->playback, priv);
1481 rsnd_dai_call(remove, &rdai->capture, priv);
1487 static int rsnd_remove(struct platform_device *pdev)
1489 struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
1490 struct rsnd_dai *rdai;
1491 void (*remove_func[])(struct rsnd_priv *priv) = {
1503 pm_runtime_disable(&pdev->dev);
1505 for_each_rsnd_dai(rdai, priv, i) {
1506 ret |= rsnd_dai_call(remove, &rdai->playback, priv);
1507 ret |= rsnd_dai_call(remove, &rdai->capture, priv);
1510 for (i = 0; i < ARRAY_SIZE(remove_func); i++)
1511 remove_func[i](priv);
1513 snd_soc_unregister_component(&pdev->dev);
1514 snd_soc_unregister_platform(&pdev->dev);
1519 static int rsnd_suspend(struct device *dev)
1521 struct rsnd_priv *priv = dev_get_drvdata(dev);
1523 rsnd_adg_clk_disable(priv);
1528 static int rsnd_resume(struct device *dev)
1530 struct rsnd_priv *priv = dev_get_drvdata(dev);
1532 rsnd_adg_clk_enable(priv);
1537 static const struct dev_pm_ops rsnd_pm_ops = {
1538 .suspend = rsnd_suspend,
1539 .resume = rsnd_resume,
1542 static struct platform_driver rsnd_driver = {
1544 .name = "rcar_sound",
1546 .of_match_table = rsnd_of_match,
1548 .probe = rsnd_probe,
1549 .remove = rsnd_remove,
1551 module_platform_driver(rsnd_driver);
1553 MODULE_LICENSE("GPL");
1554 MODULE_DESCRIPTION("Renesas R-Car audio driver");
1555 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
1556 MODULE_ALIAS("platform:rcar-pcm-audio");