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