kernel-wide: replace USHORT_MAX, SHORT_MAX and SHORT_MIN with USHRT_MAX, SHRT_MAX...
[linux-2.6/next.git] / sound / soc / codecs / wm9090.c
blob1592250daec0ff0eda0896536d2a8d1234951b17
1 /*
2 * ALSA SoC WM9090 driver
4 * Copyright 2009, 2010 Wolfson Microelectronics
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA
23 #include <linux/module.h>
24 #include <linux/errno.h>
25 #include <linux/device.h>
26 #include <linux/i2c.h>
27 #include <linux/delay.h>
28 #include <linux/slab.h>
29 #include <sound/initval.h>
30 #include <sound/soc.h>
31 #include <sound/soc-dapm.h>
32 #include <sound/tlv.h>
33 #include <sound/wm9090.h>
35 #include "wm9090.h"
37 static struct snd_soc_codec *wm9090_codec;
39 static const u16 wm9090_reg_defaults[] = {
40 0x9093, /* R0 - Software Reset */
41 0x0006, /* R1 - Power Management (1) */
42 0x6000, /* R2 - Power Management (2) */
43 0x0000, /* R3 - Power Management (3) */
44 0x0000, /* R4 */
45 0x0000, /* R5 */
46 0x01C0, /* R6 - Clocking 1 */
47 0x0000, /* R7 */
48 0x0000, /* R8 */
49 0x0000, /* R9 */
50 0x0000, /* R10 */
51 0x0000, /* R11 */
52 0x0000, /* R12 */
53 0x0000, /* R13 */
54 0x0000, /* R14 */
55 0x0000, /* R15 */
56 0x0000, /* R16 */
57 0x0000, /* R17 */
58 0x0000, /* R18 */
59 0x0000, /* R19 */
60 0x0000, /* R20 */
61 0x0000, /* R21 */
62 0x0003, /* R22 - IN1 Line Control */
63 0x0003, /* R23 - IN2 Line Control */
64 0x0083, /* R24 - IN1 Line Input A Volume */
65 0x0083, /* R25 - IN1 Line Input B Volume */
66 0x0083, /* R26 - IN2 Line Input A Volume */
67 0x0083, /* R27 - IN2 Line Input B Volume */
68 0x002D, /* R28 - Left Output Volume */
69 0x002D, /* R29 - Right Output Volume */
70 0x0000, /* R30 */
71 0x0000, /* R31 */
72 0x0000, /* R32 */
73 0x0000, /* R33 */
74 0x0100, /* R34 - SPKMIXL Attenuation */
75 0x0000, /* R35 */
76 0x0010, /* R36 - SPKOUT Mixers */
77 0x0140, /* R37 - ClassD3 */
78 0x0039, /* R38 - Speaker Volume Left */
79 0x0000, /* R39 */
80 0x0000, /* R40 */
81 0x0000, /* R41 */
82 0x0000, /* R42 */
83 0x0000, /* R43 */
84 0x0000, /* R44 */
85 0x0000, /* R45 - Output Mixer1 */
86 0x0000, /* R46 - Output Mixer2 */
87 0x0100, /* R47 - Output Mixer3 */
88 0x0100, /* R48 - Output Mixer4 */
89 0x0000, /* R49 */
90 0x0000, /* R50 */
91 0x0000, /* R51 */
92 0x0000, /* R52 */
93 0x0000, /* R53 */
94 0x0000, /* R54 - Speaker Mixer */
95 0x0000, /* R55 */
96 0x0000, /* R56 */
97 0x000D, /* R57 - AntiPOP2 */
98 0x0000, /* R58 */
99 0x0000, /* R59 */
100 0x0000, /* R60 */
101 0x0000, /* R61 */
102 0x0000, /* R62 */
103 0x0000, /* R63 */
104 0x0000, /* R64 */
105 0x0000, /* R65 */
106 0x0000, /* R66 */
107 0x0000, /* R67 */
108 0x0000, /* R68 */
109 0x0000, /* R69 */
110 0x0000, /* R70 - Write Sequencer 0 */
111 0x0000, /* R71 - Write Sequencer 1 */
112 0x0000, /* R72 - Write Sequencer 2 */
113 0x0000, /* R73 - Write Sequencer 3 */
114 0x0000, /* R74 - Write Sequencer 4 */
115 0x0000, /* R75 - Write Sequencer 5 */
116 0x1F25, /* R76 - Charge Pump 1 */
117 0x0000, /* R77 */
118 0x0000, /* R78 */
119 0x0000, /* R79 */
120 0x0000, /* R80 */
121 0x0000, /* R81 */
122 0x0000, /* R82 */
123 0x0000, /* R83 */
124 0x0000, /* R84 - DC Servo 0 */
125 0x054A, /* R85 - DC Servo 1 */
126 0x0000, /* R86 */
127 0x0000, /* R87 - DC Servo 3 */
128 0x0000, /* R88 - DC Servo Readback 0 */
129 0x0000, /* R89 - DC Servo Readback 1 */
130 0x0000, /* R90 - DC Servo Readback 2 */
131 0x0000, /* R91 */
132 0x0000, /* R92 */
133 0x0000, /* R93 */
134 0x0000, /* R94 */
135 0x0000, /* R95 */
136 0x0100, /* R96 - Analogue HP 0 */
137 0x0000, /* R97 */
138 0x8640, /* R98 - AGC Control 0 */
139 0xC000, /* R99 - AGC Control 1 */
140 0x0200, /* R100 - AGC Control 2 */
143 /* This struct is used to save the context */
144 struct wm9090_priv {
145 /* We're not really registering as a CODEC since ASoC core
146 * does not yet support multiple CODECs but having the CODEC
147 * structure means we can reuse some of the ASoC core
148 * features.
150 struct snd_soc_codec codec;
151 struct mutex mutex;
152 u16 reg_cache[WM9090_MAX_REGISTER + 1];
153 struct wm9090_platform_data pdata;
156 static int wm9090_volatile(unsigned int reg)
158 switch (reg) {
159 case WM9090_SOFTWARE_RESET:
160 case WM9090_DC_SERVO_0:
161 case WM9090_DC_SERVO_READBACK_0:
162 case WM9090_DC_SERVO_READBACK_1:
163 case WM9090_DC_SERVO_READBACK_2:
164 return 1;
166 default:
167 return 0;
171 static void wait_for_dc_servo(struct snd_soc_codec *codec)
173 unsigned int reg;
174 int count = 0;
176 dev_dbg(codec->dev, "Waiting for DC servo...\n");
177 do {
178 count++;
179 msleep(1);
180 reg = snd_soc_read(codec, WM9090_DC_SERVO_READBACK_0);
181 dev_dbg(codec->dev, "DC servo status: %x\n", reg);
182 } while ((reg & WM9090_DCS_CAL_COMPLETE_MASK)
183 != WM9090_DCS_CAL_COMPLETE_MASK && count < 1000);
185 if ((reg & WM9090_DCS_CAL_COMPLETE_MASK)
186 != WM9090_DCS_CAL_COMPLETE_MASK)
187 dev_err(codec->dev, "Timed out waiting for DC Servo\n");
190 static const unsigned int in_tlv[] = {
191 TLV_DB_RANGE_HEAD(6),
192 0, 0, TLV_DB_SCALE_ITEM(-600, 0, 0),
193 1, 3, TLV_DB_SCALE_ITEM(-350, 350, 0),
194 4, 6, TLV_DB_SCALE_ITEM(600, 600, 0),
196 static const unsigned int mix_tlv[] = {
197 TLV_DB_RANGE_HEAD(4),
198 0, 2, TLV_DB_SCALE_ITEM(-1200, 300, 0),
199 3, 3, TLV_DB_SCALE_ITEM(0, 0, 0),
201 static const DECLARE_TLV_DB_SCALE(out_tlv, -5700, 100, 0);
202 static const unsigned int spkboost_tlv[] = {
203 TLV_DB_RANGE_HEAD(7),
204 0, 6, TLV_DB_SCALE_ITEM(0, 150, 0),
205 7, 7, TLV_DB_SCALE_ITEM(1200, 0, 0),
208 static const struct snd_kcontrol_new wm9090_controls[] = {
209 SOC_SINGLE_TLV("IN1A Volume", WM9090_IN1_LINE_INPUT_A_VOLUME, 0, 6, 0,
210 in_tlv),
211 SOC_SINGLE("IN1A Switch", WM9090_IN1_LINE_INPUT_A_VOLUME, 7, 1, 1),
212 SOC_SINGLE("IN1A ZC Switch", WM9090_IN1_LINE_INPUT_A_VOLUME, 6, 1, 0),
214 SOC_SINGLE_TLV("IN2A Volume", WM9090_IN2_LINE_INPUT_A_VOLUME, 0, 6, 0,
215 in_tlv),
216 SOC_SINGLE("IN2A Switch", WM9090_IN2_LINE_INPUT_A_VOLUME, 7, 1, 1),
217 SOC_SINGLE("IN2A ZC Switch", WM9090_IN2_LINE_INPUT_A_VOLUME, 6, 1, 0),
219 SOC_SINGLE("MIXOUTL Switch", WM9090_OUTPUT_MIXER3, 8, 1, 1),
220 SOC_SINGLE_TLV("MIXOUTL IN1A Volume", WM9090_OUTPUT_MIXER3, 6, 3, 1,
221 mix_tlv),
222 SOC_SINGLE_TLV("MIXOUTL IN2A Volume", WM9090_OUTPUT_MIXER3, 2, 3, 1,
223 mix_tlv),
225 SOC_SINGLE("MIXOUTR Switch", WM9090_OUTPUT_MIXER4, 8, 1, 1),
226 SOC_SINGLE_TLV("MIXOUTR IN1A Volume", WM9090_OUTPUT_MIXER4, 6, 3, 1,
227 mix_tlv),
228 SOC_SINGLE_TLV("MIXOUTR IN2A Volume", WM9090_OUTPUT_MIXER4, 2, 3, 1,
229 mix_tlv),
231 SOC_SINGLE("SPKMIX Switch", WM9090_SPKMIXL_ATTENUATION, 8, 1, 1),
232 SOC_SINGLE_TLV("SPKMIX IN1A Volume", WM9090_SPKMIXL_ATTENUATION, 6, 3, 1,
233 mix_tlv),
234 SOC_SINGLE_TLV("SPKMIX IN2A Volume", WM9090_SPKMIXL_ATTENUATION, 2, 3, 1,
235 mix_tlv),
237 SOC_DOUBLE_R_TLV("Headphone Volume", WM9090_LEFT_OUTPUT_VOLUME,
238 WM9090_RIGHT_OUTPUT_VOLUME, 0, 63, 0, out_tlv),
239 SOC_DOUBLE_R("Headphone Switch", WM9090_LEFT_OUTPUT_VOLUME,
240 WM9090_RIGHT_OUTPUT_VOLUME, 6, 1, 1),
241 SOC_DOUBLE_R("Headphone ZC Switch", WM9090_LEFT_OUTPUT_VOLUME,
242 WM9090_RIGHT_OUTPUT_VOLUME, 7, 1, 0),
244 SOC_SINGLE_TLV("Speaker Volume", WM9090_SPEAKER_VOLUME_LEFT, 0, 63, 0,
245 out_tlv),
246 SOC_SINGLE("Speaker Switch", WM9090_SPEAKER_VOLUME_LEFT, 6, 1, 1),
247 SOC_SINGLE("Speaker ZC Switch", WM9090_SPEAKER_VOLUME_LEFT, 7, 1, 0),
248 SOC_SINGLE_TLV("Speaker Boost Volume", WM9090_CLASSD3, 3, 7, 0, spkboost_tlv),
251 static const struct snd_kcontrol_new wm9090_in1_se_controls[] = {
252 SOC_SINGLE_TLV("IN1B Volume", WM9090_IN1_LINE_INPUT_B_VOLUME, 0, 6, 0,
253 in_tlv),
254 SOC_SINGLE("IN1B Switch", WM9090_IN1_LINE_INPUT_B_VOLUME, 7, 1, 1),
255 SOC_SINGLE("IN1B ZC Switch", WM9090_IN1_LINE_INPUT_B_VOLUME, 6, 1, 0),
257 SOC_SINGLE_TLV("SPKMIX IN1B Volume", WM9090_SPKMIXL_ATTENUATION, 4, 3, 1,
258 mix_tlv),
259 SOC_SINGLE_TLV("MIXOUTL IN1B Volume", WM9090_OUTPUT_MIXER3, 4, 3, 1,
260 mix_tlv),
261 SOC_SINGLE_TLV("MIXOUTR IN1B Volume", WM9090_OUTPUT_MIXER4, 4, 3, 1,
262 mix_tlv),
265 static const struct snd_kcontrol_new wm9090_in2_se_controls[] = {
266 SOC_SINGLE_TLV("IN2B Volume", WM9090_IN2_LINE_INPUT_B_VOLUME, 0, 6, 0,
267 in_tlv),
268 SOC_SINGLE("IN2B Switch", WM9090_IN2_LINE_INPUT_B_VOLUME, 7, 1, 1),
269 SOC_SINGLE("IN2B ZC Switch", WM9090_IN2_LINE_INPUT_B_VOLUME, 6, 1, 0),
271 SOC_SINGLE_TLV("SPKMIX IN2B Volume", WM9090_SPKMIXL_ATTENUATION, 0, 3, 1,
272 mix_tlv),
273 SOC_SINGLE_TLV("MIXOUTL IN2B Volume", WM9090_OUTPUT_MIXER3, 0, 3, 1,
274 mix_tlv),
275 SOC_SINGLE_TLV("MIXOUTR IN2B Volume", WM9090_OUTPUT_MIXER4, 0, 3, 1,
276 mix_tlv),
279 static int hp_ev(struct snd_soc_dapm_widget *w,
280 struct snd_kcontrol *kcontrol, int event)
282 struct snd_soc_codec *codec = w->codec;
283 unsigned int reg = snd_soc_read(codec, WM9090_ANALOGUE_HP_0);
285 switch (event) {
286 case SND_SOC_DAPM_POST_PMU:
287 snd_soc_update_bits(codec, WM9090_CHARGE_PUMP_1,
288 WM9090_CP_ENA, WM9090_CP_ENA);
290 msleep(5);
292 snd_soc_update_bits(codec, WM9090_POWER_MANAGEMENT_1,
293 WM9090_HPOUT1L_ENA | WM9090_HPOUT1R_ENA,
294 WM9090_HPOUT1L_ENA | WM9090_HPOUT1R_ENA);
296 reg |= WM9090_HPOUT1L_DLY | WM9090_HPOUT1R_DLY;
297 snd_soc_write(codec, WM9090_ANALOGUE_HP_0, reg);
299 /* Start the DC servo. We don't currently use the
300 * ability to save the state since we don't have full
301 * control of the analogue paths and they can change
302 * DC offsets; see the WM8904 driver for an example of
303 * doing so.
305 snd_soc_write(codec, WM9090_DC_SERVO_0,
306 WM9090_DCS_ENA_CHAN_0 |
307 WM9090_DCS_ENA_CHAN_1 |
308 WM9090_DCS_TRIG_STARTUP_1 |
309 WM9090_DCS_TRIG_STARTUP_0);
310 wait_for_dc_servo(codec);
312 reg |= WM9090_HPOUT1R_OUTP | WM9090_HPOUT1R_RMV_SHORT |
313 WM9090_HPOUT1L_OUTP | WM9090_HPOUT1L_RMV_SHORT;
314 snd_soc_write(codec, WM9090_ANALOGUE_HP_0, reg);
315 break;
317 case SND_SOC_DAPM_PRE_PMD:
318 reg &= ~(WM9090_HPOUT1L_RMV_SHORT |
319 WM9090_HPOUT1L_DLY |
320 WM9090_HPOUT1L_OUTP |
321 WM9090_HPOUT1R_RMV_SHORT |
322 WM9090_HPOUT1R_DLY |
323 WM9090_HPOUT1R_OUTP);
325 snd_soc_write(codec, WM9090_ANALOGUE_HP_0, reg);
327 snd_soc_write(codec, WM9090_DC_SERVO_0, 0);
329 snd_soc_update_bits(codec, WM9090_POWER_MANAGEMENT_1,
330 WM9090_HPOUT1L_ENA | WM9090_HPOUT1R_ENA,
333 snd_soc_update_bits(codec, WM9090_CHARGE_PUMP_1,
334 WM9090_CP_ENA, 0);
335 break;
338 return 0;
341 static const struct snd_kcontrol_new spkmix[] = {
342 SOC_DAPM_SINGLE("IN1A Switch", WM9090_SPEAKER_MIXER, 6, 1, 0),
343 SOC_DAPM_SINGLE("IN1B Switch", WM9090_SPEAKER_MIXER, 4, 1, 0),
344 SOC_DAPM_SINGLE("IN2A Switch", WM9090_SPEAKER_MIXER, 2, 1, 0),
345 SOC_DAPM_SINGLE("IN2B Switch", WM9090_SPEAKER_MIXER, 0, 1, 0),
348 static const struct snd_kcontrol_new spkout[] = {
349 SOC_DAPM_SINGLE("Mixer Switch", WM9090_SPKOUT_MIXERS, 4, 1, 0),
352 static const struct snd_kcontrol_new mixoutl[] = {
353 SOC_DAPM_SINGLE("IN1A Switch", WM9090_OUTPUT_MIXER1, 6, 1, 0),
354 SOC_DAPM_SINGLE("IN1B Switch", WM9090_OUTPUT_MIXER1, 4, 1, 0),
355 SOC_DAPM_SINGLE("IN2A Switch", WM9090_OUTPUT_MIXER1, 2, 1, 0),
356 SOC_DAPM_SINGLE("IN2B Switch", WM9090_OUTPUT_MIXER1, 0, 1, 0),
359 static const struct snd_kcontrol_new mixoutr[] = {
360 SOC_DAPM_SINGLE("IN1A Switch", WM9090_OUTPUT_MIXER2, 6, 1, 0),
361 SOC_DAPM_SINGLE("IN1B Switch", WM9090_OUTPUT_MIXER2, 4, 1, 0),
362 SOC_DAPM_SINGLE("IN2A Switch", WM9090_OUTPUT_MIXER2, 2, 1, 0),
363 SOC_DAPM_SINGLE("IN2B Switch", WM9090_OUTPUT_MIXER2, 0, 1, 0),
366 static const struct snd_soc_dapm_widget wm9090_dapm_widgets[] = {
367 SND_SOC_DAPM_INPUT("IN1+"),
368 SND_SOC_DAPM_INPUT("IN1-"),
369 SND_SOC_DAPM_INPUT("IN2+"),
370 SND_SOC_DAPM_INPUT("IN2-"),
372 SND_SOC_DAPM_SUPPLY("OSC", WM9090_POWER_MANAGEMENT_1, 3, 0, NULL, 0),
374 SND_SOC_DAPM_PGA("IN1A PGA", WM9090_POWER_MANAGEMENT_2, 7, 0, NULL, 0),
375 SND_SOC_DAPM_PGA("IN1B PGA", WM9090_POWER_MANAGEMENT_2, 6, 0, NULL, 0),
376 SND_SOC_DAPM_PGA("IN2A PGA", WM9090_POWER_MANAGEMENT_2, 5, 0, NULL, 0),
377 SND_SOC_DAPM_PGA("IN2B PGA", WM9090_POWER_MANAGEMENT_2, 4, 0, NULL, 0),
379 SND_SOC_DAPM_MIXER("SPKMIX", WM9090_POWER_MANAGEMENT_3, 3, 0,
380 spkmix, ARRAY_SIZE(spkmix)),
381 SND_SOC_DAPM_MIXER("MIXOUTL", WM9090_POWER_MANAGEMENT_3, 5, 0,
382 mixoutl, ARRAY_SIZE(mixoutl)),
383 SND_SOC_DAPM_MIXER("MIXOUTR", WM9090_POWER_MANAGEMENT_3, 4, 0,
384 mixoutr, ARRAY_SIZE(mixoutr)),
386 SND_SOC_DAPM_PGA_E("HP PGA", SND_SOC_NOPM, 0, 0, NULL, 0,
387 hp_ev, SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
389 SND_SOC_DAPM_PGA("SPKPGA", WM9090_POWER_MANAGEMENT_3, 8, 0, NULL, 0),
390 SND_SOC_DAPM_MIXER("SPKOUT", WM9090_POWER_MANAGEMENT_1, 12, 0,
391 spkout, ARRAY_SIZE(spkout)),
393 SND_SOC_DAPM_OUTPUT("HPR"),
394 SND_SOC_DAPM_OUTPUT("HPL"),
395 SND_SOC_DAPM_OUTPUT("Speaker"),
398 static const struct snd_soc_dapm_route audio_map[] = {
399 { "IN1A PGA", NULL, "IN1+" },
400 { "IN2A PGA", NULL, "IN2+" },
402 { "SPKMIX", "IN1A Switch", "IN1A PGA" },
403 { "SPKMIX", "IN2A Switch", "IN2A PGA" },
405 { "MIXOUTL", "IN1A Switch", "IN1A PGA" },
406 { "MIXOUTL", "IN2A Switch", "IN2A PGA" },
408 { "MIXOUTR", "IN1A Switch", "IN1A PGA" },
409 { "MIXOUTR", "IN2A Switch", "IN2A PGA" },
411 { "HP PGA", NULL, "OSC" },
412 { "HP PGA", NULL, "MIXOUTL" },
413 { "HP PGA", NULL, "MIXOUTR" },
415 { "HPL", NULL, "HP PGA" },
416 { "HPR", NULL, "HP PGA" },
418 { "SPKPGA", NULL, "OSC" },
419 { "SPKPGA", NULL, "SPKMIX" },
421 { "SPKOUT", "Mixer Switch", "SPKPGA" },
423 { "Speaker", NULL, "SPKOUT" },
426 static const struct snd_soc_dapm_route audio_map_in1_se[] = {
427 { "IN1B PGA", NULL, "IN1-" },
429 { "SPKMIX", "IN1B Switch", "IN1B PGA" },
430 { "MIXOUTL", "IN1B Switch", "IN1B PGA" },
431 { "MIXOUTR", "IN1B Switch", "IN1B PGA" },
434 static const struct snd_soc_dapm_route audio_map_in1_diff[] = {
435 { "IN1A PGA", NULL, "IN1-" },
438 static const struct snd_soc_dapm_route audio_map_in2_se[] = {
439 { "IN2B PGA", NULL, "IN2-" },
441 { "SPKMIX", "IN2B Switch", "IN2B PGA" },
442 { "MIXOUTL", "IN2B Switch", "IN2B PGA" },
443 { "MIXOUTR", "IN2B Switch", "IN2B PGA" },
446 static const struct snd_soc_dapm_route audio_map_in2_diff[] = {
447 { "IN2A PGA", NULL, "IN2-" },
450 static int wm9090_add_controls(struct snd_soc_codec *codec)
452 struct wm9090_priv *wm9090 = snd_soc_codec_get_drvdata(codec);
453 int i;
455 snd_soc_dapm_new_controls(codec, wm9090_dapm_widgets,
456 ARRAY_SIZE(wm9090_dapm_widgets));
458 snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
460 snd_soc_add_controls(codec, wm9090_controls,
461 ARRAY_SIZE(wm9090_controls));
463 if (wm9090->pdata.lin1_diff) {
464 snd_soc_dapm_add_routes(codec, audio_map_in1_diff,
465 ARRAY_SIZE(audio_map_in1_diff));
466 } else {
467 snd_soc_dapm_add_routes(codec, audio_map_in1_se,
468 ARRAY_SIZE(audio_map_in1_se));
469 snd_soc_add_controls(codec, wm9090_in1_se_controls,
470 ARRAY_SIZE(wm9090_in1_se_controls));
473 if (wm9090->pdata.lin2_diff) {
474 snd_soc_dapm_add_routes(codec, audio_map_in2_diff,
475 ARRAY_SIZE(audio_map_in2_diff));
476 } else {
477 snd_soc_dapm_add_routes(codec, audio_map_in2_se,
478 ARRAY_SIZE(audio_map_in2_se));
479 snd_soc_add_controls(codec, wm9090_in2_se_controls,
480 ARRAY_SIZE(wm9090_in2_se_controls));
483 if (wm9090->pdata.agc_ena) {
484 for (i = 0; i < ARRAY_SIZE(wm9090->pdata.agc); i++)
485 snd_soc_write(codec, WM9090_AGC_CONTROL_0 + i,
486 wm9090->pdata.agc[i]);
487 snd_soc_update_bits(codec, WM9090_POWER_MANAGEMENT_3,
488 WM9090_AGC_ENA, WM9090_AGC_ENA);
489 } else {
490 snd_soc_update_bits(codec, WM9090_POWER_MANAGEMENT_3,
491 WM9090_AGC_ENA, 0);
494 return 0;
499 * The machine driver should call this from their set_bias_level; if there
500 * isn't one then this can just be set as the set_bias_level function.
502 static int wm9090_set_bias_level(struct snd_soc_codec *codec,
503 enum snd_soc_bias_level level)
505 u16 *reg_cache = codec->reg_cache;
506 int i, ret;
508 switch (level) {
509 case SND_SOC_BIAS_ON:
510 break;
512 case SND_SOC_BIAS_PREPARE:
513 snd_soc_update_bits(codec, WM9090_ANTIPOP2, WM9090_VMID_ENA,
514 WM9090_VMID_ENA);
515 snd_soc_update_bits(codec, WM9090_POWER_MANAGEMENT_1,
516 WM9090_BIAS_ENA |
517 WM9090_VMID_RES_MASK,
518 WM9090_BIAS_ENA |
519 1 << WM9090_VMID_RES_SHIFT);
520 msleep(1); /* Probably an overestimate */
521 break;
523 case SND_SOC_BIAS_STANDBY:
524 if (codec->bias_level == SND_SOC_BIAS_OFF) {
525 /* Restore the register cache */
526 for (i = 1; i < codec->reg_cache_size; i++) {
527 if (reg_cache[i] == wm9090_reg_defaults[i])
528 continue;
529 if (wm9090_volatile(i))
530 continue;
532 ret = snd_soc_write(codec, i, reg_cache[i]);
533 if (ret != 0)
534 dev_warn(codec->dev,
535 "Failed to restore register %d: %d\n",
536 i, ret);
540 /* We keep VMID off during standby since the combination of
541 * ground referenced outputs and class D speaker mean that
542 * latency is not an issue.
544 snd_soc_update_bits(codec, WM9090_POWER_MANAGEMENT_1,
545 WM9090_BIAS_ENA | WM9090_VMID_RES_MASK, 0);
546 snd_soc_update_bits(codec, WM9090_ANTIPOP2,
547 WM9090_VMID_ENA, 0);
548 break;
550 case SND_SOC_BIAS_OFF:
551 break;
554 codec->bias_level = level;
556 return 0;
559 static int wm9090_probe(struct platform_device *pdev)
561 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
562 struct snd_soc_codec *codec;
563 int ret = 0;
565 if (wm9090_codec == NULL) {
566 dev_err(&pdev->dev, "Codec device not registered\n");
567 return -ENODEV;
570 socdev->card->codec = wm9090_codec;
571 codec = wm9090_codec;
573 /* register pcms */
574 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
575 if (ret < 0) {
576 dev_err(codec->dev, "failed to create pcms: %d\n", ret);
577 goto pcm_err;
580 wm9090_add_controls(codec);
582 return 0;
584 pcm_err:
585 return ret;
588 #ifdef CONFIG_PM
589 static int wm9090_suspend(struct platform_device *pdev, pm_message_t state)
591 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
592 struct snd_soc_codec *codec = socdev->card->codec;
594 wm9090_set_bias_level(codec, SND_SOC_BIAS_OFF);
596 return 0;
599 static int wm9090_resume(struct platform_device *pdev)
601 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
602 struct snd_soc_codec *codec = socdev->card->codec;
604 wm9090_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
606 return 0;
608 #else
609 #define wm9090_suspend NULL
610 #define wm9090_resume NULL
611 #endif
613 static int wm9090_remove(struct platform_device *pdev)
615 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
617 snd_soc_free_pcms(socdev);
618 snd_soc_dapm_free(socdev);
620 return 0;
623 struct snd_soc_codec_device soc_codec_dev_wm9090 = {
624 .probe = wm9090_probe,
625 .remove = wm9090_remove,
626 .suspend = wm9090_suspend,
627 .resume = wm9090_resume,
629 EXPORT_SYMBOL_GPL(soc_codec_dev_wm9090);
631 static int wm9090_i2c_probe(struct i2c_client *i2c,
632 const struct i2c_device_id *id)
634 struct wm9090_priv *wm9090;
635 struct snd_soc_codec *codec;
636 int ret;
638 wm9090 = kzalloc(sizeof(*wm9090), GFP_KERNEL);
639 if (wm9090 == NULL) {
640 dev_err(&i2c->dev, "Can not allocate memory\n");
641 return -ENOMEM;
643 codec = &wm9090->codec;
645 if (i2c->dev.platform_data)
646 memcpy(&wm9090->pdata, i2c->dev.platform_data,
647 sizeof(wm9090->pdata));
649 wm9090_codec = codec;
651 i2c_set_clientdata(i2c, wm9090);
653 mutex_init(&codec->mutex);
654 INIT_LIST_HEAD(&codec->dapm_widgets);
655 INIT_LIST_HEAD(&codec->dapm_paths);
657 codec->control_data = i2c;
658 snd_soc_codec_set_drvdata(codec, wm9090);
659 codec->dev = &i2c->dev;
660 codec->name = "WM9090";
661 codec->owner = THIS_MODULE;
662 codec->bias_level = SND_SOC_BIAS_OFF;
663 codec->set_bias_level = wm9090_set_bias_level,
664 codec->reg_cache_size = WM9090_MAX_REGISTER + 1;
665 codec->reg_cache = &wm9090->reg_cache;
666 codec->volatile_register = wm9090_volatile;
668 ret = snd_soc_codec_set_cache_io(codec, 8, 16, SND_SOC_I2C);
669 if (ret != 0) {
670 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
671 goto err;
674 memcpy(&wm9090->reg_cache, wm9090_reg_defaults,
675 sizeof(wm9090->reg_cache));
677 ret = snd_soc_read(codec, WM9090_SOFTWARE_RESET);
678 if (ret < 0)
679 goto err;
680 if (ret != wm9090_reg_defaults[WM9090_SOFTWARE_RESET]) {
681 dev_err(&i2c->dev, "Device is not a WM9090, ID=%x\n", ret);
682 ret = -EINVAL;
683 goto err;
686 ret = snd_soc_write(codec, WM9090_SOFTWARE_RESET, 0);
687 if (ret < 0)
688 goto err;
690 /* Configure some defaults; they will be written out when we
691 * bring the bias up.
693 wm9090->reg_cache[WM9090_IN1_LINE_INPUT_A_VOLUME] |= WM9090_IN1_VU
694 | WM9090_IN1A_ZC;
695 wm9090->reg_cache[WM9090_IN1_LINE_INPUT_B_VOLUME] |= WM9090_IN1_VU
696 | WM9090_IN1B_ZC;
697 wm9090->reg_cache[WM9090_IN2_LINE_INPUT_A_VOLUME] |= WM9090_IN2_VU
698 | WM9090_IN2A_ZC;
699 wm9090->reg_cache[WM9090_IN2_LINE_INPUT_B_VOLUME] |= WM9090_IN2_VU
700 | WM9090_IN2B_ZC;
701 wm9090->reg_cache[WM9090_SPEAKER_VOLUME_LEFT] |=
702 WM9090_SPKOUT_VU | WM9090_SPKOUTL_ZC;
703 wm9090->reg_cache[WM9090_LEFT_OUTPUT_VOLUME] |=
704 WM9090_HPOUT1_VU | WM9090_HPOUT1L_ZC;
705 wm9090->reg_cache[WM9090_RIGHT_OUTPUT_VOLUME] |=
706 WM9090_HPOUT1_VU | WM9090_HPOUT1R_ZC;
708 wm9090->reg_cache[WM9090_CLOCKING_1] |= WM9090_TOCLK_ENA;
710 wm9090_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
712 ret = snd_soc_register_codec(codec);
713 if (ret != 0) {
714 dev_err(&i2c->dev, "Failed to register CODEC: %d\n", ret);
715 goto err_bias;
718 return 0;
720 err_bias:
721 wm9090_set_bias_level(codec, SND_SOC_BIAS_OFF);
722 err:
723 kfree(wm9090);
724 i2c_set_clientdata(i2c, NULL);
725 wm9090_codec = NULL;
727 return ret;
730 static int wm9090_i2c_remove(struct i2c_client *i2c)
732 struct wm9090_priv *wm9090 = i2c_get_clientdata(i2c);
733 struct snd_soc_codec *codec = &wm9090->codec;
735 snd_soc_unregister_codec(codec);
736 wm9090_set_bias_level(codec, SND_SOC_BIAS_OFF);
737 kfree(wm9090);
738 wm9090_codec = NULL;
740 return 0;
743 static const struct i2c_device_id wm9090_id[] = {
744 { "wm9090", 0 },
747 MODULE_DEVICE_TABLE(i2c, wm9090_id);
749 static struct i2c_driver wm9090_i2c_driver = {
750 .driver = {
751 .name = "wm9090",
752 .owner = THIS_MODULE,
754 .probe = wm9090_i2c_probe,
755 .remove = __devexit_p(wm9090_i2c_remove),
756 .id_table = wm9090_id,
759 static int __init wm9090_init(void)
761 return i2c_add_driver(&wm9090_i2c_driver);
763 module_init(wm9090_init);
765 static void __exit wm9090_exit(void)
767 i2c_del_driver(&wm9090_i2c_driver);
769 module_exit(wm9090_exit);
771 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
772 MODULE_DESCRIPTION("WM9090 ASoC driver");
773 MODULE_LICENSE("GPL");