1// SPDX-License-Identifier: GPL-2.0
2//
3// Analog Devices SSM2305 Amplifier Driver
4//
5// Copyright (C) 2018 Pengutronix, Marco Felsch <kernel@pengutronix.de>
6//
7
8#include <linux/gpio/consumer.h>
9#include <linux/module.h>
10#include <sound/soc.h>
11
12#define DRV_NAME "ssm2305"
13
14struct ssm2305 {
15	/* shutdown gpio  */
16	struct gpio_desc *gpiod_shutdown;
17};
18
19static int ssm2305_power_event(struct snd_soc_dapm_widget *w,
20			       struct snd_kcontrol *kctrl, int event)
21{
22	struct snd_soc_component *c = snd_soc_dapm_to_component(w->dapm);
23	struct ssm2305 *data = snd_soc_component_get_drvdata(c);
24
25	gpiod_set_value_cansleep(data->gpiod_shutdown,
26				 SND_SOC_DAPM_EVENT_ON(event));
27
28	return 0;
29}
30
31static const struct snd_soc_dapm_widget ssm2305_dapm_widgets[] = {
32	/* Stereo input/output */
33	SND_SOC_DAPM_INPUT("L_IN"),
34	SND_SOC_DAPM_INPUT("R_IN"),
35	SND_SOC_DAPM_OUTPUT("L_OUT"),
36	SND_SOC_DAPM_OUTPUT("R_OUT"),
37
38	SND_SOC_DAPM_SUPPLY("Power", SND_SOC_NOPM, 0, 0, ssm2305_power_event,
39			    SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
40};
41
42static const struct snd_soc_dapm_route ssm2305_dapm_routes[] = {
43	{ "L_OUT", NULL, "L_IN" },
44	{ "R_OUT", NULL, "R_IN" },
45	{ "L_IN", NULL, "Power" },
46	{ "R_IN", NULL, "Power" },
47};
48
49static const struct snd_soc_component_driver ssm2305_component_driver = {
50	.dapm_widgets		= ssm2305_dapm_widgets,
51	.num_dapm_widgets	= ARRAY_SIZE(ssm2305_dapm_widgets),
52	.dapm_routes		= ssm2305_dapm_routes,
53	.num_dapm_routes	= ARRAY_SIZE(ssm2305_dapm_routes),
54};
55
56static int ssm2305_probe(struct platform_device *pdev)
57{
58	struct device *dev = &pdev->dev;
59	struct ssm2305 *priv;
60	int err;
61
62	/* Allocate the private data */
63	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
64	if (!priv)
65		return -ENOMEM;
66
67	platform_set_drvdata(pdev, priv);
68
69	/* Get shutdown gpio */
70	priv->gpiod_shutdown = devm_gpiod_get(dev, "shutdown",
71					      GPIOD_OUT_LOW);
72	if (IS_ERR(priv->gpiod_shutdown)) {
73		err = PTR_ERR(priv->gpiod_shutdown);
74		if (err != -EPROBE_DEFER)
75			dev_err(dev, "Failed to get 'shutdown' gpio: %d\n",
76				err);
77		return err;
78	}
79
80	return devm_snd_soc_register_component(dev, &ssm2305_component_driver,
81					       NULL, 0);
82}
83
84#ifdef CONFIG_OF
85static const struct of_device_id ssm2305_of_match[] = {
86	{ .compatible = "adi,ssm2305", },
87	{ }
88};
89MODULE_DEVICE_TABLE(of, ssm2305_of_match);
90#endif
91
92static struct platform_driver ssm2305_driver = {
93	.driver = {
94		.name = DRV_NAME,
95		.of_match_table = of_match_ptr(ssm2305_of_match),
96	},
97	.probe = ssm2305_probe,
98};
99
100module_platform_driver(ssm2305_driver);
101
102MODULE_DESCRIPTION("ASoC SSM2305 amplifier driver");
103MODULE_AUTHOR("Marco Felsch <m.felsch@pengutronix.de>");
104MODULE_LICENSE("GPL v2");
105