Linux Audio

Check our new training course

Loading...
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * dmic.c  --  SoC audio for Generic Digital MICs
  4 *
  5 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  6 */
  7
  8#include <linux/delay.h>
  9#include <linux/gpio.h>
 10#include <linux/gpio/consumer.h>
 11#include <linux/platform_device.h>
 12#include <linux/slab.h>
 13#include <linux/module.h>
 14#include <sound/core.h>
 15#include <sound/pcm.h>
 16#include <sound/soc.h>
 17#include <sound/soc-dapm.h>
 18
 19#define MAX_MODESWITCH_DELAY 70
 20static int modeswitch_delay;
 21module_param(modeswitch_delay, uint, 0644);
 22
 23static int wakeup_delay;
 24module_param(wakeup_delay, uint, 0644);
 25
 26struct dmic {
 27	struct gpio_desc *gpio_en;
 28	int wakeup_delay;
 29	/* Delay after DMIC mode switch */
 30	int modeswitch_delay;
 31};
 32
 33static int dmic_daiops_trigger(struct snd_pcm_substream *substream,
 34			       int cmd, struct snd_soc_dai *dai)
 35{
 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,
 52};
 53
 54static int dmic_aif_event(struct snd_soc_dapm_widget *w,
 55			  struct snd_kcontrol *kcontrol, int event) {
 56	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
 57	struct dmic *dmic = snd_soc_component_get_drvdata(component);
 58
 59	switch (event) {
 60	case SND_SOC_DAPM_POST_PMU:
 61		if (dmic->gpio_en)
 62			gpiod_set_value_cansleep(dmic->gpio_en, 1);
 63
 64		if (dmic->wakeup_delay)
 65			msleep(dmic->wakeup_delay);
 66		break;
 67	case SND_SOC_DAPM_POST_PMD:
 68		if (dmic->gpio_en)
 69			gpiod_set_value_cansleep(dmic->gpio_en, 0);
 70		break;
 71	}
 72
 73	return 0;
 74}
 75
 76static 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			| SNDRV_PCM_FMTBIT_DSD_U8
 87			| SNDRV_PCM_FMTBIT_DSD_U16_LE
 88			| SNDRV_PCM_FMTBIT_DSD_U32_LE,
 89	},
 90	.ops    = &dmic_dai_ops,
 91};
 92
 93static int dmic_component_probe(struct snd_soc_component *component)
 94{
 95	struct dmic *dmic;
 96
 97	dmic = devm_kzalloc(component->dev, sizeof(*dmic), GFP_KERNEL);
 98	if (!dmic)
 99		return -ENOMEM;
100
101	dmic->gpio_en = devm_gpiod_get_optional(component->dev,
102						"dmicen", GPIOD_OUT_LOW);
103	if (IS_ERR(dmic->gpio_en))
104		return PTR_ERR(dmic->gpio_en);
105
106	device_property_read_u32(component->dev, "wakeup-delay-ms",
107				 &dmic->wakeup_delay);
108	device_property_read_u32(component->dev, "modeswitch-delay-ms",
109				 &dmic->modeswitch_delay);
110	if (wakeup_delay)
111		dmic->wakeup_delay  = wakeup_delay;
112	if (modeswitch_delay)
113		dmic->modeswitch_delay  = modeswitch_delay;
114
115	if (dmic->modeswitch_delay > MAX_MODESWITCH_DELAY)
116		dmic->modeswitch_delay = MAX_MODESWITCH_DELAY;
117
118	snd_soc_component_set_drvdata(component, dmic);
119
120	return 0;
121}
122
123static const struct snd_soc_dapm_widget dmic_dapm_widgets[] = {
124	SND_SOC_DAPM_AIF_OUT_E("DMIC AIF", "Capture", 0,
125			       SND_SOC_NOPM, 0, 0, dmic_aif_event,
126			       SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
127	SND_SOC_DAPM_INPUT("DMic"),
128};
129
130static const struct snd_soc_dapm_route intercon[] = {
131	{"DMIC AIF", NULL, "DMic"},
132};
133
134static const struct snd_soc_component_driver soc_dmic = {
135	.probe			= dmic_component_probe,
136	.dapm_widgets		= dmic_dapm_widgets,
137	.num_dapm_widgets	= ARRAY_SIZE(dmic_dapm_widgets),
138	.dapm_routes		= intercon,
139	.num_dapm_routes	= ARRAY_SIZE(intercon),
140	.idle_bias_on		= 1,
141	.use_pmdown_time	= 1,
142	.endianness		= 1,
 
 
 
 
 
143};
144
145static int dmic_dev_probe(struct platform_device *pdev)
146{
147	int err;
148	u32 chans;
149	struct snd_soc_dai_driver *dai_drv = &dmic_dai;
150
151	if (pdev->dev.of_node) {
152		err = of_property_read_u32(pdev->dev.of_node, "num-channels", &chans);
153		if (err && (err != -EINVAL))
154			return err;
155
156		if (!err) {
157			if (chans < 1 || chans > 8)
158				return -EINVAL;
159
160			dai_drv = devm_kzalloc(&pdev->dev, sizeof(*dai_drv), GFP_KERNEL);
161			if (!dai_drv)
162				return -ENOMEM;
163
164			memcpy(dai_drv, &dmic_dai, sizeof(*dai_drv));
165			dai_drv->capture.channels_max = chans;
166		}
167	}
168
169	return devm_snd_soc_register_component(&pdev->dev,
170			&soc_dmic, dai_drv, 1);
 
 
171}
172
173MODULE_ALIAS("platform:dmic-codec");
174
175static const struct of_device_id dmic_dev_match[] = {
176	{.compatible = "dmic-codec"},
177	{}
178};
179MODULE_DEVICE_TABLE(of, dmic_dev_match);
180
181static struct platform_driver dmic_driver = {
182	.driver = {
183		.name = "dmic-codec",
184		.of_match_table = dmic_dev_match,
185	},
186	.probe = dmic_dev_probe,
 
187};
188
189module_platform_driver(dmic_driver);
 
 
 
 
 
 
 
 
 
 
190
191MODULE_DESCRIPTION("Generic DMIC driver");
192MODULE_AUTHOR("Liam Girdwood <lrg@slimlogic.co.uk>");
193MODULE_LICENSE("GPL");
v3.1
 
  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
 
 
 
 22#include <linux/platform_device.h>
 23#include <linux/slab.h>
 
 24#include <sound/core.h>
 25#include <sound/pcm.h>
 26#include <sound/soc.h>
 27#include <sound/soc-dapm.h>
 28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 29static struct snd_soc_dai_driver dmic_dai = {
 30	.name = "dmic-hifi",
 31	.capture = {
 32		.stream_name = "Capture",
 33		.channels_min = 1,
 34		.channels_max = 8,
 35		.rates = SNDRV_PCM_RATE_CONTINUOUS,
 36		.formats = SNDRV_PCM_FMTBIT_S32_LE
 37			| SNDRV_PCM_FMTBIT_S24_LE
 38			| SNDRV_PCM_FMTBIT_S16_LE,
 
 
 
 39	},
 
 40};
 41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 42static const struct snd_soc_dapm_widget dmic_dapm_widgets[] = {
 43	SND_SOC_DAPM_AIF_OUT("DMIC AIF", "Capture", 0,
 44			     SND_SOC_NOPM, 0, 0),
 
 45	SND_SOC_DAPM_INPUT("DMic"),
 46};
 47
 48static const struct snd_soc_dapm_route intercon[] = {
 49	{"DMIC AIF", NULL, "DMic"},
 50};
 51
 52static int dmic_probe(struct snd_soc_codec *codec)
 53{
 54	struct snd_soc_dapm_context *dapm = &codec->dapm;
 55
 56	snd_soc_dapm_new_controls(dapm, dmic_dapm_widgets,
 57				  ARRAY_SIZE(dmic_dapm_widgets));
 58        snd_soc_dapm_add_routes(dapm, intercon, ARRAY_SIZE(intercon));
 59	snd_soc_dapm_new_widgets(dapm);
 60
 61	return 0;
 62}
 63
 64static struct snd_soc_codec_driver soc_dmic = {
 65	.probe	= dmic_probe,
 66};
 67
 68static int __devinit dmic_dev_probe(struct platform_device *pdev)
 69{
 70	return snd_soc_register_codec(&pdev->dev,
 71			&soc_dmic, &dmic_dai, 1);
 72}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 73
 74static int __devexit dmic_dev_remove(struct platform_device *pdev)
 75{
 76	snd_soc_unregister_codec(&pdev->dev);
 77	return 0;
 78}
 79
 80MODULE_ALIAS("platform:dmic-codec");
 81
 
 
 
 
 
 
 82static struct platform_driver dmic_driver = {
 83	.driver = {
 84		.name = "dmic-codec",
 85		.owner = THIS_MODULE,
 86	},
 87	.probe = dmic_dev_probe,
 88	.remove = __devexit_p(dmic_dev_remove),
 89};
 90
 91static int __init dmic_init(void)
 92{
 93	return platform_driver_register(&dmic_driver);
 94}
 95module_init(dmic_init);
 96
 97static void __exit dmic_exit(void)
 98{
 99	platform_driver_unregister(&dmic_driver);
100}
101module_exit(dmic_exit);
102
103MODULE_DESCRIPTION("Generic DMIC driver");
104MODULE_AUTHOR("Liam Girdwood <lrg@slimlogic.co.uk>");
105MODULE_LICENSE("GPL");