]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - sound/soc/codecs/tpa6130a2.c
ASoC: tpa6130a2: Remove goto err_gpio
[mirror_ubuntu-bionic-kernel.git] / sound / soc / codecs / tpa6130a2.c
1 /*
2 * ALSA SoC Texas Instruments TPA6130A2 headset stereo amplifier driver
3 *
4 * Copyright (C) Nokia Corporation
5 *
6 * Author: Peter Ujfalusi <peter.ujfalusi@ti.com>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA
21 */
22
23 #include <linux/module.h>
24 #include <linux/errno.h>
25 #include <linux/device.h>
26 #include <linux/i2c.h>
27 #include <linux/gpio.h>
28 #include <linux/regulator/consumer.h>
29 #include <linux/slab.h>
30 #include <sound/tpa6130a2-plat.h>
31 #include <sound/soc.h>
32 #include <sound/tlv.h>
33 #include <linux/of.h>
34 #include <linux/of_gpio.h>
35 #include <linux/regmap.h>
36
37 #include "tpa6130a2.h"
38
39 enum tpa_model {
40 TPA6130A2,
41 TPA6140A2,
42 };
43
44 /* This struct is used to save the context */
45 struct tpa6130a2_data {
46 struct device *dev;
47 struct regmap *regmap;
48 struct regulator *supply;
49 int power_gpio;
50 enum tpa_model id;
51 };
52
53 static int tpa6130a2_power(struct tpa6130a2_data *data, bool enable)
54 {
55 int ret;
56
57 if (enable) {
58 ret = regulator_enable(data->supply);
59 if (ret != 0) {
60 dev_err(data->dev,
61 "Failed to enable supply: %d\n", ret);
62 return ret;
63 }
64 /* Power on */
65 if (data->power_gpio >= 0)
66 gpio_set_value(data->power_gpio, 1);
67 } else {
68 /* Power off */
69 if (data->power_gpio >= 0)
70 gpio_set_value(data->power_gpio, 0);
71
72 ret = regulator_disable(data->supply);
73 if (ret != 0) {
74 dev_err(data->dev,
75 "Failed to disable supply: %d\n", ret);
76 return ret;
77 }
78
79 /* device regs does not match the cache state anymore */
80 regcache_mark_dirty(data->regmap);
81 }
82
83 return ret;
84 }
85
86 static int tpa6130a2_power_event(struct snd_soc_dapm_widget *w,
87 struct snd_kcontrol *kctrl, int event)
88 {
89 struct snd_soc_component *c = snd_soc_dapm_to_component(w->dapm);
90 struct tpa6130a2_data *data = snd_soc_component_get_drvdata(c);
91 int ret;
92
93 /* before widget power up */
94 if (SND_SOC_DAPM_EVENT_ON(event)) {
95 /* Turn on the chip */
96 tpa6130a2_power(data, true);
97 /* Sync the registers */
98 ret = regcache_sync(data->regmap);
99 if (ret < 0) {
100 dev_err(c->dev, "Failed to initialize chip\n");
101 tpa6130a2_power(data, false);
102 return ret;
103 }
104 /* after widget power down */
105 } else {
106 tpa6130a2_power(data, false);
107 }
108
109 return 0;
110 }
111
112 /*
113 * TPA6130 volume. From -59.5 to 4 dB with increasing step size when going
114 * down in gain.
115 */
116 static const DECLARE_TLV_DB_RANGE(tpa6130_tlv,
117 0, 1, TLV_DB_SCALE_ITEM(-5950, 600, 0),
118 2, 3, TLV_DB_SCALE_ITEM(-5000, 250, 0),
119 4, 5, TLV_DB_SCALE_ITEM(-4550, 160, 0),
120 6, 7, TLV_DB_SCALE_ITEM(-4140, 190, 0),
121 8, 9, TLV_DB_SCALE_ITEM(-3650, 120, 0),
122 10, 11, TLV_DB_SCALE_ITEM(-3330, 160, 0),
123 12, 13, TLV_DB_SCALE_ITEM(-3040, 180, 0),
124 14, 20, TLV_DB_SCALE_ITEM(-2710, 110, 0),
125 21, 37, TLV_DB_SCALE_ITEM(-1960, 74, 0),
126 38, 63, TLV_DB_SCALE_ITEM(-720, 45, 0)
127 );
128
129 static const struct snd_kcontrol_new tpa6130a2_controls[] = {
130 SOC_SINGLE_TLV("Headphone Playback Volume",
131 TPA6130A2_REG_VOL_MUTE, 0, 0x3f, 0,
132 tpa6130_tlv),
133 };
134
135 static const DECLARE_TLV_DB_RANGE(tpa6140_tlv,
136 0, 8, TLV_DB_SCALE_ITEM(-5900, 400, 0),
137 9, 16, TLV_DB_SCALE_ITEM(-2500, 200, 0),
138 17, 31, TLV_DB_SCALE_ITEM(-1000, 100, 0)
139 );
140
141 static const struct snd_kcontrol_new tpa6140a2_controls[] = {
142 SOC_SINGLE_TLV("Headphone Playback Volume",
143 TPA6130A2_REG_VOL_MUTE, 1, 0x1f, 0,
144 tpa6140_tlv),
145 };
146
147 static int tpa6130a2_component_probe(struct snd_soc_component *component)
148 {
149 struct tpa6130a2_data *data = snd_soc_component_get_drvdata(component);
150
151 if (data->id == TPA6140A2)
152 return snd_soc_add_component_controls(component,
153 tpa6140a2_controls, ARRAY_SIZE(tpa6140a2_controls));
154 else
155 return snd_soc_add_component_controls(component,
156 tpa6130a2_controls, ARRAY_SIZE(tpa6130a2_controls));
157 }
158
159 static const struct snd_soc_dapm_widget tpa6130a2_dapm_widgets[] = {
160 SND_SOC_DAPM_INPUT("LEFTIN"),
161 SND_SOC_DAPM_INPUT("RIGHTIN"),
162 SND_SOC_DAPM_OUTPUT("HPLEFT"),
163 SND_SOC_DAPM_OUTPUT("HPRIGHT"),
164
165 SND_SOC_DAPM_PGA("Left Mute", TPA6130A2_REG_VOL_MUTE,
166 TPA6130A2_HP_EN_L_SHIFT, 1, NULL, 0),
167 SND_SOC_DAPM_PGA("Right Mute", TPA6130A2_REG_VOL_MUTE,
168 TPA6130A2_HP_EN_R_SHIFT, 1, NULL, 0),
169 SND_SOC_DAPM_PGA("Left PGA", TPA6130A2_REG_CONTROL,
170 TPA6130A2_HP_EN_L_SHIFT, 0, NULL, 0),
171 SND_SOC_DAPM_PGA("Right PGA", TPA6130A2_REG_CONTROL,
172 TPA6130A2_HP_EN_R_SHIFT, 0, NULL, 0),
173
174 SND_SOC_DAPM_SUPPLY("Power", TPA6130A2_REG_CONTROL,
175 TPA6130A2_SWS_SHIFT, 1, tpa6130a2_power_event,
176 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
177 };
178
179 static const struct snd_soc_dapm_route tpa6130a2_dapm_routes[] = {
180 { "Left PGA", NULL, "LEFTIN" },
181 { "Right PGA", NULL, "RIGHTIN" },
182
183 { "Left Mute", NULL, "Left PGA" },
184 { "Right Mute", NULL, "Right PGA" },
185
186 { "HPLEFT", NULL, "Left Mute" },
187 { "HPRIGHT", NULL, "Right Mute" },
188
189 { "Left PGA", NULL, "Power" },
190 { "Right PGA", NULL, "Power" },
191 };
192
193 struct snd_soc_component_driver tpa6130a2_component_driver = {
194 .name = "tpa6130a2",
195 .probe = tpa6130a2_component_probe,
196 .dapm_widgets = tpa6130a2_dapm_widgets,
197 .num_dapm_widgets = ARRAY_SIZE(tpa6130a2_dapm_widgets),
198 .dapm_routes = tpa6130a2_dapm_routes,
199 .num_dapm_routes = ARRAY_SIZE(tpa6130a2_dapm_routes),
200 };
201
202 static const struct reg_default tpa6130a2_reg_defaults[] = {
203 { TPA6130A2_REG_CONTROL, TPA6130A2_SWS },
204 { TPA6130A2_REG_VOL_MUTE, TPA6130A2_MUTE_R | TPA6130A2_MUTE_L },
205 };
206
207 static const struct regmap_config tpa6130a2_regmap_config = {
208 .reg_bits = 8,
209 .val_bits = 8,
210 .max_register = TPA6130A2_REG_VERSION,
211 .reg_defaults = tpa6130a2_reg_defaults,
212 .num_reg_defaults = ARRAY_SIZE(tpa6130a2_reg_defaults),
213 .cache_type = REGCACHE_RBTREE,
214 };
215
216 static int tpa6130a2_probe(struct i2c_client *client,
217 const struct i2c_device_id *id)
218 {
219 struct device *dev;
220 struct tpa6130a2_data *data;
221 struct tpa6130a2_platform_data *pdata = client->dev.platform_data;
222 struct device_node *np = client->dev.of_node;
223 const char *regulator;
224 unsigned int version;
225 int ret;
226
227 dev = &client->dev;
228
229 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
230 if (!data)
231 return -ENOMEM;
232
233 data->dev = dev;
234
235 data->regmap = devm_regmap_init_i2c(client, &tpa6130a2_regmap_config);
236 if (IS_ERR(data->regmap))
237 return PTR_ERR(data->regmap);
238
239 if (pdata) {
240 data->power_gpio = pdata->power_gpio;
241 } else if (np) {
242 data->power_gpio = of_get_named_gpio(np, "power-gpio", 0);
243 } else {
244 dev_err(dev, "Platform data not set\n");
245 dump_stack();
246 return -ENODEV;
247 }
248
249 i2c_set_clientdata(client, data);
250
251 data->id = id->driver_data;
252
253 if (data->power_gpio >= 0) {
254 ret = devm_gpio_request(dev, data->power_gpio,
255 "tpa6130a2 enable");
256 if (ret < 0) {
257 dev_err(dev, "Failed to request power GPIO (%d)\n",
258 data->power_gpio);
259 return ret;
260 }
261 gpio_direction_output(data->power_gpio, 0);
262 }
263
264 switch (data->id) {
265 default:
266 dev_warn(dev, "Unknown TPA model (%d). Assuming 6130A2\n",
267 data->id);
268 case TPA6130A2:
269 regulator = "Vdd";
270 break;
271 case TPA6140A2:
272 regulator = "AVdd";
273 break;
274 }
275
276 data->supply = devm_regulator_get(dev, regulator);
277 if (IS_ERR(data->supply)) {
278 ret = PTR_ERR(data->supply);
279 dev_err(dev, "Failed to request supply: %d\n", ret);
280 return ret;
281 }
282
283 ret = tpa6130a2_power(data, true);
284 if (ret != 0)
285 return ret;
286
287
288 /* Read version */
289 regmap_read(data->regmap, TPA6130A2_REG_VERSION, &version);
290 version &= TPA6130A2_VERSION_MASK;
291 if ((version != 1) && (version != 2))
292 dev_warn(dev, "UNTESTED version detected (%d)\n", version);
293
294 /* Disable the chip */
295 ret = tpa6130a2_power(data, false);
296 if (ret != 0)
297 return ret;
298
299 return devm_snd_soc_register_component(&client->dev,
300 &tpa6130a2_component_driver, NULL, 0);
301 }
302
303 static const struct i2c_device_id tpa6130a2_id[] = {
304 { "tpa6130a2", TPA6130A2 },
305 { "tpa6140a2", TPA6140A2 },
306 { }
307 };
308 MODULE_DEVICE_TABLE(i2c, tpa6130a2_id);
309
310 #if IS_ENABLED(CONFIG_OF)
311 static const struct of_device_id tpa6130a2_of_match[] = {
312 { .compatible = "ti,tpa6130a2", },
313 { .compatible = "ti,tpa6140a2" },
314 {},
315 };
316 MODULE_DEVICE_TABLE(of, tpa6130a2_of_match);
317 #endif
318
319 static struct i2c_driver tpa6130a2_i2c_driver = {
320 .driver = {
321 .name = "tpa6130a2",
322 .of_match_table = of_match_ptr(tpa6130a2_of_match),
323 },
324 .probe = tpa6130a2_probe,
325 .id_table = tpa6130a2_id,
326 };
327
328 module_i2c_driver(tpa6130a2_i2c_driver);
329
330 MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
331 MODULE_DESCRIPTION("TPA6130A2 Headphone amplifier driver");
332 MODULE_LICENSE("GPL");