twl6040.c 31.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/mfd/twl6040.h>
30 31 32 33 34

#include <sound/core.h>
#include <sound/pcm.h>
#include <sound/pcm_params.h>
#include <sound/soc.h>
35
#include <sound/soc-dapm.h>
36 37 38 39 40
#include <sound/initval.h>
#include <sound/tlv.h>

#include "twl6040.h"

41 42 43 44 45 46 47 48
enum twl6040_dai_id {
	TWL6040_DAI_LEGACY = 0,
	TWL6040_DAI_UL,
	TWL6040_DAI_DL1,
	TWL6040_DAI_DL2,
	TWL6040_DAI_VIB,
};

49
#define TWL6040_RATES		SNDRV_PCM_RATE_8000_96000
50 51 52 53 54 55 56
#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

57
#define TWL6040_CACHEREGNUM	(TWL6040_REG_STATUS + 1)
58

59 60
struct twl6040_jack_data {
	struct snd_soc_jack *jack;
61
	struct delayed_work work;
62 63 64
	int report;
};

65 66
/* codec private data */
struct twl6040_data {
67
	int plug_irq;
68 69
	int codec_powered;
	int pll;
70
	int pll_power_mode;
71 72
	int hs_power_mode;
	int hs_power_mode_locked;
73 74
	bool dl1_unmuted;
	bool dl2_unmuted;
75
	u8 dl12_cache[TWL6040_REG_HFRCTL - TWL6040_REG_HSLCTL + 1];
76
	unsigned int clk_in;
77
	unsigned int sysclk;
78 79 80
	struct twl6040_jack_data hs_jack;
	struct snd_soc_codec *codec;
	struct mutex mutex;
81 82
};

83
/* set of rates for each pll: low-power and high-performance */
84
static const unsigned int lp_rates[] = {
85 86 87 88 89 90 91 92 93 94 95
	8000,
	11250,
	16000,
	22500,
	32000,
	44100,
	48000,
	88200,
	96000,
};

96
static const unsigned int hp_rates[] = {
97 98 99 100 101 102 103
	8000,
	16000,
	32000,
	48000,
	96000,
};

104
static const struct snd_pcm_hw_constraint_list sysclk_constraints[] = {
105 106
	{ .count = ARRAY_SIZE(lp_rates), .list = lp_rates, },
	{ .count = ARRAY_SIZE(hp_rates), .list = hp_rates, },
107 108
};

109
static unsigned int twl6040_read(struct snd_soc_codec *codec, unsigned int reg)
110 111
{
	struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
112 113 114 115 116
	struct twl6040 *twl6040 = codec->control_data;
	u8 value;

	if (reg >= TWL6040_CACHEREGNUM)
		return -EIO;
117 118 119 120 121 122 123

	switch (reg) {
	case TWL6040_REG_HSLCTL:
	case TWL6040_REG_HSRCTL:
	case TWL6040_REG_EARCTL:
	case TWL6040_REG_HFLCTL:
	case TWL6040_REG_HFRCTL:
124
		value = priv->dl12_cache[reg - TWL6040_REG_HSLCTL];
125 126
		break;
	default:
127
		value = twl6040_reg_read(twl6040, reg);
128 129 130 131 132 133
		break;
	}

	return value;
}

134 135
static bool twl6040_can_write_to_chip(struct snd_soc_codec *codec,
				  unsigned int reg)
136 137 138 139 140 141 142
{
	struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);

	switch (reg) {
	case TWL6040_REG_HSLCTL:
	case TWL6040_REG_HSRCTL:
	case TWL6040_REG_EARCTL:
143 144
		/* DL1 path */
		return priv->dl1_unmuted;
145 146
	case TWL6040_REG_HFLCTL:
	case TWL6040_REG_HFRCTL:
147
		return priv->dl2_unmuted;
148
	default:
149
		return 1;
150
	}
151 152
}

153 154
static inline void twl6040_update_dl12_cache(struct snd_soc_codec *codec,
					     u8 reg, u8 value)
155 156 157 158 159 160 161 162 163
{
	struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);

	switch (reg) {
	case TWL6040_REG_HSLCTL:
	case TWL6040_REG_HSRCTL:
	case TWL6040_REG_EARCTL:
	case TWL6040_REG_HFLCTL:
	case TWL6040_REG_HFRCTL:
164 165
		priv->dl12_cache[reg - TWL6040_REG_HSLCTL] = value;
		break;
166
	default:
167
		break;
168
	}
169 170
}

171 172 173
static int twl6040_write(struct snd_soc_codec *codec,
			unsigned int reg, unsigned int value)
{
174 175
	struct twl6040 *twl6040 = codec->control_data;

176 177 178
	if (reg >= TWL6040_CACHEREGNUM)
		return -EIO;

179
	twl6040_update_dl12_cache(codec, reg, value);
180
	if (twl6040_can_write_to_chip(codec, reg))
181 182 183
		return twl6040_reg_write(twl6040, reg, value);
	else
		return 0;
184 185
}

186
static void twl6040_init_chip(struct snd_soc_codec *codec)
187
{
188 189 190 191 192
	twl6040_read(codec, TWL6040_REG_TRIM1);
	twl6040_read(codec, TWL6040_REG_TRIM2);
	twl6040_read(codec, TWL6040_REG_TRIM3);
	twl6040_read(codec, TWL6040_REG_HSOTRIM);
	twl6040_read(codec, TWL6040_REG_HFOTRIM);
193

194 195
	/* Change chip defaults */
	/* No imput selected for microphone amplifiers */
196 197
	twl6040_write(codec, TWL6040_REG_MICLCTL, 0x18);
	twl6040_write(codec, TWL6040_REG_MICRCTL, 0x18);
198 199 200 201 202 203

	/*
	 * 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.
	 */
204 205 206 207 208
	twl6040_write(codec, TWL6040_REG_HSGAIN, 0xff);
	twl6040_write(codec, TWL6040_REG_EARCTL, 0x1e);
	twl6040_write(codec, TWL6040_REG_HFLGAIN, 0x1d);
	twl6040_write(codec, TWL6040_REG_HFRGAIN, 0x1d);
	twl6040_write(codec, TWL6040_REG_LINEGAIN, 0);
209 210 211 212 213 214
}

/* set headset dac and driver power mode */
static int headset_power_mode(struct snd_soc_codec *codec, int high_perf)
{
	int hslctl, hsrctl;
215
	int mask = TWL6040_HSDRVMODE | TWL6040_HSDACMODE;
216

217 218
	hslctl = twl6040_read(codec, TWL6040_REG_HSLCTL);
	hsrctl = twl6040_read(codec, TWL6040_REG_HSRCTL);
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233

	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;
}

234 235 236
static int twl6040_hs_dac_event(struct snd_soc_dapm_widget *w,
			struct snd_kcontrol *kcontrol, int event)
{
237 238 239 240 241 242 243 244
	struct snd_soc_codec *codec = w->codec;
	u8 hslctl, hsrctl;

	/*
	 * Workaround for Headset DC offset caused pop noise:
	 * Both HS DAC need to be turned on (before the HS driver) and off at
	 * the same time.
	 */
245 246
	hslctl = twl6040_read(codec, TWL6040_REG_HSLCTL);
	hsrctl = twl6040_read(codec, TWL6040_REG_HSRCTL);
247 248 249 250 251 252 253 254 255 256
	if (SND_SOC_DAPM_EVENT_ON(event)) {
		hslctl |= TWL6040_HSDACENA;
		hsrctl |= TWL6040_HSDACENA;
	} else {
		hslctl &= ~TWL6040_HSDACENA;
		hsrctl &= ~TWL6040_HSDACENA;
	}
	twl6040_write(codec, TWL6040_REG_HSLCTL, hslctl);
	twl6040_write(codec, TWL6040_REG_HSRCTL, hsrctl);

257 258 259 260
	msleep(1);
	return 0;
}

261
static int twl6040_ep_drv_event(struct snd_soc_dapm_widget *w,
262 263 264
			struct snd_kcontrol *kcontrol, int event)
{
	struct snd_soc_codec *codec = w->codec;
265
	struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
266
	int ret = 0;
267

268
	if (SND_SOC_DAPM_EVENT_ON(event)) {
269 270 271
		/* Earphone doesn't support low power mode */
		priv->hs_power_mode_locked = 1;
		ret = headset_power_mode(codec, 1);
272
	} else {
273 274
		priv->hs_power_mode_locked = 0;
		ret = headset_power_mode(codec, priv->hs_power_mode);
275
	}
276

277 278
	msleep(1);

279
	return ret;
280 281
}

282 283
static void twl6040_hs_jack_report(struct snd_soc_codec *codec,
				   struct snd_soc_jack *jack, int report)
284 285 286 287 288 289 290
{
	struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
	int status;

	mutex_lock(&priv->mutex);

	/* Sync status */
291
	status = twl6040_read(codec, TWL6040_REG_STATUS);
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
	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,
316
					struct twl6040_data, hs_jack.work.work);
317 318 319 320 321 322
	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);
}

323
/* audio interrupt handler */
324
static irqreturn_t twl6040_audio_handler(int irq, void *data)
325 326
{
	struct snd_soc_codec *codec = data;
327
	struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
328

329 330
	queue_delayed_work(system_power_efficient_wq,
			   &priv->hs_jack.work, msecs_to_jiffies(200));
331

332 333 334
	return IRQ_HANDLED;
}

335 336 337
static int twl6040_soc_dapm_put_vibra_enum(struct snd_kcontrol *kcontrol,
	struct snd_ctl_elem_value *ucontrol)
{
338
	struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
339 340 341 342
	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
	unsigned int val;

	/* Do not allow changes while Input/FF efect is running */
343
	val = twl6040_read(codec, e->reg);
344 345 346 347 348 349
	if (val & TWL6040_VIBENA && !(val & TWL6040_VIBSEL))
		return -EBUSY;

	return snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
}

350 351 352 353 354 355 356 357
/*
 * 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:
358
 * from 6 to 30 dB in 6 dB steps
359
 */
360
static DECLARE_TLV_DB_SCALE(mic_amp_tlv, 600, 600, 0);
361

362 363
/*
 * AFMGAIN volume control:
364
 * from -18 to 24 dB in 6 dB steps
365
 */
366
static DECLARE_TLV_DB_SCALE(afm_amp_tlv, -1800, 600, 0);
367

368 369 370 371 372 373 374 375 376 377 378 379
/*
 * 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);

380 381 382 383 384 385
/*
 * EPGAIN volume control:
 * from -24 to 6 dB in 2 dB steps
 */
static DECLARE_TLV_DB_SCALE(ep_tlv, -2400, 200, 0);

386 387 388 389 390 391 392 393 394
/* 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[] = {
395 396 397 398
	SOC_ENUM_SINGLE(TWL6040_REG_MICLCTL, 3,
			ARRAY_SIZE(twl6040_amicl_texts), twl6040_amicl_texts),
	SOC_ENUM_SINGLE(TWL6040_REG_MICRCTL, 3,
			ARRAY_SIZE(twl6040_amicr_texts), twl6040_amicr_texts),
399 400
};

401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
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),
};

423 424 425 426 427 428 429 430 431 432 433 434 435
static const char *twl6040_vibrapath_texts[] = {
	"Input FF", "Audio PDM"
};

static const struct soc_enum twl6040_vibra_enum[] = {
	SOC_ENUM_SINGLE(TWL6040_REG_VIBCTLL, 1,
			ARRAY_SIZE(twl6040_vibrapath_texts),
			twl6040_vibrapath_texts),
	SOC_ENUM_SINGLE(TWL6040_REG_VIBCTLR, 1,
			ARRAY_SIZE(twl6040_vibrapath_texts),
			twl6040_vibrapath_texts),
};

436 437 438 439 440 441 442
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 */
443 444
static const struct snd_kcontrol_new hsl_mux_controls =
	SOC_DAPM_ENUM("Route", twl6040_hs_enum[0]);
445

446 447
static const struct snd_kcontrol_new hsr_mux_controls =
	SOC_DAPM_ENUM("Route", twl6040_hs_enum[1]);
448 449

/* Handsfree DAC playback switches */
450 451
static const struct snd_kcontrol_new hfl_mux_controls =
	SOC_DAPM_ENUM("Route", twl6040_hf_enum[0]);
452

453 454
static const struct snd_kcontrol_new hfr_mux_controls =
	SOC_DAPM_ENUM("Route", twl6040_hf_enum[1]);
455

456
static const struct snd_kcontrol_new ep_path_enable_control =
457
	SOC_DAPM_SINGLE_VIRT("Switch", 1);
458

459 460 461 462 463 464
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);

465 466 467 468 469 470 471 472 473 474 475
/* Vibra playback switches */
static const struct snd_kcontrol_new vibral_mux_controls =
	SOC_DAPM_ENUM_EXT("Route", twl6040_vibra_enum[0],
		snd_soc_dapm_get_enum_double,
		twl6040_soc_dapm_put_vibra_enum);

static const struct snd_kcontrol_new vibrar_mux_controls =
	SOC_DAPM_ENUM_EXT("Route", twl6040_vibra_enum[1],
		snd_soc_dapm_get_enum_double,
		twl6040_soc_dapm_put_vibra_enum);

476
/* Headset power mode */
477
static const char *twl6040_power_mode_texts[] = {
478
	"Low-Power", "High-Performance",
479 480
};

481 482
static SOC_ENUM_SINGLE_EXT_DECL(twl6040_power_mode_enum,
				twl6040_power_mode_texts);
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511

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;
}

512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
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;
}

534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
int twl6040_get_dl1_gain(struct snd_soc_codec *codec)
{
	struct snd_soc_dapm_context *dapm = &codec->dapm;

	if (snd_soc_dapm_get_pin_status(dapm, "EP"))
		return -1; /* -1dB */

	if (snd_soc_dapm_get_pin_status(dapm, "HSOR") ||
		snd_soc_dapm_get_pin_status(dapm, "HSOL")) {

		u8 val = snd_soc_read(codec, TWL6040_REG_HSLCTL);
		if (val & TWL6040_HSDACMODE)
			/* HSDACL in LP mode */
			return -8; /* -8dB */
		else
			/* HSDACL in HP mode */
			return -1; /* -1dB */
	}
	return 0; /* 0dB */
}
EXPORT_SYMBOL_GPL(twl6040_get_dl1_gain);

556 557 558 559
int twl6040_get_clk_id(struct snd_soc_codec *codec)
{
	struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);

560
	return priv->pll_power_mode;
561 562 563
}
EXPORT_SYMBOL_GPL(twl6040_get_clk_id);

564 565 566 567 568
int twl6040_get_trim_value(struct snd_soc_codec *codec, enum twl6040_trim trim)
{
	if (unlikely(trim >= TWL6040_TRIM_INVAL))
		return -EINVAL;

569
	return twl6040_read(codec, TWL6040_REG_TRIM1 + trim);
570 571 572
}
EXPORT_SYMBOL_GPL(twl6040_get_trim_value);

573 574 575 576
int twl6040_get_hs_step_size(struct snd_soc_codec *codec)
{
	struct twl6040 *twl6040 = codec->control_data;

577
	if (twl6040_get_revid(twl6040) < TWL6040_REV_ES1_3)
578 579 580 581 582 583 584 585
		/* For ES under ES_1.3 HS step is 2 mV */
		return 2;
	else
		/* For ES_1.3 HS step is 1 mV */
		return 1;
}
EXPORT_SYMBOL_GPL(twl6040_get_hs_step_size);

586 587 588 589 590 591 592
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),

593 594
	/* AFM gains */
	SOC_DOUBLE_TLV("Aux FM Volume",
595
		TWL6040_REG_LINEGAIN, 0, 3, 7, 0, afm_amp_tlv),
596

597
	/* Playback gains */
598 599 600 601
	SOC_DOUBLE_TLV("Headset Playback Volume",
		TWL6040_REG_HSGAIN, 0, 4, 0xF, 1, hs_tlv),
	SOC_DOUBLE_R_TLV("Handsfree Playback Volume",
		TWL6040_REG_HFLGAIN, TWL6040_REG_HFRGAIN, 0, 0x1D, 1, hf_tlv),
602 603
	SOC_SINGLE_TLV("Earphone Playback Volume",
		TWL6040_REG_EARCTL, 1, 0xF, 1, ep_tlv),
604

605
	SOC_ENUM_EXT("Headset Power Mode", twl6040_power_mode_enum,
606 607
		twl6040_headset_power_get_enum,
		twl6040_headset_power_put_enum),
608 609 610

	SOC_ENUM_EXT("PLL Selection", twl6040_power_mode_enum,
		twl6040_pll_get_enum, twl6040_pll_put_enum),
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
};

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"),
626
	SND_SOC_DAPM_OUTPUT("EP"),
627 628
	SND_SOC_DAPM_OUTPUT("AUXL"),
	SND_SOC_DAPM_OUTPUT("AUXR"),
629 630
	SND_SOC_DAPM_OUTPUT("VIBRAL"),
	SND_SOC_DAPM_OUTPUT("VIBRAR"),
631 632 633 634 635 636 637 638 639 640 641 642 643

	/* 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),

644 645 646 647 648 649
	/* 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),

650
	/* ADCs */
651 652
	SND_SOC_DAPM_ADC("ADC Left", NULL, TWL6040_REG_MICLCTL, 2, 0),
	SND_SOC_DAPM_ADC("ADC Right", NULL, TWL6040_REG_MICRCTL, 2, 0),
653 654

	/* Microphone bias */
655 656 657 658 659 660 661 662
	SND_SOC_DAPM_SUPPLY("Headset Mic Bias",
			    TWL6040_REG_AMICBCTL, 0, 0, NULL, 0),
	SND_SOC_DAPM_SUPPLY("Main Mic Bias",
			    TWL6040_REG_AMICBCTL, 4, 0, NULL, 0),
	SND_SOC_DAPM_SUPPLY("Digital Mic1 Bias",
			    TWL6040_REG_DMICBCTL, 0, 0, NULL, 0),
	SND_SOC_DAPM_SUPPLY("Digital Mic2 Bias",
			    TWL6040_REG_DMICBCTL, 4, 0, NULL, 0),
663 664

	/* DACs */
665 666 667 668
	SND_SOC_DAPM_DAC("HSDAC Left", NULL, SND_SOC_NOPM, 0, 0),
	SND_SOC_DAPM_DAC("HSDAC Right", NULL, SND_SOC_NOPM, 0, 0),
	SND_SOC_DAPM_DAC("HFDAC Left", NULL, TWL6040_REG_HFLCTL, 0, 0),
	SND_SOC_DAPM_DAC("HFDAC Right", NULL, TWL6040_REG_HFRCTL, 0, 0),
669
	/* Virtual DAC for vibra path (DL4 channel) */
670
	SND_SOC_DAPM_DAC("VIBRA DAC", NULL, SND_SOC_NOPM, 0, 0),
671

672
	SND_SOC_DAPM_MUX("Handsfree Left Playback",
673
			SND_SOC_NOPM, 0, 0, &hfl_mux_controls),
674
	SND_SOC_DAPM_MUX("Handsfree Right Playback",
675 676
			SND_SOC_NOPM, 0, 0, &hfr_mux_controls),
	/* Analog playback Muxes */
677
	SND_SOC_DAPM_MUX("Headset Left Playback",
678
			SND_SOC_NOPM, 0, 0, &hsl_mux_controls),
679
	SND_SOC_DAPM_MUX("Headset Right Playback",
680
			SND_SOC_NOPM, 0, 0, &hsr_mux_controls),
681

682 683 684 685 686
	SND_SOC_DAPM_MUX("Vibra Left Playback", SND_SOC_NOPM, 0, 0,
			&vibral_mux_controls),
	SND_SOC_DAPM_MUX("Vibra Right Playback", SND_SOC_NOPM, 0, 0,
			&vibrar_mux_controls),

687 688
	SND_SOC_DAPM_SWITCH("Earphone Playback", SND_SOC_NOPM, 0, 0,
			&ep_path_enable_control),
689 690 691 692
	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),
693

694
	/* Analog playback drivers */
695 696 697 698 699 700 701 702
	SND_SOC_DAPM_OUT_DRV("HF Left Driver",
			TWL6040_REG_HFLCTL, 4, 0, NULL, 0),
	SND_SOC_DAPM_OUT_DRV("HF Right Driver",
			TWL6040_REG_HFRCTL, 4, 0, NULL, 0),
	SND_SOC_DAPM_OUT_DRV("HS Left Driver",
			TWL6040_REG_HSLCTL, 2, 0, NULL, 0),
	SND_SOC_DAPM_OUT_DRV("HS Right Driver",
			TWL6040_REG_HSRCTL, 2, 0, NULL, 0),
703 704
	SND_SOC_DAPM_OUT_DRV_E("Earphone Driver",
			TWL6040_REG_EARCTL, 0, 0, NULL, 0,
705
			twl6040_ep_drv_event,
706
			SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
707 708 709 710 711 712 713 714 715
	SND_SOC_DAPM_OUT_DRV("Vibra Left Driver",
			TWL6040_REG_VIBCTLL, 0, 0, NULL, 0),
	SND_SOC_DAPM_OUT_DRV("Vibra Right Driver",
			TWL6040_REG_VIBCTLR, 0, 0, NULL, 0),

	SND_SOC_DAPM_SUPPLY("Vibra Left Control", TWL6040_REG_VIBCTLL, 2, 0,
			    NULL, 0),
	SND_SOC_DAPM_SUPPLY("Vibra Right Control", TWL6040_REG_VIBCTLR, 2, 0,
			    NULL, 0),
716 717 718
	SND_SOC_DAPM_SUPPLY_S("HSDAC Power", 1, SND_SOC_NOPM, 0, 0,
			      twl6040_hs_dac_event,
			      SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
719 720

	/* Analog playback PGAs */
721
	SND_SOC_DAPM_PGA("HF Left PGA",
722
			TWL6040_REG_HFLCTL, 1, 0, NULL, 0),
723
	SND_SOC_DAPM_PGA("HF Right PGA",
724 725 726 727 728
			TWL6040_REG_HFRCTL, 1, 0, NULL, 0),

};

static const struct snd_soc_dapm_route intercon[] = {
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743
	/* Stream -> DAC mapping */
	{"HSDAC Left", NULL, "Legacy Playback"},
	{"HSDAC Left", NULL, "Headset Playback"},
	{"HSDAC Right", NULL, "Legacy Playback"},
	{"HSDAC Right", NULL, "Headset Playback"},

	{"HFDAC Left", NULL, "Legacy Playback"},
	{"HFDAC Left", NULL, "Handsfree Playback"},
	{"HFDAC Right", NULL, "Legacy Playback"},
	{"HFDAC Right", NULL, "Handsfree Playback"},

	{"VIBRA DAC", NULL, "Legacy Playback"},
	{"VIBRA DAC", NULL, "Vibra Playback"},

	/* ADC -> Stream mapping */
744 745 746 747
	{"Legacy Capture" , NULL, "ADC Left"},
	{"Capture", NULL, "ADC Left"},
	{"Legacy Capture", NULL, "ADC Right"},
	{"Capture" , NULL, "ADC Right"},
748

749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
	/* 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"},

764
	/* AFM path */
765 766
	{"AFMAmpL", NULL, "AFML"},
	{"AFMAmpR", NULL, "AFMR"},
767

768 769 770
	{"HSDAC Left", NULL, "HSDAC Power"},
	{"HSDAC Right", NULL, "HSDAC Power"},

771 772
	{"Headset Left Playback", "HS DAC", "HSDAC Left"},
	{"Headset Left Playback", "Line-In amp", "AFMAmpL"},
773

774 775
	{"Headset Right Playback", "HS DAC", "HSDAC Right"},
	{"Headset Right Playback", "Line-In amp", "AFMAmpR"},
776

777 778
	{"HS Left Driver", NULL, "Headset Left Playback"},
	{"HS Right Driver", NULL, "Headset Right Playback"},
779

780 781
	{"HSOL", NULL, "HS Left Driver"},
	{"HSOR", NULL, "HS Right Driver"},
782

783
	/* Earphone playback path */
784 785
	{"Earphone Playback", "Switch", "HSDAC Left"},
	{"Earphone Driver", NULL, "Earphone Playback"},
786 787
	{"EP", NULL, "Earphone Driver"},

788 789
	{"Handsfree Left Playback", "HF DAC", "HFDAC Left"},
	{"Handsfree Left Playback", "Line-In amp", "AFMAmpL"},
790

791 792
	{"Handsfree Right Playback", "HF DAC", "HFDAC Right"},
	{"Handsfree Right Playback", "Line-In amp", "AFMAmpR"},
793

794 795
	{"HF Left PGA", NULL, "Handsfree Left Playback"},
	{"HF Right PGA", NULL, "Handsfree Right Playback"},
796

797 798
	{"HF Left Driver", NULL, "HF Left PGA"},
	{"HF Right Driver", NULL, "HF Right PGA"},
799

800 801
	{"HFL", NULL, "HF Left Driver"},
	{"HFR", NULL, "HF Right Driver"},
802 803 804 805 806 807

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

	{"AUXL", NULL, "AUXL Playback"},
	{"AUXR", NULL, "AUXR Playback"},
808 809 810 811 812 813 814 815 816 817 818 819

	/* Vibrator paths */
	{"Vibra Left Playback", "Audio PDM", "VIBRA DAC"},
	{"Vibra Right Playback", "Audio PDM", "VIBRA DAC"},

	{"Vibra Left Driver", NULL, "Vibra Left Playback"},
	{"Vibra Right Driver", NULL, "Vibra Right Playback"},
	{"Vibra Left Driver", NULL, "Vibra Left Control"},
	{"Vibra Right Driver", NULL, "Vibra Right Control"},

	{"VIBRAL", NULL, "Vibra Left Driver"},
	{"VIBRAR", NULL, "Vibra Right Driver"},
820 821 822 823 824
};

static int twl6040_set_bias_level(struct snd_soc_codec *codec,
				enum snd_soc_bias_level level)
{
825
	struct twl6040 *twl6040 = codec->control_data;
826
	struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
827 828 829 830 831 832 833 834 835 836 837
	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;

838 839 840
		ret = twl6040_power(twl6040, 1);
		if (ret)
			return ret;
841

842
		priv->codec_powered = 1;
843

844 845
		/* Set external boost GPO */
		twl6040_write(codec, TWL6040_REG_GPOCTL, 0x02);
846 847 848 849 850
		break;
	case SND_SOC_BIAS_OFF:
		if (!priv->codec_powered)
			break;

851
		twl6040_power(twl6040, 0);
852 853 854 855
		priv->codec_powered = 0;
		break;
	}

L
Liam Girdwood 已提交
856
	codec->dapm.bias_level = level;
857 858 859 860 861 862 863

	return 0;
}

static int twl6040_startup(struct snd_pcm_substream *substream,
			struct snd_soc_dai *dai)
{
864
	struct snd_soc_codec *codec = dai->codec;
865
	struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
866 867 868

	snd_pcm_hw_constraint_list(substream->runtime, 0,
				SNDRV_PCM_HW_PARAM_RATE,
869
				&sysclk_constraints[priv->pll_power_mode]);
870 871 872 873 874 875 876 877

	return 0;
}

static int twl6040_hw_params(struct snd_pcm_substream *substream,
			struct snd_pcm_hw_params *params,
			struct snd_soc_dai *dai)
{
878
	struct snd_soc_codec *codec = dai->codec;
879
	struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
880 881 882 883
	int rate;

	rate = params_rate(params);
	switch (rate) {
884 885 886
	case 11250:
	case 22500:
	case 44100:
887
	case 88200:
888 889 890 891 892 893
		/* 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;
		}
894 895
		priv->sysclk = 17640000;
		break;
896 897 898 899
	case 8000:
	case 16000:
	case 32000:
	case 48000:
900 901 902 903 904 905 906 907 908 909 910
	case 96000:
		priv->sysclk = 19200000;
		break;
	default:
		dev_err(codec->dev, "unsupported rate %d\n", rate);
		return -EINVAL;
	}

	return 0;
}

911 912
static int twl6040_prepare(struct snd_pcm_substream *substream,
			struct snd_soc_dai *dai)
913
{
914
	struct snd_soc_codec *codec = dai->codec;
915
	struct twl6040 *twl6040 = codec->control_data;
916
	struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
917
	int ret;
918

919 920 921 922 923 924
	if (!priv->sysclk) {
		dev_err(codec->dev,
			"no mclk configured, call set_sysclk() on init\n");
		return -EINVAL;
	}

925 926 927 928 929 930
	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;
	}

931 932 933 934 935 936 937
	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;
938
	struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
939 940 941 942

	switch (clk_id) {
	case TWL6040_SYSCLK_SEL_LPPLL:
	case TWL6040_SYSCLK_SEL_HPPLL:
943 944
		priv->pll = clk_id;
		priv->clk_in = freq;
945 946 947 948 949 950 951 952 953
		break;
	default:
		dev_err(codec->dev, "unknown clk_id %d\n", clk_id);
		return -EINVAL;
	}

	return 0;
}

954 955 956 957 958 959 960 961 962 963
static void twl6040_mute_path(struct snd_soc_codec *codec, enum twl6040_dai_id id,
			     int mute)
{
	struct twl6040 *twl6040 = codec->control_data;
	struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
	int hslctl, hsrctl, earctl;
	int hflctl, hfrctl;

	switch (id) {
	case TWL6040_DAI_DL1:
964 965 966
		hslctl = twl6040_read(codec, TWL6040_REG_HSLCTL);
		hsrctl = twl6040_read(codec, TWL6040_REG_HSRCTL);
		earctl = twl6040_read(codec, TWL6040_REG_EARCTL);
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981

		if (mute) {
			/* Power down drivers and DACs */
			earctl &= ~0x01;
			hslctl &= ~(TWL6040_HSDRVENA | TWL6040_HSDACENA);
			hsrctl &= ~(TWL6040_HSDRVENA | TWL6040_HSDACENA);

		}

		twl6040_reg_write(twl6040, TWL6040_REG_EARCTL, earctl);
		twl6040_reg_write(twl6040, TWL6040_REG_HSLCTL, hslctl);
		twl6040_reg_write(twl6040, TWL6040_REG_HSRCTL, hsrctl);
		priv->dl1_unmuted = !mute;
		break;
	case TWL6040_DAI_DL2:
982 983
		hflctl = twl6040_read(codec, TWL6040_REG_HFLCTL);
		hfrctl = twl6040_read(codec, TWL6040_REG_HFRCTL);
984 985 986 987 988 989 990 991 992 993 994 995 996 997 998

		if (mute) {
			/* Power down drivers and DACs */
			hflctl &= ~(TWL6040_HFDACENA | TWL6040_HFPGAENA |
				    TWL6040_HFDRVENA);
			hfrctl &= ~(TWL6040_HFDACENA | TWL6040_HFPGAENA |
				    TWL6040_HFDRVENA);
		}

		twl6040_reg_write(twl6040, TWL6040_REG_HFLCTL, hflctl);
		twl6040_reg_write(twl6040, TWL6040_REG_HFRCTL, hfrctl);
		priv->dl2_unmuted = !mute;
		break;
	default:
		break;
999
	}
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
}

static int twl6040_digital_mute(struct snd_soc_dai *dai, int mute)
{
	switch (dai->id) {
	case TWL6040_DAI_LEGACY:
		twl6040_mute_path(dai->codec, TWL6040_DAI_DL1, mute);
		twl6040_mute_path(dai->codec, TWL6040_DAI_DL2, mute);
		break;
	case TWL6040_DAI_DL1:
	case TWL6040_DAI_DL2:
		twl6040_mute_path(dai->codec, dai->id, mute);
		break;
	default:
		break;
	}

	return 0;
}

1020
static const struct snd_soc_dai_ops twl6040_dai_ops = {
1021 1022
	.startup	= twl6040_startup,
	.hw_params	= twl6040_hw_params,
1023
	.prepare	= twl6040_prepare,
1024
	.set_sysclk	= twl6040_set_dai_sysclk,
1025
	.digital_mute	= twl6040_digital_mute,
1026 1027
};

L
Liam Girdwood 已提交
1028
static struct snd_soc_dai_driver twl6040_dai[] = {
1029
{
1030
	.name = "twl6040-legacy",
1031
	.id = TWL6040_DAI_LEGACY,
1032
	.playback = {
1033
		.stream_name = "Legacy Playback",
1034
		.channels_min = 1,
1035
		.channels_max = 5,
1036 1037 1038 1039
		.rates = TWL6040_RATES,
		.formats = TWL6040_FORMATS,
	},
	.capture = {
1040
		.stream_name = "Legacy Capture",
1041 1042
		.channels_min = 1,
		.channels_max = 2,
1043 1044 1045
		.rates = TWL6040_RATES,
		.formats = TWL6040_FORMATS,
	},
1046 1047
	.ops = &twl6040_dai_ops,
},
L
Liam Girdwood 已提交
1048 1049
{
	.name = "twl6040-ul",
1050
	.id = TWL6040_DAI_UL,
1051 1052 1053 1054 1055 1056 1057 1058
	.capture = {
		.stream_name = "Capture",
		.channels_min = 1,
		.channels_max = 2,
		.rates = TWL6040_RATES,
		.formats = TWL6040_FORMATS,
	},
	.ops = &twl6040_dai_ops,
L
Liam Girdwood 已提交
1059 1060 1061
},
{
	.name = "twl6040-dl1",
1062
	.id = TWL6040_DAI_DL1,
1063
	.playback = {
L
Liam Girdwood 已提交
1064
		.stream_name = "Headset Playback",
1065
		.channels_min = 1,
L
Liam Girdwood 已提交
1066
		.channels_max = 2,
1067 1068 1069
		.rates = TWL6040_RATES,
		.formats = TWL6040_FORMATS,
	},
L
Liam Girdwood 已提交
1070 1071 1072 1073
	.ops = &twl6040_dai_ops,
},
{
	.name = "twl6040-dl2",
1074
	.id = TWL6040_DAI_DL2,
L
Liam Girdwood 已提交
1075 1076
	.playback = {
		.stream_name = "Handsfree Playback",
1077 1078 1079 1080 1081 1082
		.channels_min = 1,
		.channels_max = 2,
		.rates = TWL6040_RATES,
		.formats = TWL6040_FORMATS,
	},
	.ops = &twl6040_dai_ops,
L
Liam Girdwood 已提交
1083 1084 1085
},
{
	.name = "twl6040-vib",
1086
	.id = TWL6040_DAI_VIB,
L
Liam Girdwood 已提交
1087 1088
	.playback = {
		.stream_name = "Vibra Playback",
1089 1090
		.channels_min = 1,
		.channels_max = 1,
L
Liam Girdwood 已提交
1091 1092 1093 1094 1095
		.rates = SNDRV_PCM_RATE_CONTINUOUS,
		.formats = TWL6040_FORMATS,
	},
	.ops = &twl6040_dai_ops,
},
1096 1097 1098
};

#ifdef CONFIG_PM
1099
static int twl6040_suspend(struct snd_soc_codec *codec)
1100 1101 1102 1103 1104 1105
{
	twl6040_set_bias_level(codec, SND_SOC_BIAS_OFF);

	return 0;
}

1106
static int twl6040_resume(struct snd_soc_codec *codec)
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
{
	twl6040_set_bias_level(codec, SND_SOC_BIAS_STANDBY);

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

1117
static int twl6040_probe(struct snd_soc_codec *codec)
1118 1119
{
	struct twl6040_data *priv;
1120
	struct twl6040 *twl6040 = dev_get_drvdata(codec->dev->parent);
1121 1122
	struct platform_device *pdev = container_of(codec->dev,
						   struct platform_device, dev);
1123 1124
	int ret = 0;

1125
	priv = devm_kzalloc(codec->dev, sizeof(*priv), GFP_KERNEL);
1126 1127
	if (priv == NULL)
		return -ENOMEM;
1128

1129
	snd_soc_codec_set_drvdata(codec, priv);
1130

1131
	priv->codec = codec;
1132
	codec->control_data = twl6040;
1133

1134 1135 1136
	priv->plug_irq = platform_get_irq(pdev, 0);
	if (priv->plug_irq < 0) {
		dev_err(codec->dev, "invalid irq\n");
1137
		return -EINVAL;
1138
	}
1139

1140
	INIT_DELAYED_WORK(&priv->hs_jack.work, twl6040_accessory_work);
1141 1142

	mutex_init(&priv->mutex);
1143

1144
	ret = request_threaded_irq(priv->plug_irq, NULL,
1145 1146
					twl6040_audio_handler, IRQF_NO_SUSPEND,
					"twl6040_irq_plug", codec);
1147 1148
	if (ret) {
		dev_err(codec->dev, "PLUG IRQ request failed: %d\n", ret);
1149
		return ret;
1150 1151
	}

1152
	twl6040_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1153
	twl6040_init_chip(codec);
1154

1155
	return 0;
1156 1157
}

1158
static int twl6040_remove(struct snd_soc_codec *codec)
1159
{
1160 1161 1162
	struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);

	free_irq(priv->plug_irq, codec);
1163
	twl6040_set_bias_level(codec, SND_SOC_BIAS_OFF);
1164

1165 1166
	return 0;
}
1167

1168 1169 1170 1171 1172
static struct snd_soc_codec_driver soc_codec_dev_twl6040 = {
	.probe = twl6040_probe,
	.remove = twl6040_remove,
	.suspend = twl6040_suspend,
	.resume = twl6040_resume,
1173
	.read = twl6040_read,
1174 1175
	.write = twl6040_write,
	.set_bias_level = twl6040_set_bias_level,
1176
	.ignore_pmdown_time = true,
1177 1178 1179 1180 1181 1182 1183

	.controls = twl6040_snd_controls,
	.num_controls = ARRAY_SIZE(twl6040_snd_controls),
	.dapm_widgets = twl6040_dapm_widgets,
	.num_dapm_widgets = ARRAY_SIZE(twl6040_dapm_widgets),
	.dapm_routes = intercon,
	.num_dapm_routes = ARRAY_SIZE(intercon),
1184 1185
};

1186
static int twl6040_codec_probe(struct platform_device *pdev)
1187
{
L
Liam Girdwood 已提交
1188 1189
	return snd_soc_register_codec(&pdev->dev, &soc_codec_dev_twl6040,
				      twl6040_dai, ARRAY_SIZE(twl6040_dai));
1190 1191
}

1192
static int twl6040_codec_remove(struct platform_device *pdev)
1193 1194
{
	snd_soc_unregister_codec(&pdev->dev);
1195 1196 1197 1198 1199
	return 0;
}

static struct platform_driver twl6040_codec_driver = {
	.driver = {
1200
		.name = "twl6040-codec",
1201 1202 1203
		.owner = THIS_MODULE,
	},
	.probe = twl6040_codec_probe,
1204
	.remove = twl6040_codec_remove,
1205 1206
};

1207
module_platform_driver(twl6040_codec_driver);
1208 1209 1210 1211

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