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");
85 /* Either powered on or off, we save the context */
86 data->regs[reg] = value;
91 static u8 tpa6130a2_read(int reg)
93 struct tpa6130a2_data *data;
95 BUG_ON(tpa6130a2_client == NULL);
96 data = i2c_get_clientdata(tpa6130a2_client);
98 return data->regs[reg];
101 static int tpa6130a2_initialize(void)
103 struct tpa6130a2_data *data;
106 BUG_ON(tpa6130a2_client == NULL);
107 data = i2c_get_clientdata(tpa6130a2_client);
109 for (i = 1; i < TPA6130A2_REG_VERSION; i++) {
110 ret = tpa6130a2_i2c_write(i, data->regs[i]);
118 static int tpa6130a2_power(int power)
120 struct tpa6130a2_data *data;
124 BUG_ON(tpa6130a2_client == NULL);
125 data = i2c_get_clientdata(tpa6130a2_client);
127 mutex_lock(&data->mutex);
130 if (data->power_gpio >= 0)
131 gpio_set_value(data->power_gpio, 1);
133 ret = regulator_enable(data->supply);
135 dev_err(&tpa6130a2_client->dev,
136 "Failed to enable supply: %d\n", ret);
140 data->power_state = 1;
141 ret = tpa6130a2_initialize();
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;
153 val = tpa6130a2_read(TPA6130A2_REG_CONTROL);
154 val &= ~TPA6130A2_SWS;
155 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL, val);
158 val = tpa6130a2_read(TPA6130A2_REG_CONTROL);
159 val |= TPA6130A2_SWS;
160 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL, val);
163 if (data->power_gpio >= 0)
164 gpio_set_value(data->power_gpio, 0);
166 ret = regulator_disable(data->supply);
168 dev_err(&tpa6130a2_client->dev,
169 "Failed to disable supply: %d\n", ret);
173 data->power_state = 0;
177 mutex_unlock(&data->mutex);
181 static int tpa6130a2_get_volsw(struct snd_kcontrol *kcontrol,
182 struct snd_ctl_elem_value *ucontrol)
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;
190 unsigned int mask = (1 << fls(max)) - 1;
191 unsigned int invert = mc->invert;
193 BUG_ON(tpa6130a2_client == NULL);
194 data = i2c_get_clientdata(tpa6130a2_client);
196 mutex_lock(&data->mutex);
198 ucontrol->value.integer.value[0] =
199 (tpa6130a2_read(reg) >> shift) & mask;
202 ucontrol->value.integer.value[0] =
203 max - ucontrol->value.integer.value[0];
205 mutex_unlock(&data->mutex);
209 static int tpa6130a2_put_volsw(struct snd_kcontrol *kcontrol,
210 struct snd_ctl_elem_value *ucontrol)
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;
218 unsigned int mask = (1 << fls(max)) - 1;
219 unsigned int invert = mc->invert;
220 unsigned int val = (ucontrol->value.integer.value[0] & mask);
221 unsigned int val_reg;
223 BUG_ON(tpa6130a2_client == NULL);
224 data = i2c_get_clientdata(tpa6130a2_client);
229 mutex_lock(&data->mutex);
231 val_reg = tpa6130a2_read(reg);
232 if (((val_reg >> shift) & mask) == val) {
233 mutex_unlock(&data->mutex);
237 val_reg &= ~(mask << shift);
238 val_reg |= val << shift;
239 tpa6130a2_i2c_write(reg, val_reg);
241 mutex_unlock(&data->mutex);
247 * TPA6130 volume. From -59.5 to 4 dB with increasing step size when going
250 static 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),
264 static const struct snd_kcontrol_new tpa6130a2_controls[] = {
265 SOC_SINGLE_EXT_TLV("TPA6130A2 Headphone Playback Volume",
266 TPA6130A2_REG_VOL_MUTE, 0, 0x3f, 0,
267 tpa6130a2_get_volsw, tpa6130a2_put_volsw,
271 static 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),
278 static const struct snd_kcontrol_new tpa6140a2_controls[] = {
279 SOC_SINGLE_EXT_TLV("TPA6140A2 Headphone Playback Volume",
280 TPA6130A2_REG_VOL_MUTE, 1, 0x1f, 0,
281 tpa6130a2_get_volsw, tpa6130a2_put_volsw,
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
292 static void tpa6130a2_channel_enable(u8 channel, int enable)
298 /* Enable amplifier */
299 val = tpa6130a2_read(TPA6130A2_REG_CONTROL);
301 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL, val);
304 val = tpa6130a2_read(TPA6130A2_REG_VOL_MUTE);
306 tpa6130a2_i2c_write(TPA6130A2_REG_VOL_MUTE, val);
308 /* Disable channel */
310 val = tpa6130a2_read(TPA6130A2_REG_VOL_MUTE);
312 tpa6130a2_i2c_write(TPA6130A2_REG_VOL_MUTE, val);
314 /* Disable amplifier */
315 val = tpa6130a2_read(TPA6130A2_REG_CONTROL);
317 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL, val);
321 static int tpa6130a2_left_event(struct snd_soc_dapm_widget *w,
322 struct snd_kcontrol *kcontrol, int event)
325 case SND_SOC_DAPM_POST_PMU:
326 tpa6130a2_channel_enable(TPA6130A2_HP_EN_L, 1);
328 case SND_SOC_DAPM_POST_PMD:
329 tpa6130a2_channel_enable(TPA6130A2_HP_EN_L, 0);
335 static int tpa6130a2_right_event(struct snd_soc_dapm_widget *w,
336 struct snd_kcontrol *kcontrol, int event)
339 case SND_SOC_DAPM_POST_PMU:
340 tpa6130a2_channel_enable(TPA6130A2_HP_EN_R, 1);
342 case SND_SOC_DAPM_POST_PMD:
343 tpa6130a2_channel_enable(TPA6130A2_HP_EN_R, 0);
349 static int tpa6130a2_supply_event(struct snd_soc_dapm_widget *w,
350 struct snd_kcontrol *kcontrol, int event)
355 case SND_SOC_DAPM_POST_PMU:
356 ret = tpa6130a2_power(1);
358 case SND_SOC_DAPM_POST_PMD:
359 ret = tpa6130a2_power(0);
365 static 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),
376 SND_SOC_DAPM_OUTPUT("TPA6130A2 Headphone Left"),
377 SND_SOC_DAPM_OUTPUT("TPA6130A2 Headphone Right"),
380 static const struct snd_soc_dapm_route audio_map[] = {
381 {"TPA6130A2 Headphone Left", NULL, "TPA6130A2 Left"},
382 {"TPA6130A2 Headphone Right", NULL, "TPA6130A2 Right"},
384 {"TPA6130A2 Headphone Left", NULL, "TPA6130A2 Enable"},
385 {"TPA6130A2 Headphone Right", NULL, "TPA6130A2 Enable"},
388 int tpa6130a2_add_controls(struct snd_soc_codec *codec)
390 struct tpa6130a2_data *data;
392 if (tpa6130a2_client == NULL)
395 data = i2c_get_clientdata(tpa6130a2_client);
397 snd_soc_dapm_new_controls(codec, tpa6130a2_dapm_widgets,
398 ARRAY_SIZE(tpa6130a2_dapm_widgets));
400 snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
402 if (data->id == TPA6140A2)
403 return snd_soc_add_controls(codec, tpa6140a2_controls,
404 ARRAY_SIZE(tpa6140a2_controls));
406 return snd_soc_add_controls(codec, tpa6130a2_controls,
407 ARRAY_SIZE(tpa6130a2_controls));
410 EXPORT_SYMBOL_GPL(tpa6130a2_add_controls);
412 static int __devinit tpa6130a2_probe(struct i2c_client *client,
413 const struct i2c_device_id *id)
416 struct tpa6130a2_data *data;
417 struct tpa6130a2_platform_data *pdata;
418 const char *regulator;
423 if (client->dev.platform_data == NULL) {
424 dev_err(dev, "Platform data not set\n");
429 data = kzalloc(sizeof(*data), GFP_KERNEL);
431 dev_err(dev, "Can not allocate memory\n");
435 tpa6130a2_client = client;
437 i2c_set_clientdata(tpa6130a2_client, data);
439 pdata = client->dev.platform_data;
440 data->power_gpio = pdata->power_gpio;
441 data->id = pdata->id;
443 mutex_init(&data->mutex);
445 /* Set default register values */
446 data->regs[TPA6130A2_REG_CONTROL] = TPA6130A2_SWS;
447 data->regs[TPA6130A2_REG_VOL_MUTE] = TPA6130A2_MUTE_R |
450 if (data->power_gpio >= 0) {
451 ret = gpio_request(data->power_gpio, "tpa6130a2 enable");
453 dev_err(dev, "Failed to request power GPIO (%d)\n",
457 gpio_direction_output(data->power_gpio, 0);
462 dev_warn(dev, "Unknown TPA model (%d). Assuming 6130A2\n",
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);
479 ret = tpa6130a2_power(1);
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);
490 /* Disable the chip */
491 ret = tpa6130a2_power(0);
498 regulator_put(data->supply);
500 if (data->power_gpio >= 0)
501 gpio_free(data->power_gpio);
504 i2c_set_clientdata(tpa6130a2_client, NULL);
505 tpa6130a2_client = NULL;
510 static int __devexit tpa6130a2_remove(struct i2c_client *client)
512 struct tpa6130a2_data *data = i2c_get_clientdata(client);
516 if (data->power_gpio >= 0)
517 gpio_free(data->power_gpio);
519 regulator_put(data->supply);
522 tpa6130a2_client = NULL;
527 static const struct i2c_device_id tpa6130a2_id[] = {
531 MODULE_DEVICE_TABLE(i2c, tpa6130a2_id);
533 static struct i2c_driver tpa6130a2_i2c_driver = {
536 .owner = THIS_MODULE,
538 .probe = tpa6130a2_probe,
539 .remove = __devexit_p(tpa6130a2_remove),
540 .id_table = tpa6130a2_id,
543 static int __init tpa6130a2_init(void)
545 return i2c_add_driver(&tpa6130a2_i2c_driver);
548 static void __exit tpa6130a2_exit(void)
550 i2c_del_driver(&tpa6130a2_i2c_driver);
553 MODULE_AUTHOR("Peter Ujfalusi");
554 MODULE_DESCRIPTION("TPA6130A2 Headphone amplifier driver");
555 MODULE_LICENSE("GPL");
557 module_init(tpa6130a2_init);
558 module_exit(tpa6130a2_exit);