1 /*
2  * Copyright (c) 2017 BayLibre, SAS.
3  * Author: Jerome Brunet <jbrunet@baylibre.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of version 2 of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, see <http://www.gnu.org/licenses/>.
16  * The full GNU General Public License is included in this distribution
17  * in the file called COPYING.
18  */
19 
20 #include <linux/gpio/consumer.h>
21 #include <linux/module.h>
22 #include <sound/soc.h>
23 
24 #define DRV_NAME "simple-amplifier"
25 
26 struct simple_amp {
27 	struct gpio_desc *gpiod_enable;
28 };
29 
30 static int drv_event(struct snd_soc_dapm_widget *w,
31 		     struct snd_kcontrol *control, int event)
32 {
33 	struct snd_soc_component *c = snd_soc_dapm_to_component(w->dapm);
34 	struct simple_amp *priv = snd_soc_component_get_drvdata(c);
35 	int val;
36 
37 	switch (event) {
38 	case SND_SOC_DAPM_POST_PMU:
39 		val = 1;
40 		break;
41 	case SND_SOC_DAPM_PRE_PMD:
42 		val = 0;
43 		break;
44 	default:
45 		WARN(1, "Unexpected event");
46 		return -EINVAL;
47 	}
48 
49 	gpiod_set_value_cansleep(priv->gpiod_enable, val);
50 
51 	return 0;
52 }
53 
54 static const struct snd_soc_dapm_widget simple_amp_dapm_widgets[] = {
55 	SND_SOC_DAPM_INPUT("INL"),
56 	SND_SOC_DAPM_INPUT("INR"),
57 	SND_SOC_DAPM_OUT_DRV_E("DRV", SND_SOC_NOPM, 0, 0, NULL, 0, drv_event,
58 			       (SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD)),
59 	SND_SOC_DAPM_OUTPUT("OUTL"),
60 	SND_SOC_DAPM_OUTPUT("OUTR"),
61 };
62 
63 static const struct snd_soc_dapm_route simple_amp_dapm_routes[] = {
64 	{ "DRV", NULL, "INL" },
65 	{ "DRV", NULL, "INR" },
66 	{ "OUTL", NULL, "DRV" },
67 	{ "OUTR", NULL, "DRV" },
68 };
69 
70 static const struct snd_soc_component_driver simple_amp_component_driver = {
71 	.dapm_widgets		= simple_amp_dapm_widgets,
72 	.num_dapm_widgets	= ARRAY_SIZE(simple_amp_dapm_widgets),
73 	.dapm_routes		= simple_amp_dapm_routes,
74 	.num_dapm_routes	= ARRAY_SIZE(simple_amp_dapm_routes),
75 };
76 
77 static int simple_amp_probe(struct platform_device *pdev)
78 {
79 	struct device *dev = &pdev->dev;
80 	struct simple_amp *priv;
81 	int err;
82 
83 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
84 	if (priv == NULL)
85 		return -ENOMEM;
86 	platform_set_drvdata(pdev, priv);
87 
88 	priv->gpiod_enable = devm_gpiod_get(dev, "enable", GPIOD_OUT_LOW);
89 	if (IS_ERR(priv->gpiod_enable)) {
90 		err = PTR_ERR(priv->gpiod_enable);
91 		if (err != -EPROBE_DEFER)
92 			dev_err(dev, "Failed to get 'enable' gpio: %d", err);
93 		return err;
94 	}
95 
96 	return devm_snd_soc_register_component(dev,
97 					       &simple_amp_component_driver,
98 					       NULL, 0);
99 }
100 
101 #ifdef CONFIG_OF
102 static const struct of_device_id simple_amp_ids[] = {
103 	{ .compatible = "dioo,dio2125", },
104 	{ .compatible = "simple-audio-amplifier", },
105 	{ }
106 };
107 MODULE_DEVICE_TABLE(of, simple_amp_ids);
108 #endif
109 
110 static struct platform_driver simple_amp_driver = {
111 	.driver = {
112 		.name = DRV_NAME,
113 		.of_match_table = of_match_ptr(simple_amp_ids),
114 	},
115 	.probe = simple_amp_probe,
116 };
117 
118 module_platform_driver(simple_amp_driver);
119 
120 MODULE_DESCRIPTION("ASoC Simple Audio Amplifier driver");
121 MODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>");
122 MODULE_LICENSE("GPL");
123