tpa6130a2.c 13.3 KB
Newer Older
P
Peter Ujfalusi 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 * ALSA SoC Texas Instruments TPA6130A2 headset stereo amplifier driver
 *
 * Copyright (C) Nokia Corporation
 *
 * Author: Peter Ujfalusi <peter.ujfalusi@nokia.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 */

#include <linux/module.h>
#include <linux/errno.h>
#include <linux/device.h>
#include <linux/i2c.h>
#include <linux/gpio.h>
28
#include <linux/regulator/consumer.h>
29
#include <linux/slab.h>
P
Peter Ujfalusi 已提交
30 31 32 33 34 35
#include <sound/tpa6130a2-plat.h>
#include <sound/soc.h>
#include <sound/tlv.h>

#include "tpa6130a2.h"

36
static struct i2c_client *tpa6130a2_client;
P
Peter Ujfalusi 已提交
37 38 39 40 41

/* This struct is used to save the context */
struct tpa6130a2_data {
	struct mutex mutex;
	unsigned char regs[TPA6130A2_CACHEREGNUM];
42
	struct regulator *supply;
P
Peter Ujfalusi 已提交
43 44
	int power_gpio;
	unsigned char power_state;
45
	enum tpa_model id;
P
Peter Ujfalusi 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
};

static int tpa6130a2_i2c_read(int reg)
{
	struct tpa6130a2_data *data;
	int val;

	BUG_ON(tpa6130a2_client == NULL);
	data = i2c_get_clientdata(tpa6130a2_client);

	/* If powered off, return the cached value */
	if (data->power_state) {
		val = i2c_smbus_read_byte_data(tpa6130a2_client, reg);
		if (val < 0)
			dev_err(&tpa6130a2_client->dev, "Read failed\n");
		else
			data->regs[reg] = val;
	} else {
		val = data->regs[reg];
	}

	return val;
}

static int tpa6130a2_i2c_write(int reg, u8 value)
{
	struct tpa6130a2_data *data;
	int val = 0;

	BUG_ON(tpa6130a2_client == NULL);
	data = i2c_get_clientdata(tpa6130a2_client);

	if (data->power_state) {
		val = i2c_smbus_write_byte_data(tpa6130a2_client, reg, value);
80
		if (val < 0) {
P
Peter Ujfalusi 已提交
81
			dev_err(&tpa6130a2_client->dev, "Write failed\n");
82 83
			return val;
		}
P
Peter Ujfalusi 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
	}

	/* Either powered on or off, we save the context */
	data->regs[reg] = value;

	return val;
}

static u8 tpa6130a2_read(int reg)
{
	struct tpa6130a2_data *data;

	BUG_ON(tpa6130a2_client == NULL);
	data = i2c_get_clientdata(tpa6130a2_client);

	return data->regs[reg];
}

102
static int tpa6130a2_initialize(void)
P
Peter Ujfalusi 已提交
103 104
{
	struct tpa6130a2_data *data;
105
	int i, ret = 0;
P
Peter Ujfalusi 已提交
106 107 108 109

	BUG_ON(tpa6130a2_client == NULL);
	data = i2c_get_clientdata(tpa6130a2_client);

110 111 112 113 114 115 116
	for (i = 1; i < TPA6130A2_REG_VERSION; i++) {
		ret = tpa6130a2_i2c_write(i, data->regs[i]);
		if (ret < 0)
			break;
	}

	return ret;
P
Peter Ujfalusi 已提交
117 118
}

119
static int tpa6130a2_power(int power)
P
Peter Ujfalusi 已提交
120 121 122
{
	struct	tpa6130a2_data *data;
	u8	val;
123
	int	ret = 0;
P
Peter Ujfalusi 已提交
124 125 126 127 128

	BUG_ON(tpa6130a2_client == NULL);
	data = i2c_get_clientdata(tpa6130a2_client);

	mutex_lock(&data->mutex);
129
	if (power && !data->power_state) {
130

131
		ret = regulator_enable(data->supply);
132 133
		if (ret != 0) {
			dev_err(&tpa6130a2_client->dev,
134
				"Failed to enable supply: %d\n", ret);
135
			goto exit;
P
Peter Ujfalusi 已提交
136
		}
137 138 139
		/* Power on */
		if (data->power_gpio >= 0)
			gpio_set_value(data->power_gpio, 1);
140 141

		data->power_state = 1;
142 143 144 145 146 147 148 149 150 151
		ret = tpa6130a2_initialize();
		if (ret < 0) {
			dev_err(&tpa6130a2_client->dev,
				"Failed to initialize chip\n");
			if (data->power_gpio >= 0)
				gpio_set_value(data->power_gpio, 0);
			regulator_disable(data->supply);
			data->power_state = 0;
			goto exit;
		}
152

P
Peter Ujfalusi 已提交
153 154 155 156
		/* Clear SWS */
		val = tpa6130a2_read(TPA6130A2_REG_CONTROL);
		val &= ~TPA6130A2_SWS;
		tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL, val);
157
	} else if (!power && data->power_state) {
P
Peter Ujfalusi 已提交
158 159 160 161
		/* set SWS */
		val = tpa6130a2_read(TPA6130A2_REG_CONTROL);
		val |= TPA6130A2_SWS;
		tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL, val);
162

P
Peter Ujfalusi 已提交
163
		/* Power off */
164
		if (data->power_gpio >= 0)
P
Peter Ujfalusi 已提交
165
			gpio_set_value(data->power_gpio, 0);
166

167
		ret = regulator_disable(data->supply);
168 169
		if (ret != 0) {
			dev_err(&tpa6130a2_client->dev,
170
				"Failed to disable supply: %d\n", ret);
171
			goto exit;
P
Peter Ujfalusi 已提交
172
		}
173 174

		data->power_state = 0;
P
Peter Ujfalusi 已提交
175
	}
176 177

exit:
P
Peter Ujfalusi 已提交
178
	mutex_unlock(&data->mutex);
179
	return ret;
P
Peter Ujfalusi 已提交
180 181
}

182
static int tpa6130a2_get_volsw(struct snd_kcontrol *kcontrol,
P
Peter Ujfalusi 已提交
183 184 185 186 187 188 189
		struct snd_ctl_elem_value *ucontrol)
{
	struct soc_mixer_control *mc =
		(struct soc_mixer_control *)kcontrol->private_value;
	struct tpa6130a2_data *data;
	unsigned int reg = mc->reg;
	unsigned int shift = mc->shift;
190 191
	int max = mc->max;
	unsigned int mask = (1 << fls(max)) - 1;
P
Peter Ujfalusi 已提交
192 193 194 195 196 197 198 199 200 201 202 203
	unsigned int invert = mc->invert;

	BUG_ON(tpa6130a2_client == NULL);
	data = i2c_get_clientdata(tpa6130a2_client);

	mutex_lock(&data->mutex);

	ucontrol->value.integer.value[0] =
		(tpa6130a2_read(reg) >> shift) & mask;

	if (invert)
		ucontrol->value.integer.value[0] =
204
			max - ucontrol->value.integer.value[0];
P
Peter Ujfalusi 已提交
205 206 207 208 209

	mutex_unlock(&data->mutex);
	return 0;
}

210
static int tpa6130a2_put_volsw(struct snd_kcontrol *kcontrol,
P
Peter Ujfalusi 已提交
211 212 213 214 215 216 217
		struct snd_ctl_elem_value *ucontrol)
{
	struct soc_mixer_control *mc =
		(struct soc_mixer_control *)kcontrol->private_value;
	struct tpa6130a2_data *data;
	unsigned int reg = mc->reg;
	unsigned int shift = mc->shift;
218 219
	int max = mc->max;
	unsigned int mask = (1 << fls(max)) - 1;
P
Peter Ujfalusi 已提交
220 221 222 223 224 225 226 227
	unsigned int invert = mc->invert;
	unsigned int val = (ucontrol->value.integer.value[0] & mask);
	unsigned int val_reg;

	BUG_ON(tpa6130a2_client == NULL);
	data = i2c_get_clientdata(tpa6130a2_client);

	if (invert)
228
		val = max - val;
P
Peter Ujfalusi 已提交
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267

	mutex_lock(&data->mutex);

	val_reg = tpa6130a2_read(reg);
	if (((val_reg >> shift) & mask) == val) {
		mutex_unlock(&data->mutex);
		return 0;
	}

	val_reg &= ~(mask << shift);
	val_reg |= val << shift;
	tpa6130a2_i2c_write(reg, val_reg);

	mutex_unlock(&data->mutex);

	return 1;
}

/*
 * TPA6130 volume. From -59.5 to 4 dB with increasing step size when going
 * down in gain.
 */
static const unsigned int tpa6130_tlv[] = {
	TLV_DB_RANGE_HEAD(10),
	0, 1, TLV_DB_SCALE_ITEM(-5950, 600, 0),
	2, 3, TLV_DB_SCALE_ITEM(-5000, 250, 0),
	4, 5, TLV_DB_SCALE_ITEM(-4550, 160, 0),
	6, 7, TLV_DB_SCALE_ITEM(-4140, 190, 0),
	8, 9, TLV_DB_SCALE_ITEM(-3650, 120, 0),
	10, 11, TLV_DB_SCALE_ITEM(-3330, 160, 0),
	12, 13, TLV_DB_SCALE_ITEM(-3040, 180, 0),
	14, 20, TLV_DB_SCALE_ITEM(-2710, 110, 0),
	21, 37, TLV_DB_SCALE_ITEM(-1960, 74, 0),
	38, 63, TLV_DB_SCALE_ITEM(-720, 45, 0),
};

static const struct snd_kcontrol_new tpa6130a2_controls[] = {
	SOC_SINGLE_EXT_TLV("TPA6130A2 Headphone Playback Volume",
		       TPA6130A2_REG_VOL_MUTE, 0, 0x3f, 0,
268
		       tpa6130a2_get_volsw, tpa6130a2_put_volsw,
P
Peter Ujfalusi 已提交
269 270 271
		       tpa6130_tlv),
};

272 273 274 275 276 277 278 279
static const unsigned int tpa6140_tlv[] = {
	TLV_DB_RANGE_HEAD(3),
	0, 8, TLV_DB_SCALE_ITEM(-5900, 400, 0),
	9, 16, TLV_DB_SCALE_ITEM(-2500, 200, 0),
	17, 31, TLV_DB_SCALE_ITEM(-1000, 100, 0),
};

static const struct snd_kcontrol_new tpa6140a2_controls[] = {
280 281
	SOC_SINGLE_EXT_TLV("TPA6140A2 Headphone Playback Volume",
		       TPA6130A2_REG_VOL_MUTE, 1, 0x1f, 0,
282
		       tpa6130a2_get_volsw, tpa6130a2_put_volsw,
283
		       tpa6140_tlv),
284 285
};

P
Peter Ujfalusi 已提交
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
/*
 * Enable or disable channel (left or right)
 * The bit number for mute and amplifier are the same per channel:
 * bit 6: Right channel
 * bit 7: Left channel
 * in both registers.
 */
static void tpa6130a2_channel_enable(u8 channel, int enable)
{
	u8	val;

	if (enable) {
		/* Enable channel */
		/* Enable amplifier */
		val = tpa6130a2_read(TPA6130A2_REG_CONTROL);
		val |= channel;
		tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL, val);

		/* Unmute channel */
		val = tpa6130a2_read(TPA6130A2_REG_VOL_MUTE);
		val &= ~channel;
		tpa6130a2_i2c_write(TPA6130A2_REG_VOL_MUTE, val);
	} else {
		/* Disable channel */
		/* Mute channel */
		val = tpa6130a2_read(TPA6130A2_REG_VOL_MUTE);
		val |= channel;
		tpa6130a2_i2c_write(TPA6130A2_REG_VOL_MUTE, val);

		/* Disable amplifier */
		val = tpa6130a2_read(TPA6130A2_REG_CONTROL);
		val &= ~channel;
		tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL, val);
	}
}

static int tpa6130a2_left_event(struct snd_soc_dapm_widget *w,
		struct snd_kcontrol *kcontrol, int event)
{
	switch (event) {
	case SND_SOC_DAPM_POST_PMU:
		tpa6130a2_channel_enable(TPA6130A2_HP_EN_L, 1);
		break;
	case SND_SOC_DAPM_POST_PMD:
		tpa6130a2_channel_enable(TPA6130A2_HP_EN_L, 0);
		break;
	}
	return 0;
}

static int tpa6130a2_right_event(struct snd_soc_dapm_widget *w,
		struct snd_kcontrol *kcontrol, int event)
{
	switch (event) {
	case SND_SOC_DAPM_POST_PMU:
		tpa6130a2_channel_enable(TPA6130A2_HP_EN_R, 1);
		break;
	case SND_SOC_DAPM_POST_PMD:
		tpa6130a2_channel_enable(TPA6130A2_HP_EN_R, 0);
		break;
	}
	return 0;
}

static int tpa6130a2_supply_event(struct snd_soc_dapm_widget *w,
		struct snd_kcontrol *kcontrol, int event)
{
353 354
	int ret = 0;

P
Peter Ujfalusi 已提交
355 356
	switch (event) {
	case SND_SOC_DAPM_POST_PMU:
357
		ret = tpa6130a2_power(1);
P
Peter Ujfalusi 已提交
358 359
		break;
	case SND_SOC_DAPM_POST_PMD:
360
		ret = tpa6130a2_power(0);
P
Peter Ujfalusi 已提交
361 362
		break;
	}
363
	return ret;
P
Peter Ujfalusi 已提交
364 365 366 367 368 369 370 371 372 373 374 375 376
}

static const struct snd_soc_dapm_widget tpa6130a2_dapm_widgets[] = {
	SND_SOC_DAPM_PGA_E("TPA6130A2 Left", SND_SOC_NOPM,
			0, 0, NULL, 0, tpa6130a2_left_event,
			SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD),
	SND_SOC_DAPM_PGA_E("TPA6130A2 Right", SND_SOC_NOPM,
			0, 0, NULL, 0, tpa6130a2_right_event,
			SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD),
	SND_SOC_DAPM_SUPPLY("TPA6130A2 Enable", SND_SOC_NOPM,
			0, 0, tpa6130a2_supply_event,
			SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD),
	/* Outputs */
377 378
	SND_SOC_DAPM_OUTPUT("TPA6130A2 Headphone Left"),
	SND_SOC_DAPM_OUTPUT("TPA6130A2 Headphone Right"),
P
Peter Ujfalusi 已提交
379 380 381 382 383 384 385 386 387 388 389 390
};

static const struct snd_soc_dapm_route audio_map[] = {
	{"TPA6130A2 Headphone Left", NULL, "TPA6130A2 Left"},
	{"TPA6130A2 Headphone Right", NULL, "TPA6130A2 Right"},

	{"TPA6130A2 Headphone Left", NULL, "TPA6130A2 Enable"},
	{"TPA6130A2 Headphone Right", NULL, "TPA6130A2 Enable"},
};

int tpa6130a2_add_controls(struct snd_soc_codec *codec)
{
391
	struct	tpa6130a2_data *data;
L
Liam Girdwood 已提交
392
	struct snd_soc_dapm_context *dapm = &codec->dapm;
393

394 395 396
	if (tpa6130a2_client == NULL)
		return -ENODEV;

397 398
	data = i2c_get_clientdata(tpa6130a2_client);

L
Liam Girdwood 已提交
399
	snd_soc_dapm_new_controls(dapm, tpa6130a2_dapm_widgets,
P
Peter Ujfalusi 已提交
400 401
				ARRAY_SIZE(tpa6130a2_dapm_widgets));

L
Liam Girdwood 已提交
402
	snd_soc_dapm_add_routes(dapm, audio_map, ARRAY_SIZE(audio_map));
P
Peter Ujfalusi 已提交
403

404 405 406 407 408 409
	if (data->id == TPA6140A2)
		return snd_soc_add_controls(codec, tpa6140a2_controls,
						ARRAY_SIZE(tpa6140a2_controls));
	else
		return snd_soc_add_controls(codec, tpa6130a2_controls,
						ARRAY_SIZE(tpa6130a2_controls));
P
Peter Ujfalusi 已提交
410 411 412 413

}
EXPORT_SYMBOL_GPL(tpa6130a2_add_controls);

414 415
static int __devinit tpa6130a2_probe(struct i2c_client *client,
				     const struct i2c_device_id *id)
P
Peter Ujfalusi 已提交
416 417 418 419
{
	struct device *dev;
	struct tpa6130a2_data *data;
	struct tpa6130a2_platform_data *pdata;
420 421
	const char *regulator;
	int ret;
P
Peter Ujfalusi 已提交
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440

	dev = &client->dev;

	if (client->dev.platform_data == NULL) {
		dev_err(dev, "Platform data not set\n");
		dump_stack();
		return -ENODEV;
	}

	data = kzalloc(sizeof(*data), GFP_KERNEL);
	if (data == NULL) {
		dev_err(dev, "Can not allocate memory\n");
		return -ENOMEM;
	}

	tpa6130a2_client = client;

	i2c_set_clientdata(tpa6130a2_client, data);

441
	pdata = client->dev.platform_data;
P
Peter Ujfalusi 已提交
442
	data->power_gpio = pdata->power_gpio;
443
	data->id = pdata->id;
P
Peter Ujfalusi 已提交
444 445 446 447 448 449 450 451 452 453 454 455 456

	mutex_init(&data->mutex);

	/* Set default register values */
	data->regs[TPA6130A2_REG_CONTROL] =	TPA6130A2_SWS;
	data->regs[TPA6130A2_REG_VOL_MUTE] =	TPA6130A2_MUTE_R |
						TPA6130A2_MUTE_L;

	if (data->power_gpio >= 0) {
		ret = gpio_request(data->power_gpio, "tpa6130a2 enable");
		if (ret < 0) {
			dev_err(dev, "Failed to request power GPIO (%d)\n",
				data->power_gpio);
457
			goto err_gpio;
P
Peter Ujfalusi 已提交
458 459 460 461
		}
		gpio_direction_output(data->power_gpio, 0);
	}

462
	switch (data->id) {
463 464 465
	default:
		dev_warn(dev, "Unknown TPA model (%d). Assuming 6130A2\n",
			 pdata->id);
466
	case TPA6130A2:
467
		regulator = "Vdd";
468 469
		break;
	case TPA6140A2:
470
		regulator = "AVdd";
471 472
		break;
	}
473

474 475 476 477
	data->supply = regulator_get(dev, regulator);
	if (IS_ERR(data->supply)) {
		ret = PTR_ERR(data->supply);
		dev_err(dev, "Failed to request supply: %d\n", ret);
478 479 480 481 482 483 484
		goto err_regulator;
	}

	ret = tpa6130a2_power(1);
	if (ret != 0)
		goto err_power;

P
Peter Ujfalusi 已提交
485 486 487 488 489 490 491 492

	/* Read version */
	ret = tpa6130a2_i2c_read(TPA6130A2_REG_VERSION) &
				 TPA6130A2_VERSION_MASK;
	if ((ret != 1) && (ret != 2))
		dev_warn(dev, "UNTESTED version detected (%d)\n", ret);

	/* Disable the chip */
493 494 495
	ret = tpa6130a2_power(0);
	if (ret != 0)
		goto err_power;
P
Peter Ujfalusi 已提交
496 497

	return 0;
498 499

err_power:
500
	regulator_put(data->supply);
501 502 503 504
err_regulator:
	if (data->power_gpio >= 0)
		gpio_free(data->power_gpio);
err_gpio:
P
Peter Ujfalusi 已提交
505 506
	kfree(data);
	i2c_set_clientdata(tpa6130a2_client, NULL);
507
	tpa6130a2_client = NULL;
P
Peter Ujfalusi 已提交
508 509 510 511

	return ret;
}

512
static int __devexit tpa6130a2_remove(struct i2c_client *client)
P
Peter Ujfalusi 已提交
513 514 515 516 517 518 519
{
	struct tpa6130a2_data *data = i2c_get_clientdata(client);

	tpa6130a2_power(0);

	if (data->power_gpio >= 0)
		gpio_free(data->power_gpio);
520

521
	regulator_put(data->supply);
522

P
Peter Ujfalusi 已提交
523
	kfree(data);
524
	tpa6130a2_client = NULL;
P
Peter Ujfalusi 已提交
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560

	return 0;
}

static const struct i2c_device_id tpa6130a2_id[] = {
	{ "tpa6130a2", 0 },
	{ }
};
MODULE_DEVICE_TABLE(i2c, tpa6130a2_id);

static struct i2c_driver tpa6130a2_i2c_driver = {
	.driver = {
		.name = "tpa6130a2",
		.owner = THIS_MODULE,
	},
	.probe = tpa6130a2_probe,
	.remove = __devexit_p(tpa6130a2_remove),
	.id_table = tpa6130a2_id,
};

static int __init tpa6130a2_init(void)
{
	return i2c_add_driver(&tpa6130a2_i2c_driver);
}

static void __exit tpa6130a2_exit(void)
{
	i2c_del_driver(&tpa6130a2_i2c_driver);
}

MODULE_AUTHOR("Peter Ujfalusi");
MODULE_DESCRIPTION("TPA6130A2 Headphone amplifier driver");
MODULE_LICENSE("GPL");

module_init(tpa6130a2_init);
module_exit(tpa6130a2_exit);