Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mason/btrfs...
[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 }
d5876ce1 154 } else {
493b67ef
PU
155 /* set SWS */
156 val = tpa6130a2_read(TPA6130A2_REG_CONTROL);
157 val |= TPA6130A2_SWS;
158 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL, val);
7c4e6492 159
493b67ef 160 /* Power off */
7c4e6492 161 if (data->power_gpio >= 0)
493b67ef 162 gpio_set_value(data->power_gpio, 0);
7c4e6492 163
ad8332c1 164 ret = regulator_disable(data->supply);
7c4e6492
IK
165 if (ret != 0) {
166 dev_err(&tpa6130a2_client->dev,
ad8332c1 167 "Failed to disable supply: %d\n", ret);
7c4e6492 168 goto exit;
493b67ef 169 }
7c4e6492
IK
170
171 data->power_state = 0;
493b67ef 172 }
7c4e6492
IK
173
174exit:
493b67ef 175 mutex_unlock(&data->mutex);
7c4e6492 176 return ret;
493b67ef
PU
177}
178
bd843edf 179static int tpa6130a2_get_volsw(struct snd_kcontrol *kcontrol,
493b67ef
PU
180 struct snd_ctl_elem_value *ucontrol)
181{
182 struct soc_mixer_control *mc =
183 (struct soc_mixer_control *)kcontrol->private_value;
184 struct tpa6130a2_data *data;
185 unsigned int reg = mc->reg;
186 unsigned int shift = mc->shift;
bd843edf
PU
187 int max = mc->max;
188 unsigned int mask = (1 << fls(max)) - 1;
493b67ef
PU
189 unsigned int invert = mc->invert;
190
191 BUG_ON(tpa6130a2_client == NULL);
192 data = i2c_get_clientdata(tpa6130a2_client);
193
194 mutex_lock(&data->mutex);
195
196 ucontrol->value.integer.value[0] =
197 (tpa6130a2_read(reg) >> shift) & mask;
198
199 if (invert)
200 ucontrol->value.integer.value[0] =
bd843edf 201 max - ucontrol->value.integer.value[0];
493b67ef
PU
202
203 mutex_unlock(&data->mutex);
204 return 0;
205}
206
bd843edf 207static int tpa6130a2_put_volsw(struct snd_kcontrol *kcontrol,
493b67ef
PU
208 struct snd_ctl_elem_value *ucontrol)
209{
210 struct soc_mixer_control *mc =
211 (struct soc_mixer_control *)kcontrol->private_value;
212 struct tpa6130a2_data *data;
213 unsigned int reg = mc->reg;
214 unsigned int shift = mc->shift;
bd843edf
PU
215 int max = mc->max;
216 unsigned int mask = (1 << fls(max)) - 1;
493b67ef
PU
217 unsigned int invert = mc->invert;
218 unsigned int val = (ucontrol->value.integer.value[0] & mask);
219 unsigned int val_reg;
220
221 BUG_ON(tpa6130a2_client == NULL);
222 data = i2c_get_clientdata(tpa6130a2_client);
223
224 if (invert)
bd843edf 225 val = max - val;
493b67ef
PU
226
227 mutex_lock(&data->mutex);
228
229 val_reg = tpa6130a2_read(reg);
230 if (((val_reg >> shift) & mask) == val) {
231 mutex_unlock(&data->mutex);
232 return 0;
233 }
234
235 val_reg &= ~(mask << shift);
236 val_reg |= val << shift;
237 tpa6130a2_i2c_write(reg, val_reg);
238
239 mutex_unlock(&data->mutex);
240
241 return 1;
242}
243
244/*
245 * TPA6130 volume. From -59.5 to 4 dB with increasing step size when going
246 * down in gain.
247 */
248static const unsigned int tpa6130_tlv[] = {
249 TLV_DB_RANGE_HEAD(10),
250 0, 1, TLV_DB_SCALE_ITEM(-5950, 600, 0),
251 2, 3, TLV_DB_SCALE_ITEM(-5000, 250, 0),
252 4, 5, TLV_DB_SCALE_ITEM(-4550, 160, 0),
253 6, 7, TLV_DB_SCALE_ITEM(-4140, 190, 0),
254 8, 9, TLV_DB_SCALE_ITEM(-3650, 120, 0),
255 10, 11, TLV_DB_SCALE_ITEM(-3330, 160, 0),
256 12, 13, TLV_DB_SCALE_ITEM(-3040, 180, 0),
257 14, 20, TLV_DB_SCALE_ITEM(-2710, 110, 0),
258 21, 37, TLV_DB_SCALE_ITEM(-1960, 74, 0),
259 38, 63, TLV_DB_SCALE_ITEM(-720, 45, 0),
260};
261
262static const struct snd_kcontrol_new tpa6130a2_controls[] = {
263 SOC_SINGLE_EXT_TLV("TPA6130A2 Headphone Playback Volume",
264 TPA6130A2_REG_VOL_MUTE, 0, 0x3f, 0,
bd843edf 265 tpa6130a2_get_volsw, tpa6130a2_put_volsw,
493b67ef
PU
266 tpa6130_tlv),
267};
268
e5e5b31e
PU
269static const unsigned int tpa6140_tlv[] = {
270 TLV_DB_RANGE_HEAD(3),
271 0, 8, TLV_DB_SCALE_ITEM(-5900, 400, 0),
272 9, 16, TLV_DB_SCALE_ITEM(-2500, 200, 0),
273 17, 31, TLV_DB_SCALE_ITEM(-1000, 100, 0),
274};
275
276static const struct snd_kcontrol_new tpa6140a2_controls[] = {
826e962c
PU
277 SOC_SINGLE_EXT_TLV("TPA6140A2 Headphone Playback Volume",
278 TPA6130A2_REG_VOL_MUTE, 1, 0x1f, 0,
bd843edf 279 tpa6130a2_get_volsw, tpa6130a2_put_volsw,
826e962c 280 tpa6140_tlv),
e5e5b31e
PU
281};
282
493b67ef
PU
283/*
284 * Enable or disable channel (left or right)
285 * The bit number for mute and amplifier are the same per channel:
286 * bit 6: Right channel
287 * bit 7: Left channel
288 * in both registers.
289 */
290static void tpa6130a2_channel_enable(u8 channel, int enable)
291{
493b67ef
PU
292 u8 val;
293
493b67ef
PU
294 if (enable) {
295 /* Enable channel */
296 /* Enable amplifier */
297 val = tpa6130a2_read(TPA6130A2_REG_CONTROL);
298 val |= channel;
d534bacd 299 val &= ~TPA6130A2_SWS;
493b67ef
PU
300 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL, val);
301
302 /* Unmute channel */
303 val = tpa6130a2_read(TPA6130A2_REG_VOL_MUTE);
304 val &= ~channel;
305 tpa6130a2_i2c_write(TPA6130A2_REG_VOL_MUTE, val);
306 } else {
307 /* Disable channel */
308 /* Mute channel */
309 val = tpa6130a2_read(TPA6130A2_REG_VOL_MUTE);
310 val |= channel;
311 tpa6130a2_i2c_write(TPA6130A2_REG_VOL_MUTE, val);
312
313 /* Disable amplifier */
314 val = tpa6130a2_read(TPA6130A2_REG_CONTROL);
315 val &= ~channel;
316 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL, val);
317 }
318}
319
39646871 320int tpa6130a2_stereo_enable(struct snd_soc_codec *codec, int enable)
493b67ef 321{
7c4e6492 322 int ret = 0;
39646871 323 if (enable) {
7c4e6492 324 ret = tpa6130a2_power(1);
39646871
PU
325 if (ret < 0)
326 return ret;
327 tpa6130a2_channel_enable(TPA6130A2_HP_EN_R | TPA6130A2_HP_EN_L,
328 1);
329 } else {
330 tpa6130a2_channel_enable(TPA6130A2_HP_EN_R | TPA6130A2_HP_EN_L,
331 0);
7c4e6492 332 ret = tpa6130a2_power(0);
493b67ef 333 }
39646871 334
7c4e6492 335 return ret;
493b67ef 336}
39646871 337EXPORT_SYMBOL_GPL(tpa6130a2_stereo_enable);
493b67ef
PU
338
339int tpa6130a2_add_controls(struct snd_soc_codec *codec)
340{
e5e5b31e
PU
341 struct tpa6130a2_data *data;
342
872a64d7
PU
343 if (tpa6130a2_client == NULL)
344 return -ENODEV;
345
e5e5b31e
PU
346 data = i2c_get_clientdata(tpa6130a2_client);
347
e5e5b31e
PU
348 if (data->id == TPA6140A2)
349 return snd_soc_add_controls(codec, tpa6140a2_controls,
350 ARRAY_SIZE(tpa6140a2_controls));
351 else
352 return snd_soc_add_controls(codec, tpa6130a2_controls,
353 ARRAY_SIZE(tpa6130a2_controls));
493b67ef
PU
354}
355EXPORT_SYMBOL_GPL(tpa6130a2_add_controls);
356
735fe4cf
MB
357static int __devinit tpa6130a2_probe(struct i2c_client *client,
358 const struct i2c_device_id *id)
493b67ef
PU
359{
360 struct device *dev;
361 struct tpa6130a2_data *data;
362 struct tpa6130a2_platform_data *pdata;
ad8332c1
JN
363 const char *regulator;
364 int ret;
493b67ef
PU
365
366 dev = &client->dev;
367
368 if (client->dev.platform_data == NULL) {
369 dev_err(dev, "Platform data not set\n");
370 dump_stack();
371 return -ENODEV;
372 }
373
374 data = kzalloc(sizeof(*data), GFP_KERNEL);
375 if (data == NULL) {
376 dev_err(dev, "Can not allocate memory\n");
377 return -ENOMEM;
378 }
379
380 tpa6130a2_client = client;
381
382 i2c_set_clientdata(tpa6130a2_client, data);
383
ebab1b1d 384 pdata = client->dev.platform_data;
493b67ef 385 data->power_gpio = pdata->power_gpio;
e5e5b31e 386 data->id = pdata->id;
493b67ef
PU
387
388 mutex_init(&data->mutex);
389
390 /* Set default register values */
391 data->regs[TPA6130A2_REG_CONTROL] = TPA6130A2_SWS;
392 data->regs[TPA6130A2_REG_VOL_MUTE] = TPA6130A2_MUTE_R |
393 TPA6130A2_MUTE_L;
394
395 if (data->power_gpio >= 0) {
396 ret = gpio_request(data->power_gpio, "tpa6130a2 enable");
397 if (ret < 0) {
398 dev_err(dev, "Failed to request power GPIO (%d)\n",
399 data->power_gpio);
7c4e6492 400 goto err_gpio;
493b67ef
PU
401 }
402 gpio_direction_output(data->power_gpio, 0);
493b67ef
PU
403 }
404
e5e5b31e 405 switch (data->id) {
ad8332c1
JN
406 default:
407 dev_warn(dev, "Unknown TPA model (%d). Assuming 6130A2\n",
408 pdata->id);
2138301e 409 case TPA6130A2:
ad8332c1 410 regulator = "Vdd";
2138301e
IK
411 break;
412 case TPA6140A2:
ad8332c1 413 regulator = "AVdd";
2138301e 414 break;
2138301e 415 }
7c4e6492 416
ad8332c1
JN
417 data->supply = regulator_get(dev, regulator);
418 if (IS_ERR(data->supply)) {
419 ret = PTR_ERR(data->supply);
420 dev_err(dev, "Failed to request supply: %d\n", ret);
7c4e6492
IK
421 goto err_regulator;
422 }
423
424 ret = tpa6130a2_power(1);
425 if (ret != 0)
426 goto err_power;
427
493b67ef
PU
428
429 /* Read version */
430 ret = tpa6130a2_i2c_read(TPA6130A2_REG_VERSION) &
431 TPA6130A2_VERSION_MASK;
432 if ((ret != 1) && (ret != 2))
433 dev_warn(dev, "UNTESTED version detected (%d)\n", ret);
434
435 /* Disable the chip */
7c4e6492
IK
436 ret = tpa6130a2_power(0);
437 if (ret != 0)
438 goto err_power;
493b67ef
PU
439
440 return 0;
7c4e6492
IK
441
442err_power:
ad8332c1 443 regulator_put(data->supply);
7c4e6492
IK
444err_regulator:
445 if (data->power_gpio >= 0)
446 gpio_free(data->power_gpio);
447err_gpio:
493b67ef
PU
448 kfree(data);
449 i2c_set_clientdata(tpa6130a2_client, NULL);
ebab1b1d 450 tpa6130a2_client = NULL;
493b67ef
PU
451
452 return ret;
453}
454
735fe4cf 455static int __devexit tpa6130a2_remove(struct i2c_client *client)
493b67ef
PU
456{
457 struct tpa6130a2_data *data = i2c_get_clientdata(client);
458
459 tpa6130a2_power(0);
460
461 if (data->power_gpio >= 0)
462 gpio_free(data->power_gpio);
7c4e6492 463
ad8332c1 464 regulator_put(data->supply);
7c4e6492 465
493b67ef 466 kfree(data);
ebab1b1d 467 tpa6130a2_client = NULL;
493b67ef
PU
468
469 return 0;
470}
471
472static const struct i2c_device_id tpa6130a2_id[] = {
473 { "tpa6130a2", 0 },
474 { }
475};
476MODULE_DEVICE_TABLE(i2c, tpa6130a2_id);
477
478static struct i2c_driver tpa6130a2_i2c_driver = {
479 .driver = {
480 .name = "tpa6130a2",
481 .owner = THIS_MODULE,
482 },
483 .probe = tpa6130a2_probe,
484 .remove = __devexit_p(tpa6130a2_remove),
485 .id_table = tpa6130a2_id,
486};
487
488static int __init tpa6130a2_init(void)
489{
490 return i2c_add_driver(&tpa6130a2_i2c_driver);
491}
492
493static void __exit tpa6130a2_exit(void)
494{
495 i2c_del_driver(&tpa6130a2_i2c_driver);
496}
497
498MODULE_AUTHOR("Peter Ujfalusi");
499MODULE_DESCRIPTION("TPA6130A2 Headphone amplifier driver");
500MODULE_LICENSE("GPL");
501
502module_init(tpa6130a2_init);
503module_exit(tpa6130a2_exit);
This page took 0.099255 seconds and 5 git commands to generate.