Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (c) 2020 Bootlin SA
0004  * Author: Alexandre Belloni <alexandre.belloni@bootlin.com>
0005  */
0006 
0007 #include <linux/gpio/consumer.h>
0008 #include <linux/module.h>
0009 #include <linux/regulator/consumer.h>
0010 #include <sound/soc.h>
0011 
0012 struct simple_mux {
0013     struct gpio_desc *gpiod_mux;
0014     unsigned int mux;
0015 };
0016 
0017 static const char * const simple_mux_texts[] = {
0018     "Input 1", "Input 2"
0019 };
0020 
0021 static SOC_ENUM_SINGLE_EXT_DECL(simple_mux_enum, simple_mux_texts);
0022 
0023 static int simple_mux_control_get(struct snd_kcontrol *kcontrol,
0024                   struct snd_ctl_elem_value *ucontrol)
0025 {
0026     struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
0027     struct snd_soc_component *c = snd_soc_dapm_to_component(dapm);
0028     struct simple_mux *priv = snd_soc_component_get_drvdata(c);
0029 
0030     ucontrol->value.enumerated.item[0] = priv->mux;
0031 
0032     return 0;
0033 }
0034 
0035 static int simple_mux_control_put(struct snd_kcontrol *kcontrol,
0036                   struct snd_ctl_elem_value *ucontrol)
0037 {
0038     struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
0039     struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
0040     struct snd_soc_component *c = snd_soc_dapm_to_component(dapm);
0041     struct simple_mux *priv = snd_soc_component_get_drvdata(c);
0042 
0043     if (ucontrol->value.enumerated.item[0] > e->items)
0044         return -EINVAL;
0045 
0046     if (priv->mux == ucontrol->value.enumerated.item[0])
0047         return 0;
0048 
0049     priv->mux = ucontrol->value.enumerated.item[0];
0050 
0051     gpiod_set_value_cansleep(priv->gpiod_mux, priv->mux);
0052 
0053     return snd_soc_dapm_mux_update_power(dapm, kcontrol,
0054                          ucontrol->value.enumerated.item[0],
0055                          e, NULL);
0056 }
0057 
0058 static const struct snd_kcontrol_new simple_mux_mux =
0059     SOC_DAPM_ENUM_EXT("Muxer", simple_mux_enum, simple_mux_control_get, simple_mux_control_put);
0060 
0061 static const struct snd_soc_dapm_widget simple_mux_dapm_widgets[] = {
0062     SND_SOC_DAPM_INPUT("IN1"),
0063     SND_SOC_DAPM_INPUT("IN2"),
0064     SND_SOC_DAPM_MUX("MUX", SND_SOC_NOPM, 0, 0, &simple_mux_mux),
0065     SND_SOC_DAPM_OUTPUT("OUT"),
0066 };
0067 
0068 static const struct snd_soc_dapm_route simple_mux_dapm_routes[] = {
0069     { "OUT", NULL, "MUX" },
0070     { "MUX", "Input 1", "IN1" },
0071     { "MUX", "Input 2", "IN2" },
0072 };
0073 
0074 static const struct snd_soc_component_driver simple_mux_component_driver = {
0075     .dapm_widgets       = simple_mux_dapm_widgets,
0076     .num_dapm_widgets   = ARRAY_SIZE(simple_mux_dapm_widgets),
0077     .dapm_routes        = simple_mux_dapm_routes,
0078     .num_dapm_routes    = ARRAY_SIZE(simple_mux_dapm_routes),
0079 };
0080 
0081 static int simple_mux_probe(struct platform_device *pdev)
0082 {
0083     struct device *dev = &pdev->dev;
0084     struct simple_mux *priv;
0085 
0086     priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
0087     if (!priv)
0088         return -ENOMEM;
0089 
0090     dev_set_drvdata(dev, priv);
0091 
0092     priv->gpiod_mux = devm_gpiod_get(dev, "mux", GPIOD_OUT_LOW);
0093     if (IS_ERR(priv->gpiod_mux))
0094         return dev_err_probe(dev, PTR_ERR(priv->gpiod_mux),
0095                      "Failed to get 'mux' gpio");
0096 
0097     return devm_snd_soc_register_component(dev, &simple_mux_component_driver, NULL, 0);
0098 }
0099 
0100 #ifdef CONFIG_OF
0101 static const struct of_device_id simple_mux_ids[] = {
0102     { .compatible = "simple-audio-mux", },
0103     { }
0104 };
0105 MODULE_DEVICE_TABLE(of, simple_mux_ids);
0106 #endif
0107 
0108 static struct platform_driver simple_mux_driver = {
0109     .driver = {
0110         .name = "simple-mux",
0111         .of_match_table = of_match_ptr(simple_mux_ids),
0112     },
0113     .probe = simple_mux_probe,
0114 };
0115 
0116 module_platform_driver(simple_mux_driver);
0117 
0118 MODULE_DESCRIPTION("ASoC Simple Audio Mux driver");
0119 MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@bootlin.com>");
0120 MODULE_LICENSE("GPL");