1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2020 Bootlin SA 4 * Author: Alexandre Belloni <alexandre.belloni@bootlin.com> 5 */ 6 7 #include <linux/gpio/consumer.h> 8 #include <linux/module.h> 9 #include <linux/regulator/consumer.h> 10 #include <sound/soc.h> 11 12 #define MUX_TEXT_SIZE 2 13 #define MUX_WIDGET_SIZE 4 14 #define MUX_ROUTE_SIZE 3 15 struct simple_mux { 16 struct gpio_desc *gpiod_mux; 17 unsigned int mux; 18 const char *mux_texts[MUX_TEXT_SIZE]; 19 struct soc_enum mux_enum; 20 struct snd_kcontrol_new mux_mux; 21 struct snd_soc_dapm_widget mux_widgets[MUX_WIDGET_SIZE]; 22 struct snd_soc_dapm_route mux_routes[MUX_ROUTE_SIZE]; 23 struct snd_soc_component_driver mux_driver; 24 }; 25 26 static const char * const simple_mux_texts[MUX_TEXT_SIZE] = { 27 "Input 1", "Input 2" 28 }; 29 30 static SOC_ENUM_SINGLE_EXT_DECL(simple_mux_enum, simple_mux_texts); 31 32 static int simple_mux_control_get(struct snd_kcontrol *kcontrol, 33 struct snd_ctl_elem_value *ucontrol) 34 { 35 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol); 36 struct snd_soc_component *c = snd_soc_dapm_to_component(dapm); 37 struct simple_mux *priv = snd_soc_component_get_drvdata(c); 38 39 ucontrol->value.enumerated.item[0] = priv->mux; 40 41 return 0; 42 } 43 44 static int simple_mux_control_put(struct snd_kcontrol *kcontrol, 45 struct snd_ctl_elem_value *ucontrol) 46 { 47 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol); 48 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 49 struct snd_soc_component *c = snd_soc_dapm_to_component(dapm); 50 struct simple_mux *priv = snd_soc_component_get_drvdata(c); 51 52 if (ucontrol->value.enumerated.item[0] > e->items) 53 return -EINVAL; 54 55 if (priv->mux == ucontrol->value.enumerated.item[0]) 56 return 0; 57 58 priv->mux = ucontrol->value.enumerated.item[0]; 59 60 gpiod_set_value_cansleep(priv->gpiod_mux, priv->mux); 61 62 return snd_soc_dapm_mux_update_power(dapm, kcontrol, 63 ucontrol->value.enumerated.item[0], 64 e, NULL); 65 } 66 67 static unsigned int simple_mux_read(struct snd_soc_component *component, 68 unsigned int reg) 69 { 70 struct simple_mux *priv = snd_soc_component_get_drvdata(component); 71 72 return priv->mux; 73 } 74 75 static const struct snd_kcontrol_new simple_mux_mux = 76 SOC_DAPM_ENUM_EXT("Muxer", simple_mux_enum, simple_mux_control_get, simple_mux_control_put); 77 78 static const struct snd_soc_dapm_widget simple_mux_dapm_widgets[MUX_WIDGET_SIZE] = { 79 SND_SOC_DAPM_INPUT("IN1"), 80 SND_SOC_DAPM_INPUT("IN2"), 81 SND_SOC_DAPM_MUX("MUX", SND_SOC_NOPM, 0, 0, &simple_mux_mux), // see simple_mux_probe() 82 SND_SOC_DAPM_OUTPUT("OUT"), 83 }; 84 85 static const struct snd_soc_dapm_route simple_mux_dapm_routes[MUX_ROUTE_SIZE] = { 86 { "OUT", NULL, "MUX" }, 87 { "MUX", "Input 1", "IN1" }, // see simple_mux_probe() 88 { "MUX", "Input 2", "IN2" }, // see simple_mux_probe() 89 }; 90 91 static int simple_mux_probe(struct platform_device *pdev) 92 { 93 struct device *dev = &pdev->dev; 94 struct device_node *np = dev->of_node; 95 struct simple_mux *priv; 96 97 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 98 if (!priv) 99 return -ENOMEM; 100 101 dev_set_drvdata(dev, priv); 102 103 priv->gpiod_mux = devm_gpiod_get(dev, "mux", GPIOD_OUT_LOW); 104 if (IS_ERR(priv->gpiod_mux)) 105 return dev_err_probe(dev, PTR_ERR(priv->gpiod_mux), 106 "Failed to get 'mux' gpio"); 107 108 /* Copy default settings */ 109 memcpy(&priv->mux_texts, &simple_mux_texts, sizeof(priv->mux_texts)); 110 memcpy(&priv->mux_enum, &simple_mux_enum, sizeof(priv->mux_enum)); 111 memcpy(&priv->mux_mux, &simple_mux_mux, sizeof(priv->mux_mux)); 112 memcpy(&priv->mux_widgets, &simple_mux_dapm_widgets, sizeof(priv->mux_widgets)); 113 memcpy(&priv->mux_routes, &simple_mux_dapm_routes, sizeof(priv->mux_routes)); 114 115 priv->mux_driver.dapm_widgets = priv->mux_widgets; 116 priv->mux_driver.num_dapm_widgets = MUX_WIDGET_SIZE; 117 priv->mux_driver.dapm_routes = priv->mux_routes; 118 priv->mux_driver.num_dapm_routes = MUX_ROUTE_SIZE; 119 priv->mux_driver.read = simple_mux_read; 120 121 /* Overwrite text ("Input 1", "Input 2") if property exists */ 122 of_property_read_string_array(np, "state-labels", priv->mux_texts, MUX_TEXT_SIZE); 123 124 /* switch to use priv data instead of default */ 125 priv->mux_enum.texts = priv->mux_texts; 126 priv->mux_mux.private_value = (unsigned long)&priv->mux_enum; 127 priv->mux_widgets[2].kcontrol_news = &priv->mux_mux; 128 priv->mux_routes[1].control = priv->mux_texts[0]; // "Input 1" 129 priv->mux_routes[2].control = priv->mux_texts[1]; // "Input 2" 130 131 return devm_snd_soc_register_component(dev, &priv->mux_driver, NULL, 0); 132 } 133 134 #ifdef CONFIG_OF 135 static const struct of_device_id simple_mux_ids[] = { 136 { .compatible = "simple-audio-mux", }, 137 { } 138 }; 139 MODULE_DEVICE_TABLE(of, simple_mux_ids); 140 #endif 141 142 static struct platform_driver simple_mux_driver = { 143 .driver = { 144 .name = "simple-mux", 145 .of_match_table = of_match_ptr(simple_mux_ids), 146 }, 147 .probe = simple_mux_probe, 148 }; 149 150 module_platform_driver(simple_mux_driver); 151 152 MODULE_DESCRIPTION("ASoC Simple Audio Mux driver"); 153 MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@bootlin.com>"); 154 MODULE_LICENSE("GPL"); 155
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.