aboutsummaryrefslogtreecommitdiffstats
path: root/sound/soc/codecs/bd28623.c
blob: 31904ef5c88bf9e9ba885fe03dcf1724a4760957 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// SPDX-License-Identifier: GPL-2.0
//
// ROHM BD28623MUV class D speaker amplifier codec driver.
//
// Copyright (c) 2018 Socionext Inc.

#include <linux/delay.h>
#include <linux/gpio/consumer.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/regulator/consumer.h>
#include <sound/pcm.h>
#include <sound/soc.h>

#define BD28623_NUM_SUPPLIES    3

static const char *const bd28623_supply_names[BD28623_NUM_SUPPLIES] = {
	"VCCA",
	"VCCP1",
	"VCCP2",
};

struct bd28623_priv {
	struct device *dev;
	struct regulator_bulk_data supplies[BD28623_NUM_SUPPLIES];
	struct gpio_desc *reset_gpio;
	struct gpio_desc *mute_gpio;

	int switch_spk;
};

static const struct snd_soc_dapm_widget bd28623_widgets[] = {
	SND_SOC_DAPM_DAC("DAC", "Playback", SND_SOC_NOPM, 0, 0),
	SND_SOC_DAPM_OUTPUT("OUT1P"),
	SND_SOC_DAPM_OUTPUT("OUT1N"),
	SND_SOC_DAPM_OUTPUT("OUT2P"),
	SND_SOC_DAPM_OUTPUT("OUT2N"),
};

static const struct snd_soc_dapm_route bd28623_routes[] = {
	{ "OUT1P", NULL, "DAC" },
	{ "OUT1N", NULL, "DAC" },
	{ "OUT2P", NULL, "DAC" },
	{ "OUT2N", NULL, "DAC" },
};

static int bd28623_power_on(struct bd28623_priv *bd)
{
	int ret;

	ret = regulator_bulk_enable(ARRAY_SIZE(bd->supplies), bd->supplies);
	if (ret) {
		dev_err(bd->dev, "Failed to enable supplies: %d\n", ret);
		return ret;
	}

	gpiod_set_value_cansleep(bd->reset_gpio, 0);
	usleep_range(300000, 400000);

	return 0;
}

static void bd28623_power_off(struct bd28623_priv *bd)
{
	gpiod_set_value_cansleep(bd->reset_gpio, 1);

	regulator_bulk_disable(ARRAY_SIZE(bd->supplies), bd->supplies);
}

static int bd28623_get_switch_spk(struct snd_kcontrol *kcontrol,
				  struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_component *component =
		snd_soc_kcontrol_component(kcontrol);
	struct bd28623_priv *bd = snd_soc_component_get_drvdata(component);

	ucontrol->value.integer.value[0] = bd->switch_spk;

	return 0;
}

static int bd28623_set_switch_spk(struct snd_kcontrol *kcontrol,
				  struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_component *component =
		snd_soc_kcontrol_component(kcontrol);
	struct bd28623_priv *bd = snd_soc_component_get_drvdata(component);

	if (bd->switch_spk == ucontrol->value.integer.value[0])
		return 0;

	bd->switch_spk = ucontrol->value.integer.value[0];

	gpiod_set_value_cansleep(bd->mute_gpio, bd->switch_spk ? 0 : 1);

	return 0;
}

static const struct snd_kcontrol_new bd28623_controls[] = {
	SOC_SINGLE_BOOL_EXT("Speaker Switch", 0,
			    bd28623_get_switch_spk, bd28623_set_switch_spk),
};

static int bd28623_codec_probe(struct snd_soc_component *component)
{
	struct bd28623_priv *bd = snd_soc_component_get_drvdata(component);
	int ret;

	bd->switch_spk = 1;

	ret = bd28623_power_on(bd);
	if (ret)
		return ret;

	gpiod_set_value_cansleep(bd->mute_gpio, bd->switch_spk ? 0 : 1);

	return 0;
}

static void bd28623_codec_remove(struct snd_soc_component *component)
{
	struct bd28623_priv *bd = snd_soc_component_get_drvdata(component);

	bd28623_power_off(bd);
}

static int bd28623_codec_suspend(struct snd_soc_component *component)
{
	struct bd28623_priv *bd = snd_soc_component_get_drvdata(component);

	bd28623_power_off(bd);

	return 0;
}

static int bd28623_codec_resume(struct snd_soc_component *component)
{
	struct bd28623_priv *bd = snd_soc_component_get_drvdata(component);
	int ret;

	ret = bd28623_power_on(bd);
	if (ret)
		return ret;

	gpiod_set_value_cansleep(bd->mute_gpio, bd->switch_spk ? 0 : 1);

	return 0;
}

static const struct snd_soc_component_driver soc_codec_bd = {
	.probe			= bd28623_codec_probe,
	.remove			= bd28623_codec_remove,
	.suspend		= bd28623_codec_suspend,
	.resume			= bd28623_codec_resume,
	.dapm_widgets		= bd28623_widgets,
	.num_dapm_widgets	= ARRAY_SIZE(bd28623_widgets),
	.dapm_routes		= bd28623_routes,
	.num_dapm_routes	= ARRAY_SIZE(bd28623_routes),
	.controls		= bd28623_controls,
	.num_controls		= ARRAY_SIZE(bd28623_controls),
	.idle_bias_on		= 1,
	.use_pmdown_time	= 1,
	.endianness		= 1,
	.non_legacy_dai_naming	= 1,
};

static struct snd_soc_dai_driver soc_dai_bd = {
	.name     = "bd28623-speaker",
	.playback = {
		.stream_name  = "Playback",
		.formats      = SNDRV_PCM_FMTBIT_S32_LE |
				SNDRV_PCM_FMTBIT_S24_LE |
				SNDRV_PCM_FMTBIT_S16_LE,
		.rates        = SNDRV_PCM_RATE_48000 |
				SNDRV_PCM_RATE_44100 |
				SNDRV_PCM_RATE_32000,
		.channels_min = 2,
		.channels_max = 2,
	},
};

static int bd28623_probe(struct platform_device *pdev)
{
	struct bd28623_priv *bd;
	struct device *dev = &pdev->dev;
	int i, ret;

	bd = devm_kzalloc(&pdev->dev, sizeof(struct bd28623_priv), GFP_KERNEL);
	if (!bd)
		return -ENOMEM;

	for (i = 0; i < ARRAY_SIZE(bd->supplies); i++)
		bd->supplies[i].supply = bd28623_supply_names[i];

	ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(bd->supplies),
				      bd->supplies);
	if (ret) {
		dev_err(dev, "Failed to get supplies: %d\n", ret);
		return ret;
	}

	bd->reset_gpio = devm_gpiod_get_optional(dev, "reset",
						 GPIOD_OUT_HIGH);
	if (IS_ERR(bd->reset_gpio)) {
		dev_err(dev, "Failed to request reset_gpio: %ld\n",
			PTR_ERR(bd->reset_gpio));
		return PTR_ERR(bd->reset_gpio);
	}

	bd->mute_gpio = devm_gpiod_get_optional(dev, "mute",
						GPIOD_OUT_HIGH);
	if (IS_ERR(bd->mute_gpio)) {
		dev_err(dev, "Failed to request mute_gpio: %ld\n",
			PTR_ERR(bd->mute_gpio));
		return PTR_ERR(bd->mute_gpio);
	}

	platform_set_drvdata(pdev, bd);
	bd->dev = dev;

	return devm_snd_soc_register_component(dev, &soc_codec_bd,
					       &soc_dai_bd, 1);
}

static const struct of_device_id bd28623_of_match[] = {
	{ .compatible = "rohm,bd28623", },
	{}
};
MODULE_DEVICE_TABLE(of, bd28623_of_match);

static struct platform_driver bd28623_codec_driver = {
	.driver = {
		.name = "bd28623",
		.of_match_table = of_match_ptr(bd28623_of_match),
	},
	.probe  = bd28623_probe,
};
module_platform_driver(bd28623_codec_driver);

MODULE_AUTHOR("Katsuhiro Suzuki <suzuki.katsuhiro@socionext.com>");
MODULE_DESCRIPTION("ROHM BD28623 speaker amplifier driver");
MODULE_LICENSE("GPL v2");