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