xref: /linux/sound/soc/codecs/simple-mux.c (revision 509d3f45847627f4c5cdce004c3ec79262b5239c)
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/mux/driver.h>
10 #include <linux/regulator/consumer.h>
11 #include <sound/soc.h>
12 
13 #define MUX_TEXT_SIZE	2
14 #define MUX_WIDGET_SIZE	4
15 #define MUX_ROUTE_SIZE	3
16 struct simple_mux {
17 	struct gpio_desc *gpiod_mux;
18 	unsigned int mux;
19 	const char *mux_texts[MUX_TEXT_SIZE];
20 	unsigned int idle_state;
21 	struct soc_enum mux_enum;
22 	struct snd_kcontrol_new mux_mux;
23 	struct snd_soc_dapm_widget mux_widgets[MUX_WIDGET_SIZE];
24 	struct snd_soc_dapm_route mux_routes[MUX_ROUTE_SIZE];
25 	struct snd_soc_component_driver mux_driver;
26 };
27 
28 static const char * const simple_mux_texts[MUX_TEXT_SIZE] = {
29 	"Input 1", "Input 2"
30 };
31 
32 static SOC_ENUM_SINGLE_EXT_DECL(simple_mux_enum, simple_mux_texts);
33 
34 static int simple_mux_control_get(struct snd_kcontrol *kcontrol,
35 				  struct snd_ctl_elem_value *ucontrol)
36 {
37 	struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_to_dapm(kcontrol);
38 	struct snd_soc_component *c = snd_soc_dapm_to_component(dapm);
39 	struct simple_mux *priv = snd_soc_component_get_drvdata(c);
40 
41 	ucontrol->value.enumerated.item[0] = priv->mux;
42 
43 	return 0;
44 }
45 
46 static int simple_mux_control_put(struct snd_kcontrol *kcontrol,
47 				  struct snd_ctl_elem_value *ucontrol)
48 {
49 	struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_to_dapm(kcontrol);
50 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
51 	struct snd_soc_component *c = snd_soc_dapm_to_component(dapm);
52 	struct simple_mux *priv = snd_soc_component_get_drvdata(c);
53 
54 	if (ucontrol->value.enumerated.item[0] > e->items)
55 		return -EINVAL;
56 
57 	if (priv->mux == ucontrol->value.enumerated.item[0])
58 		return 0;
59 
60 	priv->mux = ucontrol->value.enumerated.item[0];
61 
62 	if (priv->idle_state != MUX_IDLE_AS_IS &&
63 	    snd_soc_dapm_get_bias_level(dapm) < SND_SOC_BIAS_PREPARE)
64 		return 0;
65 
66 	gpiod_set_value_cansleep(priv->gpiod_mux, priv->mux);
67 
68 	return snd_soc_dapm_mux_update_power(dapm, kcontrol,
69 					     ucontrol->value.enumerated.item[0],
70 					     e, NULL);
71 }
72 
73 static unsigned int simple_mux_read(struct snd_soc_component *component,
74 				    unsigned int reg)
75 {
76 	struct simple_mux *priv = snd_soc_component_get_drvdata(component);
77 
78 	return priv->mux;
79 }
80 
81 static const struct snd_kcontrol_new simple_mux_mux =
82 	SOC_DAPM_ENUM_EXT("Muxer", simple_mux_enum, simple_mux_control_get, simple_mux_control_put);
83 
84 static int simple_mux_event(struct snd_soc_dapm_widget *w,
85 			    struct snd_kcontrol *kcontrol, int event)
86 {
87 	struct snd_soc_component *c = snd_soc_dapm_to_component(w->dapm);
88 	struct simple_mux *priv = snd_soc_component_get_drvdata(c);
89 
90 	if (priv->idle_state != MUX_IDLE_AS_IS) {
91 		switch (event) {
92 		case SND_SOC_DAPM_PRE_PMU:
93 			gpiod_set_value_cansleep(priv->gpiod_mux, priv->mux);
94 			break;
95 		case SND_SOC_DAPM_POST_PMD:
96 			gpiod_set_value_cansleep(priv->gpiod_mux, priv->idle_state);
97 			break;
98 		default:
99 			break;
100 		}
101 	}
102 
103 	return 0;
104 }
105 
106 static const struct snd_soc_dapm_widget simple_mux_dapm_widgets[MUX_WIDGET_SIZE] = {
107 	SND_SOC_DAPM_INPUT("IN1"),
108 	SND_SOC_DAPM_INPUT("IN2"),
109 	SND_SOC_DAPM_MUX_E("MUX", SND_SOC_NOPM, 0, 0, &simple_mux_mux, // see simple_mux_probe()
110 			   simple_mux_event, SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
111 	SND_SOC_DAPM_OUTPUT("OUT"),
112 };
113 
114 static const struct snd_soc_dapm_route simple_mux_dapm_routes[MUX_ROUTE_SIZE] = {
115 	{ "OUT", NULL, "MUX" },
116 	{ "MUX", "Input 1", "IN1" }, // see simple_mux_probe()
117 	{ "MUX", "Input 2", "IN2" }, // see simple_mux_probe()
118 };
119 
120 static int simple_mux_probe(struct platform_device *pdev)
121 {
122 	struct device *dev = &pdev->dev;
123 	struct device_node *np = dev->of_node;
124 	struct simple_mux *priv;
125 	int ret;
126 
127 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
128 	if (!priv)
129 		return -ENOMEM;
130 
131 	dev_set_drvdata(dev, priv);
132 
133 	priv->gpiod_mux = devm_gpiod_get(dev, "mux", GPIOD_OUT_LOW);
134 	if (IS_ERR(priv->gpiod_mux))
135 		return dev_err_probe(dev, PTR_ERR(priv->gpiod_mux),
136 				     "Failed to get 'mux' gpio");
137 
138 	/* Copy default settings */
139 	memcpy(&priv->mux_texts,	&simple_mux_texts,		sizeof(priv->mux_texts));
140 	memcpy(&priv->mux_enum,		&simple_mux_enum,		sizeof(priv->mux_enum));
141 	memcpy(&priv->mux_mux,		&simple_mux_mux,		sizeof(priv->mux_mux));
142 	memcpy(&priv->mux_widgets,	&simple_mux_dapm_widgets,	sizeof(priv->mux_widgets));
143 	memcpy(&priv->mux_routes,	&simple_mux_dapm_routes,	sizeof(priv->mux_routes));
144 
145 	priv->mux_driver.dapm_widgets		= priv->mux_widgets;
146 	priv->mux_driver.num_dapm_widgets	= MUX_WIDGET_SIZE;
147 	priv->mux_driver.dapm_routes		= priv->mux_routes;
148 	priv->mux_driver.num_dapm_routes	= MUX_ROUTE_SIZE;
149 	priv->mux_driver.read			= simple_mux_read;
150 
151 	/* Overwrite text ("Input 1", "Input 2") if property exists */
152 	of_property_read_string_array(np, "state-labels", priv->mux_texts, MUX_TEXT_SIZE);
153 
154 	ret = of_property_read_u32(np, "idle-state", &priv->idle_state);
155 	if (ret < 0) {
156 		priv->idle_state = MUX_IDLE_AS_IS;
157 	} else if (priv->idle_state != MUX_IDLE_AS_IS && priv->idle_state >= 2) {
158 		dev_err(dev, "invalid idle-state %u\n", priv->idle_state);
159 		return -EINVAL;
160 	}
161 
162 	/* switch to use priv data instead of default */
163 	priv->mux_enum.texts			= priv->mux_texts;
164 	priv->mux_mux.private_value		= (unsigned long)&priv->mux_enum;
165 	priv->mux_widgets[2].kcontrol_news	= &priv->mux_mux;
166 	priv->mux_routes[1].control		= priv->mux_texts[0]; // "Input 1"
167 	priv->mux_routes[2].control		= priv->mux_texts[1]; // "Input 2"
168 
169 	return devm_snd_soc_register_component(dev, &priv->mux_driver, NULL, 0);
170 }
171 
172 #ifdef CONFIG_OF
173 static const struct of_device_id simple_mux_ids[] = {
174 	{ .compatible = "simple-audio-mux", },
175 	{ }
176 };
177 MODULE_DEVICE_TABLE(of, simple_mux_ids);
178 #endif
179 
180 static struct platform_driver simple_mux_driver = {
181 	.driver = {
182 		.name = "simple-mux",
183 		.of_match_table = of_match_ptr(simple_mux_ids),
184 	},
185 	.probe = simple_mux_probe,
186 };
187 
188 module_platform_driver(simple_mux_driver);
189 
190 MODULE_DESCRIPTION("ASoC Simple Audio Mux driver");
191 MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@bootlin.com>");
192 MODULE_LICENSE("GPL");
193