xref: /linux/sound/soc/codecs/bd28623.c (revision 2aa680df68062e4e0c356ec2aa7100c13654907b)
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // ROHM BD28623MUV class D speaker amplifier codec driver.
4 //
5 // Copyright (c) 2018 Socionext Inc.
6 
7 #include <linux/delay.h>
8 #include <linux/gpio/consumer.h>
9 #include <linux/module.h>
10 #include <linux/of.h>
11 #include <linux/regulator/consumer.h>
12 #include <sound/pcm.h>
13 #include <sound/soc.h>
14 
15 #define BD28623_NUM_SUPPLIES    3
16 
17 static const char *const bd28623_supply_names[BD28623_NUM_SUPPLIES] = {
18 	"VCCA",
19 	"VCCP1",
20 	"VCCP2",
21 };
22 
23 struct bd28623_priv {
24 	struct device *dev;
25 	struct regulator_bulk_data supplies[BD28623_NUM_SUPPLIES];
26 	struct gpio_desc *reset_gpio;
27 	struct gpio_desc *mute_gpio;
28 
29 	int switch_spk;
30 };
31 
32 static const struct snd_soc_dapm_widget bd28623_widgets[] = {
33 	SND_SOC_DAPM_DAC("DAC", "Playback", SND_SOC_NOPM, 0, 0),
34 	SND_SOC_DAPM_OUTPUT("OUT1P"),
35 	SND_SOC_DAPM_OUTPUT("OUT1N"),
36 	SND_SOC_DAPM_OUTPUT("OUT2P"),
37 	SND_SOC_DAPM_OUTPUT("OUT2N"),
38 };
39 
40 static const struct snd_soc_dapm_route bd28623_routes[] = {
41 	{ "OUT1P", NULL, "DAC" },
42 	{ "OUT1N", NULL, "DAC" },
43 	{ "OUT2P", NULL, "DAC" },
44 	{ "OUT2N", NULL, "DAC" },
45 };
46 
47 static int bd28623_power_on(struct bd28623_priv *bd)
48 {
49 	int ret;
50 
51 	ret = regulator_bulk_enable(ARRAY_SIZE(bd->supplies), bd->supplies);
52 	if (ret) {
53 		dev_err(bd->dev, "Failed to enable supplies: %d\n", ret);
54 		return ret;
55 	}
56 
57 	gpiod_set_value_cansleep(bd->reset_gpio, 0);
58 	usleep_range(300000, 400000);
59 
60 	return 0;
61 }
62 
63 static void bd28623_power_off(struct bd28623_priv *bd)
64 {
65 	gpiod_set_value_cansleep(bd->reset_gpio, 1);
66 
67 	regulator_bulk_disable(ARRAY_SIZE(bd->supplies), bd->supplies);
68 }
69 
70 static int bd28623_get_switch_spk(struct snd_kcontrol *kcontrol,
71 				  struct snd_ctl_elem_value *ucontrol)
72 {
73 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
74 	struct bd28623_priv *bd = snd_soc_component_get_drvdata(component);
75 
76 	ucontrol->value.integer.value[0] = bd->switch_spk;
77 
78 	return 0;
79 }
80 
81 static int bd28623_set_switch_spk(struct snd_kcontrol *kcontrol,
82 				  struct snd_ctl_elem_value *ucontrol)
83 {
84 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
85 	struct bd28623_priv *bd = snd_soc_component_get_drvdata(component);
86 
87 	if (bd->switch_spk == ucontrol->value.integer.value[0])
88 		return 0;
89 
90 	bd->switch_spk = ucontrol->value.integer.value[0];
91 
92 	gpiod_set_value_cansleep(bd->mute_gpio, bd->switch_spk ? 0 : 1);
93 
94 	return 0;
95 }
96 
97 static const struct snd_kcontrol_new bd28623_controls[] = {
98 	SOC_SINGLE_BOOL_EXT("Speaker Switch", 0,
99 			    bd28623_get_switch_spk, bd28623_set_switch_spk),
100 };
101 
102 static int bd28623_codec_probe(struct snd_soc_component *component)
103 {
104 	struct bd28623_priv *bd = snd_soc_component_get_drvdata(component);
105 	int ret;
106 
107 	bd->switch_spk = 1;
108 
109 	ret = bd28623_power_on(bd);
110 	if (ret)
111 		return ret;
112 
113 	gpiod_set_value_cansleep(bd->mute_gpio, bd->switch_spk ? 0 : 1);
114 
115 	return 0;
116 }
117 
118 static void bd28623_codec_remove(struct snd_soc_component *component)
119 {
120 	struct bd28623_priv *bd = snd_soc_component_get_drvdata(component);
121 
122 	bd28623_power_off(bd);
123 }
124 
125 static int bd28623_codec_suspend(struct snd_soc_component *component)
126 {
127 	struct bd28623_priv *bd = snd_soc_component_get_drvdata(component);
128 
129 	bd28623_power_off(bd);
130 
131 	return 0;
132 }
133 
134 static int bd28623_codec_resume(struct snd_soc_component *component)
135 {
136 	struct bd28623_priv *bd = snd_soc_component_get_drvdata(component);
137 	int ret;
138 
139 	ret = bd28623_power_on(bd);
140 	if (ret)
141 		return ret;
142 
143 	gpiod_set_value_cansleep(bd->mute_gpio, bd->switch_spk ? 0 : 1);
144 
145 	return 0;
146 }
147 
148 static const struct snd_soc_component_driver soc_codec_bd = {
149 	.probe			= bd28623_codec_probe,
150 	.remove			= bd28623_codec_remove,
151 	.suspend		= bd28623_codec_suspend,
152 	.resume			= bd28623_codec_resume,
153 	.dapm_widgets		= bd28623_widgets,
154 	.num_dapm_widgets	= ARRAY_SIZE(bd28623_widgets),
155 	.dapm_routes		= bd28623_routes,
156 	.num_dapm_routes	= ARRAY_SIZE(bd28623_routes),
157 	.controls		= bd28623_controls,
158 	.num_controls		= ARRAY_SIZE(bd28623_controls),
159 	.idle_bias_on		= 1,
160 	.use_pmdown_time	= 1,
161 	.endianness		= 1,
162 };
163 
164 static struct snd_soc_dai_driver soc_dai_bd = {
165 	.name     = "bd28623-speaker",
166 	.playback = {
167 		.stream_name  = "Playback",
168 		.formats      = SNDRV_PCM_FMTBIT_S32_LE |
169 				SNDRV_PCM_FMTBIT_S24_LE |
170 				SNDRV_PCM_FMTBIT_S16_LE,
171 		.rates        = SNDRV_PCM_RATE_48000 |
172 				SNDRV_PCM_RATE_44100 |
173 				SNDRV_PCM_RATE_32000,
174 		.channels_min = 2,
175 		.channels_max = 2,
176 	},
177 };
178 
179 static int bd28623_probe(struct platform_device *pdev)
180 {
181 	struct bd28623_priv *bd;
182 	struct device *dev = &pdev->dev;
183 	int i, ret;
184 
185 	bd = devm_kzalloc(&pdev->dev, sizeof(struct bd28623_priv), GFP_KERNEL);
186 	if (!bd)
187 		return -ENOMEM;
188 
189 	for (i = 0; i < ARRAY_SIZE(bd->supplies); i++)
190 		bd->supplies[i].supply = bd28623_supply_names[i];
191 
192 	ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(bd->supplies),
193 				      bd->supplies);
194 	if (ret) {
195 		dev_err(dev, "Failed to get supplies: %d\n", ret);
196 		return ret;
197 	}
198 
199 	bd->reset_gpio = devm_gpiod_get_optional(dev, "reset",
200 						 GPIOD_OUT_HIGH);
201 	if (IS_ERR(bd->reset_gpio)) {
202 		dev_err(dev, "Failed to request reset_gpio: %ld\n",
203 			PTR_ERR(bd->reset_gpio));
204 		return PTR_ERR(bd->reset_gpio);
205 	}
206 
207 	bd->mute_gpio = devm_gpiod_get_optional(dev, "mute",
208 						GPIOD_OUT_HIGH);
209 	if (IS_ERR(bd->mute_gpio)) {
210 		dev_err(dev, "Failed to request mute_gpio: %ld\n",
211 			PTR_ERR(bd->mute_gpio));
212 		return PTR_ERR(bd->mute_gpio);
213 	}
214 
215 	platform_set_drvdata(pdev, bd);
216 	bd->dev = dev;
217 
218 	return devm_snd_soc_register_component(dev, &soc_codec_bd,
219 					       &soc_dai_bd, 1);
220 }
221 
222 static const struct of_device_id bd28623_of_match[] __maybe_unused = {
223 	{ .compatible = "rohm,bd28623", },
224 	{}
225 };
226 MODULE_DEVICE_TABLE(of, bd28623_of_match);
227 
228 static struct platform_driver bd28623_codec_driver = {
229 	.driver = {
230 		.name = "bd28623",
231 		.of_match_table = of_match_ptr(bd28623_of_match),
232 	},
233 	.probe  = bd28623_probe,
234 };
235 module_platform_driver(bd28623_codec_driver);
236 
237 MODULE_AUTHOR("Katsuhiro Suzuki <suzuki.katsuhiro@socionext.com>");
238 MODULE_DESCRIPTION("ROHM BD28623 speaker amplifier driver");
239 MODULE_LICENSE("GPL v2");
240