David Lambert | a710770 | 2011-01-06 08:00:37 -0600 | [diff] [blame] | 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 | |
Matthias Kaehlcke | 05c9b30 | 2018-02-16 09:53:12 -0800 | [diff] [blame] | 22 | #include <linux/delay.h> |
huang lin | 23c7159 | 2017-08-17 10:24:44 +0800 | [diff] [blame] | 23 | #include <linux/gpio.h> |
| 24 | #include <linux/gpio/consumer.h> |
David Lambert | a710770 | 2011-01-06 08:00:37 -0600 | [diff] [blame] | 25 | #include <linux/platform_device.h> |
| 26 | #include <linux/slab.h> |
Paul Gortmaker | da155d5 | 2011-07-15 12:38:28 -0400 | [diff] [blame] | 27 | #include <linux/module.h> |
David Lambert | a710770 | 2011-01-06 08:00:37 -0600 | [diff] [blame] | 28 | #include <sound/core.h> |
| 29 | #include <sound/pcm.h> |
| 30 | #include <sound/soc.h> |
| 31 | #include <sound/soc-dapm.h> |
| 32 | |
Jenny TC | bc0a7db | 2018-11-28 12:22:45 +0530 | [diff] [blame] | 33 | #define MAX_MODESWITCH_DELAY 70 |
| 34 | static int modeswitch_delay; |
| 35 | module_param(modeswitch_delay, uint, 0644); |
| 36 | |
Jenny TC | f6f30a6 | 2018-11-28 12:22:46 +0530 | [diff] [blame] | 37 | static int wakeup_delay; |
| 38 | module_param(wakeup_delay, uint, 0644); |
| 39 | |
Matthias Kaehlcke | 05c9b30 | 2018-02-16 09:53:12 -0800 | [diff] [blame] | 40 | struct dmic { |
| 41 | struct gpio_desc *gpio_en; |
| 42 | int wakeup_delay; |
Jenny TC | bc0a7db | 2018-11-28 12:22:45 +0530 | [diff] [blame] | 43 | /* Delay after DMIC mode switch */ |
| 44 | int modeswitch_delay; |
| 45 | }; |
| 46 | |
| 47 | int dmic_daiops_trigger(struct snd_pcm_substream *substream, |
| 48 | int cmd, struct snd_soc_dai *dai) |
| 49 | { |
| 50 | struct snd_soc_component *component = dai->component; |
| 51 | struct dmic *dmic = snd_soc_component_get_drvdata(component); |
| 52 | |
| 53 | switch (cmd) { |
| 54 | case SNDRV_PCM_TRIGGER_STOP: |
| 55 | if (dmic->modeswitch_delay) |
| 56 | mdelay(dmic->modeswitch_delay); |
| 57 | |
| 58 | break; |
| 59 | } |
| 60 | |
| 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | static const struct snd_soc_dai_ops dmic_dai_ops = { |
| 65 | .trigger = dmic_daiops_trigger, |
Matthias Kaehlcke | 05c9b30 | 2018-02-16 09:53:12 -0800 | [diff] [blame] | 66 | }; |
huang lin | 23c7159 | 2017-08-17 10:24:44 +0800 | [diff] [blame] | 67 | |
Matthias Kaehlcke | 05c9b30 | 2018-02-16 09:53:12 -0800 | [diff] [blame] | 68 | static int dmic_aif_event(struct snd_soc_dapm_widget *w, |
| 69 | struct snd_kcontrol *kcontrol, int event) { |
Kuninori Morimoto | 15b7c5d | 2018-02-21 02:06:29 +0000 | [diff] [blame] | 70 | struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm); |
| 71 | struct dmic *dmic = snd_soc_component_get_drvdata(component); |
huang lin | 23c7159 | 2017-08-17 10:24:44 +0800 | [diff] [blame] | 72 | |
Matthias Kaehlcke | 05c9b30 | 2018-02-16 09:53:12 -0800 | [diff] [blame] | 73 | switch (event) { |
| 74 | case SND_SOC_DAPM_POST_PMU: |
| 75 | if (dmic->gpio_en) |
| 76 | gpiod_set_value(dmic->gpio_en, 1); |
| 77 | |
| 78 | if (dmic->wakeup_delay) |
| 79 | msleep(dmic->wakeup_delay); |
huang lin | 23c7159 | 2017-08-17 10:24:44 +0800 | [diff] [blame] | 80 | break; |
Matthias Kaehlcke | 05c9b30 | 2018-02-16 09:53:12 -0800 | [diff] [blame] | 81 | case SND_SOC_DAPM_POST_PMD: |
| 82 | if (dmic->gpio_en) |
| 83 | gpiod_set_value(dmic->gpio_en, 0); |
huang lin | 23c7159 | 2017-08-17 10:24:44 +0800 | [diff] [blame] | 84 | break; |
| 85 | } |
| 86 | |
| 87 | return 0; |
| 88 | } |
| 89 | |
David Lambert | a710770 | 2011-01-06 08:00:37 -0600 | [diff] [blame] | 90 | static struct snd_soc_dai_driver dmic_dai = { |
| 91 | .name = "dmic-hifi", |
| 92 | .capture = { |
| 93 | .stream_name = "Capture", |
| 94 | .channels_min = 1, |
| 95 | .channels_max = 8, |
| 96 | .rates = SNDRV_PCM_RATE_CONTINUOUS, |
| 97 | .formats = SNDRV_PCM_FMTBIT_S32_LE |
| 98 | | SNDRV_PCM_FMTBIT_S24_LE |
| 99 | | SNDRV_PCM_FMTBIT_S16_LE, |
| 100 | }, |
Jenny TC | bc0a7db | 2018-11-28 12:22:45 +0530 | [diff] [blame] | 101 | .ops = &dmic_dai_ops, |
David Lambert | a710770 | 2011-01-06 08:00:37 -0600 | [diff] [blame] | 102 | }; |
| 103 | |
Kuninori Morimoto | 6d6c394 | 2018-01-29 04:40:29 +0000 | [diff] [blame] | 104 | static int dmic_component_probe(struct snd_soc_component *component) |
huang lin | 23c7159 | 2017-08-17 10:24:44 +0800 | [diff] [blame] | 105 | { |
Matthias Kaehlcke | 05c9b30 | 2018-02-16 09:53:12 -0800 | [diff] [blame] | 106 | struct dmic *dmic; |
huang lin | 23c7159 | 2017-08-17 10:24:44 +0800 | [diff] [blame] | 107 | |
Matthias Kaehlcke | 05c9b30 | 2018-02-16 09:53:12 -0800 | [diff] [blame] | 108 | dmic = devm_kzalloc(component->dev, sizeof(*dmic), GFP_KERNEL); |
| 109 | if (!dmic) |
| 110 | return -ENOMEM; |
huang lin | 23c7159 | 2017-08-17 10:24:44 +0800 | [diff] [blame] | 111 | |
Matthias Kaehlcke | 05c9b30 | 2018-02-16 09:53:12 -0800 | [diff] [blame] | 112 | dmic->gpio_en = devm_gpiod_get_optional(component->dev, |
| 113 | "dmicen", GPIOD_OUT_LOW); |
| 114 | if (IS_ERR(dmic->gpio_en)) |
| 115 | return PTR_ERR(dmic->gpio_en); |
| 116 | |
| 117 | device_property_read_u32(component->dev, "wakeup-delay-ms", |
| 118 | &dmic->wakeup_delay); |
Jenny TC | bc0a7db | 2018-11-28 12:22:45 +0530 | [diff] [blame] | 119 | device_property_read_u32(component->dev, "modeswitch-delay-ms", |
| 120 | &dmic->modeswitch_delay); |
Jenny TC | f6f30a6 | 2018-11-28 12:22:46 +0530 | [diff] [blame] | 121 | if (wakeup_delay) |
| 122 | dmic->wakeup_delay = wakeup_delay; |
Jenny TC | bc0a7db | 2018-11-28 12:22:45 +0530 | [diff] [blame] | 123 | if (modeswitch_delay) |
| 124 | dmic->modeswitch_delay = modeswitch_delay; |
| 125 | |
| 126 | if (dmic->modeswitch_delay > MAX_MODESWITCH_DELAY) |
| 127 | dmic->modeswitch_delay = MAX_MODESWITCH_DELAY; |
Matthias Kaehlcke | 05c9b30 | 2018-02-16 09:53:12 -0800 | [diff] [blame] | 128 | |
| 129 | snd_soc_component_set_drvdata(component, dmic); |
huang lin | 23c7159 | 2017-08-17 10:24:44 +0800 | [diff] [blame] | 130 | |
| 131 | return 0; |
| 132 | } |
| 133 | |
Misael Lopez Cruz | d5e4b0a | 2011-05-12 16:26:20 +0100 | [diff] [blame] | 134 | static const struct snd_soc_dapm_widget dmic_dapm_widgets[] = { |
Matthias Kaehlcke | 05c9b30 | 2018-02-16 09:53:12 -0800 | [diff] [blame] | 135 | SND_SOC_DAPM_AIF_OUT_E("DMIC AIF", "Capture", 0, |
| 136 | SND_SOC_NOPM, 0, 0, dmic_aif_event, |
| 137 | SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD), |
Misael Lopez Cruz | d5e4b0a | 2011-05-12 16:26:20 +0100 | [diff] [blame] | 138 | SND_SOC_DAPM_INPUT("DMic"), |
| 139 | }; |
| 140 | |
| 141 | static const struct snd_soc_dapm_route intercon[] = { |
| 142 | {"DMIC AIF", NULL, "DMic"}, |
| 143 | }; |
| 144 | |
Kuninori Morimoto | 6d6c394 | 2018-01-29 04:40:29 +0000 | [diff] [blame] | 145 | static const struct snd_soc_component_driver soc_dmic = { |
| 146 | .probe = dmic_component_probe, |
| 147 | .dapm_widgets = dmic_dapm_widgets, |
| 148 | .num_dapm_widgets = ARRAY_SIZE(dmic_dapm_widgets), |
| 149 | .dapm_routes = intercon, |
| 150 | .num_dapm_routes = ARRAY_SIZE(intercon), |
| 151 | .idle_bias_on = 1, |
| 152 | .use_pmdown_time = 1, |
| 153 | .endianness = 1, |
| 154 | .non_legacy_dai_naming = 1, |
Misael Lopez Cruz | d5e4b0a | 2011-05-12 16:26:20 +0100 | [diff] [blame] | 155 | }; |
David Lambert | a710770 | 2011-01-06 08:00:37 -0600 | [diff] [blame] | 156 | |
Bill Pemberton | 7a79e94 | 2012-12-07 09:26:37 -0500 | [diff] [blame] | 157 | static int dmic_dev_probe(struct platform_device *pdev) |
David Lambert | a710770 | 2011-01-06 08:00:37 -0600 | [diff] [blame] | 158 | { |
Matthias Kaehlcke | 7fb59e9 | 2018-01-05 12:39:57 -0800 | [diff] [blame] | 159 | int err; |
| 160 | u32 chans; |
| 161 | struct snd_soc_dai_driver *dai_drv = &dmic_dai; |
| 162 | |
| 163 | if (pdev->dev.of_node) { |
| 164 | err = of_property_read_u32(pdev->dev.of_node, "num-channels", &chans); |
Matthias Kaehlcke | 35b84bf | 2018-01-19 15:36:50 -0800 | [diff] [blame] | 165 | if (err && (err != -EINVAL)) |
Matthias Kaehlcke | 7fb59e9 | 2018-01-05 12:39:57 -0800 | [diff] [blame] | 166 | return err; |
| 167 | |
| 168 | if (!err) { |
| 169 | if (chans < 1 || chans > 8) |
| 170 | return -EINVAL; |
| 171 | |
| 172 | dai_drv = devm_kzalloc(&pdev->dev, sizeof(*dai_drv), GFP_KERNEL); |
| 173 | if (!dai_drv) |
| 174 | return -ENOMEM; |
| 175 | |
| 176 | memcpy(dai_drv, &dmic_dai, sizeof(*dai_drv)); |
| 177 | dai_drv->capture.channels_max = chans; |
| 178 | } |
| 179 | } |
| 180 | |
Kuninori Morimoto | 6d6c394 | 2018-01-29 04:40:29 +0000 | [diff] [blame] | 181 | return devm_snd_soc_register_component(&pdev->dev, |
Matthias Kaehlcke | 7fb59e9 | 2018-01-05 12:39:57 -0800 | [diff] [blame] | 182 | &soc_dmic, dai_drv, 1); |
David Lambert | a710770 | 2011-01-06 08:00:37 -0600 | [diff] [blame] | 183 | } |
| 184 | |
David Lambert | a710770 | 2011-01-06 08:00:37 -0600 | [diff] [blame] | 185 | MODULE_ALIAS("platform:dmic-codec"); |
| 186 | |
Arnaud Pouliquen | 29685e2 | 2017-07-25 10:48:14 +0200 | [diff] [blame] | 187 | static const struct of_device_id dmic_dev_match[] = { |
| 188 | {.compatible = "dmic-codec"}, |
| 189 | {} |
| 190 | }; |
Jerome Brunet | cb06a03 | 2018-08-29 17:00:49 +0200 | [diff] [blame] | 191 | MODULE_DEVICE_TABLE(of, dmic_dev_match); |
Arnaud Pouliquen | 29685e2 | 2017-07-25 10:48:14 +0200 | [diff] [blame] | 192 | |
David Lambert | a710770 | 2011-01-06 08:00:37 -0600 | [diff] [blame] | 193 | static struct platform_driver dmic_driver = { |
| 194 | .driver = { |
| 195 | .name = "dmic-codec", |
Arnaud Pouliquen | 29685e2 | 2017-07-25 10:48:14 +0200 | [diff] [blame] | 196 | .of_match_table = dmic_dev_match, |
David Lambert | a710770 | 2011-01-06 08:00:37 -0600 | [diff] [blame] | 197 | }, |
| 198 | .probe = dmic_dev_probe, |
David Lambert | a710770 | 2011-01-06 08:00:37 -0600 | [diff] [blame] | 199 | }; |
| 200 | |
Mark Brown | 5bbcc3c | 2011-11-23 22:52:08 +0000 | [diff] [blame] | 201 | module_platform_driver(dmic_driver); |
David Lambert | a710770 | 2011-01-06 08:00:37 -0600 | [diff] [blame] | 202 | |
| 203 | MODULE_DESCRIPTION("Generic DMIC driver"); |
| 204 | MODULE_AUTHOR("Liam Girdwood <lrg@slimlogic.co.uk>"); |
| 205 | MODULE_LICENSE("GPL"); |