aboutsummaryrefslogtreecommitdiffstats
path: root/sound/soc/codecs/wm8978.c
diff options
context:
space:
mode:
Diffstat (limited to 'sound/soc/codecs/wm8978.c')
-rw-r--r--sound/soc/codecs/wm8978.c1124
1 files changed, 1124 insertions, 0 deletions
diff --git a/sound/soc/codecs/wm8978.c b/sound/soc/codecs/wm8978.c
new file mode 100644
index 000000000000..d9d4e9dd1adb
--- /dev/null
+++ b/sound/soc/codecs/wm8978.c
@@ -0,0 +1,1124 @@
1/*
2 * wm8978.c -- WM8978 ALSA SoC Audio Codec driver
3 *
4 * Copyright (C) 2009-2010 Guennadi Liakhovetski <g.liakhovetski@gmx.de>
5 * Copyright (C) 2007 Carlos Munoz <carlos@kenati.com>
6 * Copyright 2006-2009 Wolfson Microelectronics PLC.
7 * Based on wm8974 and wm8990 by Liam Girdwood <lrg@slimlogic.co.uk>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14#include <linux/module.h>
15#include <linux/moduleparam.h>
16#include <linux/kernel.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/core.h>
23#include <sound/pcm.h>
24#include <sound/pcm_params.h>
25#include <sound/soc.h>
26#include <sound/soc-dapm.h>
27#include <sound/initval.h>
28#include <sound/tlv.h>
29#include <asm/div64.h>
30
31#include "wm8978.h"
32
33static struct snd_soc_codec *wm8978_codec;
34
35/* wm8978 register cache. Note that register 0 is not included in the cache. */
36static const u16 wm8978_reg[WM8978_CACHEREGNUM] = {
37 0x0000, 0x0000, 0x0000, 0x0000, /* 0x00...0x03 */
38 0x0050, 0x0000, 0x0140, 0x0000, /* 0x04...0x07 */
39 0x0000, 0x0000, 0x0000, 0x00ff, /* 0x08...0x0b */
40 0x00ff, 0x0000, 0x0100, 0x00ff, /* 0x0c...0x0f */
41 0x00ff, 0x0000, 0x012c, 0x002c, /* 0x10...0x13 */
42 0x002c, 0x002c, 0x002c, 0x0000, /* 0x14...0x17 */
43 0x0032, 0x0000, 0x0000, 0x0000, /* 0x18...0x1b */
44 0x0000, 0x0000, 0x0000, 0x0000, /* 0x1c...0x1f */
45 0x0038, 0x000b, 0x0032, 0x0000, /* 0x20...0x23 */
46 0x0008, 0x000c, 0x0093, 0x00e9, /* 0x24...0x27 */
47 0x0000, 0x0000, 0x0000, 0x0000, /* 0x28...0x2b */
48 0x0033, 0x0010, 0x0010, 0x0100, /* 0x2c...0x2f */
49 0x0100, 0x0002, 0x0001, 0x0001, /* 0x30...0x33 */
50 0x0039, 0x0039, 0x0039, 0x0039, /* 0x34...0x37 */
51 0x0001, 0x0001, /* 0x38...0x3b */
52};
53
54/* codec private data */
55struct wm8978_priv {
56 struct snd_soc_codec codec;
57 unsigned int f_pllout;
58 unsigned int f_mclk;
59 unsigned int f_256fs;
60 unsigned int f_opclk;
61 enum wm8978_sysclk_src sysclk;
62 u16 reg_cache[WM8978_CACHEREGNUM];
63};
64
65static const char *wm8978_companding[] = {"Off", "NC", "u-law", "A-law"};
66static const char *wm8978_eqmode[] = {"Capture", "Playback"};
67static const char *wm8978_bw[] = {"Narrow", "Wide"};
68static const char *wm8978_eq1[] = {"80Hz", "105Hz", "135Hz", "175Hz"};
69static const char *wm8978_eq2[] = {"230Hz", "300Hz", "385Hz", "500Hz"};
70static const char *wm8978_eq3[] = {"650Hz", "850Hz", "1.1kHz", "1.4kHz"};
71static const char *wm8978_eq4[] = {"1.8kHz", "2.4kHz", "3.2kHz", "4.1kHz"};
72static const char *wm8978_eq5[] = {"5.3kHz", "6.9kHz", "9kHz", "11.7kHz"};
73static const char *wm8978_alc3[] = {"ALC", "Limiter"};
74static const char *wm8978_alc1[] = {"Off", "Right", "Left", "Both"};
75
76static const SOC_ENUM_SINGLE_DECL(adc_compand, WM8978_COMPANDING_CONTROL, 1,
77 wm8978_companding);
78static const SOC_ENUM_SINGLE_DECL(dac_compand, WM8978_COMPANDING_CONTROL, 3,
79 wm8978_companding);
80static const SOC_ENUM_SINGLE_DECL(eqmode, WM8978_EQ1, 8, wm8978_eqmode);
81static const SOC_ENUM_SINGLE_DECL(eq1, WM8978_EQ1, 5, wm8978_eq1);
82static const SOC_ENUM_SINGLE_DECL(eq2bw, WM8978_EQ2, 8, wm8978_bw);
83static const SOC_ENUM_SINGLE_DECL(eq2, WM8978_EQ2, 5, wm8978_eq2);
84static const SOC_ENUM_SINGLE_DECL(eq3bw, WM8978_EQ3, 8, wm8978_bw);
85static const SOC_ENUM_SINGLE_DECL(eq3, WM8978_EQ3, 5, wm8978_eq3);
86static const SOC_ENUM_SINGLE_DECL(eq4bw, WM8978_EQ4, 8, wm8978_bw);
87static const SOC_ENUM_SINGLE_DECL(eq4, WM8978_EQ4, 5, wm8978_eq4);
88static const SOC_ENUM_SINGLE_DECL(eq5, WM8978_EQ5, 5, wm8978_eq5);
89static const SOC_ENUM_SINGLE_DECL(alc3, WM8978_ALC_CONTROL_3, 8, wm8978_alc3);
90static const SOC_ENUM_SINGLE_DECL(alc1, WM8978_ALC_CONTROL_1, 7, wm8978_alc1);
91
92static const DECLARE_TLV_DB_SCALE(digital_tlv, -12750, 50, 1);
93static const DECLARE_TLV_DB_SCALE(eq_tlv, -1200, 100, 0);
94static const DECLARE_TLV_DB_SCALE(inpga_tlv, -1200, 75, 0);
95static const DECLARE_TLV_DB_SCALE(spk_tlv, -5700, 100, 0);
96static const DECLARE_TLV_DB_SCALE(boost_tlv, -1500, 300, 1);
97
98static const struct snd_kcontrol_new wm8978_snd_controls[] = {
99
100 SOC_SINGLE("Digital Loopback Switch",
101 WM8978_COMPANDING_CONTROL, 0, 1, 0),
102
103 SOC_ENUM("ADC Companding", adc_compand),
104 SOC_ENUM("DAC Companding", dac_compand),
105
106 SOC_DOUBLE("DAC Inversion Switch", WM8978_DAC_CONTROL, 0, 1, 1, 0),
107
108 SOC_DOUBLE_R_TLV("PCM Volume",
109 WM8978_LEFT_DAC_DIGITAL_VOLUME, WM8978_RIGHT_DAC_DIGITAL_VOLUME,
110 0, 255, 0, digital_tlv),
111
112 SOC_SINGLE("High Pass Filter Switch", WM8978_ADC_CONTROL, 8, 1, 0),
113 SOC_SINGLE("High Pass Cut Off", WM8978_ADC_CONTROL, 4, 7, 0),
114 SOC_DOUBLE("ADC Inversion Switch", WM8978_ADC_CONTROL, 0, 1, 1, 0),
115
116 SOC_DOUBLE_R_TLV("ADC Volume",
117 WM8978_LEFT_ADC_DIGITAL_VOLUME, WM8978_RIGHT_ADC_DIGITAL_VOLUME,
118 0, 255, 0, digital_tlv),
119
120 SOC_ENUM("Equaliser Function", eqmode),
121 SOC_ENUM("EQ1 Cut Off", eq1),
122 SOC_SINGLE_TLV("EQ1 Volume", WM8978_EQ1, 0, 24, 1, eq_tlv),
123
124 SOC_ENUM("Equaliser EQ2 Bandwith", eq2bw),
125 SOC_ENUM("EQ2 Cut Off", eq2),
126 SOC_SINGLE_TLV("EQ2 Volume", WM8978_EQ2, 0, 24, 1, eq_tlv),
127
128 SOC_ENUM("Equaliser EQ3 Bandwith", eq3bw),
129 SOC_ENUM("EQ3 Cut Off", eq3),
130 SOC_SINGLE_TLV("EQ3 Volume", WM8978_EQ3, 0, 24, 1, eq_tlv),
131
132 SOC_ENUM("Equaliser EQ4 Bandwith", eq4bw),
133 SOC_ENUM("EQ4 Cut Off", eq4),
134 SOC_SINGLE_TLV("EQ4 Volume", WM8978_EQ4, 0, 24, 1, eq_tlv),
135
136 SOC_ENUM("EQ5 Cut Off", eq5),
137 SOC_SINGLE_TLV("EQ5 Volume", WM8978_EQ5, 0, 24, 1, eq_tlv),
138
139 SOC_SINGLE("DAC Playback Limiter Switch",
140 WM8978_DAC_LIMITER_1, 8, 1, 0),
141 SOC_SINGLE("DAC Playback Limiter Decay",
142 WM8978_DAC_LIMITER_1, 4, 15, 0),
143 SOC_SINGLE("DAC Playback Limiter Attack",
144 WM8978_DAC_LIMITER_1, 0, 15, 0),
145
146 SOC_SINGLE("DAC Playback Limiter Threshold",
147 WM8978_DAC_LIMITER_2, 4, 7, 0),
148 SOC_SINGLE("DAC Playback Limiter Boost",
149 WM8978_DAC_LIMITER_2, 0, 15, 0),
150
151 SOC_ENUM("ALC Enable Switch", alc1),
152 SOC_SINGLE("ALC Capture Min Gain", WM8978_ALC_CONTROL_1, 0, 7, 0),
153 SOC_SINGLE("ALC Capture Max Gain", WM8978_ALC_CONTROL_1, 3, 7, 0),
154
155 SOC_SINGLE("ALC Capture Hold", WM8978_ALC_CONTROL_2, 4, 7, 0),
156 SOC_SINGLE("ALC Capture Target", WM8978_ALC_CONTROL_2, 0, 15, 0),
157
158 SOC_ENUM("ALC Capture Mode", alc3),
159 SOC_SINGLE("ALC Capture Decay", WM8978_ALC_CONTROL_3, 4, 15, 0),
160 SOC_SINGLE("ALC Capture Attack", WM8978_ALC_CONTROL_3, 0, 15, 0),
161
162 SOC_SINGLE("ALC Capture Noise Gate Switch", WM8978_NOISE_GATE, 3, 1, 0),
163 SOC_SINGLE("ALC Capture Noise Gate Threshold",
164 WM8978_NOISE_GATE, 0, 7, 0),
165
166 SOC_DOUBLE_R("Capture PGA ZC Switch",
167 WM8978_LEFT_INP_PGA_CONTROL, WM8978_RIGHT_INP_PGA_CONTROL,
168 7, 1, 0),
169
170 /* OUT1 - Headphones */
171 SOC_DOUBLE_R("Headphone Playback ZC Switch",
172 WM8978_LOUT1_HP_CONTROL, WM8978_ROUT1_HP_CONTROL, 7, 1, 0),
173
174 SOC_DOUBLE_R_TLV("Headphone Playback Volume",
175 WM8978_LOUT1_HP_CONTROL, WM8978_ROUT1_HP_CONTROL,
176 0, 63, 0, spk_tlv),
177
178 /* OUT2 - Speakers */
179 SOC_DOUBLE_R("Speaker Playback ZC Switch",
180 WM8978_LOUT2_SPK_CONTROL, WM8978_ROUT2_SPK_CONTROL, 7, 1, 0),
181
182 SOC_DOUBLE_R_TLV("Speaker Playback Volume",
183 WM8978_LOUT2_SPK_CONTROL, WM8978_ROUT2_SPK_CONTROL,
184 0, 63, 0, spk_tlv),
185
186 /* OUT3/4 - Line Output */
187 SOC_DOUBLE_R("Line Playback Switch",
188 WM8978_OUT3_MIXER_CONTROL, WM8978_OUT4_MIXER_CONTROL, 6, 1, 1),
189
190 /* Mixer #3: Boost (Input) mixer */
191 SOC_DOUBLE_R("PGA Boost (+20dB)",
192 WM8978_LEFT_ADC_BOOST_CONTROL, WM8978_RIGHT_ADC_BOOST_CONTROL,
193 8, 1, 0),
194 SOC_DOUBLE_R_TLV("L2/R2 Boost Volume",
195 WM8978_LEFT_ADC_BOOST_CONTROL, WM8978_RIGHT_ADC_BOOST_CONTROL,
196 4, 7, 0, boost_tlv),
197 SOC_DOUBLE_R_TLV("Aux Boost Volume",
198 WM8978_LEFT_ADC_BOOST_CONTROL, WM8978_RIGHT_ADC_BOOST_CONTROL,
199 0, 7, 0, boost_tlv),
200
201 /* Input PGA volume */
202 SOC_DOUBLE_R_TLV("Input PGA Volume",
203 WM8978_LEFT_INP_PGA_CONTROL, WM8978_RIGHT_INP_PGA_CONTROL,
204 0, 63, 0, inpga_tlv),
205
206 /* Headphone */
207 SOC_DOUBLE_R("Headphone Switch",
208 WM8978_LOUT1_HP_CONTROL, WM8978_ROUT1_HP_CONTROL, 6, 1, 1),
209
210 /* Speaker */
211 SOC_DOUBLE_R("Speaker Switch",
212 WM8978_LOUT2_SPK_CONTROL, WM8978_ROUT2_SPK_CONTROL, 6, 1, 1),
213};
214
215/* Mixer #1: Output (OUT1, OUT2) Mixer: mix AUX, Input mixer output and DAC */
216static const struct snd_kcontrol_new wm8978_left_out_mixer[] = {
217 SOC_DAPM_SINGLE("Line Bypass Switch", WM8978_LEFT_MIXER_CONTROL, 1, 1, 0),
218 SOC_DAPM_SINGLE("Aux Playback Switch", WM8978_LEFT_MIXER_CONTROL, 5, 1, 0),
219 SOC_DAPM_SINGLE("PCM Playback Switch", WM8978_LEFT_MIXER_CONTROL, 0, 1, 0),
220};
221
222static const struct snd_kcontrol_new wm8978_right_out_mixer[] = {
223 SOC_DAPM_SINGLE("Line Bypass Switch", WM8978_RIGHT_MIXER_CONTROL, 1, 1, 0),
224 SOC_DAPM_SINGLE("Aux Playback Switch", WM8978_RIGHT_MIXER_CONTROL, 5, 1, 0),
225 SOC_DAPM_SINGLE("PCM Playback Switch", WM8978_RIGHT_MIXER_CONTROL, 0, 1, 0),
226};
227
228/* OUT3/OUT4 Mixer not implemented */
229
230/* Mixer #2: Input PGA Mute */
231static const struct snd_kcontrol_new wm8978_left_input_mixer[] = {
232 SOC_DAPM_SINGLE("L2 Switch", WM8978_INPUT_CONTROL, 2, 1, 0),
233 SOC_DAPM_SINGLE("MicN Switch", WM8978_INPUT_CONTROL, 1, 1, 0),
234 SOC_DAPM_SINGLE("MicP Switch", WM8978_INPUT_CONTROL, 0, 1, 0),
235};
236static const struct snd_kcontrol_new wm8978_right_input_mixer[] = {
237 SOC_DAPM_SINGLE("R2 Switch", WM8978_INPUT_CONTROL, 6, 1, 0),
238 SOC_DAPM_SINGLE("MicN Switch", WM8978_INPUT_CONTROL, 5, 1, 0),
239 SOC_DAPM_SINGLE("MicP Switch", WM8978_INPUT_CONTROL, 4, 1, 0),
240};
241
242static const struct snd_soc_dapm_widget wm8978_dapm_widgets[] = {
243 SND_SOC_DAPM_DAC("Left DAC", "Left HiFi Playback",
244 WM8978_POWER_MANAGEMENT_3, 0, 0),
245 SND_SOC_DAPM_DAC("Right DAC", "Right HiFi Playback",
246 WM8978_POWER_MANAGEMENT_3, 1, 0),
247 SND_SOC_DAPM_ADC("Left ADC", "Left HiFi Capture",
248 WM8978_POWER_MANAGEMENT_2, 0, 0),
249 SND_SOC_DAPM_ADC("Right ADC", "Right HiFi Capture",
250 WM8978_POWER_MANAGEMENT_2, 1, 0),
251
252 /* Mixer #1: OUT1,2 */
253 SOC_MIXER_ARRAY("Left Output Mixer", WM8978_POWER_MANAGEMENT_3,
254 2, 0, wm8978_left_out_mixer),
255 SOC_MIXER_ARRAY("Right Output Mixer", WM8978_POWER_MANAGEMENT_3,
256 3, 0, wm8978_right_out_mixer),
257
258 SOC_MIXER_ARRAY("Left Input Mixer", WM8978_POWER_MANAGEMENT_2,
259 2, 0, wm8978_left_input_mixer),
260 SOC_MIXER_ARRAY("Right Input Mixer", WM8978_POWER_MANAGEMENT_2,
261 3, 0, wm8978_right_input_mixer),
262
263 SND_SOC_DAPM_PGA("Left Boost Mixer", WM8978_POWER_MANAGEMENT_2,
264 4, 0, NULL, 0),
265 SND_SOC_DAPM_PGA("Right Boost Mixer", WM8978_POWER_MANAGEMENT_2,
266 5, 0, NULL, 0),
267
268 SND_SOC_DAPM_PGA("Left Capture PGA", WM8978_LEFT_INP_PGA_CONTROL,
269 6, 1, NULL, 0),
270 SND_SOC_DAPM_PGA("Right Capture PGA", WM8978_RIGHT_INP_PGA_CONTROL,
271 6, 1, NULL, 0),
272
273 SND_SOC_DAPM_PGA("Left Headphone Out", WM8978_POWER_MANAGEMENT_2,
274 7, 0, NULL, 0),
275 SND_SOC_DAPM_PGA("Right Headphone Out", WM8978_POWER_MANAGEMENT_2,
276 8, 0, NULL, 0),
277
278 SND_SOC_DAPM_PGA("Left Speaker Out", WM8978_POWER_MANAGEMENT_3,
279 6, 0, NULL, 0),
280 SND_SOC_DAPM_PGA("Right Speaker Out", WM8978_POWER_MANAGEMENT_3,
281 5, 0, NULL, 0),
282
283 SND_SOC_DAPM_MIXER("OUT4 VMID", WM8978_POWER_MANAGEMENT_3,
284 8, 0, NULL, 0),
285
286 SND_SOC_DAPM_MICBIAS("Mic Bias", WM8978_POWER_MANAGEMENT_1, 4, 0),
287
288 SND_SOC_DAPM_INPUT("LMICN"),
289 SND_SOC_DAPM_INPUT("LMICP"),
290 SND_SOC_DAPM_INPUT("RMICN"),
291 SND_SOC_DAPM_INPUT("RMICP"),
292 SND_SOC_DAPM_INPUT("LAUX"),
293 SND_SOC_DAPM_INPUT("RAUX"),
294 SND_SOC_DAPM_INPUT("L2"),
295 SND_SOC_DAPM_INPUT("R2"),
296 SND_SOC_DAPM_OUTPUT("LHP"),
297 SND_SOC_DAPM_OUTPUT("RHP"),
298 SND_SOC_DAPM_OUTPUT("LSPK"),
299 SND_SOC_DAPM_OUTPUT("RSPK"),
300};
301
302static const struct snd_soc_dapm_route audio_map[] = {
303 /* Output mixer */
304 {"Right Output Mixer", "PCM Playback Switch", "Right DAC"},
305 {"Right Output Mixer", "Aux Playback Switch", "RAUX"},
306 {"Right Output Mixer", "Line Bypass Switch", "Right Boost Mixer"},
307
308 {"Left Output Mixer", "PCM Playback Switch", "Left DAC"},
309 {"Left Output Mixer", "Aux Playback Switch", "LAUX"},
310 {"Left Output Mixer", "Line Bypass Switch", "Left Boost Mixer"},
311
312 /* Outputs */
313 {"Right Headphone Out", NULL, "Right Output Mixer"},
314 {"RHP", NULL, "Right Headphone Out"},
315
316 {"Left Headphone Out", NULL, "Left Output Mixer"},
317 {"LHP", NULL, "Left Headphone Out"},
318
319 {"Right Speaker Out", NULL, "Right Output Mixer"},
320 {"RSPK", NULL, "Right Speaker Out"},
321
322 {"Left Speaker Out", NULL, "Left Output Mixer"},
323 {"LSPK", NULL, "Left Speaker Out"},
324
325 /* Boost Mixer */
326 {"Right ADC", NULL, "Right Boost Mixer"},
327
328 {"Right Boost Mixer", NULL, "RAUX"},
329 {"Right Boost Mixer", NULL, "Right Capture PGA"},
330 {"Right Boost Mixer", NULL, "R2"},
331
332 {"Left ADC", NULL, "Left Boost Mixer"},
333
334 {"Left Boost Mixer", NULL, "LAUX"},
335 {"Left Boost Mixer", NULL, "Left Capture PGA"},
336 {"Left Boost Mixer", NULL, "L2"},
337
338 /* Input PGA */
339 {"Right Capture PGA", NULL, "Right Input Mixer"},
340 {"Left Capture PGA", NULL, "Left Input Mixer"},
341
342 {"Right Input Mixer", "R2 Switch", "R2"},
343 {"Right Input Mixer", "MicN Switch", "RMICN"},
344 {"Right Input Mixer", "MicP Switch", "RMICP"},
345
346 {"Left Input Mixer", "L2 Switch", "L2"},
347 {"Left Input Mixer", "MicN Switch", "LMICN"},
348 {"Left Input Mixer", "MicP Switch", "LMICP"},
349};
350
351static int wm8978_add_widgets(struct snd_soc_codec *codec)
352{
353 snd_soc_dapm_new_controls(codec, wm8978_dapm_widgets,
354 ARRAY_SIZE(wm8978_dapm_widgets));
355
356 /* set up the WM8978 audio map */
357 snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
358
359 return 0;
360}
361
362/* PLL divisors */
363struct wm8978_pll_div {
364 u32 k;
365 u8 n;
366 u8 div2;
367};
368
369#define FIXED_PLL_SIZE (1 << 24)
370
371static void pll_factors(struct wm8978_pll_div *pll_div, unsigned int target,
372 unsigned int source)
373{
374 u64 k_part;
375 unsigned int k, n_div, n_mod;
376
377 n_div = target / source;
378 if (n_div < 6) {
379 source >>= 1;
380 pll_div->div2 = 1;
381 n_div = target / source;
382 } else {
383 pll_div->div2 = 0;
384 }
385
386 if (n_div < 6 || n_div > 12)
387 dev_warn(wm8978_codec->dev,
388 "WM8978 N value exceeds recommended range! N = %u\n",
389 n_div);
390
391 pll_div->n = n_div;
392 n_mod = target - source * n_div;
393 k_part = FIXED_PLL_SIZE * (long long)n_mod + source / 2;
394
395 do_div(k_part, source);
396
397 k = k_part & 0xFFFFFFFF;
398
399 pll_div->k = k;
400}
401/*
402 * Calculate internal frequencies and dividers, according to Figure 40
403 * "PLL and Clock Select Circuit" in WM8978 datasheet Rev. 2.6
404 */
405static int wm8978_configure_pll(struct snd_soc_codec *codec)
406{
407 struct wm8978_priv *wm8978 = codec->private_data;
408 struct wm8978_pll_div pll_div;
409 unsigned int f_opclk = wm8978->f_opclk, f_mclk = wm8978->f_mclk,
410 f_256fs = wm8978->f_256fs;
411 unsigned int f2, opclk_div;
412
413 if (!f_mclk)
414 return -EINVAL;
415
416 if (f_opclk) {
417 /*
418 * The user needs OPCLK. Choose OPCLKDIV to put
419 * 6 <= R = f2 / f1 < 13, 1 <= OPCLKDIV <= 4.
420 * f_opclk = f_mclk * prescale * R / 4 / OPCLKDIV, where
421 * prescale = 1, or prescale = 2. Prescale is calculated inside
422 * pll_factors(). We have to select f_PLLOUT, such that
423 * f_mclk * 3 / 4 <= f_PLLOUT < f_mclk * 13 / 4. Must be
424 * f_mclk * 3 / 16 <= f_opclk < f_mclk * 13 / 4.
425 */
426 if (16 * f_opclk < 3 * f_mclk || 4 * f_opclk >= 13 * f_mclk)
427 return -EINVAL;
428
429 if (4 * f_opclk < 3 * f_mclk)
430 /* Have to use OPCLKDIV */
431 opclk_div = (3 * f_mclk / 4 + f_opclk - 1) / f_opclk;
432 else
433 opclk_div = 1;
434
435 dev_dbg(codec->dev, "%s: OPCLKDIV=%d\n", __func__, opclk_div);
436
437 snd_soc_update_bits(codec, WM8978_GPIO_CONTROL, 0x30,
438 (opclk_div - 1) << 4);
439
440 wm8978->f_pllout = f_opclk * opclk_div;
441 } else if (f_256fs) {
442 /*
443 * Not using OPCLK, choose R:
444 * 6 <= R = f2 / f1 < 13, to put 1 <= MCLKDIV <= 12.
445 * f_256fs = f_mclk * prescale * R / 4 / MCLKDIV, where
446 * prescale = 1, or prescale = 2. Prescale is calculated inside
447 * pll_factors(). We have to select f_PLLOUT, such that
448 * f_mclk * 3 / 4 <= f_PLLOUT < f_mclk * 13 / 4. Must be
449 * f_mclk * 3 / 48 <= f_256fs < f_mclk * 13 / 4. This means MCLK
450 * must be 3.781MHz <= f_MCLK <= 32.768MHz
451 */
452 if (48 * f_256fs < 3 * f_mclk || 4 * f_256fs >= 13 * f_mclk)
453 return -EINVAL;
454
455 /*
456 * MCLKDIV will be selected in .hw_params(), just choose a
457 * suitable f_PLLOUT
458 */
459 if (4 * f_256fs < 3 * f_mclk)
460 /* Will have to use MCLKDIV */
461 wm8978->f_pllout = wm8978->f_mclk * 3 / 4;
462 else
463 wm8978->f_pllout = f_256fs;
464
465 /* GPIO1 into default mode as input - before configuring PLL */
466 snd_soc_update_bits(codec, WM8978_GPIO_CONTROL, 7, 0);
467 } else {
468 return -EINVAL;
469 }
470
471 f2 = wm8978->f_pllout * 4;
472
473 dev_dbg(codec->dev, "%s: f_MCLK=%uHz, f_PLLOUT=%uHz\n", __func__,
474 wm8978->f_mclk, wm8978->f_pllout);
475
476 pll_factors(&pll_div, f2, wm8978->f_mclk);
477
478 dev_dbg(codec->dev, "%s: calculated PLL N=0x%x, K=0x%x, div2=%d\n",
479 __func__, pll_div.n, pll_div.k, pll_div.div2);
480
481 /* Turn PLL off for configuration... */
482 snd_soc_update_bits(codec, WM8978_POWER_MANAGEMENT_1, 0x20, 0);
483
484 snd_soc_write(codec, WM8978_PLL_N, (pll_div.div2 << 4) | pll_div.n);
485 snd_soc_write(codec, WM8978_PLL_K1, pll_div.k >> 18);
486 snd_soc_write(codec, WM8978_PLL_K2, (pll_div.k >> 9) & 0x1ff);
487 snd_soc_write(codec, WM8978_PLL_K3, pll_div.k & 0x1ff);
488
489 /* ...and on again */
490 snd_soc_update_bits(codec, WM8978_POWER_MANAGEMENT_1, 0x20, 0x20);
491
492 if (f_opclk)
493 /* Output PLL (OPCLK) to GPIO1 */
494 snd_soc_update_bits(codec, WM8978_GPIO_CONTROL, 7, 4);
495
496 return 0;
497}
498
499/*
500 * Configure WM8978 clock dividers.
501 */
502static int wm8978_set_dai_clkdiv(struct snd_soc_dai *codec_dai,
503 int div_id, int div)
504{
505 struct snd_soc_codec *codec = codec_dai->codec;
506 struct wm8978_priv *wm8978 = codec->private_data;
507 int ret = 0;
508
509 switch (div_id) {
510 case WM8978_OPCLKRATE:
511 wm8978->f_opclk = div;
512
513 if (wm8978->f_mclk)
514 ret = wm8978_configure_pll(codec);
515 break;
516 case WM8978_MCLKDIV:
517 if (div & ~0xe0)
518 return -EINVAL;
519 snd_soc_update_bits(codec, WM8978_CLOCKING, 0xe0, div);
520 break;
521 case WM8978_ADCCLK:
522 if (div & ~8)
523 return -EINVAL;
524 snd_soc_update_bits(codec, WM8978_ADC_CONTROL, 8, div);
525 break;
526 case WM8978_DACCLK:
527 if (div & ~8)
528 return -EINVAL;
529 snd_soc_update_bits(codec, WM8978_DAC_CONTROL, 8, div);
530 break;
531 case WM8978_BCLKDIV:
532 if (div & ~0x1c)
533 return -EINVAL;
534 snd_soc_update_bits(codec, WM8978_CLOCKING, 0x1c, div);
535 break;
536 default:
537 return -EINVAL;
538 }
539
540 dev_dbg(codec->dev, "%s: ID %d, value %u\n", __func__, div_id, div);
541
542 return ret;
543}
544
545/*
546 * @freq: when .set_pll() us not used, freq is codec MCLK input frequency
547 */
548static int wm8978_set_dai_sysclk(struct snd_soc_dai *codec_dai, int clk_id,
549 unsigned int freq, int dir)
550{
551 struct snd_soc_codec *codec = codec_dai->codec;
552 struct wm8978_priv *wm8978 = codec->private_data;
553 int ret = 0;
554
555 dev_dbg(codec->dev, "%s: ID %d, freq %u\n", __func__, clk_id, freq);
556
557 if (freq) {
558 wm8978->f_mclk = freq;
559
560 /* Even if MCLK is used for system clock, might have to drive OPCLK */
561 if (wm8978->f_opclk)
562 ret = wm8978_configure_pll(codec);
563
564 /* Our sysclk is fixed to 256 * fs, will configure in .hw_params() */
565
566 if (!ret)
567 wm8978->sysclk = clk_id;
568 }
569
570 if (wm8978->sysclk == WM8978_PLL && (!freq || clk_id == WM8978_MCLK)) {
571 /* Clock CODEC directly from MCLK */
572 snd_soc_update_bits(codec, WM8978_CLOCKING, 0x100, 0);
573
574 /* GPIO1 into default mode as input - before configuring PLL */
575 snd_soc_update_bits(codec, WM8978_GPIO_CONTROL, 7, 0);
576
577 /* Turn off PLL */
578 snd_soc_update_bits(codec, WM8978_POWER_MANAGEMENT_1, 0x20, 0);
579 wm8978->sysclk = WM8978_MCLK;
580 wm8978->f_pllout = 0;
581 wm8978->f_opclk = 0;
582 }
583
584 return ret;
585}
586
587/*
588 * Set ADC and Voice DAC format.
589 */
590static int wm8978_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
591{
592 struct snd_soc_codec *codec = codec_dai->codec;
593 /*
594 * BCLK polarity mask = 0x100, LRC clock polarity mask = 0x80,
595 * Data Format mask = 0x18: all will be calculated anew
596 */
597 u16 iface = snd_soc_read(codec, WM8978_AUDIO_INTERFACE) & ~0x198;
598 u16 clk = snd_soc_read(codec, WM8978_CLOCKING);
599
600 dev_dbg(codec->dev, "%s\n", __func__);
601
602 /* set master/slave audio interface */
603 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
604 case SND_SOC_DAIFMT_CBM_CFM:
605 clk |= 1;
606 break;
607 case SND_SOC_DAIFMT_CBS_CFS:
608 clk &= ~1;
609 break;
610 default:
611 return -EINVAL;
612 }
613
614 /* interface format */
615 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
616 case SND_SOC_DAIFMT_I2S:
617 iface |= 0x10;
618 break;
619 case SND_SOC_DAIFMT_RIGHT_J:
620 break;
621 case SND_SOC_DAIFMT_LEFT_J:
622 iface |= 0x8;
623 break;
624 case SND_SOC_DAIFMT_DSP_A:
625 iface |= 0x18;
626 break;
627 default:
628 return -EINVAL;
629 }
630
631 /* clock inversion */
632 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
633 case SND_SOC_DAIFMT_NB_NF:
634 break;
635 case SND_SOC_DAIFMT_IB_IF:
636 iface |= 0x180;
637 break;
638 case SND_SOC_DAIFMT_IB_NF:
639 iface |= 0x100;
640 break;
641 case SND_SOC_DAIFMT_NB_IF:
642 iface |= 0x80;
643 break;
644 default:
645 return -EINVAL;
646 }
647
648 snd_soc_write(codec, WM8978_AUDIO_INTERFACE, iface);
649 snd_soc_write(codec, WM8978_CLOCKING, clk);
650
651 return 0;
652}
653
654/* MCLK dividers */
655static const int mclk_numerator[] = {1, 3, 2, 3, 4, 6, 8, 12};
656static const int mclk_denominator[] = {1, 2, 1, 1, 1, 1, 1, 1};
657
658/*
659 * Set PCM DAI bit size and sample rate.
660 */
661static int wm8978_hw_params(struct snd_pcm_substream *substream,
662 struct snd_pcm_hw_params *params,
663 struct snd_soc_dai *dai)
664{
665 struct snd_soc_pcm_runtime *rtd = substream->private_data;
666 struct snd_soc_device *socdev = rtd->socdev;
667 struct snd_soc_codec *codec = socdev->card->codec;
668 struct wm8978_priv *wm8978 = codec->private_data;
669 /* Word length mask = 0x60 */
670 u16 iface_ctl = snd_soc_read(codec, WM8978_AUDIO_INTERFACE) & ~0x60;
671 /* Sampling rate mask = 0xe (for filters) */
672 u16 add_ctl = snd_soc_read(codec, WM8978_ADDITIONAL_CONTROL) & ~0xe;
673 u16 clking = snd_soc_read(codec, WM8978_CLOCKING);
674 enum wm8978_sysclk_src current_clk_id = clking & 0x100 ?
675 WM8978_PLL : WM8978_MCLK;
676 unsigned int f_sel, diff, diff_best = INT_MAX;
677 int i, best = 0;
678
679 if (!wm8978->f_mclk)
680 return -EINVAL;
681
682 /* bit size */
683 switch (params_format(params)) {
684 case SNDRV_PCM_FORMAT_S16_LE:
685 break;
686 case SNDRV_PCM_FORMAT_S20_3LE:
687 iface_ctl |= 0x20;
688 break;
689 case SNDRV_PCM_FORMAT_S24_LE:
690 iface_ctl |= 0x40;
691 break;
692 case SNDRV_PCM_FORMAT_S32_LE:
693 iface_ctl |= 0x60;
694 break;
695 }
696
697 /* filter coefficient */
698 switch (params_rate(params)) {
699 case 8000:
700 add_ctl |= 0x5 << 1;
701 break;
702 case 11025:
703 add_ctl |= 0x4 << 1;
704 break;
705 case 16000:
706 add_ctl |= 0x3 << 1;
707 break;
708 case 22050:
709 add_ctl |= 0x2 << 1;
710 break;
711 case 32000:
712 add_ctl |= 0x1 << 1;
713 break;
714 case 44100:
715 case 48000:
716 break;
717 }
718
719 /* Sampling rate is known now, can configure the MCLK divider */
720 wm8978->f_256fs = params_rate(params) * 256;
721
722 if (wm8978->sysclk == WM8978_MCLK) {
723 f_sel = wm8978->f_mclk;
724 } else {
725 if (!wm8978->f_pllout) {
726 int ret = wm8978_configure_pll(codec);
727 if (ret < 0)
728 return ret;
729 }
730 f_sel = wm8978->f_pllout;
731 }
732
733 /*
734 * In some cases it is possible to reconfigure PLL to a higher frequency
735 * by raising OPCLKDIV, but normally OPCLK is configured to 256 * fs or
736 * 512 * fs, so, we should be fine.
737 */
738 if (f_sel < wm8978->f_256fs || f_sel > 12 * wm8978->f_256fs)
739 return -EINVAL;
740
741 for (i = 0; i < ARRAY_SIZE(mclk_numerator); i++) {
742 diff = abs(wm8978->f_256fs * 3 -
743 f_sel * 3 * mclk_denominator[i] / mclk_numerator[i]);
744
745 if (diff < diff_best) {
746 diff_best = diff;
747 best = i;
748 }
749
750 if (!diff)
751 break;
752 }
753
754 if (diff)
755 dev_warn(codec->dev, "Imprecise clock: %u%s\n",
756 f_sel * mclk_denominator[best] / mclk_numerator[best],
757 wm8978->sysclk == WM8978_MCLK ?
758 ", consider using PLL" : "");
759
760 dev_dbg(codec->dev, "%s: fmt %d, rate %u, MCLK divisor #%d\n", __func__,
761 params_format(params), params_rate(params), best);
762
763 /* MCLK divisor mask = 0xe0 */
764 snd_soc_update_bits(codec, WM8978_CLOCKING, 0xe0, best << 5);
765
766 snd_soc_write(codec, WM8978_AUDIO_INTERFACE, iface_ctl);
767 snd_soc_write(codec, WM8978_ADDITIONAL_CONTROL, add_ctl);
768
769 if (wm8978->sysclk != current_clk_id) {
770 if (wm8978->sysclk == WM8978_PLL)
771 /* Run CODEC from PLL instead of MCLK */
772 snd_soc_update_bits(codec, WM8978_CLOCKING,
773 0x100, 0x100);
774 else
775 /* Clock CODEC directly from MCLK */
776 snd_soc_update_bits(codec, WM8978_CLOCKING, 0x100, 0);
777 }
778
779 return 0;
780}
781
782static int wm8978_mute(struct snd_soc_dai *dai, int mute)
783{
784 struct snd_soc_codec *codec = dai->codec;
785
786 dev_dbg(codec->dev, "%s: %d\n", __func__, mute);
787
788 if (mute)
789 snd_soc_update_bits(codec, WM8978_DAC_CONTROL, 0x40, 0x40);
790 else
791 snd_soc_update_bits(codec, WM8978_DAC_CONTROL, 0x40, 0);
792
793 return 0;
794}
795
796static int wm8978_set_bias_level(struct snd_soc_codec *codec,
797 enum snd_soc_bias_level level)
798{
799 u16 power1 = snd_soc_read(codec, WM8978_POWER_MANAGEMENT_1) & ~3;
800
801 switch (level) {
802 case SND_SOC_BIAS_ON:
803 case SND_SOC_BIAS_PREPARE:
804 power1 |= 1; /* VMID 75k */
805 snd_soc_write(codec, WM8978_POWER_MANAGEMENT_1, power1);
806 break;
807 case SND_SOC_BIAS_STANDBY:
808 /* bit 3: enable bias, bit 2: enable I/O tie off buffer */
809 power1 |= 0xc;
810
811 if (codec->bias_level == SND_SOC_BIAS_OFF) {
812 /* Initial cap charge at VMID 5k */
813 snd_soc_write(codec, WM8978_POWER_MANAGEMENT_1,
814 power1 | 0x3);
815 mdelay(100);
816 }
817
818 power1 |= 0x2; /* VMID 500k */
819 snd_soc_write(codec, WM8978_POWER_MANAGEMENT_1, power1);
820 break;
821 case SND_SOC_BIAS_OFF:
822 /* Preserve PLL - OPCLK may be used by someone */
823 snd_soc_update_bits(codec, WM8978_POWER_MANAGEMENT_1, ~0x20, 0);
824 snd_soc_write(codec, WM8978_POWER_MANAGEMENT_2, 0);
825 snd_soc_write(codec, WM8978_POWER_MANAGEMENT_3, 0);
826 break;
827 }
828
829 dev_dbg(codec->dev, "%s: %d, %x\n", __func__, level, power1);
830
831 codec->bias_level = level;
832 return 0;
833}
834
835#define WM8978_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
836 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
837
838static struct snd_soc_dai_ops wm8978_dai_ops = {
839 .hw_params = wm8978_hw_params,
840 .digital_mute = wm8978_mute,
841 .set_fmt = wm8978_set_dai_fmt,
842 .set_clkdiv = wm8978_set_dai_clkdiv,
843 .set_sysclk = wm8978_set_dai_sysclk,
844};
845
846/* Also supports 12kHz */
847struct snd_soc_dai wm8978_dai = {
848 .name = "WM8978 HiFi",
849 .id = 1,
850 .playback = {
851 .stream_name = "Playback",
852 .channels_min = 1,
853 .channels_max = 2,
854 .rates = SNDRV_PCM_RATE_8000_48000,
855 .formats = WM8978_FORMATS,
856 },
857 .capture = {
858 .stream_name = "Capture",
859 .channels_min = 1,
860 .channels_max = 2,
861 .rates = SNDRV_PCM_RATE_8000_48000,
862 .formats = WM8978_FORMATS,
863 },
864 .ops = &wm8978_dai_ops,
865};
866EXPORT_SYMBOL_GPL(wm8978_dai);
867
868static int wm8978_suspend(struct platform_device *pdev, pm_message_t state)
869{
870 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
871 struct snd_soc_codec *codec = socdev->card->codec;
872
873 wm8978_set_bias_level(codec, SND_SOC_BIAS_OFF);
874 /* Also switch PLL off */
875 snd_soc_write(codec, WM8978_POWER_MANAGEMENT_1, 0);
876 /* Put to sleep */
877 snd_soc_write(codec, WM8978_POWER_MANAGEMENT_2, 0x40);
878
879 return 0;
880}
881
882static int wm8978_resume(struct platform_device *pdev)
883{
884 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
885 struct snd_soc_codec *codec = socdev->card->codec;
886 struct wm8978_priv *wm8978 = codec->private_data;
887 int i;
888 u16 *cache = codec->reg_cache;
889
890 /* Wake up the codec */
891 snd_soc_write(codec, WM8978_POWER_MANAGEMENT_2, 0);
892
893 /* Sync reg_cache with the hardware */
894 for (i = 0; i < ARRAY_SIZE(wm8978_reg); i++) {
895 if (i == WM8978_RESET)
896 continue;
897 if (cache[i] != wm8978_reg[i])
898 snd_soc_write(codec, i, cache[i]);
899 }
900
901 wm8978_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
902
903 if (wm8978->f_pllout)
904 /* Switch PLL on */
905 snd_soc_update_bits(codec, WM8978_POWER_MANAGEMENT_1, 0x20, 0x20);
906
907 return 0;
908}
909
910static int wm8978_probe(struct platform_device *pdev)
911{
912 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
913 struct snd_soc_codec *codec;
914 int ret = 0;
915
916 if (wm8978_codec == NULL) {
917 dev_err(&pdev->dev, "Codec device not registered\n");
918 return -ENODEV;
919 }
920
921 socdev->card->codec = wm8978_codec;
922 codec = wm8978_codec;
923
924 /* register pcms */
925 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
926 if (ret < 0) {
927 dev_err(codec->dev, "failed to create pcms: %d\n", ret);
928 goto pcm_err;
929 }
930
931 snd_soc_add_controls(codec, wm8978_snd_controls,
932 ARRAY_SIZE(wm8978_snd_controls));
933 wm8978_add_widgets(codec);
934
935pcm_err:
936 return ret;
937}
938
939/* power down chip */
940static int wm8978_remove(struct platform_device *pdev)
941{
942 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
943
944 snd_soc_free_pcms(socdev);
945 snd_soc_dapm_free(socdev);
946
947 return 0;
948}
949
950struct snd_soc_codec_device soc_codec_dev_wm8978 = {
951 .probe = wm8978_probe,
952 .remove = wm8978_remove,
953 .suspend = wm8978_suspend,
954 .resume = wm8978_resume,
955};
956EXPORT_SYMBOL_GPL(soc_codec_dev_wm8978);
957
958/*
959 * These registers contain an "update" bit - bit 8. This means, for example,
960 * that one can write new DAC digital volume for both channels, but only when
961 * the update bit is set, will also the volume be updated - simultaneously for
962 * both channels.
963 */
964static const int update_reg[] = {
965 WM8978_LEFT_DAC_DIGITAL_VOLUME,
966 WM8978_RIGHT_DAC_DIGITAL_VOLUME,
967 WM8978_LEFT_ADC_DIGITAL_VOLUME,
968 WM8978_RIGHT_ADC_DIGITAL_VOLUME,
969 WM8978_LEFT_INP_PGA_CONTROL,
970 WM8978_RIGHT_INP_PGA_CONTROL,
971 WM8978_LOUT1_HP_CONTROL,
972 WM8978_ROUT1_HP_CONTROL,
973 WM8978_LOUT2_SPK_CONTROL,
974 WM8978_ROUT2_SPK_CONTROL,
975};
976
977static __devinit int wm8978_register(struct wm8978_priv *wm8978)
978{
979 int ret, i;
980 struct snd_soc_codec *codec = &wm8978->codec;
981
982 if (wm8978_codec) {
983 dev_err(codec->dev, "Another WM8978 is registered\n");
984 return -EINVAL;
985 }
986
987 /*
988 * Set default system clock to PLL, it is more precise, this is also the
989 * default hardware setting
990 */
991 wm8978->sysclk = WM8978_PLL;
992
993 mutex_init(&codec->mutex);
994 INIT_LIST_HEAD(&codec->dapm_widgets);
995 INIT_LIST_HEAD(&codec->dapm_paths);
996
997 codec->private_data = wm8978;
998 codec->name = "WM8978";
999 codec->owner = THIS_MODULE;
1000 codec->bias_level = SND_SOC_BIAS_OFF;
1001 codec->set_bias_level = wm8978_set_bias_level;
1002 codec->dai = &wm8978_dai;
1003 codec->num_dai = 1;
1004 codec->reg_cache_size = WM8978_CACHEREGNUM;
1005 codec->reg_cache = &wm8978->reg_cache;
1006
1007 ret = snd_soc_codec_set_cache_io(codec, 7, 9, SND_SOC_I2C);
1008 if (ret < 0) {
1009 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
1010 goto err;
1011 }
1012
1013 memcpy(codec->reg_cache, wm8978_reg, sizeof(wm8978_reg));
1014
1015 /*
1016 * Set the update bit in all registers, that have one. This way all
1017 * writes to those registers will also cause the update bit to be
1018 * written.
1019 */
1020 for (i = 0; i < ARRAY_SIZE(update_reg); i++)
1021 ((u16 *)codec->reg_cache)[update_reg[i]] |= 0x100;
1022
1023 /* Reset the codec */
1024 ret = snd_soc_write(codec, WM8978_RESET, 0);
1025 if (ret < 0) {
1026 dev_err(codec->dev, "Failed to issue reset\n");
1027 goto err;
1028 }
1029
1030 wm8978_dai.dev = codec->dev;
1031
1032 wm8978_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1033
1034 wm8978_codec = codec;
1035
1036 ret = snd_soc_register_codec(codec);
1037 if (ret != 0) {
1038 dev_err(codec->dev, "Failed to register codec: %d\n", ret);
1039 goto err;
1040 }
1041
1042 ret = snd_soc_register_dai(&wm8978_dai);
1043 if (ret != 0) {
1044 dev_err(codec->dev, "Failed to register DAI: %d\n", ret);
1045 goto err_codec;
1046 }
1047
1048 return 0;
1049
1050err_codec:
1051 snd_soc_unregister_codec(codec);
1052err:
1053 kfree(wm8978);
1054 return ret;
1055}
1056
1057static __devexit void wm8978_unregister(struct wm8978_priv *wm8978)
1058{
1059 wm8978_set_bias_level(&wm8978->codec, SND_SOC_BIAS_OFF);
1060 snd_soc_unregister_dai(&wm8978_dai);
1061 snd_soc_unregister_codec(&wm8978->codec);
1062 kfree(wm8978);
1063 wm8978_codec = NULL;
1064}
1065
1066static __devinit int wm8978_i2c_probe(struct i2c_client *i2c,
1067 const struct i2c_device_id *id)
1068{
1069 struct wm8978_priv *wm8978;
1070 struct snd_soc_codec *codec;
1071
1072 wm8978 = kzalloc(sizeof(struct wm8978_priv), GFP_KERNEL);
1073 if (wm8978 == NULL)
1074 return -ENOMEM;
1075
1076 codec = &wm8978->codec;
1077 codec->hw_write = (hw_write_t)i2c_master_send;
1078
1079 i2c_set_clientdata(i2c, wm8978);
1080 codec->control_data = i2c;
1081
1082 codec->dev = &i2c->dev;
1083
1084 return wm8978_register(wm8978);
1085}
1086
1087static __devexit int wm8978_i2c_remove(struct i2c_client *client)
1088{
1089 struct wm8978_priv *wm8978 = i2c_get_clientdata(client);
1090 wm8978_unregister(wm8978);
1091 return 0;
1092}
1093
1094static const struct i2c_device_id wm8978_i2c_id[] = {
1095 { "wm8978", 0 },
1096 { }
1097};
1098MODULE_DEVICE_TABLE(i2c, wm8978_i2c_id);
1099
1100static struct i2c_driver wm8978_i2c_driver = {
1101 .driver = {
1102 .name = "WM8978",
1103 .owner = THIS_MODULE,
1104 },
1105 .probe = wm8978_i2c_probe,
1106 .remove = __devexit_p(wm8978_i2c_remove),
1107 .id_table = wm8978_i2c_id,
1108};
1109
1110static int __init wm8978_modinit(void)
1111{
1112 return i2c_add_driver(&wm8978_i2c_driver);
1113}
1114module_init(wm8978_modinit);
1115
1116static void __exit wm8978_exit(void)
1117{
1118 i2c_del_driver(&wm8978_i2c_driver);
1119}
1120module_exit(wm8978_exit);
1121
1122MODULE_DESCRIPTION("ASoC WM8978 codec driver");
1123MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
1124MODULE_LICENSE("GPL");
s="hl opt">, 0x0422, 0x0844, 0x1088, 0x01b0, 0x44e0, 0x23c0, 0xed80, 0x1011, 0x0116, 0x022c, 0x0458, 0x08b0, 0x8c60, 0x2740, 0x4e80, 0x0411, 0x0822, 0x1044, 0x0158, 0x02b0, 0x2360, 0x46c0, 0xab80, 0x0811, 0x1022, 0x012c, 0x0258, 0x04b0, 0x4660, 0x8cc0, 0x2780, 0x2071, 0x40e2, 0xa0c4, 0x0108, 0x0210, 0x0420, 0x0840, 0x1080, 0x4071, 0x80e2, 0x0104, 0x0208, 0x0410, 0x0820, 0x1040, 0x2080, 0x8071, 0x0102, 0x0204, 0x0408, 0x0810, 0x1020, 0x2040, 0x4080, 0x019d, 0x03d6, 0x136c, 0x2198, 0x50b0, 0xb2e0, 0x0740, 0x0e80, 0x0189, 0x03ea, 0x072c, 0x0e58, 0x1cb0, 0x56e0, 0x37c0, 0xf580, 0x01fd, 0x0376, 0x06ec, 0x0bb8, 0x1110, 0x2220, 0x4440, 0x8880, 0x0163, 0x02c6, 0x1104, 0x0758, 0x0eb0, 0x2be0, 0x6140, 0xc280, 0x02fd, 0x01c6, 0x0b5c, 0x1108, 0x07b0, 0x25a0, 0x8840, 0x6180, 0x0801, 0x012e, 0x025c, 0x04b8, 0x1370, 0x26e0, 0x57c0, 0xb580, 0x0401, 0x0802, 0x015c, 0x02b8, 0x22b0, 0x13e0, 0x7140, 0xe280, 0x0201, 0x0402, 0x0804, 0x01b8, 0x11b0, 0x31a0, 0x8040, 0x7180, 0x0101, 0x0202, 0x0404, 0x0808, 0x1010, 0x2020, 0x4040, 0x8080, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080, 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000, 0x8000, }; static int decode_syndrome(u16 syndrome, u16 *vectors, int num_vecs, int v_dim) { unsigned int i, err_sym; for (err_sym = 0; err_sym < num_vecs / v_dim; err_sym++) { u16 s = syndrome; int v_idx = err_sym * v_dim; int v_end = (err_sym + 1) * v_dim; /* walk over all 16 bits of the syndrome */ for (i = 1; i < (1U << 16); i <<= 1) { /* if bit is set in that eigenvector... */ if (v_idx < v_end && vectors[v_idx] & i) { u16 ev_comp = vectors[v_idx++]; /* ... and bit set in the modified syndrome, */ if (s & i) { /* remove it. */ s ^= ev_comp; if (!s) return err_sym; } } else if (s & i) /* can't get to zero, move to next symbol */ break; } } debugf0("syndrome(%x) not found\n", syndrome); return -1; } static int map_err_sym_to_channel(int err_sym, int sym_size) { if (sym_size == 4) switch (err_sym) { case 0x20: case 0x21: return 0; break; case 0x22: case 0x23: return 1; break; default: return err_sym >> 4; break; } /* x8 symbols */ else switch (err_sym) { /* imaginary bits not in a DIMM */ case 0x10: WARN(1, KERN_ERR "Invalid error symbol: 0x%x\n", err_sym); return -1; break; case 0x11: return 0; break; case 0x12: return 1; break; default: return err_sym >> 3; break; } return -1; } static int get_channel_from_ecc_syndrome(struct mem_ctl_info *mci, u16 syndrome) { struct amd64_pvt *pvt = mci->pvt_info; int err_sym = -1; if (pvt->syn_type == 8) err_sym = decode_syndrome(syndrome, x8_vectors, ARRAY_SIZE(x8_vectors), pvt->syn_type); else if (pvt->syn_type == 4) err_sym = decode_syndrome(syndrome, x4_vectors, ARRAY_SIZE(x4_vectors), pvt->syn_type); else { amd64_printk(KERN_WARNING, "%s: Illegal syndrome type: %u\n", __func__, pvt->syn_type); return err_sym; } return map_err_sym_to_channel(err_sym, pvt->syn_type); } /* * Handle any Correctable Errors (CEs) that have occurred. Check for valid ERROR * ADDRESS and process. */ static void amd64_handle_ce(struct mem_ctl_info *mci, struct err_regs *info) { struct amd64_pvt *pvt = mci->pvt_info; u64 sys_addr; /* Ensure that the Error Address is VALID */ if ((info->nbsh & K8_NBSH_VALID_ERROR_ADDR) == 0) { amd64_mc_printk(mci, KERN_ERR, "HW has no ERROR_ADDRESS available\n"); edac_mc_handle_ce_no_info(mci, EDAC_MOD_STR); return; } sys_addr = pvt->ops->get_error_address(mci, info); amd64_mc_printk(mci, KERN_ERR, "CE ERROR_ADDRESS= 0x%llx\n", sys_addr); pvt->ops->map_sysaddr_to_csrow(mci, info, sys_addr); } /* Handle any Un-correctable Errors (UEs) */ static void amd64_handle_ue(struct mem_ctl_info *mci, struct err_regs *info) { struct amd64_pvt *pvt = mci->pvt_info; struct mem_ctl_info *log_mci, *src_mci = NULL; int csrow; u64 sys_addr; u32 page, offset; log_mci = mci; if ((info->nbsh & K8_NBSH_VALID_ERROR_ADDR) == 0) { amd64_mc_printk(mci, KERN_CRIT, "HW has no ERROR_ADDRESS available\n"); edac_mc_handle_ue_no_info(log_mci, EDAC_MOD_STR); return; } sys_addr = pvt->ops->get_error_address(mci, info); /* * Find out which node the error address belongs to. This may be * different from the node that detected the error. */ src_mci = find_mc_by_sys_addr(mci, sys_addr); if (!src_mci) { amd64_mc_printk(mci, KERN_CRIT, "ERROR ADDRESS (0x%lx) value NOT mapped to a MC\n", (unsigned long)sys_addr); edac_mc_handle_ue_no_info(log_mci, EDAC_MOD_STR); return; } log_mci = src_mci; csrow = sys_addr_to_csrow(log_mci, sys_addr); if (csrow < 0) { amd64_mc_printk(mci, KERN_CRIT, "ERROR_ADDRESS (0x%lx) value NOT mapped to 'csrow'\n", (unsigned long)sys_addr); edac_mc_handle_ue_no_info(log_mci, EDAC_MOD_STR); } else { error_address_to_page_and_offset(sys_addr, &page, &offset); edac_mc_handle_ue(log_mci, page, offset, csrow, EDAC_MOD_STR); } } static inline void __amd64_decode_bus_error(struct mem_ctl_info *mci, struct err_regs *info) { u32 ec = ERROR_CODE(info->nbsl); u32 xec = EXT_ERROR_CODE(info->nbsl); int ecc_type = (info->nbsh >> 13) & 0x3; /* Bail early out if this was an 'observed' error */ if (PP(ec) == K8_NBSL_PP_OBS) return; /* Do only ECC errors */ if (xec && xec != F10_NBSL_EXT_ERR_ECC) return; if (ecc_type == 2) amd64_handle_ce(mci, info); else if (ecc_type == 1) amd64_handle_ue(mci, info); } void amd64_decode_bus_error(int node_id, struct mce *m, u32 nbcfg) { struct mem_ctl_info *mci = mci_lookup[node_id]; struct err_regs regs; regs.nbsl = (u32) m->status; regs.nbsh = (u32)(m->status >> 32); regs.nbeal = (u32) m->addr; regs.nbeah = (u32)(m->addr >> 32); regs.nbcfg = nbcfg; __amd64_decode_bus_error(mci, &regs); /* * Check the UE bit of the NB status high register, if set generate some * logs. If NOT a GART error, then process the event as a NO-INFO event. * If it was a GART error, skip that process. * * FIXME: this should go somewhere else, if at all. */ if (regs.nbsh & K8_NBSH_UC_ERR && !report_gart_errors) edac_mc_handle_ue_no_info(mci, "UE bit is set"); } /* * Input: * 1) struct amd64_pvt which contains pvt->dram_f2_ctl pointer * 2) AMD Family index value * * Ouput: * Upon return of 0, the following filled in: * * struct pvt->addr_f1_ctl * struct pvt->misc_f3_ctl * * Filled in with related device funcitions of 'dram_f2_ctl' * These devices are "reserved" via the pci_get_device() * * Upon return of 1 (error status): * * Nothing reserved */ static int amd64_reserve_mc_sibling_devices(struct amd64_pvt *pvt, int mc_idx) { const struct amd64_family_type *amd64_dev = &amd64_family_types[mc_idx]; /* Reserve the ADDRESS MAP Device */ pvt->addr_f1_ctl = pci_get_related_function(pvt->dram_f2_ctl->vendor, amd64_dev->addr_f1_ctl, pvt->dram_f2_ctl); if (!pvt->addr_f1_ctl) { amd64_printk(KERN_ERR, "error address map device not found: " "vendor %x device 0x%x (broken BIOS?)\n", PCI_VENDOR_ID_AMD, amd64_dev->addr_f1_ctl); return 1; } /* Reserve the MISC Device */ pvt->misc_f3_ctl = pci_get_related_function(pvt->dram_f2_ctl->vendor, amd64_dev->misc_f3_ctl, pvt->dram_f2_ctl); if (!pvt->misc_f3_ctl) { pci_dev_put(pvt->addr_f1_ctl); pvt->addr_f1_ctl = NULL; amd64_printk(KERN_ERR, "error miscellaneous device not found: " "vendor %x device 0x%x (broken BIOS?)\n", PCI_VENDOR_ID_AMD, amd64_dev->misc_f3_ctl); return 1; } debugf1(" Addr Map device PCI Bus ID:\t%s\n", pci_name(pvt->addr_f1_ctl)); debugf1(" DRAM MEM-CTL PCI Bus ID:\t%s\n", pci_name(pvt->dram_f2_ctl)); debugf1(" Misc device PCI Bus ID:\t%s\n", pci_name(pvt->misc_f3_ctl)); return 0; } static void amd64_free_mc_sibling_devices(struct amd64_pvt *pvt) { pci_dev_put(pvt->addr_f1_ctl); pci_dev_put(pvt->misc_f3_ctl); } /* * Retrieve the hardware registers of the memory controller (this includes the * 'Address Map' and 'Misc' device regs) */ static void amd64_read_mc_registers(struct amd64_pvt *pvt) { u64 msr_val; u32 tmp; int dram; /* * Retrieve TOP_MEM and TOP_MEM2; no masking off of reserved bits since * those are Read-As-Zero */ rdmsrl(MSR_K8_TOP_MEM1, pvt->top_mem); debugf0(" TOP_MEM: 0x%016llx\n", pvt->top_mem); /* check first whether TOP_MEM2 is enabled */ rdmsrl(MSR_K8_SYSCFG, msr_val); if (msr_val & (1U << 21)) { rdmsrl(MSR_K8_TOP_MEM2, pvt->top_mem2); debugf0(" TOP_MEM2: 0x%016llx\n", pvt->top_mem2); } else debugf0(" TOP_MEM2 disabled.\n"); amd64_cpu_display_info(pvt); amd64_read_pci_cfg(pvt->misc_f3_ctl, K8_NBCAP, &pvt->nbcap); if (pvt->ops->read_dram_ctl_register) pvt->ops->read_dram_ctl_register(pvt); for (dram = 0; dram < DRAM_REG_COUNT; dram++) { /* * Call CPU specific READ function to get the DRAM Base and * Limit values from the DCT. */ pvt->ops->read_dram_base_limit(pvt, dram); /* * Only print out debug info on rows with both R and W Enabled. * Normal processing, compiler should optimize this whole 'if' * debug output block away. */ if (pvt->dram_rw_en[dram] != 0) { debugf1(" DRAM-BASE[%d]: 0x%016llx " "DRAM-LIMIT: 0x%016llx\n", dram, pvt->dram_base[dram], pvt->dram_limit[dram]); debugf1(" IntlvEn=%s %s %s " "IntlvSel=%d DstNode=%d\n", pvt->dram_IntlvEn[dram] ? "Enabled" : "Disabled", (pvt->dram_rw_en[dram] & 0x2) ? "W" : "!W", (pvt->dram_rw_en[dram] & 0x1) ? "R" : "!R", pvt->dram_IntlvSel[dram], pvt->dram_DstNode[dram]); } } amd64_read_dct_base_mask(pvt); amd64_read_pci_cfg(pvt->addr_f1_ctl, K8_DHAR, &pvt->dhar); amd64_read_dbam_reg(pvt); amd64_read_pci_cfg(pvt->misc_f3_ctl, F10_ONLINE_SPARE, &pvt->online_spare); amd64_read_pci_cfg(pvt->dram_f2_ctl, F10_DCLR_0, &pvt->dclr0); amd64_read_pci_cfg(pvt->dram_f2_ctl, F10_DCHR_0, &pvt->dchr0); if (boot_cpu_data.x86 >= 0x10) { if (!dct_ganging_enabled(pvt)) { amd64_read_pci_cfg(pvt->dram_f2_ctl, F10_DCLR_1, &pvt->dclr1); amd64_read_pci_cfg(pvt->dram_f2_ctl, F10_DCHR_1, &pvt->dchr1); } amd64_read_pci_cfg(pvt->misc_f3_ctl, EXT_NB_MCA_CFG, &tmp); } if (boot_cpu_data.x86 == 0x10 && boot_cpu_data.x86_model > 7 && /* F3x180[EccSymbolSize]=1 => x8 symbols */ tmp & BIT(25)) pvt->syn_type = 8; else pvt->syn_type = 4; amd64_dump_misc_regs(pvt); } /* * NOTE: CPU Revision Dependent code * * Input: * @csrow_nr ChipSelect Row Number (0..pvt->cs_count-1) * k8 private pointer to --> * DRAM Bank Address mapping register * node_id * DCL register where dual_channel_active is * * The DBAM register consists of 4 sets of 4 bits each definitions: * * Bits: CSROWs * 0-3 CSROWs 0 and 1 * 4-7 CSROWs 2 and 3 * 8-11 CSROWs 4 and 5 * 12-15 CSROWs 6 and 7 * * Values range from: 0 to 15 * The meaning of the values depends on CPU revision and dual-channel state, * see relevant BKDG more info. * * The memory controller provides for total of only 8 CSROWs in its current * architecture. Each "pair" of CSROWs normally represents just one DIMM in * single channel or two (2) DIMMs in dual channel mode. * * The following code logic collapses the various tables for CSROW based on CPU * revision. * * Returns: * The number of PAGE_SIZE pages on the specified CSROW number it * encompasses * */ static u32 amd64_csrow_nr_pages(int csrow_nr, struct amd64_pvt *pvt) { u32 cs_mode, nr_pages; /* * The math on this doesn't look right on the surface because x/2*4 can * be simplified to x*2 but this expression makes use of the fact that * it is integral math where 1/2=0. This intermediate value becomes the * number of bits to shift the DBAM register to extract the proper CSROW * field. */ cs_mode = (pvt->dbam0 >> ((csrow_nr / 2) * 4)) & 0xF; nr_pages = pvt->ops->dbam_to_cs(pvt, cs_mode) << (20 - PAGE_SHIFT); /* * If dual channel then double the memory size of single channel. * Channel count is 1 or 2 */ nr_pages <<= (pvt->channel_count - 1); debugf0(" (csrow=%d) DBAM map index= %d\n", csrow_nr, cs_mode); debugf0(" nr_pages= %u channel-count = %d\n", nr_pages, pvt->channel_count); return nr_pages; } /* * Initialize the array of csrow attribute instances, based on the values * from pci config hardware registers. */ static int amd64_init_csrows(struct mem_ctl_info *mci) { struct csrow_info *csrow; struct amd64_pvt *pvt; u64 input_addr_min, input_addr_max, sys_addr; int i, empty = 1; pvt = mci->pvt_info; amd64_read_pci_cfg(pvt->misc_f3_ctl, K8_NBCFG, &pvt->nbcfg); debugf0("NBCFG= 0x%x CHIPKILL= %s DRAM ECC= %s\n", pvt->nbcfg, (pvt->nbcfg & K8_NBCFG_CHIPKILL) ? "Enabled" : "Disabled", (pvt->nbcfg & K8_NBCFG_ECC_ENABLE) ? "Enabled" : "Disabled" ); for (i = 0; i < pvt->cs_count; i++) { csrow = &mci->csrows[i]; if ((pvt->dcsb0[i] & K8_DCSB_CS_ENABLE) == 0) { debugf1("----CSROW %d EMPTY for node %d\n", i, pvt->mc_node_id); continue; } debugf1("----CSROW %d VALID for MC node %d\n", i, pvt->mc_node_id); empty = 0; csrow->nr_pages = amd64_csrow_nr_pages(i, pvt); find_csrow_limits(mci, i, &input_addr_min, &input_addr_max); sys_addr = input_addr_to_sys_addr(mci, input_addr_min); csrow->first_page = (u32) (sys_addr >> PAGE_SHIFT); sys_addr = input_addr_to_sys_addr(mci, input_addr_max); csrow->last_page = (u32) (sys_addr >> PAGE_SHIFT); csrow->page_mask = ~mask_from_dct_mask(pvt, i); /* 8 bytes of resolution */ csrow->mtype = amd64_determine_memory_type(pvt); debugf1(" for MC node %d csrow %d:\n", pvt->mc_node_id, i); debugf1(" input_addr_min: 0x%lx input_addr_max: 0x%lx\n", (unsigned long)input_addr_min, (unsigned long)input_addr_max); debugf1(" sys_addr: 0x%lx page_mask: 0x%lx\n", (unsigned long)sys_addr, csrow->page_mask); debugf1(" nr_pages: %u first_page: 0x%lx " "last_page: 0x%lx\n", (unsigned)csrow->nr_pages, csrow->first_page, csrow->last_page); /* * determine whether CHIPKILL or JUST ECC or NO ECC is operating */ if (pvt->nbcfg & K8_NBCFG_ECC_ENABLE) csrow->edac_mode = (pvt->nbcfg & K8_NBCFG_CHIPKILL) ? EDAC_S4ECD4ED : EDAC_SECDED; else csrow->edac_mode = EDAC_NONE; } return empty; } /* get all cores on this DCT */ static void get_cpus_on_this_dct_cpumask(struct cpumask *mask, int nid) { int cpu; for_each_online_cpu(cpu) if (amd_get_nb_id(cpu) == nid) cpumask_set_cpu(cpu, mask); } /* check MCG_CTL on all the cpus on this node */ static bool amd64_nb_mce_bank_enabled_on_node(int nid) { cpumask_var_t mask; int cpu, nbe; bool ret = false; if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) { amd64_printk(KERN_WARNING, "%s: error allocating mask\n", __func__); return false; } get_cpus_on_this_dct_cpumask(mask, nid); rdmsr_on_cpus(mask, MSR_IA32_MCG_CTL, msrs); for_each_cpu(cpu, mask) { struct msr *reg = per_cpu_ptr(msrs, cpu); nbe = reg->l & K8_MSR_MCGCTL_NBE; debugf0("core: %u, MCG_CTL: 0x%llx, NB MSR is %s\n", cpu, reg->q, (nbe ? "enabled" : "disabled")); if (!nbe) goto out; } ret = true; out: free_cpumask_var(mask); return ret; } static int amd64_toggle_ecc_err_reporting(struct amd64_pvt *pvt, bool on) { cpumask_var_t cmask; int cpu; if (!zalloc_cpumask_var(&cmask, GFP_KERNEL)) { amd64_printk(KERN_WARNING, "%s: error allocating mask\n", __func__); return false; } get_cpus_on_this_dct_cpumask(cmask, pvt->mc_node_id); rdmsr_on_cpus(cmask, MSR_IA32_MCG_CTL, msrs); for_each_cpu(cpu, cmask) { struct msr *reg = per_cpu_ptr(msrs, cpu); if (on) { if (reg->l & K8_MSR_MCGCTL_NBE) pvt->flags.nb_mce_enable = 1; reg->l |= K8_MSR_MCGCTL_NBE; } else { /* * Turn off NB MCE reporting only when it was off before */ if (!pvt->flags.nb_mce_enable) reg->l &= ~K8_MSR_MCGCTL_NBE; } } wrmsr_on_cpus(cmask, MSR_IA32_MCG_CTL, msrs); free_cpumask_var(cmask); return 0; } static void amd64_enable_ecc_error_reporting(struct mem_ctl_info *mci) { struct amd64_pvt *pvt = mci->pvt_info; u32 value, mask = K8_NBCTL_CECCEn | K8_NBCTL_UECCEn; amd64_read_pci_cfg(pvt->misc_f3_ctl, K8_NBCTL, &value); /* turn on UECCn and CECCEn bits */ pvt->old_nbctl = value & mask; pvt->nbctl_mcgctl_saved = 1; value |= mask; pci_write_config_dword(pvt->misc_f3_ctl, K8_NBCTL, value); if (amd64_toggle_ecc_err_reporting(pvt, ON)) amd64_printk(KERN_WARNING, "Error enabling ECC reporting over " "MCGCTL!\n"); amd64_read_pci_cfg(pvt->misc_f3_ctl, K8_NBCFG, &value); debugf0("NBCFG(1)= 0x%x CHIPKILL= %s ECC_ENABLE= %s\n", value, (value & K8_NBCFG_CHIPKILL) ? "Enabled" : "Disabled", (value & K8_NBCFG_ECC_ENABLE) ? "Enabled" : "Disabled"); if (!(value & K8_NBCFG_ECC_ENABLE)) { amd64_printk(KERN_WARNING, "This node reports that DRAM ECC is " "currently Disabled; ENABLING now\n"); pvt->flags.nb_ecc_prev = 0; /* Attempt to turn on DRAM ECC Enable */ value |= K8_NBCFG_ECC_ENABLE; pci_write_config_dword(pvt->misc_f3_ctl, K8_NBCFG, value); amd64_read_pci_cfg(pvt->misc_f3_ctl, K8_NBCFG, &value); if (!(value & K8_NBCFG_ECC_ENABLE)) { amd64_printk(KERN_WARNING, "Hardware rejects Enabling DRAM ECC checking\n" "Check memory DIMM configuration\n"); } else { amd64_printk(KERN_DEBUG, "Hardware accepted DRAM ECC Enable\n"); } } else { pvt->flags.nb_ecc_prev = 1; } debugf0("NBCFG(2)= 0x%x CHIPKILL= %s ECC_ENABLE= %s\n", value, (value & K8_NBCFG_CHIPKILL) ? "Enabled" : "Disabled", (value & K8_NBCFG_ECC_ENABLE) ? "Enabled" : "Disabled"); pvt->ctl_error_info.nbcfg = value; } static void amd64_restore_ecc_error_reporting(struct amd64_pvt *pvt) { u32 value, mask = K8_NBCTL_CECCEn | K8_NBCTL_UECCEn; if (!pvt->nbctl_mcgctl_saved) return; amd64_read_pci_cfg(pvt->misc_f3_ctl, K8_NBCTL, &value); value &= ~mask; value |= pvt->old_nbctl; pci_write_config_dword(pvt->misc_f3_ctl, K8_NBCTL, value); /* restore previous BIOS DRAM ECC "off" setting which we force-enabled */ if (!pvt->flags.nb_ecc_prev) { amd64_read_pci_cfg(pvt->misc_f3_ctl, K8_NBCFG, &value); value &= ~K8_NBCFG_ECC_ENABLE; pci_write_config_dword(pvt->misc_f3_ctl, K8_NBCFG, value); } /* restore the NB Enable MCGCTL bit */ if (amd64_toggle_ecc_err_reporting(pvt, OFF)) amd64_printk(KERN_WARNING, "Error restoring NB MCGCTL settings!\n"); } /* * EDAC requires that the BIOS have ECC enabled before taking over the * processing of ECC errors. This is because the BIOS can properly initialize * the memory system completely. A command line option allows to force-enable * hardware ECC later in amd64_enable_ecc_error_reporting(). */ static const char *ecc_msg = "ECC disabled in the BIOS or no ECC capability, module will not load.\n" " Either enable ECC checking or force module loading by setting " "'ecc_enable_override'.\n" " (Note that use of the override may cause unknown side effects.)\n"; static int amd64_check_ecc_enabled(struct amd64_pvt *pvt) { u32 value; u8 ecc_enabled = 0; bool nb_mce_en = false; amd64_read_pci_cfg(pvt->misc_f3_ctl, K8_NBCFG, &value); ecc_enabled = !!(value & K8_NBCFG_ECC_ENABLE); if (!ecc_enabled) amd64_printk(KERN_NOTICE, "This node reports that Memory ECC " "is currently disabled, set F3x%x[22] (%s).\n", K8_NBCFG, pci_name(pvt->misc_f3_ctl)); else amd64_printk(KERN_INFO, "ECC is enabled by BIOS.\n"); nb_mce_en = amd64_nb_mce_bank_enabled_on_node(pvt->mc_node_id); if (!nb_mce_en) amd64_printk(KERN_NOTICE, "NB MCE bank disabled, set MSR " "0x%08x[4] on node %d to enable.\n", MSR_IA32_MCG_CTL, pvt->mc_node_id); if (!ecc_enabled || !nb_mce_en) { if (!ecc_enable_override) { amd64_printk(KERN_NOTICE, "%s", ecc_msg); return -ENODEV; } else { amd64_printk(KERN_WARNING, "Forcing ECC checking on!\n"); } } return 0; } struct mcidev_sysfs_attribute sysfs_attrs[ARRAY_SIZE(amd64_dbg_attrs) + ARRAY_SIZE(amd64_inj_attrs) + 1]; struct mcidev_sysfs_attribute terminator = { .attr = { .name = NULL } }; static void amd64_set_mc_sysfs_attributes(struct mem_ctl_info *mci) { unsigned int i = 0, j = 0; for (; i < ARRAY_SIZE(amd64_dbg_attrs); i++) sysfs_attrs[i] = amd64_dbg_attrs[i]; for (j = 0; j < ARRAY_SIZE(amd64_inj_attrs); j++, i++) sysfs_attrs[i] = amd64_inj_attrs[j]; sysfs_attrs[i] = terminator; mci->mc_driver_sysfs_attributes = sysfs_attrs; } static void amd64_setup_mci_misc_attributes(struct mem_ctl_info *mci) { struct amd64_pvt *pvt = mci->pvt_info; mci->mtype_cap = MEM_FLAG_DDR2 | MEM_FLAG_RDDR2; mci->edac_ctl_cap = EDAC_FLAG_NONE; if (pvt->nbcap & K8_NBCAP_SECDED) mci->edac_ctl_cap |= EDAC_FLAG_SECDED; if (pvt->nbcap & K8_NBCAP_CHIPKILL) mci->edac_ctl_cap |= EDAC_FLAG_S4ECD4ED; mci->edac_cap = amd64_determine_edac_cap(pvt); mci->mod_name = EDAC_MOD_STR; mci->mod_ver = EDAC_AMD64_VERSION; mci->ctl_name = get_amd_family_name(pvt->mc_type_index); mci->dev_name = pci_name(pvt->dram_f2_ctl); mci->ctl_page_to_phys = NULL; /* memory scrubber interface */ mci->set_sdram_scrub_rate = amd64_set_scrub_rate; mci->get_sdram_scrub_rate = amd64_get_scrub_rate; } /* * Init stuff for this DRAM Controller device. * * Due to a hardware feature on Fam10h CPUs, the Enable Extended Configuration * Space feature MUST be enabled on ALL Processors prior to actually reading * from the ECS registers. Since the loading of the module can occur on any * 'core', and cores don't 'see' all the other processors ECS data when the * others are NOT enabled. Our solution is to first enable ECS access in this * routine on all processors, gather some data in a amd64_pvt structure and * later come back in a finish-setup function to perform that final * initialization. See also amd64_init_2nd_stage() for that. */ static int amd64_probe_one_instance(struct pci_dev *dram_f2_ctl, int mc_type_index) { struct amd64_pvt *pvt = NULL; int err = 0, ret; ret = -ENOMEM; pvt = kzalloc(sizeof(struct amd64_pvt), GFP_KERNEL); if (!pvt) goto err_exit; pvt->mc_node_id = get_node_id(dram_f2_ctl); pvt->dram_f2_ctl = dram_f2_ctl; pvt->ext_model = boot_cpu_data.x86_model >> 4; pvt->mc_type_index = mc_type_index; pvt->ops = family_ops(mc_type_index); /* * We have the dram_f2_ctl device as an argument, now go reserve its * sibling devices from the PCI system. */ ret = -ENODEV; err = amd64_reserve_mc_sibling_devices(pvt, mc_type_index); if (err) goto err_free; ret = -EINVAL; err = amd64_check_ecc_enabled(pvt); if (err) goto err_put; /* * Key operation here: setup of HW prior to performing ops on it. Some * setup is required to access ECS data. After this is performed, the * 'teardown' function must be called upon error and normal exit paths. */ if (boot_cpu_data.x86 >= 0x10) amd64_setup(pvt); /* * Save the pointer to the private data for use in 2nd initialization * stage */ pvt_lookup[pvt->mc_node_id] = pvt; return 0; err_put: amd64_free_mc_sibling_devices(pvt); err_free: kfree(pvt); err_exit: return ret; } /* * This is the finishing stage of the init code. Needs to be performed after all * MCs' hardware have been prepped for accessing extended config space. */ static int amd64_init_2nd_stage(struct amd64_pvt *pvt) { int node_id = pvt->mc_node_id; struct mem_ctl_info *mci; int ret = -ENODEV; amd64_read_mc_registers(pvt); /* * We need to determine how many memory channels there are. Then use * that information for calculating the size of the dynamic instance * tables in the 'mci' structure */ pvt->channel_count = pvt->ops->early_channel_count(pvt); if (pvt->channel_count < 0) goto err_exit; ret = -ENOMEM; mci = edac_mc_alloc(0, pvt->cs_count, pvt->channel_count, node_id); if (!mci) goto err_exit; mci->pvt_info = pvt; mci->dev = &pvt->dram_f2_ctl->dev; amd64_setup_mci_misc_attributes(mci); if (amd64_init_csrows(mci)) mci->edac_cap = EDAC_FLAG_NONE; amd64_enable_ecc_error_reporting(mci); amd64_set_mc_sysfs_attributes(mci); ret = -ENODEV; if (edac_mc_add_mc(mci)) { debugf1("failed edac_mc_add_mc()\n"); goto err_add_mc; } mci_lookup[node_id] = mci; pvt_lookup[node_id] = NULL; /* register stuff with EDAC MCE */ if (report_gart_errors) amd_report_gart_errors(true); amd_register_ecc_decoder(amd64_decode_bus_error); return 0; err_add_mc: edac_mc_free(mci); err_exit: debugf0("failure to init 2nd stage: ret=%d\n", ret); amd64_restore_ecc_error_reporting(pvt); if (boot_cpu_data.x86 > 0xf) amd64_teardown(pvt); amd64_free_mc_sibling_devices(pvt); kfree(pvt_lookup[pvt->mc_node_id]); pvt_lookup[node_id] = NULL; return ret; } static int __devinit amd64_init_one_instance(struct pci_dev *pdev, const struct pci_device_id *mc_type) { int ret = 0; debugf0("(MC node=%d,mc_type='%s')\n", get_node_id(pdev), get_amd_family_name(mc_type->driver_data)); ret = pci_enable_device(pdev); if (ret < 0) ret = -EIO; else ret = amd64_probe_one_instance(pdev, mc_type->driver_data); if (ret < 0) debugf0("ret=%d\n", ret); return ret; } static void __devexit amd64_remove_one_instance(struct pci_dev *pdev) { struct mem_ctl_info *mci; struct amd64_pvt *pvt; /* Remove from EDAC CORE tracking list */ mci = edac_mc_del_mc(&pdev->dev); if (!mci) return; pvt = mci->pvt_info; amd64_restore_ecc_error_reporting(pvt); if (boot_cpu_data.x86 > 0xf) amd64_teardown(pvt); amd64_free_mc_sibling_devices(pvt); /* unregister from EDAC MCE */ amd_report_gart_errors(false); amd_unregister_ecc_decoder(amd64_decode_bus_error); /* Free the EDAC CORE resources */ mci->pvt_info = NULL; mci_lookup[pvt->mc_node_id] = NULL; kfree(pvt); edac_mc_free(mci); } /* * This table is part of the interface for loading drivers for PCI devices. The * PCI core identifies what devices are on a system during boot, and then * inquiry this table to see if this driver is for a given device found. */ static const struct pci_device_id amd64_pci_table[] __devinitdata = { { .vendor = PCI_VENDOR_ID_AMD, .device = PCI_DEVICE_ID_AMD_K8_NB_MEMCTL, .subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID, .class = 0, .class_mask = 0, .driver_data = K8_CPUS }, { .vendor = PCI_VENDOR_ID_AMD, .device = PCI_DEVICE_ID_AMD_10H_NB_DRAM, .subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID, .class = 0, .class_mask = 0, .driver_data = F10_CPUS }, { .vendor = PCI_VENDOR_ID_AMD, .device = PCI_DEVICE_ID_AMD_11H_NB_DRAM, .subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID, .class = 0, .class_mask = 0, .driver_data = F11_CPUS }, {0, } }; MODULE_DEVICE_TABLE(pci, amd64_pci_table); static struct pci_driver amd64_pci_driver = { .name = EDAC_MOD_STR, .probe = amd64_init_one_instance, .remove = __devexit_p(amd64_remove_one_instance), .id_table = amd64_pci_table, }; static void amd64_setup_pci_device(void) { struct mem_ctl_info *mci; struct amd64_pvt *pvt; if (amd64_ctl_pci) return; mci = mci_lookup[0]; if (mci) { pvt = mci->pvt_info; amd64_ctl_pci = edac_pci_create_generic_ctl(&pvt->dram_f2_ctl->dev, EDAC_MOD_STR); if (!amd64_ctl_pci) { pr_warning("%s(): Unable to create PCI control\n", __func__); pr_warning("%s(): PCI error report via EDAC not set\n", __func__); } } } static int __init amd64_edac_init(void) { int nb, err = -ENODEV; bool load_ok = false; edac_printk(KERN_INFO, EDAC_MOD_STR, EDAC_AMD64_VERSION "\n"); opstate_init(); if (cache_k8_northbridges() < 0) goto err_ret; msrs = msrs_alloc(); if (!msrs) goto err_ret; err = pci_register_driver(&amd64_pci_driver); if (err) goto err_pci; /* * At this point, the array 'pvt_lookup[]' contains pointers to alloc'd * amd64_pvt structs. These will be used in the 2nd stage init function * to finish initialization of the MC instances. */ err = -ENODEV; for (nb = 0; nb < k8_northbridges.num; nb++) { if (!pvt_lookup[nb]) continue; err = amd64_init_2nd_stage(pvt_lookup[nb]); if (err) goto err_2nd_stage; load_ok = true; } if (load_ok) { amd64_setup_pci_device(); return 0; } err_2nd_stage: pci_unregister_driver(&amd64_pci_driver); err_pci: msrs_free(msrs); msrs = NULL; err_ret: return err; } static void __exit amd64_edac_exit(void) { if (amd64_ctl_pci) edac_pci_release_generic_ctl(amd64_ctl_pci); pci_unregister_driver(&amd64_pci_driver); msrs_free(msrs); msrs = NULL; } module_init(amd64_edac_init); module_exit(amd64_edac_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("SoftwareBitMaker: Doug Thompson, " "Dave Peterson, Thayne Harbaugh"); MODULE_DESCRIPTION("MC support for AMD64 memory controllers - " EDAC_AMD64_VERSION); module_param(edac_op_state, int, 0444); MODULE_PARM_DESC(edac_op_state, "EDAC Error Reporting state: 0=Poll,1=NMI");