xref: /openbmc/linux/sound/soc/codecs/rt1015p.c (revision 801543b2)
1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // rt1015p.c  --  RT1015P ALSA SoC audio amplifier driver
4 //
5 // Copyright 2020 The Linux Foundation. All rights reserved.
6 
7 #include <linux/acpi.h>
8 #include <linux/delay.h>
9 #include <linux/device.h>
10 #include <linux/err.h>
11 #include <linux/gpio.h>
12 #include <linux/gpio/consumer.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/of.h>
16 #include <linux/platform_device.h>
17 #include <sound/pcm.h>
18 #include <sound/soc.h>
19 #include <sound/soc-dai.h>
20 #include <sound/soc-dapm.h>
21 
22 struct rt1015p_priv {
23 	struct gpio_desc *sdb;
24 	bool calib_done;
25 };
26 
27 static int rt1015p_sdb_event(struct snd_soc_dapm_widget *w,
28 		struct snd_kcontrol *kcontrol, int event)
29 {
30 	struct snd_soc_component *component =
31 		snd_soc_dapm_to_component(w->dapm);
32 	struct rt1015p_priv *rt1015p =
33 		snd_soc_component_get_drvdata(component);
34 
35 	if (!rt1015p->sdb)
36 		return 0;
37 
38 	switch (event) {
39 	case SND_SOC_DAPM_PRE_PMU:
40 		gpiod_set_value_cansleep(rt1015p->sdb, 1);
41 		dev_dbg(component->dev, "set sdb to 1");
42 
43 		if (!rt1015p->calib_done) {
44 			msleep(300);
45 			rt1015p->calib_done = true;
46 		}
47 		break;
48 	case SND_SOC_DAPM_POST_PMD:
49 		gpiod_set_value_cansleep(rt1015p->sdb, 0);
50 		dev_dbg(component->dev, "set sdb to 0");
51 		break;
52 	default:
53 		break;
54 	}
55 
56 	return 0;
57 }
58 
59 static const struct snd_soc_dapm_widget rt1015p_dapm_widgets[] = {
60 	SND_SOC_DAPM_OUTPUT("Speaker"),
61 	SND_SOC_DAPM_OUT_DRV_E("SDB", SND_SOC_NOPM, 0, 0, NULL, 0,
62 			rt1015p_sdb_event,
63 			SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
64 };
65 
66 static const struct snd_soc_dapm_route rt1015p_dapm_routes[] = {
67 	{"SDB", NULL, "HiFi Playback"},
68 	{"Speaker", NULL, "SDB"},
69 };
70 
71 #ifdef CONFIG_PM
72 static int rt1015p_suspend(struct snd_soc_component *component)
73 {
74 	struct rt1015p_priv *rt1015p = snd_soc_component_get_drvdata(component);
75 
76 	rt1015p->calib_done = false;
77 	return 0;
78 }
79 #else
80 #define rt1015p_suspend NULL
81 #endif
82 
83 static const struct snd_soc_component_driver rt1015p_component_driver = {
84 	.suspend		= rt1015p_suspend,
85 	.dapm_widgets		= rt1015p_dapm_widgets,
86 	.num_dapm_widgets	= ARRAY_SIZE(rt1015p_dapm_widgets),
87 	.dapm_routes		= rt1015p_dapm_routes,
88 	.num_dapm_routes	= ARRAY_SIZE(rt1015p_dapm_routes),
89 	.idle_bias_on		= 1,
90 	.use_pmdown_time	= 1,
91 	.endianness		= 1,
92 };
93 
94 static struct snd_soc_dai_driver rt1015p_dai_driver = {
95 	.name = "HiFi",
96 	.playback = {
97 		.stream_name	= "HiFi Playback",
98 		.formats	= SNDRV_PCM_FMTBIT_S24 |
99 					SNDRV_PCM_FMTBIT_S32,
100 		.rates		= SNDRV_PCM_RATE_48000,
101 		.channels_min	= 1,
102 		.channels_max	= 2,
103 	},
104 };
105 
106 static int rt1015p_platform_probe(struct platform_device *pdev)
107 {
108 	struct rt1015p_priv *rt1015p;
109 
110 	rt1015p = devm_kzalloc(&pdev->dev, sizeof(*rt1015p), GFP_KERNEL);
111 	if (!rt1015p)
112 		return -ENOMEM;
113 
114 	rt1015p->sdb = devm_gpiod_get_optional(&pdev->dev,
115 				"sdb", GPIOD_OUT_LOW);
116 	if (IS_ERR(rt1015p->sdb))
117 		return PTR_ERR(rt1015p->sdb);
118 
119 	dev_set_drvdata(&pdev->dev, rt1015p);
120 
121 	return devm_snd_soc_register_component(&pdev->dev,
122 			&rt1015p_component_driver,
123 			&rt1015p_dai_driver, 1);
124 }
125 
126 #ifdef CONFIG_OF
127 static const struct of_device_id rt1015p_device_id[] = {
128 	{ .compatible = "realtek,rt1015p" },
129 	{ .compatible = "realtek,rt1019p" },
130 	{}
131 };
132 MODULE_DEVICE_TABLE(of, rt1015p_device_id);
133 #endif
134 
135 #ifdef CONFIG_ACPI
136 static const struct acpi_device_id rt1015p_acpi_match[] = {
137 	{ "RTL1015", 0},
138 	{ "RTL1019", 0},
139 	{ },
140 };
141 MODULE_DEVICE_TABLE(acpi, rt1015p_acpi_match);
142 #endif
143 
144 static struct platform_driver rt1015p_platform_driver = {
145 	.driver = {
146 		.name = "rt1015p",
147 		.of_match_table = of_match_ptr(rt1015p_device_id),
148 		.acpi_match_table = ACPI_PTR(rt1015p_acpi_match),
149 	},
150 	.probe = rt1015p_platform_probe,
151 };
152 module_platform_driver(rt1015p_platform_driver);
153 
154 MODULE_DESCRIPTION("ASoC RT1015P driver");
155 MODULE_LICENSE("GPL v2");
156