xref: /linux/sound/soc/codecs/da7210.c (revision cdb138080b78146d1cdadba9f5dadbeb97445b91)
1 /*
2  * DA7210 ALSA Soc codec driver
3  *
4  * Copyright (c) 2009 Dialog Semiconductor
5  * Written by David Chen <Dajun.chen@diasemi.com>
6  *
7  * Copyright (C) 2009 Renesas Solutions Corp.
8  * Cleanups by Kuninori Morimoto <morimoto.kuninori@renesas.com>
9  *
10  * Tested on SuperH Ecovec24 board with S16/S24 LE in 48KHz using I2S
11  *
12  * This program is free software; you can redistribute  it and/or modify it
13  * under  the terms of  the GNU General  Public License as published by the
14  * Free Software Foundation;  either version 2 of the  License, or (at your
15  * option) any later version.
16  */
17 
18 #include <linux/delay.h>
19 #include <linux/i2c.h>
20 #include <linux/platform_device.h>
21 #include <linux/slab.h>
22 #include <sound/pcm.h>
23 #include <sound/pcm_params.h>
24 #include <sound/soc-dapm.h>
25 #include <sound/initval.h>
26 #include <sound/tlv.h>
27 
28 #include "da7210.h"
29 
30 /* DA7210 register space */
31 #define DA7210_STATUS			0x02
32 #define DA7210_STARTUP1			0x03
33 #define DA7210_MIC_L			0x07
34 #define DA7210_MIC_R			0x08
35 #define DA7210_INMIX_L			0x0D
36 #define DA7210_INMIX_R			0x0E
37 #define DA7210_ADC_HPF			0x0F
38 #define DA7210_ADC			0x10
39 #define DA7210_DAC_HPF			0x14
40 #define DA7210_DAC_L			0x15
41 #define DA7210_DAC_R			0x16
42 #define DA7210_DAC_SEL			0x17
43 #define DA7210_OUTMIX_L			0x1C
44 #define DA7210_OUTMIX_R			0x1D
45 #define DA7210_HP_L_VOL			0x21
46 #define DA7210_HP_R_VOL			0x22
47 #define DA7210_HP_CFG			0x23
48 #define DA7210_DAI_SRC_SEL		0x25
49 #define DA7210_DAI_CFG1			0x26
50 #define DA7210_DAI_CFG3			0x28
51 #define DA7210_PLL_DIV1			0x29
52 #define DA7210_PLL_DIV2			0x2A
53 #define DA7210_PLL_DIV3			0x2B
54 #define DA7210_PLL			0x2C
55 #define DA7210_A_HID_UNLOCK		0x8A
56 #define DA7210_A_TEST_UNLOCK		0x8B
57 #define DA7210_A_PLL1			0x90
58 #define DA7210_A_CP_MODE		0xA7
59 
60 /* STARTUP1 bit fields */
61 #define DA7210_SC_MST_EN		(1 << 0)
62 
63 /* MIC_L bit fields */
64 #define DA7210_MICBIAS_EN		(1 << 6)
65 #define DA7210_MIC_L_EN			(1 << 7)
66 
67 /* MIC_R bit fields */
68 #define DA7210_MIC_R_EN			(1 << 7)
69 
70 /* INMIX_L bit fields */
71 #define DA7210_IN_L_EN			(1 << 7)
72 
73 /* INMIX_R bit fields */
74 #define DA7210_IN_R_EN			(1 << 7)
75 
76 /* ADC bit fields */
77 #define DA7210_ADC_L_EN			(1 << 3)
78 #define DA7210_ADC_R_EN			(1 << 7)
79 
80 /* DAC/ADC HPF fields */
81 #define DA7210_VOICE_F0_MASK		(0x7 << 4)
82 #define DA7210_VOICE_F0_25		(1 << 4)
83 #define DA7210_VOICE_EN			(1 << 7)
84 
85 /* DAC_SEL bit fields */
86 #define DA7210_DAC_L_SRC_DAI_L		(4 << 0)
87 #define DA7210_DAC_L_EN			(1 << 3)
88 #define DA7210_DAC_R_SRC_DAI_R		(5 << 4)
89 #define DA7210_DAC_R_EN			(1 << 7)
90 
91 /* OUTMIX_L bit fields */
92 #define DA7210_OUT_L_EN			(1 << 7)
93 
94 /* OUTMIX_R bit fields */
95 #define DA7210_OUT_R_EN			(1 << 7)
96 
97 /* HP_CFG bit fields */
98 #define DA7210_HP_2CAP_MODE		(1 << 1)
99 #define DA7210_HP_SENSE_EN		(1 << 2)
100 #define DA7210_HP_L_EN			(1 << 3)
101 #define DA7210_HP_MODE			(1 << 6)
102 #define DA7210_HP_R_EN			(1 << 7)
103 
104 /* DAI_SRC_SEL bit fields */
105 #define DA7210_DAI_OUT_L_SRC		(6 << 0)
106 #define DA7210_DAI_OUT_R_SRC		(7 << 4)
107 
108 /* DAI_CFG1 bit fields */
109 #define DA7210_DAI_WORD_S16_LE		(0 << 0)
110 #define DA7210_DAI_WORD_S24_LE		(2 << 0)
111 #define DA7210_DAI_FLEN_64BIT		(1 << 2)
112 #define DA7210_DAI_MODE_MASTER		(1 << 7)
113 
114 /* DAI_CFG3 bit fields */
115 #define DA7210_DAI_FORMAT_I2SMODE	(0 << 0)
116 #define DA7210_DAI_OE			(1 << 3)
117 #define DA7210_DAI_EN			(1 << 7)
118 
119 /*PLL_DIV3 bit fields */
120 #define DA7210_MCLK_RANGE_10_20_MHZ	(1 << 4)
121 #define DA7210_PLL_BYP			(1 << 6)
122 
123 /* PLL bit fields */
124 #define DA7210_PLL_FS_MASK		(0xF << 0)
125 #define DA7210_PLL_FS_8000		(0x1 << 0)
126 #define DA7210_PLL_FS_11025		(0x2 << 0)
127 #define DA7210_PLL_FS_12000		(0x3 << 0)
128 #define DA7210_PLL_FS_16000		(0x5 << 0)
129 #define DA7210_PLL_FS_22050		(0x6 << 0)
130 #define DA7210_PLL_FS_24000		(0x7 << 0)
131 #define DA7210_PLL_FS_32000		(0x9 << 0)
132 #define DA7210_PLL_FS_44100		(0xA << 0)
133 #define DA7210_PLL_FS_48000		(0xB << 0)
134 #define DA7210_PLL_FS_88200		(0xE << 0)
135 #define DA7210_PLL_FS_96000		(0xF << 0)
136 #define DA7210_PLL_EN			(0x1 << 7)
137 
138 #define DA7210_VERSION "0.0.1"
139 
140 /*
141  * Playback Volume
142  *
143  * max		: 0x3F (+15.0 dB)
144  *		   (1.5 dB step)
145  * min		: 0x11 (-54.0 dB)
146  * mute		: 0x10
147  * reserved	: 0x00 - 0x0F
148  *
149  * ** FIXME **
150  *
151  * Reserved area are considered as "mute".
152  * -> min = -79.5 dB
153  */
154 static const DECLARE_TLV_DB_SCALE(hp_out_tlv, -7950, 150, 1);
155 
156 static const struct snd_kcontrol_new da7210_snd_controls[] = {
157 
158 	SOC_DOUBLE_R_TLV("HeadPhone Playback Volume",
159 			 DA7210_HP_L_VOL, DA7210_HP_R_VOL,
160 			 0, 0x3F, 0, hp_out_tlv),
161 };
162 
163 /* Codec private data */
164 struct da7210_priv {
165 	struct snd_soc_codec codec;
166 };
167 
168 static struct snd_soc_codec *da7210_codec;
169 
170 /*
171  * Register cache
172  */
173 static const u8 da7210_reg[] = {
174 	0x00, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,	/* R0  - R7  */
175 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,	/* R8  - RF  */
176 	0x00, 0x00, 0x00, 0x00, 0x08, 0x10, 0x10, 0x54,	/* R10 - R17 */
177 	0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,	/* R18 - R1F */
178 	0x00, 0x00, 0x00, 0x02, 0x00, 0x76, 0x00, 0x00,	/* R20 - R27 */
179 	0x04, 0x00, 0x00, 0x30, 0x2A, 0x00, 0x40, 0x00,	/* R28 - R2F */
180 	0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0x00,	/* R30 - R37 */
181 	0x40, 0x00, 0x40, 0x00, 0x40, 0x00, 0x00, 0x00,	/* R38 - R3F */
182 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,	/* R40 - R4F */
183 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,	/* R48 - R4F */
184 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,	/* R50 - R57 */
185 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,	/* R58 - R5F */
186 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,	/* R60 - R67 */
187 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,	/* R68 - R6F */
188 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,	/* R70 - R77 */
189 	0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x54, 0x00,	/* R78 - R7F */
190 	0x00, 0x00, 0x2C, 0x00, 0x00, 0x00, 0x00, 0x00,	/* R80 - R87 */
191 	0x00,						/* R88       */
192 };
193 
194 /*
195  * Read da7210 register cache
196  */
197 static inline u32 da7210_read_reg_cache(struct snd_soc_codec *codec, u32 reg)
198 {
199 	u8 *cache = codec->reg_cache;
200 	BUG_ON(reg >= ARRAY_SIZE(da7210_reg));
201 	return cache[reg];
202 }
203 
204 /*
205  * Write to the da7210 register space
206  */
207 static int da7210_write(struct snd_soc_codec *codec, u32 reg, u32 value)
208 {
209 	u8 *cache = codec->reg_cache;
210 	u8 data[2];
211 
212 	BUG_ON(codec->volatile_register);
213 
214 	data[0] = reg & 0xff;
215 	data[1] = value & 0xff;
216 
217 	if (reg >= codec->reg_cache_size)
218 		return -EIO;
219 
220 	if (2 != codec->hw_write(codec->control_data, data, 2))
221 		return -EIO;
222 
223 	cache[reg] = value;
224 	return 0;
225 }
226 
227 /*
228  * Read from the da7210 register space.
229  */
230 static inline u32 da7210_read(struct snd_soc_codec *codec, u32 reg)
231 {
232 	if (DA7210_STATUS == reg)
233 		return i2c_smbus_read_byte_data(codec->control_data, reg);
234 
235 	return da7210_read_reg_cache(codec, reg);
236 }
237 
238 static int da7210_startup(struct snd_pcm_substream *substream,
239 			  struct snd_soc_dai *dai)
240 {
241 	int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
242 	struct snd_soc_codec *codec = dai->codec;
243 
244 	if (is_play) {
245 		/* Enable Out */
246 		snd_soc_update_bits(codec, DA7210_OUTMIX_L, 0x1F, 0x10);
247 		snd_soc_update_bits(codec, DA7210_OUTMIX_R, 0x1F, 0x10);
248 
249 	} else {
250 		/* Volume 7 */
251 		snd_soc_update_bits(codec, DA7210_MIC_L, 0x7, 0x7);
252 		snd_soc_update_bits(codec, DA7210_MIC_R, 0x7, 0x7);
253 
254 		/* Enable Mic */
255 		snd_soc_update_bits(codec, DA7210_INMIX_L, 0x1F, 0x1);
256 		snd_soc_update_bits(codec, DA7210_INMIX_R, 0x1F, 0x1);
257 	}
258 
259 	return 0;
260 }
261 
262 /*
263  * Set PCM DAI word length.
264  */
265 static int da7210_hw_params(struct snd_pcm_substream *substream,
266 			    struct snd_pcm_hw_params *params,
267 			    struct snd_soc_dai *dai)
268 {
269 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
270 	struct snd_soc_device *socdev = rtd->socdev;
271 	struct snd_soc_codec *codec = socdev->card->codec;
272 	u32 dai_cfg1;
273 	u32 hpf_reg, hpf_mask, hpf_value;
274 	u32 fs, bypass;
275 
276 	/* set DAI source to Left and Right ADC */
277 	da7210_write(codec, DA7210_DAI_SRC_SEL,
278 		     DA7210_DAI_OUT_R_SRC | DA7210_DAI_OUT_L_SRC);
279 
280 	/* Enable DAI */
281 	da7210_write(codec, DA7210_DAI_CFG3, DA7210_DAI_OE | DA7210_DAI_EN);
282 
283 	dai_cfg1 = 0xFC & da7210_read(codec, DA7210_DAI_CFG1);
284 
285 	switch (params_format(params)) {
286 	case SNDRV_PCM_FORMAT_S16_LE:
287 		dai_cfg1 |= DA7210_DAI_WORD_S16_LE;
288 		break;
289 	case SNDRV_PCM_FORMAT_S24_LE:
290 		dai_cfg1 |= DA7210_DAI_WORD_S24_LE;
291 		break;
292 	default:
293 		return -EINVAL;
294 	}
295 
296 	da7210_write(codec, DA7210_DAI_CFG1, dai_cfg1);
297 
298 	hpf_reg = (SNDRV_PCM_STREAM_PLAYBACK == substream->stream) ?
299 		DA7210_DAC_HPF : DA7210_ADC_HPF;
300 
301 	switch (params_rate(params)) {
302 	case 8000:
303 		fs		= DA7210_PLL_FS_8000;
304 		hpf_mask	= DA7210_VOICE_F0_MASK	| DA7210_VOICE_EN;
305 		hpf_value	= DA7210_VOICE_F0_25	| DA7210_VOICE_EN;
306 		bypass		= DA7210_PLL_BYP;
307 		break;
308 	case 11025:
309 		fs		= DA7210_PLL_FS_11025;
310 		hpf_mask	= DA7210_VOICE_F0_MASK	| DA7210_VOICE_EN;
311 		hpf_value	= DA7210_VOICE_F0_25	| DA7210_VOICE_EN;
312 		bypass		= 0;
313 		break;
314 	case 12000:
315 		fs		= DA7210_PLL_FS_12000;
316 		hpf_mask	= DA7210_VOICE_F0_MASK	| DA7210_VOICE_EN;
317 		hpf_value	= DA7210_VOICE_F0_25	| DA7210_VOICE_EN;
318 		bypass		= DA7210_PLL_BYP;
319 		break;
320 	case 16000:
321 		fs		= DA7210_PLL_FS_16000;
322 		hpf_mask	= DA7210_VOICE_F0_MASK	| DA7210_VOICE_EN;
323 		hpf_value	= DA7210_VOICE_F0_25	| DA7210_VOICE_EN;
324 		bypass		= DA7210_PLL_BYP;
325 		break;
326 	case 22050:
327 		fs		= DA7210_PLL_FS_22050;
328 		hpf_mask	= DA7210_VOICE_EN;
329 		hpf_value	= 0;
330 		bypass		= 0;
331 		break;
332 	case 32000:
333 		fs		= DA7210_PLL_FS_32000;
334 		hpf_mask	= DA7210_VOICE_EN;
335 		hpf_value	= 0;
336 		bypass		= DA7210_PLL_BYP;
337 		break;
338 	case 44100:
339 		fs		= DA7210_PLL_FS_44100;
340 		hpf_mask	= DA7210_VOICE_EN;
341 		hpf_value	= 0;
342 		bypass		= 0;
343 		break;
344 	case 48000:
345 		fs		= DA7210_PLL_FS_48000;
346 		hpf_mask	= DA7210_VOICE_EN;
347 		hpf_value	= 0;
348 		bypass		= DA7210_PLL_BYP;
349 		break;
350 	case 88200:
351 		fs		= DA7210_PLL_FS_88200;
352 		hpf_mask	= DA7210_VOICE_EN;
353 		hpf_value	= 0;
354 		bypass		= 0;
355 		break;
356 	case 96000:
357 		fs		= DA7210_PLL_FS_96000;
358 		hpf_mask	= DA7210_VOICE_EN;
359 		hpf_value	= 0;
360 		bypass		= DA7210_PLL_BYP;
361 		break;
362 	default:
363 		return -EINVAL;
364 	}
365 
366 	/* Disable active mode */
367 	snd_soc_update_bits(codec, DA7210_STARTUP1, DA7210_SC_MST_EN, 0);
368 
369 	snd_soc_update_bits(codec, hpf_reg, hpf_mask, hpf_value);
370 	snd_soc_update_bits(codec, DA7210_PLL, DA7210_PLL_FS_MASK, fs);
371 	snd_soc_update_bits(codec, DA7210_PLL_DIV3, DA7210_PLL_BYP, bypass);
372 
373 	/* Enable active mode */
374 	snd_soc_update_bits(codec, DA7210_STARTUP1,
375 			    DA7210_SC_MST_EN, DA7210_SC_MST_EN);
376 
377 	return 0;
378 }
379 
380 /*
381  * Set DAI mode and Format
382  */
383 static int da7210_set_dai_fmt(struct snd_soc_dai *codec_dai, u32 fmt)
384 {
385 	struct snd_soc_codec *codec = codec_dai->codec;
386 	u32 dai_cfg1;
387 	u32 dai_cfg3;
388 
389 	dai_cfg1 = 0x7f & da7210_read(codec, DA7210_DAI_CFG1);
390 	dai_cfg3 = 0xfc & da7210_read(codec, DA7210_DAI_CFG3);
391 
392 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
393 	case SND_SOC_DAIFMT_CBM_CFM:
394 		dai_cfg1 |= DA7210_DAI_MODE_MASTER;
395 		break;
396 	default:
397 		return -EINVAL;
398 	}
399 
400 	/* FIXME
401 	 *
402 	 * It support I2S only now
403 	 */
404 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
405 	case SND_SOC_DAIFMT_I2S:
406 		dai_cfg3 |= DA7210_DAI_FORMAT_I2SMODE;
407 		break;
408 	default:
409 		return -EINVAL;
410 	}
411 
412 	/* FIXME
413 	 *
414 	 * It support 64bit data transmission only now
415 	 */
416 	dai_cfg1 |= DA7210_DAI_FLEN_64BIT;
417 
418 	da7210_write(codec, DA7210_DAI_CFG1, dai_cfg1);
419 	da7210_write(codec, DA7210_DAI_CFG3, dai_cfg3);
420 
421 	return 0;
422 }
423 
424 #define DA7210_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE)
425 
426 /* DAI operations */
427 static struct snd_soc_dai_ops da7210_dai_ops = {
428 	.startup	= da7210_startup,
429 	.hw_params	= da7210_hw_params,
430 	.set_fmt	= da7210_set_dai_fmt,
431 };
432 
433 struct snd_soc_dai da7210_dai = {
434 	.name = "DA7210 IIS",
435 	.id = 0,
436 	/* playback capabilities */
437 	.playback = {
438 		.stream_name = "Playback",
439 		.channels_min = 1,
440 		.channels_max = 2,
441 		.rates = SNDRV_PCM_RATE_8000_96000,
442 		.formats = DA7210_FORMATS,
443 	},
444 	/* capture capabilities */
445 	.capture = {
446 		.stream_name = "Capture",
447 		.channels_min = 1,
448 		.channels_max = 2,
449 		.rates = SNDRV_PCM_RATE_8000_96000,
450 		.formats = DA7210_FORMATS,
451 	},
452 	.ops = &da7210_dai_ops,
453 	.symmetric_rates = 1,
454 };
455 EXPORT_SYMBOL_GPL(da7210_dai);
456 
457 /*
458  * Initialize the DA7210 driver
459  * register the mixer and dsp interfaces with the kernel
460  */
461 static int da7210_init(struct da7210_priv *da7210)
462 {
463 	struct snd_soc_codec *codec = &da7210->codec;
464 	int ret = 0;
465 
466 	if (da7210_codec) {
467 		dev_err(codec->dev, "Another da7210 is registered\n");
468 		return -EINVAL;
469 	}
470 
471 	mutex_init(&codec->mutex);
472 	INIT_LIST_HEAD(&codec->dapm_widgets);
473 	INIT_LIST_HEAD(&codec->dapm_paths);
474 
475 	snd_soc_codec_set_drvdata(codec, da7210);
476 	codec->name		= "DA7210";
477 	codec->owner		= THIS_MODULE;
478 	codec->read		= da7210_read;
479 	codec->write		= da7210_write;
480 	codec->dai		= &da7210_dai;
481 	codec->num_dai		= 1;
482 	codec->hw_write		= (hw_write_t)i2c_master_send;
483 	codec->reg_cache_size	= ARRAY_SIZE(da7210_reg);
484 	codec->reg_cache	= kmemdup(da7210_reg,
485 					  sizeof(da7210_reg), GFP_KERNEL);
486 
487 	if (!codec->reg_cache)
488 		return -ENOMEM;
489 
490 	da7210_dai.dev = codec->dev;
491 	da7210_codec = codec;
492 
493 	ret = snd_soc_register_codec(codec);
494 	if (ret) {
495 		dev_err(codec->dev, "Failed to register CODEC: %d\n", ret);
496 		goto init_err;
497 	}
498 
499 	ret = snd_soc_register_dai(&da7210_dai);
500 	if (ret) {
501 		dev_err(codec->dev, "Failed to register DAI: %d\n", ret);
502 		goto codec_err;
503 	}
504 
505 	/* FIXME
506 	 *
507 	 * This driver use fixed value here
508 	 * And below settings expects MCLK = 12.288MHz
509 	 *
510 	 * When you select different MCLK, please check...
511 	 *      DA7210_PLL_DIV1 val
512 	 *      DA7210_PLL_DIV2 val
513 	 *      DA7210_PLL_DIV3 val
514 	 *      DA7210_PLL_DIV3 :: DA7210_MCLK_RANGExxx
515 	 */
516 
517 	/*
518 	 * make sure that DA7210 use bypass mode before start up
519 	 */
520 	da7210_write(codec, DA7210_STARTUP1, 0);
521 	da7210_write(codec, DA7210_PLL_DIV3,
522 		     DA7210_MCLK_RANGE_10_20_MHZ | DA7210_PLL_BYP);
523 
524 	/*
525 	 * ADC settings
526 	 */
527 
528 	/* Enable Left & Right MIC PGA and Mic Bias */
529 	da7210_write(codec, DA7210_MIC_L, DA7210_MIC_L_EN | DA7210_MICBIAS_EN);
530 	da7210_write(codec, DA7210_MIC_R, DA7210_MIC_R_EN);
531 
532 	/* Enable Left and Right input PGA */
533 	da7210_write(codec, DA7210_INMIX_L, DA7210_IN_L_EN);
534 	da7210_write(codec, DA7210_INMIX_R, DA7210_IN_R_EN);
535 
536 	/* Enable Left and Right ADC */
537 	da7210_write(codec, DA7210_ADC, DA7210_ADC_L_EN | DA7210_ADC_R_EN);
538 
539 	/*
540 	 * DAC settings
541 	 */
542 
543 	/* Enable Left and Right DAC */
544 	da7210_write(codec, DA7210_DAC_SEL,
545 		     DA7210_DAC_L_SRC_DAI_L | DA7210_DAC_L_EN |
546 		     DA7210_DAC_R_SRC_DAI_R | DA7210_DAC_R_EN);
547 
548 	/* Enable Left and Right out PGA */
549 	da7210_write(codec, DA7210_OUTMIX_L, DA7210_OUT_L_EN);
550 	da7210_write(codec, DA7210_OUTMIX_R, DA7210_OUT_R_EN);
551 
552 	/* Enable Left and Right HeadPhone PGA */
553 	da7210_write(codec, DA7210_HP_CFG,
554 		     DA7210_HP_2CAP_MODE | DA7210_HP_SENSE_EN |
555 		     DA7210_HP_L_EN | DA7210_HP_MODE | DA7210_HP_R_EN);
556 
557 	/* Diable PLL and bypass it */
558 	da7210_write(codec, DA7210_PLL, DA7210_PLL_FS_48000);
559 
560 	/*
561 	 * If 48kHz sound came, it use bypass mode,
562 	 * and when it is 44.1kHz, it use PLL.
563 	 *
564 	 * This time, this driver sets PLL always ON
565 	 * and controls bypass/PLL mode by switching
566 	 * DA7210_PLL_DIV3 :: DA7210_PLL_BYP bit.
567 	 *   see da7210_hw_params
568 	 */
569 	da7210_write(codec, DA7210_PLL_DIV1, 0xE5); /* MCLK = 12.288MHz */
570 	da7210_write(codec, DA7210_PLL_DIV2, 0x99);
571 	da7210_write(codec, DA7210_PLL_DIV3, 0x0A |
572 		     DA7210_MCLK_RANGE_10_20_MHZ | DA7210_PLL_BYP);
573 	snd_soc_update_bits(codec, DA7210_PLL, DA7210_PLL_EN, DA7210_PLL_EN);
574 
575 	/* As suggested by Dialog */
576 	da7210_write(codec, DA7210_A_HID_UNLOCK,	0x8B); /* unlock */
577 	da7210_write(codec, DA7210_A_TEST_UNLOCK,	0xB4);
578 	da7210_write(codec, DA7210_A_PLL1,		0x01);
579 	da7210_write(codec, DA7210_A_CP_MODE,		0x7C);
580 	da7210_write(codec, DA7210_A_HID_UNLOCK,	0x00); /* re-lock */
581 	da7210_write(codec, DA7210_A_TEST_UNLOCK,	0x00);
582 
583 	/* Activate all enabled subsystem */
584 	da7210_write(codec, DA7210_STARTUP1, DA7210_SC_MST_EN);
585 
586 	return ret;
587 
588 codec_err:
589 	snd_soc_unregister_codec(codec);
590 init_err:
591 	kfree(codec->reg_cache);
592 	codec->reg_cache = NULL;
593 
594 	return ret;
595 
596 }
597 
598 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
599 static int __devinit da7210_i2c_probe(struct i2c_client *i2c,
600 			   	      const struct i2c_device_id *id)
601 {
602 	struct da7210_priv *da7210;
603 	struct snd_soc_codec *codec;
604 	int ret;
605 
606 	da7210 = kzalloc(sizeof(struct da7210_priv), GFP_KERNEL);
607 	if (!da7210)
608 		return -ENOMEM;
609 
610 	codec = &da7210->codec;
611 	codec->dev = &i2c->dev;
612 
613 	i2c_set_clientdata(i2c, da7210);
614 	codec->control_data = i2c;
615 
616 	ret = da7210_init(da7210);
617 	if (ret < 0) {
618 		pr_err("Failed to initialise da7210 audio codec\n");
619 		kfree(da7210);
620 	}
621 
622 	return ret;
623 }
624 
625 static int __devexit da7210_i2c_remove(struct i2c_client *client)
626 {
627 	struct da7210_priv *da7210 = i2c_get_clientdata(client);
628 
629 	snd_soc_unregister_dai(&da7210_dai);
630 	kfree(da7210->codec.reg_cache);
631 	kfree(da7210);
632 	da7210_codec = NULL;
633 
634 	return 0;
635 }
636 
637 static const struct i2c_device_id da7210_i2c_id[] = {
638 	{ "da7210", 0 },
639 	{ }
640 };
641 MODULE_DEVICE_TABLE(i2c, da7210_i2c_id);
642 
643 /* I2C codec control layer */
644 static struct i2c_driver da7210_i2c_driver = {
645 	.driver = {
646 		.name = "DA7210 I2C Codec",
647 		.owner = THIS_MODULE,
648 	},
649 	.probe = da7210_i2c_probe,
650 	.remove =  __devexit_p(da7210_i2c_remove),
651 	.id_table = da7210_i2c_id,
652 };
653 #endif
654 
655 static int da7210_probe(struct platform_device *pdev)
656 {
657 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
658 	struct snd_soc_codec *codec;
659 	int ret;
660 
661 	if (!da7210_codec) {
662 		dev_err(&pdev->dev, "Codec device not registered\n");
663 		return -ENODEV;
664 	}
665 
666 	socdev->card->codec = da7210_codec;
667 	codec = da7210_codec;
668 
669 	/* Register pcms */
670 	ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
671 	if (ret < 0)
672 		goto pcm_err;
673 
674 	snd_soc_add_controls(da7210_codec, da7210_snd_controls,
675 			     ARRAY_SIZE(da7210_snd_controls));
676 
677 	dev_info(&pdev->dev, "DA7210 Audio Codec %s\n", DA7210_VERSION);
678 
679 pcm_err:
680 	return ret;
681 }
682 
683 static int da7210_remove(struct platform_device *pdev)
684 {
685 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
686 
687 	snd_soc_free_pcms(socdev);
688 	snd_soc_dapm_free(socdev);
689 
690 	return 0;
691 }
692 
693 struct snd_soc_codec_device soc_codec_dev_da7210 = {
694 	.probe =	da7210_probe,
695 	.remove =	da7210_remove,
696 };
697 EXPORT_SYMBOL_GPL(soc_codec_dev_da7210);
698 
699 static int __init da7210_modinit(void)
700 {
701 	int ret = 0;
702 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
703 	ret = i2c_add_driver(&da7210_i2c_driver);
704 #endif
705 	return ret;
706 }
707 module_init(da7210_modinit);
708 
709 static void __exit da7210_exit(void)
710 {
711 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
712 	i2c_del_driver(&da7210_i2c_driver);
713 #endif
714 }
715 module_exit(da7210_exit);
716 
717 MODULE_DESCRIPTION("ASoC DA7210 driver");
718 MODULE_AUTHOR("David Chen, Kuninori Morimoto");
719 MODULE_LICENSE("GPL");
720