ALSA: hda - Don't set invalid connection index in Realtek initialiaiton
[linux-2.6/kvm.git] / sound / soc / codecs / tpa6130a2.c
blob958d49c969ac3d78696f2276f131d9b13ba71d9f
1 /*
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
20 * 02110-1301 USA
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] = {
40 "CPVSS",
41 "Vdd",
44 static const char *tpa6140a2_supply_names[TPA6130A2_NUM_SUPPLIES] = {
45 "HPVdd",
46 "AVdd",
49 /* This struct is used to save the context */
50 struct tpa6130a2_data {
51 struct mutex mutex;
52 unsigned char regs[TPA6130A2_CACHEREGNUM];
53 struct regulator_bulk_data supplies[TPA6130A2_NUM_SUPPLIES];
54 int power_gpio;
55 unsigned char power_state;
58 static int tpa6130a2_i2c_read(int reg)
60 struct tpa6130a2_data *data;
61 int val;
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);
69 if (val < 0)
70 dev_err(&tpa6130a2_client->dev, "Read failed\n");
71 else
72 data->regs[reg] = val;
73 } else {
74 val = data->regs[reg];
77 return val;
80 static int tpa6130a2_i2c_write(int reg, u8 value)
82 struct tpa6130a2_data *data;
83 int val = 0;
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);
90 if (val < 0)
91 dev_err(&tpa6130a2_client->dev, "Write failed\n");
94 /* Either powered on or off, we save the context */
95 data->regs[reg] = value;
97 return val;
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;
113 int i;
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;
125 u8 val;
126 int ret;
128 BUG_ON(tpa6130a2_client == NULL);
129 data = i2c_get_clientdata(tpa6130a2_client);
131 mutex_lock(&data->mutex);
132 if (power) {
133 /* Power on */
134 if (data->power_gpio >= 0)
135 gpio_set_value(data->power_gpio, 1);
137 ret = regulator_bulk_enable(ARRAY_SIZE(data->supplies),
138 data->supplies);
139 if (ret != 0) {
140 dev_err(&tpa6130a2_client->dev,
141 "Failed to enable supplies: %d\n", ret);
142 goto exit;
145 data->power_state = 1;
146 tpa6130a2_initialize();
148 /* Clear SWS */
149 val = tpa6130a2_read(TPA6130A2_REG_CONTROL);
150 val &= ~TPA6130A2_SWS;
151 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL, val);
152 } else {
153 /* set SWS */
154 val = tpa6130a2_read(TPA6130A2_REG_CONTROL);
155 val |= TPA6130A2_SWS;
156 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL, val);
158 /* Power off */
159 if (data->power_gpio >= 0)
160 gpio_set_value(data->power_gpio, 0);
162 ret = regulator_bulk_disable(ARRAY_SIZE(data->supplies),
163 data->supplies);
164 if (ret != 0) {
165 dev_err(&tpa6130a2_client->dev,
166 "Failed to disable supplies: %d\n", ret);
167 goto exit;
170 data->power_state = 0;
173 exit:
174 mutex_unlock(&data->mutex);
175 return ret;
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;
197 if (invert)
198 ucontrol->value.integer.value[0] =
199 mask - ucontrol->value.integer.value[0];
201 mutex_unlock(&data->mutex);
202 return 0;
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);
221 if (invert)
222 val = mask - val;
224 mutex_lock(&data->mutex);
226 val_reg = tpa6130a2_read(reg);
227 if (((val_reg >> shift) & mask) == val) {
228 mutex_unlock(&data->mutex);
229 return 0;
232 val_reg &= ~(mask << shift);
233 val_reg |= val << shift;
234 tpa6130a2_i2c_write(reg, val_reg);
236 mutex_unlock(&data->mutex);
238 return 1;
242 * TPA6130 volume. From -59.5 to 4 dB with increasing step size when going
243 * down in gain.
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,
263 tpa6130_tlv),
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
271 * in both registers.
273 static void tpa6130a2_channel_enable(u8 channel, int enable)
275 u8 val;
277 if (enable) {
278 /* Enable channel */
279 /* Enable amplifier */
280 val = tpa6130a2_read(TPA6130A2_REG_CONTROL);
281 val |= channel;
282 tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL, val);
284 /* Unmute channel */
285 val = tpa6130a2_read(TPA6130A2_REG_VOL_MUTE);
286 val &= ~channel;
287 tpa6130a2_i2c_write(TPA6130A2_REG_VOL_MUTE, val);
288 } else {
289 /* Disable channel */
290 /* Mute channel */
291 val = tpa6130a2_read(TPA6130A2_REG_VOL_MUTE);
292 val |= channel;
293 tpa6130a2_i2c_write(TPA6130A2_REG_VOL_MUTE, val);
295 /* Disable amplifier */
296 val = tpa6130a2_read(TPA6130A2_REG_CONTROL);
297 val &= ~channel;
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)
305 switch (event) {
306 case SND_SOC_DAPM_POST_PMU:
307 tpa6130a2_channel_enable(TPA6130A2_HP_EN_L, 1);
308 break;
309 case SND_SOC_DAPM_POST_PMD:
310 tpa6130a2_channel_enable(TPA6130A2_HP_EN_L, 0);
311 break;
313 return 0;
316 static int tpa6130a2_right_event(struct snd_soc_dapm_widget *w,
317 struct snd_kcontrol *kcontrol, int event)
319 switch (event) {
320 case SND_SOC_DAPM_POST_PMU:
321 tpa6130a2_channel_enable(TPA6130A2_HP_EN_R, 1);
322 break;
323 case SND_SOC_DAPM_POST_PMD:
324 tpa6130a2_channel_enable(TPA6130A2_HP_EN_R, 0);
325 break;
327 return 0;
330 static int tpa6130a2_supply_event(struct snd_soc_dapm_widget *w,
331 struct snd_kcontrol *kcontrol, int event)
333 int ret = 0;
335 switch (event) {
336 case SND_SOC_DAPM_POST_PMU:
337 ret = tpa6130a2_power(1);
338 break;
339 case SND_SOC_DAPM_POST_PMD:
340 ret = tpa6130a2_power(0);
341 break;
343 return ret;
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),
356 /* Outputs */
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)
385 struct device *dev;
386 struct tpa6130a2_data *data;
387 struct tpa6130a2_platform_data *pdata;
388 int i, ret;
390 dev = &client->dev;
392 if (client->dev.platform_data == NULL) {
393 dev_err(dev, "Platform data not set\n");
394 dump_stack();
395 return -ENODEV;
398 data = kzalloc(sizeof(*data), GFP_KERNEL);
399 if (data == NULL) {
400 dev_err(dev, "Can not allocate memory\n");
401 return -ENOMEM;
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 |
416 TPA6130A2_MUTE_L;
418 if (data->power_gpio >= 0) {
419 ret = gpio_request(data->power_gpio, "tpa6130a2 enable");
420 if (ret < 0) {
421 dev_err(dev, "Failed to request power GPIO (%d)\n",
422 data->power_gpio);
423 goto err_gpio;
425 gpio_direction_output(data->power_gpio, 0);
428 switch (pdata->id) {
429 case TPA6130A2:
430 for (i = 0; i < ARRAY_SIZE(data->supplies); i++)
431 data->supplies[i].supply = tpa6130a2_supply_names[i];
432 break;
433 case TPA6140A2:
434 for (i = 0; i < ARRAY_SIZE(data->supplies); i++)
435 data->supplies[i].supply = tpa6140a2_supply_names[i];;
436 break;
437 default:
438 dev_warn(dev, "Unknown TPA model (%d). Assuming 6130A2\n",
439 pdata->id);
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),
445 data->supplies);
446 if (ret != 0) {
447 dev_err(dev, "Failed to request supplies: %d\n", ret);
448 goto err_regulator;
451 ret = tpa6130a2_power(1);
452 if (ret != 0)
453 goto err_power;
456 /* Read version */
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);
464 if (ret != 0)
465 goto err_power;
467 return 0;
469 err_power:
470 regulator_bulk_free(ARRAY_SIZE(data->supplies), data->supplies);
471 err_regulator:
472 if (data->power_gpio >= 0)
473 gpio_free(data->power_gpio);
474 err_gpio:
475 kfree(data);
476 i2c_set_clientdata(tpa6130a2_client, NULL);
477 tpa6130a2_client = NULL;
479 return ret;
482 static int __devexit tpa6130a2_remove(struct i2c_client *client)
484 struct tpa6130a2_data *data = i2c_get_clientdata(client);
486 tpa6130a2_power(0);
488 if (data->power_gpio >= 0)
489 gpio_free(data->power_gpio);
491 regulator_bulk_free(ARRAY_SIZE(data->supplies), data->supplies);
493 kfree(data);
494 tpa6130a2_client = NULL;
496 return 0;
499 static const struct i2c_device_id tpa6130a2_id[] = {
500 { "tpa6130a2", 0 },
503 MODULE_DEVICE_TABLE(i2c, tpa6130a2_id);
505 static struct i2c_driver tpa6130a2_i2c_driver = {
506 .driver = {
507 .name = "tpa6130a2",
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);