]> git.karo-electronics.de Git - linux-beck.git/blob - sound/soc/codecs/wm8731.c
7da360ee1fee5364103f646db1b76717d767b05a
[linux-beck.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/platform_device.h>
23 #include <linux/regulator/consumer.h>
24 #include <linux/spi/spi.h>
25 #include <sound/core.h>
26 #include <sound/pcm.h>
27 #include <sound/pcm_params.h>
28 #include <sound/soc.h>
29 #include <sound/soc-dapm.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         enum snd_soc_control_type control_type;
46         void *control_data;
47         struct regulator_bulk_data supplies[WM8731_NUM_SUPPLIES];
48         u16 reg_cache[WM8731_CACHEREGNUM];
49         unsigned int sysclk;
50 };
51
52
53 /*
54  * wm8731 register cache
55  * We can't read the WM8731 register space when we are
56  * using 2 wire for device control, so we cache them instead.
57  * There is no point in caching the reset register
58  */
59 static const u16 wm8731_reg[WM8731_CACHEREGNUM] = {
60         0x0097, 0x0097, 0x0079, 0x0079,
61         0x000a, 0x0008, 0x009f, 0x000a,
62         0x0000, 0x0000
63 };
64
65 #define wm8731_reset(c) snd_soc_write(c, WM8731_RESET, 0)
66
67 static const char *wm8731_input_select[] = {"Line In", "Mic"};
68 static const char *wm8731_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"};
69
70 static const struct soc_enum wm8731_enum[] = {
71         SOC_ENUM_SINGLE(WM8731_APANA, 2, 2, wm8731_input_select),
72         SOC_ENUM_SINGLE(WM8731_APDIGI, 1, 4, wm8731_deemph),
73 };
74
75 static const DECLARE_TLV_DB_SCALE(in_tlv, -3450, 150, 0);
76 static const DECLARE_TLV_DB_SCALE(sidetone_tlv, -1500, 300, 0);
77 static const DECLARE_TLV_DB_SCALE(out_tlv, -12100, 100, 1);
78
79 static const struct snd_kcontrol_new wm8731_snd_controls[] = {
80
81 SOC_DOUBLE_R_TLV("Master Playback Volume", WM8731_LOUT1V, WM8731_ROUT1V,
82                  0, 127, 0, out_tlv),
83 SOC_DOUBLE_R("Master Playback ZC Switch", WM8731_LOUT1V, WM8731_ROUT1V,
84         7, 1, 0),
85
86 SOC_DOUBLE_R_TLV("Capture Volume", WM8731_LINVOL, WM8731_RINVOL, 0, 31, 0,
87                  in_tlv),
88 SOC_DOUBLE_R("Line Capture Switch", WM8731_LINVOL, WM8731_RINVOL, 7, 1, 1),
89
90 SOC_SINGLE("Mic Boost (+20dB)", WM8731_APANA, 0, 1, 0),
91 SOC_SINGLE("Mic Capture Switch", WM8731_APANA, 1, 1, 1),
92
93 SOC_SINGLE_TLV("Sidetone Playback Volume", WM8731_APANA, 6, 3, 1,
94                sidetone_tlv),
95
96 SOC_SINGLE("ADC High Pass Filter Switch", WM8731_APDIGI, 0, 1, 1),
97 SOC_SINGLE("Store DC Offset Switch", WM8731_APDIGI, 4, 1, 0),
98
99 SOC_ENUM("Playback De-emphasis", wm8731_enum[1]),
100 };
101
102 /* Output Mixer */
103 static const struct snd_kcontrol_new wm8731_output_mixer_controls[] = {
104 SOC_DAPM_SINGLE("Line Bypass Switch", WM8731_APANA, 3, 1, 0),
105 SOC_DAPM_SINGLE("Mic Sidetone Switch", WM8731_APANA, 5, 1, 0),
106 SOC_DAPM_SINGLE("HiFi Playback Switch", WM8731_APANA, 4, 1, 0),
107 };
108
109 /* Input mux */
110 static const struct snd_kcontrol_new wm8731_input_mux_controls =
111 SOC_DAPM_ENUM("Input Select", wm8731_enum[0]);
112
113 static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
114 SND_SOC_DAPM_MIXER("Output Mixer", WM8731_PWR, 4, 1,
115         &wm8731_output_mixer_controls[0],
116         ARRAY_SIZE(wm8731_output_mixer_controls)),
117 SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8731_PWR, 3, 1),
118 SND_SOC_DAPM_OUTPUT("LOUT"),
119 SND_SOC_DAPM_OUTPUT("LHPOUT"),
120 SND_SOC_DAPM_OUTPUT("ROUT"),
121 SND_SOC_DAPM_OUTPUT("RHPOUT"),
122 SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8731_PWR, 2, 1),
123 SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &wm8731_input_mux_controls),
124 SND_SOC_DAPM_PGA("Line Input", WM8731_PWR, 0, 1, NULL, 0),
125 SND_SOC_DAPM_MICBIAS("Mic Bias", WM8731_PWR, 1, 1),
126 SND_SOC_DAPM_INPUT("MICIN"),
127 SND_SOC_DAPM_INPUT("RLINEIN"),
128 SND_SOC_DAPM_INPUT("LLINEIN"),
129 };
130
131 static const struct snd_soc_dapm_route intercon[] = {
132         /* output mixer */
133         {"Output Mixer", "Line Bypass Switch", "Line Input"},
134         {"Output Mixer", "HiFi Playback Switch", "DAC"},
135         {"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
136
137         /* outputs */
138         {"RHPOUT", NULL, "Output Mixer"},
139         {"ROUT", NULL, "Output Mixer"},
140         {"LHPOUT", NULL, "Output Mixer"},
141         {"LOUT", NULL, "Output Mixer"},
142
143         /* input mux */
144         {"Input Mux", "Line In", "Line Input"},
145         {"Input Mux", "Mic", "Mic Bias"},
146         {"ADC", NULL, "Input Mux"},
147
148         /* inputs */
149         {"Line Input", NULL, "LLINEIN"},
150         {"Line Input", NULL, "RLINEIN"},
151         {"Mic Bias", NULL, "MICIN"},
152 };
153
154 static int wm8731_add_widgets(struct snd_soc_codec *codec)
155 {
156         snd_soc_dapm_new_controls(codec, wm8731_dapm_widgets,
157                                   ARRAY_SIZE(wm8731_dapm_widgets));
158
159         snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon));
160
161         return 0;
162 }
163
164 struct _coeff_div {
165         u32 mclk;
166         u32 rate;
167         u16 fs;
168         u8 sr:4;
169         u8 bosr:1;
170         u8 usb:1;
171 };
172
173 /* codec mclk clock divider coefficients */
174 static const struct _coeff_div coeff_div[] = {
175         /* 48k */
176         {12288000, 48000, 256, 0x0, 0x0, 0x0},
177         {18432000, 48000, 384, 0x0, 0x1, 0x0},
178         {12000000, 48000, 250, 0x0, 0x0, 0x1},
179
180         /* 32k */
181         {12288000, 32000, 384, 0x6, 0x0, 0x0},
182         {18432000, 32000, 576, 0x6, 0x1, 0x0},
183         {12000000, 32000, 375, 0x6, 0x0, 0x1},
184
185         /* 8k */
186         {12288000, 8000, 1536, 0x3, 0x0, 0x0},
187         {18432000, 8000, 2304, 0x3, 0x1, 0x0},
188         {11289600, 8000, 1408, 0xb, 0x0, 0x0},
189         {16934400, 8000, 2112, 0xb, 0x1, 0x0},
190         {12000000, 8000, 1500, 0x3, 0x0, 0x1},
191
192         /* 96k */
193         {12288000, 96000, 128, 0x7, 0x0, 0x0},
194         {18432000, 96000, 192, 0x7, 0x1, 0x0},
195         {12000000, 96000, 125, 0x7, 0x0, 0x1},
196
197         /* 44.1k */
198         {11289600, 44100, 256, 0x8, 0x0, 0x0},
199         {16934400, 44100, 384, 0x8, 0x1, 0x0},
200         {12000000, 44100, 272, 0x8, 0x1, 0x1},
201
202         /* 88.2k */
203         {11289600, 88200, 128, 0xf, 0x0, 0x0},
204         {16934400, 88200, 192, 0xf, 0x1, 0x0},
205         {12000000, 88200, 136, 0xf, 0x1, 0x1},
206 };
207
208 static inline int get_coeff(int mclk, int rate)
209 {
210         int i;
211
212         for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
213                 if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
214                         return i;
215         }
216         return 0;
217 }
218
219 static int wm8731_hw_params(struct snd_pcm_substream *substream,
220                             struct snd_pcm_hw_params *params,
221                             struct snd_soc_dai *dai)
222 {
223         struct snd_soc_codec *codec = dai->codec;
224         struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
225         u16 iface = snd_soc_read(codec, WM8731_IFACE) & 0xfff3;
226         int i = get_coeff(wm8731->sysclk, params_rate(params));
227         u16 srate = (coeff_div[i].sr << 2) |
228                 (coeff_div[i].bosr << 1) | coeff_div[i].usb;
229
230         snd_soc_write(codec, WM8731_SRATE, srate);
231
232         /* bit size */
233         switch (params_format(params)) {
234         case SNDRV_PCM_FORMAT_S16_LE:
235                 break;
236         case SNDRV_PCM_FORMAT_S20_3LE:
237                 iface |= 0x0004;
238                 break;
239         case SNDRV_PCM_FORMAT_S24_LE:
240                 iface |= 0x0008;
241                 break;
242         }
243
244         snd_soc_write(codec, WM8731_IFACE, iface);
245         return 0;
246 }
247
248 static int wm8731_pcm_prepare(struct snd_pcm_substream *substream,
249                               struct snd_soc_dai *dai)
250 {
251         struct snd_soc_codec *codec = dai->codec;
252
253         /* set active */
254         snd_soc_write(codec, WM8731_ACTIVE, 0x0001);
255
256         return 0;
257 }
258
259 static void wm8731_shutdown(struct snd_pcm_substream *substream,
260                             struct snd_soc_dai *dai)
261 {
262         struct snd_soc_codec *codec = dai->codec;
263
264         /* deactivate */
265         if (!codec->active) {
266                 udelay(50);
267                 snd_soc_write(codec, WM8731_ACTIVE, 0x0);
268         }
269 }
270
271 static int wm8731_mute(struct snd_soc_dai *dai, int mute)
272 {
273         struct snd_soc_codec *codec = dai->codec;
274         u16 mute_reg = snd_soc_read(codec, WM8731_APDIGI) & 0xfff7;
275
276         if (mute)
277                 snd_soc_write(codec, WM8731_APDIGI, mute_reg | 0x8);
278         else
279                 snd_soc_write(codec, WM8731_APDIGI, mute_reg);
280         return 0;
281 }
282
283 static int wm8731_set_dai_sysclk(struct snd_soc_dai *codec_dai,
284                 int clk_id, unsigned int freq, int dir)
285 {
286         struct snd_soc_codec *codec = codec_dai->codec;
287         struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
288
289         switch (freq) {
290         case 11289600:
291         case 12000000:
292         case 12288000:
293         case 16934400:
294         case 18432000:
295                 wm8731->sysclk = freq;
296                 return 0;
297         }
298         return -EINVAL;
299 }
300
301
302 static int wm8731_set_dai_fmt(struct snd_soc_dai *codec_dai,
303                 unsigned int fmt)
304 {
305         struct snd_soc_codec *codec = codec_dai->codec;
306         u16 iface = 0;
307
308         /* set master/slave audio interface */
309         switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
310         case SND_SOC_DAIFMT_CBM_CFM:
311                 iface |= 0x0040;
312                 break;
313         case SND_SOC_DAIFMT_CBS_CFS:
314                 break;
315         default:
316                 return -EINVAL;
317         }
318
319         /* interface format */
320         switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
321         case SND_SOC_DAIFMT_I2S:
322                 iface |= 0x0002;
323                 break;
324         case SND_SOC_DAIFMT_RIGHT_J:
325                 break;
326         case SND_SOC_DAIFMT_LEFT_J:
327                 iface |= 0x0001;
328                 break;
329         case SND_SOC_DAIFMT_DSP_A:
330                 iface |= 0x0003;
331                 break;
332         case SND_SOC_DAIFMT_DSP_B:
333                 iface |= 0x0013;
334                 break;
335         default:
336                 return -EINVAL;
337         }
338
339         /* clock inversion */
340         switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
341         case SND_SOC_DAIFMT_NB_NF:
342                 break;
343         case SND_SOC_DAIFMT_IB_IF:
344                 iface |= 0x0090;
345                 break;
346         case SND_SOC_DAIFMT_IB_NF:
347                 iface |= 0x0080;
348                 break;
349         case SND_SOC_DAIFMT_NB_IF:
350                 iface |= 0x0010;
351                 break;
352         default:
353                 return -EINVAL;
354         }
355
356         /* set iface */
357         snd_soc_write(codec, WM8731_IFACE, iface);
358         return 0;
359 }
360
361 static int wm8731_set_bias_level(struct snd_soc_codec *codec,
362                                  enum snd_soc_bias_level level)
363 {
364         struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
365         int i, ret;
366         u8 data[2];
367         u16 *cache = codec->reg_cache;
368         u16 reg;
369
370         switch (level) {
371         case SND_SOC_BIAS_ON:
372                 break;
373         case SND_SOC_BIAS_PREPARE:
374                 break;
375         case SND_SOC_BIAS_STANDBY:
376                 if (codec->bias_level == SND_SOC_BIAS_OFF) {
377                         ret = regulator_bulk_enable(ARRAY_SIZE(wm8731->supplies),
378                                                     wm8731->supplies);
379                         if (ret != 0)
380                                 return ret;
381
382                         /* Sync reg_cache with the hardware */
383                         for (i = 0; i < ARRAY_SIZE(wm8731_reg); i++) {
384                                 if (cache[i] == wm8731_reg[i])
385                                         continue;
386
387                                 data[0] = (i << 1) | ((cache[i] >> 8)
388                                                       & 0x0001);
389                                 data[1] = cache[i] & 0x00ff;
390                                 codec->hw_write(codec->control_data, data, 2);
391                         }
392                 }
393
394                 /* Clear PWROFF, gate CLKOUT, everything else as-is */
395                 reg = snd_soc_read(codec, WM8731_PWR) & 0xff7f;
396                 snd_soc_write(codec, WM8731_PWR, reg | 0x0040);
397                 break;
398         case SND_SOC_BIAS_OFF:
399                 snd_soc_write(codec, WM8731_ACTIVE, 0x0);
400                 snd_soc_write(codec, WM8731_PWR, 0xffff);
401                 regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies),
402                                        wm8731->supplies);
403                 break;
404         }
405         codec->bias_level = level;
406         return 0;
407 }
408
409 #define WM8731_RATES SNDRV_PCM_RATE_8000_96000
410
411 #define WM8731_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
412         SNDRV_PCM_FMTBIT_S24_LE)
413
414 static struct snd_soc_dai_ops wm8731_dai_ops = {
415         .prepare        = wm8731_pcm_prepare,
416         .hw_params      = wm8731_hw_params,
417         .shutdown       = wm8731_shutdown,
418         .digital_mute   = wm8731_mute,
419         .set_sysclk     = wm8731_set_dai_sysclk,
420         .set_fmt        = wm8731_set_dai_fmt,
421 };
422
423 static struct snd_soc_dai_driver wm8731_dai = {
424         .name = "wm8731-hifi",
425         .playback = {
426                 .stream_name = "Playback",
427                 .channels_min = 1,
428                 .channels_max = 2,
429                 .rates = WM8731_RATES,
430                 .formats = WM8731_FORMATS,},
431         .capture = {
432                 .stream_name = "Capture",
433                 .channels_min = 1,
434                 .channels_max = 2,
435                 .rates = WM8731_RATES,
436                 .formats = WM8731_FORMATS,},
437         .ops = &wm8731_dai_ops,
438         .symmetric_rates = 1,
439 };
440
441 #ifdef CONFIG_PM
442 static int wm8731_suspend(struct snd_soc_codec *codec, pm_message_t state)
443 {
444         wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF);
445
446         return 0;
447 }
448
449 static int wm8731_resume(struct snd_soc_codec *codec)
450 {
451         wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
452
453         return 0;
454 }
455 #else
456 #define wm8731_suspend NULL
457 #define wm8731_resume NULL
458 #endif
459
460 static int wm8731_probe(struct snd_soc_codec *codec)
461 {
462         struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
463         int ret = 0, i;
464
465         codec->bias_level = SND_SOC_BIAS_OFF,
466         codec->control_data = wm8731->control_data;
467
468         ret = snd_soc_codec_set_cache_io(codec, 7, 9, wm8731->control_type);
469         if (ret < 0) {
470                 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
471                 return ret;
472         }
473
474         for (i = 0; i < ARRAY_SIZE(wm8731->supplies); i++)
475                 wm8731->supplies[i].supply = wm8731_supply_names[i];
476
477         ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(wm8731->supplies),
478                                  wm8731->supplies);
479         if (ret != 0) {
480                 dev_err(codec->dev, "Failed to request supplies: %d\n", ret);
481                 return ret;
482         }
483
484         ret = regulator_bulk_enable(ARRAY_SIZE(wm8731->supplies),
485                                     wm8731->supplies);
486         if (ret != 0) {
487                 dev_err(codec->dev, "Failed to enable supplies: %d\n", ret);
488                 goto err_regulator_get;
489         }
490
491         ret = wm8731_reset(codec);
492         if (ret < 0) {
493                 dev_err(codec->dev, "Failed to issue reset: %d\n", ret);
494                 goto err_regulator_enable;
495         }
496
497         wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
498
499         /* Latch the update bits */
500         snd_soc_update_bits(codec, WM8731_LOUT1V, 0x100, 0);
501         snd_soc_update_bits(codec, WM8731_ROUT1V, 0x100, 0);
502         snd_soc_update_bits(codec, WM8731_LINVOL, 0x100, 0);
503         snd_soc_update_bits(codec, WM8731_RINVOL, 0x100, 0);
504
505         /* Disable bypass path by default */
506         snd_soc_update_bits(codec, WM8731_APANA, 0x4, 0);
507
508         snd_soc_add_controls(codec, wm8731_snd_controls,
509                              ARRAY_SIZE(wm8731_snd_controls));
510         wm8731_add_widgets(codec);
511
512         /* Regulators will have been enabled by bias management */
513         regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies);
514
515         return 0;
516
517 err_regulator_enable:
518         regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies);
519 err_regulator_get:
520         regulator_bulk_free(ARRAY_SIZE(wm8731->supplies), wm8731->supplies);
521
522         kfree(wm8731);
523         return ret;
524 }
525
526 /* power down chip */
527 static int wm8731_remove(struct snd_soc_codec *codec)
528 {
529         struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
530
531         wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF);
532
533         regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies);
534         regulator_bulk_free(ARRAY_SIZE(wm8731->supplies), wm8731->supplies);
535
536         return 0;
537 }
538
539 static struct snd_soc_codec_driver soc_codec_dev_wm8731 = {
540         .probe =        wm8731_probe,
541         .remove =       wm8731_remove,
542         .suspend =      wm8731_suspend,
543         .resume =       wm8731_resume,
544         .set_bias_level = wm8731_set_bias_level,
545         .reg_cache_size = sizeof(wm8731_reg),
546         .reg_word_size = sizeof(u16),
547         .reg_cache_default = wm8731_reg,
548 };
549
550 #if defined(CONFIG_SPI_MASTER)
551 static int __devinit wm8731_spi_probe(struct spi_device *spi)
552 {
553         struct wm8731_priv *wm8731;
554         int ret;
555
556         wm8731 = kzalloc(sizeof(struct wm8731_priv), GFP_KERNEL);
557         if (wm8731 == NULL)
558                 return -ENOMEM;
559
560         wm8731->control_data = spi;
561         wm8731->control_type = SND_SOC_SPI;
562         spi_set_drvdata(spi, wm8731);
563
564         ret = snd_soc_register_codec(&spi->dev,
565                         &soc_codec_dev_wm8731, &wm8731_dai, 1);
566         if (ret < 0)
567                 kfree(wm8731);
568         return ret;
569 }
570
571 static int __devexit wm8731_spi_remove(struct spi_device *spi)
572 {
573         snd_soc_unregister_codec(&spi->dev);
574         kfree(spi_get_drvdata(spi));
575         return 0;
576 }
577
578 static struct spi_driver wm8731_spi_driver = {
579         .driver = {
580                 .name   = "wm8731-codec",
581                 .bus    = &spi_bus_type,
582                 .owner  = THIS_MODULE,
583         },
584         .probe          = wm8731_spi_probe,
585         .remove         = __devexit_p(wm8731_spi_remove),
586 };
587 #endif /* CONFIG_SPI_MASTER */
588
589 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
590 static __devinit int wm8731_i2c_probe(struct i2c_client *i2c,
591                                       const struct i2c_device_id *id)
592 {
593         struct wm8731_priv *wm8731;
594         int ret;
595
596         wm8731 = kzalloc(sizeof(struct wm8731_priv), GFP_KERNEL);
597         if (wm8731 == NULL)
598                 return -ENOMEM;
599
600         i2c_set_clientdata(i2c, wm8731);
601         wm8731->control_data = i2c;
602         wm8731->control_type = SND_SOC_I2C;
603
604         ret =  snd_soc_register_codec(&i2c->dev,
605                         &soc_codec_dev_wm8731, &wm8731_dai, 1);
606         if (ret < 0)
607                 kfree(wm8731);
608         return ret;
609 }
610
611 static __devexit int wm8731_i2c_remove(struct i2c_client *client)
612 {
613         snd_soc_unregister_codec(&client->dev);
614         kfree(i2c_get_clientdata(client));
615         return 0;
616 }
617
618 static const struct i2c_device_id wm8731_i2c_id[] = {
619         { "wm8731", 0 },
620         { }
621 };
622 MODULE_DEVICE_TABLE(i2c, wm8731_i2c_id);
623
624 static struct i2c_driver wm8731_i2c_driver = {
625         .driver = {
626                 .name = "wm8731-codec",
627                 .owner = THIS_MODULE,
628         },
629         .probe =    wm8731_i2c_probe,
630         .remove =   __devexit_p(wm8731_i2c_remove),
631         .id_table = wm8731_i2c_id,
632 };
633 #endif
634
635 static int __init wm8731_modinit(void)
636 {
637         int ret = 0;
638 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
639         ret = i2c_add_driver(&wm8731_i2c_driver);
640         if (ret != 0) {
641                 printk(KERN_ERR "Failed to register WM8731 I2C driver: %d\n",
642                        ret);
643         }
644 #endif
645 #if defined(CONFIG_SPI_MASTER)
646         ret = spi_register_driver(&wm8731_spi_driver);
647         if (ret != 0) {
648                 printk(KERN_ERR "Failed to register WM8731 SPI driver: %d\n",
649                        ret);
650         }
651 #endif
652         return ret;
653 }
654 module_init(wm8731_modinit);
655
656 static void __exit wm8731_exit(void)
657 {
658 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
659         i2c_del_driver(&wm8731_i2c_driver);
660 #endif
661 #if defined(CONFIG_SPI_MASTER)
662         spi_unregister_driver(&wm8731_spi_driver);
663 #endif
664 }
665 module_exit(wm8731_exit);
666
667 MODULE_DESCRIPTION("ASoC WM8731 driver");
668 MODULE_AUTHOR("Richard Purdie");
669 MODULE_LICENSE("GPL");