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