blob: 351aa55c384e2f9cd34e47a14c4f55796adfa94a [file] [log] [blame]
Jerome Brunet85825d52017-03-06 18:44:50 +01001/*
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>
Vasily Khoruzhick6debd012018-11-22 18:23:21 +080022#include <linux/regulator/consumer.h>
Jerome Brunet85825d52017-03-06 18:44:50 +010023#include <sound/soc.h>
24
Jerome Brunet8d881bb2018-06-26 14:11:27 +020025#define DRV_NAME "simple-amplifier"
Jerome Brunet85825d52017-03-06 18:44:50 +010026
Jerome Brunet8d881bb2018-06-26 14:11:27 +020027struct simple_amp {
Jerome Brunet85825d52017-03-06 18:44:50 +010028 struct gpio_desc *gpiod_enable;
29};
30
31static 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);
Jerome Brunet8d881bb2018-06-26 14:11:27 +020035 struct simple_amp *priv = snd_soc_component_get_drvdata(c);
Jerome Brunet85825d52017-03-06 18:44:50 +010036 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
Jerome Brunetea2a2ad2017-03-07 14:12:22 +010050 gpiod_set_value_cansleep(priv->gpiod_enable, val);
Jerome Brunet85825d52017-03-06 18:44:50 +010051
52 return 0;
53}
54
Jerome Brunet8d881bb2018-06-26 14:11:27 +020055static const struct snd_soc_dapm_widget simple_amp_dapm_widgets[] = {
Jerome Brunet85825d52017-03-06 18:44:50 +010056 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"),
Vasily Khoruzhick6debd012018-11-22 18:23:21 +080062 SND_SOC_DAPM_REGULATOR_SUPPLY("VCC", 20, 0),
Jerome Brunet85825d52017-03-06 18:44:50 +010063};
64
Jerome Brunet8d881bb2018-06-26 14:11:27 +020065static const struct snd_soc_dapm_route simple_amp_dapm_routes[] = {
Jerome Brunet85825d52017-03-06 18:44:50 +010066 { "DRV", NULL, "INL" },
67 { "DRV", NULL, "INR" },
Vasily Khoruzhick6debd012018-11-22 18:23:21 +080068 { "OUTL", NULL, "VCC" },
69 { "OUTR", NULL, "VCC" },
Jerome Brunet85825d52017-03-06 18:44:50 +010070 { "OUTL", NULL, "DRV" },
71 { "OUTR", NULL, "DRV" },
72};
73
Jerome Brunet8d881bb2018-06-26 14:11:27 +020074static 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),
Jerome Brunet85825d52017-03-06 18:44:50 +010079};
80
Jerome Brunet8d881bb2018-06-26 14:11:27 +020081static int simple_amp_probe(struct platform_device *pdev)
Jerome Brunet85825d52017-03-06 18:44:50 +010082{
83 struct device *dev = &pdev->dev;
Jerome Brunet8d881bb2018-06-26 14:11:27 +020084 struct simple_amp *priv;
Jerome Brunet85825d52017-03-06 18:44:50 +010085 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
Mylène Josserand2944d292019-03-18 11:39:38 +010092 priv->gpiod_enable = devm_gpiod_get_optional(dev, "enable",
93 GPIOD_OUT_LOW);
Jerome Brunet85825d52017-03-06 18:44:50 +010094 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
Jerome Brunet8d881bb2018-06-26 14:11:27 +0200101 return devm_snd_soc_register_component(dev,
102 &simple_amp_component_driver,
Jerome Brunet85825d52017-03-06 18:44:50 +0100103 NULL, 0);
104}
105
106#ifdef CONFIG_OF
Jerome Brunet8d881bb2018-06-26 14:11:27 +0200107static const struct of_device_id simple_amp_ids[] = {
Jerome Brunet85825d52017-03-06 18:44:50 +0100108 { .compatible = "dioo,dio2125", },
Jerome Brunet8ed237e2018-06-26 14:11:28 +0200109 { .compatible = "simple-audio-amplifier", },
Jerome Brunet85825d52017-03-06 18:44:50 +0100110 { }
111};
Jerome Brunet8d881bb2018-06-26 14:11:27 +0200112MODULE_DEVICE_TABLE(of, simple_amp_ids);
Jerome Brunet85825d52017-03-06 18:44:50 +0100113#endif
114
Jerome Brunet8d881bb2018-06-26 14:11:27 +0200115static struct platform_driver simple_amp_driver = {
Jerome Brunet85825d52017-03-06 18:44:50 +0100116 .driver = {
117 .name = DRV_NAME,
Jerome Brunet8d881bb2018-06-26 14:11:27 +0200118 .of_match_table = of_match_ptr(simple_amp_ids),
Jerome Brunet85825d52017-03-06 18:44:50 +0100119 },
Jerome Brunet8d881bb2018-06-26 14:11:27 +0200120 .probe = simple_amp_probe,
Jerome Brunet85825d52017-03-06 18:44:50 +0100121};
122
Jerome Brunet8d881bb2018-06-26 14:11:27 +0200123module_platform_driver(simple_amp_driver);
Jerome Brunet85825d52017-03-06 18:44:50 +0100124
Jerome Brunet8d881bb2018-06-26 14:11:27 +0200125MODULE_DESCRIPTION("ASoC Simple Audio Amplifier driver");
Jerome Brunet85825d52017-03-06 18:44:50 +0100126MODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>");
127MODULE_LICENSE("GPL");