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