xref: /linux/sound/soc/codecs/wm8731.c (revision 3051e41ab7daaa59d4564f20b25dcb8c03f35f2b)
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>
2240e0aa64SRichard Purdie #include <sound/core.h>
2340e0aa64SRichard Purdie #include <sound/pcm.h>
2440e0aa64SRichard Purdie #include <sound/pcm_params.h>
2540e0aa64SRichard Purdie #include <sound/soc.h>
2640e0aa64SRichard Purdie #include <sound/soc-dapm.h>
2740e0aa64SRichard Purdie #include <sound/initval.h>
2840e0aa64SRichard Purdie 
2940e0aa64SRichard Purdie #include "wm8731.h"
3040e0aa64SRichard Purdie 
3140e0aa64SRichard Purdie #define AUDIO_NAME "wm8731"
32b36d61d4SFrank Mandarino #define WM8731_VERSION "0.13"
3340e0aa64SRichard Purdie 
3440e0aa64SRichard Purdie struct snd_soc_codec_device soc_codec_dev_wm8731;
3540e0aa64SRichard Purdie 
36b36d61d4SFrank Mandarino /* codec private data */
37b36d61d4SFrank Mandarino struct wm8731_priv {
38b36d61d4SFrank Mandarino 	unsigned int sysclk;
39b36d61d4SFrank Mandarino };
40b36d61d4SFrank Mandarino 
4140e0aa64SRichard Purdie /*
4240e0aa64SRichard Purdie  * wm8731 register cache
4340e0aa64SRichard Purdie  * We can't read the WM8731 register space when we are
4440e0aa64SRichard Purdie  * using 2 wire for device control, so we cache them instead.
4540e0aa64SRichard Purdie  * There is no point in caching the reset register
4640e0aa64SRichard Purdie  */
4740e0aa64SRichard Purdie static const u16 wm8731_reg[WM8731_CACHEREGNUM] = {
4840e0aa64SRichard Purdie     0x0097, 0x0097, 0x0079, 0x0079,
4940e0aa64SRichard Purdie     0x000a, 0x0008, 0x009f, 0x000a,
5040e0aa64SRichard Purdie     0x0000, 0x0000
5140e0aa64SRichard Purdie };
5240e0aa64SRichard Purdie 
5340e0aa64SRichard Purdie /*
5440e0aa64SRichard Purdie  * read wm8731 register cache
5540e0aa64SRichard Purdie  */
5640e0aa64SRichard Purdie static inline unsigned int wm8731_read_reg_cache(struct snd_soc_codec *codec,
5740e0aa64SRichard Purdie 	unsigned int reg)
5840e0aa64SRichard Purdie {
5940e0aa64SRichard Purdie 	u16 *cache = codec->reg_cache;
6040e0aa64SRichard Purdie 	if (reg == WM8731_RESET)
6140e0aa64SRichard Purdie 		return 0;
6240e0aa64SRichard Purdie 	if (reg >= WM8731_CACHEREGNUM)
6340e0aa64SRichard Purdie 		return -1;
6440e0aa64SRichard Purdie 	return cache[reg];
6540e0aa64SRichard Purdie }
6640e0aa64SRichard Purdie 
6740e0aa64SRichard Purdie /*
6840e0aa64SRichard Purdie  * write wm8731 register cache
6940e0aa64SRichard Purdie  */
7040e0aa64SRichard Purdie static inline void wm8731_write_reg_cache(struct snd_soc_codec *codec,
7140e0aa64SRichard Purdie 	u16 reg, unsigned int value)
7240e0aa64SRichard Purdie {
7340e0aa64SRichard Purdie 	u16 *cache = codec->reg_cache;
7440e0aa64SRichard Purdie 	if (reg >= WM8731_CACHEREGNUM)
7540e0aa64SRichard Purdie 		return;
7640e0aa64SRichard Purdie 	cache[reg] = value;
7740e0aa64SRichard Purdie }
7840e0aa64SRichard Purdie 
7940e0aa64SRichard Purdie /*
8040e0aa64SRichard Purdie  * write to the WM8731 register space
8140e0aa64SRichard Purdie  */
8240e0aa64SRichard Purdie static int wm8731_write(struct snd_soc_codec *codec, unsigned int reg,
8340e0aa64SRichard Purdie 	unsigned int value)
8440e0aa64SRichard Purdie {
8540e0aa64SRichard Purdie 	u8 data[2];
8640e0aa64SRichard Purdie 
8740e0aa64SRichard Purdie 	/* data is
8840e0aa64SRichard Purdie 	 *   D15..D9 WM8731 register offset
8940e0aa64SRichard Purdie 	 *   D8...D0 register data
9040e0aa64SRichard Purdie 	 */
9140e0aa64SRichard Purdie 	data[0] = (reg << 1) | ((value >> 8) & 0x0001);
9240e0aa64SRichard Purdie 	data[1] = value & 0x00ff;
9340e0aa64SRichard Purdie 
9440e0aa64SRichard Purdie 	wm8731_write_reg_cache(codec, reg, value);
9540e0aa64SRichard Purdie 	if (codec->hw_write(codec->control_data, data, 2) == 2)
9640e0aa64SRichard Purdie 		return 0;
9740e0aa64SRichard Purdie 	else
9840e0aa64SRichard Purdie 		return -EIO;
9940e0aa64SRichard Purdie }
10040e0aa64SRichard Purdie 
10140e0aa64SRichard Purdie #define wm8731_reset(c)	wm8731_write(c, WM8731_RESET, 0)
10240e0aa64SRichard Purdie 
10340e0aa64SRichard Purdie static const char *wm8731_input_select[] = {"Line In", "Mic"};
10440e0aa64SRichard Purdie static const char *wm8731_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"};
10540e0aa64SRichard Purdie 
10640e0aa64SRichard Purdie static const struct soc_enum wm8731_enum[] = {
10740e0aa64SRichard Purdie 	SOC_ENUM_SINGLE(WM8731_APANA, 2, 2, wm8731_input_select),
10840e0aa64SRichard Purdie 	SOC_ENUM_SINGLE(WM8731_APDIGI, 1, 4, wm8731_deemph),
10940e0aa64SRichard Purdie };
11040e0aa64SRichard Purdie 
11140e0aa64SRichard Purdie static const struct snd_kcontrol_new wm8731_snd_controls[] = {
11240e0aa64SRichard Purdie 
113bd903b6eSLiam Girdwood SOC_DOUBLE_R("Master Playback Volume", WM8731_LOUT1V, WM8731_ROUT1V,
114bd903b6eSLiam Girdwood 	0, 127, 0),
115bd903b6eSLiam Girdwood SOC_DOUBLE_R("Master Playback ZC Switch", WM8731_LOUT1V, WM8731_ROUT1V,
116bd903b6eSLiam Girdwood 	7, 1, 0),
11740e0aa64SRichard Purdie 
11840e0aa64SRichard Purdie SOC_DOUBLE_R("Capture Volume", WM8731_LINVOL, WM8731_RINVOL, 0, 31, 0),
11940e0aa64SRichard Purdie SOC_DOUBLE_R("Line Capture Switch", WM8731_LINVOL, WM8731_RINVOL, 7, 1, 1),
12040e0aa64SRichard Purdie 
12140e0aa64SRichard Purdie SOC_SINGLE("Mic Boost (+20dB)", WM8731_APANA, 0, 1, 0),
12240e0aa64SRichard Purdie SOC_SINGLE("Capture Mic Switch", WM8731_APANA, 1, 1, 1),
12340e0aa64SRichard Purdie 
12440e0aa64SRichard Purdie SOC_SINGLE("Sidetone Playback Volume", WM8731_APANA, 6, 3, 1),
12540e0aa64SRichard Purdie 
12640e0aa64SRichard Purdie SOC_SINGLE("ADC High Pass Filter Switch", WM8731_APDIGI, 0, 1, 1),
12740e0aa64SRichard Purdie SOC_SINGLE("Store DC Offset Switch", WM8731_APDIGI, 4, 1, 0),
12840e0aa64SRichard Purdie 
12940e0aa64SRichard Purdie SOC_ENUM("Playback De-emphasis", wm8731_enum[1]),
13040e0aa64SRichard Purdie };
13140e0aa64SRichard Purdie 
13240e0aa64SRichard Purdie /* add non dapm controls */
13340e0aa64SRichard Purdie static int wm8731_add_controls(struct snd_soc_codec *codec)
13440e0aa64SRichard Purdie {
13540e0aa64SRichard Purdie 	int err, i;
13640e0aa64SRichard Purdie 
13740e0aa64SRichard Purdie 	for (i = 0; i < ARRAY_SIZE(wm8731_snd_controls); i++) {
138d454aee9SMark Brown 		err = snd_ctl_add(codec->card,
139d454aee9SMark Brown 				  snd_soc_cnew(&wm8731_snd_controls[i],
140d454aee9SMark Brown 						codec, NULL));
141d454aee9SMark Brown 		if (err < 0)
14240e0aa64SRichard Purdie 			return err;
14340e0aa64SRichard Purdie 	}
14440e0aa64SRichard Purdie 
14540e0aa64SRichard Purdie 	return 0;
14640e0aa64SRichard Purdie }
14740e0aa64SRichard Purdie 
14840e0aa64SRichard Purdie /* Output Mixer */
14940e0aa64SRichard Purdie static const struct snd_kcontrol_new wm8731_output_mixer_controls[] = {
15040e0aa64SRichard Purdie SOC_DAPM_SINGLE("Line Bypass Switch", WM8731_APANA, 3, 1, 0),
15140e0aa64SRichard Purdie SOC_DAPM_SINGLE("Mic Sidetone Switch", WM8731_APANA, 5, 1, 0),
15240e0aa64SRichard Purdie SOC_DAPM_SINGLE("HiFi Playback Switch", WM8731_APANA, 4, 1, 0),
15340e0aa64SRichard Purdie };
15440e0aa64SRichard Purdie 
15540e0aa64SRichard Purdie /* Input mux */
15640e0aa64SRichard Purdie static const struct snd_kcontrol_new wm8731_input_mux_controls =
15740e0aa64SRichard Purdie SOC_DAPM_ENUM("Input Select", wm8731_enum[0]);
15840e0aa64SRichard Purdie 
15940e0aa64SRichard Purdie static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
16040e0aa64SRichard Purdie SND_SOC_DAPM_MIXER("Output Mixer", WM8731_PWR, 4, 1,
16140e0aa64SRichard Purdie 	&wm8731_output_mixer_controls[0],
16240e0aa64SRichard Purdie 	ARRAY_SIZE(wm8731_output_mixer_controls)),
16340e0aa64SRichard Purdie SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8731_PWR, 3, 1),
16440e0aa64SRichard Purdie SND_SOC_DAPM_OUTPUT("LOUT"),
16540e0aa64SRichard Purdie SND_SOC_DAPM_OUTPUT("LHPOUT"),
16640e0aa64SRichard Purdie SND_SOC_DAPM_OUTPUT("ROUT"),
16740e0aa64SRichard Purdie SND_SOC_DAPM_OUTPUT("RHPOUT"),
16840e0aa64SRichard Purdie SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8731_PWR, 2, 1),
16940e0aa64SRichard Purdie SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &wm8731_input_mux_controls),
17040e0aa64SRichard Purdie SND_SOC_DAPM_PGA("Line Input", WM8731_PWR, 0, 1, NULL, 0),
17140e0aa64SRichard Purdie SND_SOC_DAPM_MICBIAS("Mic Bias", WM8731_PWR, 1, 1),
17240e0aa64SRichard Purdie SND_SOC_DAPM_INPUT("MICIN"),
17340e0aa64SRichard Purdie SND_SOC_DAPM_INPUT("RLINEIN"),
17440e0aa64SRichard Purdie SND_SOC_DAPM_INPUT("LLINEIN"),
17540e0aa64SRichard Purdie };
17640e0aa64SRichard Purdie 
177a65f0568SMark Brown static const struct snd_soc_dapm_route intercon[] = {
17840e0aa64SRichard Purdie 	/* output mixer */
17940e0aa64SRichard Purdie 	{"Output Mixer", "Line Bypass Switch", "Line Input"},
18040e0aa64SRichard Purdie 	{"Output Mixer", "HiFi Playback Switch", "DAC"},
18140e0aa64SRichard Purdie 	{"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
18240e0aa64SRichard Purdie 
18340e0aa64SRichard Purdie 	/* outputs */
18440e0aa64SRichard Purdie 	{"RHPOUT", NULL, "Output Mixer"},
18540e0aa64SRichard Purdie 	{"ROUT", NULL, "Output Mixer"},
18640e0aa64SRichard Purdie 	{"LHPOUT", NULL, "Output Mixer"},
18740e0aa64SRichard Purdie 	{"LOUT", NULL, "Output Mixer"},
18840e0aa64SRichard Purdie 
18940e0aa64SRichard Purdie 	/* input mux */
19040e0aa64SRichard Purdie 	{"Input Mux", "Line In", "Line Input"},
19140e0aa64SRichard Purdie 	{"Input Mux", "Mic", "Mic Bias"},
19240e0aa64SRichard Purdie 	{"ADC", NULL, "Input Mux"},
19340e0aa64SRichard Purdie 
19440e0aa64SRichard Purdie 	/* inputs */
19540e0aa64SRichard Purdie 	{"Line Input", NULL, "LLINEIN"},
19640e0aa64SRichard Purdie 	{"Line Input", NULL, "RLINEIN"},
19740e0aa64SRichard Purdie 	{"Mic Bias", NULL, "MICIN"},
19840e0aa64SRichard Purdie };
19940e0aa64SRichard Purdie 
20040e0aa64SRichard Purdie static int wm8731_add_widgets(struct snd_soc_codec *codec)
20140e0aa64SRichard Purdie {
202a65f0568SMark Brown 	snd_soc_dapm_new_controls(codec, wm8731_dapm_widgets,
203a65f0568SMark Brown 				  ARRAY_SIZE(wm8731_dapm_widgets));
20440e0aa64SRichard Purdie 
205a65f0568SMark Brown 	snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon));
20640e0aa64SRichard Purdie 
20740e0aa64SRichard Purdie 	snd_soc_dapm_new_widgets(codec);
20840e0aa64SRichard Purdie 	return 0;
20940e0aa64SRichard Purdie }
21040e0aa64SRichard Purdie 
21140e0aa64SRichard Purdie struct _coeff_div {
21240e0aa64SRichard Purdie 	u32 mclk;
21340e0aa64SRichard Purdie 	u32 rate;
21440e0aa64SRichard Purdie 	u16 fs;
21540e0aa64SRichard Purdie 	u8 sr:4;
21640e0aa64SRichard Purdie 	u8 bosr:1;
21740e0aa64SRichard Purdie 	u8 usb:1;
21840e0aa64SRichard Purdie };
21940e0aa64SRichard Purdie 
22040e0aa64SRichard Purdie /* codec mclk clock divider coefficients */
22140e0aa64SRichard Purdie static const struct _coeff_div coeff_div[] = {
22240e0aa64SRichard Purdie 	/* 48k */
22340e0aa64SRichard Purdie 	{12288000, 48000, 256, 0x0, 0x0, 0x0},
22440e0aa64SRichard Purdie 	{18432000, 48000, 384, 0x0, 0x1, 0x0},
22540e0aa64SRichard Purdie 	{12000000, 48000, 250, 0x0, 0x0, 0x1},
22640e0aa64SRichard Purdie 
22740e0aa64SRichard Purdie 	/* 32k */
22840e0aa64SRichard Purdie 	{12288000, 32000, 384, 0x6, 0x0, 0x0},
22940e0aa64SRichard Purdie 	{18432000, 32000, 576, 0x6, 0x1, 0x0},
230298a2c75SFrank Mandarino 	{12000000, 32000, 375, 0x6, 0x0, 0x1},
23140e0aa64SRichard Purdie 
23240e0aa64SRichard Purdie 	/* 8k */
23340e0aa64SRichard Purdie 	{12288000, 8000, 1536, 0x3, 0x0, 0x0},
23440e0aa64SRichard Purdie 	{18432000, 8000, 2304, 0x3, 0x1, 0x0},
23540e0aa64SRichard Purdie 	{11289600, 8000, 1408, 0xb, 0x0, 0x0},
23640e0aa64SRichard Purdie 	{16934400, 8000, 2112, 0xb, 0x1, 0x0},
23740e0aa64SRichard Purdie 	{12000000, 8000, 1500, 0x3, 0x0, 0x1},
23840e0aa64SRichard Purdie 
23940e0aa64SRichard Purdie 	/* 96k */
24040e0aa64SRichard Purdie 	{12288000, 96000, 128, 0x7, 0x0, 0x0},
24140e0aa64SRichard Purdie 	{18432000, 96000, 192, 0x7, 0x1, 0x0},
24240e0aa64SRichard Purdie 	{12000000, 96000, 125, 0x7, 0x0, 0x1},
24340e0aa64SRichard Purdie 
24440e0aa64SRichard Purdie 	/* 44.1k */
24540e0aa64SRichard Purdie 	{11289600, 44100, 256, 0x8, 0x0, 0x0},
24640e0aa64SRichard Purdie 	{16934400, 44100, 384, 0x8, 0x1, 0x0},
24740e0aa64SRichard Purdie 	{12000000, 44100, 272, 0x8, 0x1, 0x1},
24840e0aa64SRichard Purdie 
24940e0aa64SRichard Purdie 	/* 88.2k */
25040e0aa64SRichard Purdie 	{11289600, 88200, 128, 0xf, 0x0, 0x0},
25140e0aa64SRichard Purdie 	{16934400, 88200, 192, 0xf, 0x1, 0x0},
25240e0aa64SRichard Purdie 	{12000000, 88200, 136, 0xf, 0x1, 0x1},
25340e0aa64SRichard Purdie };
25440e0aa64SRichard Purdie 
25540e0aa64SRichard Purdie static inline int get_coeff(int mclk, int rate)
25640e0aa64SRichard Purdie {
25740e0aa64SRichard Purdie 	int i;
25840e0aa64SRichard Purdie 
25940e0aa64SRichard Purdie 	for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
26040e0aa64SRichard Purdie 		if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
26140e0aa64SRichard Purdie 			return i;
26240e0aa64SRichard Purdie 	}
26340e0aa64SRichard Purdie 	return 0;
26440e0aa64SRichard Purdie }
26540e0aa64SRichard Purdie 
266b36d61d4SFrank Mandarino static int wm8731_hw_params(struct snd_pcm_substream *substream,
267b36d61d4SFrank Mandarino 	struct snd_pcm_hw_params *params)
26840e0aa64SRichard Purdie {
269b36d61d4SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
270b36d61d4SFrank Mandarino 	struct snd_soc_device *socdev = rtd->socdev;
271b36d61d4SFrank Mandarino 	struct snd_soc_codec *codec = socdev->codec;
272b36d61d4SFrank Mandarino 	struct wm8731_priv *wm8731 = codec->private_data;
273b36d61d4SFrank Mandarino 	u16 iface = wm8731_read_reg_cache(codec, WM8731_IFACE) & 0xfff3;
274b36d61d4SFrank Mandarino 	int i = get_coeff(wm8731->sysclk, params_rate(params));
275b36d61d4SFrank Mandarino 	u16 srate = (coeff_div[i].sr << 2) |
276b36d61d4SFrank Mandarino 		(coeff_div[i].bosr << 1) | coeff_div[i].usb;
27740e0aa64SRichard Purdie 
278b36d61d4SFrank Mandarino 	wm8731_write(codec, WM8731_SRATE, srate);
27940e0aa64SRichard Purdie 
280b36d61d4SFrank Mandarino 	/* bit size */
281b36d61d4SFrank Mandarino 	switch (params_format(params)) {
282b36d61d4SFrank Mandarino 	case SNDRV_PCM_FORMAT_S16_LE:
283b36d61d4SFrank Mandarino 		break;
284b36d61d4SFrank Mandarino 	case SNDRV_PCM_FORMAT_S20_3LE:
285b36d61d4SFrank Mandarino 		iface |= 0x0004;
286b36d61d4SFrank Mandarino 		break;
287b36d61d4SFrank Mandarino 	case SNDRV_PCM_FORMAT_S24_LE:
288b36d61d4SFrank Mandarino 		iface |= 0x0008;
289b36d61d4SFrank Mandarino 		break;
290b36d61d4SFrank Mandarino 	}
291b36d61d4SFrank Mandarino 
292b36d61d4SFrank Mandarino 	wm8731_write(codec, WM8731_IFACE, iface);
293b36d61d4SFrank Mandarino 	return 0;
29440e0aa64SRichard Purdie }
29540e0aa64SRichard Purdie 
29640e0aa64SRichard Purdie static int wm8731_pcm_prepare(struct snd_pcm_substream *substream)
29740e0aa64SRichard Purdie {
29840e0aa64SRichard Purdie 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
29940e0aa64SRichard Purdie 	struct snd_soc_device *socdev = rtd->socdev;
30040e0aa64SRichard Purdie 	struct snd_soc_codec *codec = socdev->codec;
30140e0aa64SRichard Purdie 
30240e0aa64SRichard Purdie 	/* set active */
30340e0aa64SRichard Purdie 	wm8731_write(codec, WM8731_ACTIVE, 0x0001);
304b36d61d4SFrank Mandarino 
30540e0aa64SRichard Purdie 	return 0;
30640e0aa64SRichard Purdie }
30740e0aa64SRichard Purdie 
30840e0aa64SRichard Purdie static void wm8731_shutdown(struct snd_pcm_substream *substream)
30940e0aa64SRichard Purdie {
31040e0aa64SRichard Purdie 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
31140e0aa64SRichard Purdie 	struct snd_soc_device *socdev = rtd->socdev;
31240e0aa64SRichard Purdie 	struct snd_soc_codec *codec = socdev->codec;
31340e0aa64SRichard Purdie 
31440e0aa64SRichard Purdie 	/* deactivate */
31540e0aa64SRichard Purdie 	if (!codec->active) {
31640e0aa64SRichard Purdie 		udelay(50);
31740e0aa64SRichard Purdie 		wm8731_write(codec, WM8731_ACTIVE, 0x0);
31840e0aa64SRichard Purdie 	}
31940e0aa64SRichard Purdie }
32040e0aa64SRichard Purdie 
321e550e17fSLiam Girdwood static int wm8731_mute(struct snd_soc_dai *dai, int mute)
32240e0aa64SRichard Purdie {
323b36d61d4SFrank Mandarino 	struct snd_soc_codec *codec = dai->codec;
32440e0aa64SRichard Purdie 	u16 mute_reg = wm8731_read_reg_cache(codec, WM8731_APDIGI) & 0xfff7;
325b36d61d4SFrank Mandarino 
32640e0aa64SRichard Purdie 	if (mute)
32740e0aa64SRichard Purdie 		wm8731_write(codec, WM8731_APDIGI, mute_reg | 0x8);
32840e0aa64SRichard Purdie 	else
32940e0aa64SRichard Purdie 		wm8731_write(codec, WM8731_APDIGI, mute_reg);
33040e0aa64SRichard Purdie 	return 0;
33140e0aa64SRichard Purdie }
33240e0aa64SRichard Purdie 
333e550e17fSLiam Girdwood static int wm8731_set_dai_sysclk(struct snd_soc_dai *codec_dai,
334b36d61d4SFrank Mandarino 		int clk_id, unsigned int freq, int dir)
335b36d61d4SFrank Mandarino {
336b36d61d4SFrank Mandarino 	struct snd_soc_codec *codec = codec_dai->codec;
337b36d61d4SFrank Mandarino 	struct wm8731_priv *wm8731 = codec->private_data;
338b36d61d4SFrank Mandarino 
339b36d61d4SFrank Mandarino 	switch (freq) {
340b36d61d4SFrank Mandarino 	case 11289600:
341b36d61d4SFrank Mandarino 	case 12000000:
342b36d61d4SFrank Mandarino 	case 12288000:
343b36d61d4SFrank Mandarino 	case 16934400:
344b36d61d4SFrank Mandarino 	case 18432000:
345b36d61d4SFrank Mandarino 		wm8731->sysclk = freq;
346b36d61d4SFrank Mandarino 		return 0;
347b36d61d4SFrank Mandarino 	}
348b36d61d4SFrank Mandarino 	return -EINVAL;
349b36d61d4SFrank Mandarino }
350b36d61d4SFrank Mandarino 
351b36d61d4SFrank Mandarino 
352e550e17fSLiam Girdwood static int wm8731_set_dai_fmt(struct snd_soc_dai *codec_dai,
353b36d61d4SFrank Mandarino 		unsigned int fmt)
354b36d61d4SFrank Mandarino {
355b36d61d4SFrank Mandarino 	struct snd_soc_codec *codec = codec_dai->codec;
356b36d61d4SFrank Mandarino 	u16 iface = 0;
357b36d61d4SFrank Mandarino 
358b36d61d4SFrank Mandarino 	/* set master/slave audio interface */
359b36d61d4SFrank Mandarino 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
360b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_CBM_CFM:
361b36d61d4SFrank Mandarino 		iface |= 0x0040;
362b36d61d4SFrank Mandarino 		break;
363b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_CBS_CFS:
364b36d61d4SFrank Mandarino 		break;
365b36d61d4SFrank Mandarino 	default:
366b36d61d4SFrank Mandarino 		return -EINVAL;
367b36d61d4SFrank Mandarino 	}
368b36d61d4SFrank Mandarino 
369b36d61d4SFrank Mandarino 	/* interface format */
370b36d61d4SFrank Mandarino 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
371b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_I2S:
372b36d61d4SFrank Mandarino 		iface |= 0x0002;
373b36d61d4SFrank Mandarino 		break;
374b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_RIGHT_J:
375b36d61d4SFrank Mandarino 		break;
376b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_LEFT_J:
377b36d61d4SFrank Mandarino 		iface |= 0x0001;
378b36d61d4SFrank Mandarino 		break;
379b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_DSP_A:
380b36d61d4SFrank Mandarino 		iface |= 0x0003;
381b36d61d4SFrank Mandarino 		break;
382b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_DSP_B:
383b36d61d4SFrank Mandarino 		iface |= 0x0013;
384b36d61d4SFrank Mandarino 		break;
385b36d61d4SFrank Mandarino 	default:
386b36d61d4SFrank Mandarino 		return -EINVAL;
387b36d61d4SFrank Mandarino 	}
388b36d61d4SFrank Mandarino 
389b36d61d4SFrank Mandarino 	/* clock inversion */
390b36d61d4SFrank Mandarino 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
391b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_NB_NF:
392b36d61d4SFrank Mandarino 		break;
393b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_IB_IF:
394b36d61d4SFrank Mandarino 		iface |= 0x0090;
395b36d61d4SFrank Mandarino 		break;
396b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_IB_NF:
397b36d61d4SFrank Mandarino 		iface |= 0x0080;
398b36d61d4SFrank Mandarino 		break;
399b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_NB_IF:
400b36d61d4SFrank Mandarino 		iface |= 0x0010;
401b36d61d4SFrank Mandarino 		break;
402b36d61d4SFrank Mandarino 	default:
403b36d61d4SFrank Mandarino 		return -EINVAL;
404b36d61d4SFrank Mandarino 	}
405b36d61d4SFrank Mandarino 
406b36d61d4SFrank Mandarino 	/* set iface */
407b36d61d4SFrank Mandarino 	wm8731_write(codec, WM8731_IFACE, iface);
408b36d61d4SFrank Mandarino 	return 0;
409b36d61d4SFrank Mandarino }
410b36d61d4SFrank Mandarino 
4110be9898aSMark Brown static int wm8731_set_bias_level(struct snd_soc_codec *codec,
4120be9898aSMark Brown 				 enum snd_soc_bias_level level)
41340e0aa64SRichard Purdie {
41440e0aa64SRichard Purdie 	u16 reg = wm8731_read_reg_cache(codec, WM8731_PWR) & 0xff7f;
41540e0aa64SRichard Purdie 
4160be9898aSMark Brown 	switch (level) {
4170be9898aSMark Brown 	case SND_SOC_BIAS_ON:
41840e0aa64SRichard Purdie 		/* vref/mid, osc on, dac unmute */
41940e0aa64SRichard Purdie 		wm8731_write(codec, WM8731_PWR, reg);
42040e0aa64SRichard Purdie 		break;
4210be9898aSMark Brown 	case SND_SOC_BIAS_PREPARE:
42240e0aa64SRichard Purdie 		break;
4230be9898aSMark Brown 	case SND_SOC_BIAS_STANDBY:
42440e0aa64SRichard Purdie 		/* everything off except vref/vmid, */
42540e0aa64SRichard Purdie 		wm8731_write(codec, WM8731_PWR, reg | 0x0040);
42640e0aa64SRichard Purdie 		break;
4270be9898aSMark Brown 	case SND_SOC_BIAS_OFF:
42840e0aa64SRichard Purdie 		/* everything off, dac mute, inactive */
42940e0aa64SRichard Purdie 		wm8731_write(codec, WM8731_ACTIVE, 0x0);
43040e0aa64SRichard Purdie 		wm8731_write(codec, WM8731_PWR, 0xffff);
43140e0aa64SRichard Purdie 		break;
43240e0aa64SRichard Purdie 	}
4330be9898aSMark Brown 	codec->bias_level = level;
43440e0aa64SRichard Purdie 	return 0;
43540e0aa64SRichard Purdie }
43640e0aa64SRichard Purdie 
437b36d61d4SFrank Mandarino #define WM8731_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
438b36d61d4SFrank Mandarino 		SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
439b36d61d4SFrank Mandarino 		SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\
440b36d61d4SFrank Mandarino 		SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |\
441b36d61d4SFrank Mandarino 		SNDRV_PCM_RATE_96000)
442b36d61d4SFrank Mandarino 
443b36d61d4SFrank Mandarino #define WM8731_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
444b36d61d4SFrank Mandarino 	SNDRV_PCM_FMTBIT_S24_LE)
445b36d61d4SFrank Mandarino 
446e550e17fSLiam Girdwood struct snd_soc_dai wm8731_dai = {
44740e0aa64SRichard Purdie 	.name = "WM8731",
44840e0aa64SRichard Purdie 	.playback = {
44940e0aa64SRichard Purdie 		.stream_name = "Playback",
45040e0aa64SRichard Purdie 		.channels_min = 1,
45140e0aa64SRichard Purdie 		.channels_max = 2,
452b36d61d4SFrank Mandarino 		.rates = WM8731_RATES,
453b36d61d4SFrank Mandarino 		.formats = WM8731_FORMATS,},
45440e0aa64SRichard Purdie 	.capture = {
45540e0aa64SRichard Purdie 		.stream_name = "Capture",
45640e0aa64SRichard Purdie 		.channels_min = 1,
45740e0aa64SRichard Purdie 		.channels_max = 2,
458b36d61d4SFrank Mandarino 		.rates = WM8731_RATES,
459b36d61d4SFrank Mandarino 		.formats = WM8731_FORMATS,},
46040e0aa64SRichard Purdie 	.ops = {
46140e0aa64SRichard Purdie 		.prepare = wm8731_pcm_prepare,
462b36d61d4SFrank Mandarino 		.hw_params = wm8731_hw_params,
46340e0aa64SRichard Purdie 		.shutdown = wm8731_shutdown,
46440e0aa64SRichard Purdie 	},
465b36d61d4SFrank Mandarino 	.dai_ops = {
466b36d61d4SFrank Mandarino 		.digital_mute = wm8731_mute,
467b36d61d4SFrank Mandarino 		.set_sysclk = wm8731_set_dai_sysclk,
468b36d61d4SFrank Mandarino 		.set_fmt = wm8731_set_dai_fmt,
469b36d61d4SFrank Mandarino 	}
47040e0aa64SRichard Purdie };
47140e0aa64SRichard Purdie EXPORT_SYMBOL_GPL(wm8731_dai);
47240e0aa64SRichard Purdie 
47340e0aa64SRichard Purdie static int wm8731_suspend(struct platform_device *pdev, pm_message_t state)
47440e0aa64SRichard Purdie {
47540e0aa64SRichard Purdie 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
47640e0aa64SRichard Purdie 	struct snd_soc_codec *codec = socdev->codec;
47740e0aa64SRichard Purdie 
47840e0aa64SRichard Purdie 	wm8731_write(codec, WM8731_ACTIVE, 0x0);
4790be9898aSMark Brown 	wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF);
48040e0aa64SRichard Purdie 	return 0;
48140e0aa64SRichard Purdie }
48240e0aa64SRichard Purdie 
48340e0aa64SRichard Purdie static int wm8731_resume(struct platform_device *pdev)
48440e0aa64SRichard Purdie {
48540e0aa64SRichard Purdie 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
48640e0aa64SRichard Purdie 	struct snd_soc_codec *codec = socdev->codec;
48740e0aa64SRichard Purdie 	int i;
48840e0aa64SRichard Purdie 	u8 data[2];
48940e0aa64SRichard Purdie 	u16 *cache = codec->reg_cache;
49040e0aa64SRichard Purdie 
49140e0aa64SRichard Purdie 	/* Sync reg_cache with the hardware */
49240e0aa64SRichard Purdie 	for (i = 0; i < ARRAY_SIZE(wm8731_reg); i++) {
49340e0aa64SRichard Purdie 		data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001);
49440e0aa64SRichard Purdie 		data[1] = cache[i] & 0x00ff;
49540e0aa64SRichard Purdie 		codec->hw_write(codec->control_data, data, 2);
49640e0aa64SRichard Purdie 	}
4970be9898aSMark Brown 	wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
4980be9898aSMark Brown 	wm8731_set_bias_level(codec, codec->suspend_bias_level);
49940e0aa64SRichard Purdie 	return 0;
50040e0aa64SRichard Purdie }
50140e0aa64SRichard Purdie 
50240e0aa64SRichard Purdie /*
50340e0aa64SRichard Purdie  * initialise the WM8731 driver
50440e0aa64SRichard Purdie  * register the mixer and dsp interfaces with the kernel
50540e0aa64SRichard Purdie  */
50640e0aa64SRichard Purdie static int wm8731_init(struct snd_soc_device *socdev)
50740e0aa64SRichard Purdie {
50840e0aa64SRichard Purdie 	struct snd_soc_codec *codec = socdev->codec;
50940e0aa64SRichard Purdie 	int reg, ret = 0;
51040e0aa64SRichard Purdie 
51140e0aa64SRichard Purdie 	codec->name = "WM8731";
51240e0aa64SRichard Purdie 	codec->owner = THIS_MODULE;
51340e0aa64SRichard Purdie 	codec->read = wm8731_read_reg_cache;
51440e0aa64SRichard Purdie 	codec->write = wm8731_write;
5150be9898aSMark Brown 	codec->set_bias_level = wm8731_set_bias_level;
51640e0aa64SRichard Purdie 	codec->dai = &wm8731_dai;
51740e0aa64SRichard Purdie 	codec->num_dai = 1;
518d751b233SMark Brown 	codec->reg_cache_size = ARRAY_SIZE(wm8731_reg);
51988cb4290STakashi Iwai 	codec->reg_cache = kmemdup(wm8731_reg, sizeof(wm8731_reg), GFP_KERNEL);
52040e0aa64SRichard Purdie 	if (codec->reg_cache == NULL)
52140e0aa64SRichard Purdie 		return -ENOMEM;
52240e0aa64SRichard Purdie 
52340e0aa64SRichard Purdie 	wm8731_reset(codec);
52440e0aa64SRichard Purdie 
52540e0aa64SRichard Purdie 	/* register pcms */
52640e0aa64SRichard Purdie 	ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
52740e0aa64SRichard Purdie 	if (ret < 0) {
528e35115a5SLiam Girdwood 		printk(KERN_ERR "wm8731: failed to create pcms\n");
529e35115a5SLiam Girdwood 		goto pcm_err;
53040e0aa64SRichard Purdie 	}
53140e0aa64SRichard Purdie 
53240e0aa64SRichard Purdie 	/* power on device */
5330be9898aSMark Brown 	wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
53440e0aa64SRichard Purdie 
53540e0aa64SRichard Purdie 	/* set the update bits */
53640e0aa64SRichard Purdie 	reg = wm8731_read_reg_cache(codec, WM8731_LOUT1V);
537389619f1SVille Syrjala 	wm8731_write(codec, WM8731_LOUT1V, reg & ~0x0100);
53840e0aa64SRichard Purdie 	reg = wm8731_read_reg_cache(codec, WM8731_ROUT1V);
539389619f1SVille Syrjala 	wm8731_write(codec, WM8731_ROUT1V, reg & ~0x0100);
54040e0aa64SRichard Purdie 	reg = wm8731_read_reg_cache(codec, WM8731_LINVOL);
541389619f1SVille Syrjala 	wm8731_write(codec, WM8731_LINVOL, reg & ~0x0100);
54240e0aa64SRichard Purdie 	reg = wm8731_read_reg_cache(codec, WM8731_RINVOL);
543389619f1SVille Syrjala 	wm8731_write(codec, WM8731_RINVOL, reg & ~0x0100);
54440e0aa64SRichard Purdie 
54540e0aa64SRichard Purdie 	wm8731_add_controls(codec);
54640e0aa64SRichard Purdie 	wm8731_add_widgets(codec);
54740e0aa64SRichard Purdie 	ret = snd_soc_register_card(socdev);
54840e0aa64SRichard Purdie 	if (ret < 0) {
549e35115a5SLiam Girdwood 		printk(KERN_ERR "wm8731: failed to register card\n");
550e35115a5SLiam Girdwood 		goto card_err;
55140e0aa64SRichard Purdie 	}
55240e0aa64SRichard Purdie 
55340e0aa64SRichard Purdie 	return ret;
554e35115a5SLiam Girdwood 
555e35115a5SLiam Girdwood card_err:
556e35115a5SLiam Girdwood 	snd_soc_free_pcms(socdev);
557e35115a5SLiam Girdwood 	snd_soc_dapm_free(socdev);
558e35115a5SLiam Girdwood pcm_err:
559e35115a5SLiam Girdwood 	kfree(codec->reg_cache);
560e35115a5SLiam Girdwood 	return ret;
56140e0aa64SRichard Purdie }
56240e0aa64SRichard Purdie 
56340e0aa64SRichard Purdie static struct snd_soc_device *wm8731_socdev;
56440e0aa64SRichard Purdie 
56540e0aa64SRichard Purdie #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
56640e0aa64SRichard Purdie 
56740e0aa64SRichard Purdie /*
56840e0aa64SRichard Purdie  * WM8731 2 wire address is determined by GPIO5
56940e0aa64SRichard Purdie  * state during powerup.
57040e0aa64SRichard Purdie  *    low  = 0x1a
57140e0aa64SRichard Purdie  *    high = 0x1b
57240e0aa64SRichard Purdie  */
57340e0aa64SRichard Purdie static unsigned short normal_i2c[] = { 0, I2C_CLIENT_END };
57440e0aa64SRichard Purdie 
57540e0aa64SRichard Purdie /* Magic definition of all other variables and things */
57640e0aa64SRichard Purdie I2C_CLIENT_INSMOD;
57740e0aa64SRichard Purdie 
57840e0aa64SRichard Purdie static struct i2c_driver wm8731_i2c_driver;
57940e0aa64SRichard Purdie static struct i2c_client client_template;
58040e0aa64SRichard Purdie 
58140e0aa64SRichard Purdie /* If the i2c layer weren't so broken, we could pass this kind of data
58240e0aa64SRichard Purdie    around */
58340e0aa64SRichard Purdie 
58440e0aa64SRichard Purdie static int wm8731_codec_probe(struct i2c_adapter *adap, int addr, int kind)
58540e0aa64SRichard Purdie {
58640e0aa64SRichard Purdie 	struct snd_soc_device *socdev = wm8731_socdev;
58740e0aa64SRichard Purdie 	struct wm8731_setup_data *setup = socdev->codec_data;
58840e0aa64SRichard Purdie 	struct snd_soc_codec *codec = socdev->codec;
58940e0aa64SRichard Purdie 	struct i2c_client *i2c;
59040e0aa64SRichard Purdie 	int ret;
59140e0aa64SRichard Purdie 
59240e0aa64SRichard Purdie 	if (addr != setup->i2c_address)
59340e0aa64SRichard Purdie 		return -ENODEV;
59440e0aa64SRichard Purdie 
59540e0aa64SRichard Purdie 	client_template.adapter = adap;
59640e0aa64SRichard Purdie 	client_template.addr = addr;
59740e0aa64SRichard Purdie 
59888cb4290STakashi Iwai 	i2c = kmemdup(&client_template, sizeof(client_template), GFP_KERNEL);
599*3051e41aSJean Delvare 	if (i2c == NULL)
60040e0aa64SRichard Purdie 		return -ENOMEM;
601*3051e41aSJean Delvare 
60240e0aa64SRichard Purdie 	i2c_set_clientdata(i2c, codec);
60340e0aa64SRichard Purdie 	codec->control_data = i2c;
60440e0aa64SRichard Purdie 
60540e0aa64SRichard Purdie 	ret = i2c_attach_client(i2c);
60640e0aa64SRichard Purdie 	if (ret < 0) {
607a5c95e90SMark Brown 		pr_err("failed to attach codec at addr %x\n", addr);
60840e0aa64SRichard Purdie 		goto err;
60940e0aa64SRichard Purdie 	}
61040e0aa64SRichard Purdie 
61140e0aa64SRichard Purdie 	ret = wm8731_init(socdev);
61240e0aa64SRichard Purdie 	if (ret < 0) {
613a5c95e90SMark Brown 		pr_err("failed to initialise WM8731\n");
61440e0aa64SRichard Purdie 		goto err;
61540e0aa64SRichard Purdie 	}
61640e0aa64SRichard Purdie 	return ret;
61740e0aa64SRichard Purdie 
61840e0aa64SRichard Purdie err:
61940e0aa64SRichard Purdie 	kfree(i2c);
62040e0aa64SRichard Purdie 	return ret;
62140e0aa64SRichard Purdie }
62240e0aa64SRichard Purdie 
62340e0aa64SRichard Purdie static int wm8731_i2c_detach(struct i2c_client *client)
62440e0aa64SRichard Purdie {
62540e0aa64SRichard Purdie 	struct snd_soc_codec *codec = i2c_get_clientdata(client);
62640e0aa64SRichard Purdie 	i2c_detach_client(client);
62740e0aa64SRichard Purdie 	kfree(codec->reg_cache);
62840e0aa64SRichard Purdie 	kfree(client);
62940e0aa64SRichard Purdie 	return 0;
63040e0aa64SRichard Purdie }
63140e0aa64SRichard Purdie 
63240e0aa64SRichard Purdie static int wm8731_i2c_attach(struct i2c_adapter *adap)
63340e0aa64SRichard Purdie {
63440e0aa64SRichard Purdie 	return i2c_probe(adap, &addr_data, wm8731_codec_probe);
63540e0aa64SRichard Purdie }
63640e0aa64SRichard Purdie 
63740e0aa64SRichard Purdie /* corgi i2c codec control layer */
63840e0aa64SRichard Purdie static struct i2c_driver wm8731_i2c_driver = {
63940e0aa64SRichard Purdie 	.driver = {
64040e0aa64SRichard Purdie 		.name = "WM8731 I2C Codec",
64140e0aa64SRichard Purdie 		.owner = THIS_MODULE,
64240e0aa64SRichard Purdie 	},
64340e0aa64SRichard Purdie 	.id =             I2C_DRIVERID_WM8731,
64440e0aa64SRichard Purdie 	.attach_adapter = wm8731_i2c_attach,
64540e0aa64SRichard Purdie 	.detach_client =  wm8731_i2c_detach,
64640e0aa64SRichard Purdie 	.command =        NULL,
64740e0aa64SRichard Purdie };
64840e0aa64SRichard Purdie 
64940e0aa64SRichard Purdie static struct i2c_client client_template = {
65040e0aa64SRichard Purdie 	.name =   "WM8731",
65140e0aa64SRichard Purdie 	.driver = &wm8731_i2c_driver,
65240e0aa64SRichard Purdie };
65340e0aa64SRichard Purdie #endif
65440e0aa64SRichard Purdie 
65540e0aa64SRichard Purdie static int wm8731_probe(struct platform_device *pdev)
65640e0aa64SRichard Purdie {
65740e0aa64SRichard Purdie 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
65840e0aa64SRichard Purdie 	struct wm8731_setup_data *setup;
65940e0aa64SRichard Purdie 	struct snd_soc_codec *codec;
660b36d61d4SFrank Mandarino 	struct wm8731_priv *wm8731;
66140e0aa64SRichard Purdie 	int ret = 0;
66240e0aa64SRichard Purdie 
663a5c95e90SMark Brown 	pr_info("WM8731 Audio Codec %s", WM8731_VERSION);
66440e0aa64SRichard Purdie 
66540e0aa64SRichard Purdie 	setup = socdev->codec_data;
66640e0aa64SRichard Purdie 	codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
66740e0aa64SRichard Purdie 	if (codec == NULL)
66840e0aa64SRichard Purdie 		return -ENOMEM;
66940e0aa64SRichard Purdie 
670b36d61d4SFrank Mandarino 	wm8731 = kzalloc(sizeof(struct wm8731_priv), GFP_KERNEL);
671b36d61d4SFrank Mandarino 	if (wm8731 == NULL) {
672b36d61d4SFrank Mandarino 		kfree(codec);
673b36d61d4SFrank Mandarino 		return -ENOMEM;
674b36d61d4SFrank Mandarino 	}
675b36d61d4SFrank Mandarino 
676b36d61d4SFrank Mandarino 	codec->private_data = wm8731;
67740e0aa64SRichard Purdie 	socdev->codec = codec;
67840e0aa64SRichard Purdie 	mutex_init(&codec->mutex);
67940e0aa64SRichard Purdie 	INIT_LIST_HEAD(&codec->dapm_widgets);
68040e0aa64SRichard Purdie 	INIT_LIST_HEAD(&codec->dapm_paths);
68140e0aa64SRichard Purdie 
68240e0aa64SRichard Purdie 	wm8731_socdev = socdev;
68340e0aa64SRichard Purdie #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
68440e0aa64SRichard Purdie 	if (setup->i2c_address) {
68540e0aa64SRichard Purdie 		normal_i2c[0] = setup->i2c_address;
68640e0aa64SRichard Purdie 		codec->hw_write = (hw_write_t)i2c_master_send;
68740e0aa64SRichard Purdie 		ret = i2c_add_driver(&wm8731_i2c_driver);
68840e0aa64SRichard Purdie 		if (ret != 0)
68940e0aa64SRichard Purdie 			printk(KERN_ERR "can't add i2c driver");
69040e0aa64SRichard Purdie 	}
69140e0aa64SRichard Purdie #else
69240e0aa64SRichard Purdie 	/* Add other interfaces here */
69340e0aa64SRichard Purdie #endif
694*3051e41aSJean Delvare 
695*3051e41aSJean Delvare 	if (ret != 0) {
696*3051e41aSJean Delvare 		kfree(codec->private_data);
697*3051e41aSJean Delvare 		kfree(codec);
698*3051e41aSJean Delvare 	}
69940e0aa64SRichard Purdie 	return ret;
70040e0aa64SRichard Purdie }
70140e0aa64SRichard Purdie 
70240e0aa64SRichard Purdie /* power down chip */
70340e0aa64SRichard Purdie static int wm8731_remove(struct platform_device *pdev)
70440e0aa64SRichard Purdie {
70540e0aa64SRichard Purdie 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
70640e0aa64SRichard Purdie 	struct snd_soc_codec *codec = socdev->codec;
70740e0aa64SRichard Purdie 
70840e0aa64SRichard Purdie 	if (codec->control_data)
7090be9898aSMark Brown 		wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF);
71040e0aa64SRichard Purdie 
71140e0aa64SRichard Purdie 	snd_soc_free_pcms(socdev);
71240e0aa64SRichard Purdie 	snd_soc_dapm_free(socdev);
71340e0aa64SRichard Purdie #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
71440e0aa64SRichard Purdie 	i2c_del_driver(&wm8731_i2c_driver);
71540e0aa64SRichard Purdie #endif
716b36d61d4SFrank Mandarino 	kfree(codec->private_data);
71740e0aa64SRichard Purdie 	kfree(codec);
71840e0aa64SRichard Purdie 
71940e0aa64SRichard Purdie 	return 0;
72040e0aa64SRichard Purdie }
72140e0aa64SRichard Purdie 
72240e0aa64SRichard Purdie struct snd_soc_codec_device soc_codec_dev_wm8731 = {
72340e0aa64SRichard Purdie 	.probe = 	wm8731_probe,
72440e0aa64SRichard Purdie 	.remove = 	wm8731_remove,
72540e0aa64SRichard Purdie 	.suspend = 	wm8731_suspend,
72640e0aa64SRichard Purdie 	.resume =	wm8731_resume,
72740e0aa64SRichard Purdie };
72840e0aa64SRichard Purdie EXPORT_SYMBOL_GPL(soc_codec_dev_wm8731);
72940e0aa64SRichard Purdie 
73040e0aa64SRichard Purdie MODULE_DESCRIPTION("ASoC WM8731 driver");
73140e0aa64SRichard Purdie MODULE_AUTHOR("Richard Purdie");
73240e0aa64SRichard Purdie MODULE_LICENSE("GPL");
733