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 <sound/tpa6130a2-plat.h>
29 #include <sound/soc.h>
30 #include <sound/soc-dapm.h>
31 #include <sound/tlv.h>
33 #include "tpa6130a2.h"
35 static struct i2c_client
*tpa6130a2_client
;
37 /* This struct is used to save the context */
38 struct tpa6130a2_data
{
40 unsigned char regs
[TPA6130A2_CACHEREGNUM
];
42 unsigned char power_state
;
45 static int tpa6130a2_i2c_read(int reg
)
47 struct tpa6130a2_data
*data
;
50 BUG_ON(tpa6130a2_client
== NULL
);
51 data
= i2c_get_clientdata(tpa6130a2_client
);
53 /* If powered off, return the cached value */
54 if (data
->power_state
) {
55 val
= i2c_smbus_read_byte_data(tpa6130a2_client
, reg
);
57 dev_err(&tpa6130a2_client
->dev
, "Read failed\n");
59 data
->regs
[reg
] = val
;
61 val
= data
->regs
[reg
];
67 static int tpa6130a2_i2c_write(int reg
, u8 value
)
69 struct tpa6130a2_data
*data
;
72 BUG_ON(tpa6130a2_client
== NULL
);
73 data
= i2c_get_clientdata(tpa6130a2_client
);
75 if (data
->power_state
) {
76 val
= i2c_smbus_write_byte_data(tpa6130a2_client
, reg
, value
);
78 dev_err(&tpa6130a2_client
->dev
, "Write failed\n");
81 /* Either powered on or off, we save the context */
82 data
->regs
[reg
] = value
;
87 static u8
tpa6130a2_read(int reg
)
89 struct tpa6130a2_data
*data
;
91 BUG_ON(tpa6130a2_client
== NULL
);
92 data
= i2c_get_clientdata(tpa6130a2_client
);
94 return data
->regs
[reg
];
97 static void tpa6130a2_initialize(void)
99 struct tpa6130a2_data
*data
;
102 BUG_ON(tpa6130a2_client
== NULL
);
103 data
= i2c_get_clientdata(tpa6130a2_client
);
105 for (i
= 1; i
< TPA6130A2_REG_VERSION
; i
++)
106 tpa6130a2_i2c_write(i
, data
->regs
[i
]);
109 static void tpa6130a2_power(int power
)
111 struct tpa6130a2_data
*data
;
114 BUG_ON(tpa6130a2_client
== NULL
);
115 data
= i2c_get_clientdata(tpa6130a2_client
);
117 mutex_lock(&data
->mutex
);
120 if (data
->power_gpio
>= 0) {
121 gpio_set_value(data
->power_gpio
, 1);
122 data
->power_state
= 1;
123 tpa6130a2_initialize();
126 val
= tpa6130a2_read(TPA6130A2_REG_CONTROL
);
127 val
&= ~TPA6130A2_SWS
;
128 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL
, val
);
131 val
= tpa6130a2_read(TPA6130A2_REG_CONTROL
);
132 val
|= TPA6130A2_SWS
;
133 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL
, val
);
135 if (data
->power_gpio
>= 0) {
136 gpio_set_value(data
->power_gpio
, 0);
137 data
->power_state
= 0;
140 mutex_unlock(&data
->mutex
);
143 static int tpa6130a2_get_reg(struct snd_kcontrol
*kcontrol
,
144 struct snd_ctl_elem_value
*ucontrol
)
146 struct soc_mixer_control
*mc
=
147 (struct soc_mixer_control
*)kcontrol
->private_value
;
148 struct tpa6130a2_data
*data
;
149 unsigned int reg
= mc
->reg
;
150 unsigned int shift
= mc
->shift
;
151 unsigned int mask
= mc
->max
;
152 unsigned int invert
= mc
->invert
;
154 BUG_ON(tpa6130a2_client
== NULL
);
155 data
= i2c_get_clientdata(tpa6130a2_client
);
157 mutex_lock(&data
->mutex
);
159 ucontrol
->value
.integer
.value
[0] =
160 (tpa6130a2_read(reg
) >> shift
) & mask
;
163 ucontrol
->value
.integer
.value
[0] =
164 mask
- ucontrol
->value
.integer
.value
[0];
166 mutex_unlock(&data
->mutex
);
170 static int tpa6130a2_set_reg(struct snd_kcontrol
*kcontrol
,
171 struct snd_ctl_elem_value
*ucontrol
)
173 struct soc_mixer_control
*mc
=
174 (struct soc_mixer_control
*)kcontrol
->private_value
;
175 struct tpa6130a2_data
*data
;
176 unsigned int reg
= mc
->reg
;
177 unsigned int shift
= mc
->shift
;
178 unsigned int mask
= mc
->max
;
179 unsigned int invert
= mc
->invert
;
180 unsigned int val
= (ucontrol
->value
.integer
.value
[0] & mask
);
181 unsigned int val_reg
;
183 BUG_ON(tpa6130a2_client
== NULL
);
184 data
= i2c_get_clientdata(tpa6130a2_client
);
189 mutex_lock(&data
->mutex
);
191 val_reg
= tpa6130a2_read(reg
);
192 if (((val_reg
>> shift
) & mask
) == val
) {
193 mutex_unlock(&data
->mutex
);
197 val_reg
&= ~(mask
<< shift
);
198 val_reg
|= val
<< shift
;
199 tpa6130a2_i2c_write(reg
, val_reg
);
201 mutex_unlock(&data
->mutex
);
207 * TPA6130 volume. From -59.5 to 4 dB with increasing step size when going
210 static const unsigned int tpa6130_tlv
[] = {
211 TLV_DB_RANGE_HEAD(10),
212 0, 1, TLV_DB_SCALE_ITEM(-5950, 600, 0),
213 2, 3, TLV_DB_SCALE_ITEM(-5000, 250, 0),
214 4, 5, TLV_DB_SCALE_ITEM(-4550, 160, 0),
215 6, 7, TLV_DB_SCALE_ITEM(-4140, 190, 0),
216 8, 9, TLV_DB_SCALE_ITEM(-3650, 120, 0),
217 10, 11, TLV_DB_SCALE_ITEM(-3330, 160, 0),
218 12, 13, TLV_DB_SCALE_ITEM(-3040, 180, 0),
219 14, 20, TLV_DB_SCALE_ITEM(-2710, 110, 0),
220 21, 37, TLV_DB_SCALE_ITEM(-1960, 74, 0),
221 38, 63, TLV_DB_SCALE_ITEM(-720, 45, 0),
224 static const struct snd_kcontrol_new tpa6130a2_controls
[] = {
225 SOC_SINGLE_EXT_TLV("TPA6130A2 Headphone Playback Volume",
226 TPA6130A2_REG_VOL_MUTE
, 0, 0x3f, 0,
227 tpa6130a2_get_reg
, tpa6130a2_set_reg
,
232 * Enable or disable channel (left or right)
233 * The bit number for mute and amplifier are the same per channel:
234 * bit 6: Right channel
235 * bit 7: Left channel
238 static void tpa6130a2_channel_enable(u8 channel
, int enable
)
240 struct tpa6130a2_data
*data
;
243 BUG_ON(tpa6130a2_client
== NULL
);
244 data
= i2c_get_clientdata(tpa6130a2_client
);
248 /* Enable amplifier */
249 val
= tpa6130a2_read(TPA6130A2_REG_CONTROL
);
251 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL
, val
);
254 val
= tpa6130a2_read(TPA6130A2_REG_VOL_MUTE
);
256 tpa6130a2_i2c_write(TPA6130A2_REG_VOL_MUTE
, val
);
258 /* Disable channel */
260 val
= tpa6130a2_read(TPA6130A2_REG_VOL_MUTE
);
262 tpa6130a2_i2c_write(TPA6130A2_REG_VOL_MUTE
, val
);
264 /* Disable amplifier */
265 val
= tpa6130a2_read(TPA6130A2_REG_CONTROL
);
267 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL
, val
);
271 static int tpa6130a2_left_event(struct snd_soc_dapm_widget
*w
,
272 struct snd_kcontrol
*kcontrol
, int event
)
275 case SND_SOC_DAPM_POST_PMU
:
276 tpa6130a2_channel_enable(TPA6130A2_HP_EN_L
, 1);
278 case SND_SOC_DAPM_POST_PMD
:
279 tpa6130a2_channel_enable(TPA6130A2_HP_EN_L
, 0);
285 static int tpa6130a2_right_event(struct snd_soc_dapm_widget
*w
,
286 struct snd_kcontrol
*kcontrol
, int event
)
289 case SND_SOC_DAPM_POST_PMU
:
290 tpa6130a2_channel_enable(TPA6130A2_HP_EN_R
, 1);
292 case SND_SOC_DAPM_POST_PMD
:
293 tpa6130a2_channel_enable(TPA6130A2_HP_EN_R
, 0);
299 static int tpa6130a2_supply_event(struct snd_soc_dapm_widget
*w
,
300 struct snd_kcontrol
*kcontrol
, int event
)
303 case SND_SOC_DAPM_POST_PMU
:
306 case SND_SOC_DAPM_POST_PMD
:
313 static const struct snd_soc_dapm_widget tpa6130a2_dapm_widgets
[] = {
314 SND_SOC_DAPM_PGA_E("TPA6130A2 Left", SND_SOC_NOPM
,
315 0, 0, NULL
, 0, tpa6130a2_left_event
,
316 SND_SOC_DAPM_POST_PMU
|SND_SOC_DAPM_POST_PMD
),
317 SND_SOC_DAPM_PGA_E("TPA6130A2 Right", SND_SOC_NOPM
,
318 0, 0, NULL
, 0, tpa6130a2_right_event
,
319 SND_SOC_DAPM_POST_PMU
|SND_SOC_DAPM_POST_PMD
),
320 SND_SOC_DAPM_SUPPLY("TPA6130A2 Enable", SND_SOC_NOPM
,
321 0, 0, tpa6130a2_supply_event
,
322 SND_SOC_DAPM_POST_PMU
|SND_SOC_DAPM_POST_PMD
),
324 SND_SOC_DAPM_HP("TPA6130A2 Headphone Left", NULL
),
325 SND_SOC_DAPM_HP("TPA6130A2 Headphone Right", NULL
),
328 static const struct snd_soc_dapm_route audio_map
[] = {
329 {"TPA6130A2 Headphone Left", NULL
, "TPA6130A2 Left"},
330 {"TPA6130A2 Headphone Right", NULL
, "TPA6130A2 Right"},
332 {"TPA6130A2 Headphone Left", NULL
, "TPA6130A2 Enable"},
333 {"TPA6130A2 Headphone Right", NULL
, "TPA6130A2 Enable"},
336 int tpa6130a2_add_controls(struct snd_soc_codec
*codec
)
338 snd_soc_dapm_new_controls(codec
, tpa6130a2_dapm_widgets
,
339 ARRAY_SIZE(tpa6130a2_dapm_widgets
));
341 snd_soc_dapm_add_routes(codec
, audio_map
, ARRAY_SIZE(audio_map
));
343 return snd_soc_add_controls(codec
, tpa6130a2_controls
,
344 ARRAY_SIZE(tpa6130a2_controls
));
347 EXPORT_SYMBOL_GPL(tpa6130a2_add_controls
);
349 static int tpa6130a2_probe(struct i2c_client
*client
,
350 const struct i2c_device_id
*id
)
353 struct tpa6130a2_data
*data
;
354 struct tpa6130a2_platform_data
*pdata
;
359 if (client
->dev
.platform_data
== NULL
) {
360 dev_err(dev
, "Platform data not set\n");
365 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
367 dev_err(dev
, "Can not allocate memory\n");
371 tpa6130a2_client
= client
;
373 i2c_set_clientdata(tpa6130a2_client
, data
);
375 pdata
= client
->dev
.platform_data
;
376 data
->power_gpio
= pdata
->power_gpio
;
378 mutex_init(&data
->mutex
);
380 /* Set default register values */
381 data
->regs
[TPA6130A2_REG_CONTROL
] = TPA6130A2_SWS
;
382 data
->regs
[TPA6130A2_REG_VOL_MUTE
] = TPA6130A2_MUTE_R
|
385 if (data
->power_gpio
>= 0) {
386 ret
= gpio_request(data
->power_gpio
, "tpa6130a2 enable");
388 dev_err(dev
, "Failed to request power GPIO (%d)\n",
392 gpio_direction_output(data
->power_gpio
, 0);
394 data
->power_state
= 1;
395 tpa6130a2_initialize();
401 ret
= tpa6130a2_i2c_read(TPA6130A2_REG_VERSION
) &
402 TPA6130A2_VERSION_MASK
;
403 if ((ret
!= 1) && (ret
!= 2))
404 dev_warn(dev
, "UNTESTED version detected (%d)\n", ret
);
406 /* Disable the chip */
412 i2c_set_clientdata(tpa6130a2_client
, NULL
);
413 tpa6130a2_client
= NULL
;
418 static int tpa6130a2_remove(struct i2c_client
*client
)
420 struct tpa6130a2_data
*data
= i2c_get_clientdata(client
);
424 if (data
->power_gpio
>= 0)
425 gpio_free(data
->power_gpio
);
427 tpa6130a2_client
= NULL
;
432 static const struct i2c_device_id tpa6130a2_id
[] = {
436 MODULE_DEVICE_TABLE(i2c
, tpa6130a2_id
);
438 static struct i2c_driver tpa6130a2_i2c_driver
= {
441 .owner
= THIS_MODULE
,
443 .probe
= tpa6130a2_probe
,
444 .remove
= __devexit_p(tpa6130a2_remove
),
445 .id_table
= tpa6130a2_id
,
448 static int __init
tpa6130a2_init(void)
450 return i2c_add_driver(&tpa6130a2_i2c_driver
);
453 static void __exit
tpa6130a2_exit(void)
455 i2c_del_driver(&tpa6130a2_i2c_driver
);
458 MODULE_AUTHOR("Peter Ujfalusi");
459 MODULE_DESCRIPTION("TPA6130A2 Headphone amplifier driver");
460 MODULE_LICENSE("GPL");
462 module_init(tpa6130a2_init
);
463 module_exit(tpa6130a2_exit
);