da7210.c 16.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * DA7210 ALSA Soc codec driver
 *
 * Copyright (c) 2009 Dialog Semiconductor
 * Written by David Chen <Dajun.chen@diasemi.com>
 *
 * Copyright (C) 2009 Renesas Solutions Corp.
 * Cleanups by Kuninori Morimoto <morimoto.kuninori@renesas.com>
 *
 * Tested on SuperH Ecovec24 board with S16/S24 LE in 48KHz using I2S
 *
 * This program is free software; you can redistribute  it and/or modify it
 * under  the terms of  the GNU General  Public License as published by the
 * Free Software Foundation;  either version 2 of the  License, or (at your
 * option) any later version.
 */

#include <linux/delay.h>
#include <linux/i2c.h>
#include <linux/platform_device.h>
21
#include <linux/slab.h>
22 23
#include <sound/pcm.h>
#include <sound/pcm_params.h>
L
Liam Girdwood 已提交
24
#include <sound/soc.h>
25
#include <sound/initval.h>
26
#include <sound/tlv.h>
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

/* DA7210 register space */
#define DA7210_STATUS			0x02
#define DA7210_STARTUP1			0x03
#define DA7210_MIC_L			0x07
#define DA7210_MIC_R			0x08
#define DA7210_INMIX_L			0x0D
#define DA7210_INMIX_R			0x0E
#define DA7210_ADC_HPF			0x0F
#define DA7210_ADC			0x10
#define DA7210_DAC_HPF			0x14
#define DA7210_DAC_L			0x15
#define DA7210_DAC_R			0x16
#define DA7210_DAC_SEL			0x17
#define DA7210_OUTMIX_L			0x1C
#define DA7210_OUTMIX_R			0x1D
#define DA7210_HP_L_VOL			0x21
#define DA7210_HP_R_VOL			0x22
#define DA7210_HP_CFG			0x23
#define DA7210_DAI_SRC_SEL		0x25
#define DA7210_DAI_CFG1			0x26
#define DA7210_DAI_CFG3			0x28
49 50
#define DA7210_PLL_DIV1			0x29
#define DA7210_PLL_DIV2			0x2A
51 52
#define DA7210_PLL_DIV3			0x2B
#define DA7210_PLL			0x2C
53 54 55 56
#define DA7210_A_HID_UNLOCK		0x8A
#define DA7210_A_TEST_UNLOCK		0x8B
#define DA7210_A_PLL1			0x90
#define DA7210_A_CP_MODE		0xA7
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

/* STARTUP1 bit fields */
#define DA7210_SC_MST_EN		(1 << 0)

/* MIC_L bit fields */
#define DA7210_MICBIAS_EN		(1 << 6)
#define DA7210_MIC_L_EN			(1 << 7)

/* MIC_R bit fields */
#define DA7210_MIC_R_EN			(1 << 7)

/* INMIX_L bit fields */
#define DA7210_IN_L_EN			(1 << 7)

/* INMIX_R bit fields */
#define DA7210_IN_R_EN			(1 << 7)

/* ADC bit fields */
#define DA7210_ADC_L_EN			(1 << 3)
#define DA7210_ADC_R_EN			(1 << 7)

78 79 80 81
/* DAC/ADC HPF fields */
#define DA7210_VOICE_F0_MASK		(0x7 << 4)
#define DA7210_VOICE_F0_25		(1 << 4)
#define DA7210_VOICE_EN			(1 << 7)
M
Mark Brown 已提交
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
/* DAC_SEL bit fields */
#define DA7210_DAC_L_SRC_DAI_L		(4 << 0)
#define DA7210_DAC_L_EN			(1 << 3)
#define DA7210_DAC_R_SRC_DAI_R		(5 << 4)
#define DA7210_DAC_R_EN			(1 << 7)

/* OUTMIX_L bit fields */
#define DA7210_OUT_L_EN			(1 << 7)

/* OUTMIX_R bit fields */
#define DA7210_OUT_R_EN			(1 << 7)

/* HP_CFG bit fields */
#define DA7210_HP_2CAP_MODE		(1 << 1)
#define DA7210_HP_SENSE_EN		(1 << 2)
#define DA7210_HP_L_EN			(1 << 3)
#define DA7210_HP_MODE			(1 << 6)
#define DA7210_HP_R_EN			(1 << 7)

/* DAI_SRC_SEL bit fields */
#define DA7210_DAI_OUT_L_SRC		(6 << 0)
#define DA7210_DAI_OUT_R_SRC		(7 << 4)

/* DAI_CFG1 bit fields */
#define DA7210_DAI_WORD_S16_LE		(0 << 0)
108
#define DA7210_DAI_WORD_S20_3LE		(1 << 0)
109
#define DA7210_DAI_WORD_S24_LE		(2 << 0)
110
#define DA7210_DAI_WORD_S32_LE		(3 << 0)
111
#define DA7210_DAI_FLEN_64BIT		(1 << 2)
112
#define DA7210_DAI_MODE_SLAVE		(0 << 7)
113 114 115 116
#define DA7210_DAI_MODE_MASTER		(1 << 7)

/* DAI_CFG3 bit fields */
#define DA7210_DAI_FORMAT_I2SMODE	(0 << 0)
117 118
#define DA7210_DAI_FORMAT_LEFT_J	(1 << 0)
#define DA7210_DAI_FORMAT_RIGHT_J	(2 << 0)
119 120 121 122 123 124 125 126
#define DA7210_DAI_OE			(1 << 3)
#define DA7210_DAI_EN			(1 << 7)

/*PLL_DIV3 bit fields */
#define DA7210_MCLK_RANGE_10_20_MHZ	(1 << 4)
#define DA7210_PLL_BYP			(1 << 6)

/* PLL bit fields */
127 128
#define DA7210_PLL_FS_MASK		(0xF << 0)
#define DA7210_PLL_FS_8000		(0x1 << 0)
129
#define DA7210_PLL_FS_11025		(0x2 << 0)
130 131
#define DA7210_PLL_FS_12000		(0x3 << 0)
#define DA7210_PLL_FS_16000		(0x5 << 0)
132
#define DA7210_PLL_FS_22050		(0x6 << 0)
133 134
#define DA7210_PLL_FS_24000		(0x7 << 0)
#define DA7210_PLL_FS_32000		(0x9 << 0)
135
#define DA7210_PLL_FS_44100		(0xA << 0)
136
#define DA7210_PLL_FS_48000		(0xB << 0)
137
#define DA7210_PLL_FS_88200		(0xE << 0)
138
#define DA7210_PLL_FS_96000		(0xF << 0)
139
#define DA7210_PLL_EN			(0x1 << 7)
140 141 142

#define DA7210_VERSION "0.0.1"

143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
/*
 * Playback Volume
 *
 * max		: 0x3F (+15.0 dB)
 *		   (1.5 dB step)
 * min		: 0x11 (-54.0 dB)
 * mute		: 0x10
 * reserved	: 0x00 - 0x0F
 *
 * ** FIXME **
 *
 * Reserved area are considered as "mute".
 * -> min = -79.5 dB
 */
static const DECLARE_TLV_DB_SCALE(hp_out_tlv, -7950, 150, 1);

static const struct snd_kcontrol_new da7210_snd_controls[] = {

	SOC_DOUBLE_R_TLV("HeadPhone Playback Volume",
			 DA7210_HP_L_VOL, DA7210_HP_R_VOL,
			 0, 0x3F, 0, hp_out_tlv),
};

166 167
/* Codec private data */
struct da7210_priv {
168
	enum snd_soc_control_type control_type;
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
};

/*
 * Register cache
 */
static const u8 da7210_reg[] = {
	0x00, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,	/* R0  - R7  */
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,	/* R8  - RF  */
	0x00, 0x00, 0x00, 0x00, 0x08, 0x10, 0x10, 0x54,	/* R10 - R17 */
	0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,	/* R18 - R1F */
	0x00, 0x00, 0x00, 0x02, 0x00, 0x76, 0x00, 0x00,	/* R20 - R27 */
	0x04, 0x00, 0x00, 0x30, 0x2A, 0x00, 0x40, 0x00,	/* R28 - R2F */
	0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00,	/* R30 - R37 */
	0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x00, 0x00,	/* R38 - R3F */
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,	/* R40 - R4F */
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,	/* R48 - R4F */
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,	/* R50 - R57 */
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,	/* R58 - R5F */
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,	/* R60 - R67 */
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,	/* R68 - R6F */
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,	/* R70 - R77 */
	0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x54, 0x00,	/* R78 - R7F */
	0x00, 0x00, 0x2C, 0x00, 0x00, 0x00, 0x00, 0x00,	/* R80 - R87 */
	0x00,						/* R88       */
};

A
Axel Lin 已提交
195 196
static int da7210_volatile_register(struct snd_soc_codec *codec,
				    unsigned int reg)
197
{
A
Axel Lin 已提交
198 199 200 201 202 203
	switch (reg) {
	case DA7210_STATUS:
		return 1;
	default:
		return 0;
	}
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
}
static int da7210_startup(struct snd_pcm_substream *substream,
			  struct snd_soc_dai *dai)
{
	int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
	struct snd_soc_codec *codec = dai->codec;

	if (is_play) {
		/* Enable Out */
		snd_soc_update_bits(codec, DA7210_OUTMIX_L, 0x1F, 0x10);
		snd_soc_update_bits(codec, DA7210_OUTMIX_R, 0x1F, 0x10);

	} else {
		/* Volume 7 */
		snd_soc_update_bits(codec, DA7210_MIC_L, 0x7, 0x7);
		snd_soc_update_bits(codec, DA7210_MIC_R, 0x7, 0x7);

		/* Enable Mic */
		snd_soc_update_bits(codec, DA7210_INMIX_L, 0x1F, 0x1);
		snd_soc_update_bits(codec, DA7210_INMIX_R, 0x1F, 0x1);
	}

	return 0;
}

/*
 * Set PCM DAI word length.
 */
static int da7210_hw_params(struct snd_pcm_substream *substream,
			    struct snd_pcm_hw_params *params,
			    struct snd_soc_dai *dai)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
237
	struct snd_soc_codec *codec = rtd->codec;
238
	u32 dai_cfg1;
239
	u32 hpf_reg, hpf_mask, hpf_value;
240
	u32 fs, bypass;
241 242

	/* set DAI source to Left and Right ADC */
A
Axel Lin 已提交
243
	snd_soc_write(codec, DA7210_DAI_SRC_SEL,
244 245 246
		     DA7210_DAI_OUT_R_SRC | DA7210_DAI_OUT_L_SRC);

	/* Enable DAI */
A
Axel Lin 已提交
247
	snd_soc_write(codec, DA7210_DAI_CFG3, DA7210_DAI_OE | DA7210_DAI_EN);
248

A
Axel Lin 已提交
249
	dai_cfg1 = 0xFC & snd_soc_read(codec, DA7210_DAI_CFG1);
250 251 252 253 254

	switch (params_format(params)) {
	case SNDRV_PCM_FORMAT_S16_LE:
		dai_cfg1 |= DA7210_DAI_WORD_S16_LE;
		break;
255 256 257
	case SNDRV_PCM_FORMAT_S20_3LE:
		dai_cfg1 |= DA7210_DAI_WORD_S20_3LE;
		break;
258 259 260
	case SNDRV_PCM_FORMAT_S24_LE:
		dai_cfg1 |= DA7210_DAI_WORD_S24_LE;
		break;
261 262 263
	case SNDRV_PCM_FORMAT_S32_LE:
		dai_cfg1 |= DA7210_DAI_WORD_S32_LE;
		break;
264 265 266 267
	default:
		return -EINVAL;
	}

A
Axel Lin 已提交
268
	snd_soc_write(codec, DA7210_DAI_CFG1, dai_cfg1);
269

270 271 272
	hpf_reg = (SNDRV_PCM_STREAM_PLAYBACK == substream->stream) ?
		DA7210_DAC_HPF : DA7210_ADC_HPF;

273
	switch (params_rate(params)) {
274 275 276 277
	case 8000:
		fs		= DA7210_PLL_FS_8000;
		hpf_mask	= DA7210_VOICE_F0_MASK	| DA7210_VOICE_EN;
		hpf_value	= DA7210_VOICE_F0_25	| DA7210_VOICE_EN;
278 279 280 281 282 283 284
		bypass		= DA7210_PLL_BYP;
		break;
	case 11025:
		fs		= DA7210_PLL_FS_11025;
		hpf_mask	= DA7210_VOICE_F0_MASK	| DA7210_VOICE_EN;
		hpf_value	= DA7210_VOICE_F0_25	| DA7210_VOICE_EN;
		bypass		= 0;
285 286 287 288 289
		break;
	case 12000:
		fs		= DA7210_PLL_FS_12000;
		hpf_mask	= DA7210_VOICE_F0_MASK	| DA7210_VOICE_EN;
		hpf_value	= DA7210_VOICE_F0_25	| DA7210_VOICE_EN;
290
		bypass		= DA7210_PLL_BYP;
291 292 293 294 295
		break;
	case 16000:
		fs		= DA7210_PLL_FS_16000;
		hpf_mask	= DA7210_VOICE_F0_MASK	| DA7210_VOICE_EN;
		hpf_value	= DA7210_VOICE_F0_25	| DA7210_VOICE_EN;
296 297 298 299 300 301 302
		bypass		= DA7210_PLL_BYP;
		break;
	case 22050:
		fs		= DA7210_PLL_FS_22050;
		hpf_mask	= DA7210_VOICE_EN;
		hpf_value	= 0;
		bypass		= 0;
303 304 305 306 307
		break;
	case 32000:
		fs		= DA7210_PLL_FS_32000;
		hpf_mask	= DA7210_VOICE_EN;
		hpf_value	= 0;
308 309 310 311 312 313 314
		bypass		= DA7210_PLL_BYP;
		break;
	case 44100:
		fs		= DA7210_PLL_FS_44100;
		hpf_mask	= DA7210_VOICE_EN;
		hpf_value	= 0;
		bypass		= 0;
315
		break;
316
	case 48000:
317 318 319
		fs		= DA7210_PLL_FS_48000;
		hpf_mask	= DA7210_VOICE_EN;
		hpf_value	= 0;
320 321 322 323 324 325 326
		bypass		= DA7210_PLL_BYP;
		break;
	case 88200:
		fs		= DA7210_PLL_FS_88200;
		hpf_mask	= DA7210_VOICE_EN;
		hpf_value	= 0;
		bypass		= 0;
327 328 329 330 331
		break;
	case 96000:
		fs		= DA7210_PLL_FS_96000;
		hpf_mask	= DA7210_VOICE_EN;
		hpf_value	= 0;
332
		bypass		= DA7210_PLL_BYP;
333 334 335 336 337
		break;
	default:
		return -EINVAL;
	}

338 339 340
	/* Disable active mode */
	snd_soc_update_bits(codec, DA7210_STARTUP1, DA7210_SC_MST_EN, 0);

341 342
	snd_soc_update_bits(codec, hpf_reg, hpf_mask, hpf_value);
	snd_soc_update_bits(codec, DA7210_PLL, DA7210_PLL_FS_MASK, fs);
343 344 345 346 347
	snd_soc_update_bits(codec, DA7210_PLL_DIV3, DA7210_PLL_BYP, bypass);

	/* Enable active mode */
	snd_soc_update_bits(codec, DA7210_STARTUP1,
			    DA7210_SC_MST_EN, DA7210_SC_MST_EN);
348 349 350 351 352 353 354 355 356 357 358 359 360

	return 0;
}

/*
 * Set DAI mode and Format
 */
static int da7210_set_dai_fmt(struct snd_soc_dai *codec_dai, u32 fmt)
{
	struct snd_soc_codec *codec = codec_dai->codec;
	u32 dai_cfg1;
	u32 dai_cfg3;

A
Axel Lin 已提交
361 362
	dai_cfg1 = 0x7f & snd_soc_read(codec, DA7210_DAI_CFG1);
	dai_cfg3 = 0xfc & snd_soc_read(codec, DA7210_DAI_CFG3);
363 364 365 366 367

	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
	case SND_SOC_DAIFMT_CBM_CFM:
		dai_cfg1 |= DA7210_DAI_MODE_MASTER;
		break;
368 369 370
	case SND_SOC_DAIFMT_CBS_CFS:
		dai_cfg1 |= DA7210_DAI_MODE_SLAVE;
		break;
371 372 373 374 375 376 377 378 379 380 381 382
	default:
		return -EINVAL;
	}

	/* FIXME
	 *
	 * It support I2S only now
	 */
	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
	case SND_SOC_DAIFMT_I2S:
		dai_cfg3 |= DA7210_DAI_FORMAT_I2SMODE;
		break;
383 384 385 386 387 388
	case SND_SOC_DAIFMT_LEFT_J:
		dai_cfg3 |= DA7210_DAI_FORMAT_LEFT_J;
		break;
	case SND_SOC_DAIFMT_RIGHT_J:
		dai_cfg3 |= DA7210_DAI_FORMAT_RIGHT_J;
		break;
389 390 391 392 393 394 395 396 397 398
	default:
		return -EINVAL;
	}

	/* FIXME
	 *
	 * It support 64bit data transmission only now
	 */
	dai_cfg1 |= DA7210_DAI_FLEN_64BIT;

A
Axel Lin 已提交
399 400
	snd_soc_write(codec, DA7210_DAI_CFG1, dai_cfg1);
	snd_soc_write(codec, DA7210_DAI_CFG3, dai_cfg3);
401 402 403 404

	return 0;
}

405 406
#define DA7210_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
			SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
407 408 409 410 411 412 413 414

/* DAI operations */
static struct snd_soc_dai_ops da7210_dai_ops = {
	.startup	= da7210_startup,
	.hw_params	= da7210_hw_params,
	.set_fmt	= da7210_set_dai_fmt,
};

415 416
static struct snd_soc_dai_driver da7210_dai = {
	.name = "da7210-hifi",
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
	/* playback capabilities */
	.playback = {
		.stream_name = "Playback",
		.channels_min = 1,
		.channels_max = 2,
		.rates = SNDRV_PCM_RATE_8000_96000,
		.formats = DA7210_FORMATS,
	},
	/* capture capabilities */
	.capture = {
		.stream_name = "Capture",
		.channels_min = 1,
		.channels_max = 2,
		.rates = SNDRV_PCM_RATE_8000_96000,
		.formats = DA7210_FORMATS,
	},
	.ops = &da7210_dai_ops,
434
	.symmetric_rates = 1,
435 436
};

437
static int da7210_probe(struct snd_soc_codec *codec)
438
{
439
	struct da7210_priv *da7210 = snd_soc_codec_get_drvdata(codec);
A
Axel Lin 已提交
440
	int ret;
441

442
	dev_info(codec->dev, "DA7210 Audio Codec %s\n", DA7210_VERSION);
443

A
Axel Lin 已提交
444 445 446 447 448
	ret = snd_soc_codec_set_cache_io(codec, 8, 8, da7210->control_type);
	if (ret < 0) {
		dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
		return ret;
	}
449 450 451 452

	/* FIXME
	 *
	 * This driver use fixed value here
453 454 455 456 457 458 459
	 * And below settings expects MCLK = 12.288MHz
	 *
	 * When you select different MCLK, please check...
	 *      DA7210_PLL_DIV1 val
	 *      DA7210_PLL_DIV2 val
	 *      DA7210_PLL_DIV3 val
	 *      DA7210_PLL_DIV3 :: DA7210_MCLK_RANGExxx
460 461
	 */

462 463
	/*
	 * make sure that DA7210 use bypass mode before start up
464
	 */
A
Axel Lin 已提交
465 466
	snd_soc_write(codec, DA7210_STARTUP1, 0);
	snd_soc_write(codec, DA7210_PLL_DIV3,
467
		     DA7210_MCLK_RANGE_10_20_MHZ | DA7210_PLL_BYP);
468 469 470 471 472 473

	/*
	 * ADC settings
	 */

	/* Enable Left & Right MIC PGA and Mic Bias */
A
Axel Lin 已提交
474 475
	snd_soc_write(codec, DA7210_MIC_L, DA7210_MIC_L_EN | DA7210_MICBIAS_EN);
	snd_soc_write(codec, DA7210_MIC_R, DA7210_MIC_R_EN);
476 477

	/* Enable Left and Right input PGA */
A
Axel Lin 已提交
478 479
	snd_soc_write(codec, DA7210_INMIX_L, DA7210_IN_L_EN);
	snd_soc_write(codec, DA7210_INMIX_R, DA7210_IN_R_EN);
480 481

	/* Enable Left and Right ADC */
A
Axel Lin 已提交
482
	snd_soc_write(codec, DA7210_ADC, DA7210_ADC_L_EN | DA7210_ADC_R_EN);
483 484 485 486 487 488

	/*
	 * DAC settings
	 */

	/* Enable Left and Right DAC */
A
Axel Lin 已提交
489
	snd_soc_write(codec, DA7210_DAC_SEL,
490 491 492 493
		     DA7210_DAC_L_SRC_DAI_L | DA7210_DAC_L_EN |
		     DA7210_DAC_R_SRC_DAI_R | DA7210_DAC_R_EN);

	/* Enable Left and Right out PGA */
A
Axel Lin 已提交
494 495
	snd_soc_write(codec, DA7210_OUTMIX_L, DA7210_OUT_L_EN);
	snd_soc_write(codec, DA7210_OUTMIX_R, DA7210_OUT_R_EN);
496 497

	/* Enable Left and Right HeadPhone PGA */
A
Axel Lin 已提交
498
	snd_soc_write(codec, DA7210_HP_CFG,
499 500 501 502
		     DA7210_HP_2CAP_MODE | DA7210_HP_SENSE_EN |
		     DA7210_HP_L_EN | DA7210_HP_MODE | DA7210_HP_R_EN);

	/* Diable PLL and bypass it */
A
Axel Lin 已提交
503
	snd_soc_write(codec, DA7210_PLL, DA7210_PLL_FS_48000);
504

505 506 507 508 509 510 511 512 513
	/*
	 * If 48kHz sound came, it use bypass mode,
	 * and when it is 44.1kHz, it use PLL.
	 *
	 * This time, this driver sets PLL always ON
	 * and controls bypass/PLL mode by switching
	 * DA7210_PLL_DIV3 :: DA7210_PLL_BYP bit.
	 *   see da7210_hw_params
	 */
A
Axel Lin 已提交
514 515 516
	snd_soc_write(codec, DA7210_PLL_DIV1, 0xE5); /* MCLK = 12.288MHz */
	snd_soc_write(codec, DA7210_PLL_DIV2, 0x99);
	snd_soc_write(codec, DA7210_PLL_DIV3, 0x0A |
517
		     DA7210_MCLK_RANGE_10_20_MHZ | DA7210_PLL_BYP);
518 519 520
	snd_soc_update_bits(codec, DA7210_PLL, DA7210_PLL_EN, DA7210_PLL_EN);

	/* As suggested by Dialog */
A
Axel Lin 已提交
521 522 523 524 525 526
	snd_soc_write(codec, DA7210_A_HID_UNLOCK,	0x8B); /* unlock */
	snd_soc_write(codec, DA7210_A_TEST_UNLOCK,	0xB4);
	snd_soc_write(codec, DA7210_A_PLL1,		0x01);
	snd_soc_write(codec, DA7210_A_CP_MODE,		0x7C);
	snd_soc_write(codec, DA7210_A_HID_UNLOCK,	0x00); /* re-lock */
	snd_soc_write(codec, DA7210_A_TEST_UNLOCK,	0x00);
527 528

	/* Activate all enabled subsystem */
A
Axel Lin 已提交
529
	snd_soc_write(codec, DA7210_STARTUP1, DA7210_SC_MST_EN);
530

531 532
	snd_soc_add_controls(codec, da7210_snd_controls,
			     ARRAY_SIZE(da7210_snd_controls));
533

534
	dev_info(codec->dev, "DA7210 Audio Codec %s\n", DA7210_VERSION);
535

536
	return 0;
537 538
}

539
static struct snd_soc_codec_driver soc_codec_dev_da7210 = {
K
Kuninori Morimoto 已提交
540 541 542
	.probe			= da7210_probe,
	.reg_cache_size		= ARRAY_SIZE(da7210_reg),
	.reg_word_size		= sizeof(u8),
543
	.reg_cache_default	= da7210_reg,
A
Axel Lin 已提交
544
	.volatile_register	= da7210_volatile_register,
545 546
};

547
#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
548 549
static int __devinit da7210_i2c_probe(struct i2c_client *i2c,
			   	      const struct i2c_device_id *id)
550 551 552 553 554 555 556 557 558
{
	struct da7210_priv *da7210;
	int ret;

	da7210 = kzalloc(sizeof(struct da7210_priv), GFP_KERNEL);
	if (!da7210)
		return -ENOMEM;

	i2c_set_clientdata(i2c, da7210);
559
	da7210->control_type = SND_SOC_I2C;
560

561 562 563
	ret =  snd_soc_register_codec(&i2c->dev,
			&soc_codec_dev_da7210, &da7210_dai, 1);
	if (ret < 0)
564
		kfree(da7210);
565 566 567 568

	return ret;
}

569
static int __devexit da7210_i2c_remove(struct i2c_client *client)
570
{
571 572
	snd_soc_unregister_codec(&client->dev);
	kfree(i2c_get_clientdata(client));
573 574 575 576 577 578 579 580 581 582 583 584
	return 0;
}

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

/* I2C codec control layer */
static struct i2c_driver da7210_i2c_driver = {
	.driver = {
585
		.name = "da7210-codec",
586 587
		.owner = THIS_MODULE,
	},
K
Kuninori Morimoto 已提交
588 589 590
	.probe		= da7210_i2c_probe,
	.remove		= __devexit_p(da7210_i2c_remove),
	.id_table	= da7210_i2c_id,
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
};
#endif

static int __init da7210_modinit(void)
{
	int ret = 0;
#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
	ret = i2c_add_driver(&da7210_i2c_driver);
#endif
	return ret;
}
module_init(da7210_modinit);

static void __exit da7210_exit(void)
{
#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
	i2c_del_driver(&da7210_i2c_driver);
#endif
}
module_exit(da7210_exit);

MODULE_DESCRIPTION("ASoC DA7210 driver");
MODULE_AUTHOR("David Chen, Kuninori Morimoto");
MODULE_LICENSE("GPL");