xref: /linux/sound/soc/codecs/wm8731.c (revision 22d22ee5146ae823b1e93fe2887a7cba56015091)
140e0aa64SRichard Purdie /*
240e0aa64SRichard Purdie  * wm8731.c  --  WM8731 ALSA SoC Audio driver
340e0aa64SRichard Purdie  *
440e0aa64SRichard Purdie  * Copyright 2005 Openedhand Ltd.
540e0aa64SRichard Purdie  *
640e0aa64SRichard Purdie  * Author: Richard Purdie <richard@openedhand.com>
740e0aa64SRichard Purdie  *
840e0aa64SRichard Purdie  * Based on wm8753.c by Liam Girdwood
940e0aa64SRichard Purdie  *
1040e0aa64SRichard Purdie  * This program is free software; you can redistribute it and/or modify
1140e0aa64SRichard Purdie  * it under the terms of the GNU General Public License version 2 as
1240e0aa64SRichard Purdie  * published by the Free Software Foundation.
1340e0aa64SRichard Purdie  */
1440e0aa64SRichard Purdie 
1540e0aa64SRichard Purdie #include <linux/module.h>
1640e0aa64SRichard Purdie #include <linux/moduleparam.h>
1740e0aa64SRichard Purdie #include <linux/init.h>
1840e0aa64SRichard Purdie #include <linux/delay.h>
1940e0aa64SRichard Purdie #include <linux/pm.h>
2040e0aa64SRichard Purdie #include <linux/i2c.h>
2140e0aa64SRichard Purdie #include <linux/platform_device.h>
22d2a40355SCliff Cai #include <linux/spi/spi.h>
2340e0aa64SRichard Purdie #include <sound/core.h>
2440e0aa64SRichard Purdie #include <sound/pcm.h>
2540e0aa64SRichard Purdie #include <sound/pcm_params.h>
2640e0aa64SRichard Purdie #include <sound/soc.h>
2740e0aa64SRichard Purdie #include <sound/soc-dapm.h>
2840e0aa64SRichard Purdie #include <sound/initval.h>
2940e0aa64SRichard Purdie 
3040e0aa64SRichard Purdie #include "wm8731.h"
3140e0aa64SRichard Purdie 
3240e0aa64SRichard Purdie struct snd_soc_codec_device soc_codec_dev_wm8731;
3340e0aa64SRichard Purdie 
34b36d61d4SFrank Mandarino /* codec private data */
35b36d61d4SFrank Mandarino struct wm8731_priv {
36b36d61d4SFrank Mandarino 	unsigned int sysclk;
37b36d61d4SFrank Mandarino };
38b36d61d4SFrank Mandarino 
3940e0aa64SRichard Purdie /*
4040e0aa64SRichard Purdie  * wm8731 register cache
4140e0aa64SRichard Purdie  * We can't read the WM8731 register space when we are
4240e0aa64SRichard Purdie  * using 2 wire for device control, so we cache them instead.
4340e0aa64SRichard Purdie  * There is no point in caching the reset register
4440e0aa64SRichard Purdie  */
4540e0aa64SRichard Purdie static const u16 wm8731_reg[WM8731_CACHEREGNUM] = {
4640e0aa64SRichard Purdie     0x0097, 0x0097, 0x0079, 0x0079,
4740e0aa64SRichard Purdie     0x000a, 0x0008, 0x009f, 0x000a,
4840e0aa64SRichard Purdie     0x0000, 0x0000
4940e0aa64SRichard Purdie };
5040e0aa64SRichard Purdie 
5140e0aa64SRichard Purdie /*
5240e0aa64SRichard Purdie  * read wm8731 register cache
5340e0aa64SRichard Purdie  */
5440e0aa64SRichard Purdie static inline unsigned int wm8731_read_reg_cache(struct snd_soc_codec *codec,
5540e0aa64SRichard Purdie 	unsigned int reg)
5640e0aa64SRichard Purdie {
5740e0aa64SRichard Purdie 	u16 *cache = codec->reg_cache;
5840e0aa64SRichard Purdie 	if (reg == WM8731_RESET)
5940e0aa64SRichard Purdie 		return 0;
6040e0aa64SRichard Purdie 	if (reg >= WM8731_CACHEREGNUM)
6140e0aa64SRichard Purdie 		return -1;
6240e0aa64SRichard Purdie 	return cache[reg];
6340e0aa64SRichard Purdie }
6440e0aa64SRichard Purdie 
6540e0aa64SRichard Purdie /*
6640e0aa64SRichard Purdie  * write wm8731 register cache
6740e0aa64SRichard Purdie  */
6840e0aa64SRichard Purdie static inline void wm8731_write_reg_cache(struct snd_soc_codec *codec,
6940e0aa64SRichard Purdie 	u16 reg, unsigned int value)
7040e0aa64SRichard Purdie {
7140e0aa64SRichard Purdie 	u16 *cache = codec->reg_cache;
7240e0aa64SRichard Purdie 	if (reg >= WM8731_CACHEREGNUM)
7340e0aa64SRichard Purdie 		return;
7440e0aa64SRichard Purdie 	cache[reg] = value;
7540e0aa64SRichard Purdie }
7640e0aa64SRichard Purdie 
7740e0aa64SRichard Purdie /*
7840e0aa64SRichard Purdie  * write to the WM8731 register space
7940e0aa64SRichard Purdie  */
8040e0aa64SRichard Purdie static int wm8731_write(struct snd_soc_codec *codec, unsigned int reg,
8140e0aa64SRichard Purdie 	unsigned int value)
8240e0aa64SRichard Purdie {
8340e0aa64SRichard Purdie 	u8 data[2];
8440e0aa64SRichard Purdie 
8540e0aa64SRichard Purdie 	/* data is
8640e0aa64SRichard Purdie 	 *   D15..D9 WM8731 register offset
8740e0aa64SRichard Purdie 	 *   D8...D0 register data
8840e0aa64SRichard Purdie 	 */
8940e0aa64SRichard Purdie 	data[0] = (reg << 1) | ((value >> 8) & 0x0001);
9040e0aa64SRichard Purdie 	data[1] = value & 0x00ff;
9140e0aa64SRichard Purdie 
9240e0aa64SRichard Purdie 	wm8731_write_reg_cache(codec, reg, value);
9340e0aa64SRichard Purdie 	if (codec->hw_write(codec->control_data, data, 2) == 2)
9440e0aa64SRichard Purdie 		return 0;
9540e0aa64SRichard Purdie 	else
9640e0aa64SRichard Purdie 		return -EIO;
9740e0aa64SRichard Purdie }
9840e0aa64SRichard Purdie 
9940e0aa64SRichard Purdie #define wm8731_reset(c)	wm8731_write(c, WM8731_RESET, 0)
10040e0aa64SRichard Purdie 
10140e0aa64SRichard Purdie static const char *wm8731_input_select[] = {"Line In", "Mic"};
10240e0aa64SRichard Purdie static const char *wm8731_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"};
10340e0aa64SRichard Purdie 
10440e0aa64SRichard Purdie static const struct soc_enum wm8731_enum[] = {
10540e0aa64SRichard Purdie 	SOC_ENUM_SINGLE(WM8731_APANA, 2, 2, wm8731_input_select),
10640e0aa64SRichard Purdie 	SOC_ENUM_SINGLE(WM8731_APDIGI, 1, 4, wm8731_deemph),
10740e0aa64SRichard Purdie };
10840e0aa64SRichard Purdie 
10940e0aa64SRichard Purdie static const struct snd_kcontrol_new wm8731_snd_controls[] = {
11040e0aa64SRichard Purdie 
111bd903b6eSLiam Girdwood SOC_DOUBLE_R("Master Playback Volume", WM8731_LOUT1V, WM8731_ROUT1V,
112bd903b6eSLiam Girdwood 	0, 127, 0),
113bd903b6eSLiam Girdwood SOC_DOUBLE_R("Master Playback ZC Switch", WM8731_LOUT1V, WM8731_ROUT1V,
114bd903b6eSLiam Girdwood 	7, 1, 0),
11540e0aa64SRichard Purdie 
11640e0aa64SRichard Purdie SOC_DOUBLE_R("Capture Volume", WM8731_LINVOL, WM8731_RINVOL, 0, 31, 0),
11740e0aa64SRichard Purdie SOC_DOUBLE_R("Line Capture Switch", WM8731_LINVOL, WM8731_RINVOL, 7, 1, 1),
11840e0aa64SRichard Purdie 
11940e0aa64SRichard Purdie SOC_SINGLE("Mic Boost (+20dB)", WM8731_APANA, 0, 1, 0),
12040e0aa64SRichard Purdie SOC_SINGLE("Capture Mic Switch", WM8731_APANA, 1, 1, 1),
12140e0aa64SRichard Purdie 
12240e0aa64SRichard Purdie SOC_SINGLE("Sidetone Playback Volume", WM8731_APANA, 6, 3, 1),
12340e0aa64SRichard Purdie 
12440e0aa64SRichard Purdie SOC_SINGLE("ADC High Pass Filter Switch", WM8731_APDIGI, 0, 1, 1),
12540e0aa64SRichard Purdie SOC_SINGLE("Store DC Offset Switch", WM8731_APDIGI, 4, 1, 0),
12640e0aa64SRichard Purdie 
12740e0aa64SRichard Purdie SOC_ENUM("Playback De-emphasis", wm8731_enum[1]),
12840e0aa64SRichard Purdie };
12940e0aa64SRichard Purdie 
13040e0aa64SRichard Purdie /* Output Mixer */
13140e0aa64SRichard Purdie static const struct snd_kcontrol_new wm8731_output_mixer_controls[] = {
13240e0aa64SRichard Purdie SOC_DAPM_SINGLE("Line Bypass Switch", WM8731_APANA, 3, 1, 0),
13340e0aa64SRichard Purdie SOC_DAPM_SINGLE("Mic Sidetone Switch", WM8731_APANA, 5, 1, 0),
13440e0aa64SRichard Purdie SOC_DAPM_SINGLE("HiFi Playback Switch", WM8731_APANA, 4, 1, 0),
13540e0aa64SRichard Purdie };
13640e0aa64SRichard Purdie 
13740e0aa64SRichard Purdie /* Input mux */
13840e0aa64SRichard Purdie static const struct snd_kcontrol_new wm8731_input_mux_controls =
13940e0aa64SRichard Purdie SOC_DAPM_ENUM("Input Select", wm8731_enum[0]);
14040e0aa64SRichard Purdie 
14140e0aa64SRichard Purdie static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
14240e0aa64SRichard Purdie SND_SOC_DAPM_MIXER("Output Mixer", WM8731_PWR, 4, 1,
14340e0aa64SRichard Purdie 	&wm8731_output_mixer_controls[0],
14440e0aa64SRichard Purdie 	ARRAY_SIZE(wm8731_output_mixer_controls)),
14540e0aa64SRichard Purdie SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8731_PWR, 3, 1),
14640e0aa64SRichard Purdie SND_SOC_DAPM_OUTPUT("LOUT"),
14740e0aa64SRichard Purdie SND_SOC_DAPM_OUTPUT("LHPOUT"),
14840e0aa64SRichard Purdie SND_SOC_DAPM_OUTPUT("ROUT"),
14940e0aa64SRichard Purdie SND_SOC_DAPM_OUTPUT("RHPOUT"),
15040e0aa64SRichard Purdie SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8731_PWR, 2, 1),
15140e0aa64SRichard Purdie SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &wm8731_input_mux_controls),
15240e0aa64SRichard Purdie SND_SOC_DAPM_PGA("Line Input", WM8731_PWR, 0, 1, NULL, 0),
15340e0aa64SRichard Purdie SND_SOC_DAPM_MICBIAS("Mic Bias", WM8731_PWR, 1, 1),
15440e0aa64SRichard Purdie SND_SOC_DAPM_INPUT("MICIN"),
15540e0aa64SRichard Purdie SND_SOC_DAPM_INPUT("RLINEIN"),
15640e0aa64SRichard Purdie SND_SOC_DAPM_INPUT("LLINEIN"),
15740e0aa64SRichard Purdie };
15840e0aa64SRichard Purdie 
159a65f0568SMark Brown static const struct snd_soc_dapm_route intercon[] = {
16040e0aa64SRichard Purdie 	/* output mixer */
16140e0aa64SRichard Purdie 	{"Output Mixer", "Line Bypass Switch", "Line Input"},
16240e0aa64SRichard Purdie 	{"Output Mixer", "HiFi Playback Switch", "DAC"},
16340e0aa64SRichard Purdie 	{"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
16440e0aa64SRichard Purdie 
16540e0aa64SRichard Purdie 	/* outputs */
16640e0aa64SRichard Purdie 	{"RHPOUT", NULL, "Output Mixer"},
16740e0aa64SRichard Purdie 	{"ROUT", NULL, "Output Mixer"},
16840e0aa64SRichard Purdie 	{"LHPOUT", NULL, "Output Mixer"},
16940e0aa64SRichard Purdie 	{"LOUT", NULL, "Output Mixer"},
17040e0aa64SRichard Purdie 
17140e0aa64SRichard Purdie 	/* input mux */
17240e0aa64SRichard Purdie 	{"Input Mux", "Line In", "Line Input"},
17340e0aa64SRichard Purdie 	{"Input Mux", "Mic", "Mic Bias"},
17440e0aa64SRichard Purdie 	{"ADC", NULL, "Input Mux"},
17540e0aa64SRichard Purdie 
17640e0aa64SRichard Purdie 	/* inputs */
17740e0aa64SRichard Purdie 	{"Line Input", NULL, "LLINEIN"},
17840e0aa64SRichard Purdie 	{"Line Input", NULL, "RLINEIN"},
17940e0aa64SRichard Purdie 	{"Mic Bias", NULL, "MICIN"},
18040e0aa64SRichard Purdie };
18140e0aa64SRichard Purdie 
18240e0aa64SRichard Purdie static int wm8731_add_widgets(struct snd_soc_codec *codec)
18340e0aa64SRichard Purdie {
184a65f0568SMark Brown 	snd_soc_dapm_new_controls(codec, wm8731_dapm_widgets,
185a65f0568SMark Brown 				  ARRAY_SIZE(wm8731_dapm_widgets));
18640e0aa64SRichard Purdie 
187a65f0568SMark Brown 	snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon));
18840e0aa64SRichard Purdie 
18940e0aa64SRichard Purdie 	snd_soc_dapm_new_widgets(codec);
19040e0aa64SRichard Purdie 	return 0;
19140e0aa64SRichard Purdie }
19240e0aa64SRichard Purdie 
19340e0aa64SRichard Purdie struct _coeff_div {
19440e0aa64SRichard Purdie 	u32 mclk;
19540e0aa64SRichard Purdie 	u32 rate;
19640e0aa64SRichard Purdie 	u16 fs;
19740e0aa64SRichard Purdie 	u8 sr:4;
19840e0aa64SRichard Purdie 	u8 bosr:1;
19940e0aa64SRichard Purdie 	u8 usb:1;
20040e0aa64SRichard Purdie };
20140e0aa64SRichard Purdie 
20240e0aa64SRichard Purdie /* codec mclk clock divider coefficients */
20340e0aa64SRichard Purdie static const struct _coeff_div coeff_div[] = {
20440e0aa64SRichard Purdie 	/* 48k */
20540e0aa64SRichard Purdie 	{12288000, 48000, 256, 0x0, 0x0, 0x0},
20640e0aa64SRichard Purdie 	{18432000, 48000, 384, 0x0, 0x1, 0x0},
20740e0aa64SRichard Purdie 	{12000000, 48000, 250, 0x0, 0x0, 0x1},
20840e0aa64SRichard Purdie 
20940e0aa64SRichard Purdie 	/* 32k */
21040e0aa64SRichard Purdie 	{12288000, 32000, 384, 0x6, 0x0, 0x0},
21140e0aa64SRichard Purdie 	{18432000, 32000, 576, 0x6, 0x1, 0x0},
212298a2c75SFrank Mandarino 	{12000000, 32000, 375, 0x6, 0x0, 0x1},
21340e0aa64SRichard Purdie 
21440e0aa64SRichard Purdie 	/* 8k */
21540e0aa64SRichard Purdie 	{12288000, 8000, 1536, 0x3, 0x0, 0x0},
21640e0aa64SRichard Purdie 	{18432000, 8000, 2304, 0x3, 0x1, 0x0},
21740e0aa64SRichard Purdie 	{11289600, 8000, 1408, 0xb, 0x0, 0x0},
21840e0aa64SRichard Purdie 	{16934400, 8000, 2112, 0xb, 0x1, 0x0},
21940e0aa64SRichard Purdie 	{12000000, 8000, 1500, 0x3, 0x0, 0x1},
22040e0aa64SRichard Purdie 
22140e0aa64SRichard Purdie 	/* 96k */
22240e0aa64SRichard Purdie 	{12288000, 96000, 128, 0x7, 0x0, 0x0},
22340e0aa64SRichard Purdie 	{18432000, 96000, 192, 0x7, 0x1, 0x0},
22440e0aa64SRichard Purdie 	{12000000, 96000, 125, 0x7, 0x0, 0x1},
22540e0aa64SRichard Purdie 
22640e0aa64SRichard Purdie 	/* 44.1k */
22740e0aa64SRichard Purdie 	{11289600, 44100, 256, 0x8, 0x0, 0x0},
22840e0aa64SRichard Purdie 	{16934400, 44100, 384, 0x8, 0x1, 0x0},
22940e0aa64SRichard Purdie 	{12000000, 44100, 272, 0x8, 0x1, 0x1},
23040e0aa64SRichard Purdie 
23140e0aa64SRichard Purdie 	/* 88.2k */
23240e0aa64SRichard Purdie 	{11289600, 88200, 128, 0xf, 0x0, 0x0},
23340e0aa64SRichard Purdie 	{16934400, 88200, 192, 0xf, 0x1, 0x0},
23440e0aa64SRichard Purdie 	{12000000, 88200, 136, 0xf, 0x1, 0x1},
23540e0aa64SRichard Purdie };
23640e0aa64SRichard Purdie 
23740e0aa64SRichard Purdie static inline int get_coeff(int mclk, int rate)
23840e0aa64SRichard Purdie {
23940e0aa64SRichard Purdie 	int i;
24040e0aa64SRichard Purdie 
24140e0aa64SRichard Purdie 	for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
24240e0aa64SRichard Purdie 		if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
24340e0aa64SRichard Purdie 			return i;
24440e0aa64SRichard Purdie 	}
24540e0aa64SRichard Purdie 	return 0;
24640e0aa64SRichard Purdie }
24740e0aa64SRichard Purdie 
248b36d61d4SFrank Mandarino static int wm8731_hw_params(struct snd_pcm_substream *substream,
249dee89c4dSMark Brown 			    struct snd_pcm_hw_params *params,
250dee89c4dSMark Brown 			    struct snd_soc_dai *dai)
25140e0aa64SRichard Purdie {
252b36d61d4SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
253b36d61d4SFrank Mandarino 	struct snd_soc_device *socdev = rtd->socdev;
2546627a653SMark Brown 	struct snd_soc_codec *codec = socdev->card->codec;
255b36d61d4SFrank Mandarino 	struct wm8731_priv *wm8731 = codec->private_data;
256b36d61d4SFrank Mandarino 	u16 iface = wm8731_read_reg_cache(codec, WM8731_IFACE) & 0xfff3;
257b36d61d4SFrank Mandarino 	int i = get_coeff(wm8731->sysclk, params_rate(params));
258b36d61d4SFrank Mandarino 	u16 srate = (coeff_div[i].sr << 2) |
259b36d61d4SFrank Mandarino 		(coeff_div[i].bosr << 1) | coeff_div[i].usb;
26040e0aa64SRichard Purdie 
261b36d61d4SFrank Mandarino 	wm8731_write(codec, WM8731_SRATE, srate);
26240e0aa64SRichard Purdie 
263b36d61d4SFrank Mandarino 	/* bit size */
264b36d61d4SFrank Mandarino 	switch (params_format(params)) {
265b36d61d4SFrank Mandarino 	case SNDRV_PCM_FORMAT_S16_LE:
266b36d61d4SFrank Mandarino 		break;
267b36d61d4SFrank Mandarino 	case SNDRV_PCM_FORMAT_S20_3LE:
268b36d61d4SFrank Mandarino 		iface |= 0x0004;
269b36d61d4SFrank Mandarino 		break;
270b36d61d4SFrank Mandarino 	case SNDRV_PCM_FORMAT_S24_LE:
271b36d61d4SFrank Mandarino 		iface |= 0x0008;
272b36d61d4SFrank Mandarino 		break;
273b36d61d4SFrank Mandarino 	}
274b36d61d4SFrank Mandarino 
275b36d61d4SFrank Mandarino 	wm8731_write(codec, WM8731_IFACE, iface);
276b36d61d4SFrank Mandarino 	return 0;
27740e0aa64SRichard Purdie }
27840e0aa64SRichard Purdie 
279dee89c4dSMark Brown static int wm8731_pcm_prepare(struct snd_pcm_substream *substream,
280dee89c4dSMark Brown 			      struct snd_soc_dai *dai)
28140e0aa64SRichard Purdie {
28240e0aa64SRichard Purdie 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
28340e0aa64SRichard Purdie 	struct snd_soc_device *socdev = rtd->socdev;
2846627a653SMark Brown 	struct snd_soc_codec *codec = socdev->card->codec;
28540e0aa64SRichard Purdie 
28640e0aa64SRichard Purdie 	/* set active */
28740e0aa64SRichard Purdie 	wm8731_write(codec, WM8731_ACTIVE, 0x0001);
288b36d61d4SFrank Mandarino 
28940e0aa64SRichard Purdie 	return 0;
29040e0aa64SRichard Purdie }
29140e0aa64SRichard Purdie 
292dee89c4dSMark Brown static void wm8731_shutdown(struct snd_pcm_substream *substream,
293dee89c4dSMark Brown 			    struct snd_soc_dai *dai)
29440e0aa64SRichard Purdie {
29540e0aa64SRichard Purdie 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
29640e0aa64SRichard Purdie 	struct snd_soc_device *socdev = rtd->socdev;
2976627a653SMark Brown 	struct snd_soc_codec *codec = socdev->card->codec;
29840e0aa64SRichard Purdie 
29940e0aa64SRichard Purdie 	/* deactivate */
30040e0aa64SRichard Purdie 	if (!codec->active) {
30140e0aa64SRichard Purdie 		udelay(50);
30240e0aa64SRichard Purdie 		wm8731_write(codec, WM8731_ACTIVE, 0x0);
30340e0aa64SRichard Purdie 	}
30440e0aa64SRichard Purdie }
30540e0aa64SRichard Purdie 
306e550e17fSLiam Girdwood static int wm8731_mute(struct snd_soc_dai *dai, int mute)
30740e0aa64SRichard Purdie {
308b36d61d4SFrank Mandarino 	struct snd_soc_codec *codec = dai->codec;
30940e0aa64SRichard Purdie 	u16 mute_reg = wm8731_read_reg_cache(codec, WM8731_APDIGI) & 0xfff7;
310b36d61d4SFrank Mandarino 
31140e0aa64SRichard Purdie 	if (mute)
31240e0aa64SRichard Purdie 		wm8731_write(codec, WM8731_APDIGI, mute_reg | 0x8);
31340e0aa64SRichard Purdie 	else
31440e0aa64SRichard Purdie 		wm8731_write(codec, WM8731_APDIGI, mute_reg);
31540e0aa64SRichard Purdie 	return 0;
31640e0aa64SRichard Purdie }
31740e0aa64SRichard Purdie 
318e550e17fSLiam Girdwood static int wm8731_set_dai_sysclk(struct snd_soc_dai *codec_dai,
319b36d61d4SFrank Mandarino 		int clk_id, unsigned int freq, int dir)
320b36d61d4SFrank Mandarino {
321b36d61d4SFrank Mandarino 	struct snd_soc_codec *codec = codec_dai->codec;
322b36d61d4SFrank Mandarino 	struct wm8731_priv *wm8731 = codec->private_data;
323b36d61d4SFrank Mandarino 
324b36d61d4SFrank Mandarino 	switch (freq) {
325b36d61d4SFrank Mandarino 	case 11289600:
326b36d61d4SFrank Mandarino 	case 12000000:
327b36d61d4SFrank Mandarino 	case 12288000:
328b36d61d4SFrank Mandarino 	case 16934400:
329b36d61d4SFrank Mandarino 	case 18432000:
330b36d61d4SFrank Mandarino 		wm8731->sysclk = freq;
331b36d61d4SFrank Mandarino 		return 0;
332b36d61d4SFrank Mandarino 	}
333b36d61d4SFrank Mandarino 	return -EINVAL;
334b36d61d4SFrank Mandarino }
335b36d61d4SFrank Mandarino 
336b36d61d4SFrank Mandarino 
337e550e17fSLiam Girdwood static int wm8731_set_dai_fmt(struct snd_soc_dai *codec_dai,
338b36d61d4SFrank Mandarino 		unsigned int fmt)
339b36d61d4SFrank Mandarino {
340b36d61d4SFrank Mandarino 	struct snd_soc_codec *codec = codec_dai->codec;
341b36d61d4SFrank Mandarino 	u16 iface = 0;
342b36d61d4SFrank Mandarino 
343b36d61d4SFrank Mandarino 	/* set master/slave audio interface */
344b36d61d4SFrank Mandarino 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
345b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_CBM_CFM:
346b36d61d4SFrank Mandarino 		iface |= 0x0040;
347b36d61d4SFrank Mandarino 		break;
348b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_CBS_CFS:
349b36d61d4SFrank Mandarino 		break;
350b36d61d4SFrank Mandarino 	default:
351b36d61d4SFrank Mandarino 		return -EINVAL;
352b36d61d4SFrank Mandarino 	}
353b36d61d4SFrank Mandarino 
354b36d61d4SFrank Mandarino 	/* interface format */
355b36d61d4SFrank Mandarino 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
356b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_I2S:
357b36d61d4SFrank Mandarino 		iface |= 0x0002;
358b36d61d4SFrank Mandarino 		break;
359b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_RIGHT_J:
360b36d61d4SFrank Mandarino 		break;
361b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_LEFT_J:
362b36d61d4SFrank Mandarino 		iface |= 0x0001;
363b36d61d4SFrank Mandarino 		break;
364b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_DSP_A:
365b36d61d4SFrank Mandarino 		iface |= 0x0003;
366b36d61d4SFrank Mandarino 		break;
367b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_DSP_B:
368b36d61d4SFrank Mandarino 		iface |= 0x0013;
369b36d61d4SFrank Mandarino 		break;
370b36d61d4SFrank Mandarino 	default:
371b36d61d4SFrank Mandarino 		return -EINVAL;
372b36d61d4SFrank Mandarino 	}
373b36d61d4SFrank Mandarino 
374b36d61d4SFrank Mandarino 	/* clock inversion */
375b36d61d4SFrank Mandarino 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
376b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_NB_NF:
377b36d61d4SFrank Mandarino 		break;
378b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_IB_IF:
379b36d61d4SFrank Mandarino 		iface |= 0x0090;
380b36d61d4SFrank Mandarino 		break;
381b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_IB_NF:
382b36d61d4SFrank Mandarino 		iface |= 0x0080;
383b36d61d4SFrank Mandarino 		break;
384b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_NB_IF:
385b36d61d4SFrank Mandarino 		iface |= 0x0010;
386b36d61d4SFrank Mandarino 		break;
387b36d61d4SFrank Mandarino 	default:
388b36d61d4SFrank Mandarino 		return -EINVAL;
389b36d61d4SFrank Mandarino 	}
390b36d61d4SFrank Mandarino 
391b36d61d4SFrank Mandarino 	/* set iface */
392b36d61d4SFrank Mandarino 	wm8731_write(codec, WM8731_IFACE, iface);
393b36d61d4SFrank Mandarino 	return 0;
394b36d61d4SFrank Mandarino }
395b36d61d4SFrank Mandarino 
3960be9898aSMark Brown static int wm8731_set_bias_level(struct snd_soc_codec *codec,
3970be9898aSMark Brown 				 enum snd_soc_bias_level level)
39840e0aa64SRichard Purdie {
399*22d22ee5SMark Brown 	u16 reg;
40040e0aa64SRichard Purdie 
4010be9898aSMark Brown 	switch (level) {
4020be9898aSMark Brown 	case SND_SOC_BIAS_ON:
40340e0aa64SRichard Purdie 		break;
4040be9898aSMark Brown 	case SND_SOC_BIAS_PREPARE:
40540e0aa64SRichard Purdie 		break;
4060be9898aSMark Brown 	case SND_SOC_BIAS_STANDBY:
407*22d22ee5SMark Brown 		/* Clear PWROFF, gate CLKOUT, everything else as-is */
408*22d22ee5SMark Brown 		reg = wm8731_read_reg_cache(codec, WM8731_PWR) & 0xff7f;
40940e0aa64SRichard Purdie 		wm8731_write(codec, WM8731_PWR, reg | 0x0040);
41040e0aa64SRichard Purdie 		break;
4110be9898aSMark Brown 	case SND_SOC_BIAS_OFF:
41240e0aa64SRichard Purdie 		wm8731_write(codec, WM8731_ACTIVE, 0x0);
41340e0aa64SRichard Purdie 		wm8731_write(codec, WM8731_PWR, 0xffff);
41440e0aa64SRichard Purdie 		break;
41540e0aa64SRichard Purdie 	}
4160be9898aSMark Brown 	codec->bias_level = level;
41740e0aa64SRichard Purdie 	return 0;
41840e0aa64SRichard Purdie }
41940e0aa64SRichard Purdie 
420b36d61d4SFrank Mandarino #define WM8731_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
421b36d61d4SFrank Mandarino 		SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
422b36d61d4SFrank Mandarino 		SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\
423b36d61d4SFrank Mandarino 		SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |\
424b36d61d4SFrank Mandarino 		SNDRV_PCM_RATE_96000)
425b36d61d4SFrank Mandarino 
426b36d61d4SFrank Mandarino #define WM8731_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
427b36d61d4SFrank Mandarino 	SNDRV_PCM_FMTBIT_S24_LE)
428b36d61d4SFrank Mandarino 
429e550e17fSLiam Girdwood struct snd_soc_dai wm8731_dai = {
43040e0aa64SRichard Purdie 	.name = "WM8731",
43140e0aa64SRichard Purdie 	.playback = {
43240e0aa64SRichard Purdie 		.stream_name = "Playback",
43340e0aa64SRichard Purdie 		.channels_min = 1,
43440e0aa64SRichard Purdie 		.channels_max = 2,
435b36d61d4SFrank Mandarino 		.rates = WM8731_RATES,
436b36d61d4SFrank Mandarino 		.formats = WM8731_FORMATS,},
43740e0aa64SRichard Purdie 	.capture = {
43840e0aa64SRichard Purdie 		.stream_name = "Capture",
43940e0aa64SRichard Purdie 		.channels_min = 1,
44040e0aa64SRichard Purdie 		.channels_max = 2,
441b36d61d4SFrank Mandarino 		.rates = WM8731_RATES,
442b36d61d4SFrank Mandarino 		.formats = WM8731_FORMATS,},
44340e0aa64SRichard Purdie 	.ops = {
44440e0aa64SRichard Purdie 		.prepare = wm8731_pcm_prepare,
445b36d61d4SFrank Mandarino 		.hw_params = wm8731_hw_params,
44640e0aa64SRichard Purdie 		.shutdown = wm8731_shutdown,
447b36d61d4SFrank Mandarino 		.digital_mute = wm8731_mute,
448b36d61d4SFrank Mandarino 		.set_sysclk = wm8731_set_dai_sysclk,
449b36d61d4SFrank Mandarino 		.set_fmt = wm8731_set_dai_fmt,
450b36d61d4SFrank Mandarino 	}
45140e0aa64SRichard Purdie };
45240e0aa64SRichard Purdie EXPORT_SYMBOL_GPL(wm8731_dai);
45340e0aa64SRichard Purdie 
45440e0aa64SRichard Purdie static int wm8731_suspend(struct platform_device *pdev, pm_message_t state)
45540e0aa64SRichard Purdie {
45640e0aa64SRichard Purdie 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
4576627a653SMark Brown 	struct snd_soc_codec *codec = socdev->card->codec;
45840e0aa64SRichard Purdie 
45940e0aa64SRichard Purdie 	wm8731_write(codec, WM8731_ACTIVE, 0x0);
4600be9898aSMark Brown 	wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF);
46140e0aa64SRichard Purdie 	return 0;
46240e0aa64SRichard Purdie }
46340e0aa64SRichard Purdie 
46440e0aa64SRichard Purdie static int wm8731_resume(struct platform_device *pdev)
46540e0aa64SRichard Purdie {
46640e0aa64SRichard Purdie 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
4676627a653SMark Brown 	struct snd_soc_codec *codec = socdev->card->codec;
46840e0aa64SRichard Purdie 	int i;
46940e0aa64SRichard Purdie 	u8 data[2];
47040e0aa64SRichard Purdie 	u16 *cache = codec->reg_cache;
47140e0aa64SRichard Purdie 
47240e0aa64SRichard Purdie 	/* Sync reg_cache with the hardware */
47340e0aa64SRichard Purdie 	for (i = 0; i < ARRAY_SIZE(wm8731_reg); i++) {
47440e0aa64SRichard Purdie 		data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001);
47540e0aa64SRichard Purdie 		data[1] = cache[i] & 0x00ff;
47640e0aa64SRichard Purdie 		codec->hw_write(codec->control_data, data, 2);
47740e0aa64SRichard Purdie 	}
4780be9898aSMark Brown 	wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
4790be9898aSMark Brown 	wm8731_set_bias_level(codec, codec->suspend_bias_level);
48040e0aa64SRichard Purdie 	return 0;
48140e0aa64SRichard Purdie }
48240e0aa64SRichard Purdie 
48340e0aa64SRichard Purdie /*
48440e0aa64SRichard Purdie  * initialise the WM8731 driver
48540e0aa64SRichard Purdie  * register the mixer and dsp interfaces with the kernel
48640e0aa64SRichard Purdie  */
48740e0aa64SRichard Purdie static int wm8731_init(struct snd_soc_device *socdev)
48840e0aa64SRichard Purdie {
4896627a653SMark Brown 	struct snd_soc_codec *codec = socdev->card->codec;
49040e0aa64SRichard Purdie 	int reg, ret = 0;
49140e0aa64SRichard Purdie 
49240e0aa64SRichard Purdie 	codec->name = "WM8731";
49340e0aa64SRichard Purdie 	codec->owner = THIS_MODULE;
49440e0aa64SRichard Purdie 	codec->read = wm8731_read_reg_cache;
49540e0aa64SRichard Purdie 	codec->write = wm8731_write;
4960be9898aSMark Brown 	codec->set_bias_level = wm8731_set_bias_level;
49740e0aa64SRichard Purdie 	codec->dai = &wm8731_dai;
49840e0aa64SRichard Purdie 	codec->num_dai = 1;
499d751b233SMark Brown 	codec->reg_cache_size = ARRAY_SIZE(wm8731_reg);
50088cb4290STakashi Iwai 	codec->reg_cache = kmemdup(wm8731_reg, sizeof(wm8731_reg), GFP_KERNEL);
50140e0aa64SRichard Purdie 	if (codec->reg_cache == NULL)
50240e0aa64SRichard Purdie 		return -ENOMEM;
50340e0aa64SRichard Purdie 
50440e0aa64SRichard Purdie 	wm8731_reset(codec);
50540e0aa64SRichard Purdie 
50640e0aa64SRichard Purdie 	/* register pcms */
50740e0aa64SRichard Purdie 	ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
50840e0aa64SRichard Purdie 	if (ret < 0) {
509e35115a5SLiam Girdwood 		printk(KERN_ERR "wm8731: failed to create pcms\n");
510e35115a5SLiam Girdwood 		goto pcm_err;
51140e0aa64SRichard Purdie 	}
51240e0aa64SRichard Purdie 
51340e0aa64SRichard Purdie 	/* power on device */
5140be9898aSMark Brown 	wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
51540e0aa64SRichard Purdie 
51640e0aa64SRichard Purdie 	/* set the update bits */
51740e0aa64SRichard Purdie 	reg = wm8731_read_reg_cache(codec, WM8731_LOUT1V);
518389619f1SVille Syrjala 	wm8731_write(codec, WM8731_LOUT1V, reg & ~0x0100);
51940e0aa64SRichard Purdie 	reg = wm8731_read_reg_cache(codec, WM8731_ROUT1V);
520389619f1SVille Syrjala 	wm8731_write(codec, WM8731_ROUT1V, reg & ~0x0100);
52140e0aa64SRichard Purdie 	reg = wm8731_read_reg_cache(codec, WM8731_LINVOL);
522389619f1SVille Syrjala 	wm8731_write(codec, WM8731_LINVOL, reg & ~0x0100);
52340e0aa64SRichard Purdie 	reg = wm8731_read_reg_cache(codec, WM8731_RINVOL);
524389619f1SVille Syrjala 	wm8731_write(codec, WM8731_RINVOL, reg & ~0x0100);
52540e0aa64SRichard Purdie 
5263e8e1952SIan Molton 	snd_soc_add_controls(codec, wm8731_snd_controls,
5273e8e1952SIan Molton 				ARRAY_SIZE(wm8731_snd_controls));
52840e0aa64SRichard Purdie 	wm8731_add_widgets(codec);
529968a6025SMark Brown 	ret = snd_soc_init_card(socdev);
53040e0aa64SRichard Purdie 	if (ret < 0) {
531e35115a5SLiam Girdwood 		printk(KERN_ERR "wm8731: failed to register card\n");
532e35115a5SLiam Girdwood 		goto card_err;
53340e0aa64SRichard Purdie 	}
53440e0aa64SRichard Purdie 
53540e0aa64SRichard Purdie 	return ret;
536e35115a5SLiam Girdwood 
537e35115a5SLiam Girdwood card_err:
538e35115a5SLiam Girdwood 	snd_soc_free_pcms(socdev);
539e35115a5SLiam Girdwood 	snd_soc_dapm_free(socdev);
540e35115a5SLiam Girdwood pcm_err:
541e35115a5SLiam Girdwood 	kfree(codec->reg_cache);
542e35115a5SLiam Girdwood 	return ret;
54340e0aa64SRichard Purdie }
54440e0aa64SRichard Purdie 
54540e0aa64SRichard Purdie static struct snd_soc_device *wm8731_socdev;
54640e0aa64SRichard Purdie 
54740e0aa64SRichard Purdie #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
54840e0aa64SRichard Purdie 
54940e0aa64SRichard Purdie /*
55040e0aa64SRichard Purdie  * WM8731 2 wire address is determined by GPIO5
55140e0aa64SRichard Purdie  * state during powerup.
55240e0aa64SRichard Purdie  *    low  = 0x1a
55340e0aa64SRichard Purdie  *    high = 0x1b
55440e0aa64SRichard Purdie  */
55540e0aa64SRichard Purdie 
55681297c8aSJean Delvare static int wm8731_i2c_probe(struct i2c_client *i2c,
55781297c8aSJean Delvare 			    const struct i2c_device_id *id)
55840e0aa64SRichard Purdie {
55940e0aa64SRichard Purdie 	struct snd_soc_device *socdev = wm8731_socdev;
5606627a653SMark Brown 	struct snd_soc_codec *codec = socdev->card->codec;
56140e0aa64SRichard Purdie 	int ret;
56240e0aa64SRichard Purdie 
56340e0aa64SRichard Purdie 	i2c_set_clientdata(i2c, codec);
56440e0aa64SRichard Purdie 	codec->control_data = i2c;
56540e0aa64SRichard Purdie 
56640e0aa64SRichard Purdie 	ret = wm8731_init(socdev);
56781297c8aSJean Delvare 	if (ret < 0)
568a5c95e90SMark Brown 		pr_err("failed to initialise WM8731\n");
56940e0aa64SRichard Purdie 
57040e0aa64SRichard Purdie 	return ret;
57140e0aa64SRichard Purdie }
57240e0aa64SRichard Purdie 
57381297c8aSJean Delvare static int wm8731_i2c_remove(struct i2c_client *client)
57440e0aa64SRichard Purdie {
57540e0aa64SRichard Purdie 	struct snd_soc_codec *codec = i2c_get_clientdata(client);
57640e0aa64SRichard Purdie 	kfree(codec->reg_cache);
57740e0aa64SRichard Purdie 	return 0;
57840e0aa64SRichard Purdie }
57940e0aa64SRichard Purdie 
58081297c8aSJean Delvare static const struct i2c_device_id wm8731_i2c_id[] = {
58181297c8aSJean Delvare 	{ "wm8731", 0 },
58281297c8aSJean Delvare 	{ }
58381297c8aSJean Delvare };
58481297c8aSJean Delvare MODULE_DEVICE_TABLE(i2c, wm8731_i2c_id);
58540e0aa64SRichard Purdie 
58640e0aa64SRichard Purdie static struct i2c_driver wm8731_i2c_driver = {
58740e0aa64SRichard Purdie 	.driver = {
58840e0aa64SRichard Purdie 		.name = "WM8731 I2C Codec",
58940e0aa64SRichard Purdie 		.owner = THIS_MODULE,
59040e0aa64SRichard Purdie 	},
59181297c8aSJean Delvare 	.probe =    wm8731_i2c_probe,
59281297c8aSJean Delvare 	.remove =   wm8731_i2c_remove,
59381297c8aSJean Delvare 	.id_table = wm8731_i2c_id,
59440e0aa64SRichard Purdie };
59540e0aa64SRichard Purdie 
59681297c8aSJean Delvare static int wm8731_add_i2c_device(struct platform_device *pdev,
59781297c8aSJean Delvare 				 const struct wm8731_setup_data *setup)
59881297c8aSJean Delvare {
59981297c8aSJean Delvare 	struct i2c_board_info info;
60081297c8aSJean Delvare 	struct i2c_adapter *adapter;
60181297c8aSJean Delvare 	struct i2c_client *client;
60281297c8aSJean Delvare 	int ret;
60381297c8aSJean Delvare 
60481297c8aSJean Delvare 	ret = i2c_add_driver(&wm8731_i2c_driver);
60581297c8aSJean Delvare 	if (ret != 0) {
60681297c8aSJean Delvare 		dev_err(&pdev->dev, "can't add i2c driver\n");
60781297c8aSJean Delvare 		return ret;
60881297c8aSJean Delvare 	}
60981297c8aSJean Delvare 
61081297c8aSJean Delvare 	memset(&info, 0, sizeof(struct i2c_board_info));
61181297c8aSJean Delvare 	info.addr = setup->i2c_address;
61281297c8aSJean Delvare 	strlcpy(info.type, "wm8731", I2C_NAME_SIZE);
61381297c8aSJean Delvare 
61481297c8aSJean Delvare 	adapter = i2c_get_adapter(setup->i2c_bus);
61581297c8aSJean Delvare 	if (!adapter) {
61681297c8aSJean Delvare 		dev_err(&pdev->dev, "can't get i2c adapter %d\n",
61781297c8aSJean Delvare 			setup->i2c_bus);
61881297c8aSJean Delvare 		goto err_driver;
61981297c8aSJean Delvare 	}
62081297c8aSJean Delvare 
62181297c8aSJean Delvare 	client = i2c_new_device(adapter, &info);
62281297c8aSJean Delvare 	i2c_put_adapter(adapter);
62381297c8aSJean Delvare 	if (!client) {
62481297c8aSJean Delvare 		dev_err(&pdev->dev, "can't add i2c device at 0x%x\n",
62581297c8aSJean Delvare 			(unsigned int)info.addr);
62681297c8aSJean Delvare 		goto err_driver;
62781297c8aSJean Delvare 	}
62881297c8aSJean Delvare 
62981297c8aSJean Delvare 	return 0;
63081297c8aSJean Delvare 
63181297c8aSJean Delvare err_driver:
63281297c8aSJean Delvare 	i2c_del_driver(&wm8731_i2c_driver);
63381297c8aSJean Delvare 	return -ENODEV;
63481297c8aSJean Delvare }
63540e0aa64SRichard Purdie #endif
63640e0aa64SRichard Purdie 
637d2a40355SCliff Cai #if defined(CONFIG_SPI_MASTER)
638d2a40355SCliff Cai static int __devinit wm8731_spi_probe(struct spi_device *spi)
639d2a40355SCliff Cai {
640d2a40355SCliff Cai 	struct snd_soc_device *socdev = wm8731_socdev;
6416627a653SMark Brown 	struct snd_soc_codec *codec = socdev->card->codec;
642d2a40355SCliff Cai 	int ret;
643d2a40355SCliff Cai 
644d2a40355SCliff Cai 	codec->control_data = spi;
645d2a40355SCliff Cai 
646d2a40355SCliff Cai 	ret = wm8731_init(socdev);
647d2a40355SCliff Cai 	if (ret < 0)
648d2a40355SCliff Cai 		dev_err(&spi->dev, "failed to initialise WM8731\n");
649d2a40355SCliff Cai 
650d2a40355SCliff Cai 	return ret;
651d2a40355SCliff Cai }
652d2a40355SCliff Cai 
653d2a40355SCliff Cai static int __devexit wm8731_spi_remove(struct spi_device *spi)
654d2a40355SCliff Cai {
655d2a40355SCliff Cai 	return 0;
656d2a40355SCliff Cai }
657d2a40355SCliff Cai 
658d2a40355SCliff Cai static struct spi_driver wm8731_spi_driver = {
659d2a40355SCliff Cai 	.driver = {
660d2a40355SCliff Cai 		.name	= "wm8731",
661d2a40355SCliff Cai 		.bus	= &spi_bus_type,
662d2a40355SCliff Cai 		.owner	= THIS_MODULE,
663d2a40355SCliff Cai 	},
664d2a40355SCliff Cai 	.probe		= wm8731_spi_probe,
665d2a40355SCliff Cai 	.remove		= __devexit_p(wm8731_spi_remove),
666d2a40355SCliff Cai };
667d2a40355SCliff Cai 
668d2a40355SCliff Cai static int wm8731_spi_write(struct spi_device *spi, const char *data, int len)
669d2a40355SCliff Cai {
670d2a40355SCliff Cai 	struct spi_transfer t;
671d2a40355SCliff Cai 	struct spi_message m;
6728569be3cSAlan Horstmann 	u8 msg[2];
673d2a40355SCliff Cai 
674d2a40355SCliff Cai 	if (len <= 0)
675d2a40355SCliff Cai 		return 0;
676d2a40355SCliff Cai 
6778569be3cSAlan Horstmann 	msg[0] = data[0];
6788569be3cSAlan Horstmann 	msg[1] = data[1];
679d2a40355SCliff Cai 
680d2a40355SCliff Cai 	spi_message_init(&m);
681d2a40355SCliff Cai 	memset(&t, 0, (sizeof t));
682d2a40355SCliff Cai 
683d2a40355SCliff Cai 	t.tx_buf = &msg[0];
684d2a40355SCliff Cai 	t.len = len;
685d2a40355SCliff Cai 
686d2a40355SCliff Cai 	spi_message_add_tail(&t, &m);
687d2a40355SCliff Cai 	spi_sync(spi, &m);
688d2a40355SCliff Cai 
689d2a40355SCliff Cai 	return len;
690d2a40355SCliff Cai }
691d2a40355SCliff Cai #endif /* CONFIG_SPI_MASTER */
692d2a40355SCliff Cai 
69340e0aa64SRichard Purdie static int wm8731_probe(struct platform_device *pdev)
69440e0aa64SRichard Purdie {
69540e0aa64SRichard Purdie 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
69640e0aa64SRichard Purdie 	struct wm8731_setup_data *setup;
69740e0aa64SRichard Purdie 	struct snd_soc_codec *codec;
698b36d61d4SFrank Mandarino 	struct wm8731_priv *wm8731;
69940e0aa64SRichard Purdie 	int ret = 0;
70040e0aa64SRichard Purdie 
70140e0aa64SRichard Purdie 	setup = socdev->codec_data;
70240e0aa64SRichard Purdie 	codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
70340e0aa64SRichard Purdie 	if (codec == NULL)
70440e0aa64SRichard Purdie 		return -ENOMEM;
70540e0aa64SRichard Purdie 
706b36d61d4SFrank Mandarino 	wm8731 = kzalloc(sizeof(struct wm8731_priv), GFP_KERNEL);
707b36d61d4SFrank Mandarino 	if (wm8731 == NULL) {
708b36d61d4SFrank Mandarino 		kfree(codec);
709b36d61d4SFrank Mandarino 		return -ENOMEM;
710b36d61d4SFrank Mandarino 	}
711b36d61d4SFrank Mandarino 
712b36d61d4SFrank Mandarino 	codec->private_data = wm8731;
7136627a653SMark Brown 	socdev->card->codec = codec;
71440e0aa64SRichard Purdie 	mutex_init(&codec->mutex);
71540e0aa64SRichard Purdie 	INIT_LIST_HEAD(&codec->dapm_widgets);
71640e0aa64SRichard Purdie 	INIT_LIST_HEAD(&codec->dapm_paths);
71740e0aa64SRichard Purdie 
71840e0aa64SRichard Purdie 	wm8731_socdev = socdev;
719d2a40355SCliff Cai 	ret = -ENODEV;
720d2a40355SCliff Cai 
72140e0aa64SRichard Purdie #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
72240e0aa64SRichard Purdie 	if (setup->i2c_address) {
72340e0aa64SRichard Purdie 		codec->hw_write = (hw_write_t)i2c_master_send;
72481297c8aSJean Delvare 		ret = wm8731_add_i2c_device(pdev, setup);
72540e0aa64SRichard Purdie 	}
726d2a40355SCliff Cai #endif
727d2a40355SCliff Cai #if defined(CONFIG_SPI_MASTER)
728d2a40355SCliff Cai 	if (setup->spi) {
729d2a40355SCliff Cai 		codec->hw_write = (hw_write_t)wm8731_spi_write;
730d2a40355SCliff Cai 		ret = spi_register_driver(&wm8731_spi_driver);
731d2a40355SCliff Cai 		if (ret != 0)
732d2a40355SCliff Cai 			printk(KERN_ERR "can't add spi driver");
733d2a40355SCliff Cai 	}
73440e0aa64SRichard Purdie #endif
7353051e41aSJean Delvare 
7363051e41aSJean Delvare 	if (ret != 0) {
7373051e41aSJean Delvare 		kfree(codec->private_data);
7383051e41aSJean Delvare 		kfree(codec);
7393051e41aSJean Delvare 	}
74040e0aa64SRichard Purdie 	return ret;
74140e0aa64SRichard Purdie }
74240e0aa64SRichard Purdie 
74340e0aa64SRichard Purdie /* power down chip */
74440e0aa64SRichard Purdie static int wm8731_remove(struct platform_device *pdev)
74540e0aa64SRichard Purdie {
74640e0aa64SRichard Purdie 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
7476627a653SMark Brown 	struct snd_soc_codec *codec = socdev->card->codec;
74840e0aa64SRichard Purdie 
74940e0aa64SRichard Purdie 	if (codec->control_data)
7500be9898aSMark Brown 		wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF);
75140e0aa64SRichard Purdie 
75240e0aa64SRichard Purdie 	snd_soc_free_pcms(socdev);
75340e0aa64SRichard Purdie 	snd_soc_dapm_free(socdev);
75440e0aa64SRichard Purdie #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
75581297c8aSJean Delvare 	i2c_unregister_device(codec->control_data);
75640e0aa64SRichard Purdie 	i2c_del_driver(&wm8731_i2c_driver);
75740e0aa64SRichard Purdie #endif
758d2a40355SCliff Cai #if defined(CONFIG_SPI_MASTER)
759d2a40355SCliff Cai 	spi_unregister_driver(&wm8731_spi_driver);
760d2a40355SCliff Cai #endif
761b36d61d4SFrank Mandarino 	kfree(codec->private_data);
76240e0aa64SRichard Purdie 	kfree(codec);
76340e0aa64SRichard Purdie 
76440e0aa64SRichard Purdie 	return 0;
76540e0aa64SRichard Purdie }
76640e0aa64SRichard Purdie 
76740e0aa64SRichard Purdie struct snd_soc_codec_device soc_codec_dev_wm8731 = {
76840e0aa64SRichard Purdie 	.probe = 	wm8731_probe,
76940e0aa64SRichard Purdie 	.remove = 	wm8731_remove,
77040e0aa64SRichard Purdie 	.suspend = 	wm8731_suspend,
77140e0aa64SRichard Purdie 	.resume =	wm8731_resume,
77240e0aa64SRichard Purdie };
77340e0aa64SRichard Purdie EXPORT_SYMBOL_GPL(soc_codec_dev_wm8731);
77440e0aa64SRichard Purdie 
775c9b3a40fSTakashi Iwai static int __init wm8731_modinit(void)
77664089b84SMark Brown {
77764089b84SMark Brown 	return snd_soc_register_dai(&wm8731_dai);
77864089b84SMark Brown }
77964089b84SMark Brown module_init(wm8731_modinit);
78064089b84SMark Brown 
78164089b84SMark Brown static void __exit wm8731_exit(void)
78264089b84SMark Brown {
78364089b84SMark Brown 	snd_soc_unregister_dai(&wm8731_dai);
78464089b84SMark Brown }
78564089b84SMark Brown module_exit(wm8731_exit);
78664089b84SMark Brown 
78740e0aa64SRichard Purdie MODULE_DESCRIPTION("ASoC WM8731 driver");
78840e0aa64SRichard Purdie MODULE_AUTHOR("Richard Purdie");
78940e0aa64SRichard Purdie MODULE_LICENSE("GPL");
790