2 * ALSA SoC Texas Instruments TPA6130A2 headset stereo amplifier driver
4 * Copyright (C) Nokia Corporation
6 * Author: Peter Ujfalusi <peter.ujfalusi@ti.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/tlv.h>
33 #include <linux/of_gpio.h>
35 #include "tpa6130a2.h"
42 static struct i2c_client *tpa6130a2_client;
44 /* This struct is used to save the context */
45 struct tpa6130a2_data {
47 unsigned char regs[TPA6130A2_CACHEREGNUM];
48 struct regulator *supply;
54 static int tpa6130a2_i2c_read(int reg)
56 struct tpa6130a2_data *data;
59 BUG_ON(tpa6130a2_client == NULL);
60 data = i2c_get_clientdata(tpa6130a2_client);
62 /* If powered off, return the cached value */
63 if (data->power_state) {
64 val = i2c_smbus_read_byte_data(tpa6130a2_client, reg);
66 dev_err(&tpa6130a2_client->dev, "Read failed\n");
68 data->regs[reg] = val;
70 val = data->regs[reg];
76 static int tpa6130a2_i2c_write(int reg, u8 value)
78 struct tpa6130a2_data *data;
81 BUG_ON(tpa6130a2_client == NULL);
82 data = i2c_get_clientdata(tpa6130a2_client);
84 if (data->power_state) {
85 val = i2c_smbus_write_byte_data(tpa6130a2_client, reg, value);
87 dev_err(&tpa6130a2_client->dev, "Write failed\n");
92 /* Either powered on or off, we save the context */
93 data->regs[reg] = value;
98 static u8 tpa6130a2_read(int reg)
100 struct tpa6130a2_data *data;
102 BUG_ON(tpa6130a2_client == NULL);
103 data = i2c_get_clientdata(tpa6130a2_client);
105 return data->regs[reg];
108 static int tpa6130a2_initialize(void)
110 struct tpa6130a2_data *data;
113 BUG_ON(tpa6130a2_client == NULL);
114 data = i2c_get_clientdata(tpa6130a2_client);
116 for (i = 1; i < TPA6130A2_REG_VERSION; i++) {
117 ret = tpa6130a2_i2c_write(i, data->regs[i]);
125 static int tpa6130a2_power(u8 power)
127 struct tpa6130a2_data *data;
131 BUG_ON(tpa6130a2_client == NULL);
132 data = i2c_get_clientdata(tpa6130a2_client);
134 mutex_lock(&data->mutex);
135 if (power == data->power_state)
139 ret = regulator_enable(data->supply);
141 dev_err(&tpa6130a2_client->dev,
142 "Failed to enable supply: %d\n", ret);
146 if (data->power_gpio >= 0)
147 gpio_set_value(data->power_gpio, 1);
149 data->power_state = 1;
150 ret = tpa6130a2_initialize();
152 dev_err(&tpa6130a2_client->dev,
153 "Failed to initialize chip\n");
154 if (data->power_gpio >= 0)
155 gpio_set_value(data->power_gpio, 0);
156 regulator_disable(data->supply);
157 data->power_state = 0;
162 val = tpa6130a2_read(TPA6130A2_REG_CONTROL);
163 val |= TPA6130A2_SWS;
164 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL, val);
167 if (data->power_gpio >= 0)
168 gpio_set_value(data->power_gpio, 0);
170 ret = regulator_disable(data->supply);
172 dev_err(&tpa6130a2_client->dev,
173 "Failed to disable supply: %d\n", ret);
177 data->power_state = 0;
181 mutex_unlock(&data->mutex);
185 static int tpa6130a2_get_volsw(struct snd_kcontrol *kcontrol,
186 struct snd_ctl_elem_value *ucontrol)
188 struct soc_mixer_control *mc =
189 (struct soc_mixer_control *)kcontrol->private_value;
190 struct tpa6130a2_data *data;
191 unsigned int reg = mc->reg;
192 unsigned int shift = mc->shift;
194 unsigned int mask = (1 << fls(max)) - 1;
195 unsigned int invert = mc->invert;
197 BUG_ON(tpa6130a2_client == NULL);
198 data = i2c_get_clientdata(tpa6130a2_client);
200 mutex_lock(&data->mutex);
202 ucontrol->value.integer.value[0] =
203 (tpa6130a2_read(reg) >> shift) & mask;
206 ucontrol->value.integer.value[0] =
207 max - ucontrol->value.integer.value[0];
209 mutex_unlock(&data->mutex);
213 static int tpa6130a2_put_volsw(struct snd_kcontrol *kcontrol,
214 struct snd_ctl_elem_value *ucontrol)
216 struct soc_mixer_control *mc =
217 (struct soc_mixer_control *)kcontrol->private_value;
218 struct tpa6130a2_data *data;
219 unsigned int reg = mc->reg;
220 unsigned int shift = mc->shift;
222 unsigned int mask = (1 << fls(max)) - 1;
223 unsigned int invert = mc->invert;
224 unsigned int val = (ucontrol->value.integer.value[0] & mask);
225 unsigned int val_reg;
227 BUG_ON(tpa6130a2_client == NULL);
228 data = i2c_get_clientdata(tpa6130a2_client);
233 mutex_lock(&data->mutex);
235 val_reg = tpa6130a2_read(reg);
236 if (((val_reg >> shift) & mask) == val) {
237 mutex_unlock(&data->mutex);
241 val_reg &= ~(mask << shift);
242 val_reg |= val << shift;
243 tpa6130a2_i2c_write(reg, val_reg);
245 mutex_unlock(&data->mutex);
251 * TPA6130 volume. From -59.5 to 4 dB with increasing step size when going
254 static const unsigned int tpa6130_tlv[] = {
255 TLV_DB_RANGE_HEAD(10),
256 0, 1, TLV_DB_SCALE_ITEM(-5950, 600, 0),
257 2, 3, TLV_DB_SCALE_ITEM(-5000, 250, 0),
258 4, 5, TLV_DB_SCALE_ITEM(-4550, 160, 0),
259 6, 7, TLV_DB_SCALE_ITEM(-4140, 190, 0),
260 8, 9, TLV_DB_SCALE_ITEM(-3650, 120, 0),
261 10, 11, TLV_DB_SCALE_ITEM(-3330, 160, 0),
262 12, 13, TLV_DB_SCALE_ITEM(-3040, 180, 0),
263 14, 20, TLV_DB_SCALE_ITEM(-2710, 110, 0),
264 21, 37, TLV_DB_SCALE_ITEM(-1960, 74, 0),
265 38, 63, TLV_DB_SCALE_ITEM(-720, 45, 0),
268 static const struct snd_kcontrol_new tpa6130a2_controls[] = {
269 SOC_SINGLE_EXT_TLV("TPA6130A2 Headphone Playback Volume",
270 TPA6130A2_REG_VOL_MUTE, 0, 0x3f, 0,
271 tpa6130a2_get_volsw, tpa6130a2_put_volsw,
275 static const unsigned int tpa6140_tlv[] = {
276 TLV_DB_RANGE_HEAD(3),
277 0, 8, TLV_DB_SCALE_ITEM(-5900, 400, 0),
278 9, 16, TLV_DB_SCALE_ITEM(-2500, 200, 0),
279 17, 31, TLV_DB_SCALE_ITEM(-1000, 100, 0),
282 static const struct snd_kcontrol_new tpa6140a2_controls[] = {
283 SOC_SINGLE_EXT_TLV("TPA6140A2 Headphone Playback Volume",
284 TPA6130A2_REG_VOL_MUTE, 1, 0x1f, 0,
285 tpa6130a2_get_volsw, tpa6130a2_put_volsw,
290 * Enable or disable channel (left or right)
291 * The bit number for mute and amplifier are the same per channel:
292 * bit 6: Right channel
293 * bit 7: Left channel
296 static void tpa6130a2_channel_enable(u8 channel, int enable)
302 /* Enable amplifier */
303 val = tpa6130a2_read(TPA6130A2_REG_CONTROL);
305 val &= ~TPA6130A2_SWS;
306 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL, val);
309 val = tpa6130a2_read(TPA6130A2_REG_VOL_MUTE);
311 tpa6130a2_i2c_write(TPA6130A2_REG_VOL_MUTE, val);
313 /* Disable channel */
315 val = tpa6130a2_read(TPA6130A2_REG_VOL_MUTE);
317 tpa6130a2_i2c_write(TPA6130A2_REG_VOL_MUTE, val);
319 /* Disable amplifier */
320 val = tpa6130a2_read(TPA6130A2_REG_CONTROL);
322 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL, val);
326 int tpa6130a2_stereo_enable(struct snd_soc_codec *codec, int enable)
330 ret = tpa6130a2_power(1);
333 tpa6130a2_channel_enable(TPA6130A2_HP_EN_R | TPA6130A2_HP_EN_L,
336 tpa6130a2_channel_enable(TPA6130A2_HP_EN_R | TPA6130A2_HP_EN_L,
338 ret = tpa6130a2_power(0);
343 EXPORT_SYMBOL_GPL(tpa6130a2_stereo_enable);
345 int tpa6130a2_add_controls(struct snd_soc_codec *codec)
347 struct tpa6130a2_data *data;
349 if (tpa6130a2_client == NULL)
352 data = i2c_get_clientdata(tpa6130a2_client);
354 if (data->id == TPA6140A2)
355 return snd_soc_add_codec_controls(codec, tpa6140a2_controls,
356 ARRAY_SIZE(tpa6140a2_controls));
358 return snd_soc_add_codec_controls(codec, tpa6130a2_controls,
359 ARRAY_SIZE(tpa6130a2_controls));
361 EXPORT_SYMBOL_GPL(tpa6130a2_add_controls);
363 static int tpa6130a2_probe(struct i2c_client *client,
364 const struct i2c_device_id *id)
367 struct tpa6130a2_data *data;
368 struct tpa6130a2_platform_data *pdata = client->dev.platform_data;
369 struct device_node *np = client->dev.of_node;
370 const char *regulator;
375 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
377 dev_err(dev, "Can not allocate memory\n");
382 data->power_gpio = pdata->power_gpio;
384 data->power_gpio = of_get_named_gpio(np, "power-gpio", 0);
386 dev_err(dev, "Platform data not set\n");
391 tpa6130a2_client = client;
393 i2c_set_clientdata(tpa6130a2_client, data);
395 data->id = id->driver_data;
397 mutex_init(&data->mutex);
399 /* Set default register values */
400 data->regs[TPA6130A2_REG_CONTROL] = TPA6130A2_SWS;
401 data->regs[TPA6130A2_REG_VOL_MUTE] = TPA6130A2_MUTE_R |
404 if (data->power_gpio >= 0) {
405 ret = devm_gpio_request(dev, data->power_gpio,
408 dev_err(dev, "Failed to request power GPIO (%d)\n",
412 gpio_direction_output(data->power_gpio, 0);
417 dev_warn(dev, "Unknown TPA model (%d). Assuming 6130A2\n",
427 data->supply = devm_regulator_get(dev, regulator);
428 if (IS_ERR(data->supply)) {
429 ret = PTR_ERR(data->supply);
430 dev_err(dev, "Failed to request supply: %d\n", ret);
434 ret = tpa6130a2_power(1);
440 ret = tpa6130a2_i2c_read(TPA6130A2_REG_VERSION) &
441 TPA6130A2_VERSION_MASK;
442 if ((ret != 1) && (ret != 2))
443 dev_warn(dev, "UNTESTED version detected (%d)\n", ret);
445 /* Disable the chip */
446 ret = tpa6130a2_power(0);
453 tpa6130a2_client = NULL;
458 static int tpa6130a2_remove(struct i2c_client *client)
461 tpa6130a2_client = NULL;
466 static const struct i2c_device_id tpa6130a2_id[] = {
467 { "tpa6130a2", TPA6130A2 },
468 { "tpa6140a2", TPA6140A2 },
471 MODULE_DEVICE_TABLE(i2c, tpa6130a2_id);
473 #if IS_ENABLED(CONFIG_OF)
474 static const struct of_device_id tpa6130a2_of_match[] = {
475 { .compatible = "ti,tpa6130a2", },
476 { .compatible = "ti,tpa6140a2" },
479 MODULE_DEVICE_TABLE(of, tpa6130a2_of_match);
482 static struct i2c_driver tpa6130a2_i2c_driver = {
485 .owner = THIS_MODULE,
486 .of_match_table = of_match_ptr(tpa6130a2_of_match),
488 .probe = tpa6130a2_probe,
489 .remove = tpa6130a2_remove,
490 .id_table = tpa6130a2_id,
493 module_i2c_driver(tpa6130a2_i2c_driver);
495 MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
496 MODULE_DESCRIPTION("TPA6130A2 Headphone amplifier driver");
497 MODULE_LICENSE("GPL");