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 static const char *tpa6140a2_supply_names
[TPA6130A2_NUM_SUPPLIES
] = {
49 /* This struct is used to save the context */
50 struct tpa6130a2_data
{
52 unsigned char regs
[TPA6130A2_CACHEREGNUM
];
53 struct regulator_bulk_data supplies
[TPA6130A2_NUM_SUPPLIES
];
55 unsigned char power_state
;
58 static int tpa6130a2_i2c_read(int reg
)
60 struct tpa6130a2_data
*data
;
63 BUG_ON(tpa6130a2_client
== NULL
);
64 data
= i2c_get_clientdata(tpa6130a2_client
);
66 /* If powered off, return the cached value */
67 if (data
->power_state
) {
68 val
= i2c_smbus_read_byte_data(tpa6130a2_client
, reg
);
70 dev_err(&tpa6130a2_client
->dev
, "Read failed\n");
72 data
->regs
[reg
] = val
;
74 val
= data
->regs
[reg
];
80 static int tpa6130a2_i2c_write(int reg
, u8 value
)
82 struct tpa6130a2_data
*data
;
85 BUG_ON(tpa6130a2_client
== NULL
);
86 data
= i2c_get_clientdata(tpa6130a2_client
);
88 if (data
->power_state
) {
89 val
= i2c_smbus_write_byte_data(tpa6130a2_client
, reg
, value
);
91 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 BUG_ON(tpa6130a2_client
== NULL
);
105 data
= i2c_get_clientdata(tpa6130a2_client
);
107 return data
->regs
[reg
];
110 static void tpa6130a2_initialize(void)
112 struct tpa6130a2_data
*data
;
115 BUG_ON(tpa6130a2_client
== NULL
);
116 data
= i2c_get_clientdata(tpa6130a2_client
);
118 for (i
= 1; i
< TPA6130A2_REG_VERSION
; i
++)
119 tpa6130a2_i2c_write(i
, data
->regs
[i
]);
122 static int tpa6130a2_power(int power
)
124 struct tpa6130a2_data
*data
;
128 BUG_ON(tpa6130a2_client
== NULL
);
129 data
= i2c_get_clientdata(tpa6130a2_client
);
131 mutex_lock(&data
->mutex
);
134 if (data
->power_gpio
>= 0)
135 gpio_set_value(data
->power_gpio
, 1);
137 ret
= regulator_bulk_enable(ARRAY_SIZE(data
->supplies
),
140 dev_err(&tpa6130a2_client
->dev
,
141 "Failed to enable supplies: %d\n", ret
);
145 data
->power_state
= 1;
146 tpa6130a2_initialize();
149 val
= tpa6130a2_read(TPA6130A2_REG_CONTROL
);
150 val
&= ~TPA6130A2_SWS
;
151 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL
, val
);
154 val
= tpa6130a2_read(TPA6130A2_REG_CONTROL
);
155 val
|= TPA6130A2_SWS
;
156 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL
, val
);
159 if (data
->power_gpio
>= 0)
160 gpio_set_value(data
->power_gpio
, 0);
162 ret
= regulator_bulk_disable(ARRAY_SIZE(data
->supplies
),
165 dev_err(&tpa6130a2_client
->dev
,
166 "Failed to disable supplies: %d\n", ret
);
170 data
->power_state
= 0;
174 mutex_unlock(&data
->mutex
);
178 static int tpa6130a2_get_reg(struct snd_kcontrol
*kcontrol
,
179 struct snd_ctl_elem_value
*ucontrol
)
181 struct soc_mixer_control
*mc
=
182 (struct soc_mixer_control
*)kcontrol
->private_value
;
183 struct tpa6130a2_data
*data
;
184 unsigned int reg
= mc
->reg
;
185 unsigned int shift
= mc
->shift
;
186 unsigned int mask
= mc
->max
;
187 unsigned int invert
= mc
->invert
;
189 BUG_ON(tpa6130a2_client
== NULL
);
190 data
= i2c_get_clientdata(tpa6130a2_client
);
192 mutex_lock(&data
->mutex
);
194 ucontrol
->value
.integer
.value
[0] =
195 (tpa6130a2_read(reg
) >> shift
) & mask
;
198 ucontrol
->value
.integer
.value
[0] =
199 mask
- ucontrol
->value
.integer
.value
[0];
201 mutex_unlock(&data
->mutex
);
205 static int tpa6130a2_set_reg(struct snd_kcontrol
*kcontrol
,
206 struct snd_ctl_elem_value
*ucontrol
)
208 struct soc_mixer_control
*mc
=
209 (struct soc_mixer_control
*)kcontrol
->private_value
;
210 struct tpa6130a2_data
*data
;
211 unsigned int reg
= mc
->reg
;
212 unsigned int shift
= mc
->shift
;
213 unsigned int mask
= mc
->max
;
214 unsigned int invert
= mc
->invert
;
215 unsigned int val
= (ucontrol
->value
.integer
.value
[0] & mask
);
216 unsigned int val_reg
;
218 BUG_ON(tpa6130a2_client
== NULL
);
219 data
= i2c_get_clientdata(tpa6130a2_client
);
224 mutex_lock(&data
->mutex
);
226 val_reg
= tpa6130a2_read(reg
);
227 if (((val_reg
>> shift
) & mask
) == val
) {
228 mutex_unlock(&data
->mutex
);
232 val_reg
&= ~(mask
<< shift
);
233 val_reg
|= val
<< shift
;
234 tpa6130a2_i2c_write(reg
, val_reg
);
236 mutex_unlock(&data
->mutex
);
242 * TPA6130 volume. From -59.5 to 4 dB with increasing step size when going
245 static const unsigned int tpa6130_tlv
[] = {
246 TLV_DB_RANGE_HEAD(10),
247 0, 1, TLV_DB_SCALE_ITEM(-5950, 600, 0),
248 2, 3, TLV_DB_SCALE_ITEM(-5000, 250, 0),
249 4, 5, TLV_DB_SCALE_ITEM(-4550, 160, 0),
250 6, 7, TLV_DB_SCALE_ITEM(-4140, 190, 0),
251 8, 9, TLV_DB_SCALE_ITEM(-3650, 120, 0),
252 10, 11, TLV_DB_SCALE_ITEM(-3330, 160, 0),
253 12, 13, TLV_DB_SCALE_ITEM(-3040, 180, 0),
254 14, 20, TLV_DB_SCALE_ITEM(-2710, 110, 0),
255 21, 37, TLV_DB_SCALE_ITEM(-1960, 74, 0),
256 38, 63, TLV_DB_SCALE_ITEM(-720, 45, 0),
259 static const struct snd_kcontrol_new tpa6130a2_controls
[] = {
260 SOC_SINGLE_EXT_TLV("TPA6130A2 Headphone Playback Volume",
261 TPA6130A2_REG_VOL_MUTE
, 0, 0x3f, 0,
262 tpa6130a2_get_reg
, tpa6130a2_set_reg
,
267 * Enable or disable channel (left or right)
268 * The bit number for mute and amplifier are the same per channel:
269 * bit 6: Right channel
270 * bit 7: Left channel
273 static void tpa6130a2_channel_enable(u8 channel
, int enable
)
279 /* Enable amplifier */
280 val
= tpa6130a2_read(TPA6130A2_REG_CONTROL
);
282 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL
, val
);
285 val
= tpa6130a2_read(TPA6130A2_REG_VOL_MUTE
);
287 tpa6130a2_i2c_write(TPA6130A2_REG_VOL_MUTE
, val
);
289 /* Disable channel */
291 val
= tpa6130a2_read(TPA6130A2_REG_VOL_MUTE
);
293 tpa6130a2_i2c_write(TPA6130A2_REG_VOL_MUTE
, val
);
295 /* Disable amplifier */
296 val
= tpa6130a2_read(TPA6130A2_REG_CONTROL
);
298 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL
, val
);
302 static int tpa6130a2_left_event(struct snd_soc_dapm_widget
*w
,
303 struct snd_kcontrol
*kcontrol
, int event
)
306 case SND_SOC_DAPM_POST_PMU
:
307 tpa6130a2_channel_enable(TPA6130A2_HP_EN_L
, 1);
309 case SND_SOC_DAPM_POST_PMD
:
310 tpa6130a2_channel_enable(TPA6130A2_HP_EN_L
, 0);
316 static int tpa6130a2_right_event(struct snd_soc_dapm_widget
*w
,
317 struct snd_kcontrol
*kcontrol
, int event
)
320 case SND_SOC_DAPM_POST_PMU
:
321 tpa6130a2_channel_enable(TPA6130A2_HP_EN_R
, 1);
323 case SND_SOC_DAPM_POST_PMD
:
324 tpa6130a2_channel_enable(TPA6130A2_HP_EN_R
, 0);
330 static int tpa6130a2_supply_event(struct snd_soc_dapm_widget
*w
,
331 struct snd_kcontrol
*kcontrol
, int event
)
336 case SND_SOC_DAPM_POST_PMU
:
337 ret
= tpa6130a2_power(1);
339 case SND_SOC_DAPM_POST_PMD
:
340 ret
= tpa6130a2_power(0);
346 static const struct snd_soc_dapm_widget tpa6130a2_dapm_widgets
[] = {
347 SND_SOC_DAPM_PGA_E("TPA6130A2 Left", SND_SOC_NOPM
,
348 0, 0, NULL
, 0, tpa6130a2_left_event
,
349 SND_SOC_DAPM_POST_PMU
|SND_SOC_DAPM_POST_PMD
),
350 SND_SOC_DAPM_PGA_E("TPA6130A2 Right", SND_SOC_NOPM
,
351 0, 0, NULL
, 0, tpa6130a2_right_event
,
352 SND_SOC_DAPM_POST_PMU
|SND_SOC_DAPM_POST_PMD
),
353 SND_SOC_DAPM_SUPPLY("TPA6130A2 Enable", SND_SOC_NOPM
,
354 0, 0, tpa6130a2_supply_event
,
355 SND_SOC_DAPM_POST_PMU
|SND_SOC_DAPM_POST_PMD
),
357 SND_SOC_DAPM_HP("TPA6130A2 Headphone Left", NULL
),
358 SND_SOC_DAPM_HP("TPA6130A2 Headphone Right", NULL
),
361 static const struct snd_soc_dapm_route audio_map
[] = {
362 {"TPA6130A2 Headphone Left", NULL
, "TPA6130A2 Left"},
363 {"TPA6130A2 Headphone Right", NULL
, "TPA6130A2 Right"},
365 {"TPA6130A2 Headphone Left", NULL
, "TPA6130A2 Enable"},
366 {"TPA6130A2 Headphone Right", NULL
, "TPA6130A2 Enable"},
369 int tpa6130a2_add_controls(struct snd_soc_codec
*codec
)
371 snd_soc_dapm_new_controls(codec
, tpa6130a2_dapm_widgets
,
372 ARRAY_SIZE(tpa6130a2_dapm_widgets
));
374 snd_soc_dapm_add_routes(codec
, audio_map
, ARRAY_SIZE(audio_map
));
376 return snd_soc_add_controls(codec
, tpa6130a2_controls
,
377 ARRAY_SIZE(tpa6130a2_controls
));
380 EXPORT_SYMBOL_GPL(tpa6130a2_add_controls
);
382 static int __devinit
tpa6130a2_probe(struct i2c_client
*client
,
383 const struct i2c_device_id
*id
)
386 struct tpa6130a2_data
*data
;
387 struct tpa6130a2_platform_data
*pdata
;
392 if (client
->dev
.platform_data
== NULL
) {
393 dev_err(dev
, "Platform data not set\n");
398 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
400 dev_err(dev
, "Can not allocate memory\n");
404 tpa6130a2_client
= client
;
406 i2c_set_clientdata(tpa6130a2_client
, data
);
408 pdata
= client
->dev
.platform_data
;
409 data
->power_gpio
= pdata
->power_gpio
;
411 mutex_init(&data
->mutex
);
413 /* Set default register values */
414 data
->regs
[TPA6130A2_REG_CONTROL
] = TPA6130A2_SWS
;
415 data
->regs
[TPA6130A2_REG_VOL_MUTE
] = TPA6130A2_MUTE_R
|
418 if (data
->power_gpio
>= 0) {
419 ret
= gpio_request(data
->power_gpio
, "tpa6130a2 enable");
421 dev_err(dev
, "Failed to request power GPIO (%d)\n",
425 gpio_direction_output(data
->power_gpio
, 0);
430 for (i
= 0; i
< ARRAY_SIZE(data
->supplies
); i
++)
431 data
->supplies
[i
].supply
= tpa6130a2_supply_names
[i
];
434 for (i
= 0; i
< ARRAY_SIZE(data
->supplies
); i
++)
435 data
->supplies
[i
].supply
= tpa6140a2_supply_names
[i
];;
438 dev_warn(dev
, "Unknown TPA model (%d). Assuming 6130A2\n",
440 for (i
= 0; i
< ARRAY_SIZE(data
->supplies
); i
++)
441 data
->supplies
[i
].supply
= tpa6130a2_supply_names
[i
];
444 ret
= regulator_bulk_get(dev
, ARRAY_SIZE(data
->supplies
),
447 dev_err(dev
, "Failed to request supplies: %d\n", ret
);
451 ret
= tpa6130a2_power(1);
457 ret
= tpa6130a2_i2c_read(TPA6130A2_REG_VERSION
) &
458 TPA6130A2_VERSION_MASK
;
459 if ((ret
!= 1) && (ret
!= 2))
460 dev_warn(dev
, "UNTESTED version detected (%d)\n", ret
);
462 /* Disable the chip */
463 ret
= tpa6130a2_power(0);
470 regulator_bulk_free(ARRAY_SIZE(data
->supplies
), data
->supplies
);
472 if (data
->power_gpio
>= 0)
473 gpio_free(data
->power_gpio
);
476 i2c_set_clientdata(tpa6130a2_client
, NULL
);
477 tpa6130a2_client
= NULL
;
482 static int __devexit
tpa6130a2_remove(struct i2c_client
*client
)
484 struct tpa6130a2_data
*data
= i2c_get_clientdata(client
);
488 if (data
->power_gpio
>= 0)
489 gpio_free(data
->power_gpio
);
491 regulator_bulk_free(ARRAY_SIZE(data
->supplies
), data
->supplies
);
494 tpa6130a2_client
= NULL
;
499 static const struct i2c_device_id tpa6130a2_id
[] = {
503 MODULE_DEVICE_TABLE(i2c
, tpa6130a2_id
);
505 static struct i2c_driver tpa6130a2_i2c_driver
= {
508 .owner
= THIS_MODULE
,
510 .probe
= tpa6130a2_probe
,
511 .remove
= __devexit_p(tpa6130a2_remove
),
512 .id_table
= tpa6130a2_id
,
515 static int __init
tpa6130a2_init(void)
517 return i2c_add_driver(&tpa6130a2_i2c_driver
);
520 static void __exit
tpa6130a2_exit(void)
522 i2c_del_driver(&tpa6130a2_i2c_driver
);
525 MODULE_AUTHOR("Peter Ujfalusi");
526 MODULE_DESCRIPTION("TPA6130A2 Headphone amplifier driver");
527 MODULE_LICENSE("GPL");
529 module_init(tpa6130a2_init
);
530 module_exit(tpa6130a2_exit
);