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 
14 struct simple_amp {
15 	struct gpio_desc *gpiod_enable;
16 };
17 
18 static 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 
42 static 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 
52 static 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 
61 static 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 
68 static int simple_amp_probe(struct platform_device *pdev)
69 {
70 	struct device *dev = &pdev->dev;
71 	struct simple_amp *priv;
72 
73 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
74 	if (priv == NULL)
75 		return -ENOMEM;
76 	platform_set_drvdata(pdev, priv);
77 
78 	priv->gpiod_enable = devm_gpiod_get_optional(dev, "enable",
79 						     GPIOD_OUT_LOW);
80 	if (IS_ERR(priv->gpiod_enable))
81 		return dev_err_probe(dev, PTR_ERR(priv->gpiod_enable),
82 				     "Failed to get 'enable' gpio");
83 
84 	return devm_snd_soc_register_component(dev,
85 					       &simple_amp_component_driver,
86 					       NULL, 0);
87 }
88 
89 #ifdef CONFIG_OF
90 static const struct of_device_id simple_amp_ids[] = {
91 	{ .compatible = "dioo,dio2125", },
92 	{ .compatible = "simple-audio-amplifier", },
93 	{ }
94 };
95 MODULE_DEVICE_TABLE(of, simple_amp_ids);
96 #endif
97 
98 static struct platform_driver simple_amp_driver = {
99 	.driver = {
100 		.name = DRV_NAME,
101 		.of_match_table = of_match_ptr(simple_amp_ids),
102 	},
103 	.probe = simple_amp_probe,
104 };
105 
106 module_platform_driver(simple_amp_driver);
107 
108 MODULE_DESCRIPTION("ASoC Simple Audio Amplifier driver");
109 MODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>");
110 MODULE_LICENSE("GPL");
111