Merge branches 'pm-cpufreq', 'pm-cpuidle', 'pm-devfreq', 'pm-opp' and 'pm-tools'
[linux-drm-fsl-dcu.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 wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(source->codec);
221
222         return wm8731->sysclk_type == WM8731_SYSCLK_XTAL;
223 }
224
225 static const struct snd_soc_dapm_route wm8731_intercon[] = {
226         {"DAC", NULL, "OSC", wm8731_check_osc},
227         {"ADC", NULL, "OSC", wm8731_check_osc},
228         {"DAC", NULL, "ACTIVE"},
229         {"ADC", NULL, "ACTIVE"},
230
231         /* output mixer */
232         {"Output Mixer", "Line Bypass Switch", "Line Input"},
233         {"Output Mixer", "HiFi Playback Switch", "DAC"},
234         {"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
235
236         /* outputs */
237         {"RHPOUT", NULL, "Output Mixer"},
238         {"ROUT", NULL, "Output Mixer"},
239         {"LHPOUT", NULL, "Output Mixer"},
240         {"LOUT", NULL, "Output Mixer"},
241
242         /* input mux */
243         {"Input Mux", "Line In", "Line Input"},
244         {"Input Mux", "Mic", "Mic Bias"},
245         {"ADC", NULL, "Input Mux"},
246
247         /* inputs */
248         {"Line Input", NULL, "LLINEIN"},
249         {"Line Input", NULL, "RLINEIN"},
250         {"Mic Bias", NULL, "MICIN"},
251 };
252
253 struct _coeff_div {
254         u32 mclk;
255         u32 rate;
256         u16 fs;
257         u8 sr:4;
258         u8 bosr:1;
259         u8 usb:1;
260 };
261
262 /* codec mclk clock divider coefficients */
263 static const struct _coeff_div coeff_div[] = {
264         /* 48k */
265         {12288000, 48000, 256, 0x0, 0x0, 0x0},
266         {18432000, 48000, 384, 0x0, 0x1, 0x0},
267         {12000000, 48000, 250, 0x0, 0x0, 0x1},
268
269         /* 32k */
270         {12288000, 32000, 384, 0x6, 0x0, 0x0},
271         {18432000, 32000, 576, 0x6, 0x1, 0x0},
272         {12000000, 32000, 375, 0x6, 0x0, 0x1},
273
274         /* 8k */
275         {12288000, 8000, 1536, 0x3, 0x0, 0x0},
276         {18432000, 8000, 2304, 0x3, 0x1, 0x0},
277         {11289600, 8000, 1408, 0xb, 0x0, 0x0},
278         {16934400, 8000, 2112, 0xb, 0x1, 0x0},
279         {12000000, 8000, 1500, 0x3, 0x0, 0x1},
280
281         /* 96k */
282         {12288000, 96000, 128, 0x7, 0x0, 0x0},
283         {18432000, 96000, 192, 0x7, 0x1, 0x0},
284         {12000000, 96000, 125, 0x7, 0x0, 0x1},
285
286         /* 44.1k */
287         {11289600, 44100, 256, 0x8, 0x0, 0x0},
288         {16934400, 44100, 384, 0x8, 0x1, 0x0},
289         {12000000, 44100, 272, 0x8, 0x1, 0x1},
290
291         /* 88.2k */
292         {11289600, 88200, 128, 0xf, 0x0, 0x0},
293         {16934400, 88200, 192, 0xf, 0x1, 0x0},
294         {12000000, 88200, 136, 0xf, 0x1, 0x1},
295 };
296
297 /* rates constraints */
298 static const unsigned int wm8731_rates_12000000[] = {
299         8000, 32000, 44100, 48000, 96000, 88200,
300 };
301
302 static const unsigned int wm8731_rates_12288000_18432000[] = {
303         8000, 32000, 48000, 96000,
304 };
305
306 static const unsigned int wm8731_rates_11289600_16934400[] = {
307         8000, 44100, 88200,
308 };
309
310 static const struct snd_pcm_hw_constraint_list wm8731_constraints_12000000 = {
311         .list = wm8731_rates_12000000,
312         .count = ARRAY_SIZE(wm8731_rates_12000000),
313 };
314
315 static const
316 struct snd_pcm_hw_constraint_list wm8731_constraints_12288000_18432000 = {
317         .list = wm8731_rates_12288000_18432000,
318         .count = ARRAY_SIZE(wm8731_rates_12288000_18432000),
319 };
320
321 static const
322 struct snd_pcm_hw_constraint_list wm8731_constraints_11289600_16934400 = {
323         .list = wm8731_rates_11289600_16934400,
324         .count = ARRAY_SIZE(wm8731_rates_11289600_16934400),
325 };
326
327 static inline int get_coeff(int mclk, int rate)
328 {
329         int i;
330
331         for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
332                 if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
333                         return i;
334         }
335         return 0;
336 }
337
338 static int wm8731_hw_params(struct snd_pcm_substream *substream,
339                             struct snd_pcm_hw_params *params,
340                             struct snd_soc_dai *dai)
341 {
342         struct snd_soc_codec *codec = dai->codec;
343         struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
344         u16 iface = snd_soc_read(codec, WM8731_IFACE) & 0xfff3;
345         int i = get_coeff(wm8731->sysclk, params_rate(params));
346         u16 srate = (coeff_div[i].sr << 2) |
347                 (coeff_div[i].bosr << 1) | coeff_div[i].usb;
348
349         wm8731->playback_fs = params_rate(params);
350
351         snd_soc_write(codec, WM8731_SRATE, srate);
352
353         /* bit size */
354         switch (params_width(params)) {
355         case 16:
356                 break;
357         case 20:
358                 iface |= 0x0004;
359                 break;
360         case 24:
361                 iface |= 0x0008;
362                 break;
363         }
364
365         wm8731_set_deemph(codec);
366
367         snd_soc_write(codec, WM8731_IFACE, iface);
368         return 0;
369 }
370
371 static int wm8731_mute(struct snd_soc_dai *dai, int mute)
372 {
373         struct snd_soc_codec *codec = dai->codec;
374         u16 mute_reg = snd_soc_read(codec, WM8731_APDIGI) & 0xfff7;
375
376         if (mute)
377                 snd_soc_write(codec, WM8731_APDIGI, mute_reg | 0x8);
378         else
379                 snd_soc_write(codec, WM8731_APDIGI, mute_reg);
380         return 0;
381 }
382
383 static int wm8731_set_dai_sysclk(struct snd_soc_dai *codec_dai,
384                 int clk_id, unsigned int freq, int dir)
385 {
386         struct snd_soc_codec *codec = codec_dai->codec;
387         struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
388
389         switch (clk_id) {
390         case WM8731_SYSCLK_XTAL:
391         case WM8731_SYSCLK_MCLK:
392                 wm8731->sysclk_type = clk_id;
393                 break;
394         default:
395                 return -EINVAL;
396         }
397
398         switch (freq) {
399         case 0:
400                 wm8731->constraints = NULL;
401                 break;
402         case 12000000:
403                 wm8731->constraints = &wm8731_constraints_12000000;
404                 break;
405         case 12288000:
406         case 18432000:
407                 wm8731->constraints = &wm8731_constraints_12288000_18432000;
408                 break;
409         case 16934400:
410         case 11289600:
411                 wm8731->constraints = &wm8731_constraints_11289600_16934400;
412                 break;
413         default:
414                 return -EINVAL;
415         }
416
417         wm8731->sysclk = freq;
418
419         snd_soc_dapm_sync(&codec->dapm);
420
421         return 0;
422 }
423
424
425 static int wm8731_set_dai_fmt(struct snd_soc_dai *codec_dai,
426                 unsigned int fmt)
427 {
428         struct snd_soc_codec *codec = codec_dai->codec;
429         u16 iface = 0;
430
431         /* set master/slave audio interface */
432         switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
433         case SND_SOC_DAIFMT_CBM_CFM:
434                 iface |= 0x0040;
435                 break;
436         case SND_SOC_DAIFMT_CBS_CFS:
437                 break;
438         default:
439                 return -EINVAL;
440         }
441
442         /* interface format */
443         switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
444         case SND_SOC_DAIFMT_I2S:
445                 iface |= 0x0002;
446                 break;
447         case SND_SOC_DAIFMT_RIGHT_J:
448                 break;
449         case SND_SOC_DAIFMT_LEFT_J:
450                 iface |= 0x0001;
451                 break;
452         case SND_SOC_DAIFMT_DSP_A:
453                 iface |= 0x0013;
454                 break;
455         case SND_SOC_DAIFMT_DSP_B:
456                 iface |= 0x0003;
457                 break;
458         default:
459                 return -EINVAL;
460         }
461
462         /* clock inversion */
463         switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
464         case SND_SOC_DAIFMT_NB_NF:
465                 break;
466         case SND_SOC_DAIFMT_IB_IF:
467                 iface |= 0x0090;
468                 break;
469         case SND_SOC_DAIFMT_IB_NF:
470                 iface |= 0x0080;
471                 break;
472         case SND_SOC_DAIFMT_NB_IF:
473                 iface |= 0x0010;
474                 break;
475         default:
476                 return -EINVAL;
477         }
478
479         /* set iface */
480         snd_soc_write(codec, WM8731_IFACE, iface);
481         return 0;
482 }
483
484 static int wm8731_set_bias_level(struct snd_soc_codec *codec,
485                                  enum snd_soc_bias_level level)
486 {
487         struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
488         int ret;
489         u16 reg;
490
491         switch (level) {
492         case SND_SOC_BIAS_ON:
493                 break;
494         case SND_SOC_BIAS_PREPARE:
495                 break;
496         case SND_SOC_BIAS_STANDBY:
497                 if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
498                         ret = regulator_bulk_enable(ARRAY_SIZE(wm8731->supplies),
499                                                     wm8731->supplies);
500                         if (ret != 0)
501                                 return ret;
502
503                         regcache_sync(wm8731->regmap);
504                 }
505
506                 /* Clear PWROFF, gate CLKOUT, everything else as-is */
507                 reg = snd_soc_read(codec, WM8731_PWR) & 0xff7f;
508                 snd_soc_write(codec, WM8731_PWR, reg | 0x0040);
509                 break;
510         case SND_SOC_BIAS_OFF:
511                 snd_soc_write(codec, WM8731_PWR, 0xffff);
512                 regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies),
513                                        wm8731->supplies);
514                 regcache_mark_dirty(wm8731->regmap);
515                 break;
516         }
517         codec->dapm.bias_level = level;
518         return 0;
519 }
520
521 static int wm8731_startup(struct snd_pcm_substream *substream,
522         struct snd_soc_dai *dai)
523 {
524         struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(dai->codec);
525
526         if (wm8731->constraints)
527                 snd_pcm_hw_constraint_list(substream->runtime, 0,
528                                            SNDRV_PCM_HW_PARAM_RATE,
529                                            wm8731->constraints);
530
531         return 0;
532 }
533
534 #define WM8731_RATES SNDRV_PCM_RATE_8000_96000
535
536 #define WM8731_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
537         SNDRV_PCM_FMTBIT_S24_LE)
538
539 static const struct snd_soc_dai_ops wm8731_dai_ops = {
540         .startup        = wm8731_startup,
541         .hw_params      = wm8731_hw_params,
542         .digital_mute   = wm8731_mute,
543         .set_sysclk     = wm8731_set_dai_sysclk,
544         .set_fmt        = wm8731_set_dai_fmt,
545 };
546
547 static struct snd_soc_dai_driver wm8731_dai = {
548         .name = "wm8731-hifi",
549         .playback = {
550                 .stream_name = "Playback",
551                 .channels_min = 1,
552                 .channels_max = 2,
553                 .rates = WM8731_RATES,
554                 .formats = WM8731_FORMATS,},
555         .capture = {
556                 .stream_name = "Capture",
557                 .channels_min = 1,
558                 .channels_max = 2,
559                 .rates = WM8731_RATES,
560                 .formats = WM8731_FORMATS,},
561         .ops = &wm8731_dai_ops,
562         .symmetric_rates = 1,
563 };
564
565 static int wm8731_probe(struct snd_soc_codec *codec)
566 {
567         struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
568         int ret = 0, i;
569
570         for (i = 0; i < ARRAY_SIZE(wm8731->supplies); i++)
571                 wm8731->supplies[i].supply = wm8731_supply_names[i];
572
573         ret = devm_regulator_bulk_get(codec->dev, ARRAY_SIZE(wm8731->supplies),
574                                  wm8731->supplies);
575         if (ret != 0) {
576                 dev_err(codec->dev, "Failed to request supplies: %d\n", ret);
577                 return ret;
578         }
579
580         ret = regulator_bulk_enable(ARRAY_SIZE(wm8731->supplies),
581                                     wm8731->supplies);
582         if (ret != 0) {
583                 dev_err(codec->dev, "Failed to enable supplies: %d\n", ret);
584                 return ret;
585         }
586
587         ret = wm8731_reset(codec);
588         if (ret < 0) {
589                 dev_err(codec->dev, "Failed to issue reset: %d\n", ret);
590                 goto err_regulator_enable;
591         }
592
593         wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
594
595         /* Latch the update bits */
596         snd_soc_update_bits(codec, WM8731_LOUT1V, 0x100, 0);
597         snd_soc_update_bits(codec, WM8731_ROUT1V, 0x100, 0);
598         snd_soc_update_bits(codec, WM8731_LINVOL, 0x100, 0);
599         snd_soc_update_bits(codec, WM8731_RINVOL, 0x100, 0);
600
601         /* Disable bypass path by default */
602         snd_soc_update_bits(codec, WM8731_APANA, 0x8, 0);
603
604         /* Regulators will have been enabled by bias management */
605         regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies);
606
607         return 0;
608
609 err_regulator_enable:
610         regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies);
611
612         return ret;
613 }
614
615 /* power down chip */
616 static int wm8731_remove(struct snd_soc_codec *codec)
617 {
618         struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
619
620         regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies);
621
622         return 0;
623 }
624
625 static struct snd_soc_codec_driver soc_codec_dev_wm8731 = {
626         .probe =        wm8731_probe,
627         .remove =       wm8731_remove,
628         .set_bias_level = wm8731_set_bias_level,
629         .suspend_bias_off = true,
630
631         .dapm_widgets = wm8731_dapm_widgets,
632         .num_dapm_widgets = ARRAY_SIZE(wm8731_dapm_widgets),
633         .dapm_routes = wm8731_intercon,
634         .num_dapm_routes = ARRAY_SIZE(wm8731_intercon),
635         .controls =     wm8731_snd_controls,
636         .num_controls = ARRAY_SIZE(wm8731_snd_controls),
637 };
638
639 static const struct of_device_id wm8731_of_match[] = {
640         { .compatible = "wlf,wm8731", },
641         { }
642 };
643
644 MODULE_DEVICE_TABLE(of, wm8731_of_match);
645
646 static const struct regmap_config wm8731_regmap = {
647         .reg_bits = 7,
648         .val_bits = 9,
649
650         .max_register = WM8731_RESET,
651         .volatile_reg = wm8731_volatile,
652         .writeable_reg = wm8731_writeable,
653
654         .cache_type = REGCACHE_RBTREE,
655         .reg_defaults = wm8731_reg_defaults,
656         .num_reg_defaults = ARRAY_SIZE(wm8731_reg_defaults),
657 };
658
659 #if defined(CONFIG_SPI_MASTER)
660 static int wm8731_spi_probe(struct spi_device *spi)
661 {
662         struct wm8731_priv *wm8731;
663         int ret;
664
665         wm8731 = devm_kzalloc(&spi->dev, sizeof(*wm8731), GFP_KERNEL);
666         if (wm8731 == NULL)
667                 return -ENOMEM;
668
669         mutex_init(&wm8731->lock);
670
671         wm8731->regmap = devm_regmap_init_spi(spi, &wm8731_regmap);
672         if (IS_ERR(wm8731->regmap)) {
673                 ret = PTR_ERR(wm8731->regmap);
674                 dev_err(&spi->dev, "Failed to allocate register map: %d\n",
675                         ret);
676                 return ret;
677         }
678
679         spi_set_drvdata(spi, wm8731);
680
681         ret = snd_soc_register_codec(&spi->dev,
682                         &soc_codec_dev_wm8731, &wm8731_dai, 1);
683         if (ret != 0) {
684                 dev_err(&spi->dev, "Failed to register CODEC: %d\n", ret);
685                 return ret;
686         }
687
688         return 0;
689 }
690
691 static int wm8731_spi_remove(struct spi_device *spi)
692 {
693         snd_soc_unregister_codec(&spi->dev);
694         return 0;
695 }
696
697 static struct spi_driver wm8731_spi_driver = {
698         .driver = {
699                 .name   = "wm8731",
700                 .owner  = THIS_MODULE,
701                 .of_match_table = wm8731_of_match,
702         },
703         .probe          = wm8731_spi_probe,
704         .remove         = wm8731_spi_remove,
705 };
706 #endif /* CONFIG_SPI_MASTER */
707
708 #if IS_ENABLED(CONFIG_I2C)
709 static int wm8731_i2c_probe(struct i2c_client *i2c,
710                             const struct i2c_device_id *id)
711 {
712         struct wm8731_priv *wm8731;
713         int ret;
714
715         wm8731 = devm_kzalloc(&i2c->dev, sizeof(struct wm8731_priv),
716                               GFP_KERNEL);
717         if (wm8731 == NULL)
718                 return -ENOMEM;
719
720         mutex_init(&wm8731->lock);
721
722         wm8731->regmap = devm_regmap_init_i2c(i2c, &wm8731_regmap);
723         if (IS_ERR(wm8731->regmap)) {
724                 ret = PTR_ERR(wm8731->regmap);
725                 dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
726                         ret);
727                 return ret;
728         }
729
730         i2c_set_clientdata(i2c, wm8731);
731
732         ret = snd_soc_register_codec(&i2c->dev,
733                         &soc_codec_dev_wm8731, &wm8731_dai, 1);
734         if (ret != 0) {
735                 dev_err(&i2c->dev, "Failed to register CODEC: %d\n", ret);
736                 return ret;
737         }
738
739         return 0;
740 }
741
742 static int wm8731_i2c_remove(struct i2c_client *client)
743 {
744         snd_soc_unregister_codec(&client->dev);
745         return 0;
746 }
747
748 static const struct i2c_device_id wm8731_i2c_id[] = {
749         { "wm8731", 0 },
750         { }
751 };
752 MODULE_DEVICE_TABLE(i2c, wm8731_i2c_id);
753
754 static struct i2c_driver wm8731_i2c_driver = {
755         .driver = {
756                 .name = "wm8731",
757                 .owner = THIS_MODULE,
758                 .of_match_table = wm8731_of_match,
759         },
760         .probe =    wm8731_i2c_probe,
761         .remove =   wm8731_i2c_remove,
762         .id_table = wm8731_i2c_id,
763 };
764 #endif
765
766 static int __init wm8731_modinit(void)
767 {
768         int ret = 0;
769 #if IS_ENABLED(CONFIG_I2C)
770         ret = i2c_add_driver(&wm8731_i2c_driver);
771         if (ret != 0) {
772                 printk(KERN_ERR "Failed to register WM8731 I2C driver: %d\n",
773                        ret);
774         }
775 #endif
776 #if defined(CONFIG_SPI_MASTER)
777         ret = spi_register_driver(&wm8731_spi_driver);
778         if (ret != 0) {
779                 printk(KERN_ERR "Failed to register WM8731 SPI driver: %d\n",
780                        ret);
781         }
782 #endif
783         return ret;
784 }
785 module_init(wm8731_modinit);
786
787 static void __exit wm8731_exit(void)
788 {
789 #if IS_ENABLED(CONFIG_I2C)
790         i2c_del_driver(&wm8731_i2c_driver);
791 #endif
792 #if defined(CONFIG_SPI_MASTER)
793         spi_unregister_driver(&wm8731_spi_driver);
794 #endif
795 }
796 module_exit(wm8731_exit);
797
798 MODULE_DESCRIPTION("ASoC WM8731 driver");
799 MODULE_AUTHOR("Richard Purdie");
800 MODULE_LICENSE("GPL");