Merge branch 'slab/for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/penber...
[cascardo/linux.git] / sound / soc / codecs / wm8731.c
1 /*
2  * wm8731.c  --  WM8731 ALSA SoC Audio driver
3  *
4  * Copyright 2005 Openedhand Ltd.
5  *
6  * Author: Richard Purdie <richard@openedhand.com>
7  *
8  * Based on wm8753.c by Liam Girdwood
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation.
13  */
14
15 #include <linux/module.h>
16 #include <linux/moduleparam.h>
17 #include <linux/init.h>
18 #include <linux/delay.h>
19 #include <linux/pm.h>
20 #include <linux/i2c.h>
21 #include <linux/slab.h>
22 #include <linux/regmap.h>
23 #include <linux/regulator/consumer.h>
24 #include <linux/spi/spi.h>
25 #include <linux/of_device.h>
26 #include <sound/core.h>
27 #include <sound/pcm.h>
28 #include <sound/pcm_params.h>
29 #include <sound/soc.h>
30 #include <sound/initval.h>
31 #include <sound/tlv.h>
32
33 #include "wm8731.h"
34
35 #define WM8731_NUM_SUPPLIES 4
36 static const char *wm8731_supply_names[WM8731_NUM_SUPPLIES] = {
37         "AVDD",
38         "HPVDD",
39         "DCVDD",
40         "DBVDD",
41 };
42
43 /* codec private data */
44 struct wm8731_priv {
45         struct regmap *regmap;
46         struct regulator_bulk_data supplies[WM8731_NUM_SUPPLIES];
47         unsigned int sysclk;
48         int sysclk_type;
49         int playback_fs;
50         bool deemph;
51 };
52
53
54 /*
55  * wm8731 register cache
56  */
57 static const struct reg_default wm8731_reg_defaults[] = {
58         { 0, 0x0097 },
59         { 1, 0x0097 },
60         { 2, 0x0079 },
61         { 3, 0x0079 },
62         { 4, 0x000a },
63         { 5, 0x0008 },
64         { 6, 0x009f },
65         { 7, 0x000a },
66         { 8, 0x0000 },
67         { 9, 0x0000 },
68 };
69
70 static bool wm8731_volatile(struct device *dev, unsigned int reg)
71 {
72         return reg == WM8731_RESET;
73 }
74
75 static bool wm8731_writeable(struct device *dev, unsigned int reg)
76 {
77         return reg <= WM8731_RESET;
78 }
79
80 #define wm8731_reset(c) snd_soc_write(c, WM8731_RESET, 0)
81
82 static const char *wm8731_input_select[] = {"Line In", "Mic"};
83
84 static const struct soc_enum wm8731_insel_enum =
85         SOC_ENUM_SINGLE(WM8731_APANA, 2, 2, wm8731_input_select);
86
87 static int wm8731_deemph[] = { 0, 32000, 44100, 48000 };
88
89 static int wm8731_set_deemph(struct snd_soc_codec *codec)
90 {
91         struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
92         int val, i, best;
93
94         /* If we're using deemphasis select the nearest available sample
95          * rate.
96          */
97         if (wm8731->deemph) {
98                 best = 1;
99                 for (i = 2; i < ARRAY_SIZE(wm8731_deemph); i++) {
100                         if (abs(wm8731_deemph[i] - wm8731->playback_fs) <
101                             abs(wm8731_deemph[best] - wm8731->playback_fs))
102                                 best = i;
103                 }
104
105                 val = best << 1;
106         } else {
107                 best = 0;
108                 val = 0;
109         }
110
111         dev_dbg(codec->dev, "Set deemphasis %d (%dHz)\n",
112                 best, wm8731_deemph[best]);
113
114         return snd_soc_update_bits(codec, WM8731_APDIGI, 0x6, val);
115 }
116
117 static int wm8731_get_deemph(struct snd_kcontrol *kcontrol,
118                              struct snd_ctl_elem_value *ucontrol)
119 {
120         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
121         struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
122
123         ucontrol->value.enumerated.item[0] = wm8731->deemph;
124
125         return 0;
126 }
127
128 static int wm8731_put_deemph(struct snd_kcontrol *kcontrol,
129                              struct snd_ctl_elem_value *ucontrol)
130 {
131         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
132         struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
133         int deemph = ucontrol->value.enumerated.item[0];
134         int ret = 0;
135
136         if (deemph > 1)
137                 return -EINVAL;
138
139         mutex_lock(&codec->mutex);
140         if (wm8731->deemph != deemph) {
141                 wm8731->deemph = deemph;
142
143                 wm8731_set_deemph(codec);
144
145                 ret = 1;
146         }
147         mutex_unlock(&codec->mutex);
148
149         return ret;
150 }
151
152 static const DECLARE_TLV_DB_SCALE(in_tlv, -3450, 150, 0);
153 static const DECLARE_TLV_DB_SCALE(sidetone_tlv, -1500, 300, 0);
154 static const DECLARE_TLV_DB_SCALE(out_tlv, -12100, 100, 1);
155 static const DECLARE_TLV_DB_SCALE(mic_tlv, 0, 2000, 0);
156
157 static const struct snd_kcontrol_new wm8731_snd_controls[] = {
158
159 SOC_DOUBLE_R_TLV("Master Playback Volume", WM8731_LOUT1V, WM8731_ROUT1V,
160                  0, 127, 0, out_tlv),
161 SOC_DOUBLE_R("Master Playback ZC Switch", WM8731_LOUT1V, WM8731_ROUT1V,
162         7, 1, 0),
163
164 SOC_DOUBLE_R_TLV("Capture Volume", WM8731_LINVOL, WM8731_RINVOL, 0, 31, 0,
165                  in_tlv),
166 SOC_DOUBLE_R("Line Capture Switch", WM8731_LINVOL, WM8731_RINVOL, 7, 1, 1),
167
168 SOC_SINGLE_TLV("Mic Boost Volume", WM8731_APANA, 0, 1, 0, mic_tlv),
169 SOC_SINGLE("Mic Capture Switch", WM8731_APANA, 1, 1, 1),
170
171 SOC_SINGLE_TLV("Sidetone Playback Volume", WM8731_APANA, 6, 3, 1,
172                sidetone_tlv),
173
174 SOC_SINGLE("ADC High Pass Filter Switch", WM8731_APDIGI, 0, 1, 1),
175 SOC_SINGLE("Store DC Offset Switch", WM8731_APDIGI, 4, 1, 0),
176
177 SOC_SINGLE_BOOL_EXT("Playback Deemphasis Switch", 0,
178                     wm8731_get_deemph, wm8731_put_deemph),
179 };
180
181 /* Output Mixer */
182 static const struct snd_kcontrol_new wm8731_output_mixer_controls[] = {
183 SOC_DAPM_SINGLE("Line Bypass Switch", WM8731_APANA, 3, 1, 0),
184 SOC_DAPM_SINGLE("Mic Sidetone Switch", WM8731_APANA, 5, 1, 0),
185 SOC_DAPM_SINGLE("HiFi Playback Switch", WM8731_APANA, 4, 1, 0),
186 };
187
188 /* Input mux */
189 static const struct snd_kcontrol_new wm8731_input_mux_controls =
190 SOC_DAPM_ENUM("Input Select", wm8731_insel_enum);
191
192 static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
193 SND_SOC_DAPM_SUPPLY("ACTIVE",WM8731_ACTIVE, 0, 0, NULL, 0),
194 SND_SOC_DAPM_SUPPLY("OSC", WM8731_PWR, 5, 1, NULL, 0),
195 SND_SOC_DAPM_MIXER("Output Mixer", WM8731_PWR, 4, 1,
196         &wm8731_output_mixer_controls[0],
197         ARRAY_SIZE(wm8731_output_mixer_controls)),
198 SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8731_PWR, 3, 1),
199 SND_SOC_DAPM_OUTPUT("LOUT"),
200 SND_SOC_DAPM_OUTPUT("LHPOUT"),
201 SND_SOC_DAPM_OUTPUT("ROUT"),
202 SND_SOC_DAPM_OUTPUT("RHPOUT"),
203 SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8731_PWR, 2, 1),
204 SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &wm8731_input_mux_controls),
205 SND_SOC_DAPM_PGA("Line Input", WM8731_PWR, 0, 1, NULL, 0),
206 SND_SOC_DAPM_MICBIAS("Mic Bias", WM8731_PWR, 1, 1),
207 SND_SOC_DAPM_INPUT("MICIN"),
208 SND_SOC_DAPM_INPUT("RLINEIN"),
209 SND_SOC_DAPM_INPUT("LLINEIN"),
210 };
211
212 static int wm8731_check_osc(struct snd_soc_dapm_widget *source,
213                             struct snd_soc_dapm_widget *sink)
214 {
215         struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(source->codec);
216
217         return wm8731->sysclk_type == WM8731_SYSCLK_XTAL;
218 }
219
220 static const struct snd_soc_dapm_route wm8731_intercon[] = {
221         {"DAC", NULL, "OSC", wm8731_check_osc},
222         {"ADC", NULL, "OSC", wm8731_check_osc},
223         {"DAC", NULL, "ACTIVE"},
224         {"ADC", NULL, "ACTIVE"},
225
226         /* output mixer */
227         {"Output Mixer", "Line Bypass Switch", "Line Input"},
228         {"Output Mixer", "HiFi Playback Switch", "DAC"},
229         {"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
230
231         /* outputs */
232         {"RHPOUT", NULL, "Output Mixer"},
233         {"ROUT", NULL, "Output Mixer"},
234         {"LHPOUT", NULL, "Output Mixer"},
235         {"LOUT", NULL, "Output Mixer"},
236
237         /* input mux */
238         {"Input Mux", "Line In", "Line Input"},
239         {"Input Mux", "Mic", "Mic Bias"},
240         {"ADC", NULL, "Input Mux"},
241
242         /* inputs */
243         {"Line Input", NULL, "LLINEIN"},
244         {"Line Input", NULL, "RLINEIN"},
245         {"Mic Bias", NULL, "MICIN"},
246 };
247
248 struct _coeff_div {
249         u32 mclk;
250         u32 rate;
251         u16 fs;
252         u8 sr:4;
253         u8 bosr:1;
254         u8 usb:1;
255 };
256
257 /* codec mclk clock divider coefficients */
258 static const struct _coeff_div coeff_div[] = {
259         /* 48k */
260         {12288000, 48000, 256, 0x0, 0x0, 0x0},
261         {18432000, 48000, 384, 0x0, 0x1, 0x0},
262         {12000000, 48000, 250, 0x0, 0x0, 0x1},
263
264         /* 32k */
265         {12288000, 32000, 384, 0x6, 0x0, 0x0},
266         {18432000, 32000, 576, 0x6, 0x1, 0x0},
267         {12000000, 32000, 375, 0x6, 0x0, 0x1},
268
269         /* 8k */
270         {12288000, 8000, 1536, 0x3, 0x0, 0x0},
271         {18432000, 8000, 2304, 0x3, 0x1, 0x0},
272         {11289600, 8000, 1408, 0xb, 0x0, 0x0},
273         {16934400, 8000, 2112, 0xb, 0x1, 0x0},
274         {12000000, 8000, 1500, 0x3, 0x0, 0x1},
275
276         /* 96k */
277         {12288000, 96000, 128, 0x7, 0x0, 0x0},
278         {18432000, 96000, 192, 0x7, 0x1, 0x0},
279         {12000000, 96000, 125, 0x7, 0x0, 0x1},
280
281         /* 44.1k */
282         {11289600, 44100, 256, 0x8, 0x0, 0x0},
283         {16934400, 44100, 384, 0x8, 0x1, 0x0},
284         {12000000, 44100, 272, 0x8, 0x1, 0x1},
285
286         /* 88.2k */
287         {11289600, 88200, 128, 0xf, 0x0, 0x0},
288         {16934400, 88200, 192, 0xf, 0x1, 0x0},
289         {12000000, 88200, 136, 0xf, 0x1, 0x1},
290 };
291
292 static inline int get_coeff(int mclk, int rate)
293 {
294         int i;
295
296         for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
297                 if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
298                         return i;
299         }
300         return 0;
301 }
302
303 static int wm8731_hw_params(struct snd_pcm_substream *substream,
304                             struct snd_pcm_hw_params *params,
305                             struct snd_soc_dai *dai)
306 {
307         struct snd_soc_codec *codec = dai->codec;
308         struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
309         u16 iface = snd_soc_read(codec, WM8731_IFACE) & 0xfff3;
310         int i = get_coeff(wm8731->sysclk, params_rate(params));
311         u16 srate = (coeff_div[i].sr << 2) |
312                 (coeff_div[i].bosr << 1) | coeff_div[i].usb;
313
314         wm8731->playback_fs = params_rate(params);
315
316         snd_soc_write(codec, WM8731_SRATE, srate);
317
318         /* bit size */
319         switch (params_format(params)) {
320         case SNDRV_PCM_FORMAT_S16_LE:
321                 break;
322         case SNDRV_PCM_FORMAT_S20_3LE:
323                 iface |= 0x0004;
324                 break;
325         case SNDRV_PCM_FORMAT_S24_LE:
326                 iface |= 0x0008;
327                 break;
328         }
329
330         wm8731_set_deemph(codec);
331
332         snd_soc_write(codec, WM8731_IFACE, iface);
333         return 0;
334 }
335
336 static int wm8731_mute(struct snd_soc_dai *dai, int mute)
337 {
338         struct snd_soc_codec *codec = dai->codec;
339         u16 mute_reg = snd_soc_read(codec, WM8731_APDIGI) & 0xfff7;
340
341         if (mute)
342                 snd_soc_write(codec, WM8731_APDIGI, mute_reg | 0x8);
343         else
344                 snd_soc_write(codec, WM8731_APDIGI, mute_reg);
345         return 0;
346 }
347
348 static int wm8731_set_dai_sysclk(struct snd_soc_dai *codec_dai,
349                 int clk_id, unsigned int freq, int dir)
350 {
351         struct snd_soc_codec *codec = codec_dai->codec;
352         struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
353
354         switch (clk_id) {
355         case WM8731_SYSCLK_XTAL:
356         case WM8731_SYSCLK_MCLK:
357                 wm8731->sysclk_type = clk_id;
358                 break;
359         default:
360                 return -EINVAL;
361         }
362
363         switch (freq) {
364         case 11289600:
365         case 12000000:
366         case 12288000:
367         case 16934400:
368         case 18432000:
369                 wm8731->sysclk = freq;
370                 break;
371         default:
372                 return -EINVAL;
373         }
374
375         snd_soc_dapm_sync(&codec->dapm);
376
377         return 0;
378 }
379
380
381 static int wm8731_set_dai_fmt(struct snd_soc_dai *codec_dai,
382                 unsigned int fmt)
383 {
384         struct snd_soc_codec *codec = codec_dai->codec;
385         u16 iface = 0;
386
387         /* set master/slave audio interface */
388         switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
389         case SND_SOC_DAIFMT_CBM_CFM:
390                 iface |= 0x0040;
391                 break;
392         case SND_SOC_DAIFMT_CBS_CFS:
393                 break;
394         default:
395                 return -EINVAL;
396         }
397
398         /* interface format */
399         switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
400         case SND_SOC_DAIFMT_I2S:
401                 iface |= 0x0002;
402                 break;
403         case SND_SOC_DAIFMT_RIGHT_J:
404                 break;
405         case SND_SOC_DAIFMT_LEFT_J:
406                 iface |= 0x0001;
407                 break;
408         case SND_SOC_DAIFMT_DSP_A:
409                 iface |= 0x0003;
410                 break;
411         case SND_SOC_DAIFMT_DSP_B:
412                 iface |= 0x0013;
413                 break;
414         default:
415                 return -EINVAL;
416         }
417
418         /* clock inversion */
419         switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
420         case SND_SOC_DAIFMT_NB_NF:
421                 break;
422         case SND_SOC_DAIFMT_IB_IF:
423                 iface |= 0x0090;
424                 break;
425         case SND_SOC_DAIFMT_IB_NF:
426                 iface |= 0x0080;
427                 break;
428         case SND_SOC_DAIFMT_NB_IF:
429                 iface |= 0x0010;
430                 break;
431         default:
432                 return -EINVAL;
433         }
434
435         /* set iface */
436         snd_soc_write(codec, WM8731_IFACE, iface);
437         return 0;
438 }
439
440 static int wm8731_set_bias_level(struct snd_soc_codec *codec,
441                                  enum snd_soc_bias_level level)
442 {
443         struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
444         int ret;
445         u16 reg;
446
447         switch (level) {
448         case SND_SOC_BIAS_ON:
449                 break;
450         case SND_SOC_BIAS_PREPARE:
451                 break;
452         case SND_SOC_BIAS_STANDBY:
453                 if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
454                         ret = regulator_bulk_enable(ARRAY_SIZE(wm8731->supplies),
455                                                     wm8731->supplies);
456                         if (ret != 0)
457                                 return ret;
458
459                         regcache_sync(wm8731->regmap);
460                 }
461
462                 /* Clear PWROFF, gate CLKOUT, everything else as-is */
463                 reg = snd_soc_read(codec, WM8731_PWR) & 0xff7f;
464                 snd_soc_write(codec, WM8731_PWR, reg | 0x0040);
465                 break;
466         case SND_SOC_BIAS_OFF:
467                 snd_soc_write(codec, WM8731_PWR, 0xffff);
468                 regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies),
469                                        wm8731->supplies);
470                 regcache_mark_dirty(wm8731->regmap);
471                 break;
472         }
473         codec->dapm.bias_level = level;
474         return 0;
475 }
476
477 #define WM8731_RATES SNDRV_PCM_RATE_8000_96000
478
479 #define WM8731_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
480         SNDRV_PCM_FMTBIT_S24_LE)
481
482 static const struct snd_soc_dai_ops wm8731_dai_ops = {
483         .hw_params      = wm8731_hw_params,
484         .digital_mute   = wm8731_mute,
485         .set_sysclk     = wm8731_set_dai_sysclk,
486         .set_fmt        = wm8731_set_dai_fmt,
487 };
488
489 static struct snd_soc_dai_driver wm8731_dai = {
490         .name = "wm8731-hifi",
491         .playback = {
492                 .stream_name = "Playback",
493                 .channels_min = 1,
494                 .channels_max = 2,
495                 .rates = WM8731_RATES,
496                 .formats = WM8731_FORMATS,},
497         .capture = {
498                 .stream_name = "Capture",
499                 .channels_min = 1,
500                 .channels_max = 2,
501                 .rates = WM8731_RATES,
502                 .formats = WM8731_FORMATS,},
503         .ops = &wm8731_dai_ops,
504         .symmetric_rates = 1,
505 };
506
507 #ifdef CONFIG_PM
508 static int wm8731_suspend(struct snd_soc_codec *codec)
509 {
510         wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF);
511
512         return 0;
513 }
514
515 static int wm8731_resume(struct snd_soc_codec *codec)
516 {
517         wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
518
519         return 0;
520 }
521 #else
522 #define wm8731_suspend NULL
523 #define wm8731_resume NULL
524 #endif
525
526 static int wm8731_probe(struct snd_soc_codec *codec)
527 {
528         struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
529         int ret = 0, i;
530
531         codec->control_data = wm8731->regmap;
532         ret = snd_soc_codec_set_cache_io(codec, 7, 9, SND_SOC_REGMAP);
533         if (ret < 0) {
534                 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
535                 return ret;
536         }
537
538         for (i = 0; i < ARRAY_SIZE(wm8731->supplies); i++)
539                 wm8731->supplies[i].supply = wm8731_supply_names[i];
540
541         ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(wm8731->supplies),
542                                  wm8731->supplies);
543         if (ret != 0) {
544                 dev_err(codec->dev, "Failed to request supplies: %d\n", ret);
545                 return ret;
546         }
547
548         ret = regulator_bulk_enable(ARRAY_SIZE(wm8731->supplies),
549                                     wm8731->supplies);
550         if (ret != 0) {
551                 dev_err(codec->dev, "Failed to enable supplies: %d\n", ret);
552                 goto err_regulator_get;
553         }
554
555         ret = wm8731_reset(codec);
556         if (ret < 0) {
557                 dev_err(codec->dev, "Failed to issue reset: %d\n", ret);
558                 goto err_regulator_enable;
559         }
560
561         wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
562
563         /* Latch the update bits */
564         snd_soc_update_bits(codec, WM8731_LOUT1V, 0x100, 0);
565         snd_soc_update_bits(codec, WM8731_ROUT1V, 0x100, 0);
566         snd_soc_update_bits(codec, WM8731_LINVOL, 0x100, 0);
567         snd_soc_update_bits(codec, WM8731_RINVOL, 0x100, 0);
568
569         /* Disable bypass path by default */
570         snd_soc_update_bits(codec, WM8731_APANA, 0x8, 0);
571
572         /* Regulators will have been enabled by bias management */
573         regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies);
574
575         return 0;
576
577 err_regulator_enable:
578         regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies);
579 err_regulator_get:
580         regulator_bulk_free(ARRAY_SIZE(wm8731->supplies), wm8731->supplies);
581
582         return ret;
583 }
584
585 /* power down chip */
586 static int wm8731_remove(struct snd_soc_codec *codec)
587 {
588         struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
589
590         wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF);
591
592         regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies);
593         regulator_bulk_free(ARRAY_SIZE(wm8731->supplies), wm8731->supplies);
594
595         return 0;
596 }
597
598 static struct snd_soc_codec_driver soc_codec_dev_wm8731 = {
599         .probe =        wm8731_probe,
600         .remove =       wm8731_remove,
601         .suspend =      wm8731_suspend,
602         .resume =       wm8731_resume,
603         .set_bias_level = wm8731_set_bias_level,
604         .dapm_widgets = wm8731_dapm_widgets,
605         .num_dapm_widgets = ARRAY_SIZE(wm8731_dapm_widgets),
606         .dapm_routes = wm8731_intercon,
607         .num_dapm_routes = ARRAY_SIZE(wm8731_intercon),
608         .controls =     wm8731_snd_controls,
609         .num_controls = ARRAY_SIZE(wm8731_snd_controls),
610 };
611
612 static const struct of_device_id wm8731_of_match[] = {
613         { .compatible = "wlf,wm8731", },
614         { }
615 };
616
617 MODULE_DEVICE_TABLE(of, wm8731_of_match);
618
619 static const struct regmap_config wm8731_regmap = {
620         .reg_bits = 7,
621         .val_bits = 9,
622
623         .max_register = WM8731_RESET,
624         .volatile_reg = wm8731_volatile,
625         .writeable_reg = wm8731_writeable,
626
627         .cache_type = REGCACHE_RBTREE,
628         .reg_defaults = wm8731_reg_defaults,
629         .num_reg_defaults = ARRAY_SIZE(wm8731_reg_defaults),
630 };
631
632 #if defined(CONFIG_SPI_MASTER)
633 static int __devinit wm8731_spi_probe(struct spi_device *spi)
634 {
635         struct wm8731_priv *wm8731;
636         int ret;
637
638         wm8731 = devm_kzalloc(&spi->dev, sizeof(struct wm8731_priv),
639                               GFP_KERNEL);
640         if (wm8731 == NULL)
641                 return -ENOMEM;
642
643         wm8731->regmap = devm_regmap_init_spi(spi, &wm8731_regmap);
644         if (IS_ERR(wm8731->regmap)) {
645                 ret = PTR_ERR(wm8731->regmap);
646                 dev_err(&spi->dev, "Failed to allocate register map: %d\n",
647                         ret);
648                 return ret;
649         }
650
651         spi_set_drvdata(spi, wm8731);
652
653         ret = snd_soc_register_codec(&spi->dev,
654                         &soc_codec_dev_wm8731, &wm8731_dai, 1);
655         if (ret != 0) {
656                 dev_err(&spi->dev, "Failed to register CODEC: %d\n", ret);
657                 return ret;
658         }
659
660         return 0;
661 }
662
663 static int __devexit wm8731_spi_remove(struct spi_device *spi)
664 {
665         snd_soc_unregister_codec(&spi->dev);
666         return 0;
667 }
668
669 static struct spi_driver wm8731_spi_driver = {
670         .driver = {
671                 .name   = "wm8731",
672                 .owner  = THIS_MODULE,
673                 .of_match_table = wm8731_of_match,
674         },
675         .probe          = wm8731_spi_probe,
676         .remove         = __devexit_p(wm8731_spi_remove),
677 };
678 #endif /* CONFIG_SPI_MASTER */
679
680 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
681 static __devinit int wm8731_i2c_probe(struct i2c_client *i2c,
682                                       const struct i2c_device_id *id)
683 {
684         struct wm8731_priv *wm8731;
685         int ret;
686
687         wm8731 = devm_kzalloc(&i2c->dev, sizeof(struct wm8731_priv),
688                               GFP_KERNEL);
689         if (wm8731 == NULL)
690                 return -ENOMEM;
691
692         wm8731->regmap = devm_regmap_init_i2c(i2c, &wm8731_regmap);
693         if (IS_ERR(wm8731->regmap)) {
694                 ret = PTR_ERR(wm8731->regmap);
695                 dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
696                         ret);
697                 return ret;
698         }
699
700         i2c_set_clientdata(i2c, wm8731);
701
702         ret = snd_soc_register_codec(&i2c->dev,
703                         &soc_codec_dev_wm8731, &wm8731_dai, 1);
704         if (ret != 0) {
705                 dev_err(&i2c->dev, "Failed to register CODEC: %d\n", ret);
706                 return ret;
707         }
708
709         return 0;
710 }
711
712 static __devexit int wm8731_i2c_remove(struct i2c_client *client)
713 {
714         snd_soc_unregister_codec(&client->dev);
715         return 0;
716 }
717
718 static const struct i2c_device_id wm8731_i2c_id[] = {
719         { "wm8731", 0 },
720         { }
721 };
722 MODULE_DEVICE_TABLE(i2c, wm8731_i2c_id);
723
724 static struct i2c_driver wm8731_i2c_driver = {
725         .driver = {
726                 .name = "wm8731",
727                 .owner = THIS_MODULE,
728                 .of_match_table = wm8731_of_match,
729         },
730         .probe =    wm8731_i2c_probe,
731         .remove =   __devexit_p(wm8731_i2c_remove),
732         .id_table = wm8731_i2c_id,
733 };
734 #endif
735
736 static int __init wm8731_modinit(void)
737 {
738         int ret = 0;
739 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
740         ret = i2c_add_driver(&wm8731_i2c_driver);
741         if (ret != 0) {
742                 printk(KERN_ERR "Failed to register WM8731 I2C driver: %d\n",
743                        ret);
744         }
745 #endif
746 #if defined(CONFIG_SPI_MASTER)
747         ret = spi_register_driver(&wm8731_spi_driver);
748         if (ret != 0) {
749                 printk(KERN_ERR "Failed to register WM8731 SPI driver: %d\n",
750                        ret);
751         }
752 #endif
753         return ret;
754 }
755 module_init(wm8731_modinit);
756
757 static void __exit wm8731_exit(void)
758 {
759 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
760         i2c_del_driver(&wm8731_i2c_driver);
761 #endif
762 #if defined(CONFIG_SPI_MASTER)
763         spi_unregister_driver(&wm8731_spi_driver);
764 #endif
765 }
766 module_exit(wm8731_exit);
767
768 MODULE_DESCRIPTION("ASoC WM8731 driver");
769 MODULE_AUTHOR("Richard Purdie");
770 MODULE_LICENSE("GPL");