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 <linux/slab.h>
30 #include <sound/tpa6130a2-plat.h>
31 #include <sound/soc.h>
32 #include <sound/soc-dapm.h>
33 #include <sound/tlv.h>
35 #include "tpa6130a2.h"
37 static struct i2c_client
*tpa6130a2_client
;
39 /* This struct is used to save the context */
40 struct tpa6130a2_data
{
42 unsigned char regs
[TPA6130A2_CACHEREGNUM
];
43 struct regulator
*supply
;
45 unsigned char power_state
;
49 static int tpa6130a2_i2c_read(int reg
)
51 struct tpa6130a2_data
*data
;
54 BUG_ON(tpa6130a2_client
== NULL
);
55 data
= i2c_get_clientdata(tpa6130a2_client
);
57 /* If powered off, return the cached value */
58 if (data
->power_state
) {
59 val
= i2c_smbus_read_byte_data(tpa6130a2_client
, reg
);
61 dev_err(&tpa6130a2_client
->dev
, "Read failed\n");
63 data
->regs
[reg
] = val
;
65 val
= data
->regs
[reg
];
71 static int tpa6130a2_i2c_write(int reg
, u8 value
)
73 struct tpa6130a2_data
*data
;
76 BUG_ON(tpa6130a2_client
== NULL
);
77 data
= i2c_get_clientdata(tpa6130a2_client
);
79 if (data
->power_state
) {
80 val
= i2c_smbus_write_byte_data(tpa6130a2_client
, reg
, value
);
82 dev_err(&tpa6130a2_client
->dev
, "Write failed\n");
85 /* Either powered on or off, we save the context */
86 data
->regs
[reg
] = value
;
91 static u8
tpa6130a2_read(int reg
)
93 struct tpa6130a2_data
*data
;
95 BUG_ON(tpa6130a2_client
== NULL
);
96 data
= i2c_get_clientdata(tpa6130a2_client
);
98 return data
->regs
[reg
];
101 static void tpa6130a2_initialize(void)
103 struct tpa6130a2_data
*data
;
106 BUG_ON(tpa6130a2_client
== NULL
);
107 data
= i2c_get_clientdata(tpa6130a2_client
);
109 for (i
= 1; i
< TPA6130A2_REG_VERSION
; i
++)
110 tpa6130a2_i2c_write(i
, data
->regs
[i
]);
113 static int tpa6130a2_power(int power
)
115 struct tpa6130a2_data
*data
;
119 BUG_ON(tpa6130a2_client
== NULL
);
120 data
= i2c_get_clientdata(tpa6130a2_client
);
122 mutex_lock(&data
->mutex
);
125 if (data
->power_gpio
>= 0)
126 gpio_set_value(data
->power_gpio
, 1);
128 ret
= regulator_enable(data
->supply
);
130 dev_err(&tpa6130a2_client
->dev
,
131 "Failed to enable supply: %d\n", ret
);
135 data
->power_state
= 1;
136 tpa6130a2_initialize();
139 val
= tpa6130a2_read(TPA6130A2_REG_CONTROL
);
140 val
&= ~TPA6130A2_SWS
;
141 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL
, val
);
144 val
= tpa6130a2_read(TPA6130A2_REG_CONTROL
);
145 val
|= TPA6130A2_SWS
;
146 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL
, val
);
149 if (data
->power_gpio
>= 0)
150 gpio_set_value(data
->power_gpio
, 0);
152 ret
= regulator_disable(data
->supply
);
154 dev_err(&tpa6130a2_client
->dev
,
155 "Failed to disable supply: %d\n", ret
);
159 data
->power_state
= 0;
163 mutex_unlock(&data
->mutex
);
167 static int tpa6130a2_get_volsw(struct snd_kcontrol
*kcontrol
,
168 struct snd_ctl_elem_value
*ucontrol
)
170 struct soc_mixer_control
*mc
=
171 (struct soc_mixer_control
*)kcontrol
->private_value
;
172 struct tpa6130a2_data
*data
;
173 unsigned int reg
= mc
->reg
;
174 unsigned int shift
= mc
->shift
;
176 unsigned int mask
= (1 << fls(max
)) - 1;
177 unsigned int invert
= mc
->invert
;
179 BUG_ON(tpa6130a2_client
== NULL
);
180 data
= i2c_get_clientdata(tpa6130a2_client
);
182 mutex_lock(&data
->mutex
);
184 ucontrol
->value
.integer
.value
[0] =
185 (tpa6130a2_read(reg
) >> shift
) & mask
;
188 ucontrol
->value
.integer
.value
[0] =
189 max
- ucontrol
->value
.integer
.value
[0];
191 mutex_unlock(&data
->mutex
);
195 static int tpa6130a2_put_volsw(struct snd_kcontrol
*kcontrol
,
196 struct snd_ctl_elem_value
*ucontrol
)
198 struct soc_mixer_control
*mc
=
199 (struct soc_mixer_control
*)kcontrol
->private_value
;
200 struct tpa6130a2_data
*data
;
201 unsigned int reg
= mc
->reg
;
202 unsigned int shift
= mc
->shift
;
204 unsigned int mask
= (1 << fls(max
)) - 1;
205 unsigned int invert
= mc
->invert
;
206 unsigned int val
= (ucontrol
->value
.integer
.value
[0] & mask
);
207 unsigned int val_reg
;
209 BUG_ON(tpa6130a2_client
== NULL
);
210 data
= i2c_get_clientdata(tpa6130a2_client
);
215 mutex_lock(&data
->mutex
);
217 val_reg
= tpa6130a2_read(reg
);
218 if (((val_reg
>> shift
) & mask
) == val
) {
219 mutex_unlock(&data
->mutex
);
223 val_reg
&= ~(mask
<< shift
);
224 val_reg
|= val
<< shift
;
225 tpa6130a2_i2c_write(reg
, val_reg
);
227 mutex_unlock(&data
->mutex
);
233 * TPA6130 volume. From -59.5 to 4 dB with increasing step size when going
236 static const unsigned int tpa6130_tlv
[] = {
237 TLV_DB_RANGE_HEAD(10),
238 0, 1, TLV_DB_SCALE_ITEM(-5950, 600, 0),
239 2, 3, TLV_DB_SCALE_ITEM(-5000, 250, 0),
240 4, 5, TLV_DB_SCALE_ITEM(-4550, 160, 0),
241 6, 7, TLV_DB_SCALE_ITEM(-4140, 190, 0),
242 8, 9, TLV_DB_SCALE_ITEM(-3650, 120, 0),
243 10, 11, TLV_DB_SCALE_ITEM(-3330, 160, 0),
244 12, 13, TLV_DB_SCALE_ITEM(-3040, 180, 0),
245 14, 20, TLV_DB_SCALE_ITEM(-2710, 110, 0),
246 21, 37, TLV_DB_SCALE_ITEM(-1960, 74, 0),
247 38, 63, TLV_DB_SCALE_ITEM(-720, 45, 0),
250 static const struct snd_kcontrol_new tpa6130a2_controls
[] = {
251 SOC_SINGLE_EXT_TLV("TPA6130A2 Headphone Playback Volume",
252 TPA6130A2_REG_VOL_MUTE
, 0, 0x3f, 0,
253 tpa6130a2_get_volsw
, tpa6130a2_put_volsw
,
257 static const unsigned int tpa6140_tlv
[] = {
258 TLV_DB_RANGE_HEAD(3),
259 0, 8, TLV_DB_SCALE_ITEM(-5900, 400, 0),
260 9, 16, TLV_DB_SCALE_ITEM(-2500, 200, 0),
261 17, 31, TLV_DB_SCALE_ITEM(-1000, 100, 0),
264 static const struct snd_kcontrol_new tpa6140a2_controls
[] = {
265 SOC_SINGLE_EXT_TLV("TPA6140A2 Headphone Playback Volume",
266 TPA6130A2_REG_VOL_MUTE
, 1, 0x1f, 0,
267 tpa6130a2_get_volsw
, tpa6130a2_put_volsw
,
272 * Enable or disable channel (left or right)
273 * The bit number for mute and amplifier are the same per channel:
274 * bit 6: Right channel
275 * bit 7: Left channel
278 static void tpa6130a2_channel_enable(u8 channel
, int enable
)
284 /* Enable amplifier */
285 val
= tpa6130a2_read(TPA6130A2_REG_CONTROL
);
287 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL
, val
);
290 val
= tpa6130a2_read(TPA6130A2_REG_VOL_MUTE
);
292 tpa6130a2_i2c_write(TPA6130A2_REG_VOL_MUTE
, val
);
294 /* Disable channel */
296 val
= tpa6130a2_read(TPA6130A2_REG_VOL_MUTE
);
298 tpa6130a2_i2c_write(TPA6130A2_REG_VOL_MUTE
, val
);
300 /* Disable amplifier */
301 val
= tpa6130a2_read(TPA6130A2_REG_CONTROL
);
303 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL
, val
);
307 static int tpa6130a2_left_event(struct snd_soc_dapm_widget
*w
,
308 struct snd_kcontrol
*kcontrol
, int event
)
311 case SND_SOC_DAPM_POST_PMU
:
312 tpa6130a2_channel_enable(TPA6130A2_HP_EN_L
, 1);
314 case SND_SOC_DAPM_POST_PMD
:
315 tpa6130a2_channel_enable(TPA6130A2_HP_EN_L
, 0);
321 static int tpa6130a2_right_event(struct snd_soc_dapm_widget
*w
,
322 struct snd_kcontrol
*kcontrol
, int event
)
325 case SND_SOC_DAPM_POST_PMU
:
326 tpa6130a2_channel_enable(TPA6130A2_HP_EN_R
, 1);
328 case SND_SOC_DAPM_POST_PMD
:
329 tpa6130a2_channel_enable(TPA6130A2_HP_EN_R
, 0);
335 static int tpa6130a2_supply_event(struct snd_soc_dapm_widget
*w
,
336 struct snd_kcontrol
*kcontrol
, int event
)
341 case SND_SOC_DAPM_POST_PMU
:
342 ret
= tpa6130a2_power(1);
344 case SND_SOC_DAPM_POST_PMD
:
345 ret
= tpa6130a2_power(0);
351 static const struct snd_soc_dapm_widget tpa6130a2_dapm_widgets
[] = {
352 SND_SOC_DAPM_PGA_E("TPA6130A2 Left", SND_SOC_NOPM
,
353 0, 0, NULL
, 0, tpa6130a2_left_event
,
354 SND_SOC_DAPM_POST_PMU
|SND_SOC_DAPM_POST_PMD
),
355 SND_SOC_DAPM_PGA_E("TPA6130A2 Right", SND_SOC_NOPM
,
356 0, 0, NULL
, 0, tpa6130a2_right_event
,
357 SND_SOC_DAPM_POST_PMU
|SND_SOC_DAPM_POST_PMD
),
358 SND_SOC_DAPM_SUPPLY("TPA6130A2 Enable", SND_SOC_NOPM
,
359 0, 0, tpa6130a2_supply_event
,
360 SND_SOC_DAPM_POST_PMU
|SND_SOC_DAPM_POST_PMD
),
362 SND_SOC_DAPM_OUTPUT("TPA6130A2 Headphone Left"),
363 SND_SOC_DAPM_OUTPUT("TPA6130A2 Headphone Right"),
366 static const struct snd_soc_dapm_route audio_map
[] = {
367 {"TPA6130A2 Headphone Left", NULL
, "TPA6130A2 Left"},
368 {"TPA6130A2 Headphone Right", NULL
, "TPA6130A2 Right"},
370 {"TPA6130A2 Headphone Left", NULL
, "TPA6130A2 Enable"},
371 {"TPA6130A2 Headphone Right", NULL
, "TPA6130A2 Enable"},
374 int tpa6130a2_add_controls(struct snd_soc_codec
*codec
)
376 struct tpa6130a2_data
*data
;
378 BUG_ON(tpa6130a2_client
== NULL
);
379 data
= i2c_get_clientdata(tpa6130a2_client
);
381 snd_soc_dapm_new_controls(codec
, tpa6130a2_dapm_widgets
,
382 ARRAY_SIZE(tpa6130a2_dapm_widgets
));
384 snd_soc_dapm_add_routes(codec
, audio_map
, ARRAY_SIZE(audio_map
));
386 if (data
->id
== TPA6140A2
)
387 return snd_soc_add_controls(codec
, tpa6140a2_controls
,
388 ARRAY_SIZE(tpa6140a2_controls
));
390 return snd_soc_add_controls(codec
, tpa6130a2_controls
,
391 ARRAY_SIZE(tpa6130a2_controls
));
394 EXPORT_SYMBOL_GPL(tpa6130a2_add_controls
);
396 static int __devinit
tpa6130a2_probe(struct i2c_client
*client
,
397 const struct i2c_device_id
*id
)
400 struct tpa6130a2_data
*data
;
401 struct tpa6130a2_platform_data
*pdata
;
402 const char *regulator
;
407 if (client
->dev
.platform_data
== NULL
) {
408 dev_err(dev
, "Platform data not set\n");
413 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
415 dev_err(dev
, "Can not allocate memory\n");
419 tpa6130a2_client
= client
;
421 i2c_set_clientdata(tpa6130a2_client
, data
);
423 pdata
= client
->dev
.platform_data
;
424 data
->power_gpio
= pdata
->power_gpio
;
425 data
->id
= pdata
->id
;
427 mutex_init(&data
->mutex
);
429 /* Set default register values */
430 data
->regs
[TPA6130A2_REG_CONTROL
] = TPA6130A2_SWS
;
431 data
->regs
[TPA6130A2_REG_VOL_MUTE
] = TPA6130A2_MUTE_R
|
434 if (data
->power_gpio
>= 0) {
435 ret
= gpio_request(data
->power_gpio
, "tpa6130a2 enable");
437 dev_err(dev
, "Failed to request power GPIO (%d)\n",
441 gpio_direction_output(data
->power_gpio
, 0);
446 dev_warn(dev
, "Unknown TPA model (%d). Assuming 6130A2\n",
456 data
->supply
= regulator_get(dev
, regulator
);
457 if (IS_ERR(data
->supply
)) {
458 ret
= PTR_ERR(data
->supply
);
459 dev_err(dev
, "Failed to request supply: %d\n", ret
);
463 ret
= tpa6130a2_power(1);
469 ret
= tpa6130a2_i2c_read(TPA6130A2_REG_VERSION
) &
470 TPA6130A2_VERSION_MASK
;
471 if ((ret
!= 1) && (ret
!= 2))
472 dev_warn(dev
, "UNTESTED version detected (%d)\n", ret
);
474 /* Disable the chip */
475 ret
= tpa6130a2_power(0);
482 regulator_put(data
->supply
);
484 if (data
->power_gpio
>= 0)
485 gpio_free(data
->power_gpio
);
488 i2c_set_clientdata(tpa6130a2_client
, NULL
);
489 tpa6130a2_client
= NULL
;
494 static int __devexit
tpa6130a2_remove(struct i2c_client
*client
)
496 struct tpa6130a2_data
*data
= i2c_get_clientdata(client
);
500 if (data
->power_gpio
>= 0)
501 gpio_free(data
->power_gpio
);
503 regulator_put(data
->supply
);
506 tpa6130a2_client
= NULL
;
511 static const struct i2c_device_id tpa6130a2_id
[] = {
515 MODULE_DEVICE_TABLE(i2c
, tpa6130a2_id
);
517 static struct i2c_driver tpa6130a2_i2c_driver
= {
520 .owner
= THIS_MODULE
,
522 .probe
= tpa6130a2_probe
,
523 .remove
= __devexit_p(tpa6130a2_remove
),
524 .id_table
= tpa6130a2_id
,
527 static int __init
tpa6130a2_init(void)
529 return i2c_add_driver(&tpa6130a2_i2c_driver
);
532 static void __exit
tpa6130a2_exit(void)
534 i2c_del_driver(&tpa6130a2_i2c_driver
);
537 MODULE_AUTHOR("Peter Ujfalusi");
538 MODULE_DESCRIPTION("TPA6130A2 Headphone amplifier driver");
539 MODULE_LICENSE("GPL");
541 module_init(tpa6130a2_init
);
542 module_exit(tpa6130a2_exit
);