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