Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 //
0003 // rt1015p.c  --  RT1015P ALSA SoC audio amplifier driver
0004 //
0005 // Copyright 2020 The Linux Foundation. All rights reserved.
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/module.h>
0015 #include <linux/of.h>
0016 #include <linux/platform_device.h>
0017 #include <sound/pcm.h>
0018 #include <sound/soc.h>
0019 #include <sound/soc-dai.h>
0020 #include <sound/soc-dapm.h>
0021 
0022 struct rt1015p_priv {
0023     struct gpio_desc *sdb;
0024     bool calib_done;
0025 };
0026 
0027 static int rt1015p_sdb_event(struct snd_soc_dapm_widget *w,
0028         struct snd_kcontrol *kcontrol, int event)
0029 {
0030     struct snd_soc_component *component =
0031         snd_soc_dapm_to_component(w->dapm);
0032     struct rt1015p_priv *rt1015p =
0033         snd_soc_component_get_drvdata(component);
0034 
0035     if (!rt1015p->sdb)
0036         return 0;
0037 
0038     switch (event) {
0039     case SND_SOC_DAPM_PRE_PMU:
0040         gpiod_set_value_cansleep(rt1015p->sdb, 1);
0041         dev_dbg(component->dev, "set sdb to 1");
0042 
0043         if (!rt1015p->calib_done) {
0044             msleep(300);
0045             rt1015p->calib_done = true;
0046         }
0047         break;
0048     case SND_SOC_DAPM_POST_PMD:
0049         gpiod_set_value_cansleep(rt1015p->sdb, 0);
0050         dev_dbg(component->dev, "set sdb to 0");
0051         break;
0052     default:
0053         break;
0054     }
0055 
0056     return 0;
0057 }
0058 
0059 static const struct snd_soc_dapm_widget rt1015p_dapm_widgets[] = {
0060     SND_SOC_DAPM_OUTPUT("Speaker"),
0061     SND_SOC_DAPM_OUT_DRV_E("SDB", SND_SOC_NOPM, 0, 0, NULL, 0,
0062             rt1015p_sdb_event,
0063             SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
0064 };
0065 
0066 static const struct snd_soc_dapm_route rt1015p_dapm_routes[] = {
0067     {"SDB", NULL, "HiFi Playback"},
0068     {"Speaker", NULL, "SDB"},
0069 };
0070 
0071 #ifdef CONFIG_PM
0072 static int rt1015p_suspend(struct snd_soc_component *component)
0073 {
0074     struct rt1015p_priv *rt1015p = snd_soc_component_get_drvdata(component);
0075 
0076     rt1015p->calib_done = false;
0077     return 0;
0078 }
0079 #else
0080 #define rt1015p_suspend NULL
0081 #endif
0082 
0083 static const struct snd_soc_component_driver rt1015p_component_driver = {
0084     .suspend        = rt1015p_suspend,
0085     .dapm_widgets       = rt1015p_dapm_widgets,
0086     .num_dapm_widgets   = ARRAY_SIZE(rt1015p_dapm_widgets),
0087     .dapm_routes        = rt1015p_dapm_routes,
0088     .num_dapm_routes    = ARRAY_SIZE(rt1015p_dapm_routes),
0089     .idle_bias_on       = 1,
0090     .use_pmdown_time    = 1,
0091     .endianness     = 1,
0092 };
0093 
0094 static struct snd_soc_dai_driver rt1015p_dai_driver = {
0095     .name = "HiFi",
0096     .playback = {
0097         .stream_name    = "HiFi Playback",
0098         .formats    = SNDRV_PCM_FMTBIT_S24 |
0099                     SNDRV_PCM_FMTBIT_S32,
0100         .rates      = SNDRV_PCM_RATE_48000,
0101         .channels_min   = 1,
0102         .channels_max   = 2,
0103     },
0104 };
0105 
0106 static int rt1015p_platform_probe(struct platform_device *pdev)
0107 {
0108     struct rt1015p_priv *rt1015p;
0109 
0110     rt1015p = devm_kzalloc(&pdev->dev, sizeof(*rt1015p), GFP_KERNEL);
0111     if (!rt1015p)
0112         return -ENOMEM;
0113 
0114     rt1015p->sdb = devm_gpiod_get_optional(&pdev->dev,
0115                 "sdb", GPIOD_OUT_LOW);
0116     if (IS_ERR(rt1015p->sdb))
0117         return PTR_ERR(rt1015p->sdb);
0118 
0119     dev_set_drvdata(&pdev->dev, rt1015p);
0120 
0121     return devm_snd_soc_register_component(&pdev->dev,
0122             &rt1015p_component_driver,
0123             &rt1015p_dai_driver, 1);
0124 }
0125 
0126 #ifdef CONFIG_OF
0127 static const struct of_device_id rt1015p_device_id[] = {
0128     { .compatible = "realtek,rt1015p" },
0129     { .compatible = "realtek,rt1019p" },
0130     {}
0131 };
0132 MODULE_DEVICE_TABLE(of, rt1015p_device_id);
0133 #endif
0134 
0135 #ifdef CONFIG_ACPI
0136 static const struct acpi_device_id rt1015p_acpi_match[] = {
0137     { "RTL1015", 0},
0138     { "RTL1019", 0},
0139     { },
0140 };
0141 MODULE_DEVICE_TABLE(acpi, rt1015p_acpi_match);
0142 #endif
0143 
0144 static struct platform_driver rt1015p_platform_driver = {
0145     .driver = {
0146         .name = "rt1015p",
0147         .of_match_table = of_match_ptr(rt1015p_device_id),
0148         .acpi_match_table = ACPI_PTR(rt1015p_acpi_match),
0149     },
0150     .probe = rt1015p_platform_probe,
0151 };
0152 module_platform_driver(rt1015p_platform_driver);
0153 
0154 MODULE_DESCRIPTION("ASoC RT1015P driver");
0155 MODULE_LICENSE("GPL v2");