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 struct dma_chan
*rsnd_mod_dma_req(struct rsnd_dai_stream
*io
,
125 struct rsnd_mod
*mod
)
127 if (!mod
|| !mod
->ops
|| !mod
->ops
->dma_req
)
130 return mod
->ops
->dma_req(io
, mod
);
133 u32
*rsnd_mod_get_status(struct rsnd_dai_stream
*io
,
134 struct rsnd_mod
*mod
,
135 enum rsnd_mod_type type
)
140 int rsnd_mod_init(struct rsnd_priv
*priv
,
141 struct rsnd_mod
*mod
,
142 struct rsnd_mod_ops
*ops
,
144 u32
* (*get_status
)(struct rsnd_dai_stream
*io
,
145 struct rsnd_mod
*mod
,
146 enum rsnd_mod_type type
),
147 enum rsnd_mod_type type
,
150 int ret
= clk_prepare(clk
);
160 mod
->get_status
= get_status
;
165 void rsnd_mod_quit(struct rsnd_mod
*mod
)
167 clk_unprepare(mod
->clk
);
171 void rsnd_mod_interrupt(struct rsnd_mod
*mod
,
172 void (*callback
)(struct rsnd_mod
*mod
,
173 struct rsnd_dai_stream
*io
))
175 struct rsnd_priv
*priv
= rsnd_mod_to_priv(mod
);
176 struct rsnd_dai_stream
*io
;
177 struct rsnd_dai
*rdai
;
180 for_each_rsnd_dai(rdai
, priv
, i
) {
181 io
= &rdai
->playback
;
182 if (mod
== io
->mod
[mod
->type
])
186 if (mod
== io
->mod
[mod
->type
])
191 int rsnd_io_is_working(struct rsnd_dai_stream
*io
)
193 /* see rsnd_dai_stream_init/quit() */
195 return snd_pcm_running(io
->substream
);
200 int rsnd_runtime_channel_original(struct rsnd_dai_stream
*io
)
202 struct snd_pcm_runtime
*runtime
= rsnd_io_to_runtime(io
);
204 return runtime
->channels
;
207 int rsnd_runtime_channel_after_ctu(struct rsnd_dai_stream
*io
)
209 int chan
= rsnd_runtime_channel_original(io
);
210 struct rsnd_mod
*ctu_mod
= rsnd_io_to_mod_ctu(io
);
213 u32 converted_chan
= rsnd_ctu_converted_channel(ctu_mod
);
216 return converted_chan
;
222 int rsnd_runtime_channel_for_ssi(struct rsnd_dai_stream
*io
)
224 struct rsnd_dai
*rdai
= rsnd_io_to_rdai(io
);
225 int chan
= rsnd_io_is_play(io
) ?
226 rsnd_runtime_channel_after_ctu(io
) :
227 rsnd_runtime_channel_original(io
);
230 if (rsnd_runtime_is_ssi_multi(io
))
231 chan
/= rsnd_rdai_ssi_lane_get(rdai
);
233 /* TDM Extend Mode needs 8ch */
240 int rsnd_runtime_is_ssi_multi(struct rsnd_dai_stream
*io
)
242 struct rsnd_dai
*rdai
= rsnd_io_to_rdai(io
);
243 int lane
= rsnd_rdai_ssi_lane_get(rdai
);
244 int chan
= rsnd_io_is_play(io
) ?
245 rsnd_runtime_channel_after_ctu(io
) :
246 rsnd_runtime_channel_original(io
);
248 return (chan
> 2) && (lane
> 1);
251 int rsnd_runtime_is_ssi_tdm(struct rsnd_dai_stream
*io
)
253 return rsnd_runtime_channel_for_ssi(io
) >= 6;
259 u32
rsnd_get_adinr_bit(struct rsnd_mod
*mod
, struct rsnd_dai_stream
*io
)
261 struct rsnd_priv
*priv
= rsnd_mod_to_priv(mod
);
262 struct snd_pcm_runtime
*runtime
= rsnd_io_to_runtime(io
);
263 struct device
*dev
= rsnd_priv_to_dev(priv
);
265 switch (runtime
->sample_bits
) {
272 dev_warn(dev
, "not supported sample bits\n");
280 u32
rsnd_get_dalign(struct rsnd_mod
*mod
, struct rsnd_dai_stream
*io
)
282 struct rsnd_mod
*ssiu
= rsnd_io_to_mod_ssiu(io
);
283 struct rsnd_mod
*target
;
284 struct snd_pcm_runtime
*runtime
= rsnd_io_to_runtime(io
);
285 u32 val
= 0x76543210;
289 * *Hardware* L/R and *Software* L/R are inverted.
290 * We need to care about inversion timing to control
291 * Playback/Capture correctly.
292 * The point is [DVC] needs *Hardware* L/R, [MEM] needs *Software* L/R
294 * sL/R : software L/R
295 * hL/R : hardware L/R
296 * (*) : conversion timing
299 * sL/R (*) hL/R hL/R hL/R hL/R hL/R
300 * [MEM] -> [SRC] -> [DVC] -> [CMD] -> [SSIU] -> [SSI] -> codec
303 * hL/R hL/R hL/R hL/R hL/R (*) sL/R
304 * codec -> [SSI] -> [SSIU] -> [SRC] -> [DVC] -> [CMD] -> [MEM]
306 if (rsnd_io_is_play(io
)) {
307 struct rsnd_mod
*src
= rsnd_io_to_mod_src(io
);
309 target
= src
? src
: ssiu
;
311 struct rsnd_mod
*cmd
= rsnd_io_to_mod_cmd(io
);
313 target
= cmd
? cmd
: ssiu
;
316 mask
<<= runtime
->channels
* 4;
319 switch (runtime
->sample_bits
) {
321 val
|= 0x67452301 & ~mask
;
324 val
|= 0x76543210 & ~mask
;
329 * exchange channeles on SRC if possible,
330 * otherwise, R/L volume settings on DVC
331 * changes inverted channels
339 u32
rsnd_get_busif_shift(struct rsnd_dai_stream
*io
, struct rsnd_mod
*mod
)
341 enum rsnd_mod_type playback_mods
[] = {
346 enum rsnd_mod_type capture_mods
[] = {
351 struct snd_pcm_runtime
*runtime
= rsnd_io_to_runtime(io
);
352 struct rsnd_mod
*tmod
= NULL
;
353 enum rsnd_mod_type
*mods
=
354 rsnd_io_is_play(io
) ?
355 playback_mods
: capture_mods
;
359 * This is needed for 24bit data
360 * We need to shift 8bit
362 * Linux 24bit data is located as 0x00******
363 * HW 24bit data is located as 0x******00
366 switch (runtime
->sample_bits
) {
373 for (i
= 0; i
< ARRAY_SIZE(playback_mods
); i
++) {
374 tmod
= rsnd_io_to_mod(io
, mods
[i
]);
382 if (rsnd_io_is_play(io
))
383 return (0 << 20) | /* shift to Left */
384 (8 << 16); /* 8bit */
386 return (1 << 20) | /* shift to Right */
387 (8 << 16); /* 8bit */
393 struct rsnd_mod
*rsnd_mod_next(int *iterator
,
394 struct rsnd_dai_stream
*io
,
395 enum rsnd_mod_type
*array
,
398 struct rsnd_mod
*mod
;
399 enum rsnd_mod_type type
;
400 int max
= array
? array_size
: RSND_MOD_MAX
;
402 for (; *iterator
< max
; (*iterator
)++) {
403 type
= (array
) ? array
[*iterator
] : *iterator
;
404 mod
= rsnd_io_to_mod(io
, type
);
412 static enum rsnd_mod_type rsnd_mod_sequence
[][RSND_MOD_MAX
] = {
446 static int rsnd_status_update(u32
*status
,
447 int shift
, int add
, int timing
)
449 u32 mask
= 0xF << shift
;
450 u8 val
= (*status
>> shift
) & 0xF;
451 u8 next_val
= (val
+ add
) & 0xF;
452 int func_call
= (val
== timing
);
454 if (next_val
== 0xF) /* underflow case */
457 *status
= (*status
& ~mask
) + (next_val
<< shift
);
462 #define rsnd_dai_call(fn, io, param...) \
464 struct device *dev = rsnd_priv_to_dev(rsnd_io_to_priv(io)); \
465 struct rsnd_mod *mod; \
466 int is_play = rsnd_io_is_play(io); \
468 enum rsnd_mod_type *types = rsnd_mod_sequence[is_play]; \
469 for_each_rsnd_mod_arrays(i, mod, io, types, RSND_MOD_MAX) { \
471 u32 *status = mod->get_status(io, mod, types[i]); \
472 int func_call = rsnd_status_update(status, \
473 __rsnd_mod_shift_##fn, \
474 __rsnd_mod_add_##fn, \
475 __rsnd_mod_call_##fn); \
476 dev_dbg(dev, "%s[%d]\t0x%08x %s\n", \
477 rsnd_mod_name(mod), rsnd_mod_id(mod), *status, \
478 (func_call && (mod)->ops->fn) ? #fn : ""); \
479 if (func_call && (mod)->ops->fn) \
480 tmp = (mod)->ops->fn(mod, io, param); \
482 dev_err(dev, "%s[%d] : %s error %d\n", \
483 rsnd_mod_name(mod), rsnd_mod_id(mod), \
490 int rsnd_dai_connect(struct rsnd_mod
*mod
,
491 struct rsnd_dai_stream
*io
,
492 enum rsnd_mod_type type
)
494 struct rsnd_priv
*priv
;
500 if (io
->mod
[type
] == mod
)
506 priv
= rsnd_mod_to_priv(mod
);
507 dev
= rsnd_priv_to_dev(priv
);
511 dev_dbg(dev
, "%s[%d] is connected to io (%s)\n",
512 rsnd_mod_name(mod
), rsnd_mod_id(mod
),
513 rsnd_io_is_play(io
) ? "Playback" : "Capture");
518 static void rsnd_dai_disconnect(struct rsnd_mod
*mod
,
519 struct rsnd_dai_stream
*io
,
520 enum rsnd_mod_type type
)
522 io
->mod
[type
] = NULL
;
525 int rsnd_rdai_channels_ctrl(struct rsnd_dai
*rdai
,
528 if (max_channels
> 0)
529 rdai
->max_channels
= max_channels
;
531 return rdai
->max_channels
;
534 int rsnd_rdai_ssi_lane_ctrl(struct rsnd_dai
*rdai
,
538 rdai
->ssi_lane
= ssi_lane
;
540 return rdai
->ssi_lane
;
543 struct rsnd_dai
*rsnd_rdai_get(struct rsnd_priv
*priv
, int id
)
545 if ((id
< 0) || (id
>= rsnd_rdai_nr(priv
)))
548 return priv
->rdai
+ id
;
551 #define rsnd_dai_to_priv(dai) snd_soc_dai_get_drvdata(dai)
552 static struct rsnd_dai
*rsnd_dai_to_rdai(struct snd_soc_dai
*dai
)
554 struct rsnd_priv
*priv
= rsnd_dai_to_priv(dai
);
556 return rsnd_rdai_get(priv
, dai
->id
);
560 * rsnd_soc_dai functions
562 void rsnd_dai_period_elapsed(struct rsnd_dai_stream
*io
)
564 struct snd_pcm_substream
*substream
= io
->substream
;
567 * this function should be called...
569 * - if rsnd_dai_pointer_update() returns true
570 * - without spin lock
573 snd_pcm_period_elapsed(substream
);
576 static void rsnd_dai_stream_init(struct rsnd_dai_stream
*io
,
577 struct snd_pcm_substream
*substream
)
579 io
->substream
= substream
;
582 static void rsnd_dai_stream_quit(struct rsnd_dai_stream
*io
)
584 io
->substream
= NULL
;
588 struct snd_soc_dai
*rsnd_substream_to_dai(struct snd_pcm_substream
*substream
)
590 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
596 struct rsnd_dai_stream
*rsnd_rdai_to_io(struct rsnd_dai
*rdai
,
597 struct snd_pcm_substream
*substream
)
599 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
600 return &rdai
->playback
;
602 return &rdai
->capture
;
605 static int rsnd_soc_dai_trigger(struct snd_pcm_substream
*substream
, int cmd
,
606 struct snd_soc_dai
*dai
)
608 struct rsnd_priv
*priv
= rsnd_dai_to_priv(dai
);
609 struct rsnd_dai
*rdai
= rsnd_dai_to_rdai(dai
);
610 struct rsnd_dai_stream
*io
= rsnd_rdai_to_io(rdai
, substream
);
614 spin_lock_irqsave(&priv
->lock
, flags
);
617 case SNDRV_PCM_TRIGGER_START
:
618 case SNDRV_PCM_TRIGGER_RESUME
:
619 rsnd_dai_stream_init(io
, substream
);
621 ret
= rsnd_dai_call(init
, io
, priv
);
623 goto dai_trigger_end
;
625 ret
= rsnd_dai_call(start
, io
, priv
);
627 goto dai_trigger_end
;
629 ret
= rsnd_dai_call(irq
, io
, priv
, 1);
631 goto dai_trigger_end
;
634 case SNDRV_PCM_TRIGGER_STOP
:
635 case SNDRV_PCM_TRIGGER_SUSPEND
:
636 ret
= rsnd_dai_call(irq
, io
, priv
, 0);
638 ret
|= rsnd_dai_call(stop
, io
, priv
);
640 ret
|= rsnd_dai_call(quit
, io
, priv
);
642 rsnd_dai_stream_quit(io
);
649 spin_unlock_irqrestore(&priv
->lock
, flags
);
654 static int rsnd_soc_dai_set_fmt(struct snd_soc_dai
*dai
, unsigned int fmt
)
656 struct rsnd_dai
*rdai
= rsnd_dai_to_rdai(dai
);
658 /* set master/slave audio interface */
659 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
660 case SND_SOC_DAIFMT_CBM_CFM
:
661 rdai
->clk_master
= 0;
663 case SND_SOC_DAIFMT_CBS_CFS
:
664 rdai
->clk_master
= 1; /* codec is slave, cpu is master */
671 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
672 case SND_SOC_DAIFMT_I2S
:
674 rdai
->data_alignment
= 0;
675 rdai
->frm_clk_inv
= 0;
677 case SND_SOC_DAIFMT_LEFT_J
:
679 rdai
->data_alignment
= 0;
680 rdai
->frm_clk_inv
= 1;
682 case SND_SOC_DAIFMT_RIGHT_J
:
684 rdai
->data_alignment
= 1;
685 rdai
->frm_clk_inv
= 1;
689 /* set clock inversion */
690 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
691 case SND_SOC_DAIFMT_NB_IF
:
692 rdai
->frm_clk_inv
= !rdai
->frm_clk_inv
;
694 case SND_SOC_DAIFMT_IB_NF
:
695 rdai
->bit_clk_inv
= !rdai
->bit_clk_inv
;
697 case SND_SOC_DAIFMT_IB_IF
:
698 rdai
->bit_clk_inv
= !rdai
->bit_clk_inv
;
699 rdai
->frm_clk_inv
= !rdai
->frm_clk_inv
;
701 case SND_SOC_DAIFMT_NB_NF
:
709 static int rsnd_soc_set_dai_tdm_slot(struct snd_soc_dai
*dai
,
710 u32 tx_mask
, u32 rx_mask
,
711 int slots
, int slot_width
)
713 struct rsnd_priv
*priv
= rsnd_dai_to_priv(dai
);
714 struct rsnd_dai
*rdai
= rsnd_dai_to_rdai(dai
);
715 struct device
*dev
= rsnd_priv_to_dev(priv
);
721 /* TDM Extend Mode */
722 rsnd_rdai_channels_set(rdai
, slots
);
723 rsnd_rdai_ssi_lane_set(rdai
, 1);
726 dev_err(dev
, "unsupported TDM slots (%d)\n", slots
);
733 static unsigned int rsnd_soc_hw_channels_list
[] = {
737 static unsigned int rsnd_soc_hw_rate_list
[] = {
752 static int rsnd_soc_hw_rule(struct rsnd_priv
*priv
,
753 unsigned int *list
, int list_num
,
754 struct snd_interval
*baseline
, struct snd_interval
*iv
)
756 struct snd_interval p
;
760 snd_interval_any(&p
);
764 for (i
= 0; i
< list_num
; i
++) {
766 if (!snd_interval_test(iv
, list
[i
]))
769 rate
= rsnd_ssi_clk_query(priv
,
770 baseline
->min
, list
[i
], NULL
);
772 p
.min
= min(p
.min
, list
[i
]);
773 p
.max
= max(p
.max
, list
[i
]);
776 rate
= rsnd_ssi_clk_query(priv
,
777 baseline
->max
, list
[i
], NULL
);
779 p
.min
= min(p
.min
, list
[i
]);
780 p
.max
= max(p
.max
, list
[i
]);
784 return snd_interval_refine(iv
, &p
);
787 static int rsnd_soc_hw_rule_rate(struct snd_pcm_hw_params
*params
,
788 struct snd_pcm_hw_rule
*rule
)
790 struct snd_interval
*ic_
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_CHANNELS
);
791 struct snd_interval
*ir
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_RATE
);
792 struct snd_interval ic
;
793 struct snd_soc_dai
*dai
= rule
->private;
794 struct rsnd_dai
*rdai
= rsnd_dai_to_rdai(dai
);
795 struct rsnd_priv
*priv
= rsnd_rdai_to_priv(rdai
);
798 * possible sampling rate limitation is same as
799 * 2ch if it supports multi ssi
802 if (1 < rsnd_rdai_ssi_lane_get(rdai
)) {
807 return rsnd_soc_hw_rule(priv
, rsnd_soc_hw_rate_list
,
808 ARRAY_SIZE(rsnd_soc_hw_rate_list
),
813 static int rsnd_soc_hw_rule_channels(struct snd_pcm_hw_params
*params
,
814 struct snd_pcm_hw_rule
*rule
)
816 struct snd_interval
*ic_
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_CHANNELS
);
817 struct snd_interval
*ir
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_RATE
);
818 struct snd_interval ic
;
819 struct snd_soc_dai
*dai
= rule
->private;
820 struct rsnd_dai
*rdai
= rsnd_dai_to_rdai(dai
);
821 struct rsnd_priv
*priv
= rsnd_rdai_to_priv(rdai
);
824 * possible sampling rate limitation is same as
825 * 2ch if it supports multi ssi
828 if (1 < rsnd_rdai_ssi_lane_get(rdai
)) {
833 return rsnd_soc_hw_rule(priv
, rsnd_soc_hw_channels_list
,
834 ARRAY_SIZE(rsnd_soc_hw_channels_list
),
838 static const struct snd_pcm_hardware rsnd_pcm_hardware
= {
839 .info
= SNDRV_PCM_INFO_INTERLEAVED
|
840 SNDRV_PCM_INFO_MMAP
|
841 SNDRV_PCM_INFO_MMAP_VALID
,
842 .buffer_bytes_max
= 64 * 1024,
843 .period_bytes_min
= 32,
844 .period_bytes_max
= 8192,
850 static int rsnd_soc_dai_startup(struct snd_pcm_substream
*substream
,
851 struct snd_soc_dai
*dai
)
853 struct rsnd_dai
*rdai
= rsnd_dai_to_rdai(dai
);
854 struct rsnd_priv
*priv
= rsnd_rdai_to_priv(rdai
);
855 struct rsnd_dai_stream
*io
= rsnd_rdai_to_io(rdai
, substream
);
856 struct snd_pcm_hw_constraint_list
*constraint
= &rdai
->constraint
;
857 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
858 unsigned int max_channels
= rsnd_rdai_channels_get(rdai
);
864 * It depends on Platform design
866 constraint
->list
= rsnd_soc_hw_channels_list
;
867 constraint
->count
= 0;
868 constraint
->mask
= 0;
870 for (i
= 0; i
< ARRAY_SIZE(rsnd_soc_hw_channels_list
); i
++) {
871 if (rsnd_soc_hw_channels_list
[i
] > max_channels
)
873 constraint
->count
= i
+ 1;
876 snd_soc_set_runtime_hwparams(substream
, &rsnd_pcm_hardware
);
878 snd_pcm_hw_constraint_list(runtime
, 0,
879 SNDRV_PCM_HW_PARAM_CHANNELS
, constraint
);
881 snd_pcm_hw_constraint_integer(runtime
,
882 SNDRV_PCM_HW_PARAM_PERIODS
);
885 * Sampling Rate / Channel Limitation
886 * It depends on Clock Master Mode
888 if (rsnd_rdai_is_clk_master(rdai
)) {
889 snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_RATE
,
890 rsnd_soc_hw_rule_rate
, dai
,
891 SNDRV_PCM_HW_PARAM_CHANNELS
, -1);
892 snd_pcm_hw_rule_add(runtime
, 0, SNDRV_PCM_HW_PARAM_CHANNELS
,
893 rsnd_soc_hw_rule_channels
, dai
,
894 SNDRV_PCM_HW_PARAM_RATE
, -1);
898 * call rsnd_dai_call without spinlock
900 ret
= rsnd_dai_call(nolock_start
, io
, priv
);
902 rsnd_dai_call(nolock_stop
, io
, priv
);
907 static void rsnd_soc_dai_shutdown(struct snd_pcm_substream
*substream
,
908 struct snd_soc_dai
*dai
)
910 struct rsnd_dai
*rdai
= rsnd_dai_to_rdai(dai
);
911 struct rsnd_priv
*priv
= rsnd_rdai_to_priv(rdai
);
912 struct rsnd_dai_stream
*io
= rsnd_rdai_to_io(rdai
, substream
);
915 * call rsnd_dai_call without spinlock
917 rsnd_dai_call(nolock_stop
, io
, priv
);
920 static const struct snd_soc_dai_ops rsnd_soc_dai_ops
= {
921 .startup
= rsnd_soc_dai_startup
,
922 .shutdown
= rsnd_soc_dai_shutdown
,
923 .trigger
= rsnd_soc_dai_trigger
,
924 .set_fmt
= rsnd_soc_dai_set_fmt
,
925 .set_tdm_slot
= rsnd_soc_set_dai_tdm_slot
,
928 void rsnd_parse_connect_common(struct rsnd_dai
*rdai
,
929 struct rsnd_mod
* (*mod_get
)(struct rsnd_priv
*priv
, int id
),
930 struct device_node
*node
,
931 struct device_node
*playback
,
932 struct device_node
*capture
)
934 struct rsnd_priv
*priv
= rsnd_rdai_to_priv(rdai
);
935 struct device_node
*np
;
936 struct rsnd_mod
*mod
;
943 for_each_child_of_node(node
, np
) {
944 mod
= mod_get(priv
, i
);
946 rsnd_dai_connect(mod
, &rdai
->playback
, mod
->type
);
948 rsnd_dai_connect(mod
, &rdai
->capture
, mod
->type
);
955 static struct device_node
*rsnd_dai_of_node(struct rsnd_priv
*priv
,
958 struct device
*dev
= rsnd_priv_to_dev(priv
);
959 struct device_node
*np
= dev
->of_node
;
960 struct device_node
*dai_node
;
961 struct device_node
*ret
;
966 * parse both previous dai (= rcar_sound,dai), and
967 * graph dai (= ports/port)
969 dai_node
= of_get_child_by_name(np
, RSND_NODE_DAI
);
972 goto of_node_compatible
;
977 dai_node
= of_graph_get_next_endpoint(np
, NULL
);
986 of_node_put(dai_node
);
991 static void __rsnd_dai_probe(struct rsnd_priv
*priv
,
992 struct device_node
*dai_np
,
993 int dai_i
, int is_graph
)
995 struct device_node
*playback
, *capture
;
996 struct rsnd_dai_stream
*io_playback
;
997 struct rsnd_dai_stream
*io_capture
;
998 struct snd_soc_dai_driver
*drv
;
999 struct rsnd_dai
*rdai
;
1000 struct device
*dev
= rsnd_priv_to_dev(priv
);
1003 rdai
= rsnd_rdai_get(priv
, dai_i
);
1004 drv
= priv
->daidrv
+ dai_i
;
1005 io_playback
= &rdai
->playback
;
1006 io_capture
= &rdai
->capture
;
1008 snprintf(rdai
->name
, RSND_DAI_NAME_SIZE
, "rsnd-dai.%d", dai_i
);
1011 drv
->name
= rdai
->name
;
1012 drv
->ops
= &rsnd_soc_dai_ops
;
1014 snprintf(rdai
->playback
.name
, RSND_DAI_NAME_SIZE
,
1015 "DAI%d Playback", dai_i
);
1016 drv
->playback
.rates
= RSND_RATES
;
1017 drv
->playback
.formats
= RSND_FMTS
;
1018 drv
->playback
.channels_min
= 2;
1019 drv
->playback
.channels_max
= 8;
1020 drv
->playback
.stream_name
= rdai
->playback
.name
;
1022 snprintf(rdai
->capture
.name
, RSND_DAI_NAME_SIZE
,
1023 "DAI%d Capture", dai_i
);
1024 drv
->capture
.rates
= RSND_RATES
;
1025 drv
->capture
.formats
= RSND_FMTS
;
1026 drv
->capture
.channels_min
= 2;
1027 drv
->capture
.channels_max
= 8;
1028 drv
->capture
.stream_name
= rdai
->capture
.name
;
1030 rdai
->playback
.rdai
= rdai
;
1031 rdai
->capture
.rdai
= rdai
;
1032 rsnd_rdai_channels_set(rdai
, 2); /* default 2ch */
1033 rsnd_rdai_ssi_lane_set(rdai
, 1); /* default 1lane */
1035 for (io_i
= 0;; io_i
++) {
1036 playback
= of_parse_phandle(dai_np
, "playback", io_i
);
1037 capture
= of_parse_phandle(dai_np
, "capture", io_i
);
1039 if (!playback
&& !capture
)
1042 rsnd_parse_connect_ssi(rdai
, playback
, capture
);
1043 rsnd_parse_connect_src(rdai
, playback
, capture
);
1044 rsnd_parse_connect_ctu(rdai
, playback
, capture
);
1045 rsnd_parse_connect_mix(rdai
, playback
, capture
);
1046 rsnd_parse_connect_dvc(rdai
, playback
, capture
);
1048 of_node_put(playback
);
1049 of_node_put(capture
);
1052 dev_dbg(dev
, "%s (%s/%s)\n", rdai
->name
,
1053 rsnd_io_to_mod_ssi(io_playback
) ? "play" : " -- ",
1054 rsnd_io_to_mod_ssi(io_capture
) ? "capture" : " -- ");
1057 static int rsnd_dai_probe(struct rsnd_priv
*priv
)
1059 struct device_node
*dai_node
;
1060 struct device_node
*dai_np
;
1061 struct snd_soc_dai_driver
*rdrv
;
1062 struct device
*dev
= rsnd_priv_to_dev(priv
);
1063 struct rsnd_dai
*rdai
;
1068 dai_node
= rsnd_dai_of_node(priv
, &is_graph
);
1070 nr
= of_graph_get_endpoint_count(dai_node
);
1072 nr
= of_get_child_count(dai_node
);
1077 rdrv
= devm_kzalloc(dev
, sizeof(*rdrv
) * nr
, GFP_KERNEL
);
1078 rdai
= devm_kzalloc(dev
, sizeof(*rdai
) * nr
, GFP_KERNEL
);
1083 priv
->daidrv
= rdrv
;
1091 for_each_endpoint_of_node(dai_node
, dai_np
) {
1092 __rsnd_dai_probe(priv
, dai_np
, dai_i
, is_graph
);
1093 rsnd_ssi_parse_hdmi_connection(priv
, dai_np
, dai_i
);
1097 for_each_child_of_node(dai_node
, dai_np
)
1098 __rsnd_dai_probe(priv
, dai_np
, dai_i
++, is_graph
);
1107 static int rsnd_hw_params(struct snd_pcm_substream
*substream
,
1108 struct snd_pcm_hw_params
*hw_params
)
1110 struct snd_soc_dai
*dai
= rsnd_substream_to_dai(substream
);
1111 struct rsnd_dai
*rdai
= rsnd_dai_to_rdai(dai
);
1112 struct rsnd_dai_stream
*io
= rsnd_rdai_to_io(rdai
, substream
);
1115 ret
= rsnd_dai_call(hw_params
, io
, substream
, hw_params
);
1119 return snd_pcm_lib_malloc_pages(substream
,
1120 params_buffer_bytes(hw_params
));
1123 static snd_pcm_uframes_t
rsnd_pointer(struct snd_pcm_substream
*substream
)
1125 struct snd_soc_dai
*dai
= rsnd_substream_to_dai(substream
);
1126 struct rsnd_dai
*rdai
= rsnd_dai_to_rdai(dai
);
1127 struct rsnd_dai_stream
*io
= rsnd_rdai_to_io(rdai
, substream
);
1128 snd_pcm_uframes_t pointer
= 0;
1130 rsnd_dai_call(pointer
, io
, &pointer
);
1135 static const struct snd_pcm_ops rsnd_pcm_ops
= {
1136 .ioctl
= snd_pcm_lib_ioctl
,
1137 .hw_params
= rsnd_hw_params
,
1138 .hw_free
= snd_pcm_lib_free_pages
,
1139 .pointer
= rsnd_pointer
,
1145 static int rsnd_kctrl_info(struct snd_kcontrol
*kctrl
,
1146 struct snd_ctl_elem_info
*uinfo
)
1148 struct rsnd_kctrl_cfg
*cfg
= snd_kcontrol_chip(kctrl
);
1151 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
1152 uinfo
->count
= cfg
->size
;
1153 uinfo
->value
.enumerated
.items
= cfg
->max
;
1154 if (uinfo
->value
.enumerated
.item
>= cfg
->max
)
1155 uinfo
->value
.enumerated
.item
= cfg
->max
- 1;
1156 strlcpy(uinfo
->value
.enumerated
.name
,
1157 cfg
->texts
[uinfo
->value
.enumerated
.item
],
1158 sizeof(uinfo
->value
.enumerated
.name
));
1160 uinfo
->count
= cfg
->size
;
1161 uinfo
->value
.integer
.min
= 0;
1162 uinfo
->value
.integer
.max
= cfg
->max
;
1163 uinfo
->type
= (cfg
->max
== 1) ?
1164 SNDRV_CTL_ELEM_TYPE_BOOLEAN
:
1165 SNDRV_CTL_ELEM_TYPE_INTEGER
;
1171 static int rsnd_kctrl_get(struct snd_kcontrol
*kctrl
,
1172 struct snd_ctl_elem_value
*uc
)
1174 struct rsnd_kctrl_cfg
*cfg
= snd_kcontrol_chip(kctrl
);
1177 for (i
= 0; i
< cfg
->size
; i
++)
1179 uc
->value
.enumerated
.item
[i
] = cfg
->val
[i
];
1181 uc
->value
.integer
.value
[i
] = cfg
->val
[i
];
1186 static int rsnd_kctrl_put(struct snd_kcontrol
*kctrl
,
1187 struct snd_ctl_elem_value
*uc
)
1189 struct rsnd_kctrl_cfg
*cfg
= snd_kcontrol_chip(kctrl
);
1192 if (!cfg
->accept(cfg
->io
))
1195 for (i
= 0; i
< cfg
->size
; i
++) {
1197 change
|= (uc
->value
.enumerated
.item
[i
] != cfg
->val
[i
]);
1198 cfg
->val
[i
] = uc
->value
.enumerated
.item
[i
];
1200 change
|= (uc
->value
.integer
.value
[i
] != cfg
->val
[i
]);
1201 cfg
->val
[i
] = uc
->value
.integer
.value
[i
];
1205 if (change
&& cfg
->update
)
1206 cfg
->update(cfg
->io
, cfg
->mod
);
1211 int rsnd_kctrl_accept_anytime(struct rsnd_dai_stream
*io
)
1216 int rsnd_kctrl_accept_runtime(struct rsnd_dai_stream
*io
)
1218 struct snd_pcm_runtime
*runtime
= rsnd_io_to_runtime(io
);
1223 struct rsnd_kctrl_cfg
*rsnd_kctrl_init_m(struct rsnd_kctrl_cfg_m
*cfg
)
1225 cfg
->cfg
.val
= cfg
->val
;
1230 struct rsnd_kctrl_cfg
*rsnd_kctrl_init_s(struct rsnd_kctrl_cfg_s
*cfg
)
1232 cfg
->cfg
.val
= &cfg
->val
;
1237 const char * const volume_ramp_rate
[] = {
1238 "128 dB/1 step", /* 00000 */
1239 "64 dB/1 step", /* 00001 */
1240 "32 dB/1 step", /* 00010 */
1241 "16 dB/1 step", /* 00011 */
1242 "8 dB/1 step", /* 00100 */
1243 "4 dB/1 step", /* 00101 */
1244 "2 dB/1 step", /* 00110 */
1245 "1 dB/1 step", /* 00111 */
1246 "0.5 dB/1 step", /* 01000 */
1247 "0.25 dB/1 step", /* 01001 */
1248 "0.125 dB/1 step", /* 01010 = VOLUME_RAMP_MAX_MIX */
1249 "0.125 dB/2 steps", /* 01011 */
1250 "0.125 dB/4 steps", /* 01100 */
1251 "0.125 dB/8 steps", /* 01101 */
1252 "0.125 dB/16 steps", /* 01110 */
1253 "0.125 dB/32 steps", /* 01111 */
1254 "0.125 dB/64 steps", /* 10000 */
1255 "0.125 dB/128 steps", /* 10001 */
1256 "0.125 dB/256 steps", /* 10010 */
1257 "0.125 dB/512 steps", /* 10011 */
1258 "0.125 dB/1024 steps", /* 10100 */
1259 "0.125 dB/2048 steps", /* 10101 */
1260 "0.125 dB/4096 steps", /* 10110 */
1261 "0.125 dB/8192 steps", /* 10111 = VOLUME_RAMP_MAX_DVC */
1264 int rsnd_kctrl_new(struct rsnd_mod
*mod
,
1265 struct rsnd_dai_stream
*io
,
1266 struct snd_soc_pcm_runtime
*rtd
,
1267 const unsigned char *name
,
1268 int (*accept
)(struct rsnd_dai_stream
*io
),
1269 void (*update
)(struct rsnd_dai_stream
*io
,
1270 struct rsnd_mod
*mod
),
1271 struct rsnd_kctrl_cfg
*cfg
,
1272 const char * const *texts
,
1276 struct snd_card
*card
= rtd
->card
->snd_card
;
1277 struct snd_kcontrol
*kctrl
;
1278 struct snd_kcontrol_new knew
= {
1279 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
1281 .info
= rsnd_kctrl_info
,
1283 .get
= rsnd_kctrl_get
,
1284 .put
= rsnd_kctrl_put
,
1288 if (size
> RSND_MAX_CHANNELS
)
1291 kctrl
= snd_ctl_new1(&knew
, cfg
);
1295 ret
= snd_ctl_add(card
, kctrl
);
1302 cfg
->accept
= accept
;
1303 cfg
->update
= update
;
1316 #define PREALLOC_BUFFER (32 * 1024)
1317 #define PREALLOC_BUFFER_MAX (32 * 1024)
1319 static int rsnd_pcm_new(struct snd_soc_pcm_runtime
*rtd
)
1321 struct snd_soc_dai
*dai
= rtd
->cpu_dai
;
1322 struct rsnd_dai
*rdai
= rsnd_dai_to_rdai(dai
);
1325 ret
= rsnd_dai_call(pcm_new
, &rdai
->playback
, rtd
);
1329 ret
= rsnd_dai_call(pcm_new
, &rdai
->capture
, rtd
);
1333 return snd_pcm_lib_preallocate_pages_for_all(
1335 SNDRV_DMA_TYPE_CONTINUOUS
,
1336 snd_dma_continuous_data(GFP_KERNEL
),
1337 PREALLOC_BUFFER
, PREALLOC_BUFFER_MAX
);
1340 static const struct snd_soc_platform_driver rsnd_soc_platform
= {
1341 .ops
= &rsnd_pcm_ops
,
1342 .pcm_new
= rsnd_pcm_new
,
1345 static const struct snd_soc_component_driver rsnd_soc_component
= {
1349 static int rsnd_rdai_continuance_probe(struct rsnd_priv
*priv
,
1350 struct rsnd_dai_stream
*io
)
1354 ret
= rsnd_dai_call(probe
, io
, priv
);
1355 if (ret
== -EAGAIN
) {
1356 struct rsnd_mod
*ssi_mod
= rsnd_io_to_mod_ssi(io
);
1357 struct rsnd_mod
*mod
;
1361 * Fallback to PIO mode
1365 * call "remove" for SSI/SRC/DVC
1366 * SSI will be switch to PIO mode if it was DMA mode
1369 * rsnd_ssi_fallback()
1371 rsnd_dai_call(remove
, io
, priv
);
1374 * remove all mod from io
1375 * and, re connect ssi
1377 for_each_rsnd_mod(i
, mod
, io
)
1378 rsnd_dai_disconnect(mod
, io
, i
);
1379 rsnd_dai_connect(ssi_mod
, io
, RSND_MOD_SSI
);
1384 rsnd_dai_call(fallback
, io
, priv
);
1388 * DAI has SSI which is PIO mode only now.
1390 ret
= rsnd_dai_call(probe
, io
, priv
);
1399 static int rsnd_probe(struct platform_device
*pdev
)
1401 struct rsnd_priv
*priv
;
1402 struct device
*dev
= &pdev
->dev
;
1403 struct rsnd_dai
*rdai
;
1404 int (*probe_func
[])(struct rsnd_priv
*priv
) = {
1422 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
1427 priv
->flags
= (unsigned long)of_device_get_match_data(dev
);
1428 spin_lock_init(&priv
->lock
);
1433 for (i
= 0; i
< ARRAY_SIZE(probe_func
); i
++) {
1434 ret
= probe_func
[i
](priv
);
1439 for_each_rsnd_dai(rdai
, priv
, i
) {
1440 ret
= rsnd_rdai_continuance_probe(priv
, &rdai
->playback
);
1442 goto exit_snd_probe
;
1444 ret
= rsnd_rdai_continuance_probe(priv
, &rdai
->capture
);
1446 goto exit_snd_probe
;
1449 dev_set_drvdata(dev
, priv
);
1454 ret
= snd_soc_register_platform(dev
, &rsnd_soc_platform
);
1456 dev_err(dev
, "cannot snd soc register\n");
1460 ret
= snd_soc_register_component(dev
, &rsnd_soc_component
,
1461 priv
->daidrv
, rsnd_rdai_nr(priv
));
1463 dev_err(dev
, "cannot snd dai register\n");
1467 pm_runtime_enable(dev
);
1469 dev_info(dev
, "probed\n");
1473 snd_soc_unregister_platform(dev
);
1475 for_each_rsnd_dai(rdai
, priv
, i
) {
1476 rsnd_dai_call(remove
, &rdai
->playback
, priv
);
1477 rsnd_dai_call(remove
, &rdai
->capture
, priv
);
1483 static int rsnd_remove(struct platform_device
*pdev
)
1485 struct rsnd_priv
*priv
= dev_get_drvdata(&pdev
->dev
);
1486 struct rsnd_dai
*rdai
;
1487 void (*remove_func
[])(struct rsnd_priv
*priv
) = {
1499 pm_runtime_disable(&pdev
->dev
);
1501 for_each_rsnd_dai(rdai
, priv
, i
) {
1502 ret
|= rsnd_dai_call(remove
, &rdai
->playback
, priv
);
1503 ret
|= rsnd_dai_call(remove
, &rdai
->capture
, priv
);
1506 for (i
= 0; i
< ARRAY_SIZE(remove_func
); i
++)
1507 remove_func
[i
](priv
);
1509 snd_soc_unregister_component(&pdev
->dev
);
1510 snd_soc_unregister_platform(&pdev
->dev
);
1515 static int rsnd_suspend(struct device
*dev
)
1517 struct rsnd_priv
*priv
= dev_get_drvdata(dev
);
1519 rsnd_adg_clk_disable(priv
);
1524 static int rsnd_resume(struct device
*dev
)
1526 struct rsnd_priv
*priv
= dev_get_drvdata(dev
);
1528 rsnd_adg_clk_enable(priv
);
1533 static const struct dev_pm_ops rsnd_pm_ops
= {
1534 .suspend
= rsnd_suspend
,
1535 .resume
= rsnd_resume
,
1538 static struct platform_driver rsnd_driver
= {
1540 .name
= "rcar_sound",
1542 .of_match_table
= rsnd_of_match
,
1544 .probe
= rsnd_probe
,
1545 .remove
= rsnd_remove
,
1547 module_platform_driver(rsnd_driver
);
1549 MODULE_LICENSE("GPL");
1550 MODULE_DESCRIPTION("Renesas R-Car audio driver");
1551 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
1552 MODULE_ALIAS("platform:rcar-pcm-audio");