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