1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * ALSA SoC Texas Instruments TPA6130A2 headset stereo amplifier driver
4 *
5 * Copyright (C) Nokia Corporation
6 *
7 * Author: Peter Ujfalusi <peter.ujfalusi@ti.com>
8 */
9
10 #include <linux/device.h>
11 #include <linux/errno.h>
12 #include <linux/gpio/consumer.h>
13 #include <linux/i2c.h>
14 #include <linux/module.h>
15 #include <linux/of.h>
16 #include <linux/regmap.h>
17 #include <linux/regulator/consumer.h>
18 #include <linux/slab.h>
19 #include <sound/soc.h>
20 #include <sound/tlv.h>
21
22 #include "tpa6130a2.h"
23
24 enum tpa_model {
25 TPA6130A2,
26 TPA6140A2,
27 };
28
29 /* This struct is used to save the context */
30 struct tpa6130a2_data {
31 struct device *dev;
32 struct regmap *regmap;
33 struct regulator *supply;
34 struct gpio_desc *power_gpio;
35 enum tpa_model id;
36 };
37
tpa6130a2_power(struct tpa6130a2_data * data,bool enable)38 static int tpa6130a2_power(struct tpa6130a2_data *data, bool enable)
39 {
40 int ret = 0, ret2;
41
42 if (enable) {
43 ret = regulator_enable(data->supply);
44 if (ret != 0) {
45 dev_err(data->dev,
46 "Failed to enable supply: %d\n", ret);
47 return ret;
48 }
49 /* Power on */
50 gpiod_set_value(data->power_gpio, 1);
51
52 /* Sync registers */
53 regcache_cache_only(data->regmap, false);
54 ret = regcache_sync(data->regmap);
55 if (ret != 0) {
56 dev_err(data->dev,
57 "Failed to sync registers: %d\n", ret);
58 regcache_cache_only(data->regmap, true);
59 gpiod_set_value(data->power_gpio, 0);
60 ret2 = regulator_disable(data->supply);
61 if (ret2 != 0)
62 dev_err(data->dev,
63 "Failed to disable supply: %d\n", ret2);
64 return ret;
65 }
66 } else {
67 /* Powered off device does not retain registers. While device
68 * is off, any register updates (i.e. volume changes) should
69 * happen in cache only.
70 */
71 regcache_mark_dirty(data->regmap);
72 regcache_cache_only(data->regmap, true);
73
74 /* Power off */
75 gpiod_set_value(data->power_gpio, 0);
76
77 ret = regulator_disable(data->supply);
78 if (ret != 0) {
79 dev_err(data->dev,
80 "Failed to disable supply: %d\n", ret);
81 return ret;
82 }
83 }
84
85 return ret;
86 }
87
tpa6130a2_power_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kctrl,int event)88 static int tpa6130a2_power_event(struct snd_soc_dapm_widget *w,
89 struct snd_kcontrol *kctrl, int event)
90 {
91 struct snd_soc_component *c = snd_soc_dapm_to_component(w->dapm);
92 struct tpa6130a2_data *data = snd_soc_component_get_drvdata(c);
93
94 if (SND_SOC_DAPM_EVENT_ON(event)) {
95 /* Before widget power up: turn chip on, sync registers */
96 return tpa6130a2_power(data, true);
97 } else {
98 /* After widget power down: turn chip off */
99 return tpa6130a2_power(data, false);
100 }
101 }
102
103 /*
104 * TPA6130 volume. From -59.5 to 4 dB with increasing step size when going
105 * down in gain.
106 */
107 static const DECLARE_TLV_DB_RANGE(tpa6130_tlv,
108 0, 1, TLV_DB_SCALE_ITEM(-5950, 600, 0),
109 2, 3, TLV_DB_SCALE_ITEM(-5000, 250, 0),
110 4, 5, TLV_DB_SCALE_ITEM(-4550, 160, 0),
111 6, 7, TLV_DB_SCALE_ITEM(-4140, 190, 0),
112 8, 9, TLV_DB_SCALE_ITEM(-3650, 120, 0),
113 10, 11, TLV_DB_SCALE_ITEM(-3330, 160, 0),
114 12, 13, TLV_DB_SCALE_ITEM(-3040, 180, 0),
115 14, 20, TLV_DB_SCALE_ITEM(-2710, 110, 0),
116 21, 37, TLV_DB_SCALE_ITEM(-1960, 74, 0),
117 38, 63, TLV_DB_SCALE_ITEM(-720, 45, 0)
118 );
119
120 static const struct snd_kcontrol_new tpa6130a2_controls[] = {
121 SOC_SINGLE_TLV("Headphone Playback Volume",
122 TPA6130A2_REG_VOL_MUTE, 0, 0x3f, 0,
123 tpa6130_tlv),
124 };
125
126 static const DECLARE_TLV_DB_RANGE(tpa6140_tlv,
127 0, 8, TLV_DB_SCALE_ITEM(-5900, 400, 0),
128 9, 16, TLV_DB_SCALE_ITEM(-2500, 200, 0),
129 17, 31, TLV_DB_SCALE_ITEM(-1000, 100, 0)
130 );
131
132 static const struct snd_kcontrol_new tpa6140a2_controls[] = {
133 SOC_SINGLE_TLV("Headphone Playback Volume",
134 TPA6130A2_REG_VOL_MUTE, 1, 0x1f, 0,
135 tpa6140_tlv),
136 };
137
tpa6130a2_component_probe(struct snd_soc_component * component)138 static int tpa6130a2_component_probe(struct snd_soc_component *component)
139 {
140 struct tpa6130a2_data *data = snd_soc_component_get_drvdata(component);
141
142 if (data->id == TPA6140A2)
143 return snd_soc_add_component_controls(component,
144 tpa6140a2_controls, ARRAY_SIZE(tpa6140a2_controls));
145 else
146 return snd_soc_add_component_controls(component,
147 tpa6130a2_controls, ARRAY_SIZE(tpa6130a2_controls));
148 }
149
150 static const struct snd_soc_dapm_widget tpa6130a2_dapm_widgets[] = {
151 SND_SOC_DAPM_INPUT("LEFTIN"),
152 SND_SOC_DAPM_INPUT("RIGHTIN"),
153 SND_SOC_DAPM_OUTPUT("HPLEFT"),
154 SND_SOC_DAPM_OUTPUT("HPRIGHT"),
155
156 SND_SOC_DAPM_PGA("Left Mute", TPA6130A2_REG_VOL_MUTE,
157 TPA6130A2_HP_EN_L_SHIFT, 1, NULL, 0),
158 SND_SOC_DAPM_PGA("Right Mute", TPA6130A2_REG_VOL_MUTE,
159 TPA6130A2_HP_EN_R_SHIFT, 1, NULL, 0),
160 SND_SOC_DAPM_PGA("Left PGA", TPA6130A2_REG_CONTROL,
161 TPA6130A2_HP_EN_L_SHIFT, 0, NULL, 0),
162 SND_SOC_DAPM_PGA("Right PGA", TPA6130A2_REG_CONTROL,
163 TPA6130A2_HP_EN_R_SHIFT, 0, NULL, 0),
164
165 SND_SOC_DAPM_SUPPLY("Power", TPA6130A2_REG_CONTROL,
166 TPA6130A2_SWS_SHIFT, 1, tpa6130a2_power_event,
167 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
168 };
169
170 static const struct snd_soc_dapm_route tpa6130a2_dapm_routes[] = {
171 { "Left PGA", NULL, "LEFTIN" },
172 { "Right PGA", NULL, "RIGHTIN" },
173
174 { "Left Mute", NULL, "Left PGA" },
175 { "Right Mute", NULL, "Right PGA" },
176
177 { "HPLEFT", NULL, "Left Mute" },
178 { "HPRIGHT", NULL, "Right Mute" },
179
180 { "Left PGA", NULL, "Power" },
181 { "Right PGA", NULL, "Power" },
182 };
183
184 static const struct snd_soc_component_driver tpa6130a2_component_driver = {
185 .name = "tpa6130a2",
186 .probe = tpa6130a2_component_probe,
187 .dapm_widgets = tpa6130a2_dapm_widgets,
188 .num_dapm_widgets = ARRAY_SIZE(tpa6130a2_dapm_widgets),
189 .dapm_routes = tpa6130a2_dapm_routes,
190 .num_dapm_routes = ARRAY_SIZE(tpa6130a2_dapm_routes),
191 };
192
193 static const struct reg_default tpa6130a2_reg_defaults[] = {
194 { TPA6130A2_REG_CONTROL, TPA6130A2_SWS },
195 { TPA6130A2_REG_VOL_MUTE, TPA6130A2_MUTE_R | TPA6130A2_MUTE_L },
196 };
197
198 static const struct regmap_config tpa6130a2_regmap_config = {
199 .reg_bits = 8,
200 .val_bits = 8,
201 .max_register = TPA6130A2_REG_VERSION,
202 .reg_defaults = tpa6130a2_reg_defaults,
203 .num_reg_defaults = ARRAY_SIZE(tpa6130a2_reg_defaults),
204 .cache_type = REGCACHE_RBTREE,
205 };
206
tpa6130a2_probe(struct i2c_client * client)207 static int tpa6130a2_probe(struct i2c_client *client)
208 {
209 struct device *dev;
210 struct tpa6130a2_data *data;
211 struct device_node *np = client->dev.of_node;
212 const char *regulator;
213 unsigned int version;
214 int ret;
215
216 dev = &client->dev;
217
218 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
219 if (!data)
220 return -ENOMEM;
221
222 data->dev = dev;
223
224 data->regmap = devm_regmap_init_i2c(client, &tpa6130a2_regmap_config);
225 if (IS_ERR(data->regmap))
226 return PTR_ERR(data->regmap);
227
228 if (np) {
229 data->power_gpio = devm_gpiod_get_optional(dev, "power", GPIOD_OUT_LOW);
230 if (IS_ERR(data->power_gpio)) {
231 return dev_err_probe(dev, PTR_ERR(data->power_gpio),
232 "Failed to request power GPIO\n");
233 }
234 gpiod_set_consumer_name(data->power_gpio, "tpa6130a2 enable");
235 } else {
236 dev_err(dev, "Platform data not set\n");
237 dump_stack();
238 return -ENODEV;
239 }
240
241 i2c_set_clientdata(client, data);
242
243 data->id = (uintptr_t)i2c_get_match_data(client);
244
245 switch (data->id) {
246 default:
247 dev_warn(dev, "Unknown TPA model (%d). Assuming 6130A2\n",
248 data->id);
249 fallthrough;
250 case TPA6130A2:
251 regulator = "Vdd";
252 break;
253 case TPA6140A2:
254 regulator = "AVdd";
255 break;
256 }
257
258 data->supply = devm_regulator_get(dev, regulator);
259 if (IS_ERR(data->supply)) {
260 ret = PTR_ERR(data->supply);
261 dev_err(dev, "Failed to request supply: %d\n", ret);
262 return ret;
263 }
264
265 ret = tpa6130a2_power(data, true);
266 if (ret != 0)
267 return ret;
268
269
270 /* Read version */
271 regmap_read(data->regmap, TPA6130A2_REG_VERSION, &version);
272 version &= TPA6130A2_VERSION_MASK;
273 if ((version != 1) && (version != 2))
274 dev_warn(dev, "UNTESTED version detected (%d)\n", version);
275
276 /* Disable the chip */
277 ret = tpa6130a2_power(data, false);
278 if (ret != 0)
279 return ret;
280
281 return devm_snd_soc_register_component(&client->dev,
282 &tpa6130a2_component_driver, NULL, 0);
283 }
284
285 #if IS_ENABLED(CONFIG_OF)
286 static const struct of_device_id tpa6130a2_of_match[] = {
287 { .compatible = "ti,tpa6130a2", },
288 { .compatible = "ti,tpa6140a2" },
289 {},
290 };
291 MODULE_DEVICE_TABLE(of, tpa6130a2_of_match);
292 #endif
293
294 static struct i2c_driver tpa6130a2_i2c_driver = {
295 .driver = {
296 .name = "tpa6130a2",
297 .of_match_table = of_match_ptr(tpa6130a2_of_match),
298 },
299 .probe = tpa6130a2_probe,
300 };
301
302 module_i2c_driver(tpa6130a2_i2c_driver);
303
304 MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
305 MODULE_DESCRIPTION("TPA6130A2 Headphone amplifier driver");
306 MODULE_LICENSE("GPL");
307