1 // SPDX-License-Identifier: GPL-2.0-only
3 * dmic.c -- SoC audio for Generic Digital MICs
8 #include <linux/delay.h>
9 #include <linux/gpio.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/platform_device.h>
12 #include <linux/slab.h>
13 #include <linux/module.h>
14 #include <sound/core.h>
15 #include <sound/pcm.h>
16 #include <sound/soc.h>
17 #include <sound/soc-dapm.h>
19 #define MAX_MODESWITCH_DELAY 70
20 static int modeswitch_delay;
21 module_param(modeswitch_delay, uint, 0644);
23 static int wakeup_delay;
24 module_param(wakeup_delay, uint, 0644);
27 struct gpio_desc *gpio_en;
29 /* Delay after DMIC mode switch */
33 static int dmic_daiops_trigger(struct snd_pcm_substream *substream,
34 int cmd, struct snd_soc_dai *dai)
36 struct snd_soc_component *component = dai->component;
37 struct dmic *dmic = snd_soc_component_get_drvdata(component);
40 case SNDRV_PCM_TRIGGER_STOP:
41 if (dmic->modeswitch_delay)
42 mdelay(dmic->modeswitch_delay);
50 static const struct snd_soc_dai_ops dmic_dai_ops = {
51 .trigger = dmic_daiops_trigger,
54 static int dmic_aif_event(struct snd_soc_dapm_widget *w,
55 struct snd_kcontrol *kcontrol, int event) {
56 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
57 struct dmic *dmic = snd_soc_component_get_drvdata(component);
60 case SND_SOC_DAPM_POST_PMU:
62 gpiod_set_value_cansleep(dmic->gpio_en, 1);
64 if (dmic->wakeup_delay)
65 msleep(dmic->wakeup_delay);
67 case SND_SOC_DAPM_POST_PMD:
69 gpiod_set_value_cansleep(dmic->gpio_en, 0);
76 static struct snd_soc_dai_driver dmic_dai = {
79 .stream_name = "Capture",
82 .rates = SNDRV_PCM_RATE_CONTINUOUS,
83 .formats = SNDRV_PCM_FMTBIT_S32_LE
84 | SNDRV_PCM_FMTBIT_S24_LE
85 | SNDRV_PCM_FMTBIT_S16_LE
86 | SNDRV_PCM_FMTBIT_DSD_U8
87 | SNDRV_PCM_FMTBIT_DSD_U16_LE
88 | SNDRV_PCM_FMTBIT_DSD_U32_LE,
93 static int dmic_component_probe(struct snd_soc_component *component)
97 dmic = devm_kzalloc(component->dev, sizeof(*dmic), GFP_KERNEL);
101 dmic->gpio_en = devm_gpiod_get_optional(component->dev,
102 "dmicen", GPIOD_OUT_LOW);
103 if (IS_ERR(dmic->gpio_en))
104 return PTR_ERR(dmic->gpio_en);
106 device_property_read_u32(component->dev, "wakeup-delay-ms",
107 &dmic->wakeup_delay);
108 device_property_read_u32(component->dev, "modeswitch-delay-ms",
109 &dmic->modeswitch_delay);
111 dmic->wakeup_delay = wakeup_delay;
112 if (modeswitch_delay)
113 dmic->modeswitch_delay = modeswitch_delay;
115 if (dmic->modeswitch_delay > MAX_MODESWITCH_DELAY)
116 dmic->modeswitch_delay = MAX_MODESWITCH_DELAY;
118 snd_soc_component_set_drvdata(component, dmic);
123 static const struct snd_soc_dapm_widget dmic_dapm_widgets[] = {
124 SND_SOC_DAPM_AIF_OUT_E("DMIC AIF", "Capture", 0,
125 SND_SOC_NOPM, 0, 0, dmic_aif_event,
126 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
127 SND_SOC_DAPM_INPUT("DMic"),
130 static const struct snd_soc_dapm_route intercon[] = {
131 {"DMIC AIF", NULL, "DMic"},
134 static const struct snd_soc_component_driver soc_dmic = {
135 .probe = dmic_component_probe,
136 .dapm_widgets = dmic_dapm_widgets,
137 .num_dapm_widgets = ARRAY_SIZE(dmic_dapm_widgets),
138 .dapm_routes = intercon,
139 .num_dapm_routes = ARRAY_SIZE(intercon),
141 .use_pmdown_time = 1,
143 .non_legacy_dai_naming = 1,
146 static int dmic_dev_probe(struct platform_device *pdev)
150 struct snd_soc_dai_driver *dai_drv = &dmic_dai;
152 if (pdev->dev.of_node) {
153 err = of_property_read_u32(pdev->dev.of_node, "num-channels", &chans);
154 if (err && (err != -EINVAL))
158 if (chans < 1 || chans > 8)
161 dai_drv = devm_kzalloc(&pdev->dev, sizeof(*dai_drv), GFP_KERNEL);
165 memcpy(dai_drv, &dmic_dai, sizeof(*dai_drv));
166 dai_drv->capture.channels_max = chans;
170 return devm_snd_soc_register_component(&pdev->dev,
171 &soc_dmic, dai_drv, 1);
174 MODULE_ALIAS("platform:dmic-codec");
176 static const struct of_device_id dmic_dev_match[] = {
177 {.compatible = "dmic-codec"},
180 MODULE_DEVICE_TABLE(of, dmic_dev_match);
182 static struct platform_driver dmic_driver = {
184 .name = "dmic-codec",
185 .of_match_table = dmic_dev_match,
187 .probe = dmic_dev_probe,
190 module_platform_driver(dmic_driver);
192 MODULE_DESCRIPTION("Generic DMIC driver");
194 MODULE_LICENSE("GPL");