twl6040.c 40.4 KB
Newer Older
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 TWL6040 codec driver
 *
 * Author:	 Misael Lopez Cruz <x0052729@ti.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/moduleparam.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/pm.h>
#include <linux/platform_device.h>
28
#include <linux/slab.h>
29
#include <linux/i2c/twl.h>
30
#include <linux/mfd/twl6040.h>
31 32 33 34 35 36 37 38 39 40

#include <sound/core.h>
#include <sound/pcm.h>
#include <sound/pcm_params.h>
#include <sound/soc.h>
#include <sound/initval.h>
#include <sound/tlv.h>

#include "twl6040.h"

41
#define TWL6040_RATES		SNDRV_PCM_RATE_8000_96000
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
#define TWL6040_FORMATS	(SNDRV_PCM_FMTBIT_S32_LE)

#define TWL6040_OUTHS_0dB 0x00
#define TWL6040_OUTHS_M30dB 0x0F
#define TWL6040_OUTHF_0dB 0x03
#define TWL6040_OUTHF_M52dB 0x1D

#define TWL6040_RAMP_NONE	0
#define TWL6040_RAMP_UP		1
#define TWL6040_RAMP_DOWN	2

#define TWL6040_HSL_VOL_MASK	0x0F
#define TWL6040_HSL_VOL_SHIFT	0
#define TWL6040_HSR_VOL_MASK	0xF0
#define TWL6040_HSR_VOL_SHIFT	4
#define TWL6040_HF_VOL_MASK	0x1F
#define TWL6040_HF_VOL_SHIFT	0

60 61 62 63
/* Shadow register used by the driver */
#define TWL6040_REG_SW_SHADOW	0x2F
#define TWL6040_CACHEREGNUM	(TWL6040_REG_SW_SHADOW + 1)

64 65 66
/* TWL6040_REG_SW_SHADOW (0x2F) fields */
#define TWL6040_EAR_PATH_ENABLE	0x01

67 68 69 70 71 72 73 74
struct twl6040_output {
	u16 active;
	u16 left_vol;
	u16 right_vol;
	u16 left_step;
	u16 right_step;
	unsigned int step_delay;
	u16 ramp;
75
	struct delayed_work work;
76 77
	struct completion ramp_done;
};
78

79 80
struct twl6040_jack_data {
	struct snd_soc_jack *jack;
81
	struct delayed_work work;
82 83 84
	int report;
};

85 86
/* codec private data */
struct twl6040_data {
87
	int plug_irq;
88 89 90
	int codec_powered;
	int pll;
	int non_lp;
91
	int pll_power_mode;
92 93
	int hs_power_mode;
	int hs_power_mode_locked;
94
	unsigned int clk_in;
95
	unsigned int sysclk;
96 97 98 99
	u16 hs_left_step;
	u16 hs_right_step;
	u16 hf_left_step;
	u16 hf_right_step;
100 101 102 103
	struct twl6040_jack_data hs_jack;
	struct snd_soc_codec *codec;
	struct workqueue_struct *workqueue;
	struct mutex mutex;
104 105
	struct twl6040_output headset;
	struct twl6040_output handsfree;
106 107 108 109 110 111
};

/*
 * twl6040 register cache & default register settings
 */
static const u8 twl6040_reg[TWL6040_CACHEREGNUM] = {
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
	0x00, /* not used	0x00	*/
	0x4B, /* REG_ASICID	0x01 (ro) */
	0x00, /* REG_ASICREV	0x02 (ro) */
	0x00, /* REG_INTID	0x03	*/
	0x00, /* REG_INTMR	0x04	*/
	0x00, /* REG_NCPCTRL	0x05	*/
	0x00, /* REG_LDOCTL	0x06	*/
	0x60, /* REG_HPPLLCTL	0x07	*/
	0x00, /* REG_LPPLLCTL	0x08	*/
	0x4A, /* REG_LPPLLDIV	0x09	*/
	0x00, /* REG_AMICBCTL	0x0A	*/
	0x00, /* REG_DMICBCTL	0x0B	*/
	0x00, /* REG_MICLCTL	0x0C	*/
	0x00, /* REG_MICRCTL	0x0D	*/
	0x00, /* REG_MICGAIN	0x0E	*/
	0x1B, /* REG_LINEGAIN	0x0F	*/
	0x00, /* REG_HSLCTL	0x10	*/
	0x00, /* REG_HSRCTL	0x11	*/
	0x00, /* REG_HSGAIN	0x12	*/
	0x00, /* REG_EARCTL	0x13	*/
	0x00, /* REG_HFLCTL	0x14	*/
	0x00, /* REG_HFLGAIN	0x15	*/
	0x00, /* REG_HFRCTL	0x16	*/
	0x00, /* REG_HFRGAIN	0x17	*/
	0x00, /* REG_VIBCTLL	0x18	*/
	0x00, /* REG_VIBDATL	0x19	*/
	0x00, /* REG_VIBCTLR	0x1A	*/
	0x00, /* REG_VIBDATR	0x1B	*/
	0x00, /* REG_HKCTL1	0x1C	*/
	0x00, /* REG_HKCTL2	0x1D	*/
	0x00, /* REG_GPOCTL	0x1E	*/
	0x00, /* REG_ALB	0x1F	*/
	0x00, /* REG_DLB	0x20	*/
	0x00, /* not used	0x21	*/
	0x00, /* not used	0x22	*/
	0x00, /* not used	0x23	*/
	0x00, /* not used	0x24	*/
	0x00, /* not used	0x25	*/
	0x00, /* not used	0x26	*/
	0x00, /* not used	0x27	*/
	0x00, /* REG_TRIM1	0x28	*/
	0x00, /* REG_TRIM2	0x29	*/
	0x00, /* REG_TRIM3	0x2A	*/
	0x00, /* REG_HSOTRIM	0x2B	*/
	0x00, /* REG_HFOTRIM	0x2C	*/
	0x09, /* REG_ACCCTL	0x2D	*/
	0x00, /* REG_STATUS	0x2E (ro) */
159 160

	0x00, /* REG_SW_SHADOW	0x2F - Shadow, non HW register */
161 162
};

163 164
/* List of registers to be restored after power up */
static const int twl6040_restore_list[] = {
165 166 167 168 169 170 171 172 173 174 175 176 177 178
	TWL6040_REG_MICLCTL,
	TWL6040_REG_MICRCTL,
	TWL6040_REG_MICGAIN,
	TWL6040_REG_LINEGAIN,
	TWL6040_REG_HSLCTL,
	TWL6040_REG_HSRCTL,
	TWL6040_REG_HSGAIN,
	TWL6040_REG_EARCTL,
	TWL6040_REG_HFLCTL,
	TWL6040_REG_HFLGAIN,
	TWL6040_REG_HFRCTL,
	TWL6040_REG_HFRGAIN,
};

179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
/* set of rates for each pll: low-power and high-performance */
static unsigned int lp_rates[] = {
	8000,
	11250,
	16000,
	22500,
	32000,
	44100,
	48000,
	88200,
	96000,
};

static unsigned int hp_rates[] = {
	8000,
	16000,
	32000,
	48000,
	96000,
};

200 201 202
static struct snd_pcm_hw_constraint_list sysclk_constraints[] = {
	{ .count = ARRAY_SIZE(lp_rates), .list = lp_rates, },
	{ .count = ARRAY_SIZE(hp_rates), .list = hp_rates, },
203 204
};

205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
/*
 * read twl6040 register cache
 */
static inline unsigned int twl6040_read_reg_cache(struct snd_soc_codec *codec,
						unsigned int reg)
{
	u8 *cache = codec->reg_cache;

	if (reg >= TWL6040_CACHEREGNUM)
		return -EIO;

	return cache[reg];
}

/*
 * write twl6040 register cache
 */
static inline void twl6040_write_reg_cache(struct snd_soc_codec *codec,
						u8 reg, u8 value)
{
	u8 *cache = codec->reg_cache;

	if (reg >= TWL6040_CACHEREGNUM)
		return;
	cache[reg] = value;
}

/*
 * read from twl6040 hardware register
 */
static int twl6040_read_reg_volatile(struct snd_soc_codec *codec,
			unsigned int reg)
{
238
	struct twl6040 *twl6040 = codec->control_data;
239 240 241 242 243
	u8 value;

	if (reg >= TWL6040_CACHEREGNUM)
		return -EIO;

244 245 246 247 248 249
	if (likely(reg < TWL6040_REG_SW_SHADOW)) {
		value = twl6040_reg_read(twl6040, reg);
		twl6040_write_reg_cache(codec, reg, value);
	} else {
		value = twl6040_read_reg_cache(codec, reg);
	}
250 251 252 253 254 255 256 257 258 259

	return value;
}

/*
 * write to the twl6040 register space
 */
static int twl6040_write(struct snd_soc_codec *codec,
			unsigned int reg, unsigned int value)
{
260 261
	struct twl6040 *twl6040 = codec->control_data;

262 263 264 265
	if (reg >= TWL6040_CACHEREGNUM)
		return -EIO;

	twl6040_write_reg_cache(codec, reg, value);
266 267 268 269
	if (likely(reg < TWL6040_REG_SW_SHADOW))
		return twl6040_reg_write(twl6040, reg, value);
	else
		return 0;
270 271
}

272
static void twl6040_init_chip(struct snd_soc_codec *codec)
273
{
274 275 276
	struct twl6040 *twl6040 = codec->control_data;
	u8 val;

277
	/* Update reg_cache: ASICREV, and TRIM values */
278 279
	val = twl6040_get_revid(twl6040);
	twl6040_write_reg_cache(codec, TWL6040_REG_ASICREV, val);
280

281 282 283 284 285 286
	twl6040_read_reg_volatile(codec, TWL6040_REG_TRIM1);
	twl6040_read_reg_volatile(codec, TWL6040_REG_TRIM2);
	twl6040_read_reg_volatile(codec, TWL6040_REG_TRIM3);
	twl6040_read_reg_volatile(codec, TWL6040_REG_HSOTRIM);
	twl6040_read_reg_volatile(codec, TWL6040_REG_HFOTRIM);

287 288 289 290
	/* Change chip defaults */
	/* No imput selected for microphone amplifiers */
	twl6040_write_reg_cache(codec, TWL6040_REG_MICLCTL, 0x18);
	twl6040_write_reg_cache(codec, TWL6040_REG_MICRCTL, 0x18);
291 292 293 294 295 296 297 298 299 300 301

	/*
	 * We need to lower the default gain values, so the ramp code
	 * can work correctly for the first playback.
	 * This reduces the pop noise heard at the first playback.
	 */
	twl6040_write_reg_cache(codec, TWL6040_REG_HSGAIN, 0xff);
	twl6040_write_reg_cache(codec, TWL6040_REG_EARCTL, 0x1e);
	twl6040_write_reg_cache(codec, TWL6040_REG_HFLGAIN, 0x1d);
	twl6040_write_reg_cache(codec, TWL6040_REG_HFRGAIN, 0x1d);
	twl6040_write_reg_cache(codec, TWL6040_REG_LINEGAIN, 0);
302 303
}

304
static void twl6040_restore_regs(struct snd_soc_codec *codec)
305 306 307 308
{
	u8 *cache = codec->reg_cache;
	int reg, i;

309 310
	for (i = 0; i < ARRAY_SIZE(twl6040_restore_list); i++) {
		reg = twl6040_restore_list[i];
311 312 313 314
		twl6040_write(codec, reg, cache[reg]);
	}
}

315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
/*
 * Ramp HS PGA volume to minimise pops at stream startup and shutdown.
 */
static inline int twl6040_hs_ramp_step(struct snd_soc_codec *codec,
			unsigned int left_step, unsigned int right_step)
{

	struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
	struct twl6040_output *headset = &priv->headset;
	int left_complete = 0, right_complete = 0;
	u8 reg, val;

	/* left channel */
	left_step = (left_step > 0xF) ? 0xF : left_step;
	reg = twl6040_read_reg_cache(codec, TWL6040_REG_HSGAIN);
	val = (~reg & TWL6040_HSL_VOL_MASK);

	if (headset->ramp == TWL6040_RAMP_UP) {
		/* ramp step up */
		if (val < headset->left_vol) {
335 336 337 338 339
			if (val + left_step > headset->left_vol)
				val = headset->left_vol;
			else
				val += left_step;

340 341 342 343 344 345 346 347 348
			reg &= ~TWL6040_HSL_VOL_MASK;
			twl6040_write(codec, TWL6040_REG_HSGAIN,
					(reg | (~val & TWL6040_HSL_VOL_MASK)));
		} else {
			left_complete = 1;
		}
	} else if (headset->ramp == TWL6040_RAMP_DOWN) {
		/* ramp step down */
		if (val > 0x0) {
349 350 351 352 353
			if ((int)val - (int)left_step < 0)
				val = 0;
			else
				val -= left_step;

354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
			reg &= ~TWL6040_HSL_VOL_MASK;
			twl6040_write(codec, TWL6040_REG_HSGAIN, reg |
						(~val & TWL6040_HSL_VOL_MASK));
		} else {
			left_complete = 1;
		}
	}

	/* right channel */
	right_step = (right_step > 0xF) ? 0xF : right_step;
	reg = twl6040_read_reg_cache(codec, TWL6040_REG_HSGAIN);
	val = (~reg & TWL6040_HSR_VOL_MASK) >> TWL6040_HSR_VOL_SHIFT;

	if (headset->ramp == TWL6040_RAMP_UP) {
		/* ramp step up */
		if (val < headset->right_vol) {
370 371 372 373 374
			if (val + right_step > headset->right_vol)
				val = headset->right_vol;
			else
				val += right_step;

375 376 377 378 379 380 381 382 383
			reg &= ~TWL6040_HSR_VOL_MASK;
			twl6040_write(codec, TWL6040_REG_HSGAIN,
				(reg | (~val << TWL6040_HSR_VOL_SHIFT)));
		} else {
			right_complete = 1;
		}
	} else if (headset->ramp == TWL6040_RAMP_DOWN) {
		/* ramp step down */
		if (val > 0x0) {
384 385 386 387 388
			if ((int)val - (int)right_step < 0)
				val = 0;
			else
				val -= right_step;

389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
			reg &= ~TWL6040_HSR_VOL_MASK;
			twl6040_write(codec, TWL6040_REG_HSGAIN,
					 reg | (~val << TWL6040_HSR_VOL_SHIFT));
		} else {
			right_complete = 1;
		}
	}

	return left_complete & right_complete;
}

/*
 * Ramp HF PGA volume to minimise pops at stream startup and shutdown.
 */
static inline int twl6040_hf_ramp_step(struct snd_soc_codec *codec,
			unsigned int left_step, unsigned int right_step)
{
	struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
	struct twl6040_output *handsfree = &priv->handsfree;
	int left_complete = 0, right_complete = 0;
	u16 reg, val;

	/* left channel */
	left_step = (left_step > 0x1D) ? 0x1D : left_step;
	reg = twl6040_read_reg_cache(codec, TWL6040_REG_HFLGAIN);
	reg = 0x1D - reg;
	val = (reg & TWL6040_HF_VOL_MASK);
	if (handsfree->ramp == TWL6040_RAMP_UP) {
		/* ramp step up */
		if (val < handsfree->left_vol) {
419 420 421 422 423
			if (val + left_step > handsfree->left_vol)
				val = handsfree->left_vol;
			else
				val += left_step;

424 425 426 427 428 429 430 431 432
			reg &= ~TWL6040_HF_VOL_MASK;
			twl6040_write(codec, TWL6040_REG_HFLGAIN,
						reg | (0x1D - val));
		} else {
			left_complete = 1;
		}
	} else if (handsfree->ramp == TWL6040_RAMP_DOWN) {
		/* ramp step down */
		if (val > 0) {
433 434 435 436 437
			if ((int)val - (int)left_step < 0)
				val = 0;
			else
				val -= left_step;

438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
			reg &= ~TWL6040_HF_VOL_MASK;
			twl6040_write(codec, TWL6040_REG_HFLGAIN,
						reg | (0x1D - val));
		} else {
			left_complete = 1;
		}
	}

	/* right channel */
	right_step = (right_step > 0x1D) ? 0x1D : right_step;
	reg = twl6040_read_reg_cache(codec, TWL6040_REG_HFRGAIN);
	reg = 0x1D - reg;
	val = (reg & TWL6040_HF_VOL_MASK);
	if (handsfree->ramp == TWL6040_RAMP_UP) {
		/* ramp step up */
		if (val < handsfree->right_vol) {
454 455 456 457 458
			if (val + right_step > handsfree->right_vol)
				val = handsfree->right_vol;
			else
				val += right_step;

459 460 461 462 463 464 465 466 467
			reg &= ~TWL6040_HF_VOL_MASK;
			twl6040_write(codec, TWL6040_REG_HFRGAIN,
						reg | (0x1D - val));
		} else {
			right_complete = 1;
		}
	} else if (handsfree->ramp == TWL6040_RAMP_DOWN) {
		/* ramp step down */
		if (val > 0) {
468 469 470 471 472
			if ((int)val - (int)right_step < 0)
				val = 0;
			else
				val -= right_step;

473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
			reg &= ~TWL6040_HF_VOL_MASK;
			twl6040_write(codec, TWL6040_REG_HFRGAIN,
						reg | (0x1D - val));
		}
	}

	return left_complete & right_complete;
}

/*
 * This work ramps both output PGAs at stream start/stop time to
 * minimise pop associated with DAPM power switching.
 */
static void twl6040_pga_hs_work(struct work_struct *work)
{
	struct twl6040_data *priv =
489
		container_of(work, struct twl6040_data, headset.work.work);
490 491 492 493 494 495 496 497
	struct snd_soc_codec *codec = priv->codec;
	struct twl6040_output *headset = &priv->headset;
	int i, headset_complete;

	/* do we need to ramp at all ? */
	if (headset->ramp == TWL6040_RAMP_NONE)
		return;

498 499
	/* HS PGA gain range: 0x0 - 0xf (0 - 15) */
	for (i = 0; i < 16; i++) {
500 501 502
		headset_complete = twl6040_hs_ramp_step(codec,
						headset->left_step,
						headset->right_step);
503 504 505 506 507

		/* ramp finished ? */
		if (headset_complete)
			break;

508 509
		schedule_timeout_interruptible(
				msecs_to_jiffies(headset->step_delay));
510 511 512 513 514 515 516 517 518 519 520 521 522 523
	}

	if (headset->ramp == TWL6040_RAMP_DOWN) {
		headset->active = 0;
		complete(&headset->ramp_done);
	} else {
		headset->active = 1;
	}
	headset->ramp = TWL6040_RAMP_NONE;
}

static void twl6040_pga_hf_work(struct work_struct *work)
{
	struct twl6040_data *priv =
524
		container_of(work, struct twl6040_data, handsfree.work.work);
525 526 527 528 529 530 531 532
	struct snd_soc_codec *codec = priv->codec;
	struct twl6040_output *handsfree = &priv->handsfree;
	int i, handsfree_complete;

	/* do we need to ramp at all ? */
	if (handsfree->ramp == TWL6040_RAMP_NONE)
		return;

533 534 535
	/*
	 * HF PGA gain range: 0x00 - 0x1d (0 - 29) */
	for (i = 0; i < 30; i++) {
536 537 538
		handsfree_complete = twl6040_hf_ramp_step(codec,
						handsfree->left_step,
						handsfree->right_step);
539 540 541 542 543

		/* ramp finished ? */
		if (handsfree_complete)
			break;

544 545
		schedule_timeout_interruptible(
				msecs_to_jiffies(handsfree->step_delay));
546 547 548 549 550 551 552 553 554 555 556
	}


	if (handsfree->ramp == TWL6040_RAMP_DOWN) {
		handsfree->active = 0;
		complete(&handsfree->ramp_done);
	} else
		handsfree->active = 1;
	handsfree->ramp = TWL6040_RAMP_NONE;
}

557
static int out_drv_event(struct snd_soc_dapm_widget *w,
558 559 560 561 562 563 564 565
			struct snd_kcontrol *kcontrol, int event)
{
	struct snd_soc_codec *codec = w->codec;
	struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
	struct twl6040_output *out;
	struct delayed_work *work;

	switch (w->shift) {
566
	case 2: /* Headset output driver */
567
		out = &priv->headset;
568 569 570 571 572 573 574
		work = &out->work;
		/*
		 * Make sure, that we do not mess up variables for already
		 * executing work.
		 */
		cancel_delayed_work_sync(work);

575 576
		out->left_step = priv->hs_left_step;
		out->right_step = priv->hs_right_step;
577 578
		out->step_delay = 5;	/* 5 ms between volume ramp steps */
		break;
579
	case 4: /* Handsfree output driver */
580
		out = &priv->handsfree;
581 582 583 584 585 586 587
		work = &out->work;
		/*
		 * Make sure, that we do not mess up variables for already
		 * executing work.
		 */
		cancel_delayed_work_sync(work);

588 589
		out->left_step = priv->hf_left_step;
		out->right_step = priv->hf_right_step;
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
		out->step_delay = 5;	/* 5 ms between volume ramp steps */
		if (SND_SOC_DAPM_EVENT_ON(event))
			priv->non_lp++;
		else
			priv->non_lp--;
		break;
	default:
		return -1;
	}

	switch (event) {
	case SND_SOC_DAPM_POST_PMU:
		if (out->active)
			break;

		/* don't use volume ramp for power-up */
606
		out->ramp = TWL6040_RAMP_UP;
607 608 609
		out->left_step = out->left_vol;
		out->right_step = out->right_vol;

610
		queue_delayed_work(priv->workqueue, work, msecs_to_jiffies(1));
611 612 613 614 615 616
		break;

	case SND_SOC_DAPM_PRE_PMD:
		if (!out->active)
			break;

617 618 619
		/* use volume ramp for power-down */
		out->ramp = TWL6040_RAMP_DOWN;
		INIT_COMPLETION(out->ramp_done);
620

621
		queue_delayed_work(priv->workqueue, work, msecs_to_jiffies(1));
622

623 624
		wait_for_completion_timeout(&out->ramp_done,
					    msecs_to_jiffies(2000));
625 626 627 628 629 630
		break;
	}

	return 0;
}

631 632 633 634
/* set headset dac and driver power mode */
static int headset_power_mode(struct snd_soc_codec *codec, int high_perf)
{
	int hslctl, hsrctl;
635
	int mask = TWL6040_HSDRVMODE | TWL6040_HSDACMODE;
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653

	hslctl = twl6040_read_reg_cache(codec, TWL6040_REG_HSLCTL);
	hsrctl = twl6040_read_reg_cache(codec, TWL6040_REG_HSRCTL);

	if (high_perf) {
		hslctl &= ~mask;
		hsrctl &= ~mask;
	} else {
		hslctl |= mask;
		hsrctl |= mask;
	}

	twl6040_write(codec, TWL6040_REG_HSLCTL, hslctl);
	twl6040_write(codec, TWL6040_REG_HSRCTL, hsrctl);

	return 0;
}

654 655 656 657 658 659 660
static int twl6040_hs_dac_event(struct snd_soc_dapm_widget *w,
			struct snd_kcontrol *kcontrol, int event)
{
	msleep(1);
	return 0;
}

661 662 663 664
static int twl6040_power_mode_event(struct snd_soc_dapm_widget *w,
			struct snd_kcontrol *kcontrol, int event)
{
	struct snd_soc_codec *codec = w->codec;
665
	struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
666
	int ret = 0;
667

668
	if (SND_SOC_DAPM_EVENT_ON(event)) {
669
		priv->non_lp++;
670 671 672 673 674 675
		if (!strcmp(w->name, "Earphone Driver")) {
			/* Earphone doesn't support low power mode */
			priv->hs_power_mode_locked = 1;
			ret = headset_power_mode(codec, 1);
		}
	} else {
676
		priv->non_lp--;
677 678 679 680 681
		if (!strcmp(w->name, "Earphone Driver")) {
			priv->hs_power_mode_locked = 0;
			ret = headset_power_mode(codec, priv->hs_power_mode);
		}
	}
682

683 684
	msleep(1);

685
	return ret;
686 687
}

688 689
static void twl6040_hs_jack_report(struct snd_soc_codec *codec,
				   struct snd_soc_jack *jack, int report)
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
{
	struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
	int status;

	mutex_lock(&priv->mutex);

	/* Sync status */
	status = twl6040_read_reg_volatile(codec, TWL6040_REG_STATUS);
	if (status & TWL6040_PLUGCOMP)
		snd_soc_jack_report(jack, report, report);
	else
		snd_soc_jack_report(jack, 0, report);

	mutex_unlock(&priv->mutex);
}

void twl6040_hs_jack_detect(struct snd_soc_codec *codec,
				struct snd_soc_jack *jack, int report)
{
	struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
	struct twl6040_jack_data *hs_jack = &priv->hs_jack;

	hs_jack->jack = jack;
	hs_jack->report = report;

	twl6040_hs_jack_report(codec, hs_jack->jack, hs_jack->report);
}
EXPORT_SYMBOL_GPL(twl6040_hs_jack_detect);

static void twl6040_accessory_work(struct work_struct *work)
{
	struct twl6040_data *priv = container_of(work,
722
					struct twl6040_data, hs_jack.work.work);
723 724 725 726 727 728
	struct snd_soc_codec *codec = priv->codec;
	struct twl6040_jack_data *hs_jack = &priv->hs_jack;

	twl6040_hs_jack_report(codec, hs_jack->jack, hs_jack->report);
}

729
/* audio interrupt handler */
730
static irqreturn_t twl6040_audio_handler(int irq, void *data)
731 732
{
	struct snd_soc_codec *codec = data;
733
	struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
734

735
	queue_delayed_work(priv->workqueue, &priv->hs_jack.work,
736
			   msecs_to_jiffies(200));
737

738 739 740
	return IRQ_HANDLED;
}

741 742 743 744 745 746 747 748 749 750 751 752 753
static int twl6040_put_volsw(struct snd_kcontrol *kcontrol,
				  struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
	struct twl6040_data *twl6040_priv = snd_soc_codec_get_drvdata(codec);
	struct twl6040_output *out = NULL;
	struct soc_mixer_control *mc =
		(struct soc_mixer_control *)kcontrol->private_value;
	int ret;

	/* For HS and HF we shadow the values and only actually write
	 * them out when active in order to ensure the amplifier comes on
	 * as quietly as possible. */
754
	switch (mc->reg) {
755 756 757
	case TWL6040_REG_HSGAIN:
		out = &twl6040_priv->headset;
		break;
758 759 760
	case TWL6040_REG_HFLGAIN:
		out = &twl6040_priv->handsfree;
		break;
761
	default:
762
		return -EINVAL;
763 764
	}

765 766 767 768
	out->left_vol = ucontrol->value.integer.value[0];
	out->right_vol = ucontrol->value.integer.value[1];
	if (!out->active)
		return 1;
769

770 771 772 773 774 775
	/* call the appropriate handler depending on the rreg */
	if (mc->rreg)
		ret = snd_soc_put_volsw_2r(kcontrol, ucontrol);
	else
		ret = snd_soc_put_volsw(kcontrol, ucontrol);

776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
	if (ret < 0)
		return ret;

	return 1;
}

static int twl6040_get_volsw(struct snd_kcontrol *kcontrol,
				struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
	struct twl6040_data *twl6040_priv = snd_soc_codec_get_drvdata(codec);
	struct twl6040_output *out = &twl6040_priv->headset;
	struct soc_mixer_control *mc =
		(struct soc_mixer_control *)kcontrol->private_value;

791
	switch (mc->reg) {
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
	case TWL6040_REG_HSGAIN:
		out = &twl6040_priv->headset;
		break;
	case TWL6040_REG_HFLGAIN:
		out = &twl6040_priv->handsfree;
		break;
	default:
		break;
	}

	if (out) {
		ucontrol->value.integer.value[0] = out->left_vol;
		ucontrol->value.integer.value[1] = out->right_vol;
		return 0;
	}

808 809 810 811 812
	/* call the appropriate handler depending on the rreg */
	if (mc->rreg)
		return snd_soc_get_volsw_2r(kcontrol, ucontrol);
	else
		return snd_soc_get_volsw(kcontrol, ucontrol);
813 814
}

815 816 817 818 819 820 821 822
/*
 * MICATT volume control:
 * from -6 to 0 dB in 6 dB steps
 */
static DECLARE_TLV_DB_SCALE(mic_preamp_tlv, -600, 600, 0);

/*
 * MICGAIN volume control:
823
 * from 6 to 30 dB in 6 dB steps
824
 */
825
static DECLARE_TLV_DB_SCALE(mic_amp_tlv, 600, 600, 0);
826

827 828
/*
 * AFMGAIN volume control:
829
 * from -18 to 24 dB in 6 dB steps
830
 */
831
static DECLARE_TLV_DB_SCALE(afm_amp_tlv, -1800, 600, 0);
832

833 834 835 836 837 838 839 840 841 842 843 844
/*
 * HSGAIN volume control:
 * from -30 to 0 dB in 2 dB steps
 */
static DECLARE_TLV_DB_SCALE(hs_tlv, -3000, 200, 0);

/*
 * HFGAIN volume control:
 * from -52 to 6 dB in 2 dB steps
 */
static DECLARE_TLV_DB_SCALE(hf_tlv, -5200, 200, 0);

845 846 847 848 849 850
/*
 * EPGAIN volume control:
 * from -24 to 6 dB in 2 dB steps
 */
static DECLARE_TLV_DB_SCALE(ep_tlv, -2400, 200, 0);

851 852 853 854 855 856 857 858 859
/* Left analog microphone selection */
static const char *twl6040_amicl_texts[] =
	{"Headset Mic", "Main Mic", "Aux/FM Left", "Off"};

/* Right analog microphone selection */
static const char *twl6040_amicr_texts[] =
	{"Headset Mic", "Sub Mic", "Aux/FM Right", "Off"};

static const struct soc_enum twl6040_enum[] = {
860 861
	SOC_ENUM_SINGLE(TWL6040_REG_MICLCTL, 3, 4, twl6040_amicl_texts),
	SOC_ENUM_SINGLE(TWL6040_REG_MICRCTL, 3, 4, twl6040_amicr_texts),
862 863
};

864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
static const char *twl6040_hs_texts[] = {
	"Off", "HS DAC", "Line-In amp"
};

static const struct soc_enum twl6040_hs_enum[] = {
	SOC_ENUM_SINGLE(TWL6040_REG_HSLCTL, 5, ARRAY_SIZE(twl6040_hs_texts),
			twl6040_hs_texts),
	SOC_ENUM_SINGLE(TWL6040_REG_HSRCTL, 5, ARRAY_SIZE(twl6040_hs_texts),
			twl6040_hs_texts),
};

static const char *twl6040_hf_texts[] = {
	"Off", "HF DAC", "Line-In amp"
};

static const struct soc_enum twl6040_hf_enum[] = {
	SOC_ENUM_SINGLE(TWL6040_REG_HFLCTL, 2, ARRAY_SIZE(twl6040_hf_texts),
			twl6040_hf_texts),
	SOC_ENUM_SINGLE(TWL6040_REG_HFRCTL, 2, ARRAY_SIZE(twl6040_hf_texts),
			twl6040_hf_texts),
};

886 887 888 889 890 891 892
static const struct snd_kcontrol_new amicl_control =
	SOC_DAPM_ENUM("Route", twl6040_enum[0]);

static const struct snd_kcontrol_new amicr_control =
	SOC_DAPM_ENUM("Route", twl6040_enum[1]);

/* Headset DAC playback switches */
893 894
static const struct snd_kcontrol_new hsl_mux_controls =
	SOC_DAPM_ENUM("Route", twl6040_hs_enum[0]);
895

896 897
static const struct snd_kcontrol_new hsr_mux_controls =
	SOC_DAPM_ENUM("Route", twl6040_hs_enum[1]);
898 899

/* Handsfree DAC playback switches */
900 901
static const struct snd_kcontrol_new hfl_mux_controls =
	SOC_DAPM_ENUM("Route", twl6040_hf_enum[0]);
902

903 904
static const struct snd_kcontrol_new hfr_mux_controls =
	SOC_DAPM_ENUM("Route", twl6040_hf_enum[1]);
905

906 907
static const struct snd_kcontrol_new ep_path_enable_control =
	SOC_DAPM_SINGLE("Switch", TWL6040_REG_SW_SHADOW, 0, 1, 0);
908

909 910 911 912 913 914
static const struct snd_kcontrol_new auxl_switch_control =
	SOC_DAPM_SINGLE("Switch", TWL6040_REG_HFLCTL, 6, 1, 0);

static const struct snd_kcontrol_new auxr_switch_control =
	SOC_DAPM_SINGLE("Switch", TWL6040_REG_HFRCTL, 6, 1, 0);

915
/* Headset power mode */
916
static const char *twl6040_power_mode_texts[] = {
917 918 919
	"Low-Power", "High-Perfomance",
};

920 921 922
static const struct soc_enum twl6040_power_mode_enum =
	SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(twl6040_power_mode_texts),
			twl6040_power_mode_texts);
923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951

static int twl6040_headset_power_get_enum(struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
	struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);

	ucontrol->value.enumerated.item[0] = priv->hs_power_mode;

	return 0;
}

static int twl6040_headset_power_put_enum(struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
	struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
	int high_perf = ucontrol->value.enumerated.item[0];
	int ret = 0;

	if (!priv->hs_power_mode_locked)
		ret = headset_power_mode(codec, high_perf);

	if (!ret)
		priv->hs_power_mode = high_perf;

	return ret;
}

952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977
static int twl6040_pll_get_enum(struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
	struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);

	ucontrol->value.enumerated.item[0] = priv->pll_power_mode;

	return 0;
}

static int twl6040_pll_put_enum(struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
	struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);

	priv->pll_power_mode = ucontrol->value.enumerated.item[0];

	return 0;
}

int twl6040_get_clk_id(struct snd_soc_codec *codec)
{
	struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);

978
	return priv->pll_power_mode;
979 980 981
}
EXPORT_SYMBOL_GPL(twl6040_get_clk_id);

982 983 984 985 986 987 988 989 990
int twl6040_get_trim_value(struct snd_soc_codec *codec, enum twl6040_trim trim)
{
	if (unlikely(trim >= TWL6040_TRIM_INVAL))
		return -EINVAL;

	return twl6040_read_reg_cache(codec, TWL6040_REG_TRIM1 + trim);
}
EXPORT_SYMBOL_GPL(twl6040_get_trim_value);

991 992 993 994 995 996 997
static const struct snd_kcontrol_new twl6040_snd_controls[] = {
	/* Capture gains */
	SOC_DOUBLE_TLV("Capture Preamplifier Volume",
		TWL6040_REG_MICGAIN, 6, 7, 1, 1, mic_preamp_tlv),
	SOC_DOUBLE_TLV("Capture Volume",
		TWL6040_REG_MICGAIN, 0, 3, 4, 0, mic_amp_tlv),

998 999
	/* AFM gains */
	SOC_DOUBLE_TLV("Aux FM Volume",
1000
		TWL6040_REG_LINEGAIN, 0, 3, 7, 0, afm_amp_tlv),
1001

1002
	/* Playback gains */
1003 1004 1005 1006 1007 1008
	SOC_DOUBLE_EXT_TLV("Headset Playback Volume",
		TWL6040_REG_HSGAIN, 0, 4, 0xF, 1, twl6040_get_volsw,
		twl6040_put_volsw, hs_tlv),
	SOC_DOUBLE_R_EXT_TLV("Handsfree Playback Volume",
		TWL6040_REG_HFLGAIN, TWL6040_REG_HFRGAIN, 0, 0x1D, 1,
		twl6040_get_volsw, twl6040_put_volsw, hf_tlv),
1009 1010
	SOC_SINGLE_TLV("Earphone Playback Volume",
		TWL6040_REG_EARCTL, 1, 0xF, 1, ep_tlv),
1011

1012
	SOC_ENUM_EXT("Headset Power Mode", twl6040_power_mode_enum,
1013 1014
		twl6040_headset_power_get_enum,
		twl6040_headset_power_put_enum),
1015 1016 1017

	SOC_ENUM_EXT("PLL Selection", twl6040_power_mode_enum,
		twl6040_pll_get_enum, twl6040_pll_put_enum),
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
};

static const struct snd_soc_dapm_widget twl6040_dapm_widgets[] = {
	/* Inputs */
	SND_SOC_DAPM_INPUT("MAINMIC"),
	SND_SOC_DAPM_INPUT("HSMIC"),
	SND_SOC_DAPM_INPUT("SUBMIC"),
	SND_SOC_DAPM_INPUT("AFML"),
	SND_SOC_DAPM_INPUT("AFMR"),

	/* Outputs */
	SND_SOC_DAPM_OUTPUT("HSOL"),
	SND_SOC_DAPM_OUTPUT("HSOR"),
	SND_SOC_DAPM_OUTPUT("HFL"),
	SND_SOC_DAPM_OUTPUT("HFR"),
1033
	SND_SOC_DAPM_OUTPUT("EP"),
1034 1035
	SND_SOC_DAPM_OUTPUT("AUXL"),
	SND_SOC_DAPM_OUTPUT("AUXR"),
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048

	/* Analog input muxes for the capture amplifiers */
	SND_SOC_DAPM_MUX("Analog Left Capture Route",
			SND_SOC_NOPM, 0, 0, &amicl_control),
	SND_SOC_DAPM_MUX("Analog Right Capture Route",
			SND_SOC_NOPM, 0, 0, &amicr_control),

	/* Analog capture PGAs */
	SND_SOC_DAPM_PGA("MicAmpL",
			TWL6040_REG_MICLCTL, 0, 0, NULL, 0),
	SND_SOC_DAPM_PGA("MicAmpR",
			TWL6040_REG_MICRCTL, 0, 0, NULL, 0),

1049 1050 1051 1052 1053 1054
	/* Auxiliary FM PGAs */
	SND_SOC_DAPM_PGA("AFMAmpL",
			TWL6040_REG_MICLCTL, 1, 0, NULL, 0),
	SND_SOC_DAPM_PGA("AFMAmpR",
			TWL6040_REG_MICRCTL, 1, 0, NULL, 0),

1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
	/* ADCs */
	SND_SOC_DAPM_ADC("ADC Left", "Left Front Capture",
			TWL6040_REG_MICLCTL, 2, 0),
	SND_SOC_DAPM_ADC("ADC Right", "Right Front Capture",
			TWL6040_REG_MICRCTL, 2, 0),

	/* Microphone bias */
	SND_SOC_DAPM_MICBIAS("Headset Mic Bias",
			TWL6040_REG_AMICBCTL, 0, 0),
	SND_SOC_DAPM_MICBIAS("Main Mic Bias",
			TWL6040_REG_AMICBCTL, 4, 0),
	SND_SOC_DAPM_MICBIAS("Digital Mic1 Bias",
			TWL6040_REG_DMICBCTL, 0, 0),
	SND_SOC_DAPM_MICBIAS("Digital Mic2 Bias",
			TWL6040_REG_DMICBCTL, 4, 0),

	/* DACs */
1072 1073 1074 1075 1076 1077 1078 1079
	SND_SOC_DAPM_DAC_E("HSDAC Left", "Headset Playback",
			TWL6040_REG_HSLCTL, 0, 0,
			twl6040_hs_dac_event,
			SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
	SND_SOC_DAPM_DAC_E("HSDAC Right", "Headset Playback",
			TWL6040_REG_HSRCTL, 0, 0,
			twl6040_hs_dac_event,
			SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1080 1081 1082 1083 1084 1085 1086 1087 1088
	SND_SOC_DAPM_DAC_E("HFDAC Left", "Handsfree Playback",
			TWL6040_REG_HFLCTL, 0, 0,
			twl6040_power_mode_event,
			SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
	SND_SOC_DAPM_DAC_E("HFDAC Right", "Handsfree Playback",
			TWL6040_REG_HFRCTL, 0, 0,
			twl6040_power_mode_event,
			SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),

1089
	SND_SOC_DAPM_MUX("Handsfree Left Playback",
1090
			SND_SOC_NOPM, 0, 0, &hfl_mux_controls),
1091
	SND_SOC_DAPM_MUX("Handsfree Right Playback",
1092 1093
			SND_SOC_NOPM, 0, 0, &hfr_mux_controls),
	/* Analog playback Muxes */
1094
	SND_SOC_DAPM_MUX("Headset Left Playback",
1095
			SND_SOC_NOPM, 0, 0, &hsl_mux_controls),
1096
	SND_SOC_DAPM_MUX("Headset Right Playback",
1097
			SND_SOC_NOPM, 0, 0, &hsr_mux_controls),
1098

1099 1100
	SND_SOC_DAPM_SWITCH("Earphone Playback", SND_SOC_NOPM, 0, 0,
			&ep_path_enable_control),
1101 1102 1103 1104
	SND_SOC_DAPM_SWITCH("AUXL Playback", SND_SOC_NOPM, 0, 0,
			&auxl_switch_control),
	SND_SOC_DAPM_SWITCH("AUXR Playback", SND_SOC_NOPM, 0, 0,
			&auxr_switch_control),
1105

1106
	/* Analog playback drivers */
1107
	SND_SOC_DAPM_OUT_DRV_E("HF Left Driver",
1108
			TWL6040_REG_HFLCTL, 4, 0, NULL, 0,
1109
			out_drv_event,
1110
			SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1111
	SND_SOC_DAPM_OUT_DRV_E("HF Right Driver",
1112
			TWL6040_REG_HFRCTL, 4, 0, NULL, 0,
1113
			out_drv_event,
1114
			SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1115
	SND_SOC_DAPM_OUT_DRV_E("HS Left Driver",
1116
			TWL6040_REG_HSLCTL, 2, 0, NULL, 0,
1117
			out_drv_event,
1118
			SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1119
	SND_SOC_DAPM_OUT_DRV_E("HS Right Driver",
1120
			TWL6040_REG_HSRCTL, 2, 0, NULL, 0,
1121
			out_drv_event,
1122
			SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1123 1124
	SND_SOC_DAPM_OUT_DRV_E("Earphone Driver",
			TWL6040_REG_EARCTL, 0, 0, NULL, 0,
1125 1126
			twl6040_power_mode_event,
			SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
1127 1128

	/* Analog playback PGAs */
1129
	SND_SOC_DAPM_PGA("HF Left PGA",
1130
			TWL6040_REG_HFLCTL, 1, 0, NULL, 0),
1131
	SND_SOC_DAPM_PGA("HF Right PGA",
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
			TWL6040_REG_HFRCTL, 1, 0, NULL, 0),

};

static const struct snd_soc_dapm_route intercon[] = {
	/* Capture path */
	{"Analog Left Capture Route", "Headset Mic", "HSMIC"},
	{"Analog Left Capture Route", "Main Mic", "MAINMIC"},
	{"Analog Left Capture Route", "Aux/FM Left", "AFML"},

	{"Analog Right Capture Route", "Headset Mic", "HSMIC"},
	{"Analog Right Capture Route", "Sub Mic", "SUBMIC"},
	{"Analog Right Capture Route", "Aux/FM Right", "AFMR"},

	{"MicAmpL", NULL, "Analog Left Capture Route"},
	{"MicAmpR", NULL, "Analog Right Capture Route"},

	{"ADC Left", NULL, "MicAmpL"},
	{"ADC Right", NULL, "MicAmpR"},

1152
	/* AFM path */
1153 1154
	{"AFMAmpL", NULL, "AFML"},
	{"AFMAmpR", NULL, "AFMR"},
1155

1156 1157
	{"Headset Left Playback", "HS DAC", "HSDAC Left"},
	{"Headset Left Playback", "Line-In amp", "AFMAmpL"},
1158

1159 1160
	{"Headset Right Playback", "HS DAC", "HSDAC Right"},
	{"Headset Right Playback", "Line-In amp", "AFMAmpR"},
1161

1162 1163
	{"HS Left Driver", NULL, "Headset Left Playback"},
	{"HS Right Driver", NULL, "Headset Right Playback"},
1164

1165 1166
	{"HSOL", NULL, "HS Left Driver"},
	{"HSOR", NULL, "HS Right Driver"},
1167

1168
	/* Earphone playback path */
1169 1170
	{"Earphone Playback", "Switch", "HSDAC Left"},
	{"Earphone Driver", NULL, "Earphone Playback"},
1171 1172
	{"EP", NULL, "Earphone Driver"},

1173 1174
	{"Handsfree Left Playback", "HF DAC", "HFDAC Left"},
	{"Handsfree Left Playback", "Line-In amp", "AFMAmpL"},
1175

1176 1177
	{"Handsfree Right Playback", "HF DAC", "HFDAC Right"},
	{"Handsfree Right Playback", "Line-In amp", "AFMAmpR"},
1178

1179 1180
	{"HF Left PGA", NULL, "Handsfree Left Playback"},
	{"HF Right PGA", NULL, "Handsfree Right Playback"},
1181

1182 1183
	{"HF Left Driver", NULL, "HF Left PGA"},
	{"HF Right Driver", NULL, "HF Right PGA"},
1184

1185 1186
	{"HFL", NULL, "HF Left Driver"},
	{"HFR", NULL, "HF Right Driver"},
1187 1188 1189 1190 1191 1192

	{"AUXL Playback", "Switch", "HF Left PGA"},
	{"AUXR Playback", "Switch", "HF Right PGA"},

	{"AUXL", NULL, "AUXL Playback"},
	{"AUXR", NULL, "AUXR Playback"},
1193 1194 1195 1196
};

static int twl6040_add_widgets(struct snd_soc_codec *codec)
{
L
Liam Girdwood 已提交
1197
	struct snd_soc_dapm_context *dapm = &codec->dapm;
1198

L
Liam Girdwood 已提交
1199 1200 1201 1202
	snd_soc_dapm_new_controls(dapm, twl6040_dapm_widgets,
				 ARRAY_SIZE(twl6040_dapm_widgets));
	snd_soc_dapm_add_routes(dapm, intercon, ARRAY_SIZE(intercon));
	snd_soc_dapm_new_widgets(dapm);
1203 1204 1205 1206 1207 1208 1209

	return 0;
}

static int twl6040_set_bias_level(struct snd_soc_codec *codec,
				enum snd_soc_bias_level level)
{
1210
	struct twl6040 *twl6040 = codec->control_data;
1211
	struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
	int ret;

	switch (level) {
	case SND_SOC_BIAS_ON:
		break;
	case SND_SOC_BIAS_PREPARE:
		break;
	case SND_SOC_BIAS_STANDBY:
		if (priv->codec_powered)
			break;

1223 1224 1225
		ret = twl6040_power(twl6040, 1);
		if (ret)
			return ret;
1226

1227
		priv->codec_powered = 1;
1228

1229
		twl6040_restore_regs(codec);
1230 1231 1232

		/* Set external boost GPO */
		twl6040_write(codec, TWL6040_REG_GPOCTL, 0x02);
1233 1234 1235 1236 1237
		break;
	case SND_SOC_BIAS_OFF:
		if (!priv->codec_powered)
			break;

1238
		twl6040_power(twl6040, 0);
1239 1240 1241 1242
		priv->codec_powered = 0;
		break;
	}

L
Liam Girdwood 已提交
1243
	codec->dapm.bias_level = level;
1244 1245 1246 1247 1248 1249 1250 1251

	return 0;
}

static int twl6040_startup(struct snd_pcm_substream *substream,
			struct snd_soc_dai *dai)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
1252
	struct snd_soc_codec *codec = rtd->codec;
1253
	struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1254 1255 1256

	snd_pcm_hw_constraint_list(substream->runtime, 0,
				SNDRV_PCM_HW_PARAM_RATE,
1257
				&sysclk_constraints[priv->pll_power_mode]);
1258 1259 1260 1261 1262 1263 1264 1265 1266

	return 0;
}

static int twl6040_hw_params(struct snd_pcm_substream *substream,
			struct snd_pcm_hw_params *params,
			struct snd_soc_dai *dai)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
1267
	struct snd_soc_codec *codec = rtd->codec;
1268
	struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1269 1270 1271 1272
	int rate;

	rate = params_rate(params);
	switch (rate) {
1273 1274 1275
	case 11250:
	case 22500:
	case 44100:
1276
	case 88200:
1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289
		/* These rates are not supported when HPPLL is in use */
		if (unlikely(priv->pll == TWL6040_SYSCLK_SEL_HPPLL)) {
			dev_err(codec->dev, "HPPLL does not support rate %d\n",
				rate);
			return -EINVAL;
		}
		/* Capture is not supported with 17.64MHz sysclk */
		if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
			dev_err(codec->dev,
				"capture mode is not supported at %dHz\n",
				rate);
			return -EINVAL;
		}
1290 1291
		priv->sysclk = 17640000;
		break;
1292 1293 1294 1295
	case 8000:
	case 16000:
	case 32000:
	case 48000:
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306
	case 96000:
		priv->sysclk = 19200000;
		break;
	default:
		dev_err(codec->dev, "unsupported rate %d\n", rate);
		return -EINVAL;
	}

	return 0;
}

1307 1308
static int twl6040_prepare(struct snd_pcm_substream *substream,
			struct snd_soc_dai *dai)
1309 1310
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
1311
	struct snd_soc_codec *codec = rtd->codec;
1312
	struct twl6040 *twl6040 = codec->control_data;
1313
	struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1314
	int ret;
1315

1316 1317 1318 1319 1320 1321 1322
	if (!priv->sysclk) {
		dev_err(codec->dev,
			"no mclk configured, call set_sysclk() on init\n");
		return -EINVAL;
	}

	if ((priv->sysclk == 17640000) && priv->non_lp) {
1323 1324 1325 1326 1327
			dev_err(codec->dev,
				"some enabled paths aren't supported at %dHz\n",
				priv->sysclk);
			return -EPERM;
	}
1328 1329 1330 1331 1332 1333 1334

	ret = twl6040_set_pll(twl6040, priv->pll, priv->clk_in, priv->sysclk);
	if (ret) {
		dev_err(codec->dev, "Can not set PLL (%d)\n", ret);
		return -EPERM;
	}

1335 1336 1337 1338 1339 1340 1341
	return 0;
}

static int twl6040_set_dai_sysclk(struct snd_soc_dai *codec_dai,
		int clk_id, unsigned int freq, int dir)
{
	struct snd_soc_codec *codec = codec_dai->codec;
1342
	struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1343 1344 1345 1346

	switch (clk_id) {
	case TWL6040_SYSCLK_SEL_LPPLL:
	case TWL6040_SYSCLK_SEL_HPPLL:
1347 1348
		priv->pll = clk_id;
		priv->clk_in = freq;
1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360
		break;
	default:
		dev_err(codec->dev, "unknown clk_id %d\n", clk_id);
		return -EINVAL;
	}

	return 0;
}

static struct snd_soc_dai_ops twl6040_dai_ops = {
	.startup	= twl6040_startup,
	.hw_params	= twl6040_hw_params,
1361
	.prepare	= twl6040_prepare,
1362 1363 1364
	.set_sysclk	= twl6040_set_dai_sysclk,
};

L
Liam Girdwood 已提交
1365
static struct snd_soc_dai_driver twl6040_dai[] = {
1366
{
1367
	.name = "twl6040-legacy",
1368 1369 1370
	.playback = {
		.stream_name = "Playback",
		.channels_min = 1,
1371
		.channels_max = 5,
1372 1373 1374 1375 1376 1377 1378
		.rates = TWL6040_RATES,
		.formats = TWL6040_FORMATS,
	},
	.capture = {
		.stream_name = "Capture",
		.channels_min = 1,
		.channels_max = 2,
1379 1380 1381
		.rates = TWL6040_RATES,
		.formats = TWL6040_FORMATS,
	},
1382 1383
	.ops = &twl6040_dai_ops,
},
L
Liam Girdwood 已提交
1384 1385
{
	.name = "twl6040-ul",
1386 1387 1388 1389 1390 1391 1392 1393
	.capture = {
		.stream_name = "Capture",
		.channels_min = 1,
		.channels_max = 2,
		.rates = TWL6040_RATES,
		.formats = TWL6040_FORMATS,
	},
	.ops = &twl6040_dai_ops,
L
Liam Girdwood 已提交
1394 1395 1396
},
{
	.name = "twl6040-dl1",
1397
	.playback = {
L
Liam Girdwood 已提交
1398
		.stream_name = "Headset Playback",
1399
		.channels_min = 1,
L
Liam Girdwood 已提交
1400
		.channels_max = 2,
1401 1402 1403
		.rates = TWL6040_RATES,
		.formats = TWL6040_FORMATS,
	},
L
Liam Girdwood 已提交
1404 1405 1406 1407 1408 1409
	.ops = &twl6040_dai_ops,
},
{
	.name = "twl6040-dl2",
	.playback = {
		.stream_name = "Handsfree Playback",
1410 1411 1412 1413 1414 1415
		.channels_min = 1,
		.channels_max = 2,
		.rates = TWL6040_RATES,
		.formats = TWL6040_FORMATS,
	},
	.ops = &twl6040_dai_ops,
L
Liam Girdwood 已提交
1416 1417 1418 1419 1420
},
{
	.name = "twl6040-vib",
	.playback = {
		.stream_name = "Vibra Playback",
1421 1422
		.channels_min = 1,
		.channels_max = 1,
L
Liam Girdwood 已提交
1423 1424 1425 1426 1427
		.rates = SNDRV_PCM_RATE_CONTINUOUS,
		.formats = TWL6040_FORMATS,
	},
	.ops = &twl6040_dai_ops,
},
1428 1429 1430
};

#ifdef CONFIG_PM
1431
static int twl6040_suspend(struct snd_soc_codec *codec, pm_message_t state)
1432 1433 1434 1435 1436 1437
{
	twl6040_set_bias_level(codec, SND_SOC_BIAS_OFF);

	return 0;
}

1438
static int twl6040_resume(struct snd_soc_codec *codec)
1439 1440
{
	twl6040_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1441
	twl6040_set_bias_level(codec, codec->dapm.suspend_bias_level);
1442 1443 1444 1445 1446 1447 1448 1449

	return 0;
}
#else
#define twl6040_suspend NULL
#define twl6040_resume NULL
#endif

1450
static int twl6040_probe(struct snd_soc_codec *codec)
1451 1452
{
	struct twl6040_data *priv;
1453
	struct twl4030_codec_data *pdata = dev_get_platdata(codec->dev);
1454 1455
	struct platform_device *pdev = container_of(codec->dev,
						   struct platform_device, dev);
1456 1457 1458 1459 1460
	int ret = 0;

	priv = kzalloc(sizeof(struct twl6040_data), GFP_KERNEL);
	if (priv == NULL)
		return -ENOMEM;
1461
	snd_soc_codec_set_drvdata(codec, priv);
1462

1463
	priv->codec = codec;
1464
	codec->control_data = dev_get_drvdata(codec->dev->parent);
1465

1466 1467 1468 1469 1470 1471 1472
	if (pdata && pdata->hs_left_step && pdata->hs_right_step) {
		priv->hs_left_step = pdata->hs_left_step;
		priv->hs_right_step = pdata->hs_right_step;
	} else {
		priv->hs_left_step = 1;
		priv->hs_right_step = 1;
	}
1473

1474 1475 1476 1477 1478 1479 1480
	if (pdata && pdata->hf_left_step && pdata->hf_right_step) {
		priv->hf_left_step = pdata->hf_left_step;
		priv->hf_right_step = pdata->hf_right_step;
	} else {
		priv->hf_left_step = 1;
		priv->hf_right_step = 1;
	}
1481

1482 1483 1484 1485 1486 1487
	priv->plug_irq = platform_get_irq(pdev, 0);
	if (priv->plug_irq < 0) {
		dev_err(codec->dev, "invalid irq\n");
		ret = -EINVAL;
		goto work_err;
	}
1488

1489
	priv->workqueue = alloc_workqueue("twl6040-codec", 0, 0);
1490 1491
	if (!priv->workqueue) {
		ret = -ENOMEM;
1492
		goto work_err;
1493
	}
1494

1495
	INIT_DELAYED_WORK(&priv->hs_jack.work, twl6040_accessory_work);
1496 1497
	INIT_DELAYED_WORK(&priv->headset.work, twl6040_pga_hs_work);
	INIT_DELAYED_WORK(&priv->handsfree.work, twl6040_pga_hf_work);
1498 1499

	mutex_init(&priv->mutex);
1500

1501 1502
	init_completion(&priv->headset.ramp_done);
	init_completion(&priv->handsfree.ramp_done);
1503

1504 1505
	ret = request_threaded_irq(priv->plug_irq, NULL, twl6040_audio_handler,
				   0, "twl6040_irq_plug", codec);
1506 1507 1508 1509 1510
	if (ret) {
		dev_err(codec->dev, "PLUG IRQ request failed: %d\n", ret);
		goto plugirq_err;
	}

1511
	twl6040_init_chip(codec);
1512

1513 1514 1515
	/* power on device */
	ret = twl6040_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
	if (ret)
1516
		goto bias_err;
1517

1518 1519 1520
	snd_soc_add_controls(codec, twl6040_snd_controls,
				ARRAY_SIZE(twl6040_snd_controls));
	twl6040_add_widgets(codec);
1521 1522 1523

	return 0;

1524
bias_err:
1525
	free_irq(priv->plug_irq, codec);
1526
plugirq_err:
1527 1528
	destroy_workqueue(priv->workqueue);
work_err:
1529 1530 1531 1532
	kfree(priv);
	return ret;
}

1533
static int twl6040_remove(struct snd_soc_codec *codec)
1534
{
1535
	struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
1536

1537
	twl6040_set_bias_level(codec, SND_SOC_BIAS_OFF);
1538
	free_irq(priv->plug_irq, codec);
1539
	destroy_workqueue(priv->workqueue);
1540
	kfree(priv);
1541

1542 1543
	return 0;
}
1544

1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559
static struct snd_soc_codec_driver soc_codec_dev_twl6040 = {
	.probe = twl6040_probe,
	.remove = twl6040_remove,
	.suspend = twl6040_suspend,
	.resume = twl6040_resume,
	.read = twl6040_read_reg_cache,
	.write = twl6040_write,
	.set_bias_level = twl6040_set_bias_level,
	.reg_cache_size = ARRAY_SIZE(twl6040_reg),
	.reg_word_size = sizeof(u8),
	.reg_cache_default = twl6040_reg,
};

static int __devinit twl6040_codec_probe(struct platform_device *pdev)
{
L
Liam Girdwood 已提交
1560 1561
	return snd_soc_register_codec(&pdev->dev, &soc_codec_dev_twl6040,
				      twl6040_dai, ARRAY_SIZE(twl6040_dai));
1562 1563 1564 1565 1566
}

static int __devexit twl6040_codec_remove(struct platform_device *pdev)
{
	snd_soc_unregister_codec(&pdev->dev);
1567 1568 1569 1570 1571
	return 0;
}

static struct platform_driver twl6040_codec_driver = {
	.driver = {
1572
		.name = "twl6040-codec",
1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593
		.owner = THIS_MODULE,
	},
	.probe = twl6040_codec_probe,
	.remove = __devexit_p(twl6040_codec_remove),
};

static int __init twl6040_codec_init(void)
{
	return platform_driver_register(&twl6040_codec_driver);
}
module_init(twl6040_codec_init);

static void __exit twl6040_codec_exit(void)
{
	platform_driver_unregister(&twl6040_codec_driver);
}
module_exit(twl6040_codec_exit);

MODULE_DESCRIPTION("ASoC TWL6040 codec driver");
MODULE_AUTHOR("Misael Lopez Cruz");
MODULE_LICENSE("GPL");