ASoC: Decouple DAPM from CODECs
[deliverable/linux.git] / sound / soc / codecs / wm9090.c
1 /*
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
37 static const u16 wm9090_reg_defaults[] = {
38 0x9093, /* R0 - Software Reset */
39 0x0006, /* R1 - Power Management (1) */
40 0x6000, /* R2 - Power Management (2) */
41 0x0000, /* R3 - Power Management (3) */
42 0x0000, /* R4 */
43 0x0000, /* R5 */
44 0x01C0, /* R6 - Clocking 1 */
45 0x0000, /* R7 */
46 0x0000, /* R8 */
47 0x0000, /* R9 */
48 0x0000, /* R10 */
49 0x0000, /* R11 */
50 0x0000, /* R12 */
51 0x0000, /* R13 */
52 0x0000, /* R14 */
53 0x0000, /* R15 */
54 0x0000, /* R16 */
55 0x0000, /* R17 */
56 0x0000, /* R18 */
57 0x0000, /* R19 */
58 0x0000, /* R20 */
59 0x0000, /* R21 */
60 0x0003, /* R22 - IN1 Line Control */
61 0x0003, /* R23 - IN2 Line Control */
62 0x0083, /* R24 - IN1 Line Input A Volume */
63 0x0083, /* R25 - IN1 Line Input B Volume */
64 0x0083, /* R26 - IN2 Line Input A Volume */
65 0x0083, /* R27 - IN2 Line Input B Volume */
66 0x002D, /* R28 - Left Output Volume */
67 0x002D, /* R29 - Right Output Volume */
68 0x0000, /* R30 */
69 0x0000, /* R31 */
70 0x0000, /* R32 */
71 0x0000, /* R33 */
72 0x0100, /* R34 - SPKMIXL Attenuation */
73 0x0000, /* R35 */
74 0x0010, /* R36 - SPKOUT Mixers */
75 0x0140, /* R37 - ClassD3 */
76 0x0039, /* R38 - Speaker Volume Left */
77 0x0000, /* R39 */
78 0x0000, /* R40 */
79 0x0000, /* R41 */
80 0x0000, /* R42 */
81 0x0000, /* R43 */
82 0x0000, /* R44 */
83 0x0000, /* R45 - Output Mixer1 */
84 0x0000, /* R46 - Output Mixer2 */
85 0x0100, /* R47 - Output Mixer3 */
86 0x0100, /* R48 - Output Mixer4 */
87 0x0000, /* R49 */
88 0x0000, /* R50 */
89 0x0000, /* R51 */
90 0x0000, /* R52 */
91 0x0000, /* R53 */
92 0x0000, /* R54 - Speaker Mixer */
93 0x0000, /* R55 */
94 0x0000, /* R56 */
95 0x000D, /* R57 - AntiPOP2 */
96 0x0000, /* R58 */
97 0x0000, /* R59 */
98 0x0000, /* R60 */
99 0x0000, /* R61 */
100 0x0000, /* R62 */
101 0x0000, /* R63 */
102 0x0000, /* R64 */
103 0x0000, /* R65 */
104 0x0000, /* R66 */
105 0x0000, /* R67 */
106 0x0000, /* R68 */
107 0x0000, /* R69 */
108 0x0000, /* R70 - Write Sequencer 0 */
109 0x0000, /* R71 - Write Sequencer 1 */
110 0x0000, /* R72 - Write Sequencer 2 */
111 0x0000, /* R73 - Write Sequencer 3 */
112 0x0000, /* R74 - Write Sequencer 4 */
113 0x0000, /* R75 - Write Sequencer 5 */
114 0x1F25, /* R76 - Charge Pump 1 */
115 0x0000, /* R77 */
116 0x0000, /* R78 */
117 0x0000, /* R79 */
118 0x0000, /* R80 */
119 0x0000, /* R81 */
120 0x0000, /* R82 */
121 0x0000, /* R83 */
122 0x0000, /* R84 - DC Servo 0 */
123 0x054A, /* R85 - DC Servo 1 */
124 0x0000, /* R86 */
125 0x0000, /* R87 - DC Servo 3 */
126 0x0000, /* R88 - DC Servo Readback 0 */
127 0x0000, /* R89 - DC Servo Readback 1 */
128 0x0000, /* R90 - DC Servo Readback 2 */
129 0x0000, /* R91 */
130 0x0000, /* R92 */
131 0x0000, /* R93 */
132 0x0000, /* R94 */
133 0x0000, /* R95 */
134 0x0100, /* R96 - Analogue HP 0 */
135 0x0000, /* R97 */
136 0x8640, /* R98 - AGC Control 0 */
137 0xC000, /* R99 - AGC Control 1 */
138 0x0200, /* R100 - AGC Control 2 */
139 };
140
141 /* This struct is used to save the context */
142 struct wm9090_priv {
143 struct mutex mutex;
144 u16 reg_cache[WM9090_MAX_REGISTER + 1];
145 struct wm9090_platform_data pdata;
146 void *control_data;
147 };
148
149 static int wm9090_volatile(unsigned int reg)
150 {
151 switch (reg) {
152 case WM9090_SOFTWARE_RESET:
153 case WM9090_DC_SERVO_0:
154 case WM9090_DC_SERVO_READBACK_0:
155 case WM9090_DC_SERVO_READBACK_1:
156 case WM9090_DC_SERVO_READBACK_2:
157 return 1;
158
159 default:
160 return 0;
161 }
162 }
163
164 static void wait_for_dc_servo(struct snd_soc_codec *codec)
165 {
166 unsigned int reg;
167 int count = 0;
168
169 dev_dbg(codec->dev, "Waiting for DC servo...\n");
170 do {
171 count++;
172 msleep(1);
173 reg = snd_soc_read(codec, WM9090_DC_SERVO_READBACK_0);
174 dev_dbg(codec->dev, "DC servo status: %x\n", reg);
175 } while ((reg & WM9090_DCS_CAL_COMPLETE_MASK)
176 != WM9090_DCS_CAL_COMPLETE_MASK && count < 1000);
177
178 if ((reg & WM9090_DCS_CAL_COMPLETE_MASK)
179 != WM9090_DCS_CAL_COMPLETE_MASK)
180 dev_err(codec->dev, "Timed out waiting for DC Servo\n");
181 }
182
183 static const unsigned int in_tlv[] = {
184 TLV_DB_RANGE_HEAD(6),
185 0, 0, TLV_DB_SCALE_ITEM(-600, 0, 0),
186 1, 3, TLV_DB_SCALE_ITEM(-350, 350, 0),
187 4, 6, TLV_DB_SCALE_ITEM(600, 600, 0),
188 };
189 static const unsigned int mix_tlv[] = {
190 TLV_DB_RANGE_HEAD(4),
191 0, 2, TLV_DB_SCALE_ITEM(-1200, 300, 0),
192 3, 3, TLV_DB_SCALE_ITEM(0, 0, 0),
193 };
194 static const DECLARE_TLV_DB_SCALE(out_tlv, -5700, 100, 0);
195 static const unsigned int spkboost_tlv[] = {
196 TLV_DB_RANGE_HEAD(7),
197 0, 6, TLV_DB_SCALE_ITEM(0, 150, 0),
198 7, 7, TLV_DB_SCALE_ITEM(1200, 0, 0),
199 };
200
201 static const struct snd_kcontrol_new wm9090_controls[] = {
202 SOC_SINGLE_TLV("IN1A Volume", WM9090_IN1_LINE_INPUT_A_VOLUME, 0, 6, 0,
203 in_tlv),
204 SOC_SINGLE("IN1A Switch", WM9090_IN1_LINE_INPUT_A_VOLUME, 7, 1, 1),
205 SOC_SINGLE("IN1A ZC Switch", WM9090_IN1_LINE_INPUT_A_VOLUME, 6, 1, 0),
206
207 SOC_SINGLE_TLV("IN2A Volume", WM9090_IN2_LINE_INPUT_A_VOLUME, 0, 6, 0,
208 in_tlv),
209 SOC_SINGLE("IN2A Switch", WM9090_IN2_LINE_INPUT_A_VOLUME, 7, 1, 1),
210 SOC_SINGLE("IN2A ZC Switch", WM9090_IN2_LINE_INPUT_A_VOLUME, 6, 1, 0),
211
212 SOC_SINGLE("MIXOUTL Switch", WM9090_OUTPUT_MIXER3, 8, 1, 1),
213 SOC_SINGLE_TLV("MIXOUTL IN1A Volume", WM9090_OUTPUT_MIXER3, 6, 3, 1,
214 mix_tlv),
215 SOC_SINGLE_TLV("MIXOUTL IN2A Volume", WM9090_OUTPUT_MIXER3, 2, 3, 1,
216 mix_tlv),
217
218 SOC_SINGLE("MIXOUTR Switch", WM9090_OUTPUT_MIXER4, 8, 1, 1),
219 SOC_SINGLE_TLV("MIXOUTR IN1A Volume", WM9090_OUTPUT_MIXER4, 6, 3, 1,
220 mix_tlv),
221 SOC_SINGLE_TLV("MIXOUTR IN2A Volume", WM9090_OUTPUT_MIXER4, 2, 3, 1,
222 mix_tlv),
223
224 SOC_SINGLE("SPKMIX Switch", WM9090_SPKMIXL_ATTENUATION, 8, 1, 1),
225 SOC_SINGLE_TLV("SPKMIX IN1A Volume", WM9090_SPKMIXL_ATTENUATION, 6, 3, 1,
226 mix_tlv),
227 SOC_SINGLE_TLV("SPKMIX IN2A Volume", WM9090_SPKMIXL_ATTENUATION, 2, 3, 1,
228 mix_tlv),
229
230 SOC_DOUBLE_R_TLV("Headphone Volume", WM9090_LEFT_OUTPUT_VOLUME,
231 WM9090_RIGHT_OUTPUT_VOLUME, 0, 63, 0, out_tlv),
232 SOC_DOUBLE_R("Headphone Switch", WM9090_LEFT_OUTPUT_VOLUME,
233 WM9090_RIGHT_OUTPUT_VOLUME, 6, 1, 1),
234 SOC_DOUBLE_R("Headphone ZC Switch", WM9090_LEFT_OUTPUT_VOLUME,
235 WM9090_RIGHT_OUTPUT_VOLUME, 7, 1, 0),
236
237 SOC_SINGLE_TLV("Speaker Volume", WM9090_SPEAKER_VOLUME_LEFT, 0, 63, 0,
238 out_tlv),
239 SOC_SINGLE("Speaker Switch", WM9090_SPEAKER_VOLUME_LEFT, 6, 1, 1),
240 SOC_SINGLE("Speaker ZC Switch", WM9090_SPEAKER_VOLUME_LEFT, 7, 1, 0),
241 SOC_SINGLE_TLV("Speaker Boost Volume", WM9090_CLASSD3, 3, 7, 0, spkboost_tlv),
242 };
243
244 static const struct snd_kcontrol_new wm9090_in1_se_controls[] = {
245 SOC_SINGLE_TLV("IN1B Volume", WM9090_IN1_LINE_INPUT_B_VOLUME, 0, 6, 0,
246 in_tlv),
247 SOC_SINGLE("IN1B Switch", WM9090_IN1_LINE_INPUT_B_VOLUME, 7, 1, 1),
248 SOC_SINGLE("IN1B ZC Switch", WM9090_IN1_LINE_INPUT_B_VOLUME, 6, 1, 0),
249
250 SOC_SINGLE_TLV("SPKMIX IN1B Volume", WM9090_SPKMIXL_ATTENUATION, 4, 3, 1,
251 mix_tlv),
252 SOC_SINGLE_TLV("MIXOUTL IN1B Volume", WM9090_OUTPUT_MIXER3, 4, 3, 1,
253 mix_tlv),
254 SOC_SINGLE_TLV("MIXOUTR IN1B Volume", WM9090_OUTPUT_MIXER4, 4, 3, 1,
255 mix_tlv),
256 };
257
258 static const struct snd_kcontrol_new wm9090_in2_se_controls[] = {
259 SOC_SINGLE_TLV("IN2B Volume", WM9090_IN2_LINE_INPUT_B_VOLUME, 0, 6, 0,
260 in_tlv),
261 SOC_SINGLE("IN2B Switch", WM9090_IN2_LINE_INPUT_B_VOLUME, 7, 1, 1),
262 SOC_SINGLE("IN2B ZC Switch", WM9090_IN2_LINE_INPUT_B_VOLUME, 6, 1, 0),
263
264 SOC_SINGLE_TLV("SPKMIX IN2B Volume", WM9090_SPKMIXL_ATTENUATION, 0, 3, 1,
265 mix_tlv),
266 SOC_SINGLE_TLV("MIXOUTL IN2B Volume", WM9090_OUTPUT_MIXER3, 0, 3, 1,
267 mix_tlv),
268 SOC_SINGLE_TLV("MIXOUTR IN2B Volume", WM9090_OUTPUT_MIXER4, 0, 3, 1,
269 mix_tlv),
270 };
271
272 static int hp_ev(struct snd_soc_dapm_widget *w,
273 struct snd_kcontrol *kcontrol, int event)
274 {
275 struct snd_soc_codec *codec = w->codec;
276 unsigned int reg = snd_soc_read(codec, WM9090_ANALOGUE_HP_0);
277
278 switch (event) {
279 case SND_SOC_DAPM_POST_PMU:
280 snd_soc_update_bits(codec, WM9090_CHARGE_PUMP_1,
281 WM9090_CP_ENA, WM9090_CP_ENA);
282
283 msleep(5);
284
285 snd_soc_update_bits(codec, WM9090_POWER_MANAGEMENT_1,
286 WM9090_HPOUT1L_ENA | WM9090_HPOUT1R_ENA,
287 WM9090_HPOUT1L_ENA | WM9090_HPOUT1R_ENA);
288
289 reg |= WM9090_HPOUT1L_DLY | WM9090_HPOUT1R_DLY;
290 snd_soc_write(codec, WM9090_ANALOGUE_HP_0, reg);
291
292 /* Start the DC servo. We don't currently use the
293 * ability to save the state since we don't have full
294 * control of the analogue paths and they can change
295 * DC offsets; see the WM8904 driver for an example of
296 * doing so.
297 */
298 snd_soc_write(codec, WM9090_DC_SERVO_0,
299 WM9090_DCS_ENA_CHAN_0 |
300 WM9090_DCS_ENA_CHAN_1 |
301 WM9090_DCS_TRIG_STARTUP_1 |
302 WM9090_DCS_TRIG_STARTUP_0);
303 wait_for_dc_servo(codec);
304
305 reg |= WM9090_HPOUT1R_OUTP | WM9090_HPOUT1R_RMV_SHORT |
306 WM9090_HPOUT1L_OUTP | WM9090_HPOUT1L_RMV_SHORT;
307 snd_soc_write(codec, WM9090_ANALOGUE_HP_0, reg);
308 break;
309
310 case SND_SOC_DAPM_PRE_PMD:
311 reg &= ~(WM9090_HPOUT1L_RMV_SHORT |
312 WM9090_HPOUT1L_DLY |
313 WM9090_HPOUT1L_OUTP |
314 WM9090_HPOUT1R_RMV_SHORT |
315 WM9090_HPOUT1R_DLY |
316 WM9090_HPOUT1R_OUTP);
317
318 snd_soc_write(codec, WM9090_ANALOGUE_HP_0, reg);
319
320 snd_soc_write(codec, WM9090_DC_SERVO_0, 0);
321
322 snd_soc_update_bits(codec, WM9090_POWER_MANAGEMENT_1,
323 WM9090_HPOUT1L_ENA | WM9090_HPOUT1R_ENA,
324 0);
325
326 snd_soc_update_bits(codec, WM9090_CHARGE_PUMP_1,
327 WM9090_CP_ENA, 0);
328 break;
329 }
330
331 return 0;
332 }
333
334 static const struct snd_kcontrol_new spkmix[] = {
335 SOC_DAPM_SINGLE("IN1A Switch", WM9090_SPEAKER_MIXER, 6, 1, 0),
336 SOC_DAPM_SINGLE("IN1B Switch", WM9090_SPEAKER_MIXER, 4, 1, 0),
337 SOC_DAPM_SINGLE("IN2A Switch", WM9090_SPEAKER_MIXER, 2, 1, 0),
338 SOC_DAPM_SINGLE("IN2B Switch", WM9090_SPEAKER_MIXER, 0, 1, 0),
339 };
340
341 static const struct snd_kcontrol_new spkout[] = {
342 SOC_DAPM_SINGLE("Mixer Switch", WM9090_SPKOUT_MIXERS, 4, 1, 0),
343 };
344
345 static const struct snd_kcontrol_new mixoutl[] = {
346 SOC_DAPM_SINGLE("IN1A Switch", WM9090_OUTPUT_MIXER1, 6, 1, 0),
347 SOC_DAPM_SINGLE("IN1B Switch", WM9090_OUTPUT_MIXER1, 4, 1, 0),
348 SOC_DAPM_SINGLE("IN2A Switch", WM9090_OUTPUT_MIXER1, 2, 1, 0),
349 SOC_DAPM_SINGLE("IN2B Switch", WM9090_OUTPUT_MIXER1, 0, 1, 0),
350 };
351
352 static const struct snd_kcontrol_new mixoutr[] = {
353 SOC_DAPM_SINGLE("IN1A Switch", WM9090_OUTPUT_MIXER2, 6, 1, 0),
354 SOC_DAPM_SINGLE("IN1B Switch", WM9090_OUTPUT_MIXER2, 4, 1, 0),
355 SOC_DAPM_SINGLE("IN2A Switch", WM9090_OUTPUT_MIXER2, 2, 1, 0),
356 SOC_DAPM_SINGLE("IN2B Switch", WM9090_OUTPUT_MIXER2, 0, 1, 0),
357 };
358
359 static const struct snd_soc_dapm_widget wm9090_dapm_widgets[] = {
360 SND_SOC_DAPM_INPUT("IN1+"),
361 SND_SOC_DAPM_INPUT("IN1-"),
362 SND_SOC_DAPM_INPUT("IN2+"),
363 SND_SOC_DAPM_INPUT("IN2-"),
364
365 SND_SOC_DAPM_SUPPLY("OSC", WM9090_POWER_MANAGEMENT_1, 3, 0, NULL, 0),
366
367 SND_SOC_DAPM_PGA("IN1A PGA", WM9090_POWER_MANAGEMENT_2, 7, 0, NULL, 0),
368 SND_SOC_DAPM_PGA("IN1B PGA", WM9090_POWER_MANAGEMENT_2, 6, 0, NULL, 0),
369 SND_SOC_DAPM_PGA("IN2A PGA", WM9090_POWER_MANAGEMENT_2, 5, 0, NULL, 0),
370 SND_SOC_DAPM_PGA("IN2B PGA", WM9090_POWER_MANAGEMENT_2, 4, 0, NULL, 0),
371
372 SND_SOC_DAPM_MIXER("SPKMIX", WM9090_POWER_MANAGEMENT_3, 3, 0,
373 spkmix, ARRAY_SIZE(spkmix)),
374 SND_SOC_DAPM_MIXER("MIXOUTL", WM9090_POWER_MANAGEMENT_3, 5, 0,
375 mixoutl, ARRAY_SIZE(mixoutl)),
376 SND_SOC_DAPM_MIXER("MIXOUTR", WM9090_POWER_MANAGEMENT_3, 4, 0,
377 mixoutr, ARRAY_SIZE(mixoutr)),
378
379 SND_SOC_DAPM_PGA_E("HP PGA", SND_SOC_NOPM, 0, 0, NULL, 0,
380 hp_ev, SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
381
382 SND_SOC_DAPM_PGA("SPKPGA", WM9090_POWER_MANAGEMENT_3, 8, 0, NULL, 0),
383 SND_SOC_DAPM_MIXER("SPKOUT", WM9090_POWER_MANAGEMENT_1, 12, 0,
384 spkout, ARRAY_SIZE(spkout)),
385
386 SND_SOC_DAPM_OUTPUT("HPR"),
387 SND_SOC_DAPM_OUTPUT("HPL"),
388 SND_SOC_DAPM_OUTPUT("Speaker"),
389 };
390
391 static const struct snd_soc_dapm_route audio_map[] = {
392 { "IN1A PGA", NULL, "IN1+" },
393 { "IN2A PGA", NULL, "IN2+" },
394
395 { "SPKMIX", "IN1A Switch", "IN1A PGA" },
396 { "SPKMIX", "IN2A Switch", "IN2A PGA" },
397
398 { "MIXOUTL", "IN1A Switch", "IN1A PGA" },
399 { "MIXOUTL", "IN2A Switch", "IN2A PGA" },
400
401 { "MIXOUTR", "IN1A Switch", "IN1A PGA" },
402 { "MIXOUTR", "IN2A Switch", "IN2A PGA" },
403
404 { "HP PGA", NULL, "OSC" },
405 { "HP PGA", NULL, "MIXOUTL" },
406 { "HP PGA", NULL, "MIXOUTR" },
407
408 { "HPL", NULL, "HP PGA" },
409 { "HPR", NULL, "HP PGA" },
410
411 { "SPKPGA", NULL, "OSC" },
412 { "SPKPGA", NULL, "SPKMIX" },
413
414 { "SPKOUT", "Mixer Switch", "SPKPGA" },
415
416 { "Speaker", NULL, "SPKOUT" },
417 };
418
419 static const struct snd_soc_dapm_route audio_map_in1_se[] = {
420 { "IN1B PGA", NULL, "IN1-" },
421
422 { "SPKMIX", "IN1B Switch", "IN1B PGA" },
423 { "MIXOUTL", "IN1B Switch", "IN1B PGA" },
424 { "MIXOUTR", "IN1B Switch", "IN1B PGA" },
425 };
426
427 static const struct snd_soc_dapm_route audio_map_in1_diff[] = {
428 { "IN1A PGA", NULL, "IN1-" },
429 };
430
431 static const struct snd_soc_dapm_route audio_map_in2_se[] = {
432 { "IN2B PGA", NULL, "IN2-" },
433
434 { "SPKMIX", "IN2B Switch", "IN2B PGA" },
435 { "MIXOUTL", "IN2B Switch", "IN2B PGA" },
436 { "MIXOUTR", "IN2B Switch", "IN2B PGA" },
437 };
438
439 static const struct snd_soc_dapm_route audio_map_in2_diff[] = {
440 { "IN2A PGA", NULL, "IN2-" },
441 };
442
443 static int wm9090_add_controls(struct snd_soc_codec *codec)
444 {
445 struct wm9090_priv *wm9090 = snd_soc_codec_get_drvdata(codec);
446 struct snd_soc_dapm_context *dapm = &codec->dapm;
447 int i;
448
449 snd_soc_dapm_new_controls(dapm, wm9090_dapm_widgets,
450 ARRAY_SIZE(wm9090_dapm_widgets));
451
452 snd_soc_dapm_add_routes(dapm, audio_map, ARRAY_SIZE(audio_map));
453
454 snd_soc_add_controls(codec, wm9090_controls,
455 ARRAY_SIZE(wm9090_controls));
456
457 if (wm9090->pdata.lin1_diff) {
458 snd_soc_dapm_add_routes(dapm, audio_map_in1_diff,
459 ARRAY_SIZE(audio_map_in1_diff));
460 } else {
461 snd_soc_dapm_add_routes(dapm, audio_map_in1_se,
462 ARRAY_SIZE(audio_map_in1_se));
463 snd_soc_add_controls(codec, wm9090_in1_se_controls,
464 ARRAY_SIZE(wm9090_in1_se_controls));
465 }
466
467 if (wm9090->pdata.lin2_diff) {
468 snd_soc_dapm_add_routes(dapm, audio_map_in2_diff,
469 ARRAY_SIZE(audio_map_in2_diff));
470 } else {
471 snd_soc_dapm_add_routes(dapm, audio_map_in2_se,
472 ARRAY_SIZE(audio_map_in2_se));
473 snd_soc_add_controls(codec, wm9090_in2_se_controls,
474 ARRAY_SIZE(wm9090_in2_se_controls));
475 }
476
477 if (wm9090->pdata.agc_ena) {
478 for (i = 0; i < ARRAY_SIZE(wm9090->pdata.agc); i++)
479 snd_soc_write(codec, WM9090_AGC_CONTROL_0 + i,
480 wm9090->pdata.agc[i]);
481 snd_soc_update_bits(codec, WM9090_POWER_MANAGEMENT_3,
482 WM9090_AGC_ENA, WM9090_AGC_ENA);
483 } else {
484 snd_soc_update_bits(codec, WM9090_POWER_MANAGEMENT_3,
485 WM9090_AGC_ENA, 0);
486 }
487
488 return 0;
489
490 }
491
492 /*
493 * The machine driver should call this from their set_bias_level; if there
494 * isn't one then this can just be set as the set_bias_level function.
495 */
496 static int wm9090_set_bias_level(struct snd_soc_codec *codec,
497 enum snd_soc_bias_level level)
498 {
499 u16 *reg_cache = codec->reg_cache;
500 int i, ret;
501
502 switch (level) {
503 case SND_SOC_BIAS_ON:
504 break;
505
506 case SND_SOC_BIAS_PREPARE:
507 snd_soc_update_bits(codec, WM9090_ANTIPOP2, WM9090_VMID_ENA,
508 WM9090_VMID_ENA);
509 snd_soc_update_bits(codec, WM9090_POWER_MANAGEMENT_1,
510 WM9090_BIAS_ENA |
511 WM9090_VMID_RES_MASK,
512 WM9090_BIAS_ENA |
513 1 << WM9090_VMID_RES_SHIFT);
514 msleep(1); /* Probably an overestimate */
515 break;
516
517 case SND_SOC_BIAS_STANDBY:
518 if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
519 /* Restore the register cache */
520 for (i = 1; i < codec->driver->reg_cache_size; i++) {
521 if (reg_cache[i] == wm9090_reg_defaults[i])
522 continue;
523 if (wm9090_volatile(i))
524 continue;
525
526 ret = snd_soc_write(codec, i, reg_cache[i]);
527 if (ret != 0)
528 dev_warn(codec->dev,
529 "Failed to restore register %d: %d\n",
530 i, ret);
531 }
532 }
533
534 /* We keep VMID off during standby since the combination of
535 * ground referenced outputs and class D speaker mean that
536 * latency is not an issue.
537 */
538 snd_soc_update_bits(codec, WM9090_POWER_MANAGEMENT_1,
539 WM9090_BIAS_ENA | WM9090_VMID_RES_MASK, 0);
540 snd_soc_update_bits(codec, WM9090_ANTIPOP2,
541 WM9090_VMID_ENA, 0);
542 break;
543
544 case SND_SOC_BIAS_OFF:
545 break;
546 }
547
548 codec->dapm.bias_level = level;
549
550 return 0;
551 }
552
553 static int wm9090_probe(struct snd_soc_codec *codec)
554 {
555 struct wm9090_priv *wm9090 = snd_soc_codec_get_drvdata(codec);
556 int ret;
557
558 codec->control_data = wm9090->control_data;
559 ret = snd_soc_codec_set_cache_io(codec, 8, 16, SND_SOC_I2C);
560 if (ret != 0) {
561 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
562 return ret;
563 }
564
565 ret = snd_soc_read(codec, WM9090_SOFTWARE_RESET);
566 if (ret < 0)
567 return ret;
568 if (ret != wm9090_reg_defaults[WM9090_SOFTWARE_RESET]) {
569 dev_err(codec->dev, "Device is not a WM9090, ID=%x\n", ret);
570 return -EINVAL;
571 }
572
573 ret = snd_soc_write(codec, WM9090_SOFTWARE_RESET, 0);
574 if (ret < 0)
575 return ret;
576
577 /* Configure some defaults; they will be written out when we
578 * bring the bias up.
579 */
580 wm9090->reg_cache[WM9090_IN1_LINE_INPUT_A_VOLUME] |= WM9090_IN1_VU
581 | WM9090_IN1A_ZC;
582 wm9090->reg_cache[WM9090_IN1_LINE_INPUT_B_VOLUME] |= WM9090_IN1_VU
583 | WM9090_IN1B_ZC;
584 wm9090->reg_cache[WM9090_IN2_LINE_INPUT_A_VOLUME] |= WM9090_IN2_VU
585 | WM9090_IN2A_ZC;
586 wm9090->reg_cache[WM9090_IN2_LINE_INPUT_B_VOLUME] |= WM9090_IN2_VU
587 | WM9090_IN2B_ZC;
588 wm9090->reg_cache[WM9090_SPEAKER_VOLUME_LEFT] |=
589 WM9090_SPKOUT_VU | WM9090_SPKOUTL_ZC;
590 wm9090->reg_cache[WM9090_LEFT_OUTPUT_VOLUME] |=
591 WM9090_HPOUT1_VU | WM9090_HPOUT1L_ZC;
592 wm9090->reg_cache[WM9090_RIGHT_OUTPUT_VOLUME] |=
593 WM9090_HPOUT1_VU | WM9090_HPOUT1R_ZC;
594
595 wm9090->reg_cache[WM9090_CLOCKING_1] |= WM9090_TOCLK_ENA;
596
597 wm9090_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
598
599 wm9090_add_controls(codec);
600
601 return 0;
602 }
603
604 #ifdef CONFIG_PM
605 static int wm9090_suspend(struct snd_soc_codec *codec, pm_message_t state)
606 {
607 wm9090_set_bias_level(codec, SND_SOC_BIAS_OFF);
608
609 return 0;
610 }
611
612 static int wm9090_resume(struct snd_soc_codec *codec)
613 {
614 wm9090_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
615
616 return 0;
617 }
618 #else
619 #define wm9090_suspend NULL
620 #define wm9090_resume NULL
621 #endif
622
623 static int wm9090_remove(struct snd_soc_codec *codec)
624 {
625 wm9090_set_bias_level(codec, SND_SOC_BIAS_OFF);
626
627 return 0;
628 }
629
630 static struct snd_soc_codec_driver soc_codec_dev_wm9090 = {
631 .probe = wm9090_probe,
632 .remove = wm9090_remove,
633 .suspend = wm9090_suspend,
634 .resume = wm9090_resume,
635 .set_bias_level = wm9090_set_bias_level,
636 .reg_cache_size = (WM9090_MAX_REGISTER + 1),
637 .reg_word_size = sizeof(u16),
638 .reg_cache_default = wm9090_reg_defaults,
639 .volatile_register = wm9090_volatile,
640 };
641
642 static int wm9090_i2c_probe(struct i2c_client *i2c,
643 const struct i2c_device_id *id)
644 {
645 struct wm9090_priv *wm9090;
646 int ret;
647
648 wm9090 = kzalloc(sizeof(*wm9090), GFP_KERNEL);
649 if (wm9090 == NULL) {
650 dev_err(&i2c->dev, "Can not allocate memory\n");
651 return -ENOMEM;
652 }
653
654 if (i2c->dev.platform_data)
655 memcpy(&wm9090->pdata, i2c->dev.platform_data,
656 sizeof(wm9090->pdata));
657
658 i2c_set_clientdata(i2c, wm9090);
659 wm9090->control_data = i2c;
660 mutex_init(&wm9090->mutex);
661
662 ret = snd_soc_register_codec(&i2c->dev,
663 &soc_codec_dev_wm9090, NULL, 0);
664 if (ret < 0)
665 kfree(wm9090);
666 return ret;
667 }
668
669 static int __devexit wm9090_i2c_remove(struct i2c_client *i2c)
670 {
671 struct wm9090_priv *wm9090 = i2c_get_clientdata(i2c);
672
673 snd_soc_unregister_codec(&i2c->dev);
674 kfree(wm9090);
675
676 return 0;
677 }
678
679 static const struct i2c_device_id wm9090_id[] = {
680 { "wm9090", 0 },
681 { }
682 };
683 MODULE_DEVICE_TABLE(i2c, wm9090_id);
684
685 static struct i2c_driver wm9090_i2c_driver = {
686 .driver = {
687 .name = "wm9090-codec",
688 .owner = THIS_MODULE,
689 },
690 .probe = wm9090_i2c_probe,
691 .remove = __devexit_p(wm9090_i2c_remove),
692 .id_table = wm9090_id,
693 };
694
695 static int __init wm9090_init(void)
696 {
697 return i2c_add_driver(&wm9090_i2c_driver);
698 }
699 module_init(wm9090_init);
700
701 static void __exit wm9090_exit(void)
702 {
703 i2c_del_driver(&wm9090_i2c_driver);
704 }
705 module_exit(wm9090_exit);
706
707 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
708 MODULE_DESCRIPTION("WM9090 ASoC driver");
709 MODULE_LICENSE("GPL");
This page took 0.076758 seconds and 5 git commands to generate.