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 /* This struct is used to save the context */
45 struct tpa6130a2_data {
47 unsigned char regs[TPA6130A2_CACHEREGNUM];
48 struct regulator_bulk_data supplies[TPA6130A2_NUM_SUPPLIES];
50 unsigned char power_state;
53 static int tpa6130a2_i2c_read(int reg)
55 struct tpa6130a2_data *data;
58 BUG_ON(tpa6130a2_client == NULL);
59 data = i2c_get_clientdata(tpa6130a2_client);
61 /* If powered off, return the cached value */
62 if (data->power_state) {
63 val = i2c_smbus_read_byte_data(tpa6130a2_client, reg);
65 dev_err(&tpa6130a2_client->dev, "Read failed\n");
67 data->regs[reg] = val;
69 val = data->regs[reg];
75 static int tpa6130a2_i2c_write(int reg, u8 value)
77 struct tpa6130a2_data *data;
80 BUG_ON(tpa6130a2_client == NULL);
81 data = i2c_get_clientdata(tpa6130a2_client);
83 if (data->power_state) {
84 val = i2c_smbus_write_byte_data(tpa6130a2_client, reg, value);
86 dev_err(&tpa6130a2_client->dev, "Write failed\n");
89 /* Either powered on or off, we save the context */
90 data->regs[reg] = value;
95 static u8 tpa6130a2_read(int reg)
97 struct tpa6130a2_data *data;
99 BUG_ON(tpa6130a2_client == NULL);
100 data = i2c_get_clientdata(tpa6130a2_client);
102 return data->regs[reg];
105 static void tpa6130a2_initialize(void)
107 struct tpa6130a2_data *data;
110 BUG_ON(tpa6130a2_client == NULL);
111 data = i2c_get_clientdata(tpa6130a2_client);
113 for (i = 1; i < TPA6130A2_REG_VERSION; i++)
114 tpa6130a2_i2c_write(i, data->regs[i]);
117 static int tpa6130a2_power(int power)
119 struct tpa6130a2_data *data;
123 BUG_ON(tpa6130a2_client == NULL);
124 data = i2c_get_clientdata(tpa6130a2_client);
126 mutex_lock(&data->mutex);
129 if (data->power_gpio >= 0)
130 gpio_set_value(data->power_gpio, 1);
132 ret = regulator_bulk_enable(ARRAY_SIZE(data->supplies),
135 dev_err(&tpa6130a2_client->dev,
136 "Failed to enable supplies: %d\n", ret);
140 data->power_state = 1;
141 tpa6130a2_initialize();
144 val = tpa6130a2_read(TPA6130A2_REG_CONTROL);
145 val &= ~TPA6130A2_SWS;
146 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL, val);
149 val = tpa6130a2_read(TPA6130A2_REG_CONTROL);
150 val |= TPA6130A2_SWS;
151 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL, val);
154 if (data->power_gpio >= 0)
155 gpio_set_value(data->power_gpio, 0);
157 ret = regulator_bulk_disable(ARRAY_SIZE(data->supplies),
160 dev_err(&tpa6130a2_client->dev,
161 "Failed to disable supplies: %d\n", ret);
165 data->power_state = 0;
169 mutex_unlock(&data->mutex);
173 static int tpa6130a2_get_reg(struct snd_kcontrol *kcontrol,
174 struct snd_ctl_elem_value *ucontrol)
176 struct soc_mixer_control *mc =
177 (struct soc_mixer_control *)kcontrol->private_value;
178 struct tpa6130a2_data *data;
179 unsigned int reg = mc->reg;
180 unsigned int shift = mc->shift;
181 unsigned int mask = mc->max;
182 unsigned int invert = mc->invert;
184 BUG_ON(tpa6130a2_client == NULL);
185 data = i2c_get_clientdata(tpa6130a2_client);
187 mutex_lock(&data->mutex);
189 ucontrol->value.integer.value[0] =
190 (tpa6130a2_read(reg) >> shift) & mask;
193 ucontrol->value.integer.value[0] =
194 mask - ucontrol->value.integer.value[0];
196 mutex_unlock(&data->mutex);
200 static int tpa6130a2_set_reg(struct snd_kcontrol *kcontrol,
201 struct snd_ctl_elem_value *ucontrol)
203 struct soc_mixer_control *mc =
204 (struct soc_mixer_control *)kcontrol->private_value;
205 struct tpa6130a2_data *data;
206 unsigned int reg = mc->reg;
207 unsigned int shift = mc->shift;
208 unsigned int mask = mc->max;
209 unsigned int invert = mc->invert;
210 unsigned int val = (ucontrol->value.integer.value[0] & mask);
211 unsigned int val_reg;
213 BUG_ON(tpa6130a2_client == NULL);
214 data = i2c_get_clientdata(tpa6130a2_client);
219 mutex_lock(&data->mutex);
221 val_reg = tpa6130a2_read(reg);
222 if (((val_reg >> shift) & mask) == val) {
223 mutex_unlock(&data->mutex);
227 val_reg &= ~(mask << shift);
228 val_reg |= val << shift;
229 tpa6130a2_i2c_write(reg, val_reg);
231 mutex_unlock(&data->mutex);
237 * TPA6130 volume. From -59.5 to 4 dB with increasing step size when going
240 static const unsigned int tpa6130_tlv[] = {
241 TLV_DB_RANGE_HEAD(10),
242 0, 1, TLV_DB_SCALE_ITEM(-5950, 600, 0),
243 2, 3, TLV_DB_SCALE_ITEM(-5000, 250, 0),
244 4, 5, TLV_DB_SCALE_ITEM(-4550, 160, 0),
245 6, 7, TLV_DB_SCALE_ITEM(-4140, 190, 0),
246 8, 9, TLV_DB_SCALE_ITEM(-3650, 120, 0),
247 10, 11, TLV_DB_SCALE_ITEM(-3330, 160, 0),
248 12, 13, TLV_DB_SCALE_ITEM(-3040, 180, 0),
249 14, 20, TLV_DB_SCALE_ITEM(-2710, 110, 0),
250 21, 37, TLV_DB_SCALE_ITEM(-1960, 74, 0),
251 38, 63, TLV_DB_SCALE_ITEM(-720, 45, 0),
254 static const struct snd_kcontrol_new tpa6130a2_controls[] = {
255 SOC_SINGLE_EXT_TLV("TPA6130A2 Headphone Playback Volume",
256 TPA6130A2_REG_VOL_MUTE, 0, 0x3f, 0,
257 tpa6130a2_get_reg, tpa6130a2_set_reg,
262 * Enable or disable channel (left or right)
263 * The bit number for mute and amplifier are the same per channel:
264 * bit 6: Right channel
265 * bit 7: Left channel
268 static void tpa6130a2_channel_enable(u8 channel, int enable)
274 /* Enable amplifier */
275 val = tpa6130a2_read(TPA6130A2_REG_CONTROL);
277 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL, val);
280 val = tpa6130a2_read(TPA6130A2_REG_VOL_MUTE);
282 tpa6130a2_i2c_write(TPA6130A2_REG_VOL_MUTE, val);
284 /* Disable channel */
286 val = tpa6130a2_read(TPA6130A2_REG_VOL_MUTE);
288 tpa6130a2_i2c_write(TPA6130A2_REG_VOL_MUTE, val);
290 /* Disable amplifier */
291 val = tpa6130a2_read(TPA6130A2_REG_CONTROL);
293 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL, val);
297 static int tpa6130a2_left_event(struct snd_soc_dapm_widget *w,
298 struct snd_kcontrol *kcontrol, int event)
301 case SND_SOC_DAPM_POST_PMU:
302 tpa6130a2_channel_enable(TPA6130A2_HP_EN_L, 1);
304 case SND_SOC_DAPM_POST_PMD:
305 tpa6130a2_channel_enable(TPA6130A2_HP_EN_L, 0);
311 static int tpa6130a2_right_event(struct snd_soc_dapm_widget *w,
312 struct snd_kcontrol *kcontrol, int event)
315 case SND_SOC_DAPM_POST_PMU:
316 tpa6130a2_channel_enable(TPA6130A2_HP_EN_R, 1);
318 case SND_SOC_DAPM_POST_PMD:
319 tpa6130a2_channel_enable(TPA6130A2_HP_EN_R, 0);
325 static int tpa6130a2_supply_event(struct snd_soc_dapm_widget *w,
326 struct snd_kcontrol *kcontrol, int event)
331 case SND_SOC_DAPM_POST_PMU:
332 ret = tpa6130a2_power(1);
334 case SND_SOC_DAPM_POST_PMD:
335 ret = tpa6130a2_power(0);
341 static const struct snd_soc_dapm_widget tpa6130a2_dapm_widgets[] = {
342 SND_SOC_DAPM_PGA_E("TPA6130A2 Left", SND_SOC_NOPM,
343 0, 0, NULL, 0, tpa6130a2_left_event,
344 SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD),
345 SND_SOC_DAPM_PGA_E("TPA6130A2 Right", SND_SOC_NOPM,
346 0, 0, NULL, 0, tpa6130a2_right_event,
347 SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD),
348 SND_SOC_DAPM_SUPPLY("TPA6130A2 Enable", SND_SOC_NOPM,
349 0, 0, tpa6130a2_supply_event,
350 SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD),
352 SND_SOC_DAPM_HP("TPA6130A2 Headphone Left", NULL),
353 SND_SOC_DAPM_HP("TPA6130A2 Headphone Right", NULL),
356 static const struct snd_soc_dapm_route audio_map[] = {
357 {"TPA6130A2 Headphone Left", NULL, "TPA6130A2 Left"},
358 {"TPA6130A2 Headphone Right", NULL, "TPA6130A2 Right"},
360 {"TPA6130A2 Headphone Left", NULL, "TPA6130A2 Enable"},
361 {"TPA6130A2 Headphone Right", NULL, "TPA6130A2 Enable"},
364 int tpa6130a2_add_controls(struct snd_soc_codec *codec)
366 snd_soc_dapm_new_controls(codec, tpa6130a2_dapm_widgets,
367 ARRAY_SIZE(tpa6130a2_dapm_widgets));
369 snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
371 return snd_soc_add_controls(codec, tpa6130a2_controls,
372 ARRAY_SIZE(tpa6130a2_controls));
375 EXPORT_SYMBOL_GPL(tpa6130a2_add_controls);
377 static int tpa6130a2_probe(struct i2c_client *client,
378 const struct i2c_device_id *id)
381 struct tpa6130a2_data *data;
382 struct tpa6130a2_platform_data *pdata;
387 if (client->dev.platform_data == NULL) {
388 dev_err(dev, "Platform data not set\n");
393 data = kzalloc(sizeof(*data), GFP_KERNEL);
395 dev_err(dev, "Can not allocate memory\n");
399 tpa6130a2_client = client;
401 i2c_set_clientdata(tpa6130a2_client, data);
403 pdata = client->dev.platform_data;
404 data->power_gpio = pdata->power_gpio;
406 mutex_init(&data->mutex);
408 /* Set default register values */
409 data->regs[TPA6130A2_REG_CONTROL] = TPA6130A2_SWS;
410 data->regs[TPA6130A2_REG_VOL_MUTE] = TPA6130A2_MUTE_R |
413 if (data->power_gpio >= 0) {
414 ret = gpio_request(data->power_gpio, "tpa6130a2 enable");
416 dev_err(dev, "Failed to request power GPIO (%d)\n",
420 gpio_direction_output(data->power_gpio, 0);
423 for (i = 0; i < ARRAY_SIZE(data->supplies); i++)
424 data->supplies[i].supply = tpa6130a2_supply_names[i];
426 ret = regulator_bulk_get(dev, ARRAY_SIZE(data->supplies),
429 dev_err(dev, "Failed to request supplies: %d\n", ret);
433 ret = tpa6130a2_power(1);
439 ret = tpa6130a2_i2c_read(TPA6130A2_REG_VERSION) &
440 TPA6130A2_VERSION_MASK;
441 if ((ret != 1) && (ret != 2))
442 dev_warn(dev, "UNTESTED version detected (%d)\n", ret);
444 /* Disable the chip */
445 ret = tpa6130a2_power(0);
452 regulator_bulk_free(ARRAY_SIZE(data->supplies), data->supplies);
454 if (data->power_gpio >= 0)
455 gpio_free(data->power_gpio);
458 i2c_set_clientdata(tpa6130a2_client, NULL);
459 tpa6130a2_client = NULL;
464 static int tpa6130a2_remove(struct i2c_client *client)
466 struct tpa6130a2_data *data = i2c_get_clientdata(client);
470 if (data->power_gpio >= 0)
471 gpio_free(data->power_gpio);
473 regulator_bulk_free(ARRAY_SIZE(data->supplies), data->supplies);
476 tpa6130a2_client = NULL;
481 static const struct i2c_device_id tpa6130a2_id[] = {
485 MODULE_DEVICE_TABLE(i2c, tpa6130a2_id);
487 static struct i2c_driver tpa6130a2_i2c_driver = {
490 .owner = THIS_MODULE,
492 .probe = tpa6130a2_probe,
493 .remove = __devexit_p(tpa6130a2_remove),
494 .id_table = tpa6130a2_id,
497 static int __init tpa6130a2_init(void)
499 return i2c_add_driver(&tpa6130a2_i2c_driver);
502 static void __exit tpa6130a2_exit(void)
504 i2c_del_driver(&tpa6130a2_i2c_driver);
507 MODULE_AUTHOR("Peter Ujfalusi");
508 MODULE_DESCRIPTION("TPA6130A2 Headphone amplifier driver");
509 MODULE_LICENSE("GPL");
511 module_init(tpa6130a2_init);
512 module_exit(tpa6130a2_exit);