xref: /linux/sound/soc/codecs/wm8731.c (revision d00efa648d3c78ba212f08facf0defdff385dc28)
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>
29*d00efa64SMark Brown #include <sound/tlv.h>
3040e0aa64SRichard Purdie 
3140e0aa64SRichard Purdie #include "wm8731.h"
3240e0aa64SRichard Purdie 
335998102bSMark Brown static struct snd_soc_codec *wm8731_codec;
3440e0aa64SRichard Purdie struct snd_soc_codec_device soc_codec_dev_wm8731;
3540e0aa64SRichard Purdie 
36b36d61d4SFrank Mandarino /* codec private data */
37b36d61d4SFrank Mandarino struct wm8731_priv {
385998102bSMark Brown 	struct snd_soc_codec codec;
395998102bSMark Brown 	u16 reg_cache[WM8731_CACHEREGNUM];
40b36d61d4SFrank Mandarino 	unsigned int sysclk;
41b36d61d4SFrank Mandarino };
42b36d61d4SFrank Mandarino 
43a8035c8fSMark Brown #ifdef CONFIG_SPI_MASTER
44a8035c8fSMark Brown static int wm8731_spi_write(struct spi_device *spi, const char *data, int len);
45a8035c8fSMark Brown #endif
46a8035c8fSMark Brown 
4740e0aa64SRichard Purdie /*
4840e0aa64SRichard Purdie  * wm8731 register cache
4940e0aa64SRichard Purdie  * We can't read the WM8731 register space when we are
5040e0aa64SRichard Purdie  * using 2 wire for device control, so we cache them instead.
5140e0aa64SRichard Purdie  * There is no point in caching the reset register
5240e0aa64SRichard Purdie  */
5340e0aa64SRichard Purdie static const u16 wm8731_reg[WM8731_CACHEREGNUM] = {
5440e0aa64SRichard Purdie     0x0097, 0x0097, 0x0079, 0x0079,
5540e0aa64SRichard Purdie     0x000a, 0x0008, 0x009f, 0x000a,
5640e0aa64SRichard Purdie     0x0000, 0x0000
5740e0aa64SRichard Purdie };
5840e0aa64SRichard Purdie 
5940e0aa64SRichard Purdie /*
6040e0aa64SRichard Purdie  * read wm8731 register cache
6140e0aa64SRichard Purdie  */
6240e0aa64SRichard Purdie static inline unsigned int wm8731_read_reg_cache(struct snd_soc_codec *codec,
6340e0aa64SRichard Purdie 	unsigned int reg)
6440e0aa64SRichard Purdie {
6540e0aa64SRichard Purdie 	u16 *cache = codec->reg_cache;
6640e0aa64SRichard Purdie 	if (reg == WM8731_RESET)
6740e0aa64SRichard Purdie 		return 0;
6840e0aa64SRichard Purdie 	if (reg >= WM8731_CACHEREGNUM)
6940e0aa64SRichard Purdie 		return -1;
7040e0aa64SRichard Purdie 	return cache[reg];
7140e0aa64SRichard Purdie }
7240e0aa64SRichard Purdie 
7340e0aa64SRichard Purdie /*
7440e0aa64SRichard Purdie  * write wm8731 register cache
7540e0aa64SRichard Purdie  */
7640e0aa64SRichard Purdie static inline void wm8731_write_reg_cache(struct snd_soc_codec *codec,
7740e0aa64SRichard Purdie 	u16 reg, unsigned int value)
7840e0aa64SRichard Purdie {
7940e0aa64SRichard Purdie 	u16 *cache = codec->reg_cache;
8040e0aa64SRichard Purdie 	if (reg >= WM8731_CACHEREGNUM)
8140e0aa64SRichard Purdie 		return;
8240e0aa64SRichard Purdie 	cache[reg] = value;
8340e0aa64SRichard Purdie }
8440e0aa64SRichard Purdie 
8540e0aa64SRichard Purdie /*
8640e0aa64SRichard Purdie  * write to the WM8731 register space
8740e0aa64SRichard Purdie  */
8840e0aa64SRichard Purdie static int wm8731_write(struct snd_soc_codec *codec, unsigned int reg,
8940e0aa64SRichard Purdie 	unsigned int value)
9040e0aa64SRichard Purdie {
9140e0aa64SRichard Purdie 	u8 data[2];
9240e0aa64SRichard Purdie 
9340e0aa64SRichard Purdie 	/* data is
9440e0aa64SRichard Purdie 	 *   D15..D9 WM8731 register offset
9540e0aa64SRichard Purdie 	 *   D8...D0 register data
9640e0aa64SRichard Purdie 	 */
9740e0aa64SRichard Purdie 	data[0] = (reg << 1) | ((value >> 8) & 0x0001);
9840e0aa64SRichard Purdie 	data[1] = value & 0x00ff;
9940e0aa64SRichard Purdie 
10040e0aa64SRichard Purdie 	wm8731_write_reg_cache(codec, reg, value);
10140e0aa64SRichard Purdie 	if (codec->hw_write(codec->control_data, data, 2) == 2)
10240e0aa64SRichard Purdie 		return 0;
10340e0aa64SRichard Purdie 	else
10440e0aa64SRichard Purdie 		return -EIO;
10540e0aa64SRichard Purdie }
10640e0aa64SRichard Purdie 
10740e0aa64SRichard Purdie #define wm8731_reset(c)	wm8731_write(c, WM8731_RESET, 0)
10840e0aa64SRichard Purdie 
10940e0aa64SRichard Purdie static const char *wm8731_input_select[] = {"Line In", "Mic"};
11040e0aa64SRichard Purdie static const char *wm8731_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"};
11140e0aa64SRichard Purdie 
11240e0aa64SRichard Purdie static const struct soc_enum wm8731_enum[] = {
11340e0aa64SRichard Purdie 	SOC_ENUM_SINGLE(WM8731_APANA, 2, 2, wm8731_input_select),
11440e0aa64SRichard Purdie 	SOC_ENUM_SINGLE(WM8731_APDIGI, 1, 4, wm8731_deemph),
11540e0aa64SRichard Purdie };
11640e0aa64SRichard Purdie 
117*d00efa64SMark Brown static const DECLARE_TLV_DB_SCALE(in_tlv, -3450, 150, 0);
118*d00efa64SMark Brown static const DECLARE_TLV_DB_SCALE(sidetone_tlv, -1500, 300, 0);
119*d00efa64SMark Brown static const DECLARE_TLV_DB_SCALE(out_tlv, -12100, 100, 1);
120*d00efa64SMark Brown 
12140e0aa64SRichard Purdie static const struct snd_kcontrol_new wm8731_snd_controls[] = {
12240e0aa64SRichard Purdie 
123*d00efa64SMark Brown SOC_DOUBLE_R_TLV("Master Playback Volume", WM8731_LOUT1V, WM8731_ROUT1V,
124*d00efa64SMark Brown 		 0, 127, 0, out_tlv),
125bd903b6eSLiam Girdwood SOC_DOUBLE_R("Master Playback ZC Switch", WM8731_LOUT1V, WM8731_ROUT1V,
126bd903b6eSLiam Girdwood 	7, 1, 0),
12740e0aa64SRichard Purdie 
128*d00efa64SMark Brown SOC_DOUBLE_R_TLV("Capture Volume", WM8731_LINVOL, WM8731_RINVOL, 0, 31, 0,
129*d00efa64SMark Brown 		 in_tlv),
13040e0aa64SRichard Purdie SOC_DOUBLE_R("Line Capture Switch", WM8731_LINVOL, WM8731_RINVOL, 7, 1, 1),
13140e0aa64SRichard Purdie 
13240e0aa64SRichard Purdie SOC_SINGLE("Mic Boost (+20dB)", WM8731_APANA, 0, 1, 0),
13340e0aa64SRichard Purdie SOC_SINGLE("Capture Mic Switch", WM8731_APANA, 1, 1, 1),
13440e0aa64SRichard Purdie 
135*d00efa64SMark Brown SOC_SINGLE_TLV("Sidetone Playback Volume", WM8731_APANA, 6, 3, 1,
136*d00efa64SMark Brown 	       sidetone_tlv),
13740e0aa64SRichard Purdie 
13840e0aa64SRichard Purdie SOC_SINGLE("ADC High Pass Filter Switch", WM8731_APDIGI, 0, 1, 1),
13940e0aa64SRichard Purdie SOC_SINGLE("Store DC Offset Switch", WM8731_APDIGI, 4, 1, 0),
14040e0aa64SRichard Purdie 
14140e0aa64SRichard Purdie SOC_ENUM("Playback De-emphasis", wm8731_enum[1]),
14240e0aa64SRichard Purdie };
14340e0aa64SRichard Purdie 
14440e0aa64SRichard Purdie /* Output Mixer */
14540e0aa64SRichard Purdie static const struct snd_kcontrol_new wm8731_output_mixer_controls[] = {
14640e0aa64SRichard Purdie SOC_DAPM_SINGLE("Line Bypass Switch", WM8731_APANA, 3, 1, 0),
14740e0aa64SRichard Purdie SOC_DAPM_SINGLE("Mic Sidetone Switch", WM8731_APANA, 5, 1, 0),
14840e0aa64SRichard Purdie SOC_DAPM_SINGLE("HiFi Playback Switch", WM8731_APANA, 4, 1, 0),
14940e0aa64SRichard Purdie };
15040e0aa64SRichard Purdie 
15140e0aa64SRichard Purdie /* Input mux */
15240e0aa64SRichard Purdie static const struct snd_kcontrol_new wm8731_input_mux_controls =
15340e0aa64SRichard Purdie SOC_DAPM_ENUM("Input Select", wm8731_enum[0]);
15440e0aa64SRichard Purdie 
15540e0aa64SRichard Purdie static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
15640e0aa64SRichard Purdie SND_SOC_DAPM_MIXER("Output Mixer", WM8731_PWR, 4, 1,
15740e0aa64SRichard Purdie 	&wm8731_output_mixer_controls[0],
15840e0aa64SRichard Purdie 	ARRAY_SIZE(wm8731_output_mixer_controls)),
15940e0aa64SRichard Purdie SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8731_PWR, 3, 1),
16040e0aa64SRichard Purdie SND_SOC_DAPM_OUTPUT("LOUT"),
16140e0aa64SRichard Purdie SND_SOC_DAPM_OUTPUT("LHPOUT"),
16240e0aa64SRichard Purdie SND_SOC_DAPM_OUTPUT("ROUT"),
16340e0aa64SRichard Purdie SND_SOC_DAPM_OUTPUT("RHPOUT"),
16440e0aa64SRichard Purdie SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8731_PWR, 2, 1),
16540e0aa64SRichard Purdie SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &wm8731_input_mux_controls),
16640e0aa64SRichard Purdie SND_SOC_DAPM_PGA("Line Input", WM8731_PWR, 0, 1, NULL, 0),
16740e0aa64SRichard Purdie SND_SOC_DAPM_MICBIAS("Mic Bias", WM8731_PWR, 1, 1),
16840e0aa64SRichard Purdie SND_SOC_DAPM_INPUT("MICIN"),
16940e0aa64SRichard Purdie SND_SOC_DAPM_INPUT("RLINEIN"),
17040e0aa64SRichard Purdie SND_SOC_DAPM_INPUT("LLINEIN"),
17140e0aa64SRichard Purdie };
17240e0aa64SRichard Purdie 
173a65f0568SMark Brown static const struct snd_soc_dapm_route intercon[] = {
17440e0aa64SRichard Purdie 	/* output mixer */
17540e0aa64SRichard Purdie 	{"Output Mixer", "Line Bypass Switch", "Line Input"},
17640e0aa64SRichard Purdie 	{"Output Mixer", "HiFi Playback Switch", "DAC"},
17740e0aa64SRichard Purdie 	{"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
17840e0aa64SRichard Purdie 
17940e0aa64SRichard Purdie 	/* outputs */
18040e0aa64SRichard Purdie 	{"RHPOUT", NULL, "Output Mixer"},
18140e0aa64SRichard Purdie 	{"ROUT", NULL, "Output Mixer"},
18240e0aa64SRichard Purdie 	{"LHPOUT", NULL, "Output Mixer"},
18340e0aa64SRichard Purdie 	{"LOUT", NULL, "Output Mixer"},
18440e0aa64SRichard Purdie 
18540e0aa64SRichard Purdie 	/* input mux */
18640e0aa64SRichard Purdie 	{"Input Mux", "Line In", "Line Input"},
18740e0aa64SRichard Purdie 	{"Input Mux", "Mic", "Mic Bias"},
18840e0aa64SRichard Purdie 	{"ADC", NULL, "Input Mux"},
18940e0aa64SRichard Purdie 
19040e0aa64SRichard Purdie 	/* inputs */
19140e0aa64SRichard Purdie 	{"Line Input", NULL, "LLINEIN"},
19240e0aa64SRichard Purdie 	{"Line Input", NULL, "RLINEIN"},
19340e0aa64SRichard Purdie 	{"Mic Bias", NULL, "MICIN"},
19440e0aa64SRichard Purdie };
19540e0aa64SRichard Purdie 
19640e0aa64SRichard Purdie static int wm8731_add_widgets(struct snd_soc_codec *codec)
19740e0aa64SRichard Purdie {
198a65f0568SMark Brown 	snd_soc_dapm_new_controls(codec, wm8731_dapm_widgets,
199a65f0568SMark Brown 				  ARRAY_SIZE(wm8731_dapm_widgets));
20040e0aa64SRichard Purdie 
201a65f0568SMark Brown 	snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon));
20240e0aa64SRichard Purdie 
20340e0aa64SRichard Purdie 	snd_soc_dapm_new_widgets(codec);
20440e0aa64SRichard Purdie 	return 0;
20540e0aa64SRichard Purdie }
20640e0aa64SRichard Purdie 
20740e0aa64SRichard Purdie struct _coeff_div {
20840e0aa64SRichard Purdie 	u32 mclk;
20940e0aa64SRichard Purdie 	u32 rate;
21040e0aa64SRichard Purdie 	u16 fs;
21140e0aa64SRichard Purdie 	u8 sr:4;
21240e0aa64SRichard Purdie 	u8 bosr:1;
21340e0aa64SRichard Purdie 	u8 usb:1;
21440e0aa64SRichard Purdie };
21540e0aa64SRichard Purdie 
21640e0aa64SRichard Purdie /* codec mclk clock divider coefficients */
21740e0aa64SRichard Purdie static const struct _coeff_div coeff_div[] = {
21840e0aa64SRichard Purdie 	/* 48k */
21940e0aa64SRichard Purdie 	{12288000, 48000, 256, 0x0, 0x0, 0x0},
22040e0aa64SRichard Purdie 	{18432000, 48000, 384, 0x0, 0x1, 0x0},
22140e0aa64SRichard Purdie 	{12000000, 48000, 250, 0x0, 0x0, 0x1},
22240e0aa64SRichard Purdie 
22340e0aa64SRichard Purdie 	/* 32k */
22440e0aa64SRichard Purdie 	{12288000, 32000, 384, 0x6, 0x0, 0x0},
22540e0aa64SRichard Purdie 	{18432000, 32000, 576, 0x6, 0x1, 0x0},
226298a2c75SFrank Mandarino 	{12000000, 32000, 375, 0x6, 0x0, 0x1},
22740e0aa64SRichard Purdie 
22840e0aa64SRichard Purdie 	/* 8k */
22940e0aa64SRichard Purdie 	{12288000, 8000, 1536, 0x3, 0x0, 0x0},
23040e0aa64SRichard Purdie 	{18432000, 8000, 2304, 0x3, 0x1, 0x0},
23140e0aa64SRichard Purdie 	{11289600, 8000, 1408, 0xb, 0x0, 0x0},
23240e0aa64SRichard Purdie 	{16934400, 8000, 2112, 0xb, 0x1, 0x0},
23340e0aa64SRichard Purdie 	{12000000, 8000, 1500, 0x3, 0x0, 0x1},
23440e0aa64SRichard Purdie 
23540e0aa64SRichard Purdie 	/* 96k */
23640e0aa64SRichard Purdie 	{12288000, 96000, 128, 0x7, 0x0, 0x0},
23740e0aa64SRichard Purdie 	{18432000, 96000, 192, 0x7, 0x1, 0x0},
23840e0aa64SRichard Purdie 	{12000000, 96000, 125, 0x7, 0x0, 0x1},
23940e0aa64SRichard Purdie 
24040e0aa64SRichard Purdie 	/* 44.1k */
24140e0aa64SRichard Purdie 	{11289600, 44100, 256, 0x8, 0x0, 0x0},
24240e0aa64SRichard Purdie 	{16934400, 44100, 384, 0x8, 0x1, 0x0},
24340e0aa64SRichard Purdie 	{12000000, 44100, 272, 0x8, 0x1, 0x1},
24440e0aa64SRichard Purdie 
24540e0aa64SRichard Purdie 	/* 88.2k */
24640e0aa64SRichard Purdie 	{11289600, 88200, 128, 0xf, 0x0, 0x0},
24740e0aa64SRichard Purdie 	{16934400, 88200, 192, 0xf, 0x1, 0x0},
24840e0aa64SRichard Purdie 	{12000000, 88200, 136, 0xf, 0x1, 0x1},
24940e0aa64SRichard Purdie };
25040e0aa64SRichard Purdie 
25140e0aa64SRichard Purdie static inline int get_coeff(int mclk, int rate)
25240e0aa64SRichard Purdie {
25340e0aa64SRichard Purdie 	int i;
25440e0aa64SRichard Purdie 
25540e0aa64SRichard Purdie 	for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
25640e0aa64SRichard Purdie 		if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
25740e0aa64SRichard Purdie 			return i;
25840e0aa64SRichard Purdie 	}
25940e0aa64SRichard Purdie 	return 0;
26040e0aa64SRichard Purdie }
26140e0aa64SRichard Purdie 
262b36d61d4SFrank Mandarino static int wm8731_hw_params(struct snd_pcm_substream *substream,
263dee89c4dSMark Brown 			    struct snd_pcm_hw_params *params,
264dee89c4dSMark Brown 			    struct snd_soc_dai *dai)
26540e0aa64SRichard Purdie {
266b36d61d4SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
267b36d61d4SFrank Mandarino 	struct snd_soc_device *socdev = rtd->socdev;
2686627a653SMark Brown 	struct snd_soc_codec *codec = socdev->card->codec;
269b36d61d4SFrank Mandarino 	struct wm8731_priv *wm8731 = codec->private_data;
270b36d61d4SFrank Mandarino 	u16 iface = wm8731_read_reg_cache(codec, WM8731_IFACE) & 0xfff3;
271b36d61d4SFrank Mandarino 	int i = get_coeff(wm8731->sysclk, params_rate(params));
272b36d61d4SFrank Mandarino 	u16 srate = (coeff_div[i].sr << 2) |
273b36d61d4SFrank Mandarino 		(coeff_div[i].bosr << 1) | coeff_div[i].usb;
27440e0aa64SRichard Purdie 
275b36d61d4SFrank Mandarino 	wm8731_write(codec, WM8731_SRATE, srate);
27640e0aa64SRichard Purdie 
277b36d61d4SFrank Mandarino 	/* bit size */
278b36d61d4SFrank Mandarino 	switch (params_format(params)) {
279b36d61d4SFrank Mandarino 	case SNDRV_PCM_FORMAT_S16_LE:
280b36d61d4SFrank Mandarino 		break;
281b36d61d4SFrank Mandarino 	case SNDRV_PCM_FORMAT_S20_3LE:
282b36d61d4SFrank Mandarino 		iface |= 0x0004;
283b36d61d4SFrank Mandarino 		break;
284b36d61d4SFrank Mandarino 	case SNDRV_PCM_FORMAT_S24_LE:
285b36d61d4SFrank Mandarino 		iface |= 0x0008;
286b36d61d4SFrank Mandarino 		break;
287b36d61d4SFrank Mandarino 	}
288b36d61d4SFrank Mandarino 
289b36d61d4SFrank Mandarino 	wm8731_write(codec, WM8731_IFACE, iface);
290b36d61d4SFrank Mandarino 	return 0;
29140e0aa64SRichard Purdie }
29240e0aa64SRichard Purdie 
293dee89c4dSMark Brown static int wm8731_pcm_prepare(struct snd_pcm_substream *substream,
294dee89c4dSMark Brown 			      struct snd_soc_dai *dai)
29540e0aa64SRichard Purdie {
29640e0aa64SRichard Purdie 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
29740e0aa64SRichard Purdie 	struct snd_soc_device *socdev = rtd->socdev;
2986627a653SMark Brown 	struct snd_soc_codec *codec = socdev->card->codec;
29940e0aa64SRichard Purdie 
30040e0aa64SRichard Purdie 	/* set active */
30140e0aa64SRichard Purdie 	wm8731_write(codec, WM8731_ACTIVE, 0x0001);
302b36d61d4SFrank Mandarino 
30340e0aa64SRichard Purdie 	return 0;
30440e0aa64SRichard Purdie }
30540e0aa64SRichard Purdie 
306dee89c4dSMark Brown static void wm8731_shutdown(struct snd_pcm_substream *substream,
307dee89c4dSMark Brown 			    struct snd_soc_dai *dai)
30840e0aa64SRichard Purdie {
30940e0aa64SRichard Purdie 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
31040e0aa64SRichard Purdie 	struct snd_soc_device *socdev = rtd->socdev;
3116627a653SMark Brown 	struct snd_soc_codec *codec = socdev->card->codec;
31240e0aa64SRichard Purdie 
31340e0aa64SRichard Purdie 	/* deactivate */
31440e0aa64SRichard Purdie 	if (!codec->active) {
31540e0aa64SRichard Purdie 		udelay(50);
31640e0aa64SRichard Purdie 		wm8731_write(codec, WM8731_ACTIVE, 0x0);
31740e0aa64SRichard Purdie 	}
31840e0aa64SRichard Purdie }
31940e0aa64SRichard Purdie 
320e550e17fSLiam Girdwood static int wm8731_mute(struct snd_soc_dai *dai, int mute)
32140e0aa64SRichard Purdie {
322b36d61d4SFrank Mandarino 	struct snd_soc_codec *codec = dai->codec;
32340e0aa64SRichard Purdie 	u16 mute_reg = wm8731_read_reg_cache(codec, WM8731_APDIGI) & 0xfff7;
324b36d61d4SFrank Mandarino 
32540e0aa64SRichard Purdie 	if (mute)
32640e0aa64SRichard Purdie 		wm8731_write(codec, WM8731_APDIGI, mute_reg | 0x8);
32740e0aa64SRichard Purdie 	else
32840e0aa64SRichard Purdie 		wm8731_write(codec, WM8731_APDIGI, mute_reg);
32940e0aa64SRichard Purdie 	return 0;
33040e0aa64SRichard Purdie }
33140e0aa64SRichard Purdie 
332e550e17fSLiam Girdwood static int wm8731_set_dai_sysclk(struct snd_soc_dai *codec_dai,
333b36d61d4SFrank Mandarino 		int clk_id, unsigned int freq, int dir)
334b36d61d4SFrank Mandarino {
335b36d61d4SFrank Mandarino 	struct snd_soc_codec *codec = codec_dai->codec;
336b36d61d4SFrank Mandarino 	struct wm8731_priv *wm8731 = codec->private_data;
337b36d61d4SFrank Mandarino 
338b36d61d4SFrank Mandarino 	switch (freq) {
339b36d61d4SFrank Mandarino 	case 11289600:
340b36d61d4SFrank Mandarino 	case 12000000:
341b36d61d4SFrank Mandarino 	case 12288000:
342b36d61d4SFrank Mandarino 	case 16934400:
343b36d61d4SFrank Mandarino 	case 18432000:
344b36d61d4SFrank Mandarino 		wm8731->sysclk = freq;
345b36d61d4SFrank Mandarino 		return 0;
346b36d61d4SFrank Mandarino 	}
347b36d61d4SFrank Mandarino 	return -EINVAL;
348b36d61d4SFrank Mandarino }
349b36d61d4SFrank Mandarino 
350b36d61d4SFrank Mandarino 
351e550e17fSLiam Girdwood static int wm8731_set_dai_fmt(struct snd_soc_dai *codec_dai,
352b36d61d4SFrank Mandarino 		unsigned int fmt)
353b36d61d4SFrank Mandarino {
354b36d61d4SFrank Mandarino 	struct snd_soc_codec *codec = codec_dai->codec;
355b36d61d4SFrank Mandarino 	u16 iface = 0;
356b36d61d4SFrank Mandarino 
357b36d61d4SFrank Mandarino 	/* set master/slave audio interface */
358b36d61d4SFrank Mandarino 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
359b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_CBM_CFM:
360b36d61d4SFrank Mandarino 		iface |= 0x0040;
361b36d61d4SFrank Mandarino 		break;
362b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_CBS_CFS:
363b36d61d4SFrank Mandarino 		break;
364b36d61d4SFrank Mandarino 	default:
365b36d61d4SFrank Mandarino 		return -EINVAL;
366b36d61d4SFrank Mandarino 	}
367b36d61d4SFrank Mandarino 
368b36d61d4SFrank Mandarino 	/* interface format */
369b36d61d4SFrank Mandarino 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
370b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_I2S:
371b36d61d4SFrank Mandarino 		iface |= 0x0002;
372b36d61d4SFrank Mandarino 		break;
373b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_RIGHT_J:
374b36d61d4SFrank Mandarino 		break;
375b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_LEFT_J:
376b36d61d4SFrank Mandarino 		iface |= 0x0001;
377b36d61d4SFrank Mandarino 		break;
378b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_DSP_A:
379b36d61d4SFrank Mandarino 		iface |= 0x0003;
380b36d61d4SFrank Mandarino 		break;
381b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_DSP_B:
382b36d61d4SFrank Mandarino 		iface |= 0x0013;
383b36d61d4SFrank Mandarino 		break;
384b36d61d4SFrank Mandarino 	default:
385b36d61d4SFrank Mandarino 		return -EINVAL;
386b36d61d4SFrank Mandarino 	}
387b36d61d4SFrank Mandarino 
388b36d61d4SFrank Mandarino 	/* clock inversion */
389b36d61d4SFrank Mandarino 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
390b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_NB_NF:
391b36d61d4SFrank Mandarino 		break;
392b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_IB_IF:
393b36d61d4SFrank Mandarino 		iface |= 0x0090;
394b36d61d4SFrank Mandarino 		break;
395b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_IB_NF:
396b36d61d4SFrank Mandarino 		iface |= 0x0080;
397b36d61d4SFrank Mandarino 		break;
398b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_NB_IF:
399b36d61d4SFrank Mandarino 		iface |= 0x0010;
400b36d61d4SFrank Mandarino 		break;
401b36d61d4SFrank Mandarino 	default:
402b36d61d4SFrank Mandarino 		return -EINVAL;
403b36d61d4SFrank Mandarino 	}
404b36d61d4SFrank Mandarino 
405b36d61d4SFrank Mandarino 	/* set iface */
406b36d61d4SFrank Mandarino 	wm8731_write(codec, WM8731_IFACE, iface);
407b36d61d4SFrank Mandarino 	return 0;
408b36d61d4SFrank Mandarino }
409b36d61d4SFrank Mandarino 
4100be9898aSMark Brown static int wm8731_set_bias_level(struct snd_soc_codec *codec,
4110be9898aSMark Brown 				 enum snd_soc_bias_level level)
41240e0aa64SRichard Purdie {
41322d22ee5SMark Brown 	u16 reg;
41440e0aa64SRichard Purdie 
4150be9898aSMark Brown 	switch (level) {
4160be9898aSMark Brown 	case SND_SOC_BIAS_ON:
41740e0aa64SRichard Purdie 		break;
4180be9898aSMark Brown 	case SND_SOC_BIAS_PREPARE:
41940e0aa64SRichard Purdie 		break;
4200be9898aSMark Brown 	case SND_SOC_BIAS_STANDBY:
42122d22ee5SMark Brown 		/* Clear PWROFF, gate CLKOUT, everything else as-is */
42222d22ee5SMark Brown 		reg = wm8731_read_reg_cache(codec, WM8731_PWR) & 0xff7f;
42340e0aa64SRichard Purdie 		wm8731_write(codec, WM8731_PWR, reg | 0x0040);
42440e0aa64SRichard Purdie 		break;
4250be9898aSMark Brown 	case SND_SOC_BIAS_OFF:
42640e0aa64SRichard Purdie 		wm8731_write(codec, WM8731_ACTIVE, 0x0);
42740e0aa64SRichard Purdie 		wm8731_write(codec, WM8731_PWR, 0xffff);
42840e0aa64SRichard Purdie 		break;
42940e0aa64SRichard Purdie 	}
4300be9898aSMark Brown 	codec->bias_level = level;
43140e0aa64SRichard Purdie 	return 0;
43240e0aa64SRichard Purdie }
43340e0aa64SRichard Purdie 
434b36d61d4SFrank Mandarino #define WM8731_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
435b36d61d4SFrank Mandarino 		SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
436b36d61d4SFrank Mandarino 		SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\
437b36d61d4SFrank Mandarino 		SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |\
438b36d61d4SFrank Mandarino 		SNDRV_PCM_RATE_96000)
439b36d61d4SFrank Mandarino 
440b36d61d4SFrank Mandarino #define WM8731_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
441b36d61d4SFrank Mandarino 	SNDRV_PCM_FMTBIT_S24_LE)
442b36d61d4SFrank Mandarino 
4436335d055SEric Miao static struct snd_soc_dai_ops wm8731_dai_ops = {
4446335d055SEric Miao 	.prepare	= wm8731_pcm_prepare,
4456335d055SEric Miao 	.hw_params	= wm8731_hw_params,
4466335d055SEric Miao 	.shutdown	= wm8731_shutdown,
4476335d055SEric Miao 	.digital_mute	= wm8731_mute,
4486335d055SEric Miao 	.set_sysclk	= wm8731_set_dai_sysclk,
4496335d055SEric Miao 	.set_fmt	= wm8731_set_dai_fmt,
4506335d055SEric Miao };
4516335d055SEric Miao 
452e550e17fSLiam Girdwood struct snd_soc_dai wm8731_dai = {
45340e0aa64SRichard Purdie 	.name = "WM8731",
45440e0aa64SRichard Purdie 	.playback = {
45540e0aa64SRichard Purdie 		.stream_name = "Playback",
45640e0aa64SRichard Purdie 		.channels_min = 1,
45740e0aa64SRichard Purdie 		.channels_max = 2,
458b36d61d4SFrank Mandarino 		.rates = WM8731_RATES,
459b36d61d4SFrank Mandarino 		.formats = WM8731_FORMATS,},
46040e0aa64SRichard Purdie 	.capture = {
46140e0aa64SRichard Purdie 		.stream_name = "Capture",
46240e0aa64SRichard Purdie 		.channels_min = 1,
46340e0aa64SRichard Purdie 		.channels_max = 2,
464b36d61d4SFrank Mandarino 		.rates = WM8731_RATES,
465b36d61d4SFrank Mandarino 		.formats = WM8731_FORMATS,},
4666335d055SEric Miao 	.ops = &wm8731_dai_ops,
46740e0aa64SRichard Purdie };
46840e0aa64SRichard Purdie EXPORT_SYMBOL_GPL(wm8731_dai);
46940e0aa64SRichard Purdie 
470b3b50b3fSMark Brown #ifdef CONFIG_PM
47140e0aa64SRichard Purdie static int wm8731_suspend(struct platform_device *pdev, pm_message_t state)
47240e0aa64SRichard Purdie {
47340e0aa64SRichard Purdie 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
4746627a653SMark Brown 	struct snd_soc_codec *codec = socdev->card->codec;
47540e0aa64SRichard Purdie 
47640e0aa64SRichard Purdie 	wm8731_write(codec, WM8731_ACTIVE, 0x0);
4770be9898aSMark Brown 	wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF);
47840e0aa64SRichard Purdie 	return 0;
47940e0aa64SRichard Purdie }
48040e0aa64SRichard Purdie 
48140e0aa64SRichard Purdie static int wm8731_resume(struct platform_device *pdev)
48240e0aa64SRichard Purdie {
48340e0aa64SRichard Purdie 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
4846627a653SMark Brown 	struct snd_soc_codec *codec = socdev->card->codec;
48540e0aa64SRichard Purdie 	int i;
48640e0aa64SRichard Purdie 	u8 data[2];
48740e0aa64SRichard Purdie 	u16 *cache = codec->reg_cache;
48840e0aa64SRichard Purdie 
48940e0aa64SRichard Purdie 	/* Sync reg_cache with the hardware */
49040e0aa64SRichard Purdie 	for (i = 0; i < ARRAY_SIZE(wm8731_reg); i++) {
49140e0aa64SRichard Purdie 		data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001);
49240e0aa64SRichard Purdie 		data[1] = cache[i] & 0x00ff;
49340e0aa64SRichard Purdie 		codec->hw_write(codec->control_data, data, 2);
49440e0aa64SRichard Purdie 	}
4950be9898aSMark Brown 	wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
4960be9898aSMark Brown 	wm8731_set_bias_level(codec, codec->suspend_bias_level);
49740e0aa64SRichard Purdie 	return 0;
49840e0aa64SRichard Purdie }
499b3b50b3fSMark Brown #else
500b3b50b3fSMark Brown #define wm8731_suspend NULL
501b3b50b3fSMark Brown #define wm8731_resume NULL
502b3b50b3fSMark Brown #endif
50340e0aa64SRichard Purdie 
5045998102bSMark Brown static int wm8731_probe(struct platform_device *pdev)
50540e0aa64SRichard Purdie {
5065998102bSMark Brown 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
5075998102bSMark Brown 	struct snd_soc_codec *codec;
5085998102bSMark Brown 	int ret = 0;
50940e0aa64SRichard Purdie 
5105998102bSMark Brown 	if (wm8731_codec == NULL) {
5115998102bSMark Brown 		dev_err(&pdev->dev, "Codec device not registered\n");
5125998102bSMark Brown 		return -ENODEV;
5135998102bSMark Brown 	}
51440e0aa64SRichard Purdie 
5155998102bSMark Brown 	socdev->card->codec = wm8731_codec;
5165998102bSMark Brown 	codec = wm8731_codec;
51740e0aa64SRichard Purdie 
51840e0aa64SRichard Purdie 	/* register pcms */
51940e0aa64SRichard Purdie 	ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
52040e0aa64SRichard Purdie 	if (ret < 0) {
5215998102bSMark Brown 		dev_err(codec->dev, "failed to create pcms: %d\n", ret);
522e35115a5SLiam Girdwood 		goto pcm_err;
52340e0aa64SRichard Purdie 	}
52440e0aa64SRichard Purdie 
5253e8e1952SIan Molton 	snd_soc_add_controls(codec, wm8731_snd_controls,
5263e8e1952SIan Molton 			     ARRAY_SIZE(wm8731_snd_controls));
52740e0aa64SRichard Purdie 	wm8731_add_widgets(codec);
528968a6025SMark Brown 	ret = snd_soc_init_card(socdev);
52940e0aa64SRichard Purdie 	if (ret < 0) {
5305998102bSMark Brown 		dev_err(codec->dev, "failed to register card: %d\n", ret);
531e35115a5SLiam Girdwood 		goto card_err;
53240e0aa64SRichard Purdie 	}
53340e0aa64SRichard Purdie 
53440e0aa64SRichard Purdie 	return ret;
535e35115a5SLiam Girdwood 
536e35115a5SLiam Girdwood card_err:
537e35115a5SLiam Girdwood 	snd_soc_free_pcms(socdev);
538e35115a5SLiam Girdwood 	snd_soc_dapm_free(socdev);
539e35115a5SLiam Girdwood pcm_err:
54040e0aa64SRichard Purdie 	return ret;
54140e0aa64SRichard Purdie }
54240e0aa64SRichard Purdie 
54340e0aa64SRichard Purdie /* power down chip */
54440e0aa64SRichard Purdie static int wm8731_remove(struct platform_device *pdev)
54540e0aa64SRichard Purdie {
54640e0aa64SRichard Purdie 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
54740e0aa64SRichard Purdie 
54840e0aa64SRichard Purdie 	snd_soc_free_pcms(socdev);
54940e0aa64SRichard Purdie 	snd_soc_dapm_free(socdev);
55040e0aa64SRichard Purdie 
55140e0aa64SRichard Purdie 	return 0;
55240e0aa64SRichard Purdie }
55340e0aa64SRichard Purdie 
55440e0aa64SRichard Purdie struct snd_soc_codec_device soc_codec_dev_wm8731 = {
55540e0aa64SRichard Purdie 	.probe = 	wm8731_probe,
55640e0aa64SRichard Purdie 	.remove = 	wm8731_remove,
55740e0aa64SRichard Purdie 	.suspend = 	wm8731_suspend,
55840e0aa64SRichard Purdie 	.resume =	wm8731_resume,
55940e0aa64SRichard Purdie };
56040e0aa64SRichard Purdie EXPORT_SYMBOL_GPL(soc_codec_dev_wm8731);
56140e0aa64SRichard Purdie 
5625998102bSMark Brown static int wm8731_register(struct wm8731_priv *wm8731)
563a8035c8fSMark Brown {
564a8035c8fSMark Brown 	int ret;
5655998102bSMark Brown 	struct snd_soc_codec *codec = &wm8731->codec;
5665998102bSMark Brown 	u16 reg;
567a8035c8fSMark Brown 
5685998102bSMark Brown 	if (wm8731_codec) {
5695998102bSMark Brown 		dev_err(codec->dev, "Another WM8731 is registered\n");
570fe5422fcSMark Brown 		ret = -EINVAL;
571fe5422fcSMark Brown 		goto err;
5725998102bSMark Brown 	}
573a8035c8fSMark Brown 
5745998102bSMark Brown 	mutex_init(&codec->mutex);
5755998102bSMark Brown 	INIT_LIST_HEAD(&codec->dapm_widgets);
5765998102bSMark Brown 	INIT_LIST_HEAD(&codec->dapm_paths);
577a8035c8fSMark Brown 
5785998102bSMark Brown 	codec->private_data = wm8731;
5795998102bSMark Brown 	codec->name = "WM8731";
5805998102bSMark Brown 	codec->owner = THIS_MODULE;
5815998102bSMark Brown 	codec->read = wm8731_read_reg_cache;
5825998102bSMark Brown 	codec->write = wm8731_write;
5835998102bSMark Brown 	codec->bias_level = SND_SOC_BIAS_OFF;
5845998102bSMark Brown 	codec->set_bias_level = wm8731_set_bias_level;
5855998102bSMark Brown 	codec->dai = &wm8731_dai;
5865998102bSMark Brown 	codec->num_dai = 1;
5875998102bSMark Brown 	codec->reg_cache_size = WM8731_CACHEREGNUM;
5885998102bSMark Brown 	codec->reg_cache = &wm8731->reg_cache;
5895998102bSMark Brown 
5905998102bSMark Brown 	memcpy(codec->reg_cache, wm8731_reg, sizeof(wm8731_reg));
5915998102bSMark Brown 
592519cf2dfSMark Brown 	ret = wm8731_reset(codec);
593519cf2dfSMark Brown 	if (ret < 0) {
594fe5422fcSMark Brown 		dev_err(codec->dev, "Failed to issue reset: %d\n", ret);
595fe5422fcSMark Brown 		goto err;
596519cf2dfSMark Brown 	}
597519cf2dfSMark Brown 
5985998102bSMark Brown 	wm8731_dai.dev = codec->dev;
5995998102bSMark Brown 
6005998102bSMark Brown 	wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
6015998102bSMark Brown 
6025998102bSMark Brown 	/* Latch the update bits */
6035998102bSMark Brown 	reg = wm8731_read_reg_cache(codec, WM8731_LOUT1V);
6045998102bSMark Brown 	wm8731_write(codec, WM8731_LOUT1V, reg & ~0x0100);
6055998102bSMark Brown 	reg = wm8731_read_reg_cache(codec, WM8731_ROUT1V);
6065998102bSMark Brown 	wm8731_write(codec, WM8731_ROUT1V, reg & ~0x0100);
6075998102bSMark Brown 	reg = wm8731_read_reg_cache(codec, WM8731_LINVOL);
6085998102bSMark Brown 	wm8731_write(codec, WM8731_LINVOL, reg & ~0x0100);
6095998102bSMark Brown 	reg = wm8731_read_reg_cache(codec, WM8731_RINVOL);
6105998102bSMark Brown 	wm8731_write(codec, WM8731_RINVOL, reg & ~0x0100);
6115998102bSMark Brown 
612ce3bdaa8SMark Brown 	/* Disable bypass path by default */
613ce3bdaa8SMark Brown 	reg = wm8731_read_reg_cache(codec, WM8731_APANA);
614ce3bdaa8SMark Brown 	wm8731_write(codec, WM8731_APANA, reg & ~0x4);
615ce3bdaa8SMark Brown 
6165998102bSMark Brown 	wm8731_codec = codec;
6175998102bSMark Brown 
6185998102bSMark Brown 	ret = snd_soc_register_codec(codec);
6195998102bSMark Brown 	if (ret != 0) {
6205998102bSMark Brown 		dev_err(codec->dev, "Failed to register codec: %d\n", ret);
621fe5422fcSMark Brown 		goto err;
622a8035c8fSMark Brown 	}
623a8035c8fSMark Brown 
6245998102bSMark Brown 	ret = snd_soc_register_dai(&wm8731_dai);
6255998102bSMark Brown 	if (ret != 0) {
6265998102bSMark Brown 		dev_err(codec->dev, "Failed to register DAI: %d\n", ret);
6275998102bSMark Brown 		snd_soc_unregister_codec(codec);
628fe5422fcSMark Brown 		goto err_codec;
6295998102bSMark Brown 	}
6305998102bSMark Brown 
631a8035c8fSMark Brown 	return 0;
632fe5422fcSMark Brown 
633fe5422fcSMark Brown err_codec:
634fe5422fcSMark Brown 	snd_soc_unregister_codec(codec);
635fe5422fcSMark Brown err:
636fe5422fcSMark Brown 	kfree(wm8731);
637fe5422fcSMark Brown 	return ret;
638a8035c8fSMark Brown }
639a8035c8fSMark Brown 
6405998102bSMark Brown static void wm8731_unregister(struct wm8731_priv *wm8731)
6415998102bSMark Brown {
6425998102bSMark Brown 	wm8731_set_bias_level(&wm8731->codec, SND_SOC_BIAS_OFF);
6435998102bSMark Brown 	snd_soc_unregister_dai(&wm8731_dai);
6445998102bSMark Brown 	snd_soc_unregister_codec(&wm8731->codec);
6455998102bSMark Brown 	kfree(wm8731);
6465998102bSMark Brown 	wm8731_codec = NULL;
6475998102bSMark Brown }
648a8035c8fSMark Brown 
6495998102bSMark Brown #if defined(CONFIG_SPI_MASTER)
650a8035c8fSMark Brown static int wm8731_spi_write(struct spi_device *spi, const char *data, int len)
651a8035c8fSMark Brown {
652a8035c8fSMark Brown 	struct spi_transfer t;
653a8035c8fSMark Brown 	struct spi_message m;
654a8035c8fSMark Brown 	u8 msg[2];
655a8035c8fSMark Brown 
656a8035c8fSMark Brown 	if (len <= 0)
657a8035c8fSMark Brown 		return 0;
658a8035c8fSMark Brown 
659a8035c8fSMark Brown 	msg[0] = data[0];
660a8035c8fSMark Brown 	msg[1] = data[1];
661a8035c8fSMark Brown 
662a8035c8fSMark Brown 	spi_message_init(&m);
663a8035c8fSMark Brown 	memset(&t, 0, (sizeof t));
664a8035c8fSMark Brown 
665a8035c8fSMark Brown 	t.tx_buf = &msg[0];
666a8035c8fSMark Brown 	t.len = len;
667a8035c8fSMark Brown 
668a8035c8fSMark Brown 	spi_message_add_tail(&t, &m);
669a8035c8fSMark Brown 	spi_sync(spi, &m);
670a8035c8fSMark Brown 
671a8035c8fSMark Brown 	return len;
672a8035c8fSMark Brown }
6735998102bSMark Brown 
6745998102bSMark Brown static int __devinit wm8731_spi_probe(struct spi_device *spi)
6755998102bSMark Brown {
6765998102bSMark Brown 	struct snd_soc_codec *codec;
6775998102bSMark Brown 	struct wm8731_priv *wm8731;
6785998102bSMark Brown 
6795998102bSMark Brown 	wm8731 = kzalloc(sizeof(struct wm8731_priv), GFP_KERNEL);
6805998102bSMark Brown 	if (wm8731 == NULL)
6815998102bSMark Brown 		return -ENOMEM;
6825998102bSMark Brown 
6835998102bSMark Brown 	codec = &wm8731->codec;
6845998102bSMark Brown 	codec->control_data = spi;
6855998102bSMark Brown 	codec->hw_write = (hw_write_t)wm8731_spi_write;
6865998102bSMark Brown 	codec->dev = &spi->dev;
6875998102bSMark Brown 
688ae31c1fbSGreg Kroah-Hartman 	dev_set_drvdata(&spi->dev, wm8731);
68993b760b7SMark Brown 
6905998102bSMark Brown 	return wm8731_register(wm8731);
6915998102bSMark Brown }
6925998102bSMark Brown 
6935998102bSMark Brown static int __devexit wm8731_spi_remove(struct spi_device *spi)
6945998102bSMark Brown {
695ae31c1fbSGreg Kroah-Hartman 	struct wm8731_priv *wm8731 = dev_get_drvdata(&spi->dev);
69693b760b7SMark Brown 
69793b760b7SMark Brown 	wm8731_unregister(wm8731);
69893b760b7SMark Brown 
6995998102bSMark Brown 	return 0;
7005998102bSMark Brown }
7015998102bSMark Brown 
702b3b50b3fSMark Brown #ifdef CONFIG_PM
703b3b50b3fSMark Brown static int wm8731_spi_suspend(struct spi_device *spi, pm_message_t msg)
704b3b50b3fSMark Brown {
705b3b50b3fSMark Brown 	return snd_soc_suspend_device(&spi->dev);
706b3b50b3fSMark Brown }
707b3b50b3fSMark Brown 
708b3b50b3fSMark Brown static int wm8731_spi_resume(struct spi_device *spi)
709b3b50b3fSMark Brown {
710b3b50b3fSMark Brown 	return snd_soc_resume_device(&spi->dev);
711b3b50b3fSMark Brown }
712b3b50b3fSMark Brown #else
713b3b50b3fSMark Brown #define wm8731_spi_suspend NULL
714b3b50b3fSMark Brown #define wm8731_spi_resume NULL
715b3b50b3fSMark Brown #endif
716b3b50b3fSMark Brown 
7175998102bSMark Brown static struct spi_driver wm8731_spi_driver = {
7185998102bSMark Brown 	.driver = {
7195998102bSMark Brown 		.name	= "wm8731",
7205998102bSMark Brown 		.bus	= &spi_bus_type,
7215998102bSMark Brown 		.owner	= THIS_MODULE,
7225998102bSMark Brown 	},
7235998102bSMark Brown 	.probe		= wm8731_spi_probe,
724b3b50b3fSMark Brown 	.suspend	= wm8731_spi_suspend,
725b3b50b3fSMark Brown 	.resume		= wm8731_spi_resume,
7265998102bSMark Brown 	.remove		= __devexit_p(wm8731_spi_remove),
7275998102bSMark Brown };
728a8035c8fSMark Brown #endif /* CONFIG_SPI_MASTER */
729a8035c8fSMark Brown 
730a8035c8fSMark Brown #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
731c6f29811SMark Brown static __devinit int wm8731_i2c_probe(struct i2c_client *i2c,
732a8035c8fSMark Brown 				      const struct i2c_device_id *id)
733a8035c8fSMark Brown {
7345998102bSMark Brown 	struct wm8731_priv *wm8731;
7355998102bSMark Brown 	struct snd_soc_codec *codec;
736a8035c8fSMark Brown 
7375998102bSMark Brown 	wm8731 = kzalloc(sizeof(struct wm8731_priv), GFP_KERNEL);
7385998102bSMark Brown 	if (wm8731 == NULL)
7395998102bSMark Brown 		return -ENOMEM;
7405998102bSMark Brown 
7415998102bSMark Brown 	codec = &wm8731->codec;
7425998102bSMark Brown 	codec->hw_write = (hw_write_t)i2c_master_send;
7435998102bSMark Brown 
7445998102bSMark Brown 	i2c_set_clientdata(i2c, wm8731);
745a8035c8fSMark Brown 	codec->control_data = i2c;
746a8035c8fSMark Brown 
7475998102bSMark Brown 	codec->dev = &i2c->dev;
748a8035c8fSMark Brown 
7495998102bSMark Brown 	return wm8731_register(wm8731);
750a8035c8fSMark Brown }
751a8035c8fSMark Brown 
752c6f29811SMark Brown static __devexit int wm8731_i2c_remove(struct i2c_client *client)
753a8035c8fSMark Brown {
7545998102bSMark Brown 	struct wm8731_priv *wm8731 = i2c_get_clientdata(client);
7555998102bSMark Brown 	wm8731_unregister(wm8731);
756a8035c8fSMark Brown 	return 0;
757a8035c8fSMark Brown }
758a8035c8fSMark Brown 
759b3b50b3fSMark Brown #ifdef CONFIG_PM
760b3b50b3fSMark Brown static int wm8731_i2c_suspend(struct i2c_client *i2c, pm_message_t msg)
761b3b50b3fSMark Brown {
762b3b50b3fSMark Brown 	return snd_soc_suspend_device(&i2c->dev);
763b3b50b3fSMark Brown }
764b3b50b3fSMark Brown 
765b3b50b3fSMark Brown static int wm8731_i2c_resume(struct i2c_client *i2c)
766b3b50b3fSMark Brown {
767b3b50b3fSMark Brown 	return snd_soc_resume_device(&i2c->dev);
768b3b50b3fSMark Brown }
769b3b50b3fSMark Brown #else
770b3b50b3fSMark Brown #define wm8731_i2c_suspend NULL
771b3b50b3fSMark Brown #define wm8731_i2c_resume NULL
772b3b50b3fSMark Brown #endif
773b3b50b3fSMark Brown 
774a8035c8fSMark Brown static const struct i2c_device_id wm8731_i2c_id[] = {
775a8035c8fSMark Brown 	{ "wm8731", 0 },
776a8035c8fSMark Brown 	{ }
777a8035c8fSMark Brown };
778a8035c8fSMark Brown MODULE_DEVICE_TABLE(i2c, wm8731_i2c_id);
779a8035c8fSMark Brown 
780a8035c8fSMark Brown static struct i2c_driver wm8731_i2c_driver = {
781a8035c8fSMark Brown 	.driver = {
782a8035c8fSMark Brown 		.name = "WM8731 I2C Codec",
783a8035c8fSMark Brown 		.owner = THIS_MODULE,
784a8035c8fSMark Brown 	},
785a8035c8fSMark Brown 	.probe =    wm8731_i2c_probe,
786c6f29811SMark Brown 	.remove =   __devexit_p(wm8731_i2c_remove),
787b3b50b3fSMark Brown 	.suspend =  wm8731_i2c_suspend,
788b3b50b3fSMark Brown 	.resume =   wm8731_i2c_resume,
789a8035c8fSMark Brown 	.id_table = wm8731_i2c_id,
790a8035c8fSMark Brown };
791a8035c8fSMark Brown #endif
792a8035c8fSMark Brown 
793c9b3a40fSTakashi Iwai static int __init wm8731_modinit(void)
79464089b84SMark Brown {
7955998102bSMark Brown 	int ret;
7965998102bSMark Brown #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
7975998102bSMark Brown 	ret = i2c_add_driver(&wm8731_i2c_driver);
7985998102bSMark Brown 	if (ret != 0) {
7995998102bSMark Brown 		printk(KERN_ERR "Failed to register WM8731 I2C driver: %d\n",
8005998102bSMark Brown 		       ret);
8015998102bSMark Brown 	}
8025998102bSMark Brown #endif
8035998102bSMark Brown #if defined(CONFIG_SPI_MASTER)
8045998102bSMark Brown 	ret = spi_register_driver(&wm8731_spi_driver);
8055998102bSMark Brown 	if (ret != 0) {
8065998102bSMark Brown 		printk(KERN_ERR "Failed to register WM8731 SPI driver: %d\n",
8075998102bSMark Brown 		       ret);
8085998102bSMark Brown 	}
8095998102bSMark Brown #endif
8105998102bSMark Brown 	return 0;
81164089b84SMark Brown }
81264089b84SMark Brown module_init(wm8731_modinit);
81364089b84SMark Brown 
81464089b84SMark Brown static void __exit wm8731_exit(void)
81564089b84SMark Brown {
8165998102bSMark Brown #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
8175998102bSMark Brown 	i2c_del_driver(&wm8731_i2c_driver);
8185998102bSMark Brown #endif
8195998102bSMark Brown #if defined(CONFIG_SPI_MASTER)
8205998102bSMark Brown 	spi_unregister_driver(&wm8731_spi_driver);
8215998102bSMark Brown #endif
82264089b84SMark Brown }
82364089b84SMark Brown module_exit(wm8731_exit);
82464089b84SMark Brown 
82540e0aa64SRichard Purdie MODULE_DESCRIPTION("ASoC WM8731 driver");
82640e0aa64SRichard Purdie MODULE_AUTHOR("Richard Purdie");
82740e0aa64SRichard Purdie MODULE_LICENSE("GPL");
828