Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /* Copyright (c) 2010-2011,2013-2015 The Linux Foundation. All rights reserved.
0003  *
0004  * max98357a.c -- MAX98357A ALSA SoC Codec driver
0005  */
0006 
0007 #include <linux/acpi.h>
0008 #include <linux/delay.h>
0009 #include <linux/device.h>
0010 #include <linux/err.h>
0011 #include <linux/gpio.h>
0012 #include <linux/gpio/consumer.h>
0013 #include <linux/kernel.h>
0014 #include <linux/mod_devicetable.h>
0015 #include <linux/module.h>
0016 #include <linux/of.h>
0017 #include <linux/platform_device.h>
0018 #include <sound/pcm.h>
0019 #include <sound/soc.h>
0020 #include <sound/soc-dai.h>
0021 #include <sound/soc-dapm.h>
0022 
0023 struct max98357a_priv {
0024     struct gpio_desc *sdmode;
0025     unsigned int sdmode_delay;
0026     int sdmode_switch;
0027 };
0028 
0029 static int max98357a_daiops_trigger(struct snd_pcm_substream *substream,
0030         int cmd, struct snd_soc_dai *dai)
0031 {
0032     struct snd_soc_component *component = dai->component;
0033     struct max98357a_priv *max98357a =
0034         snd_soc_component_get_drvdata(component);
0035 
0036     if (!max98357a->sdmode)
0037         return 0;
0038 
0039     switch (cmd) {
0040     case SNDRV_PCM_TRIGGER_START:
0041     case SNDRV_PCM_TRIGGER_RESUME:
0042     case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
0043         mdelay(max98357a->sdmode_delay);
0044         if (max98357a->sdmode_switch) {
0045             gpiod_set_value(max98357a->sdmode, 1);
0046             dev_dbg(component->dev, "set sdmode to 1");
0047         }
0048         break;
0049     case SNDRV_PCM_TRIGGER_STOP:
0050     case SNDRV_PCM_TRIGGER_SUSPEND:
0051     case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
0052         gpiod_set_value(max98357a->sdmode, 0);
0053         dev_dbg(component->dev, "set sdmode to 0");
0054         break;
0055     }
0056 
0057     return 0;
0058 }
0059 
0060 static int max98357a_sdmode_event(struct snd_soc_dapm_widget *w,
0061         struct snd_kcontrol *kcontrol, int event)
0062 {
0063     struct snd_soc_component *component =
0064         snd_soc_dapm_to_component(w->dapm);
0065     struct max98357a_priv *max98357a =
0066         snd_soc_component_get_drvdata(component);
0067 
0068     if (event & SND_SOC_DAPM_POST_PMU)
0069         max98357a->sdmode_switch = 1;
0070     else if (event & SND_SOC_DAPM_POST_PMD)
0071         max98357a->sdmode_switch = 0;
0072 
0073     return 0;
0074 }
0075 
0076 static const struct snd_soc_dapm_widget max98357a_dapm_widgets[] = {
0077     SND_SOC_DAPM_OUTPUT("Speaker"),
0078     SND_SOC_DAPM_OUT_DRV_E("SD_MODE", SND_SOC_NOPM, 0, 0, NULL, 0,
0079             max98357a_sdmode_event,
0080             SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
0081 };
0082 
0083 static const struct snd_soc_dapm_route max98357a_dapm_routes[] = {
0084     {"SD_MODE", NULL, "HiFi Playback"},
0085     {"Speaker", NULL, "SD_MODE"},
0086 };
0087 
0088 static const struct snd_soc_component_driver max98357a_component_driver = {
0089     .dapm_widgets       = max98357a_dapm_widgets,
0090     .num_dapm_widgets   = ARRAY_SIZE(max98357a_dapm_widgets),
0091     .dapm_routes        = max98357a_dapm_routes,
0092     .num_dapm_routes    = ARRAY_SIZE(max98357a_dapm_routes),
0093     .idle_bias_on       = 1,
0094     .use_pmdown_time    = 1,
0095     .endianness     = 1,
0096 };
0097 
0098 static const struct snd_soc_dai_ops max98357a_dai_ops = {
0099     .trigger        = max98357a_daiops_trigger,
0100 };
0101 
0102 static struct snd_soc_dai_driver max98357a_dai_driver = {
0103     .name = "HiFi",
0104     .playback = {
0105         .stream_name    = "HiFi Playback",
0106         .formats    = SNDRV_PCM_FMTBIT_S16 |
0107                     SNDRV_PCM_FMTBIT_S24 |
0108                     SNDRV_PCM_FMTBIT_S32,
0109         .rates      = SNDRV_PCM_RATE_8000 |
0110                     SNDRV_PCM_RATE_16000 |
0111                     SNDRV_PCM_RATE_32000 |
0112                     SNDRV_PCM_RATE_44100 |
0113                     SNDRV_PCM_RATE_48000 |
0114                     SNDRV_PCM_RATE_88200 |
0115                     SNDRV_PCM_RATE_96000,
0116         .rate_min   = 8000,
0117         .rate_max   = 96000,
0118         .channels_min   = 1,
0119         .channels_max   = 2,
0120     },
0121     .ops    = &max98357a_dai_ops,
0122 };
0123 
0124 static int max98357a_platform_probe(struct platform_device *pdev)
0125 {
0126     struct max98357a_priv *max98357a;
0127     int ret;
0128 
0129     max98357a = devm_kzalloc(&pdev->dev, sizeof(*max98357a), GFP_KERNEL);
0130     if (!max98357a)
0131         return -ENOMEM;
0132 
0133     max98357a->sdmode = devm_gpiod_get_optional(&pdev->dev,
0134                 "sdmode", GPIOD_OUT_LOW);
0135     if (IS_ERR(max98357a->sdmode))
0136         return PTR_ERR(max98357a->sdmode);
0137 
0138     ret = device_property_read_u32(&pdev->dev, "sdmode-delay",
0139                     &max98357a->sdmode_delay);
0140     if (ret) {
0141         max98357a->sdmode_delay = 0;
0142         dev_dbg(&pdev->dev,
0143             "no optional property 'sdmode-delay' found, "
0144             "default: no delay\n");
0145     }
0146 
0147     dev_set_drvdata(&pdev->dev, max98357a);
0148 
0149     return devm_snd_soc_register_component(&pdev->dev,
0150             &max98357a_component_driver,
0151             &max98357a_dai_driver, 1);
0152 }
0153 
0154 #ifdef CONFIG_OF
0155 static const struct of_device_id max98357a_device_id[] = {
0156     { .compatible = "maxim,max98357a" },
0157     { .compatible = "maxim,max98360a" },
0158     {}
0159 };
0160 MODULE_DEVICE_TABLE(of, max98357a_device_id);
0161 #endif
0162 
0163 #ifdef CONFIG_ACPI
0164 static const struct acpi_device_id max98357a_acpi_match[] = {
0165     { "MX98357A", 0 },
0166     { "MX98360A", 0 },
0167     {},
0168 };
0169 MODULE_DEVICE_TABLE(acpi, max98357a_acpi_match);
0170 #endif
0171 
0172 static struct platform_driver max98357a_platform_driver = {
0173     .driver = {
0174         .name = "max98357a",
0175         .of_match_table = of_match_ptr(max98357a_device_id),
0176         .acpi_match_table = ACPI_PTR(max98357a_acpi_match),
0177     },
0178     .probe  = max98357a_platform_probe,
0179 };
0180 module_platform_driver(max98357a_platform_driver);
0181 
0182 MODULE_DESCRIPTION("Maxim MAX98357A Codec Driver");
0183 MODULE_LICENSE("GPL v2");