]> git.karo-electronics.de Git - mv-sheeva.git/blob - sound/soc/codecs/wm8955.c
ASoC: multi-component - ASoC Multi-Component Support
[mv-sheeva.git] / sound / soc / codecs / wm8955.c
1 /*
2  * wm8955.c  --  WM8955 ALSA SoC Audio driver
3  *
4  * Copyright 2009 Wolfson Microelectronics plc
5  *
6  * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/init.h>
16 #include <linux/delay.h>
17 #include <linux/pm.h>
18 #include <linux/i2c.h>
19 #include <linux/platform_device.h>
20 #include <linux/regulator/consumer.h>
21 #include <linux/slab.h>
22 #include <sound/core.h>
23 #include <sound/pcm.h>
24 #include <sound/pcm_params.h>
25 #include <sound/soc.h>
26 #include <sound/soc-dapm.h>
27 #include <sound/initval.h>
28 #include <sound/tlv.h>
29 #include <sound/wm8955.h>
30
31 #include "wm8955.h"
32
33 #define WM8955_NUM_SUPPLIES 4
34 static const char *wm8955_supply_names[WM8955_NUM_SUPPLIES] = {
35         "DCVDD",
36         "DBVDD",
37         "HPVDD",
38         "AVDD",
39 };
40
41 /* codec private data */
42 struct wm8955_priv {
43         enum snd_soc_control_type control_type;
44         void *control_data;
45
46         u16 reg_cache[WM8955_MAX_REGISTER + 1];
47
48         unsigned int mclk_rate;
49
50         int deemph;
51         int fs;
52
53         struct regulator_bulk_data supplies[WM8955_NUM_SUPPLIES];
54 };
55
56 static const u16 wm8955_reg[WM8955_MAX_REGISTER + 1] = {
57         0x0000,     /* R0 */
58         0x0000,     /* R1 */
59         0x0079,     /* R2  - LOUT1 volume */
60         0x0079,     /* R3  - ROUT1 volume */
61         0x0000,     /* R4 */
62         0x0008,     /* R5  - DAC Control */
63         0x0000,     /* R6 */
64         0x000A,     /* R7  - Audio Interface */
65         0x0000,     /* R8  - Sample Rate */
66         0x0000,     /* R9 */
67         0x00FF,     /* R10 - Left DAC volume */
68         0x00FF,     /* R11 - Right DAC volume */
69         0x000F,     /* R12 - Bass control */
70         0x000F,     /* R13 - Treble control */
71         0x0000,     /* R14 */
72         0x0000,     /* R15 - Reset */
73         0x0000,     /* R16 */
74         0x0000,     /* R17 */
75         0x0000,     /* R18 */
76         0x0000,     /* R19 */
77         0x0000,     /* R20 */
78         0x0000,     /* R21 */
79         0x0000,     /* R22 */
80         0x00C1,     /* R23 - Additional control (1) */
81         0x0000,     /* R24 - Additional control (2) */
82         0x0000,     /* R25 - Power Management (1) */
83         0x0000,     /* R26 - Power Management (2) */
84         0x0000,     /* R27 - Additional Control (3) */
85         0x0000,     /* R28 */
86         0x0000,     /* R29 */
87         0x0000,     /* R30 */
88         0x0000,     /* R31 */
89         0x0000,     /* R32 */
90         0x0000,     /* R33 */
91         0x0050,     /* R34 - Left out Mix (1) */
92         0x0050,     /* R35 - Left out Mix (2) */
93         0x0050,     /* R36 - Right out Mix (1) */
94         0x0050,     /* R37 - Right Out Mix (2) */
95         0x0050,     /* R38 - Mono out Mix (1) */
96         0x0050,     /* R39 - Mono out Mix (2) */
97         0x0079,     /* R40 - LOUT2 volume */
98         0x0079,     /* R41 - ROUT2 volume */
99         0x0079,     /* R42 - MONOOUT volume */
100         0x0000,     /* R43 - Clocking / PLL */
101         0x0103,     /* R44 - PLL Control 1 */
102         0x0024,     /* R45 - PLL Control 2 */
103         0x01BA,     /* R46 - PLL Control 3 */
104         0x0000,     /* R47 */
105         0x0000,     /* R48 */
106         0x0000,     /* R49 */
107         0x0000,     /* R50 */
108         0x0000,     /* R51 */
109         0x0000,     /* R52 */
110         0x0000,     /* R53 */
111         0x0000,     /* R54 */
112         0x0000,     /* R55 */
113         0x0000,     /* R56 */
114         0x0000,     /* R57 */
115         0x0000,     /* R58 */
116         0x0000,     /* R59 - PLL Control 4 */
117 };
118
119 static int wm8955_reset(struct snd_soc_codec *codec)
120 {
121         return snd_soc_write(codec, WM8955_RESET, 0);
122 }
123
124 struct pll_factors {
125         int n;
126         int k;
127         int outdiv;
128 };
129
130 /* The size in bits of the FLL divide multiplied by 10
131  * to allow rounding later */
132 #define FIXED_FLL_SIZE ((1 << 22) * 10)
133
134 static int wm8995_pll_factors(struct device *dev,
135                               int Fref, int Fout, struct pll_factors *pll)
136 {
137         u64 Kpart;
138         unsigned int K, Ndiv, Nmod, target;
139
140         dev_dbg(dev, "Fref=%u Fout=%u\n", Fref, Fout);
141
142         /* The oscilator should run at should be 90-100MHz, and
143          * there's a divide by 4 plus an optional divide by 2 in the
144          * output path to generate the system clock.  The clock table
145          * is sortd so we should always generate a suitable target. */
146         target = Fout * 4;
147         if (target < 90000000) {
148                 pll->outdiv = 1;
149                 target *= 2;
150         } else {
151                 pll->outdiv = 0;
152         }
153
154         WARN_ON(target < 90000000 || target > 100000000);
155
156         dev_dbg(dev, "Fvco=%dHz\n", target);
157
158         /* Now, calculate N.K */
159         Ndiv = target / Fref;
160
161         pll->n = Ndiv;
162         Nmod = target % Fref;
163         dev_dbg(dev, "Nmod=%d\n", Nmod);
164
165         /* Calculate fractional part - scale up so we can round. */
166         Kpart = FIXED_FLL_SIZE * (long long)Nmod;
167
168         do_div(Kpart, Fref);
169
170         K = Kpart & 0xFFFFFFFF;
171
172         if ((K % 10) >= 5)
173                 K += 5;
174
175         /* Move down to proper range now rounding is done */
176         pll->k = K / 10;
177
178         dev_dbg(dev, "N=%x K=%x OUTDIV=%x\n", pll->n, pll->k, pll->outdiv);
179
180         return 0;
181 }
182
183 /* Lookup table specifiying SRATE (table 25 in datasheet); some of the
184  * output frequencies have been rounded to the standard frequencies
185  * they are intended to match where the error is slight. */
186 static struct {
187         int mclk;
188         int fs;
189         int usb;
190         int sr;
191 } clock_cfgs[] = {
192         { 18432000,  8000, 0,  3, },
193         { 18432000, 12000, 0,  9, },
194         { 18432000, 16000, 0, 11, },
195         { 18432000, 24000, 0, 29, },
196         { 18432000, 32000, 0, 13, },
197         { 18432000, 48000, 0,  1, },
198         { 18432000, 96000, 0, 15, },
199
200         { 16934400,  8018, 0, 19, },
201         { 16934400, 11025, 0, 25, },
202         { 16934400, 22050, 0, 27, },
203         { 16934400, 44100, 0, 17, },
204         { 16934400, 88200, 0, 31, },
205
206         { 12000000,  8000, 1,  2, },
207         { 12000000, 11025, 1, 25, },
208         { 12000000, 12000, 1,  8, },
209         { 12000000, 16000, 1, 10, },
210         { 12000000, 22050, 1, 27, },
211         { 12000000, 24000, 1, 28, },
212         { 12000000, 32000, 1, 12, },
213         { 12000000, 44100, 1, 17, },
214         { 12000000, 48000, 1,  0, },
215         { 12000000, 88200, 1, 31, },
216         { 12000000, 96000, 1, 14, },
217
218         { 12288000,  8000, 0,  2, },
219         { 12288000, 12000, 0,  8, },
220         { 12288000, 16000, 0, 10, },
221         { 12288000, 24000, 0, 28, },
222         { 12288000, 32000, 0, 12, },
223         { 12288000, 48000, 0,  0, },
224         { 12288000, 96000, 0, 14, },
225
226         { 12289600,  8018, 0, 18, },
227         { 12289600, 11025, 0, 24, },
228         { 12289600, 22050, 0, 26, },
229         { 11289600, 44100, 0, 16, },
230         { 11289600, 88200, 0, 31, },
231 };
232
233 static int wm8955_configure_clocking(struct snd_soc_codec *codec)
234 {
235         struct wm8955_priv *wm8955 = snd_soc_codec_get_drvdata(codec);
236         int i, ret, val;
237         int clocking = 0;
238         int srate = 0;
239         int sr = -1;
240         struct pll_factors pll;
241
242         /* If we're not running a sample rate currently just pick one */
243         if (wm8955->fs == 0)
244                 wm8955->fs = 8000;
245
246         /* Can we generate an exact output? */
247         for (i = 0; i < ARRAY_SIZE(clock_cfgs); i++) {
248                 if (wm8955->fs != clock_cfgs[i].fs)
249                         continue;
250                 sr = i;
251
252                 if (wm8955->mclk_rate == clock_cfgs[i].mclk)
253                         break;
254         }
255
256         /* We should never get here with an unsupported sample rate */
257         if (sr == -1) {
258                 dev_err(codec->dev, "Sample rate %dHz unsupported\n",
259                         wm8955->fs);
260                 WARN_ON(sr == -1);
261                 return -EINVAL;
262         }
263
264         if (i == ARRAY_SIZE(clock_cfgs)) {
265                 /* If we can't generate the right clock from MCLK then
266                  * we should configure the PLL to supply us with an
267                  * appropriate clock.
268                  */
269                 clocking |= WM8955_MCLKSEL;
270
271                 /* Use the last divider configuration we saw for the
272                  * sample rate. */
273                 ret = wm8995_pll_factors(codec->dev, wm8955->mclk_rate,
274                                          clock_cfgs[sr].mclk, &pll);
275                 if (ret != 0) {
276                         dev_err(codec->dev,
277                                 "Unable to generate %dHz from %dHz MCLK\n",
278                                 wm8955->fs, wm8955->mclk_rate);
279                         return -EINVAL;
280                 }
281
282                 snd_soc_update_bits(codec, WM8955_PLL_CONTROL_1,
283                                     WM8955_N_MASK | WM8955_K_21_18_MASK,
284                                     (pll.n << WM8955_N_SHIFT) |
285                                     pll.k >> 18);
286                 snd_soc_update_bits(codec, WM8955_PLL_CONTROL_2,
287                                     WM8955_K_17_9_MASK,
288                                     (pll.k >> 9) & WM8955_K_17_9_MASK);
289                 snd_soc_update_bits(codec, WM8955_PLL_CONTROL_2,
290                                     WM8955_K_8_0_MASK,
291                                     pll.k & WM8955_K_8_0_MASK);
292                 if (pll.k)
293                         snd_soc_update_bits(codec, WM8955_PLL_CONTROL_4,
294                                             WM8955_KEN, WM8955_KEN);
295                 else
296                         snd_soc_update_bits(codec, WM8955_PLL_CONTROL_4,
297                                             WM8955_KEN, 0);
298
299                 if (pll.outdiv)
300                         val = WM8955_PLL_RB | WM8955_PLLOUTDIV2;
301                 else
302                         val = WM8955_PLL_RB;
303
304                 /* Now start the PLL running */
305                 snd_soc_update_bits(codec, WM8955_CLOCKING_PLL,
306                                     WM8955_PLL_RB | WM8955_PLLOUTDIV2, val);
307                 snd_soc_update_bits(codec, WM8955_CLOCKING_PLL,
308                                     WM8955_PLLEN, WM8955_PLLEN);
309         }
310
311         srate = clock_cfgs[sr].usb | (clock_cfgs[sr].sr << WM8955_SR_SHIFT);
312
313         snd_soc_update_bits(codec, WM8955_SAMPLE_RATE,
314                             WM8955_USB | WM8955_SR_MASK, srate);
315         snd_soc_update_bits(codec, WM8955_CLOCKING_PLL,
316                             WM8955_MCLKSEL, clocking);
317
318         return 0;
319 }
320
321 static int wm8955_sysclk(struct snd_soc_dapm_widget *w,
322                          struct snd_kcontrol *kcontrol, int event)
323 {
324         struct snd_soc_codec *codec = w->codec;
325         int ret = 0;
326
327         /* Always disable the clocks - if we're doing reconfiguration this
328          * avoids misclocking.
329          */
330         snd_soc_update_bits(codec, WM8955_POWER_MANAGEMENT_1,
331                             WM8955_DIGENB, 0);
332         snd_soc_update_bits(codec, WM8955_CLOCKING_PLL,
333                             WM8955_PLL_RB | WM8955_PLLEN, 0);
334
335         switch (event) {
336         case SND_SOC_DAPM_POST_PMD:
337                 break;
338         case SND_SOC_DAPM_PRE_PMU:
339                 ret = wm8955_configure_clocking(codec);
340                 break;
341         default:
342                 ret = -EINVAL;
343                 break;
344         }
345
346         return ret;
347 }
348
349 static int deemph_settings[] = { 0, 32000, 44100, 48000 };
350
351 static int wm8955_set_deemph(struct snd_soc_codec *codec)
352 {
353         struct wm8955_priv *wm8955 = snd_soc_codec_get_drvdata(codec);
354         int val, i, best;
355
356         /* If we're using deemphasis select the nearest available sample
357          * rate.
358          */
359         if (wm8955->deemph) {
360                 best = 1;
361                 for (i = 2; i < ARRAY_SIZE(deemph_settings); i++) {
362                         if (abs(deemph_settings[i] - wm8955->fs) <
363                             abs(deemph_settings[best] - wm8955->fs))
364                                 best = i;
365                 }
366
367                 val = best << WM8955_DEEMPH_SHIFT;
368         } else {
369                 val = 0;
370         }
371
372         dev_dbg(codec->dev, "Set deemphasis %d\n", val);
373
374         return snd_soc_update_bits(codec, WM8955_DAC_CONTROL,
375                                    WM8955_DEEMPH_MASK, val);
376 }
377
378 static int wm8955_get_deemph(struct snd_kcontrol *kcontrol,
379                              struct snd_ctl_elem_value *ucontrol)
380 {
381         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
382         struct wm8955_priv *wm8955 = snd_soc_codec_get_drvdata(codec);
383
384         return wm8955->deemph;
385 }
386
387 static int wm8955_put_deemph(struct snd_kcontrol *kcontrol,
388                              struct snd_ctl_elem_value *ucontrol)
389 {
390         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
391         struct wm8955_priv *wm8955 = snd_soc_codec_get_drvdata(codec);
392         int deemph = ucontrol->value.enumerated.item[0];
393
394         if (deemph > 1)
395                 return -EINVAL;
396
397         wm8955->deemph = deemph;
398
399         return wm8955_set_deemph(codec);
400 }
401
402 static const char *bass_mode_text[] = {
403         "Linear", "Adaptive",
404 };
405
406 static const struct soc_enum bass_mode =
407         SOC_ENUM_SINGLE(WM8955_BASS_CONTROL, 7, 2, bass_mode_text);
408
409 static const char *bass_cutoff_text[] = {
410         "Low", "High"
411 };
412
413 static const struct soc_enum bass_cutoff =
414         SOC_ENUM_SINGLE(WM8955_BASS_CONTROL, 6, 2, bass_cutoff_text);
415
416 static const char *treble_cutoff_text[] = {
417         "High", "Low"
418 };
419
420 static const struct soc_enum treble_cutoff =
421         SOC_ENUM_SINGLE(WM8955_TREBLE_CONTROL, 6, 2, treble_cutoff_text);
422
423 static const DECLARE_TLV_DB_SCALE(digital_tlv, -12750, 50, 1);
424 static const DECLARE_TLV_DB_SCALE(atten_tlv, -600, 600, 0);
425 static const DECLARE_TLV_DB_SCALE(bypass_tlv, -1500, 300, 0);
426 static const DECLARE_TLV_DB_SCALE(mono_tlv, -2100, 300, 0);
427 static const DECLARE_TLV_DB_SCALE(out_tlv, -12100, 100, 1);
428 static const DECLARE_TLV_DB_SCALE(treble_tlv, -1200, 150, 1);
429
430 static const struct snd_kcontrol_new wm8955_snd_controls[] = {
431 SOC_DOUBLE_R_TLV("Digital Playback Volume", WM8955_LEFT_DAC_VOLUME,
432                  WM8955_RIGHT_DAC_VOLUME, 0, 255, 0, digital_tlv),
433 SOC_SINGLE_TLV("Playback Attenuation Volume", WM8955_DAC_CONTROL, 7, 1, 1,
434                atten_tlv),
435 SOC_SINGLE_BOOL_EXT("DAC Deemphasis Switch", 0,
436                     wm8955_get_deemph, wm8955_put_deemph),
437
438 SOC_ENUM("Bass Mode", bass_mode),
439 SOC_ENUM("Bass Cutoff", bass_cutoff),
440 SOC_SINGLE("Bass Volume", WM8955_BASS_CONTROL, 0, 15, 1),
441
442 SOC_ENUM("Treble Cutoff", treble_cutoff),
443 SOC_SINGLE_TLV("Treble Volume", WM8955_TREBLE_CONTROL, 0, 14, 1, treble_tlv),
444
445 SOC_SINGLE_TLV("Left Bypass Volume", WM8955_LEFT_OUT_MIX_1, 4, 7, 1,
446                bypass_tlv),
447 SOC_SINGLE_TLV("Left Mono Volume", WM8955_LEFT_OUT_MIX_2, 4, 7, 1,
448                bypass_tlv),
449
450 SOC_SINGLE_TLV("Right Mono Volume", WM8955_RIGHT_OUT_MIX_1, 4, 7, 1,
451                bypass_tlv),
452 SOC_SINGLE_TLV("Right Bypass Volume", WM8955_RIGHT_OUT_MIX_2, 4, 7, 1,
453                bypass_tlv),
454
455 /* Not a stereo pair so they line up with the DAPM switches */
456 SOC_SINGLE_TLV("Mono Left Bypass Volume", WM8955_MONO_OUT_MIX_1, 4, 7, 1,
457                mono_tlv),
458 SOC_SINGLE_TLV("Mono Right Bypass Volume", WM8955_MONO_OUT_MIX_2, 4, 7, 1,
459                mono_tlv),
460
461 SOC_DOUBLE_R_TLV("Headphone Volume", WM8955_LOUT1_VOLUME,
462                  WM8955_ROUT1_VOLUME, 0, 127, 0, out_tlv),
463 SOC_DOUBLE_R("Headphone ZC Switch", WM8955_LOUT1_VOLUME,
464              WM8955_ROUT1_VOLUME, 7, 1, 0),
465
466 SOC_DOUBLE_R_TLV("Speaker Volume", WM8955_LOUT2_VOLUME,
467                  WM8955_ROUT2_VOLUME, 0, 127, 0, out_tlv),
468 SOC_DOUBLE_R("Speaker ZC Switch", WM8955_LOUT2_VOLUME,
469              WM8955_ROUT2_VOLUME, 7, 1, 0),
470
471 SOC_SINGLE_TLV("Mono Volume", WM8955_MONOOUT_VOLUME, 0, 127, 0, out_tlv),
472 SOC_SINGLE("Mono ZC Switch", WM8955_MONOOUT_VOLUME, 7, 1, 0),
473 };
474
475 static const struct snd_kcontrol_new lmixer[] = {
476 SOC_DAPM_SINGLE("Playback Switch", WM8955_LEFT_OUT_MIX_1, 8, 1, 0),
477 SOC_DAPM_SINGLE("Bypass Switch", WM8955_LEFT_OUT_MIX_1, 7, 1, 0),
478 SOC_DAPM_SINGLE("Right Playback Switch", WM8955_LEFT_OUT_MIX_2, 8, 1, 0),
479 SOC_DAPM_SINGLE("Mono Switch", WM8955_LEFT_OUT_MIX_2, 7, 1, 0),
480 };
481
482 static const struct snd_kcontrol_new rmixer[] = {
483 SOC_DAPM_SINGLE("Left Playback Switch", WM8955_RIGHT_OUT_MIX_1, 8, 1, 0),
484 SOC_DAPM_SINGLE("Mono Switch", WM8955_RIGHT_OUT_MIX_1, 7, 1, 0),
485 SOC_DAPM_SINGLE("Playback Switch", WM8955_RIGHT_OUT_MIX_2, 8, 1, 0),
486 SOC_DAPM_SINGLE("Bypass Switch", WM8955_RIGHT_OUT_MIX_2, 7, 1, 0),
487 };
488
489 static const struct snd_kcontrol_new mmixer[] = {
490 SOC_DAPM_SINGLE("Left Playback Switch", WM8955_MONO_OUT_MIX_1, 8, 1, 0),
491 SOC_DAPM_SINGLE("Left Bypass Switch", WM8955_MONO_OUT_MIX_1, 7, 1, 0),
492 SOC_DAPM_SINGLE("Right Playback Switch", WM8955_MONO_OUT_MIX_2, 8, 1, 0),
493 SOC_DAPM_SINGLE("Right Bypass Switch", WM8955_MONO_OUT_MIX_2, 7, 1, 0),
494 };
495
496 static const struct snd_soc_dapm_widget wm8955_dapm_widgets[] = {
497 SND_SOC_DAPM_INPUT("MONOIN-"),
498 SND_SOC_DAPM_INPUT("MONOIN+"),
499 SND_SOC_DAPM_INPUT("LINEINR"),
500 SND_SOC_DAPM_INPUT("LINEINL"),
501
502 SND_SOC_DAPM_PGA("Mono Input", SND_SOC_NOPM, 0, 0, NULL, 0),
503
504 SND_SOC_DAPM_SUPPLY("SYSCLK", WM8955_POWER_MANAGEMENT_1, 0, 1, wm8955_sysclk,
505                     SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
506 SND_SOC_DAPM_SUPPLY("TSDEN", WM8955_ADDITIONAL_CONTROL_1, 8, 0, NULL, 0),
507
508 SND_SOC_DAPM_DAC("DACL", "Playback", WM8955_POWER_MANAGEMENT_2, 8, 0),
509 SND_SOC_DAPM_DAC("DACR", "Playback", WM8955_POWER_MANAGEMENT_2, 7, 0),
510
511 SND_SOC_DAPM_PGA("LOUT1 PGA", WM8955_POWER_MANAGEMENT_2, 6, 0, NULL, 0),
512 SND_SOC_DAPM_PGA("ROUT1 PGA", WM8955_POWER_MANAGEMENT_2, 5, 0, NULL, 0),
513 SND_SOC_DAPM_PGA("LOUT2 PGA", WM8955_POWER_MANAGEMENT_2, 4, 0, NULL, 0),
514 SND_SOC_DAPM_PGA("ROUT2 PGA", WM8955_POWER_MANAGEMENT_2, 3, 0, NULL, 0),
515 SND_SOC_DAPM_PGA("MOUT PGA", WM8955_POWER_MANAGEMENT_2, 2, 0, NULL, 0),
516 SND_SOC_DAPM_PGA("OUT3 PGA", WM8955_POWER_MANAGEMENT_2, 1, 0, NULL, 0),
517
518 /* The names are chosen to make the control names nice */
519 SND_SOC_DAPM_MIXER("Left", SND_SOC_NOPM, 0, 0,
520                    lmixer, ARRAY_SIZE(lmixer)),
521 SND_SOC_DAPM_MIXER("Right", SND_SOC_NOPM, 0, 0,
522                    rmixer, ARRAY_SIZE(rmixer)),
523 SND_SOC_DAPM_MIXER("Mono", SND_SOC_NOPM, 0, 0,
524                    mmixer, ARRAY_SIZE(mmixer)),
525
526 SND_SOC_DAPM_OUTPUT("LOUT1"),
527 SND_SOC_DAPM_OUTPUT("ROUT1"),
528 SND_SOC_DAPM_OUTPUT("LOUT2"),
529 SND_SOC_DAPM_OUTPUT("ROUT2"),
530 SND_SOC_DAPM_OUTPUT("MONOOUT"),
531 SND_SOC_DAPM_OUTPUT("OUT3"),
532 };
533
534 static const struct snd_soc_dapm_route wm8955_intercon[] = {
535         { "DACL", NULL, "SYSCLK" },
536         { "DACR", NULL, "SYSCLK" },
537
538         { "Mono Input", NULL, "MONOIN-" },
539         { "Mono Input", NULL, "MONOIN+" },
540
541         { "Left", "Playback Switch", "DACL" },
542         { "Left", "Right Playback Switch", "DACR" },
543         { "Left", "Bypass Switch", "LINEINL" },
544         { "Left", "Mono Switch", "Mono Input" },
545
546         { "Right", "Playback Switch", "DACR" },
547         { "Right", "Left Playback Switch", "DACL" },
548         { "Right", "Bypass Switch", "LINEINR" },
549         { "Right", "Mono Switch", "Mono Input" },
550
551         { "Mono", "Left Playback Switch", "DACL" },
552         { "Mono", "Right Playback Switch", "DACR" },
553         { "Mono", "Left Bypass Switch", "LINEINL" },
554         { "Mono", "Right Bypass Switch", "LINEINR" },
555
556         { "LOUT1 PGA", NULL, "Left" },
557         { "LOUT1", NULL, "TSDEN" },
558         { "LOUT1", NULL, "LOUT1 PGA" },
559
560         { "ROUT1 PGA", NULL, "Right" },
561         { "ROUT1", NULL, "TSDEN" },
562         { "ROUT1", NULL, "ROUT1 PGA" },
563
564         { "LOUT2 PGA", NULL, "Left" },
565         { "LOUT2", NULL, "TSDEN" },
566         { "LOUT2", NULL, "LOUT2 PGA" },
567
568         { "ROUT2 PGA", NULL, "Right" },
569         { "ROUT2", NULL, "TSDEN" },
570         { "ROUT2", NULL, "ROUT2 PGA" },
571
572         { "MOUT PGA", NULL, "Mono" },
573         { "MONOOUT", NULL, "MOUT PGA" },
574
575         /* OUT3 not currently implemented */
576         { "OUT3", NULL, "OUT3 PGA" },
577 };
578
579 static int wm8955_add_widgets(struct snd_soc_codec *codec)
580 {
581         snd_soc_add_controls(codec, wm8955_snd_controls,
582                              ARRAY_SIZE(wm8955_snd_controls));
583
584         snd_soc_dapm_new_controls(codec, wm8955_dapm_widgets,
585                                   ARRAY_SIZE(wm8955_dapm_widgets));
586
587         snd_soc_dapm_add_routes(codec, wm8955_intercon,
588                                 ARRAY_SIZE(wm8955_intercon));
589
590         return 0;
591 }
592
593 static int wm8955_hw_params(struct snd_pcm_substream *substream,
594                             struct snd_pcm_hw_params *params,
595                             struct snd_soc_dai *dai)
596 {
597         struct snd_soc_codec *codec = dai->codec;
598         struct wm8955_priv *wm8955 = snd_soc_codec_get_drvdata(codec);
599         int ret;
600         int wl;
601
602         switch (params_format(params)) {
603         case SNDRV_PCM_FORMAT_S16_LE:
604                 wl = 0;
605                 break;
606         case SNDRV_PCM_FORMAT_S20_3LE:
607                 wl = 0x4;
608                 break;
609         case SNDRV_PCM_FORMAT_S24_LE:
610                 wl = 0x8;
611                 break;
612         case SNDRV_PCM_FORMAT_S32_LE:
613                 wl = 0xc;
614                 break;
615         default:
616                 return -EINVAL;
617         }
618         snd_soc_update_bits(codec, WM8955_AUDIO_INTERFACE,
619                             WM8955_WL_MASK, wl);
620
621         wm8955->fs = params_rate(params);
622         wm8955_set_deemph(codec);
623
624         /* If the chip is clocked then disable the clocks and force a
625          * reconfiguration, otherwise DAPM will power up the
626          * clocks for us later. */
627         ret = snd_soc_read(codec, WM8955_POWER_MANAGEMENT_1);
628         if (ret < 0)
629                 return ret;
630         if (ret & WM8955_DIGENB) {
631                 snd_soc_update_bits(codec, WM8955_POWER_MANAGEMENT_1,
632                                     WM8955_DIGENB, 0);
633                 snd_soc_update_bits(codec, WM8955_CLOCKING_PLL,
634                                     WM8955_PLL_RB | WM8955_PLLEN, 0);
635
636                 wm8955_configure_clocking(codec);
637         }
638
639         return 0;
640 }
641
642
643 static int wm8955_set_sysclk(struct snd_soc_dai *dai, int clk_id,
644                              unsigned int freq, int dir)
645 {
646         struct snd_soc_codec *codec = dai->codec;
647         struct wm8955_priv *priv = snd_soc_codec_get_drvdata(codec);
648         int div;
649
650         switch (clk_id) {
651         case WM8955_CLK_MCLK:
652                 if (freq > 15000000) {
653                         priv->mclk_rate = freq /= 2;
654                         div = WM8955_MCLKDIV2;
655                 } else {
656                         priv->mclk_rate = freq;
657                         div = 0;
658                 }
659
660                 snd_soc_update_bits(codec, WM8955_SAMPLE_RATE,
661                                     WM8955_MCLKDIV2, div);
662                 break;
663
664         default:
665                 return -EINVAL;
666         }
667
668         dev_dbg(dai->dev, "Clock source is %d at %uHz\n", clk_id, freq);
669
670         return 0;
671 }
672
673 static int wm8955_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
674 {
675         struct snd_soc_codec *codec = dai->codec;
676         u16 aif = 0;
677
678         switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
679         case SND_SOC_DAIFMT_CBS_CFS:
680                 break;
681         case SND_SOC_DAIFMT_CBM_CFM:
682                 aif |= WM8955_MS;
683                 break;
684         default:
685                 return -EINVAL;
686         }
687
688         switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
689         case SND_SOC_DAIFMT_DSP_B:
690                 aif |= WM8955_LRP;
691         case SND_SOC_DAIFMT_DSP_A:
692                 aif |= 0x3;
693                 break;
694         case SND_SOC_DAIFMT_I2S:
695                 aif |= 0x2;
696                 break;
697         case SND_SOC_DAIFMT_RIGHT_J:
698                 break;
699         case SND_SOC_DAIFMT_LEFT_J:
700                 aif |= 0x1;
701                 break;
702         default:
703                 return -EINVAL;
704         }
705
706         switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
707         case SND_SOC_DAIFMT_DSP_A:
708         case SND_SOC_DAIFMT_DSP_B:
709                 /* frame inversion not valid for DSP modes */
710                 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
711                 case SND_SOC_DAIFMT_NB_NF:
712                         break;
713                 case SND_SOC_DAIFMT_IB_NF:
714                         aif |= WM8955_BCLKINV;
715                         break;
716                 default:
717                         return -EINVAL;
718                 }
719                 break;
720
721         case SND_SOC_DAIFMT_I2S:
722         case SND_SOC_DAIFMT_RIGHT_J:
723         case SND_SOC_DAIFMT_LEFT_J:
724                 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
725                 case SND_SOC_DAIFMT_NB_NF:
726                         break;
727                 case SND_SOC_DAIFMT_IB_IF:
728                         aif |= WM8955_BCLKINV | WM8955_LRP;
729                         break;
730                 case SND_SOC_DAIFMT_IB_NF:
731                         aif |= WM8955_BCLKINV;
732                         break;
733                 case SND_SOC_DAIFMT_NB_IF:
734                         aif |= WM8955_LRP;
735                         break;
736                 default:
737                         return -EINVAL;
738                 }
739                 break;
740         default:
741                 return -EINVAL;
742         }
743
744         snd_soc_update_bits(codec, WM8955_AUDIO_INTERFACE,
745                             WM8955_MS | WM8955_FORMAT_MASK | WM8955_BCLKINV |
746                             WM8955_LRP, aif);
747
748         return 0;
749 }
750
751
752 static int wm8955_digital_mute(struct snd_soc_dai *codec_dai, int mute)
753 {
754         struct snd_soc_codec *codec = codec_dai->codec;
755         int val;
756
757         if (mute)
758                 val = WM8955_DACMU;
759         else
760                 val = 0;
761
762         snd_soc_update_bits(codec, WM8955_DAC_CONTROL, WM8955_DACMU, val);
763
764         return 0;
765 }
766
767 static int wm8955_set_bias_level(struct snd_soc_codec *codec,
768                                  enum snd_soc_bias_level level)
769 {
770         struct wm8955_priv *wm8955 = snd_soc_codec_get_drvdata(codec);
771         int ret, i;
772
773         switch (level) {
774         case SND_SOC_BIAS_ON:
775                 break;
776
777         case SND_SOC_BIAS_PREPARE:
778                 /* VMID resistance 2*50k */
779                 snd_soc_update_bits(codec, WM8955_POWER_MANAGEMENT_1,
780                                     WM8955_VMIDSEL_MASK,
781                                     0x1 << WM8955_VMIDSEL_SHIFT);
782
783                 /* Default bias current */
784                 snd_soc_update_bits(codec, WM8955_ADDITIONAL_CONTROL_1,
785                                     WM8955_VSEL_MASK,
786                                     0x2 << WM8955_VSEL_SHIFT);
787                 break;
788
789         case SND_SOC_BIAS_STANDBY:
790                 if (codec->bias_level == SND_SOC_BIAS_OFF) {
791                         ret = regulator_bulk_enable(ARRAY_SIZE(wm8955->supplies),
792                                                     wm8955->supplies);
793                         if (ret != 0) {
794                                 dev_err(codec->dev,
795                                         "Failed to enable supplies: %d\n",
796                                         ret);
797                                 return ret;
798                         }
799
800                         /* Sync back cached values if they're
801                          * different from the hardware default.
802                          */
803                         for (i = 0; i < ARRAY_SIZE(wm8955->reg_cache); i++) {
804                                 if (i == WM8955_RESET)
805                                         continue;
806
807                                 if (wm8955->reg_cache[i] == wm8955_reg[i])
808                                         continue;
809
810                                 snd_soc_write(codec, i, wm8955->reg_cache[i]);
811                         }
812
813                         /* Enable VREF and VMID */
814                         snd_soc_update_bits(codec, WM8955_POWER_MANAGEMENT_1,
815                                             WM8955_VREF |
816                                             WM8955_VMIDSEL_MASK,
817                                             WM8955_VREF |
818                                             0x3 << WM8955_VREF_SHIFT);
819
820                         /* Let VMID ramp */
821                         msleep(500);
822
823                         /* High resistance VROI to maintain outputs */
824                         snd_soc_update_bits(codec,
825                                             WM8955_ADDITIONAL_CONTROL_3,
826                                             WM8955_VROI, WM8955_VROI);
827                 }
828
829                 /* Maintain VMID with 2*250k */
830                 snd_soc_update_bits(codec, WM8955_POWER_MANAGEMENT_1,
831                                     WM8955_VMIDSEL_MASK,
832                                     0x2 << WM8955_VMIDSEL_SHIFT);
833
834                 /* Minimum bias current */
835                 snd_soc_update_bits(codec, WM8955_ADDITIONAL_CONTROL_1,
836                                     WM8955_VSEL_MASK, 0);
837                 break;
838
839         case SND_SOC_BIAS_OFF:
840                 /* Low resistance VROI to help discharge */
841                 snd_soc_update_bits(codec,
842                                     WM8955_ADDITIONAL_CONTROL_3,
843                                     WM8955_VROI, 0);
844
845                 /* Turn off VMID and VREF */
846                 snd_soc_update_bits(codec, WM8955_POWER_MANAGEMENT_1,
847                                     WM8955_VREF |
848                                     WM8955_VMIDSEL_MASK, 0);
849
850                 regulator_bulk_disable(ARRAY_SIZE(wm8955->supplies),
851                                        wm8955->supplies);
852                 break;
853         }
854         codec->bias_level = level;
855         return 0;
856 }
857
858 #define WM8955_RATES SNDRV_PCM_RATE_8000_96000
859
860 #define WM8955_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
861                         SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
862
863 static struct snd_soc_dai_ops wm8955_dai_ops = {
864         .set_sysclk = wm8955_set_sysclk,
865         .set_fmt = wm8955_set_fmt,
866         .hw_params = wm8955_hw_params,
867         .digital_mute = wm8955_digital_mute,
868 };
869
870 static struct snd_soc_dai_driver wm8955_dai = {
871         .name = "wm8955-hifi",
872         .playback = {
873                 .stream_name = "Playback",
874                 .channels_min = 2,
875                 .channels_max = 2,
876                 .rates = WM8955_RATES,
877                 .formats = WM8955_FORMATS,
878         },
879         .ops = &wm8955_dai_ops,
880 };
881
882 #ifdef CONFIG_PM
883 static int wm8955_suspend(struct snd_soc_codec *codec, pm_message_t state)
884 {
885         wm8955_set_bias_level(codec, SND_SOC_BIAS_OFF);
886
887         return 0;
888 }
889
890 static int wm8955_resume(struct snd_soc_codec *codec)
891 {
892         wm8955_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
893
894         return 0;
895 }
896 #else
897 #define wm8955_suspend NULL
898 #define wm8955_resume NULL
899 #endif
900
901 static int wm8955_probe(struct snd_soc_codec *codec)
902 {
903         struct wm8955_priv *wm8955 = snd_soc_codec_get_drvdata(codec);
904         struct wm8955_pdata *pdata = dev_get_platdata(codec->dev);
905         int ret, i;
906
907         codec->control_data = wm8955->control_data;
908         ret = snd_soc_codec_set_cache_io(codec, 7, 9, wm8955->control_type);
909         if (ret != 0) {
910                 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
911                 return ret;
912         }
913
914         for (i = 0; i < ARRAY_SIZE(wm8955->supplies); i++)
915                 wm8955->supplies[i].supply = wm8955_supply_names[i];
916
917         ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(wm8955->supplies),
918                                  wm8955->supplies);
919         if (ret != 0) {
920                 dev_err(codec->dev, "Failed to request supplies: %d\n", ret);
921                 return ret;
922         }
923
924         ret = regulator_bulk_enable(ARRAY_SIZE(wm8955->supplies),
925                                     wm8955->supplies);
926         if (ret != 0) {
927                 dev_err(codec->dev, "Failed to enable supplies: %d\n", ret);
928                 goto err_get;
929         }
930
931         ret = wm8955_reset(codec);
932         if (ret < 0) {
933                 dev_err(codec->dev, "Failed to issue reset: %d\n", ret);
934                 goto err_enable;
935         }
936
937         /* Change some default settings - latch VU and enable ZC */
938         wm8955->reg_cache[WM8955_LEFT_DAC_VOLUME] |= WM8955_LDVU;
939         wm8955->reg_cache[WM8955_RIGHT_DAC_VOLUME] |= WM8955_RDVU;
940         wm8955->reg_cache[WM8955_LOUT1_VOLUME] |= WM8955_LO1VU | WM8955_LO1ZC;
941         wm8955->reg_cache[WM8955_ROUT1_VOLUME] |= WM8955_RO1VU | WM8955_RO1ZC;
942         wm8955->reg_cache[WM8955_LOUT2_VOLUME] |= WM8955_LO2VU | WM8955_LO2ZC;
943         wm8955->reg_cache[WM8955_ROUT2_VOLUME] |= WM8955_RO2VU | WM8955_RO2ZC;
944         wm8955->reg_cache[WM8955_MONOOUT_VOLUME] |= WM8955_MOZC;
945
946         /* Also enable adaptive bass boost by default */
947         wm8955->reg_cache[WM8955_BASS_CONTROL] |= WM8955_BB;
948
949         /* Set platform data values */
950         if (pdata) {
951                 if (pdata->out2_speaker)
952                         wm8955->reg_cache[WM8955_ADDITIONAL_CONTROL_2]
953                                 |= WM8955_ROUT2INV;
954
955                 if (pdata->monoin_diff)
956                         wm8955->reg_cache[WM8955_MONO_OUT_MIX_1]
957                                 |= WM8955_DMEN;
958         }
959
960         wm8955_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
961
962         /* Bias level configuration will have done an extra enable */
963         regulator_bulk_disable(ARRAY_SIZE(wm8955->supplies), wm8955->supplies);
964
965         wm8955_add_widgets(codec);
966         return 0;
967
968 err_enable:
969         regulator_bulk_disable(ARRAY_SIZE(wm8955->supplies), wm8955->supplies);
970 err_get:
971         regulator_bulk_free(ARRAY_SIZE(wm8955->supplies), wm8955->supplies);
972         return ret;
973 }
974
975 static int wm8955_remove(struct snd_soc_codec *codec)
976 {
977         struct wm8955_priv *wm8955 = snd_soc_codec_get_drvdata(codec);
978
979         wm8955_set_bias_level(codec, SND_SOC_BIAS_OFF);
980         regulator_bulk_free(ARRAY_SIZE(wm8955->supplies), wm8955->supplies);
981         return 0;
982 }
983
984 static struct snd_soc_codec_driver soc_codec_dev_wm8955 = {
985         .probe =        wm8955_probe,
986         .remove =       wm8955_remove,
987         .suspend =      wm8955_suspend,
988         .resume =       wm8955_resume,
989         .set_bias_level = wm8955_set_bias_level,
990         .reg_cache_size = ARRAY_SIZE(wm8955_reg),
991         .reg_word_size = sizeof(u16),
992         .reg_cache_default = wm8955_reg,
993 };
994
995 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
996 static __devinit int wm8955_i2c_probe(struct i2c_client *i2c,
997                                       const struct i2c_device_id *id)
998 {
999         struct wm8955_priv *wm8955;
1000         int ret;
1001
1002         wm8955 = kzalloc(sizeof(struct wm8955_priv), GFP_KERNEL);
1003         if (wm8955 == NULL)
1004                 return -ENOMEM;
1005
1006         i2c_set_clientdata(i2c, wm8955);
1007         wm8955->control_data = i2c;
1008
1009         ret = snd_soc_register_codec(&i2c->dev,
1010                         &soc_codec_dev_wm8955, &wm8955_dai, 1);
1011         if (ret < 0)
1012                 kfree(wm8955);
1013         return ret;
1014 }
1015
1016 static __devexit int wm8955_i2c_remove(struct i2c_client *client)
1017 {
1018         snd_soc_unregister_codec(&client->dev);
1019         kfree(i2c_get_clientdata(client));
1020         return 0;
1021 }
1022
1023 static const struct i2c_device_id wm8955_i2c_id[] = {
1024         { "wm8955", 0 },
1025         { }
1026 };
1027 MODULE_DEVICE_TABLE(i2c, wm8955_i2c_id);
1028
1029 static struct i2c_driver wm8955_i2c_driver = {
1030         .driver = {
1031                 .name = "wm8955-codec",
1032                 .owner = THIS_MODULE,
1033         },
1034         .probe =    wm8955_i2c_probe,
1035         .remove =   __devexit_p(wm8955_i2c_remove),
1036         .id_table = wm8955_i2c_id,
1037 };
1038 #endif
1039
1040 static int __init wm8955_modinit(void)
1041 {
1042         int ret = 0;
1043 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
1044         ret = i2c_add_driver(&wm8955_i2c_driver);
1045         if (ret != 0) {
1046                 printk(KERN_ERR "Failed to register WM8955 I2C driver: %d\n",
1047                        ret);
1048         }
1049 #endif
1050         return ret;
1051 }
1052 module_init(wm8955_modinit);
1053
1054 static void __exit wm8955_exit(void)
1055 {
1056 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
1057         i2c_del_driver(&wm8955_i2c_driver);
1058 #endif
1059 }
1060 module_exit(wm8955_exit);
1061
1062 MODULE_DESCRIPTION("ASoC WM8955 driver");
1063 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
1064 MODULE_LICENSE("GPL");