blob: f4eb0a438a3adda5a28941321c07d671225d7297 [file] [log] [blame]
David Lamberta7107702011-01-06 08:00:37 -06001/*
2 * dmic.c -- SoC audio for Generic Digital MICs
3 *
4 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * version 2 as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18 * 02110-1301 USA
19 *
20 */
21
Matthias Kaehlcke05c9b302018-02-16 09:53:12 -080022#include <linux/delay.h>
huang lin23c71592017-08-17 10:24:44 +080023#include <linux/gpio.h>
24#include <linux/gpio/consumer.h>
David Lamberta7107702011-01-06 08:00:37 -060025#include <linux/platform_device.h>
26#include <linux/slab.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040027#include <linux/module.h>
David Lamberta7107702011-01-06 08:00:37 -060028#include <sound/core.h>
29#include <sound/pcm.h>
30#include <sound/soc.h>
31#include <sound/soc-dapm.h>
32
Jenny TCbc0a7db2018-11-28 12:22:45 +053033#define MAX_MODESWITCH_DELAY 70
34static int modeswitch_delay;
35module_param(modeswitch_delay, uint, 0644);
36
Matthias Kaehlcke05c9b302018-02-16 09:53:12 -080037struct dmic {
38 struct gpio_desc *gpio_en;
39 int wakeup_delay;
Jenny TCbc0a7db2018-11-28 12:22:45 +053040 /* Delay after DMIC mode switch */
41 int modeswitch_delay;
42};
43
44int dmic_daiops_trigger(struct snd_pcm_substream *substream,
45 int cmd, struct snd_soc_dai *dai)
46{
47 struct snd_soc_component *component = dai->component;
48 struct dmic *dmic = snd_soc_component_get_drvdata(component);
49
50 switch (cmd) {
51 case SNDRV_PCM_TRIGGER_STOP:
52 if (dmic->modeswitch_delay)
53 mdelay(dmic->modeswitch_delay);
54
55 break;
56 }
57
58 return 0;
59}
60
61static const struct snd_soc_dai_ops dmic_dai_ops = {
62 .trigger = dmic_daiops_trigger,
Matthias Kaehlcke05c9b302018-02-16 09:53:12 -080063};
huang lin23c71592017-08-17 10:24:44 +080064
Matthias Kaehlcke05c9b302018-02-16 09:53:12 -080065static int dmic_aif_event(struct snd_soc_dapm_widget *w,
66 struct snd_kcontrol *kcontrol, int event) {
Kuninori Morimoto15b7c5d2018-02-21 02:06:29 +000067 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
68 struct dmic *dmic = snd_soc_component_get_drvdata(component);
huang lin23c71592017-08-17 10:24:44 +080069
Matthias Kaehlcke05c9b302018-02-16 09:53:12 -080070 switch (event) {
71 case SND_SOC_DAPM_POST_PMU:
72 if (dmic->gpio_en)
73 gpiod_set_value(dmic->gpio_en, 1);
74
75 if (dmic->wakeup_delay)
76 msleep(dmic->wakeup_delay);
huang lin23c71592017-08-17 10:24:44 +080077 break;
Matthias Kaehlcke05c9b302018-02-16 09:53:12 -080078 case SND_SOC_DAPM_POST_PMD:
79 if (dmic->gpio_en)
80 gpiod_set_value(dmic->gpio_en, 0);
huang lin23c71592017-08-17 10:24:44 +080081 break;
82 }
83
84 return 0;
85}
86
David Lamberta7107702011-01-06 08:00:37 -060087static struct snd_soc_dai_driver dmic_dai = {
88 .name = "dmic-hifi",
89 .capture = {
90 .stream_name = "Capture",
91 .channels_min = 1,
92 .channels_max = 8,
93 .rates = SNDRV_PCM_RATE_CONTINUOUS,
94 .formats = SNDRV_PCM_FMTBIT_S32_LE
95 | SNDRV_PCM_FMTBIT_S24_LE
96 | SNDRV_PCM_FMTBIT_S16_LE,
97 },
Jenny TCbc0a7db2018-11-28 12:22:45 +053098 .ops = &dmic_dai_ops,
David Lamberta7107702011-01-06 08:00:37 -060099};
100
Kuninori Morimoto6d6c3942018-01-29 04:40:29 +0000101static int dmic_component_probe(struct snd_soc_component *component)
huang lin23c71592017-08-17 10:24:44 +0800102{
Matthias Kaehlcke05c9b302018-02-16 09:53:12 -0800103 struct dmic *dmic;
huang lin23c71592017-08-17 10:24:44 +0800104
Matthias Kaehlcke05c9b302018-02-16 09:53:12 -0800105 dmic = devm_kzalloc(component->dev, sizeof(*dmic), GFP_KERNEL);
106 if (!dmic)
107 return -ENOMEM;
huang lin23c71592017-08-17 10:24:44 +0800108
Matthias Kaehlcke05c9b302018-02-16 09:53:12 -0800109 dmic->gpio_en = devm_gpiod_get_optional(component->dev,
110 "dmicen", GPIOD_OUT_LOW);
111 if (IS_ERR(dmic->gpio_en))
112 return PTR_ERR(dmic->gpio_en);
113
114 device_property_read_u32(component->dev, "wakeup-delay-ms",
115 &dmic->wakeup_delay);
Jenny TCbc0a7db2018-11-28 12:22:45 +0530116 device_property_read_u32(component->dev, "modeswitch-delay-ms",
117 &dmic->modeswitch_delay);
118 if (modeswitch_delay)
119 dmic->modeswitch_delay = modeswitch_delay;
120
121 if (dmic->modeswitch_delay > MAX_MODESWITCH_DELAY)
122 dmic->modeswitch_delay = MAX_MODESWITCH_DELAY;
Matthias Kaehlcke05c9b302018-02-16 09:53:12 -0800123
124 snd_soc_component_set_drvdata(component, dmic);
huang lin23c71592017-08-17 10:24:44 +0800125
126 return 0;
127}
128
Misael Lopez Cruzd5e4b0a2011-05-12 16:26:20 +0100129static const struct snd_soc_dapm_widget dmic_dapm_widgets[] = {
Matthias Kaehlcke05c9b302018-02-16 09:53:12 -0800130 SND_SOC_DAPM_AIF_OUT_E("DMIC AIF", "Capture", 0,
131 SND_SOC_NOPM, 0, 0, dmic_aif_event,
132 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
Misael Lopez Cruzd5e4b0a2011-05-12 16:26:20 +0100133 SND_SOC_DAPM_INPUT("DMic"),
134};
135
136static const struct snd_soc_dapm_route intercon[] = {
137 {"DMIC AIF", NULL, "DMic"},
138};
139
Kuninori Morimoto6d6c3942018-01-29 04:40:29 +0000140static const struct snd_soc_component_driver soc_dmic = {
141 .probe = dmic_component_probe,
142 .dapm_widgets = dmic_dapm_widgets,
143 .num_dapm_widgets = ARRAY_SIZE(dmic_dapm_widgets),
144 .dapm_routes = intercon,
145 .num_dapm_routes = ARRAY_SIZE(intercon),
146 .idle_bias_on = 1,
147 .use_pmdown_time = 1,
148 .endianness = 1,
149 .non_legacy_dai_naming = 1,
Misael Lopez Cruzd5e4b0a2011-05-12 16:26:20 +0100150};
David Lamberta7107702011-01-06 08:00:37 -0600151
Bill Pemberton7a79e942012-12-07 09:26:37 -0500152static int dmic_dev_probe(struct platform_device *pdev)
David Lamberta7107702011-01-06 08:00:37 -0600153{
Matthias Kaehlcke7fb59e92018-01-05 12:39:57 -0800154 int err;
155 u32 chans;
156 struct snd_soc_dai_driver *dai_drv = &dmic_dai;
157
158 if (pdev->dev.of_node) {
159 err = of_property_read_u32(pdev->dev.of_node, "num-channels", &chans);
Matthias Kaehlcke35b84bf2018-01-19 15:36:50 -0800160 if (err && (err != -EINVAL))
Matthias Kaehlcke7fb59e92018-01-05 12:39:57 -0800161 return err;
162
163 if (!err) {
164 if (chans < 1 || chans > 8)
165 return -EINVAL;
166
167 dai_drv = devm_kzalloc(&pdev->dev, sizeof(*dai_drv), GFP_KERNEL);
168 if (!dai_drv)
169 return -ENOMEM;
170
171 memcpy(dai_drv, &dmic_dai, sizeof(*dai_drv));
172 dai_drv->capture.channels_max = chans;
173 }
174 }
175
Kuninori Morimoto6d6c3942018-01-29 04:40:29 +0000176 return devm_snd_soc_register_component(&pdev->dev,
Matthias Kaehlcke7fb59e92018-01-05 12:39:57 -0800177 &soc_dmic, dai_drv, 1);
David Lamberta7107702011-01-06 08:00:37 -0600178}
179
David Lamberta7107702011-01-06 08:00:37 -0600180MODULE_ALIAS("platform:dmic-codec");
181
Arnaud Pouliquen29685e22017-07-25 10:48:14 +0200182static const struct of_device_id dmic_dev_match[] = {
183 {.compatible = "dmic-codec"},
184 {}
185};
Jerome Brunetcb06a032018-08-29 17:00:49 +0200186MODULE_DEVICE_TABLE(of, dmic_dev_match);
Arnaud Pouliquen29685e22017-07-25 10:48:14 +0200187
David Lamberta7107702011-01-06 08:00:37 -0600188static struct platform_driver dmic_driver = {
189 .driver = {
190 .name = "dmic-codec",
Arnaud Pouliquen29685e22017-07-25 10:48:14 +0200191 .of_match_table = dmic_dev_match,
David Lamberta7107702011-01-06 08:00:37 -0600192 },
193 .probe = dmic_dev_probe,
David Lamberta7107702011-01-06 08:00:37 -0600194};
195
Mark Brown5bbcc3c2011-11-23 22:52:08 +0000196module_platform_driver(dmic_driver);
David Lamberta7107702011-01-06 08:00:37 -0600197
198MODULE_DESCRIPTION("Generic DMIC driver");
199MODULE_AUTHOR("Liam Girdwood <lrg@slimlogic.co.uk>");
200MODULE_LICENSE("GPL");