cs4270.c 22.6 KB
Newer Older
1 2 3 4 5
/*
 * CS4270 ALSA SoC (ASoC) codec driver
 *
 * Author: Timur Tabi <timur@freescale.com>
 *
6 7 8 9
 * Copyright 2007-2009 Freescale Semiconductor, Inc.  This file is licensed
 * under the terms of the GNU General Public License version 2.  This
 * program is licensed "as is" without any warranty of any kind, whether
 * express or implied.
10 11 12 13 14
 *
 * This is an ASoC device driver for the Cirrus Logic CS4270 codec.
 *
 * Current features/limitations:
 *
15 16 17 18 19 20
 * - Software mode is supported.  Stand-alone mode is not supported.
 * - Only I2C is supported, not SPI
 * - Support for master and slave mode
 * - The machine driver's 'startup' function must call
 *   cs4270_set_dai_sysclk() with the value of MCLK.
 * - Only I2S and left-justified modes are supported
21
 * - Power management is supported
22 23 24 25
 */

#include <linux/module.h>
#include <linux/platform_device.h>
26
#include <linux/slab.h>
27 28 29 30
#include <sound/core.h>
#include <sound/soc.h>
#include <sound/initval.h>
#include <linux/i2c.h>
31
#include <linux/delay.h>
32
#include <linux/regulator/consumer.h>
33

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
/*
 * The codec isn't really big-endian or little-endian, since the I2S
 * interface requires data to be sent serially with the MSbit first.
 * However, to support BE and LE I2S devices, we specify both here.  That
 * way, ALSA will always match the bit patterns.
 */
#define CS4270_FORMATS (SNDRV_PCM_FMTBIT_S8      | \
			SNDRV_PCM_FMTBIT_S16_LE  | SNDRV_PCM_FMTBIT_S16_BE  | \
			SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE | \
			SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE | \
			SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE | \
			SNDRV_PCM_FMTBIT_S24_LE  | SNDRV_PCM_FMTBIT_S24_BE)

/* CS4270 registers addresses */
#define CS4270_CHIPID	0x01	/* Chip ID */
#define CS4270_PWRCTL	0x02	/* Power Control */
#define CS4270_MODE	0x03	/* Mode Control */
#define CS4270_FORMAT	0x04	/* Serial Format, ADC/DAC Control */
#define CS4270_TRANS	0x05	/* Transition Control */
#define CS4270_MUTE	0x06	/* Mute Control */
#define CS4270_VOLA	0x07	/* DAC Channel A Volume Control */
#define CS4270_VOLB	0x08	/* DAC Channel B Volume Control */

#define CS4270_FIRSTREG	0x01
#define CS4270_LASTREG	0x08
#define CS4270_NUMREGS	(CS4270_LASTREG - CS4270_FIRSTREG + 1)
60
#define CS4270_I2C_INCR	0x80
61

62 63 64 65 66 67 68
/* Bit masks for the CS4270 registers */
#define CS4270_CHIPID_ID	0xF0
#define CS4270_CHIPID_REV	0x0F
#define CS4270_PWRCTL_FREEZE	0x80
#define CS4270_PWRCTL_PDN_ADC	0x20
#define CS4270_PWRCTL_PDN_DAC	0x02
#define CS4270_PWRCTL_PDN	0x01
69 70
#define CS4270_PWRCTL_PDN_ALL	\
	(CS4270_PWRCTL_PDN_ADC | CS4270_PWRCTL_PDN_DAC | CS4270_PWRCTL_PDN)
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
#define CS4270_MODE_SPEED_MASK	0x30
#define CS4270_MODE_1X		0x00
#define CS4270_MODE_2X		0x10
#define CS4270_MODE_4X		0x20
#define CS4270_MODE_SLAVE	0x30
#define CS4270_MODE_DIV_MASK	0x0E
#define CS4270_MODE_DIV1	0x00
#define CS4270_MODE_DIV15	0x02
#define CS4270_MODE_DIV2	0x04
#define CS4270_MODE_DIV3	0x06
#define CS4270_MODE_DIV4	0x08
#define CS4270_MODE_POPGUARD	0x01
#define CS4270_FORMAT_FREEZE_A	0x80
#define CS4270_FORMAT_FREEZE_B	0x40
#define CS4270_FORMAT_LOOPBACK	0x20
#define CS4270_FORMAT_DAC_MASK	0x18
#define CS4270_FORMAT_DAC_LJ	0x00
#define CS4270_FORMAT_DAC_I2S	0x08
#define CS4270_FORMAT_DAC_RJ16	0x18
#define CS4270_FORMAT_DAC_RJ24	0x10
#define CS4270_FORMAT_ADC_MASK	0x01
#define CS4270_FORMAT_ADC_LJ	0x00
#define CS4270_FORMAT_ADC_I2S	0x01
#define CS4270_TRANS_ONE_VOL	0x80
#define CS4270_TRANS_SOFT	0x40
#define CS4270_TRANS_ZERO	0x20
#define CS4270_TRANS_INV_ADC_A	0x08
#define CS4270_TRANS_INV_ADC_B	0x10
#define CS4270_TRANS_INV_DAC_A	0x02
#define CS4270_TRANS_INV_DAC_B	0x04
#define CS4270_TRANS_DEEMPH	0x01
#define CS4270_MUTE_AUTO	0x20
#define CS4270_MUTE_ADC_A	0x08
#define CS4270_MUTE_ADC_B	0x10
#define CS4270_MUTE_POLARITY	0x04
#define CS4270_MUTE_DAC_A	0x01
#define CS4270_MUTE_DAC_B	0x02

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
/* Power-on default values for the registers
 *
 * This array contains the power-on default values of the registers, with the
 * exception of the "CHIPID" register (01h).  The lower four bits of that
 * register contain the hardware revision, so it is treated as volatile.
 *
 * Also note that on the CS4270, the first readable register is 1, but ASoC
 * assumes the first register is 0.  Therfore, the array must have an entry for
 * register 0, but we use cs4270_reg_is_readable() to tell ASoC that it can't
 * be read.
 */
static const u8 cs4270_default_reg_cache[CS4270_LASTREG + 1] = {
	0x00, 0x00, 0x00, 0x30, 0x00, 0x60, 0x20, 0x00, 0x00
};

124 125 126 127
static const char *supply_names[] = {
	"va", "vd", "vlc"
};

128 129
/* Private data for the CS4270 */
struct cs4270_private {
130 131
	enum snd_soc_control_type control_type;
	void *control_data;
132 133
	unsigned int mclk; /* Input frequency of the MCLK pin */
	unsigned int mode; /* The mode (I2S or left-justified) */
134
	unsigned int slave_mode;
135
	unsigned int manual_mute;
136 137 138

	/* power domain regulators */
	struct regulator_bulk_data supplies[ARRAY_SIZE(supply_names)];
139 140
};

141 142 143 144 145 146 147
/**
 * struct cs4270_mode_ratios - clock ratio tables
 * @ratio: the ratio of MCLK to the sample rate
 * @speed_mode: the Speed Mode bits to set in the Mode Control register for
 *              this ratio
 * @mclk: the Ratio Select bits to set in the Mode Control register for this
 *        ratio
148 149 150 151 152 153 154 155
 *
 * The data for this chart is taken from Table 5 of the CS4270 reference
 * manual.
 *
 * This table is used to determine how to program the Mode Control register.
 * It is also used by cs4270_set_dai_sysclk() to tell ALSA which sampling
 * rates the CS4270 currently supports.
 *
156
 * @speed_mode is the corresponding bit pattern to be written to the
157 158
 * MODE bits of the Mode Control Register
 *
159
 * @mclk is the corresponding bit pattern to be wirten to the MCLK bits of
160 161 162 163 164
 * the Mode Control Register.
 *
 * In situations where a single ratio is represented by multiple speed
 * modes, we favor the slowest speed.  E.g, for a ratio of 128, we pick
 * double-speed instead of quad-speed.  However, the CS4270 errata states
165
 * that divide-By-1.5 can cause failures, so we avoid that mode where
166 167
 * possible.
 *
168 169
 * Errata: There is an errata for the CS4270 where divide-by-1.5 does not
 * work if Vd is 3.3V.  If this effects you, select the
170 171 172
 * CONFIG_SND_SOC_CS4270_VD33_ERRATA Kconfig option, and the driver will
 * never select any sample rates that require divide-by-1.5.
 */
173
struct cs4270_mode_ratios {
174 175 176
	unsigned int ratio;
	u8 speed_mode;
	u8 mclk;
177 178
};

179
static struct cs4270_mode_ratios cs4270_mode_ratios[] = {
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
	{64, CS4270_MODE_4X, CS4270_MODE_DIV1},
#ifndef CONFIG_SND_SOC_CS4270_VD33_ERRATA
	{96, CS4270_MODE_4X, CS4270_MODE_DIV15},
#endif
	{128, CS4270_MODE_2X, CS4270_MODE_DIV1},
	{192, CS4270_MODE_4X, CS4270_MODE_DIV3},
	{256, CS4270_MODE_1X, CS4270_MODE_DIV1},
	{384, CS4270_MODE_2X, CS4270_MODE_DIV3},
	{512, CS4270_MODE_1X, CS4270_MODE_DIV2},
	{768, CS4270_MODE_1X, CS4270_MODE_DIV3},
	{1024, CS4270_MODE_1X, CS4270_MODE_DIV4}
};

/* The number of MCLK/LRCK ratios supported by the CS4270 */
#define NUM_MCLK_RATIOS		ARRAY_SIZE(cs4270_mode_ratios)
195

196
static int cs4270_reg_is_readable(struct snd_soc_codec *codec, unsigned int reg)
197 198 199 200
{
	return (reg >= CS4270_FIRSTREG) && (reg <= CS4270_LASTREG);
}

201
static int cs4270_reg_is_volatile(struct snd_soc_codec *codec, unsigned int reg)
202 203 204 205 206 207 208 209
{
	/* Unreadable registers are considered volatile */
	if ((reg < CS4270_FIRSTREG) || (reg > CS4270_LASTREG))
		return 1;

	return reg == CS4270_CHIPID;
}

210 211 212 213 214 215
/**
 * cs4270_set_dai_sysclk - determine the CS4270 samples rates.
 * @codec_dai: the codec DAI
 * @clk_id: the clock ID (ignored)
 * @freq: the MCLK input frequency
 * @dir: the clock direction (ignored)
216
 *
217 218
 * This function is used to tell the codec driver what the input MCLK
 * frequency is.
219 220 221
 *
 * The value of MCLK is used to determine which sample rates are supported
 * by the CS4270.  The ratio of MCLK / Fs must be equal to one of nine
222
 * supported values - 64, 96, 128, 192, 256, 384, 512, 768, and 1024.
223 224 225
 *
 * This function calculates the nine ratios and determines which ones match
 * a standard sample rate.  If there's a match, then it is added to the list
226
 * of supported sample rates.
227 228 229 230
 *
 * This function must be called by the machine driver's 'startup' function,
 * otherwise the list of supported sample rates will not be available in
 * time for ALSA.
231 232 233 234 235
 *
 * For setups with variable MCLKs, pass 0 as 'freq' argument. This will cause
 * theoretically possible sample rates to be enabled. Call it again with a
 * proper value set one the external clock is set (most probably you would do
 * that from a machine's driver 'hw_param' hook.
236
 */
237
static int cs4270_set_dai_sysclk(struct snd_soc_dai *codec_dai,
238 239 240
				 int clk_id, unsigned int freq, int dir)
{
	struct snd_soc_codec *codec = codec_dai->codec;
241
	struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
242 243 244 245 246

	cs4270->mclk = freq;
	return 0;
}

247 248 249 250
/**
 * cs4270_set_dai_fmt - configure the codec for the selected audio format
 * @codec_dai: the codec DAI
 * @format: a SND_SOC_DAIFMT_x value indicating the data format
251 252 253 254 255 256 257 258 259
 *
 * This function takes a bitmask of SND_SOC_DAIFMT_x bits and programs the
 * codec accordingly.
 *
 * Currently, this function only supports SND_SOC_DAIFMT_I2S and
 * SND_SOC_DAIFMT_LEFT_J.  The CS4270 codec also supports right-justified
 * data for playback only, but ASoC currently does not support different
 * formats for playback vs. record.
 */
260
static int cs4270_set_dai_fmt(struct snd_soc_dai *codec_dai,
261 262 263
			      unsigned int format)
{
	struct snd_soc_codec *codec = codec_dai->codec;
264
	struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
265 266
	int ret = 0;

267
	/* set DAI format */
268 269 270 271 272 273
	switch (format & SND_SOC_DAIFMT_FORMAT_MASK) {
	case SND_SOC_DAIFMT_I2S:
	case SND_SOC_DAIFMT_LEFT_J:
		cs4270->mode = format & SND_SOC_DAIFMT_FORMAT_MASK;
		break;
	default:
274
		dev_err(codec->dev, "invalid dai format\n");
275 276 277
		ret = -EINVAL;
	}

278 279 280 281 282 283 284 285 286
	/* set master/slave audio interface */
	switch (format & SND_SOC_DAIFMT_MASTER_MASK) {
	case SND_SOC_DAIFMT_CBS_CFS:
		cs4270->slave_mode = 1;
		break;
	case SND_SOC_DAIFMT_CBM_CFM:
		cs4270->slave_mode = 0;
		break;
	default:
287
		/* all other modes are unsupported by the hardware */
288 289 290
		ret = -EINVAL;
	}

291 292 293
	return ret;
}

294 295 296 297 298
/**
 * cs4270_hw_params - program the CS4270 with the given hardware parameters.
 * @substream: the audio stream
 * @params: the hardware parameters to set
 * @dai: the SOC DAI (ignored)
299
 *
300 301 302 303 304
 * This function programs the hardware with the values provided.
 * Specifically, the sample rate and the data format.
 *
 * The .ops functions are used to provide board-specific data, like input
 * frequencies, to this driver.  This function takes that information,
305 306 307 308
 * combines it with the hardware parameters provided, and programs the
 * hardware accordingly.
 */
static int cs4270_hw_params(struct snd_pcm_substream *substream,
309 310
			    struct snd_pcm_hw_params *params,
			    struct snd_soc_dai *dai)
311 312
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
313
	struct snd_soc_codec *codec = rtd->codec;
314
	struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
315
	int ret;
316 317 318 319 320 321 322 323 324 325
	unsigned int i;
	unsigned int rate;
	unsigned int ratio;
	int reg;

	/* Figure out which MCLK/LRCK ratio to use */

	rate = params_rate(params);	/* Sampling rate, in Hz */
	ratio = cs4270->mclk / rate;	/* MCLK/LRCK ratio */

326
	for (i = 0; i < NUM_MCLK_RATIOS; i++) {
327
		if (cs4270_mode_ratios[i].ratio == ratio)
328 329 330
			break;
	}

331
	if (i == NUM_MCLK_RATIOS) {
332
		/* We did not find a matching ratio */
333
		dev_err(codec->dev, "could not find matching ratio\n");
334 335 336
		return -EINVAL;
	}

337
	/* Set the sample rate */
338 339 340

	reg = snd_soc_read(codec, CS4270_MODE);
	reg &= ~(CS4270_MODE_SPEED_MASK | CS4270_MODE_DIV_MASK);
341 342 343 344 345 346
	reg |= cs4270_mode_ratios[i].mclk;

	if (cs4270->slave_mode)
		reg |= CS4270_MODE_SLAVE;
	else
		reg |= cs4270_mode_ratios[i].speed_mode;
347 348 349

	ret = snd_soc_write(codec, CS4270_MODE, reg);
	if (ret < 0) {
350
		dev_err(codec->dev, "i2c write failed\n");
351 352 353
		return ret;
	}

354
	/* Set the DAI format */
355 356 357 358 359 360 361 362 363 364 365 366

	reg = snd_soc_read(codec, CS4270_FORMAT);
	reg &= ~(CS4270_FORMAT_DAC_MASK | CS4270_FORMAT_ADC_MASK);

	switch (cs4270->mode) {
	case SND_SOC_DAIFMT_I2S:
		reg |= CS4270_FORMAT_DAC_I2S | CS4270_FORMAT_ADC_I2S;
		break;
	case SND_SOC_DAIFMT_LEFT_J:
		reg |= CS4270_FORMAT_DAC_LJ | CS4270_FORMAT_ADC_LJ;
		break;
	default:
367
		dev_err(codec->dev, "unknown dai format\n");
368 369 370 371 372
		return -EINVAL;
	}

	ret = snd_soc_write(codec, CS4270_FORMAT, reg);
	if (ret < 0) {
373
		dev_err(codec->dev, "i2c write failed\n");
374 375 376 377 378 379
		return ret;
	}

	return ret;
}

380
/**
381
 * cs4270_dai_mute - enable/disable the CS4270 external mute
382 383
 * @dai: the SOC DAI
 * @mute: 0 = disable mute, 1 = enable mute
384 385 386 387 388 389
 *
 * This function toggles the mute bits in the MUTE register.  The CS4270's
 * mute capability is intended for external muting circuitry, so if the
 * board does not have the MUTEA or MUTEB pins connected to such circuitry,
 * then this function will do nothing.
 */
390
static int cs4270_dai_mute(struct snd_soc_dai *dai, int mute)
391 392
{
	struct snd_soc_codec *codec = dai->codec;
393
	struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
394 395 396 397 398
	int reg6;

	reg6 = snd_soc_read(codec, CS4270_MUTE);

	if (mute)
399
		reg6 |= CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B;
400
	else {
401
		reg6 &= ~(CS4270_MUTE_DAC_A | CS4270_MUTE_DAC_B);
402 403
		reg6 |= cs4270->manual_mute;
	}
404 405 406 407

	return snd_soc_write(codec, CS4270_MUTE, reg6);
}

408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
/**
 * cs4270_soc_put_mute - put callback for the 'Master Playback switch'
 * 			 alsa control.
 * @kcontrol: mixer control
 * @ucontrol: control element information
 *
 * This function basically passes the arguments on to the generic
 * snd_soc_put_volsw() function and saves the mute information in
 * our private data structure. This is because we want to prevent
 * cs4270_dai_mute() neglecting the user's decision to manually
 * mute the codec's output.
 *
 * Returns 0 for success.
 */
static int cs4270_soc_put_mute(struct snd_kcontrol *kcontrol,
				struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
426
	struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
427 428 429 430 431 432 433 434 435
	int left = !ucontrol->value.integer.value[0];
	int right = !ucontrol->value.integer.value[1];

	cs4270->manual_mute = (left ? CS4270_MUTE_DAC_A : 0) |
			      (right ? CS4270_MUTE_DAC_B : 0);

	return snd_soc_put_volsw(kcontrol, ucontrol);
}

436 437 438
/* A list of non-DAPM controls that the CS4270 supports */
static const struct snd_kcontrol_new cs4270_snd_controls[] = {
	SOC_DOUBLE_R("Master Playback Volume",
439 440 441 442
		CS4270_VOLA, CS4270_VOLB, 0, 0xFF, 1),
	SOC_SINGLE("Digital Sidetone Switch", CS4270_FORMAT, 5, 1, 0),
	SOC_SINGLE("Soft Ramp Switch", CS4270_TRANS, 6, 1, 0),
	SOC_SINGLE("Zero Cross Switch", CS4270_TRANS, 5, 1, 0),
443
	SOC_SINGLE("De-emphasis filter", CS4270_TRANS, 0, 1, 0),
444 445
	SOC_SINGLE("Popguard Switch", CS4270_MODE, 0, 1, 1),
	SOC_SINGLE("Auto-Mute Switch", CS4270_MUTE, 5, 1, 0),
446 447 448
	SOC_DOUBLE("Master Capture Switch", CS4270_MUTE, 3, 4, 1, 1),
	SOC_DOUBLE_EXT("Master Playback Switch", CS4270_MUTE, 0, 1, 1, 1,
		snd_soc_get_volsw, cs4270_soc_put_mute),
449 450
};

451 452 453 454
static struct snd_soc_dai_ops cs4270_dai_ops = {
	.hw_params	= cs4270_hw_params,
	.set_sysclk	= cs4270_set_dai_sysclk,
	.set_fmt	= cs4270_set_dai_fmt,
455
	.digital_mute	= cs4270_dai_mute,
456 457
};

M
Mark Brown 已提交
458
static struct snd_soc_dai_driver cs4270_dai = {
459
	.name = "cs4270-hifi",
460 461 462 463
	.playback = {
		.stream_name = "Playback",
		.channels_min = 1,
		.channels_max = 2,
464 465 466
		.rates = SNDRV_PCM_RATE_CONTINUOUS,
		.rate_min = 4000,
		.rate_max = 216000,
467 468 469 470 471 472
		.formats = CS4270_FORMATS,
	},
	.capture = {
		.stream_name = "Capture",
		.channels_min = 1,
		.channels_max = 2,
473 474 475
		.rates = SNDRV_PCM_RATE_CONTINUOUS,
		.rate_min = 4000,
		.rate_max = 216000,
476 477
		.formats = CS4270_FORMATS,
	},
478
	.ops = &cs4270_dai_ops,
479 480
};

481 482 483 484 485 486
/**
 * cs4270_probe - ASoC probe function
 * @pdev: platform device
 *
 * This function is called when ASoC has all the pieces it needs to
 * instantiate a sound driver.
487
 */
488
static int cs4270_probe(struct snd_soc_codec *codec)
489
{
490
	struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
491
	int i, ret;
492

493
	codec->control_data = cs4270->control_data;
494

495 496 497 498
	/* Tell ASoC what kind of I/O to use to read the registers.  ASoC will
	 * then do the I2C transactions itself.
	 */
	ret = snd_soc_codec_set_cache_io(codec, 8, 8, cs4270->control_type);
499
	if (ret < 0) {
500
		dev_err(codec->dev, "failed to set cache I/O (ret=%i)\n", ret);
501
		return ret;
502 503
	}

504 505 506 507 508
	/* Disable auto-mute.  This feature appears to be buggy.  In some
	 * situations, auto-mute will not deactivate when it should, so we want
	 * this feature disabled by default.  An application (e.g. alsactl) can
	 * re-enabled it by using the controls.
	 */
509
	ret = snd_soc_update_bits(codec, CS4270_MUTE, CS4270_MUTE_AUTO, 0);
510
	if (ret < 0) {
511
		dev_err(codec->dev, "i2c write failed\n");
512 513 514 515 516 517 518 519
		return ret;
	}

	/* Disable automatic volume control.  The hardware enables, and it
	 * causes volume change commands to be delayed, sometimes until after
	 * playback has started.  An application (e.g. alsactl) can
	 * re-enabled it by using the controls.
	 */
520 521
	ret = snd_soc_update_bits(codec, CS4270_TRANS,
		CS4270_TRANS_SOFT | CS4270_TRANS_ZERO, 0);
522
	if (ret < 0) {
523
		dev_err(codec->dev, "i2c write failed\n");
524 525 526
		return ret;
	}

527 528 529
	/* Add the non-DAPM controls */
	ret = snd_soc_add_controls(codec, cs4270_snd_controls,
				ARRAY_SIZE(cs4270_snd_controls));
530
	if (ret < 0) {
531 532
		dev_err(codec->dev, "failed to add controls\n");
		return ret;
533 534
	}

535 536 537 538 539 540 541 542 543 544 545 546 547
	/* get the power supply regulators */
	for (i = 0; i < ARRAY_SIZE(supply_names); i++)
		cs4270->supplies[i].supply = supply_names[i];

	ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(cs4270->supplies),
				 cs4270->supplies);
	if (ret < 0)
		return ret;

	ret = regulator_bulk_enable(ARRAY_SIZE(cs4270->supplies),
				    cs4270->supplies);
	if (ret < 0)
		goto error_free_regulators;
548

549 550
	return 0;

551 552 553
error_free_regulators:
	regulator_bulk_free(ARRAY_SIZE(cs4270->supplies),
			    cs4270->supplies);
554 555 556 557

	return ret;
}

558
/**
559 560
 * cs4270_remove - ASoC remove function
 * @pdev: platform device
561
 *
562
 * This function is the counterpart to cs4270_probe().
563
 */
564
static int cs4270_remove(struct snd_soc_codec *codec)
565
{
566
	struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
567

568 569
	regulator_bulk_disable(ARRAY_SIZE(cs4270->supplies), cs4270->supplies);
	regulator_bulk_free(ARRAY_SIZE(cs4270->supplies), cs4270->supplies);
570 571

	return 0;
572 573
};

574 575 576 577 578 579 580 581 582 583 584
#ifdef CONFIG_PM

/* This suspend/resume implementation can handle both - a simple standby
 * where the codec remains powered, and a full suspend, where the voltage
 * domain the codec is connected to is teared down and/or any other hardware
 * reset condition is asserted.
 *
 * The codec's own power saving features are enabled in the suspend callback,
 * and all registers are written back to the hardware when resuming.
 */

585
static int cs4270_soc_suspend(struct snd_soc_codec *codec, pm_message_t mesg)
586
{
587
	struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
588
	int reg, ret;
589

590 591 592 593 594 595 596 597 598 599 600 601
	reg = snd_soc_read(codec, CS4270_PWRCTL) | CS4270_PWRCTL_PDN_ALL;
	if (reg < 0)
		return reg;

	ret = snd_soc_write(codec, CS4270_PWRCTL, reg);
	if (ret < 0)
		return ret;

	regulator_bulk_disable(ARRAY_SIZE(cs4270->supplies),
			       cs4270->supplies);

	return 0;
602 603
}

604
static int cs4270_soc_resume(struct snd_soc_codec *codec)
605
{
606
	struct cs4270_private *cs4270 = snd_soc_codec_get_drvdata(codec);
607
	struct i2c_client *i2c_client = codec->control_data;
608 609
	int reg;

610 611 612
	regulator_bulk_enable(ARRAY_SIZE(cs4270->supplies),
			      cs4270->supplies);

613 614 615 616 617 618 619 620
	/* In case the device was put to hard reset during sleep, we need to
	 * wait 500ns here before any I2C communication. */
	ndelay(500);

	/* first restore the entire register cache ... */
	for (reg = CS4270_FIRSTREG; reg <= CS4270_LASTREG; reg++) {
		u8 val = snd_soc_read(codec, reg);

621
		if (i2c_smbus_write_byte_data(i2c_client, reg, val)) {
622 623 624 625 626 627 628 629 630 631 632 633
			dev_err(codec->dev, "i2c write failed\n");
			return -EIO;
		}
	}

	/* ... then disable the power-down bits */
	reg = snd_soc_read(codec, CS4270_PWRCTL);
	reg &= ~CS4270_PWRCTL_PDN_ALL;

	return snd_soc_write(codec, CS4270_PWRCTL, reg);
}
#else
634 635
#define cs4270_soc_suspend	NULL
#define cs4270_soc_resume	NULL
636 637
#endif /* CONFIG_PM */

638
/*
639
 * ASoC codec driver structure
640
 */
641 642 643 644 645 646 647 648 649 650
static const struct snd_soc_codec_driver soc_codec_device_cs4270 = {
	.probe =		cs4270_probe,
	.remove =		cs4270_remove,
	.suspend =		cs4270_soc_suspend,
	.resume =		cs4270_soc_resume,
	.volatile_register =	cs4270_reg_is_volatile,
	.readable_register =	cs4270_reg_is_readable,
	.reg_cache_size =	CS4270_LASTREG + 1,
	.reg_word_size =	sizeof(u8),
	.reg_cache_default =	cs4270_default_reg_cache,
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 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
};

/**
 * cs4270_i2c_probe - initialize the I2C interface of the CS4270
 * @i2c_client: the I2C client object
 * @id: the I2C device ID (ignored)
 *
 * This function is called whenever the I2C subsystem finds a device that
 * matches the device ID given via a prior call to i2c_add_driver().
 */
static int cs4270_i2c_probe(struct i2c_client *i2c_client,
	const struct i2c_device_id *id)
{
	struct cs4270_private *cs4270;
	int ret;

	/* Verify that we have a CS4270 */

	ret = i2c_smbus_read_byte_data(i2c_client, CS4270_CHIPID);
	if (ret < 0) {
		dev_err(&i2c_client->dev, "failed to read i2c at addr %X\n",
		       i2c_client->addr);
		return ret;
	}
	/* The top four bits of the chip ID should be 1100. */
	if ((ret & 0xF0) != 0xC0) {
		dev_err(&i2c_client->dev, "device at addr %X is not a CS4270\n",
		       i2c_client->addr);
		return -ENODEV;
	}

	dev_info(&i2c_client->dev, "found device at i2c address %X\n",
		i2c_client->addr);
	dev_info(&i2c_client->dev, "hardware revision %X\n", ret & 0xF);

	cs4270 = kzalloc(sizeof(struct cs4270_private), GFP_KERNEL);
	if (!cs4270) {
		dev_err(&i2c_client->dev, "could not allocate codec\n");
		return -ENOMEM;
	}

	i2c_set_clientdata(i2c_client, cs4270);
	cs4270->control_data = i2c_client;
	cs4270->control_type = SND_SOC_I2C;

	ret = snd_soc_register_codec(&i2c_client->dev,
			&soc_codec_device_cs4270, &cs4270_dai, 1);
	if (ret < 0)
		kfree(cs4270);
	return ret;
}

/**
 * cs4270_i2c_remove - remove an I2C device
 * @i2c_client: the I2C client object
 *
 * This function is the counterpart to cs4270_i2c_probe().
 */
static int cs4270_i2c_remove(struct i2c_client *i2c_client)
{
	snd_soc_unregister_codec(&i2c_client->dev);
	kfree(i2c_get_clientdata(i2c_client));
	return 0;
}

/*
 * cs4270_id - I2C device IDs supported by this driver
 */
A
Axel Lin 已提交
719
static const struct i2c_device_id cs4270_id[] = {
720 721 722 723 724
	{"cs4270", 0},
	{}
};
MODULE_DEVICE_TABLE(i2c, cs4270_id);

725 726 727 728 729 730
/*
 * cs4270_i2c_driver - I2C device identification
 *
 * This structure tells the I2C subsystem how to identify and support a
 * given I2C device type.
 */
731 732
static struct i2c_driver cs4270_i2c_driver = {
	.driver = {
733
		.name = "cs4270-codec",
734 735 736 737
		.owner = THIS_MODULE,
	},
	.id_table = cs4270_id,
	.probe = cs4270_i2c_probe,
738
	.remove = cs4270_i2c_remove,
739
};
740

741
static int __init cs4270_init(void)
M
Mark Brown 已提交
742
{
743
	return i2c_add_driver(&cs4270_i2c_driver);
M
Mark Brown 已提交
744 745 746 747 748
}
module_init(cs4270_init);

static void __exit cs4270_exit(void)
{
749
	i2c_del_driver(&cs4270_i2c_driver);
M
Mark Brown 已提交
750 751 752
}
module_exit(cs4270_exit);

753 754 755
MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
MODULE_DESCRIPTION("Cirrus Logic CS4270 ALSA SoC Codec Driver");
MODULE_LICENSE("GPL");