blob: 32a8f71d51f1a88ffc07aa40b58d63885ec869c7 [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
huang lin23c71592017-08-17 10:24:44 +080022#include <linux/gpio.h>
23#include <linux/gpio/consumer.h>
David Lamberta7107702011-01-06 08:00:37 -060024#include <linux/platform_device.h>
25#include <linux/slab.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040026#include <linux/module.h>
David Lamberta7107702011-01-06 08:00:37 -060027#include <sound/core.h>
28#include <sound/pcm.h>
29#include <sound/soc.h>
30#include <sound/soc-dapm.h>
31
huang lin23c71592017-08-17 10:24:44 +080032static int dmic_daiops_trigger(struct snd_pcm_substream *substream,
33 int cmd, struct snd_soc_dai *dai)
34{
35 struct gpio_desc *dmic_en = snd_soc_dai_get_drvdata(dai);
36
37 if (!dmic_en)
38 return 0;
39
40 switch (cmd) {
41 case SNDRV_PCM_TRIGGER_START:
42 case SNDRV_PCM_TRIGGER_RESUME:
43 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
44 gpiod_set_value(dmic_en, 1);
45 break;
46 case SNDRV_PCM_TRIGGER_STOP:
47 case SNDRV_PCM_TRIGGER_SUSPEND:
48 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
49 gpiod_set_value(dmic_en, 0);
50 break;
51 }
52
53 return 0;
54}
55
56static const struct snd_soc_dai_ops dmic_dai_ops = {
57 .trigger = dmic_daiops_trigger,
58};
59
David Lamberta7107702011-01-06 08:00:37 -060060static struct snd_soc_dai_driver dmic_dai = {
61 .name = "dmic-hifi",
62 .capture = {
63 .stream_name = "Capture",
64 .channels_min = 1,
65 .channels_max = 8,
66 .rates = SNDRV_PCM_RATE_CONTINUOUS,
67 .formats = SNDRV_PCM_FMTBIT_S32_LE
68 | SNDRV_PCM_FMTBIT_S24_LE
69 | SNDRV_PCM_FMTBIT_S16_LE,
70 },
huang lin23c71592017-08-17 10:24:44 +080071 .ops = &dmic_dai_ops,
David Lamberta7107702011-01-06 08:00:37 -060072};
73
Kuninori Morimoto6d6c3942018-01-29 04:40:29 +000074static int dmic_component_probe(struct snd_soc_component *component)
huang lin23c71592017-08-17 10:24:44 +080075{
76 struct gpio_desc *dmic_en;
77
Kuninori Morimoto6d6c3942018-01-29 04:40:29 +000078 dmic_en = devm_gpiod_get_optional(component->dev,
huang lin23c71592017-08-17 10:24:44 +080079 "dmicen", GPIOD_OUT_LOW);
80 if (IS_ERR(dmic_en))
81 return PTR_ERR(dmic_en);
82
Kuninori Morimoto6d6c3942018-01-29 04:40:29 +000083 snd_soc_component_set_drvdata(component, dmic_en);
huang lin23c71592017-08-17 10:24:44 +080084
85 return 0;
86}
87
Misael Lopez Cruzd5e4b0a2011-05-12 16:26:20 +010088static const struct snd_soc_dapm_widget dmic_dapm_widgets[] = {
89 SND_SOC_DAPM_AIF_OUT("DMIC AIF", "Capture", 0,
90 SND_SOC_NOPM, 0, 0),
91 SND_SOC_DAPM_INPUT("DMic"),
92};
93
94static const struct snd_soc_dapm_route intercon[] = {
95 {"DMIC AIF", NULL, "DMic"},
96};
97
Kuninori Morimoto6d6c3942018-01-29 04:40:29 +000098static const struct snd_soc_component_driver soc_dmic = {
99 .probe = dmic_component_probe,
100 .dapm_widgets = dmic_dapm_widgets,
101 .num_dapm_widgets = ARRAY_SIZE(dmic_dapm_widgets),
102 .dapm_routes = intercon,
103 .num_dapm_routes = ARRAY_SIZE(intercon),
104 .idle_bias_on = 1,
105 .use_pmdown_time = 1,
106 .endianness = 1,
107 .non_legacy_dai_naming = 1,
Misael Lopez Cruzd5e4b0a2011-05-12 16:26:20 +0100108};
David Lamberta7107702011-01-06 08:00:37 -0600109
Bill Pemberton7a79e942012-12-07 09:26:37 -0500110static int dmic_dev_probe(struct platform_device *pdev)
David Lamberta7107702011-01-06 08:00:37 -0600111{
Matthias Kaehlcke7fb59e92018-01-05 12:39:57 -0800112 int err;
113 u32 chans;
114 struct snd_soc_dai_driver *dai_drv = &dmic_dai;
115
116 if (pdev->dev.of_node) {
117 err = of_property_read_u32(pdev->dev.of_node, "num-channels", &chans);
Matthias Kaehlcke35b84bf2018-01-19 15:36:50 -0800118 if (err && (err != -EINVAL))
Matthias Kaehlcke7fb59e92018-01-05 12:39:57 -0800119 return err;
120
121 if (!err) {
122 if (chans < 1 || chans > 8)
123 return -EINVAL;
124
125 dai_drv = devm_kzalloc(&pdev->dev, sizeof(*dai_drv), GFP_KERNEL);
126 if (!dai_drv)
127 return -ENOMEM;
128
129 memcpy(dai_drv, &dmic_dai, sizeof(*dai_drv));
130 dai_drv->capture.channels_max = chans;
131 }
132 }
133
Kuninori Morimoto6d6c3942018-01-29 04:40:29 +0000134 return devm_snd_soc_register_component(&pdev->dev,
Matthias Kaehlcke7fb59e92018-01-05 12:39:57 -0800135 &soc_dmic, dai_drv, 1);
David Lamberta7107702011-01-06 08:00:37 -0600136}
137
David Lamberta7107702011-01-06 08:00:37 -0600138MODULE_ALIAS("platform:dmic-codec");
139
Arnaud Pouliquen29685e22017-07-25 10:48:14 +0200140static const struct of_device_id dmic_dev_match[] = {
141 {.compatible = "dmic-codec"},
142 {}
143};
144
David Lamberta7107702011-01-06 08:00:37 -0600145static struct platform_driver dmic_driver = {
146 .driver = {
147 .name = "dmic-codec",
Arnaud Pouliquen29685e22017-07-25 10:48:14 +0200148 .of_match_table = dmic_dev_match,
David Lamberta7107702011-01-06 08:00:37 -0600149 },
150 .probe = dmic_dev_probe,
David Lamberta7107702011-01-06 08:00:37 -0600151};
152
Mark Brown5bbcc3c2011-11-23 22:52:08 +0000153module_platform_driver(dmic_driver);
David Lamberta7107702011-01-06 08:00:37 -0600154
155MODULE_DESCRIPTION("Generic DMIC driver");
156MODULE_AUTHOR("Liam Girdwood <lrg@slimlogic.co.uk>");
157MODULE_LICENSE("GPL");