xref: /linux/sound/soc/codecs/dmic.c (revision f4cdf7ca9a1fdcca413157df19753f388a5a224e)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * dmic.c  --  SoC audio for Generic Digital MICs
4  *
5  * Author: Liam Girdwood <lrg@slimlogic.co.uk>
6  */
7 
8 #include <linux/delay.h>
9 #include <linux/gpio/consumer.h>
10 #include <linux/platform_device.h>
11 #include <linux/regulator/consumer.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>
18 
19 #define MAX_MODESWITCH_DELAY 70
20 static int modeswitch_delay;
21 module_param(modeswitch_delay, uint, 0644);
22 
23 static int wakeup_delay;
24 module_param(wakeup_delay, uint, 0644);
25 
26 struct dmic {
27 	struct gpio_desc *gpio_en;
28 	struct regulator *vref;
29 	int wakeup_delay;
30 	/* Delay after DMIC mode switch */
31 	int modeswitch_delay;
32 };
33 
34 static int dmic_daiops_trigger(struct snd_pcm_substream *substream,
35 			       int cmd, struct snd_soc_dai *dai)
36 {
37 	struct snd_soc_component *component = dai->component;
38 	struct dmic *dmic = snd_soc_component_get_drvdata(component);
39 
40 	switch (cmd) {
41 	case SNDRV_PCM_TRIGGER_STOP:
42 		if (dmic->modeswitch_delay)
43 			mdelay(dmic->modeswitch_delay);
44 
45 		break;
46 	}
47 
48 	return 0;
49 }
50 
51 static const struct snd_soc_dai_ops dmic_dai_ops = {
52 	.trigger	= dmic_daiops_trigger,
53 };
54 
55 static int dmic_aif_event(struct snd_soc_dapm_widget *w,
56 			  struct snd_kcontrol *kcontrol, int event) {
57 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
58 	struct dmic *dmic = snd_soc_component_get_drvdata(component);
59 	int ret = 0;
60 
61 	switch (event) {
62 	case SND_SOC_DAPM_POST_PMU:
63 		if (dmic->gpio_en)
64 			gpiod_set_value_cansleep(dmic->gpio_en, 1);
65 
66 		if (dmic->vref) {
67 			ret = regulator_enable(dmic->vref);
68 			if (ret)
69 				return ret;
70 		}
71 
72 		if (dmic->wakeup_delay)
73 			msleep(dmic->wakeup_delay);
74 		break;
75 	case SND_SOC_DAPM_POST_PMD:
76 		if (dmic->gpio_en)
77 			gpiod_set_value_cansleep(dmic->gpio_en, 0);
78 
79 		if (dmic->vref)
80 			ret = regulator_disable(dmic->vref);
81 
82 		break;
83 	}
84 
85 	return ret;
86 }
87 
88 static struct snd_soc_dai_driver dmic_dai = {
89 	.name = "dmic-hifi",
90 	.capture = {
91 		.stream_name = "Capture",
92 		.channels_min = 1,
93 		.channels_max = 8,
94 		.rates = SNDRV_PCM_RATE_CONTINUOUS,
95 		.formats = SNDRV_PCM_FMTBIT_S32_LE
96 			| SNDRV_PCM_FMTBIT_S24_LE
97 			| SNDRV_PCM_FMTBIT_S16_LE
98 			| SNDRV_PCM_FMTBIT_DSD_U8
99 			| SNDRV_PCM_FMTBIT_DSD_U16_LE
100 			| SNDRV_PCM_FMTBIT_DSD_U32_LE
101 			| SNDRV_PCM_FMTBIT_DSD_U16_BE
102 			| SNDRV_PCM_FMTBIT_DSD_U32_BE,
103 	},
104 	.ops    = &dmic_dai_ops,
105 };
106 
107 static int dmic_component_probe(struct snd_soc_component *component)
108 {
109 	struct dmic *dmic;
110 
111 	dmic = devm_kzalloc(component->dev, sizeof(*dmic), GFP_KERNEL);
112 	if (!dmic)
113 		return -ENOMEM;
114 
115 	dmic->vref = devm_regulator_get_optional(component->dev, "vref");
116 	if (IS_ERR(dmic->vref)) {
117 		if (PTR_ERR(dmic->vref) != -ENODEV)
118 			return dev_err_probe(component->dev, PTR_ERR(dmic->vref),
119 					     "Failed to get vref\n");
120 		dmic->vref = NULL;
121 	}
122 
123 	dmic->gpio_en = devm_gpiod_get_optional(component->dev,
124 						"dmicen", GPIOD_OUT_LOW);
125 	if (IS_ERR(dmic->gpio_en))
126 		return PTR_ERR(dmic->gpio_en);
127 
128 	device_property_read_u32(component->dev, "wakeup-delay-ms",
129 				 &dmic->wakeup_delay);
130 	device_property_read_u32(component->dev, "modeswitch-delay-ms",
131 				 &dmic->modeswitch_delay);
132 	if (wakeup_delay)
133 		dmic->wakeup_delay  = wakeup_delay;
134 	if (modeswitch_delay)
135 		dmic->modeswitch_delay  = modeswitch_delay;
136 
137 	if (dmic->modeswitch_delay > MAX_MODESWITCH_DELAY)
138 		dmic->modeswitch_delay = MAX_MODESWITCH_DELAY;
139 
140 	snd_soc_component_set_drvdata(component, dmic);
141 
142 	return 0;
143 }
144 
145 static const struct snd_soc_dapm_widget dmic_dapm_widgets[] = {
146 	SND_SOC_DAPM_AIF_OUT_E("DMIC AIF", "Capture", 0,
147 			       SND_SOC_NOPM, 0, 0, dmic_aif_event,
148 			       SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
149 	SND_SOC_DAPM_INPUT("DMic"),
150 };
151 
152 static const struct snd_soc_dapm_route intercon[] = {
153 	{"DMIC AIF", NULL, "DMic"},
154 };
155 
156 static const struct snd_soc_component_driver soc_dmic = {
157 	.probe			= dmic_component_probe,
158 	.dapm_widgets		= dmic_dapm_widgets,
159 	.num_dapm_widgets	= ARRAY_SIZE(dmic_dapm_widgets),
160 	.dapm_routes		= intercon,
161 	.num_dapm_routes	= ARRAY_SIZE(intercon),
162 	.idle_bias_on		= 1,
163 	.use_pmdown_time	= 1,
164 	.endianness		= 1,
165 };
166 
167 static int dmic_dev_probe(struct platform_device *pdev)
168 {
169 	int err;
170 	u32 chans;
171 	struct snd_soc_dai_driver *dai_drv = &dmic_dai;
172 
173 	if (pdev->dev.of_node) {
174 		err = of_property_read_u32(pdev->dev.of_node, "num-channels", &chans);
175 		if (err && (err != -EINVAL))
176 			return err;
177 
178 		if (!err) {
179 			if (chans < 1 || chans > 8)
180 				return -EINVAL;
181 
182 			dai_drv = devm_kzalloc(&pdev->dev, sizeof(*dai_drv), GFP_KERNEL);
183 			if (!dai_drv)
184 				return -ENOMEM;
185 
186 			memcpy(dai_drv, &dmic_dai, sizeof(*dai_drv));
187 			dai_drv->capture.channels_max = chans;
188 		}
189 	}
190 
191 	return devm_snd_soc_register_component(&pdev->dev,
192 			&soc_dmic, dai_drv, 1);
193 }
194 
195 MODULE_ALIAS("platform:dmic-codec");
196 
197 static const struct of_device_id dmic_dev_match[] = {
198 	{.compatible = "dmic-codec"},
199 	{}
200 };
201 MODULE_DEVICE_TABLE(of, dmic_dev_match);
202 
203 static struct platform_driver dmic_driver = {
204 	.driver = {
205 		.name = "dmic-codec",
206 		.of_match_table = dmic_dev_match,
207 	},
208 	.probe = dmic_dev_probe,
209 };
210 
211 module_platform_driver(dmic_driver);
212 
213 MODULE_DESCRIPTION("Generic DMIC driver");
214 MODULE_AUTHOR("Liam Girdwood <lrg@slimlogic.co.uk>");
215 MODULE_LICENSE("GPL");
216