xref: /openbmc/linux/sound/soc/codecs/simple-mux.c (revision 7ae9fb1b7ecbb5d85d07857943f677fd1a559b18)
1342fbb75SAlexandre Belloni // SPDX-License-Identifier: GPL-2.0-only
2342fbb75SAlexandre Belloni /*
3342fbb75SAlexandre Belloni  * Copyright (c) 2020 Bootlin SA
4342fbb75SAlexandre Belloni  * Author: Alexandre Belloni <alexandre.belloni@bootlin.com>
5342fbb75SAlexandre Belloni  */
6342fbb75SAlexandre Belloni 
7342fbb75SAlexandre Belloni #include <linux/gpio/consumer.h>
8342fbb75SAlexandre Belloni #include <linux/module.h>
9342fbb75SAlexandre Belloni #include <linux/regulator/consumer.h>
10342fbb75SAlexandre Belloni #include <sound/soc.h>
11342fbb75SAlexandre Belloni 
12342fbb75SAlexandre Belloni struct simple_mux {
13342fbb75SAlexandre Belloni 	struct gpio_desc *gpiod_mux;
14342fbb75SAlexandre Belloni 	unsigned int mux;
15342fbb75SAlexandre Belloni };
16342fbb75SAlexandre Belloni 
17342fbb75SAlexandre Belloni static const char * const simple_mux_texts[] = {
18342fbb75SAlexandre Belloni 	"Input 1", "Input 2"
19342fbb75SAlexandre Belloni };
20342fbb75SAlexandre Belloni 
21342fbb75SAlexandre Belloni static SOC_ENUM_SINGLE_EXT_DECL(simple_mux_enum, simple_mux_texts);
22342fbb75SAlexandre Belloni 
simple_mux_control_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)23342fbb75SAlexandre Belloni static int simple_mux_control_get(struct snd_kcontrol *kcontrol,
24342fbb75SAlexandre Belloni 				  struct snd_ctl_elem_value *ucontrol)
25342fbb75SAlexandre Belloni {
26342fbb75SAlexandre Belloni 	struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
27342fbb75SAlexandre Belloni 	struct snd_soc_component *c = snd_soc_dapm_to_component(dapm);
28342fbb75SAlexandre Belloni 	struct simple_mux *priv = snd_soc_component_get_drvdata(c);
29342fbb75SAlexandre Belloni 
30342fbb75SAlexandre Belloni 	ucontrol->value.enumerated.item[0] = priv->mux;
31342fbb75SAlexandre Belloni 
32342fbb75SAlexandre Belloni 	return 0;
33342fbb75SAlexandre Belloni }
34342fbb75SAlexandre Belloni 
simple_mux_control_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)35342fbb75SAlexandre Belloni static int simple_mux_control_put(struct snd_kcontrol *kcontrol,
36342fbb75SAlexandre Belloni 				  struct snd_ctl_elem_value *ucontrol)
37342fbb75SAlexandre Belloni {
38342fbb75SAlexandre Belloni 	struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
39342fbb75SAlexandre Belloni 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
40342fbb75SAlexandre Belloni 	struct snd_soc_component *c = snd_soc_dapm_to_component(dapm);
41342fbb75SAlexandre Belloni 	struct simple_mux *priv = snd_soc_component_get_drvdata(c);
42342fbb75SAlexandre Belloni 
43342fbb75SAlexandre Belloni 	if (ucontrol->value.enumerated.item[0] > e->items)
44342fbb75SAlexandre Belloni 		return -EINVAL;
45342fbb75SAlexandre Belloni 
46342fbb75SAlexandre Belloni 	if (priv->mux == ucontrol->value.enumerated.item[0])
47342fbb75SAlexandre Belloni 		return 0;
48342fbb75SAlexandre Belloni 
49342fbb75SAlexandre Belloni 	priv->mux = ucontrol->value.enumerated.item[0];
50342fbb75SAlexandre Belloni 
51342fbb75SAlexandre Belloni 	gpiod_set_value_cansleep(priv->gpiod_mux, priv->mux);
52342fbb75SAlexandre Belloni 
53342fbb75SAlexandre Belloni 	return snd_soc_dapm_mux_update_power(dapm, kcontrol,
54342fbb75SAlexandre Belloni 					     ucontrol->value.enumerated.item[0],
55342fbb75SAlexandre Belloni 					     e, NULL);
56342fbb75SAlexandre Belloni }
57342fbb75SAlexandre Belloni 
simple_mux_read(struct snd_soc_component * component,unsigned int reg)58*f7d97cb5SMaarten Zanders static unsigned int simple_mux_read(struct snd_soc_component *component,
59*f7d97cb5SMaarten Zanders 				    unsigned int reg)
60*f7d97cb5SMaarten Zanders {
61*f7d97cb5SMaarten Zanders 	struct simple_mux *priv = snd_soc_component_get_drvdata(component);
62*f7d97cb5SMaarten Zanders 
63*f7d97cb5SMaarten Zanders 	return priv->mux;
64*f7d97cb5SMaarten Zanders }
65*f7d97cb5SMaarten Zanders 
66342fbb75SAlexandre Belloni static const struct snd_kcontrol_new simple_mux_mux =
67342fbb75SAlexandre Belloni 	SOC_DAPM_ENUM_EXT("Muxer", simple_mux_enum, simple_mux_control_get, simple_mux_control_put);
68342fbb75SAlexandre Belloni 
69342fbb75SAlexandre Belloni static const struct snd_soc_dapm_widget simple_mux_dapm_widgets[] = {
70342fbb75SAlexandre Belloni 	SND_SOC_DAPM_INPUT("IN1"),
71342fbb75SAlexandre Belloni 	SND_SOC_DAPM_INPUT("IN2"),
72342fbb75SAlexandre Belloni 	SND_SOC_DAPM_MUX("MUX", SND_SOC_NOPM, 0, 0, &simple_mux_mux),
73342fbb75SAlexandre Belloni 	SND_SOC_DAPM_OUTPUT("OUT"),
74342fbb75SAlexandre Belloni };
75342fbb75SAlexandre Belloni 
76342fbb75SAlexandre Belloni static const struct snd_soc_dapm_route simple_mux_dapm_routes[] = {
77342fbb75SAlexandre Belloni 	{ "OUT", NULL, "MUX" },
78342fbb75SAlexandre Belloni 	{ "MUX", "Input 1", "IN1" },
79342fbb75SAlexandre Belloni 	{ "MUX", "Input 2", "IN2" },
80342fbb75SAlexandre Belloni };
81342fbb75SAlexandre Belloni 
82342fbb75SAlexandre Belloni static const struct snd_soc_component_driver simple_mux_component_driver = {
83342fbb75SAlexandre Belloni 	.dapm_widgets		= simple_mux_dapm_widgets,
84342fbb75SAlexandre Belloni 	.num_dapm_widgets	= ARRAY_SIZE(simple_mux_dapm_widgets),
85342fbb75SAlexandre Belloni 	.dapm_routes		= simple_mux_dapm_routes,
86342fbb75SAlexandre Belloni 	.num_dapm_routes	= ARRAY_SIZE(simple_mux_dapm_routes),
87*f7d97cb5SMaarten Zanders 	.read			= simple_mux_read,
88342fbb75SAlexandre Belloni };
89342fbb75SAlexandre Belloni 
simple_mux_probe(struct platform_device * pdev)90342fbb75SAlexandre Belloni static int simple_mux_probe(struct platform_device *pdev)
91342fbb75SAlexandre Belloni {
92342fbb75SAlexandre Belloni 	struct device *dev = &pdev->dev;
93342fbb75SAlexandre Belloni 	struct simple_mux *priv;
94342fbb75SAlexandre Belloni 
95342fbb75SAlexandre Belloni 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
96342fbb75SAlexandre Belloni 	if (!priv)
97342fbb75SAlexandre Belloni 		return -ENOMEM;
98342fbb75SAlexandre Belloni 
99342fbb75SAlexandre Belloni 	dev_set_drvdata(dev, priv);
100342fbb75SAlexandre Belloni 
101342fbb75SAlexandre Belloni 	priv->gpiod_mux = devm_gpiod_get(dev, "mux", GPIOD_OUT_LOW);
10217d70447SKuninori Morimoto 	if (IS_ERR(priv->gpiod_mux))
10317d70447SKuninori Morimoto 		return dev_err_probe(dev, PTR_ERR(priv->gpiod_mux),
10417d70447SKuninori Morimoto 				     "Failed to get 'mux' gpio");
105342fbb75SAlexandre Belloni 
106342fbb75SAlexandre Belloni 	return devm_snd_soc_register_component(dev, &simple_mux_component_driver, NULL, 0);
107342fbb75SAlexandre Belloni }
108342fbb75SAlexandre Belloni 
109342fbb75SAlexandre Belloni #ifdef CONFIG_OF
110342fbb75SAlexandre Belloni static const struct of_device_id simple_mux_ids[] = {
111342fbb75SAlexandre Belloni 	{ .compatible = "simple-audio-mux", },
112342fbb75SAlexandre Belloni 	{ }
113342fbb75SAlexandre Belloni };
114342fbb75SAlexandre Belloni MODULE_DEVICE_TABLE(of, simple_mux_ids);
115342fbb75SAlexandre Belloni #endif
116342fbb75SAlexandre Belloni 
117342fbb75SAlexandre Belloni static struct platform_driver simple_mux_driver = {
118342fbb75SAlexandre Belloni 	.driver = {
119342fbb75SAlexandre Belloni 		.name = "simple-mux",
120342fbb75SAlexandre Belloni 		.of_match_table = of_match_ptr(simple_mux_ids),
121342fbb75SAlexandre Belloni 	},
122342fbb75SAlexandre Belloni 	.probe = simple_mux_probe,
123342fbb75SAlexandre Belloni };
124342fbb75SAlexandre Belloni 
125342fbb75SAlexandre Belloni module_platform_driver(simple_mux_driver);
126342fbb75SAlexandre Belloni 
127342fbb75SAlexandre Belloni MODULE_DESCRIPTION("ASoC Simple Audio Mux driver");
128342fbb75SAlexandre Belloni MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@bootlin.com>");
129342fbb75SAlexandre Belloni MODULE_LICENSE("GPL");
130