2 * Renesas R-Car SRU/SCU/SSIU/SSI support
4 * Copyright (C) 2013 Renesas Solutions Corp.
5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
8 * Kuninori Morimoto <morimoto.kuninori@renesas.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
16 * Renesas R-Car sound device structure
20 * SRU : Sound Routing Unit
21 * - SRC : Sampling Rate Converter
23 * - CTU : Channel Count Conversion Unit
25 * - DVC : Digital Volume and Mute Function
26 * - SSI : Serial Sound Interface
30 * SCU : Sampling Rate Converter Unit
31 * - SRC : Sampling Rate Converter
33 * - CTU : Channel Count Conversion Unit
35 * - DVC : Digital Volume and Mute Function
36 * SSIU : Serial Sound Interface Unit
37 * - SSI : Serial Sound Interface
45 * | ** this depends on Gen1/Gen2
49 * | ** these depend on data path
50 * | ** gen and platform data control it
54 * | +- playback -> [mod] -> [mod] -> [mod] -> ...
57 * | +- capture -> [mod] -> [mod] -> [mod] -> ...
61 * | +- playback -> [mod] -> [mod] -> [mod] -> ...
64 * | +- capture -> [mod] -> [mod] -> [mod] -> ...
67 * | ** these control ssi
76 * | ** these control src
86 * for_each_rsnd_dai(xx, priv, xx)
87 * rdai[0] => rdai[1] => rdai[2] => ...
89 * for_each_rsnd_mod(xx, rdai, xx)
90 * [mod] => [mod] => [mod] => ...
92 * rsnd_dai_call(xxx, fn )
93 * [mod]->fn() -> [mod]->fn() -> [mod]->fn()...
96 #include <linux/pm_runtime.h>
99 #define RSND_RATES SNDRV_PCM_RATE_8000_96000
100 #define RSND_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
102 static const struct rsnd_of_data rsnd_of_data_gen1
= {
106 static const struct rsnd_of_data rsnd_of_data_gen2
= {
110 static const struct of_device_id rsnd_of_match
[] = {
111 { .compatible
= "renesas,rcar_sound-gen1", .data
= &rsnd_of_data_gen1
},
112 { .compatible
= "renesas,rcar_sound-gen2", .data
= &rsnd_of_data_gen2
},
115 MODULE_DEVICE_TABLE(of
, rsnd_of_match
);
118 * rsnd_platform functions
120 #define rsnd_platform_call(priv, dai, func, param...) \
121 (!(priv->info->func) ? 0 : \
122 priv->info->func(param))
124 #define rsnd_is_enable_path(io, name) \
125 ((io)->info ? (io)->info->name : NULL)
126 #define rsnd_info_id(priv, io, name) \
127 ((io)->info->name - priv->info->name##_info)
132 char *rsnd_mod_name(struct rsnd_mod
*mod
)
134 if (!mod
|| !mod
->ops
)
137 return mod
->ops
->name
;
140 struct dma_chan
*rsnd_mod_dma_req(struct rsnd_dai_stream
*io
,
141 struct rsnd_mod
*mod
)
143 if (!mod
|| !mod
->ops
|| !mod
->ops
->dma_req
)
146 return mod
->ops
->dma_req(io
, mod
);
149 int rsnd_mod_init(struct rsnd_priv
*priv
,
150 struct rsnd_mod
*mod
,
151 struct rsnd_mod_ops
*ops
,
153 enum rsnd_mod_type type
,
156 int ret
= clk_prepare(clk
);
170 void rsnd_mod_quit(struct rsnd_mod
*mod
)
173 clk_unprepare(mod
->clk
);
176 void rsnd_mod_interrupt(struct rsnd_mod
*mod
,
177 void (*callback
)(struct rsnd_mod
*mod
,
178 struct rsnd_dai_stream
*io
))
180 struct rsnd_priv
*priv
= rsnd_mod_to_priv(mod
);
181 struct rsnd_dai_stream
*io
;
182 struct rsnd_dai
*rdai
;
185 for_each_rsnd_dai(rdai
, priv
, j
) {
187 for (i
= 0; i
< RSND_MOD_MAX
; i
++) {
188 io
= &rdai
->playback
;
189 if (mod
== io
->mod
[i
])
193 if (mod
== io
->mod
[i
])
199 int rsnd_io_is_working(struct rsnd_dai_stream
*io
)
201 /* see rsnd_dai_stream_init/quit() */
202 return !!io
->substream
;
208 u32
rsnd_get_adinr_bit(struct rsnd_mod
*mod
, struct rsnd_dai_stream
*io
)
210 struct rsnd_priv
*priv
= rsnd_mod_to_priv(mod
);
211 struct snd_pcm_runtime
*runtime
= rsnd_io_to_runtime(io
);
212 struct device
*dev
= rsnd_priv_to_dev(priv
);
213 u32 adinr
= runtime
->channels
;
215 switch (runtime
->sample_bits
) {
223 dev_warn(dev
, "not supported sample bits\n");
233 #define __rsnd_mod_call(mod, io, func, param...) \
235 struct rsnd_priv *priv = rsnd_mod_to_priv(mod); \
236 struct device *dev = rsnd_priv_to_dev(priv); \
237 u32 mask = 0xF << __rsnd_mod_shift_##func; \
238 u8 val = (mod->status >> __rsnd_mod_shift_##func) & 0xF; \
239 u8 add = ((val + __rsnd_mod_add_##func) & 0xF); \
242 if (val == __rsnd_mod_call_##func) { \
244 ret = (mod)->ops->func(mod, io, param); \
246 mod->status = (mod->status & ~mask) + \
247 (add << __rsnd_mod_shift_##func); \
248 dev_dbg(dev, "%s[%d] 0x%08x %s\n", \
249 rsnd_mod_name(mod), rsnd_mod_id(mod), mod->status, \
250 called ? #func : ""); \
254 #define rsnd_mod_call(mod, io, func, param...) \
255 (!(mod) ? -ENODEV : \
256 !((mod)->ops->func) ? 0 : \
257 __rsnd_mod_call(mod, io, func, param))
259 #define rsnd_dai_call(fn, io, param...) \
261 struct rsnd_mod *mod; \
263 for (i = 0; i < RSND_MOD_MAX; i++) { \
264 mod = (io)->mod[i]; \
267 ret = rsnd_mod_call(mod, io, fn, param); \
274 static int rsnd_dai_connect(struct rsnd_mod
*mod
,
275 struct rsnd_dai_stream
*io
)
280 io
->mod
[mod
->type
] = mod
;
285 static void rsnd_dai_disconnect(struct rsnd_mod
*mod
,
286 struct rsnd_dai_stream
*io
)
288 io
->mod
[mod
->type
] = NULL
;
291 struct rsnd_dai
*rsnd_rdai_get(struct rsnd_priv
*priv
, int id
)
293 if ((id
< 0) || (id
>= rsnd_rdai_nr(priv
)))
296 return priv
->rdai
+ id
;
299 #define rsnd_dai_to_priv(dai) snd_soc_dai_get_drvdata(dai)
300 static struct rsnd_dai
*rsnd_dai_to_rdai(struct snd_soc_dai
*dai
)
302 struct rsnd_priv
*priv
= rsnd_dai_to_priv(dai
);
304 return rsnd_rdai_get(priv
, dai
->id
);
308 * rsnd_soc_dai functions
310 int rsnd_dai_pointer_offset(struct rsnd_dai_stream
*io
, int additional
)
312 struct snd_pcm_substream
*substream
= io
->substream
;
313 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
314 int pos
= io
->byte_pos
+ additional
;
316 pos
%= (runtime
->periods
* io
->byte_per_period
);
321 bool rsnd_dai_pointer_update(struct rsnd_dai_stream
*io
, int byte
)
323 io
->byte_pos
+= byte
;
325 if (io
->byte_pos
>= io
->next_period_byte
) {
326 struct snd_pcm_substream
*substream
= io
->substream
;
327 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
330 io
->next_period_byte
+= io
->byte_per_period
;
332 if (io
->period_pos
>= runtime
->periods
) {
335 io
->next_period_byte
= io
->byte_per_period
;
344 void rsnd_dai_period_elapsed(struct rsnd_dai_stream
*io
)
346 struct snd_pcm_substream
*substream
= io
->substream
;
349 * this function should be called...
351 * - if rsnd_dai_pointer_update() returns true
352 * - without spin lock
355 snd_pcm_period_elapsed(substream
);
358 static void rsnd_dai_stream_init(struct rsnd_dai_stream
*io
,
359 struct snd_pcm_substream
*substream
)
361 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
363 io
->substream
= substream
;
366 io
->byte_per_period
= runtime
->period_size
*
368 samples_to_bytes(runtime
, 1);
369 io
->next_period_byte
= io
->byte_per_period
;
372 static void rsnd_dai_stream_quit(struct rsnd_dai_stream
*io
)
374 io
->substream
= NULL
;
378 struct snd_soc_dai
*rsnd_substream_to_dai(struct snd_pcm_substream
*substream
)
380 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
386 struct rsnd_dai_stream
*rsnd_rdai_to_io(struct rsnd_dai
*rdai
,
387 struct snd_pcm_substream
*substream
)
389 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
)
390 return &rdai
->playback
;
392 return &rdai
->capture
;
395 static int rsnd_soc_dai_trigger(struct snd_pcm_substream
*substream
, int cmd
,
396 struct snd_soc_dai
*dai
)
398 struct rsnd_priv
*priv
= rsnd_dai_to_priv(dai
);
399 struct rsnd_dai
*rdai
= rsnd_dai_to_rdai(dai
);
400 struct rsnd_dai_stream
*io
= rsnd_rdai_to_io(rdai
, substream
);
401 int ssi_id
= rsnd_mod_id(rsnd_io_to_mod_ssi(io
));
405 spin_lock_irqsave(&priv
->lock
, flags
);
408 case SNDRV_PCM_TRIGGER_START
:
409 rsnd_dai_stream_init(io
, substream
);
411 ret
= rsnd_platform_call(priv
, dai
, start
, ssi_id
);
413 goto dai_trigger_end
;
415 ret
= rsnd_dai_call(init
, io
, priv
);
417 goto dai_trigger_end
;
419 ret
= rsnd_dai_call(start
, io
, priv
);
421 goto dai_trigger_end
;
423 case SNDRV_PCM_TRIGGER_STOP
:
424 ret
= rsnd_dai_call(stop
, io
, priv
);
426 goto dai_trigger_end
;
428 ret
= rsnd_dai_call(quit
, io
, priv
);
430 goto dai_trigger_end
;
432 ret
= rsnd_platform_call(priv
, dai
, stop
, ssi_id
);
434 goto dai_trigger_end
;
436 rsnd_dai_stream_quit(io
);
443 spin_unlock_irqrestore(&priv
->lock
, flags
);
448 static int rsnd_soc_dai_set_fmt(struct snd_soc_dai
*dai
, unsigned int fmt
)
450 struct rsnd_dai
*rdai
= rsnd_dai_to_rdai(dai
);
452 /* set master/slave audio interface */
453 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
454 case SND_SOC_DAIFMT_CBM_CFM
:
455 rdai
->clk_master
= 0;
457 case SND_SOC_DAIFMT_CBS_CFS
:
458 rdai
->clk_master
= 1; /* codec is slave, cpu is master */
465 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
466 case SND_SOC_DAIFMT_I2S
:
468 rdai
->data_alignment
= 0;
469 rdai
->frm_clk_inv
= 0;
471 case SND_SOC_DAIFMT_LEFT_J
:
473 rdai
->data_alignment
= 0;
474 rdai
->frm_clk_inv
= 1;
476 case SND_SOC_DAIFMT_RIGHT_J
:
478 rdai
->data_alignment
= 1;
479 rdai
->frm_clk_inv
= 1;
483 /* set clock inversion */
484 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
485 case SND_SOC_DAIFMT_NB_IF
:
486 rdai
->bit_clk_inv
= rdai
->bit_clk_inv
;
487 rdai
->frm_clk_inv
= !rdai
->frm_clk_inv
;
489 case SND_SOC_DAIFMT_IB_NF
:
490 rdai
->bit_clk_inv
= !rdai
->bit_clk_inv
;
491 rdai
->frm_clk_inv
= rdai
->frm_clk_inv
;
493 case SND_SOC_DAIFMT_IB_IF
:
494 rdai
->bit_clk_inv
= !rdai
->bit_clk_inv
;
495 rdai
->frm_clk_inv
= !rdai
->frm_clk_inv
;
497 case SND_SOC_DAIFMT_NB_NF
:
505 static const struct snd_soc_dai_ops rsnd_soc_dai_ops
= {
506 .trigger
= rsnd_soc_dai_trigger
,
507 .set_fmt
= rsnd_soc_dai_set_fmt
,
510 #define rsnd_path_add(priv, io, type) \
512 struct rsnd_mod *mod; \
516 if (rsnd_is_enable_path(io, type)) { \
517 id = rsnd_info_id(priv, io, type); \
519 mod = rsnd_##type##_mod_get(priv, id); \
520 ret = rsnd_dai_connect(mod, io); \
526 #define rsnd_path_remove(priv, io, type) \
528 struct rsnd_mod *mod; \
531 if (rsnd_is_enable_path(io, type)) { \
532 id = rsnd_info_id(priv, io, type); \
534 mod = rsnd_##type##_mod_get(priv, id); \
535 rsnd_dai_disconnect(mod, io); \
540 void rsnd_path_parse(struct rsnd_priv
*priv
,
541 struct rsnd_dai_stream
*io
)
543 struct rsnd_mod
*src
= rsnd_io_to_mod_src(io
);
544 struct rsnd_mod
*dvc
= rsnd_io_to_mod_dvc(io
);
545 int src_id
= rsnd_mod_id(src
);
555 /* Gen1 is not supported */
556 if (rsnd_is_gen1(priv
))
559 rsnd_mod_write(dvc
, CMD_ROUTE_SLCT
, path
[src_id
]);
562 static int rsnd_path_init(struct rsnd_priv
*priv
,
563 struct rsnd_dai
*rdai
,
564 struct rsnd_dai_stream
*io
)
569 * Gen1 is created by SRU/SSI, and this SRU is base module of
570 * Gen2's SCU/SSIU/SSI. (Gen2 SCU/SSIU came from SRU)
573 * Gen1 SRU = Gen2 SCU + SSIU + etc
575 * Gen2 SCU path is very flexible, but, Gen1 SRU (SCU parts) is
580 ret
= rsnd_path_add(priv
, io
, src
);
585 ret
= rsnd_path_add(priv
, io
, ssi
);
590 ret
= rsnd_path_add(priv
, io
, dvc
);
597 static void rsnd_of_parse_dai(struct platform_device
*pdev
,
598 const struct rsnd_of_data
*of_data
,
599 struct rsnd_priv
*priv
)
601 struct device_node
*dai_node
, *dai_np
;
602 struct device_node
*ssi_node
, *ssi_np
;
603 struct device_node
*src_node
, *src_np
;
604 struct device_node
*dvc_node
, *dvc_np
;
605 struct device_node
*playback
, *capture
;
606 struct rsnd_dai_platform_info
*dai_info
;
607 struct rcar_snd_info
*info
= rsnd_priv_to_info(priv
);
608 struct device
*dev
= &pdev
->dev
;
610 int dai_i
, ssi_i
, src_i
, dvc_i
;
615 dai_node
= of_get_child_by_name(dev
->of_node
, "rcar_sound,dai");
619 nr
= of_get_child_count(dai_node
);
623 dai_info
= devm_kzalloc(dev
,
624 sizeof(struct rsnd_dai_platform_info
) * nr
,
627 dev_err(dev
, "dai info allocation error\n");
631 info
->dai_info_nr
= nr
;
632 info
->dai_info
= dai_info
;
634 ssi_node
= of_get_child_by_name(dev
->of_node
, "rcar_sound,ssi");
635 src_node
= of_get_child_by_name(dev
->of_node
, "rcar_sound,src");
636 dvc_node
= of_get_child_by_name(dev
->of_node
, "rcar_sound,dvc");
638 #define mod_parse(name) \
640 struct rsnd_##name##_platform_info *name##_info; \
643 for_each_child_of_node(name##_node, name##_np) { \
644 name##_info = info->name##_info + name##_i; \
646 if (name##_np == playback) \
647 dai_info->playback.name = name##_info; \
648 if (name##_np == capture) \
649 dai_info->capture.name = name##_info; \
659 for_each_child_of_node(dai_node
, dai_np
) {
660 dai_info
= info
->dai_info
+ dai_i
;
664 playback
= of_parse_phandle(dai_np
, "playback", i
);
665 capture
= of_parse_phandle(dai_np
, "capture", i
);
667 if (!playback
&& !capture
)
674 of_node_put(playback
);
675 of_node_put(capture
);
682 static int rsnd_dai_probe(struct platform_device
*pdev
,
683 const struct rsnd_of_data
*of_data
,
684 struct rsnd_priv
*priv
)
686 struct snd_soc_dai_driver
*drv
;
687 struct rcar_snd_info
*info
= rsnd_priv_to_info(priv
);
688 struct rsnd_dai
*rdai
;
689 struct rsnd_ssi_platform_info
*pmod
, *cmod
;
690 struct device
*dev
= rsnd_priv_to_dev(priv
);
694 rsnd_of_parse_dai(pdev
, of_data
, priv
);
696 dai_nr
= info
->dai_info_nr
;
698 dev_err(dev
, "no dai\n");
702 drv
= devm_kzalloc(dev
, sizeof(*drv
) * dai_nr
, GFP_KERNEL
);
703 rdai
= devm_kzalloc(dev
, sizeof(*rdai
) * dai_nr
, GFP_KERNEL
);
705 dev_err(dev
, "dai allocate failed\n");
709 priv
->rdai_nr
= dai_nr
;
713 for (i
= 0; i
< dai_nr
; i
++) {
715 pmod
= info
->dai_info
[i
].playback
.ssi
;
716 cmod
= info
->dai_info
[i
].capture
.ssi
;
721 snprintf(rdai
[i
].name
, RSND_DAI_NAME_SIZE
, "rsnd-dai.%d", i
);
725 * init snd_soc_dai_driver
727 drv
[i
].name
= rdai
[i
].name
;
728 drv
[i
].ops
= &rsnd_soc_dai_ops
;
730 snprintf(rdai
[i
].playback
.name
, RSND_DAI_NAME_SIZE
,
731 "DAI%d Playback", i
);
733 drv
[i
].playback
.rates
= RSND_RATES
;
734 drv
[i
].playback
.formats
= RSND_FMTS
;
735 drv
[i
].playback
.channels_min
= 2;
736 drv
[i
].playback
.channels_max
= 2;
737 drv
[i
].playback
.stream_name
= rdai
[i
].playback
.name
;
739 rdai
[i
].playback
.info
= &info
->dai_info
[i
].playback
;
740 rdai
[i
].playback
.rdai
= rdai
+ i
;
741 rsnd_path_init(priv
, &rdai
[i
], &rdai
[i
].playback
);
744 snprintf(rdai
[i
].capture
.name
, RSND_DAI_NAME_SIZE
,
747 drv
[i
].capture
.rates
= RSND_RATES
;
748 drv
[i
].capture
.formats
= RSND_FMTS
;
749 drv
[i
].capture
.channels_min
= 2;
750 drv
[i
].capture
.channels_max
= 2;
751 drv
[i
].capture
.stream_name
= rdai
[i
].capture
.name
;
753 rdai
[i
].capture
.info
= &info
->dai_info
[i
].capture
;
754 rdai
[i
].capture
.rdai
= rdai
+ i
;
755 rsnd_path_init(priv
, &rdai
[i
], &rdai
[i
].capture
);
758 dev_dbg(dev
, "%s (%s/%s)\n", rdai
[i
].name
,
759 pmod
? "play" : " -- ",
760 cmod
? "capture" : " -- ");
769 static struct snd_pcm_hardware rsnd_pcm_hardware
= {
770 .info
= SNDRV_PCM_INFO_INTERLEAVED
|
771 SNDRV_PCM_INFO_MMAP
|
772 SNDRV_PCM_INFO_MMAP_VALID
,
773 .buffer_bytes_max
= 64 * 1024,
774 .period_bytes_min
= 32,
775 .period_bytes_max
= 8192,
781 static int rsnd_pcm_open(struct snd_pcm_substream
*substream
)
783 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
786 snd_soc_set_runtime_hwparams(substream
, &rsnd_pcm_hardware
);
788 ret
= snd_pcm_hw_constraint_integer(runtime
,
789 SNDRV_PCM_HW_PARAM_PERIODS
);
794 static int rsnd_hw_params(struct snd_pcm_substream
*substream
,
795 struct snd_pcm_hw_params
*hw_params
)
797 struct snd_soc_dai
*dai
= rsnd_substream_to_dai(substream
);
798 struct rsnd_dai
*rdai
= rsnd_dai_to_rdai(dai
);
799 struct rsnd_dai_stream
*io
= rsnd_rdai_to_io(rdai
, substream
);
802 ret
= rsnd_dai_call(hw_params
, io
, substream
, hw_params
);
806 return snd_pcm_lib_malloc_pages(substream
,
807 params_buffer_bytes(hw_params
));
810 static snd_pcm_uframes_t
rsnd_pointer(struct snd_pcm_substream
*substream
)
812 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
813 struct snd_soc_dai
*dai
= rsnd_substream_to_dai(substream
);
814 struct rsnd_dai
*rdai
= rsnd_dai_to_rdai(dai
);
815 struct rsnd_dai_stream
*io
= rsnd_rdai_to_io(rdai
, substream
);
817 return bytes_to_frames(runtime
, io
->byte_pos
);
820 static struct snd_pcm_ops rsnd_pcm_ops
= {
821 .open
= rsnd_pcm_open
,
822 .ioctl
= snd_pcm_lib_ioctl
,
823 .hw_params
= rsnd_hw_params
,
824 .hw_free
= snd_pcm_lib_free_pages
,
825 .pointer
= rsnd_pointer
,
831 #define kcontrol_to_cfg(kctrl) ((struct rsnd_kctrl_cfg *)kctrl->private_value)
832 static int rsnd_kctrl_info(struct snd_kcontrol
*kctrl
,
833 struct snd_ctl_elem_info
*uinfo
)
835 struct rsnd_kctrl_cfg
*cfg
= kcontrol_to_cfg(kctrl
);
838 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
839 uinfo
->count
= cfg
->size
;
840 uinfo
->value
.enumerated
.items
= cfg
->max
;
841 if (uinfo
->value
.enumerated
.item
>= cfg
->max
)
842 uinfo
->value
.enumerated
.item
= cfg
->max
- 1;
843 strlcpy(uinfo
->value
.enumerated
.name
,
844 cfg
->texts
[uinfo
->value
.enumerated
.item
],
845 sizeof(uinfo
->value
.enumerated
.name
));
847 uinfo
->count
= cfg
->size
;
848 uinfo
->value
.integer
.min
= 0;
849 uinfo
->value
.integer
.max
= cfg
->max
;
850 uinfo
->type
= (cfg
->max
== 1) ?
851 SNDRV_CTL_ELEM_TYPE_BOOLEAN
:
852 SNDRV_CTL_ELEM_TYPE_INTEGER
;
858 static int rsnd_kctrl_get(struct snd_kcontrol
*kctrl
,
859 struct snd_ctl_elem_value
*uc
)
861 struct rsnd_kctrl_cfg
*cfg
= kcontrol_to_cfg(kctrl
);
864 for (i
= 0; i
< cfg
->size
; i
++)
866 uc
->value
.enumerated
.item
[i
] = cfg
->val
[i
];
868 uc
->value
.integer
.value
[i
] = cfg
->val
[i
];
873 static int rsnd_kctrl_put(struct snd_kcontrol
*kctrl
,
874 struct snd_ctl_elem_value
*uc
)
876 struct rsnd_mod
*mod
= snd_kcontrol_chip(kctrl
);
877 struct rsnd_kctrl_cfg
*cfg
= kcontrol_to_cfg(kctrl
);
880 for (i
= 0; i
< cfg
->size
; i
++) {
882 change
|= (uc
->value
.enumerated
.item
[i
] != cfg
->val
[i
]);
883 cfg
->val
[i
] = uc
->value
.enumerated
.item
[i
];
885 change
|= (uc
->value
.integer
.value
[i
] != cfg
->val
[i
]);
886 cfg
->val
[i
] = uc
->value
.integer
.value
[i
];
891 cfg
->update(cfg
->io
, mod
);
896 static int __rsnd_kctrl_new(struct rsnd_mod
*mod
,
897 struct rsnd_dai_stream
*io
,
898 struct snd_soc_pcm_runtime
*rtd
,
899 const unsigned char *name
,
900 struct rsnd_kctrl_cfg
*cfg
,
901 void (*update
)(struct rsnd_dai_stream
*io
,
902 struct rsnd_mod
*mod
))
904 struct snd_soc_card
*soc_card
= rtd
->card
;
905 struct snd_card
*card
= rtd
->card
->snd_card
;
906 struct snd_kcontrol
*kctrl
;
907 struct snd_kcontrol_new knew
= {
908 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
910 .info
= rsnd_kctrl_info
,
911 .index
= rtd
- soc_card
->rtd
,
912 .get
= rsnd_kctrl_get
,
913 .put
= rsnd_kctrl_put
,
914 .private_value
= (unsigned long)cfg
,
918 kctrl
= snd_ctl_new1(&knew
, mod
);
922 ret
= snd_ctl_add(card
, kctrl
);
924 snd_ctl_free_one(kctrl
);
928 cfg
->update
= update
;
936 void _rsnd_kctrl_remove(struct rsnd_kctrl_cfg
*cfg
)
938 snd_ctl_remove(cfg
->card
, cfg
->kctrl
);
941 int rsnd_kctrl_new_m(struct rsnd_mod
*mod
,
942 struct rsnd_dai_stream
*io
,
943 struct snd_soc_pcm_runtime
*rtd
,
944 const unsigned char *name
,
945 void (*update
)(struct rsnd_dai_stream
*io
,
946 struct rsnd_mod
*mod
),
947 struct rsnd_kctrl_cfg_m
*_cfg
,
951 _cfg
->cfg
.size
= RSND_DVC_CHANNELS
;
952 _cfg
->cfg
.val
= _cfg
->val
;
953 return __rsnd_kctrl_new(mod
, io
, rtd
, name
, &_cfg
->cfg
, update
);
956 int rsnd_kctrl_new_s(struct rsnd_mod
*mod
,
957 struct rsnd_dai_stream
*io
,
958 struct snd_soc_pcm_runtime
*rtd
,
959 const unsigned char *name
,
960 void (*update
)(struct rsnd_dai_stream
*io
,
961 struct rsnd_mod
*mod
),
962 struct rsnd_kctrl_cfg_s
*_cfg
,
967 _cfg
->cfg
.val
= &_cfg
->val
;
968 return __rsnd_kctrl_new(mod
, io
, rtd
, name
, &_cfg
->cfg
, update
);
971 int rsnd_kctrl_new_e(struct rsnd_mod
*mod
,
972 struct rsnd_dai_stream
*io
,
973 struct snd_soc_pcm_runtime
*rtd
,
974 const unsigned char *name
,
975 struct rsnd_kctrl_cfg_s
*_cfg
,
976 void (*update
)(struct rsnd_dai_stream
*io
,
977 struct rsnd_mod
*mod
),
978 const char * const *texts
,
983 _cfg
->cfg
.val
= &_cfg
->val
;
984 _cfg
->cfg
.texts
= texts
;
985 return __rsnd_kctrl_new(mod
, io
, rtd
, name
, &_cfg
->cfg
, update
);
992 #define PREALLOC_BUFFER (32 * 1024)
993 #define PREALLOC_BUFFER_MAX (32 * 1024)
995 static int rsnd_pcm_new(struct snd_soc_pcm_runtime
*rtd
)
997 struct snd_soc_dai
*dai
= rtd
->cpu_dai
;
998 struct rsnd_dai
*rdai
= rsnd_dai_to_rdai(dai
);
1001 ret
= rsnd_dai_call(pcm_new
, &rdai
->playback
, rtd
);
1005 ret
= rsnd_dai_call(pcm_new
, &rdai
->capture
, rtd
);
1009 return snd_pcm_lib_preallocate_pages_for_all(
1012 rtd
->card
->snd_card
->dev
,
1013 PREALLOC_BUFFER
, PREALLOC_BUFFER_MAX
);
1016 static struct snd_soc_platform_driver rsnd_soc_platform
= {
1017 .ops
= &rsnd_pcm_ops
,
1018 .pcm_new
= rsnd_pcm_new
,
1021 static const struct snd_soc_component_driver rsnd_soc_component
= {
1025 static int rsnd_rdai_continuance_probe(struct rsnd_priv
*priv
,
1026 struct rsnd_dai_stream
*io
)
1030 ret
= rsnd_dai_call(probe
, io
, priv
);
1031 if (ret
== -EAGAIN
) {
1033 * Fallback to PIO mode
1037 * call "remove" for SSI/SRC/DVC
1038 * SSI will be switch to PIO mode if it was DMA mode
1041 * rsnd_ssi_fallback()
1043 rsnd_dai_call(remove
, io
, priv
);
1046 * remove SRC/DVC from DAI,
1048 rsnd_path_remove(priv
, io
, src
);
1049 rsnd_path_remove(priv
, io
, dvc
);
1054 rsnd_dai_call(fallback
, io
, priv
);
1058 * DAI has SSI which is PIO mode only now.
1060 ret
= rsnd_dai_call(probe
, io
, priv
);
1069 static int rsnd_probe(struct platform_device
*pdev
)
1071 struct rcar_snd_info
*info
;
1072 struct rsnd_priv
*priv
;
1073 struct device
*dev
= &pdev
->dev
;
1074 struct rsnd_dai
*rdai
;
1075 const struct of_device_id
*of_id
= of_match_device(rsnd_of_match
, dev
);
1076 const struct rsnd_of_data
*of_data
;
1077 int (*probe_func
[])(struct platform_device
*pdev
,
1078 const struct rsnd_of_data
*of_data
,
1079 struct rsnd_priv
*priv
) = {
1093 info
= devm_kzalloc(&pdev
->dev
,
1094 sizeof(struct rcar_snd_info
), GFP_KERNEL
);
1095 of_data
= of_id
->data
;
1097 info
= pdev
->dev
.platform_data
;
1101 dev_err(dev
, "driver needs R-Car sound information\n");
1108 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
1110 dev_err(dev
, "priv allocate failed\n");
1116 spin_lock_init(&priv
->lock
);
1121 for (i
= 0; i
< ARRAY_SIZE(probe_func
); i
++) {
1122 ret
= probe_func
[i
](pdev
, of_data
, priv
);
1127 for_each_rsnd_dai(rdai
, priv
, i
) {
1128 ret
= rsnd_rdai_continuance_probe(priv
, &rdai
->playback
);
1130 goto exit_snd_probe
;
1132 ret
= rsnd_rdai_continuance_probe(priv
, &rdai
->capture
);
1134 goto exit_snd_probe
;
1137 dev_set_drvdata(dev
, priv
);
1142 ret
= snd_soc_register_platform(dev
, &rsnd_soc_platform
);
1144 dev_err(dev
, "cannot snd soc register\n");
1148 ret
= snd_soc_register_component(dev
, &rsnd_soc_component
,
1149 priv
->daidrv
, rsnd_rdai_nr(priv
));
1151 dev_err(dev
, "cannot snd dai register\n");
1155 pm_runtime_enable(dev
);
1157 dev_info(dev
, "probed\n");
1161 snd_soc_unregister_platform(dev
);
1163 for_each_rsnd_dai(rdai
, priv
, i
) {
1164 rsnd_dai_call(remove
, &rdai
->playback
, priv
);
1165 rsnd_dai_call(remove
, &rdai
->capture
, priv
);
1171 static int rsnd_remove(struct platform_device
*pdev
)
1173 struct rsnd_priv
*priv
= dev_get_drvdata(&pdev
->dev
);
1174 struct rsnd_dai
*rdai
;
1175 void (*remove_func
[])(struct platform_device
*pdev
,
1176 struct rsnd_priv
*priv
) = {
1183 pm_runtime_disable(&pdev
->dev
);
1185 for_each_rsnd_dai(rdai
, priv
, i
) {
1186 ret
|= rsnd_dai_call(remove
, &rdai
->playback
, priv
);
1187 ret
|= rsnd_dai_call(remove
, &rdai
->capture
, priv
);
1190 for (i
= 0; i
< ARRAY_SIZE(remove_func
); i
++)
1191 remove_func
[i
](pdev
, priv
);
1193 snd_soc_unregister_component(&pdev
->dev
);
1194 snd_soc_unregister_platform(&pdev
->dev
);
1199 static struct platform_driver rsnd_driver
= {
1201 .name
= "rcar_sound",
1202 .of_match_table
= rsnd_of_match
,
1204 .probe
= rsnd_probe
,
1205 .remove
= rsnd_remove
,
1207 module_platform_driver(rsnd_driver
);
1209 MODULE_LICENSE("GPL");
1210 MODULE_DESCRIPTION("Renesas R-Car audio driver");
1211 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
1212 MODULE_ALIAS("platform:rcar-pcm-audio");