Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 //
0003 // ROHM BD28623MUV class D speaker amplifier codec driver.
0004 //
0005 // Copyright (c) 2018 Socionext Inc.
0006 
0007 #include <linux/delay.h>
0008 #include <linux/gpio/consumer.h>
0009 #include <linux/module.h>
0010 #include <linux/of.h>
0011 #include <linux/regulator/consumer.h>
0012 #include <sound/pcm.h>
0013 #include <sound/soc.h>
0014 
0015 #define BD28623_NUM_SUPPLIES    3
0016 
0017 static const char *const bd28623_supply_names[BD28623_NUM_SUPPLIES] = {
0018     "VCCA",
0019     "VCCP1",
0020     "VCCP2",
0021 };
0022 
0023 struct bd28623_priv {
0024     struct device *dev;
0025     struct regulator_bulk_data supplies[BD28623_NUM_SUPPLIES];
0026     struct gpio_desc *reset_gpio;
0027     struct gpio_desc *mute_gpio;
0028 
0029     int switch_spk;
0030 };
0031 
0032 static const struct snd_soc_dapm_widget bd28623_widgets[] = {
0033     SND_SOC_DAPM_DAC("DAC", "Playback", SND_SOC_NOPM, 0, 0),
0034     SND_SOC_DAPM_OUTPUT("OUT1P"),
0035     SND_SOC_DAPM_OUTPUT("OUT1N"),
0036     SND_SOC_DAPM_OUTPUT("OUT2P"),
0037     SND_SOC_DAPM_OUTPUT("OUT2N"),
0038 };
0039 
0040 static const struct snd_soc_dapm_route bd28623_routes[] = {
0041     { "OUT1P", NULL, "DAC" },
0042     { "OUT1N", NULL, "DAC" },
0043     { "OUT2P", NULL, "DAC" },
0044     { "OUT2N", NULL, "DAC" },
0045 };
0046 
0047 static int bd28623_power_on(struct bd28623_priv *bd)
0048 {
0049     int ret;
0050 
0051     ret = regulator_bulk_enable(ARRAY_SIZE(bd->supplies), bd->supplies);
0052     if (ret) {
0053         dev_err(bd->dev, "Failed to enable supplies: %d\n", ret);
0054         return ret;
0055     }
0056 
0057     gpiod_set_value_cansleep(bd->reset_gpio, 0);
0058     usleep_range(300000, 400000);
0059 
0060     return 0;
0061 }
0062 
0063 static void bd28623_power_off(struct bd28623_priv *bd)
0064 {
0065     gpiod_set_value_cansleep(bd->reset_gpio, 1);
0066 
0067     regulator_bulk_disable(ARRAY_SIZE(bd->supplies), bd->supplies);
0068 }
0069 
0070 static int bd28623_get_switch_spk(struct snd_kcontrol *kcontrol,
0071                   struct snd_ctl_elem_value *ucontrol)
0072 {
0073     struct snd_soc_component *component =
0074         snd_soc_kcontrol_component(kcontrol);
0075     struct bd28623_priv *bd = snd_soc_component_get_drvdata(component);
0076 
0077     ucontrol->value.integer.value[0] = bd->switch_spk;
0078 
0079     return 0;
0080 }
0081 
0082 static int bd28623_set_switch_spk(struct snd_kcontrol *kcontrol,
0083                   struct snd_ctl_elem_value *ucontrol)
0084 {
0085     struct snd_soc_component *component =
0086         snd_soc_kcontrol_component(kcontrol);
0087     struct bd28623_priv *bd = snd_soc_component_get_drvdata(component);
0088 
0089     if (bd->switch_spk == ucontrol->value.integer.value[0])
0090         return 0;
0091 
0092     bd->switch_spk = ucontrol->value.integer.value[0];
0093 
0094     gpiod_set_value_cansleep(bd->mute_gpio, bd->switch_spk ? 0 : 1);
0095 
0096     return 0;
0097 }
0098 
0099 static const struct snd_kcontrol_new bd28623_controls[] = {
0100     SOC_SINGLE_BOOL_EXT("Speaker Switch", 0,
0101                 bd28623_get_switch_spk, bd28623_set_switch_spk),
0102 };
0103 
0104 static int bd28623_codec_probe(struct snd_soc_component *component)
0105 {
0106     struct bd28623_priv *bd = snd_soc_component_get_drvdata(component);
0107     int ret;
0108 
0109     bd->switch_spk = 1;
0110 
0111     ret = bd28623_power_on(bd);
0112     if (ret)
0113         return ret;
0114 
0115     gpiod_set_value_cansleep(bd->mute_gpio, bd->switch_spk ? 0 : 1);
0116 
0117     return 0;
0118 }
0119 
0120 static void bd28623_codec_remove(struct snd_soc_component *component)
0121 {
0122     struct bd28623_priv *bd = snd_soc_component_get_drvdata(component);
0123 
0124     bd28623_power_off(bd);
0125 }
0126 
0127 static int bd28623_codec_suspend(struct snd_soc_component *component)
0128 {
0129     struct bd28623_priv *bd = snd_soc_component_get_drvdata(component);
0130 
0131     bd28623_power_off(bd);
0132 
0133     return 0;
0134 }
0135 
0136 static int bd28623_codec_resume(struct snd_soc_component *component)
0137 {
0138     struct bd28623_priv *bd = snd_soc_component_get_drvdata(component);
0139     int ret;
0140 
0141     ret = bd28623_power_on(bd);
0142     if (ret)
0143         return ret;
0144 
0145     gpiod_set_value_cansleep(bd->mute_gpio, bd->switch_spk ? 0 : 1);
0146 
0147     return 0;
0148 }
0149 
0150 static const struct snd_soc_component_driver soc_codec_bd = {
0151     .probe          = bd28623_codec_probe,
0152     .remove         = bd28623_codec_remove,
0153     .suspend        = bd28623_codec_suspend,
0154     .resume         = bd28623_codec_resume,
0155     .dapm_widgets       = bd28623_widgets,
0156     .num_dapm_widgets   = ARRAY_SIZE(bd28623_widgets),
0157     .dapm_routes        = bd28623_routes,
0158     .num_dapm_routes    = ARRAY_SIZE(bd28623_routes),
0159     .controls       = bd28623_controls,
0160     .num_controls       = ARRAY_SIZE(bd28623_controls),
0161     .idle_bias_on       = 1,
0162     .use_pmdown_time    = 1,
0163     .endianness     = 1,
0164 };
0165 
0166 static struct snd_soc_dai_driver soc_dai_bd = {
0167     .name     = "bd28623-speaker",
0168     .playback = {
0169         .stream_name  = "Playback",
0170         .formats      = SNDRV_PCM_FMTBIT_S32_LE |
0171                 SNDRV_PCM_FMTBIT_S24_LE |
0172                 SNDRV_PCM_FMTBIT_S16_LE,
0173         .rates        = SNDRV_PCM_RATE_48000 |
0174                 SNDRV_PCM_RATE_44100 |
0175                 SNDRV_PCM_RATE_32000,
0176         .channels_min = 2,
0177         .channels_max = 2,
0178     },
0179 };
0180 
0181 static int bd28623_probe(struct platform_device *pdev)
0182 {
0183     struct bd28623_priv *bd;
0184     struct device *dev = &pdev->dev;
0185     int i, ret;
0186 
0187     bd = devm_kzalloc(&pdev->dev, sizeof(struct bd28623_priv), GFP_KERNEL);
0188     if (!bd)
0189         return -ENOMEM;
0190 
0191     for (i = 0; i < ARRAY_SIZE(bd->supplies); i++)
0192         bd->supplies[i].supply = bd28623_supply_names[i];
0193 
0194     ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(bd->supplies),
0195                       bd->supplies);
0196     if (ret) {
0197         dev_err(dev, "Failed to get supplies: %d\n", ret);
0198         return ret;
0199     }
0200 
0201     bd->reset_gpio = devm_gpiod_get_optional(dev, "reset",
0202                          GPIOD_OUT_HIGH);
0203     if (IS_ERR(bd->reset_gpio)) {
0204         dev_err(dev, "Failed to request reset_gpio: %ld\n",
0205             PTR_ERR(bd->reset_gpio));
0206         return PTR_ERR(bd->reset_gpio);
0207     }
0208 
0209     bd->mute_gpio = devm_gpiod_get_optional(dev, "mute",
0210                         GPIOD_OUT_HIGH);
0211     if (IS_ERR(bd->mute_gpio)) {
0212         dev_err(dev, "Failed to request mute_gpio: %ld\n",
0213             PTR_ERR(bd->mute_gpio));
0214         return PTR_ERR(bd->mute_gpio);
0215     }
0216 
0217     platform_set_drvdata(pdev, bd);
0218     bd->dev = dev;
0219 
0220     return devm_snd_soc_register_component(dev, &soc_codec_bd,
0221                            &soc_dai_bd, 1);
0222 }
0223 
0224 static const struct of_device_id bd28623_of_match[] __maybe_unused = {
0225     { .compatible = "rohm,bd28623", },
0226     {}
0227 };
0228 MODULE_DEVICE_TABLE(of, bd28623_of_match);
0229 
0230 static struct platform_driver bd28623_codec_driver = {
0231     .driver = {
0232         .name = "bd28623",
0233         .of_match_table = of_match_ptr(bd28623_of_match),
0234     },
0235     .probe  = bd28623_probe,
0236 };
0237 module_platform_driver(bd28623_codec_driver);
0238 
0239 MODULE_AUTHOR("Katsuhiro Suzuki <suzuki.katsuhiro@socionext.com>");
0240 MODULE_DESCRIPTION("ROHM BD28623 speaker amplifier driver");
0241 MODULE_LICENSE("GPL v2");