tlv320dac33.c 42.0 KB
Newer Older
1 2 3
/*
 * ALSA SoC Texas Instruments TLV320DAC33 codec driver
 *
4
 * Author: Peter Ujfalusi <peter.ujfalusi@ti.com>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
 *
 * Copyright:   (C) 2009 Nokia Corporation
 *
 * 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/i2c.h>
#include <linux/interrupt.h>
#include <linux/gpio.h>
32
#include <linux/regulator/consumer.h>
33
#include <linux/slab.h>
34 35 36 37 38 39 40 41 42 43
#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 <sound/tlv320dac33-plat.h>
#include "tlv320dac33.h"

44 45 46 47 48 49 50 51 52
/*
 * The internal FIFO is 24576 bytes long
 * It can be configured to hold 16bit or 24bit samples
 * In 16bit configuration the FIFO can hold 6144 stereo samples
 * In 24bit configuration the FIFO can hold 4096 stereo samples
 */
#define DAC33_FIFO_SIZE_16BIT	6144
#define DAC33_FIFO_SIZE_24BIT	4096
#define DAC33_MODE7_MARGIN	10	/* Safety margin for FIFO in Mode7 */
53

54 55
#define BURST_BASEFREQ_HZ	49152000

56
#define SAMPLES_TO_US(rate, samples) \
57
	(1000000000 / (((rate) * 1000) / (samples)))
58 59

#define US_TO_SAMPLES(rate, us) \
60
	((rate) / (1000000 / ((us) < 1000000 ? (us) : 1000000)))
61

62
#define UTHR_FROM_PERIOD_SIZE(samples, playrate, burstrate) \
63
	(((samples)*5000) / (((burstrate)*5000) / ((burstrate) - (playrate))))
64

65 66 67 68
static void dac33_calculate_times(struct snd_pcm_substream *substream,
				  struct snd_soc_codec *codec);
static int dac33_prepare_chip(struct snd_pcm_substream *substream,
			      struct snd_soc_codec *codec);
69

70 71 72 73 74 75 76
enum dac33_state {
	DAC33_IDLE = 0,
	DAC33_PREFILL,
	DAC33_PLAYBACK,
	DAC33_FLUSH,
};

77 78 79
enum dac33_fifo_modes {
	DAC33_FIFO_BYPASS = 0,
	DAC33_FIFO_MODE1,
80
	DAC33_FIFO_MODE7,
81 82 83
	DAC33_FIFO_LAST_MODE,
};

84 85 86 87 88 89 90
#define DAC33_NUM_SUPPLIES 3
static const char *dac33_supply_names[DAC33_NUM_SUPPLIES] = {
	"AVDD",
	"DVDD",
	"IOVDD",
};

91 92 93
struct tlv320dac33_priv {
	struct mutex mutex;
	struct work_struct work;
94
	struct snd_soc_codec *codec;
95
	struct regulator_bulk_data supplies[DAC33_NUM_SUPPLIES];
96
	struct snd_pcm_substream *substream;
97 98 99 100 101 102
	int power_gpio;
	int chip_power;
	int irq;
	unsigned int refclk;

	unsigned int alarm_threshold;	/* set to be half of LATENCY_TIME_MS */
103
	enum dac33_fifo_modes fifo_mode;/* FIFO mode selection */
104
	unsigned int fifo_size;		/* Size of the FIFO in samples */
105
	unsigned int nsample;		/* burst read amount from host */
106 107
	int mode1_latency;		/* latency caused by the i2c writes in
					 * us */
108
	u8 burst_bclkdiv;		/* BCLK divider value in burst mode */
109
	u8 *reg_cache;
110
	unsigned int burst_rate;	/* Interface speed in Burst modes */
111

112 113
	int keep_bclk;			/* Keep the BCLK continuously running
					 * in FIFO modes */
114 115 116 117 118 119 120
	spinlock_t lock;
	unsigned long long t_stamp1;	/* Time stamp for FIFO modes to */
	unsigned long long t_stamp2;	/* calculate the FIFO caused delay */

	unsigned int mode1_us_burst;	/* Time to burst read n number of
					 * samples */
	unsigned int mode7_us_to_lthr;	/* Time to reach lthr from uthr */
121

122 123
	unsigned int uthr;

124
	enum dac33_state state;
125
	struct i2c_client *i2c;
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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
};

static const u8 dac33_reg[DAC33_CACHEREGNUM] = {
0x00, 0x00, 0x00, 0x00, /* 0x00 - 0x03 */
0x00, 0x00, 0x00, 0x00, /* 0x04 - 0x07 */
0x00, 0x00, 0x00, 0x00, /* 0x08 - 0x0b */
0x00, 0x00, 0x00, 0x00, /* 0x0c - 0x0f */
0x00, 0x00, 0x00, 0x00, /* 0x10 - 0x13 */
0x00, 0x00, 0x00, 0x00, /* 0x14 - 0x17 */
0x00, 0x00, 0x00, 0x00, /* 0x18 - 0x1b */
0x00, 0x00, 0x00, 0x00, /* 0x1c - 0x1f */
0x00, 0x00, 0x00, 0x00, /* 0x20 - 0x23 */
0x00, 0x00, 0x00, 0x00, /* 0x24 - 0x27 */
0x00, 0x00, 0x00, 0x00, /* 0x28 - 0x2b */
0x00, 0x00, 0x00, 0x80, /* 0x2c - 0x2f */
0x80, 0x00, 0x00, 0x00, /* 0x30 - 0x33 */
0x00, 0x00, 0x00, 0x00, /* 0x34 - 0x37 */
0x00, 0x00,             /* 0x38 - 0x39 */
/* Registers 0x3a - 0x3f are reserved  */
            0x00, 0x00, /* 0x3a - 0x3b */
0x00, 0x00, 0x00, 0x00, /* 0x3c - 0x3f */

0x00, 0x00, 0x00, 0x00, /* 0x40 - 0x43 */
0x00, 0x80,             /* 0x44 - 0x45 */
/* Registers 0x46 - 0x47 are reserved  */
            0x80, 0x80, /* 0x46 - 0x47 */

0x80, 0x00, 0x00,       /* 0x48 - 0x4a */
/* Registers 0x4b - 0x7c are reserved  */
                  0x00, /* 0x4b        */
0x00, 0x00, 0x00, 0x00, /* 0x4c - 0x4f */
0x00, 0x00, 0x00, 0x00, /* 0x50 - 0x53 */
0x00, 0x00, 0x00, 0x00, /* 0x54 - 0x57 */
0x00, 0x00, 0x00, 0x00, /* 0x58 - 0x5b */
0x00, 0x00, 0x00, 0x00, /* 0x5c - 0x5f */
0x00, 0x00, 0x00, 0x00, /* 0x60 - 0x63 */
0x00, 0x00, 0x00, 0x00, /* 0x64 - 0x67 */
0x00, 0x00, 0x00, 0x00, /* 0x68 - 0x6b */
0x00, 0x00, 0x00, 0x00, /* 0x6c - 0x6f */
0x00, 0x00, 0x00, 0x00, /* 0x70 - 0x73 */
0x00, 0x00, 0x00, 0x00, /* 0x74 - 0x77 */
0x00, 0x00, 0x00, 0x00, /* 0x78 - 0x7b */
0x00,                   /* 0x7c        */

      0xda, 0x33, 0x03, /* 0x7d - 0x7f */
};

/* Register read and write */
static inline unsigned int dac33_read_reg_cache(struct snd_soc_codec *codec,
						unsigned reg)
{
177 178
	struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
	u8 *cache = dac33->reg_cache;
179 180 181 182 183 184 185 186 187
	if (reg >= DAC33_CACHEREGNUM)
		return 0;

	return cache[reg];
}

static inline void dac33_write_reg_cache(struct snd_soc_codec *codec,
					 u8 reg, u8 value)
{
188 189
	struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
	u8 *cache = dac33->reg_cache;
190 191 192 193 194 195 196 197 198
	if (reg >= DAC33_CACHEREGNUM)
		return;

	cache[reg] = value;
}

static int dac33_read(struct snd_soc_codec *codec, unsigned int reg,
		      u8 *value)
{
199
	struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
200
	int val, ret = 0;
201 202 203 204 205

	*value = reg & 0xff;

	/* If powered off, return the cached value */
	if (dac33->chip_power) {
206
		val = i2c_smbus_read_byte_data(dac33->i2c, value[0]);
207 208 209
		if (val < 0) {
			dev_err(codec->dev, "Read failed (%d)\n", val);
			value[0] = dac33_read_reg_cache(codec, reg);
210
			ret = val;
211 212 213 214 215 216 217 218
		} else {
			value[0] = val;
			dac33_write_reg_cache(codec, reg, val);
		}
	} else {
		value[0] = dac33_read_reg_cache(codec, reg);
	}

219
	return ret;
220 221 222 223 224
}

static int dac33_write(struct snd_soc_codec *codec, unsigned int reg,
		       unsigned int value)
{
225
	struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
226 227 228 229 230 231 232 233 234 235 236 237 238
	u8 data[2];
	int ret = 0;

	/*
	 * data is
	 *   D15..D8 dac33 register offset
	 *   D7...D0 register data
	 */
	data[0] = reg & 0xff;
	data[1] = value & 0xff;

	dac33_write_reg_cache(codec, data[0], data[1]);
	if (dac33->chip_power) {
239
		ret = i2c_master_send(dac33->i2c, data, 2);
240 241 242 243 244 245 246 247 248
		if (ret != 2)
			dev_err(codec->dev, "Write failed (%d)\n", ret);
		else
			ret = 0;
	}

	return ret;
}

249 250 251 252 253 254 255 256 257 258 259 260 261
static int dac33_write_locked(struct snd_soc_codec *codec, unsigned int reg,
			      unsigned int value)
{
	struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
	int ret;

	mutex_lock(&dac33->mutex);
	ret = dac33_write(codec, reg, value);
	mutex_unlock(&dac33->mutex);

	return ret;
}

262 263 264 265
#define DAC33_I2C_ADDR_AUTOINC	0x80
static int dac33_write16(struct snd_soc_codec *codec, unsigned int reg,
		       unsigned int value)
{
266
	struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
	u8 data[3];
	int ret = 0;

	/*
	 * data is
	 *   D23..D16 dac33 register offset
	 *   D15..D8  register data MSB
	 *   D7...D0  register data LSB
	 */
	data[0] = reg & 0xff;
	data[1] = (value >> 8) & 0xff;
	data[2] = value & 0xff;

	dac33_write_reg_cache(codec, data[0], data[1]);
	dac33_write_reg_cache(codec, data[0] + 1, data[2]);

	if (dac33->chip_power) {
		/* We need to set autoincrement mode for 16 bit writes */
		data[0] |= DAC33_I2C_ADDR_AUTOINC;
286
		ret = i2c_master_send(dac33->i2c, data, 3);
287 288 289 290 291 292 293 294 295
		if (ret != 3)
			dev_err(codec->dev, "Write failed (%d)\n", ret);
		else
			ret = 0;
	}

	return ret;
}

296
static void dac33_init_chip(struct snd_soc_codec *codec)
297
{
298
	struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
299

300
	if (unlikely(!dac33->chip_power))
301 302
		return;

303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
	/* A : DAC sample rate Fsref/1.5 */
	dac33_write(codec, DAC33_DAC_CTRL_A, DAC33_DACRATE(0));
	/* B : DAC src=normal, not muted */
	dac33_write(codec, DAC33_DAC_CTRL_B, DAC33_DACSRCR_RIGHT |
					     DAC33_DACSRCL_LEFT);
	/* C : (defaults) */
	dac33_write(codec, DAC33_DAC_CTRL_C, 0x00);

	/* 73 : volume soft stepping control,
	 clock source = internal osc (?) */
	dac33_write(codec, DAC33_ANA_VOL_SOFT_STEP_CTRL, DAC33_VOLCLKEN);

	/* Restore only selected registers (gains mostly) */
	dac33_write(codec, DAC33_LDAC_DIG_VOL_CTRL,
		    dac33_read_reg_cache(codec, DAC33_LDAC_DIG_VOL_CTRL));
	dac33_write(codec, DAC33_RDAC_DIG_VOL_CTRL,
		    dac33_read_reg_cache(codec, DAC33_RDAC_DIG_VOL_CTRL));

	dac33_write(codec, DAC33_LINEL_TO_LLO_VOL,
		    dac33_read_reg_cache(codec, DAC33_LINEL_TO_LLO_VOL));
	dac33_write(codec, DAC33_LINER_TO_RLO_VOL,
		    dac33_read_reg_cache(codec, DAC33_LINER_TO_RLO_VOL));
325 326 327 328

	dac33_write(codec, DAC33_OUT_AMP_CTRL,
		    dac33_read_reg_cache(codec, DAC33_OUT_AMP_CTRL));

329 330 331 332
	dac33_write(codec, DAC33_LDAC_PWR_CTRL,
		    dac33_read_reg_cache(codec, DAC33_LDAC_PWR_CTRL));
	dac33_write(codec, DAC33_RDAC_PWR_CTRL,
		    dac33_read_reg_cache(codec, DAC33_RDAC_PWR_CTRL));
333 334
}

335
static inline int dac33_read_id(struct snd_soc_codec *codec)
336
{
337
	int i, ret = 0;
338 339
	u8 reg;

340 341 342 343 344 345 346
	for (i = 0; i < 3; i++) {
		ret = dac33_read(codec, DAC33_DEVICE_ID_MSB + i, &reg);
		if (ret < 0)
			break;
	}

	return ret;
347 348 349 350 351 352 353 354 355 356
}

static inline void dac33_soft_power(struct snd_soc_codec *codec, int power)
{
	u8 reg;

	reg = dac33_read_reg_cache(codec, DAC33_PWR_CTRL);
	if (power)
		reg |= DAC33_PDNALLB;
	else
357 358
		reg &= ~(DAC33_PDNALLB | DAC33_OSCPDNB |
			 DAC33_DACRPDNB | DAC33_DACLPDNB);
359 360 361
	dac33_write(codec, DAC33_PWR_CTRL, reg);
}

362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
static inline void dac33_disable_digital(struct snd_soc_codec *codec)
{
	u8 reg;

	/* Stop the DAI clock */
	reg = dac33_read_reg_cache(codec, DAC33_SER_AUDIOIF_CTRL_B);
	reg &= ~DAC33_BCLKON;
	dac33_write(codec, DAC33_SER_AUDIOIF_CTRL_B, reg);

	/* Power down the Oscillator, and DACs */
	reg = dac33_read_reg_cache(codec, DAC33_PWR_CTRL);
	reg &= ~(DAC33_OSCPDNB | DAC33_DACRPDNB | DAC33_DACLPDNB);
	dac33_write(codec, DAC33_PWR_CTRL, reg);
}

377
static int dac33_hard_power(struct snd_soc_codec *codec, int power)
378
{
379
	struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
380
	int ret = 0;
381 382

	mutex_lock(&dac33->mutex);
383 384 385

	/* Safety check */
	if (unlikely(power == dac33->chip_power)) {
386
		dev_dbg(codec->dev, "Trying to set the same power state: %s\n",
387 388 389 390
			power ? "ON" : "OFF");
		goto exit;
	}

391
	if (power) {
392 393 394 395 396 397
		ret = regulator_bulk_enable(ARRAY_SIZE(dac33->supplies),
					  dac33->supplies);
		if (ret != 0) {
			dev_err(codec->dev,
				"Failed to enable supplies: %d\n", ret);
				goto exit;
398
		}
399 400 401 402 403

		if (dac33->power_gpio >= 0)
			gpio_set_value(dac33->power_gpio, 1);

		dac33->chip_power = 1;
404 405
	} else {
		dac33_soft_power(codec, 0);
406
		if (dac33->power_gpio >= 0)
407
			gpio_set_value(dac33->power_gpio, 0);
408 409 410 411 412 413 414

		ret = regulator_bulk_disable(ARRAY_SIZE(dac33->supplies),
					     dac33->supplies);
		if (ret != 0) {
			dev_err(codec->dev,
				"Failed to disable supplies: %d\n", ret);
			goto exit;
415
		}
416 417

		dac33->chip_power = 0;
418 419
	}

420 421 422
exit:
	mutex_unlock(&dac33->mutex);
	return ret;
423 424
}

425
static int dac33_playback_event(struct snd_soc_dapm_widget *w,
426 427
		struct snd_kcontrol *kcontrol, int event)
{
428 429
	struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
	struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
430 431 432 433

	switch (event) {
	case SND_SOC_DAPM_PRE_PMU:
		if (likely(dac33->substream)) {
434 435
			dac33_calculate_times(dac33->substream, codec);
			dac33_prepare_chip(dac33->substream, codec);
436 437
		}
		break;
438
	case SND_SOC_DAPM_POST_PMD:
439
		dac33_disable_digital(codec);
440
		break;
441 442 443 444
	}
	return 0;
}

445
static int dac33_get_fifo_mode(struct snd_kcontrol *kcontrol,
446 447
			 struct snd_ctl_elem_value *ucontrol)
{
448
	struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
449
	struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
450

451
	ucontrol->value.enumerated.item[0] = dac33->fifo_mode;
452 453 454 455

	return 0;
}

456
static int dac33_set_fifo_mode(struct snd_kcontrol *kcontrol,
457 458
			 struct snd_ctl_elem_value *ucontrol)
{
459
	struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
460
	struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
461 462
	int ret = 0;

463
	if (dac33->fifo_mode == ucontrol->value.enumerated.item[0])
464 465
		return 0;
	/* Do not allow changes while stream is running*/
466
	if (snd_soc_codec_is_active(codec))
467 468
		return -EPERM;

469
	if (ucontrol->value.enumerated.item[0] >= DAC33_FIFO_LAST_MODE)
470 471
		ret = -EINVAL;
	else
472
		dac33->fifo_mode = ucontrol->value.enumerated.item[0];
473 474 475 476

	return ret;
}

477 478
/* Codec operation modes */
static const char *dac33_fifo_mode_texts[] = {
479
	"Bypass", "Mode 1", "Mode 7"
480 481
};

482
static SOC_ENUM_SINGLE_EXT_DECL(dac33_fifo_mode_enum, dac33_fifo_mode_texts);
483

484 485 486 487 488 489
/* L/R Line Output Gain */
static const char *lr_lineout_gain_texts[] = {
	"Line -12dB DAC 0dB", "Line -6dB DAC 6dB",
	"Line 0dB DAC 12dB", "Line 6dB DAC 18dB",
};

490 491 492
static SOC_ENUM_SINGLE_DECL(l_lineout_gain_enum,
			    DAC33_LDAC_PWR_CTRL, 0,
			    lr_lineout_gain_texts);
493

494 495 496
static SOC_ENUM_SINGLE_DECL(r_lineout_gain_enum,
			    DAC33_RDAC_PWR_CTRL, 0,
			    lr_lineout_gain_texts);
497

498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
/*
 * DACL/R digital volume control:
 * from 0 dB to -63.5 in 0.5 dB steps
 * Need to be inverted later on:
 * 0x00 == 0 dB
 * 0x7f == -63.5 dB
 */
static DECLARE_TLV_DB_SCALE(dac_digivol_tlv, -6350, 50, 0);

static const struct snd_kcontrol_new dac33_snd_controls[] = {
	SOC_DOUBLE_R_TLV("DAC Digital Playback Volume",
		DAC33_LDAC_DIG_VOL_CTRL, DAC33_RDAC_DIG_VOL_CTRL,
		0, 0x7f, 1, dac_digivol_tlv),
	SOC_DOUBLE_R("DAC Digital Playback Switch",
		 DAC33_LDAC_DIG_VOL_CTRL, DAC33_RDAC_DIG_VOL_CTRL, 7, 1, 1),
	SOC_DOUBLE_R("Line to Line Out Volume",
		 DAC33_LINEL_TO_LLO_VOL, DAC33_LINER_TO_RLO_VOL, 0, 127, 1),
515 516
	SOC_ENUM("Left Line Output Gain", l_lineout_gain_enum),
	SOC_ENUM("Right Line Output Gain", r_lineout_gain_enum),
517 518
};

519 520 521 522 523
static const struct snd_kcontrol_new dac33_mode_snd_controls[] = {
	SOC_ENUM_EXT("FIFO Mode", dac33_fifo_mode_enum,
		 dac33_get_fifo_mode, dac33_set_fifo_mode),
};

524 525 526 527 528 529 530
/* Analog bypass */
static const struct snd_kcontrol_new dac33_dapm_abypassl_control =
	SOC_DAPM_SINGLE("Switch", DAC33_LINEL_TO_LLO_VOL, 7, 1, 1);

static const struct snd_kcontrol_new dac33_dapm_abypassr_control =
	SOC_DAPM_SINGLE("Switch", DAC33_LINER_TO_RLO_VOL, 7, 1, 1);

531 532 533
/* LOP L/R invert selection */
static const char *dac33_lr_lom_texts[] = {"DAC", "LOP"};

534 535 536
static SOC_ENUM_SINGLE_DECL(dac33_left_lom_enum,
			    DAC33_OUT_AMP_CTRL, 3,
			    dac33_lr_lom_texts);
537 538 539 540

static const struct snd_kcontrol_new dac33_dapm_left_lom_control =
SOC_DAPM_ENUM("Route", dac33_left_lom_enum);

541 542 543
static SOC_ENUM_SINGLE_DECL(dac33_right_lom_enum,
			    DAC33_OUT_AMP_CTRL, 2,
			    dac33_lr_lom_texts);
544 545 546 547

static const struct snd_kcontrol_new dac33_dapm_right_lom_control =
SOC_DAPM_ENUM("Route", dac33_right_lom_enum);

548 549 550 551 552 553 554
static const struct snd_soc_dapm_widget dac33_dapm_widgets[] = {
	SND_SOC_DAPM_OUTPUT("LEFT_LO"),
	SND_SOC_DAPM_OUTPUT("RIGHT_LO"),

	SND_SOC_DAPM_INPUT("LINEL"),
	SND_SOC_DAPM_INPUT("LINER"),

555 556
	SND_SOC_DAPM_DAC("DACL", "Left Playback", SND_SOC_NOPM, 0, 0),
	SND_SOC_DAPM_DAC("DACR", "Right Playback", SND_SOC_NOPM, 0, 0),
557 558 559 560 561 562 563

	/* Analog bypass */
	SND_SOC_DAPM_SWITCH("Analog Left Bypass", SND_SOC_NOPM, 0, 0,
				&dac33_dapm_abypassl_control),
	SND_SOC_DAPM_SWITCH("Analog Right Bypass", SND_SOC_NOPM, 0, 0,
				&dac33_dapm_abypassr_control),

564 565 566 567 568 569 570 571 572 573 574 575
	SND_SOC_DAPM_MUX("Left LOM Inverted From", SND_SOC_NOPM, 0, 0,
		&dac33_dapm_left_lom_control),
	SND_SOC_DAPM_MUX("Right LOM Inverted From", SND_SOC_NOPM, 0, 0,
		&dac33_dapm_right_lom_control),
	/*
	 * For DAPM path, when only the anlog bypass path is enabled, and the
	 * LOP inverted from the corresponding DAC side.
	 * This is needed, so we can attach the DAC power supply in this case.
	 */
	SND_SOC_DAPM_PGA("Left Bypass PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
	SND_SOC_DAPM_PGA("Right Bypass PGA", SND_SOC_NOPM, 0, 0, NULL, 0),

576
	SND_SOC_DAPM_REG(snd_soc_dapm_mixer, "Output Left Amplifier",
577
			 DAC33_OUT_AMP_PWR_CTRL, 6, 3, 3, 0),
578
	SND_SOC_DAPM_REG(snd_soc_dapm_mixer, "Output Right Amplifier",
579
			 DAC33_OUT_AMP_PWR_CTRL, 4, 3, 3, 0),
580

581 582 583 584 585
	SND_SOC_DAPM_SUPPLY("Left DAC Power",
			    DAC33_LDAC_PWR_CTRL, 2, 0, NULL, 0),
	SND_SOC_DAPM_SUPPLY("Right DAC Power",
			    DAC33_RDAC_PWR_CTRL, 2, 0, NULL, 0),

586 587 588
	SND_SOC_DAPM_SUPPLY("Codec Power",
			    DAC33_PWR_CTRL, 4, 0, NULL, 0),

589 590
	SND_SOC_DAPM_PRE("Pre Playback", dac33_playback_event),
	SND_SOC_DAPM_POST("Post Playback", dac33_playback_event),
591 592 593 594 595 596 597
};

static const struct snd_soc_dapm_route audio_map[] = {
	/* Analog bypass */
	{"Analog Left Bypass", "Switch", "LINEL"},
	{"Analog Right Bypass", "Switch", "LINER"},

598 599
	{"Output Left Amplifier", NULL, "DACL"},
	{"Output Right Amplifier", NULL, "DACR"},
600

601 602 603 604 605 606 607 608 609 610 611 612 613
	{"Left Bypass PGA", NULL, "Analog Left Bypass"},
	{"Right Bypass PGA", NULL, "Analog Right Bypass"},

	{"Left LOM Inverted From", "DAC", "Left Bypass PGA"},
	{"Right LOM Inverted From", "DAC", "Right Bypass PGA"},
	{"Left LOM Inverted From", "LOP", "Analog Left Bypass"},
	{"Right LOM Inverted From", "LOP", "Analog Right Bypass"},

	{"Output Left Amplifier", NULL, "Left LOM Inverted From"},
	{"Output Right Amplifier", NULL, "Right LOM Inverted From"},

	{"DACL", NULL, "Left DAC Power"},
	{"DACR", NULL, "Right DAC Power"},
614

615 616
	{"Left Bypass PGA", NULL, "Left DAC Power"},
	{"Right Bypass PGA", NULL, "Right DAC Power"},
617

618
	/* output */
619 620
	{"LEFT_LO", NULL, "Output Left Amplifier"},
	{"RIGHT_LO", NULL, "Output Right Amplifier"},
621 622 623

	{"LEFT_LO", NULL, "Codec Power"},
	{"RIGHT_LO", NULL, "Codec Power"},
624 625 626 627 628
};

static int dac33_set_bias_level(struct snd_soc_codec *codec,
				enum snd_soc_bias_level level)
{
629 630
	int ret;

631 632 633 634 635 636
	switch (level) {
	case SND_SOC_BIAS_ON:
		break;
	case SND_SOC_BIAS_PREPARE:
		break;
	case SND_SOC_BIAS_STANDBY:
637
		if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_OFF) {
638
			/* Coming from OFF, switch on the codec */
639 640 641 642
			ret = dac33_hard_power(codec, 1);
			if (ret != 0)
				return ret;

643 644
			dac33_init_chip(codec);
		}
645 646
		break;
	case SND_SOC_BIAS_OFF:
647
		/* Do not power off, when the codec is already off */
648
		if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_OFF)
649
			return 0;
650 651 652
		ret = dac33_hard_power(codec, 0);
		if (ret != 0)
			return ret;
653 654 655 656 657 658
		break;
	}

	return 0;
}

659 660
static inline void dac33_prefill_handler(struct tlv320dac33_priv *dac33)
{
661
	struct snd_soc_codec *codec = dac33->codec;
662
	unsigned int delay;
663
	unsigned long flags;
664 665 666 667

	switch (dac33->fifo_mode) {
	case DAC33_FIFO_MODE1:
		dac33_write16(codec, DAC33_NSAMPLE_MSB,
668
			DAC33_THRREG(dac33->nsample));
669 670

		/* Take the timestamps */
671
		spin_lock_irqsave(&dac33->lock, flags);
672 673
		dac33->t_stamp2 = ktime_to_us(ktime_get());
		dac33->t_stamp1 = dac33->t_stamp2;
674
		spin_unlock_irqrestore(&dac33->lock, flags);
675

676 677
		dac33_write16(codec, DAC33_PREFILL_MSB,
				DAC33_THRREG(dac33->alarm_threshold));
678
		/* Enable Alarm Threshold IRQ with a delay */
679 680 681
		delay = SAMPLES_TO_US(dac33->burst_rate,
				     dac33->alarm_threshold) + 1000;
		usleep_range(delay, delay + 500);
682
		dac33_write(codec, DAC33_FIFO_IRQ_MASK, DAC33_MAT);
683
		break;
684
	case DAC33_FIFO_MODE7:
685
		/* Take the timestamp */
686
		spin_lock_irqsave(&dac33->lock, flags);
687 688 689
		dac33->t_stamp1 = ktime_to_us(ktime_get());
		/* Move back the timestamp with drain time */
		dac33->t_stamp1 -= dac33->mode7_us_to_lthr;
690
		spin_unlock_irqrestore(&dac33->lock, flags);
691

692
		dac33_write16(codec, DAC33_PREFILL_MSB,
693
				DAC33_THRREG(DAC33_MODE7_MARGIN));
694 695 696

		/* Enable Upper Threshold IRQ */
		dac33_write(codec, DAC33_FIFO_IRQ_MASK, DAC33_MUT);
697
		break;
698 699 700 701 702 703 704 705 706
	default:
		dev_warn(codec->dev, "Unhandled FIFO mode: %d\n",
							dac33->fifo_mode);
		break;
	}
}

static inline void dac33_playback_handler(struct tlv320dac33_priv *dac33)
{
707
	struct snd_soc_codec *codec = dac33->codec;
708
	unsigned long flags;
709 710 711

	switch (dac33->fifo_mode) {
	case DAC33_FIFO_MODE1:
712
		/* Take the timestamp */
713
		spin_lock_irqsave(&dac33->lock, flags);
714
		dac33->t_stamp2 = ktime_to_us(ktime_get());
715
		spin_unlock_irqrestore(&dac33->lock, flags);
716

717 718 719
		dac33_write16(codec, DAC33_NSAMPLE_MSB,
				DAC33_THRREG(dac33->nsample));
		break;
720 721 722
	case DAC33_FIFO_MODE7:
		/* At the moment we are not using interrupts in mode7 */
		break;
723 724 725 726 727 728 729
	default:
		dev_warn(codec->dev, "Unhandled FIFO mode: %d\n",
							dac33->fifo_mode);
		break;
	}
}

730 731 732 733 734 735 736
static void dac33_work(struct work_struct *work)
{
	struct snd_soc_codec *codec;
	struct tlv320dac33_priv *dac33;
	u8 reg;

	dac33 = container_of(work, struct tlv320dac33_priv, work);
737
	codec = dac33->codec;
738 739 740 741 742

	mutex_lock(&dac33->mutex);
	switch (dac33->state) {
	case DAC33_PREFILL:
		dac33->state = DAC33_PLAYBACK;
743
		dac33_prefill_handler(dac33);
744 745
		break;
	case DAC33_PLAYBACK:
746
		dac33_playback_handler(dac33);
747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
		break;
	case DAC33_IDLE:
		break;
	case DAC33_FLUSH:
		dac33->state = DAC33_IDLE;
		/* Mask all interrupts from dac33 */
		dac33_write(codec, DAC33_FIFO_IRQ_MASK, 0);

		/* flush fifo */
		reg = dac33_read_reg_cache(codec, DAC33_FIFO_CTRL_A);
		reg |= DAC33_FIFOFLUSH;
		dac33_write(codec, DAC33_FIFO_CTRL_A, reg);
		break;
	}
	mutex_unlock(&dac33->mutex);
}

static irqreturn_t dac33_interrupt_handler(int irq, void *dev)
{
	struct snd_soc_codec *codec = dev;
767
	struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
768
	unsigned long flags;
769

770
	spin_lock_irqsave(&dac33->lock, flags);
771
	dac33->t_stamp1 = ktime_to_us(ktime_get());
772
	spin_unlock_irqrestore(&dac33->lock, flags);
773

774 775
	/* Do not schedule the workqueue in Mode7 */
	if (dac33->fifo_mode != DAC33_FIFO_MODE7)
776
		schedule_work(&dac33->work);
777 778 779 780 781 782

	return IRQ_HANDLED;
}

static void dac33_oscwait(struct snd_soc_codec *codec)
{
783
	int timeout = 60;
784 785 786
	u8 reg;

	do {
787
		usleep_range(1000, 2000);
788 789 790 791 792 793 794
		dac33_read(codec, DAC33_INT_OSC_STATUS, &reg);
	} while (((reg & 0x03) != DAC33_OSCSTATUS_NORMAL) && timeout--);
	if ((reg & 0x03) != DAC33_OSCSTATUS_NORMAL)
		dev_err(codec->dev,
			"internal oscillator calibration failed\n");
}

795 796 797
static int dac33_startup(struct snd_pcm_substream *substream,
			   struct snd_soc_dai *dai)
{
798
	struct snd_soc_codec *codec = dai->codec;
799 800 801 802 803 804 805 806 807 808 809
	struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);

	/* Stream started, save the substream pointer */
	dac33->substream = substream;

	return 0;
}

static void dac33_shutdown(struct snd_pcm_substream *substream,
			     struct snd_soc_dai *dai)
{
810
	struct snd_soc_codec *codec = dai->codec;
811 812 813 814 815
	struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);

	dac33->substream = NULL;
}

816 817
#define CALC_BURST_RATE(bclkdiv, bclk_per_sample) \
	(BURST_BASEFREQ_HZ / bclkdiv / bclk_per_sample)
818 819 820 821
static int dac33_hw_params(struct snd_pcm_substream *substream,
			   struct snd_pcm_hw_params *params,
			   struct snd_soc_dai *dai)
{
822
	struct snd_soc_codec *codec = dai->codec;
823
	struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
824 825 826 827 828 829 830 831 832 833 834 835

	/* Check parameters for validity */
	switch (params_rate(params)) {
	case 44100:
	case 48000:
		break;
	default:
		dev_err(codec->dev, "unsupported rate %d\n",
			params_rate(params));
		return -EINVAL;
	}

836 837
	switch (params_width(params)) {
	case 16:
838 839
		dac33->fifo_size = DAC33_FIFO_SIZE_16BIT;
		dac33->burst_rate = CALC_BURST_RATE(dac33->burst_bclkdiv, 32);
840
		break;
841
	case 32:
842 843 844
		dac33->fifo_size = DAC33_FIFO_SIZE_24BIT;
		dac33->burst_rate = CALC_BURST_RATE(dac33->burst_bclkdiv, 64);
		break;
845
	default:
846 847
		dev_err(codec->dev, "unsupported width %d\n",
			params_width(params));
848 849 850 851 852 853 854
		return -EINVAL;
	}

	return 0;
}

#define CALC_OSCSET(rate, refclk) ( \
855
	((((rate * 10000) / refclk) * 4096) + 7000) / 10000)
856 857 858 859 860 861 862 863
#define CALC_RATIOSET(rate, refclk) ( \
	((((refclk  * 100000) / rate) * 16384) + 50000) / 100000)

/*
 * tlv320dac33 is strict on the sequence of the register writes, if the register
 * writes happens in different order, than dac33 might end up in unknown state.
 * Use the known, working sequence of register writes to initialize the dac33.
 */
864 865
static int dac33_prepare_chip(struct snd_pcm_substream *substream,
			      struct snd_soc_codec *codec)
866
{
867
	struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
868
	unsigned int oscset, ratioset, pwr_ctrl, reg_tmp;
869
	u8 aictrl_a, aictrl_b, fifoctrl_a;
870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886

	switch (substream->runtime->rate) {
	case 44100:
	case 48000:
		oscset = CALC_OSCSET(substream->runtime->rate, dac33->refclk);
		ratioset = CALC_RATIOSET(substream->runtime->rate,
					 dac33->refclk);
		break;
	default:
		dev_err(codec->dev, "unsupported rate %d\n",
			substream->runtime->rate);
		return -EINVAL;
	}


	aictrl_a = dac33_read_reg_cache(codec, DAC33_SER_AUDIOIF_CTRL_A);
	aictrl_a &= ~(DAC33_NCYCL_MASK | DAC33_WLEN_MASK);
887
	/* Read FIFO control A, and clear FIFO flush bit */
888
	fifoctrl_a = dac33_read_reg_cache(codec, DAC33_FIFO_CTRL_A);
889 890
	fifoctrl_a &= ~DAC33_FIFOFLUSH;

891 892 893 894 895 896
	fifoctrl_a &= ~DAC33_WIDTH;
	switch (substream->runtime->format) {
	case SNDRV_PCM_FORMAT_S16_LE:
		aictrl_a |= (DAC33_NCYCL_16 | DAC33_WLEN_16);
		fifoctrl_a |= DAC33_WIDTH;
		break;
897 898 899
	case SNDRV_PCM_FORMAT_S32_LE:
		aictrl_a |= (DAC33_NCYCL_32 | DAC33_WLEN_24);
		break;
900 901 902 903 904 905 906
	default:
		dev_err(codec->dev, "unsupported format %d\n",
			substream->runtime->format);
		return -EINVAL;
	}

	mutex_lock(&dac33->mutex);
907 908 909 910 911 912 913 914 915 916

	if (!dac33->chip_power) {
		/*
		 * Chip is not powered yet.
		 * Do the init in the dac33_set_bias_level later.
		 */
		mutex_unlock(&dac33->mutex);
		return 0;
	}

917
	dac33_soft_power(codec, 0);
918 919 920 921 922 923 924 925
	dac33_soft_power(codec, 1);

	reg_tmp = dac33_read_reg_cache(codec, DAC33_INT_OSC_CTRL);
	dac33_write(codec, DAC33_INT_OSC_CTRL, reg_tmp);

	/* Write registers 0x08 and 0x09 (MSB, LSB) */
	dac33_write16(codec, DAC33_INT_OSC_FREQ_RAT_A, oscset);

926 927
	/* OSC calibration time */
	dac33_write(codec, DAC33_CALIB_TIME, 96);
928 929 930 931 932 933 934 935 936 937 938 939 940 941

	/* adjustment treshold & step */
	dac33_write(codec, DAC33_INT_OSC_CTRL_B, DAC33_ADJTHRSHLD(2) |
						 DAC33_ADJSTEP(1));

	/* div=4 / gain=1 / div */
	dac33_write(codec, DAC33_INT_OSC_CTRL_C, DAC33_REFDIV(4));

	pwr_ctrl = dac33_read_reg_cache(codec, DAC33_PWR_CTRL);
	pwr_ctrl |= DAC33_OSCPDNB | DAC33_DACRPDNB | DAC33_DACLPDNB;
	dac33_write(codec, DAC33_PWR_CTRL, pwr_ctrl);

	dac33_oscwait(codec);

942
	if (dac33->fifo_mode) {
943
		/* Generic for all FIFO modes */
944
		/* 50-51 : ASRC Control registers */
945
		dac33_write(codec, DAC33_ASRC_CTRL_A, DAC33_SRCLKDIV(1));
946 947 948 949 950 951 952 953
		dac33_write(codec, DAC33_ASRC_CTRL_B, 1); /* ??? */

		/* Write registers 0x34 and 0x35 (MSB, LSB) */
		dac33_write16(codec, DAC33_SRC_REF_CLK_RATIO_A, ratioset);

		/* Set interrupts to high active */
		dac33_write(codec, DAC33_INTP_CTRL_A, DAC33_INTPM_AHIGH);
	} else {
954
		/* FIFO bypass mode */
955 956 957 958 959
		/* 50-51 : ASRC Control registers */
		dac33_write(codec, DAC33_ASRC_CTRL_A, DAC33_SRCBYP);
		dac33_write(codec, DAC33_ASRC_CTRL_B, 0); /* ??? */
	}

960 961 962 963 964 965
	/* Interrupt behaviour configuration */
	switch (dac33->fifo_mode) {
	case DAC33_FIFO_MODE1:
		dac33_write(codec, DAC33_FIFO_IRQ_MODE_B,
			    DAC33_ATM(DAC33_FIFO_IRQ_MODE_LEVEL));
		break;
966
	case DAC33_FIFO_MODE7:
967 968
		dac33_write(codec, DAC33_FIFO_IRQ_MODE_A,
			DAC33_UTM(DAC33_FIFO_IRQ_MODE_LEVEL));
969
		break;
970 971 972 973 974 975 976 977 978 979 980 981 982 983 984
	default:
		/* in FIFO bypass mode, the interrupts are not used */
		break;
	}

	aictrl_b = dac33_read_reg_cache(codec, DAC33_SER_AUDIOIF_CTRL_B);

	switch (dac33->fifo_mode) {
	case DAC33_FIFO_MODE1:
		/*
		 * For mode1:
		 * Disable the FIFO bypass (Enable the use of FIFO)
		 * Select nSample mode
		 * BCLK is only running when data is needed by DAC33
		 */
985
		fifoctrl_a &= ~DAC33_FBYPAS;
986
		fifoctrl_a &= ~DAC33_FAUTO;
987 988 989 990
		if (dac33->keep_bclk)
			aictrl_b |= DAC33_BCLKON;
		else
			aictrl_b &= ~DAC33_BCLKON;
991
		break;
992 993 994 995 996 997 998 999 1000
	case DAC33_FIFO_MODE7:
		/*
		 * For mode1:
		 * Disable the FIFO bypass (Enable the use of FIFO)
		 * Select Threshold mode
		 * BCLK is only running when data is needed by DAC33
		 */
		fifoctrl_a &= ~DAC33_FBYPAS;
		fifoctrl_a |= DAC33_FAUTO;
1001 1002 1003 1004
		if (dac33->keep_bclk)
			aictrl_b |= DAC33_BCLKON;
		else
			aictrl_b &= ~DAC33_BCLKON;
1005
		break;
1006 1007 1008 1009
	default:
		/*
		 * For FIFO bypass mode:
		 * Enable the FIFO bypass (Disable the FIFO use)
L
Lucas De Marchi 已提交
1010
		 * Set the BCLK as continuous
1011
		 */
1012
		fifoctrl_a |= DAC33_FBYPAS;
1013 1014 1015
		aictrl_b |= DAC33_BCLKON;
		break;
	}
1016

1017
	dac33_write(codec, DAC33_FIFO_CTRL_A, fifoctrl_a);
1018
	dac33_write(codec, DAC33_SER_AUDIOIF_CTRL_A, aictrl_a);
1019
	dac33_write(codec, DAC33_SER_AUDIOIF_CTRL_B, aictrl_b);
1020

1021 1022 1023 1024 1025 1026 1027 1028 1029
	/*
	 * BCLK divide ratio
	 * 0: 1.5
	 * 1: 1
	 * 2: 2
	 * ...
	 * 254: 254
	 * 255: 255
	 */
1030
	if (dac33->fifo_mode)
1031 1032
		dac33_write(codec, DAC33_SER_AUDIOIF_CTRL_C,
							dac33->burst_bclkdiv);
1033
	else
1034 1035 1036 1037
		if (substream->runtime->format == SNDRV_PCM_FORMAT_S16_LE)
			dac33_write(codec, DAC33_SER_AUDIOIF_CTRL_C, 32);
		else
			dac33_write(codec, DAC33_SER_AUDIOIF_CTRL_C, 16);
1038

1039 1040
	switch (dac33->fifo_mode) {
	case DAC33_FIFO_MODE1:
1041 1042
		dac33_write16(codec, DAC33_ATHR_MSB,
			      DAC33_THRREG(dac33->alarm_threshold));
1043
		break;
1044 1045 1046 1047 1048
	case DAC33_FIFO_MODE7:
		/*
		 * Configure the threshold levels, and leave 10 sample space
		 * at the bottom, and also at the top of the FIFO
		 */
1049
		dac33_write16(codec, DAC33_UTHR_MSB, DAC33_THRREG(dac33->uthr));
1050 1051
		dac33_write16(codec, DAC33_LTHR_MSB,
			      DAC33_THRREG(DAC33_MODE7_MARGIN));
1052
		break;
1053 1054
	default:
		break;
1055 1056 1057 1058 1059 1060 1061
	}

	mutex_unlock(&dac33->mutex);

	return 0;
}

1062 1063
static void dac33_calculate_times(struct snd_pcm_substream *substream,
				  struct snd_soc_codec *codec)
1064
{
1065
	struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
1066 1067
	unsigned int period_size = substream->runtime->period_size;
	unsigned int rate = substream->runtime->rate;
1068 1069
	unsigned int nsample_limit;

1070 1071 1072 1073
	/* In bypass mode we don't need to calculate */
	if (!dac33->fifo_mode)
		return;

1074 1075
	switch (dac33->fifo_mode) {
	case DAC33_FIFO_MODE1:
1076 1077 1078
		/* Number of samples under i2c latency */
		dac33->alarm_threshold = US_TO_SAMPLES(rate,
						dac33->mode1_latency);
1079
		nsample_limit = dac33->fifo_size - dac33->alarm_threshold;
1080

1081
		if (period_size <= dac33->alarm_threshold)
1082
			/*
1083 1084
			 * Configure nSamaple to number of periods,
			 * which covers the latency requironment.
1085
			 */
1086 1087 1088 1089 1090 1091 1092 1093
			dac33->nsample = period_size *
				((dac33->alarm_threshold / period_size) +
				(dac33->alarm_threshold % period_size ?
				1 : 0));
		else if (period_size > nsample_limit)
			dac33->nsample = nsample_limit;
		else
			dac33->nsample = period_size;
1094

1095 1096 1097 1098 1099 1100
		dac33->mode1_us_burst = SAMPLES_TO_US(dac33->burst_rate,
						      dac33->nsample);
		dac33->t_stamp1 = 0;
		dac33->t_stamp2 = 0;
		break;
	case DAC33_FIFO_MODE7:
1101 1102
		dac33->uthr = UTHR_FROM_PERIOD_SIZE(period_size, rate,
						    dac33->burst_rate) + 9;
1103 1104 1105 1106
		if (dac33->uthr > (dac33->fifo_size - DAC33_MODE7_MARGIN))
			dac33->uthr = dac33->fifo_size - DAC33_MODE7_MARGIN;
		if (dac33->uthr < (DAC33_MODE7_MARGIN + 10))
			dac33->uthr = (DAC33_MODE7_MARGIN + 10);
1107

1108
		dac33->mode7_us_to_lthr =
1109
				SAMPLES_TO_US(substream->runtime->rate,
1110
					dac33->uthr - DAC33_MODE7_MARGIN + 1);
1111 1112 1113 1114 1115
		dac33->t_stamp1 = 0;
		break;
	default:
		break;
	}
1116 1117 1118 1119 1120 1121

}

static int dac33_pcm_trigger(struct snd_pcm_substream *substream, int cmd,
			     struct snd_soc_dai *dai)
{
1122
	struct snd_soc_codec *codec = dai->codec;
1123
	struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
1124 1125 1126 1127 1128 1129
	int ret = 0;

	switch (cmd) {
	case SNDRV_PCM_TRIGGER_START:
	case SNDRV_PCM_TRIGGER_RESUME:
	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1130
		if (dac33->fifo_mode) {
1131
			dac33->state = DAC33_PREFILL;
1132
			schedule_work(&dac33->work);
1133 1134 1135 1136 1137
		}
		break;
	case SNDRV_PCM_TRIGGER_STOP:
	case SNDRV_PCM_TRIGGER_SUSPEND:
	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1138
		if (dac33->fifo_mode) {
1139
			dac33->state = DAC33_FLUSH;
1140
			schedule_work(&dac33->work);
1141 1142 1143 1144 1145 1146 1147 1148 1149
		}
		break;
	default:
		ret = -EINVAL;
	}

	return ret;
}

1150 1151 1152 1153
static snd_pcm_sframes_t dac33_dai_delay(
			struct snd_pcm_substream *substream,
			struct snd_soc_dai *dai)
{
1154
	struct snd_soc_codec *codec = dai->codec;
1155 1156
	struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
	unsigned long long t0, t1, t_now;
1157
	unsigned int time_delta, uthr;
1158 1159
	int samples_out, samples_in, samples;
	snd_pcm_sframes_t delay = 0;
1160
	unsigned long flags;
1161 1162 1163 1164 1165

	switch (dac33->fifo_mode) {
	case DAC33_FIFO_BYPASS:
		break;
	case DAC33_FIFO_MODE1:
1166
		spin_lock_irqsave(&dac33->lock, flags);
1167 1168
		t0 = dac33->t_stamp1;
		t1 = dac33->t_stamp2;
1169
		spin_unlock_irqrestore(&dac33->lock, flags);
1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
		t_now = ktime_to_us(ktime_get());

		/* We have not started to fill the FIFO yet, delay is 0 */
		if (!t1)
			goto out;

		if (t0 > t1) {
			/*
			 * Phase 1:
			 * After Alarm threshold, and before nSample write
			 */
			time_delta = t_now - t0;
			samples_out = time_delta ? US_TO_SAMPLES(
						substream->runtime->rate,
						time_delta) : 0;

			if (likely(dac33->alarm_threshold > samples_out))
				delay = dac33->alarm_threshold - samples_out;
			else
				delay = 0;
		} else if ((t_now - t1) <= dac33->mode1_us_burst) {
			/*
			 * Phase 2:
			 * After nSample write (during burst operation)
			 */
			time_delta = t_now - t0;
			samples_out = time_delta ? US_TO_SAMPLES(
						substream->runtime->rate,
						time_delta) : 0;

			time_delta = t_now - t1;
			samples_in = time_delta ? US_TO_SAMPLES(
						dac33->burst_rate,
						time_delta) : 0;

			samples = dac33->alarm_threshold;
			samples += (samples_in - samples_out);

			if (likely(samples > 0))
				delay = samples;
			else
				delay = 0;
		} else {
			/*
			 * Phase 3:
			 * After burst operation, before next alarm threshold
			 */
			time_delta = t_now - t0;
			samples_out = time_delta ? US_TO_SAMPLES(
						substream->runtime->rate,
						time_delta) : 0;

			samples_in = dac33->nsample;
			samples = dac33->alarm_threshold;
			samples += (samples_in - samples_out);

			if (likely(samples > 0))
1227 1228
				delay = samples > dac33->fifo_size ?
					dac33->fifo_size : samples;
1229 1230 1231 1232 1233
			else
				delay = 0;
		}
		break;
	case DAC33_FIFO_MODE7:
1234
		spin_lock_irqsave(&dac33->lock, flags);
1235
		t0 = dac33->t_stamp1;
1236
		uthr = dac33->uthr;
1237
		spin_unlock_irqrestore(&dac33->lock, flags);
1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
		t_now = ktime_to_us(ktime_get());

		/* We have not started to fill the FIFO yet, delay is 0 */
		if (!t0)
			goto out;

		if (t_now <= t0) {
			/*
			 * Either the timestamps are messed or equal. Report
			 * maximum delay
			 */
1249
			delay = uthr;
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262
			goto out;
		}

		time_delta = t_now - t0;
		if (time_delta <= dac33->mode7_us_to_lthr) {
			/*
			* Phase 1:
			* After burst (draining phase)
			*/
			samples_out = US_TO_SAMPLES(
					substream->runtime->rate,
					time_delta);

1263 1264
			if (likely(uthr > samples_out))
				delay = uthr - samples_out;
1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279
			else
				delay = 0;
		} else {
			/*
			* Phase 2:
			* During burst operation
			*/
			time_delta = time_delta - dac33->mode7_us_to_lthr;

			samples_out = US_TO_SAMPLES(
					substream->runtime->rate,
					time_delta);
			samples_in = US_TO_SAMPLES(
					dac33->burst_rate,
					time_delta);
1280
			delay = DAC33_MODE7_MARGIN + samples_in - samples_out;
1281

1282 1283
			if (unlikely(delay > uthr))
				delay = uthr;
1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
		}
		break;
	default:
		dev_warn(codec->dev, "Unhandled FIFO mode: %d\n",
							dac33->fifo_mode);
		break;
	}
out:
	return delay;
}

1295 1296 1297 1298
static int dac33_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;
1299
	struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328
	u8 ioc_reg, asrcb_reg;

	ioc_reg = dac33_read_reg_cache(codec, DAC33_INT_OSC_CTRL);
	asrcb_reg = dac33_read_reg_cache(codec, DAC33_ASRC_CTRL_B);
	switch (clk_id) {
	case TLV320DAC33_MCLK:
		ioc_reg |= DAC33_REFSEL;
		asrcb_reg |= DAC33_SRCREFSEL;
		break;
	case TLV320DAC33_SLEEPCLK:
		ioc_reg &= ~DAC33_REFSEL;
		asrcb_reg &= ~DAC33_SRCREFSEL;
		break;
	default:
		dev_err(codec->dev, "Invalid clock ID (%d)\n", clk_id);
		break;
	}
	dac33->refclk = freq;

	dac33_write_reg_cache(codec, DAC33_INT_OSC_CTRL, ioc_reg);
	dac33_write_reg_cache(codec, DAC33_ASRC_CTRL_B, asrcb_reg);

	return 0;
}

static int dac33_set_dai_fmt(struct snd_soc_dai *codec_dai,
			     unsigned int fmt)
{
	struct snd_soc_codec *codec = codec_dai->codec;
1329
	struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341
	u8 aictrl_a, aictrl_b;

	aictrl_a = dac33_read_reg_cache(codec, DAC33_SER_AUDIOIF_CTRL_A);
	aictrl_b = dac33_read_reg_cache(codec, DAC33_SER_AUDIOIF_CTRL_B);
	/* set master/slave audio interface */
	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
	case SND_SOC_DAIFMT_CBM_CFM:
		/* Codec Master */
		aictrl_a |= (DAC33_MSBCLK | DAC33_MSWCLK);
		break;
	case SND_SOC_DAIFMT_CBS_CFS:
		/* Codec Slave */
1342 1343 1344 1345 1346
		if (dac33->fifo_mode) {
			dev_err(codec->dev, "FIFO mode requires master mode\n");
			return -EINVAL;
		} else
			aictrl_a &= ~(DAC33_MSBCLK | DAC33_MSWCLK);
1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359
		break;
	default:
		return -EINVAL;
	}

	aictrl_a &= ~DAC33_AFMT_MASK;
	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
	case SND_SOC_DAIFMT_I2S:
		aictrl_a |= DAC33_AFMT_I2S;
		break;
	case SND_SOC_DAIFMT_DSP_A:
		aictrl_a |= DAC33_AFMT_DSP;
		aictrl_b &= ~DAC33_DATA_DELAY_MASK;
1360
		aictrl_b |= DAC33_DATA_DELAY(0);
1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379
		break;
	case SND_SOC_DAIFMT_RIGHT_J:
		aictrl_a |= DAC33_AFMT_RIGHT_J;
		break;
	case SND_SOC_DAIFMT_LEFT_J:
		aictrl_a |= DAC33_AFMT_LEFT_J;
		break;
	default:
		dev_err(codec->dev, "Unsupported format (%u)\n",
			fmt & SND_SOC_DAIFMT_FORMAT_MASK);
		return -EINVAL;
	}

	dac33_write_reg_cache(codec, DAC33_SER_AUDIOIF_CTRL_A, aictrl_a);
	dac33_write_reg_cache(codec, DAC33_SER_AUDIOIF_CTRL_B, aictrl_b);

	return 0;
}

1380
static int dac33_soc_probe(struct snd_soc_codec *codec)
1381
{
1382
	struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
1383 1384
	int ret = 0;

1385
	dac33->codec = codec;
1386

1387 1388 1389 1390 1391 1392
	/* Read the tlv320dac33 ID registers */
	ret = dac33_hard_power(codec, 1);
	if (ret != 0) {
		dev_err(codec->dev, "Failed to power up codec: %d\n", ret);
		goto err_power;
	}
1393
	ret = dac33_read_id(codec);
1394
	dac33_hard_power(codec, 0);
1395

1396 1397 1398 1399 1400 1401
	if (ret < 0) {
		dev_err(codec->dev, "Failed to read chip ID: %d\n", ret);
		ret = -ENODEV;
		goto err_power;
	}

1402 1403 1404
	/* Check if the IRQ number is valid and request it */
	if (dac33->irq >= 0) {
		ret = request_irq(dac33->irq, dac33_interrupt_handler,
Y
Yong Zhang 已提交
1405
				  IRQF_TRIGGER_RISING,
1406
				  codec->component.name, codec);
1407 1408 1409 1410 1411 1412 1413 1414
		if (ret < 0) {
			dev_err(codec->dev, "Could not request IRQ%d (%d)\n",
						dac33->irq, ret);
			dac33->irq = -1;
		}
		if (dac33->irq != -1) {
			INIT_WORK(&dac33->work, dac33_work);
		}
1415 1416
	}

1417
	/* Only add the FIFO controls, if we have valid IRQ number */
1418
	if (dac33->irq >= 0)
1419
		snd_soc_add_codec_controls(codec, dac33_mode_snd_controls,
1420
				     ARRAY_SIZE(dac33_mode_snd_controls));
1421

1422
err_power:
1423 1424 1425
	return ret;
}

1426
static int dac33_soc_remove(struct snd_soc_codec *codec)
1427
{
1428
	struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
1429

1430 1431
	if (dac33->irq >= 0) {
		free_irq(dac33->irq, dac33->codec);
1432
		flush_work(&dac33->work);
1433
	}
1434 1435 1436
	return 0;
}

1437
static const struct snd_soc_codec_driver soc_codec_dev_tlv320dac33 = {
1438 1439
	.read = dac33_read_reg_cache,
	.write = dac33_write_locked,
1440
	.set_bias_level = dac33_set_bias_level,
1441
	.idle_bias_off = true,
1442

1443 1444
	.probe = dac33_soc_probe,
	.remove = dac33_soc_remove,
1445

1446 1447 1448 1449 1450 1451 1452 1453
	.component_driver = {
		.controls		= dac33_snd_controls,
		.num_controls		= ARRAY_SIZE(dac33_snd_controls),
		.dapm_widgets		= dac33_dapm_widgets,
		.num_dapm_widgets	= ARRAY_SIZE(dac33_dapm_widgets),
		.dapm_routes		= audio_map,
		.num_dapm_routes	= ARRAY_SIZE(audio_map),
	},
1454 1455 1456 1457
};

#define DAC33_RATES	(SNDRV_PCM_RATE_44100 | \
			 SNDRV_PCM_RATE_48000)
1458
#define DAC33_FORMATS	(SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
1459

1460
static const struct snd_soc_dai_ops dac33_dai_ops = {
1461
	.startup	= dac33_startup,
1462 1463 1464
	.shutdown	= dac33_shutdown,
	.hw_params	= dac33_hw_params,
	.trigger	= dac33_pcm_trigger,
1465
	.delay		= dac33_dai_delay,
1466 1467 1468 1469
	.set_sysclk	= dac33_set_dai_sysclk,
	.set_fmt	= dac33_set_dai_fmt,
};

1470 1471
static struct snd_soc_dai_driver dac33_dai = {
	.name = "tlv320dac33-hifi",
1472 1473 1474 1475 1476
	.playback = {
		.stream_name = "Playback",
		.channels_min = 2,
		.channels_max = 2,
		.rates = DAC33_RATES,
M
Mark Brown 已提交
1477
		.formats = DAC33_FORMATS,
1478
		.sig_bits = 24,
M
Mark Brown 已提交
1479
	},
1480 1481 1482
	.ops = &dac33_dai_ops,
};

1483 1484
static int dac33_i2c_probe(struct i2c_client *client,
			   const struct i2c_device_id *id)
1485 1486 1487
{
	struct tlv320dac33_platform_data *pdata;
	struct tlv320dac33_priv *dac33;
1488
	int ret, i;
1489 1490 1491 1492 1493 1494 1495

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

1496 1497
	dac33 = devm_kzalloc(&client->dev, sizeof(struct tlv320dac33_priv),
			     GFP_KERNEL);
1498 1499 1500
	if (dac33 == NULL)
		return -ENOMEM;

1501 1502 1503 1504 1505 1506 1507
	dac33->reg_cache = devm_kmemdup(&client->dev,
					dac33_reg,
					ARRAY_SIZE(dac33_reg) * sizeof(u8),
					GFP_KERNEL);
	if (!dac33->reg_cache)
		return -ENOMEM;

1508
	dac33->i2c = client;
1509
	mutex_init(&dac33->mutex);
1510
	spin_lock_init(&dac33->lock);
1511 1512 1513 1514

	i2c_set_clientdata(client, dac33);

	dac33->power_gpio = pdata->power_gpio;
1515
	dac33->burst_bclkdiv = pdata->burst_bclkdiv;
1516
	dac33->keep_bclk = pdata->keep_bclk;
1517 1518 1519
	dac33->mode1_latency = pdata->mode1_latency;
	if (!dac33->mode1_latency)
		dac33->mode1_latency = 10000; /* 10ms */
1520 1521
	dac33->irq = client->irq;
	/* Disable FIFO use by default */
1522
	dac33->fifo_mode = DAC33_FIFO_BYPASS;
1523 1524 1525 1526 1527

	/* Check if the reset GPIO number is valid and request it */
	if (dac33->power_gpio >= 0) {
		ret = gpio_request(dac33->power_gpio, "tlv320dac33 reset");
		if (ret < 0) {
1528
			dev_err(&client->dev,
1529 1530
				"Failed to request reset GPIO (%d)\n",
				dac33->power_gpio);
1531
			goto err_gpio;
1532 1533 1534 1535
		}
		gpio_direction_output(dac33->power_gpio, 0);
	}

1536 1537 1538
	for (i = 0; i < ARRAY_SIZE(dac33->supplies); i++)
		dac33->supplies[i].supply = dac33_supply_names[i];

1539
	ret = devm_regulator_bulk_get(&client->dev, ARRAY_SIZE(dac33->supplies),
1540 1541 1542
				 dac33->supplies);

	if (ret != 0) {
1543
		dev_err(&client->dev, "Failed to request supplies: %d\n", ret);
1544 1545 1546
		goto err_get;
	}

1547 1548 1549
	ret = snd_soc_register_codec(&client->dev,
			&soc_codec_dev_tlv320dac33, &dac33_dai, 1);
	if (ret < 0)
1550
		goto err_get;
1551 1552

	return ret;
1553
err_get:
1554 1555
	if (dac33->power_gpio >= 0)
		gpio_free(dac33->power_gpio);
1556
err_gpio:
1557 1558 1559
	return ret;
}

1560
static int dac33_i2c_remove(struct i2c_client *client)
1561
{
1562
	struct tlv320dac33_priv *dac33 = i2c_get_clientdata(client);
1563 1564

	if (unlikely(dac33->chip_power))
1565
		dac33_hard_power(dac33->codec, 0);
1566 1567 1568 1569

	if (dac33->power_gpio >= 0)
		gpio_free(dac33->power_gpio);

1570
	snd_soc_unregister_codec(&client->dev);
1571 1572 1573 1574 1575 1576 1577 1578 1579 1580
	return 0;
}

static const struct i2c_device_id tlv320dac33_i2c_id[] = {
	{
		.name = "tlv320dac33",
		.driver_data = 0,
	},
	{ },
};
1581
MODULE_DEVICE_TABLE(i2c, tlv320dac33_i2c_id);
1582 1583 1584

static struct i2c_driver tlv320dac33_i2c_driver = {
	.driver = {
1585
		.name = "tlv320dac33-codec",
1586 1587
	},
	.probe		= dac33_i2c_probe,
1588
	.remove		= dac33_i2c_remove,
1589 1590 1591
	.id_table	= tlv320dac33_i2c_id,
};

1592
module_i2c_driver(tlv320dac33_i2c_driver);
1593 1594

MODULE_DESCRIPTION("ASoC TLV320DAC33 codec driver");
1595
MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
1596
MODULE_LICENSE("GPL");