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