blob: 3a1db4a76a648c88ec9e1e1ae9c94eff54203752 [file] [log] [blame]
Mark Brown9a76f1f2010-08-05 13:20:59 +01001/*
2 * wm8962.c -- WM8962 ALSA SoC Audio driver
3 *
4 * Copyright 2010 Wolfson Microelectronics plc
5 *
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7 *
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/init.h>
17#include <linux/delay.h>
18#include <linux/pm.h>
19#include <linux/gcd.h>
Mark Brown3367b8d2010-09-20 17:34:58 +010020#include <linux/gpio.h>
Mark Brown9a76f1f2010-08-05 13:20:59 +010021#include <linux/i2c.h>
22#include <linux/input.h>
23#include <linux/platform_device.h>
24#include <linux/regulator/consumer.h>
25#include <linux/slab.h>
26#include <linux/workqueue.h>
27#include <sound/core.h>
Mark Brown77113082010-09-30 15:37:53 -070028#include <sound/jack.h>
Mark Brown9a76f1f2010-08-05 13:20:59 +010029#include <sound/pcm.h>
30#include <sound/pcm_params.h>
31#include <sound/soc.h>
32#include <sound/soc-dapm.h>
33#include <sound/initval.h>
34#include <sound/tlv.h>
35#include <sound/wm8962.h>
36
37#include "wm8962.h"
38
Mark Brown9a76f1f2010-08-05 13:20:59 +010039#define WM8962_NUM_SUPPLIES 8
40static const char *wm8962_supply_names[WM8962_NUM_SUPPLIES] = {
41 "DCVDD",
42 "DBVDD",
43 "AVDD",
44 "CPVDD",
45 "MICVDD",
46 "PLLVDD",
47 "SPKVDD1",
48 "SPKVDD2",
49};
50
51/* codec private data */
52struct wm8962_priv {
Mark Brown54d8d0a2010-08-12 15:02:11 +010053 struct snd_soc_codec *codec;
54
Mark Brown9a76f1f2010-08-05 13:20:59 +010055 u16 reg_cache[WM8962_MAX_REGISTER + 1];
56
57 int sysclk;
58 int sysclk_rate;
59
60 int bclk; /* Desired BCLK */
61 int lrclk;
62
63 int fll_src;
64 int fll_fref;
65 int fll_fout;
66
Mark Brown77113082010-09-30 15:37:53 -070067 struct delayed_work mic_work;
68 struct snd_soc_jack *jack;
69
Mark Brown9a76f1f2010-08-05 13:20:59 +010070 struct regulator_bulk_data supplies[WM8962_NUM_SUPPLIES];
71 struct notifier_block disable_nb[WM8962_NUM_SUPPLIES];
72
73#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
74 struct input_dev *beep;
75 struct work_struct beep_work;
76 int beep_rate;
77#endif
Mark Brown3367b8d2010-09-20 17:34:58 +010078
79#ifdef CONFIG_GPIOLIB
80 struct gpio_chip gpio_chip;
81#endif
Mark Brown9a76f1f2010-08-05 13:20:59 +010082};
83
84/* We can't use the same notifier block for more than one supply and
85 * there's no way I can see to get from a callback to the caller
86 * except container_of().
87 */
88#define WM8962_REGULATOR_EVENT(n) \
89static int wm8962_regulator_event_##n(struct notifier_block *nb, \
90 unsigned long event, void *data) \
91{ \
92 struct wm8962_priv *wm8962 = container_of(nb, struct wm8962_priv, \
93 disable_nb[n]); \
94 if (event & REGULATOR_EVENT_DISABLE) { \
Mark Brown54d8d0a2010-08-12 15:02:11 +010095 wm8962->codec->cache_sync = 1; \
Mark Brown9a76f1f2010-08-05 13:20:59 +010096 } \
97 return 0; \
98}
99
100WM8962_REGULATOR_EVENT(0)
101WM8962_REGULATOR_EVENT(1)
102WM8962_REGULATOR_EVENT(2)
103WM8962_REGULATOR_EVENT(3)
104WM8962_REGULATOR_EVENT(4)
105WM8962_REGULATOR_EVENT(5)
106WM8962_REGULATOR_EVENT(6)
107WM8962_REGULATOR_EVENT(7)
108
109static int wm8962_volatile_register(unsigned int reg)
110{
111 if (wm8962_reg_access[reg].vol)
112 return 1;
113 else
114 return 0;
115}
116
Mark Brown54d8d0a2010-08-12 15:02:11 +0100117static int wm8962_readable_register(unsigned int reg)
Mark Brown9a76f1f2010-08-05 13:20:59 +0100118{
119 if (wm8962_reg_access[reg].read)
120 return 1;
121 else
122 return 0;
123}
124
125static int wm8962_reset(struct snd_soc_codec *codec)
126{
127 return snd_soc_write(codec, WM8962_SOFTWARE_RESET, 0);
128}
129
130static const DECLARE_TLV_DB_SCALE(inpga_tlv, -2325, 75, 0);
131static const DECLARE_TLV_DB_SCALE(mixin_tlv, -1500, 300, 0);
132static const unsigned int mixinpga_tlv[] = {
133 TLV_DB_RANGE_HEAD(7),
134 0, 1, TLV_DB_SCALE_ITEM(0, 600, 0),
135 2, 2, TLV_DB_SCALE_ITEM(1300, 1300, 0),
136 3, 4, TLV_DB_SCALE_ITEM(1800, 200, 0),
137 5, 5, TLV_DB_SCALE_ITEM(2400, 0, 0),
138 6, 7, TLV_DB_SCALE_ITEM(2700, 300, 0),
139};
140static const DECLARE_TLV_DB_SCALE(beep_tlv, -9600, 600, 1);
141static const DECLARE_TLV_DB_SCALE(digital_tlv, -7200, 75, 1);
142static const DECLARE_TLV_DB_SCALE(st_tlv, -3600, 300, 0);
143static const DECLARE_TLV_DB_SCALE(inmix_tlv, -600, 600, 0);
144static const DECLARE_TLV_DB_SCALE(bypass_tlv, -1500, 300, 0);
145static const DECLARE_TLV_DB_SCALE(out_tlv, -12100, 100, 1);
146static const DECLARE_TLV_DB_SCALE(hp_tlv, -700, 100, 0);
147static const unsigned int classd_tlv[] = {
148 TLV_DB_RANGE_HEAD(7),
149 0, 6, TLV_DB_SCALE_ITEM(0, 150, 0),
150 7, 7, TLV_DB_SCALE_ITEM(1200, 0, 0),
151};
152
153/* The VU bits for the headphones are in a different register to the mute
154 * bits and only take effect on the PGA if it is actually powered.
155 */
156static int wm8962_put_hp_sw(struct snd_kcontrol *kcontrol,
157 struct snd_ctl_elem_value *ucontrol)
158{
159 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Mark Brown54d8d0a2010-08-12 15:02:11 +0100160 struct wm8962_priv *wm8962 = snd_soc_codec_get_drvdata(codec);
161 u16 *reg_cache = wm8962->reg_cache;
Mark Brown9a76f1f2010-08-05 13:20:59 +0100162 int ret;
163
164 /* Apply the update (if any) */
165 ret = snd_soc_put_volsw(kcontrol, ucontrol);
166 if (ret == 0)
167 return 0;
168
169 /* If the left PGA is enabled hit that VU bit... */
170 if (reg_cache[WM8962_PWR_MGMT_2] & WM8962_HPOUTL_PGA_ENA)
171 return snd_soc_write(codec, WM8962_HPOUTL_VOLUME,
172 reg_cache[WM8962_HPOUTL_VOLUME]);
173
174 /* ...otherwise the right. The VU is stereo. */
175 if (reg_cache[WM8962_PWR_MGMT_2] & WM8962_HPOUTR_PGA_ENA)
176 return snd_soc_write(codec, WM8962_HPOUTR_VOLUME,
177 reg_cache[WM8962_HPOUTR_VOLUME]);
178
179 return 0;
180}
181
182/* The VU bits for the speakers are in a different register to the mute
183 * bits and only take effect on the PGA if it is actually powered.
184 */
185static int wm8962_put_spk_sw(struct snd_kcontrol *kcontrol,
186 struct snd_ctl_elem_value *ucontrol)
187{
188 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Mark Brown54d8d0a2010-08-12 15:02:11 +0100189 struct wm8962_priv *wm8962 = snd_soc_codec_get_drvdata(codec);
190 u16 *reg_cache = wm8962->reg_cache;
Mark Brown9a76f1f2010-08-05 13:20:59 +0100191 int ret;
192
193 /* Apply the update (if any) */
194 ret = snd_soc_put_volsw(kcontrol, ucontrol);
195 if (ret == 0)
196 return 0;
197
198 /* If the left PGA is enabled hit that VU bit... */
199 if (reg_cache[WM8962_PWR_MGMT_2] & WM8962_SPKOUTL_PGA_ENA)
200 return snd_soc_write(codec, WM8962_SPKOUTL_VOLUME,
201 reg_cache[WM8962_SPKOUTL_VOLUME]);
202
203 /* ...otherwise the right. The VU is stereo. */
204 if (reg_cache[WM8962_PWR_MGMT_2] & WM8962_SPKOUTR_PGA_ENA)
205 return snd_soc_write(codec, WM8962_SPKOUTR_VOLUME,
206 reg_cache[WM8962_SPKOUTR_VOLUME]);
207
208 return 0;
209}
210
211static const struct snd_kcontrol_new wm8962_snd_controls[] = {
212SOC_DOUBLE("Input Mixer Switch", WM8962_INPUT_MIXER_CONTROL_1, 3, 2, 1, 1),
213
214SOC_SINGLE_TLV("MIXINL IN2L Volume", WM8962_LEFT_INPUT_MIXER_VOLUME, 6, 7, 0,
215 mixin_tlv),
216SOC_SINGLE_TLV("MIXINL PGA Volume", WM8962_LEFT_INPUT_MIXER_VOLUME, 3, 7, 0,
217 mixinpga_tlv),
218SOC_SINGLE_TLV("MIXINL IN3L Volume", WM8962_LEFT_INPUT_MIXER_VOLUME, 0, 7, 0,
219 mixin_tlv),
220
221SOC_SINGLE_TLV("MIXINR IN2R Volume", WM8962_RIGHT_INPUT_MIXER_VOLUME, 6, 7, 0,
222 mixin_tlv),
223SOC_SINGLE_TLV("MIXINR PGA Volume", WM8962_RIGHT_INPUT_MIXER_VOLUME, 3, 7, 0,
224 mixinpga_tlv),
225SOC_SINGLE_TLV("MIXINR IN3R Volume", WM8962_RIGHT_INPUT_MIXER_VOLUME, 0, 7, 0,
226 mixin_tlv),
227
228SOC_DOUBLE_R_TLV("Digital Capture Volume", WM8962_LEFT_ADC_VOLUME,
229 WM8962_RIGHT_ADC_VOLUME, 1, 127, 0, digital_tlv),
230SOC_DOUBLE_R_TLV("Capture Volume", WM8962_LEFT_INPUT_VOLUME,
231 WM8962_RIGHT_INPUT_VOLUME, 0, 63, 0, inpga_tlv),
232SOC_DOUBLE_R("Capture Switch", WM8962_LEFT_INPUT_VOLUME,
233 WM8962_RIGHT_INPUT_VOLUME, 7, 1, 1),
234SOC_DOUBLE_R("Capture ZC Switch", WM8962_LEFT_INPUT_VOLUME,
235 WM8962_RIGHT_INPUT_VOLUME, 6, 1, 1),
236
237SOC_DOUBLE_R_TLV("Sidetone Volume", WM8962_DAC_DSP_MIXING_1,
238 WM8962_DAC_DSP_MIXING_2, 4, 12, 0, st_tlv),
239
240SOC_DOUBLE_R_TLV("Digital Playback Volume", WM8962_LEFT_DAC_VOLUME,
241 WM8962_RIGHT_DAC_VOLUME, 1, 127, 0, digital_tlv),
242SOC_SINGLE("DAC High Performance Switch", WM8962_ADC_DAC_CONTROL_2, 0, 1, 0),
243
244SOC_SINGLE("ADC High Performance Switch", WM8962_ADDITIONAL_CONTROL_1,
245 5, 1, 0),
246
247SOC_SINGLE_TLV("Beep Volume", WM8962_BEEP_GENERATOR_1, 4, 15, 0, beep_tlv),
248
249SOC_DOUBLE_R_TLV("Headphone Volume", WM8962_HPOUTL_VOLUME,
250 WM8962_HPOUTR_VOLUME, 0, 127, 0, out_tlv),
251SOC_DOUBLE_EXT("Headphone Switch", WM8962_PWR_MGMT_2, 1, 0, 1, 1,
252 snd_soc_get_volsw, wm8962_put_hp_sw),
253SOC_DOUBLE_R("Headphone ZC Switch", WM8962_HPOUTL_VOLUME, WM8962_HPOUTR_VOLUME,
254 7, 1, 0),
255SOC_DOUBLE_TLV("Headphone Aux Volume", WM8962_ANALOGUE_HP_2, 3, 6, 7, 0,
256 hp_tlv),
257
258SOC_DOUBLE_R("Headphone Mixer Switch", WM8962_HEADPHONE_MIXER_3,
259 WM8962_HEADPHONE_MIXER_4, 8, 1, 1),
260
261SOC_SINGLE_TLV("HPMIXL IN4L Volume", WM8962_HEADPHONE_MIXER_3,
262 3, 7, 0, bypass_tlv),
263SOC_SINGLE_TLV("HPMIXL IN4R Volume", WM8962_HEADPHONE_MIXER_3,
264 0, 7, 0, bypass_tlv),
265SOC_SINGLE_TLV("HPMIXL MIXINL Volume", WM8962_HEADPHONE_MIXER_3,
266 7, 1, 1, inmix_tlv),
267SOC_SINGLE_TLV("HPMIXL MIXINR Volume", WM8962_HEADPHONE_MIXER_3,
268 6, 1, 1, inmix_tlv),
269
270SOC_SINGLE_TLV("HPMIXR IN4L Volume", WM8962_HEADPHONE_MIXER_4,
271 3, 7, 0, bypass_tlv),
272SOC_SINGLE_TLV("HPMIXR IN4R Volume", WM8962_HEADPHONE_MIXER_4,
273 0, 7, 0, bypass_tlv),
274SOC_SINGLE_TLV("HPMIXR MIXINL Volume", WM8962_HEADPHONE_MIXER_4,
275 7, 1, 1, inmix_tlv),
276SOC_SINGLE_TLV("HPMIXR MIXINR Volume", WM8962_HEADPHONE_MIXER_4,
277 6, 1, 1, inmix_tlv),
278
279SOC_SINGLE_TLV("Speaker Boost Volume", WM8962_CLASS_D_CONTROL_2, 0, 7, 0,
280 classd_tlv),
281};
282
283static const struct snd_kcontrol_new wm8962_spk_mono_controls[] = {
284SOC_SINGLE_TLV("Speaker Volume", WM8962_SPKOUTL_VOLUME, 0, 127, 0, out_tlv),
285SOC_SINGLE_EXT("Speaker Switch", WM8962_CLASS_D_CONTROL_1, 1, 1, 1,
286 snd_soc_get_volsw, wm8962_put_spk_sw),
287SOC_SINGLE("Speaker ZC Switch", WM8962_SPKOUTL_VOLUME, 7, 1, 0),
288
289SOC_SINGLE("Speaker Mixer Switch", WM8962_SPEAKER_MIXER_3, 8, 1, 1),
290SOC_SINGLE_TLV("Speaker Mixer IN4L Volume", WM8962_SPEAKER_MIXER_3,
291 3, 7, 0, bypass_tlv),
292SOC_SINGLE_TLV("Speaker Mixer IN4R Volume", WM8962_SPEAKER_MIXER_3,
293 0, 7, 0, bypass_tlv),
294SOC_SINGLE_TLV("Speaker Mixer MIXINL Volume", WM8962_SPEAKER_MIXER_3,
295 7, 1, 1, inmix_tlv),
296SOC_SINGLE_TLV("Speaker Mixer MIXINR Volume", WM8962_SPEAKER_MIXER_3,
297 6, 1, 1, inmix_tlv),
298SOC_SINGLE_TLV("Speaker Mixer DACL Volume", WM8962_SPEAKER_MIXER_5,
299 7, 1, 0, inmix_tlv),
300SOC_SINGLE_TLV("Speaker Mixer DACR Volume", WM8962_SPEAKER_MIXER_5,
301 6, 1, 0, inmix_tlv),
302};
303
304static const struct snd_kcontrol_new wm8962_spk_stereo_controls[] = {
305SOC_DOUBLE_R_TLV("Speaker Volume", WM8962_SPKOUTL_VOLUME,
306 WM8962_SPKOUTR_VOLUME, 0, 127, 0, out_tlv),
307SOC_DOUBLE_EXT("Speaker Switch", WM8962_CLASS_D_CONTROL_1, 1, 0, 1, 1,
308 snd_soc_get_volsw, wm8962_put_spk_sw),
309SOC_DOUBLE_R("Speaker ZC Switch", WM8962_SPKOUTL_VOLUME, WM8962_SPKOUTR_VOLUME,
310 7, 1, 0),
311
312SOC_DOUBLE_R("Speaker Mixer Switch", WM8962_SPEAKER_MIXER_3,
313 WM8962_SPEAKER_MIXER_4, 8, 1, 1),
314
315SOC_SINGLE_TLV("SPKOUTL Mixer IN4L Volume", WM8962_SPEAKER_MIXER_3,
316 3, 7, 0, bypass_tlv),
317SOC_SINGLE_TLV("SPKOUTL Mixer IN4R Volume", WM8962_SPEAKER_MIXER_3,
318 0, 7, 0, bypass_tlv),
319SOC_SINGLE_TLV("SPKOUTL Mixer MIXINL Volume", WM8962_SPEAKER_MIXER_3,
320 7, 1, 1, inmix_tlv),
321SOC_SINGLE_TLV("SPKOUTL Mixer MIXINR Volume", WM8962_SPEAKER_MIXER_3,
322 6, 1, 1, inmix_tlv),
323SOC_SINGLE_TLV("SPKOUTL Mixer DACL Volume", WM8962_SPEAKER_MIXER_5,
324 7, 1, 0, inmix_tlv),
325SOC_SINGLE_TLV("SPKOUTL Mixer DACR Volume", WM8962_SPEAKER_MIXER_5,
326 6, 1, 0, inmix_tlv),
327
328SOC_SINGLE_TLV("SPKOUTR Mixer IN4L Volume", WM8962_SPEAKER_MIXER_4,
329 3, 7, 0, bypass_tlv),
330SOC_SINGLE_TLV("SPKOUTR Mixer IN4R Volume", WM8962_SPEAKER_MIXER_4,
331 0, 7, 0, bypass_tlv),
332SOC_SINGLE_TLV("SPKOUTR Mixer MIXINL Volume", WM8962_SPEAKER_MIXER_4,
333 7, 1, 1, inmix_tlv),
334SOC_SINGLE_TLV("SPKOUTR Mixer MIXINR Volume", WM8962_SPEAKER_MIXER_4,
335 6, 1, 1, inmix_tlv),
336SOC_SINGLE_TLV("SPKOUTR Mixer DACL Volume", WM8962_SPEAKER_MIXER_5,
337 5, 1, 0, inmix_tlv),
338SOC_SINGLE_TLV("SPKOUTR Mixer DACR Volume", WM8962_SPEAKER_MIXER_5,
339 4, 1, 0, inmix_tlv),
340};
341
342static int sysclk_event(struct snd_soc_dapm_widget *w,
343 struct snd_kcontrol *kcontrol, int event)
344{
345 struct snd_soc_codec *codec = w->codec;
346 int src;
347 int fll;
348
349 src = snd_soc_read(codec, WM8962_CLOCKING2) & WM8962_SYSCLK_SRC_MASK;
350
351 switch (src) {
352 case 0: /* MCLK */
353 fll = 0;
354 break;
355 case 0x200: /* FLL */
356 fll = 1;
357 break;
358 default:
359 dev_err(codec->dev, "Unknown SYSCLK source %x\n", src);
360 return -EINVAL;
361 }
362
363 switch (event) {
364 case SND_SOC_DAPM_PRE_PMU:
365 if (fll)
366 snd_soc_update_bits(codec, WM8962_FLL_CONTROL_1,
367 WM8962_FLL_ENA, WM8962_FLL_ENA);
368 break;
369
370 case SND_SOC_DAPM_POST_PMD:
371 if (fll)
372 snd_soc_update_bits(codec, WM8962_FLL_CONTROL_1,
373 WM8962_FLL_ENA, 0);
374 break;
375
376 default:
377 BUG();
378 return -EINVAL;
379 }
380
381 return 0;
382}
383
384static int cp_event(struct snd_soc_dapm_widget *w,
385 struct snd_kcontrol *kcontrol, int event)
386{
387 switch (event) {
388 case SND_SOC_DAPM_POST_PMU:
389 msleep(5);
390 break;
391
392 default:
393 BUG();
394 return -EINVAL;
395 }
396
397 return 0;
398}
399
400static int hp_event(struct snd_soc_dapm_widget *w,
401 struct snd_kcontrol *kcontrol, int event)
402{
403 struct snd_soc_codec *codec = w->codec;
404 int timeout;
405 int reg;
406 int expected = (WM8962_DCS_STARTUP_DONE_HP1L |
407 WM8962_DCS_STARTUP_DONE_HP1R);
408
409 switch (event) {
410 case SND_SOC_DAPM_POST_PMU:
411 snd_soc_update_bits(codec, WM8962_ANALOGUE_HP_0,
412 WM8962_HP1L_ENA | WM8962_HP1R_ENA,
413 WM8962_HP1L_ENA | WM8962_HP1R_ENA);
414 udelay(20);
415
416 snd_soc_update_bits(codec, WM8962_ANALOGUE_HP_0,
417 WM8962_HP1L_ENA_DLY | WM8962_HP1R_ENA_DLY,
418 WM8962_HP1L_ENA_DLY | WM8962_HP1R_ENA_DLY);
419
420 /* Start the DC servo */
421 snd_soc_update_bits(codec, WM8962_DC_SERVO_1,
422 WM8962_HP1L_DCS_ENA | WM8962_HP1R_DCS_ENA |
423 WM8962_HP1L_DCS_STARTUP |
424 WM8962_HP1R_DCS_STARTUP,
425 WM8962_HP1L_DCS_ENA | WM8962_HP1R_DCS_ENA |
426 WM8962_HP1L_DCS_STARTUP |
427 WM8962_HP1R_DCS_STARTUP);
428
429 /* Wait for it to complete, should be well under 100ms */
430 timeout = 0;
431 do {
432 msleep(1);
433 reg = snd_soc_read(codec, WM8962_DC_SERVO_6);
434 if (reg < 0) {
435 dev_err(codec->dev,
436 "Failed to read DCS status: %d\n",
437 reg);
438 continue;
439 }
440 dev_dbg(codec->dev, "DCS status: %x\n", reg);
441 } while (++timeout < 200 && (reg & expected) != expected);
442
443 if ((reg & expected) != expected)
444 dev_err(codec->dev, "DC servo timed out\n");
445 else
446 dev_dbg(codec->dev, "DC servo complete after %dms\n",
447 timeout);
448
449 snd_soc_update_bits(codec, WM8962_ANALOGUE_HP_0,
450 WM8962_HP1L_ENA_OUTP |
451 WM8962_HP1R_ENA_OUTP,
452 WM8962_HP1L_ENA_OUTP |
453 WM8962_HP1R_ENA_OUTP);
454 udelay(20);
455
456 snd_soc_update_bits(codec, WM8962_ANALOGUE_HP_0,
457 WM8962_HP1L_RMV_SHORT |
458 WM8962_HP1R_RMV_SHORT,
459 WM8962_HP1L_RMV_SHORT |
460 WM8962_HP1R_RMV_SHORT);
461 break;
462
463 case SND_SOC_DAPM_PRE_PMD:
464 snd_soc_update_bits(codec, WM8962_ANALOGUE_HP_0,
465 WM8962_HP1L_RMV_SHORT |
466 WM8962_HP1R_RMV_SHORT, 0);
467
468 udelay(20);
469
470 snd_soc_update_bits(codec, WM8962_DC_SERVO_1,
471 WM8962_HP1L_DCS_ENA | WM8962_HP1R_DCS_ENA |
472 WM8962_HP1L_DCS_STARTUP |
473 WM8962_HP1R_DCS_STARTUP,
474 0);
475
476 snd_soc_update_bits(codec, WM8962_ANALOGUE_HP_0,
477 WM8962_HP1L_ENA | WM8962_HP1R_ENA |
478 WM8962_HP1L_ENA_DLY | WM8962_HP1R_ENA_DLY |
479 WM8962_HP1L_ENA_OUTP |
480 WM8962_HP1R_ENA_OUTP, 0);
481
482 break;
483
484 default:
485 BUG();
486 return -EINVAL;
487
488 }
489
490 return 0;
491}
492
493/* VU bits for the output PGAs only take effect while the PGA is powered */
494static int out_pga_event(struct snd_soc_dapm_widget *w,
495 struct snd_kcontrol *kcontrol, int event)
496{
497 struct snd_soc_codec *codec = w->codec;
Mark Brown54d8d0a2010-08-12 15:02:11 +0100498 struct wm8962_priv *wm8962 = snd_soc_codec_get_drvdata(codec);
499 u16 *reg_cache = wm8962->reg_cache;
Mark Brown9a76f1f2010-08-05 13:20:59 +0100500 int reg;
501
502 switch (w->shift) {
503 case WM8962_HPOUTR_PGA_ENA_SHIFT:
504 reg = WM8962_HPOUTR_VOLUME;
505 break;
506 case WM8962_HPOUTL_PGA_ENA_SHIFT:
507 reg = WM8962_HPOUTL_VOLUME;
508 break;
509 case WM8962_SPKOUTR_PGA_ENA_SHIFT:
510 reg = WM8962_SPKOUTR_VOLUME;
511 break;
512 case WM8962_SPKOUTL_PGA_ENA_SHIFT:
513 reg = WM8962_SPKOUTL_VOLUME;
514 break;
515 default:
516 BUG();
517 return -EINVAL;
518 }
519
520 switch (event) {
521 case SND_SOC_DAPM_POST_PMU:
522 return snd_soc_write(codec, reg, reg_cache[reg]);
523 default:
524 BUG();
525 return -EINVAL;
526 }
527}
528
529static const char *st_text[] = { "None", "Right", "Left" };
530
531static const struct soc_enum str_enum =
532 SOC_ENUM_SINGLE(WM8962_DAC_DSP_MIXING_1, 2, 3, st_text);
533
534static const struct snd_kcontrol_new str_mux =
535 SOC_DAPM_ENUM("Right Sidetone", str_enum);
536
537static const struct soc_enum stl_enum =
538 SOC_ENUM_SINGLE(WM8962_DAC_DSP_MIXING_2, 2, 3, st_text);
539
540static const struct snd_kcontrol_new stl_mux =
541 SOC_DAPM_ENUM("Left Sidetone", stl_enum);
542
543static const char *outmux_text[] = { "DAC", "Mixer" };
544
545static const struct soc_enum spkoutr_enum =
546 SOC_ENUM_SINGLE(WM8962_SPEAKER_MIXER_2, 7, 2, outmux_text);
547
548static const struct snd_kcontrol_new spkoutr_mux =
549 SOC_DAPM_ENUM("SPKOUTR Mux", spkoutr_enum);
550
551static const struct soc_enum spkoutl_enum =
552 SOC_ENUM_SINGLE(WM8962_SPEAKER_MIXER_1, 7, 2, outmux_text);
553
554static const struct snd_kcontrol_new spkoutl_mux =
555 SOC_DAPM_ENUM("SPKOUTL Mux", spkoutl_enum);
556
557static const struct soc_enum hpoutr_enum =
558 SOC_ENUM_SINGLE(WM8962_HEADPHONE_MIXER_2, 7, 2, outmux_text);
559
560static const struct snd_kcontrol_new hpoutr_mux =
561 SOC_DAPM_ENUM("HPOUTR Mux", hpoutr_enum);
562
563static const struct soc_enum hpoutl_enum =
564 SOC_ENUM_SINGLE(WM8962_HEADPHONE_MIXER_1, 7, 2, outmux_text);
565
566static const struct snd_kcontrol_new hpoutl_mux =
567 SOC_DAPM_ENUM("HPOUTL Mux", hpoutl_enum);
568
569static const struct snd_kcontrol_new inpgal[] = {
570SOC_DAPM_SINGLE("IN1L Switch", WM8962_LEFT_INPUT_PGA_CONTROL, 3, 1, 0),
571SOC_DAPM_SINGLE("IN2L Switch", WM8962_LEFT_INPUT_PGA_CONTROL, 2, 1, 0),
572SOC_DAPM_SINGLE("IN3L Switch", WM8962_LEFT_INPUT_PGA_CONTROL, 1, 1, 0),
573SOC_DAPM_SINGLE("IN4L Switch", WM8962_LEFT_INPUT_PGA_CONTROL, 0, 1, 0),
574};
575
576static const struct snd_kcontrol_new inpgar[] = {
577SOC_DAPM_SINGLE("IN1R Switch", WM8962_RIGHT_INPUT_PGA_CONTROL, 3, 1, 0),
578SOC_DAPM_SINGLE("IN2R Switch", WM8962_RIGHT_INPUT_PGA_CONTROL, 2, 1, 0),
579SOC_DAPM_SINGLE("IN3R Switch", WM8962_RIGHT_INPUT_PGA_CONTROL, 1, 1, 0),
580SOC_DAPM_SINGLE("IN4R Switch", WM8962_RIGHT_INPUT_PGA_CONTROL, 0, 1, 0),
581};
582
583static const struct snd_kcontrol_new mixinl[] = {
584SOC_DAPM_SINGLE("IN2L Switch", WM8962_INPUT_MIXER_CONTROL_2, 5, 1, 0),
585SOC_DAPM_SINGLE("IN3L Switch", WM8962_INPUT_MIXER_CONTROL_2, 4, 1, 0),
586SOC_DAPM_SINGLE("PGA Switch", WM8962_INPUT_MIXER_CONTROL_2, 3, 1, 0),
587};
588
589static const struct snd_kcontrol_new mixinr[] = {
590SOC_DAPM_SINGLE("IN2R Switch", WM8962_INPUT_MIXER_CONTROL_2, 2, 1, 0),
591SOC_DAPM_SINGLE("IN3R Switch", WM8962_INPUT_MIXER_CONTROL_2, 1, 1, 0),
592SOC_DAPM_SINGLE("PGA Switch", WM8962_INPUT_MIXER_CONTROL_2, 0, 1, 0),
593};
594
595static const struct snd_kcontrol_new hpmixl[] = {
596SOC_DAPM_SINGLE("DACL Switch", WM8962_HEADPHONE_MIXER_1, 5, 1, 0),
597SOC_DAPM_SINGLE("DACR Switch", WM8962_HEADPHONE_MIXER_1, 4, 1, 0),
598SOC_DAPM_SINGLE("MIXINL Switch", WM8962_HEADPHONE_MIXER_1, 3, 1, 0),
599SOC_DAPM_SINGLE("MIXINR Switch", WM8962_HEADPHONE_MIXER_1, 2, 1, 0),
600SOC_DAPM_SINGLE("IN4L Switch", WM8962_HEADPHONE_MIXER_1, 1, 1, 0),
601SOC_DAPM_SINGLE("IN4R Switch", WM8962_HEADPHONE_MIXER_1, 0, 1, 0),
602};
603
604static const struct snd_kcontrol_new hpmixr[] = {
605SOC_DAPM_SINGLE("DACL Switch", WM8962_HEADPHONE_MIXER_2, 5, 1, 0),
606SOC_DAPM_SINGLE("DACR Switch", WM8962_HEADPHONE_MIXER_2, 4, 1, 0),
607SOC_DAPM_SINGLE("MIXINL Switch", WM8962_HEADPHONE_MIXER_2, 3, 1, 0),
608SOC_DAPM_SINGLE("MIXINR Switch", WM8962_HEADPHONE_MIXER_2, 2, 1, 0),
609SOC_DAPM_SINGLE("IN4L Switch", WM8962_HEADPHONE_MIXER_2, 1, 1, 0),
610SOC_DAPM_SINGLE("IN4R Switch", WM8962_HEADPHONE_MIXER_2, 0, 1, 0),
611};
612
613static const struct snd_kcontrol_new spkmixl[] = {
614SOC_DAPM_SINGLE("DACL Switch", WM8962_SPEAKER_MIXER_1, 5, 1, 0),
615SOC_DAPM_SINGLE("DACR Switch", WM8962_SPEAKER_MIXER_1, 4, 1, 0),
616SOC_DAPM_SINGLE("MIXINL Switch", WM8962_SPEAKER_MIXER_1, 3, 1, 0),
617SOC_DAPM_SINGLE("MIXINR Switch", WM8962_SPEAKER_MIXER_1, 2, 1, 0),
618SOC_DAPM_SINGLE("IN4L Switch", WM8962_SPEAKER_MIXER_1, 1, 1, 0),
619SOC_DAPM_SINGLE("IN4R Switch", WM8962_SPEAKER_MIXER_1, 0, 1, 0),
620};
621
622static const struct snd_kcontrol_new spkmixr[] = {
623SOC_DAPM_SINGLE("DACL Switch", WM8962_SPEAKER_MIXER_2, 5, 1, 0),
624SOC_DAPM_SINGLE("DACR Switch", WM8962_SPEAKER_MIXER_2, 4, 1, 0),
625SOC_DAPM_SINGLE("MIXINL Switch", WM8962_SPEAKER_MIXER_2, 3, 1, 0),
626SOC_DAPM_SINGLE("MIXINR Switch", WM8962_SPEAKER_MIXER_2, 2, 1, 0),
627SOC_DAPM_SINGLE("IN4L Switch", WM8962_SPEAKER_MIXER_2, 1, 1, 0),
628SOC_DAPM_SINGLE("IN4R Switch", WM8962_SPEAKER_MIXER_2, 0, 1, 0),
629};
630
631static const struct snd_soc_dapm_widget wm8962_dapm_widgets[] = {
632SND_SOC_DAPM_INPUT("IN1L"),
633SND_SOC_DAPM_INPUT("IN1R"),
634SND_SOC_DAPM_INPUT("IN2L"),
635SND_SOC_DAPM_INPUT("IN2R"),
636SND_SOC_DAPM_INPUT("IN3L"),
637SND_SOC_DAPM_INPUT("IN3R"),
638SND_SOC_DAPM_INPUT("IN4L"),
639SND_SOC_DAPM_INPUT("IN4R"),
640SND_SOC_DAPM_INPUT("Beep"),
641
Mark Browna4f28c02010-09-29 13:24:35 -0700642SND_SOC_DAPM_MICBIAS("MICBIAS", WM8962_PWR_MGMT_1, 1, 0),
643
Mark Brown9a76f1f2010-08-05 13:20:59 +0100644SND_SOC_DAPM_SUPPLY("Class G", WM8962_CHARGE_PUMP_B, 0, 1, NULL, 0),
645SND_SOC_DAPM_SUPPLY("SYSCLK", WM8962_CLOCKING2, 5, 0, sysclk_event,
646 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
647SND_SOC_DAPM_SUPPLY("Charge Pump", WM8962_CHARGE_PUMP_1, 0, 0, cp_event,
648 SND_SOC_DAPM_POST_PMU),
649SND_SOC_DAPM_SUPPLY("TOCLK", WM8962_ADDITIONAL_CONTROL_1, 0, 0, NULL, 0),
650
651SND_SOC_DAPM_MIXER("INPGAL", WM8962_LEFT_INPUT_PGA_CONTROL, 4, 0,
652 inpgal, ARRAY_SIZE(inpgal)),
653SND_SOC_DAPM_MIXER("INPGAR", WM8962_RIGHT_INPUT_PGA_CONTROL, 4, 0,
654 inpgar, ARRAY_SIZE(inpgar)),
655SND_SOC_DAPM_MIXER("MIXINL", WM8962_PWR_MGMT_1, 5, 0,
656 mixinl, ARRAY_SIZE(mixinl)),
657SND_SOC_DAPM_MIXER("MIXINR", WM8962_PWR_MGMT_1, 4, 0,
658 mixinr, ARRAY_SIZE(mixinr)),
659
660SND_SOC_DAPM_ADC("ADCL", "Capture", WM8962_PWR_MGMT_1, 3, 0),
661SND_SOC_DAPM_ADC("ADCR", "Capture", WM8962_PWR_MGMT_1, 2, 0),
662
663SND_SOC_DAPM_MUX("STL", SND_SOC_NOPM, 0, 0, &stl_mux),
664SND_SOC_DAPM_MUX("STR", SND_SOC_NOPM, 0, 0, &str_mux),
665
666SND_SOC_DAPM_DAC("DACL", "Playback", WM8962_PWR_MGMT_2, 8, 0),
667SND_SOC_DAPM_DAC("DACR", "Playback", WM8962_PWR_MGMT_2, 7, 0),
668
669SND_SOC_DAPM_PGA("Left Bypass", SND_SOC_NOPM, 0, 0, NULL, 0),
670SND_SOC_DAPM_PGA("Right Bypass", SND_SOC_NOPM, 0, 0, NULL, 0),
671
672SND_SOC_DAPM_MIXER("HPMIXL", WM8962_MIXER_ENABLES, 3, 0,
673 hpmixl, ARRAY_SIZE(hpmixl)),
674SND_SOC_DAPM_MIXER("HPMIXR", WM8962_MIXER_ENABLES, 2, 0,
675 hpmixr, ARRAY_SIZE(hpmixr)),
676
677SND_SOC_DAPM_MUX_E("HPOUTL PGA", WM8962_PWR_MGMT_2, 6, 0, &hpoutl_mux,
678 out_pga_event, SND_SOC_DAPM_POST_PMU),
679SND_SOC_DAPM_MUX_E("HPOUTR PGA", WM8962_PWR_MGMT_2, 5, 0, &hpoutr_mux,
680 out_pga_event, SND_SOC_DAPM_POST_PMU),
681
682SND_SOC_DAPM_PGA_E("HPOUT", SND_SOC_NOPM, 0, 0, NULL, 0, hp_event,
683 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
684
685SND_SOC_DAPM_OUTPUT("HPOUTL"),
686SND_SOC_DAPM_OUTPUT("HPOUTR"),
687};
688
689static const struct snd_soc_dapm_widget wm8962_dapm_spk_mono_widgets[] = {
690SND_SOC_DAPM_MIXER("Speaker Mixer", WM8962_MIXER_ENABLES, 1, 0,
691 spkmixl, ARRAY_SIZE(spkmixl)),
692SND_SOC_DAPM_MUX_E("Speaker PGA", WM8962_PWR_MGMT_2, 4, 0, &spkoutl_mux,
693 out_pga_event, SND_SOC_DAPM_POST_PMU),
694SND_SOC_DAPM_PGA("Speaker Output", WM8962_CLASS_D_CONTROL_1, 7, 0, NULL, 0),
695SND_SOC_DAPM_OUTPUT("SPKOUT"),
696};
697
698static const struct snd_soc_dapm_widget wm8962_dapm_spk_stereo_widgets[] = {
699SND_SOC_DAPM_MIXER("SPKOUTL Mixer", WM8962_MIXER_ENABLES, 1, 0,
700 spkmixl, ARRAY_SIZE(spkmixl)),
701SND_SOC_DAPM_MIXER("SPKOUTR Mixer", WM8962_MIXER_ENABLES, 0, 0,
702 spkmixr, ARRAY_SIZE(spkmixr)),
703
704SND_SOC_DAPM_MUX_E("SPKOUTL PGA", WM8962_PWR_MGMT_2, 4, 0, &spkoutl_mux,
705 out_pga_event, SND_SOC_DAPM_POST_PMU),
706SND_SOC_DAPM_MUX_E("SPKOUTR PGA", WM8962_PWR_MGMT_2, 3, 0, &spkoutr_mux,
707 out_pga_event, SND_SOC_DAPM_POST_PMU),
708
709SND_SOC_DAPM_PGA("SPKOUTR Output", WM8962_CLASS_D_CONTROL_1, 7, 0, NULL, 0),
710SND_SOC_DAPM_PGA("SPKOUTL Output", WM8962_CLASS_D_CONTROL_1, 6, 0, NULL, 0),
711
712SND_SOC_DAPM_OUTPUT("SPKOUTL"),
713SND_SOC_DAPM_OUTPUT("SPKOUTR"),
714};
715
716static const struct snd_soc_dapm_route wm8962_intercon[] = {
717 { "INPGAL", "IN1L Switch", "IN1L" },
718 { "INPGAL", "IN2L Switch", "IN2L" },
719 { "INPGAL", "IN3L Switch", "IN3L" },
720 { "INPGAL", "IN4L Switch", "IN4L" },
721
722 { "INPGAR", "IN1R Switch", "IN1R" },
723 { "INPGAR", "IN2R Switch", "IN2R" },
724 { "INPGAR", "IN3R Switch", "IN3R" },
725 { "INPGAR", "IN4R Switch", "IN4R" },
726
727 { "MIXINL", "IN2L Switch", "IN2L" },
728 { "MIXINL", "IN3L Switch", "IN3L" },
729 { "MIXINL", "PGA Switch", "INPGAL" },
730
731 { "MIXINR", "IN2R Switch", "IN2R" },
732 { "MIXINR", "IN3R Switch", "IN3R" },
733 { "MIXINR", "PGA Switch", "INPGAR" },
734
735 { "ADCL", NULL, "SYSCLK" },
736 { "ADCL", NULL, "TOCLK" },
737 { "ADCL", NULL, "MIXINL" },
738
739 { "ADCR", NULL, "SYSCLK" },
740 { "ADCR", NULL, "TOCLK" },
741 { "ADCR", NULL, "MIXINR" },
742
743 { "STL", "Left", "ADCL" },
744 { "STL", "Right", "ADCR" },
745
746 { "STR", "Left", "ADCL" },
747 { "STR", "Right", "ADCR" },
748
749 { "DACL", NULL, "SYSCLK" },
750 { "DACL", NULL, "TOCLK" },
751 { "DACL", NULL, "Beep" },
752 { "DACL", NULL, "STL" },
753
754 { "DACR", NULL, "SYSCLK" },
755 { "DACR", NULL, "TOCLK" },
756 { "DACR", NULL, "Beep" },
757 { "DACR", NULL, "STR" },
758
759 { "HPMIXL", "IN4L Switch", "IN4L" },
760 { "HPMIXL", "IN4R Switch", "IN4R" },
761 { "HPMIXL", "DACL Switch", "DACL" },
762 { "HPMIXL", "DACR Switch", "DACR" },
763 { "HPMIXL", "MIXINL Switch", "MIXINL" },
764 { "HPMIXL", "MIXINR Switch", "MIXINR" },
765
766 { "HPMIXR", "IN4L Switch", "IN4L" },
767 { "HPMIXR", "IN4R Switch", "IN4R" },
768 { "HPMIXR", "DACL Switch", "DACL" },
769 { "HPMIXR", "DACR Switch", "DACR" },
770 { "HPMIXR", "MIXINL Switch", "MIXINL" },
771 { "HPMIXR", "MIXINR Switch", "MIXINR" },
772
773 { "Left Bypass", NULL, "HPMIXL" },
774 { "Left Bypass", NULL, "Class G" },
775
776 { "Right Bypass", NULL, "HPMIXR" },
777 { "Right Bypass", NULL, "Class G" },
778
779 { "HPOUTL PGA", "Mixer", "Left Bypass" },
780 { "HPOUTL PGA", "DAC", "DACL" },
781
782 { "HPOUTR PGA", "Mixer", "Right Bypass" },
783 { "HPOUTR PGA", "DAC", "DACR" },
784
785 { "HPOUT", NULL, "HPOUTL PGA" },
786 { "HPOUT", NULL, "HPOUTR PGA" },
787 { "HPOUT", NULL, "Charge Pump" },
788 { "HPOUT", NULL, "SYSCLK" },
789 { "HPOUT", NULL, "TOCLK" },
790
791 { "HPOUTL", NULL, "HPOUT" },
792 { "HPOUTR", NULL, "HPOUT" },
793};
794
795static const struct snd_soc_dapm_route wm8962_spk_mono_intercon[] = {
796 { "Speaker Mixer", "IN4L Switch", "IN4L" },
797 { "Speaker Mixer", "IN4R Switch", "IN4R" },
798 { "Speaker Mixer", "DACL Switch", "DACL" },
799 { "Speaker Mixer", "DACR Switch", "DACR" },
800 { "Speaker Mixer", "MIXINL Switch", "MIXINL" },
801 { "Speaker Mixer", "MIXINR Switch", "MIXINR" },
802
803 { "Speaker PGA", "Mixer", "Speaker Mixer" },
804 { "Speaker PGA", "DAC", "DACL" },
805
806 { "Speaker Output", NULL, "Speaker PGA" },
807 { "Speaker Output", NULL, "SYSCLK" },
808 { "Speaker Output", NULL, "TOCLK" },
809
810 { "SPKOUT", NULL, "Speaker Output" },
811};
812
813static const struct snd_soc_dapm_route wm8962_spk_stereo_intercon[] = {
814 { "SPKOUTL Mixer", "IN4L Switch", "IN4L" },
815 { "SPKOUTL Mixer", "IN4R Switch", "IN4R" },
816 { "SPKOUTL Mixer", "DACL Switch", "DACL" },
817 { "SPKOUTL Mixer", "DACR Switch", "DACR" },
818 { "SPKOUTL Mixer", "MIXINL Switch", "MIXINL" },
819 { "SPKOUTL Mixer", "MIXINR Switch", "MIXINR" },
820
821 { "SPKOUTR Mixer", "IN4L Switch", "IN4L" },
822 { "SPKOUTR Mixer", "IN4R Switch", "IN4R" },
823 { "SPKOUTR Mixer", "DACL Switch", "DACL" },
824 { "SPKOUTR Mixer", "DACR Switch", "DACR" },
825 { "SPKOUTR Mixer", "MIXINL Switch", "MIXINL" },
826 { "SPKOUTR Mixer", "MIXINR Switch", "MIXINR" },
827
828 { "SPKOUTL PGA", "Mixer", "SPKOUTL Mixer" },
829 { "SPKOUTL PGA", "DAC", "DACL" },
830
831 { "SPKOUTR PGA", "Mixer", "SPKOUTR Mixer" },
832 { "SPKOUTR PGA", "DAC", "DACR" },
833
834 { "SPKOUTL Output", NULL, "SPKOUTL PGA" },
835 { "SPKOUTL Output", NULL, "SYSCLK" },
836 { "SPKOUTL Output", NULL, "TOCLK" },
837
838 { "SPKOUTR Output", NULL, "SPKOUTR PGA" },
839 { "SPKOUTR Output", NULL, "SYSCLK" },
840 { "SPKOUTR Output", NULL, "TOCLK" },
841
842 { "SPKOUTL", NULL, "SPKOUTL Output" },
843 { "SPKOUTR", NULL, "SPKOUTR Output" },
844};
845
846static int wm8962_add_widgets(struct snd_soc_codec *codec)
847{
848 struct wm8962_pdata *pdata = dev_get_platdata(codec->dev);
849
850 snd_soc_add_controls(codec, wm8962_snd_controls,
851 ARRAY_SIZE(wm8962_snd_controls));
852 if (pdata && pdata->spk_mono)
853 snd_soc_add_controls(codec, wm8962_spk_mono_controls,
854 ARRAY_SIZE(wm8962_spk_mono_controls));
855 else
856 snd_soc_add_controls(codec, wm8962_spk_stereo_controls,
857 ARRAY_SIZE(wm8962_spk_stereo_controls));
858
859
860 snd_soc_dapm_new_controls(codec, wm8962_dapm_widgets,
861 ARRAY_SIZE(wm8962_dapm_widgets));
862 if (pdata && pdata->spk_mono)
863 snd_soc_dapm_new_controls(codec, wm8962_dapm_spk_mono_widgets,
864 ARRAY_SIZE(wm8962_dapm_spk_mono_widgets));
865 else
866 snd_soc_dapm_new_controls(codec, wm8962_dapm_spk_stereo_widgets,
867 ARRAY_SIZE(wm8962_dapm_spk_stereo_widgets));
868
869 snd_soc_dapm_add_routes(codec, wm8962_intercon,
870 ARRAY_SIZE(wm8962_intercon));
871 if (pdata && pdata->spk_mono)
872 snd_soc_dapm_add_routes(codec, wm8962_spk_mono_intercon,
873 ARRAY_SIZE(wm8962_spk_mono_intercon));
874 else
875 snd_soc_dapm_add_routes(codec, wm8962_spk_stereo_intercon,
876 ARRAY_SIZE(wm8962_spk_stereo_intercon));
877
878
879 snd_soc_dapm_disable_pin(codec, "Beep");
880
881 return 0;
882}
883
884static void wm8962_sync_cache(struct snd_soc_codec *codec)
885{
886 struct wm8962_priv *wm8962 = snd_soc_codec_get_drvdata(codec);
887 int i;
888
889 if (!codec->cache_sync)
890 return;
891
892 dev_dbg(codec->dev, "Syncing cache\n");
893
894 codec->cache_only = 0;
895
896 /* Sync back cached values if they're different from the
897 * hardware default.
898 */
899 for (i = 1; i < ARRAY_SIZE(wm8962->reg_cache); i++) {
900 if (i == WM8962_SOFTWARE_RESET)
901 continue;
902 if (wm8962->reg_cache[i] == wm8962_reg[i])
903 continue;
904
905 snd_soc_write(codec, i, wm8962->reg_cache[i]);
906 }
907
908 codec->cache_sync = 0;
909}
910
911/* -1 for reserved values */
912static const int bclk_divs[] = {
913 1, -1, 2, 3, 4, -1, 6, 8, -1, 12, 16, 24, -1, 32, 32, 32
914};
915
916static void wm8962_configure_bclk(struct snd_soc_codec *codec)
917{
918 struct wm8962_priv *wm8962 = snd_soc_codec_get_drvdata(codec);
919 int dspclk, i;
920 int clocking2 = 0;
921 int aif2 = 0;
922
Mark Brown9a76f1f2010-08-05 13:20:59 +0100923 if (!wm8962->bclk) {
924 dev_dbg(codec->dev, "No BCLK rate configured\n");
925 return;
926 }
927
928 dspclk = snd_soc_read(codec, WM8962_CLOCKING1);
929 if (dspclk < 0) {
930 dev_err(codec->dev, "Failed to read DSPCLK: %d\n", dspclk);
931 return;
932 }
933
934 dspclk = (dspclk & WM8962_DSPCLK_DIV_MASK) >> WM8962_DSPCLK_DIV_SHIFT;
935 switch (dspclk) {
936 case 0:
937 dspclk = wm8962->sysclk_rate;
938 break;
939 case 1:
940 dspclk = wm8962->sysclk_rate / 2;
941 break;
942 case 2:
943 dspclk = wm8962->sysclk_rate / 4;
944 break;
945 default:
946 dev_warn(codec->dev, "Unknown DSPCLK divisor read back\n");
947 dspclk = wm8962->sysclk;
948 }
949
950 dev_dbg(codec->dev, "DSPCLK is %dHz, BCLK %d\n", dspclk, wm8962->bclk);
951
952 /* We're expecting an exact match */
953 for (i = 0; i < ARRAY_SIZE(bclk_divs); i++) {
954 if (bclk_divs[i] < 0)
955 continue;
956
957 if (dspclk / bclk_divs[i] == wm8962->bclk) {
958 dev_dbg(codec->dev, "Selected BCLK_DIV %d for %dHz\n",
959 bclk_divs[i], wm8962->bclk);
960 clocking2 |= i;
961 break;
962 }
963 }
964 if (i == ARRAY_SIZE(bclk_divs)) {
965 dev_err(codec->dev, "Unsupported BCLK ratio %d\n",
966 dspclk / wm8962->bclk);
967 return;
968 }
969
970 aif2 |= wm8962->bclk / wm8962->lrclk;
971 dev_dbg(codec->dev, "Selected LRCLK divisor %d for %dHz\n",
972 wm8962->bclk / wm8962->lrclk, wm8962->lrclk);
973
974 snd_soc_update_bits(codec, WM8962_CLOCKING2,
975 WM8962_BCLK_DIV_MASK, clocking2);
976 snd_soc_update_bits(codec, WM8962_AUDIO_INTERFACE_2,
977 WM8962_AIF_RATE_MASK, aif2);
978}
979
980static int wm8962_set_bias_level(struct snd_soc_codec *codec,
981 enum snd_soc_bias_level level)
982{
983 struct wm8962_priv *wm8962 = snd_soc_codec_get_drvdata(codec);
984 int ret;
985
986 if (level == codec->bias_level)
987 return 0;
988
989 switch (level) {
990 case SND_SOC_BIAS_ON:
991 break;
992
993 case SND_SOC_BIAS_PREPARE:
994 /* VMID 2*50k */
995 snd_soc_update_bits(codec, WM8962_PWR_MGMT_1,
996 WM8962_VMID_SEL_MASK, 0x80);
997 break;
998
999 case SND_SOC_BIAS_STANDBY:
1000 if (codec->bias_level == SND_SOC_BIAS_OFF) {
1001 ret = regulator_bulk_enable(ARRAY_SIZE(wm8962->supplies),
1002 wm8962->supplies);
1003 if (ret != 0) {
1004 dev_err(codec->dev,
1005 "Failed to enable supplies: %d\n",
1006 ret);
1007 return ret;
1008 }
1009
1010 wm8962_sync_cache(codec);
1011
1012 snd_soc_update_bits(codec, WM8962_ANTI_POP,
1013 WM8962_STARTUP_BIAS_ENA |
1014 WM8962_VMID_BUF_ENA,
1015 WM8962_STARTUP_BIAS_ENA |
1016 WM8962_VMID_BUF_ENA);
1017
1018 /* Bias enable at 2*50k for ramp */
1019 snd_soc_update_bits(codec, WM8962_PWR_MGMT_1,
1020 WM8962_VMID_SEL_MASK |
1021 WM8962_BIAS_ENA,
1022 WM8962_BIAS_ENA | 0x180);
1023
1024 msleep(5);
1025
1026 snd_soc_update_bits(codec, WM8962_CLOCKING2,
1027 WM8962_CLKREG_OVD,
1028 WM8962_CLKREG_OVD);
1029
1030 wm8962_configure_bclk(codec);
1031 }
1032
1033 /* VMID 2*250k */
1034 snd_soc_update_bits(codec, WM8962_PWR_MGMT_1,
1035 WM8962_VMID_SEL_MASK, 0x100);
1036 break;
1037
1038 case SND_SOC_BIAS_OFF:
1039 snd_soc_update_bits(codec, WM8962_PWR_MGMT_1,
1040 WM8962_VMID_SEL_MASK | WM8962_BIAS_ENA, 0);
1041
1042 snd_soc_update_bits(codec, WM8962_ANTI_POP,
1043 WM8962_STARTUP_BIAS_ENA |
1044 WM8962_VMID_BUF_ENA, 0);
1045
1046 regulator_bulk_disable(ARRAY_SIZE(wm8962->supplies),
1047 wm8962->supplies);
1048 break;
1049 }
1050 codec->bias_level = level;
1051 return 0;
1052}
1053
1054static const struct {
1055 int rate;
1056 int reg;
1057} sr_vals[] = {
1058 { 48000, 0 },
1059 { 44100, 0 },
1060 { 32000, 1 },
1061 { 22050, 2 },
1062 { 24000, 2 },
1063 { 16000, 3 },
1064 { 11025, 4 },
1065 { 12000, 4 },
1066 { 8000, 5 },
1067 { 88200, 6 },
1068 { 96000, 6 },
1069};
1070
1071static const int sysclk_rates[] = {
1072 64, 128, 192, 256, 384, 512, 768, 1024, 1408, 1536,
1073};
1074
1075static int wm8962_hw_params(struct snd_pcm_substream *substream,
1076 struct snd_pcm_hw_params *params,
1077 struct snd_soc_dai *dai)
1078{
1079 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Mark Brown54d8d0a2010-08-12 15:02:11 +01001080 struct snd_soc_codec *codec = rtd->codec;
Mark Brown9a76f1f2010-08-05 13:20:59 +01001081 struct wm8962_priv *wm8962 = snd_soc_codec_get_drvdata(codec);
1082 int rate = params_rate(params);
1083 int i;
1084 int aif0 = 0;
1085 int adctl3 = 0;
1086 int clocking4 = 0;
1087
1088 wm8962->bclk = snd_soc_params_to_bclk(params);
1089 wm8962->lrclk = params_rate(params);
1090
1091 for (i = 0; i < ARRAY_SIZE(sr_vals); i++) {
1092 if (sr_vals[i].rate == rate) {
1093 adctl3 |= sr_vals[i].reg;
1094 break;
1095 }
1096 }
1097 if (i == ARRAY_SIZE(sr_vals)) {
1098 dev_err(codec->dev, "Unsupported rate %dHz\n", rate);
1099 return -EINVAL;
1100 }
1101
1102 if (rate % 8000 == 0)
1103 adctl3 |= WM8962_SAMPLE_RATE_INT_MODE;
1104
1105 for (i = 0; i < ARRAY_SIZE(sysclk_rates); i++) {
1106 if (sysclk_rates[i] == wm8962->sysclk_rate / rate) {
1107 clocking4 |= i << WM8962_SYSCLK_RATE_SHIFT;
1108 break;
1109 }
1110 }
1111 if (i == ARRAY_SIZE(sysclk_rates)) {
1112 dev_err(codec->dev, "Unsupported sysclk ratio %d\n",
1113 wm8962->sysclk_rate / rate);
1114 return -EINVAL;
1115 }
1116
1117 switch (params_format(params)) {
1118 case SNDRV_PCM_FORMAT_S16_LE:
1119 break;
1120 case SNDRV_PCM_FORMAT_S20_3LE:
1121 aif0 |= 0x40;
1122 break;
1123 case SNDRV_PCM_FORMAT_S24_LE:
1124 aif0 |= 0x80;
1125 break;
1126 case SNDRV_PCM_FORMAT_S32_LE:
1127 aif0 |= 0xc0;
1128 break;
1129 default:
1130 return -EINVAL;
1131 }
1132
1133 snd_soc_update_bits(codec, WM8962_AUDIO_INTERFACE_0,
1134 WM8962_WL_MASK, aif0);
1135 snd_soc_update_bits(codec, WM8962_ADDITIONAL_CONTROL_3,
1136 WM8962_SAMPLE_RATE_INT_MODE |
1137 WM8962_SAMPLE_RATE_MASK, adctl3);
1138 snd_soc_update_bits(codec, WM8962_CLOCKING_4,
1139 WM8962_SYSCLK_RATE_MASK, clocking4);
1140
1141 wm8962_configure_bclk(codec);
1142
1143 return 0;
1144}
1145
1146static int wm8962_set_dai_sysclk(struct snd_soc_dai *dai, int clk_id,
1147 unsigned int freq, int dir)
1148{
1149 struct snd_soc_codec *codec = dai->codec;
1150 struct wm8962_priv *wm8962 = snd_soc_codec_get_drvdata(codec);
1151 int src;
1152
1153 switch (clk_id) {
1154 case WM8962_SYSCLK_MCLK:
1155 wm8962->sysclk = WM8962_SYSCLK_MCLK;
1156 src = 0;
1157 break;
1158 case WM8962_SYSCLK_FLL:
1159 wm8962->sysclk = WM8962_SYSCLK_FLL;
1160 src = 1 << WM8962_SYSCLK_SRC_SHIFT;
1161 WARN_ON(freq != wm8962->fll_fout);
1162 break;
1163 default:
1164 return -EINVAL;
1165 }
1166
1167 snd_soc_update_bits(codec, WM8962_CLOCKING2, WM8962_SYSCLK_SRC_MASK,
1168 src);
1169
1170 wm8962->sysclk_rate = freq;
1171
1172 return 0;
1173}
1174
1175static int wm8962_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
1176{
1177 struct snd_soc_codec *codec = dai->codec;
1178 int aif0 = 0;
1179
1180 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1181 case SND_SOC_DAIFMT_DSP_A:
1182 aif0 |= WM8962_LRCLK_INV;
1183 case SND_SOC_DAIFMT_DSP_B:
1184 aif0 |= 3;
1185
1186 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1187 case SND_SOC_DAIFMT_NB_NF:
1188 case SND_SOC_DAIFMT_IB_NF:
1189 break;
1190 default:
1191 return -EINVAL;
1192 }
1193 break;
1194
1195 case SND_SOC_DAIFMT_RIGHT_J:
1196 break;
1197 case SND_SOC_DAIFMT_LEFT_J:
1198 aif0 |= 1;
1199 break;
1200 case SND_SOC_DAIFMT_I2S:
1201 aif0 |= 2;
1202 break;
1203 default:
1204 return -EINVAL;
1205 }
1206
1207 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1208 case SND_SOC_DAIFMT_NB_NF:
1209 break;
1210 case SND_SOC_DAIFMT_IB_NF:
1211 aif0 |= WM8962_BCLK_INV;
1212 break;
1213 case SND_SOC_DAIFMT_NB_IF:
1214 aif0 |= WM8962_LRCLK_INV;
1215 break;
1216 case SND_SOC_DAIFMT_IB_IF:
1217 aif0 |= WM8962_BCLK_INV | WM8962_LRCLK_INV;
1218 break;
1219 default:
1220 return -EINVAL;
1221 }
1222
1223 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1224 case SND_SOC_DAIFMT_CBM_CFM:
1225 aif0 |= WM8962_MSTR;
1226 break;
1227 case SND_SOC_DAIFMT_CBS_CFS:
1228 break;
1229 default:
1230 return -EINVAL;
1231 }
1232
1233 snd_soc_update_bits(codec, WM8962_AUDIO_INTERFACE_0,
1234 WM8962_FMT_MASK | WM8962_BCLK_INV | WM8962_MSTR |
1235 WM8962_LRCLK_INV, aif0);
1236
1237 return 0;
1238}
1239
1240struct _fll_div {
1241 u16 fll_fratio;
1242 u16 fll_outdiv;
1243 u16 fll_refclk_div;
1244 u16 n;
1245 u16 theta;
1246 u16 lambda;
1247};
1248
1249/* The size in bits of the FLL divide multiplied by 10
1250 * to allow rounding later */
1251#define FIXED_FLL_SIZE ((1 << 16) * 10)
1252
1253static struct {
1254 unsigned int min;
1255 unsigned int max;
1256 u16 fll_fratio;
1257 int ratio;
1258} fll_fratios[] = {
1259 { 0, 64000, 4, 16 },
1260 { 64000, 128000, 3, 8 },
1261 { 128000, 256000, 2, 4 },
1262 { 256000, 1000000, 1, 2 },
1263 { 1000000, 13500000, 0, 1 },
1264};
1265
1266static int fll_factors(struct _fll_div *fll_div, unsigned int Fref,
1267 unsigned int Fout)
1268{
1269 unsigned int target;
1270 unsigned int div;
1271 unsigned int fratio, gcd_fll;
1272 int i;
1273
1274 /* Fref must be <=13.5MHz */
1275 div = 1;
1276 fll_div->fll_refclk_div = 0;
1277 while ((Fref / div) > 13500000) {
1278 div *= 2;
1279 fll_div->fll_refclk_div++;
1280
1281 if (div > 4) {
1282 pr_err("Can't scale %dMHz input down to <=13.5MHz\n",
1283 Fref);
1284 return -EINVAL;
1285 }
1286 }
1287
1288 pr_debug("FLL Fref=%u Fout=%u\n", Fref, Fout);
1289
1290 /* Apply the division for our remaining calculations */
1291 Fref /= div;
1292
1293 /* Fvco should be 90-100MHz; don't check the upper bound */
1294 div = 2;
1295 while (Fout * div < 90000000) {
1296 div++;
1297 if (div > 64) {
1298 pr_err("Unable to find FLL_OUTDIV for Fout=%uHz\n",
1299 Fout);
1300 return -EINVAL;
1301 }
1302 }
1303 target = Fout * div;
1304 fll_div->fll_outdiv = div - 1;
1305
1306 pr_debug("FLL Fvco=%dHz\n", target);
1307
1308 /* Find an appropraite FLL_FRATIO and factor it out of the target */
1309 for (i = 0; i < ARRAY_SIZE(fll_fratios); i++) {
1310 if (fll_fratios[i].min <= Fref && Fref <= fll_fratios[i].max) {
1311 fll_div->fll_fratio = fll_fratios[i].fll_fratio;
1312 fratio = fll_fratios[i].ratio;
1313 break;
1314 }
1315 }
1316 if (i == ARRAY_SIZE(fll_fratios)) {
1317 pr_err("Unable to find FLL_FRATIO for Fref=%uHz\n", Fref);
1318 return -EINVAL;
1319 }
1320
1321 fll_div->n = target / (fratio * Fref);
1322
1323 if (target % Fref == 0) {
1324 fll_div->theta = 0;
1325 fll_div->lambda = 0;
1326 } else {
1327 gcd_fll = gcd(target, fratio * Fref);
1328
1329 fll_div->theta = (target - (fll_div->n * fratio * Fref))
1330 / gcd_fll;
1331 fll_div->lambda = (fratio * Fref) / gcd_fll;
1332 }
1333
1334 pr_debug("FLL N=%x THETA=%x LAMBDA=%x\n",
1335 fll_div->n, fll_div->theta, fll_div->lambda);
1336 pr_debug("FLL_FRATIO=%x FLL_OUTDIV=%x FLL_REFCLK_DIV=%x\n",
1337 fll_div->fll_fratio, fll_div->fll_outdiv,
1338 fll_div->fll_refclk_div);
1339
1340 return 0;
1341}
1342
1343static int wm8962_set_fll(struct snd_soc_dai *dai, int fll_id, int source,
1344 unsigned int Fref, unsigned int Fout)
1345{
1346 struct snd_soc_codec *codec = dai->codec;
1347 struct wm8962_priv *wm8962 = snd_soc_codec_get_drvdata(codec);
1348 struct _fll_div fll_div;
1349 int ret;
Mark Brown61371122010-09-27 17:20:11 -07001350 int fll1 = snd_soc_read(codec, WM8962_FLL_CONTROL_1) & WM8962_FLL_ENA;
Mark Brown9a76f1f2010-08-05 13:20:59 +01001351
1352 /* Any change? */
1353 if (source == wm8962->fll_src && Fref == wm8962->fll_fref &&
1354 Fout == wm8962->fll_fout)
1355 return 0;
1356
1357 if (Fout == 0) {
1358 dev_dbg(codec->dev, "FLL disabled\n");
1359
1360 wm8962->fll_fref = 0;
1361 wm8962->fll_fout = 0;
1362
1363 snd_soc_update_bits(codec, WM8962_FLL_CONTROL_1,
1364 WM8962_FLL_ENA, 0);
1365
1366 return 0;
1367 }
1368
1369 ret = fll_factors(&fll_div, Fref, Fout);
1370 if (ret != 0)
1371 return ret;
1372
1373 switch (fll_id) {
1374 case WM8962_FLL_MCLK:
1375 case WM8962_FLL_BCLK:
1376 case WM8962_FLL_OSC:
1377 fll1 |= (fll_id - 1) << WM8962_FLL_REFCLK_SRC_SHIFT;
1378 break;
1379 case WM8962_FLL_INT:
1380 snd_soc_update_bits(codec, WM8962_FLL_CONTROL_1,
1381 WM8962_FLL_OSC_ENA, WM8962_FLL_OSC_ENA);
1382 snd_soc_update_bits(codec, WM8962_FLL_CONTROL_5,
1383 WM8962_FLL_FRC_NCO, WM8962_FLL_FRC_NCO);
1384 break;
1385 default:
1386 dev_err(codec->dev, "Unknown FLL source %d\n", ret);
1387 return -EINVAL;
1388 }
1389
1390 if (fll_div.theta || fll_div.lambda)
1391 fll1 |= WM8962_FLL_FRAC;
1392
1393 /* Stop the FLL while we reconfigure */
1394 snd_soc_update_bits(codec, WM8962_FLL_CONTROL_1, WM8962_FLL_ENA, 0);
1395
1396 snd_soc_update_bits(codec, WM8962_FLL_CONTROL_2,
1397 WM8962_FLL_OUTDIV_MASK |
1398 WM8962_FLL_REFCLK_DIV_MASK,
1399 (fll_div.fll_outdiv << WM8962_FLL_OUTDIV_SHIFT) |
1400 (fll_div.fll_refclk_div));
1401
1402 snd_soc_update_bits(codec, WM8962_FLL_CONTROL_3,
1403 WM8962_FLL_FRATIO_MASK, fll_div.fll_fratio);
1404
1405 snd_soc_write(codec, WM8962_FLL_CONTROL_6, fll_div.theta);
1406 snd_soc_write(codec, WM8962_FLL_CONTROL_7, fll_div.lambda);
1407 snd_soc_write(codec, WM8962_FLL_CONTROL_8, fll_div.n);
1408
1409 snd_soc_update_bits(codec, WM8962_FLL_CONTROL_1,
1410 WM8962_FLL_FRAC | WM8962_FLL_REFCLK_SRC_MASK |
1411 WM8962_FLL_ENA, fll1);
1412
1413 dev_dbg(codec->dev, "FLL configured for %dHz->%dHz\n", Fref, Fout);
1414
1415 wm8962->fll_fref = Fref;
1416 wm8962->fll_fout = Fout;
1417 wm8962->fll_src = source;
1418
1419 return 0;
1420}
1421
1422static int wm8962_mute(struct snd_soc_dai *dai, int mute)
1423{
1424 struct snd_soc_codec *codec = dai->codec;
1425 int val;
1426
1427 if (mute)
1428 val = WM8962_DAC_MUTE;
1429 else
1430 val = 0;
1431
1432 return snd_soc_update_bits(codec, WM8962_ADC_DAC_CONTROL_1,
1433 WM8962_DAC_MUTE, val);
1434}
1435
1436#define WM8962_RATES SNDRV_PCM_RATE_8000_96000
1437
1438#define WM8962_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
1439 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
1440
1441static struct snd_soc_dai_ops wm8962_dai_ops = {
1442 .hw_params = wm8962_hw_params,
1443 .set_sysclk = wm8962_set_dai_sysclk,
1444 .set_fmt = wm8962_set_dai_fmt,
1445 .set_pll = wm8962_set_fll,
1446 .digital_mute = wm8962_mute,
1447};
1448
Mark Brown54d8d0a2010-08-12 15:02:11 +01001449static struct snd_soc_dai_driver wm8962_dai = {
1450 .name = "wm8962",
Mark Brown9a76f1f2010-08-05 13:20:59 +01001451 .playback = {
1452 .stream_name = "Playback",
1453 .channels_min = 2,
1454 .channels_max = 2,
1455 .rates = WM8962_RATES,
1456 .formats = WM8962_FORMATS,
1457 },
1458 .capture = {
1459 .stream_name = "Capture",
1460 .channels_min = 2,
1461 .channels_max = 2,
1462 .rates = WM8962_RATES,
1463 .formats = WM8962_FORMATS,
1464 },
1465 .ops = &wm8962_dai_ops,
1466 .symmetric_rates = 1,
1467};
Mark Brown9a76f1f2010-08-05 13:20:59 +01001468
Mark Brown77113082010-09-30 15:37:53 -07001469static void wm8962_mic_work(struct work_struct *work)
1470{
1471 struct wm8962_priv *wm8962 = container_of(work,
1472 struct wm8962_priv,
1473 mic_work.work);
1474 struct snd_soc_codec *codec = wm8962->codec;
1475 int status = 0;
1476 int irq_pol = 0;
1477 int reg;
1478
1479 reg = snd_soc_read(codec, WM8962_ADDITIONAL_CONTROL_4);
1480
1481 if (reg & WM8962_MICDET_STS) {
1482 status |= SND_JACK_MICROPHONE;
1483 irq_pol |= WM8962_MICD_IRQ_POL;
1484 }
1485
1486 if (reg & WM8962_MICSHORT_STS) {
1487 status |= SND_JACK_BTN_0;
1488 irq_pol |= WM8962_MICSCD_IRQ_POL;
1489 }
1490
1491 snd_soc_jack_report(wm8962->jack, status,
1492 SND_JACK_MICROPHONE | SND_JACK_BTN_0);
1493
1494 snd_soc_update_bits(codec, WM8962_MICINT_SOURCE_POL,
1495 WM8962_MICSCD_IRQ_POL |
1496 WM8962_MICD_IRQ_POL, irq_pol);
1497}
1498
Mark Brown45e65502010-09-28 16:01:20 -07001499static irqreturn_t wm8962_irq(int irq, void *data)
1500{
1501 struct snd_soc_codec *codec = data;
Mark Brown77113082010-09-30 15:37:53 -07001502 struct wm8962_priv *wm8962 = snd_soc_codec_get_drvdata(codec);
Mark Brown45e65502010-09-28 16:01:20 -07001503 int mask;
1504 int active;
1505
1506 mask = snd_soc_read(codec, WM8962_INTERRUPT_STATUS_2);
1507
1508 active = snd_soc_read(codec, WM8962_INTERRUPT_STATUS_2);
1509 active &= ~mask;
1510
1511 if (active & WM8962_FIFOS_ERR_EINT)
1512 dev_err(codec->dev, "FIFO error\n");
1513
1514 if (active & WM8962_TEMP_SHUT_EINT)
1515 dev_crit(codec->dev, "Thermal shutdown\n");
1516
Mark Brown77113082010-09-30 15:37:53 -07001517 if (active & (WM8962_MICSCD_EINT | WM8962_MICD_EINT)) {
1518 dev_dbg(codec->dev, "Microphone event detected\n");
1519
1520 schedule_delayed_work(&wm8962->mic_work,
1521 msecs_to_jiffies(250));
1522 }
1523
Mark Brown45e65502010-09-28 16:01:20 -07001524 /* Acknowledge the interrupts */
1525 snd_soc_write(codec, WM8962_INTERRUPT_STATUS_2, active);
1526
1527 return IRQ_HANDLED;
1528}
1529
Mark Brown77113082010-09-30 15:37:53 -07001530/**
1531 * wm8962_mic_detect - Enable microphone detection via the WM8962 IRQ
1532 *
1533 * @codec: WM8962 codec
1534 * @jack: jack to report detection events on
1535 *
1536 * Enable microphone detection via IRQ on the WM8962. If GPIOs are
1537 * being used to bring out signals to the processor then only platform
1538 * data configuration is needed for WM8962 and processor GPIOs should
1539 * be configured using snd_soc_jack_add_gpios() instead.
1540 *
1541 * If no jack is supplied detection will be disabled.
1542 */
1543int wm8962_mic_detect(struct snd_soc_codec *codec, struct snd_soc_jack *jack)
1544{
1545 struct wm8962_priv *wm8962 = snd_soc_codec_get_drvdata(codec);
1546 int irq_mask, enable;
1547
1548 wm8962->jack = jack;
1549 if (jack) {
1550 irq_mask = 0;
1551 enable = WM8962_MICDET_ENA;
1552 } else {
1553 irq_mask = WM8962_MICD_EINT | WM8962_MICSCD_EINT;
1554 enable = 0;
1555 }
1556
1557 snd_soc_update_bits(codec, WM8962_INTERRUPT_STATUS_2_MASK,
1558 WM8962_MICD_EINT | WM8962_MICSCD_EINT, irq_mask);
1559 snd_soc_update_bits(codec, WM8962_ADDITIONAL_CONTROL_4,
1560 WM8962_MICDET_ENA, enable);
1561
1562 /* Send an initial empty report */
1563 snd_soc_jack_report(wm8962->jack, 0,
1564 SND_JACK_MICROPHONE | SND_JACK_BTN_0);
1565
1566 return 0;
1567}
1568EXPORT_SYMBOL_GPL(wm8962_mic_detect);
1569
Mark Brown9a76f1f2010-08-05 13:20:59 +01001570#ifdef CONFIG_PM
Mark Brown54d8d0a2010-08-12 15:02:11 +01001571static int wm8962_resume(struct snd_soc_codec *codec)
Mark Brown9a76f1f2010-08-05 13:20:59 +01001572{
Mark Brown9a76f1f2010-08-05 13:20:59 +01001573 struct wm8962_priv *wm8962 = snd_soc_codec_get_drvdata(codec);
1574 u16 *reg_cache = codec->reg_cache;
1575 int i;
1576
1577 /* Restore the registers */
1578 for (i = 1; i < ARRAY_SIZE(wm8962->reg_cache); i++) {
1579 switch (i) {
1580 case WM8962_SOFTWARE_RESET:
1581 continue;
1582 default:
1583 break;
1584 }
1585
1586 if (reg_cache[i] != wm8962_reg[i])
1587 snd_soc_write(codec, i, reg_cache[i]);
1588 }
1589
1590 return 0;
1591}
1592#else
1593#define wm8962_resume NULL
1594#endif
1595
Mark Brown9a76f1f2010-08-05 13:20:59 +01001596#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
1597static int beep_rates[] = {
1598 500, 1000, 2000, 4000,
1599};
1600
1601static void wm8962_beep_work(struct work_struct *work)
1602{
1603 struct wm8962_priv *wm8962 =
1604 container_of(work, struct wm8962_priv, beep_work);
Mark Brown54d8d0a2010-08-12 15:02:11 +01001605 struct snd_soc_codec *codec = wm8962->codec;
Mark Brown9a76f1f2010-08-05 13:20:59 +01001606 int i;
1607 int reg = 0;
1608 int best = 0;
1609
1610 if (wm8962->beep_rate) {
1611 for (i = 0; i < ARRAY_SIZE(beep_rates); i++) {
1612 if (abs(wm8962->beep_rate - beep_rates[i]) <
1613 abs(wm8962->beep_rate - beep_rates[best]))
1614 best = i;
1615 }
1616
1617 dev_dbg(codec->dev, "Set beep rate %dHz for requested %dHz\n",
1618 beep_rates[best], wm8962->beep_rate);
1619
1620 reg = WM8962_BEEP_ENA | (best << WM8962_BEEP_RATE_SHIFT);
1621
1622 snd_soc_dapm_enable_pin(codec, "Beep");
1623 } else {
1624 dev_dbg(codec->dev, "Disabling beep\n");
1625 snd_soc_dapm_disable_pin(codec, "Beep");
1626 }
1627
1628 snd_soc_update_bits(codec, WM8962_BEEP_GENERATOR_1,
1629 WM8962_BEEP_ENA | WM8962_BEEP_RATE_MASK, reg);
1630
1631 snd_soc_dapm_sync(codec);
1632}
1633
1634/* For usability define a way of injecting beep events for the device -
1635 * many systems will not have a keyboard.
1636 */
1637static int wm8962_beep_event(struct input_dev *dev, unsigned int type,
1638 unsigned int code, int hz)
1639{
1640 struct snd_soc_codec *codec = input_get_drvdata(dev);
1641 struct wm8962_priv *wm8962 = snd_soc_codec_get_drvdata(codec);
1642
1643 dev_dbg(codec->dev, "Beep event %x %x\n", code, hz);
1644
1645 switch (code) {
1646 case SND_BELL:
1647 if (hz)
1648 hz = 1000;
1649 case SND_TONE:
1650 break;
1651 default:
1652 return -1;
1653 }
1654
1655 /* Kick the beep from a workqueue */
1656 wm8962->beep_rate = hz;
1657 schedule_work(&wm8962->beep_work);
1658 return 0;
1659}
1660
1661static ssize_t wm8962_beep_set(struct device *dev,
1662 struct device_attribute *attr,
1663 const char *buf, size_t count)
1664{
1665 struct wm8962_priv *wm8962 = dev_get_drvdata(dev);
1666 long int time;
1667
1668 strict_strtol(buf, 10, &time);
1669
1670 input_event(wm8962->beep, EV_SND, SND_TONE, time);
1671
1672 return count;
1673}
1674
1675static DEVICE_ATTR(beep, 0200, NULL, wm8962_beep_set);
1676
1677static void wm8962_init_beep(struct snd_soc_codec *codec)
1678{
1679 struct wm8962_priv *wm8962 = snd_soc_codec_get_drvdata(codec);
1680 int ret;
1681
1682 wm8962->beep = input_allocate_device();
1683 if (!wm8962->beep) {
1684 dev_err(codec->dev, "Failed to allocate beep device\n");
1685 return;
1686 }
1687
1688 INIT_WORK(&wm8962->beep_work, wm8962_beep_work);
1689 wm8962->beep_rate = 0;
1690
1691 wm8962->beep->name = "WM8962 Beep Generator";
1692 wm8962->beep->phys = dev_name(codec->dev);
1693 wm8962->beep->id.bustype = BUS_I2C;
1694
1695 wm8962->beep->evbit[0] = BIT_MASK(EV_SND);
1696 wm8962->beep->sndbit[0] = BIT_MASK(SND_BELL) | BIT_MASK(SND_TONE);
1697 wm8962->beep->event = wm8962_beep_event;
1698 wm8962->beep->dev.parent = codec->dev;
1699 input_set_drvdata(wm8962->beep, codec);
1700
1701 ret = input_register_device(wm8962->beep);
1702 if (ret != 0) {
1703 input_free_device(wm8962->beep);
1704 wm8962->beep = NULL;
1705 dev_err(codec->dev, "Failed to register beep device\n");
1706 }
1707
1708 ret = device_create_file(codec->dev, &dev_attr_beep);
1709 if (ret != 0) {
1710 dev_err(codec->dev, "Failed to create keyclick file: %d\n",
1711 ret);
1712 }
1713}
1714
1715static void wm8962_free_beep(struct snd_soc_codec *codec)
1716{
1717 struct wm8962_priv *wm8962 = snd_soc_codec_get_drvdata(codec);
1718
1719 device_remove_file(codec->dev, &dev_attr_beep);
1720 input_unregister_device(wm8962->beep);
1721 cancel_work_sync(&wm8962->beep_work);
1722 wm8962->beep = NULL;
1723
1724 snd_soc_update_bits(codec, WM8962_BEEP_GENERATOR_1, WM8962_BEEP_ENA,0);
1725}
1726#else
1727static void wm8962_init_beep(struct snd_soc_codec *codec)
1728{
1729}
1730
1731static void wm8962_free_beep(struct snd_soc_codec *codec)
1732{
1733}
1734#endif
1735
Mark Brown3367b8d2010-09-20 17:34:58 +01001736#ifdef CONFIG_GPIOLIB
1737static inline struct wm8962_priv *gpio_to_wm8962(struct gpio_chip *chip)
1738{
1739 return container_of(chip, struct wm8962_priv, gpio_chip);
1740}
1741
1742static int wm8962_gpio_request(struct gpio_chip *chip, unsigned offset)
1743{
1744 struct wm8962_priv *wm8962 = gpio_to_wm8962(chip);
1745 struct snd_soc_codec *codec = wm8962->codec;
1746 int mask = 0;
1747 int val;
1748
1749 /* The WM8962 GPIOs aren't linearly numbered. For simplicity
1750 * we export linear numbers and error out if the unsupported
1751 * ones are requsted.
1752 */
1753 switch (offset + 1) {
1754 case 2:
1755 mask = WM8962_CLKOUT2_SEL_MASK;
1756 val = 1 << WM8962_CLKOUT2_SEL_SHIFT;
1757 break;
1758 case 3:
1759 mask = WM8962_CLKOUT3_SEL_MASK;
1760 val = 1 << WM8962_CLKOUT3_SEL_SHIFT;
1761 break;
1762 case 5:
1763 case 6:
1764 break;
1765 default:
1766 return -EINVAL;
1767 }
1768
1769 /* Some of the GPIOs are behind MFP configuration */
1770 if (mask)
1771 snd_soc_update_bits(codec, WM8962_ANALOGUE_CLOCKING1,
1772 mask, val);
1773
1774 return 0;
1775}
1776
1777static void wm8962_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
1778{
1779 struct wm8962_priv *wm8962 = gpio_to_wm8962(chip);
1780 struct snd_soc_codec *codec = wm8962->codec;
1781
1782 snd_soc_update_bits(codec, WM8962_GPIO_BASE + offset,
1783 WM8962_GP2_LVL, value << WM8962_GP2_LVL_SHIFT);
1784}
1785
1786static int wm8962_gpio_direction_out(struct gpio_chip *chip,
1787 unsigned offset, int value)
1788{
1789 struct wm8962_priv *wm8962 = gpio_to_wm8962(chip);
1790 struct snd_soc_codec *codec = wm8962->codec;
1791 int val;
1792
1793 /* Force function 1 (logic output) */
1794 val = (1 << WM8962_GP2_FN_SHIFT) | (value << WM8962_GP2_LVL_SHIFT);
1795
1796 return snd_soc_update_bits(codec, WM8962_GPIO_BASE + offset,
1797 WM8962_GP2_FN_MASK | WM8962_GP2_LVL, val);
1798}
1799
1800static struct gpio_chip wm8962_template_chip = {
1801 .label = "wm8962",
1802 .owner = THIS_MODULE,
1803 .request = wm8962_gpio_request,
1804 .direction_output = wm8962_gpio_direction_out,
1805 .set = wm8962_gpio_set,
1806 .can_sleep = 1,
1807};
1808
1809static void wm8962_init_gpio(struct snd_soc_codec *codec)
1810{
1811 struct wm8962_priv *wm8962 = snd_soc_codec_get_drvdata(codec);
1812 struct wm8962_pdata *pdata = dev_get_platdata(codec->dev);
1813 int ret;
1814
1815 wm8962->gpio_chip = wm8962_template_chip;
1816 wm8962->gpio_chip.ngpio = WM8962_MAX_GPIO;
1817 wm8962->gpio_chip.dev = codec->dev;
1818
1819 if (pdata && pdata->gpio_base)
1820 wm8962->gpio_chip.base = pdata->gpio_base;
1821 else
1822 wm8962->gpio_chip.base = -1;
1823
1824 ret = gpiochip_add(&wm8962->gpio_chip);
1825 if (ret != 0)
1826 dev_err(codec->dev, "Failed to add GPIOs: %d\n", ret);
1827}
1828
1829static void wm8962_free_gpio(struct snd_soc_codec *codec)
1830{
1831 struct wm8962_priv *wm8962 = snd_soc_codec_get_drvdata(codec);
1832 int ret;
1833
1834 ret = gpiochip_remove(&wm8962->gpio_chip);
1835 if (ret != 0)
1836 dev_err(codec->dev, "Failed to remove GPIOs: %d\n", ret);
1837}
1838#else
1839static void wm8962_init_gpio(struct snd_soc_codec *codec)
1840{
1841}
1842
1843static void wm8962_free_gpio(struct snd_soc_codec *codec)
1844{
1845}
1846#endif
1847
Mark Brown54d8d0a2010-08-12 15:02:11 +01001848static int wm8962_probe(struct snd_soc_codec *codec)
Mark Brown9a76f1f2010-08-05 13:20:59 +01001849{
1850 int ret;
Mark Brown54d8d0a2010-08-12 15:02:11 +01001851 struct wm8962_priv *wm8962 = snd_soc_codec_get_drvdata(codec);
Mark Brown9a76f1f2010-08-05 13:20:59 +01001852 struct wm8962_pdata *pdata = dev_get_platdata(codec->dev);
Mark Brown45e65502010-09-28 16:01:20 -07001853 struct i2c_client *i2c = container_of(codec->dev, struct i2c_client,
1854 dev);
1855 int i, trigger, irq_pol;
Mark Brown9a76f1f2010-08-05 13:20:59 +01001856
Mark Brown54d8d0a2010-08-12 15:02:11 +01001857 wm8962->codec = codec;
Mark Brown77113082010-09-30 15:37:53 -07001858 INIT_DELAYED_WORK(&wm8962->mic_work, wm8962_mic_work);
Mark Brown9a76f1f2010-08-05 13:20:59 +01001859
Mark Brown9a76f1f2010-08-05 13:20:59 +01001860 codec->cache_sync = 1;
1861 codec->idle_bias_off = 1;
Mark Brown9a76f1f2010-08-05 13:20:59 +01001862
Mark Brown54d8d0a2010-08-12 15:02:11 +01001863 ret = snd_soc_codec_set_cache_io(codec, 16, 16, SND_SOC_I2C);
Mark Brown9a76f1f2010-08-05 13:20:59 +01001864 if (ret != 0) {
1865 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
1866 goto err;
1867 }
1868
1869 for (i = 0; i < ARRAY_SIZE(wm8962->supplies); i++)
1870 wm8962->supplies[i].supply = wm8962_supply_names[i];
1871
1872 ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(wm8962->supplies),
1873 wm8962->supplies);
1874 if (ret != 0) {
1875 dev_err(codec->dev, "Failed to request supplies: %d\n", ret);
1876 goto err;
1877 }
1878
1879 wm8962->disable_nb[0].notifier_call = wm8962_regulator_event_0;
1880 wm8962->disable_nb[1].notifier_call = wm8962_regulator_event_1;
1881 wm8962->disable_nb[2].notifier_call = wm8962_regulator_event_2;
1882 wm8962->disable_nb[3].notifier_call = wm8962_regulator_event_3;
1883 wm8962->disable_nb[4].notifier_call = wm8962_regulator_event_4;
1884 wm8962->disable_nb[5].notifier_call = wm8962_regulator_event_5;
1885 wm8962->disable_nb[6].notifier_call = wm8962_regulator_event_6;
1886 wm8962->disable_nb[7].notifier_call = wm8962_regulator_event_7;
1887
1888 /* This should really be moved into the regulator core */
1889 for (i = 0; i < ARRAY_SIZE(wm8962->supplies); i++) {
1890 ret = regulator_register_notifier(wm8962->supplies[i].consumer,
1891 &wm8962->disable_nb[i]);
1892 if (ret != 0) {
1893 dev_err(codec->dev,
1894 "Failed to register regulator notifier: %d\n",
1895 ret);
1896 }
1897 }
1898
1899 ret = regulator_bulk_enable(ARRAY_SIZE(wm8962->supplies),
1900 wm8962->supplies);
1901 if (ret != 0) {
1902 dev_err(codec->dev, "Failed to enable supplies: %d\n", ret);
1903 goto err_get;
1904 }
1905
1906 ret = snd_soc_read(codec, WM8962_SOFTWARE_RESET);
1907 if (ret < 0) {
1908 dev_err(codec->dev, "Failed to read ID register\n");
1909 goto err_enable;
1910 }
1911 if (ret != wm8962_reg[WM8962_SOFTWARE_RESET]) {
1912 dev_err(codec->dev, "Device is not a WM8962, ID %x != %x\n",
1913 ret, wm8962_reg[WM8962_SOFTWARE_RESET]);
1914 ret = -EINVAL;
1915 goto err_enable;
1916 }
1917
1918 ret = snd_soc_read(codec, WM8962_RIGHT_INPUT_VOLUME);
1919 if (ret < 0) {
1920 dev_err(codec->dev, "Failed to read device revision: %d\n",
1921 ret);
1922 goto err_enable;
1923 }
1924
1925 dev_info(codec->dev, "customer id %x revision %c\n",
1926 (ret & WM8962_CUST_ID_MASK) >> WM8962_CUST_ID_SHIFT,
1927 ((ret & WM8962_CHIP_REV_MASK) >> WM8962_CHIP_REV_SHIFT)
1928 + 'A');
1929
1930 ret = wm8962_reset(codec);
1931 if (ret < 0) {
1932 dev_err(codec->dev, "Failed to issue reset\n");
1933 goto err_enable;
1934 }
1935
1936 /* SYSCLK defaults to on; make sure it is off so we can safely
1937 * write to registers if the device is declocked.
1938 */
1939 snd_soc_update_bits(codec, WM8962_CLOCKING2, WM8962_SYSCLK_ENA, 0);
1940
1941 regulator_bulk_disable(ARRAY_SIZE(wm8962->supplies), wm8962->supplies);
1942
1943 if (pdata) {
1944 /* Apply static configuration for GPIOs */
1945 for (i = 0; i < ARRAY_SIZE(pdata->gpio_init); i++)
1946 if (pdata->gpio_init[i])
1947 snd_soc_write(codec, 0x200 + i,
1948 pdata->gpio_init[i] & 0xffff);
1949
1950 /* Put the speakers into mono mode? */
1951 if (pdata->spk_mono)
1952 wm8962->reg_cache[WM8962_CLASS_D_CONTROL_2]
1953 |= WM8962_SPK_MONO;
Mark Browna4f28c02010-09-29 13:24:35 -07001954
1955 /* Micbias setup, detection enable and detection
1956 * threasholds. */
1957 if (pdata->mic_cfg)
1958 snd_soc_update_bits(codec, WM8962_ADDITIONAL_CONTROL_4,
1959 WM8962_MICDET_ENA |
1960 WM8962_MICDET_THR_MASK |
1961 WM8962_MICSHORT_THR_MASK |
1962 WM8962_MICBIAS_LVL,
1963 pdata->mic_cfg);
Mark Brown9a76f1f2010-08-05 13:20:59 +01001964 }
1965
1966 /* Latch volume update bits */
1967 wm8962->reg_cache[WM8962_LEFT_INPUT_VOLUME] |= WM8962_IN_VU;
1968 wm8962->reg_cache[WM8962_RIGHT_INPUT_VOLUME] |= WM8962_IN_VU;
1969 wm8962->reg_cache[WM8962_LEFT_ADC_VOLUME] |= WM8962_ADC_VU;
1970 wm8962->reg_cache[WM8962_RIGHT_ADC_VOLUME] |= WM8962_ADC_VU;
1971 wm8962->reg_cache[WM8962_LEFT_DAC_VOLUME] |= WM8962_DAC_VU;
1972 wm8962->reg_cache[WM8962_RIGHT_DAC_VOLUME] |= WM8962_DAC_VU;
1973 wm8962->reg_cache[WM8962_SPKOUTL_VOLUME] |= WM8962_SPKOUT_VU;
1974 wm8962->reg_cache[WM8962_SPKOUTR_VOLUME] |= WM8962_SPKOUT_VU;
1975 wm8962->reg_cache[WM8962_HPOUTL_VOLUME] |= WM8962_HPOUT_VU;
1976 wm8962->reg_cache[WM8962_HPOUTR_VOLUME] |= WM8962_HPOUT_VU;
1977
Mark Brown54d8d0a2010-08-12 15:02:11 +01001978 wm8962_add_widgets(codec);
Mark Brown9a76f1f2010-08-05 13:20:59 +01001979
1980 wm8962_init_beep(codec);
Mark Brown3367b8d2010-09-20 17:34:58 +01001981 wm8962_init_gpio(codec);
Mark Brown9a76f1f2010-08-05 13:20:59 +01001982
Mark Brown45e65502010-09-28 16:01:20 -07001983 if (i2c->irq) {
1984 if (pdata && pdata->irq_active_low) {
1985 trigger = IRQF_TRIGGER_LOW;
1986 irq_pol = WM8962_IRQ_POL;
1987 } else {
1988 trigger = IRQF_TRIGGER_HIGH;
1989 irq_pol = 0;
1990 }
1991
1992 snd_soc_update_bits(codec, WM8962_INTERRUPT_CONTROL,
1993 WM8962_IRQ_POL, irq_pol);
1994
1995 ret = request_threaded_irq(i2c->irq, NULL, wm8962_irq,
1996 trigger | IRQF_ONESHOT,
1997 "wm8962", codec);
1998 if (ret != 0) {
1999 dev_err(codec->dev, "Failed to request IRQ %d: %d\n",
2000 i2c->irq, ret);
2001 /* Non-fatal */
2002 } else {
2003 /* Enable error reporting IRQs by default */
2004 snd_soc_update_bits(codec,
2005 WM8962_INTERRUPT_STATUS_2_MASK,
2006 WM8962_TEMP_SHUT_EINT |
2007 WM8962_FIFOS_ERR_EINT, 0);
2008 }
2009 }
2010
Mark Brown9a76f1f2010-08-05 13:20:59 +01002011 return 0;
2012
2013err_enable:
2014 regulator_bulk_disable(ARRAY_SIZE(wm8962->supplies), wm8962->supplies);
2015err_get:
2016 regulator_bulk_free(ARRAY_SIZE(wm8962->supplies), wm8962->supplies);
2017err:
2018 kfree(wm8962);
2019 return ret;
2020}
2021
Mark Brown54d8d0a2010-08-12 15:02:11 +01002022static int wm8962_remove(struct snd_soc_codec *codec)
Mark Brown9a76f1f2010-08-05 13:20:59 +01002023{
Mark Brown54d8d0a2010-08-12 15:02:11 +01002024 struct wm8962_priv *wm8962 = snd_soc_codec_get_drvdata(codec);
Mark Brown45e65502010-09-28 16:01:20 -07002025 struct i2c_client *i2c = container_of(codec->dev, struct i2c_client,
2026 dev);
Mark Brown9a76f1f2010-08-05 13:20:59 +01002027 int i;
2028
Mark Brown45e65502010-09-28 16:01:20 -07002029 if (i2c->irq)
2030 free_irq(i2c->irq, codec);
2031
Mark Brown77113082010-09-30 15:37:53 -07002032 cancel_delayed_work_sync(&wm8962->mic_work);
2033
Mark Brown3367b8d2010-09-20 17:34:58 +01002034 wm8962_free_gpio(codec);
Mark Brown54d8d0a2010-08-12 15:02:11 +01002035 wm8962_free_beep(codec);
Mark Brown9a76f1f2010-08-05 13:20:59 +01002036 for (i = 0; i < ARRAY_SIZE(wm8962->supplies); i++)
2037 regulator_unregister_notifier(wm8962->supplies[i].consumer,
2038 &wm8962->disable_nb[i]);
2039 regulator_bulk_free(ARRAY_SIZE(wm8962->supplies), wm8962->supplies);
Mark Brown54d8d0a2010-08-12 15:02:11 +01002040
2041 return 0;
Mark Brown9a76f1f2010-08-05 13:20:59 +01002042}
2043
Mark Brown54d8d0a2010-08-12 15:02:11 +01002044static struct snd_soc_codec_driver soc_codec_dev_wm8962 = {
2045 .probe = wm8962_probe,
2046 .remove = wm8962_remove,
2047 .resume = wm8962_resume,
2048 .set_bias_level = wm8962_set_bias_level,
Dimitris Papastamos6946e032010-09-10 18:24:08 +01002049 .reg_cache_size = WM8962_MAX_REGISTER + 1,
Mark Brown54d8d0a2010-08-12 15:02:11 +01002050 .reg_word_size = sizeof(u16),
2051 .reg_cache_default = wm8962_reg,
2052 .volatile_register = wm8962_volatile_register,
2053 .readable_register = wm8962_readable_register,
2054};
2055
Mark Brown9a76f1f2010-08-05 13:20:59 +01002056#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
2057static __devinit int wm8962_i2c_probe(struct i2c_client *i2c,
2058 const struct i2c_device_id *id)
2059{
2060 struct wm8962_priv *wm8962;
Mark Brown54d8d0a2010-08-12 15:02:11 +01002061 int ret;
Mark Brown9a76f1f2010-08-05 13:20:59 +01002062
2063 wm8962 = kzalloc(sizeof(struct wm8962_priv), GFP_KERNEL);
2064 if (wm8962 == NULL)
2065 return -ENOMEM;
2066
Mark Brown9a76f1f2010-08-05 13:20:59 +01002067 i2c_set_clientdata(i2c, wm8962);
Mark Brown9a76f1f2010-08-05 13:20:59 +01002068
Mark Brown54d8d0a2010-08-12 15:02:11 +01002069 ret = snd_soc_register_codec(&i2c->dev,
2070 &soc_codec_dev_wm8962, &wm8962_dai, 1);
2071 if (ret < 0)
2072 kfree(wm8962);
Mark Brown9a76f1f2010-08-05 13:20:59 +01002073
Mark Brown54d8d0a2010-08-12 15:02:11 +01002074 return ret;
Mark Brown9a76f1f2010-08-05 13:20:59 +01002075}
2076
2077static __devexit int wm8962_i2c_remove(struct i2c_client *client)
2078{
Mark Brown54d8d0a2010-08-12 15:02:11 +01002079 snd_soc_unregister_codec(&client->dev);
2080 kfree(i2c_get_clientdata(client));
Mark Brown9a76f1f2010-08-05 13:20:59 +01002081 return 0;
2082}
2083
2084static const struct i2c_device_id wm8962_i2c_id[] = {
2085 { "wm8962", 0 },
2086 { }
2087};
2088MODULE_DEVICE_TABLE(i2c, wm8962_i2c_id);
2089
2090static struct i2c_driver wm8962_i2c_driver = {
2091 .driver = {
Mark Brownea738ba2010-09-20 20:36:19 +01002092 .name = "wm8962",
Mark Brown9a76f1f2010-08-05 13:20:59 +01002093 .owner = THIS_MODULE,
2094 },
2095 .probe = wm8962_i2c_probe,
2096 .remove = __devexit_p(wm8962_i2c_remove),
2097 .id_table = wm8962_i2c_id,
2098};
2099#endif
2100
2101static int __init wm8962_modinit(void)
2102{
2103 int ret;
2104#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
2105 ret = i2c_add_driver(&wm8962_i2c_driver);
2106 if (ret != 0) {
2107 printk(KERN_ERR "Failed to register WM8962 I2C driver: %d\n",
2108 ret);
2109 }
2110#endif
2111 return 0;
2112}
2113module_init(wm8962_modinit);
2114
2115static void __exit wm8962_exit(void)
2116{
2117#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
2118 i2c_del_driver(&wm8962_i2c_driver);
2119#endif
2120}
2121module_exit(wm8962_exit);
2122
2123MODULE_DESCRIPTION("ASoC WM8962 driver");
2124MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
2125MODULE_LICENSE("GPL");