]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blob - sound/soc/codecs/tpa6130a2.c
Merge remote-tracking branches 'asoc/topic/rt5659', 'asoc/topic/rt5660', 'asoc/topic...
[mirror_ubuntu-hirsute-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 = 0, ret2;
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
68 /* Sync registers */
69 regcache_cache_only(data->regmap, false);
70 ret = regcache_sync(data->regmap);
71 if (ret != 0) {
72 dev_err(data->dev,
73 "Failed to sync registers: %d\n", ret);
74 goto regcache_sync_failed;
75 }
76 } else {
77 /* Powered off device does not retain registers. While device
78 * is off, any register updates (i.e. volume changes) should
79 * happen in cache only.
80 */
81 regcache_mark_dirty(data->regmap);
82 regcache_sync_failed:
83 regcache_cache_only(data->regmap, true);
84
85 /* Power off */
86 if (data->power_gpio >= 0)
87 gpio_set_value(data->power_gpio, 0);
88
89 ret2 = regulator_disable(data->supply);
90 if (ret2 != 0) {
91 dev_err(data->dev,
92 "Failed to disable supply: %d\n", ret2);
93 return ret ? ret : ret2;
94 }
95 }
96
97 return ret;
98 }
99
100 static int tpa6130a2_power_event(struct snd_soc_dapm_widget *w,
101 struct snd_kcontrol *kctrl, int event)
102 {
103 struct snd_soc_component *c = snd_soc_dapm_to_component(w->dapm);
104 struct tpa6130a2_data *data = snd_soc_component_get_drvdata(c);
105
106 if (SND_SOC_DAPM_EVENT_ON(event)) {
107 /* Before widget power up: turn chip on, sync registers */
108 return tpa6130a2_power(data, true);
109 } else {
110 /* After widget power down: turn chip off */
111 return tpa6130a2_power(data, false);
112 }
113 }
114
115 /*
116 * TPA6130 volume. From -59.5 to 4 dB with increasing step size when going
117 * down in gain.
118 */
119 static const DECLARE_TLV_DB_RANGE(tpa6130_tlv,
120 0, 1, TLV_DB_SCALE_ITEM(-5950, 600, 0),
121 2, 3, TLV_DB_SCALE_ITEM(-5000, 250, 0),
122 4, 5, TLV_DB_SCALE_ITEM(-4550, 160, 0),
123 6, 7, TLV_DB_SCALE_ITEM(-4140, 190, 0),
124 8, 9, TLV_DB_SCALE_ITEM(-3650, 120, 0),
125 10, 11, TLV_DB_SCALE_ITEM(-3330, 160, 0),
126 12, 13, TLV_DB_SCALE_ITEM(-3040, 180, 0),
127 14, 20, TLV_DB_SCALE_ITEM(-2710, 110, 0),
128 21, 37, TLV_DB_SCALE_ITEM(-1960, 74, 0),
129 38, 63, TLV_DB_SCALE_ITEM(-720, 45, 0)
130 );
131
132 static const struct snd_kcontrol_new tpa6130a2_controls[] = {
133 SOC_SINGLE_TLV("Headphone Playback Volume",
134 TPA6130A2_REG_VOL_MUTE, 0, 0x3f, 0,
135 tpa6130_tlv),
136 };
137
138 static const DECLARE_TLV_DB_RANGE(tpa6140_tlv,
139 0, 8, TLV_DB_SCALE_ITEM(-5900, 400, 0),
140 9, 16, TLV_DB_SCALE_ITEM(-2500, 200, 0),
141 17, 31, TLV_DB_SCALE_ITEM(-1000, 100, 0)
142 );
143
144 static const struct snd_kcontrol_new tpa6140a2_controls[] = {
145 SOC_SINGLE_TLV("Headphone Playback Volume",
146 TPA6130A2_REG_VOL_MUTE, 1, 0x1f, 0,
147 tpa6140_tlv),
148 };
149
150 static int tpa6130a2_component_probe(struct snd_soc_component *component)
151 {
152 struct tpa6130a2_data *data = snd_soc_component_get_drvdata(component);
153
154 if (data->id == TPA6140A2)
155 return snd_soc_add_component_controls(component,
156 tpa6140a2_controls, ARRAY_SIZE(tpa6140a2_controls));
157 else
158 return snd_soc_add_component_controls(component,
159 tpa6130a2_controls, ARRAY_SIZE(tpa6130a2_controls));
160 }
161
162 static const struct snd_soc_dapm_widget tpa6130a2_dapm_widgets[] = {
163 SND_SOC_DAPM_INPUT("LEFTIN"),
164 SND_SOC_DAPM_INPUT("RIGHTIN"),
165 SND_SOC_DAPM_OUTPUT("HPLEFT"),
166 SND_SOC_DAPM_OUTPUT("HPRIGHT"),
167
168 SND_SOC_DAPM_PGA("Left Mute", TPA6130A2_REG_VOL_MUTE,
169 TPA6130A2_HP_EN_L_SHIFT, 1, NULL, 0),
170 SND_SOC_DAPM_PGA("Right Mute", TPA6130A2_REG_VOL_MUTE,
171 TPA6130A2_HP_EN_R_SHIFT, 1, NULL, 0),
172 SND_SOC_DAPM_PGA("Left PGA", TPA6130A2_REG_CONTROL,
173 TPA6130A2_HP_EN_L_SHIFT, 0, NULL, 0),
174 SND_SOC_DAPM_PGA("Right PGA", TPA6130A2_REG_CONTROL,
175 TPA6130A2_HP_EN_R_SHIFT, 0, NULL, 0),
176
177 SND_SOC_DAPM_SUPPLY("Power", TPA6130A2_REG_CONTROL,
178 TPA6130A2_SWS_SHIFT, 1, tpa6130a2_power_event,
179 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
180 };
181
182 static const struct snd_soc_dapm_route tpa6130a2_dapm_routes[] = {
183 { "Left PGA", NULL, "LEFTIN" },
184 { "Right PGA", NULL, "RIGHTIN" },
185
186 { "Left Mute", NULL, "Left PGA" },
187 { "Right Mute", NULL, "Right PGA" },
188
189 { "HPLEFT", NULL, "Left Mute" },
190 { "HPRIGHT", NULL, "Right Mute" },
191
192 { "Left PGA", NULL, "Power" },
193 { "Right PGA", NULL, "Power" },
194 };
195
196 struct snd_soc_component_driver tpa6130a2_component_driver = {
197 .name = "tpa6130a2",
198 .probe = tpa6130a2_component_probe,
199 .dapm_widgets = tpa6130a2_dapm_widgets,
200 .num_dapm_widgets = ARRAY_SIZE(tpa6130a2_dapm_widgets),
201 .dapm_routes = tpa6130a2_dapm_routes,
202 .num_dapm_routes = ARRAY_SIZE(tpa6130a2_dapm_routes),
203 };
204
205 static const struct reg_default tpa6130a2_reg_defaults[] = {
206 { TPA6130A2_REG_CONTROL, TPA6130A2_SWS },
207 { TPA6130A2_REG_VOL_MUTE, TPA6130A2_MUTE_R | TPA6130A2_MUTE_L },
208 };
209
210 static const struct regmap_config tpa6130a2_regmap_config = {
211 .reg_bits = 8,
212 .val_bits = 8,
213 .max_register = TPA6130A2_REG_VERSION,
214 .reg_defaults = tpa6130a2_reg_defaults,
215 .num_reg_defaults = ARRAY_SIZE(tpa6130a2_reg_defaults),
216 .cache_type = REGCACHE_RBTREE,
217 };
218
219 static int tpa6130a2_probe(struct i2c_client *client,
220 const struct i2c_device_id *id)
221 {
222 struct device *dev;
223 struct tpa6130a2_data *data;
224 struct tpa6130a2_platform_data *pdata = client->dev.platform_data;
225 struct device_node *np = client->dev.of_node;
226 const char *regulator;
227 unsigned int version;
228 int ret;
229
230 dev = &client->dev;
231
232 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
233 if (!data)
234 return -ENOMEM;
235
236 data->dev = dev;
237
238 data->regmap = devm_regmap_init_i2c(client, &tpa6130a2_regmap_config);
239 if (IS_ERR(data->regmap))
240 return PTR_ERR(data->regmap);
241
242 if (pdata) {
243 data->power_gpio = pdata->power_gpio;
244 } else if (np) {
245 data->power_gpio = of_get_named_gpio(np, "power-gpio", 0);
246 } else {
247 dev_err(dev, "Platform data not set\n");
248 dump_stack();
249 return -ENODEV;
250 }
251
252 i2c_set_clientdata(client, data);
253
254 data->id = id->driver_data;
255
256 if (data->power_gpio >= 0) {
257 ret = devm_gpio_request(dev, data->power_gpio,
258 "tpa6130a2 enable");
259 if (ret < 0) {
260 dev_err(dev, "Failed to request power GPIO (%d)\n",
261 data->power_gpio);
262 return ret;
263 }
264 gpio_direction_output(data->power_gpio, 0);
265 }
266
267 switch (data->id) {
268 default:
269 dev_warn(dev, "Unknown TPA model (%d). Assuming 6130A2\n",
270 data->id);
271 case TPA6130A2:
272 regulator = "Vdd";
273 break;
274 case TPA6140A2:
275 regulator = "AVdd";
276 break;
277 }
278
279 data->supply = devm_regulator_get(dev, regulator);
280 if (IS_ERR(data->supply)) {
281 ret = PTR_ERR(data->supply);
282 dev_err(dev, "Failed to request supply: %d\n", ret);
283 return ret;
284 }
285
286 ret = tpa6130a2_power(data, true);
287 if (ret != 0)
288 return ret;
289
290
291 /* Read version */
292 regmap_read(data->regmap, TPA6130A2_REG_VERSION, &version);
293 version &= TPA6130A2_VERSION_MASK;
294 if ((version != 1) && (version != 2))
295 dev_warn(dev, "UNTESTED version detected (%d)\n", version);
296
297 /* Disable the chip */
298 ret = tpa6130a2_power(data, false);
299 if (ret != 0)
300 return ret;
301
302 return devm_snd_soc_register_component(&client->dev,
303 &tpa6130a2_component_driver, NULL, 0);
304 }
305
306 static const struct i2c_device_id tpa6130a2_id[] = {
307 { "tpa6130a2", TPA6130A2 },
308 { "tpa6140a2", TPA6140A2 },
309 { }
310 };
311 MODULE_DEVICE_TABLE(i2c, tpa6130a2_id);
312
313 #if IS_ENABLED(CONFIG_OF)
314 static const struct of_device_id tpa6130a2_of_match[] = {
315 { .compatible = "ti,tpa6130a2", },
316 { .compatible = "ti,tpa6140a2" },
317 {},
318 };
319 MODULE_DEVICE_TABLE(of, tpa6130a2_of_match);
320 #endif
321
322 static struct i2c_driver tpa6130a2_i2c_driver = {
323 .driver = {
324 .name = "tpa6130a2",
325 .of_match_table = of_match_ptr(tpa6130a2_of_match),
326 },
327 .probe = tpa6130a2_probe,
328 .id_table = tpa6130a2_id,
329 };
330
331 module_i2c_driver(tpa6130a2_i2c_driver);
332
333 MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
334 MODULE_DESCRIPTION("TPA6130A2 Headphone amplifier driver");
335 MODULE_LICENSE("GPL");