Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 //
0003 // nau8315.c  --  NAU8315 ALSA SoC Audio Amplifier Driver
0004 //
0005 // Copyright 2020 Nuvoton Technology Crop.
0006 //
0007 // Author: David Lin <ctlin0@nuvoton.com>
0008 //
0009 // Based on MAX98357A.c
0010 
0011 #include <linux/acpi.h>
0012 #include <linux/device.h>
0013 #include <linux/err.h>
0014 #include <linux/gpio.h>
0015 #include <linux/gpio/consumer.h>
0016 #include <linux/kernel.h>
0017 #include <linux/module.h>
0018 #include <linux/of.h>
0019 #include <linux/platform_device.h>
0020 #include <sound/pcm.h>
0021 #include <sound/soc.h>
0022 #include <sound/soc-dai.h>
0023 #include <sound/soc-dapm.h>
0024 
0025 struct nau8315_priv {
0026     struct gpio_desc *enable;
0027     int enpin_switch;
0028 };
0029 
0030 static int nau8315_daiops_trigger(struct snd_pcm_substream *substream,
0031         int cmd, struct snd_soc_dai *dai)
0032 {
0033     struct snd_soc_component *component = dai->component;
0034     struct nau8315_priv *nau8315 =
0035         snd_soc_component_get_drvdata(component);
0036 
0037     if (!nau8315->enable)
0038         return 0;
0039 
0040     switch (cmd) {
0041     case SNDRV_PCM_TRIGGER_START:
0042     case SNDRV_PCM_TRIGGER_RESUME:
0043     case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
0044         if (nau8315->enpin_switch) {
0045             gpiod_set_value(nau8315->enable, 1);
0046             dev_dbg(component->dev, "set enable 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(nau8315->enable, 0);
0053         dev_dbg(component->dev, "set enable to 0");
0054         break;
0055     }
0056 
0057     return 0;
0058 }
0059 
0060 static int nau8315_enpin_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 nau8315_priv *nau8315 =
0066         snd_soc_component_get_drvdata(component);
0067 
0068     if (event & SND_SOC_DAPM_PRE_PMU)
0069         nau8315->enpin_switch = 1;
0070     else if (event & SND_SOC_DAPM_POST_PMD)
0071         nau8315->enpin_switch = 0;
0072 
0073     return 0;
0074 }
0075 
0076 static const struct snd_soc_dapm_widget nau8315_dapm_widgets[] = {
0077     SND_SOC_DAPM_OUTPUT("Speaker"),
0078     SND_SOC_DAPM_OUT_DRV_E("EN_Pin", SND_SOC_NOPM, 0, 0, NULL, 0,
0079             nau8315_enpin_event,
0080             SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
0081 };
0082 
0083 static const struct snd_soc_dapm_route nau8315_dapm_routes[] = {
0084     {"EN_Pin", NULL, "HiFi Playback"},
0085     {"Speaker", NULL, "EN_Pin"},
0086 };
0087 
0088 static const struct snd_soc_component_driver nau8315_component_driver = {
0089     .dapm_widgets       = nau8315_dapm_widgets,
0090     .num_dapm_widgets   = ARRAY_SIZE(nau8315_dapm_widgets),
0091     .dapm_routes        = nau8315_dapm_routes,
0092     .num_dapm_routes    = ARRAY_SIZE(nau8315_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 nau8315_dai_ops = {
0099     .trigger    = nau8315_daiops_trigger,
0100 };
0101 
0102 #define NAU8315_RATES SNDRV_PCM_RATE_8000_96000
0103 #define NAU8315_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_3LE)
0104 
0105 static struct snd_soc_dai_driver nau8315_dai_driver = {
0106     .name = "nau8315-hifi",
0107     .playback = {
0108         .stream_name    = "HiFi Playback",
0109         .formats    = NAU8315_FORMATS,
0110         .rates      = NAU8315_RATES,
0111         .channels_min   = 1,
0112         .channels_max   = 2,
0113     },
0114     .ops    = &nau8315_dai_ops,
0115 };
0116 
0117 static int nau8315_platform_probe(struct platform_device *pdev)
0118 {
0119     struct nau8315_priv *nau8315;
0120 
0121     nau8315 = devm_kzalloc(&pdev->dev, sizeof(*nau8315), GFP_KERNEL);
0122     if (!nau8315)
0123         return -ENOMEM;
0124 
0125     nau8315->enable = devm_gpiod_get_optional(&pdev->dev,
0126                 "enable", GPIOD_OUT_LOW);
0127     if (IS_ERR(nau8315->enable))
0128         return PTR_ERR(nau8315->enable);
0129 
0130     dev_set_drvdata(&pdev->dev, nau8315);
0131 
0132     return devm_snd_soc_register_component(&pdev->dev,
0133             &nau8315_component_driver,
0134             &nau8315_dai_driver, 1);
0135 }
0136 
0137 #ifdef CONFIG_OF
0138 static const struct of_device_id nau8315_device_id[] = {
0139     { .compatible = "nuvoton,nau8315" },
0140     {}
0141 };
0142 MODULE_DEVICE_TABLE(of, nau8315_device_id);
0143 #endif
0144 
0145 #ifdef CONFIG_ACPI
0146 static const struct acpi_device_id nau8315_acpi_match[] = {
0147     { "NVTN2010", 0 },
0148     {},
0149 };
0150 MODULE_DEVICE_TABLE(acpi, nau8315_acpi_match);
0151 #endif
0152 
0153 static struct platform_driver nau8315_platform_driver = {
0154     .driver = {
0155         .name = "nau8315",
0156         .of_match_table = of_match_ptr(nau8315_device_id),
0157         .acpi_match_table = ACPI_PTR(nau8315_acpi_match),
0158     },
0159     .probe  = nau8315_platform_probe,
0160 };
0161 module_platform_driver(nau8315_platform_driver);
0162 
0163 MODULE_DESCRIPTION("ASoC NAU8315 Mono Class-D Amplifier Driver");
0164 MODULE_AUTHOR("David Lin <ctlin0@nuvoton.com>");
0165 MODULE_LICENSE("GPL v2");