| 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)) { |
| 118 |
if (PTR_ERR(dmic->vref) != -ENODEV) |
| 119 |
return dev_err_probe(component->dev, PTR_ERR(dmic->vref), |
| 120 |
"Failed to get vref\n"); |
| 121 |
dmic->vref = NULL; |
| 122 |
} |
| 123 |
|
| 124 |
dmic->gpio_en = devm_gpiod_get_optional(component->dev, |
| 125 |
"dmicen", GPIOD_OUT_LOW); |
| 126 |
if (IS_ERR(dmic->gpio_en)) |
| 127 |
return PTR_ERR(dmic->gpio_en); |
| 128 |
|
| 129 |
device_property_read_u32(component->dev, "wakeup-delay-ms", |
| 130 |
&dmic->wakeup_delay); |
| 131 |
device_property_read_u32(component->dev, "modeswitch-delay-ms", |
| 132 |
&dmic->modeswitch_delay); |
| 133 |
if (wakeup_delay) |
| 134 |
dmic->wakeup_delay = wakeup_delay; |
| 135 |
if (modeswitch_delay) |
| 136 |
dmic->modeswitch_delay = modeswitch_delay; |
| 137 |
|
| 138 |
if (dmic->modeswitch_delay > MAX_MODESWITCH_DELAY) |
| 139 |
dmic->modeswitch_delay = MAX_MODESWITCH_DELAY; |
| 140 |
|
| 141 |
snd_soc_component_set_drvdata(component, dmic); |
| 142 |
|
| 143 |
return 0; |
| 144 |
} |
| 145 |
|
| 146 |
static const struct snd_soc_dapm_widget dmic_dapm_widgets[] = { |
| 147 |
SND_SOC_DAPM_AIF_OUT_E("DMIC AIF", "Capture", 0, |
| 148 |
SND_SOC_NOPM, 0, 0, dmic_aif_event, |
| 149 |
SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD), |
| 150 |
SND_SOC_DAPM_INPUT("DMic"), |
| 151 |
}; |
| 152 |
|
| 153 |
static const struct snd_soc_dapm_route intercon[] = { |
| 154 |
{"DMIC AIF", NULL, "DMic"}, |
| 155 |
}; |
| 156 |
|
| 157 |
static const struct snd_soc_component_driver soc_dmic = { |
| 158 |
.probe = dmic_component_probe, |
| 159 |
.dapm_widgets = dmic_dapm_widgets, |
| 160 |
.num_dapm_widgets = ARRAY_SIZE(dmic_dapm_widgets), |
| 161 |
.dapm_routes = intercon, |
| 162 |
.num_dapm_routes = ARRAY_SIZE(intercon), |
| 163 |
.idle_bias_on = 1, |
| 164 |
.use_pmdown_time = 1, |
| 165 |
.endianness = 1, |
| 166 |
}; |
| 167 |
|
| 168 |
static int dmic_dev_probe(struct platform_device *pdev) |
| 169 |
{ |
| 170 |
int err; |
| 171 |
u32 chans; |
| 172 |
struct snd_soc_dai_driver *dai_drv = &dmic_dai; |
| 173 |
|
| 174 |
if (pdev->dev.of_node) { |
| 175 |
err = of_property_read_u32(pdev->dev.of_node, "num-channels", &chans); |
| 176 |
if (err && (err != -EINVAL)) |
| 177 |
return err; |
| 178 |
|
| 179 |
if (!err) { |
| 180 |
if (chans < 1 || chans > 8) |
| 181 |
return -EINVAL; |
| 182 |
|
| 183 |
dai_drv = devm_kzalloc(&pdev->dev, sizeof(*dai_drv), GFP_KERNEL); |
| 184 |
if (!dai_drv) |
| 185 |
return -ENOMEM; |
| 186 |
|
| 187 |
memcpy(dai_drv, &dmic_dai, sizeof(*dai_drv)); |
| 188 |
dai_drv->capture.channels_max = chans; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
return devm_snd_soc_register_component(&pdev->dev, |
| 193 |
&soc_dmic, dai_drv, 1); |
| 194 |
} |
| 195 |
|
| 196 |
MODULE_ALIAS("platform:dmic-codec"); |
| 197 |
|
| 198 |
static const struct of_device_id dmic_dev_match[] = { |
| 199 |
{.compatible = "dmic-codec"}, |
| 200 |
{} |
| 201 |
}; |
| 202 |
MODULE_DEVICE_TABLE(of, dmic_dev_match); |
| 203 |
|
| 204 |
static struct platform_driver dmic_driver = { |
| 205 |
.driver = { |
| 206 |
.name = "dmic-codec", |
| 207 |
.of_match_table = dmic_dev_match, |
| 208 |
}, |
| 209 |
.probe = dmic_dev_probe, |
| 210 |
}; |
| 211 |
|
| 212 |
module_platform_driver(dmic_driver); |
| 213 |
|
| 214 |
MODULE_DESCRIPTION("Generic DMIC driver"); |
| 215 |
MODULE_AUTHOR("Liam Girdwood <[email protected]>"); |
| 216 |
MODULE_LICENSE("GPL"); |