Merge remote-tracking branches 'asoc/topic/rt5645', 'asoc/topic/sam9g20_wm8731',...
[linux-2.6-block.git] / sound / soc / codecs / dmic.c
CommitLineData
a710770e
DL
1/*
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
23c7159a 22#include <linux/gpio.h>
23#include <linux/gpio/consumer.h>
a710770e
DL
24#include <linux/platform_device.h>
25#include <linux/slab.h>
da155d5b 26#include <linux/module.h>
a710770e
DL
27#include <sound/core.h>
28#include <sound/pcm.h>
29#include <sound/soc.h>
30#include <sound/soc-dapm.h>
31
23c7159a 32static 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
a710770e
DL
60static 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 },
23c7159a 71 .ops = &dmic_dai_ops,
a710770e
DL
72};
73
23c7159a 74static int dmic_codec_probe(struct snd_soc_codec *codec)
75{
76 struct gpio_desc *dmic_en;
77
78 dmic_en = devm_gpiod_get_optional(codec->dev,
79 "dmicen", GPIOD_OUT_LOW);
80 if (IS_ERR(dmic_en))
81 return PTR_ERR(dmic_en);
82
83 snd_soc_codec_set_drvdata(codec, dmic_en);
84
85 return 0;
86}
87
d5e4b0ad
MLC
88static 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
23c7159a 98static const struct snd_soc_codec_driver soc_dmic = {
99 .probe = dmic_codec_probe,
a73b8e89
KM
100 .component_driver = {
101 .dapm_widgets = dmic_dapm_widgets,
102 .num_dapm_widgets = ARRAY_SIZE(dmic_dapm_widgets),
103 .dapm_routes = intercon,
104 .num_dapm_routes = ARRAY_SIZE(intercon),
105 },
d5e4b0ad 106};
a710770e 107
7a79e94e 108static int dmic_dev_probe(struct platform_device *pdev)
a710770e 109{
7fb59e94
MK
110 int err;
111 u32 chans;
112 struct snd_soc_dai_driver *dai_drv = &dmic_dai;
113
114 if (pdev->dev.of_node) {
115 err = of_property_read_u32(pdev->dev.of_node, "num-channels", &chans);
35b84bf0 116 if (err && (err != -EINVAL))
7fb59e94
MK
117 return err;
118
119 if (!err) {
120 if (chans < 1 || chans > 8)
121 return -EINVAL;
122
123 dai_drv = devm_kzalloc(&pdev->dev, sizeof(*dai_drv), GFP_KERNEL);
124 if (!dai_drv)
125 return -ENOMEM;
126
127 memcpy(dai_drv, &dmic_dai, sizeof(*dai_drv));
128 dai_drv->capture.channels_max = chans;
129 }
130 }
131
a710770e 132 return snd_soc_register_codec(&pdev->dev,
7fb59e94 133 &soc_dmic, dai_drv, 1);
a710770e
DL
134}
135
7a79e94e 136static int dmic_dev_remove(struct platform_device *pdev)
a710770e
DL
137{
138 snd_soc_unregister_codec(&pdev->dev);
139 return 0;
140}
141
142MODULE_ALIAS("platform:dmic-codec");
143
29685e20
AP
144static const struct of_device_id dmic_dev_match[] = {
145 {.compatible = "dmic-codec"},
146 {}
147};
148
a710770e
DL
149static struct platform_driver dmic_driver = {
150 .driver = {
151 .name = "dmic-codec",
29685e20 152 .of_match_table = dmic_dev_match,
a710770e
DL
153 },
154 .probe = dmic_dev_probe,
7a79e94e 155 .remove = dmic_dev_remove,
a710770e
DL
156};
157
5bbcc3c0 158module_platform_driver(dmic_driver);
a710770e
DL
159
160MODULE_DESCRIPTION("Generic DMIC driver");
161MODULE_AUTHOR("Liam Girdwood <lrg@slimlogic.co.uk>");
162MODULE_LICENSE("GPL");