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 #define TPA6130A2_NUM_SUPPLIES 2
39 static const char *tpa6130a2_supply_names[TPA6130A2_NUM_SUPPLIES] = {
44 static const char *tpa6140a2_supply_names[TPA6130A2_NUM_SUPPLIES] = {
49 /* This struct is used to save the context */
50 struct tpa6130a2_data {
52 unsigned char regs[TPA6130A2_CACHEREGNUM];
53 struct regulator_bulk_data supplies[TPA6130A2_NUM_SUPPLIES];
55 unsigned char power_state;
59 static int tpa6130a2_i2c_read(int reg)
61 struct tpa6130a2_data *data;
64 BUG_ON(tpa6130a2_client == NULL);
65 data = i2c_get_clientdata(tpa6130a2_client);
67 /* If powered off, return the cached value */
68 if (data->power_state) {
69 val = i2c_smbus_read_byte_data(tpa6130a2_client, reg);
71 dev_err(&tpa6130a2_client->dev, "Read failed\n");
73 data->regs[reg] = val;
75 val = data->regs[reg];
81 static int tpa6130a2_i2c_write(int reg, u8 value)
83 struct tpa6130a2_data *data;
86 BUG_ON(tpa6130a2_client == NULL);
87 data = i2c_get_clientdata(tpa6130a2_client);
89 if (data->power_state) {
90 val = i2c_smbus_write_byte_data(tpa6130a2_client, reg, value);
92 dev_err(&tpa6130a2_client->dev, "Write failed\n");
95 /* Either powered on or off, we save the context */
96 data->regs[reg] = value;
101 static u8 tpa6130a2_read(int reg)
103 struct tpa6130a2_data *data;
105 BUG_ON(tpa6130a2_client == NULL);
106 data = i2c_get_clientdata(tpa6130a2_client);
108 return data->regs[reg];
111 static void tpa6130a2_initialize(void)
113 struct tpa6130a2_data *data;
116 BUG_ON(tpa6130a2_client == NULL);
117 data = i2c_get_clientdata(tpa6130a2_client);
119 for (i = 1; i < TPA6130A2_REG_VERSION; i++)
120 tpa6130a2_i2c_write(i, data->regs[i]);
123 static int tpa6130a2_power(int power)
125 struct tpa6130a2_data *data;
129 BUG_ON(tpa6130a2_client == NULL);
130 data = i2c_get_clientdata(tpa6130a2_client);
132 mutex_lock(&data->mutex);
135 if (data->power_gpio >= 0)
136 gpio_set_value(data->power_gpio, 1);
138 ret = regulator_bulk_enable(ARRAY_SIZE(data->supplies),
141 dev_err(&tpa6130a2_client->dev,
142 "Failed to enable supplies: %d\n", ret);
146 data->power_state = 1;
147 tpa6130a2_initialize();
150 val = tpa6130a2_read(TPA6130A2_REG_CONTROL);
151 val &= ~TPA6130A2_SWS;
152 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL, val);
155 val = tpa6130a2_read(TPA6130A2_REG_CONTROL);
156 val |= TPA6130A2_SWS;
157 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL, val);
160 if (data->power_gpio >= 0)
161 gpio_set_value(data->power_gpio, 0);
163 ret = regulator_bulk_disable(ARRAY_SIZE(data->supplies),
166 dev_err(&tpa6130a2_client->dev,
167 "Failed to disable supplies: %d\n", ret);
171 data->power_state = 0;
175 mutex_unlock(&data->mutex);
179 static int tpa6130a2_get_volsw(struct snd_kcontrol *kcontrol,
180 struct snd_ctl_elem_value *ucontrol)
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;
188 unsigned int mask = (1 << fls(max)) - 1;
189 unsigned int invert = mc->invert;
191 BUG_ON(tpa6130a2_client == NULL);
192 data = i2c_get_clientdata(tpa6130a2_client);
194 mutex_lock(&data->mutex);
196 ucontrol->value.integer.value[0] =
197 (tpa6130a2_read(reg) >> shift) & mask;
200 ucontrol->value.integer.value[0] =
201 max - ucontrol->value.integer.value[0];
203 mutex_unlock(&data->mutex);
207 static int tpa6130a2_put_volsw(struct snd_kcontrol *kcontrol,
208 struct snd_ctl_elem_value *ucontrol)
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;
216 unsigned int mask = (1 << fls(max)) - 1;
217 unsigned int invert = mc->invert;
218 unsigned int val = (ucontrol->value.integer.value[0] & mask);
219 unsigned int val_reg;
221 BUG_ON(tpa6130a2_client == NULL);
222 data = i2c_get_clientdata(tpa6130a2_client);
227 mutex_lock(&data->mutex);
229 val_reg = tpa6130a2_read(reg);
230 if (((val_reg >> shift) & mask) == val) {
231 mutex_unlock(&data->mutex);
235 val_reg &= ~(mask << shift);
236 val_reg |= val << shift;
237 tpa6130a2_i2c_write(reg, val_reg);
239 mutex_unlock(&data->mutex);
245 * TPA6130 volume. From -59.5 to 4 dB with increasing step size when going
248 static 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),
262 static const struct snd_kcontrol_new tpa6130a2_controls[] = {
263 SOC_SINGLE_EXT_TLV("TPA6130A2 Headphone Playback Volume",
264 TPA6130A2_REG_VOL_MUTE, 0, 0x3f, 0,
265 tpa6130a2_get_volsw, tpa6130a2_put_volsw,
269 static 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),
276 static const struct snd_kcontrol_new tpa6140a2_controls[] = {
277 SOC_SINGLE_EXT_TLV("TPA6140A2 Headphone Playback Volume",
278 TPA6130A2_REG_VOL_MUTE, 1, 0x1f, 0,
279 tpa6130a2_get_volsw, tpa6130a2_put_volsw,
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
290 static void tpa6130a2_channel_enable(u8 channel, int enable)
296 /* Enable amplifier */
297 val = tpa6130a2_read(TPA6130A2_REG_CONTROL);
299 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL, val);
302 val = tpa6130a2_read(TPA6130A2_REG_VOL_MUTE);
304 tpa6130a2_i2c_write(TPA6130A2_REG_VOL_MUTE, val);
306 /* Disable channel */
308 val = tpa6130a2_read(TPA6130A2_REG_VOL_MUTE);
310 tpa6130a2_i2c_write(TPA6130A2_REG_VOL_MUTE, val);
312 /* Disable amplifier */
313 val = tpa6130a2_read(TPA6130A2_REG_CONTROL);
315 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL, val);
319 static int tpa6130a2_left_event(struct snd_soc_dapm_widget *w,
320 struct snd_kcontrol *kcontrol, int event)
323 case SND_SOC_DAPM_POST_PMU:
324 tpa6130a2_channel_enable(TPA6130A2_HP_EN_L, 1);
326 case SND_SOC_DAPM_POST_PMD:
327 tpa6130a2_channel_enable(TPA6130A2_HP_EN_L, 0);
333 static int tpa6130a2_right_event(struct snd_soc_dapm_widget *w,
334 struct snd_kcontrol *kcontrol, int event)
337 case SND_SOC_DAPM_POST_PMU:
338 tpa6130a2_channel_enable(TPA6130A2_HP_EN_R, 1);
340 case SND_SOC_DAPM_POST_PMD:
341 tpa6130a2_channel_enable(TPA6130A2_HP_EN_R, 0);
347 static int tpa6130a2_supply_event(struct snd_soc_dapm_widget *w,
348 struct snd_kcontrol *kcontrol, int event)
353 case SND_SOC_DAPM_POST_PMU:
354 ret = tpa6130a2_power(1);
356 case SND_SOC_DAPM_POST_PMD:
357 ret = tpa6130a2_power(0);
363 static const struct snd_soc_dapm_widget tpa6130a2_dapm_widgets[] = {
364 SND_SOC_DAPM_PGA_E("TPA6130A2 Left", SND_SOC_NOPM,
365 0, 0, NULL, 0, tpa6130a2_left_event,
366 SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD),
367 SND_SOC_DAPM_PGA_E("TPA6130A2 Right", SND_SOC_NOPM,
368 0, 0, NULL, 0, tpa6130a2_right_event,
369 SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD),
370 SND_SOC_DAPM_SUPPLY("TPA6130A2 Enable", SND_SOC_NOPM,
371 0, 0, tpa6130a2_supply_event,
372 SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD),
374 SND_SOC_DAPM_OUTPUT("TPA6130A2 Headphone Left"),
375 SND_SOC_DAPM_OUTPUT("TPA6130A2 Headphone Right"),
378 static const struct snd_soc_dapm_route audio_map[] = {
379 {"TPA6130A2 Headphone Left", NULL, "TPA6130A2 Left"},
380 {"TPA6130A2 Headphone Right", NULL, "TPA6130A2 Right"},
382 {"TPA6130A2 Headphone Left", NULL, "TPA6130A2 Enable"},
383 {"TPA6130A2 Headphone Right", NULL, "TPA6130A2 Enable"},
386 int tpa6130a2_add_controls(struct snd_soc_codec *codec)
388 struct tpa6130a2_data *data;
390 BUG_ON(tpa6130a2_client == NULL);
391 data = i2c_get_clientdata(tpa6130a2_client);
393 snd_soc_dapm_new_controls(codec, tpa6130a2_dapm_widgets,
394 ARRAY_SIZE(tpa6130a2_dapm_widgets));
396 snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
398 if (data->id == TPA6140A2)
399 return snd_soc_add_controls(codec, tpa6140a2_controls,
400 ARRAY_SIZE(tpa6140a2_controls));
402 return snd_soc_add_controls(codec, tpa6130a2_controls,
403 ARRAY_SIZE(tpa6130a2_controls));
406 EXPORT_SYMBOL_GPL(tpa6130a2_add_controls);
408 static int __devinit tpa6130a2_probe(struct i2c_client *client,
409 const struct i2c_device_id *id)
412 struct tpa6130a2_data *data;
413 struct tpa6130a2_platform_data *pdata;
418 if (client->dev.platform_data == NULL) {
419 dev_err(dev, "Platform data not set\n");
424 data = kzalloc(sizeof(*data), GFP_KERNEL);
426 dev_err(dev, "Can not allocate memory\n");
430 tpa6130a2_client = client;
432 i2c_set_clientdata(tpa6130a2_client, data);
434 pdata = client->dev.platform_data;
435 data->power_gpio = pdata->power_gpio;
436 data->id = pdata->id;
438 mutex_init(&data->mutex);
440 /* Set default register values */
441 data->regs[TPA6130A2_REG_CONTROL] = TPA6130A2_SWS;
442 data->regs[TPA6130A2_REG_VOL_MUTE] = TPA6130A2_MUTE_R |
445 if (data->power_gpio >= 0) {
446 ret = gpio_request(data->power_gpio, "tpa6130a2 enable");
448 dev_err(dev, "Failed to request power GPIO (%d)\n",
452 gpio_direction_output(data->power_gpio, 0);
457 for (i = 0; i < ARRAY_SIZE(data->supplies); i++)
458 data->supplies[i].supply = tpa6130a2_supply_names[i];
461 for (i = 0; i < ARRAY_SIZE(data->supplies); i++)
462 data->supplies[i].supply = tpa6140a2_supply_names[i];;
465 dev_warn(dev, "Unknown TPA model (%d). Assuming 6130A2\n",
467 for (i = 0; i < ARRAY_SIZE(data->supplies); i++)
468 data->supplies[i].supply = tpa6130a2_supply_names[i];
471 ret = regulator_bulk_get(dev, ARRAY_SIZE(data->supplies),
474 dev_err(dev, "Failed to request supplies: %d\n", ret);
478 ret = tpa6130a2_power(1);
484 ret = tpa6130a2_i2c_read(TPA6130A2_REG_VERSION) &
485 TPA6130A2_VERSION_MASK;
486 if ((ret != 1) && (ret != 2))
487 dev_warn(dev, "UNTESTED version detected (%d)\n", ret);
489 /* Disable the chip */
490 ret = tpa6130a2_power(0);
497 regulator_bulk_free(ARRAY_SIZE(data->supplies), data->supplies);
499 if (data->power_gpio >= 0)
500 gpio_free(data->power_gpio);
503 i2c_set_clientdata(tpa6130a2_client, NULL);
504 tpa6130a2_client = NULL;
509 static int __devexit tpa6130a2_remove(struct i2c_client *client)
511 struct tpa6130a2_data *data = i2c_get_clientdata(client);
515 if (data->power_gpio >= 0)
516 gpio_free(data->power_gpio);
518 regulator_bulk_free(ARRAY_SIZE(data->supplies), data->supplies);
521 tpa6130a2_client = NULL;
526 static const struct i2c_device_id tpa6130a2_id[] = {
530 MODULE_DEVICE_TABLE(i2c, tpa6130a2_id);
532 static struct i2c_driver tpa6130a2_i2c_driver = {
535 .owner = THIS_MODULE,
537 .probe = tpa6130a2_probe,
538 .remove = __devexit_p(tpa6130a2_remove),
539 .id_table = tpa6130a2_id,
542 static int __init tpa6130a2_init(void)
544 return i2c_add_driver(&tpa6130a2_i2c_driver);
547 static void __exit tpa6130a2_exit(void)
549 i2c_del_driver(&tpa6130a2_i2c_driver);
552 MODULE_AUTHOR("Peter Ujfalusi");
553 MODULE_DESCRIPTION("TPA6130A2 Headphone amplifier driver");
554 MODULE_LICENSE("GPL");
556 module_init(tpa6130a2_init);
557 module_exit(tpa6130a2_exit);