blob: 11fb2dd40c5c0c0b37d00d48fb1ad14dbe100337 [file] [log] [blame]
Mark Brownb35a28a2009-12-18 12:00:22 +00001/*
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>
Mark Brown95860fd2011-12-29 21:42:36 +000019#include <linux/regmap.h>
Mark Brownb35a28a2009-12-18 12:00:22 +000020#include <linux/regulator/consumer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Mark Brownb35a28a2009-12-18 12:00:22 +000022#include <sound/core.h>
23#include <sound/pcm.h>
24#include <sound/pcm_params.h>
25#include <sound/soc.h>
Mark Brownb35a28a2009-12-18 12:00:22 +000026#include <sound/initval.h>
27#include <sound/tlv.h>
28#include <sound/wm8955.h>
29
30#include "wm8955.h"
31
Mark Brownb35a28a2009-12-18 12:00:22 +000032#define WM8955_NUM_SUPPLIES 4
33static const char *wm8955_supply_names[WM8955_NUM_SUPPLIES] = {
34 "DCVDD",
35 "DBVDD",
36 "HPVDD",
37 "AVDD",
38};
39
40/* codec private data */
41struct wm8955_priv {
Mark Brown95860fd2011-12-29 21:42:36 +000042 struct regmap *regmap;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000043
Mark Brownb35a28a2009-12-18 12:00:22 +000044 unsigned int mclk_rate;
45
46 int deemph;
47 int fs;
48
49 struct regulator_bulk_data supplies[WM8955_NUM_SUPPLIES];
Mark Brownb35a28a2009-12-18 12:00:22 +000050};
51
Mark Brown95860fd2011-12-29 21:42:36 +000052static const struct reg_default wm8955_reg_defaults[] = {
53 { 2, 0x0079 }, /* R2 - LOUT1 volume */
54 { 3, 0x0079 }, /* R3 - ROUT1 volume */
55 { 5, 0x0008 }, /* R5 - DAC Control */
56 { 7, 0x000A }, /* R7 - Audio Interface */
57 { 8, 0x0000 }, /* R8 - Sample Rate */
58 { 10, 0x00FF }, /* R10 - Left DAC volume */
59 { 11, 0x00FF }, /* R11 - Right DAC volume */
60 { 12, 0x000F }, /* R12 - Bass control */
61 { 13, 0x000F }, /* R13 - Treble control */
62 { 23, 0x00C1 }, /* R23 - Additional control (1) */
63 { 24, 0x0000 }, /* R24 - Additional control (2) */
64 { 25, 0x0000 }, /* R25 - Power Management (1) */
65 { 26, 0x0000 }, /* R26 - Power Management (2) */
66 { 27, 0x0000 }, /* R27 - Additional Control (3) */
67 { 34, 0x0050 }, /* R34 - Left out Mix (1) */
68 { 35, 0x0050 }, /* R35 - Left out Mix (2) */
69 { 36, 0x0050 }, /* R36 - Right out Mix (1) */
70 { 37, 0x0050 }, /* R37 - Right Out Mix (2) */
71 { 38, 0x0050 }, /* R38 - Mono out Mix (1) */
72 { 39, 0x0050 }, /* R39 - Mono out Mix (2) */
73 { 40, 0x0079 }, /* R40 - LOUT2 volume */
74 { 41, 0x0079 }, /* R41 - ROUT2 volume */
75 { 42, 0x0079 }, /* R42 - MONOOUT volume */
76 { 43, 0x0000 }, /* R43 - Clocking / PLL */
77 { 44, 0x0103 }, /* R44 - PLL Control 1 */
78 { 45, 0x0024 }, /* R45 - PLL Control 2 */
79 { 46, 0x01BA }, /* R46 - PLL Control 3 */
80 { 59, 0x0000 }, /* R59 - PLL Control 4 */
Mark Brownb35a28a2009-12-18 12:00:22 +000081};
82
Mark Brown95860fd2011-12-29 21:42:36 +000083static bool wm8955_writeable(struct device *dev, unsigned int reg)
84{
85 switch (reg) {
86 case WM8955_LOUT1_VOLUME:
87 case WM8955_ROUT1_VOLUME:
88 case WM8955_DAC_CONTROL:
89 case WM8955_AUDIO_INTERFACE:
90 case WM8955_SAMPLE_RATE:
91 case WM8955_LEFT_DAC_VOLUME:
92 case WM8955_RIGHT_DAC_VOLUME:
93 case WM8955_BASS_CONTROL:
94 case WM8955_TREBLE_CONTROL:
95 case WM8955_RESET:
96 case WM8955_ADDITIONAL_CONTROL_1:
97 case WM8955_ADDITIONAL_CONTROL_2:
98 case WM8955_POWER_MANAGEMENT_1:
99 case WM8955_POWER_MANAGEMENT_2:
100 case WM8955_ADDITIONAL_CONTROL_3:
101 case WM8955_LEFT_OUT_MIX_1:
102 case WM8955_LEFT_OUT_MIX_2:
103 case WM8955_RIGHT_OUT_MIX_1:
104 case WM8955_RIGHT_OUT_MIX_2:
105 case WM8955_MONO_OUT_MIX_1:
106 case WM8955_MONO_OUT_MIX_2:
107 case WM8955_LOUT2_VOLUME:
108 case WM8955_ROUT2_VOLUME:
109 case WM8955_MONOOUT_VOLUME:
110 case WM8955_CLOCKING_PLL:
111 case WM8955_PLL_CONTROL_1:
112 case WM8955_PLL_CONTROL_2:
113 case WM8955_PLL_CONTROL_3:
114 case WM8955_PLL_CONTROL_4:
115 return true;
116 default:
117 return false;
118 }
119}
120
121static bool wm8955_volatile(struct device *dev, unsigned int reg)
122{
123 switch (reg) {
124 case WM8955_RESET:
125 return true;
126 default:
127 return false;
128 }
129}
130
Mark Brownb35a28a2009-12-18 12:00:22 +0000131static int wm8955_reset(struct snd_soc_codec *codec)
132{
133 return snd_soc_write(codec, WM8955_RESET, 0);
134}
135
136struct pll_factors {
137 int n;
138 int k;
139 int outdiv;
140};
141
142/* The size in bits of the FLL divide multiplied by 10
143 * to allow rounding later */
144#define FIXED_FLL_SIZE ((1 << 22) * 10)
145
146static int wm8995_pll_factors(struct device *dev,
147 int Fref, int Fout, struct pll_factors *pll)
148{
149 u64 Kpart;
150 unsigned int K, Ndiv, Nmod, target;
151
152 dev_dbg(dev, "Fref=%u Fout=%u\n", Fref, Fout);
153
154 /* The oscilator should run at should be 90-100MHz, and
155 * there's a divide by 4 plus an optional divide by 2 in the
156 * output path to generate the system clock. The clock table
157 * is sortd so we should always generate a suitable target. */
158 target = Fout * 4;
159 if (target < 90000000) {
160 pll->outdiv = 1;
161 target *= 2;
162 } else {
163 pll->outdiv = 0;
164 }
165
166 WARN_ON(target < 90000000 || target > 100000000);
167
168 dev_dbg(dev, "Fvco=%dHz\n", target);
169
170 /* Now, calculate N.K */
171 Ndiv = target / Fref;
172
173 pll->n = Ndiv;
174 Nmod = target % Fref;
175 dev_dbg(dev, "Nmod=%d\n", Nmod);
176
177 /* Calculate fractional part - scale up so we can round. */
178 Kpart = FIXED_FLL_SIZE * (long long)Nmod;
179
180 do_div(Kpart, Fref);
181
182 K = Kpart & 0xFFFFFFFF;
183
184 if ((K % 10) >= 5)
185 K += 5;
186
187 /* Move down to proper range now rounding is done */
188 pll->k = K / 10;
189
190 dev_dbg(dev, "N=%x K=%x OUTDIV=%x\n", pll->n, pll->k, pll->outdiv);
191
192 return 0;
193}
194
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300195/* Lookup table specifying SRATE (table 25 in datasheet); some of the
Mark Brownb35a28a2009-12-18 12:00:22 +0000196 * output frequencies have been rounded to the standard frequencies
197 * they are intended to match where the error is slight. */
198static struct {
199 int mclk;
200 int fs;
201 int usb;
202 int sr;
203} clock_cfgs[] = {
204 { 18432000, 8000, 0, 3, },
205 { 18432000, 12000, 0, 9, },
206 { 18432000, 16000, 0, 11, },
207 { 18432000, 24000, 0, 29, },
208 { 18432000, 32000, 0, 13, },
209 { 18432000, 48000, 0, 1, },
210 { 18432000, 96000, 0, 15, },
211
212 { 16934400, 8018, 0, 19, },
213 { 16934400, 11025, 0, 25, },
214 { 16934400, 22050, 0, 27, },
215 { 16934400, 44100, 0, 17, },
216 { 16934400, 88200, 0, 31, },
217
218 { 12000000, 8000, 1, 2, },
219 { 12000000, 11025, 1, 25, },
220 { 12000000, 12000, 1, 8, },
221 { 12000000, 16000, 1, 10, },
222 { 12000000, 22050, 1, 27, },
223 { 12000000, 24000, 1, 28, },
224 { 12000000, 32000, 1, 12, },
225 { 12000000, 44100, 1, 17, },
226 { 12000000, 48000, 1, 0, },
227 { 12000000, 88200, 1, 31, },
228 { 12000000, 96000, 1, 14, },
229
230 { 12288000, 8000, 0, 2, },
231 { 12288000, 12000, 0, 8, },
232 { 12288000, 16000, 0, 10, },
233 { 12288000, 24000, 0, 28, },
234 { 12288000, 32000, 0, 12, },
235 { 12288000, 48000, 0, 0, },
236 { 12288000, 96000, 0, 14, },
237
238 { 12289600, 8018, 0, 18, },
239 { 12289600, 11025, 0, 24, },
240 { 12289600, 22050, 0, 26, },
241 { 11289600, 44100, 0, 16, },
242 { 11289600, 88200, 0, 31, },
243};
244
245static int wm8955_configure_clocking(struct snd_soc_codec *codec)
246{
Mark Brownb2c812e2010-04-14 15:35:19 +0900247 struct wm8955_priv *wm8955 = snd_soc_codec_get_drvdata(codec);
Mark Brownb35a28a2009-12-18 12:00:22 +0000248 int i, ret, val;
249 int clocking = 0;
250 int srate = 0;
251 int sr = -1;
252 struct pll_factors pll;
253
254 /* If we're not running a sample rate currently just pick one */
255 if (wm8955->fs == 0)
256 wm8955->fs = 8000;
257
258 /* Can we generate an exact output? */
259 for (i = 0; i < ARRAY_SIZE(clock_cfgs); i++) {
260 if (wm8955->fs != clock_cfgs[i].fs)
261 continue;
262 sr = i;
263
264 if (wm8955->mclk_rate == clock_cfgs[i].mclk)
265 break;
266 }
267
268 /* We should never get here with an unsupported sample rate */
269 if (sr == -1) {
270 dev_err(codec->dev, "Sample rate %dHz unsupported\n",
271 wm8955->fs);
272 WARN_ON(sr == -1);
273 return -EINVAL;
274 }
275
276 if (i == ARRAY_SIZE(clock_cfgs)) {
277 /* If we can't generate the right clock from MCLK then
278 * we should configure the PLL to supply us with an
279 * appropriate clock.
280 */
281 clocking |= WM8955_MCLKSEL;
282
283 /* Use the last divider configuration we saw for the
284 * sample rate. */
285 ret = wm8995_pll_factors(codec->dev, wm8955->mclk_rate,
286 clock_cfgs[sr].mclk, &pll);
287 if (ret != 0) {
288 dev_err(codec->dev,
289 "Unable to generate %dHz from %dHz MCLK\n",
290 wm8955->fs, wm8955->mclk_rate);
291 return -EINVAL;
292 }
293
294 snd_soc_update_bits(codec, WM8955_PLL_CONTROL_1,
295 WM8955_N_MASK | WM8955_K_21_18_MASK,
296 (pll.n << WM8955_N_SHIFT) |
297 pll.k >> 18);
298 snd_soc_update_bits(codec, WM8955_PLL_CONTROL_2,
299 WM8955_K_17_9_MASK,
300 (pll.k >> 9) & WM8955_K_17_9_MASK);
301 snd_soc_update_bits(codec, WM8955_PLL_CONTROL_2,
302 WM8955_K_8_0_MASK,
303 pll.k & WM8955_K_8_0_MASK);
304 if (pll.k)
305 snd_soc_update_bits(codec, WM8955_PLL_CONTROL_4,
306 WM8955_KEN, WM8955_KEN);
307 else
308 snd_soc_update_bits(codec, WM8955_PLL_CONTROL_4,
309 WM8955_KEN, 0);
310
311 if (pll.outdiv)
312 val = WM8955_PLL_RB | WM8955_PLLOUTDIV2;
313 else
314 val = WM8955_PLL_RB;
315
316 /* Now start the PLL running */
317 snd_soc_update_bits(codec, WM8955_CLOCKING_PLL,
318 WM8955_PLL_RB | WM8955_PLLOUTDIV2, val);
319 snd_soc_update_bits(codec, WM8955_CLOCKING_PLL,
320 WM8955_PLLEN, WM8955_PLLEN);
321 }
322
323 srate = clock_cfgs[sr].usb | (clock_cfgs[sr].sr << WM8955_SR_SHIFT);
324
325 snd_soc_update_bits(codec, WM8955_SAMPLE_RATE,
326 WM8955_USB | WM8955_SR_MASK, srate);
327 snd_soc_update_bits(codec, WM8955_CLOCKING_PLL,
328 WM8955_MCLKSEL, clocking);
329
330 return 0;
331}
332
333static int wm8955_sysclk(struct snd_soc_dapm_widget *w,
334 struct snd_kcontrol *kcontrol, int event)
335{
336 struct snd_soc_codec *codec = w->codec;
337 int ret = 0;
338
339 /* Always disable the clocks - if we're doing reconfiguration this
340 * avoids misclocking.
341 */
342 snd_soc_update_bits(codec, WM8955_POWER_MANAGEMENT_1,
343 WM8955_DIGENB, 0);
344 snd_soc_update_bits(codec, WM8955_CLOCKING_PLL,
345 WM8955_PLL_RB | WM8955_PLLEN, 0);
346
347 switch (event) {
348 case SND_SOC_DAPM_POST_PMD:
349 break;
350 case SND_SOC_DAPM_PRE_PMU:
351 ret = wm8955_configure_clocking(codec);
352 break;
353 default:
354 ret = -EINVAL;
355 break;
356 }
357
358 return ret;
359}
360
361static int deemph_settings[] = { 0, 32000, 44100, 48000 };
362
363static int wm8955_set_deemph(struct snd_soc_codec *codec)
364{
Mark Brownb2c812e2010-04-14 15:35:19 +0900365 struct wm8955_priv *wm8955 = snd_soc_codec_get_drvdata(codec);
Mark Brownb35a28a2009-12-18 12:00:22 +0000366 int val, i, best;
367
368 /* If we're using deemphasis select the nearest available sample
369 * rate.
370 */
371 if (wm8955->deemph) {
372 best = 1;
373 for (i = 2; i < ARRAY_SIZE(deemph_settings); i++) {
374 if (abs(deemph_settings[i] - wm8955->fs) <
375 abs(deemph_settings[best] - wm8955->fs))
376 best = i;
377 }
378
379 val = best << WM8955_DEEMPH_SHIFT;
380 } else {
381 val = 0;
382 }
383
384 dev_dbg(codec->dev, "Set deemphasis %d\n", val);
385
386 return snd_soc_update_bits(codec, WM8955_DAC_CONTROL,
387 WM8955_DEEMPH_MASK, val);
388}
389
390static int wm8955_get_deemph(struct snd_kcontrol *kcontrol,
391 struct snd_ctl_elem_value *ucontrol)
392{
393 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Mark Brownb2c812e2010-04-14 15:35:19 +0900394 struct wm8955_priv *wm8955 = snd_soc_codec_get_drvdata(codec);
Mark Brownb35a28a2009-12-18 12:00:22 +0000395
Dmitry Artamonow3f343f82010-12-08 23:36:17 +0300396 ucontrol->value.enumerated.item[0] = wm8955->deemph;
397 return 0;
Mark Brownb35a28a2009-12-18 12:00:22 +0000398}
399
400static int wm8955_put_deemph(struct snd_kcontrol *kcontrol,
401 struct snd_ctl_elem_value *ucontrol)
402{
403 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Mark Brownb2c812e2010-04-14 15:35:19 +0900404 struct wm8955_priv *wm8955 = snd_soc_codec_get_drvdata(codec);
Mark Brownb35a28a2009-12-18 12:00:22 +0000405 int deemph = ucontrol->value.enumerated.item[0];
406
407 if (deemph > 1)
408 return -EINVAL;
409
410 wm8955->deemph = deemph;
411
412 return wm8955_set_deemph(codec);
413}
414
415static const char *bass_mode_text[] = {
416 "Linear", "Adaptive",
417};
418
419static const struct soc_enum bass_mode =
420 SOC_ENUM_SINGLE(WM8955_BASS_CONTROL, 7, 2, bass_mode_text);
421
422static const char *bass_cutoff_text[] = {
423 "Low", "High"
424};
425
426static const struct soc_enum bass_cutoff =
427 SOC_ENUM_SINGLE(WM8955_BASS_CONTROL, 6, 2, bass_cutoff_text);
428
429static const char *treble_cutoff_text[] = {
430 "High", "Low"
431};
432
433static const struct soc_enum treble_cutoff =
434 SOC_ENUM_SINGLE(WM8955_TREBLE_CONTROL, 6, 2, treble_cutoff_text);
435
436static const DECLARE_TLV_DB_SCALE(digital_tlv, -12750, 50, 1);
437static const DECLARE_TLV_DB_SCALE(atten_tlv, -600, 600, 0);
438static const DECLARE_TLV_DB_SCALE(bypass_tlv, -1500, 300, 0);
439static const DECLARE_TLV_DB_SCALE(mono_tlv, -2100, 300, 0);
440static const DECLARE_TLV_DB_SCALE(out_tlv, -12100, 100, 1);
441static const DECLARE_TLV_DB_SCALE(treble_tlv, -1200, 150, 1);
442
443static const struct snd_kcontrol_new wm8955_snd_controls[] = {
444SOC_DOUBLE_R_TLV("Digital Playback Volume", WM8955_LEFT_DAC_VOLUME,
445 WM8955_RIGHT_DAC_VOLUME, 0, 255, 0, digital_tlv),
446SOC_SINGLE_TLV("Playback Attenuation Volume", WM8955_DAC_CONTROL, 7, 1, 1,
447 atten_tlv),
448SOC_SINGLE_BOOL_EXT("DAC Deemphasis Switch", 0,
449 wm8955_get_deemph, wm8955_put_deemph),
450
451SOC_ENUM("Bass Mode", bass_mode),
452SOC_ENUM("Bass Cutoff", bass_cutoff),
453SOC_SINGLE("Bass Volume", WM8955_BASS_CONTROL, 0, 15, 1),
454
455SOC_ENUM("Treble Cutoff", treble_cutoff),
456SOC_SINGLE_TLV("Treble Volume", WM8955_TREBLE_CONTROL, 0, 14, 1, treble_tlv),
457
458SOC_SINGLE_TLV("Left Bypass Volume", WM8955_LEFT_OUT_MIX_1, 4, 7, 1,
459 bypass_tlv),
460SOC_SINGLE_TLV("Left Mono Volume", WM8955_LEFT_OUT_MIX_2, 4, 7, 1,
461 bypass_tlv),
462
463SOC_SINGLE_TLV("Right Mono Volume", WM8955_RIGHT_OUT_MIX_1, 4, 7, 1,
464 bypass_tlv),
465SOC_SINGLE_TLV("Right Bypass Volume", WM8955_RIGHT_OUT_MIX_2, 4, 7, 1,
466 bypass_tlv),
467
468/* Not a stereo pair so they line up with the DAPM switches */
469SOC_SINGLE_TLV("Mono Left Bypass Volume", WM8955_MONO_OUT_MIX_1, 4, 7, 1,
470 mono_tlv),
471SOC_SINGLE_TLV("Mono Right Bypass Volume", WM8955_MONO_OUT_MIX_2, 4, 7, 1,
472 mono_tlv),
473
474SOC_DOUBLE_R_TLV("Headphone Volume", WM8955_LOUT1_VOLUME,
475 WM8955_ROUT1_VOLUME, 0, 127, 0, out_tlv),
476SOC_DOUBLE_R("Headphone ZC Switch", WM8955_LOUT1_VOLUME,
477 WM8955_ROUT1_VOLUME, 7, 1, 0),
478
479SOC_DOUBLE_R_TLV("Speaker Volume", WM8955_LOUT2_VOLUME,
480 WM8955_ROUT2_VOLUME, 0, 127, 0, out_tlv),
481SOC_DOUBLE_R("Speaker ZC Switch", WM8955_LOUT2_VOLUME,
482 WM8955_ROUT2_VOLUME, 7, 1, 0),
483
484SOC_SINGLE_TLV("Mono Volume", WM8955_MONOOUT_VOLUME, 0, 127, 0, out_tlv),
485SOC_SINGLE("Mono ZC Switch", WM8955_MONOOUT_VOLUME, 7, 1, 0),
486};
487
488static const struct snd_kcontrol_new lmixer[] = {
489SOC_DAPM_SINGLE("Playback Switch", WM8955_LEFT_OUT_MIX_1, 8, 1, 0),
490SOC_DAPM_SINGLE("Bypass Switch", WM8955_LEFT_OUT_MIX_1, 7, 1, 0),
491SOC_DAPM_SINGLE("Right Playback Switch", WM8955_LEFT_OUT_MIX_2, 8, 1, 0),
492SOC_DAPM_SINGLE("Mono Switch", WM8955_LEFT_OUT_MIX_2, 7, 1, 0),
493};
494
495static const struct snd_kcontrol_new rmixer[] = {
496SOC_DAPM_SINGLE("Left Playback Switch", WM8955_RIGHT_OUT_MIX_1, 8, 1, 0),
497SOC_DAPM_SINGLE("Mono Switch", WM8955_RIGHT_OUT_MIX_1, 7, 1, 0),
498SOC_DAPM_SINGLE("Playback Switch", WM8955_RIGHT_OUT_MIX_2, 8, 1, 0),
499SOC_DAPM_SINGLE("Bypass Switch", WM8955_RIGHT_OUT_MIX_2, 7, 1, 0),
500};
501
502static const struct snd_kcontrol_new mmixer[] = {
503SOC_DAPM_SINGLE("Left Playback Switch", WM8955_MONO_OUT_MIX_1, 8, 1, 0),
504SOC_DAPM_SINGLE("Left Bypass Switch", WM8955_MONO_OUT_MIX_1, 7, 1, 0),
505SOC_DAPM_SINGLE("Right Playback Switch", WM8955_MONO_OUT_MIX_2, 8, 1, 0),
506SOC_DAPM_SINGLE("Right Bypass Switch", WM8955_MONO_OUT_MIX_2, 7, 1, 0),
507};
508
509static const struct snd_soc_dapm_widget wm8955_dapm_widgets[] = {
510SND_SOC_DAPM_INPUT("MONOIN-"),
511SND_SOC_DAPM_INPUT("MONOIN+"),
512SND_SOC_DAPM_INPUT("LINEINR"),
513SND_SOC_DAPM_INPUT("LINEINL"),
514
515SND_SOC_DAPM_PGA("Mono Input", SND_SOC_NOPM, 0, 0, NULL, 0),
516
517SND_SOC_DAPM_SUPPLY("SYSCLK", WM8955_POWER_MANAGEMENT_1, 0, 1, wm8955_sysclk,
518 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
519SND_SOC_DAPM_SUPPLY("TSDEN", WM8955_ADDITIONAL_CONTROL_1, 8, 0, NULL, 0),
520
521SND_SOC_DAPM_DAC("DACL", "Playback", WM8955_POWER_MANAGEMENT_2, 8, 0),
522SND_SOC_DAPM_DAC("DACR", "Playback", WM8955_POWER_MANAGEMENT_2, 7, 0),
523
524SND_SOC_DAPM_PGA("LOUT1 PGA", WM8955_POWER_MANAGEMENT_2, 6, 0, NULL, 0),
525SND_SOC_DAPM_PGA("ROUT1 PGA", WM8955_POWER_MANAGEMENT_2, 5, 0, NULL, 0),
526SND_SOC_DAPM_PGA("LOUT2 PGA", WM8955_POWER_MANAGEMENT_2, 4, 0, NULL, 0),
527SND_SOC_DAPM_PGA("ROUT2 PGA", WM8955_POWER_MANAGEMENT_2, 3, 0, NULL, 0),
528SND_SOC_DAPM_PGA("MOUT PGA", WM8955_POWER_MANAGEMENT_2, 2, 0, NULL, 0),
529SND_SOC_DAPM_PGA("OUT3 PGA", WM8955_POWER_MANAGEMENT_2, 1, 0, NULL, 0),
530
531/* The names are chosen to make the control names nice */
532SND_SOC_DAPM_MIXER("Left", SND_SOC_NOPM, 0, 0,
533 lmixer, ARRAY_SIZE(lmixer)),
534SND_SOC_DAPM_MIXER("Right", SND_SOC_NOPM, 0, 0,
535 rmixer, ARRAY_SIZE(rmixer)),
536SND_SOC_DAPM_MIXER("Mono", SND_SOC_NOPM, 0, 0,
537 mmixer, ARRAY_SIZE(mmixer)),
538
539SND_SOC_DAPM_OUTPUT("LOUT1"),
540SND_SOC_DAPM_OUTPUT("ROUT1"),
541SND_SOC_DAPM_OUTPUT("LOUT2"),
542SND_SOC_DAPM_OUTPUT("ROUT2"),
543SND_SOC_DAPM_OUTPUT("MONOOUT"),
544SND_SOC_DAPM_OUTPUT("OUT3"),
545};
546
547static const struct snd_soc_dapm_route wm8955_intercon[] = {
548 { "DACL", NULL, "SYSCLK" },
549 { "DACR", NULL, "SYSCLK" },
550
551 { "Mono Input", NULL, "MONOIN-" },
552 { "Mono Input", NULL, "MONOIN+" },
553
554 { "Left", "Playback Switch", "DACL" },
555 { "Left", "Right Playback Switch", "DACR" },
556 { "Left", "Bypass Switch", "LINEINL" },
557 { "Left", "Mono Switch", "Mono Input" },
558
559 { "Right", "Playback Switch", "DACR" },
560 { "Right", "Left Playback Switch", "DACL" },
561 { "Right", "Bypass Switch", "LINEINR" },
562 { "Right", "Mono Switch", "Mono Input" },
563
564 { "Mono", "Left Playback Switch", "DACL" },
565 { "Mono", "Right Playback Switch", "DACR" },
566 { "Mono", "Left Bypass Switch", "LINEINL" },
567 { "Mono", "Right Bypass Switch", "LINEINR" },
568
569 { "LOUT1 PGA", NULL, "Left" },
570 { "LOUT1", NULL, "TSDEN" },
571 { "LOUT1", NULL, "LOUT1 PGA" },
572
573 { "ROUT1 PGA", NULL, "Right" },
574 { "ROUT1", NULL, "TSDEN" },
575 { "ROUT1", NULL, "ROUT1 PGA" },
576
577 { "LOUT2 PGA", NULL, "Left" },
578 { "LOUT2", NULL, "TSDEN" },
579 { "LOUT2", NULL, "LOUT2 PGA" },
580
581 { "ROUT2 PGA", NULL, "Right" },
582 { "ROUT2", NULL, "TSDEN" },
583 { "ROUT2", NULL, "ROUT2 PGA" },
584
585 { "MOUT PGA", NULL, "Mono" },
586 { "MONOOUT", NULL, "MOUT PGA" },
587
588 /* OUT3 not currently implemented */
589 { "OUT3", NULL, "OUT3 PGA" },
590};
591
592static int wm8955_add_widgets(struct snd_soc_codec *codec)
593{
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200594 struct snd_soc_dapm_context *dapm = &codec->dapm;
595
Mark Brownb35a28a2009-12-18 12:00:22 +0000596 snd_soc_add_controls(codec, wm8955_snd_controls,
597 ARRAY_SIZE(wm8955_snd_controls));
598
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200599 snd_soc_dapm_new_controls(dapm, wm8955_dapm_widgets,
Mark Brownb35a28a2009-12-18 12:00:22 +0000600 ARRAY_SIZE(wm8955_dapm_widgets));
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200601 snd_soc_dapm_add_routes(dapm, wm8955_intercon,
Mark Brownb35a28a2009-12-18 12:00:22 +0000602 ARRAY_SIZE(wm8955_intercon));
603
604 return 0;
605}
606
607static int wm8955_hw_params(struct snd_pcm_substream *substream,
608 struct snd_pcm_hw_params *params,
609 struct snd_soc_dai *dai)
610{
611 struct snd_soc_codec *codec = dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900612 struct wm8955_priv *wm8955 = snd_soc_codec_get_drvdata(codec);
Mark Brownb35a28a2009-12-18 12:00:22 +0000613 int ret;
614 int wl;
615
616 switch (params_format(params)) {
617 case SNDRV_PCM_FORMAT_S16_LE:
618 wl = 0;
619 break;
620 case SNDRV_PCM_FORMAT_S20_3LE:
621 wl = 0x4;
622 break;
623 case SNDRV_PCM_FORMAT_S24_LE:
624 wl = 0x8;
625 break;
626 case SNDRV_PCM_FORMAT_S32_LE:
627 wl = 0xc;
628 break;
629 default:
630 return -EINVAL;
631 }
632 snd_soc_update_bits(codec, WM8955_AUDIO_INTERFACE,
633 WM8955_WL_MASK, wl);
634
635 wm8955->fs = params_rate(params);
636 wm8955_set_deemph(codec);
637
638 /* If the chip is clocked then disable the clocks and force a
639 * reconfiguration, otherwise DAPM will power up the
640 * clocks for us later. */
641 ret = snd_soc_read(codec, WM8955_POWER_MANAGEMENT_1);
642 if (ret < 0)
643 return ret;
644 if (ret & WM8955_DIGENB) {
645 snd_soc_update_bits(codec, WM8955_POWER_MANAGEMENT_1,
646 WM8955_DIGENB, 0);
647 snd_soc_update_bits(codec, WM8955_CLOCKING_PLL,
648 WM8955_PLL_RB | WM8955_PLLEN, 0);
649
650 wm8955_configure_clocking(codec);
651 }
652
653 return 0;
654}
655
656
657static int wm8955_set_sysclk(struct snd_soc_dai *dai, int clk_id,
658 unsigned int freq, int dir)
659{
660 struct snd_soc_codec *codec = dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +0900661 struct wm8955_priv *priv = snd_soc_codec_get_drvdata(codec);
Mark Brownb35a28a2009-12-18 12:00:22 +0000662 int div;
663
664 switch (clk_id) {
665 case WM8955_CLK_MCLK:
666 if (freq > 15000000) {
667 priv->mclk_rate = freq /= 2;
668 div = WM8955_MCLKDIV2;
669 } else {
670 priv->mclk_rate = freq;
671 div = 0;
672 }
673
674 snd_soc_update_bits(codec, WM8955_SAMPLE_RATE,
675 WM8955_MCLKDIV2, div);
676 break;
677
678 default:
679 return -EINVAL;
680 }
681
682 dev_dbg(dai->dev, "Clock source is %d at %uHz\n", clk_id, freq);
683
684 return 0;
685}
686
687static int wm8955_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
688{
689 struct snd_soc_codec *codec = dai->codec;
690 u16 aif = 0;
691
692 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
693 case SND_SOC_DAIFMT_CBS_CFS:
694 break;
695 case SND_SOC_DAIFMT_CBM_CFM:
696 aif |= WM8955_MS;
697 break;
698 default:
699 return -EINVAL;
700 }
701
702 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
703 case SND_SOC_DAIFMT_DSP_B:
704 aif |= WM8955_LRP;
705 case SND_SOC_DAIFMT_DSP_A:
706 aif |= 0x3;
707 break;
708 case SND_SOC_DAIFMT_I2S:
709 aif |= 0x2;
710 break;
711 case SND_SOC_DAIFMT_RIGHT_J:
712 break;
713 case SND_SOC_DAIFMT_LEFT_J:
714 aif |= 0x1;
715 break;
716 default:
717 return -EINVAL;
718 }
719
720 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
721 case SND_SOC_DAIFMT_DSP_A:
722 case SND_SOC_DAIFMT_DSP_B:
723 /* frame inversion not valid for DSP modes */
724 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
725 case SND_SOC_DAIFMT_NB_NF:
726 break;
727 case SND_SOC_DAIFMT_IB_NF:
728 aif |= WM8955_BCLKINV;
729 break;
730 default:
731 return -EINVAL;
732 }
733 break;
734
735 case SND_SOC_DAIFMT_I2S:
736 case SND_SOC_DAIFMT_RIGHT_J:
737 case SND_SOC_DAIFMT_LEFT_J:
738 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
739 case SND_SOC_DAIFMT_NB_NF:
740 break;
741 case SND_SOC_DAIFMT_IB_IF:
742 aif |= WM8955_BCLKINV | WM8955_LRP;
743 break;
744 case SND_SOC_DAIFMT_IB_NF:
745 aif |= WM8955_BCLKINV;
746 break;
747 case SND_SOC_DAIFMT_NB_IF:
748 aif |= WM8955_LRP;
749 break;
750 default:
751 return -EINVAL;
752 }
753 break;
754 default:
755 return -EINVAL;
756 }
757
758 snd_soc_update_bits(codec, WM8955_AUDIO_INTERFACE,
759 WM8955_MS | WM8955_FORMAT_MASK | WM8955_BCLKINV |
760 WM8955_LRP, aif);
761
762 return 0;
763}
764
765
766static int wm8955_digital_mute(struct snd_soc_dai *codec_dai, int mute)
767{
768 struct snd_soc_codec *codec = codec_dai->codec;
769 int val;
770
771 if (mute)
772 val = WM8955_DACMU;
773 else
774 val = 0;
775
776 snd_soc_update_bits(codec, WM8955_DAC_CONTROL, WM8955_DACMU, val);
777
778 return 0;
779}
780
781static int wm8955_set_bias_level(struct snd_soc_codec *codec,
782 enum snd_soc_bias_level level)
783{
Mark Brownb2c812e2010-04-14 15:35:19 +0900784 struct wm8955_priv *wm8955 = snd_soc_codec_get_drvdata(codec);
Mark Brown95860fd2011-12-29 21:42:36 +0000785 int ret;
Mark Brownb35a28a2009-12-18 12:00:22 +0000786
787 switch (level) {
788 case SND_SOC_BIAS_ON:
789 break;
790
791 case SND_SOC_BIAS_PREPARE:
792 /* VMID resistance 2*50k */
793 snd_soc_update_bits(codec, WM8955_POWER_MANAGEMENT_1,
794 WM8955_VMIDSEL_MASK,
795 0x1 << WM8955_VMIDSEL_SHIFT);
796
797 /* Default bias current */
798 snd_soc_update_bits(codec, WM8955_ADDITIONAL_CONTROL_1,
799 WM8955_VSEL_MASK,
800 0x2 << WM8955_VSEL_SHIFT);
801 break;
802
803 case SND_SOC_BIAS_STANDBY:
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200804 if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
Mark Brownb35a28a2009-12-18 12:00:22 +0000805 ret = regulator_bulk_enable(ARRAY_SIZE(wm8955->supplies),
806 wm8955->supplies);
807 if (ret != 0) {
808 dev_err(codec->dev,
809 "Failed to enable supplies: %d\n",
810 ret);
811 return ret;
812 }
813
Mark Brown95860fd2011-12-29 21:42:36 +0000814 regcache_sync(wm8955->regmap);
Mark Brownb35a28a2009-12-18 12:00:22 +0000815
816 /* Enable VREF and VMID */
817 snd_soc_update_bits(codec, WM8955_POWER_MANAGEMENT_1,
818 WM8955_VREF |
819 WM8955_VMIDSEL_MASK,
820 WM8955_VREF |
821 0x3 << WM8955_VREF_SHIFT);
822
823 /* Let VMID ramp */
824 msleep(500);
825
826 /* High resistance VROI to maintain outputs */
827 snd_soc_update_bits(codec,
828 WM8955_ADDITIONAL_CONTROL_3,
829 WM8955_VROI, WM8955_VROI);
830 }
831
832 /* Maintain VMID with 2*250k */
833 snd_soc_update_bits(codec, WM8955_POWER_MANAGEMENT_1,
834 WM8955_VMIDSEL_MASK,
835 0x2 << WM8955_VMIDSEL_SHIFT);
836
837 /* Minimum bias current */
838 snd_soc_update_bits(codec, WM8955_ADDITIONAL_CONTROL_1,
839 WM8955_VSEL_MASK, 0);
840 break;
841
842 case SND_SOC_BIAS_OFF:
843 /* Low resistance VROI to help discharge */
844 snd_soc_update_bits(codec,
845 WM8955_ADDITIONAL_CONTROL_3,
846 WM8955_VROI, 0);
847
848 /* Turn off VMID and VREF */
849 snd_soc_update_bits(codec, WM8955_POWER_MANAGEMENT_1,
850 WM8955_VREF |
851 WM8955_VMIDSEL_MASK, 0);
852
853 regulator_bulk_disable(ARRAY_SIZE(wm8955->supplies),
854 wm8955->supplies);
855 break;
856 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200857 codec->dapm.bias_level = level;
Mark Brownb35a28a2009-12-18 12:00:22 +0000858 return 0;
859}
860
861#define WM8955_RATES SNDRV_PCM_RATE_8000_96000
862
863#define WM8955_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
864 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
865
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100866static const struct snd_soc_dai_ops wm8955_dai_ops = {
Mark Brownb35a28a2009-12-18 12:00:22 +0000867 .set_sysclk = wm8955_set_sysclk,
868 .set_fmt = wm8955_set_fmt,
869 .hw_params = wm8955_hw_params,
870 .digital_mute = wm8955_digital_mute,
871};
872
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000873static struct snd_soc_dai_driver wm8955_dai = {
874 .name = "wm8955-hifi",
Mark Brownb35a28a2009-12-18 12:00:22 +0000875 .playback = {
876 .stream_name = "Playback",
877 .channels_min = 2,
878 .channels_max = 2,
879 .rates = WM8955_RATES,
880 .formats = WM8955_FORMATS,
881 },
882 .ops = &wm8955_dai_ops,
883};
Mark Brownb35a28a2009-12-18 12:00:22 +0000884
885#ifdef CONFIG_PM
Lars-Peter Clausen84b315e2011-12-02 10:18:28 +0100886static int wm8955_suspend(struct snd_soc_codec *codec)
Mark Brownb35a28a2009-12-18 12:00:22 +0000887{
Mark Brown95860fd2011-12-29 21:42:36 +0000888 struct wm8955_priv *wm8955 = snd_soc_codec_get_drvdata(codec);
889
Mark Brownb35a28a2009-12-18 12:00:22 +0000890 wm8955_set_bias_level(codec, SND_SOC_BIAS_OFF);
891
Mark Brown95860fd2011-12-29 21:42:36 +0000892 regcache_mark_dirty(wm8955->regmap);
893
Mark Brownb35a28a2009-12-18 12:00:22 +0000894 return 0;
895}
896
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000897static int wm8955_resume(struct snd_soc_codec *codec)
Mark Brownb35a28a2009-12-18 12:00:22 +0000898{
Mark Brownb35a28a2009-12-18 12:00:22 +0000899 wm8955_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
900
901 return 0;
902}
903#else
904#define wm8955_suspend NULL
905#define wm8955_resume NULL
906#endif
907
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000908static int wm8955_probe(struct snd_soc_codec *codec)
Mark Brownb35a28a2009-12-18 12:00:22 +0000909{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000910 struct wm8955_priv *wm8955 = snd_soc_codec_get_drvdata(codec);
911 struct wm8955_pdata *pdata = dev_get_platdata(codec->dev);
912 int ret, i;
Mark Brownb35a28a2009-12-18 12:00:22 +0000913
Mark Brown95860fd2011-12-29 21:42:36 +0000914 codec->control_data = wm8955->regmap;
915
916 ret = snd_soc_codec_set_cache_io(codec, 7, 9, SND_SOC_REGMAP);
Mark Brownb35a28a2009-12-18 12:00:22 +0000917 if (ret != 0) {
918 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000919 return ret;
Mark Brownb35a28a2009-12-18 12:00:22 +0000920 }
921
922 for (i = 0; i < ARRAY_SIZE(wm8955->supplies); i++)
923 wm8955->supplies[i].supply = wm8955_supply_names[i];
924
925 ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(wm8955->supplies),
926 wm8955->supplies);
927 if (ret != 0) {
928 dev_err(codec->dev, "Failed to request supplies: %d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000929 return ret;
Mark Brownb35a28a2009-12-18 12:00:22 +0000930 }
931
932 ret = regulator_bulk_enable(ARRAY_SIZE(wm8955->supplies),
933 wm8955->supplies);
934 if (ret != 0) {
935 dev_err(codec->dev, "Failed to enable supplies: %d\n", ret);
936 goto err_get;
937 }
938
939 ret = wm8955_reset(codec);
940 if (ret < 0) {
941 dev_err(codec->dev, "Failed to issue reset: %d\n", ret);
942 goto err_enable;
943 }
944
Mark Brownb35a28a2009-12-18 12:00:22 +0000945 /* Change some default settings - latch VU and enable ZC */
Mark Browna1b3b5e2010-12-24 16:59:30 +0000946 snd_soc_update_bits(codec, WM8955_LEFT_DAC_VOLUME,
947 WM8955_LDVU, WM8955_LDVU);
948 snd_soc_update_bits(codec, WM8955_RIGHT_DAC_VOLUME,
949 WM8955_RDVU, WM8955_RDVU);
950 snd_soc_update_bits(codec, WM8955_LOUT1_VOLUME,
951 WM8955_LO1VU | WM8955_LO1ZC,
952 WM8955_LO1VU | WM8955_LO1ZC);
953 snd_soc_update_bits(codec, WM8955_ROUT1_VOLUME,
954 WM8955_RO1VU | WM8955_RO1ZC,
955 WM8955_RO1VU | WM8955_RO1ZC);
956 snd_soc_update_bits(codec, WM8955_LOUT2_VOLUME,
957 WM8955_LO2VU | WM8955_LO2ZC,
958 WM8955_LO2VU | WM8955_LO2ZC);
959 snd_soc_update_bits(codec, WM8955_ROUT2_VOLUME,
960 WM8955_RO2VU | WM8955_RO2ZC,
961 WM8955_RO2VU | WM8955_RO2ZC);
962 snd_soc_update_bits(codec, WM8955_MONOOUT_VOLUME,
963 WM8955_MOZC, WM8955_MOZC);
Mark Brownb35a28a2009-12-18 12:00:22 +0000964
965 /* Also enable adaptive bass boost by default */
Mark Browna1b3b5e2010-12-24 16:59:30 +0000966 snd_soc_update_bits(codec, WM8955_BASS_CONTROL, WM8955_BB, WM8955_BB);
Mark Brownb35a28a2009-12-18 12:00:22 +0000967
968 /* Set platform data values */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000969 if (pdata) {
970 if (pdata->out2_speaker)
Mark Brown95860fd2011-12-29 21:42:36 +0000971 snd_soc_update_bits(codec, WM8955_ADDITIONAL_CONTROL_2,
972 WM8955_ROUT2INV, WM8955_ROUT2INV);
Mark Brownb35a28a2009-12-18 12:00:22 +0000973
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000974 if (pdata->monoin_diff)
Mark Brown95860fd2011-12-29 21:42:36 +0000975 snd_soc_update_bits(codec, WM8955_MONO_OUT_MIX_1,
976 WM8955_DMEN, WM8955_DMEN);
Mark Brownb35a28a2009-12-18 12:00:22 +0000977 }
978
979 wm8955_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
980
981 /* Bias level configuration will have done an extra enable */
982 regulator_bulk_disable(ARRAY_SIZE(wm8955->supplies), wm8955->supplies);
983
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000984 wm8955_add_widgets(codec);
Mark Brownb35a28a2009-12-18 12:00:22 +0000985 return 0;
986
Mark Brownb35a28a2009-12-18 12:00:22 +0000987err_enable:
988 regulator_bulk_disable(ARRAY_SIZE(wm8955->supplies), wm8955->supplies);
989err_get:
990 regulator_bulk_free(ARRAY_SIZE(wm8955->supplies), wm8955->supplies);
Mark Brownb35a28a2009-12-18 12:00:22 +0000991 return ret;
992}
993
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000994static int wm8955_remove(struct snd_soc_codec *codec)
Mark Brownb35a28a2009-12-18 12:00:22 +0000995{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000996 struct wm8955_priv *wm8955 = snd_soc_codec_get_drvdata(codec);
997
998 wm8955_set_bias_level(codec, SND_SOC_BIAS_OFF);
Mark Brownb35a28a2009-12-18 12:00:22 +0000999 regulator_bulk_free(ARRAY_SIZE(wm8955->supplies), wm8955->supplies);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001000 return 0;
Mark Brownb35a28a2009-12-18 12:00:22 +00001001}
1002
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001003static struct snd_soc_codec_driver soc_codec_dev_wm8955 = {
1004 .probe = wm8955_probe,
1005 .remove = wm8955_remove,
1006 .suspend = wm8955_suspend,
1007 .resume = wm8955_resume,
1008 .set_bias_level = wm8955_set_bias_level,
Mark Brown95860fd2011-12-29 21:42:36 +00001009};
1010
1011static const struct regmap_config wm8955_regmap = {
1012 .reg_bits = 7,
1013 .val_bits = 9,
1014
1015 .max_register = WM8955_MAX_REGISTER,
1016 .volatile_reg = wm8955_volatile,
1017 .writeable_reg = wm8955_writeable,
1018
1019 .cache_type = REGCACHE_RBTREE,
1020 .reg_defaults = wm8955_reg_defaults,
1021 .num_reg_defaults = ARRAY_SIZE(wm8955_reg_defaults),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001022};
1023
Mark Brownb35a28a2009-12-18 12:00:22 +00001024static __devinit int wm8955_i2c_probe(struct i2c_client *i2c,
1025 const struct i2c_device_id *id)
1026{
1027 struct wm8955_priv *wm8955;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001028 int ret;
Mark Brownb35a28a2009-12-18 12:00:22 +00001029
Mark Brownba5c88d2011-12-29 21:23:04 +00001030 wm8955 = devm_kzalloc(&i2c->dev, sizeof(struct wm8955_priv),
1031 GFP_KERNEL);
Mark Brownb35a28a2009-12-18 12:00:22 +00001032 if (wm8955 == NULL)
1033 return -ENOMEM;
1034
Mark Brown95860fd2011-12-29 21:42:36 +00001035 wm8955->regmap = regmap_init_i2c(i2c, &wm8955_regmap);
1036 if (IS_ERR(wm8955->regmap)) {
1037 ret = PTR_ERR(wm8955->regmap);
1038 dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
1039 ret);
1040 return ret;
1041 }
1042
Mark Brownb35a28a2009-12-18 12:00:22 +00001043 i2c_set_clientdata(i2c, wm8955);
Mark Brownb35a28a2009-12-18 12:00:22 +00001044
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001045 ret = snd_soc_register_codec(&i2c->dev,
1046 &soc_codec_dev_wm8955, &wm8955_dai, 1);
Mark Brown95860fd2011-12-29 21:42:36 +00001047 if (ret != 0)
1048 goto err;
Mark Brownba5c88d2011-12-29 21:23:04 +00001049
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001050 return ret;
Mark Brown95860fd2011-12-29 21:42:36 +00001051
1052err:
1053 regmap_exit(wm8955->regmap);
1054 return ret;
Mark Brownb35a28a2009-12-18 12:00:22 +00001055}
1056
1057static __devexit int wm8955_i2c_remove(struct i2c_client *client)
1058{
Mark Brown95860fd2011-12-29 21:42:36 +00001059 struct wm8955_priv *wm8955 = i2c_get_clientdata(client);
1060
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001061 snd_soc_unregister_codec(&client->dev);
Mark Brown95860fd2011-12-29 21:42:36 +00001062 regmap_exit(wm8955->regmap);
1063
Mark Brownb35a28a2009-12-18 12:00:22 +00001064 return 0;
1065}
1066
1067static const struct i2c_device_id wm8955_i2c_id[] = {
1068 { "wm8955", 0 },
1069 { }
1070};
1071MODULE_DEVICE_TABLE(i2c, wm8955_i2c_id);
1072
1073static struct i2c_driver wm8955_i2c_driver = {
1074 .driver = {
Mark Brown091edcc2011-12-02 22:08:49 +00001075 .name = "wm8955",
Mark Brownb35a28a2009-12-18 12:00:22 +00001076 .owner = THIS_MODULE,
1077 },
1078 .probe = wm8955_i2c_probe,
1079 .remove = __devexit_p(wm8955_i2c_remove),
1080 .id_table = wm8955_i2c_id,
1081};
Mark Brownb35a28a2009-12-18 12:00:22 +00001082
1083static int __init wm8955_modinit(void)
1084{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001085 int ret = 0;
Mark Brownb35a28a2009-12-18 12:00:22 +00001086 ret = i2c_add_driver(&wm8955_i2c_driver);
1087 if (ret != 0) {
1088 printk(KERN_ERR "Failed to register WM8955 I2C driver: %d\n",
1089 ret);
1090 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001091 return ret;
Mark Brownb35a28a2009-12-18 12:00:22 +00001092}
1093module_init(wm8955_modinit);
1094
1095static void __exit wm8955_exit(void)
1096{
Mark Brownb35a28a2009-12-18 12:00:22 +00001097 i2c_del_driver(&wm8955_i2c_driver);
Mark Brownb35a28a2009-12-18 12:00:22 +00001098}
1099module_exit(wm8955_exit);
1100
1101MODULE_DESCRIPTION("ASoC WM8955 driver");
1102MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
1103MODULE_LICENSE("GPL");