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 <sound/tpa6130a2-plat.h>
30 #include <sound/soc.h>
31 #include <sound/soc-dapm.h>
32 #include <sound/tlv.h>
34 #include "tpa6130a2.h"
36 static struct i2c_client *tpa6130a2_client;
38 /* This struct is used to save the context */
39 struct tpa6130a2_data {
41 unsigned char regs[TPA6130A2_CACHEREGNUM];
42 struct regulator *supply;
44 unsigned char power_state;
48 static int tpa6130a2_i2c_read(int reg)
50 struct tpa6130a2_data *data;
53 BUG_ON(tpa6130a2_client == NULL);
54 data = i2c_get_clientdata(tpa6130a2_client);
56 /* If powered off, return the cached value */
57 if (data->power_state) {
58 val = i2c_smbus_read_byte_data(tpa6130a2_client, reg);
60 dev_err(&tpa6130a2_client->dev, "Read failed\n");
62 data->regs[reg] = val;
64 val = data->regs[reg];
70 static int tpa6130a2_i2c_write(int reg, u8 value)
72 struct tpa6130a2_data *data;
75 BUG_ON(tpa6130a2_client == NULL);
76 data = i2c_get_clientdata(tpa6130a2_client);
78 if (data->power_state) {
79 val = i2c_smbus_write_byte_data(tpa6130a2_client, reg, value);
81 dev_err(&tpa6130a2_client->dev, "Write failed\n");
84 /* Either powered on or off, we save the context */
85 data->regs[reg] = value;
90 static u8 tpa6130a2_read(int reg)
92 struct tpa6130a2_data *data;
94 BUG_ON(tpa6130a2_client == NULL);
95 data = i2c_get_clientdata(tpa6130a2_client);
97 return data->regs[reg];
100 static void tpa6130a2_initialize(void)
102 struct tpa6130a2_data *data;
105 BUG_ON(tpa6130a2_client == NULL);
106 data = i2c_get_clientdata(tpa6130a2_client);
108 for (i = 1; i < TPA6130A2_REG_VERSION; i++)
109 tpa6130a2_i2c_write(i, data->regs[i]);
112 static int tpa6130a2_power(int power)
114 struct tpa6130a2_data *data;
118 BUG_ON(tpa6130a2_client == NULL);
119 data = i2c_get_clientdata(tpa6130a2_client);
121 mutex_lock(&data->mutex);
124 if (data->power_gpio >= 0)
125 gpio_set_value(data->power_gpio, 1);
127 ret = regulator_enable(data->supply);
129 dev_err(&tpa6130a2_client->dev,
130 "Failed to enable supply: %d\n", ret);
134 data->power_state = 1;
135 tpa6130a2_initialize();
138 val = tpa6130a2_read(TPA6130A2_REG_CONTROL);
139 val &= ~TPA6130A2_SWS;
140 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL, val);
143 val = tpa6130a2_read(TPA6130A2_REG_CONTROL);
144 val |= TPA6130A2_SWS;
145 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL, val);
148 if (data->power_gpio >= 0)
149 gpio_set_value(data->power_gpio, 0);
151 ret = regulator_disable(data->supply);
153 dev_err(&tpa6130a2_client->dev,
154 "Failed to disable supply: %d\n", ret);
158 data->power_state = 0;
162 mutex_unlock(&data->mutex);
166 static int tpa6130a2_get_volsw(struct snd_kcontrol *kcontrol,
167 struct snd_ctl_elem_value *ucontrol)
169 struct soc_mixer_control *mc =
170 (struct soc_mixer_control *)kcontrol->private_value;
171 struct tpa6130a2_data *data;
172 unsigned int reg = mc->reg;
173 unsigned int shift = mc->shift;
175 unsigned int mask = (1 << fls(max)) - 1;
176 unsigned int invert = mc->invert;
178 BUG_ON(tpa6130a2_client == NULL);
179 data = i2c_get_clientdata(tpa6130a2_client);
181 mutex_lock(&data->mutex);
183 ucontrol->value.integer.value[0] =
184 (tpa6130a2_read(reg) >> shift) & mask;
187 ucontrol->value.integer.value[0] =
188 max - ucontrol->value.integer.value[0];
190 mutex_unlock(&data->mutex);
194 static int tpa6130a2_put_volsw(struct snd_kcontrol *kcontrol,
195 struct snd_ctl_elem_value *ucontrol)
197 struct soc_mixer_control *mc =
198 (struct soc_mixer_control *)kcontrol->private_value;
199 struct tpa6130a2_data *data;
200 unsigned int reg = mc->reg;
201 unsigned int shift = mc->shift;
203 unsigned int mask = (1 << fls(max)) - 1;
204 unsigned int invert = mc->invert;
205 unsigned int val = (ucontrol->value.integer.value[0] & mask);
206 unsigned int val_reg;
208 BUG_ON(tpa6130a2_client == NULL);
209 data = i2c_get_clientdata(tpa6130a2_client);
214 mutex_lock(&data->mutex);
216 val_reg = tpa6130a2_read(reg);
217 if (((val_reg >> shift) & mask) == val) {
218 mutex_unlock(&data->mutex);
222 val_reg &= ~(mask << shift);
223 val_reg |= val << shift;
224 tpa6130a2_i2c_write(reg, val_reg);
226 mutex_unlock(&data->mutex);
232 * TPA6130 volume. From -59.5 to 4 dB with increasing step size when going
235 static const unsigned int tpa6130_tlv[] = {
236 TLV_DB_RANGE_HEAD(10),
237 0, 1, TLV_DB_SCALE_ITEM(-5950, 600, 0),
238 2, 3, TLV_DB_SCALE_ITEM(-5000, 250, 0),
239 4, 5, TLV_DB_SCALE_ITEM(-4550, 160, 0),
240 6, 7, TLV_DB_SCALE_ITEM(-4140, 190, 0),
241 8, 9, TLV_DB_SCALE_ITEM(-3650, 120, 0),
242 10, 11, TLV_DB_SCALE_ITEM(-3330, 160, 0),
243 12, 13, TLV_DB_SCALE_ITEM(-3040, 180, 0),
244 14, 20, TLV_DB_SCALE_ITEM(-2710, 110, 0),
245 21, 37, TLV_DB_SCALE_ITEM(-1960, 74, 0),
246 38, 63, TLV_DB_SCALE_ITEM(-720, 45, 0),
249 static const struct snd_kcontrol_new tpa6130a2_controls[] = {
250 SOC_SINGLE_EXT_TLV("TPA6130A2 Headphone Playback Volume",
251 TPA6130A2_REG_VOL_MUTE, 0, 0x3f, 0,
252 tpa6130a2_get_volsw, tpa6130a2_put_volsw,
256 static const unsigned int tpa6140_tlv[] = {
257 TLV_DB_RANGE_HEAD(3),
258 0, 8, TLV_DB_SCALE_ITEM(-5900, 400, 0),
259 9, 16, TLV_DB_SCALE_ITEM(-2500, 200, 0),
260 17, 31, TLV_DB_SCALE_ITEM(-1000, 100, 0),
263 static const struct snd_kcontrol_new tpa6140a2_controls[] = {
264 SOC_SINGLE_EXT_TLV("TPA6140A2 Headphone Playback Volume",
265 TPA6130A2_REG_VOL_MUTE, 1, 0x1f, 0,
266 tpa6130a2_get_volsw, tpa6130a2_put_volsw,
271 * Enable or disable channel (left or right)
272 * The bit number for mute and amplifier are the same per channel:
273 * bit 6: Right channel
274 * bit 7: Left channel
277 static void tpa6130a2_channel_enable(u8 channel, int enable)
283 /* Enable amplifier */
284 val = tpa6130a2_read(TPA6130A2_REG_CONTROL);
286 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL, val);
289 val = tpa6130a2_read(TPA6130A2_REG_VOL_MUTE);
291 tpa6130a2_i2c_write(TPA6130A2_REG_VOL_MUTE, val);
293 /* Disable channel */
295 val = tpa6130a2_read(TPA6130A2_REG_VOL_MUTE);
297 tpa6130a2_i2c_write(TPA6130A2_REG_VOL_MUTE, val);
299 /* Disable amplifier */
300 val = tpa6130a2_read(TPA6130A2_REG_CONTROL);
302 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL, val);
306 static int tpa6130a2_left_event(struct snd_soc_dapm_widget *w,
307 struct snd_kcontrol *kcontrol, int event)
310 case SND_SOC_DAPM_POST_PMU:
311 tpa6130a2_channel_enable(TPA6130A2_HP_EN_L, 1);
313 case SND_SOC_DAPM_POST_PMD:
314 tpa6130a2_channel_enable(TPA6130A2_HP_EN_L, 0);
320 static int tpa6130a2_right_event(struct snd_soc_dapm_widget *w,
321 struct snd_kcontrol *kcontrol, int event)
324 case SND_SOC_DAPM_POST_PMU:
325 tpa6130a2_channel_enable(TPA6130A2_HP_EN_R, 1);
327 case SND_SOC_DAPM_POST_PMD:
328 tpa6130a2_channel_enable(TPA6130A2_HP_EN_R, 0);
334 static int tpa6130a2_supply_event(struct snd_soc_dapm_widget *w,
335 struct snd_kcontrol *kcontrol, int event)
340 case SND_SOC_DAPM_POST_PMU:
341 ret = tpa6130a2_power(1);
343 case SND_SOC_DAPM_POST_PMD:
344 ret = tpa6130a2_power(0);
350 static const struct snd_soc_dapm_widget tpa6130a2_dapm_widgets[] = {
351 SND_SOC_DAPM_PGA_E("TPA6130A2 Left", SND_SOC_NOPM,
352 0, 0, NULL, 0, tpa6130a2_left_event,
353 SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD),
354 SND_SOC_DAPM_PGA_E("TPA6130A2 Right", SND_SOC_NOPM,
355 0, 0, NULL, 0, tpa6130a2_right_event,
356 SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD),
357 SND_SOC_DAPM_SUPPLY("TPA6130A2 Enable", SND_SOC_NOPM,
358 0, 0, tpa6130a2_supply_event,
359 SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD),
361 SND_SOC_DAPM_OUTPUT("TPA6130A2 Headphone Left"),
362 SND_SOC_DAPM_OUTPUT("TPA6130A2 Headphone Right"),
365 static const struct snd_soc_dapm_route audio_map[] = {
366 {"TPA6130A2 Headphone Left", NULL, "TPA6130A2 Left"},
367 {"TPA6130A2 Headphone Right", NULL, "TPA6130A2 Right"},
369 {"TPA6130A2 Headphone Left", NULL, "TPA6130A2 Enable"},
370 {"TPA6130A2 Headphone Right", NULL, "TPA6130A2 Enable"},
373 int tpa6130a2_add_controls(struct snd_soc_codec *codec)
375 struct tpa6130a2_data *data;
377 BUG_ON(tpa6130a2_client == NULL);
378 data = i2c_get_clientdata(tpa6130a2_client);
380 snd_soc_dapm_new_controls(codec, tpa6130a2_dapm_widgets,
381 ARRAY_SIZE(tpa6130a2_dapm_widgets));
383 snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
385 if (data->id == TPA6140A2)
386 return snd_soc_add_controls(codec, tpa6140a2_controls,
387 ARRAY_SIZE(tpa6140a2_controls));
389 return snd_soc_add_controls(codec, tpa6130a2_controls,
390 ARRAY_SIZE(tpa6130a2_controls));
393 EXPORT_SYMBOL_GPL(tpa6130a2_add_controls);
395 static int __devinit tpa6130a2_probe(struct i2c_client *client,
396 const struct i2c_device_id *id)
399 struct tpa6130a2_data *data;
400 struct tpa6130a2_platform_data *pdata;
401 const char *regulator;
406 if (client->dev.platform_data == NULL) {
407 dev_err(dev, "Platform data not set\n");
412 data = kzalloc(sizeof(*data), GFP_KERNEL);
414 dev_err(dev, "Can not allocate memory\n");
418 tpa6130a2_client = client;
420 i2c_set_clientdata(tpa6130a2_client, data);
422 pdata = client->dev.platform_data;
423 data->power_gpio = pdata->power_gpio;
424 data->id = pdata->id;
426 mutex_init(&data->mutex);
428 /* Set default register values */
429 data->regs[TPA6130A2_REG_CONTROL] = TPA6130A2_SWS;
430 data->regs[TPA6130A2_REG_VOL_MUTE] = TPA6130A2_MUTE_R |
433 if (data->power_gpio >= 0) {
434 ret = gpio_request(data->power_gpio, "tpa6130a2 enable");
436 dev_err(dev, "Failed to request power GPIO (%d)\n",
440 gpio_direction_output(data->power_gpio, 0);
445 dev_warn(dev, "Unknown TPA model (%d). Assuming 6130A2\n",
455 data->supply = regulator_get(dev, regulator);
456 if (IS_ERR(data->supply)) {
457 ret = PTR_ERR(data->supply);
458 dev_err(dev, "Failed to request supply: %d\n", ret);
462 ret = tpa6130a2_power(1);
468 ret = tpa6130a2_i2c_read(TPA6130A2_REG_VERSION) &
469 TPA6130A2_VERSION_MASK;
470 if ((ret != 1) && (ret != 2))
471 dev_warn(dev, "UNTESTED version detected (%d)\n", ret);
473 /* Disable the chip */
474 ret = tpa6130a2_power(0);
481 regulator_put(data->supply);
483 if (data->power_gpio >= 0)
484 gpio_free(data->power_gpio);
487 i2c_set_clientdata(tpa6130a2_client, NULL);
488 tpa6130a2_client = NULL;
493 static int __devexit tpa6130a2_remove(struct i2c_client *client)
495 struct tpa6130a2_data *data = i2c_get_clientdata(client);
499 if (data->power_gpio >= 0)
500 gpio_free(data->power_gpio);
502 regulator_put(data->supply);
505 tpa6130a2_client = NULL;
510 static const struct i2c_device_id tpa6130a2_id[] = {
514 MODULE_DEVICE_TABLE(i2c, tpa6130a2_id);
516 static struct i2c_driver tpa6130a2_i2c_driver = {
519 .owner = THIS_MODULE,
521 .probe = tpa6130a2_probe,
522 .remove = __devexit_p(tpa6130a2_remove),
523 .id_table = tpa6130a2_id,
526 static int __init tpa6130a2_init(void)
528 return i2c_add_driver(&tpa6130a2_i2c_driver);
531 static void __exit tpa6130a2_exit(void)
533 i2c_del_driver(&tpa6130a2_i2c_driver);
536 MODULE_AUTHOR("Peter Ujfalusi");
537 MODULE_DESCRIPTION("TPA6130A2 Headphone amplifier driver");
538 MODULE_LICENSE("GPL");
540 module_init(tpa6130a2_init);
541 module_exit(tpa6130a2_exit);