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