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 if (WARN_ON(!tpa6130a2_client
))
61 data
= i2c_get_clientdata(tpa6130a2_client
);
63 /* If powered off, return the cached value */
64 if (data
->power_state
) {
65 val
= i2c_smbus_read_byte_data(tpa6130a2_client
, reg
);
67 dev_err(&tpa6130a2_client
->dev
, "Read failed\n");
69 data
->regs
[reg
] = val
;
71 val
= data
->regs
[reg
];
77 static int tpa6130a2_i2c_write(int reg
, u8 value
)
79 struct tpa6130a2_data
*data
;
82 if (WARN_ON(!tpa6130a2_client
))
84 data
= i2c_get_clientdata(tpa6130a2_client
);
86 if (data
->power_state
) {
87 val
= i2c_smbus_write_byte_data(tpa6130a2_client
, reg
, value
);
89 dev_err(&tpa6130a2_client
->dev
, "Write failed\n");
94 /* Either powered on or off, we save the context */
95 data
->regs
[reg
] = value
;
100 static u8
tpa6130a2_read(int reg
)
102 struct tpa6130a2_data
*data
;
104 if (WARN_ON(!tpa6130a2_client
))
106 data
= i2c_get_clientdata(tpa6130a2_client
);
108 return data
->regs
[reg
];
111 static int tpa6130a2_initialize(void)
113 struct tpa6130a2_data
*data
;
116 if (WARN_ON(!tpa6130a2_client
))
118 data
= i2c_get_clientdata(tpa6130a2_client
);
120 for (i
= 1; i
< TPA6130A2_REG_VERSION
; i
++) {
121 ret
= tpa6130a2_i2c_write(i
, data
->regs
[i
]);
129 static int tpa6130a2_power(u8 power
)
131 struct tpa6130a2_data
*data
;
135 if (WARN_ON(!tpa6130a2_client
))
137 data
= i2c_get_clientdata(tpa6130a2_client
);
139 mutex_lock(&data
->mutex
);
140 if (power
== data
->power_state
)
144 ret
= regulator_enable(data
->supply
);
146 dev_err(&tpa6130a2_client
->dev
,
147 "Failed to enable supply: %d\n", ret
);
151 if (data
->power_gpio
>= 0)
152 gpio_set_value(data
->power_gpio
, 1);
154 data
->power_state
= 1;
155 ret
= tpa6130a2_initialize();
157 dev_err(&tpa6130a2_client
->dev
,
158 "Failed to initialize chip\n");
159 if (data
->power_gpio
>= 0)
160 gpio_set_value(data
->power_gpio
, 0);
161 regulator_disable(data
->supply
);
162 data
->power_state
= 0;
167 val
= tpa6130a2_read(TPA6130A2_REG_CONTROL
);
168 val
|= TPA6130A2_SWS
;
169 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL
, val
);
172 if (data
->power_gpio
>= 0)
173 gpio_set_value(data
->power_gpio
, 0);
175 ret
= regulator_disable(data
->supply
);
177 dev_err(&tpa6130a2_client
->dev
,
178 "Failed to disable supply: %d\n", ret
);
182 data
->power_state
= 0;
186 mutex_unlock(&data
->mutex
);
190 static int tpa6130a2_get_volsw(struct snd_kcontrol
*kcontrol
,
191 struct snd_ctl_elem_value
*ucontrol
)
193 struct soc_mixer_control
*mc
=
194 (struct soc_mixer_control
*)kcontrol
->private_value
;
195 struct tpa6130a2_data
*data
;
196 unsigned int reg
= mc
->reg
;
197 unsigned int shift
= mc
->shift
;
199 unsigned int mask
= (1 << fls(max
)) - 1;
200 unsigned int invert
= mc
->invert
;
202 if (WARN_ON(!tpa6130a2_client
))
204 data
= i2c_get_clientdata(tpa6130a2_client
);
206 mutex_lock(&data
->mutex
);
208 ucontrol
->value
.integer
.value
[0] =
209 (tpa6130a2_read(reg
) >> shift
) & mask
;
212 ucontrol
->value
.integer
.value
[0] =
213 max
- ucontrol
->value
.integer
.value
[0];
215 mutex_unlock(&data
->mutex
);
219 static int tpa6130a2_put_volsw(struct snd_kcontrol
*kcontrol
,
220 struct snd_ctl_elem_value
*ucontrol
)
222 struct soc_mixer_control
*mc
=
223 (struct soc_mixer_control
*)kcontrol
->private_value
;
224 struct tpa6130a2_data
*data
;
225 unsigned int reg
= mc
->reg
;
226 unsigned int shift
= mc
->shift
;
228 unsigned int mask
= (1 << fls(max
)) - 1;
229 unsigned int invert
= mc
->invert
;
230 unsigned int val
= (ucontrol
->value
.integer
.value
[0] & mask
);
231 unsigned int val_reg
;
233 if (WARN_ON(!tpa6130a2_client
))
235 data
= i2c_get_clientdata(tpa6130a2_client
);
240 mutex_lock(&data
->mutex
);
242 val_reg
= tpa6130a2_read(reg
);
243 if (((val_reg
>> shift
) & mask
) == val
) {
244 mutex_unlock(&data
->mutex
);
248 val_reg
&= ~(mask
<< shift
);
249 val_reg
|= val
<< shift
;
250 tpa6130a2_i2c_write(reg
, val_reg
);
252 mutex_unlock(&data
->mutex
);
258 * TPA6130 volume. From -59.5 to 4 dB with increasing step size when going
261 static const unsigned int tpa6130_tlv
[] = {
262 TLV_DB_RANGE_HEAD(10),
263 0, 1, TLV_DB_SCALE_ITEM(-5950, 600, 0),
264 2, 3, TLV_DB_SCALE_ITEM(-5000, 250, 0),
265 4, 5, TLV_DB_SCALE_ITEM(-4550, 160, 0),
266 6, 7, TLV_DB_SCALE_ITEM(-4140, 190, 0),
267 8, 9, TLV_DB_SCALE_ITEM(-3650, 120, 0),
268 10, 11, TLV_DB_SCALE_ITEM(-3330, 160, 0),
269 12, 13, TLV_DB_SCALE_ITEM(-3040, 180, 0),
270 14, 20, TLV_DB_SCALE_ITEM(-2710, 110, 0),
271 21, 37, TLV_DB_SCALE_ITEM(-1960, 74, 0),
272 38, 63, TLV_DB_SCALE_ITEM(-720, 45, 0),
275 static const struct snd_kcontrol_new tpa6130a2_controls
[] = {
276 SOC_SINGLE_EXT_TLV("TPA6130A2 Headphone Playback Volume",
277 TPA6130A2_REG_VOL_MUTE
, 0, 0x3f, 0,
278 tpa6130a2_get_volsw
, tpa6130a2_put_volsw
,
282 static const unsigned int tpa6140_tlv
[] = {
283 TLV_DB_RANGE_HEAD(3),
284 0, 8, TLV_DB_SCALE_ITEM(-5900, 400, 0),
285 9, 16, TLV_DB_SCALE_ITEM(-2500, 200, 0),
286 17, 31, TLV_DB_SCALE_ITEM(-1000, 100, 0),
289 static const struct snd_kcontrol_new tpa6140a2_controls
[] = {
290 SOC_SINGLE_EXT_TLV("TPA6140A2 Headphone Playback Volume",
291 TPA6130A2_REG_VOL_MUTE
, 1, 0x1f, 0,
292 tpa6130a2_get_volsw
, tpa6130a2_put_volsw
,
297 * Enable or disable channel (left or right)
298 * The bit number for mute and amplifier are the same per channel:
299 * bit 6: Right channel
300 * bit 7: Left channel
303 static void tpa6130a2_channel_enable(u8 channel
, int enable
)
309 /* Enable amplifier */
310 val
= tpa6130a2_read(TPA6130A2_REG_CONTROL
);
312 val
&= ~TPA6130A2_SWS
;
313 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL
, val
);
316 val
= tpa6130a2_read(TPA6130A2_REG_VOL_MUTE
);
318 tpa6130a2_i2c_write(TPA6130A2_REG_VOL_MUTE
, val
);
320 /* Disable channel */
322 val
= tpa6130a2_read(TPA6130A2_REG_VOL_MUTE
);
324 tpa6130a2_i2c_write(TPA6130A2_REG_VOL_MUTE
, val
);
326 /* Disable amplifier */
327 val
= tpa6130a2_read(TPA6130A2_REG_CONTROL
);
329 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL
, val
);
333 int tpa6130a2_stereo_enable(struct snd_soc_codec
*codec
, int enable
)
337 ret
= tpa6130a2_power(1);
340 tpa6130a2_channel_enable(TPA6130A2_HP_EN_R
| TPA6130A2_HP_EN_L
,
343 tpa6130a2_channel_enable(TPA6130A2_HP_EN_R
| TPA6130A2_HP_EN_L
,
345 ret
= tpa6130a2_power(0);
350 EXPORT_SYMBOL_GPL(tpa6130a2_stereo_enable
);
352 int tpa6130a2_add_controls(struct snd_soc_codec
*codec
)
354 struct tpa6130a2_data
*data
;
356 if (tpa6130a2_client
== NULL
)
359 data
= i2c_get_clientdata(tpa6130a2_client
);
361 if (data
->id
== TPA6140A2
)
362 return snd_soc_add_codec_controls(codec
, tpa6140a2_controls
,
363 ARRAY_SIZE(tpa6140a2_controls
));
365 return snd_soc_add_codec_controls(codec
, tpa6130a2_controls
,
366 ARRAY_SIZE(tpa6130a2_controls
));
368 EXPORT_SYMBOL_GPL(tpa6130a2_add_controls
);
370 static int tpa6130a2_probe(struct i2c_client
*client
,
371 const struct i2c_device_id
*id
)
374 struct tpa6130a2_data
*data
;
375 struct tpa6130a2_platform_data
*pdata
= client
->dev
.platform_data
;
376 struct device_node
*np
= client
->dev
.of_node
;
377 const char *regulator
;
382 data
= devm_kzalloc(&client
->dev
, sizeof(*data
), GFP_KERNEL
);
384 dev_err(dev
, "Can not allocate memory\n");
389 data
->power_gpio
= pdata
->power_gpio
;
391 data
->power_gpio
= of_get_named_gpio(np
, "power-gpio", 0);
393 dev_err(dev
, "Platform data not set\n");
398 tpa6130a2_client
= client
;
400 i2c_set_clientdata(tpa6130a2_client
, data
);
402 data
->id
= id
->driver_data
;
404 mutex_init(&data
->mutex
);
406 /* Set default register values */
407 data
->regs
[TPA6130A2_REG_CONTROL
] = TPA6130A2_SWS
;
408 data
->regs
[TPA6130A2_REG_VOL_MUTE
] = TPA6130A2_MUTE_R
|
411 if (data
->power_gpio
>= 0) {
412 ret
= devm_gpio_request(dev
, data
->power_gpio
,
415 dev_err(dev
, "Failed to request power GPIO (%d)\n",
419 gpio_direction_output(data
->power_gpio
, 0);
424 dev_warn(dev
, "Unknown TPA model (%d). Assuming 6130A2\n",
434 data
->supply
= devm_regulator_get(dev
, regulator
);
435 if (IS_ERR(data
->supply
)) {
436 ret
= PTR_ERR(data
->supply
);
437 dev_err(dev
, "Failed to request supply: %d\n", ret
);
441 ret
= tpa6130a2_power(1);
447 ret
= tpa6130a2_i2c_read(TPA6130A2_REG_VERSION
) &
448 TPA6130A2_VERSION_MASK
;
449 if ((ret
!= 1) && (ret
!= 2))
450 dev_warn(dev
, "UNTESTED version detected (%d)\n", ret
);
452 /* Disable the chip */
453 ret
= tpa6130a2_power(0);
460 tpa6130a2_client
= NULL
;
465 static int tpa6130a2_remove(struct i2c_client
*client
)
468 tpa6130a2_client
= NULL
;
473 static const struct i2c_device_id tpa6130a2_id
[] = {
474 { "tpa6130a2", TPA6130A2
},
475 { "tpa6140a2", TPA6140A2
},
478 MODULE_DEVICE_TABLE(i2c
, tpa6130a2_id
);
480 #if IS_ENABLED(CONFIG_OF)
481 static const struct of_device_id tpa6130a2_of_match
[] = {
482 { .compatible
= "ti,tpa6130a2", },
483 { .compatible
= "ti,tpa6140a2" },
486 MODULE_DEVICE_TABLE(of
, tpa6130a2_of_match
);
489 static struct i2c_driver tpa6130a2_i2c_driver
= {
492 .owner
= THIS_MODULE
,
493 .of_match_table
= of_match_ptr(tpa6130a2_of_match
),
495 .probe
= tpa6130a2_probe
,
496 .remove
= tpa6130a2_remove
,
497 .id_table
= tpa6130a2_id
,
500 module_i2c_driver(tpa6130a2_i2c_driver
);
502 MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
503 MODULE_DESCRIPTION("TPA6130A2 Headphone amplifier driver");
504 MODULE_LICENSE("GPL");