xref: /linux/sound/soc/codecs/dmic.c (revision 3271be0628917a97d8757b02b5bc40856fd03476)
12b27bdccSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2a710770eSDavid Lambert /*
3a710770eSDavid Lambert  * dmic.c  --  SoC audio for Generic Digital MICs
4a710770eSDavid Lambert  *
5a710770eSDavid Lambert  * Author: Liam Girdwood <lrg@slimlogic.co.uk>
6a710770eSDavid Lambert  */
7a710770eSDavid Lambert 
805c9b302SMatthias Kaehlcke #include <linux/delay.h>
923c7159aShuang lin #include <linux/gpio.h>
1023c7159aShuang lin #include <linux/gpio/consumer.h>
11a710770eSDavid Lambert #include <linux/platform_device.h>
12a710770eSDavid Lambert #include <linux/slab.h>
13da155d5bSPaul Gortmaker #include <linux/module.h>
14a710770eSDavid Lambert #include <sound/core.h>
15a710770eSDavid Lambert #include <sound/pcm.h>
16a710770eSDavid Lambert #include <sound/soc.h>
17a710770eSDavid Lambert #include <sound/soc-dapm.h>
18a710770eSDavid Lambert 
19bc0a7dbcSJenny TC #define MAX_MODESWITCH_DELAY 70
20bc0a7dbcSJenny TC static int modeswitch_delay;
21bc0a7dbcSJenny TC module_param(modeswitch_delay, uint, 0644);
22bc0a7dbcSJenny TC 
23f6f30a60SJenny TC static int wakeup_delay;
24f6f30a60SJenny TC module_param(wakeup_delay, uint, 0644);
25f6f30a60SJenny TC 
2605c9b302SMatthias Kaehlcke struct dmic {
2705c9b302SMatthias Kaehlcke 	struct gpio_desc *gpio_en;
2805c9b302SMatthias Kaehlcke 	int wakeup_delay;
29bc0a7dbcSJenny TC 	/* Delay after DMIC mode switch */
30bc0a7dbcSJenny TC 	int modeswitch_delay;
31bc0a7dbcSJenny TC };
32bc0a7dbcSJenny TC 
33902d8222SPierre-Louis Bossart static int dmic_daiops_trigger(struct snd_pcm_substream *substream,
34bc0a7dbcSJenny TC 			       int cmd, struct snd_soc_dai *dai)
35bc0a7dbcSJenny TC {
36bc0a7dbcSJenny TC 	struct snd_soc_component *component = dai->component;
37bc0a7dbcSJenny TC 	struct dmic *dmic = snd_soc_component_get_drvdata(component);
38bc0a7dbcSJenny TC 
39bc0a7dbcSJenny TC 	switch (cmd) {
40bc0a7dbcSJenny TC 	case SNDRV_PCM_TRIGGER_STOP:
41bc0a7dbcSJenny TC 		if (dmic->modeswitch_delay)
42bc0a7dbcSJenny TC 			mdelay(dmic->modeswitch_delay);
43bc0a7dbcSJenny TC 
44bc0a7dbcSJenny TC 		break;
45bc0a7dbcSJenny TC 	}
46bc0a7dbcSJenny TC 
47bc0a7dbcSJenny TC 	return 0;
48bc0a7dbcSJenny TC }
49bc0a7dbcSJenny TC 
50bc0a7dbcSJenny TC static const struct snd_soc_dai_ops dmic_dai_ops = {
51bc0a7dbcSJenny TC 	.trigger	= dmic_daiops_trigger,
5223c7159aShuang lin };
5323c7159aShuang lin 
5405c9b302SMatthias Kaehlcke static int dmic_aif_event(struct snd_soc_dapm_widget *w,
5505c9b302SMatthias Kaehlcke 			  struct snd_kcontrol *kcontrol, int event) {
5615b7c5deSKuninori Morimoto 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
5715b7c5deSKuninori Morimoto 	struct dmic *dmic = snd_soc_component_get_drvdata(component);
5805c9b302SMatthias Kaehlcke 
5905c9b302SMatthias Kaehlcke 	switch (event) {
6005c9b302SMatthias Kaehlcke 	case SND_SOC_DAPM_POST_PMU:
6105c9b302SMatthias Kaehlcke 		if (dmic->gpio_en)
625d7e0b15SMark Brown 			gpiod_set_value_cansleep(dmic->gpio_en, 1);
6305c9b302SMatthias Kaehlcke 
6405c9b302SMatthias Kaehlcke 		if (dmic->wakeup_delay)
6505c9b302SMatthias Kaehlcke 			msleep(dmic->wakeup_delay);
6605c9b302SMatthias Kaehlcke 		break;
6705c9b302SMatthias Kaehlcke 	case SND_SOC_DAPM_POST_PMD:
6805c9b302SMatthias Kaehlcke 		if (dmic->gpio_en)
695d7e0b15SMark Brown 			gpiod_set_value_cansleep(dmic->gpio_en, 0);
7005c9b302SMatthias Kaehlcke 		break;
7105c9b302SMatthias Kaehlcke 	}
7205c9b302SMatthias Kaehlcke 
7305c9b302SMatthias Kaehlcke 	return 0;
7405c9b302SMatthias Kaehlcke }
7505c9b302SMatthias Kaehlcke 
76a710770eSDavid Lambert static struct snd_soc_dai_driver dmic_dai = {
77a710770eSDavid Lambert 	.name = "dmic-hifi",
78a710770eSDavid Lambert 	.capture = {
79a710770eSDavid Lambert 		.stream_name = "Capture",
80a710770eSDavid Lambert 		.channels_min = 1,
81a710770eSDavid Lambert 		.channels_max = 8,
82a710770eSDavid Lambert 		.rates = SNDRV_PCM_RATE_CONTINUOUS,
83a710770eSDavid Lambert 		.formats = SNDRV_PCM_FMTBIT_S32_LE
84a710770eSDavid Lambert 			| SNDRV_PCM_FMTBIT_S24_LE
85*3271be06SShengjiu Wang 			| SNDRV_PCM_FMTBIT_S16_LE
86*3271be06SShengjiu Wang 			| SNDRV_PCM_FMTBIT_DSD_U8
87*3271be06SShengjiu Wang 			| SNDRV_PCM_FMTBIT_DSD_U16_LE
88*3271be06SShengjiu Wang 			| SNDRV_PCM_FMTBIT_DSD_U32_LE,
89a710770eSDavid Lambert 	},
90bc0a7dbcSJenny TC 	.ops    = &dmic_dai_ops,
91a710770eSDavid Lambert };
92a710770eSDavid Lambert 
936d6c3946SKuninori Morimoto static int dmic_component_probe(struct snd_soc_component *component)
9423c7159aShuang lin {
9505c9b302SMatthias Kaehlcke 	struct dmic *dmic;
9623c7159aShuang lin 
9705c9b302SMatthias Kaehlcke 	dmic = devm_kzalloc(component->dev, sizeof(*dmic), GFP_KERNEL);
9805c9b302SMatthias Kaehlcke 	if (!dmic)
9905c9b302SMatthias Kaehlcke 		return -ENOMEM;
10005c9b302SMatthias Kaehlcke 
10105c9b302SMatthias Kaehlcke 	dmic->gpio_en = devm_gpiod_get_optional(component->dev,
10223c7159aShuang lin 						"dmicen", GPIOD_OUT_LOW);
10305c9b302SMatthias Kaehlcke 	if (IS_ERR(dmic->gpio_en))
10405c9b302SMatthias Kaehlcke 		return PTR_ERR(dmic->gpio_en);
10523c7159aShuang lin 
10605c9b302SMatthias Kaehlcke 	device_property_read_u32(component->dev, "wakeup-delay-ms",
10705c9b302SMatthias Kaehlcke 				 &dmic->wakeup_delay);
108bc0a7dbcSJenny TC 	device_property_read_u32(component->dev, "modeswitch-delay-ms",
109bc0a7dbcSJenny TC 				 &dmic->modeswitch_delay);
110f6f30a60SJenny TC 	if (wakeup_delay)
111f6f30a60SJenny TC 		dmic->wakeup_delay  = wakeup_delay;
112bc0a7dbcSJenny TC 	if (modeswitch_delay)
113bc0a7dbcSJenny TC 		dmic->modeswitch_delay  = modeswitch_delay;
114bc0a7dbcSJenny TC 
115bc0a7dbcSJenny TC 	if (dmic->modeswitch_delay > MAX_MODESWITCH_DELAY)
116bc0a7dbcSJenny TC 		dmic->modeswitch_delay = MAX_MODESWITCH_DELAY;
11705c9b302SMatthias Kaehlcke 
11805c9b302SMatthias Kaehlcke 	snd_soc_component_set_drvdata(component, dmic);
11923c7159aShuang lin 
12023c7159aShuang lin 	return 0;
12123c7159aShuang lin }
12223c7159aShuang lin 
123d5e4b0adSMisael Lopez Cruz static const struct snd_soc_dapm_widget dmic_dapm_widgets[] = {
12405c9b302SMatthias Kaehlcke 	SND_SOC_DAPM_AIF_OUT_E("DMIC AIF", "Capture", 0,
12505c9b302SMatthias Kaehlcke 			       SND_SOC_NOPM, 0, 0, dmic_aif_event,
12605c9b302SMatthias Kaehlcke 			       SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
127d5e4b0adSMisael Lopez Cruz 	SND_SOC_DAPM_INPUT("DMic"),
128d5e4b0adSMisael Lopez Cruz };
129d5e4b0adSMisael Lopez Cruz 
130d5e4b0adSMisael Lopez Cruz static const struct snd_soc_dapm_route intercon[] = {
131d5e4b0adSMisael Lopez Cruz 	{"DMIC AIF", NULL, "DMic"},
132d5e4b0adSMisael Lopez Cruz };
133d5e4b0adSMisael Lopez Cruz 
1346d6c3946SKuninori Morimoto static const struct snd_soc_component_driver soc_dmic = {
1356d6c3946SKuninori Morimoto 	.probe			= dmic_component_probe,
136a85f9da7SLars-Peter Clausen 	.dapm_widgets		= dmic_dapm_widgets,
137a85f9da7SLars-Peter Clausen 	.num_dapm_widgets	= ARRAY_SIZE(dmic_dapm_widgets),
138a85f9da7SLars-Peter Clausen 	.dapm_routes		= intercon,
139a85f9da7SLars-Peter Clausen 	.num_dapm_routes	= ARRAY_SIZE(intercon),
1406d6c3946SKuninori Morimoto 	.idle_bias_on		= 1,
1416d6c3946SKuninori Morimoto 	.use_pmdown_time	= 1,
1426d6c3946SKuninori Morimoto 	.endianness		= 1,
1436d6c3946SKuninori Morimoto 	.non_legacy_dai_naming	= 1,
144d5e4b0adSMisael Lopez Cruz };
145a710770eSDavid Lambert 
1467a79e94eSBill Pemberton static int dmic_dev_probe(struct platform_device *pdev)
147a710770eSDavid Lambert {
1487fb59e94SMatthias Kaehlcke 	int err;
1497fb59e94SMatthias Kaehlcke 	u32 chans;
1507fb59e94SMatthias Kaehlcke 	struct snd_soc_dai_driver *dai_drv = &dmic_dai;
1517fb59e94SMatthias Kaehlcke 
1527fb59e94SMatthias Kaehlcke 	if (pdev->dev.of_node) {
1537fb59e94SMatthias Kaehlcke 		err = of_property_read_u32(pdev->dev.of_node, "num-channels", &chans);
15435b84bf0SMatthias Kaehlcke 		if (err && (err != -EINVAL))
1557fb59e94SMatthias Kaehlcke 			return err;
1567fb59e94SMatthias Kaehlcke 
1577fb59e94SMatthias Kaehlcke 		if (!err) {
1587fb59e94SMatthias Kaehlcke 			if (chans < 1 || chans > 8)
1597fb59e94SMatthias Kaehlcke 				return -EINVAL;
1607fb59e94SMatthias Kaehlcke 
1617fb59e94SMatthias Kaehlcke 			dai_drv = devm_kzalloc(&pdev->dev, sizeof(*dai_drv), GFP_KERNEL);
1627fb59e94SMatthias Kaehlcke 			if (!dai_drv)
1637fb59e94SMatthias Kaehlcke 				return -ENOMEM;
1647fb59e94SMatthias Kaehlcke 
1657fb59e94SMatthias Kaehlcke 			memcpy(dai_drv, &dmic_dai, sizeof(*dai_drv));
1667fb59e94SMatthias Kaehlcke 			dai_drv->capture.channels_max = chans;
1677fb59e94SMatthias Kaehlcke 		}
1687fb59e94SMatthias Kaehlcke 	}
1697fb59e94SMatthias Kaehlcke 
1706d6c3946SKuninori Morimoto 	return devm_snd_soc_register_component(&pdev->dev,
1717fb59e94SMatthias Kaehlcke 			&soc_dmic, dai_drv, 1);
172a710770eSDavid Lambert }
173a710770eSDavid Lambert 
174a710770eSDavid Lambert MODULE_ALIAS("platform:dmic-codec");
175a710770eSDavid Lambert 
17629685e20SArnaud Pouliquen static const struct of_device_id dmic_dev_match[] = {
17729685e20SArnaud Pouliquen 	{.compatible = "dmic-codec"},
17829685e20SArnaud Pouliquen 	{}
17929685e20SArnaud Pouliquen };
180cb06a037SJerome Brunet MODULE_DEVICE_TABLE(of, dmic_dev_match);
18129685e20SArnaud Pouliquen 
182a710770eSDavid Lambert static struct platform_driver dmic_driver = {
183a710770eSDavid Lambert 	.driver = {
184a710770eSDavid Lambert 		.name = "dmic-codec",
18529685e20SArnaud Pouliquen 		.of_match_table = dmic_dev_match,
186a710770eSDavid Lambert 	},
187a710770eSDavid Lambert 	.probe = dmic_dev_probe,
188a710770eSDavid Lambert };
189a710770eSDavid Lambert 
1905bbcc3c0SMark Brown module_platform_driver(dmic_driver);
191a710770eSDavid Lambert 
192a710770eSDavid Lambert MODULE_DESCRIPTION("Generic DMIC driver");
193a710770eSDavid Lambert MODULE_AUTHOR("Liam Girdwood <lrg@slimlogic.co.uk>");
194a710770eSDavid Lambert MODULE_LICENSE("GPL");
195