ASoC: Decouple DAPM from CODECs
[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);
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;
75e3f313 122 int ret = 0;
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 390 struct tpa6130a2_data *data;
ce6120cc 391 struct snd_soc_dapm_context *dapm = &codec->dapm;
e5e5b31e 392
872a64d7
PU
393 if (tpa6130a2_client == NULL)
394 return -ENODEV;
395
e5e5b31e
PU
396 data = i2c_get_clientdata(tpa6130a2_client);
397
ce6120cc 398 snd_soc_dapm_new_controls(dapm, tpa6130a2_dapm_widgets,
493b67ef
PU
399 ARRAY_SIZE(tpa6130a2_dapm_widgets));
400
ce6120cc 401 snd_soc_dapm_add_routes(dapm, audio_map, ARRAY_SIZE(audio_map));
493b67ef 402
e5e5b31e
PU
403 if (data->id == TPA6140A2)
404 return snd_soc_add_controls(codec, tpa6140a2_controls,
405 ARRAY_SIZE(tpa6140a2_controls));
406 else
407 return snd_soc_add_controls(codec, tpa6130a2_controls,
408 ARRAY_SIZE(tpa6130a2_controls));
493b67ef
PU
409
410}
411EXPORT_SYMBOL_GPL(tpa6130a2_add_controls);
412
735fe4cf
MB
413static int __devinit tpa6130a2_probe(struct i2c_client *client,
414 const struct i2c_device_id *id)
493b67ef
PU
415{
416 struct device *dev;
417 struct tpa6130a2_data *data;
418 struct tpa6130a2_platform_data *pdata;
ad8332c1
JN
419 const char *regulator;
420 int ret;
493b67ef
PU
421
422 dev = &client->dev;
423
424 if (client->dev.platform_data == NULL) {
425 dev_err(dev, "Platform data not set\n");
426 dump_stack();
427 return -ENODEV;
428 }
429
430 data = kzalloc(sizeof(*data), GFP_KERNEL);
431 if (data == NULL) {
432 dev_err(dev, "Can not allocate memory\n");
433 return -ENOMEM;
434 }
435
436 tpa6130a2_client = client;
437
438 i2c_set_clientdata(tpa6130a2_client, data);
439
ebab1b1d 440 pdata = client->dev.platform_data;
493b67ef 441 data->power_gpio = pdata->power_gpio;
e5e5b31e 442 data->id = pdata->id;
493b67ef
PU
443
444 mutex_init(&data->mutex);
445
446 /* Set default register values */
447 data->regs[TPA6130A2_REG_CONTROL] = TPA6130A2_SWS;
448 data->regs[TPA6130A2_REG_VOL_MUTE] = TPA6130A2_MUTE_R |
449 TPA6130A2_MUTE_L;
450
451 if (data->power_gpio >= 0) {
452 ret = gpio_request(data->power_gpio, "tpa6130a2 enable");
453 if (ret < 0) {
454 dev_err(dev, "Failed to request power GPIO (%d)\n",
455 data->power_gpio);
7c4e6492 456 goto err_gpio;
493b67ef
PU
457 }
458 gpio_direction_output(data->power_gpio, 0);
493b67ef
PU
459 }
460
e5e5b31e 461 switch (data->id) {
ad8332c1
JN
462 default:
463 dev_warn(dev, "Unknown TPA model (%d). Assuming 6130A2\n",
464 pdata->id);
2138301e 465 case TPA6130A2:
ad8332c1 466 regulator = "Vdd";
2138301e
IK
467 break;
468 case TPA6140A2:
ad8332c1 469 regulator = "AVdd";
2138301e 470 break;
2138301e 471 }
7c4e6492 472
ad8332c1
JN
473 data->supply = regulator_get(dev, regulator);
474 if (IS_ERR(data->supply)) {
475 ret = PTR_ERR(data->supply);
476 dev_err(dev, "Failed to request supply: %d\n", ret);
7c4e6492
IK
477 goto err_regulator;
478 }
479
480 ret = tpa6130a2_power(1);
481 if (ret != 0)
482 goto err_power;
483
493b67ef
PU
484
485 /* Read version */
486 ret = tpa6130a2_i2c_read(TPA6130A2_REG_VERSION) &
487 TPA6130A2_VERSION_MASK;
488 if ((ret != 1) && (ret != 2))
489 dev_warn(dev, "UNTESTED version detected (%d)\n", ret);
490
491 /* Disable the chip */
7c4e6492
IK
492 ret = tpa6130a2_power(0);
493 if (ret != 0)
494 goto err_power;
493b67ef
PU
495
496 return 0;
7c4e6492
IK
497
498err_power:
ad8332c1 499 regulator_put(data->supply);
7c4e6492
IK
500err_regulator:
501 if (data->power_gpio >= 0)
502 gpio_free(data->power_gpio);
503err_gpio:
493b67ef
PU
504 kfree(data);
505 i2c_set_clientdata(tpa6130a2_client, NULL);
ebab1b1d 506 tpa6130a2_client = NULL;
493b67ef
PU
507
508 return ret;
509}
510
735fe4cf 511static int __devexit tpa6130a2_remove(struct i2c_client *client)
493b67ef
PU
512{
513 struct tpa6130a2_data *data = i2c_get_clientdata(client);
514
515 tpa6130a2_power(0);
516
517 if (data->power_gpio >= 0)
518 gpio_free(data->power_gpio);
7c4e6492 519
ad8332c1 520 regulator_put(data->supply);
7c4e6492 521
493b67ef 522 kfree(data);
ebab1b1d 523 tpa6130a2_client = NULL;
493b67ef
PU
524
525 return 0;
526}
527
528static const struct i2c_device_id tpa6130a2_id[] = {
529 { "tpa6130a2", 0 },
530 { }
531};
532MODULE_DEVICE_TABLE(i2c, tpa6130a2_id);
533
534static struct i2c_driver tpa6130a2_i2c_driver = {
535 .driver = {
536 .name = "tpa6130a2",
537 .owner = THIS_MODULE,
538 },
539 .probe = tpa6130a2_probe,
540 .remove = __devexit_p(tpa6130a2_remove),
541 .id_table = tpa6130a2_id,
542};
543
544static int __init tpa6130a2_init(void)
545{
546 return i2c_add_driver(&tpa6130a2_i2c_driver);
547}
548
549static void __exit tpa6130a2_exit(void)
550{
551 i2c_del_driver(&tpa6130a2_i2c_driver);
552}
553
554MODULE_AUTHOR("Peter Ujfalusi");
555MODULE_DESCRIPTION("TPA6130A2 Headphone amplifier driver");
556MODULE_LICENSE("GPL");
557
558module_init(tpa6130a2_init);
559module_exit(tpa6130a2_exit);
This page took 0.096028 seconds and 5 git commands to generate.