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