2 * ALSA SoC Texas Instruments TPA6130A2 headset stereo amplifier driver
4 * Copyright (C) Nokia Corporation
6 * Author: Peter Ujfalusi <peter.ujfalusi@nokia.com>
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.
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.
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
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>
28 #include <linux/regulator/consumer.h>
29 #include <linux/slab.h>
30 #include <sound/tpa6130a2-plat.h>
31 #include <sound/soc.h>
32 #include <sound/soc-dapm.h>
33 #include <sound/tlv.h>
35 #include "tpa6130a2.h"
37 static struct i2c_client *tpa6130a2_client;
39 /* This struct is used to save the context */
40 struct tpa6130a2_data {
42 unsigned char regs[TPA6130A2_CACHEREGNUM];
43 struct regulator *supply;
45 unsigned char power_state;
49 static int tpa6130a2_i2c_read(int reg)
51 struct tpa6130a2_data *data;
54 BUG_ON(tpa6130a2_client == NULL);
55 data = i2c_get_clientdata(tpa6130a2_client);
57 /* If powered off, return the cached value */
58 if (data->power_state) {
59 val = i2c_smbus_read_byte_data(tpa6130a2_client, reg);
61 dev_err(&tpa6130a2_client->dev, "Read failed\n");
63 data->regs[reg] = val;
65 val = data->regs[reg];
71 static int tpa6130a2_i2c_write(int reg, u8 value)
73 struct tpa6130a2_data *data;
76 BUG_ON(tpa6130a2_client == NULL);
77 data = i2c_get_clientdata(tpa6130a2_client);
79 if (data->power_state) {
80 val = i2c_smbus_write_byte_data(tpa6130a2_client, reg, value);
82 dev_err(&tpa6130a2_client->dev, "Write failed\n");
87 /* Either powered on or off, we save the context */
88 data->regs[reg] = value;
93 static u8 tpa6130a2_read(int reg)
95 struct tpa6130a2_data *data;
97 BUG_ON(tpa6130a2_client == NULL);
98 data = i2c_get_clientdata(tpa6130a2_client);
100 return data->regs[reg];
103 static int tpa6130a2_initialize(void)
105 struct tpa6130a2_data *data;
108 BUG_ON(tpa6130a2_client == NULL);
109 data = i2c_get_clientdata(tpa6130a2_client);
111 for (i = 1; i < TPA6130A2_REG_VERSION; i++) {
112 ret = tpa6130a2_i2c_write(i, data->regs[i]);
120 static int tpa6130a2_power(int power)
122 struct tpa6130a2_data *data;
126 BUG_ON(tpa6130a2_client == NULL);
127 data = i2c_get_clientdata(tpa6130a2_client);
129 mutex_lock(&data->mutex);
130 if (power && !data->power_state) {
132 if (data->power_gpio >= 0)
133 gpio_set_value(data->power_gpio, 1);
135 ret = regulator_enable(data->supply);
137 dev_err(&tpa6130a2_client->dev,
138 "Failed to enable supply: %d\n", ret);
142 data->power_state = 1;
143 ret = tpa6130a2_initialize();
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;
155 val = tpa6130a2_read(TPA6130A2_REG_CONTROL);
156 val &= ~TPA6130A2_SWS;
157 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL, val);
158 } else if (!power && data->power_state) {
160 val = tpa6130a2_read(TPA6130A2_REG_CONTROL);
161 val |= TPA6130A2_SWS;
162 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL, val);
165 if (data->power_gpio >= 0)
166 gpio_set_value(data->power_gpio, 0);
168 ret = regulator_disable(data->supply);
170 dev_err(&tpa6130a2_client->dev,
171 "Failed to disable supply: %d\n", ret);
175 data->power_state = 0;
179 mutex_unlock(&data->mutex);
183 static int tpa6130a2_get_volsw(struct snd_kcontrol *kcontrol,
184 struct snd_ctl_elem_value *ucontrol)
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;
192 unsigned int mask = (1 << fls(max)) - 1;
193 unsigned int invert = mc->invert;
195 BUG_ON(tpa6130a2_client == NULL);
196 data = i2c_get_clientdata(tpa6130a2_client);
198 mutex_lock(&data->mutex);
200 ucontrol->value.integer.value[0] =
201 (tpa6130a2_read(reg) >> shift) & mask;
204 ucontrol->value.integer.value[0] =
205 max - ucontrol->value.integer.value[0];
207 mutex_unlock(&data->mutex);
211 static int tpa6130a2_put_volsw(struct snd_kcontrol *kcontrol,
212 struct snd_ctl_elem_value *ucontrol)
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;
220 unsigned int mask = (1 << fls(max)) - 1;
221 unsigned int invert = mc->invert;
222 unsigned int val = (ucontrol->value.integer.value[0] & mask);
223 unsigned int val_reg;
225 BUG_ON(tpa6130a2_client == NULL);
226 data = i2c_get_clientdata(tpa6130a2_client);
231 mutex_lock(&data->mutex);
233 val_reg = tpa6130a2_read(reg);
234 if (((val_reg >> shift) & mask) == val) {
235 mutex_unlock(&data->mutex);
239 val_reg &= ~(mask << shift);
240 val_reg |= val << shift;
241 tpa6130a2_i2c_write(reg, val_reg);
243 mutex_unlock(&data->mutex);
249 * TPA6130 volume. From -59.5 to 4 dB with increasing step size when going
252 static 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),
266 static const struct snd_kcontrol_new tpa6130a2_controls[] = {
267 SOC_SINGLE_EXT_TLV("TPA6130A2 Headphone Playback Volume",
268 TPA6130A2_REG_VOL_MUTE, 0, 0x3f, 0,
269 tpa6130a2_get_volsw, tpa6130a2_put_volsw,
273 static 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),
280 static const struct snd_kcontrol_new tpa6140a2_controls[] = {
281 SOC_SINGLE_EXT_TLV("TPA6140A2 Headphone Playback Volume",
282 TPA6130A2_REG_VOL_MUTE, 1, 0x1f, 0,
283 tpa6130a2_get_volsw, tpa6130a2_put_volsw,
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
294 static void tpa6130a2_channel_enable(u8 channel, int enable)
300 /* Enable amplifier */
301 val = tpa6130a2_read(TPA6130A2_REG_CONTROL);
303 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL, val);
306 val = tpa6130a2_read(TPA6130A2_REG_VOL_MUTE);
308 tpa6130a2_i2c_write(TPA6130A2_REG_VOL_MUTE, val);
310 /* Disable channel */
312 val = tpa6130a2_read(TPA6130A2_REG_VOL_MUTE);
314 tpa6130a2_i2c_write(TPA6130A2_REG_VOL_MUTE, val);
316 /* Disable amplifier */
317 val = tpa6130a2_read(TPA6130A2_REG_CONTROL);
319 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL, val);
323 static int tpa6130a2_left_event(struct snd_soc_dapm_widget *w,
324 struct snd_kcontrol *kcontrol, int event)
327 case SND_SOC_DAPM_POST_PMU:
328 tpa6130a2_channel_enable(TPA6130A2_HP_EN_L, 1);
330 case SND_SOC_DAPM_POST_PMD:
331 tpa6130a2_channel_enable(TPA6130A2_HP_EN_L, 0);
337 static int tpa6130a2_right_event(struct snd_soc_dapm_widget *w,
338 struct snd_kcontrol *kcontrol, int event)
341 case SND_SOC_DAPM_POST_PMU:
342 tpa6130a2_channel_enable(TPA6130A2_HP_EN_R, 1);
344 case SND_SOC_DAPM_POST_PMD:
345 tpa6130a2_channel_enable(TPA6130A2_HP_EN_R, 0);
351 static int tpa6130a2_supply_event(struct snd_soc_dapm_widget *w,
352 struct snd_kcontrol *kcontrol, int event)
357 case SND_SOC_DAPM_POST_PMU:
358 ret = tpa6130a2_power(1);
360 case SND_SOC_DAPM_POST_PMD:
361 ret = tpa6130a2_power(0);
367 static 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),
378 SND_SOC_DAPM_OUTPUT("TPA6130A2 Headphone Left"),
379 SND_SOC_DAPM_OUTPUT("TPA6130A2 Headphone Right"),
382 static const struct snd_soc_dapm_route audio_map[] = {
383 {"TPA6130A2 Headphone Left", NULL, "TPA6130A2 Left"},
384 {"TPA6130A2 Headphone Right", NULL, "TPA6130A2 Right"},
386 {"TPA6130A2 Headphone Left", NULL, "TPA6130A2 Enable"},
387 {"TPA6130A2 Headphone Right", NULL, "TPA6130A2 Enable"},
390 int tpa6130a2_add_controls(struct snd_soc_codec *codec)
392 struct tpa6130a2_data *data;
394 if (tpa6130a2_client == NULL)
397 data = i2c_get_clientdata(tpa6130a2_client);
399 snd_soc_dapm_new_controls(codec, tpa6130a2_dapm_widgets,
400 ARRAY_SIZE(tpa6130a2_dapm_widgets));
402 snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
404 if (data->id == TPA6140A2)
405 return snd_soc_add_controls(codec, tpa6140a2_controls,
406 ARRAY_SIZE(tpa6140a2_controls));
408 return snd_soc_add_controls(codec, tpa6130a2_controls,
409 ARRAY_SIZE(tpa6130a2_controls));
412 EXPORT_SYMBOL_GPL(tpa6130a2_add_controls);
414 static int __devinit tpa6130a2_probe(struct i2c_client *client,
415 const struct i2c_device_id *id)
418 struct tpa6130a2_data *data;
419 struct tpa6130a2_platform_data *pdata;
420 const char *regulator;
425 if (client->dev.platform_data == NULL) {
426 dev_err(dev, "Platform data not set\n");
431 data = kzalloc(sizeof(*data), GFP_KERNEL);
433 dev_err(dev, "Can not allocate memory\n");
437 tpa6130a2_client = client;
439 i2c_set_clientdata(tpa6130a2_client, data);
441 pdata = client->dev.platform_data;
442 data->power_gpio = pdata->power_gpio;
443 data->id = pdata->id;
445 mutex_init(&data->mutex);
447 /* Set default register values */
448 data->regs[TPA6130A2_REG_CONTROL] = TPA6130A2_SWS;
449 data->regs[TPA6130A2_REG_VOL_MUTE] = TPA6130A2_MUTE_R |
452 if (data->power_gpio >= 0) {
453 ret = gpio_request(data->power_gpio, "tpa6130a2 enable");
455 dev_err(dev, "Failed to request power GPIO (%d)\n",
459 gpio_direction_output(data->power_gpio, 0);
464 dev_warn(dev, "Unknown TPA model (%d). Assuming 6130A2\n",
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);
481 ret = tpa6130a2_power(1);
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);
492 /* Disable the chip */
493 ret = tpa6130a2_power(0);
500 regulator_put(data->supply);
502 if (data->power_gpio >= 0)
503 gpio_free(data->power_gpio);
506 i2c_set_clientdata(tpa6130a2_client, NULL);
507 tpa6130a2_client = NULL;
512 static int __devexit tpa6130a2_remove(struct i2c_client *client)
514 struct tpa6130a2_data *data = i2c_get_clientdata(client);
518 if (data->power_gpio >= 0)
519 gpio_free(data->power_gpio);
521 regulator_put(data->supply);
524 tpa6130a2_client = NULL;
529 static const struct i2c_device_id tpa6130a2_id[] = {
533 MODULE_DEVICE_TABLE(i2c, tpa6130a2_id);
535 static struct i2c_driver tpa6130a2_i2c_driver = {
538 .owner = THIS_MODULE,
540 .probe = tpa6130a2_probe,
541 .remove = __devexit_p(tpa6130a2_remove),
542 .id_table = tpa6130a2_id,
545 static int __init tpa6130a2_init(void)
547 return i2c_add_driver(&tpa6130a2_i2c_driver);
550 static void __exit tpa6130a2_exit(void)
552 i2c_del_driver(&tpa6130a2_i2c_driver);
555 MODULE_AUTHOR("Peter Ujfalusi");
556 MODULE_DESCRIPTION("TPA6130A2 Headphone amplifier driver");
557 MODULE_LICENSE("GPL");
559 module_init(tpa6130a2_init);
560 module_exit(tpa6130a2_exit);