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