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 <linux/of_gpio.h>
36 #include "tpa6130a2.h"
43 static struct i2c_client
*tpa6130a2_client
;
45 /* This struct is used to save the context */
46 struct tpa6130a2_data
{
48 unsigned char regs
[TPA6130A2_CACHEREGNUM
];
49 struct regulator
*supply
;
55 static int tpa6130a2_i2c_read(int reg
)
57 struct tpa6130a2_data
*data
;
60 if (WARN_ON(!tpa6130a2_client
))
62 data
= i2c_get_clientdata(tpa6130a2_client
);
64 /* If powered off, return the cached value */
65 if (data
->power_state
) {
66 val
= i2c_smbus_read_byte_data(tpa6130a2_client
, reg
);
68 dev_err(&tpa6130a2_client
->dev
, "Read failed\n");
70 data
->regs
[reg
] = val
;
72 val
= data
->regs
[reg
];
78 static int tpa6130a2_i2c_write(int reg
, u8 value
)
80 struct tpa6130a2_data
*data
;
83 if (WARN_ON(!tpa6130a2_client
))
85 data
= i2c_get_clientdata(tpa6130a2_client
);
87 if (data
->power_state
) {
88 val
= i2c_smbus_write_byte_data(tpa6130a2_client
, reg
, value
);
90 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 if (WARN_ON(!tpa6130a2_client
))
107 data
= i2c_get_clientdata(tpa6130a2_client
);
109 return data
->regs
[reg
];
112 static int tpa6130a2_initialize(void)
114 struct tpa6130a2_data
*data
;
117 if (WARN_ON(!tpa6130a2_client
))
119 data
= i2c_get_clientdata(tpa6130a2_client
);
121 for (i
= 1; i
< TPA6130A2_REG_VERSION
; i
++) {
122 ret
= tpa6130a2_i2c_write(i
, data
->regs
[i
]);
130 static int tpa6130a2_power(u8 power
)
132 struct tpa6130a2_data
*data
;
136 if (WARN_ON(!tpa6130a2_client
))
138 data
= i2c_get_clientdata(tpa6130a2_client
);
140 mutex_lock(&data
->mutex
);
141 if (power
== data
->power_state
)
145 ret
= regulator_enable(data
->supply
);
147 dev_err(&tpa6130a2_client
->dev
,
148 "Failed to enable supply: %d\n", ret
);
152 if (data
->power_gpio
>= 0)
153 gpio_set_value(data
->power_gpio
, 1);
155 data
->power_state
= 1;
156 ret
= tpa6130a2_initialize();
158 dev_err(&tpa6130a2_client
->dev
,
159 "Failed to initialize chip\n");
160 if (data
->power_gpio
>= 0)
161 gpio_set_value(data
->power_gpio
, 0);
162 regulator_disable(data
->supply
);
163 data
->power_state
= 0;
168 val
= tpa6130a2_read(TPA6130A2_REG_CONTROL
);
169 val
|= TPA6130A2_SWS
;
170 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL
, val
);
173 if (data
->power_gpio
>= 0)
174 gpio_set_value(data
->power_gpio
, 0);
176 ret
= regulator_disable(data
->supply
);
178 dev_err(&tpa6130a2_client
->dev
,
179 "Failed to disable supply: %d\n", ret
);
183 data
->power_state
= 0;
187 mutex_unlock(&data
->mutex
);
191 static int tpa6130a2_get_volsw(struct snd_kcontrol
*kcontrol
,
192 struct snd_ctl_elem_value
*ucontrol
)
194 struct soc_mixer_control
*mc
=
195 (struct soc_mixer_control
*)kcontrol
->private_value
;
196 struct tpa6130a2_data
*data
;
197 unsigned int reg
= mc
->reg
;
198 unsigned int shift
= mc
->shift
;
200 unsigned int mask
= (1 << fls(max
)) - 1;
201 unsigned int invert
= mc
->invert
;
203 if (WARN_ON(!tpa6130a2_client
))
205 data
= i2c_get_clientdata(tpa6130a2_client
);
207 mutex_lock(&data
->mutex
);
209 ucontrol
->value
.integer
.value
[0] =
210 (tpa6130a2_read(reg
) >> shift
) & mask
;
213 ucontrol
->value
.integer
.value
[0] =
214 max
- ucontrol
->value
.integer
.value
[0];
216 mutex_unlock(&data
->mutex
);
220 static int tpa6130a2_put_volsw(struct snd_kcontrol
*kcontrol
,
221 struct snd_ctl_elem_value
*ucontrol
)
223 struct soc_mixer_control
*mc
=
224 (struct soc_mixer_control
*)kcontrol
->private_value
;
225 struct tpa6130a2_data
*data
;
226 unsigned int reg
= mc
->reg
;
227 unsigned int shift
= mc
->shift
;
229 unsigned int mask
= (1 << fls(max
)) - 1;
230 unsigned int invert
= mc
->invert
;
231 unsigned int val
= (ucontrol
->value
.integer
.value
[0] & mask
);
232 unsigned int val_reg
;
234 if (WARN_ON(!tpa6130a2_client
))
236 data
= i2c_get_clientdata(tpa6130a2_client
);
241 mutex_lock(&data
->mutex
);
243 val_reg
= tpa6130a2_read(reg
);
244 if (((val_reg
>> shift
) & mask
) == val
) {
245 mutex_unlock(&data
->mutex
);
249 val_reg
&= ~(mask
<< shift
);
250 val_reg
|= val
<< shift
;
251 tpa6130a2_i2c_write(reg
, val_reg
);
253 mutex_unlock(&data
->mutex
);
259 * TPA6130 volume. From -59.5 to 4 dB with increasing step size when going
262 static const DECLARE_TLV_DB_RANGE(tpa6130_tlv
,
263 0, 1, TLV_DB_SCALE_ITEM(-5950, 600, 0),
264 2, 3, TLV_DB_SCALE_ITEM(-5000, 250, 0),
265 4, 5, TLV_DB_SCALE_ITEM(-4550, 160, 0),
266 6, 7, TLV_DB_SCALE_ITEM(-4140, 190, 0),
267 8, 9, TLV_DB_SCALE_ITEM(-3650, 120, 0),
268 10, 11, TLV_DB_SCALE_ITEM(-3330, 160, 0),
269 12, 13, TLV_DB_SCALE_ITEM(-3040, 180, 0),
270 14, 20, TLV_DB_SCALE_ITEM(-2710, 110, 0),
271 21, 37, TLV_DB_SCALE_ITEM(-1960, 74, 0),
272 38, 63, TLV_DB_SCALE_ITEM(-720, 45, 0)
275 static const struct snd_kcontrol_new tpa6130a2_controls
[] = {
276 SOC_SINGLE_EXT_TLV("TPA6130A2 Headphone Playback Volume",
277 TPA6130A2_REG_VOL_MUTE
, 0, 0x3f, 0,
278 tpa6130a2_get_volsw
, tpa6130a2_put_volsw
,
282 static const DECLARE_TLV_DB_RANGE(tpa6140_tlv
,
283 0, 8, TLV_DB_SCALE_ITEM(-5900, 400, 0),
284 9, 16, TLV_DB_SCALE_ITEM(-2500, 200, 0),
285 17, 31, TLV_DB_SCALE_ITEM(-1000, 100, 0)
288 static const struct snd_kcontrol_new tpa6140a2_controls
[] = {
289 SOC_SINGLE_EXT_TLV("TPA6140A2 Headphone Playback Volume",
290 TPA6130A2_REG_VOL_MUTE
, 1, 0x1f, 0,
291 tpa6130a2_get_volsw
, tpa6130a2_put_volsw
,
296 * Enable or disable channel (left or right)
297 * The bit number for mute and amplifier are the same per channel:
298 * bit 6: Right channel
299 * bit 7: Left channel
302 static void tpa6130a2_channel_enable(u8 channel
, int enable
)
308 /* Enable amplifier */
309 val
= tpa6130a2_read(TPA6130A2_REG_CONTROL
);
311 val
&= ~TPA6130A2_SWS
;
312 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL
, val
);
315 val
= tpa6130a2_read(TPA6130A2_REG_VOL_MUTE
);
317 tpa6130a2_i2c_write(TPA6130A2_REG_VOL_MUTE
, val
);
319 /* Disable channel */
321 val
= tpa6130a2_read(TPA6130A2_REG_VOL_MUTE
);
323 tpa6130a2_i2c_write(TPA6130A2_REG_VOL_MUTE
, val
);
325 /* Disable amplifier */
326 val
= tpa6130a2_read(TPA6130A2_REG_CONTROL
);
328 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL
, val
);
332 int tpa6130a2_stereo_enable(struct snd_soc_codec
*codec
, int enable
)
336 ret
= tpa6130a2_power(1);
339 tpa6130a2_channel_enable(TPA6130A2_HP_EN_R
| TPA6130A2_HP_EN_L
,
342 tpa6130a2_channel_enable(TPA6130A2_HP_EN_R
| TPA6130A2_HP_EN_L
,
344 ret
= tpa6130a2_power(0);
349 EXPORT_SYMBOL_GPL(tpa6130a2_stereo_enable
);
351 int tpa6130a2_add_controls(struct snd_soc_codec
*codec
)
353 struct tpa6130a2_data
*data
;
355 if (tpa6130a2_client
== NULL
)
358 data
= i2c_get_clientdata(tpa6130a2_client
);
360 if (data
->id
== TPA6140A2
)
361 return snd_soc_add_codec_controls(codec
, tpa6140a2_controls
,
362 ARRAY_SIZE(tpa6140a2_controls
));
364 return snd_soc_add_codec_controls(codec
, tpa6130a2_controls
,
365 ARRAY_SIZE(tpa6130a2_controls
));
367 EXPORT_SYMBOL_GPL(tpa6130a2_add_controls
);
369 static int tpa6130a2_probe(struct i2c_client
*client
,
370 const struct i2c_device_id
*id
)
373 struct tpa6130a2_data
*data
;
374 struct tpa6130a2_platform_data
*pdata
= client
->dev
.platform_data
;
375 struct device_node
*np
= client
->dev
.of_node
;
376 const char *regulator
;
381 data
= devm_kzalloc(&client
->dev
, sizeof(*data
), GFP_KERNEL
);
386 data
->power_gpio
= pdata
->power_gpio
;
388 data
->power_gpio
= of_get_named_gpio(np
, "power-gpio", 0);
390 dev_err(dev
, "Platform data not set\n");
395 tpa6130a2_client
= client
;
397 i2c_set_clientdata(tpa6130a2_client
, data
);
399 data
->id
= id
->driver_data
;
401 mutex_init(&data
->mutex
);
403 /* Set default register values */
404 data
->regs
[TPA6130A2_REG_CONTROL
] = TPA6130A2_SWS
;
405 data
->regs
[TPA6130A2_REG_VOL_MUTE
] = TPA6130A2_MUTE_R
|
408 if (data
->power_gpio
>= 0) {
409 ret
= devm_gpio_request(dev
, data
->power_gpio
,
412 dev_err(dev
, "Failed to request power GPIO (%d)\n",
416 gpio_direction_output(data
->power_gpio
, 0);
421 dev_warn(dev
, "Unknown TPA model (%d). Assuming 6130A2\n",
431 data
->supply
= devm_regulator_get(dev
, regulator
);
432 if (IS_ERR(data
->supply
)) {
433 ret
= PTR_ERR(data
->supply
);
434 dev_err(dev
, "Failed to request supply: %d\n", ret
);
438 ret
= tpa6130a2_power(1);
444 ret
= tpa6130a2_i2c_read(TPA6130A2_REG_VERSION
) &
445 TPA6130A2_VERSION_MASK
;
446 if ((ret
!= 1) && (ret
!= 2))
447 dev_warn(dev
, "UNTESTED version detected (%d)\n", ret
);
449 /* Disable the chip */
450 ret
= tpa6130a2_power(0);
457 tpa6130a2_client
= NULL
;
462 static int tpa6130a2_remove(struct i2c_client
*client
)
465 tpa6130a2_client
= NULL
;
470 static const struct i2c_device_id tpa6130a2_id
[] = {
471 { "tpa6130a2", TPA6130A2
},
472 { "tpa6140a2", TPA6140A2
},
475 MODULE_DEVICE_TABLE(i2c
, tpa6130a2_id
);
477 #if IS_ENABLED(CONFIG_OF)
478 static const struct of_device_id tpa6130a2_of_match
[] = {
479 { .compatible
= "ti,tpa6130a2", },
480 { .compatible
= "ti,tpa6140a2" },
483 MODULE_DEVICE_TABLE(of
, tpa6130a2_of_match
);
486 static struct i2c_driver tpa6130a2_i2c_driver
= {
489 .of_match_table
= of_match_ptr(tpa6130a2_of_match
),
491 .probe
= tpa6130a2_probe
,
492 .remove
= tpa6130a2_remove
,
493 .id_table
= tpa6130a2_id
,
496 module_i2c_driver(tpa6130a2_i2c_driver
);
498 MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
499 MODULE_DESCRIPTION("TPA6130A2 Headphone amplifier driver");
500 MODULE_LICENSE("GPL");