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