2 * Renesas R-Car SRU/SCU/SSIU/SSI support
4 * Copyright (C) 2013 Renesas Solutions Corp.
5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
8 * Kuninori Morimoto <morimoto.kuninori@renesas.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
16 * Renesas R-Car sound device structure
20 * SRU : Sound Routing Unit
21 * - SRC : Sampling Rate Converter
23 * - CTU : Channel Count Conversion Unit
25 * - DVC : Digital Volume and Mute Function
26 * - SSI : Serial Sound Interface
30 * SCU : Sampling Rate Converter Unit
31 * - SRC : Sampling Rate Converter
33 * - CTU : Channel Count Conversion Unit
35 * - DVC : Digital Volume and Mute Function
36 * SSIU : Serial Sound Interface Unit
37 * - SSI : Serial Sound Interface
45 * | ** this depends on Gen1/Gen2
49 * | ** these depend on data path
50 * | ** gen and platform data control it
54 * | +- playback -> [mod] -> [mod] -> [mod] -> ...
57 * | +- capture -> [mod] -> [mod] -> [mod] -> ...
61 * | +- playback -> [mod] -> [mod] -> [mod] -> ...
64 * | +- capture -> [mod] -> [mod] -> [mod] -> ...
67 * | ** these control ssi
76 * | ** these control src
86 * for_each_rsnd_dai(xx, priv, xx)
87 * rdai[0] => rdai[1] => rdai[2] => ...
89 * for_each_rsnd_mod(xx, rdai, xx)
90 * [mod] => [mod] => [mod] => ...
92 * rsnd_dai_call(xxx, fn )
93 * [mod]->fn() -> [mod]->fn() -> [mod]->fn()...
96 #include <linux/pm_runtime.h>
99 #define RSND_RATES SNDRV_PCM_RATE_8000_192000
100 #define RSND_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
102 static const struct of_device_id rsnd_of_match
[] = {
103 { .compatible
= "renesas,rcar_sound-gen1", .data
= (void *)RSND_GEN1
},
104 { .compatible
= "renesas,rcar_sound-gen2", .data
= (void *)RSND_GEN2
},
105 { .compatible
= "renesas,rcar_sound-gen3", .data
= (void *)RSND_GEN2
}, /* gen2 compatible */
108 MODULE_DEVICE_TABLE(of
, rsnd_of_match
);
113 void rsnd_mod_make_sure(struct rsnd_mod
*mod
, enum rsnd_mod_type type
)
115 if (mod
->type
!= type
) {
116 struct rsnd_priv
*priv
= rsnd_mod_to_priv(mod
);
117 struct device
*dev
= rsnd_priv_to_dev(priv
);
119 dev_warn(dev
, "%s[%d] is not your expected module\n",
120 rsnd_mod_name(mod
), rsnd_mod_id(mod
));
124 char *rsnd_mod_name(struct rsnd_mod
*mod
)
126 if (!mod
|| !mod
->ops
)
129 return mod
->ops
->name
;
132 struct dma_chan
*rsnd_mod_dma_req(struct rsnd_dai_stream
*io
,
133 struct rsnd_mod
*mod
)
135 if (!mod
|| !mod
->ops
|| !mod
->ops
->dma_req
)
138 return mod
->ops
->dma_req(io
, mod
);
141 u32
*rsnd_mod_get_status(struct rsnd_dai_stream
*io
,
142 struct rsnd_mod
*mod
,
143 enum rsnd_mod_type type
)
148 int rsnd_mod_init(struct rsnd_priv
*priv
,
149 struct rsnd_mod
*mod
,
150 struct rsnd_mod_ops
*ops
,
152 u32
* (*get_status
)(struct rsnd_dai_stream
*io
,
153 struct rsnd_mod
*mod
,
154 enum rsnd_mod_type type
),
155 enum rsnd_mod_type type
,
158 int ret
= clk_prepare(clk
);
168 mod
->get_status
= get_status
;
173 void rsnd_mod_quit(struct rsnd_mod
*mod
)
176 clk_unprepare(mod
->clk
);
180 void rsnd_mod_interrupt(struct rsnd_mod
*mod
,
181 void (*callback
)(struct rsnd_mod
*mod
,
182 struct rsnd_dai_stream
*io
))
184 struct rsnd_priv
*priv
= rsnd_mod_to_priv(mod
);
185 struct rsnd_dai_stream
*io
;
186 struct rsnd_dai
*rdai
;
189 for_each_rsnd_dai(rdai
, priv
, i
) {
190 io
= &rdai
->playback
;
191 if (mod
== io
->mod
[mod
->type
])
195 if (mod
== io
->mod
[mod
->type
])
200 int rsnd_io_is_working(struct rsnd_dai_stream
*io
)
202 /* see rsnd_dai_stream_init/quit() */
203 return !!io
->substream
;
206 void rsnd_set_slot(struct rsnd_dai
*rdai
,
210 rdai
->slots_num
= num
;
213 int rsnd_get_slot(struct rsnd_dai_stream
*io
)
215 struct rsnd_dai
*rdai
= rsnd_io_to_rdai(io
);
220 int rsnd_get_slot_num(struct rsnd_dai_stream
*io
)
222 struct rsnd_dai
*rdai
= rsnd_io_to_rdai(io
);
224 return rdai
->slots_num
;
227 int rsnd_runtime_channel_original(struct rsnd_dai_stream
*io
)
229 struct snd_pcm_runtime
*runtime
= rsnd_io_to_runtime(io
);
231 return runtime
->channels
;
234 int rsnd_runtime_channel_after_ctu(struct rsnd_dai_stream
*io
)
236 int chan
= rsnd_runtime_channel_original(io
);
237 struct rsnd_mod
*ctu_mod
= rsnd_io_to_mod_ctu(io
);
240 u32 converted_chan
= rsnd_ctu_converted_channel(ctu_mod
);
243 return converted_chan
;
249 int rsnd_runtime_channel_for_ssi(struct rsnd_dai_stream
*io
)
251 int chan
= rsnd_io_is_play(io
) ?
252 rsnd_runtime_channel_after_ctu(io
) :
253 rsnd_runtime_channel_original(io
);
256 if (rsnd_runtime_is_ssi_multi(io
))
257 chan
/= rsnd_get_slot_num(io
);
259 /* TDM Extend Mode needs 8ch */
266 int rsnd_runtime_is_ssi_multi(struct rsnd_dai_stream
*io
)
268 int slots
= rsnd_get_slot_num(io
);
269 int chan
= rsnd_io_is_play(io
) ?
270 rsnd_runtime_channel_after_ctu(io
) :
271 rsnd_runtime_channel_original(io
);
273 return (chan
>= 6) && (slots
> 1);
276 int rsnd_runtime_is_ssi_tdm(struct rsnd_dai_stream
*io
)
278 return rsnd_runtime_channel_for_ssi(io
) >= 6;
284 u32
rsnd_get_adinr_bit(struct rsnd_mod
*mod
, struct rsnd_dai_stream
*io
)
286 struct rsnd_priv
*priv
= rsnd_mod_to_priv(mod
);
287 struct snd_pcm_runtime
*runtime
= rsnd_io_to_runtime(io
);
288 struct device
*dev
= rsnd_priv_to_dev(priv
);
290 switch (runtime
->sample_bits
) {
297 dev_warn(dev
, "not supported sample bits\n");
305 u32
rsnd_get_dalign(struct rsnd_mod
*mod
, struct rsnd_dai_stream
*io
)
307 struct rsnd_mod
*ssiu
= rsnd_io_to_mod_ssiu(io
);
308 struct rsnd_mod
*target
;
309 struct snd_pcm_runtime
*runtime
= rsnd_io_to_runtime(io
);
310 u32 val
= 0x76543210;
313 if (rsnd_io_is_play(io
)) {
314 struct rsnd_mod
*src
= rsnd_io_to_mod_src(io
);
316 target
= src
? src
: ssiu
;
318 struct rsnd_mod
*cmd
= rsnd_io_to_mod_cmd(io
);
320 target
= cmd
? cmd
: ssiu
;
323 mask
<<= runtime
->channels
* 4;
326 switch (runtime
->sample_bits
) {
328 val
|= 0x67452301 & ~mask
;
331 val
|= 0x76543210 & ~mask
;
336 * exchange channeles on SRC if possible,
337 * otherwise, R/L volume settings on DVC
338 * changes inverted channels
349 struct rsnd_mod
*rsnd_mod_next(int *iterator
,
350 struct rsnd_dai_stream
*io
,
351 enum rsnd_mod_type
*array
,
354 struct rsnd_mod
*mod
;
355 enum rsnd_mod_type type
;
356 int max
= array
? array_size
: RSND_MOD_MAX
;
358 for (; *iterator
< max
; (*iterator
)++) {
359 type
= (array
) ? array
[*iterator
] : *iterator
;
370 static enum rsnd_mod_type rsnd_mod_sequence
[][RSND_MOD_MAX
] = {
404 static int rsnd_status_update(u32
*status
,
405 int shift
, int add
, int timing
)
407 u32 mask
= 0xF << shift
;
408 u8 val
= (*status
>> shift
) & 0xF;
409 u8 next_val
= (val
+ add
) & 0xF;
410 int func_call
= (val
== timing
);
412 if (next_val
== 0xF) /* underflow case */
415 *status
= (*status
& ~mask
) + (next_val
<< shift
);
420 #define rsnd_dai_call(fn, io, param...) \
422 struct rsnd_priv *priv = rsnd_io_to_priv(io); \
423 struct device *dev = rsnd_priv_to_dev(priv); \
424 struct rsnd_mod *mod; \
425 int is_play = rsnd_io_is_play(io); \
427 enum rsnd_mod_type *types = rsnd_mod_sequence[is_play]; \
428 for_each_rsnd_mod_arrays(i, mod, io, types, RSND_MOD_MAX) { \
430 u32 *status = mod->get_status(io, mod, types[i]); \
431 int func_call = rsnd_status_update(status, \
432 __rsnd_mod_shift_##fn, \
433 __rsnd_mod_add_##fn, \
434 __rsnd_mod_call_##fn); \
435 dev_dbg(dev, "%s[%d]\t0x%08x %s\n", \
436 rsnd_mod_name(mod), rsnd_mod_id(mod), *status, \
437 (func_call && (mod)->ops->fn) ? #fn : ""); \
438 if (func_call && (mod)->ops->fn) \
439 tmp = (mod)->ops->fn(mod, io, param); \
441 dev_err(dev, "%s[%d] : %s error %d\n", \
442 rsnd_mod_name(mod), rsnd_mod_id(mod), \
449 int rsnd_dai_connect(struct rsnd_mod
*mod
,
450 struct rsnd_dai_stream
*io
,
451 enum rsnd_mod_type type
)
453 struct rsnd_priv
*priv
;
459 if (io
->mod
[type
] == mod
)
465 priv
= rsnd_mod_to_priv(mod
);
466 dev
= rsnd_priv_to_dev(priv
);
470 dev_dbg(dev
, "%s[%d] is connected to io (%s)\n",
471 rsnd_mod_name(mod
), rsnd_mod_id(mod
),
472 rsnd_io_is_play(io
) ? "Playback" : "Capture");
477 static void rsnd_dai_disconnect(struct rsnd_mod
*mod
,
478 struct rsnd_dai_stream
*io
,
479 enum rsnd_mod_type type
)
481 io
->mod
[type
] = NULL
;
484 struct rsnd_dai
*rsnd_rdai_get(struct rsnd_priv
*priv
, int id
)
486 if ((id
< 0) || (id
>= rsnd_rdai_nr(priv
)))
489 return priv
->rdai
+ id
;
492 #define rsnd_dai_to_priv(dai) snd_soc_dai_get_drvdata(dai)
493 static struct rsnd_dai
*rsnd_dai_to_rdai(struct snd_soc_dai
*dai
)
495 struct rsnd_priv
*priv
= rsnd_dai_to_priv(dai
);
497 return rsnd_rdai_get(priv
, dai
->id
);
501 * rsnd_soc_dai functions
503 int rsnd_dai_pointer_offset(struct rsnd_dai_stream
*io
, int additional
)
505 struct snd_pcm_substream
*substream
= io
->substream
;
506 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
507 int pos
= io
->byte_pos
+ additional
;
509 pos
%= (runtime
->periods
* io
->byte_per_period
);
514 bool rsnd_dai_pointer_update(struct rsnd_dai_stream
*io
, int byte
)
516 io
->byte_pos
+= byte
;
518 if (io
->byte_pos
>= io
->next_period_byte
) {
519 struct snd_pcm_substream
*substream
= io
->substream
;
520 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
523 io
->next_period_byte
+= io
->byte_per_period
;
525 if (io
->period_pos
>= runtime
->periods
) {
528 io
->next_period_byte
= io
->byte_per_period
;
537 void rsnd_dai_period_elapsed(struct rsnd_dai_stream
*io
)
539 struct snd_pcm_substream
*substream
= io
->substream
;
542 * this function should be called...
544 * - if rsnd_dai_pointer_update() returns true
545 * - without spin lock
548 snd_pcm_period_elapsed(substream
);
551 static void rsnd_dai_stream_init(struct rsnd_dai_stream
*io
,
552 struct snd_pcm_substream
*substream
)
554 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
556 io
->substream
= substream
;
559 io
->byte_per_period
= runtime
->period_size
*
561 samples_to_bytes(runtime
, 1);
562 io
->next_period_byte
= io
->byte_per_period
;
565 static void rsnd_dai_stream_quit(struct rsnd_dai_stream
*io
)
567 io
->substream
= NULL
;
571 struct snd_soc_dai
*rsnd_substream_to_dai(struct snd_pcm_substream
*substream
)
573 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
579 struct rsnd_dai_stream
*rsnd_rdai_to_io(struct rsnd_dai
*rdai
,
580 struct snd_pcm_substream
*substream
)
582 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
583 return &rdai
->playback
;
585 return &rdai
->capture
;
588 static int rsnd_soc_dai_trigger(struct snd_pcm_substream
*substream
, int cmd
,
589 struct snd_soc_dai
*dai
)
591 struct rsnd_priv
*priv
= rsnd_dai_to_priv(dai
);
592 struct rsnd_dai
*rdai
= rsnd_dai_to_rdai(dai
);
593 struct rsnd_dai_stream
*io
= rsnd_rdai_to_io(rdai
, substream
);
597 spin_lock_irqsave(&priv
->lock
, flags
);
600 case SNDRV_PCM_TRIGGER_START
:
601 case SNDRV_PCM_TRIGGER_RESUME
:
602 rsnd_dai_stream_init(io
, substream
);
604 ret
= rsnd_dai_call(init
, io
, priv
);
606 goto dai_trigger_end
;
608 ret
= rsnd_dai_call(start
, io
, priv
);
610 goto dai_trigger_end
;
612 ret
= rsnd_dai_call(irq
, io
, priv
, 1);
614 goto dai_trigger_end
;
617 case SNDRV_PCM_TRIGGER_STOP
:
618 case SNDRV_PCM_TRIGGER_SUSPEND
:
619 ret
= rsnd_dai_call(irq
, io
, priv
, 0);
621 ret
|= rsnd_dai_call(stop
, io
, priv
);
623 ret
|= rsnd_dai_call(quit
, io
, priv
);
625 rsnd_dai_stream_quit(io
);
632 spin_unlock_irqrestore(&priv
->lock
, flags
);
637 static int rsnd_soc_dai_set_fmt(struct snd_soc_dai
*dai
, unsigned int fmt
)
639 struct rsnd_dai
*rdai
= rsnd_dai_to_rdai(dai
);
641 /* set master/slave audio interface */
642 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
643 case SND_SOC_DAIFMT_CBM_CFM
:
644 rdai
->clk_master
= 0;
646 case SND_SOC_DAIFMT_CBS_CFS
:
647 rdai
->clk_master
= 1; /* codec is slave, cpu is master */
654 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
655 case SND_SOC_DAIFMT_I2S
:
657 rdai
->data_alignment
= 0;
658 rdai
->frm_clk_inv
= 0;
660 case SND_SOC_DAIFMT_LEFT_J
:
662 rdai
->data_alignment
= 0;
663 rdai
->frm_clk_inv
= 1;
665 case SND_SOC_DAIFMT_RIGHT_J
:
667 rdai
->data_alignment
= 1;
668 rdai
->frm_clk_inv
= 1;
672 /* set clock inversion */
673 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
674 case SND_SOC_DAIFMT_NB_IF
:
675 rdai
->frm_clk_inv
= !rdai
->frm_clk_inv
;
677 case SND_SOC_DAIFMT_IB_NF
:
678 rdai
->bit_clk_inv
= !rdai
->bit_clk_inv
;
680 case SND_SOC_DAIFMT_IB_IF
:
681 rdai
->bit_clk_inv
= !rdai
->bit_clk_inv
;
682 rdai
->frm_clk_inv
= !rdai
->frm_clk_inv
;
684 case SND_SOC_DAIFMT_NB_NF
:
692 static int rsnd_soc_set_dai_tdm_slot(struct snd_soc_dai
*dai
,
693 u32 tx_mask
, u32 rx_mask
,
694 int slots
, int slot_width
)
696 struct rsnd_priv
*priv
= rsnd_dai_to_priv(dai
);
697 struct rsnd_dai
*rdai
= rsnd_dai_to_rdai(dai
);
698 struct device
*dev
= rsnd_priv_to_dev(priv
);
702 /* TDM Extend Mode */
703 rsnd_set_slot(rdai
, slots
, 1);
706 dev_err(dev
, "unsupported TDM slots (%d)\n", slots
);
713 static int rsnd_soc_dai_startup(struct snd_pcm_substream
*substream
,
714 struct snd_soc_dai
*dai
)
716 struct rsnd_dai
*rdai
= rsnd_dai_to_rdai(dai
);
717 struct rsnd_dai_stream
*io
= rsnd_rdai_to_io(rdai
, substream
);
720 * call rsnd_dai_call without spinlock
722 return rsnd_dai_call(nolock_start
, io
, priv
);
725 static void rsnd_soc_dai_shutdown(struct snd_pcm_substream
*substream
,
726 struct snd_soc_dai
*dai
)
728 struct rsnd_dai
*rdai
= rsnd_dai_to_rdai(dai
);
729 struct rsnd_dai_stream
*io
= rsnd_rdai_to_io(rdai
, substream
);
732 * call rsnd_dai_call without spinlock
734 rsnd_dai_call(nolock_stop
, io
, priv
);
737 static const struct snd_soc_dai_ops rsnd_soc_dai_ops
= {
738 .startup
= rsnd_soc_dai_startup
,
739 .shutdown
= rsnd_soc_dai_shutdown
,
740 .trigger
= rsnd_soc_dai_trigger
,
741 .set_fmt
= rsnd_soc_dai_set_fmt
,
742 .set_tdm_slot
= rsnd_soc_set_dai_tdm_slot
,
745 void rsnd_parse_connect_common(struct rsnd_dai
*rdai
,
746 struct rsnd_mod
* (*mod_get
)(struct rsnd_priv
*priv
, int id
),
747 struct device_node
*node
,
748 struct device_node
*playback
,
749 struct device_node
*capture
)
751 struct rsnd_priv
*priv
= rsnd_rdai_to_priv(rdai
);
752 struct device_node
*np
;
753 struct rsnd_mod
*mod
;
760 for_each_child_of_node(node
, np
) {
761 mod
= mod_get(priv
, i
);
763 rsnd_dai_connect(mod
, &rdai
->playback
, mod
->type
);
765 rsnd_dai_connect(mod
, &rdai
->capture
, mod
->type
);
772 static int rsnd_dai_probe(struct rsnd_priv
*priv
)
774 struct device_node
*dai_node
;
775 struct device_node
*dai_np
;
776 struct device_node
*playback
, *capture
;
777 struct rsnd_dai_stream
*io_playback
;
778 struct rsnd_dai_stream
*io_capture
;
779 struct snd_soc_dai_driver
*rdrv
, *drv
;
780 struct rsnd_dai
*rdai
;
781 struct device
*dev
= rsnd_priv_to_dev(priv
);
785 dai_node
= rsnd_dai_of_node(priv
);
786 nr
= of_get_child_count(dai_node
);
789 goto rsnd_dai_probe_done
;
792 rdrv
= devm_kzalloc(dev
, sizeof(*rdrv
) * nr
, GFP_KERNEL
);
793 rdai
= devm_kzalloc(dev
, sizeof(*rdai
) * nr
, GFP_KERNEL
);
794 if (!rdrv
|| !rdai
) {
796 goto rsnd_dai_probe_done
;
807 for_each_child_of_node(dai_node
, dai_np
) {
808 rdai
= rsnd_rdai_get(priv
, dai_i
);
810 io_playback
= &rdai
->playback
;
811 io_capture
= &rdai
->capture
;
813 snprintf(rdai
->name
, RSND_DAI_NAME_SIZE
, "rsnd-dai.%d", dai_i
);
816 drv
->name
= rdai
->name
;
817 drv
->ops
= &rsnd_soc_dai_ops
;
819 snprintf(rdai
->playback
.name
, RSND_DAI_NAME_SIZE
,
820 "DAI%d Playback", dai_i
);
821 drv
->playback
.rates
= RSND_RATES
;
822 drv
->playback
.formats
= RSND_FMTS
;
823 drv
->playback
.channels_min
= 2;
824 drv
->playback
.channels_max
= 6;
825 drv
->playback
.stream_name
= rdai
->playback
.name
;
827 snprintf(rdai
->capture
.name
, RSND_DAI_NAME_SIZE
,
828 "DAI%d Capture", dai_i
);
829 drv
->capture
.rates
= RSND_RATES
;
830 drv
->capture
.formats
= RSND_FMTS
;
831 drv
->capture
.channels_min
= 2;
832 drv
->capture
.channels_max
= 6;
833 drv
->capture
.stream_name
= rdai
->capture
.name
;
835 rdai
->playback
.rdai
= rdai
;
836 rdai
->capture
.rdai
= rdai
;
837 rsnd_set_slot(rdai
, 2, 1); /* default */
839 for (io_i
= 0;; io_i
++) {
840 playback
= of_parse_phandle(dai_np
, "playback", io_i
);
841 capture
= of_parse_phandle(dai_np
, "capture", io_i
);
843 if (!playback
&& !capture
)
846 rsnd_parse_connect_ssi(rdai
, playback
, capture
);
847 rsnd_parse_connect_src(rdai
, playback
, capture
);
848 rsnd_parse_connect_ctu(rdai
, playback
, capture
);
849 rsnd_parse_connect_mix(rdai
, playback
, capture
);
850 rsnd_parse_connect_dvc(rdai
, playback
, capture
);
852 of_node_put(playback
);
853 of_node_put(capture
);
858 dev_dbg(dev
, "%s (%s/%s)\n", rdai
->name
,
859 rsnd_io_to_mod_ssi(io_playback
) ? "play" : " -- ",
860 rsnd_io_to_mod_ssi(io_capture
) ? "capture" : " -- ");
866 of_node_put(dai_node
);
874 static struct snd_pcm_hardware rsnd_pcm_hardware
= {
875 .info
= SNDRV_PCM_INFO_INTERLEAVED
|
876 SNDRV_PCM_INFO_MMAP
|
877 SNDRV_PCM_INFO_MMAP_VALID
,
878 .buffer_bytes_max
= 64 * 1024,
879 .period_bytes_min
= 32,
880 .period_bytes_max
= 8192,
886 static int rsnd_pcm_open(struct snd_pcm_substream
*substream
)
888 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
891 snd_soc_set_runtime_hwparams(substream
, &rsnd_pcm_hardware
);
893 ret
= snd_pcm_hw_constraint_integer(runtime
,
894 SNDRV_PCM_HW_PARAM_PERIODS
);
899 static int rsnd_hw_params(struct snd_pcm_substream
*substream
,
900 struct snd_pcm_hw_params
*hw_params
)
902 struct snd_soc_dai
*dai
= rsnd_substream_to_dai(substream
);
903 struct rsnd_dai
*rdai
= rsnd_dai_to_rdai(dai
);
904 struct rsnd_dai_stream
*io
= rsnd_rdai_to_io(rdai
, substream
);
907 ret
= rsnd_dai_call(hw_params
, io
, substream
, hw_params
);
911 return snd_pcm_lib_malloc_pages(substream
,
912 params_buffer_bytes(hw_params
));
915 static snd_pcm_uframes_t
rsnd_pointer(struct snd_pcm_substream
*substream
)
917 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
918 struct snd_soc_dai
*dai
= rsnd_substream_to_dai(substream
);
919 struct rsnd_dai
*rdai
= rsnd_dai_to_rdai(dai
);
920 struct rsnd_dai_stream
*io
= rsnd_rdai_to_io(rdai
, substream
);
922 return bytes_to_frames(runtime
, io
->byte_pos
);
925 static struct snd_pcm_ops rsnd_pcm_ops
= {
926 .open
= rsnd_pcm_open
,
927 .ioctl
= snd_pcm_lib_ioctl
,
928 .hw_params
= rsnd_hw_params
,
929 .hw_free
= snd_pcm_lib_free_pages
,
930 .pointer
= rsnd_pointer
,
936 #define kcontrol_to_cfg(kctrl) ((struct rsnd_kctrl_cfg *)kctrl->private_value)
937 static int rsnd_kctrl_info(struct snd_kcontrol
*kctrl
,
938 struct snd_ctl_elem_info
*uinfo
)
940 struct rsnd_kctrl_cfg
*cfg
= kcontrol_to_cfg(kctrl
);
943 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
944 uinfo
->count
= cfg
->size
;
945 uinfo
->value
.enumerated
.items
= cfg
->max
;
946 if (uinfo
->value
.enumerated
.item
>= cfg
->max
)
947 uinfo
->value
.enumerated
.item
= cfg
->max
- 1;
948 strlcpy(uinfo
->value
.enumerated
.name
,
949 cfg
->texts
[uinfo
->value
.enumerated
.item
],
950 sizeof(uinfo
->value
.enumerated
.name
));
952 uinfo
->count
= cfg
->size
;
953 uinfo
->value
.integer
.min
= 0;
954 uinfo
->value
.integer
.max
= cfg
->max
;
955 uinfo
->type
= (cfg
->max
== 1) ?
956 SNDRV_CTL_ELEM_TYPE_BOOLEAN
:
957 SNDRV_CTL_ELEM_TYPE_INTEGER
;
963 static int rsnd_kctrl_get(struct snd_kcontrol
*kctrl
,
964 struct snd_ctl_elem_value
*uc
)
966 struct rsnd_kctrl_cfg
*cfg
= kcontrol_to_cfg(kctrl
);
969 for (i
= 0; i
< cfg
->size
; i
++)
971 uc
->value
.enumerated
.item
[i
] = cfg
->val
[i
];
973 uc
->value
.integer
.value
[i
] = cfg
->val
[i
];
978 static int rsnd_kctrl_put(struct snd_kcontrol
*kctrl
,
979 struct snd_ctl_elem_value
*uc
)
981 struct rsnd_mod
*mod
= snd_kcontrol_chip(kctrl
);
982 struct rsnd_kctrl_cfg
*cfg
= kcontrol_to_cfg(kctrl
);
985 for (i
= 0; i
< cfg
->size
; i
++) {
987 change
|= (uc
->value
.enumerated
.item
[i
] != cfg
->val
[i
]);
988 cfg
->val
[i
] = uc
->value
.enumerated
.item
[i
];
990 change
|= (uc
->value
.integer
.value
[i
] != cfg
->val
[i
]);
991 cfg
->val
[i
] = uc
->value
.integer
.value
[i
];
995 if (change
&& cfg
->update
)
996 cfg
->update(cfg
->io
, mod
);
1001 struct rsnd_kctrl_cfg
*rsnd_kctrl_init_m(struct rsnd_kctrl_cfg_m
*cfg
)
1003 cfg
->cfg
.val
= cfg
->val
;
1008 struct rsnd_kctrl_cfg
*rsnd_kctrl_init_s(struct rsnd_kctrl_cfg_s
*cfg
)
1010 cfg
->cfg
.val
= &cfg
->val
;
1015 int rsnd_kctrl_new(struct rsnd_mod
*mod
,
1016 struct rsnd_dai_stream
*io
,
1017 struct snd_soc_pcm_runtime
*rtd
,
1018 const unsigned char *name
,
1019 void (*update
)(struct rsnd_dai_stream
*io
,
1020 struct rsnd_mod
*mod
),
1021 struct rsnd_kctrl_cfg
*cfg
,
1022 const char * const *texts
,
1026 struct snd_card
*card
= rtd
->card
->snd_card
;
1027 struct snd_kcontrol
*kctrl
;
1028 struct snd_kcontrol_new knew
= {
1029 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
1031 .info
= rsnd_kctrl_info
,
1033 .get
= rsnd_kctrl_get
,
1034 .put
= rsnd_kctrl_put
,
1035 .private_value
= (unsigned long)cfg
,
1039 if (size
> RSND_MAX_CHANNELS
)
1042 kctrl
= snd_ctl_new1(&knew
, mod
);
1046 ret
= snd_ctl_add(card
, kctrl
);
1053 cfg
->update
= update
;
1065 #define PREALLOC_BUFFER (32 * 1024)
1066 #define PREALLOC_BUFFER_MAX (32 * 1024)
1068 static int rsnd_pcm_new(struct snd_soc_pcm_runtime
*rtd
)
1070 struct snd_soc_dai
*dai
= rtd
->cpu_dai
;
1071 struct rsnd_dai
*rdai
= rsnd_dai_to_rdai(dai
);
1074 ret
= rsnd_dai_call(pcm_new
, &rdai
->playback
, rtd
);
1078 ret
= rsnd_dai_call(pcm_new
, &rdai
->capture
, rtd
);
1082 return snd_pcm_lib_preallocate_pages_for_all(
1084 SNDRV_DMA_TYPE_CONTINUOUS
,
1085 snd_dma_continuous_data(GFP_KERNEL
),
1086 PREALLOC_BUFFER
, PREALLOC_BUFFER_MAX
);
1089 static struct snd_soc_platform_driver rsnd_soc_platform
= {
1090 .ops
= &rsnd_pcm_ops
,
1091 .pcm_new
= rsnd_pcm_new
,
1094 static const struct snd_soc_component_driver rsnd_soc_component
= {
1098 static int rsnd_rdai_continuance_probe(struct rsnd_priv
*priv
,
1099 struct rsnd_dai_stream
*io
)
1103 ret
= rsnd_dai_call(probe
, io
, priv
);
1104 if (ret
== -EAGAIN
) {
1105 struct rsnd_mod
*ssi_mod
= rsnd_io_to_mod_ssi(io
);
1106 struct rsnd_mod
*mod
;
1110 * Fallback to PIO mode
1114 * call "remove" for SSI/SRC/DVC
1115 * SSI will be switch to PIO mode if it was DMA mode
1118 * rsnd_ssi_fallback()
1120 rsnd_dai_call(remove
, io
, priv
);
1123 * remove all mod from io
1124 * and, re connect ssi
1126 for_each_rsnd_mod(i
, mod
, io
)
1127 rsnd_dai_disconnect(mod
, io
, i
);
1128 rsnd_dai_connect(ssi_mod
, io
, RSND_MOD_SSI
);
1133 rsnd_dai_call(fallback
, io
, priv
);
1137 * DAI has SSI which is PIO mode only now.
1139 ret
= rsnd_dai_call(probe
, io
, priv
);
1148 static int rsnd_probe(struct platform_device
*pdev
)
1150 struct rsnd_priv
*priv
;
1151 struct device
*dev
= &pdev
->dev
;
1152 struct rsnd_dai
*rdai
;
1153 int (*probe_func
[])(struct rsnd_priv
*priv
) = {
1171 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
1173 dev_err(dev
, "priv allocate failed\n");
1178 priv
->flags
= (unsigned long)of_device_get_match_data(dev
);
1179 spin_lock_init(&priv
->lock
);
1184 for (i
= 0; i
< ARRAY_SIZE(probe_func
); i
++) {
1185 ret
= probe_func
[i
](priv
);
1190 for_each_rsnd_dai(rdai
, priv
, i
) {
1191 ret
= rsnd_rdai_continuance_probe(priv
, &rdai
->playback
);
1193 goto exit_snd_probe
;
1195 ret
= rsnd_rdai_continuance_probe(priv
, &rdai
->capture
);
1197 goto exit_snd_probe
;
1200 dev_set_drvdata(dev
, priv
);
1205 ret
= snd_soc_register_platform(dev
, &rsnd_soc_platform
);
1207 dev_err(dev
, "cannot snd soc register\n");
1211 ret
= snd_soc_register_component(dev
, &rsnd_soc_component
,
1212 priv
->daidrv
, rsnd_rdai_nr(priv
));
1214 dev_err(dev
, "cannot snd dai register\n");
1218 pm_runtime_enable(dev
);
1220 dev_info(dev
, "probed\n");
1224 snd_soc_unregister_platform(dev
);
1226 for_each_rsnd_dai(rdai
, priv
, i
) {
1227 rsnd_dai_call(remove
, &rdai
->playback
, priv
);
1228 rsnd_dai_call(remove
, &rdai
->capture
, priv
);
1234 static int rsnd_remove(struct platform_device
*pdev
)
1236 struct rsnd_priv
*priv
= dev_get_drvdata(&pdev
->dev
);
1237 struct rsnd_dai
*rdai
;
1238 void (*remove_func
[])(struct rsnd_priv
*priv
) = {
1250 pm_runtime_disable(&pdev
->dev
);
1252 for_each_rsnd_dai(rdai
, priv
, i
) {
1253 ret
|= rsnd_dai_call(remove
, &rdai
->playback
, priv
);
1254 ret
|= rsnd_dai_call(remove
, &rdai
->capture
, priv
);
1257 for (i
= 0; i
< ARRAY_SIZE(remove_func
); i
++)
1258 remove_func
[i
](priv
);
1260 snd_soc_unregister_component(&pdev
->dev
);
1261 snd_soc_unregister_platform(&pdev
->dev
);
1266 static int rsnd_suspend(struct device
*dev
)
1268 struct rsnd_priv
*priv
= dev_get_drvdata(dev
);
1270 rsnd_adg_clk_disable(priv
);
1275 static int rsnd_resume(struct device
*dev
)
1277 struct rsnd_priv
*priv
= dev_get_drvdata(dev
);
1279 rsnd_adg_clk_enable(priv
);
1284 static struct dev_pm_ops rsnd_pm_ops
= {
1285 .suspend
= rsnd_suspend
,
1286 .resume
= rsnd_resume
,
1289 static struct platform_driver rsnd_driver
= {
1291 .name
= "rcar_sound",
1293 .of_match_table
= rsnd_of_match
,
1295 .probe
= rsnd_probe
,
1296 .remove
= rsnd_remove
,
1298 module_platform_driver(rsnd_driver
);
1300 MODULE_LICENSE("GPL");
1301 MODULE_DESCRIPTION("Renesas R-Car audio driver");
1302 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
1303 MODULE_ALIAS("platform:rcar-pcm-audio");