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