]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blob - sound/soc/codecs/tpa6130a2.c
Merge branch 'docs-next' of git://git.lwn.net/linux-2.6
[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@nokia.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/soc-dapm.h>
33 #include <sound/tlv.h>
34
35 #include "tpa6130a2.h"
36
37 static struct i2c_client *tpa6130a2_client;
38
39 /* This struct is used to save the context */
40 struct tpa6130a2_data {
41 struct mutex mutex;
42 unsigned char regs[TPA6130A2_CACHEREGNUM];
43 struct regulator *supply;
44 int power_gpio;
45 unsigned char power_state;
46 enum tpa_model id;
47 };
48
49 static int tpa6130a2_i2c_read(int reg)
50 {
51 struct tpa6130a2_data *data;
52 int val;
53
54 BUG_ON(tpa6130a2_client == NULL);
55 data = i2c_get_clientdata(tpa6130a2_client);
56
57 /* If powered off, return the cached value */
58 if (data->power_state) {
59 val = i2c_smbus_read_byte_data(tpa6130a2_client, reg);
60 if (val < 0)
61 dev_err(&tpa6130a2_client->dev, "Read failed\n");
62 else
63 data->regs[reg] = val;
64 } else {
65 val = data->regs[reg];
66 }
67
68 return val;
69 }
70
71 static int tpa6130a2_i2c_write(int reg, u8 value)
72 {
73 struct tpa6130a2_data *data;
74 int val = 0;
75
76 BUG_ON(tpa6130a2_client == NULL);
77 data = i2c_get_clientdata(tpa6130a2_client);
78
79 if (data->power_state) {
80 val = i2c_smbus_write_byte_data(tpa6130a2_client, reg, value);
81 if (val < 0)
82 dev_err(&tpa6130a2_client->dev, "Write failed\n");
83 }
84
85 /* Either powered on or off, we save the context */
86 data->regs[reg] = value;
87
88 return val;
89 }
90
91 static u8 tpa6130a2_read(int reg)
92 {
93 struct tpa6130a2_data *data;
94
95 BUG_ON(tpa6130a2_client == NULL);
96 data = i2c_get_clientdata(tpa6130a2_client);
97
98 return data->regs[reg];
99 }
100
101 static void tpa6130a2_initialize(void)
102 {
103 struct tpa6130a2_data *data;
104 int i;
105
106 BUG_ON(tpa6130a2_client == NULL);
107 data = i2c_get_clientdata(tpa6130a2_client);
108
109 for (i = 1; i < TPA6130A2_REG_VERSION; i++)
110 tpa6130a2_i2c_write(i, data->regs[i]);
111 }
112
113 static int tpa6130a2_power(int power)
114 {
115 struct tpa6130a2_data *data;
116 u8 val;
117 int ret;
118
119 BUG_ON(tpa6130a2_client == NULL);
120 data = i2c_get_clientdata(tpa6130a2_client);
121
122 mutex_lock(&data->mutex);
123 if (power) {
124 /* Power on */
125 if (data->power_gpio >= 0)
126 gpio_set_value(data->power_gpio, 1);
127
128 ret = regulator_enable(data->supply);
129 if (ret != 0) {
130 dev_err(&tpa6130a2_client->dev,
131 "Failed to enable supply: %d\n", ret);
132 goto exit;
133 }
134
135 data->power_state = 1;
136 tpa6130a2_initialize();
137
138 /* Clear SWS */
139 val = tpa6130a2_read(TPA6130A2_REG_CONTROL);
140 val &= ~TPA6130A2_SWS;
141 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL, val);
142 } else {
143 /* set SWS */
144 val = tpa6130a2_read(TPA6130A2_REG_CONTROL);
145 val |= TPA6130A2_SWS;
146 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL, val);
147
148 /* Power off */
149 if (data->power_gpio >= 0)
150 gpio_set_value(data->power_gpio, 0);
151
152 ret = regulator_disable(data->supply);
153 if (ret != 0) {
154 dev_err(&tpa6130a2_client->dev,
155 "Failed to disable supply: %d\n", ret);
156 goto exit;
157 }
158
159 data->power_state = 0;
160 }
161
162 exit:
163 mutex_unlock(&data->mutex);
164 return ret;
165 }
166
167 static int tpa6130a2_get_volsw(struct snd_kcontrol *kcontrol,
168 struct snd_ctl_elem_value *ucontrol)
169 {
170 struct soc_mixer_control *mc =
171 (struct soc_mixer_control *)kcontrol->private_value;
172 struct tpa6130a2_data *data;
173 unsigned int reg = mc->reg;
174 unsigned int shift = mc->shift;
175 int max = mc->max;
176 unsigned int mask = (1 << fls(max)) - 1;
177 unsigned int invert = mc->invert;
178
179 BUG_ON(tpa6130a2_client == NULL);
180 data = i2c_get_clientdata(tpa6130a2_client);
181
182 mutex_lock(&data->mutex);
183
184 ucontrol->value.integer.value[0] =
185 (tpa6130a2_read(reg) >> shift) & mask;
186
187 if (invert)
188 ucontrol->value.integer.value[0] =
189 max - ucontrol->value.integer.value[0];
190
191 mutex_unlock(&data->mutex);
192 return 0;
193 }
194
195 static int tpa6130a2_put_volsw(struct snd_kcontrol *kcontrol,
196 struct snd_ctl_elem_value *ucontrol)
197 {
198 struct soc_mixer_control *mc =
199 (struct soc_mixer_control *)kcontrol->private_value;
200 struct tpa6130a2_data *data;
201 unsigned int reg = mc->reg;
202 unsigned int shift = mc->shift;
203 int max = mc->max;
204 unsigned int mask = (1 << fls(max)) - 1;
205 unsigned int invert = mc->invert;
206 unsigned int val = (ucontrol->value.integer.value[0] & mask);
207 unsigned int val_reg;
208
209 BUG_ON(tpa6130a2_client == NULL);
210 data = i2c_get_clientdata(tpa6130a2_client);
211
212 if (invert)
213 val = max - val;
214
215 mutex_lock(&data->mutex);
216
217 val_reg = tpa6130a2_read(reg);
218 if (((val_reg >> shift) & mask) == val) {
219 mutex_unlock(&data->mutex);
220 return 0;
221 }
222
223 val_reg &= ~(mask << shift);
224 val_reg |= val << shift;
225 tpa6130a2_i2c_write(reg, val_reg);
226
227 mutex_unlock(&data->mutex);
228
229 return 1;
230 }
231
232 /*
233 * TPA6130 volume. From -59.5 to 4 dB with increasing step size when going
234 * down in gain.
235 */
236 static const unsigned int tpa6130_tlv[] = {
237 TLV_DB_RANGE_HEAD(10),
238 0, 1, TLV_DB_SCALE_ITEM(-5950, 600, 0),
239 2, 3, TLV_DB_SCALE_ITEM(-5000, 250, 0),
240 4, 5, TLV_DB_SCALE_ITEM(-4550, 160, 0),
241 6, 7, TLV_DB_SCALE_ITEM(-4140, 190, 0),
242 8, 9, TLV_DB_SCALE_ITEM(-3650, 120, 0),
243 10, 11, TLV_DB_SCALE_ITEM(-3330, 160, 0),
244 12, 13, TLV_DB_SCALE_ITEM(-3040, 180, 0),
245 14, 20, TLV_DB_SCALE_ITEM(-2710, 110, 0),
246 21, 37, TLV_DB_SCALE_ITEM(-1960, 74, 0),
247 38, 63, TLV_DB_SCALE_ITEM(-720, 45, 0),
248 };
249
250 static const struct snd_kcontrol_new tpa6130a2_controls[] = {
251 SOC_SINGLE_EXT_TLV("TPA6130A2 Headphone Playback Volume",
252 TPA6130A2_REG_VOL_MUTE, 0, 0x3f, 0,
253 tpa6130a2_get_volsw, tpa6130a2_put_volsw,
254 tpa6130_tlv),
255 };
256
257 static const unsigned int tpa6140_tlv[] = {
258 TLV_DB_RANGE_HEAD(3),
259 0, 8, TLV_DB_SCALE_ITEM(-5900, 400, 0),
260 9, 16, TLV_DB_SCALE_ITEM(-2500, 200, 0),
261 17, 31, TLV_DB_SCALE_ITEM(-1000, 100, 0),
262 };
263
264 static const struct snd_kcontrol_new tpa6140a2_controls[] = {
265 SOC_SINGLE_EXT_TLV("TPA6140A2 Headphone Playback Volume",
266 TPA6130A2_REG_VOL_MUTE, 1, 0x1f, 0,
267 tpa6130a2_get_volsw, tpa6130a2_put_volsw,
268 tpa6140_tlv),
269 };
270
271 /*
272 * Enable or disable channel (left or right)
273 * The bit number for mute and amplifier are the same per channel:
274 * bit 6: Right channel
275 * bit 7: Left channel
276 * in both registers.
277 */
278 static void tpa6130a2_channel_enable(u8 channel, int enable)
279 {
280 u8 val;
281
282 if (enable) {
283 /* Enable channel */
284 /* Enable amplifier */
285 val = tpa6130a2_read(TPA6130A2_REG_CONTROL);
286 val |= channel;
287 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL, val);
288
289 /* Unmute channel */
290 val = tpa6130a2_read(TPA6130A2_REG_VOL_MUTE);
291 val &= ~channel;
292 tpa6130a2_i2c_write(TPA6130A2_REG_VOL_MUTE, val);
293 } else {
294 /* Disable channel */
295 /* Mute channel */
296 val = tpa6130a2_read(TPA6130A2_REG_VOL_MUTE);
297 val |= channel;
298 tpa6130a2_i2c_write(TPA6130A2_REG_VOL_MUTE, val);
299
300 /* Disable amplifier */
301 val = tpa6130a2_read(TPA6130A2_REG_CONTROL);
302 val &= ~channel;
303 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL, val);
304 }
305 }
306
307 static int tpa6130a2_left_event(struct snd_soc_dapm_widget *w,
308 struct snd_kcontrol *kcontrol, int event)
309 {
310 switch (event) {
311 case SND_SOC_DAPM_POST_PMU:
312 tpa6130a2_channel_enable(TPA6130A2_HP_EN_L, 1);
313 break;
314 case SND_SOC_DAPM_POST_PMD:
315 tpa6130a2_channel_enable(TPA6130A2_HP_EN_L, 0);
316 break;
317 }
318 return 0;
319 }
320
321 static int tpa6130a2_right_event(struct snd_soc_dapm_widget *w,
322 struct snd_kcontrol *kcontrol, int event)
323 {
324 switch (event) {
325 case SND_SOC_DAPM_POST_PMU:
326 tpa6130a2_channel_enable(TPA6130A2_HP_EN_R, 1);
327 break;
328 case SND_SOC_DAPM_POST_PMD:
329 tpa6130a2_channel_enable(TPA6130A2_HP_EN_R, 0);
330 break;
331 }
332 return 0;
333 }
334
335 static int tpa6130a2_supply_event(struct snd_soc_dapm_widget *w,
336 struct snd_kcontrol *kcontrol, int event)
337 {
338 int ret = 0;
339
340 switch (event) {
341 case SND_SOC_DAPM_POST_PMU:
342 ret = tpa6130a2_power(1);
343 break;
344 case SND_SOC_DAPM_POST_PMD:
345 ret = tpa6130a2_power(0);
346 break;
347 }
348 return ret;
349 }
350
351 static const struct snd_soc_dapm_widget tpa6130a2_dapm_widgets[] = {
352 SND_SOC_DAPM_PGA_E("TPA6130A2 Left", SND_SOC_NOPM,
353 0, 0, NULL, 0, tpa6130a2_left_event,
354 SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD),
355 SND_SOC_DAPM_PGA_E("TPA6130A2 Right", SND_SOC_NOPM,
356 0, 0, NULL, 0, tpa6130a2_right_event,
357 SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD),
358 SND_SOC_DAPM_SUPPLY("TPA6130A2 Enable", SND_SOC_NOPM,
359 0, 0, tpa6130a2_supply_event,
360 SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD),
361 /* Outputs */
362 SND_SOC_DAPM_OUTPUT("TPA6130A2 Headphone Left"),
363 SND_SOC_DAPM_OUTPUT("TPA6130A2 Headphone Right"),
364 };
365
366 static const struct snd_soc_dapm_route audio_map[] = {
367 {"TPA6130A2 Headphone Left", NULL, "TPA6130A2 Left"},
368 {"TPA6130A2 Headphone Right", NULL, "TPA6130A2 Right"},
369
370 {"TPA6130A2 Headphone Left", NULL, "TPA6130A2 Enable"},
371 {"TPA6130A2 Headphone Right", NULL, "TPA6130A2 Enable"},
372 };
373
374 int tpa6130a2_add_controls(struct snd_soc_codec *codec)
375 {
376 struct tpa6130a2_data *data;
377
378 BUG_ON(tpa6130a2_client == NULL);
379 data = i2c_get_clientdata(tpa6130a2_client);
380
381 snd_soc_dapm_new_controls(codec, tpa6130a2_dapm_widgets,
382 ARRAY_SIZE(tpa6130a2_dapm_widgets));
383
384 snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
385
386 if (data->id == TPA6140A2)
387 return snd_soc_add_controls(codec, tpa6140a2_controls,
388 ARRAY_SIZE(tpa6140a2_controls));
389 else
390 return snd_soc_add_controls(codec, tpa6130a2_controls,
391 ARRAY_SIZE(tpa6130a2_controls));
392
393 }
394 EXPORT_SYMBOL_GPL(tpa6130a2_add_controls);
395
396 static int __devinit tpa6130a2_probe(struct i2c_client *client,
397 const struct i2c_device_id *id)
398 {
399 struct device *dev;
400 struct tpa6130a2_data *data;
401 struct tpa6130a2_platform_data *pdata;
402 const char *regulator;
403 int ret;
404
405 dev = &client->dev;
406
407 if (client->dev.platform_data == NULL) {
408 dev_err(dev, "Platform data not set\n");
409 dump_stack();
410 return -ENODEV;
411 }
412
413 data = kzalloc(sizeof(*data), GFP_KERNEL);
414 if (data == NULL) {
415 dev_err(dev, "Can not allocate memory\n");
416 return -ENOMEM;
417 }
418
419 tpa6130a2_client = client;
420
421 i2c_set_clientdata(tpa6130a2_client, data);
422
423 pdata = client->dev.platform_data;
424 data->power_gpio = pdata->power_gpio;
425 data->id = pdata->id;
426
427 mutex_init(&data->mutex);
428
429 /* Set default register values */
430 data->regs[TPA6130A2_REG_CONTROL] = TPA6130A2_SWS;
431 data->regs[TPA6130A2_REG_VOL_MUTE] = TPA6130A2_MUTE_R |
432 TPA6130A2_MUTE_L;
433
434 if (data->power_gpio >= 0) {
435 ret = gpio_request(data->power_gpio, "tpa6130a2 enable");
436 if (ret < 0) {
437 dev_err(dev, "Failed to request power GPIO (%d)\n",
438 data->power_gpio);
439 goto err_gpio;
440 }
441 gpio_direction_output(data->power_gpio, 0);
442 }
443
444 switch (data->id) {
445 default:
446 dev_warn(dev, "Unknown TPA model (%d). Assuming 6130A2\n",
447 pdata->id);
448 case TPA6130A2:
449 regulator = "Vdd";
450 break;
451 case TPA6140A2:
452 regulator = "AVdd";
453 break;
454 }
455
456 data->supply = regulator_get(dev, regulator);
457 if (IS_ERR(data->supply)) {
458 ret = PTR_ERR(data->supply);
459 dev_err(dev, "Failed to request supply: %d\n", ret);
460 goto err_regulator;
461 }
462
463 ret = tpa6130a2_power(1);
464 if (ret != 0)
465 goto err_power;
466
467
468 /* Read version */
469 ret = tpa6130a2_i2c_read(TPA6130A2_REG_VERSION) &
470 TPA6130A2_VERSION_MASK;
471 if ((ret != 1) && (ret != 2))
472 dev_warn(dev, "UNTESTED version detected (%d)\n", ret);
473
474 /* Disable the chip */
475 ret = tpa6130a2_power(0);
476 if (ret != 0)
477 goto err_power;
478
479 return 0;
480
481 err_power:
482 regulator_put(data->supply);
483 err_regulator:
484 if (data->power_gpio >= 0)
485 gpio_free(data->power_gpio);
486 err_gpio:
487 kfree(data);
488 i2c_set_clientdata(tpa6130a2_client, NULL);
489 tpa6130a2_client = NULL;
490
491 return ret;
492 }
493
494 static int __devexit tpa6130a2_remove(struct i2c_client *client)
495 {
496 struct tpa6130a2_data *data = i2c_get_clientdata(client);
497
498 tpa6130a2_power(0);
499
500 if (data->power_gpio >= 0)
501 gpio_free(data->power_gpio);
502
503 regulator_put(data->supply);
504
505 kfree(data);
506 tpa6130a2_client = NULL;
507
508 return 0;
509 }
510
511 static const struct i2c_device_id tpa6130a2_id[] = {
512 { "tpa6130a2", 0 },
513 { }
514 };
515 MODULE_DEVICE_TABLE(i2c, tpa6130a2_id);
516
517 static struct i2c_driver tpa6130a2_i2c_driver = {
518 .driver = {
519 .name = "tpa6130a2",
520 .owner = THIS_MODULE,
521 },
522 .probe = tpa6130a2_probe,
523 .remove = __devexit_p(tpa6130a2_remove),
524 .id_table = tpa6130a2_id,
525 };
526
527 static int __init tpa6130a2_init(void)
528 {
529 return i2c_add_driver(&tpa6130a2_i2c_driver);
530 }
531
532 static void __exit tpa6130a2_exit(void)
533 {
534 i2c_del_driver(&tpa6130a2_i2c_driver);
535 }
536
537 MODULE_AUTHOR("Peter Ujfalusi");
538 MODULE_DESCRIPTION("TPA6130A2 Headphone amplifier driver");
539 MODULE_LICENSE("GPL");
540
541 module_init(tpa6130a2_init);
542 module_exit(tpa6130a2_exit);