blob: 1592250daec0ff0eda0896536d2a8d1234951b17 [file] [log] [blame]
Mark Brown39b8eab2010-04-28 18:36:10 +01001/*
2 * ALSA SoC WM9090 driver
3 *
4 * Copyright 2009, 2010 Wolfson Microelectronics
5 *
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7 *
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.
11 *
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.
16 *
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
21 */
22
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>
34
35#include "wm9090.h"
36
37static struct snd_soc_codec *wm9090_codec;
38
39static 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 */
141};
142
143/* This struct is used to save the context */
144struct 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.
149 */
150 struct snd_soc_codec codec;
151 struct mutex mutex;
152 u16 reg_cache[WM9090_MAX_REGISTER + 1];
153 struct wm9090_platform_data pdata;
154};
155
156static int wm9090_volatile(unsigned int reg)
157{
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;
165
166 default:
167 return 0;
168 }
169}
170
171static void wait_for_dc_servo(struct snd_soc_codec *codec)
172{
173 unsigned int reg;
174 int count = 0;
175
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);
184
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");
188}
189
190static 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),
195};
196static 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),
200};
201static const DECLARE_TLV_DB_SCALE(out_tlv, -5700, 100, 0);
202static 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),
206};
207
208static const struct snd_kcontrol_new wm9090_controls[] = {
209SOC_SINGLE_TLV("IN1A Volume", WM9090_IN1_LINE_INPUT_A_VOLUME, 0, 6, 0,
210 in_tlv),
211SOC_SINGLE("IN1A Switch", WM9090_IN1_LINE_INPUT_A_VOLUME, 7, 1, 1),
212SOC_SINGLE("IN1A ZC Switch", WM9090_IN1_LINE_INPUT_A_VOLUME, 6, 1, 0),
213
214SOC_SINGLE_TLV("IN2A Volume", WM9090_IN2_LINE_INPUT_A_VOLUME, 0, 6, 0,
215 in_tlv),
216SOC_SINGLE("IN2A Switch", WM9090_IN2_LINE_INPUT_A_VOLUME, 7, 1, 1),
217SOC_SINGLE("IN2A ZC Switch", WM9090_IN2_LINE_INPUT_A_VOLUME, 6, 1, 0),
218
219SOC_SINGLE("MIXOUTL Switch", WM9090_OUTPUT_MIXER3, 8, 1, 1),
220SOC_SINGLE_TLV("MIXOUTL IN1A Volume", WM9090_OUTPUT_MIXER3, 6, 3, 1,
221 mix_tlv),
222SOC_SINGLE_TLV("MIXOUTL IN2A Volume", WM9090_OUTPUT_MIXER3, 2, 3, 1,
223 mix_tlv),
224
225SOC_SINGLE("MIXOUTR Switch", WM9090_OUTPUT_MIXER4, 8, 1, 1),
226SOC_SINGLE_TLV("MIXOUTR IN1A Volume", WM9090_OUTPUT_MIXER4, 6, 3, 1,
227 mix_tlv),
228SOC_SINGLE_TLV("MIXOUTR IN2A Volume", WM9090_OUTPUT_MIXER4, 2, 3, 1,
229 mix_tlv),
230
231SOC_SINGLE("SPKMIX Switch", WM9090_SPKMIXL_ATTENUATION, 8, 1, 1),
232SOC_SINGLE_TLV("SPKMIX IN1A Volume", WM9090_SPKMIXL_ATTENUATION, 6, 3, 1,
233 mix_tlv),
234SOC_SINGLE_TLV("SPKMIX IN2A Volume", WM9090_SPKMIXL_ATTENUATION, 2, 3, 1,
235 mix_tlv),
236
237SOC_DOUBLE_R_TLV("Headphone Volume", WM9090_LEFT_OUTPUT_VOLUME,
238 WM9090_RIGHT_OUTPUT_VOLUME, 0, 63, 0, out_tlv),
239SOC_DOUBLE_R("Headphone Switch", WM9090_LEFT_OUTPUT_VOLUME,
240 WM9090_RIGHT_OUTPUT_VOLUME, 6, 1, 1),
241SOC_DOUBLE_R("Headphone ZC Switch", WM9090_LEFT_OUTPUT_VOLUME,
242 WM9090_RIGHT_OUTPUT_VOLUME, 7, 1, 0),
243
244SOC_SINGLE_TLV("Speaker Volume", WM9090_SPEAKER_VOLUME_LEFT, 0, 63, 0,
245 out_tlv),
246SOC_SINGLE("Speaker Switch", WM9090_SPEAKER_VOLUME_LEFT, 6, 1, 1),
247SOC_SINGLE("Speaker ZC Switch", WM9090_SPEAKER_VOLUME_LEFT, 7, 1, 0),
248SOC_SINGLE_TLV("Speaker Boost Volume", WM9090_CLASSD3, 3, 7, 0, spkboost_tlv),
249};
250
251static const struct snd_kcontrol_new wm9090_in1_se_controls[] = {
252SOC_SINGLE_TLV("IN1B Volume", WM9090_IN1_LINE_INPUT_B_VOLUME, 0, 6, 0,
253 in_tlv),
254SOC_SINGLE("IN1B Switch", WM9090_IN1_LINE_INPUT_B_VOLUME, 7, 1, 1),
255SOC_SINGLE("IN1B ZC Switch", WM9090_IN1_LINE_INPUT_B_VOLUME, 6, 1, 0),
256
257SOC_SINGLE_TLV("SPKMIX IN1B Volume", WM9090_SPKMIXL_ATTENUATION, 4, 3, 1,
258 mix_tlv),
259SOC_SINGLE_TLV("MIXOUTL IN1B Volume", WM9090_OUTPUT_MIXER3, 4, 3, 1,
260 mix_tlv),
261SOC_SINGLE_TLV("MIXOUTR IN1B Volume", WM9090_OUTPUT_MIXER4, 4, 3, 1,
262 mix_tlv),
263};
264
265static const struct snd_kcontrol_new wm9090_in2_se_controls[] = {
266SOC_SINGLE_TLV("IN2B Volume", WM9090_IN2_LINE_INPUT_B_VOLUME, 0, 6, 0,
267 in_tlv),
268SOC_SINGLE("IN2B Switch", WM9090_IN2_LINE_INPUT_B_VOLUME, 7, 1, 1),
269SOC_SINGLE("IN2B ZC Switch", WM9090_IN2_LINE_INPUT_B_VOLUME, 6, 1, 0),
270
271SOC_SINGLE_TLV("SPKMIX IN2B Volume", WM9090_SPKMIXL_ATTENUATION, 0, 3, 1,
272 mix_tlv),
273SOC_SINGLE_TLV("MIXOUTL IN2B Volume", WM9090_OUTPUT_MIXER3, 0, 3, 1,
274 mix_tlv),
275SOC_SINGLE_TLV("MIXOUTR IN2B Volume", WM9090_OUTPUT_MIXER4, 0, 3, 1,
276 mix_tlv),
277};
278
279static int hp_ev(struct snd_soc_dapm_widget *w,
280 struct snd_kcontrol *kcontrol, int event)
281{
282 struct snd_soc_codec *codec = w->codec;
283 unsigned int reg = snd_soc_read(codec, WM9090_ANALOGUE_HP_0);
284
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);
289
290 msleep(5);
291
292 snd_soc_update_bits(codec, WM9090_POWER_MANAGEMENT_1,
293 WM9090_HPOUT1L_ENA | WM9090_HPOUT1R_ENA,
294 WM9090_HPOUT1L_ENA | WM9090_HPOUT1R_ENA);
295
296 reg |= WM9090_HPOUT1L_DLY | WM9090_HPOUT1R_DLY;
297 snd_soc_write(codec, WM9090_ANALOGUE_HP_0, reg);
298
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.
304 */
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);
311
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;
316
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);
324
325 snd_soc_write(codec, WM9090_ANALOGUE_HP_0, reg);
326
327 snd_soc_write(codec, WM9090_DC_SERVO_0, 0);
328
329 snd_soc_update_bits(codec, WM9090_POWER_MANAGEMENT_1,
330 WM9090_HPOUT1L_ENA | WM9090_HPOUT1R_ENA,
331 0);
332
333 snd_soc_update_bits(codec, WM9090_CHARGE_PUMP_1,
334 WM9090_CP_ENA, 0);
335 break;
336 }
337
338 return 0;
339}
340
341static const struct snd_kcontrol_new spkmix[] = {
342SOC_DAPM_SINGLE("IN1A Switch", WM9090_SPEAKER_MIXER, 6, 1, 0),
343SOC_DAPM_SINGLE("IN1B Switch", WM9090_SPEAKER_MIXER, 4, 1, 0),
344SOC_DAPM_SINGLE("IN2A Switch", WM9090_SPEAKER_MIXER, 2, 1, 0),
345SOC_DAPM_SINGLE("IN2B Switch", WM9090_SPEAKER_MIXER, 0, 1, 0),
346};
347
348static const struct snd_kcontrol_new spkout[] = {
349SOC_DAPM_SINGLE("Mixer Switch", WM9090_SPKOUT_MIXERS, 4, 1, 0),
350};
351
352static const struct snd_kcontrol_new mixoutl[] = {
353SOC_DAPM_SINGLE("IN1A Switch", WM9090_OUTPUT_MIXER1, 6, 1, 0),
354SOC_DAPM_SINGLE("IN1B Switch", WM9090_OUTPUT_MIXER1, 4, 1, 0),
355SOC_DAPM_SINGLE("IN2A Switch", WM9090_OUTPUT_MIXER1, 2, 1, 0),
356SOC_DAPM_SINGLE("IN2B Switch", WM9090_OUTPUT_MIXER1, 0, 1, 0),
357};
358
359static const struct snd_kcontrol_new mixoutr[] = {
360SOC_DAPM_SINGLE("IN1A Switch", WM9090_OUTPUT_MIXER2, 6, 1, 0),
361SOC_DAPM_SINGLE("IN1B Switch", WM9090_OUTPUT_MIXER2, 4, 1, 0),
362SOC_DAPM_SINGLE("IN2A Switch", WM9090_OUTPUT_MIXER2, 2, 1, 0),
363SOC_DAPM_SINGLE("IN2B Switch", WM9090_OUTPUT_MIXER2, 0, 1, 0),
364};
365
366static const struct snd_soc_dapm_widget wm9090_dapm_widgets[] = {
367SND_SOC_DAPM_INPUT("IN1+"),
368SND_SOC_DAPM_INPUT("IN1-"),
369SND_SOC_DAPM_INPUT("IN2+"),
370SND_SOC_DAPM_INPUT("IN2-"),
371
372SND_SOC_DAPM_SUPPLY("OSC", WM9090_POWER_MANAGEMENT_1, 3, 0, NULL, 0),
373
374SND_SOC_DAPM_PGA("IN1A PGA", WM9090_POWER_MANAGEMENT_2, 7, 0, NULL, 0),
375SND_SOC_DAPM_PGA("IN1B PGA", WM9090_POWER_MANAGEMENT_2, 6, 0, NULL, 0),
376SND_SOC_DAPM_PGA("IN2A PGA", WM9090_POWER_MANAGEMENT_2, 5, 0, NULL, 0),
377SND_SOC_DAPM_PGA("IN2B PGA", WM9090_POWER_MANAGEMENT_2, 4, 0, NULL, 0),
378
379SND_SOC_DAPM_MIXER("SPKMIX", WM9090_POWER_MANAGEMENT_3, 3, 0,
380 spkmix, ARRAY_SIZE(spkmix)),
381SND_SOC_DAPM_MIXER("MIXOUTL", WM9090_POWER_MANAGEMENT_3, 5, 0,
382 mixoutl, ARRAY_SIZE(mixoutl)),
383SND_SOC_DAPM_MIXER("MIXOUTR", WM9090_POWER_MANAGEMENT_3, 4, 0,
384 mixoutr, ARRAY_SIZE(mixoutr)),
385
386SND_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),
388
389SND_SOC_DAPM_PGA("SPKPGA", WM9090_POWER_MANAGEMENT_3, 8, 0, NULL, 0),
390SND_SOC_DAPM_MIXER("SPKOUT", WM9090_POWER_MANAGEMENT_1, 12, 0,
391 spkout, ARRAY_SIZE(spkout)),
392
393SND_SOC_DAPM_OUTPUT("HPR"),
394SND_SOC_DAPM_OUTPUT("HPL"),
395SND_SOC_DAPM_OUTPUT("Speaker"),
396};
397
398static const struct snd_soc_dapm_route audio_map[] = {
399 { "IN1A PGA", NULL, "IN1+" },
400 { "IN2A PGA", NULL, "IN2+" },
401
402 { "SPKMIX", "IN1A Switch", "IN1A PGA" },
403 { "SPKMIX", "IN2A Switch", "IN2A PGA" },
404
405 { "MIXOUTL", "IN1A Switch", "IN1A PGA" },
406 { "MIXOUTL", "IN2A Switch", "IN2A PGA" },
407
408 { "MIXOUTR", "IN1A Switch", "IN1A PGA" },
409 { "MIXOUTR", "IN2A Switch", "IN2A PGA" },
410
411 { "HP PGA", NULL, "OSC" },
412 { "HP PGA", NULL, "MIXOUTL" },
413 { "HP PGA", NULL, "MIXOUTR" },
414
415 { "HPL", NULL, "HP PGA" },
416 { "HPR", NULL, "HP PGA" },
417
418 { "SPKPGA", NULL, "OSC" },
419 { "SPKPGA", NULL, "SPKMIX" },
420
421 { "SPKOUT", "Mixer Switch", "SPKPGA" },
422
423 { "Speaker", NULL, "SPKOUT" },
424};
425
426static const struct snd_soc_dapm_route audio_map_in1_se[] = {
427 { "IN1B PGA", NULL, "IN1-" },
428
429 { "SPKMIX", "IN1B Switch", "IN1B PGA" },
430 { "MIXOUTL", "IN1B Switch", "IN1B PGA" },
431 { "MIXOUTR", "IN1B Switch", "IN1B PGA" },
432};
433
434static const struct snd_soc_dapm_route audio_map_in1_diff[] = {
435 { "IN1A PGA", NULL, "IN1-" },
436};
437
438static const struct snd_soc_dapm_route audio_map_in2_se[] = {
439 { "IN2B PGA", NULL, "IN2-" },
440
441 { "SPKMIX", "IN2B Switch", "IN2B PGA" },
442 { "MIXOUTL", "IN2B Switch", "IN2B PGA" },
443 { "MIXOUTR", "IN2B Switch", "IN2B PGA" },
444};
445
446static const struct snd_soc_dapm_route audio_map_in2_diff[] = {
447 { "IN2A PGA", NULL, "IN2-" },
448};
449
450static int wm9090_add_controls(struct snd_soc_codec *codec)
451{
452 struct wm9090_priv *wm9090 = snd_soc_codec_get_drvdata(codec);
453 int i;
454
455 snd_soc_dapm_new_controls(codec, wm9090_dapm_widgets,
456 ARRAY_SIZE(wm9090_dapm_widgets));
457
458 snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
459
460 snd_soc_add_controls(codec, wm9090_controls,
461 ARRAY_SIZE(wm9090_controls));
462
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));
471 }
472
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));
481 }
482
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);
492 }
493
494 return 0;
495
496}
497
498/*
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.
501 */
502static int wm9090_set_bias_level(struct snd_soc_codec *codec,
503 enum snd_soc_bias_level level)
504{
505 u16 *reg_cache = codec->reg_cache;
506 int i, ret;
507
508 switch (level) {
509 case SND_SOC_BIAS_ON:
510 break;
511
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;
522
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;
531
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);
537 }
538 }
539
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.
543 */
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;
549
550 case SND_SOC_BIAS_OFF:
551 break;
552 }
553
554 codec->bias_level = level;
555
556 return 0;
557}
558
559static int wm9090_probe(struct platform_device *pdev)
560{
561 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
562 struct snd_soc_codec *codec;
563 int ret = 0;
564
565 if (wm9090_codec == NULL) {
566 dev_err(&pdev->dev, "Codec device not registered\n");
567 return -ENODEV;
568 }
569
570 socdev->card->codec = wm9090_codec;
571 codec = wm9090_codec;
572
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;
578 }
579
580 wm9090_add_controls(codec);
581
582 return 0;
583
584pcm_err:
585 return ret;
586}
587
588#ifdef CONFIG_PM
589static int wm9090_suspend(struct platform_device *pdev, pm_message_t state)
590{
591 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
592 struct snd_soc_codec *codec = socdev->card->codec;
593
594 wm9090_set_bias_level(codec, SND_SOC_BIAS_OFF);
595
596 return 0;
597}
598
599static int wm9090_resume(struct platform_device *pdev)
600{
601 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
602 struct snd_soc_codec *codec = socdev->card->codec;
603
604 wm9090_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
605
606 return 0;
607}
608#else
609#define wm9090_suspend NULL
610#define wm9090_resume NULL
611#endif
612
613static int wm9090_remove(struct platform_device *pdev)
614{
615 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
616
617 snd_soc_free_pcms(socdev);
618 snd_soc_dapm_free(socdev);
619
620 return 0;
621}
622
623struct snd_soc_codec_device soc_codec_dev_wm9090 = {
624 .probe = wm9090_probe,
625 .remove = wm9090_remove,
626 .suspend = wm9090_suspend,
627 .resume = wm9090_resume,
628};
629EXPORT_SYMBOL_GPL(soc_codec_dev_wm9090);
630
631static int wm9090_i2c_probe(struct i2c_client *i2c,
632 const struct i2c_device_id *id)
633{
634 struct wm9090_priv *wm9090;
635 struct snd_soc_codec *codec;
636 int ret;
637
638 wm9090 = kzalloc(sizeof(*wm9090), GFP_KERNEL);
639 if (wm9090 == NULL) {
640 dev_err(&i2c->dev, "Can not allocate memory\n");
641 return -ENOMEM;
642 }
643 codec = &wm9090->codec;
644
645 if (i2c->dev.platform_data)
646 memcpy(&wm9090->pdata, i2c->dev.platform_data,
647 sizeof(wm9090->pdata));
648
649 wm9090_codec = codec;
650
651 i2c_set_clientdata(i2c, wm9090);
652
653 mutex_init(&codec->mutex);
654 INIT_LIST_HEAD(&codec->dapm_widgets);
655 INIT_LIST_HEAD(&codec->dapm_paths);
656
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;
667
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;
672 }
673
674 memcpy(&wm9090->reg_cache, wm9090_reg_defaults,
675 sizeof(wm9090->reg_cache));
676
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;
684 }
685
686 ret = snd_soc_write(codec, WM9090_SOFTWARE_RESET, 0);
687 if (ret < 0)
688 goto err;
689
690 /* Configure some defaults; they will be written out when we
691 * bring the bias up.
692 */
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;
707
708 wm9090->reg_cache[WM9090_CLOCKING_1] |= WM9090_TOCLK_ENA;
709
710 wm9090_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
711
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;
716 }
717
718 return 0;
719
720err_bias:
721 wm9090_set_bias_level(codec, SND_SOC_BIAS_OFF);
722err:
723 kfree(wm9090);
724 i2c_set_clientdata(i2c, NULL);
725 wm9090_codec = NULL;
726
727 return ret;
728}
729
730static int wm9090_i2c_remove(struct i2c_client *i2c)
731{
732 struct wm9090_priv *wm9090 = i2c_get_clientdata(i2c);
733 struct snd_soc_codec *codec = &wm9090->codec;
734
735 snd_soc_unregister_codec(codec);
736 wm9090_set_bias_level(codec, SND_SOC_BIAS_OFF);
737 kfree(wm9090);
738 wm9090_codec = NULL;
739
740 return 0;
741}
742
743static const struct i2c_device_id wm9090_id[] = {
744 { "wm9090", 0 },
745 { }
746};
747MODULE_DEVICE_TABLE(i2c, wm9090_id);
748
749static struct i2c_driver wm9090_i2c_driver = {
750 .driver = {
751 .name = "wm9090",
752 .owner = THIS_MODULE,
753 },
754 .probe = wm9090_i2c_probe,
755 .remove = __devexit_p(wm9090_i2c_remove),
756 .id_table = wm9090_id,
757};
758
759static int __init wm9090_init(void)
760{
761 return i2c_add_driver(&wm9090_i2c_driver);
762}
763module_init(wm9090_init);
764
765static void __exit wm9090_exit(void)
766{
767 i2c_del_driver(&wm9090_i2c_driver);
768}
769module_exit(wm9090_exit);
770
771MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
772MODULE_DESCRIPTION("WM9090 ASoC driver");
773MODULE_LICENSE("GPL");