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
;
59 static int tpa6130a2_i2c_read(int reg
)
61 struct tpa6130a2_data
*data
;
64 BUG_ON(tpa6130a2_client
== NULL
);
65 data
= i2c_get_clientdata(tpa6130a2_client
);
67 /* If powered off, return the cached value */
68 if (data
->power_state
) {
69 val
= i2c_smbus_read_byte_data(tpa6130a2_client
, reg
);
71 dev_err(&tpa6130a2_client
->dev
, "Read failed\n");
73 data
->regs
[reg
] = val
;
75 val
= data
->regs
[reg
];
81 static int tpa6130a2_i2c_write(int reg
, u8 value
)
83 struct tpa6130a2_data
*data
;
86 BUG_ON(tpa6130a2_client
== NULL
);
87 data
= i2c_get_clientdata(tpa6130a2_client
);
89 if (data
->power_state
) {
90 val
= i2c_smbus_write_byte_data(tpa6130a2_client
, reg
, value
);
92 dev_err(&tpa6130a2_client
->dev
, "Write failed\n");
95 /* Either powered on or off, we save the context */
96 data
->regs
[reg
] = value
;
101 static u8
tpa6130a2_read(int reg
)
103 struct tpa6130a2_data
*data
;
105 BUG_ON(tpa6130a2_client
== NULL
);
106 data
= i2c_get_clientdata(tpa6130a2_client
);
108 return data
->regs
[reg
];
111 static void tpa6130a2_initialize(void)
113 struct tpa6130a2_data
*data
;
116 BUG_ON(tpa6130a2_client
== NULL
);
117 data
= i2c_get_clientdata(tpa6130a2_client
);
119 for (i
= 1; i
< TPA6130A2_REG_VERSION
; i
++)
120 tpa6130a2_i2c_write(i
, data
->regs
[i
]);
123 static int tpa6130a2_power(int power
)
125 struct tpa6130a2_data
*data
;
129 BUG_ON(tpa6130a2_client
== NULL
);
130 data
= i2c_get_clientdata(tpa6130a2_client
);
132 mutex_lock(&data
->mutex
);
135 if (data
->power_gpio
>= 0)
136 gpio_set_value(data
->power_gpio
, 1);
138 ret
= regulator_bulk_enable(ARRAY_SIZE(data
->supplies
),
141 dev_err(&tpa6130a2_client
->dev
,
142 "Failed to enable supplies: %d\n", ret
);
146 data
->power_state
= 1;
147 tpa6130a2_initialize();
150 val
= tpa6130a2_read(TPA6130A2_REG_CONTROL
);
151 val
&= ~TPA6130A2_SWS
;
152 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL
, val
);
155 val
= tpa6130a2_read(TPA6130A2_REG_CONTROL
);
156 val
|= TPA6130A2_SWS
;
157 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL
, val
);
160 if (data
->power_gpio
>= 0)
161 gpio_set_value(data
->power_gpio
, 0);
163 ret
= regulator_bulk_disable(ARRAY_SIZE(data
->supplies
),
166 dev_err(&tpa6130a2_client
->dev
,
167 "Failed to disable supplies: %d\n", ret
);
171 data
->power_state
= 0;
175 mutex_unlock(&data
->mutex
);
179 static int tpa6130a2_get_reg(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
;
187 unsigned int mask
= mc
->max
;
188 unsigned int invert
= mc
->invert
;
190 BUG_ON(tpa6130a2_client
== NULL
);
191 data
= i2c_get_clientdata(tpa6130a2_client
);
193 mutex_lock(&data
->mutex
);
195 ucontrol
->value
.integer
.value
[0] =
196 (tpa6130a2_read(reg
) >> shift
) & mask
;
199 ucontrol
->value
.integer
.value
[0] =
200 mask
- ucontrol
->value
.integer
.value
[0];
202 mutex_unlock(&data
->mutex
);
206 static int tpa6130a2_set_reg(struct snd_kcontrol
*kcontrol
,
207 struct snd_ctl_elem_value
*ucontrol
)
209 struct soc_mixer_control
*mc
=
210 (struct soc_mixer_control
*)kcontrol
->private_value
;
211 struct tpa6130a2_data
*data
;
212 unsigned int reg
= mc
->reg
;
213 unsigned int shift
= mc
->shift
;
214 unsigned int mask
= mc
->max
;
215 unsigned int invert
= mc
->invert
;
216 unsigned int val
= (ucontrol
->value
.integer
.value
[0] & mask
);
217 unsigned int val_reg
;
219 BUG_ON(tpa6130a2_client
== NULL
);
220 data
= i2c_get_clientdata(tpa6130a2_client
);
225 mutex_lock(&data
->mutex
);
227 val_reg
= tpa6130a2_read(reg
);
228 if (((val_reg
>> shift
) & mask
) == val
) {
229 mutex_unlock(&data
->mutex
);
233 val_reg
&= ~(mask
<< shift
);
234 val_reg
|= val
<< shift
;
235 tpa6130a2_i2c_write(reg
, val_reg
);
237 mutex_unlock(&data
->mutex
);
243 * TPA6130 volume. From -59.5 to 4 dB with increasing step size when going
246 static const unsigned int tpa6130_tlv
[] = {
247 TLV_DB_RANGE_HEAD(10),
248 0, 1, TLV_DB_SCALE_ITEM(-5950, 600, 0),
249 2, 3, TLV_DB_SCALE_ITEM(-5000, 250, 0),
250 4, 5, TLV_DB_SCALE_ITEM(-4550, 160, 0),
251 6, 7, TLV_DB_SCALE_ITEM(-4140, 190, 0),
252 8, 9, TLV_DB_SCALE_ITEM(-3650, 120, 0),
253 10, 11, TLV_DB_SCALE_ITEM(-3330, 160, 0),
254 12, 13, TLV_DB_SCALE_ITEM(-3040, 180, 0),
255 14, 20, TLV_DB_SCALE_ITEM(-2710, 110, 0),
256 21, 37, TLV_DB_SCALE_ITEM(-1960, 74, 0),
257 38, 63, TLV_DB_SCALE_ITEM(-720, 45, 0),
260 static const struct snd_kcontrol_new tpa6130a2_controls
[] = {
261 SOC_SINGLE_EXT_TLV("TPA6130A2 Headphone Playback Volume",
262 TPA6130A2_REG_VOL_MUTE
, 0, 0x3f, 0,
263 tpa6130a2_get_reg
, tpa6130a2_set_reg
,
267 static const unsigned int tpa6140_tlv
[] = {
268 TLV_DB_RANGE_HEAD(3),
269 0, 8, TLV_DB_SCALE_ITEM(-5900, 400, 0),
270 9, 16, TLV_DB_SCALE_ITEM(-2500, 200, 0),
271 17, 31, TLV_DB_SCALE_ITEM(-1000, 100, 0),
274 static const struct snd_kcontrol_new tpa6140a2_controls
[] = {
275 SOC_SINGLE_EXT_TLV("TPA6140A2 Headphone Playback Volume",
276 TPA6130A2_REG_VOL_MUTE
, 1, 0x1f, 0,
277 tpa6130a2_get_reg
, tpa6130a2_set_reg
,
282 * Enable or disable channel (left or right)
283 * The bit number for mute and amplifier are the same per channel:
284 * bit 6: Right channel
285 * bit 7: Left channel
288 static void tpa6130a2_channel_enable(u8 channel
, int enable
)
294 /* Enable amplifier */
295 val
= tpa6130a2_read(TPA6130A2_REG_CONTROL
);
297 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL
, val
);
300 val
= tpa6130a2_read(TPA6130A2_REG_VOL_MUTE
);
302 tpa6130a2_i2c_write(TPA6130A2_REG_VOL_MUTE
, val
);
304 /* Disable channel */
306 val
= tpa6130a2_read(TPA6130A2_REG_VOL_MUTE
);
308 tpa6130a2_i2c_write(TPA6130A2_REG_VOL_MUTE
, val
);
310 /* Disable amplifier */
311 val
= tpa6130a2_read(TPA6130A2_REG_CONTROL
);
313 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL
, val
);
317 static int tpa6130a2_left_event(struct snd_soc_dapm_widget
*w
,
318 struct snd_kcontrol
*kcontrol
, int event
)
321 case SND_SOC_DAPM_POST_PMU
:
322 tpa6130a2_channel_enable(TPA6130A2_HP_EN_L
, 1);
324 case SND_SOC_DAPM_POST_PMD
:
325 tpa6130a2_channel_enable(TPA6130A2_HP_EN_L
, 0);
331 static int tpa6130a2_right_event(struct snd_soc_dapm_widget
*w
,
332 struct snd_kcontrol
*kcontrol
, int event
)
335 case SND_SOC_DAPM_POST_PMU
:
336 tpa6130a2_channel_enable(TPA6130A2_HP_EN_R
, 1);
338 case SND_SOC_DAPM_POST_PMD
:
339 tpa6130a2_channel_enable(TPA6130A2_HP_EN_R
, 0);
345 static int tpa6130a2_supply_event(struct snd_soc_dapm_widget
*w
,
346 struct snd_kcontrol
*kcontrol
, int event
)
351 case SND_SOC_DAPM_POST_PMU
:
352 ret
= tpa6130a2_power(1);
354 case SND_SOC_DAPM_POST_PMD
:
355 ret
= tpa6130a2_power(0);
361 static const struct snd_soc_dapm_widget tpa6130a2_dapm_widgets
[] = {
362 SND_SOC_DAPM_PGA_E("TPA6130A2 Left", SND_SOC_NOPM
,
363 0, 0, NULL
, 0, tpa6130a2_left_event
,
364 SND_SOC_DAPM_POST_PMU
|SND_SOC_DAPM_POST_PMD
),
365 SND_SOC_DAPM_PGA_E("TPA6130A2 Right", SND_SOC_NOPM
,
366 0, 0, NULL
, 0, tpa6130a2_right_event
,
367 SND_SOC_DAPM_POST_PMU
|SND_SOC_DAPM_POST_PMD
),
368 SND_SOC_DAPM_SUPPLY("TPA6130A2 Enable", SND_SOC_NOPM
,
369 0, 0, tpa6130a2_supply_event
,
370 SND_SOC_DAPM_POST_PMU
|SND_SOC_DAPM_POST_PMD
),
372 SND_SOC_DAPM_HP("TPA6130A2 Headphone Left", NULL
),
373 SND_SOC_DAPM_HP("TPA6130A2 Headphone Right", NULL
),
376 static const struct snd_soc_dapm_route audio_map
[] = {
377 {"TPA6130A2 Headphone Left", NULL
, "TPA6130A2 Left"},
378 {"TPA6130A2 Headphone Right", NULL
, "TPA6130A2 Right"},
380 {"TPA6130A2 Headphone Left", NULL
, "TPA6130A2 Enable"},
381 {"TPA6130A2 Headphone Right", NULL
, "TPA6130A2 Enable"},
384 int tpa6130a2_add_controls(struct snd_soc_codec
*codec
)
386 struct tpa6130a2_data
*data
;
388 BUG_ON(tpa6130a2_client
== NULL
);
389 data
= i2c_get_clientdata(tpa6130a2_client
);
391 snd_soc_dapm_new_controls(codec
, tpa6130a2_dapm_widgets
,
392 ARRAY_SIZE(tpa6130a2_dapm_widgets
));
394 snd_soc_dapm_add_routes(codec
, audio_map
, ARRAY_SIZE(audio_map
));
396 if (data
->id
== TPA6140A2
)
397 return snd_soc_add_controls(codec
, tpa6140a2_controls
,
398 ARRAY_SIZE(tpa6140a2_controls
));
400 return snd_soc_add_controls(codec
, tpa6130a2_controls
,
401 ARRAY_SIZE(tpa6130a2_controls
));
404 EXPORT_SYMBOL_GPL(tpa6130a2_add_controls
);
406 static int __devinit
tpa6130a2_probe(struct i2c_client
*client
,
407 const struct i2c_device_id
*id
)
410 struct tpa6130a2_data
*data
;
411 struct tpa6130a2_platform_data
*pdata
;
416 if (client
->dev
.platform_data
== NULL
) {
417 dev_err(dev
, "Platform data not set\n");
422 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
424 dev_err(dev
, "Can not allocate memory\n");
428 tpa6130a2_client
= client
;
430 i2c_set_clientdata(tpa6130a2_client
, data
);
432 pdata
= client
->dev
.platform_data
;
433 data
->power_gpio
= pdata
->power_gpio
;
434 data
->id
= pdata
->id
;
436 mutex_init(&data
->mutex
);
438 /* Set default register values */
439 data
->regs
[TPA6130A2_REG_CONTROL
] = TPA6130A2_SWS
;
440 data
->regs
[TPA6130A2_REG_VOL_MUTE
] = TPA6130A2_MUTE_R
|
443 if (data
->power_gpio
>= 0) {
444 ret
= gpio_request(data
->power_gpio
, "tpa6130a2 enable");
446 dev_err(dev
, "Failed to request power GPIO (%d)\n",
450 gpio_direction_output(data
->power_gpio
, 0);
455 for (i
= 0; i
< ARRAY_SIZE(data
->supplies
); i
++)
456 data
->supplies
[i
].supply
= tpa6130a2_supply_names
[i
];
459 for (i
= 0; i
< ARRAY_SIZE(data
->supplies
); i
++)
460 data
->supplies
[i
].supply
= tpa6140a2_supply_names
[i
];;
463 dev_warn(dev
, "Unknown TPA model (%d). Assuming 6130A2\n",
465 for (i
= 0; i
< ARRAY_SIZE(data
->supplies
); i
++)
466 data
->supplies
[i
].supply
= tpa6130a2_supply_names
[i
];
469 ret
= regulator_bulk_get(dev
, ARRAY_SIZE(data
->supplies
),
472 dev_err(dev
, "Failed to request supplies: %d\n", ret
);
476 ret
= tpa6130a2_power(1);
482 ret
= tpa6130a2_i2c_read(TPA6130A2_REG_VERSION
) &
483 TPA6130A2_VERSION_MASK
;
484 if ((ret
!= 1) && (ret
!= 2))
485 dev_warn(dev
, "UNTESTED version detected (%d)\n", ret
);
487 /* Disable the chip */
488 ret
= tpa6130a2_power(0);
495 regulator_bulk_free(ARRAY_SIZE(data
->supplies
), data
->supplies
);
497 if (data
->power_gpio
>= 0)
498 gpio_free(data
->power_gpio
);
501 i2c_set_clientdata(tpa6130a2_client
, NULL
);
502 tpa6130a2_client
= NULL
;
507 static int __devexit
tpa6130a2_remove(struct i2c_client
*client
)
509 struct tpa6130a2_data
*data
= i2c_get_clientdata(client
);
513 if (data
->power_gpio
>= 0)
514 gpio_free(data
->power_gpio
);
516 regulator_bulk_free(ARRAY_SIZE(data
->supplies
), data
->supplies
);
519 tpa6130a2_client
= NULL
;
524 static const struct i2c_device_id tpa6130a2_id
[] = {
528 MODULE_DEVICE_TABLE(i2c
, tpa6130a2_id
);
530 static struct i2c_driver tpa6130a2_i2c_driver
= {
533 .owner
= THIS_MODULE
,
535 .probe
= tpa6130a2_probe
,
536 .remove
= __devexit_p(tpa6130a2_remove
),
537 .id_table
= tpa6130a2_id
,
540 static int __init
tpa6130a2_init(void)
542 return i2c_add_driver(&tpa6130a2_i2c_driver
);
545 static void __exit
tpa6130a2_exit(void)
547 i2c_del_driver(&tpa6130a2_i2c_driver
);
550 MODULE_AUTHOR("Peter Ujfalusi");
551 MODULE_DESCRIPTION("TPA6130A2 Headphone amplifier driver");
552 MODULE_LICENSE("GPL");
554 module_init(tpa6130a2_init
);
555 module_exit(tpa6130a2_exit
);