blob: f5560a49b9e52a555a8ca2f94422eee05f0958b3 [file] [log] [blame]
Thomas Gleixner2b27bdc2019-05-29 16:57:50 -07001// SPDX-License-Identifier: GPL-2.0-only
David Lamberta7107702011-01-06 08:00:37 -06002/*
3 * dmic.c -- SoC audio for Generic Digital MICs
4 *
5 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
David Lamberta7107702011-01-06 08:00:37 -06006 */
7
Matthias Kaehlcke05c9b302018-02-16 09:53:12 -08008#include <linux/delay.h>
huang lin23c71592017-08-17 10:24:44 +08009#include <linux/gpio.h>
10#include <linux/gpio/consumer.h>
David Lamberta7107702011-01-06 08:00:37 -060011#include <linux/platform_device.h>
12#include <linux/slab.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040013#include <linux/module.h>
David Lamberta7107702011-01-06 08:00:37 -060014#include <sound/core.h>
15#include <sound/pcm.h>
16#include <sound/soc.h>
17#include <sound/soc-dapm.h>
18
Jenny TCbc0a7db2018-11-28 12:22:45 +053019#define MAX_MODESWITCH_DELAY 70
20static int modeswitch_delay;
21module_param(modeswitch_delay, uint, 0644);
22
Jenny TCf6f30a62018-11-28 12:22:46 +053023static int wakeup_delay;
24module_param(wakeup_delay, uint, 0644);
25
Matthias Kaehlcke05c9b302018-02-16 09:53:12 -080026struct dmic {
27 struct gpio_desc *gpio_en;
28 int wakeup_delay;
Jenny TCbc0a7db2018-11-28 12:22:45 +053029 /* Delay after DMIC mode switch */
30 int modeswitch_delay;
31};
32
Pierre-Louis Bossart902d8222019-01-04 20:02:29 -060033static int dmic_daiops_trigger(struct snd_pcm_substream *substream,
34 int cmd, struct snd_soc_dai *dai)
Jenny TCbc0a7db2018-11-28 12:22:45 +053035{
36 struct snd_soc_component *component = dai->component;
37 struct dmic *dmic = snd_soc_component_get_drvdata(component);
38
39 switch (cmd) {
40 case SNDRV_PCM_TRIGGER_STOP:
41 if (dmic->modeswitch_delay)
42 mdelay(dmic->modeswitch_delay);
43
44 break;
45 }
46
47 return 0;
48}
49
50static const struct snd_soc_dai_ops dmic_dai_ops = {
51 .trigger = dmic_daiops_trigger,
Matthias Kaehlcke05c9b302018-02-16 09:53:12 -080052};
huang lin23c71592017-08-17 10:24:44 +080053
Matthias Kaehlcke05c9b302018-02-16 09:53:12 -080054static int dmic_aif_event(struct snd_soc_dapm_widget *w,
55 struct snd_kcontrol *kcontrol, int event) {
Kuninori Morimoto15b7c5d2018-02-21 02:06:29 +000056 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
57 struct dmic *dmic = snd_soc_component_get_drvdata(component);
huang lin23c71592017-08-17 10:24:44 +080058
Matthias Kaehlcke05c9b302018-02-16 09:53:12 -080059 switch (event) {
60 case SND_SOC_DAPM_POST_PMU:
61 if (dmic->gpio_en)
62 gpiod_set_value(dmic->gpio_en, 1);
63
64 if (dmic->wakeup_delay)
65 msleep(dmic->wakeup_delay);
huang lin23c71592017-08-17 10:24:44 +080066 break;
Matthias Kaehlcke05c9b302018-02-16 09:53:12 -080067 case SND_SOC_DAPM_POST_PMD:
68 if (dmic->gpio_en)
69 gpiod_set_value(dmic->gpio_en, 0);
huang lin23c71592017-08-17 10:24:44 +080070 break;
71 }
72
73 return 0;
74}
75
David Lamberta7107702011-01-06 08:00:37 -060076static struct snd_soc_dai_driver dmic_dai = {
77 .name = "dmic-hifi",
78 .capture = {
79 .stream_name = "Capture",
80 .channels_min = 1,
81 .channels_max = 8,
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 },
Jenny TCbc0a7db2018-11-28 12:22:45 +053087 .ops = &dmic_dai_ops,
David Lamberta7107702011-01-06 08:00:37 -060088};
89
Kuninori Morimoto6d6c3942018-01-29 04:40:29 +000090static int dmic_component_probe(struct snd_soc_component *component)
huang lin23c71592017-08-17 10:24:44 +080091{
Matthias Kaehlcke05c9b302018-02-16 09:53:12 -080092 struct dmic *dmic;
huang lin23c71592017-08-17 10:24:44 +080093
Matthias Kaehlcke05c9b302018-02-16 09:53:12 -080094 dmic = devm_kzalloc(component->dev, sizeof(*dmic), GFP_KERNEL);
95 if (!dmic)
96 return -ENOMEM;
huang lin23c71592017-08-17 10:24:44 +080097
Matthias Kaehlcke05c9b302018-02-16 09:53:12 -080098 dmic->gpio_en = devm_gpiod_get_optional(component->dev,
99 "dmicen", GPIOD_OUT_LOW);
100 if (IS_ERR(dmic->gpio_en))
101 return PTR_ERR(dmic->gpio_en);
102
103 device_property_read_u32(component->dev, "wakeup-delay-ms",
104 &dmic->wakeup_delay);
Jenny TCbc0a7db2018-11-28 12:22:45 +0530105 device_property_read_u32(component->dev, "modeswitch-delay-ms",
106 &dmic->modeswitch_delay);
Jenny TCf6f30a62018-11-28 12:22:46 +0530107 if (wakeup_delay)
108 dmic->wakeup_delay = wakeup_delay;
Jenny TCbc0a7db2018-11-28 12:22:45 +0530109 if (modeswitch_delay)
110 dmic->modeswitch_delay = modeswitch_delay;
111
112 if (dmic->modeswitch_delay > MAX_MODESWITCH_DELAY)
113 dmic->modeswitch_delay = MAX_MODESWITCH_DELAY;
Matthias Kaehlcke05c9b302018-02-16 09:53:12 -0800114
115 snd_soc_component_set_drvdata(component, dmic);
huang lin23c71592017-08-17 10:24:44 +0800116
117 return 0;
118}
119
Misael Lopez Cruzd5e4b0a2011-05-12 16:26:20 +0100120static const struct snd_soc_dapm_widget dmic_dapm_widgets[] = {
Matthias Kaehlcke05c9b302018-02-16 09:53:12 -0800121 SND_SOC_DAPM_AIF_OUT_E("DMIC AIF", "Capture", 0,
122 SND_SOC_NOPM, 0, 0, dmic_aif_event,
123 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
Misael Lopez Cruzd5e4b0a2011-05-12 16:26:20 +0100124 SND_SOC_DAPM_INPUT("DMic"),
125};
126
127static const struct snd_soc_dapm_route intercon[] = {
128 {"DMIC AIF", NULL, "DMic"},
129};
130
Kuninori Morimoto6d6c3942018-01-29 04:40:29 +0000131static const struct snd_soc_component_driver soc_dmic = {
132 .probe = dmic_component_probe,
133 .dapm_widgets = dmic_dapm_widgets,
134 .num_dapm_widgets = ARRAY_SIZE(dmic_dapm_widgets),
135 .dapm_routes = intercon,
136 .num_dapm_routes = ARRAY_SIZE(intercon),
137 .idle_bias_on = 1,
138 .use_pmdown_time = 1,
139 .endianness = 1,
140 .non_legacy_dai_naming = 1,
Misael Lopez Cruzd5e4b0a2011-05-12 16:26:20 +0100141};
David Lamberta7107702011-01-06 08:00:37 -0600142
Bill Pemberton7a79e942012-12-07 09:26:37 -0500143static int dmic_dev_probe(struct platform_device *pdev)
David Lamberta7107702011-01-06 08:00:37 -0600144{
Matthias Kaehlcke7fb59e92018-01-05 12:39:57 -0800145 int err;
146 u32 chans;
147 struct snd_soc_dai_driver *dai_drv = &dmic_dai;
148
149 if (pdev->dev.of_node) {
150 err = of_property_read_u32(pdev->dev.of_node, "num-channels", &chans);
Matthias Kaehlcke35b84bf2018-01-19 15:36:50 -0800151 if (err && (err != -EINVAL))
Matthias Kaehlcke7fb59e92018-01-05 12:39:57 -0800152 return err;
153
154 if (!err) {
155 if (chans < 1 || chans > 8)
156 return -EINVAL;
157
158 dai_drv = devm_kzalloc(&pdev->dev, sizeof(*dai_drv), GFP_KERNEL);
159 if (!dai_drv)
160 return -ENOMEM;
161
162 memcpy(dai_drv, &dmic_dai, sizeof(*dai_drv));
163 dai_drv->capture.channels_max = chans;
164 }
165 }
166
Kuninori Morimoto6d6c3942018-01-29 04:40:29 +0000167 return devm_snd_soc_register_component(&pdev->dev,
Matthias Kaehlcke7fb59e92018-01-05 12:39:57 -0800168 &soc_dmic, dai_drv, 1);
David Lamberta7107702011-01-06 08:00:37 -0600169}
170
David Lamberta7107702011-01-06 08:00:37 -0600171MODULE_ALIAS("platform:dmic-codec");
172
Arnaud Pouliquen29685e22017-07-25 10:48:14 +0200173static const struct of_device_id dmic_dev_match[] = {
174 {.compatible = "dmic-codec"},
175 {}
176};
Jerome Brunetcb06a032018-08-29 17:00:49 +0200177MODULE_DEVICE_TABLE(of, dmic_dev_match);
Arnaud Pouliquen29685e22017-07-25 10:48:14 +0200178
David Lamberta7107702011-01-06 08:00:37 -0600179static struct platform_driver dmic_driver = {
180 .driver = {
181 .name = "dmic-codec",
Arnaud Pouliquen29685e22017-07-25 10:48:14 +0200182 .of_match_table = dmic_dev_match,
David Lamberta7107702011-01-06 08:00:37 -0600183 },
184 .probe = dmic_dev_probe,
David Lamberta7107702011-01-06 08:00:37 -0600185};
186
Mark Brown5bbcc3c2011-11-23 22:52:08 +0000187module_platform_driver(dmic_driver);
David Lamberta7107702011-01-06 08:00:37 -0600188
189MODULE_DESCRIPTION("Generic DMIC driver");
190MODULE_AUTHOR("Liam Girdwood <lrg@slimlogic.co.uk>");
191MODULE_LICENSE("GPL");