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