xref: /linux/sound/soc/codecs/wm8731.c (revision 7dea7c01dac9b74faa9afa93fc9bb5f2d37521dc)
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>
22*7dea7c01SMark Brown #include <linux/regulator/consumer.h>
23d2a40355SCliff Cai #include <linux/spi/spi.h>
2440e0aa64SRichard Purdie #include <sound/core.h>
2540e0aa64SRichard Purdie #include <sound/pcm.h>
2640e0aa64SRichard Purdie #include <sound/pcm_params.h>
2740e0aa64SRichard Purdie #include <sound/soc.h>
2840e0aa64SRichard Purdie #include <sound/soc-dapm.h>
2940e0aa64SRichard Purdie #include <sound/initval.h>
30d00efa64SMark Brown #include <sound/tlv.h>
3140e0aa64SRichard Purdie 
3240e0aa64SRichard Purdie #include "wm8731.h"
3340e0aa64SRichard Purdie 
345998102bSMark Brown static struct snd_soc_codec *wm8731_codec;
3540e0aa64SRichard Purdie struct snd_soc_codec_device soc_codec_dev_wm8731;
3640e0aa64SRichard Purdie 
37*7dea7c01SMark Brown #define WM8731_NUM_SUPPLIES 4
38*7dea7c01SMark Brown static const char *wm8731_supply_names[WM8731_NUM_SUPPLIES] = {
39*7dea7c01SMark Brown 	"AVDD",
40*7dea7c01SMark Brown 	"HPVDD",
41*7dea7c01SMark Brown 	"DCVDD",
42*7dea7c01SMark Brown 	"DBVDD",
43*7dea7c01SMark Brown };
44*7dea7c01SMark Brown 
45b36d61d4SFrank Mandarino /* codec private data */
46b36d61d4SFrank Mandarino struct wm8731_priv {
475998102bSMark Brown 	struct snd_soc_codec codec;
48*7dea7c01SMark Brown 	struct regulator_bulk_data supplies[WM8731_NUM_SUPPLIES];
495998102bSMark Brown 	u16 reg_cache[WM8731_CACHEREGNUM];
50b36d61d4SFrank Mandarino 	unsigned int sysclk;
51b36d61d4SFrank Mandarino };
52b36d61d4SFrank Mandarino 
53a8035c8fSMark Brown 
5440e0aa64SRichard Purdie /*
5540e0aa64SRichard Purdie  * wm8731 register cache
5640e0aa64SRichard Purdie  * We can't read the WM8731 register space when we are
5740e0aa64SRichard Purdie  * using 2 wire for device control, so we cache them instead.
5840e0aa64SRichard Purdie  * There is no point in caching the reset register
5940e0aa64SRichard Purdie  */
6040e0aa64SRichard Purdie static const u16 wm8731_reg[WM8731_CACHEREGNUM] = {
6140e0aa64SRichard Purdie 	0x0097, 0x0097, 0x0079, 0x0079,
6240e0aa64SRichard Purdie 	0x000a, 0x0008, 0x009f, 0x000a,
6340e0aa64SRichard Purdie 	0x0000, 0x0000
6440e0aa64SRichard Purdie };
6540e0aa64SRichard Purdie 
6617a52fd6SMark Brown #define wm8731_reset(c)	snd_soc_write(c, WM8731_RESET, 0)
6740e0aa64SRichard Purdie 
6840e0aa64SRichard Purdie static const char *wm8731_input_select[] = {"Line In", "Mic"};
6940e0aa64SRichard Purdie static const char *wm8731_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"};
7040e0aa64SRichard Purdie 
7140e0aa64SRichard Purdie static const struct soc_enum wm8731_enum[] = {
7240e0aa64SRichard Purdie 	SOC_ENUM_SINGLE(WM8731_APANA, 2, 2, wm8731_input_select),
7340e0aa64SRichard Purdie 	SOC_ENUM_SINGLE(WM8731_APDIGI, 1, 4, wm8731_deemph),
7440e0aa64SRichard Purdie };
7540e0aa64SRichard Purdie 
76d00efa64SMark Brown static const DECLARE_TLV_DB_SCALE(in_tlv, -3450, 150, 0);
77d00efa64SMark Brown static const DECLARE_TLV_DB_SCALE(sidetone_tlv, -1500, 300, 0);
78d00efa64SMark Brown static const DECLARE_TLV_DB_SCALE(out_tlv, -12100, 100, 1);
79d00efa64SMark Brown 
8040e0aa64SRichard Purdie static const struct snd_kcontrol_new wm8731_snd_controls[] = {
8140e0aa64SRichard Purdie 
82d00efa64SMark Brown SOC_DOUBLE_R_TLV("Master Playback Volume", WM8731_LOUT1V, WM8731_ROUT1V,
83d00efa64SMark Brown 		 0, 127, 0, out_tlv),
84bd903b6eSLiam Girdwood SOC_DOUBLE_R("Master Playback ZC Switch", WM8731_LOUT1V, WM8731_ROUT1V,
85bd903b6eSLiam Girdwood 	7, 1, 0),
8640e0aa64SRichard Purdie 
87d00efa64SMark Brown SOC_DOUBLE_R_TLV("Capture Volume", WM8731_LINVOL, WM8731_RINVOL, 0, 31, 0,
88d00efa64SMark Brown 		 in_tlv),
8940e0aa64SRichard Purdie SOC_DOUBLE_R("Line Capture Switch", WM8731_LINVOL, WM8731_RINVOL, 7, 1, 1),
9040e0aa64SRichard Purdie 
9140e0aa64SRichard Purdie SOC_SINGLE("Mic Boost (+20dB)", WM8731_APANA, 0, 1, 0),
92ef38ed88SMark Brown SOC_SINGLE("Mic Capture Switch", WM8731_APANA, 1, 1, 1),
9340e0aa64SRichard Purdie 
94d00efa64SMark Brown SOC_SINGLE_TLV("Sidetone Playback Volume", WM8731_APANA, 6, 3, 1,
95d00efa64SMark Brown 	       sidetone_tlv),
9640e0aa64SRichard Purdie 
9740e0aa64SRichard Purdie SOC_SINGLE("ADC High Pass Filter Switch", WM8731_APDIGI, 0, 1, 1),
9840e0aa64SRichard Purdie SOC_SINGLE("Store DC Offset Switch", WM8731_APDIGI, 4, 1, 0),
9940e0aa64SRichard Purdie 
10040e0aa64SRichard Purdie SOC_ENUM("Playback De-emphasis", wm8731_enum[1]),
10140e0aa64SRichard Purdie };
10240e0aa64SRichard Purdie 
10340e0aa64SRichard Purdie /* Output Mixer */
10440e0aa64SRichard Purdie static const struct snd_kcontrol_new wm8731_output_mixer_controls[] = {
10540e0aa64SRichard Purdie SOC_DAPM_SINGLE("Line Bypass Switch", WM8731_APANA, 3, 1, 0),
10640e0aa64SRichard Purdie SOC_DAPM_SINGLE("Mic Sidetone Switch", WM8731_APANA, 5, 1, 0),
10740e0aa64SRichard Purdie SOC_DAPM_SINGLE("HiFi Playback Switch", WM8731_APANA, 4, 1, 0),
10840e0aa64SRichard Purdie };
10940e0aa64SRichard Purdie 
11040e0aa64SRichard Purdie /* Input mux */
11140e0aa64SRichard Purdie static const struct snd_kcontrol_new wm8731_input_mux_controls =
11240e0aa64SRichard Purdie SOC_DAPM_ENUM("Input Select", wm8731_enum[0]);
11340e0aa64SRichard Purdie 
11440e0aa64SRichard Purdie static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
11540e0aa64SRichard Purdie SND_SOC_DAPM_MIXER("Output Mixer", WM8731_PWR, 4, 1,
11640e0aa64SRichard Purdie 	&wm8731_output_mixer_controls[0],
11740e0aa64SRichard Purdie 	ARRAY_SIZE(wm8731_output_mixer_controls)),
11840e0aa64SRichard Purdie SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8731_PWR, 3, 1),
11940e0aa64SRichard Purdie SND_SOC_DAPM_OUTPUT("LOUT"),
12040e0aa64SRichard Purdie SND_SOC_DAPM_OUTPUT("LHPOUT"),
12140e0aa64SRichard Purdie SND_SOC_DAPM_OUTPUT("ROUT"),
12240e0aa64SRichard Purdie SND_SOC_DAPM_OUTPUT("RHPOUT"),
12340e0aa64SRichard Purdie SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8731_PWR, 2, 1),
12440e0aa64SRichard Purdie SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &wm8731_input_mux_controls),
12540e0aa64SRichard Purdie SND_SOC_DAPM_PGA("Line Input", WM8731_PWR, 0, 1, NULL, 0),
12640e0aa64SRichard Purdie SND_SOC_DAPM_MICBIAS("Mic Bias", WM8731_PWR, 1, 1),
12740e0aa64SRichard Purdie SND_SOC_DAPM_INPUT("MICIN"),
12840e0aa64SRichard Purdie SND_SOC_DAPM_INPUT("RLINEIN"),
12940e0aa64SRichard Purdie SND_SOC_DAPM_INPUT("LLINEIN"),
13040e0aa64SRichard Purdie };
13140e0aa64SRichard Purdie 
132a65f0568SMark Brown static const struct snd_soc_dapm_route intercon[] = {
13340e0aa64SRichard Purdie 	/* output mixer */
13440e0aa64SRichard Purdie 	{"Output Mixer", "Line Bypass Switch", "Line Input"},
13540e0aa64SRichard Purdie 	{"Output Mixer", "HiFi Playback Switch", "DAC"},
13640e0aa64SRichard Purdie 	{"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
13740e0aa64SRichard Purdie 
13840e0aa64SRichard Purdie 	/* outputs */
13940e0aa64SRichard Purdie 	{"RHPOUT", NULL, "Output Mixer"},
14040e0aa64SRichard Purdie 	{"ROUT", NULL, "Output Mixer"},
14140e0aa64SRichard Purdie 	{"LHPOUT", NULL, "Output Mixer"},
14240e0aa64SRichard Purdie 	{"LOUT", NULL, "Output Mixer"},
14340e0aa64SRichard Purdie 
14440e0aa64SRichard Purdie 	/* input mux */
14540e0aa64SRichard Purdie 	{"Input Mux", "Line In", "Line Input"},
14640e0aa64SRichard Purdie 	{"Input Mux", "Mic", "Mic Bias"},
14740e0aa64SRichard Purdie 	{"ADC", NULL, "Input Mux"},
14840e0aa64SRichard Purdie 
14940e0aa64SRichard Purdie 	/* inputs */
15040e0aa64SRichard Purdie 	{"Line Input", NULL, "LLINEIN"},
15140e0aa64SRichard Purdie 	{"Line Input", NULL, "RLINEIN"},
15240e0aa64SRichard Purdie 	{"Mic Bias", NULL, "MICIN"},
15340e0aa64SRichard Purdie };
15440e0aa64SRichard Purdie 
15540e0aa64SRichard Purdie static int wm8731_add_widgets(struct snd_soc_codec *codec)
15640e0aa64SRichard Purdie {
157a65f0568SMark Brown 	snd_soc_dapm_new_controls(codec, wm8731_dapm_widgets,
158a65f0568SMark Brown 				  ARRAY_SIZE(wm8731_dapm_widgets));
15940e0aa64SRichard Purdie 
160a65f0568SMark Brown 	snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon));
16140e0aa64SRichard Purdie 
16240e0aa64SRichard Purdie 	snd_soc_dapm_new_widgets(codec);
16340e0aa64SRichard Purdie 	return 0;
16440e0aa64SRichard Purdie }
16540e0aa64SRichard Purdie 
16640e0aa64SRichard Purdie struct _coeff_div {
16740e0aa64SRichard Purdie 	u32 mclk;
16840e0aa64SRichard Purdie 	u32 rate;
16940e0aa64SRichard Purdie 	u16 fs;
17040e0aa64SRichard Purdie 	u8 sr:4;
17140e0aa64SRichard Purdie 	u8 bosr:1;
17240e0aa64SRichard Purdie 	u8 usb:1;
17340e0aa64SRichard Purdie };
17440e0aa64SRichard Purdie 
17540e0aa64SRichard Purdie /* codec mclk clock divider coefficients */
17640e0aa64SRichard Purdie static const struct _coeff_div coeff_div[] = {
17740e0aa64SRichard Purdie 	/* 48k */
17840e0aa64SRichard Purdie 	{12288000, 48000, 256, 0x0, 0x0, 0x0},
17940e0aa64SRichard Purdie 	{18432000, 48000, 384, 0x0, 0x1, 0x0},
18040e0aa64SRichard Purdie 	{12000000, 48000, 250, 0x0, 0x0, 0x1},
18140e0aa64SRichard Purdie 
18240e0aa64SRichard Purdie 	/* 32k */
18340e0aa64SRichard Purdie 	{12288000, 32000, 384, 0x6, 0x0, 0x0},
18440e0aa64SRichard Purdie 	{18432000, 32000, 576, 0x6, 0x1, 0x0},
185298a2c75SFrank Mandarino 	{12000000, 32000, 375, 0x6, 0x0, 0x1},
18640e0aa64SRichard Purdie 
18740e0aa64SRichard Purdie 	/* 8k */
18840e0aa64SRichard Purdie 	{12288000, 8000, 1536, 0x3, 0x0, 0x0},
18940e0aa64SRichard Purdie 	{18432000, 8000, 2304, 0x3, 0x1, 0x0},
19040e0aa64SRichard Purdie 	{11289600, 8000, 1408, 0xb, 0x0, 0x0},
19140e0aa64SRichard Purdie 	{16934400, 8000, 2112, 0xb, 0x1, 0x0},
19240e0aa64SRichard Purdie 	{12000000, 8000, 1500, 0x3, 0x0, 0x1},
19340e0aa64SRichard Purdie 
19440e0aa64SRichard Purdie 	/* 96k */
19540e0aa64SRichard Purdie 	{12288000, 96000, 128, 0x7, 0x0, 0x0},
19640e0aa64SRichard Purdie 	{18432000, 96000, 192, 0x7, 0x1, 0x0},
19740e0aa64SRichard Purdie 	{12000000, 96000, 125, 0x7, 0x0, 0x1},
19840e0aa64SRichard Purdie 
19940e0aa64SRichard Purdie 	/* 44.1k */
20040e0aa64SRichard Purdie 	{11289600, 44100, 256, 0x8, 0x0, 0x0},
20140e0aa64SRichard Purdie 	{16934400, 44100, 384, 0x8, 0x1, 0x0},
20240e0aa64SRichard Purdie 	{12000000, 44100, 272, 0x8, 0x1, 0x1},
20340e0aa64SRichard Purdie 
20440e0aa64SRichard Purdie 	/* 88.2k */
20540e0aa64SRichard Purdie 	{11289600, 88200, 128, 0xf, 0x0, 0x0},
20640e0aa64SRichard Purdie 	{16934400, 88200, 192, 0xf, 0x1, 0x0},
20740e0aa64SRichard Purdie 	{12000000, 88200, 136, 0xf, 0x1, 0x1},
20840e0aa64SRichard Purdie };
20940e0aa64SRichard Purdie 
21040e0aa64SRichard Purdie static inline int get_coeff(int mclk, int rate)
21140e0aa64SRichard Purdie {
21240e0aa64SRichard Purdie 	int i;
21340e0aa64SRichard Purdie 
21440e0aa64SRichard Purdie 	for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
21540e0aa64SRichard Purdie 		if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
21640e0aa64SRichard Purdie 			return i;
21740e0aa64SRichard Purdie 	}
21840e0aa64SRichard Purdie 	return 0;
21940e0aa64SRichard Purdie }
22040e0aa64SRichard Purdie 
221b36d61d4SFrank Mandarino static int wm8731_hw_params(struct snd_pcm_substream *substream,
222dee89c4dSMark Brown 			    struct snd_pcm_hw_params *params,
223dee89c4dSMark Brown 			    struct snd_soc_dai *dai)
22440e0aa64SRichard Purdie {
225b36d61d4SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
226b36d61d4SFrank Mandarino 	struct snd_soc_device *socdev = rtd->socdev;
2276627a653SMark Brown 	struct snd_soc_codec *codec = socdev->card->codec;
228b36d61d4SFrank Mandarino 	struct wm8731_priv *wm8731 = codec->private_data;
22917a52fd6SMark Brown 	u16 iface = snd_soc_read(codec, WM8731_IFACE) & 0xfff3;
230b36d61d4SFrank Mandarino 	int i = get_coeff(wm8731->sysclk, params_rate(params));
231b36d61d4SFrank Mandarino 	u16 srate = (coeff_div[i].sr << 2) |
232b36d61d4SFrank Mandarino 		(coeff_div[i].bosr << 1) | coeff_div[i].usb;
23340e0aa64SRichard Purdie 
23417a52fd6SMark Brown 	snd_soc_write(codec, WM8731_SRATE, srate);
23540e0aa64SRichard Purdie 
236b36d61d4SFrank Mandarino 	/* bit size */
237b36d61d4SFrank Mandarino 	switch (params_format(params)) {
238b36d61d4SFrank Mandarino 	case SNDRV_PCM_FORMAT_S16_LE:
239b36d61d4SFrank Mandarino 		break;
240b36d61d4SFrank Mandarino 	case SNDRV_PCM_FORMAT_S20_3LE:
241b36d61d4SFrank Mandarino 		iface |= 0x0004;
242b36d61d4SFrank Mandarino 		break;
243b36d61d4SFrank Mandarino 	case SNDRV_PCM_FORMAT_S24_LE:
244b36d61d4SFrank Mandarino 		iface |= 0x0008;
245b36d61d4SFrank Mandarino 		break;
246b36d61d4SFrank Mandarino 	}
247b36d61d4SFrank Mandarino 
24817a52fd6SMark Brown 	snd_soc_write(codec, WM8731_IFACE, iface);
249b36d61d4SFrank Mandarino 	return 0;
25040e0aa64SRichard Purdie }
25140e0aa64SRichard Purdie 
252dee89c4dSMark Brown static int wm8731_pcm_prepare(struct snd_pcm_substream *substream,
253dee89c4dSMark Brown 			      struct snd_soc_dai *dai)
25440e0aa64SRichard Purdie {
25540e0aa64SRichard Purdie 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
25640e0aa64SRichard Purdie 	struct snd_soc_device *socdev = rtd->socdev;
2576627a653SMark Brown 	struct snd_soc_codec *codec = socdev->card->codec;
25840e0aa64SRichard Purdie 
25940e0aa64SRichard Purdie 	/* set active */
26017a52fd6SMark Brown 	snd_soc_write(codec, WM8731_ACTIVE, 0x0001);
261b36d61d4SFrank Mandarino 
26240e0aa64SRichard Purdie 	return 0;
26340e0aa64SRichard Purdie }
26440e0aa64SRichard Purdie 
265dee89c4dSMark Brown static void wm8731_shutdown(struct snd_pcm_substream *substream,
266dee89c4dSMark Brown 			    struct snd_soc_dai *dai)
26740e0aa64SRichard Purdie {
26840e0aa64SRichard Purdie 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
26940e0aa64SRichard Purdie 	struct snd_soc_device *socdev = rtd->socdev;
2706627a653SMark Brown 	struct snd_soc_codec *codec = socdev->card->codec;
27140e0aa64SRichard Purdie 
27240e0aa64SRichard Purdie 	/* deactivate */
27340e0aa64SRichard Purdie 	if (!codec->active) {
27440e0aa64SRichard Purdie 		udelay(50);
27517a52fd6SMark Brown 		snd_soc_write(codec, WM8731_ACTIVE, 0x0);
27640e0aa64SRichard Purdie 	}
27740e0aa64SRichard Purdie }
27840e0aa64SRichard Purdie 
279e550e17fSLiam Girdwood static int wm8731_mute(struct snd_soc_dai *dai, int mute)
28040e0aa64SRichard Purdie {
281b36d61d4SFrank Mandarino 	struct snd_soc_codec *codec = dai->codec;
28217a52fd6SMark Brown 	u16 mute_reg = snd_soc_read(codec, WM8731_APDIGI) & 0xfff7;
283b36d61d4SFrank Mandarino 
28440e0aa64SRichard Purdie 	if (mute)
28517a52fd6SMark Brown 		snd_soc_write(codec, WM8731_APDIGI, mute_reg | 0x8);
28640e0aa64SRichard Purdie 	else
28717a52fd6SMark Brown 		snd_soc_write(codec, WM8731_APDIGI, mute_reg);
28840e0aa64SRichard Purdie 	return 0;
28940e0aa64SRichard Purdie }
29040e0aa64SRichard Purdie 
291e550e17fSLiam Girdwood static int wm8731_set_dai_sysclk(struct snd_soc_dai *codec_dai,
292b36d61d4SFrank Mandarino 		int clk_id, unsigned int freq, int dir)
293b36d61d4SFrank Mandarino {
294b36d61d4SFrank Mandarino 	struct snd_soc_codec *codec = codec_dai->codec;
295b36d61d4SFrank Mandarino 	struct wm8731_priv *wm8731 = codec->private_data;
296b36d61d4SFrank Mandarino 
297b36d61d4SFrank Mandarino 	switch (freq) {
298b36d61d4SFrank Mandarino 	case 11289600:
299b36d61d4SFrank Mandarino 	case 12000000:
300b36d61d4SFrank Mandarino 	case 12288000:
301b36d61d4SFrank Mandarino 	case 16934400:
302b36d61d4SFrank Mandarino 	case 18432000:
303b36d61d4SFrank Mandarino 		wm8731->sysclk = freq;
304b36d61d4SFrank Mandarino 		return 0;
305b36d61d4SFrank Mandarino 	}
306b36d61d4SFrank Mandarino 	return -EINVAL;
307b36d61d4SFrank Mandarino }
308b36d61d4SFrank Mandarino 
309b36d61d4SFrank Mandarino 
310e550e17fSLiam Girdwood static int wm8731_set_dai_fmt(struct snd_soc_dai *codec_dai,
311b36d61d4SFrank Mandarino 		unsigned int fmt)
312b36d61d4SFrank Mandarino {
313b36d61d4SFrank Mandarino 	struct snd_soc_codec *codec = codec_dai->codec;
314b36d61d4SFrank Mandarino 	u16 iface = 0;
315b36d61d4SFrank Mandarino 
316b36d61d4SFrank Mandarino 	/* set master/slave audio interface */
317b36d61d4SFrank Mandarino 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
318b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_CBM_CFM:
319b36d61d4SFrank Mandarino 		iface |= 0x0040;
320b36d61d4SFrank Mandarino 		break;
321b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_CBS_CFS:
322b36d61d4SFrank Mandarino 		break;
323b36d61d4SFrank Mandarino 	default:
324b36d61d4SFrank Mandarino 		return -EINVAL;
325b36d61d4SFrank Mandarino 	}
326b36d61d4SFrank Mandarino 
327b36d61d4SFrank Mandarino 	/* interface format */
328b36d61d4SFrank Mandarino 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
329b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_I2S:
330b36d61d4SFrank Mandarino 		iface |= 0x0002;
331b36d61d4SFrank Mandarino 		break;
332b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_RIGHT_J:
333b36d61d4SFrank Mandarino 		break;
334b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_LEFT_J:
335b36d61d4SFrank Mandarino 		iface |= 0x0001;
336b36d61d4SFrank Mandarino 		break;
337b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_DSP_A:
338b36d61d4SFrank Mandarino 		iface |= 0x0003;
339b36d61d4SFrank Mandarino 		break;
340b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_DSP_B:
341b36d61d4SFrank Mandarino 		iface |= 0x0013;
342b36d61d4SFrank Mandarino 		break;
343b36d61d4SFrank Mandarino 	default:
344b36d61d4SFrank Mandarino 		return -EINVAL;
345b36d61d4SFrank Mandarino 	}
346b36d61d4SFrank Mandarino 
347b36d61d4SFrank Mandarino 	/* clock inversion */
348b36d61d4SFrank Mandarino 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
349b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_NB_NF:
350b36d61d4SFrank Mandarino 		break;
351b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_IB_IF:
352b36d61d4SFrank Mandarino 		iface |= 0x0090;
353b36d61d4SFrank Mandarino 		break;
354b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_IB_NF:
355b36d61d4SFrank Mandarino 		iface |= 0x0080;
356b36d61d4SFrank Mandarino 		break;
357b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_NB_IF:
358b36d61d4SFrank Mandarino 		iface |= 0x0010;
359b36d61d4SFrank Mandarino 		break;
360b36d61d4SFrank Mandarino 	default:
361b36d61d4SFrank Mandarino 		return -EINVAL;
362b36d61d4SFrank Mandarino 	}
363b36d61d4SFrank Mandarino 
364b36d61d4SFrank Mandarino 	/* set iface */
36517a52fd6SMark Brown 	snd_soc_write(codec, WM8731_IFACE, iface);
366b36d61d4SFrank Mandarino 	return 0;
367b36d61d4SFrank Mandarino }
368b36d61d4SFrank Mandarino 
3690be9898aSMark Brown static int wm8731_set_bias_level(struct snd_soc_codec *codec,
3700be9898aSMark Brown 				 enum snd_soc_bias_level level)
37140e0aa64SRichard Purdie {
37222d22ee5SMark Brown 	u16 reg;
37340e0aa64SRichard Purdie 
3740be9898aSMark Brown 	switch (level) {
3750be9898aSMark Brown 	case SND_SOC_BIAS_ON:
37640e0aa64SRichard Purdie 		break;
3770be9898aSMark Brown 	case SND_SOC_BIAS_PREPARE:
37840e0aa64SRichard Purdie 		break;
3790be9898aSMark Brown 	case SND_SOC_BIAS_STANDBY:
38022d22ee5SMark Brown 		/* Clear PWROFF, gate CLKOUT, everything else as-is */
38117a52fd6SMark Brown 		reg = snd_soc_read(codec, WM8731_PWR) & 0xff7f;
38217a52fd6SMark Brown 		snd_soc_write(codec, WM8731_PWR, reg | 0x0040);
38340e0aa64SRichard Purdie 		break;
3840be9898aSMark Brown 	case SND_SOC_BIAS_OFF:
38517a52fd6SMark Brown 		snd_soc_write(codec, WM8731_ACTIVE, 0x0);
38617a52fd6SMark Brown 		snd_soc_write(codec, WM8731_PWR, 0xffff);
38740e0aa64SRichard Purdie 		break;
38840e0aa64SRichard Purdie 	}
3890be9898aSMark Brown 	codec->bias_level = level;
39040e0aa64SRichard Purdie 	return 0;
39140e0aa64SRichard Purdie }
39240e0aa64SRichard Purdie 
393b36d61d4SFrank Mandarino #define WM8731_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
394b36d61d4SFrank Mandarino 		SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
395b36d61d4SFrank Mandarino 		SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\
396b36d61d4SFrank Mandarino 		SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |\
397b36d61d4SFrank Mandarino 		SNDRV_PCM_RATE_96000)
398b36d61d4SFrank Mandarino 
399b36d61d4SFrank Mandarino #define WM8731_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
400b36d61d4SFrank Mandarino 	SNDRV_PCM_FMTBIT_S24_LE)
401b36d61d4SFrank Mandarino 
4026335d055SEric Miao static struct snd_soc_dai_ops wm8731_dai_ops = {
4036335d055SEric Miao 	.prepare	= wm8731_pcm_prepare,
4046335d055SEric Miao 	.hw_params	= wm8731_hw_params,
4056335d055SEric Miao 	.shutdown	= wm8731_shutdown,
4066335d055SEric Miao 	.digital_mute	= wm8731_mute,
4076335d055SEric Miao 	.set_sysclk	= wm8731_set_dai_sysclk,
4086335d055SEric Miao 	.set_fmt	= wm8731_set_dai_fmt,
4096335d055SEric Miao };
4106335d055SEric Miao 
411e550e17fSLiam Girdwood struct snd_soc_dai wm8731_dai = {
41240e0aa64SRichard Purdie 	.name = "WM8731",
41340e0aa64SRichard Purdie 	.playback = {
41440e0aa64SRichard Purdie 		.stream_name = "Playback",
41540e0aa64SRichard Purdie 		.channels_min = 1,
41640e0aa64SRichard Purdie 		.channels_max = 2,
417b36d61d4SFrank Mandarino 		.rates = WM8731_RATES,
418b36d61d4SFrank Mandarino 		.formats = WM8731_FORMATS,},
41940e0aa64SRichard Purdie 	.capture = {
42040e0aa64SRichard Purdie 		.stream_name = "Capture",
42140e0aa64SRichard Purdie 		.channels_min = 1,
42240e0aa64SRichard Purdie 		.channels_max = 2,
423b36d61d4SFrank Mandarino 		.rates = WM8731_RATES,
424b36d61d4SFrank Mandarino 		.formats = WM8731_FORMATS,},
4256335d055SEric Miao 	.ops = &wm8731_dai_ops,
4264934482dSMark Brown 	.symmetric_rates = 1,
42740e0aa64SRichard Purdie };
42840e0aa64SRichard Purdie EXPORT_SYMBOL_GPL(wm8731_dai);
42940e0aa64SRichard Purdie 
430b3b50b3fSMark Brown #ifdef CONFIG_PM
43140e0aa64SRichard Purdie static int wm8731_suspend(struct platform_device *pdev, pm_message_t state)
43240e0aa64SRichard Purdie {
43340e0aa64SRichard Purdie 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
4346627a653SMark Brown 	struct snd_soc_codec *codec = socdev->card->codec;
435*7dea7c01SMark Brown 	struct wm8731_priv *wm8731 = codec->private_data;
43640e0aa64SRichard Purdie 
43717a52fd6SMark Brown 	snd_soc_write(codec, WM8731_ACTIVE, 0x0);
4380be9898aSMark Brown 	wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF);
439*7dea7c01SMark Brown 	regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies),
440*7dea7c01SMark Brown 			       wm8731->supplies);
44140e0aa64SRichard Purdie 	return 0;
44240e0aa64SRichard Purdie }
44340e0aa64SRichard Purdie 
44440e0aa64SRichard Purdie static int wm8731_resume(struct platform_device *pdev)
44540e0aa64SRichard Purdie {
44640e0aa64SRichard Purdie 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
4476627a653SMark Brown 	struct snd_soc_codec *codec = socdev->card->codec;
448*7dea7c01SMark Brown 	struct wm8731_priv *wm8731 = codec->private_data;
449*7dea7c01SMark Brown 	int i, ret;
45040e0aa64SRichard Purdie 	u8 data[2];
45140e0aa64SRichard Purdie 	u16 *cache = codec->reg_cache;
45240e0aa64SRichard Purdie 
453*7dea7c01SMark Brown 	ret = regulator_bulk_enable(ARRAY_SIZE(wm8731->supplies),
454*7dea7c01SMark Brown 				    wm8731->supplies);
455*7dea7c01SMark Brown 	if (ret != 0)
456*7dea7c01SMark Brown 		return ret;
457*7dea7c01SMark Brown 
45840e0aa64SRichard Purdie 	/* Sync reg_cache with the hardware */
45940e0aa64SRichard Purdie 	for (i = 0; i < ARRAY_SIZE(wm8731_reg); i++) {
46040e0aa64SRichard Purdie 		data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001);
46140e0aa64SRichard Purdie 		data[1] = cache[i] & 0x00ff;
46240e0aa64SRichard Purdie 		codec->hw_write(codec->control_data, data, 2);
46340e0aa64SRichard Purdie 	}
4640be9898aSMark Brown 	wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
4650be9898aSMark Brown 	wm8731_set_bias_level(codec, codec->suspend_bias_level);
466*7dea7c01SMark Brown 
46740e0aa64SRichard Purdie 	return 0;
46840e0aa64SRichard Purdie }
469b3b50b3fSMark Brown #else
470b3b50b3fSMark Brown #define wm8731_suspend NULL
471b3b50b3fSMark Brown #define wm8731_resume NULL
472b3b50b3fSMark Brown #endif
47340e0aa64SRichard Purdie 
4745998102bSMark Brown static int wm8731_probe(struct platform_device *pdev)
47540e0aa64SRichard Purdie {
4765998102bSMark Brown 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
4775998102bSMark Brown 	struct snd_soc_codec *codec;
4785998102bSMark Brown 	int ret = 0;
47940e0aa64SRichard Purdie 
4805998102bSMark Brown 	if (wm8731_codec == NULL) {
4815998102bSMark Brown 		dev_err(&pdev->dev, "Codec device not registered\n");
4825998102bSMark Brown 		return -ENODEV;
4835998102bSMark Brown 	}
48440e0aa64SRichard Purdie 
4855998102bSMark Brown 	socdev->card->codec = wm8731_codec;
4865998102bSMark Brown 	codec = wm8731_codec;
48740e0aa64SRichard Purdie 
48840e0aa64SRichard Purdie 	/* register pcms */
48940e0aa64SRichard Purdie 	ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
49040e0aa64SRichard Purdie 	if (ret < 0) {
4915998102bSMark Brown 		dev_err(codec->dev, "failed to create pcms: %d\n", ret);
492e35115a5SLiam Girdwood 		goto pcm_err;
49340e0aa64SRichard Purdie 	}
49440e0aa64SRichard Purdie 
4953e8e1952SIan Molton 	snd_soc_add_controls(codec, wm8731_snd_controls,
4963e8e1952SIan Molton 			     ARRAY_SIZE(wm8731_snd_controls));
49740e0aa64SRichard Purdie 	wm8731_add_widgets(codec);
498968a6025SMark Brown 	ret = snd_soc_init_card(socdev);
49940e0aa64SRichard Purdie 	if (ret < 0) {
5005998102bSMark Brown 		dev_err(codec->dev, "failed to register card: %d\n", ret);
501e35115a5SLiam Girdwood 		goto card_err;
50240e0aa64SRichard Purdie 	}
50340e0aa64SRichard Purdie 
50440e0aa64SRichard Purdie 	return ret;
505e35115a5SLiam Girdwood 
506e35115a5SLiam Girdwood card_err:
507e35115a5SLiam Girdwood 	snd_soc_free_pcms(socdev);
508e35115a5SLiam Girdwood 	snd_soc_dapm_free(socdev);
509e35115a5SLiam Girdwood pcm_err:
51040e0aa64SRichard Purdie 	return ret;
51140e0aa64SRichard Purdie }
51240e0aa64SRichard Purdie 
51340e0aa64SRichard Purdie /* power down chip */
51440e0aa64SRichard Purdie static int wm8731_remove(struct platform_device *pdev)
51540e0aa64SRichard Purdie {
51640e0aa64SRichard Purdie 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
51740e0aa64SRichard Purdie 
51840e0aa64SRichard Purdie 	snd_soc_free_pcms(socdev);
51940e0aa64SRichard Purdie 	snd_soc_dapm_free(socdev);
52040e0aa64SRichard Purdie 
52140e0aa64SRichard Purdie 	return 0;
52240e0aa64SRichard Purdie }
52340e0aa64SRichard Purdie 
52440e0aa64SRichard Purdie struct snd_soc_codec_device soc_codec_dev_wm8731 = {
52540e0aa64SRichard Purdie 	.probe = 	wm8731_probe,
52640e0aa64SRichard Purdie 	.remove = 	wm8731_remove,
52740e0aa64SRichard Purdie 	.suspend = 	wm8731_suspend,
52840e0aa64SRichard Purdie 	.resume =	wm8731_resume,
52940e0aa64SRichard Purdie };
53040e0aa64SRichard Purdie EXPORT_SYMBOL_GPL(soc_codec_dev_wm8731);
53140e0aa64SRichard Purdie 
5327084a42bSMark Brown static int wm8731_register(struct wm8731_priv *wm8731,
5337084a42bSMark Brown 			   enum snd_soc_control_type control)
534a8035c8fSMark Brown {
535*7dea7c01SMark Brown 	int ret, i;
5365998102bSMark Brown 	struct snd_soc_codec *codec = &wm8731->codec;
537a8035c8fSMark Brown 
5385998102bSMark Brown 	if (wm8731_codec) {
5395998102bSMark Brown 		dev_err(codec->dev, "Another WM8731 is registered\n");
540fe5422fcSMark Brown 		ret = -EINVAL;
541fe5422fcSMark Brown 		goto err;
5425998102bSMark Brown 	}
543a8035c8fSMark Brown 
5445998102bSMark Brown 	mutex_init(&codec->mutex);
5455998102bSMark Brown 	INIT_LIST_HEAD(&codec->dapm_widgets);
5465998102bSMark Brown 	INIT_LIST_HEAD(&codec->dapm_paths);
547a8035c8fSMark Brown 
5485998102bSMark Brown 	codec->private_data = wm8731;
5495998102bSMark Brown 	codec->name = "WM8731";
5505998102bSMark Brown 	codec->owner = THIS_MODULE;
5515998102bSMark Brown 	codec->bias_level = SND_SOC_BIAS_OFF;
5525998102bSMark Brown 	codec->set_bias_level = wm8731_set_bias_level;
5535998102bSMark Brown 	codec->dai = &wm8731_dai;
5545998102bSMark Brown 	codec->num_dai = 1;
5555998102bSMark Brown 	codec->reg_cache_size = WM8731_CACHEREGNUM;
5565998102bSMark Brown 	codec->reg_cache = &wm8731->reg_cache;
5575998102bSMark Brown 
5585998102bSMark Brown 	memcpy(codec->reg_cache, wm8731_reg, sizeof(wm8731_reg));
5595998102bSMark Brown 
5607084a42bSMark Brown 	ret = snd_soc_codec_set_cache_io(codec, 7, 9, control);
56117a52fd6SMark Brown 	if (ret < 0) {
56217a52fd6SMark Brown 		dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
56317a52fd6SMark Brown 		goto err;
56417a52fd6SMark Brown 	}
56517a52fd6SMark Brown 
566*7dea7c01SMark Brown 	for (i = 0; i < ARRAY_SIZE(wm8731->supplies); i++)
567*7dea7c01SMark Brown 		wm8731->supplies[i].supply = wm8731_supply_names[i];
568*7dea7c01SMark Brown 
569*7dea7c01SMark Brown 	ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(wm8731->supplies),
570*7dea7c01SMark Brown 				 wm8731->supplies);
571*7dea7c01SMark Brown 	if (ret != 0) {
572*7dea7c01SMark Brown 		dev_err(codec->dev, "Failed to request supplies: %d\n", ret);
573*7dea7c01SMark Brown 		goto err;
574*7dea7c01SMark Brown 	}
575*7dea7c01SMark Brown 
576*7dea7c01SMark Brown 	ret = regulator_bulk_enable(ARRAY_SIZE(wm8731->supplies),
577*7dea7c01SMark Brown 				    wm8731->supplies);
578*7dea7c01SMark Brown 	if (ret != 0) {
579*7dea7c01SMark Brown 		dev_err(codec->dev, "Failed to enable supplies: %d\n", ret);
580*7dea7c01SMark Brown 		goto err_regulator_get;
581*7dea7c01SMark Brown 	}
582*7dea7c01SMark Brown 
583519cf2dfSMark Brown 	ret = wm8731_reset(codec);
584519cf2dfSMark Brown 	if (ret < 0) {
585fe5422fcSMark Brown 		dev_err(codec->dev, "Failed to issue reset: %d\n", ret);
586*7dea7c01SMark Brown 		goto err_regulator_enable;
587519cf2dfSMark Brown 	}
588519cf2dfSMark Brown 
5895998102bSMark Brown 	wm8731_dai.dev = codec->dev;
5905998102bSMark Brown 
5915998102bSMark Brown 	wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
5925998102bSMark Brown 
5935998102bSMark Brown 	/* Latch the update bits */
59417a52fd6SMark Brown 	snd_soc_update_bits(codec, WM8731_LOUT1V, 0x100, 0);
59517a52fd6SMark Brown 	snd_soc_update_bits(codec, WM8731_ROUT1V, 0x100, 0);
59617a52fd6SMark Brown 	snd_soc_update_bits(codec, WM8731_LINVOL, 0x100, 0);
59717a52fd6SMark Brown 	snd_soc_update_bits(codec, WM8731_RINVOL, 0x100, 0);
5985998102bSMark Brown 
599ce3bdaa8SMark Brown 	/* Disable bypass path by default */
60017a52fd6SMark Brown 	snd_soc_update_bits(codec, WM8731_APANA, 0x4, 0);
601ce3bdaa8SMark Brown 
6025998102bSMark Brown 	wm8731_codec = codec;
6035998102bSMark Brown 
6045998102bSMark Brown 	ret = snd_soc_register_codec(codec);
6055998102bSMark Brown 	if (ret != 0) {
6065998102bSMark Brown 		dev_err(codec->dev, "Failed to register codec: %d\n", ret);
607*7dea7c01SMark Brown 		goto err_regulator_enable;
608a8035c8fSMark Brown 	}
609a8035c8fSMark Brown 
6105998102bSMark Brown 	ret = snd_soc_register_dai(&wm8731_dai);
6115998102bSMark Brown 	if (ret != 0) {
6125998102bSMark Brown 		dev_err(codec->dev, "Failed to register DAI: %d\n", ret);
6135998102bSMark Brown 		snd_soc_unregister_codec(codec);
614fe5422fcSMark Brown 		goto err_codec;
6155998102bSMark Brown 	}
6165998102bSMark Brown 
617a8035c8fSMark Brown 	return 0;
618fe5422fcSMark Brown 
619fe5422fcSMark Brown err_codec:
620fe5422fcSMark Brown 	snd_soc_unregister_codec(codec);
621*7dea7c01SMark Brown err_regulator_enable:
622*7dea7c01SMark Brown 	regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies);
623*7dea7c01SMark Brown err_regulator_get:
624*7dea7c01SMark Brown 	regulator_bulk_free(ARRAY_SIZE(wm8731->supplies), wm8731->supplies);
625fe5422fcSMark Brown err:
626fe5422fcSMark Brown 	kfree(wm8731);
627fe5422fcSMark Brown 	return ret;
628a8035c8fSMark Brown }
629a8035c8fSMark Brown 
6305998102bSMark Brown static void wm8731_unregister(struct wm8731_priv *wm8731)
6315998102bSMark Brown {
6325998102bSMark Brown 	wm8731_set_bias_level(&wm8731->codec, SND_SOC_BIAS_OFF);
6335998102bSMark Brown 	snd_soc_unregister_dai(&wm8731_dai);
6345998102bSMark Brown 	snd_soc_unregister_codec(&wm8731->codec);
635*7dea7c01SMark Brown 	regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies);
636*7dea7c01SMark Brown 	regulator_bulk_free(ARRAY_SIZE(wm8731->supplies), wm8731->supplies);
6375998102bSMark Brown 	kfree(wm8731);
6385998102bSMark Brown 	wm8731_codec = NULL;
6395998102bSMark Brown }
640a8035c8fSMark Brown 
6415998102bSMark Brown #if defined(CONFIG_SPI_MASTER)
6425998102bSMark Brown static int __devinit wm8731_spi_probe(struct spi_device *spi)
6435998102bSMark Brown {
6445998102bSMark Brown 	struct snd_soc_codec *codec;
6455998102bSMark Brown 	struct wm8731_priv *wm8731;
6465998102bSMark Brown 
6475998102bSMark Brown 	wm8731 = kzalloc(sizeof(struct wm8731_priv), GFP_KERNEL);
6485998102bSMark Brown 	if (wm8731 == NULL)
6495998102bSMark Brown 		return -ENOMEM;
6505998102bSMark Brown 
6515998102bSMark Brown 	codec = &wm8731->codec;
6525998102bSMark Brown 	codec->control_data = spi;
6535998102bSMark Brown 	codec->dev = &spi->dev;
6545998102bSMark Brown 
655ae31c1fbSGreg Kroah-Hartman 	dev_set_drvdata(&spi->dev, wm8731);
65693b760b7SMark Brown 
6577084a42bSMark Brown 	return wm8731_register(wm8731, SND_SOC_SPI);
6585998102bSMark Brown }
6595998102bSMark Brown 
6605998102bSMark Brown static int __devexit wm8731_spi_remove(struct spi_device *spi)
6615998102bSMark Brown {
662ae31c1fbSGreg Kroah-Hartman 	struct wm8731_priv *wm8731 = dev_get_drvdata(&spi->dev);
66393b760b7SMark Brown 
66493b760b7SMark Brown 	wm8731_unregister(wm8731);
66593b760b7SMark Brown 
6665998102bSMark Brown 	return 0;
6675998102bSMark Brown }
6685998102bSMark Brown 
6695998102bSMark Brown static struct spi_driver wm8731_spi_driver = {
6705998102bSMark Brown 	.driver = {
6715998102bSMark Brown 		.name	= "wm8731",
6725998102bSMark Brown 		.bus	= &spi_bus_type,
6735998102bSMark Brown 		.owner	= THIS_MODULE,
6745998102bSMark Brown 	},
6755998102bSMark Brown 	.probe		= wm8731_spi_probe,
6765998102bSMark Brown 	.remove		= __devexit_p(wm8731_spi_remove),
6775998102bSMark Brown };
678a8035c8fSMark Brown #endif /* CONFIG_SPI_MASTER */
679a8035c8fSMark Brown 
680a8035c8fSMark Brown #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
681c6f29811SMark Brown static __devinit int wm8731_i2c_probe(struct i2c_client *i2c,
682a8035c8fSMark Brown 				      const struct i2c_device_id *id)
683a8035c8fSMark Brown {
6845998102bSMark Brown 	struct wm8731_priv *wm8731;
6855998102bSMark Brown 	struct snd_soc_codec *codec;
686a8035c8fSMark Brown 
6875998102bSMark Brown 	wm8731 = kzalloc(sizeof(struct wm8731_priv), GFP_KERNEL);
6885998102bSMark Brown 	if (wm8731 == NULL)
6895998102bSMark Brown 		return -ENOMEM;
6905998102bSMark Brown 
6915998102bSMark Brown 	codec = &wm8731->codec;
6925998102bSMark Brown 
6935998102bSMark Brown 	i2c_set_clientdata(i2c, wm8731);
694a8035c8fSMark Brown 	codec->control_data = i2c;
695a8035c8fSMark Brown 
6965998102bSMark Brown 	codec->dev = &i2c->dev;
697a8035c8fSMark Brown 
6987084a42bSMark Brown 	return wm8731_register(wm8731, SND_SOC_I2C);
699a8035c8fSMark Brown }
700a8035c8fSMark Brown 
701c6f29811SMark Brown static __devexit int wm8731_i2c_remove(struct i2c_client *client)
702a8035c8fSMark Brown {
7035998102bSMark Brown 	struct wm8731_priv *wm8731 = i2c_get_clientdata(client);
7045998102bSMark Brown 	wm8731_unregister(wm8731);
705a8035c8fSMark Brown 	return 0;
706a8035c8fSMark Brown }
707a8035c8fSMark Brown 
708a8035c8fSMark Brown static const struct i2c_device_id wm8731_i2c_id[] = {
709a8035c8fSMark Brown 	{ "wm8731", 0 },
710a8035c8fSMark Brown 	{ }
711a8035c8fSMark Brown };
712a8035c8fSMark Brown MODULE_DEVICE_TABLE(i2c, wm8731_i2c_id);
713a8035c8fSMark Brown 
714a8035c8fSMark Brown static struct i2c_driver wm8731_i2c_driver = {
715a8035c8fSMark Brown 	.driver = {
716a8035c8fSMark Brown 		.name = "WM8731 I2C Codec",
717a8035c8fSMark Brown 		.owner = THIS_MODULE,
718a8035c8fSMark Brown 	},
719a8035c8fSMark Brown 	.probe =    wm8731_i2c_probe,
720c6f29811SMark Brown 	.remove =   __devexit_p(wm8731_i2c_remove),
721a8035c8fSMark Brown 	.id_table = wm8731_i2c_id,
722a8035c8fSMark Brown };
723a8035c8fSMark Brown #endif
724a8035c8fSMark Brown 
725c9b3a40fSTakashi Iwai static int __init wm8731_modinit(void)
72664089b84SMark Brown {
7275998102bSMark Brown 	int ret;
7285998102bSMark Brown #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
7295998102bSMark Brown 	ret = i2c_add_driver(&wm8731_i2c_driver);
7305998102bSMark Brown 	if (ret != 0) {
7315998102bSMark Brown 		printk(KERN_ERR "Failed to register WM8731 I2C driver: %d\n",
7325998102bSMark Brown 		       ret);
7335998102bSMark Brown 	}
7345998102bSMark Brown #endif
7355998102bSMark Brown #if defined(CONFIG_SPI_MASTER)
7365998102bSMark Brown 	ret = spi_register_driver(&wm8731_spi_driver);
7375998102bSMark Brown 	if (ret != 0) {
7385998102bSMark Brown 		printk(KERN_ERR "Failed to register WM8731 SPI driver: %d\n",
7395998102bSMark Brown 		       ret);
7405998102bSMark Brown 	}
7415998102bSMark Brown #endif
7425998102bSMark Brown 	return 0;
74364089b84SMark Brown }
74464089b84SMark Brown module_init(wm8731_modinit);
74564089b84SMark Brown 
74664089b84SMark Brown static void __exit wm8731_exit(void)
74764089b84SMark Brown {
7485998102bSMark Brown #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
7495998102bSMark Brown 	i2c_del_driver(&wm8731_i2c_driver);
7505998102bSMark Brown #endif
7515998102bSMark Brown #if defined(CONFIG_SPI_MASTER)
7525998102bSMark Brown 	spi_unregister_driver(&wm8731_spi_driver);
7535998102bSMark Brown #endif
75464089b84SMark Brown }
75564089b84SMark Brown module_exit(wm8731_exit);
75664089b84SMark Brown 
75740e0aa64SRichard Purdie MODULE_DESCRIPTION("ASoC WM8731 driver");
75840e0aa64SRichard Purdie MODULE_AUTHOR("Richard Purdie");
75940e0aa64SRichard Purdie MODULE_LICENSE("GPL");
760