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