xref: /openbmc/linux/sound/soc/codecs/simple-mux.c (revision 21ab7031)
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 struct simple_mux {
13 	struct gpio_desc *gpiod_mux;
14 	unsigned int mux;
15 };
16 
17 static const char * const simple_mux_texts[] = {
18 	"Input 1", "Input 2"
19 };
20 
21 static SOC_ENUM_SINGLE_EXT_DECL(simple_mux_enum, simple_mux_texts);
22 
23 static int simple_mux_control_get(struct snd_kcontrol *kcontrol,
24 				  struct snd_ctl_elem_value *ucontrol)
25 {
26 	struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
27 	struct snd_soc_component *c = snd_soc_dapm_to_component(dapm);
28 	struct simple_mux *priv = snd_soc_component_get_drvdata(c);
29 
30 	ucontrol->value.enumerated.item[0] = priv->mux;
31 
32 	return 0;
33 }
34 
35 static int simple_mux_control_put(struct snd_kcontrol *kcontrol,
36 				  struct snd_ctl_elem_value *ucontrol)
37 {
38 	struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
39 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
40 	struct snd_soc_component *c = snd_soc_dapm_to_component(dapm);
41 	struct simple_mux *priv = snd_soc_component_get_drvdata(c);
42 
43 	if (ucontrol->value.enumerated.item[0] > e->items)
44 		return -EINVAL;
45 
46 	if (priv->mux == ucontrol->value.enumerated.item[0])
47 		return 0;
48 
49 	priv->mux = ucontrol->value.enumerated.item[0];
50 
51 	gpiod_set_value_cansleep(priv->gpiod_mux, priv->mux);
52 
53 	return snd_soc_dapm_mux_update_power(dapm, kcontrol,
54 					     ucontrol->value.enumerated.item[0],
55 					     e, NULL);
56 }
57 
58 static unsigned int simple_mux_read(struct snd_soc_component *component,
59 				    unsigned int reg)
60 {
61 	struct simple_mux *priv = snd_soc_component_get_drvdata(component);
62 
63 	return priv->mux;
64 }
65 
66 static const struct snd_kcontrol_new simple_mux_mux =
67 	SOC_DAPM_ENUM_EXT("Muxer", simple_mux_enum, simple_mux_control_get, simple_mux_control_put);
68 
69 static const struct snd_soc_dapm_widget simple_mux_dapm_widgets[] = {
70 	SND_SOC_DAPM_INPUT("IN1"),
71 	SND_SOC_DAPM_INPUT("IN2"),
72 	SND_SOC_DAPM_MUX("MUX", SND_SOC_NOPM, 0, 0, &simple_mux_mux),
73 	SND_SOC_DAPM_OUTPUT("OUT"),
74 };
75 
76 static const struct snd_soc_dapm_route simple_mux_dapm_routes[] = {
77 	{ "OUT", NULL, "MUX" },
78 	{ "MUX", "Input 1", "IN1" },
79 	{ "MUX", "Input 2", "IN2" },
80 };
81 
82 static const struct snd_soc_component_driver simple_mux_component_driver = {
83 	.dapm_widgets		= simple_mux_dapm_widgets,
84 	.num_dapm_widgets	= ARRAY_SIZE(simple_mux_dapm_widgets),
85 	.dapm_routes		= simple_mux_dapm_routes,
86 	.num_dapm_routes	= ARRAY_SIZE(simple_mux_dapm_routes),
87 	.read			= simple_mux_read,
88 };
89 
90 static int simple_mux_probe(struct platform_device *pdev)
91 {
92 	struct device *dev = &pdev->dev;
93 	struct simple_mux *priv;
94 
95 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
96 	if (!priv)
97 		return -ENOMEM;
98 
99 	dev_set_drvdata(dev, priv);
100 
101 	priv->gpiod_mux = devm_gpiod_get(dev, "mux", GPIOD_OUT_LOW);
102 	if (IS_ERR(priv->gpiod_mux))
103 		return dev_err_probe(dev, PTR_ERR(priv->gpiod_mux),
104 				     "Failed to get 'mux' gpio");
105 
106 	return devm_snd_soc_register_component(dev, &simple_mux_component_driver, NULL, 0);
107 }
108 
109 #ifdef CONFIG_OF
110 static const struct of_device_id simple_mux_ids[] = {
111 	{ .compatible = "simple-audio-mux", },
112 	{ }
113 };
114 MODULE_DEVICE_TABLE(of, simple_mux_ids);
115 #endif
116 
117 static struct platform_driver simple_mux_driver = {
118 	.driver = {
119 		.name = "simple-mux",
120 		.of_match_table = of_match_ptr(simple_mux_ids),
121 	},
122 	.probe = simple_mux_probe,
123 };
124 
125 module_platform_driver(simple_mux_driver);
126 
127 MODULE_DESCRIPTION("ASoC Simple Audio Mux driver");
128 MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@bootlin.com>");
129 MODULE_LICENSE("GPL");
130