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>
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
;
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");
86 /* Either powered on or off, we save the context */
87 data
->regs
[reg
] = value
;
92 static u8
tpa6130a2_read(int reg
)
94 struct tpa6130a2_data
*data
;
96 BUG_ON(tpa6130a2_client
== NULL
);
97 data
= i2c_get_clientdata(tpa6130a2_client
);
99 return data
->regs
[reg
];
102 static int tpa6130a2_initialize(void)
104 struct tpa6130a2_data
*data
;
107 BUG_ON(tpa6130a2_client
== NULL
);
108 data
= i2c_get_clientdata(tpa6130a2_client
);
110 for (i
= 1; i
< TPA6130A2_REG_VERSION
; i
++) {
111 ret
= tpa6130a2_i2c_write(i
, data
->regs
[i
]);
119 static int tpa6130a2_power(u8 power
)
121 struct tpa6130a2_data
*data
;
125 BUG_ON(tpa6130a2_client
== NULL
);
126 data
= i2c_get_clientdata(tpa6130a2_client
);
128 mutex_lock(&data
->mutex
);
129 if (power
== data
->power_state
)
133 ret
= regulator_enable(data
->supply
);
135 dev_err(&tpa6130a2_client
->dev
,
136 "Failed to enable supply: %d\n", ret
);
140 if (data
->power_gpio
>= 0)
141 gpio_set_value(data
->power_gpio
, 1);
143 data
->power_state
= 1;
144 ret
= tpa6130a2_initialize();
146 dev_err(&tpa6130a2_client
->dev
,
147 "Failed to initialize chip\n");
148 if (data
->power_gpio
>= 0)
149 gpio_set_value(data
->power_gpio
, 0);
150 regulator_disable(data
->supply
);
151 data
->power_state
= 0;
156 val
= tpa6130a2_read(TPA6130A2_REG_CONTROL
);
157 val
|= TPA6130A2_SWS
;
158 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL
, val
);
161 if (data
->power_gpio
>= 0)
162 gpio_set_value(data
->power_gpio
, 0);
164 ret
= regulator_disable(data
->supply
);
166 dev_err(&tpa6130a2_client
->dev
,
167 "Failed to disable supply: %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 val
&= ~TPA6130A2_SWS
;
300 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL
, val
);
303 val
= tpa6130a2_read(TPA6130A2_REG_VOL_MUTE
);
305 tpa6130a2_i2c_write(TPA6130A2_REG_VOL_MUTE
, val
);
307 /* Disable channel */
309 val
= tpa6130a2_read(TPA6130A2_REG_VOL_MUTE
);
311 tpa6130a2_i2c_write(TPA6130A2_REG_VOL_MUTE
, val
);
313 /* Disable amplifier */
314 val
= tpa6130a2_read(TPA6130A2_REG_CONTROL
);
316 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL
, val
);
320 int tpa6130a2_stereo_enable(struct snd_soc_codec
*codec
, int enable
)
324 ret
= tpa6130a2_power(1);
327 tpa6130a2_channel_enable(TPA6130A2_HP_EN_R
| TPA6130A2_HP_EN_L
,
330 tpa6130a2_channel_enable(TPA6130A2_HP_EN_R
| TPA6130A2_HP_EN_L
,
332 ret
= tpa6130a2_power(0);
337 EXPORT_SYMBOL_GPL(tpa6130a2_stereo_enable
);
339 int tpa6130a2_add_controls(struct snd_soc_codec
*codec
)
341 struct tpa6130a2_data
*data
;
343 if (tpa6130a2_client
== NULL
)
346 data
= i2c_get_clientdata(tpa6130a2_client
);
348 if (data
->id
== TPA6140A2
)
349 return snd_soc_add_controls(codec
, tpa6140a2_controls
,
350 ARRAY_SIZE(tpa6140a2_controls
));
352 return snd_soc_add_controls(codec
, tpa6130a2_controls
,
353 ARRAY_SIZE(tpa6130a2_controls
));
355 EXPORT_SYMBOL_GPL(tpa6130a2_add_controls
);
357 static int __devinit
tpa6130a2_probe(struct i2c_client
*client
,
358 const struct i2c_device_id
*id
)
361 struct tpa6130a2_data
*data
;
362 struct tpa6130a2_platform_data
*pdata
;
363 const char *regulator
;
368 if (client
->dev
.platform_data
== NULL
) {
369 dev_err(dev
, "Platform data not set\n");
374 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
376 dev_err(dev
, "Can not allocate memory\n");
380 tpa6130a2_client
= client
;
382 i2c_set_clientdata(tpa6130a2_client
, data
);
384 pdata
= client
->dev
.platform_data
;
385 data
->power_gpio
= pdata
->power_gpio
;
386 data
->id
= pdata
->id
;
388 mutex_init(&data
->mutex
);
390 /* Set default register values */
391 data
->regs
[TPA6130A2_REG_CONTROL
] = TPA6130A2_SWS
;
392 data
->regs
[TPA6130A2_REG_VOL_MUTE
] = TPA6130A2_MUTE_R
|
395 if (data
->power_gpio
>= 0) {
396 ret
= gpio_request(data
->power_gpio
, "tpa6130a2 enable");
398 dev_err(dev
, "Failed to request power GPIO (%d)\n",
402 gpio_direction_output(data
->power_gpio
, 0);
407 dev_warn(dev
, "Unknown TPA model (%d). Assuming 6130A2\n",
417 data
->supply
= regulator_get(dev
, regulator
);
418 if (IS_ERR(data
->supply
)) {
419 ret
= PTR_ERR(data
->supply
);
420 dev_err(dev
, "Failed to request supply: %d\n", ret
);
424 ret
= tpa6130a2_power(1);
430 ret
= tpa6130a2_i2c_read(TPA6130A2_REG_VERSION
) &
431 TPA6130A2_VERSION_MASK
;
432 if ((ret
!= 1) && (ret
!= 2))
433 dev_warn(dev
, "UNTESTED version detected (%d)\n", ret
);
435 /* Disable the chip */
436 ret
= tpa6130a2_power(0);
443 regulator_put(data
->supply
);
445 if (data
->power_gpio
>= 0)
446 gpio_free(data
->power_gpio
);
449 i2c_set_clientdata(tpa6130a2_client
, NULL
);
450 tpa6130a2_client
= NULL
;
455 static int __devexit
tpa6130a2_remove(struct i2c_client
*client
)
457 struct tpa6130a2_data
*data
= i2c_get_clientdata(client
);
461 if (data
->power_gpio
>= 0)
462 gpio_free(data
->power_gpio
);
464 regulator_put(data
->supply
);
467 tpa6130a2_client
= NULL
;
472 static const struct i2c_device_id tpa6130a2_id
[] = {
476 MODULE_DEVICE_TABLE(i2c
, tpa6130a2_id
);
478 static struct i2c_driver tpa6130a2_i2c_driver
= {
481 .owner
= THIS_MODULE
,
483 .probe
= tpa6130a2_probe
,
484 .remove
= __devexit_p(tpa6130a2_remove
),
485 .id_table
= tpa6130a2_id
,
488 static int __init
tpa6130a2_init(void)
490 return i2c_add_driver(&tpa6130a2_i2c_driver
);
493 static void __exit
tpa6130a2_exit(void)
495 i2c_del_driver(&tpa6130a2_i2c_driver
);
498 MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
499 MODULE_DESCRIPTION("TPA6130A2 Headphone amplifier driver");
500 MODULE_LICENSE("GPL");
502 module_init(tpa6130a2_init
);
503 module_exit(tpa6130a2_exit
);