Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * dmic.c  --  SoC audio for Generic Digital MICs
0004  *
0005  * Author: Liam Girdwood <lrg@slimlogic.co.uk>
0006  */
0007 
0008 #include <linux/delay.h>
0009 #include <linux/gpio.h>
0010 #include <linux/gpio/consumer.h>
0011 #include <linux/platform_device.h>
0012 #include <linux/slab.h>
0013 #include <linux/module.h>
0014 #include <sound/core.h>
0015 #include <sound/pcm.h>
0016 #include <sound/soc.h>
0017 #include <sound/soc-dapm.h>
0018 
0019 #define MAX_MODESWITCH_DELAY 70
0020 static int modeswitch_delay;
0021 module_param(modeswitch_delay, uint, 0644);
0022 
0023 static int wakeup_delay;
0024 module_param(wakeup_delay, uint, 0644);
0025 
0026 struct dmic {
0027     struct gpio_desc *gpio_en;
0028     int wakeup_delay;
0029     /* Delay after DMIC mode switch */
0030     int modeswitch_delay;
0031 };
0032 
0033 static int dmic_daiops_trigger(struct snd_pcm_substream *substream,
0034                    int cmd, struct snd_soc_dai *dai)
0035 {
0036     struct snd_soc_component *component = dai->component;
0037     struct dmic *dmic = snd_soc_component_get_drvdata(component);
0038 
0039     switch (cmd) {
0040     case SNDRV_PCM_TRIGGER_STOP:
0041         if (dmic->modeswitch_delay)
0042             mdelay(dmic->modeswitch_delay);
0043 
0044         break;
0045     }
0046 
0047     return 0;
0048 }
0049 
0050 static const struct snd_soc_dai_ops dmic_dai_ops = {
0051     .trigger    = dmic_daiops_trigger,
0052 };
0053 
0054 static int dmic_aif_event(struct snd_soc_dapm_widget *w,
0055               struct snd_kcontrol *kcontrol, int event) {
0056     struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
0057     struct dmic *dmic = snd_soc_component_get_drvdata(component);
0058 
0059     switch (event) {
0060     case SND_SOC_DAPM_POST_PMU:
0061         if (dmic->gpio_en)
0062             gpiod_set_value_cansleep(dmic->gpio_en, 1);
0063 
0064         if (dmic->wakeup_delay)
0065             msleep(dmic->wakeup_delay);
0066         break;
0067     case SND_SOC_DAPM_POST_PMD:
0068         if (dmic->gpio_en)
0069             gpiod_set_value_cansleep(dmic->gpio_en, 0);
0070         break;
0071     }
0072 
0073     return 0;
0074 }
0075 
0076 static struct snd_soc_dai_driver dmic_dai = {
0077     .name = "dmic-hifi",
0078     .capture = {
0079         .stream_name = "Capture",
0080         .channels_min = 1,
0081         .channels_max = 8,
0082         .rates = SNDRV_PCM_RATE_CONTINUOUS,
0083         .formats = SNDRV_PCM_FMTBIT_S32_LE
0084             | SNDRV_PCM_FMTBIT_S24_LE
0085             | SNDRV_PCM_FMTBIT_S16_LE
0086             | SNDRV_PCM_FMTBIT_DSD_U8
0087             | SNDRV_PCM_FMTBIT_DSD_U16_LE
0088             | SNDRV_PCM_FMTBIT_DSD_U32_LE,
0089     },
0090     .ops    = &dmic_dai_ops,
0091 };
0092 
0093 static int dmic_component_probe(struct snd_soc_component *component)
0094 {
0095     struct dmic *dmic;
0096 
0097     dmic = devm_kzalloc(component->dev, sizeof(*dmic), GFP_KERNEL);
0098     if (!dmic)
0099         return -ENOMEM;
0100 
0101     dmic->gpio_en = devm_gpiod_get_optional(component->dev,
0102                         "dmicen", GPIOD_OUT_LOW);
0103     if (IS_ERR(dmic->gpio_en))
0104         return PTR_ERR(dmic->gpio_en);
0105 
0106     device_property_read_u32(component->dev, "wakeup-delay-ms",
0107                  &dmic->wakeup_delay);
0108     device_property_read_u32(component->dev, "modeswitch-delay-ms",
0109                  &dmic->modeswitch_delay);
0110     if (wakeup_delay)
0111         dmic->wakeup_delay  = wakeup_delay;
0112     if (modeswitch_delay)
0113         dmic->modeswitch_delay  = modeswitch_delay;
0114 
0115     if (dmic->modeswitch_delay > MAX_MODESWITCH_DELAY)
0116         dmic->modeswitch_delay = MAX_MODESWITCH_DELAY;
0117 
0118     snd_soc_component_set_drvdata(component, dmic);
0119 
0120     return 0;
0121 }
0122 
0123 static const struct snd_soc_dapm_widget dmic_dapm_widgets[] = {
0124     SND_SOC_DAPM_AIF_OUT_E("DMIC AIF", "Capture", 0,
0125                    SND_SOC_NOPM, 0, 0, dmic_aif_event,
0126                    SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
0127     SND_SOC_DAPM_INPUT("DMic"),
0128 };
0129 
0130 static const struct snd_soc_dapm_route intercon[] = {
0131     {"DMIC AIF", NULL, "DMic"},
0132 };
0133 
0134 static const struct snd_soc_component_driver soc_dmic = {
0135     .probe          = dmic_component_probe,
0136     .dapm_widgets       = dmic_dapm_widgets,
0137     .num_dapm_widgets   = ARRAY_SIZE(dmic_dapm_widgets),
0138     .dapm_routes        = intercon,
0139     .num_dapm_routes    = ARRAY_SIZE(intercon),
0140     .idle_bias_on       = 1,
0141     .use_pmdown_time    = 1,
0142     .endianness     = 1,
0143 };
0144 
0145 static int dmic_dev_probe(struct platform_device *pdev)
0146 {
0147     int err;
0148     u32 chans;
0149     struct snd_soc_dai_driver *dai_drv = &dmic_dai;
0150 
0151     if (pdev->dev.of_node) {
0152         err = of_property_read_u32(pdev->dev.of_node, "num-channels", &chans);
0153         if (err && (err != -EINVAL))
0154             return err;
0155 
0156         if (!err) {
0157             if (chans < 1 || chans > 8)
0158                 return -EINVAL;
0159 
0160             dai_drv = devm_kzalloc(&pdev->dev, sizeof(*dai_drv), GFP_KERNEL);
0161             if (!dai_drv)
0162                 return -ENOMEM;
0163 
0164             memcpy(dai_drv, &dmic_dai, sizeof(*dai_drv));
0165             dai_drv->capture.channels_max = chans;
0166         }
0167     }
0168 
0169     return devm_snd_soc_register_component(&pdev->dev,
0170             &soc_dmic, dai_drv, 1);
0171 }
0172 
0173 MODULE_ALIAS("platform:dmic-codec");
0174 
0175 static const struct of_device_id dmic_dev_match[] = {
0176     {.compatible = "dmic-codec"},
0177     {}
0178 };
0179 MODULE_DEVICE_TABLE(of, dmic_dev_match);
0180 
0181 static struct platform_driver dmic_driver = {
0182     .driver = {
0183         .name = "dmic-codec",
0184         .of_match_table = dmic_dev_match,
0185     },
0186     .probe = dmic_dev_probe,
0187 };
0188 
0189 module_platform_driver(dmic_driver);
0190 
0191 MODULE_DESCRIPTION("Generic DMIC driver");
0192 MODULE_AUTHOR("Liam Girdwood <lrg@slimlogic.co.uk>");
0193 MODULE_LICENSE("GPL");