blob: 82f440fcf294e1553e2ac596e38e957008ab20a5 [file] [log] [blame]
Richard Purdie40e0aa62006-10-06 18:36:07 +02001/*
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 */
Liam Girdwood527541f2006-10-13 12:33:56 +020088 {
89 .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
Takashi Iwaib3b9c1c2006-10-13 20:09:59 +020090 .pcmfmt = WM8731_HIFI_BITS,
91 .pcmrate = SNDRV_PCM_RATE_8000,
Liam Girdwood527541f2006-10-13 12:33:56 +020092 .pcmdir = WM8731_DIR,
Liam Girdwooda71a4682006-10-19 20:35:56 +020093 .flags = SND_SOC_DAI_BFS_RATE,
Takashi Iwaib3b9c1c2006-10-13 20:09:59 +020094 .fs = 1536,
Liam Girdwooda71a4682006-10-19 20:35:56 +020095 .bfs = 64,
Liam Girdwood527541f2006-10-13 12:33:56 +020096 },
97 {
98 .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
Takashi Iwaib3b9c1c2006-10-13 20:09:59 +020099 .pcmfmt = WM8731_HIFI_BITS,
100 .pcmrate = SNDRV_PCM_RATE_8000,
Liam Girdwood527541f2006-10-13 12:33:56 +0200101 .pcmdir = WM8731_DIR,
Liam Girdwooda71a4682006-10-19 20:35:56 +0200102 .flags = SND_SOC_DAI_BFS_RATE,
Takashi Iwaib3b9c1c2006-10-13 20:09:59 +0200103 .fs = 2304,
Liam Girdwooda71a4682006-10-19 20:35:56 +0200104 .bfs = 64,
Liam Girdwood527541f2006-10-13 12:33:56 +0200105 },
106 {
107 .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
Takashi Iwaib3b9c1c2006-10-13 20:09:59 +0200108 .pcmfmt = WM8731_HIFI_BITS,
109 .pcmrate = SNDRV_PCM_RATE_8000,
Liam Girdwood527541f2006-10-13 12:33:56 +0200110 .pcmdir = WM8731_DIR,
Liam Girdwooda71a4682006-10-19 20:35:56 +0200111 .flags = SND_SOC_DAI_BFS_RATE,
Takashi Iwaib3b9c1c2006-10-13 20:09:59 +0200112 .fs = 1408,
Liam Girdwooda71a4682006-10-19 20:35:56 +0200113 .bfs = 64,
Liam Girdwood527541f2006-10-13 12:33:56 +0200114 },
115 {
116 .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
Takashi Iwaib3b9c1c2006-10-13 20:09:59 +0200117 .pcmfmt = WM8731_HIFI_BITS,
118 .pcmrate = SNDRV_PCM_RATE_8000,
Liam Girdwood527541f2006-10-13 12:33:56 +0200119 .pcmdir = WM8731_DIR,
Liam Girdwooda71a4682006-10-19 20:35:56 +0200120 .flags = SND_SOC_DAI_BFS_RATE,
Takashi Iwaib3b9c1c2006-10-13 20:09:59 +0200121 .fs = 2112,
Liam Girdwooda71a4682006-10-19 20:35:56 +0200122 .bfs = 64,
Liam Girdwood527541f2006-10-13 12:33:56 +0200123 },
Richard Purdie40e0aa62006-10-06 18:36:07 +0200124
125 /* 32k */
Liam Girdwood527541f2006-10-13 12:33:56 +0200126 {
127 .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
Takashi Iwaib3b9c1c2006-10-13 20:09:59 +0200128 .pcmfmt = WM8731_HIFI_BITS,
Liam Girdwood527541f2006-10-13 12:33:56 +0200129 .pcmrate = SNDRV_PCM_RATE_32000,
130 .pcmdir = WM8731_DIR,
Liam Girdwooda71a4682006-10-19 20:35:56 +0200131 .flags = SND_SOC_DAI_BFS_RATE,
Takashi Iwaib3b9c1c2006-10-13 20:09:59 +0200132 .fs = 384,
Liam Girdwooda71a4682006-10-19 20:35:56 +0200133 .bfs = 64,
Liam Girdwood527541f2006-10-13 12:33:56 +0200134 },
135 {
136 .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
Takashi Iwaib3b9c1c2006-10-13 20:09:59 +0200137 .pcmfmt = WM8731_HIFI_BITS,
138 .pcmrate = SNDRV_PCM_RATE_32000,
Liam Girdwood527541f2006-10-13 12:33:56 +0200139 .pcmdir = WM8731_DIR,
Liam Girdwooda71a4682006-10-19 20:35:56 +0200140 .flags = SND_SOC_DAI_BFS_RATE,
Takashi Iwaib3b9c1c2006-10-13 20:09:59 +0200141 .fs = 576,
Liam Girdwooda71a4682006-10-19 20:35:56 +0200142 .bfs = 64,
Liam Girdwood527541f2006-10-13 12:33:56 +0200143 },
Richard Purdie40e0aa62006-10-06 18:36:07 +0200144
145 /* 44.1k & 48k */
Liam Girdwood527541f2006-10-13 12:33:56 +0200146 {
147 .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
Takashi Iwaib3b9c1c2006-10-13 20:09:59 +0200148 .pcmfmt = WM8731_HIFI_BITS,
Liam Girdwood527541f2006-10-13 12:33:56 +0200149 .pcmrate = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
150 .pcmdir = WM8731_DIR,
Liam Girdwooda71a4682006-10-19 20:35:56 +0200151 .flags = SND_SOC_DAI_BFS_RATE,
Takashi Iwaib3b9c1c2006-10-13 20:09:59 +0200152 .fs = 256,
Liam Girdwooda71a4682006-10-19 20:35:56 +0200153 .bfs = 64,
Liam Girdwood527541f2006-10-13 12:33:56 +0200154 },
155 {
156 .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
Takashi Iwaib3b9c1c2006-10-13 20:09:59 +0200157 .pcmfmt = WM8731_HIFI_BITS,
Liam Girdwood527541f2006-10-13 12:33:56 +0200158 .pcmrate = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
159 .pcmdir = WM8731_DIR,
Liam Girdwooda71a4682006-10-19 20:35:56 +0200160 .flags = SND_SOC_DAI_BFS_RATE,
Takashi Iwaib3b9c1c2006-10-13 20:09:59 +0200161 .fs = 384,
Liam Girdwooda71a4682006-10-19 20:35:56 +0200162 .bfs = 64,
Liam Girdwood527541f2006-10-13 12:33:56 +0200163 },
Richard Purdie40e0aa62006-10-06 18:36:07 +0200164
165 /* 88.2 & 96k */
Liam Girdwood527541f2006-10-13 12:33:56 +0200166 {
167 .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
Takashi Iwaib3b9c1c2006-10-13 20:09:59 +0200168 .pcmfmt = WM8731_HIFI_BITS,
Liam Girdwood527541f2006-10-13 12:33:56 +0200169 .pcmrate = SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000,
170 .pcmdir = WM8731_DIR,
Liam Girdwooda71a4682006-10-19 20:35:56 +0200171 .flags = SND_SOC_DAI_BFS_RATE,
Takashi Iwaib3b9c1c2006-10-13 20:09:59 +0200172 .fs = 128,
Liam Girdwooda71a4682006-10-19 20:35:56 +0200173 .bfs = 64,
Liam Girdwood527541f2006-10-13 12:33:56 +0200174 },
175 {
176 .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
Takashi Iwaib3b9c1c2006-10-13 20:09:59 +0200177 .pcmfmt = WM8731_HIFI_BITS,
Liam Girdwood527541f2006-10-13 12:33:56 +0200178 .pcmrate = SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000,
179 .pcmdir = WM8731_DIR,
Liam Girdwooda71a4682006-10-19 20:35:56 +0200180 .flags = SND_SOC_DAI_BFS_RATE,
Takashi Iwaib3b9c1c2006-10-13 20:09:59 +0200181 .fs = 192,
Liam Girdwooda71a4682006-10-19 20:35:56 +0200182 .bfs = 64,
Liam Girdwood527541f2006-10-13 12:33:56 +0200183 },
Richard Purdie40e0aa62006-10-06 18:36:07 +0200184
185 /* USB codec frame and clock master modes */
186 /* 8k */
Liam Girdwood527541f2006-10-13 12:33:56 +0200187 {
188 .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
Takashi Iwaib3b9c1c2006-10-13 20:09:59 +0200189 .pcmfmt = WM8731_HIFI_BITS,
190 .pcmrate = SNDRV_PCM_RATE_8000,
Liam Girdwood527541f2006-10-13 12:33:56 +0200191 .pcmdir = WM8731_DIR,
Takashi Iwaib3b9c1c2006-10-13 20:09:59 +0200192 .flags = SND_SOC_DAI_BFS_DIV,
193 .fs = 1500,
Liam Girdwood527541f2006-10-13 12:33:56 +0200194 .bfs = SND_SOC_FSBD(1),
195 },
Richard Purdie40e0aa62006-10-06 18:36:07 +0200196
197 /* 44.1k */
Liam Girdwood527541f2006-10-13 12:33:56 +0200198 {
199 .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
Takashi Iwaib3b9c1c2006-10-13 20:09:59 +0200200 .pcmfmt = WM8731_HIFI_BITS,
201 .pcmrate = SNDRV_PCM_RATE_44100,
Liam Girdwood527541f2006-10-13 12:33:56 +0200202 .pcmdir = WM8731_DIR,
203 .flags = SND_SOC_DAI_BFS_DIV,
Takashi Iwaib3b9c1c2006-10-13 20:09:59 +0200204 .fs = 272,
Liam Girdwood527541f2006-10-13 12:33:56 +0200205 .bfs = SND_SOC_FSBD(1),
206 },
Richard Purdie40e0aa62006-10-06 18:36:07 +0200207
208 /* 48k */
Liam Girdwood527541f2006-10-13 12:33:56 +0200209 {
210 .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
Takashi Iwaib3b9c1c2006-10-13 20:09:59 +0200211 .pcmfmt = WM8731_HIFI_BITS,
212 .pcmrate = SNDRV_PCM_RATE_48000,
Liam Girdwood527541f2006-10-13 12:33:56 +0200213 .pcmdir = WM8731_DIR,
Takashi Iwaib3b9c1c2006-10-13 20:09:59 +0200214 .flags = SND_SOC_DAI_BFS_DIV,
215 .fs = 250,
Liam Girdwood527541f2006-10-13 12:33:56 +0200216 .bfs = SND_SOC_FSBD(1),
217 },
Richard Purdie40e0aa62006-10-06 18:36:07 +0200218
219 /* 88.2k */
Liam Girdwood527541f2006-10-13 12:33:56 +0200220 {
Takashi Iwaib3b9c1c2006-10-13 20:09:59 +0200221 .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
222 .pcmfmt = WM8731_HIFI_BITS,
223 .pcmrate = SNDRV_PCM_RATE_88200,
Liam Girdwood527541f2006-10-13 12:33:56 +0200224 .pcmdir = WM8731_DIR,
225 .flags = SND_SOC_DAI_BFS_DIV,
Takashi Iwaib3b9c1c2006-10-13 20:09:59 +0200226 .fs = 136,
Liam Girdwood527541f2006-10-13 12:33:56 +0200227 .bfs = SND_SOC_FSBD(1),
228 },
Richard Purdie40e0aa62006-10-06 18:36:07 +0200229
230 /* 96k */
Liam Girdwood527541f2006-10-13 12:33:56 +0200231 {
232 .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
Takashi Iwaib3b9c1c2006-10-13 20:09:59 +0200233 .pcmfmt = WM8731_HIFI_BITS,
234 .pcmrate = SNDRV_PCM_RATE_96000,
Liam Girdwood527541f2006-10-13 12:33:56 +0200235 .pcmdir = WM8731_DIR,
Takashi Iwaib3b9c1c2006-10-13 20:09:59 +0200236 .flags = SND_SOC_DAI_BFS_DIV,
237 .fs = 125,
Liam Girdwood527541f2006-10-13 12:33:56 +0200238 .bfs = SND_SOC_FSBD(1),
239 },
Richard Purdie40e0aa62006-10-06 18:36:07 +0200240
241 /* codec frame and clock slave modes */
Liam Girdwood527541f2006-10-13 12:33:56 +0200242 {
243 .fmt = WM8731_DAIFMT | SND_SOC_DAIFMT_CBS_CFS,
Takashi Iwaib3b9c1c2006-10-13 20:09:59 +0200244 .pcmfmt = WM8731_HIFI_BITS,
245 .pcmrate = WM8731_RATES,
Liam Girdwood527541f2006-10-13 12:33:56 +0200246 .pcmdir = WM8731_DIR,
247 .flags = SND_SOC_DAI_BFS_DIV,
Takashi Iwaib3b9c1c2006-10-13 20:09:59 +0200248 .fs = SND_SOC_FS_ALL,
Liam Girdwooda71a4682006-10-19 20:35:56 +0200249 .bfs = SND_SOC_FSB_ALL,
Liam Girdwood527541f2006-10-13 12:33:56 +0200250 },
Richard Purdie40e0aa62006-10-06 18:36:07 +0200251};
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
Liam Girdwoodbd903b62006-11-09 16:35:01 +0100313SOC_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),
Richard Purdie40e0aa62006-10-06 18:36:07 +0200317
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},
Frank Mandarino298a2c72007-01-31 10:02:56 +0100438 {12000000, 32000, 375, 0x6, 0x0, 0x1},
Richard Purdie40e0aa62006-10-06 18:36:07 +0200439
440 /* 8k */
441 {12288000, 8000, 1536, 0x3, 0x0, 0x0},
442 {18432000, 8000, 2304, 0x3, 0x1, 0x0},
443 {11289600, 8000, 1408, 0xb, 0x0, 0x0},
444 {16934400, 8000, 2112, 0xb, 0x1, 0x0},
445 {12000000, 8000, 1500, 0x3, 0x0, 0x1},
446
447 /* 96k */
448 {12288000, 96000, 128, 0x7, 0x0, 0x0},
449 {18432000, 96000, 192, 0x7, 0x1, 0x0},
450 {12000000, 96000, 125, 0x7, 0x0, 0x1},
451
452 /* 44.1k */
453 {11289600, 44100, 256, 0x8, 0x0, 0x0},
454 {16934400, 44100, 384, 0x8, 0x1, 0x0},
455 {12000000, 44100, 272, 0x8, 0x1, 0x1},
456
457 /* 88.2k */
458 {11289600, 88200, 128, 0xf, 0x0, 0x0},
459 {16934400, 88200, 192, 0xf, 0x1, 0x0},
460 {12000000, 88200, 136, 0xf, 0x1, 0x1},
461};
462
463static inline int get_coeff(int mclk, int rate)
464{
465 int i;
466
467 for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
468 if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
469 return i;
470 }
471 return 0;
472}
473
474/* WM8731 supports numerous clocks per sample rate */
475static unsigned int wm8731_config_sysclk(struct snd_soc_codec_dai *dai,
476 struct snd_soc_clock_info *info, unsigned int clk)
477{
478 dai->mclk = 0;
479
480 /* check that the calculated FS and rate actually match a clock from
481 * the machine driver */
482 if (info->fs * info->rate == clk)
483 dai->mclk = clk;
484
485 return dai->mclk;
486}
487
488static int wm8731_pcm_prepare(struct snd_pcm_substream *substream)
489{
490 struct snd_soc_pcm_runtime *rtd = substream->private_data;
491 struct snd_soc_device *socdev = rtd->socdev;
492 struct snd_soc_codec *codec = socdev->codec;
493 u16 iface = 0, srate;
494 int i = get_coeff(rtd->codec_dai->mclk,
495 snd_soc_get_rate(rtd->codec_dai->dai_runtime.pcmrate));
496
497 /* set master/slave audio interface */
498 switch (rtd->codec_dai->dai_runtime.fmt & SND_SOC_DAIFMT_CLOCK_MASK) {
499 case SND_SOC_DAIFMT_CBM_CFM:
500 iface |= 0x0040;
501 break;
502 case SND_SOC_DAIFMT_CBS_CFS:
503 break;
504 }
505 srate = (coeff_div[i].sr << 2) |
506 (coeff_div[i].bosr << 1) | coeff_div[i].usb;
507 wm8731_write(codec, WM8731_SRATE, srate);
508
509 /* interface format */
510 switch (rtd->codec_dai->dai_runtime.fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
511 case SND_SOC_DAIFMT_I2S:
512 iface |= 0x0002;
513 break;
514 case SND_SOC_DAIFMT_RIGHT_J:
515 break;
516 case SND_SOC_DAIFMT_LEFT_J:
517 iface |= 0x0001;
518 break;
519 case SND_SOC_DAIFMT_DSP_A:
520 iface |= 0x0003;
521 break;
522 case SND_SOC_DAIFMT_DSP_B:
523 iface |= 0x0013;
524 break;
525 }
526
527 /* bit size */
528 switch (rtd->codec_dai->dai_runtime.pcmfmt) {
529 case SNDRV_PCM_FMTBIT_S16_LE:
530 break;
531 case SNDRV_PCM_FMTBIT_S20_3LE:
532 iface |= 0x0004;
533 break;
534 case SNDRV_PCM_FMTBIT_S24_LE:
535 iface |= 0x0008;
536 break;
537 case SNDRV_PCM_FMTBIT_S32_LE:
538 iface |= 0x000c;
539 break;
540 }
541
542 /* clock inversion */
543 switch (rtd->codec_dai->dai_runtime.fmt & SND_SOC_DAIFMT_INV_MASK) {
544 case SND_SOC_DAIFMT_NB_NF:
545 break;
546 case SND_SOC_DAIFMT_IB_IF:
547 iface |= 0x0090;
548 break;
549 case SND_SOC_DAIFMT_IB_NF:
550 iface |= 0x0080;
551 break;
552 case SND_SOC_DAIFMT_NB_IF:
553 iface |= 0x0010;
554 break;
555 }
556
557 /* set iface */
558 wm8731_write(codec, WM8731_IFACE, iface);
559
560 /* set active */
561 wm8731_write(codec, WM8731_ACTIVE, 0x0001);
562 return 0;
563}
564
565static void wm8731_shutdown(struct snd_pcm_substream *substream)
566{
567 struct snd_soc_pcm_runtime *rtd = substream->private_data;
568 struct snd_soc_device *socdev = rtd->socdev;
569 struct snd_soc_codec *codec = socdev->codec;
570
571 /* deactivate */
572 if (!codec->active) {
573 udelay(50);
574 wm8731_write(codec, WM8731_ACTIVE, 0x0);
575 }
576}
577
578static int wm8731_mute(struct snd_soc_codec *codec,
579 struct snd_soc_codec_dai *dai, int mute)
580{
581 u16 mute_reg = wm8731_read_reg_cache(codec, WM8731_APDIGI) & 0xfff7;
582 if (mute)
583 wm8731_write(codec, WM8731_APDIGI, mute_reg | 0x8);
584 else
585 wm8731_write(codec, WM8731_APDIGI, mute_reg);
586 return 0;
587}
588
589static int wm8731_dapm_event(struct snd_soc_codec *codec, int event)
590{
591 u16 reg = wm8731_read_reg_cache(codec, WM8731_PWR) & 0xff7f;
592
593 switch (event) {
594 case SNDRV_CTL_POWER_D0: /* full On */
595 /* vref/mid, osc on, dac unmute */
596 wm8731_write(codec, WM8731_PWR, reg);
597 break;
598 case SNDRV_CTL_POWER_D1: /* partial On */
599 case SNDRV_CTL_POWER_D2: /* partial On */
600 break;
601 case SNDRV_CTL_POWER_D3hot: /* Off, with power */
602 /* everything off except vref/vmid, */
603 wm8731_write(codec, WM8731_PWR, reg | 0x0040);
604 break;
605 case SNDRV_CTL_POWER_D3cold: /* Off, without power */
606 /* everything off, dac mute, inactive */
607 wm8731_write(codec, WM8731_ACTIVE, 0x0);
608 wm8731_write(codec, WM8731_PWR, 0xffff);
609 break;
610 }
611 codec->dapm_state = event;
612 return 0;
613}
614
615struct snd_soc_codec_dai wm8731_dai = {
616 .name = "WM8731",
617 .playback = {
618 .stream_name = "Playback",
619 .channels_min = 1,
620 .channels_max = 2,
621 },
622 .capture = {
623 .stream_name = "Capture",
624 .channels_min = 1,
625 .channels_max = 2,
626 },
627 .config_sysclk = wm8731_config_sysclk,
628 .digital_mute = wm8731_mute,
629 .ops = {
630 .prepare = wm8731_pcm_prepare,
631 .shutdown = wm8731_shutdown,
632 },
633 .caps = {
634 .num_modes = ARRAY_SIZE(wm8731_modes),
635 .mode = wm8731_modes,
636 },
637};
638EXPORT_SYMBOL_GPL(wm8731_dai);
639
640static int wm8731_suspend(struct platform_device *pdev, pm_message_t state)
641{
642 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
643 struct snd_soc_codec *codec = socdev->codec;
644
645 wm8731_write(codec, WM8731_ACTIVE, 0x0);
646 wm8731_dapm_event(codec, SNDRV_CTL_POWER_D3cold);
647 return 0;
648}
649
650static int wm8731_resume(struct platform_device *pdev)
651{
652 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
653 struct snd_soc_codec *codec = socdev->codec;
654 int i;
655 u8 data[2];
656 u16 *cache = codec->reg_cache;
657
658 /* Sync reg_cache with the hardware */
659 for (i = 0; i < ARRAY_SIZE(wm8731_reg); i++) {
660 data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001);
661 data[1] = cache[i] & 0x00ff;
662 codec->hw_write(codec->control_data, data, 2);
663 }
664 wm8731_dapm_event(codec, SNDRV_CTL_POWER_D3hot);
665 wm8731_dapm_event(codec, codec->suspend_dapm_state);
666 return 0;
667}
668
669/*
670 * initialise the WM8731 driver
671 * register the mixer and dsp interfaces with the kernel
672 */
673static int wm8731_init(struct snd_soc_device *socdev)
674{
675 struct snd_soc_codec *codec = socdev->codec;
676 int reg, ret = 0;
677
678 codec->name = "WM8731";
679 codec->owner = THIS_MODULE;
680 codec->read = wm8731_read_reg_cache;
681 codec->write = wm8731_write;
682 codec->dapm_event = wm8731_dapm_event;
683 codec->dai = &wm8731_dai;
684 codec->num_dai = 1;
685 codec->reg_cache_size = ARRAY_SIZE(wm8731_reg);
686
687 codec->reg_cache =
688 kzalloc(sizeof(u16) * ARRAY_SIZE(wm8731_reg), GFP_KERNEL);
689 if (codec->reg_cache == NULL)
690 return -ENOMEM;
691 memcpy(codec->reg_cache,
692 wm8731_reg, sizeof(u16) * ARRAY_SIZE(wm8731_reg));
693 codec->reg_cache_size = sizeof(u16) * ARRAY_SIZE(wm8731_reg);
694
695 wm8731_reset(codec);
696
697 /* register pcms */
698 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
699 if (ret < 0) {
Liam Girdwoode35115a2007-01-31 10:02:23 +0100700 printk(KERN_ERR "wm8731: failed to create pcms\n");
701 goto pcm_err;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200702 }
703
704 /* power on device */
705 wm8731_dapm_event(codec, SNDRV_CTL_POWER_D3hot);
706
707 /* set the update bits */
708 reg = wm8731_read_reg_cache(codec, WM8731_LOUT1V);
709 wm8731_write(codec, WM8731_LOUT1V, reg | 0x0100);
710 reg = wm8731_read_reg_cache(codec, WM8731_ROUT1V);
711 wm8731_write(codec, WM8731_ROUT1V, reg | 0x0100);
712 reg = wm8731_read_reg_cache(codec, WM8731_LINVOL);
713 wm8731_write(codec, WM8731_LINVOL, reg | 0x0100);
714 reg = wm8731_read_reg_cache(codec, WM8731_RINVOL);
715 wm8731_write(codec, WM8731_RINVOL, reg | 0x0100);
716
717 wm8731_add_controls(codec);
718 wm8731_add_widgets(codec);
719 ret = snd_soc_register_card(socdev);
720 if (ret < 0) {
Liam Girdwoode35115a2007-01-31 10:02:23 +0100721 printk(KERN_ERR "wm8731: failed to register card\n");
722 goto card_err;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200723 }
724
725 return ret;
Liam Girdwoode35115a2007-01-31 10:02:23 +0100726
727card_err:
728 snd_soc_free_pcms(socdev);
729 snd_soc_dapm_free(socdev);
730pcm_err:
731 kfree(codec->reg_cache);
732 return ret;
Richard Purdie40e0aa62006-10-06 18:36:07 +0200733}
734
735static struct snd_soc_device *wm8731_socdev;
736
737#if defined (CONFIG_I2C) || defined (CONFIG_I2C_MODULE)
738
739/*
740 * WM8731 2 wire address is determined by GPIO5
741 * state during powerup.
742 * low = 0x1a
743 * high = 0x1b
744 */
745static unsigned short normal_i2c[] = { 0, I2C_CLIENT_END };
746
747/* Magic definition of all other variables and things */
748I2C_CLIENT_INSMOD;
749
750static struct i2c_driver wm8731_i2c_driver;
751static struct i2c_client client_template;
752
753/* If the i2c layer weren't so broken, we could pass this kind of data
754 around */
755
756static int wm8731_codec_probe(struct i2c_adapter *adap, int addr, int kind)
757{
758 struct snd_soc_device *socdev = wm8731_socdev;
759 struct wm8731_setup_data *setup = socdev->codec_data;
760 struct snd_soc_codec *codec = socdev->codec;
761 struct i2c_client *i2c;
762 int ret;
763
764 if (addr != setup->i2c_address)
765 return -ENODEV;
766
767 client_template.adapter = adap;
768 client_template.addr = addr;
769
770 i2c = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
771 if (i2c == NULL) {
772 kfree(codec);
773 return -ENOMEM;
774 }
775 memcpy(i2c, &client_template, sizeof(struct i2c_client));
776 i2c_set_clientdata(i2c, codec);
777 codec->control_data = i2c;
778
779 ret = i2c_attach_client(i2c);
780 if (ret < 0) {
781 err("failed to attach codec at addr %x\n", addr);
782 goto err;
783 }
784
785 ret = wm8731_init(socdev);
786 if (ret < 0) {
787 err("failed to initialise WM8731\n");
788 goto err;
789 }
790 return ret;
791
792err:
793 kfree(codec);
794 kfree(i2c);
795 return ret;
796}
797
798static int wm8731_i2c_detach(struct i2c_client *client)
799{
800 struct snd_soc_codec* codec = i2c_get_clientdata(client);
801 i2c_detach_client(client);
802 kfree(codec->reg_cache);
803 kfree(client);
804 return 0;
805}
806
807static int wm8731_i2c_attach(struct i2c_adapter *adap)
808{
809 return i2c_probe(adap, &addr_data, wm8731_codec_probe);
810}
811
812/* corgi i2c codec control layer */
813static struct i2c_driver wm8731_i2c_driver = {
814 .driver = {
815 .name = "WM8731 I2C Codec",
816 .owner = THIS_MODULE,
817 },
818 .id = I2C_DRIVERID_WM8731,
819 .attach_adapter = wm8731_i2c_attach,
820 .detach_client = wm8731_i2c_detach,
821 .command = NULL,
822};
823
824static struct i2c_client client_template = {
825 .name = "WM8731",
826 .driver = &wm8731_i2c_driver,
827};
828#endif
829
830static int wm8731_probe(struct platform_device *pdev)
831{
832 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
833 struct wm8731_setup_data *setup;
834 struct snd_soc_codec *codec;
835 int ret = 0;
836
837 info("WM8731 Audio Codec %s", WM8731_VERSION);
838
839 setup = socdev->codec_data;
840 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
841 if (codec == NULL)
842 return -ENOMEM;
843
844 socdev->codec = codec;
845 mutex_init(&codec->mutex);
846 INIT_LIST_HEAD(&codec->dapm_widgets);
847 INIT_LIST_HEAD(&codec->dapm_paths);
848
849 wm8731_socdev = socdev;
850#if defined (CONFIG_I2C) || defined (CONFIG_I2C_MODULE)
851 if (setup->i2c_address) {
852 normal_i2c[0] = setup->i2c_address;
853 codec->hw_write = (hw_write_t)i2c_master_send;
854 ret = i2c_add_driver(&wm8731_i2c_driver);
855 if (ret != 0)
856 printk(KERN_ERR "can't add i2c driver");
857 }
858#else
859 /* Add other interfaces here */
860#endif
861 return ret;
862}
863
864/* power down chip */
865static int wm8731_remove(struct platform_device *pdev)
866{
867 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
868 struct snd_soc_codec *codec = socdev->codec;
869
870 if (codec->control_data)
871 wm8731_dapm_event(codec, SNDRV_CTL_POWER_D3cold);
872
873 snd_soc_free_pcms(socdev);
874 snd_soc_dapm_free(socdev);
875#if defined (CONFIG_I2C) || defined (CONFIG_I2C_MODULE)
876 i2c_del_driver(&wm8731_i2c_driver);
877#endif
878 kfree(codec);
879
880 return 0;
881}
882
883struct snd_soc_codec_device soc_codec_dev_wm8731 = {
884 .probe = wm8731_probe,
885 .remove = wm8731_remove,
886 .suspend = wm8731_suspend,
887 .resume = wm8731_resume,
888};
889
890EXPORT_SYMBOL_GPL(soc_codec_dev_wm8731);
891
892MODULE_DESCRIPTION("ASoC WM8731 driver");
893MODULE_AUTHOR("Richard Purdie");
894MODULE_LICENSE("GPL");