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 <linux/regulator/consumer.h>
23 #include <sound/soc.h>
24 
25 #define DRV_NAME "simple-amplifier"
26 
27 struct simple_amp {
28 	struct gpio_desc *gpiod_enable;
29 };
30 
31 static int drv_event(struct snd_soc_dapm_widget *w,
32 		     struct snd_kcontrol *control, int event)
33 {
34 	struct snd_soc_component *c = snd_soc_dapm_to_component(w->dapm);
35 	struct simple_amp *priv = snd_soc_component_get_drvdata(c);
36 	int val;
37 
38 	switch (event) {
39 	case SND_SOC_DAPM_POST_PMU:
40 		val = 1;
41 		break;
42 	case SND_SOC_DAPM_PRE_PMD:
43 		val = 0;
44 		break;
45 	default:
46 		WARN(1, "Unexpected event");
47 		return -EINVAL;
48 	}
49 
50 	gpiod_set_value_cansleep(priv->gpiod_enable, val);
51 
52 	return 0;
53 }
54 
55 static const struct snd_soc_dapm_widget simple_amp_dapm_widgets[] = {
56 	SND_SOC_DAPM_INPUT("INL"),
57 	SND_SOC_DAPM_INPUT("INR"),
58 	SND_SOC_DAPM_OUT_DRV_E("DRV", SND_SOC_NOPM, 0, 0, NULL, 0, drv_event,
59 			       (SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD)),
60 	SND_SOC_DAPM_OUTPUT("OUTL"),
61 	SND_SOC_DAPM_OUTPUT("OUTR"),
62 	SND_SOC_DAPM_REGULATOR_SUPPLY("VCC", 20, 0),
63 };
64 
65 static const struct snd_soc_dapm_route simple_amp_dapm_routes[] = {
66 	{ "DRV", NULL, "INL" },
67 	{ "DRV", NULL, "INR" },
68 	{ "OUTL", NULL, "VCC" },
69 	{ "OUTR", NULL, "VCC" },
70 	{ "OUTL", NULL, "DRV" },
71 	{ "OUTR", NULL, "DRV" },
72 };
73 
74 static const struct snd_soc_component_driver simple_amp_component_driver = {
75 	.dapm_widgets		= simple_amp_dapm_widgets,
76 	.num_dapm_widgets	= ARRAY_SIZE(simple_amp_dapm_widgets),
77 	.dapm_routes		= simple_amp_dapm_routes,
78 	.num_dapm_routes	= ARRAY_SIZE(simple_amp_dapm_routes),
79 };
80 
81 static int simple_amp_probe(struct platform_device *pdev)
82 {
83 	struct device *dev = &pdev->dev;
84 	struct simple_amp *priv;
85 	int err;
86 
87 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
88 	if (priv == NULL)
89 		return -ENOMEM;
90 	platform_set_drvdata(pdev, priv);
91 
92 	priv->gpiod_enable = devm_gpiod_get_optional(dev, "enable",
93 						     GPIOD_OUT_LOW);
94 	if (IS_ERR(priv->gpiod_enable)) {
95 		err = PTR_ERR(priv->gpiod_enable);
96 		if (err != -EPROBE_DEFER)
97 			dev_err(dev, "Failed to get 'enable' gpio: %d", err);
98 		return err;
99 	}
100 
101 	return devm_snd_soc_register_component(dev,
102 					       &simple_amp_component_driver,
103 					       NULL, 0);
104 }
105 
106 #ifdef CONFIG_OF
107 static const struct of_device_id simple_amp_ids[] = {
108 	{ .compatible = "dioo,dio2125", },
109 	{ .compatible = "simple-audio-amplifier", },
110 	{ }
111 };
112 MODULE_DEVICE_TABLE(of, simple_amp_ids);
113 #endif
114 
115 static struct platform_driver simple_amp_driver = {
116 	.driver = {
117 		.name = DRV_NAME,
118 		.of_match_table = of_match_ptr(simple_amp_ids),
119 	},
120 	.probe = simple_amp_probe,
121 };
122 
123 module_platform_driver(simple_amp_driver);
124 
125 MODULE_DESCRIPTION("ASoC Simple Audio Amplifier driver");
126 MODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>");
127 MODULE_LICENSE("GPL");
128