[ALSA] ASoC: Add support for BCLK based on (Rate * Chn * Word Size)
[deliverable/linux.git] / sound / soc / codecs / wm8731.c
1 /*
2 * wm8731.c -- WM8731 ALSA SoC Audio driver
3 *
4 * Copyright 2005 Openedhand Ltd.
5 *
6 * Author: Richard Purdie <richard@openedhand.com>
7 *
8 * Based on wm8753.c by Liam Girdwood
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
14
15 #include <linux/module.h>
16 #include <linux/moduleparam.h>
17 #include <linux/init.h>
18 #include <linux/delay.h>
19 #include <linux/pm.h>
20 #include <linux/i2c.h>
21 #include <linux/platform_device.h>
22 #include <sound/driver.h>
23 #include <sound/core.h>
24 #include <sound/pcm.h>
25 #include <sound/pcm_params.h>
26 #include <sound/soc.h>
27 #include <sound/soc-dapm.h>
28 #include <sound/initval.h>
29
30 #include "wm8731.h"
31
32 #define AUDIO_NAME "wm8731"
33 #define WM8731_VERSION "0.12"
34
35 /*
36 * Debug
37 */
38
39 #define WM8731_DEBUG 0
40
41 #ifdef WM8731_DEBUG
42 #define dbg(format, arg...) \
43 printk(KERN_DEBUG AUDIO_NAME ": " format "\n" , ## arg)
44 #else
45 #define dbg(format, arg...) do {} while (0)
46 #endif
47 #define err(format, arg...) \
48 printk(KERN_ERR AUDIO_NAME ": " format "\n" , ## arg)
49 #define info(format, arg...) \
50 printk(KERN_INFO AUDIO_NAME ": " format "\n" , ## arg)
51 #define warn(format, arg...) \
52 printk(KERN_WARNING AUDIO_NAME ": " format "\n" , ## arg)
53
54 struct snd_soc_codec_device soc_codec_dev_wm8731;
55
56 /*
57 * wm8731 register cache
58 * We can't read the WM8731 register space when we are
59 * using 2 wire for device control, so we cache them instead.
60 * There is no point in caching the reset register
61 */
62 static const u16 wm8731_reg[WM8731_CACHEREGNUM] = {
63 0x0097, 0x0097, 0x0079, 0x0079,
64 0x000a, 0x0008, 0x009f, 0x000a,
65 0x0000, 0x0000
66 };
67
68 #define WM8731_DAIFMT \
69 (SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_LEFT_J | SND_SOC_DAIFMT_RIGHT_J | \
70 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_NB_IF | SND_SOC_DAIFMT_IB_NF | \
71 SND_SOC_DAIFMT_IB_IF)
72
73 #define WM8731_DIR \
74 (SND_SOC_DAIDIR_PLAYBACK | SND_SOC_DAIDIR_CAPTURE)
75
76 #define WM8731_RATES \
77 (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 | SNDRV_PCM_RATE_16000 | \
78 SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
79 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000)
80
81 #define WM8731_HIFI_BITS \
82 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
83 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
84
85 static struct snd_soc_dai_mode wm8731_modes[] = {
86 /* codec frame and clock master modes */
87 /* 8k */
88 {
89 .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
90 .pcmfmt = WM8731_HIFI_BITS,
91 .pcmrate = SNDRV_PCM_RATE_8000,
92 .pcmdir = WM8731_DIR,
93 .flags = SND_SOC_DAI_BFS_RATE,
94 .fs = 1536,
95 .bfs = 64,
96 },
97 {
98 .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
99 .pcmfmt = WM8731_HIFI_BITS,
100 .pcmrate = SNDRV_PCM_RATE_8000,
101 .pcmdir = WM8731_DIR,
102 .flags = SND_SOC_DAI_BFS_RATE,
103 .fs = 2304,
104 .bfs = 64,
105 },
106 {
107 .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
108 .pcmfmt = WM8731_HIFI_BITS,
109 .pcmrate = SNDRV_PCM_RATE_8000,
110 .pcmdir = WM8731_DIR,
111 .flags = SND_SOC_DAI_BFS_RATE,
112 .fs = 1408,
113 .bfs = 64,
114 },
115 {
116 .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
117 .pcmfmt = WM8731_HIFI_BITS,
118 .pcmrate = SNDRV_PCM_RATE_8000,
119 .pcmdir = WM8731_DIR,
120 .flags = SND_SOC_DAI_BFS_RATE,
121 .fs = 2112,
122 .bfs = 64,
123 },
124
125 /* 32k */
126 {
127 .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
128 .pcmfmt = WM8731_HIFI_BITS,
129 .pcmrate = SNDRV_PCM_RATE_32000,
130 .pcmdir = WM8731_DIR,
131 .flags = SND_SOC_DAI_BFS_RATE,
132 .fs = 384,
133 .bfs = 64,
134 },
135 {
136 .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
137 .pcmfmt = WM8731_HIFI_BITS,
138 .pcmrate = SNDRV_PCM_RATE_32000,
139 .pcmdir = WM8731_DIR,
140 .flags = SND_SOC_DAI_BFS_RATE,
141 .fs = 576,
142 .bfs = 64,
143 },
144
145 /* 44.1k & 48k */
146 {
147 .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
148 .pcmfmt = WM8731_HIFI_BITS,
149 .pcmrate = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
150 .pcmdir = WM8731_DIR,
151 .flags = SND_SOC_DAI_BFS_RATE,
152 .fs = 256,
153 .bfs = 64,
154 },
155 {
156 .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
157 .pcmfmt = WM8731_HIFI_BITS,
158 .pcmrate = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
159 .pcmdir = WM8731_DIR,
160 .flags = SND_SOC_DAI_BFS_RATE,
161 .fs = 384,
162 .bfs = 64,
163 },
164
165 /* 88.2 & 96k */
166 {
167 .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
168 .pcmfmt = WM8731_HIFI_BITS,
169 .pcmrate = SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000,
170 .pcmdir = WM8731_DIR,
171 .flags = SND_SOC_DAI_BFS_RATE,
172 .fs = 128,
173 .bfs = 64,
174 },
175 {
176 .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
177 .pcmfmt = WM8731_HIFI_BITS,
178 .pcmrate = SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000,
179 .pcmdir = WM8731_DIR,
180 .flags = SND_SOC_DAI_BFS_RATE,
181 .fs = 192,
182 .bfs = 64,
183 },
184
185 /* USB codec frame and clock master modes */
186 /* 8k */
187 {
188 .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
189 .pcmfmt = WM8731_HIFI_BITS,
190 .pcmrate = SNDRV_PCM_RATE_8000,
191 .pcmdir = WM8731_DIR,
192 .flags = SND_SOC_DAI_BFS_DIV,
193 .fs = 1500,
194 .bfs = SND_SOC_FSBD(1),
195 },
196
197 /* 44.1k */
198 {
199 .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
200 .pcmfmt = WM8731_HIFI_BITS,
201 .pcmrate = SNDRV_PCM_RATE_44100,
202 .pcmdir = WM8731_DIR,
203 .flags = SND_SOC_DAI_BFS_DIV,
204 .fs = 272,
205 .bfs = SND_SOC_FSBD(1),
206 },
207
208 /* 48k */
209 {
210 .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
211 .pcmfmt = WM8731_HIFI_BITS,
212 .pcmrate = SNDRV_PCM_RATE_48000,
213 .pcmdir = WM8731_DIR,
214 .flags = SND_SOC_DAI_BFS_DIV,
215 .fs = 250,
216 .bfs = SND_SOC_FSBD(1),
217 },
218
219 /* 88.2k */
220 {
221 .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
222 .pcmfmt = WM8731_HIFI_BITS,
223 .pcmrate = SNDRV_PCM_RATE_88200,
224 .pcmdir = WM8731_DIR,
225 .flags = SND_SOC_DAI_BFS_DIV,
226 .fs = 136,
227 .bfs = SND_SOC_FSBD(1),
228 },
229
230 /* 96k */
231 {
232 .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
233 .pcmfmt = WM8731_HIFI_BITS,
234 .pcmrate = SNDRV_PCM_RATE_96000,
235 .pcmdir = WM8731_DIR,
236 .flags = SND_SOC_DAI_BFS_DIV,
237 .fs = 125,
238 .bfs = SND_SOC_FSBD(1),
239 },
240
241 /* codec frame and clock slave modes */
242 {
243 .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBS_CFS,
244 .pcmfmt = WM8731_HIFI_BITS,
245 .pcmrate = WM8731_RATES,
246 .pcmdir = WM8731_DIR,
247 .flags = SND_SOC_DAI_BFS_DIV,
248 .fs = SND_SOC_FS_ALL,
249 .bfs = SND_SOC_FSB_ALL,
250 },
251 };
252
253 /*
254 * read wm8731 register cache
255 */
256 static inline unsigned int wm8731_read_reg_cache(struct snd_soc_codec *codec,
257 unsigned int reg)
258 {
259 u16 *cache = codec->reg_cache;
260 if (reg == WM8731_RESET)
261 return 0;
262 if (reg >= WM8731_CACHEREGNUM)
263 return -1;
264 return cache[reg];
265 }
266
267 /*
268 * write wm8731 register cache
269 */
270 static inline void wm8731_write_reg_cache(struct snd_soc_codec *codec,
271 u16 reg, unsigned int value)
272 {
273 u16 *cache = codec->reg_cache;
274 if (reg >= WM8731_CACHEREGNUM)
275 return;
276 cache[reg] = value;
277 }
278
279 /*
280 * write to the WM8731 register space
281 */
282 static int wm8731_write(struct snd_soc_codec *codec, unsigned int reg,
283 unsigned int value)
284 {
285 u8 data[2];
286
287 /* data is
288 * D15..D9 WM8731 register offset
289 * D8...D0 register data
290 */
291 data[0] = (reg << 1) | ((value >> 8) & 0x0001);
292 data[1] = value & 0x00ff;
293
294 wm8731_write_reg_cache (codec, reg, value);
295 if (codec->hw_write(codec->control_data, data, 2) == 2)
296 return 0;
297 else
298 return -EIO;
299 }
300
301 #define wm8731_reset(c) wm8731_write(c, WM8731_RESET, 0)
302
303 static const char *wm8731_input_select[] = {"Line In", "Mic"};
304 static const char *wm8731_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"};
305
306 static const struct soc_enum wm8731_enum[] = {
307 SOC_ENUM_SINGLE(WM8731_APANA, 2, 2, wm8731_input_select),
308 SOC_ENUM_SINGLE(WM8731_APDIGI, 1, 4, wm8731_deemph),
309 };
310
311 static const struct snd_kcontrol_new wm8731_snd_controls[] = {
312
313 SOC_DOUBLE_R("Playback Volume", WM8731_LOUT1V, WM8731_ROUT1V, 0, 127, 0),
314 SOC_DOUBLE_R("Playback ZC Switch", WM8731_LOUT1V, WM8731_ROUT1V, 7, 1, 0),
315
316 SOC_DOUBLE_R("Capture Volume", WM8731_LINVOL, WM8731_RINVOL, 0, 31, 0),
317 SOC_DOUBLE_R("Line Capture Switch", WM8731_LINVOL, WM8731_RINVOL, 7, 1, 1),
318
319 SOC_SINGLE("Mic Boost (+20dB)", WM8731_APANA, 0, 1, 0),
320 SOC_SINGLE("Capture Mic Switch", WM8731_APANA, 1, 1, 1),
321
322 SOC_SINGLE("Sidetone Playback Volume", WM8731_APANA, 6, 3, 1),
323
324 SOC_SINGLE("ADC High Pass Filter Switch", WM8731_APDIGI, 0, 1, 1),
325 SOC_SINGLE("Store DC Offset Switch", WM8731_APDIGI, 4, 1, 0),
326
327 SOC_ENUM("Playback De-emphasis", wm8731_enum[1]),
328 };
329
330 /* add non dapm controls */
331 static int wm8731_add_controls(struct snd_soc_codec *codec)
332 {
333 int err, i;
334
335 for (i = 0; i < ARRAY_SIZE(wm8731_snd_controls); i++) {
336 if ((err = snd_ctl_add(codec->card,
337 snd_soc_cnew(&wm8731_snd_controls[i],codec, NULL))) < 0)
338 return err;
339 }
340
341 return 0;
342 }
343
344 /* Output Mixer */
345 static const struct snd_kcontrol_new wm8731_output_mixer_controls[] = {
346 SOC_DAPM_SINGLE("Line Bypass Switch", WM8731_APANA, 3, 1, 0),
347 SOC_DAPM_SINGLE("Mic Sidetone Switch", WM8731_APANA, 5, 1, 0),
348 SOC_DAPM_SINGLE("HiFi Playback Switch", WM8731_APANA, 4, 1, 0),
349 };
350
351 /* Input mux */
352 static const struct snd_kcontrol_new wm8731_input_mux_controls =
353 SOC_DAPM_ENUM("Input Select", wm8731_enum[0]);
354
355 static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
356 SND_SOC_DAPM_MIXER("Output Mixer", WM8731_PWR, 4, 1,
357 &wm8731_output_mixer_controls[0],
358 ARRAY_SIZE(wm8731_output_mixer_controls)),
359 SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8731_PWR, 3, 1),
360 SND_SOC_DAPM_OUTPUT("LOUT"),
361 SND_SOC_DAPM_OUTPUT("LHPOUT"),
362 SND_SOC_DAPM_OUTPUT("ROUT"),
363 SND_SOC_DAPM_OUTPUT("RHPOUT"),
364 SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8731_PWR, 2, 1),
365 SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &wm8731_input_mux_controls),
366 SND_SOC_DAPM_PGA("Line Input", WM8731_PWR, 0, 1, NULL, 0),
367 SND_SOC_DAPM_MICBIAS("Mic Bias", WM8731_PWR, 1, 1),
368 SND_SOC_DAPM_INPUT("MICIN"),
369 SND_SOC_DAPM_INPUT("RLINEIN"),
370 SND_SOC_DAPM_INPUT("LLINEIN"),
371 };
372
373 static const char *intercon[][3] = {
374 /* output mixer */
375 {"Output Mixer", "Line Bypass Switch", "Line Input"},
376 {"Output Mixer", "HiFi Playback Switch", "DAC"},
377 {"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
378
379 /* outputs */
380 {"RHPOUT", NULL, "Output Mixer"},
381 {"ROUT", NULL, "Output Mixer"},
382 {"LHPOUT", NULL, "Output Mixer"},
383 {"LOUT", NULL, "Output Mixer"},
384
385 /* input mux */
386 {"Input Mux", "Line In", "Line Input"},
387 {"Input Mux", "Mic", "Mic Bias"},
388 {"ADC", NULL, "Input Mux"},
389
390 /* inputs */
391 {"Line Input", NULL, "LLINEIN"},
392 {"Line Input", NULL, "RLINEIN"},
393 {"Mic Bias", NULL, "MICIN"},
394
395 /* terminator */
396 {NULL, NULL, NULL},
397 };
398
399 static int wm8731_add_widgets(struct snd_soc_codec *codec)
400 {
401 int i;
402
403 for(i = 0; i < ARRAY_SIZE(wm8731_dapm_widgets); i++) {
404 snd_soc_dapm_new_control(codec, &wm8731_dapm_widgets[i]);
405 }
406
407 /* set up audio path interconnects */
408 for(i = 0; intercon[i][0] != NULL; i++) {
409 snd_soc_dapm_connect_input(codec, intercon[i][0],
410 intercon[i][1], intercon[i][2]);
411 }
412
413 snd_soc_dapm_new_widgets(codec);
414 return 0;
415 }
416
417 struct _coeff_div {
418 u32 mclk;
419 u32 rate;
420 u16 fs;
421 u8 sr:4;
422 u8 bosr:1;
423 u8 usb:1;
424 };
425
426 /* codec mclk clock divider coefficients */
427 static const struct _coeff_div coeff_div[] = {
428 /* 48k */
429 {12288000, 48000, 256, 0x0, 0x0, 0x0},
430 {18432000, 48000, 384, 0x0, 0x1, 0x0},
431 {12000000, 48000, 250, 0x0, 0x0, 0x1},
432
433 /* 32k */
434 {12288000, 32000, 384, 0x6, 0x0, 0x0},
435 {18432000, 32000, 576, 0x6, 0x1, 0x0},
436
437 /* 8k */
438 {12288000, 8000, 1536, 0x3, 0x0, 0x0},
439 {18432000, 8000, 2304, 0x3, 0x1, 0x0},
440 {11289600, 8000, 1408, 0xb, 0x0, 0x0},
441 {16934400, 8000, 2112, 0xb, 0x1, 0x0},
442 {12000000, 8000, 1500, 0x3, 0x0, 0x1},
443
444 /* 96k */
445 {12288000, 96000, 128, 0x7, 0x0, 0x0},
446 {18432000, 96000, 192, 0x7, 0x1, 0x0},
447 {12000000, 96000, 125, 0x7, 0x0, 0x1},
448
449 /* 44.1k */
450 {11289600, 44100, 256, 0x8, 0x0, 0x0},
451 {16934400, 44100, 384, 0x8, 0x1, 0x0},
452 {12000000, 44100, 272, 0x8, 0x1, 0x1},
453
454 /* 88.2k */
455 {11289600, 88200, 128, 0xf, 0x0, 0x0},
456 {16934400, 88200, 192, 0xf, 0x1, 0x0},
457 {12000000, 88200, 136, 0xf, 0x1, 0x1},
458 };
459
460 static inline int get_coeff(int mclk, int rate)
461 {
462 int i;
463
464 for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
465 if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
466 return i;
467 }
468 return 0;
469 }
470
471 /* WM8731 supports numerous clocks per sample rate */
472 static unsigned int wm8731_config_sysclk(struct snd_soc_codec_dai *dai,
473 struct snd_soc_clock_info *info, unsigned int clk)
474 {
475 dai->mclk = 0;
476
477 /* check that the calculated FS and rate actually match a clock from
478 * the machine driver */
479 if (info->fs * info->rate == clk)
480 dai->mclk = clk;
481
482 return dai->mclk;
483 }
484
485 static int wm8731_pcm_prepare(struct snd_pcm_substream *substream)
486 {
487 struct snd_soc_pcm_runtime *rtd = substream->private_data;
488 struct snd_soc_device *socdev = rtd->socdev;
489 struct snd_soc_codec *codec = socdev->codec;
490 u16 iface = 0, srate;
491 int i = get_coeff(rtd->codec_dai->mclk,
492 snd_soc_get_rate(rtd->codec_dai->dai_runtime.pcmrate));
493
494 /* set master/slave audio interface */
495 switch (rtd->codec_dai->dai_runtime.fmt & SND_SOC_DAIFMT_CLOCK_MASK) {
496 case SND_SOC_DAIFMT_CBM_CFM:
497 iface |= 0x0040;
498 break;
499 case SND_SOC_DAIFMT_CBS_CFS:
500 break;
501 }
502 srate = (coeff_div[i].sr << 2) |
503 (coeff_div[i].bosr << 1) | coeff_div[i].usb;
504 wm8731_write(codec, WM8731_SRATE, srate);
505
506 /* interface format */
507 switch (rtd->codec_dai->dai_runtime.fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
508 case SND_SOC_DAIFMT_I2S:
509 iface |= 0x0002;
510 break;
511 case SND_SOC_DAIFMT_RIGHT_J:
512 break;
513 case SND_SOC_DAIFMT_LEFT_J:
514 iface |= 0x0001;
515 break;
516 case SND_SOC_DAIFMT_DSP_A:
517 iface |= 0x0003;
518 break;
519 case SND_SOC_DAIFMT_DSP_B:
520 iface |= 0x0013;
521 break;
522 }
523
524 /* bit size */
525 switch (rtd->codec_dai->dai_runtime.pcmfmt) {
526 case SNDRV_PCM_FMTBIT_S16_LE:
527 break;
528 case SNDRV_PCM_FMTBIT_S20_3LE:
529 iface |= 0x0004;
530 break;
531 case SNDRV_PCM_FMTBIT_S24_LE:
532 iface |= 0x0008;
533 break;
534 case SNDRV_PCM_FMTBIT_S32_LE:
535 iface |= 0x000c;
536 break;
537 }
538
539 /* clock inversion */
540 switch (rtd->codec_dai->dai_runtime.fmt & SND_SOC_DAIFMT_INV_MASK) {
541 case SND_SOC_DAIFMT_NB_NF:
542 break;
543 case SND_SOC_DAIFMT_IB_IF:
544 iface |= 0x0090;
545 break;
546 case SND_SOC_DAIFMT_IB_NF:
547 iface |= 0x0080;
548 break;
549 case SND_SOC_DAIFMT_NB_IF:
550 iface |= 0x0010;
551 break;
552 }
553
554 /* set iface */
555 wm8731_write(codec, WM8731_IFACE, iface);
556
557 /* set active */
558 wm8731_write(codec, WM8731_ACTIVE, 0x0001);
559 return 0;
560 }
561
562 static void wm8731_shutdown(struct snd_pcm_substream *substream)
563 {
564 struct snd_soc_pcm_runtime *rtd = substream->private_data;
565 struct snd_soc_device *socdev = rtd->socdev;
566 struct snd_soc_codec *codec = socdev->codec;
567
568 /* deactivate */
569 if (!codec->active) {
570 udelay(50);
571 wm8731_write(codec, WM8731_ACTIVE, 0x0);
572 }
573 }
574
575 static int wm8731_mute(struct snd_soc_codec *codec,
576 struct snd_soc_codec_dai *dai, int mute)
577 {
578 u16 mute_reg = wm8731_read_reg_cache(codec, WM8731_APDIGI) & 0xfff7;
579 if (mute)
580 wm8731_write(codec, WM8731_APDIGI, mute_reg | 0x8);
581 else
582 wm8731_write(codec, WM8731_APDIGI, mute_reg);
583 return 0;
584 }
585
586 static int wm8731_dapm_event(struct snd_soc_codec *codec, int event)
587 {
588 u16 reg = wm8731_read_reg_cache(codec, WM8731_PWR) & 0xff7f;
589
590 switch (event) {
591 case SNDRV_CTL_POWER_D0: /* full On */
592 /* vref/mid, osc on, dac unmute */
593 wm8731_write(codec, WM8731_PWR, reg);
594 break;
595 case SNDRV_CTL_POWER_D1: /* partial On */
596 case SNDRV_CTL_POWER_D2: /* partial On */
597 break;
598 case SNDRV_CTL_POWER_D3hot: /* Off, with power */
599 /* everything off except vref/vmid, */
600 wm8731_write(codec, WM8731_PWR, reg | 0x0040);
601 break;
602 case SNDRV_CTL_POWER_D3cold: /* Off, without power */
603 /* everything off, dac mute, inactive */
604 wm8731_write(codec, WM8731_ACTIVE, 0x0);
605 wm8731_write(codec, WM8731_PWR, 0xffff);
606 break;
607 }
608 codec->dapm_state = event;
609 return 0;
610 }
611
612 struct snd_soc_codec_dai wm8731_dai = {
613 .name = "WM8731",
614 .playback = {
615 .stream_name = "Playback",
616 .channels_min = 1,
617 .channels_max = 2,
618 },
619 .capture = {
620 .stream_name = "Capture",
621 .channels_min = 1,
622 .channels_max = 2,
623 },
624 .config_sysclk = wm8731_config_sysclk,
625 .digital_mute = wm8731_mute,
626 .ops = {
627 .prepare = wm8731_pcm_prepare,
628 .shutdown = wm8731_shutdown,
629 },
630 .caps = {
631 .num_modes = ARRAY_SIZE(wm8731_modes),
632 .mode = wm8731_modes,
633 },
634 };
635 EXPORT_SYMBOL_GPL(wm8731_dai);
636
637 static int wm8731_suspend(struct platform_device *pdev, pm_message_t state)
638 {
639 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
640 struct snd_soc_codec *codec = socdev->codec;
641
642 wm8731_write(codec, WM8731_ACTIVE, 0x0);
643 wm8731_dapm_event(codec, SNDRV_CTL_POWER_D3cold);
644 return 0;
645 }
646
647 static int wm8731_resume(struct platform_device *pdev)
648 {
649 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
650 struct snd_soc_codec *codec = socdev->codec;
651 int i;
652 u8 data[2];
653 u16 *cache = codec->reg_cache;
654
655 /* Sync reg_cache with the hardware */
656 for (i = 0; i < ARRAY_SIZE(wm8731_reg); i++) {
657 data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001);
658 data[1] = cache[i] & 0x00ff;
659 codec->hw_write(codec->control_data, data, 2);
660 }
661 wm8731_dapm_event(codec, SNDRV_CTL_POWER_D3hot);
662 wm8731_dapm_event(codec, codec->suspend_dapm_state);
663 return 0;
664 }
665
666 /*
667 * initialise the WM8731 driver
668 * register the mixer and dsp interfaces with the kernel
669 */
670 static int wm8731_init(struct snd_soc_device *socdev)
671 {
672 struct snd_soc_codec *codec = socdev->codec;
673 int reg, ret = 0;
674
675 codec->name = "WM8731";
676 codec->owner = THIS_MODULE;
677 codec->read = wm8731_read_reg_cache;
678 codec->write = wm8731_write;
679 codec->dapm_event = wm8731_dapm_event;
680 codec->dai = &wm8731_dai;
681 codec->num_dai = 1;
682 codec->reg_cache_size = ARRAY_SIZE(wm8731_reg);
683
684 codec->reg_cache =
685 kzalloc(sizeof(u16) * ARRAY_SIZE(wm8731_reg), GFP_KERNEL);
686 if (codec->reg_cache == NULL)
687 return -ENOMEM;
688 memcpy(codec->reg_cache,
689 wm8731_reg, sizeof(u16) * ARRAY_SIZE(wm8731_reg));
690 codec->reg_cache_size = sizeof(u16) * ARRAY_SIZE(wm8731_reg);
691
692 wm8731_reset(codec);
693
694 /* register pcms */
695 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
696 if (ret < 0) {
697 kfree(codec->reg_cache);
698 return ret;
699 }
700
701 /* power on device */
702 wm8731_dapm_event(codec, SNDRV_CTL_POWER_D3hot);
703
704 /* set the update bits */
705 reg = wm8731_read_reg_cache(codec, WM8731_LOUT1V);
706 wm8731_write(codec, WM8731_LOUT1V, reg | 0x0100);
707 reg = wm8731_read_reg_cache(codec, WM8731_ROUT1V);
708 wm8731_write(codec, WM8731_ROUT1V, reg | 0x0100);
709 reg = wm8731_read_reg_cache(codec, WM8731_LINVOL);
710 wm8731_write(codec, WM8731_LINVOL, reg | 0x0100);
711 reg = wm8731_read_reg_cache(codec, WM8731_RINVOL);
712 wm8731_write(codec, WM8731_RINVOL, reg | 0x0100);
713
714 wm8731_add_controls(codec);
715 wm8731_add_widgets(codec);
716 ret = snd_soc_register_card(socdev);
717 if (ret < 0) {
718 snd_soc_free_pcms(socdev);
719 snd_soc_dapm_free(socdev);
720 }
721
722 return ret;
723 }
724
725 static struct snd_soc_device *wm8731_socdev;
726
727 #if defined (CONFIG_I2C) || defined (CONFIG_I2C_MODULE)
728
729 /*
730 * WM8731 2 wire address is determined by GPIO5
731 * state during powerup.
732 * low = 0x1a
733 * high = 0x1b
734 */
735 static unsigned short normal_i2c[] = { 0, I2C_CLIENT_END };
736
737 /* Magic definition of all other variables and things */
738 I2C_CLIENT_INSMOD;
739
740 static struct i2c_driver wm8731_i2c_driver;
741 static struct i2c_client client_template;
742
743 /* If the i2c layer weren't so broken, we could pass this kind of data
744 around */
745
746 static int wm8731_codec_probe(struct i2c_adapter *adap, int addr, int kind)
747 {
748 struct snd_soc_device *socdev = wm8731_socdev;
749 struct wm8731_setup_data *setup = socdev->codec_data;
750 struct snd_soc_codec *codec = socdev->codec;
751 struct i2c_client *i2c;
752 int ret;
753
754 if (addr != setup->i2c_address)
755 return -ENODEV;
756
757 client_template.adapter = adap;
758 client_template.addr = addr;
759
760 i2c = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
761 if (i2c == NULL) {
762 kfree(codec);
763 return -ENOMEM;
764 }
765 memcpy(i2c, &client_template, sizeof(struct i2c_client));
766 i2c_set_clientdata(i2c, codec);
767 codec->control_data = i2c;
768
769 ret = i2c_attach_client(i2c);
770 if (ret < 0) {
771 err("failed to attach codec at addr %x\n", addr);
772 goto err;
773 }
774
775 ret = wm8731_init(socdev);
776 if (ret < 0) {
777 err("failed to initialise WM8731\n");
778 goto err;
779 }
780 return ret;
781
782 err:
783 kfree(codec);
784 kfree(i2c);
785 return ret;
786 }
787
788 static int wm8731_i2c_detach(struct i2c_client *client)
789 {
790 struct snd_soc_codec* codec = i2c_get_clientdata(client);
791 i2c_detach_client(client);
792 kfree(codec->reg_cache);
793 kfree(client);
794 return 0;
795 }
796
797 static int wm8731_i2c_attach(struct i2c_adapter *adap)
798 {
799 return i2c_probe(adap, &addr_data, wm8731_codec_probe);
800 }
801
802 /* corgi i2c codec control layer */
803 static struct i2c_driver wm8731_i2c_driver = {
804 .driver = {
805 .name = "WM8731 I2C Codec",
806 .owner = THIS_MODULE,
807 },
808 .id = I2C_DRIVERID_WM8731,
809 .attach_adapter = wm8731_i2c_attach,
810 .detach_client = wm8731_i2c_detach,
811 .command = NULL,
812 };
813
814 static struct i2c_client client_template = {
815 .name = "WM8731",
816 .driver = &wm8731_i2c_driver,
817 };
818 #endif
819
820 static int wm8731_probe(struct platform_device *pdev)
821 {
822 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
823 struct wm8731_setup_data *setup;
824 struct snd_soc_codec *codec;
825 int ret = 0;
826
827 info("WM8731 Audio Codec %s", WM8731_VERSION);
828
829 setup = socdev->codec_data;
830 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
831 if (codec == NULL)
832 return -ENOMEM;
833
834 socdev->codec = codec;
835 mutex_init(&codec->mutex);
836 INIT_LIST_HEAD(&codec->dapm_widgets);
837 INIT_LIST_HEAD(&codec->dapm_paths);
838
839 wm8731_socdev = socdev;
840 #if defined (CONFIG_I2C) || defined (CONFIG_I2C_MODULE)
841 if (setup->i2c_address) {
842 normal_i2c[0] = setup->i2c_address;
843 codec->hw_write = (hw_write_t)i2c_master_send;
844 ret = i2c_add_driver(&wm8731_i2c_driver);
845 if (ret != 0)
846 printk(KERN_ERR "can't add i2c driver");
847 }
848 #else
849 /* Add other interfaces here */
850 #endif
851 return ret;
852 }
853
854 /* power down chip */
855 static int wm8731_remove(struct platform_device *pdev)
856 {
857 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
858 struct snd_soc_codec *codec = socdev->codec;
859
860 if (codec->control_data)
861 wm8731_dapm_event(codec, SNDRV_CTL_POWER_D3cold);
862
863 snd_soc_free_pcms(socdev);
864 snd_soc_dapm_free(socdev);
865 #if defined (CONFIG_I2C) || defined (CONFIG_I2C_MODULE)
866 i2c_del_driver(&wm8731_i2c_driver);
867 #endif
868 kfree(codec);
869
870 return 0;
871 }
872
873 struct snd_soc_codec_device soc_codec_dev_wm8731 = {
874 .probe = wm8731_probe,
875 .remove = wm8731_remove,
876 .suspend = wm8731_suspend,
877 .resume = wm8731_resume,
878 };
879
880 EXPORT_SYMBOL_GPL(soc_codec_dev_wm8731);
881
882 MODULE_DESCRIPTION("ASoC WM8731 driver");
883 MODULE_AUTHOR("Richard Purdie");
884 MODULE_LICENSE("GPL");
This page took 0.061088 seconds and 5 git commands to generate.