blob: a65b781af512b1d1c794fdd2c5a5ac22c2f442ca [file] [log] [blame]
Kenneth Kiraly8bae3e22008-09-10 19:33:32 +01001/*
2 * wm8971.c -- WM8971 ALSA SoC Audio driver
3 *
4 * Copyright 2005 Lab126, Inc.
5 *
6 * Author: Kenneth Kiraly <kiraly@lab126.com>
7 *
8 * Based on wm8753.c by Liam Girdwood
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
14 */
15
16#include <linux/module.h>
17#include <linux/moduleparam.h>
18#include <linux/init.h>
19#include <linux/delay.h>
20#include <linux/pm.h>
21#include <linux/i2c.h>
22#include <linux/platform_device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Kenneth Kiraly8bae3e22008-09-10 19:33:32 +010024#include <sound/core.h>
25#include <sound/pcm.h>
26#include <sound/pcm_params.h>
27#include <sound/soc.h>
28#include <sound/soc-dapm.h>
29#include <sound/initval.h>
30
31#include "wm8971.h"
32
Kenneth Kiraly8bae3e22008-09-10 19:33:32 +010033#define WM8971_VERSION "0.9"
34
35#define WM8971_REG_COUNT 43
36
37static struct workqueue_struct *wm8971_workq = NULL;
38
39/* codec private data */
40struct wm8971_priv {
41 unsigned int sysclk;
42};
43
44/*
45 * wm8971 register cache
46 * We can't read the WM8971 register space when we
47 * are using 2 wire for device control, so we cache them instead.
48 */
49static const u16 wm8971_reg[] = {
50 0x0097, 0x0097, 0x0079, 0x0079, /* 0 */
51 0x0000, 0x0008, 0x0000, 0x000a, /* 4 */
52 0x0000, 0x0000, 0x00ff, 0x00ff, /* 8 */
53 0x000f, 0x000f, 0x0000, 0x0000, /* 12 */
54 0x0000, 0x007b, 0x0000, 0x0032, /* 16 */
55 0x0000, 0x00c3, 0x00c3, 0x00c0, /* 20 */
56 0x0000, 0x0000, 0x0000, 0x0000, /* 24 */
57 0x0000, 0x0000, 0x0000, 0x0000, /* 28 */
58 0x0000, 0x0000, 0x0050, 0x0050, /* 32 */
59 0x0050, 0x0050, 0x0050, 0x0050, /* 36 */
60 0x0079, 0x0079, 0x0079, /* 40 */
61};
62
Mark Brown17a52fd2009-07-05 17:24:50 +010063#define wm8971_reset(c) snd_soc_write(c, WM8971_RESET, 0)
Kenneth Kiraly8bae3e22008-09-10 19:33:32 +010064
65/* WM8971 Controls */
66static const char *wm8971_bass[] = { "Linear Control", "Adaptive Boost" };
67static const char *wm8971_bass_filter[] = { "130Hz @ 48kHz",
68 "200Hz @ 48kHz" };
69static const char *wm8971_treble[] = { "8kHz", "4kHz" };
70static const char *wm8971_alc_func[] = { "Off", "Right", "Left", "Stereo" };
71static const char *wm8971_ng_type[] = { "Constant PGA Gain",
72 "Mute ADC Output" };
73static const char *wm8971_deemp[] = { "None", "32kHz", "44.1kHz", "48kHz" };
74static const char *wm8971_mono_mux[] = {"Stereo", "Mono (Left)",
75 "Mono (Right)", "Digital Mono"};
76static const char *wm8971_dac_phase[] = { "Non Inverted", "Inverted" };
77static const char *wm8971_lline_mux[] = {"Line", "NC", "NC", "PGA",
78 "Differential"};
79static const char *wm8971_rline_mux[] = {"Line", "Mic", "NC", "PGA",
80 "Differential"};
81static const char *wm8971_lpga_sel[] = {"Line", "NC", "NC", "Differential"};
82static const char *wm8971_rpga_sel[] = {"Line", "Mic", "NC", "Differential"};
83static const char *wm8971_adcpol[] = {"Normal", "L Invert", "R Invert",
84 "L + R Invert"};
85
86static const struct soc_enum wm8971_enum[] = {
87 SOC_ENUM_SINGLE(WM8971_BASS, 7, 2, wm8971_bass), /* 0 */
88 SOC_ENUM_SINGLE(WM8971_BASS, 6, 2, wm8971_bass_filter),
89 SOC_ENUM_SINGLE(WM8971_TREBLE, 6, 2, wm8971_treble),
90 SOC_ENUM_SINGLE(WM8971_ALC1, 7, 4, wm8971_alc_func),
91 SOC_ENUM_SINGLE(WM8971_NGATE, 1, 2, wm8971_ng_type), /* 4 */
92 SOC_ENUM_SINGLE(WM8971_ADCDAC, 1, 4, wm8971_deemp),
93 SOC_ENUM_SINGLE(WM8971_ADCTL1, 4, 4, wm8971_mono_mux),
94 SOC_ENUM_SINGLE(WM8971_ADCTL1, 1, 2, wm8971_dac_phase),
95 SOC_ENUM_SINGLE(WM8971_LOUTM1, 0, 5, wm8971_lline_mux), /* 8 */
96 SOC_ENUM_SINGLE(WM8971_ROUTM1, 0, 5, wm8971_rline_mux),
97 SOC_ENUM_SINGLE(WM8971_LADCIN, 6, 4, wm8971_lpga_sel),
98 SOC_ENUM_SINGLE(WM8971_RADCIN, 6, 4, wm8971_rpga_sel),
99 SOC_ENUM_SINGLE(WM8971_ADCDAC, 5, 4, wm8971_adcpol), /* 12 */
100 SOC_ENUM_SINGLE(WM8971_ADCIN, 6, 4, wm8971_mono_mux),
101};
102
103static const struct snd_kcontrol_new wm8971_snd_controls[] = {
104 SOC_DOUBLE_R("Capture Volume", WM8971_LINVOL, WM8971_RINVOL, 0, 63, 0),
105 SOC_DOUBLE_R("Capture ZC Switch", WM8971_LINVOL, WM8971_RINVOL,
106 6, 1, 0),
107 SOC_DOUBLE_R("Capture Switch", WM8971_LINVOL, WM8971_RINVOL, 7, 1, 1),
108
109 SOC_DOUBLE_R("Headphone Playback ZC Switch", WM8971_LOUT1V,
110 WM8971_ROUT1V, 7, 1, 0),
111 SOC_DOUBLE_R("Speaker Playback ZC Switch", WM8971_LOUT2V,
112 WM8971_ROUT2V, 7, 1, 0),
113 SOC_SINGLE("Mono Playback ZC Switch", WM8971_MOUTV, 7, 1, 0),
114
115 SOC_DOUBLE_R("PCM Volume", WM8971_LDAC, WM8971_RDAC, 0, 255, 0),
116
117 SOC_DOUBLE_R("Bypass Left Playback Volume", WM8971_LOUTM1,
118 WM8971_LOUTM2, 4, 7, 1),
119 SOC_DOUBLE_R("Bypass Right Playback Volume", WM8971_ROUTM1,
120 WM8971_ROUTM2, 4, 7, 1),
121 SOC_DOUBLE_R("Bypass Mono Playback Volume", WM8971_MOUTM1,
122 WM8971_MOUTM2, 4, 7, 1),
123
124 SOC_DOUBLE_R("Headphone Playback Volume", WM8971_LOUT1V,
125 WM8971_ROUT1V, 0, 127, 0),
126 SOC_DOUBLE_R("Speaker Playback Volume", WM8971_LOUT2V,
127 WM8971_ROUT2V, 0, 127, 0),
128
129 SOC_ENUM("Bass Boost", wm8971_enum[0]),
130 SOC_ENUM("Bass Filter", wm8971_enum[1]),
131 SOC_SINGLE("Bass Volume", WM8971_BASS, 0, 7, 1),
132
133 SOC_SINGLE("Treble Volume", WM8971_TREBLE, 0, 7, 0),
134 SOC_ENUM("Treble Cut-off", wm8971_enum[2]),
135
136 SOC_SINGLE("Capture Filter Switch", WM8971_ADCDAC, 0, 1, 1),
137
138 SOC_SINGLE("ALC Target Volume", WM8971_ALC1, 0, 7, 0),
139 SOC_SINGLE("ALC Max Volume", WM8971_ALC1, 4, 7, 0),
140
141 SOC_SINGLE("ALC Capture Target Volume", WM8971_ALC1, 0, 7, 0),
142 SOC_SINGLE("ALC Capture Max Volume", WM8971_ALC1, 4, 7, 0),
143 SOC_ENUM("ALC Capture Function", wm8971_enum[3]),
144 SOC_SINGLE("ALC Capture ZC Switch", WM8971_ALC2, 7, 1, 0),
145 SOC_SINGLE("ALC Capture Hold Time", WM8971_ALC2, 0, 15, 0),
146 SOC_SINGLE("ALC Capture Decay Time", WM8971_ALC3, 4, 15, 0),
147 SOC_SINGLE("ALC Capture Attack Time", WM8971_ALC3, 0, 15, 0),
148 SOC_SINGLE("ALC Capture NG Threshold", WM8971_NGATE, 3, 31, 0),
149 SOC_ENUM("ALC Capture NG Type", wm8971_enum[4]),
150 SOC_SINGLE("ALC Capture NG Switch", WM8971_NGATE, 0, 1, 0),
151
152 SOC_SINGLE("Capture 6dB Attenuate", WM8971_ADCDAC, 8, 1, 0),
153 SOC_SINGLE("Playback 6dB Attenuate", WM8971_ADCDAC, 7, 1, 0),
154
155 SOC_ENUM("Playback De-emphasis", wm8971_enum[5]),
156 SOC_ENUM("Playback Function", wm8971_enum[6]),
157 SOC_ENUM("Playback Phase", wm8971_enum[7]),
158
159 SOC_DOUBLE_R("Mic Boost", WM8971_LADCIN, WM8971_RADCIN, 4, 3, 0),
160};
161
Kenneth Kiraly8bae3e22008-09-10 19:33:32 +0100162/*
163 * DAPM Controls
164 */
165
166/* Left Mixer */
167static const struct snd_kcontrol_new wm8971_left_mixer_controls[] = {
168SOC_DAPM_SINGLE("Playback Switch", WM8971_LOUTM1, 8, 1, 0),
169SOC_DAPM_SINGLE("Left Bypass Switch", WM8971_LOUTM1, 7, 1, 0),
170SOC_DAPM_SINGLE("Right Playback Switch", WM8971_LOUTM2, 8, 1, 0),
171SOC_DAPM_SINGLE("Right Bypass Switch", WM8971_LOUTM2, 7, 1, 0),
172};
173
174/* Right Mixer */
175static const struct snd_kcontrol_new wm8971_right_mixer_controls[] = {
176SOC_DAPM_SINGLE("Left Playback Switch", WM8971_ROUTM1, 8, 1, 0),
177SOC_DAPM_SINGLE("Left Bypass Switch", WM8971_ROUTM1, 7, 1, 0),
178SOC_DAPM_SINGLE("Playback Switch", WM8971_ROUTM2, 8, 1, 0),
179SOC_DAPM_SINGLE("Right Bypass Switch", WM8971_ROUTM2, 7, 1, 0),
180};
181
182/* Mono Mixer */
183static const struct snd_kcontrol_new wm8971_mono_mixer_controls[] = {
184SOC_DAPM_SINGLE("Left Playback Switch", WM8971_MOUTM1, 8, 1, 0),
185SOC_DAPM_SINGLE("Left Bypass Switch", WM8971_MOUTM1, 7, 1, 0),
186SOC_DAPM_SINGLE("Right Playback Switch", WM8971_MOUTM2, 8, 1, 0),
187SOC_DAPM_SINGLE("Right Bypass Switch", WM8971_MOUTM2, 7, 1, 0),
188};
189
190/* Left Line Mux */
191static const struct snd_kcontrol_new wm8971_left_line_controls =
192SOC_DAPM_ENUM("Route", wm8971_enum[8]);
193
194/* Right Line Mux */
195static const struct snd_kcontrol_new wm8971_right_line_controls =
196SOC_DAPM_ENUM("Route", wm8971_enum[9]);
197
198/* Left PGA Mux */
199static const struct snd_kcontrol_new wm8971_left_pga_controls =
200SOC_DAPM_ENUM("Route", wm8971_enum[10]);
201
202/* Right PGA Mux */
203static const struct snd_kcontrol_new wm8971_right_pga_controls =
204SOC_DAPM_ENUM("Route", wm8971_enum[11]);
205
206/* Mono ADC Mux */
207static const struct snd_kcontrol_new wm8971_monomux_controls =
208SOC_DAPM_ENUM("Route", wm8971_enum[13]);
209
210static const struct snd_soc_dapm_widget wm8971_dapm_widgets[] = {
211 SND_SOC_DAPM_MIXER("Left Mixer", SND_SOC_NOPM, 0, 0,
212 &wm8971_left_mixer_controls[0],
213 ARRAY_SIZE(wm8971_left_mixer_controls)),
214 SND_SOC_DAPM_MIXER("Right Mixer", SND_SOC_NOPM, 0, 0,
215 &wm8971_right_mixer_controls[0],
216 ARRAY_SIZE(wm8971_right_mixer_controls)),
217 SND_SOC_DAPM_MIXER("Mono Mixer", WM8971_PWR2, 2, 0,
218 &wm8971_mono_mixer_controls[0],
219 ARRAY_SIZE(wm8971_mono_mixer_controls)),
220
221 SND_SOC_DAPM_PGA("Right Out 2", WM8971_PWR2, 3, 0, NULL, 0),
222 SND_SOC_DAPM_PGA("Left Out 2", WM8971_PWR2, 4, 0, NULL, 0),
223 SND_SOC_DAPM_PGA("Right Out 1", WM8971_PWR2, 5, 0, NULL, 0),
224 SND_SOC_DAPM_PGA("Left Out 1", WM8971_PWR2, 6, 0, NULL, 0),
225 SND_SOC_DAPM_DAC("Right DAC", "Right Playback", WM8971_PWR2, 7, 0),
226 SND_SOC_DAPM_DAC("Left DAC", "Left Playback", WM8971_PWR2, 8, 0),
227 SND_SOC_DAPM_PGA("Mono Out 1", WM8971_PWR2, 2, 0, NULL, 0),
228
229 SND_SOC_DAPM_MICBIAS("Mic Bias", WM8971_PWR1, 1, 0),
230 SND_SOC_DAPM_ADC("Right ADC", "Right Capture", WM8971_PWR1, 2, 0),
231 SND_SOC_DAPM_ADC("Left ADC", "Left Capture", WM8971_PWR1, 3, 0),
232
233 SND_SOC_DAPM_MUX("Left PGA Mux", WM8971_PWR1, 5, 0,
234 &wm8971_left_pga_controls),
235 SND_SOC_DAPM_MUX("Right PGA Mux", WM8971_PWR1, 4, 0,
236 &wm8971_right_pga_controls),
237 SND_SOC_DAPM_MUX("Left Line Mux", SND_SOC_NOPM, 0, 0,
238 &wm8971_left_line_controls),
239 SND_SOC_DAPM_MUX("Right Line Mux", SND_SOC_NOPM, 0, 0,
240 &wm8971_right_line_controls),
241
242 SND_SOC_DAPM_MUX("Left ADC Mux", SND_SOC_NOPM, 0, 0,
243 &wm8971_monomux_controls),
244 SND_SOC_DAPM_MUX("Right ADC Mux", SND_SOC_NOPM, 0, 0,
245 &wm8971_monomux_controls),
246
247 SND_SOC_DAPM_OUTPUT("LOUT1"),
248 SND_SOC_DAPM_OUTPUT("ROUT1"),
249 SND_SOC_DAPM_OUTPUT("LOUT2"),
250 SND_SOC_DAPM_OUTPUT("ROUT2"),
251 SND_SOC_DAPM_OUTPUT("MONO"),
252
253 SND_SOC_DAPM_INPUT("LINPUT1"),
254 SND_SOC_DAPM_INPUT("RINPUT1"),
255 SND_SOC_DAPM_INPUT("MIC"),
256};
257
258static const struct snd_soc_dapm_route audio_map[] = {
259 /* left mixer */
260 {"Left Mixer", "Playback Switch", "Left DAC"},
261 {"Left Mixer", "Left Bypass Switch", "Left Line Mux"},
262 {"Left Mixer", "Right Playback Switch", "Right DAC"},
263 {"Left Mixer", "Right Bypass Switch", "Right Line Mux"},
264
265 /* right mixer */
266 {"Right Mixer", "Left Playback Switch", "Left DAC"},
267 {"Right Mixer", "Left Bypass Switch", "Left Line Mux"},
268 {"Right Mixer", "Playback Switch", "Right DAC"},
269 {"Right Mixer", "Right Bypass Switch", "Right Line Mux"},
270
271 /* left out 1 */
272 {"Left Out 1", NULL, "Left Mixer"},
273 {"LOUT1", NULL, "Left Out 1"},
274
275 /* left out 2 */
276 {"Left Out 2", NULL, "Left Mixer"},
277 {"LOUT2", NULL, "Left Out 2"},
278
279 /* right out 1 */
280 {"Right Out 1", NULL, "Right Mixer"},
281 {"ROUT1", NULL, "Right Out 1"},
282
283 /* right out 2 */
284 {"Right Out 2", NULL, "Right Mixer"},
285 {"ROUT2", NULL, "Right Out 2"},
286
287 /* mono mixer */
288 {"Mono Mixer", "Left Playback Switch", "Left DAC"},
289 {"Mono Mixer", "Left Bypass Switch", "Left Line Mux"},
290 {"Mono Mixer", "Right Playback Switch", "Right DAC"},
291 {"Mono Mixer", "Right Bypass Switch", "Right Line Mux"},
292
293 /* mono out */
294 {"Mono Out", NULL, "Mono Mixer"},
295 {"MONO1", NULL, "Mono Out"},
296
297 /* Left Line Mux */
298 {"Left Line Mux", "Line", "LINPUT1"},
299 {"Left Line Mux", "PGA", "Left PGA Mux"},
300 {"Left Line Mux", "Differential", "Differential Mux"},
301
302 /* Right Line Mux */
303 {"Right Line Mux", "Line", "RINPUT1"},
304 {"Right Line Mux", "Mic", "MIC"},
305 {"Right Line Mux", "PGA", "Right PGA Mux"},
306 {"Right Line Mux", "Differential", "Differential Mux"},
307
308 /* Left PGA Mux */
309 {"Left PGA Mux", "Line", "LINPUT1"},
310 {"Left PGA Mux", "Differential", "Differential Mux"},
311
312 /* Right PGA Mux */
313 {"Right PGA Mux", "Line", "RINPUT1"},
314 {"Right PGA Mux", "Differential", "Differential Mux"},
315
316 /* Differential Mux */
317 {"Differential Mux", "Line", "LINPUT1"},
318 {"Differential Mux", "Line", "RINPUT1"},
319
320 /* Left ADC Mux */
321 {"Left ADC Mux", "Stereo", "Left PGA Mux"},
322 {"Left ADC Mux", "Mono (Left)", "Left PGA Mux"},
323 {"Left ADC Mux", "Digital Mono", "Left PGA Mux"},
324
325 /* Right ADC Mux */
326 {"Right ADC Mux", "Stereo", "Right PGA Mux"},
327 {"Right ADC Mux", "Mono (Right)", "Right PGA Mux"},
328 {"Right ADC Mux", "Digital Mono", "Right PGA Mux"},
329
330 /* ADC */
331 {"Left ADC", NULL, "Left ADC Mux"},
332 {"Right ADC", NULL, "Right ADC Mux"},
333};
334
335static int wm8971_add_widgets(struct snd_soc_codec *codec)
336{
337 snd_soc_dapm_new_controls(codec, wm8971_dapm_widgets,
338 ARRAY_SIZE(wm8971_dapm_widgets));
339
340 snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
341
Kenneth Kiraly8bae3e22008-09-10 19:33:32 +0100342 return 0;
343}
344
345struct _coeff_div {
346 u32 mclk;
347 u32 rate;
348 u16 fs;
349 u8 sr:5;
350 u8 usb:1;
351};
352
353/* codec hifi mclk clock divider coefficients */
354static const struct _coeff_div coeff_div[] = {
355 /* 8k */
356 {12288000, 8000, 1536, 0x6, 0x0},
357 {11289600, 8000, 1408, 0x16, 0x0},
358 {18432000, 8000, 2304, 0x7, 0x0},
359 {16934400, 8000, 2112, 0x17, 0x0},
360 {12000000, 8000, 1500, 0x6, 0x1},
361
362 /* 11.025k */
363 {11289600, 11025, 1024, 0x18, 0x0},
364 {16934400, 11025, 1536, 0x19, 0x0},
365 {12000000, 11025, 1088, 0x19, 0x1},
366
367 /* 16k */
368 {12288000, 16000, 768, 0xa, 0x0},
369 {18432000, 16000, 1152, 0xb, 0x0},
370 {12000000, 16000, 750, 0xa, 0x1},
371
372 /* 22.05k */
373 {11289600, 22050, 512, 0x1a, 0x0},
374 {16934400, 22050, 768, 0x1b, 0x0},
375 {12000000, 22050, 544, 0x1b, 0x1},
376
377 /* 32k */
378 {12288000, 32000, 384, 0xc, 0x0},
379 {18432000, 32000, 576, 0xd, 0x0},
380 {12000000, 32000, 375, 0xa, 0x1},
381
382 /* 44.1k */
383 {11289600, 44100, 256, 0x10, 0x0},
384 {16934400, 44100, 384, 0x11, 0x0},
385 {12000000, 44100, 272, 0x11, 0x1},
386
387 /* 48k */
388 {12288000, 48000, 256, 0x0, 0x0},
389 {18432000, 48000, 384, 0x1, 0x0},
390 {12000000, 48000, 250, 0x0, 0x1},
391
392 /* 88.2k */
393 {11289600, 88200, 128, 0x1e, 0x0},
394 {16934400, 88200, 192, 0x1f, 0x0},
395 {12000000, 88200, 136, 0x1f, 0x1},
396
397 /* 96k */
398 {12288000, 96000, 128, 0xe, 0x0},
399 {18432000, 96000, 192, 0xf, 0x0},
400 {12000000, 96000, 125, 0xe, 0x1},
401};
402
403static int get_coeff(int mclk, int rate)
404{
405 int i;
406
407 for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
408 if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
409 return i;
410 }
411 return -EINVAL;
412}
413
414static int wm8971_set_dai_sysclk(struct snd_soc_dai *codec_dai,
415 int clk_id, unsigned int freq, int dir)
416{
417 struct snd_soc_codec *codec = codec_dai->codec;
418 struct wm8971_priv *wm8971 = codec->private_data;
419
420 switch (freq) {
421 case 11289600:
422 case 12000000:
423 case 12288000:
424 case 16934400:
425 case 18432000:
426 wm8971->sysclk = freq;
427 return 0;
428 }
429 return -EINVAL;
430}
431
432static int wm8971_set_dai_fmt(struct snd_soc_dai *codec_dai,
433 unsigned int fmt)
434{
435 struct snd_soc_codec *codec = codec_dai->codec;
436 u16 iface = 0;
437
438 /* set master/slave audio interface */
439 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
440 case SND_SOC_DAIFMT_CBM_CFM:
441 iface = 0x0040;
442 break;
443 case SND_SOC_DAIFMT_CBS_CFS:
444 break;
445 default:
446 return -EINVAL;
447 }
448
449 /* interface format */
450 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
451 case SND_SOC_DAIFMT_I2S:
452 iface |= 0x0002;
453 break;
454 case SND_SOC_DAIFMT_RIGHT_J:
455 break;
456 case SND_SOC_DAIFMT_LEFT_J:
457 iface |= 0x0001;
458 break;
459 case SND_SOC_DAIFMT_DSP_A:
460 iface |= 0x0003;
461 break;
462 case SND_SOC_DAIFMT_DSP_B:
463 iface |= 0x0013;
464 break;
465 default:
466 return -EINVAL;
467 }
468
469 /* clock inversion */
470 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
471 case SND_SOC_DAIFMT_NB_NF:
472 break;
473 case SND_SOC_DAIFMT_IB_IF:
474 iface |= 0x0090;
475 break;
476 case SND_SOC_DAIFMT_IB_NF:
477 iface |= 0x0080;
478 break;
479 case SND_SOC_DAIFMT_NB_IF:
480 iface |= 0x0010;
481 break;
482 default:
483 return -EINVAL;
484 }
485
Mark Brown17a52fd2009-07-05 17:24:50 +0100486 snd_soc_write(codec, WM8971_IFACE, iface);
Kenneth Kiraly8bae3e22008-09-10 19:33:32 +0100487 return 0;
488}
489
490static int wm8971_pcm_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000491 struct snd_pcm_hw_params *params,
492 struct snd_soc_dai *dai)
Kenneth Kiraly8bae3e22008-09-10 19:33:32 +0100493{
494 struct snd_soc_pcm_runtime *rtd = substream->private_data;
495 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown6627a652009-01-23 22:55:23 +0000496 struct snd_soc_codec *codec = socdev->card->codec;
Kenneth Kiraly8bae3e22008-09-10 19:33:32 +0100497 struct wm8971_priv *wm8971 = codec->private_data;
Mark Brown17a52fd2009-07-05 17:24:50 +0100498 u16 iface = snd_soc_read(codec, WM8971_IFACE) & 0x1f3;
499 u16 srate = snd_soc_read(codec, WM8971_SRATE) & 0x1c0;
Kenneth Kiraly8bae3e22008-09-10 19:33:32 +0100500 int coeff = get_coeff(wm8971->sysclk, params_rate(params));
501
502 /* bit size */
503 switch (params_format(params)) {
504 case SNDRV_PCM_FORMAT_S16_LE:
505 break;
506 case SNDRV_PCM_FORMAT_S20_3LE:
507 iface |= 0x0004;
508 break;
509 case SNDRV_PCM_FORMAT_S24_LE:
510 iface |= 0x0008;
511 break;
512 case SNDRV_PCM_FORMAT_S32_LE:
513 iface |= 0x000c;
514 break;
515 }
516
517 /* set iface & srate */
Mark Brown17a52fd2009-07-05 17:24:50 +0100518 snd_soc_write(codec, WM8971_IFACE, iface);
Kenneth Kiraly8bae3e22008-09-10 19:33:32 +0100519 if (coeff >= 0)
Mark Brown17a52fd2009-07-05 17:24:50 +0100520 snd_soc_write(codec, WM8971_SRATE, srate |
Kenneth Kiraly8bae3e22008-09-10 19:33:32 +0100521 (coeff_div[coeff].sr << 1) | coeff_div[coeff].usb);
522
523 return 0;
524}
525
526static int wm8971_mute(struct snd_soc_dai *dai, int mute)
527{
528 struct snd_soc_codec *codec = dai->codec;
Mark Brown17a52fd2009-07-05 17:24:50 +0100529 u16 mute_reg = snd_soc_read(codec, WM8971_ADCDAC) & 0xfff7;
Kenneth Kiraly8bae3e22008-09-10 19:33:32 +0100530
531 if (mute)
Mark Brown17a52fd2009-07-05 17:24:50 +0100532 snd_soc_write(codec, WM8971_ADCDAC, mute_reg | 0x8);
Kenneth Kiraly8bae3e22008-09-10 19:33:32 +0100533 else
Mark Brown17a52fd2009-07-05 17:24:50 +0100534 snd_soc_write(codec, WM8971_ADCDAC, mute_reg);
Kenneth Kiraly8bae3e22008-09-10 19:33:32 +0100535 return 0;
536}
537
538static int wm8971_set_bias_level(struct snd_soc_codec *codec,
539 enum snd_soc_bias_level level)
540{
Mark Brown17a52fd2009-07-05 17:24:50 +0100541 u16 pwr_reg = snd_soc_read(codec, WM8971_PWR1) & 0xfe3e;
Kenneth Kiraly8bae3e22008-09-10 19:33:32 +0100542
543 switch (level) {
544 case SND_SOC_BIAS_ON:
545 /* set vmid to 50k and unmute dac */
Mark Brown17a52fd2009-07-05 17:24:50 +0100546 snd_soc_write(codec, WM8971_PWR1, pwr_reg | 0x00c1);
Kenneth Kiraly8bae3e22008-09-10 19:33:32 +0100547 break;
548 case SND_SOC_BIAS_PREPARE:
549 break;
550 case SND_SOC_BIAS_STANDBY:
551 /* mute dac and set vmid to 500k, enable VREF */
Mark Brown17a52fd2009-07-05 17:24:50 +0100552 snd_soc_write(codec, WM8971_PWR1, pwr_reg | 0x0140);
Kenneth Kiraly8bae3e22008-09-10 19:33:32 +0100553 break;
554 case SND_SOC_BIAS_OFF:
Mark Brown17a52fd2009-07-05 17:24:50 +0100555 snd_soc_write(codec, WM8971_PWR1, 0x0001);
Kenneth Kiraly8bae3e22008-09-10 19:33:32 +0100556 break;
557 }
558 codec->bias_level = level;
559 return 0;
560}
561
562#define WM8971_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
563 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_44100 | \
564 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000)
565
566#define WM8971_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
567 SNDRV_PCM_FMTBIT_S24_LE)
568
Eric Miao6335d052009-03-03 09:41:00 +0800569static struct snd_soc_dai_ops wm8971_dai_ops = {
570 .hw_params = wm8971_pcm_hw_params,
571 .digital_mute = wm8971_mute,
572 .set_fmt = wm8971_set_dai_fmt,
573 .set_sysclk = wm8971_set_dai_sysclk,
574};
575
Kenneth Kiraly8bae3e22008-09-10 19:33:32 +0100576struct snd_soc_dai wm8971_dai = {
577 .name = "WM8971",
578 .playback = {
579 .stream_name = "Playback",
580 .channels_min = 1,
581 .channels_max = 2,
582 .rates = WM8971_RATES,
583 .formats = WM8971_FORMATS,},
584 .capture = {
585 .stream_name = "Capture",
586 .channels_min = 1,
587 .channels_max = 2,
588 .rates = WM8971_RATES,
589 .formats = WM8971_FORMATS,},
Eric Miao6335d052009-03-03 09:41:00 +0800590 .ops = &wm8971_dai_ops,
Kenneth Kiraly8bae3e22008-09-10 19:33:32 +0100591};
592EXPORT_SYMBOL_GPL(wm8971_dai);
593
594static void wm8971_work(struct work_struct *work)
595{
596 struct snd_soc_codec *codec =
597 container_of(work, struct snd_soc_codec, delayed_work.work);
598 wm8971_set_bias_level(codec, codec->bias_level);
599}
600
601static int wm8971_suspend(struct platform_device *pdev, pm_message_t state)
602{
603 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown6627a652009-01-23 22:55:23 +0000604 struct snd_soc_codec *codec = socdev->card->codec;
Kenneth Kiraly8bae3e22008-09-10 19:33:32 +0100605
606 wm8971_set_bias_level(codec, SND_SOC_BIAS_OFF);
607 return 0;
608}
609
610static int wm8971_resume(struct platform_device *pdev)
611{
612 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown6627a652009-01-23 22:55:23 +0000613 struct snd_soc_codec *codec = socdev->card->codec;
Kenneth Kiraly8bae3e22008-09-10 19:33:32 +0100614 int i;
615 u8 data[2];
616 u16 *cache = codec->reg_cache;
617 u16 reg;
618
619 /* Sync reg_cache with the hardware */
620 for (i = 0; i < ARRAY_SIZE(wm8971_reg); i++) {
621 if (i + 1 == WM8971_RESET)
622 continue;
623 data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001);
624 data[1] = cache[i] & 0x00ff;
625 codec->hw_write(codec->control_data, data, 2);
626 }
627
628 wm8971_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
629
630 /* charge wm8971 caps */
631 if (codec->suspend_bias_level == SND_SOC_BIAS_ON) {
Mark Brown17a52fd2009-07-05 17:24:50 +0100632 reg = snd_soc_read(codec, WM8971_PWR1) & 0xfe3e;
633 snd_soc_write(codec, WM8971_PWR1, reg | 0x01c0);
Kenneth Kiraly8bae3e22008-09-10 19:33:32 +0100634 codec->bias_level = SND_SOC_BIAS_ON;
635 queue_delayed_work(wm8971_workq, &codec->delayed_work,
636 msecs_to_jiffies(1000));
637 }
638
639 return 0;
640}
641
Mark Brown7084a422009-07-10 22:24:27 +0100642static int wm8971_init(struct snd_soc_device *socdev,
643 enum snd_soc_control_type control)
Kenneth Kiraly8bae3e22008-09-10 19:33:32 +0100644{
Mark Brown6627a652009-01-23 22:55:23 +0000645 struct snd_soc_codec *codec = socdev->card->codec;
Kenneth Kiraly8bae3e22008-09-10 19:33:32 +0100646 int reg, ret = 0;
647
648 codec->name = "WM8971";
649 codec->owner = THIS_MODULE;
Kenneth Kiraly8bae3e22008-09-10 19:33:32 +0100650 codec->set_bias_level = wm8971_set_bias_level;
651 codec->dai = &wm8971_dai;
652 codec->reg_cache_size = ARRAY_SIZE(wm8971_reg);
653 codec->num_dai = 1;
654 codec->reg_cache = kmemdup(wm8971_reg, sizeof(wm8971_reg), GFP_KERNEL);
655
656 if (codec->reg_cache == NULL)
657 return -ENOMEM;
658
Mark Brown7084a422009-07-10 22:24:27 +0100659 ret = snd_soc_codec_set_cache_io(codec, 7, 9, control);
Mark Brown17a52fd2009-07-05 17:24:50 +0100660 if (ret < 0) {
661 printk(KERN_ERR "wm8971: failed to set cache I/O: %d\n", ret);
662 goto err;
663 }
664
Kenneth Kiraly8bae3e22008-09-10 19:33:32 +0100665 wm8971_reset(codec);
666
667 /* register pcms */
668 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
669 if (ret < 0) {
670 printk(KERN_ERR "wm8971: failed to create pcms\n");
Mark Brown17a52fd2009-07-05 17:24:50 +0100671 goto err;
Kenneth Kiraly8bae3e22008-09-10 19:33:32 +0100672 }
673
674 /* charge output caps - set vmid to 5k for quick power up */
Mark Brown17a52fd2009-07-05 17:24:50 +0100675 reg = snd_soc_read(codec, WM8971_PWR1) & 0xfe3e;
676 snd_soc_write(codec, WM8971_PWR1, reg | 0x01c0);
Kenneth Kiraly8bae3e22008-09-10 19:33:32 +0100677 codec->bias_level = SND_SOC_BIAS_STANDBY;
678 queue_delayed_work(wm8971_workq, &codec->delayed_work,
679 msecs_to_jiffies(1000));
680
681 /* set the update bits */
Mark Brown17a52fd2009-07-05 17:24:50 +0100682 reg = snd_soc_read(codec, WM8971_LDAC);
683 snd_soc_write(codec, WM8971_LDAC, reg | 0x0100);
684 reg = snd_soc_read(codec, WM8971_RDAC);
685 snd_soc_write(codec, WM8971_RDAC, reg | 0x0100);
Kenneth Kiraly8bae3e22008-09-10 19:33:32 +0100686
Mark Brown17a52fd2009-07-05 17:24:50 +0100687 reg = snd_soc_read(codec, WM8971_LOUT1V);
688 snd_soc_write(codec, WM8971_LOUT1V, reg | 0x0100);
689 reg = snd_soc_read(codec, WM8971_ROUT1V);
690 snd_soc_write(codec, WM8971_ROUT1V, reg | 0x0100);
Kenneth Kiraly8bae3e22008-09-10 19:33:32 +0100691
Mark Brown17a52fd2009-07-05 17:24:50 +0100692 reg = snd_soc_read(codec, WM8971_LOUT2V);
693 snd_soc_write(codec, WM8971_LOUT2V, reg | 0x0100);
694 reg = snd_soc_read(codec, WM8971_ROUT2V);
695 snd_soc_write(codec, WM8971_ROUT2V, reg | 0x0100);
Kenneth Kiraly8bae3e22008-09-10 19:33:32 +0100696
Mark Brown17a52fd2009-07-05 17:24:50 +0100697 reg = snd_soc_read(codec, WM8971_LINVOL);
698 snd_soc_write(codec, WM8971_LINVOL, reg | 0x0100);
699 reg = snd_soc_read(codec, WM8971_RINVOL);
700 snd_soc_write(codec, WM8971_RINVOL, reg | 0x0100);
Kenneth Kiraly8bae3e22008-09-10 19:33:32 +0100701
Ian Molton3e8e1952009-01-09 00:23:21 +0000702 snd_soc_add_controls(codec, wm8971_snd_controls,
703 ARRAY_SIZE(wm8971_snd_controls));
Kenneth Kiraly8bae3e22008-09-10 19:33:32 +0100704 wm8971_add_widgets(codec);
Mark Brownfe3e78e2009-11-03 22:13:13 +0000705
Kenneth Kiraly8bae3e22008-09-10 19:33:32 +0100706 return ret;
707
Mark Brown17a52fd2009-07-05 17:24:50 +0100708err:
Kenneth Kiraly8bae3e22008-09-10 19:33:32 +0100709 kfree(codec->reg_cache);
710 return ret;
711}
712
713/* If the i2c layer weren't so broken, we could pass this kind of data
714 around */
715static struct snd_soc_device *wm8971_socdev;
716
717#if defined (CONFIG_I2C) || defined (CONFIG_I2C_MODULE)
718
719static int wm8971_i2c_probe(struct i2c_client *i2c,
720 const struct i2c_device_id *id)
721{
722 struct snd_soc_device *socdev = wm8971_socdev;
Mark Brown6627a652009-01-23 22:55:23 +0000723 struct snd_soc_codec *codec = socdev->card->codec;
Kenneth Kiraly8bae3e22008-09-10 19:33:32 +0100724 int ret;
725
726 i2c_set_clientdata(i2c, codec);
727
728 codec->control_data = i2c;
729
Mark Brown7084a422009-07-10 22:24:27 +0100730 ret = wm8971_init(socdev, SND_SOC_I2C);
Kenneth Kiraly8bae3e22008-09-10 19:33:32 +0100731 if (ret < 0)
732 pr_err("failed to initialise WM8971\n");
733
734 return ret;
735}
736
737static int wm8971_i2c_remove(struct i2c_client *client)
738{
739 struct snd_soc_codec *codec = i2c_get_clientdata(client);
740 kfree(codec->reg_cache);
741 return 0;
742}
743
744static const struct i2c_device_id wm8971_i2c_id[] = {
745 { "wm8971", 0 },
746 { }
747};
748MODULE_DEVICE_TABLE(i2c, wm8971_i2c_id);
749
750static struct i2c_driver wm8971_i2c_driver = {
751 .driver = {
752 .name = "WM8971 I2C Codec",
753 .owner = THIS_MODULE,
754 },
755 .probe = wm8971_i2c_probe,
756 .remove = wm8971_i2c_remove,
757 .id_table = wm8971_i2c_id,
758};
759
760static int wm8971_add_i2c_device(struct platform_device *pdev,
761 const struct wm8971_setup_data *setup)
762{
763 struct i2c_board_info info;
764 struct i2c_adapter *adapter;
765 struct i2c_client *client;
766 int ret;
767
768 ret = i2c_add_driver(&wm8971_i2c_driver);
769 if (ret != 0) {
770 dev_err(&pdev->dev, "can't add i2c driver\n");
771 return ret;
772 }
773
774 memset(&info, 0, sizeof(struct i2c_board_info));
775 info.addr = setup->i2c_address;
776 strlcpy(info.type, "wm8971", I2C_NAME_SIZE);
777
778 adapter = i2c_get_adapter(setup->i2c_bus);
779 if (!adapter) {
780 dev_err(&pdev->dev, "can't get i2c adapter %d\n",
781 setup->i2c_bus);
782 goto err_driver;
783 }
784
785 client = i2c_new_device(adapter, &info);
786 i2c_put_adapter(adapter);
787 if (!client) {
788 dev_err(&pdev->dev, "can't add i2c device at 0x%x\n",
789 (unsigned int)info.addr);
790 goto err_driver;
791 }
792
793 return 0;
794
795err_driver:
796 i2c_del_driver(&wm8971_i2c_driver);
797 return -ENODEV;
798}
799
800#endif
801
802static int wm8971_probe(struct platform_device *pdev)
803{
804 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
805 struct wm8971_setup_data *setup;
806 struct snd_soc_codec *codec;
807 struct wm8971_priv *wm8971;
808 int ret = 0;
809
810 pr_info("WM8971 Audio Codec %s", WM8971_VERSION);
811
812 setup = socdev->codec_data;
813 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
814 if (codec == NULL)
815 return -ENOMEM;
816
817 wm8971 = kzalloc(sizeof(struct wm8971_priv), GFP_KERNEL);
818 if (wm8971 == NULL) {
819 kfree(codec);
820 return -ENOMEM;
821 }
822
823 codec->private_data = wm8971;
Mark Brown6627a652009-01-23 22:55:23 +0000824 socdev->card->codec = codec;
Kenneth Kiraly8bae3e22008-09-10 19:33:32 +0100825 mutex_init(&codec->mutex);
826 INIT_LIST_HEAD(&codec->dapm_widgets);
827 INIT_LIST_HEAD(&codec->dapm_paths);
828 wm8971_socdev = socdev;
829
830 INIT_DELAYED_WORK(&codec->delayed_work, wm8971_work);
831 wm8971_workq = create_workqueue("wm8971");
832 if (wm8971_workq == NULL) {
833 kfree(codec->private_data);
834 kfree(codec);
835 return -ENOMEM;
836 }
837
838#if defined (CONFIG_I2C) || defined (CONFIG_I2C_MODULE)
839 if (setup->i2c_address) {
Kenneth Kiraly8bae3e22008-09-10 19:33:32 +0100840 ret = wm8971_add_i2c_device(pdev, setup);
841 }
842#endif
843 /* Add other interfaces here */
844
845 if (ret != 0) {
846 destroy_workqueue(wm8971_workq);
847 kfree(codec->private_data);
848 kfree(codec);
849 }
850
851 return ret;
852}
853
854/* power down chip */
855static int wm8971_remove(struct platform_device *pdev)
856{
857 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown6627a652009-01-23 22:55:23 +0000858 struct snd_soc_codec *codec = socdev->card->codec;
Kenneth Kiraly8bae3e22008-09-10 19:33:32 +0100859
860 if (codec->control_data)
861 wm8971_set_bias_level(codec, SND_SOC_BIAS_OFF);
862 if (wm8971_workq)
863 destroy_workqueue(wm8971_workq);
864 snd_soc_free_pcms(socdev);
865 snd_soc_dapm_free(socdev);
866#if defined (CONFIG_I2C) || defined (CONFIG_I2C_MODULE)
867 i2c_unregister_device(codec->control_data);
868 i2c_del_driver(&wm8971_i2c_driver);
869#endif
870 kfree(codec->private_data);
871 kfree(codec);
872
873 return 0;
874}
875
876struct snd_soc_codec_device soc_codec_dev_wm8971 = {
877 .probe = wm8971_probe,
878 .remove = wm8971_remove,
879 .suspend = wm8971_suspend,
880 .resume = wm8971_resume,
881};
882
883EXPORT_SYMBOL_GPL(soc_codec_dev_wm8971);
884
Takashi Iwaic9b3a402008-12-10 07:47:22 +0100885static int __init wm8971_modinit(void)
Mark Brown64089b82008-12-08 19:17:58 +0000886{
887 return snd_soc_register_dai(&wm8971_dai);
888}
889module_init(wm8971_modinit);
890
891static void __exit wm8971_exit(void)
892{
893 snd_soc_unregister_dai(&wm8971_dai);
894}
895module_exit(wm8971_exit);
896
Kenneth Kiraly8bae3e22008-09-10 19:33:32 +0100897MODULE_DESCRIPTION("ASoC WM8971 driver");
898MODULE_AUTHOR("Lab126");
899MODULE_LICENSE("GPL");