[ALSA] ASoC codec error reporting
[powerpc.git] / sound / soc / codecs / wm8750.c
1 /*
2  * wm8750.c -- WM8750 ALSA SoC audio driver
3  *
4  * Copyright 2005 Openedhand Ltd.
5  *
6  * Author: Richard Purdie <richard@openedhand.com>
7  *
8  * Based on WM8753.c
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 "wm8750.h"
31
32 #define AUDIO_NAME "WM8750"
33 #define WM8750_VERSION "0.11"
34
35 /*
36  * Debug
37  */
38
39 #define WM8750_DEBUG 0
40
41 #ifdef WM8750_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 /*
55  * wm8750 register cache
56  * We can't read the WM8750 register space when we
57  * are using 2 wire for device control, so we cache them instead.
58  */
59 static const u16 wm8750_reg[] = {
60         0x0097, 0x0097, 0x0079, 0x0079,  /*  0 */
61         0x0000, 0x0008, 0x0000, 0x000a,  /*  4 */
62         0x0000, 0x0000, 0x00ff, 0x00ff,  /*  8 */
63         0x000f, 0x000f, 0x0000, 0x0000,  /* 12 */
64         0x0000, 0x007b, 0x0000, 0x0032,  /* 16 */
65         0x0000, 0x00c3, 0x00c3, 0x00c0,  /* 20 */
66         0x0000, 0x0000, 0x0000, 0x0000,  /* 24 */
67         0x0000, 0x0000, 0x0000, 0x0000,  /* 28 */
68         0x0000, 0x0000, 0x0050, 0x0050,  /* 32 */
69         0x0050, 0x0050, 0x0050, 0x0050,  /* 36 */
70         0x0079, 0x0079, 0x0079,          /* 40 */
71 };
72
73 #define WM8750_HIFI_DAIFMT \
74         (SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_LEFT_J | SND_SOC_DAIFMT_RIGHT_J | \
75         SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_NB_IF | SND_SOC_DAIFMT_IB_NF | \
76         SND_SOC_DAIFMT_IB_IF)
77
78 #define WM8750_DIR \
79         (SND_SOC_DAIDIR_PLAYBACK | SND_SOC_DAIDIR_CAPTURE)
80
81 #define WM8750_HIFI_FSB \
82         (SND_SOC_FSBD(1) | SND_SOC_FSBD(2) | SND_SOC_FSBD(4) | \
83         SND_SOC_FSBD(8) | SND_SOC_FSBD(16))
84
85 #define WM8750_HIFI_RATES \
86         (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 | SNDRV_PCM_RATE_16000 | \
87         SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
88         SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000)
89
90 #define WM8750_HIFI_BITS \
91         (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
92         SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
93
94 static struct snd_soc_dai_mode wm8750_modes[] = {
95         /* common codec frame and clock master modes */
96         /* 8k */
97         {
98                 .fmt = WM8750_HIFI_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
99                 .pcmfmt = WM8750_HIFI_BITS,
100                 .pcmrate = SNDRV_PCM_RATE_8000,
101                 .pcmdir = WM8750_DIR,
102                 .flags = SND_SOC_DAI_BFS_DIV,
103                 .fs = 1536,
104                 .bfs = WM8750_HIFI_FSB,
105         },
106         {
107                 .fmt = WM8750_HIFI_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
108                 .pcmfmt = WM8750_HIFI_BITS,
109                 .pcmrate = SNDRV_PCM_RATE_8000,
110                 .pcmdir = WM8750_DIR,
111                 .flags = SND_SOC_DAI_BFS_DIV,
112                 .fs = 1408,
113                 .bfs = WM8750_HIFI_FSB,
114         },
115         {
116                 .fmt = WM8750_HIFI_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
117                 .pcmfmt = WM8750_HIFI_BITS,
118                 .pcmrate = SNDRV_PCM_RATE_8000,
119                 .pcmdir = WM8750_DIR,
120                 .flags = SND_SOC_DAI_BFS_DIV,
121                 .fs = 2304,
122                 .bfs = WM8750_HIFI_FSB,
123         },
124         {
125                 .fmt = WM8750_HIFI_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
126                 .pcmfmt = WM8750_HIFI_BITS,
127                 .pcmrate = SNDRV_PCM_RATE_8000,
128                 .pcmdir = WM8750_DIR,
129                 .flags = SND_SOC_DAI_BFS_DIV,
130                 .fs = 2112,
131                 .bfs = WM8750_HIFI_FSB,
132         },
133         {
134                 .fmt = WM8750_HIFI_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
135                 .pcmfmt = WM8750_HIFI_BITS,
136                 .pcmrate = SNDRV_PCM_RATE_8000,
137                 .pcmdir = WM8750_DIR,
138                 .flags = SND_SOC_DAI_BFS_DIV,
139                 .fs = 1500,
140                 .bfs = WM8750_HIFI_FSB,
141         },
142
143         /* 11.025k */
144         {
145                 .fmt = WM8750_HIFI_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
146                 .pcmfmt = WM8750_HIFI_BITS,
147                 .pcmrate = SNDRV_PCM_RATE_11025,
148                 .pcmdir = WM8750_DIR,
149                 .flags = SND_SOC_DAI_BFS_DIV,
150                 .fs = 1024,
151                 .bfs = WM8750_HIFI_FSB,
152         },
153         {
154                 .fmt = WM8750_HIFI_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
155                 .pcmfmt = WM8750_HIFI_BITS,
156                 .pcmrate = SNDRV_PCM_RATE_11025,
157                 .pcmdir = WM8750_DIR,
158                 .flags = SND_SOC_DAI_BFS_DIV,
159                 .fs = 1536,
160                 .bfs = WM8750_HIFI_FSB,
161         },
162         {
163                 .fmt = WM8750_HIFI_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
164                 .pcmfmt = WM8750_HIFI_BITS,
165                 .pcmrate = SNDRV_PCM_RATE_11025,
166                 .pcmdir = WM8750_DIR,
167                 .flags = SND_SOC_DAI_BFS_DIV,
168                 .fs = 1088,
169                 .bfs = WM8750_HIFI_FSB,
170         },
171
172         /* 16k */
173         {
174                 .fmt = WM8750_HIFI_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
175                 .pcmfmt = WM8750_HIFI_BITS,
176                 .pcmrate = SNDRV_PCM_RATE_16000,
177                 .pcmdir = WM8750_DIR,
178                 .flags = SND_SOC_DAI_BFS_DIV,
179                 .fs = 768,
180                 .bfs = WM8750_HIFI_FSB,
181         },
182         {
183                 .fmt = WM8750_HIFI_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
184                 .pcmfmt = WM8750_HIFI_BITS,
185                 .pcmrate = SNDRV_PCM_RATE_16000,
186                 .pcmdir = WM8750_DIR,
187                 .flags = SND_SOC_DAI_BFS_DIV,
188                 .fs = 1152,
189                 .bfs = WM8750_HIFI_FSB
190         },
191         {
192                 .fmt = WM8750_HIFI_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
193                 .pcmfmt = WM8750_HIFI_BITS,
194                 .pcmrate = SNDRV_PCM_RATE_16000,
195                 .pcmdir = WM8750_DIR,
196                 .flags = SND_SOC_DAI_BFS_DIV,
197                 .fs = 750,
198                 .bfs = WM8750_HIFI_FSB,
199         },
200
201         /* 22.05k */
202         {
203                 .fmt = WM8750_HIFI_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
204                 .pcmfmt = WM8750_HIFI_BITS,
205                 .pcmrate = SNDRV_PCM_RATE_22050,
206                 .pcmdir = WM8750_DIR,
207                 .flags = SND_SOC_DAI_BFS_DIV,
208                 .fs = 512,
209                 .bfs = WM8750_HIFI_FSB,
210         },
211         {
212                 .fmt = WM8750_HIFI_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
213                 .pcmfmt = WM8750_HIFI_BITS,
214                 .pcmrate = SNDRV_PCM_RATE_22050,
215                 .pcmdir = WM8750_DIR,
216                 .flags = SND_SOC_DAI_BFS_DIV,
217                 .fs = 768,
218                 .bfs = WM8750_HIFI_FSB,
219         },
220         {
221                 .fmt = WM8750_HIFI_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
222                 .pcmfmt = WM8750_HIFI_BITS,
223                 .pcmrate = SNDRV_PCM_RATE_22050,
224                 .pcmdir = WM8750_DIR,
225                 .flags = SND_SOC_DAI_BFS_DIV,
226                 .fs = 544,
227                 .bfs = WM8750_HIFI_FSB,
228         },
229
230         /* 32k */
231         {
232                 .fmt = WM8750_HIFI_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
233                 .pcmfmt = WM8750_HIFI_BITS,
234                 .pcmrate = SNDRV_PCM_RATE_32000,
235                 .pcmdir = WM8750_DIR,
236                 .flags = SND_SOC_DAI_BFS_DIV,
237                 .fs = 384,
238                 .bfs = WM8750_HIFI_FSB,
239         },
240         {
241                 .fmt = WM8750_HIFI_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
242                 .pcmfmt = WM8750_HIFI_BITS,
243                 .pcmrate = SNDRV_PCM_RATE_32000,
244                 .pcmdir = WM8750_DIR,
245                 .flags = SND_SOC_DAI_BFS_DIV,
246                 .fs = 576,
247                 .bfs = WM8750_HIFI_FSB,
248         },
249         {
250                 .fmt = WM8750_HIFI_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
251                 .pcmfmt = WM8750_HIFI_BITS,
252                 .pcmrate = SNDRV_PCM_RATE_32000,
253                 .pcmdir = WM8750_DIR,
254                 .flags = SND_SOC_DAI_BFS_DIV,
255                 .fs = 375,
256                 .bfs = WM8750_HIFI_FSB,
257         },
258
259         /* 44.1k & 48k */
260         {
261                 .fmt = WM8750_HIFI_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
262                 .pcmfmt = WM8750_HIFI_BITS,
263                 .pcmrate = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
264                 .pcmdir = WM8750_DIR,
265                 .flags = SND_SOC_DAI_BFS_DIV,
266                 .fs = 256,
267                 .bfs = WM8750_HIFI_FSB,
268         },
269         {
270                 .fmt = WM8750_HIFI_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
271                 .pcmfmt = WM8750_HIFI_BITS,
272                 .pcmrate = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
273                 .pcmdir = WM8750_DIR,
274                 .flags = SND_SOC_DAI_BFS_DIV,
275                 .fs = 384,
276                 .bfs = WM8750_HIFI_FSB,
277         },
278         {
279                 .fmt = WM8750_HIFI_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
280                 .pcmfmt = WM8750_HIFI_BITS,
281                 .pcmrate = SNDRV_PCM_RATE_44100,
282                 .pcmdir = WM8750_DIR,
283                 .flags = SND_SOC_DAI_BFS_DIV,
284                 .fs = 272,
285                 .bfs = WM8750_HIFI_FSB,
286         },
287         {
288                 .fmt = WM8750_HIFI_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
289                 .pcmfmt = WM8750_HIFI_BITS,
290                 .pcmrate = SNDRV_PCM_RATE_48000,
291                 .pcmdir = WM8750_DIR,
292                 .flags = SND_SOC_DAI_BFS_DIV,
293                 .fs = 250,
294                 .bfs = WM8750_HIFI_FSB,
295         },
296
297         /* 88.2k & 96k */
298         {
299                 .fmt = WM8750_HIFI_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
300                 .pcmfmt = WM8750_HIFI_BITS,
301                 .pcmrate = SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000,
302                 .pcmdir = WM8750_DIR,
303                 .flags = SND_SOC_DAI_BFS_DIV,
304                 .fs = 128,
305                 .bfs = WM8750_HIFI_FSB,
306         },
307         {
308                 .fmt = WM8750_HIFI_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
309                 .pcmfmt = WM8750_HIFI_BITS,
310                 .pcmrate = SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000,
311                 .pcmdir = WM8750_DIR,
312                 .flags = SND_SOC_DAI_BFS_DIV,
313                 .fs = 192,
314                 .bfs = WM8750_HIFI_FSB,
315         },
316         {
317                 .fmt = WM8750_HIFI_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
318                 .pcmfmt = WM8750_HIFI_BITS,
319                 .pcmrate = SNDRV_PCM_RATE_88200,
320                 .pcmdir = WM8750_DIR,
321                 .flags = SND_SOC_DAI_BFS_DIV,
322                 .fs = 136,
323                 .bfs = WM8750_HIFI_FSB,
324         },
325         {
326                 .fmt = WM8750_HIFI_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
327                 .pcmfmt = WM8750_HIFI_BITS,
328                 .pcmrate = SNDRV_PCM_RATE_96000,
329                 .pcmdir = WM8750_DIR,
330                 .flags = SND_SOC_DAI_BFS_DIV,
331                 .fs = 125,
332                 .bfs = WM8750_HIFI_FSB,
333         },
334
335         /* codec frame and clock slave modes */
336         {
337                 .fmt = WM8750_HIFI_DAIFMT | SND_SOC_DAIFMT_CBS_CFS,
338                 .pcmfmt = WM8750_HIFI_BITS,
339                 .pcmrate = WM8750_HIFI_RATES,
340                 .pcmdir = WM8750_DIR,
341                 .flags = SND_SOC_DAI_BFS_DIV,
342                 .fs = SND_SOC_FS_ALL,
343                 .bfs = SND_SOC_FSB_ALL,
344         },
345 };
346
347 /*
348  * read wm8750 register cache
349  */
350 static inline unsigned int wm8750_read_reg_cache(struct snd_soc_codec *codec,
351         unsigned int reg)
352 {
353         u16 *cache = codec->reg_cache;
354         if (reg > WM8750_CACHE_REGNUM)
355                 return -1;
356         return cache[reg];
357 }
358
359 /*
360  * write wm8750 register cache
361  */
362 static inline void wm8750_write_reg_cache(struct snd_soc_codec *codec,
363         unsigned int reg, unsigned int value)
364 {
365         u16 *cache = codec->reg_cache;
366         if (reg > WM8750_CACHE_REGNUM)
367                 return;
368         cache[reg] = value;
369 }
370
371 static int wm8750_write(struct snd_soc_codec *codec, unsigned int reg,
372         unsigned int value)
373 {
374         u8 data[2];
375
376         /* data is
377          *   D15..D9 WM8753 register offset
378          *   D8...D0 register data
379          */
380         data[0] = (reg << 1) | ((value >> 8) & 0x0001);
381         data[1] = value & 0x00ff;
382
383         wm8750_write_reg_cache (codec, reg, value);
384         if (codec->hw_write(codec->control_data, data, 2) == 2)
385                 return 0;
386         else
387                 return -EIO;
388 }
389
390 #define wm8750_reset(c) wm8750_write(c, WM8750_RESET, 0)
391
392 /*
393  * WM8750 Controls
394  */
395 static const char *wm8750_bass[] = {"Linear Control", "Adaptive Boost"};
396 static const char *wm8750_bass_filter[] = { "130Hz @ 48kHz", "200Hz @ 48kHz" };
397 static const char *wm8750_treble[] = {"8kHz", "4kHz"};
398 static const char *wm8750_3d_lc[] = {"200Hz", "500Hz"};
399 static const char *wm8750_3d_uc[] = {"2.2kHz", "1.5kHz"};
400 static const char *wm8750_3d_func[] = {"Capture", "Playback"};
401 static const char *wm8750_alc_func[] = {"Off", "Right", "Left", "Stereo"};
402 static const char *wm8750_ng_type[] = {"Constant PGA Gain",
403         "Mute ADC Output"};
404 static const char *wm8750_line_mux[] = {"Line 1", "Line 2", "Line 3", "PGA",
405         "Differential"};
406 static const char *wm8750_pga_sel[] = {"Line 1", "Line 2", "Line 3",
407         "Differential"};
408 static const char *wm8750_out3[] = {"VREF", "ROUT1 + Vol", "MonoOut",
409         "ROUT1"};
410 static const char *wm8750_diff_sel[] = {"Line 1", "Line 2"};
411 static const char *wm8750_adcpol[] = {"Normal", "L Invert", "R Invert",
412         "L + R Invert"};
413 static const char *wm8750_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"};
414 static const char *wm8750_mono_mux[] = {"Stereo", "Mono (Left)",
415         "Mono (Right)", "Digital Mono"};
416
417 static const struct soc_enum wm8750_enum[] = {
418 SOC_ENUM_SINGLE(WM8750_BASS, 7, 2, wm8750_bass),
419 SOC_ENUM_SINGLE(WM8750_BASS, 6, 2, wm8750_bass_filter),
420 SOC_ENUM_SINGLE(WM8750_TREBLE, 6, 2, wm8750_treble),
421 SOC_ENUM_SINGLE(WM8750_3D, 5, 2, wm8750_3d_lc),
422 SOC_ENUM_SINGLE(WM8750_3D, 6, 2, wm8750_3d_uc),
423 SOC_ENUM_SINGLE(WM8750_3D, 7, 2, wm8750_3d_func),
424 SOC_ENUM_SINGLE(WM8750_ALC1, 7, 4, wm8750_alc_func),
425 SOC_ENUM_SINGLE(WM8750_NGATE, 1, 2, wm8750_ng_type),
426 SOC_ENUM_SINGLE(WM8750_LOUTM1, 0, 5, wm8750_line_mux),
427 SOC_ENUM_SINGLE(WM8750_ROUTM1, 0, 5, wm8750_line_mux),
428 SOC_ENUM_SINGLE(WM8750_LADCIN, 6, 4, wm8750_pga_sel), /* 10 */
429 SOC_ENUM_SINGLE(WM8750_RADCIN, 6, 4, wm8750_pga_sel),
430 SOC_ENUM_SINGLE(WM8750_ADCTL2, 7, 4, wm8750_out3),
431 SOC_ENUM_SINGLE(WM8750_ADCIN, 8, 2, wm8750_diff_sel),
432 SOC_ENUM_SINGLE(WM8750_ADCDAC, 5, 4, wm8750_adcpol),
433 SOC_ENUM_SINGLE(WM8750_ADCDAC, 1, 4, wm8750_deemph),
434 SOC_ENUM_SINGLE(WM8750_ADCIN, 6, 4, wm8750_mono_mux), /* 16 */
435
436 };
437
438 static const struct snd_kcontrol_new wm8750_snd_controls[] = {
439
440 SOC_DOUBLE_R("Capture Volume", WM8750_LINVOL, WM8750_RINVOL, 0, 63, 0),
441 SOC_DOUBLE_R("Capture ZC Switch", WM8750_LINVOL, WM8750_RINVOL, 6, 1, 0),
442 SOC_DOUBLE_R("Capture Switch", WM8750_LINVOL, WM8750_RINVOL, 7, 1, 1),
443
444 SOC_DOUBLE_R("Headphone Playback ZC Switch", WM8750_LOUT1V,
445         WM8750_ROUT1V, 7, 1, 0),
446 SOC_DOUBLE_R("Speaker Playback ZC Switch", WM8750_LOUT2V,
447         WM8750_ROUT2V, 7, 1, 0),
448
449 SOC_ENUM("Playback De-emphasis", wm8750_enum[15]),
450
451 SOC_ENUM("Capture Polarity", wm8750_enum[14]),
452 SOC_SINGLE("Playback 6dB Attenuate", WM8750_ADCDAC, 7, 1, 0),
453 SOC_SINGLE("Capture 6dB Attenuate", WM8750_ADCDAC, 8, 1, 0),
454
455 SOC_DOUBLE_R("PCM Volume", WM8750_LDAC, WM8750_RDAC, 0, 255, 0),
456
457 SOC_ENUM("Bass Boost", wm8750_enum[0]),
458 SOC_ENUM("Bass Filter", wm8750_enum[1]),
459 SOC_SINGLE("Bass Volume", WM8750_BASS, 0, 15, 1),
460
461 SOC_SINGLE("Treble Volume", WM8750_TREBLE, 0, 15, 0),
462 SOC_ENUM("Treble Cut-off", wm8750_enum[2]),
463
464 SOC_SINGLE("3D Switch", WM8750_3D, 0, 1, 0),
465 SOC_SINGLE("3D Volume", WM8750_3D, 1, 15, 0),
466 SOC_ENUM("3D Lower Cut-off", wm8750_enum[3]),
467 SOC_ENUM("3D Upper Cut-off", wm8750_enum[4]),
468 SOC_ENUM("3D Mode", wm8750_enum[5]),
469
470 SOC_SINGLE("ALC Capture Target Volume", WM8750_ALC1, 0, 7, 0),
471 SOC_SINGLE("ALC Capture Max Volume", WM8750_ALC1, 4, 7, 0),
472 SOC_ENUM("ALC Capture Function", wm8750_enum[6]),
473 SOC_SINGLE("ALC Capture ZC Switch", WM8750_ALC2, 7, 1, 0),
474 SOC_SINGLE("ALC Capture Hold Time", WM8750_ALC2, 0, 15, 0),
475 SOC_SINGLE("ALC Capture Decay Time", WM8750_ALC3, 4, 15, 0),
476 SOC_SINGLE("ALC Capture Attack Time", WM8750_ALC3, 0, 15, 0),
477 SOC_SINGLE("ALC Capture NG Threshold", WM8750_NGATE, 3, 31, 0),
478 SOC_ENUM("ALC Capture NG Type", wm8750_enum[4]),
479 SOC_SINGLE("ALC Capture NG Switch", WM8750_NGATE, 0, 1, 0),
480
481 SOC_SINGLE("Left ADC Capture Volume", WM8750_LADC, 0, 255, 0),
482 SOC_SINGLE("Right ADC Capture Volume", WM8750_RADC, 0, 255, 0),
483
484 SOC_SINGLE("ZC Timeout Switch", WM8750_ADCTL1, 0, 1, 0),
485 SOC_SINGLE("Playback Invert Switch", WM8750_ADCTL1, 1, 1, 0),
486
487 SOC_SINGLE("Right Speaker Playback Invert Switch", WM8750_ADCTL2, 4, 1, 0),
488
489 /* Unimplemented */
490 /* ADCDAC Bit 0 - ADCHPD */
491 /* ADCDAC Bit 4 - HPOR */
492 /* ADCTL1 Bit 2,3 - DATSEL */
493 /* ADCTL1 Bit 4,5 - DMONOMIX */
494 /* ADCTL1 Bit 6,7 - VSEL */
495 /* ADCTL2 Bit 2 - LRCM */
496 /* ADCTL2 Bit 3 - TRI */
497 /* ADCTL3 Bit 5 - HPFLREN */
498 /* ADCTL3 Bit 6 - VROI */
499 /* ADCTL3 Bit 7,8 - ADCLRM */
500 /* ADCIN Bit 4 - LDCM */
501 /* ADCIN Bit 5 - RDCM */
502
503 SOC_DOUBLE_R("Mic Boost", WM8750_LADCIN, WM8750_RADCIN, 4, 3, 0),
504
505 SOC_DOUBLE_R("Bypass Left Playback Volume", WM8750_LOUTM1,
506         WM8750_LOUTM2, 4, 7, 1),
507 SOC_DOUBLE_R("Bypass Right Playback Volume", WM8750_ROUTM1,
508         WM8750_ROUTM2, 4, 7, 1),
509 SOC_DOUBLE_R("Bypass Mono Playback Volume", WM8750_MOUTM1,
510         WM8750_MOUTM2, 4, 7, 1),
511
512 SOC_SINGLE("Mono Playback ZC Switch", WM8750_MOUTV, 7, 1, 0),
513
514 SOC_DOUBLE_R("Headphone Playback Volume", WM8750_LOUT1V, WM8750_ROUT1V,
515         0, 127, 0),
516 SOC_DOUBLE_R("Speaker Playback Volume", WM8750_LOUT2V, WM8750_ROUT2V,
517         0, 127, 0),
518
519 SOC_SINGLE("Mono Playback Volume", WM8750_MOUTV, 0, 127, 0),
520
521 };
522
523 /* add non dapm controls */
524 static int wm8750_add_controls(struct snd_soc_codec *codec)
525 {
526         int err, i;
527
528         for (i = 0; i < ARRAY_SIZE(wm8750_snd_controls); i++) {
529                 err = snd_ctl_add(codec->card,
530                                 snd_soc_cnew(&wm8750_snd_controls[i],codec, NULL));
531                 if (err < 0)
532                         return err;
533         }
534         return 0;
535 }
536
537 /*
538  * DAPM Controls
539  */
540
541 /* Left Mixer */
542 static const struct snd_kcontrol_new wm8750_left_mixer_controls[] = {
543 SOC_DAPM_SINGLE("Playback Switch", WM8750_LOUTM1, 8, 1, 0),
544 SOC_DAPM_SINGLE("Left Bypass Switch", WM8750_LOUTM1, 7, 1, 0),
545 SOC_DAPM_SINGLE("Right Playback Switch", WM8750_LOUTM2, 8, 1, 0),
546 SOC_DAPM_SINGLE("Right Bypass Switch", WM8750_LOUTM2, 7, 1, 0),
547 };
548
549 /* Right Mixer */
550 static const struct snd_kcontrol_new wm8750_right_mixer_controls[] = {
551 SOC_DAPM_SINGLE("Left Playback Switch", WM8750_ROUTM1, 8, 1, 0),
552 SOC_DAPM_SINGLE("Left Bypass Switch", WM8750_ROUTM1, 7, 1, 0),
553 SOC_DAPM_SINGLE("Playback Switch", WM8750_ROUTM2, 8, 1, 0),
554 SOC_DAPM_SINGLE("Right Bypass Switch", WM8750_ROUTM2, 7, 1, 0),
555 };
556
557 /* Mono Mixer */
558 static const struct snd_kcontrol_new wm8750_mono_mixer_controls[] = {
559 SOC_DAPM_SINGLE("Left Playback Switch", WM8750_MOUTM1, 8, 1, 0),
560 SOC_DAPM_SINGLE("Left Bypass Switch", WM8750_MOUTM1, 7, 1, 0),
561 SOC_DAPM_SINGLE("Right Playback Switch", WM8750_MOUTM2, 8, 1, 0),
562 SOC_DAPM_SINGLE("Right Bypass Switch", WM8750_MOUTM2, 7, 1, 0),
563 };
564
565 /* Left Line Mux */
566 static const struct snd_kcontrol_new wm8750_left_line_controls =
567 SOC_DAPM_ENUM("Route", wm8750_enum[8]);
568
569 /* Right Line Mux */
570 static const struct snd_kcontrol_new wm8750_right_line_controls =
571 SOC_DAPM_ENUM("Route", wm8750_enum[9]);
572
573 /* Left PGA Mux */
574 static const struct snd_kcontrol_new wm8750_left_pga_controls =
575 SOC_DAPM_ENUM("Route", wm8750_enum[10]);
576
577 /* Right PGA Mux */
578 static const struct snd_kcontrol_new wm8750_right_pga_controls =
579 SOC_DAPM_ENUM("Route", wm8750_enum[11]);
580
581 /* Out 3 Mux */
582 static const struct snd_kcontrol_new wm8750_out3_controls =
583 SOC_DAPM_ENUM("Route", wm8750_enum[12]);
584
585 /* Differential Mux */
586 static const struct snd_kcontrol_new wm8750_diffmux_controls =
587 SOC_DAPM_ENUM("Route", wm8750_enum[13]);
588
589 /* Mono ADC Mux */
590 static const struct snd_kcontrol_new wm8750_monomux_controls =
591 SOC_DAPM_ENUM("Route", wm8750_enum[16]);
592
593 static const struct snd_soc_dapm_widget wm8750_dapm_widgets[] = {
594         SND_SOC_DAPM_MIXER("Left Mixer", SND_SOC_NOPM, 0, 0,
595                 &wm8750_left_mixer_controls[0],
596                 ARRAY_SIZE(wm8750_left_mixer_controls)),
597         SND_SOC_DAPM_MIXER("Right Mixer", SND_SOC_NOPM, 0, 0,
598                 &wm8750_right_mixer_controls[0],
599                 ARRAY_SIZE(wm8750_right_mixer_controls)),
600         SND_SOC_DAPM_MIXER("Mono Mixer", WM8750_PWR2, 2, 0,
601                 &wm8750_mono_mixer_controls[0],
602                 ARRAY_SIZE(wm8750_mono_mixer_controls)),
603
604         SND_SOC_DAPM_PGA("Right Out 2", WM8750_PWR2, 3, 0, NULL, 0),
605         SND_SOC_DAPM_PGA("Left Out 2", WM8750_PWR2, 4, 0, NULL, 0),
606         SND_SOC_DAPM_PGA("Right Out 1", WM8750_PWR2, 5, 0, NULL, 0),
607         SND_SOC_DAPM_PGA("Left Out 1", WM8750_PWR2, 6, 0, NULL, 0),
608         SND_SOC_DAPM_DAC("Right DAC", "Right Playback", WM8750_PWR2, 7, 0),
609         SND_SOC_DAPM_DAC("Left DAC", "Left Playback", WM8750_PWR2, 8, 0),
610
611         SND_SOC_DAPM_MICBIAS("Mic Bias", WM8750_PWR1, 1, 0),
612         SND_SOC_DAPM_ADC("Right ADC", "Right Capture", WM8750_PWR1, 2, 0),
613         SND_SOC_DAPM_ADC("Left ADC", "Left Capture", WM8750_PWR1, 3, 0),
614
615         SND_SOC_DAPM_MUX("Left PGA Mux", WM8750_PWR1, 5, 0,
616                 &wm8750_left_pga_controls),
617         SND_SOC_DAPM_MUX("Right PGA Mux", WM8750_PWR1, 4, 0,
618                 &wm8750_right_pga_controls),
619         SND_SOC_DAPM_MUX("Left Line Mux", SND_SOC_NOPM, 0, 0,
620                 &wm8750_left_line_controls),
621         SND_SOC_DAPM_MUX("Right Line Mux", SND_SOC_NOPM, 0, 0,
622                 &wm8750_right_line_controls),
623
624         SND_SOC_DAPM_MUX("Out3 Mux", SND_SOC_NOPM, 0, 0, &wm8750_out3_controls),
625         SND_SOC_DAPM_PGA("Out 3", WM8750_PWR2, 1, 0, NULL, 0),
626         SND_SOC_DAPM_PGA("Mono Out 1", WM8750_PWR2, 2, 0, NULL, 0),
627
628         SND_SOC_DAPM_MUX("Differential Mux", SND_SOC_NOPM, 0, 0,
629                 &wm8750_diffmux_controls),
630         SND_SOC_DAPM_MUX("Left ADC Mux", SND_SOC_NOPM, 0, 0,
631                 &wm8750_monomux_controls),
632         SND_SOC_DAPM_MUX("Right ADC Mux", SND_SOC_NOPM, 0, 0,
633                 &wm8750_monomux_controls),
634
635         SND_SOC_DAPM_OUTPUT("LOUT1"),
636         SND_SOC_DAPM_OUTPUT("ROUT1"),
637         SND_SOC_DAPM_OUTPUT("LOUT2"),
638         SND_SOC_DAPM_OUTPUT("ROUT2"),
639         SND_SOC_DAPM_OUTPUT("MONO"),
640         SND_SOC_DAPM_OUTPUT("OUT3"),
641
642         SND_SOC_DAPM_INPUT("LINPUT1"),
643         SND_SOC_DAPM_INPUT("LINPUT2"),
644         SND_SOC_DAPM_INPUT("LINPUT3"),
645         SND_SOC_DAPM_INPUT("RINPUT1"),
646         SND_SOC_DAPM_INPUT("RINPUT2"),
647         SND_SOC_DAPM_INPUT("RINPUT3"),
648 };
649
650 static const char *audio_map[][3] = {
651         /* left mixer */
652         {"Left Mixer", "Playback Switch", "Left DAC"},
653         {"Left Mixer", "Left Bypass Switch", "Left Line Mux"},
654         {"Left Mixer", "Right Playback Switch", "Right DAC"},
655         {"Left Mixer", "Right Bypass Switch", "Right Line Mux"},
656
657         /* right mixer */
658         {"Right Mixer", "Left Playback Switch", "Left DAC"},
659         {"Right Mixer", "Left Bypass Switch", "Left Line Mux"},
660         {"Right Mixer", "Playback Switch", "Right DAC"},
661         {"Right Mixer", "Right Bypass Switch", "Right Line Mux"},
662
663         /* left out 1 */
664         {"Left Out 1", NULL, "Left Mixer"},
665         {"LOUT1", NULL, "Left Out 1"},
666
667         /* left out 2 */
668         {"Left Out 2", NULL, "Left Mixer"},
669         {"LOUT2", NULL, "Left Out 2"},
670
671         /* right out 1 */
672         {"Right Out 1", NULL, "Right Mixer"},
673         {"ROUT1", NULL, "Right Out 1"},
674
675         /* right out 2 */
676         {"Right Out 2", NULL, "Right Mixer"},
677         {"ROUT2", NULL, "Right Out 2"},
678
679         /* mono mixer */
680         {"Mono Mixer", "Left Playback Switch", "Left DAC"},
681         {"Mono Mixer", "Left Bypass Switch", "Left Line Mux"},
682         {"Mono Mixer", "Right Playback Switch", "Right DAC"},
683         {"Mono Mixer", "Right Bypass Switch", "Right Line Mux"},
684
685         /* mono out */
686         {"Mono Out 1", NULL, "Mono Mixer"},
687         {"MONO1", NULL, "Mono Out 1"},
688
689         /* out 3 */
690         {"Out3 Mux", "VREF", "VREF"},
691         {"Out3 Mux", "ROUT1 + Vol", "ROUT1"},
692         {"Out3 Mux", "ROUT1", "Right Mixer"},
693         {"Out3 Mux", "MonoOut", "MONO1"},
694         {"Out 3", NULL, "Out3 Mux"},
695         {"OUT3", NULL, "Out 3"},
696
697         /* Left Line Mux */
698         {"Left Line Mux", "Line 1", "LINPUT1"},
699         {"Left Line Mux", "Line 2", "LINPUT2"},
700         {"Left Line Mux", "Line 3", "LINPUT3"},
701         {"Left Line Mux", "PGA", "Left PGA Mux"},
702         {"Left Line Mux", "Differential", "Differential Mux"},
703
704         /* Right Line Mux */
705         {"Right Line Mux", "Line 1", "RINPUT1"},
706         {"Right Line Mux", "Line 2", "RINPUT2"},
707         {"Right Line Mux", "Line 3", "RINPUT3"},
708         {"Right Line Mux", "PGA", "Right PGA Mux"},
709         {"Right Line Mux", "Differential", "Differential Mux"},
710
711         /* Left PGA Mux */
712         {"Left PGA Mux", "Line 1", "LINPUT1"},
713         {"Left PGA Mux", "Line 2", "LINPUT2"},
714         {"Left PGA Mux", "Line 3", "LINPUT3"},
715         {"Left PGA Mux", "Differential", "Differential Mux"},
716
717         /* Right PGA Mux */
718         {"Right PGA Mux", "Line 1", "RINPUT1"},
719         {"Right PGA Mux", "Line 2", "RINPUT2"},
720         {"Right PGA Mux", "Line 3", "RINPUT3"},
721         {"Right PGA Mux", "Differential", "Differential Mux"},
722
723         /* Differential Mux */
724         {"Differential Mux", "Line 1", "LINPUT1"},
725         {"Differential Mux", "Line 1", "RINPUT1"},
726         {"Differential Mux", "Line 2", "LINPUT2"},
727         {"Differential Mux", "Line 2", "RINPUT2"},
728
729         /* Left ADC Mux */
730         {"Left ADC Mux", "Stereo", "Left PGA Mux"},
731         {"Left ADC Mux", "Mono (Left)", "Left PGA Mux"},
732         {"Left ADC Mux", "Digital Mono", "Left PGA Mux"},
733
734         /* Right ADC Mux */
735         {"Right ADC Mux", "Stereo", "Right PGA Mux"},
736         {"Right ADC Mux", "Mono (Right)", "Right PGA Mux"},
737         {"Right ADC Mux", "Digital Mono", "Right PGA Mux"},
738
739         /* ADC */
740         {"Left ADC", NULL, "Left ADC Mux"},
741         {"Right ADC", NULL, "Right ADC Mux"},
742
743         /* terminator */
744         {NULL, NULL, NULL},
745 };
746
747 static int wm8750_add_widgets(struct snd_soc_codec *codec)
748 {
749         int i;
750
751         for(i = 0; i < ARRAY_SIZE(wm8750_dapm_widgets); i++) {
752                 snd_soc_dapm_new_control(codec, &wm8750_dapm_widgets[i]);
753         }
754
755         /* set up audio path audio_mapnects */
756         for(i = 0; audio_map[i][0] != NULL; i++) {
757                 snd_soc_dapm_connect_input(codec, audio_map[i][0],
758                         audio_map[i][1], audio_map[i][2]);
759         }
760
761         snd_soc_dapm_new_widgets(codec);
762         return 0;
763 }
764
765 struct _coeff_div {
766         u32 mclk;
767         u32 rate;
768         u16 fs;
769         u8 sr:5;
770         u8 usb:1;
771 };
772
773 /* codec hifi mclk clock divider coefficients */
774 static const struct _coeff_div coeff_div[] = {
775         /* 8k */
776         {12288000, 8000, 1536, 0x6, 0x0},
777         {11289600, 8000, 1408, 0x16, 0x0},
778         {18432000, 8000, 2304, 0x7, 0x0},
779         {16934400, 8000, 2112, 0x17, 0x0},
780         {12000000, 8000, 1500, 0x6, 0x1},
781
782         /* 11.025k */
783         {11289600, 11025, 1024, 0x18, 0x0},
784         {16934400, 11025, 1536, 0x19, 0x0},
785         {12000000, 11025, 1088, 0x19, 0x1},
786
787         /* 16k */
788         {12288000, 16000, 768, 0xa, 0x0},
789         {18432000, 16000, 1152, 0xb, 0x0},
790         {12000000, 16000, 750, 0xa, 0x1},
791
792         /* 22.05k */
793         {11289600, 22050, 512, 0x1a, 0x0},
794         {16934400, 22050, 768, 0x1b, 0x0},
795         {12000000, 22050, 544, 0x1b, 0x1},
796
797         /* 32k */
798         {12288000, 32000, 384, 0xc, 0x0},
799         {18432000, 32000, 576, 0xd, 0x0},
800         {12000000, 32000, 375, 0xa, 0x1},
801
802         /* 44.1k */
803         {11289600, 44100, 256, 0x10, 0x0},
804         {16934400, 44100, 384, 0x11, 0x0},
805         {12000000, 44100, 272, 0x11, 0x1},
806
807         /* 48k */
808         {12288000, 48000, 256, 0x0, 0x0},
809         {18432000, 48000, 384, 0x1, 0x0},
810         {12000000, 48000, 250, 0x0, 0x1},
811
812         /* 88.2k */
813         {11289600, 88200, 128, 0x1e, 0x0},
814         {16934400, 88200, 192, 0x1f, 0x0},
815         {12000000, 88200, 136, 0x1f, 0x1},
816
817         /* 96k */
818         {12288000, 96000, 128, 0xe, 0x0},
819         {18432000, 96000, 192, 0xf, 0x0},
820         {12000000, 96000, 125, 0xe, 0x1},
821 };
822
823 static inline int get_coeff(int mclk, int rate)
824 {
825         int i;
826
827         for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
828                 if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
829                         return i;
830         }
831
832         printk(KERN_ERR "wm8750: could not get coeff for mclk %d @ rate %d\n",
833                 mclk, rate);
834         return -EINVAL;
835 }
836
837 /* WM8750 supports numerous input clocks per sample rate */
838 static unsigned int wm8750_config_sysclk(struct snd_soc_codec_dai *dai,
839         struct snd_soc_clock_info *info, unsigned int clk)
840 {
841         dai->mclk = clk;
842         return dai->mclk;
843 }
844
845 static int wm8750_pcm_prepare(struct snd_pcm_substream *substream)
846 {
847         struct snd_soc_pcm_runtime *rtd = substream->private_data;
848         struct snd_soc_device *socdev = rtd->socdev;
849         struct snd_soc_codec *codec = socdev->codec;
850         u16 iface = 0, bfs, srate = 0;
851         int i = get_coeff(rtd->codec_dai->mclk,
852                 snd_soc_get_rate(rtd->codec_dai->dai_runtime.pcmrate));
853
854         /* is coefficient valid ? */
855         if (i < 0)
856                 return i;
857
858         bfs = SND_SOC_FSBD_REAL(rtd->codec_dai->dai_runtime.bfs);
859
860         /* set master/slave audio interface */
861         switch (rtd->codec_dai->dai_runtime.fmt & SND_SOC_DAIFMT_CLOCK_MASK) {
862         case SND_SOC_DAIFMT_CBM_CFM:
863                 iface = 0x0040;
864                 break;
865         case SND_SOC_DAIFMT_CBS_CFS:
866                 break;
867         }
868
869         /* interface format */
870         switch (rtd->codec_dai->dai_runtime.fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
871         case SND_SOC_DAIFMT_I2S:
872                 iface |= 0x0002;
873                 break;
874         case SND_SOC_DAIFMT_RIGHT_J:
875                 break;
876         case SND_SOC_DAIFMT_LEFT_J:
877                 iface |= 0x0001;
878                 break;
879         case SND_SOC_DAIFMT_DSP_A:
880                 iface |= 0x0003;
881                 break;
882         case SND_SOC_DAIFMT_DSP_B:
883                 iface |= 0x0013;
884                 break;
885         }
886
887         /* bit size */
888         switch (rtd->codec_dai->dai_runtime.pcmfmt) {
889         case SNDRV_PCM_FMTBIT_S16_LE:
890                 break;
891         case SNDRV_PCM_FMTBIT_S20_3LE:
892                 iface |= 0x0004;
893                 break;
894         case SNDRV_PCM_FMTBIT_S24_LE:
895                 iface |= 0x0008;
896                 break;
897         case SNDRV_PCM_FMTBIT_S32_LE:
898                 iface |= 0x000c;
899                 break;
900         }
901
902         /* clock inversion */
903         switch (rtd->codec_dai->dai_runtime.fmt & SND_SOC_DAIFMT_INV_MASK) {
904         case SND_SOC_DAIFMT_NB_NF:
905                 break;
906         case SND_SOC_DAIFMT_IB_IF:
907                 iface |= 0x0090;
908                 break;
909         case SND_SOC_DAIFMT_IB_NF:
910                 iface |= 0x0080;
911                 break;
912         case SND_SOC_DAIFMT_NB_IF:
913                 iface |= 0x0010;
914                 break;
915         }
916
917         /* set bclk divisor rate */
918         switch (bfs) {
919         case 1:
920                 break;
921         case 4:
922                 srate |= (0x1 << 7);
923                 break;
924         case 8:
925                 srate |= (0x2 << 7);
926                 break;
927         case 16:
928                 srate |= (0x3 << 7);
929                 break;
930         }
931
932         /* set iface & srate */
933         wm8750_write(codec, WM8750_IFACE, iface);
934         wm8750_write(codec, WM8750_SRATE, srate |
935                 (coeff_div[i].sr << 1) | coeff_div[i].usb);
936
937         return 0;
938 }
939
940 static int wm8750_mute(struct snd_soc_codec *codec,
941         struct snd_soc_codec_dai *dai, int mute)
942 {
943         u16 mute_reg = wm8750_read_reg_cache(codec, WM8750_ADCDAC) & 0xfff7;
944         if (mute)
945                 wm8750_write(codec, WM8750_ADCDAC, mute_reg | 0x8);
946         else
947                 wm8750_write(codec, WM8750_ADCDAC, mute_reg);
948         return 0;
949 }
950
951 static int wm8750_dapm_event(struct snd_soc_codec *codec, int event)
952 {
953         u16 pwr_reg = wm8750_read_reg_cache(codec, WM8750_PWR1) & 0xfe3e;
954
955         switch (event) {
956         case SNDRV_CTL_POWER_D0: /* full On */
957                 /* set vmid to 50k and unmute dac */
958                 wm8750_write(codec, WM8750_PWR1, pwr_reg | 0x00c0);
959                 break;
960         case SNDRV_CTL_POWER_D1: /* partial On */
961         case SNDRV_CTL_POWER_D2: /* partial On */
962                 /* set vmid to 5k for quick power up */
963                 wm8750_write(codec, WM8750_PWR1, pwr_reg | 0x01c1);
964                 break;
965         case SNDRV_CTL_POWER_D3hot: /* Off, with power */
966                 /* mute dac and set vmid to 500k, enable VREF */
967                 wm8750_write(codec, WM8750_PWR1, pwr_reg | 0x0141);
968                 break;
969         case SNDRV_CTL_POWER_D3cold: /* Off, without power */
970                 wm8750_write(codec, WM8750_PWR1, 0x0001);
971                 break;
972         }
973         codec->dapm_state = event;
974         return 0;
975 }
976
977 struct snd_soc_codec_dai wm8750_dai = {
978         .name = "WM8750",
979         .playback = {
980                 .stream_name = "Playback",
981                 .channels_min = 1,
982                 .channels_max = 2,
983         },
984         .capture = {
985                 .stream_name = "Capture",
986                 .channels_min = 1,
987                 .channels_max = 2,
988         },
989         .config_sysclk = wm8750_config_sysclk,
990         .digital_mute = wm8750_mute,
991         .ops = {
992                 .prepare = wm8750_pcm_prepare,
993         },
994         .caps = {
995                 .num_modes = ARRAY_SIZE(wm8750_modes),
996                 .mode = wm8750_modes,
997         },
998 };
999 EXPORT_SYMBOL_GPL(wm8750_dai);
1000
1001 static void wm8750_work(struct work_struct *work)
1002 {
1003         struct snd_soc_codec *codec =
1004                 container_of(work, struct snd_soc_codec, delayed_work.work);
1005         wm8750_dapm_event(codec, codec->dapm_state);
1006 }
1007
1008 static int wm8750_suspend(struct platform_device *pdev, pm_message_t state)
1009 {
1010         struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1011         struct snd_soc_codec *codec = socdev->codec;
1012
1013         wm8750_dapm_event(codec, SNDRV_CTL_POWER_D3cold);
1014         return 0;
1015 }
1016
1017 static int wm8750_resume(struct platform_device *pdev)
1018 {
1019         struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1020         struct snd_soc_codec *codec = socdev->codec;
1021         int i;
1022         u8 data[2];
1023         u16 *cache = codec->reg_cache;
1024
1025         /* Sync reg_cache with the hardware */
1026         for (i = 0; i < ARRAY_SIZE(wm8750_reg); i++) {
1027                 if (i == WM8750_RESET)
1028                         continue;
1029                 data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001);
1030                 data[1] = cache[i] & 0x00ff;
1031                 codec->hw_write(codec->control_data, data, 2);
1032         }
1033
1034         wm8750_dapm_event(codec, SNDRV_CTL_POWER_D3hot);
1035
1036         /* charge wm8750 caps */
1037         if (codec->suspend_dapm_state == SNDRV_CTL_POWER_D0) {
1038                 wm8750_dapm_event(codec, SNDRV_CTL_POWER_D2);
1039                 codec->dapm_state = SNDRV_CTL_POWER_D0;
1040                 schedule_delayed_work(&codec->delayed_work,
1041                          msecs_to_jiffies(1000));
1042         }
1043
1044         return 0;
1045 }
1046
1047 /*
1048  * initialise the WM8750 driver
1049  * register the mixer and dsp interfaces with the kernel
1050  */
1051 static int wm8750_init(struct snd_soc_device *socdev)
1052 {
1053         struct snd_soc_codec *codec = socdev->codec;
1054         int reg, ret = 0;
1055
1056         codec->name = "WM8750";
1057         codec->owner = THIS_MODULE;
1058         codec->read = wm8750_read_reg_cache;
1059         codec->write = wm8750_write;
1060         codec->dapm_event = wm8750_dapm_event;
1061         codec->dai = &wm8750_dai;
1062         codec->num_dai = 1;
1063         codec->reg_cache_size = ARRAY_SIZE(wm8750_reg);
1064
1065         codec->reg_cache =
1066                         kzalloc(sizeof(u16) * ARRAY_SIZE(wm8750_reg), GFP_KERNEL);
1067         if (codec->reg_cache == NULL)
1068                 return -ENOMEM;
1069         memcpy(codec->reg_cache, wm8750_reg,
1070                 sizeof(u16) * ARRAY_SIZE(wm8750_reg));
1071         codec->reg_cache_size = sizeof(u16) * ARRAY_SIZE(wm8750_reg);
1072
1073         wm8750_reset(codec);
1074
1075         /* register pcms */
1076         ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
1077         if (ret < 0) {
1078                 printk(KERN_ERR "wm8750: failed to create pcms\n");
1079                 goto pcm_err;
1080         }
1081
1082         /* charge output caps */
1083         wm8750_dapm_event(codec, SNDRV_CTL_POWER_D2);
1084         codec->dapm_state = SNDRV_CTL_POWER_D3hot;
1085         schedule_delayed_work(&codec->delayed_work, msecs_to_jiffies(1000));
1086
1087         /* set the update bits */
1088         reg = wm8750_read_reg_cache(codec, WM8750_LDAC);
1089         wm8750_write(codec, WM8750_LDAC, reg | 0x0100);
1090         reg = wm8750_read_reg_cache(codec, WM8750_RDAC);
1091         wm8750_write(codec, WM8750_RDAC, reg | 0x0100);
1092         reg = wm8750_read_reg_cache(codec, WM8750_LOUT1V);
1093         wm8750_write(codec, WM8750_LOUT1V, reg | 0x0100);
1094         reg = wm8750_read_reg_cache(codec, WM8750_ROUT1V);
1095         wm8750_write(codec, WM8750_ROUT1V, reg | 0x0100);
1096         reg = wm8750_read_reg_cache(codec, WM8750_LOUT2V);
1097         wm8750_write(codec, WM8750_LOUT2V, reg | 0x0100);
1098         reg = wm8750_read_reg_cache(codec, WM8750_ROUT2V);
1099         wm8750_write(codec, WM8750_ROUT2V, reg | 0x0100);
1100         reg = wm8750_read_reg_cache(codec, WM8750_LINVOL);
1101         wm8750_write(codec, WM8750_LINVOL, reg | 0x0100);
1102         reg = wm8750_read_reg_cache(codec, WM8750_RINVOL);
1103         wm8750_write(codec, WM8750_RINVOL, reg | 0x0100);
1104
1105         wm8750_add_controls(codec);
1106         wm8750_add_widgets(codec);
1107         ret = snd_soc_register_card(socdev);
1108         if (ret < 0) {
1109                 printk(KERN_ERR "wm8750: failed to register card\n");
1110                 goto card_err;
1111         }
1112         return ret;
1113
1114 card_err:
1115         snd_soc_free_pcms(socdev);
1116         snd_soc_dapm_free(socdev);
1117 pcm_err:
1118         kfree(codec->reg_cache);
1119         return ret;
1120 }
1121
1122 /* If the i2c layer weren't so broken, we could pass this kind of data
1123    around */
1124 static struct snd_soc_device *wm8750_socdev;
1125
1126 #if defined (CONFIG_I2C) || defined (CONFIG_I2C_MODULE)
1127
1128 /*
1129  * WM8731 2 wire address is determined by GPIO5
1130  * state during powerup.
1131  *    low  = 0x1a
1132  *    high = 0x1b
1133  */
1134 static unsigned short normal_i2c[] = { 0, I2C_CLIENT_END };
1135
1136 /* Magic definition of all other variables and things */
1137 I2C_CLIENT_INSMOD;
1138
1139 static struct i2c_driver wm8750_i2c_driver;
1140 static struct i2c_client client_template;
1141
1142 static int wm8750_codec_probe(struct i2c_adapter *adap, int addr, int kind)
1143 {
1144         struct snd_soc_device *socdev = wm8750_socdev;
1145         struct wm8750_setup_data *setup = socdev->codec_data;
1146         struct snd_soc_codec *codec = socdev->codec;
1147         struct i2c_client *i2c;
1148         int ret;
1149
1150         if (addr != setup->i2c_address)
1151                 return -ENODEV;
1152
1153         client_template.adapter = adap;
1154         client_template.addr = addr;
1155
1156         i2c = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1157         if (i2c == NULL) {
1158                 kfree(codec);
1159                 return -ENOMEM;
1160         }
1161         memcpy(i2c, &client_template, sizeof(struct i2c_client));
1162         i2c_set_clientdata(i2c, codec);
1163         codec->control_data = i2c;
1164
1165         ret = i2c_attach_client(i2c);
1166         if (ret < 0) {
1167                 err("failed to attach codec at addr %x\n", addr);
1168                 goto err;
1169         }
1170
1171         ret = wm8750_init(socdev);
1172         if (ret < 0) {
1173         err("failed to initialise WM8750\n");
1174                 goto err;
1175         }
1176         return ret;
1177
1178 err:
1179         kfree(codec);
1180         kfree(i2c);
1181         return ret;
1182 }
1183
1184 static int wm8750_i2c_detach(struct i2c_client *client)
1185 {
1186         struct snd_soc_codec *codec = i2c_get_clientdata(client);
1187         i2c_detach_client(client);
1188         kfree(codec->reg_cache);
1189         kfree(client);
1190         return 0;
1191 }
1192
1193 static int wm8750_i2c_attach(struct i2c_adapter *adap)
1194 {
1195         return i2c_probe(adap, &addr_data, wm8750_codec_probe);
1196 }
1197
1198 /* corgi i2c codec control layer */
1199 static struct i2c_driver wm8750_i2c_driver = {
1200         .driver = {
1201                 .name = "WM8750 I2C Codec",
1202                 .owner = THIS_MODULE,
1203         },
1204         .id =             I2C_DRIVERID_WM8750,
1205         .attach_adapter = wm8750_i2c_attach,
1206         .detach_client =  wm8750_i2c_detach,
1207         .command =        NULL,
1208 };
1209
1210 static struct i2c_client client_template = {
1211         .name =   "WM8750",
1212         .driver = &wm8750_i2c_driver,
1213 };
1214 #endif
1215
1216 static int wm8750_probe(struct platform_device *pdev)
1217 {
1218         struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1219         struct wm8750_setup_data *setup = socdev->codec_data;
1220         struct snd_soc_codec *codec;
1221         int ret = 0;
1222
1223         info("WM8750 Audio Codec %s", WM8750_VERSION);
1224         codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
1225         if (codec == NULL)
1226                 return -ENOMEM;
1227
1228         socdev->codec = codec;
1229         mutex_init(&codec->mutex);
1230         INIT_LIST_HEAD(&codec->dapm_widgets);
1231         INIT_LIST_HEAD(&codec->dapm_paths);
1232         wm8750_socdev = socdev;
1233         INIT_DELAYED_WORK(&codec->delayed_work, wm8750_work);
1234 #if defined (CONFIG_I2C) || defined (CONFIG_I2C_MODULE)
1235         if (setup->i2c_address) {
1236                 normal_i2c[0] = setup->i2c_address;
1237                 codec->hw_write = (hw_write_t)i2c_master_send;
1238                 ret = i2c_add_driver(&wm8750_i2c_driver);
1239                 if (ret != 0)
1240                         printk(KERN_ERR "can't add i2c driver");
1241         }
1242 #else
1243                 /* Add other interfaces here */
1244 #endif
1245
1246         return ret;
1247 }
1248
1249 /* power down chip */
1250 static int wm8750_remove(struct platform_device *pdev)
1251 {
1252         struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1253         struct snd_soc_codec *codec = socdev->codec;
1254
1255         if (codec->control_data)
1256                 wm8750_dapm_event(codec, SNDRV_CTL_POWER_D3cold);
1257         flush_scheduled_work();
1258         snd_soc_free_pcms(socdev);
1259         snd_soc_dapm_free(socdev);
1260 #if defined (CONFIG_I2C) || defined (CONFIG_I2C_MODULE)
1261         i2c_del_driver(&wm8750_i2c_driver);
1262 #endif
1263         kfree(codec);
1264
1265         return 0;
1266 }
1267
1268 struct snd_soc_codec_device soc_codec_dev_wm8750 = {
1269         .probe =        wm8750_probe,
1270         .remove =       wm8750_remove,
1271         .suspend =      wm8750_suspend,
1272         .resume =       wm8750_resume,
1273 };
1274
1275 EXPORT_SYMBOL_GPL(soc_codec_dev_wm8750);
1276
1277 MODULE_DESCRIPTION("ASoC WM8750 driver");
1278 MODULE_AUTHOR("Liam Girdwood");
1279 MODULE_LICENSE("GPL");