xref: /linux/sound/soc/codecs/wm8731.c (revision b36d61d45654104c04ff71055ef09c696fea5f89)
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/driver.h>
2340e0aa64SRichard Purdie #include <sound/core.h>
2440e0aa64SRichard Purdie #include <sound/pcm.h>
2540e0aa64SRichard Purdie #include <sound/pcm_params.h>
2640e0aa64SRichard Purdie #include <sound/soc.h>
2740e0aa64SRichard Purdie #include <sound/soc-dapm.h>
2840e0aa64SRichard Purdie #include <sound/initval.h>
2940e0aa64SRichard Purdie 
3040e0aa64SRichard Purdie #include "wm8731.h"
3140e0aa64SRichard Purdie 
3240e0aa64SRichard Purdie #define AUDIO_NAME "wm8731"
33*b36d61d4SFrank Mandarino #define WM8731_VERSION "0.13"
3440e0aa64SRichard Purdie 
3540e0aa64SRichard Purdie /*
3640e0aa64SRichard Purdie  * Debug
3740e0aa64SRichard Purdie  */
3840e0aa64SRichard Purdie 
3940e0aa64SRichard Purdie #define WM8731_DEBUG 0
4040e0aa64SRichard Purdie 
4140e0aa64SRichard Purdie #ifdef WM8731_DEBUG
4240e0aa64SRichard Purdie #define dbg(format, arg...) \
4340e0aa64SRichard Purdie 	printk(KERN_DEBUG AUDIO_NAME ": " format "\n" , ## arg)
4440e0aa64SRichard Purdie #else
4540e0aa64SRichard Purdie #define dbg(format, arg...) do {} while (0)
4640e0aa64SRichard Purdie #endif
4740e0aa64SRichard Purdie #define err(format, arg...) \
4840e0aa64SRichard Purdie 	printk(KERN_ERR AUDIO_NAME ": " format "\n" , ## arg)
4940e0aa64SRichard Purdie #define info(format, arg...) \
5040e0aa64SRichard Purdie 	printk(KERN_INFO AUDIO_NAME ": " format "\n" , ## arg)
5140e0aa64SRichard Purdie #define warn(format, arg...) \
5240e0aa64SRichard Purdie 	printk(KERN_WARNING AUDIO_NAME ": " format "\n" , ## arg)
5340e0aa64SRichard Purdie 
5440e0aa64SRichard Purdie struct snd_soc_codec_device soc_codec_dev_wm8731;
5540e0aa64SRichard Purdie 
56*b36d61d4SFrank Mandarino /* codec private data */
57*b36d61d4SFrank Mandarino struct wm8731_priv {
58*b36d61d4SFrank Mandarino 	unsigned int sysclk;
59*b36d61d4SFrank Mandarino };
60*b36d61d4SFrank Mandarino 
6140e0aa64SRichard Purdie /*
6240e0aa64SRichard Purdie  * wm8731 register cache
6340e0aa64SRichard Purdie  * We can't read the WM8731 register space when we are
6440e0aa64SRichard Purdie  * using 2 wire for device control, so we cache them instead.
6540e0aa64SRichard Purdie  * There is no point in caching the reset register
6640e0aa64SRichard Purdie  */
6740e0aa64SRichard Purdie static const u16 wm8731_reg[WM8731_CACHEREGNUM] = {
6840e0aa64SRichard Purdie     0x0097, 0x0097, 0x0079, 0x0079,
6940e0aa64SRichard Purdie     0x000a, 0x0008, 0x009f, 0x000a,
7040e0aa64SRichard Purdie     0x0000, 0x0000
7140e0aa64SRichard Purdie };
7240e0aa64SRichard Purdie 
7340e0aa64SRichard Purdie /*
7440e0aa64SRichard Purdie  * read wm8731 register cache
7540e0aa64SRichard Purdie  */
7640e0aa64SRichard Purdie static inline unsigned int wm8731_read_reg_cache(struct snd_soc_codec *codec,
7740e0aa64SRichard Purdie 	unsigned int reg)
7840e0aa64SRichard Purdie {
7940e0aa64SRichard Purdie 	u16 *cache = codec->reg_cache;
8040e0aa64SRichard Purdie 	if (reg == WM8731_RESET)
8140e0aa64SRichard Purdie 		return 0;
8240e0aa64SRichard Purdie 	if (reg >= WM8731_CACHEREGNUM)
8340e0aa64SRichard Purdie 		return -1;
8440e0aa64SRichard Purdie 	return cache[reg];
8540e0aa64SRichard Purdie }
8640e0aa64SRichard Purdie 
8740e0aa64SRichard Purdie /*
8840e0aa64SRichard Purdie  * write wm8731 register cache
8940e0aa64SRichard Purdie  */
9040e0aa64SRichard Purdie static inline void wm8731_write_reg_cache(struct snd_soc_codec *codec,
9140e0aa64SRichard Purdie 	u16 reg, unsigned int value)
9240e0aa64SRichard Purdie {
9340e0aa64SRichard Purdie 	u16 *cache = codec->reg_cache;
9440e0aa64SRichard Purdie 	if (reg >= WM8731_CACHEREGNUM)
9540e0aa64SRichard Purdie 		return;
9640e0aa64SRichard Purdie 	cache[reg] = value;
9740e0aa64SRichard Purdie }
9840e0aa64SRichard Purdie 
9940e0aa64SRichard Purdie /*
10040e0aa64SRichard Purdie  * write to the WM8731 register space
10140e0aa64SRichard Purdie  */
10240e0aa64SRichard Purdie static int wm8731_write(struct snd_soc_codec *codec, unsigned int reg,
10340e0aa64SRichard Purdie 	unsigned int value)
10440e0aa64SRichard Purdie {
10540e0aa64SRichard Purdie 	u8 data[2];
10640e0aa64SRichard Purdie 
10740e0aa64SRichard Purdie 	/* data is
10840e0aa64SRichard Purdie 	 *   D15..D9 WM8731 register offset
10940e0aa64SRichard Purdie 	 *   D8...D0 register data
11040e0aa64SRichard Purdie 	 */
11140e0aa64SRichard Purdie 	data[0] = (reg << 1) | ((value >> 8) & 0x0001);
11240e0aa64SRichard Purdie 	data[1] = value & 0x00ff;
11340e0aa64SRichard Purdie 
11440e0aa64SRichard Purdie 	wm8731_write_reg_cache (codec, reg, value);
11540e0aa64SRichard Purdie 	if (codec->hw_write(codec->control_data, data, 2) == 2)
11640e0aa64SRichard Purdie 		return 0;
11740e0aa64SRichard Purdie 	else
11840e0aa64SRichard Purdie 		return -EIO;
11940e0aa64SRichard Purdie }
12040e0aa64SRichard Purdie 
12140e0aa64SRichard Purdie #define wm8731_reset(c)	wm8731_write(c, WM8731_RESET, 0)
12240e0aa64SRichard Purdie 
12340e0aa64SRichard Purdie static const char *wm8731_input_select[] = {"Line In", "Mic"};
12440e0aa64SRichard Purdie static const char *wm8731_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"};
12540e0aa64SRichard Purdie 
12640e0aa64SRichard Purdie static const struct soc_enum wm8731_enum[] = {
12740e0aa64SRichard Purdie 	SOC_ENUM_SINGLE(WM8731_APANA, 2, 2, wm8731_input_select),
12840e0aa64SRichard Purdie 	SOC_ENUM_SINGLE(WM8731_APDIGI, 1, 4, wm8731_deemph),
12940e0aa64SRichard Purdie };
13040e0aa64SRichard Purdie 
13140e0aa64SRichard Purdie static const struct snd_kcontrol_new wm8731_snd_controls[] = {
13240e0aa64SRichard Purdie 
133bd903b6eSLiam Girdwood SOC_DOUBLE_R("Master Playback Volume", WM8731_LOUT1V, WM8731_ROUT1V,
134bd903b6eSLiam Girdwood 	0, 127, 0),
135bd903b6eSLiam Girdwood SOC_DOUBLE_R("Master Playback ZC Switch", WM8731_LOUT1V, WM8731_ROUT1V,
136bd903b6eSLiam Girdwood 	7, 1, 0),
13740e0aa64SRichard Purdie 
13840e0aa64SRichard Purdie SOC_DOUBLE_R("Capture Volume", WM8731_LINVOL, WM8731_RINVOL, 0, 31, 0),
13940e0aa64SRichard Purdie SOC_DOUBLE_R("Line Capture Switch", WM8731_LINVOL, WM8731_RINVOL, 7, 1, 1),
14040e0aa64SRichard Purdie 
14140e0aa64SRichard Purdie SOC_SINGLE("Mic Boost (+20dB)", WM8731_APANA, 0, 1, 0),
14240e0aa64SRichard Purdie SOC_SINGLE("Capture Mic Switch", WM8731_APANA, 1, 1, 1),
14340e0aa64SRichard Purdie 
14440e0aa64SRichard Purdie SOC_SINGLE("Sidetone Playback Volume", WM8731_APANA, 6, 3, 1),
14540e0aa64SRichard Purdie 
14640e0aa64SRichard Purdie SOC_SINGLE("ADC High Pass Filter Switch", WM8731_APDIGI, 0, 1, 1),
14740e0aa64SRichard Purdie SOC_SINGLE("Store DC Offset Switch", WM8731_APDIGI, 4, 1, 0),
14840e0aa64SRichard Purdie 
14940e0aa64SRichard Purdie SOC_ENUM("Playback De-emphasis", wm8731_enum[1]),
15040e0aa64SRichard Purdie };
15140e0aa64SRichard Purdie 
15240e0aa64SRichard Purdie /* add non dapm controls */
15340e0aa64SRichard Purdie static int wm8731_add_controls(struct snd_soc_codec *codec)
15440e0aa64SRichard Purdie {
15540e0aa64SRichard Purdie 	int err, i;
15640e0aa64SRichard Purdie 
15740e0aa64SRichard Purdie 	for (i = 0; i < ARRAY_SIZE(wm8731_snd_controls); i++) {
15840e0aa64SRichard Purdie 		if ((err = snd_ctl_add(codec->card,
15940e0aa64SRichard Purdie 				snd_soc_cnew(&wm8731_snd_controls[i],codec, NULL))) < 0)
16040e0aa64SRichard Purdie 			return err;
16140e0aa64SRichard Purdie 	}
16240e0aa64SRichard Purdie 
16340e0aa64SRichard Purdie 	return 0;
16440e0aa64SRichard Purdie }
16540e0aa64SRichard Purdie 
16640e0aa64SRichard Purdie /* Output Mixer */
16740e0aa64SRichard Purdie static const struct snd_kcontrol_new wm8731_output_mixer_controls[] = {
16840e0aa64SRichard Purdie SOC_DAPM_SINGLE("Line Bypass Switch", WM8731_APANA, 3, 1, 0),
16940e0aa64SRichard Purdie SOC_DAPM_SINGLE("Mic Sidetone Switch", WM8731_APANA, 5, 1, 0),
17040e0aa64SRichard Purdie SOC_DAPM_SINGLE("HiFi Playback Switch", WM8731_APANA, 4, 1, 0),
17140e0aa64SRichard Purdie };
17240e0aa64SRichard Purdie 
17340e0aa64SRichard Purdie /* Input mux */
17440e0aa64SRichard Purdie static const struct snd_kcontrol_new wm8731_input_mux_controls =
17540e0aa64SRichard Purdie SOC_DAPM_ENUM("Input Select", wm8731_enum[0]);
17640e0aa64SRichard Purdie 
17740e0aa64SRichard Purdie static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
17840e0aa64SRichard Purdie SND_SOC_DAPM_MIXER("Output Mixer", WM8731_PWR, 4, 1,
17940e0aa64SRichard Purdie 	&wm8731_output_mixer_controls[0],
18040e0aa64SRichard Purdie 	ARRAY_SIZE(wm8731_output_mixer_controls)),
18140e0aa64SRichard Purdie SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8731_PWR, 3, 1),
18240e0aa64SRichard Purdie SND_SOC_DAPM_OUTPUT("LOUT"),
18340e0aa64SRichard Purdie SND_SOC_DAPM_OUTPUT("LHPOUT"),
18440e0aa64SRichard Purdie SND_SOC_DAPM_OUTPUT("ROUT"),
18540e0aa64SRichard Purdie SND_SOC_DAPM_OUTPUT("RHPOUT"),
18640e0aa64SRichard Purdie SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8731_PWR, 2, 1),
18740e0aa64SRichard Purdie SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &wm8731_input_mux_controls),
18840e0aa64SRichard Purdie SND_SOC_DAPM_PGA("Line Input", WM8731_PWR, 0, 1, NULL, 0),
18940e0aa64SRichard Purdie SND_SOC_DAPM_MICBIAS("Mic Bias", WM8731_PWR, 1, 1),
19040e0aa64SRichard Purdie SND_SOC_DAPM_INPUT("MICIN"),
19140e0aa64SRichard Purdie SND_SOC_DAPM_INPUT("RLINEIN"),
19240e0aa64SRichard Purdie SND_SOC_DAPM_INPUT("LLINEIN"),
19340e0aa64SRichard Purdie };
19440e0aa64SRichard Purdie 
19540e0aa64SRichard Purdie static const char *intercon[][3] = {
19640e0aa64SRichard Purdie 	/* output mixer */
19740e0aa64SRichard Purdie 	{"Output Mixer", "Line Bypass Switch", "Line Input"},
19840e0aa64SRichard Purdie 	{"Output Mixer", "HiFi Playback Switch", "DAC"},
19940e0aa64SRichard Purdie 	{"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
20040e0aa64SRichard Purdie 
20140e0aa64SRichard Purdie 	/* outputs */
20240e0aa64SRichard Purdie 	{"RHPOUT", NULL, "Output Mixer"},
20340e0aa64SRichard Purdie 	{"ROUT", NULL, "Output Mixer"},
20440e0aa64SRichard Purdie 	{"LHPOUT", NULL, "Output Mixer"},
20540e0aa64SRichard Purdie 	{"LOUT", NULL, "Output Mixer"},
20640e0aa64SRichard Purdie 
20740e0aa64SRichard Purdie 	/* input mux */
20840e0aa64SRichard Purdie 	{"Input Mux", "Line In", "Line Input"},
20940e0aa64SRichard Purdie 	{"Input Mux", "Mic", "Mic Bias"},
21040e0aa64SRichard Purdie 	{"ADC", NULL, "Input Mux"},
21140e0aa64SRichard Purdie 
21240e0aa64SRichard Purdie 	/* inputs */
21340e0aa64SRichard Purdie 	{"Line Input", NULL, "LLINEIN"},
21440e0aa64SRichard Purdie 	{"Line Input", NULL, "RLINEIN"},
21540e0aa64SRichard Purdie 	{"Mic Bias", NULL, "MICIN"},
21640e0aa64SRichard Purdie 
21740e0aa64SRichard Purdie 	/* terminator */
21840e0aa64SRichard Purdie 	{NULL, NULL, NULL},
21940e0aa64SRichard Purdie };
22040e0aa64SRichard Purdie 
22140e0aa64SRichard Purdie static int wm8731_add_widgets(struct snd_soc_codec *codec)
22240e0aa64SRichard Purdie {
22340e0aa64SRichard Purdie 	int i;
22440e0aa64SRichard Purdie 
22540e0aa64SRichard Purdie 	for(i = 0; i < ARRAY_SIZE(wm8731_dapm_widgets); i++) {
22640e0aa64SRichard Purdie 		snd_soc_dapm_new_control(codec, &wm8731_dapm_widgets[i]);
22740e0aa64SRichard Purdie 	}
22840e0aa64SRichard Purdie 
22940e0aa64SRichard Purdie 	/* set up audio path interconnects */
23040e0aa64SRichard Purdie 	for(i = 0; intercon[i][0] != NULL; i++) {
23140e0aa64SRichard Purdie 		snd_soc_dapm_connect_input(codec, intercon[i][0],
23240e0aa64SRichard Purdie 			intercon[i][1], intercon[i][2]);
23340e0aa64SRichard Purdie 	}
23440e0aa64SRichard Purdie 
23540e0aa64SRichard Purdie 	snd_soc_dapm_new_widgets(codec);
23640e0aa64SRichard Purdie 	return 0;
23740e0aa64SRichard Purdie }
23840e0aa64SRichard Purdie 
23940e0aa64SRichard Purdie struct _coeff_div {
24040e0aa64SRichard Purdie 	u32 mclk;
24140e0aa64SRichard Purdie 	u32 rate;
24240e0aa64SRichard Purdie 	u16 fs;
24340e0aa64SRichard Purdie 	u8 sr:4;
24440e0aa64SRichard Purdie 	u8 bosr:1;
24540e0aa64SRichard Purdie 	u8 usb:1;
24640e0aa64SRichard Purdie };
24740e0aa64SRichard Purdie 
24840e0aa64SRichard Purdie /* codec mclk clock divider coefficients */
24940e0aa64SRichard Purdie static const struct _coeff_div coeff_div[] = {
25040e0aa64SRichard Purdie 	/* 48k */
25140e0aa64SRichard Purdie 	{12288000, 48000, 256, 0x0, 0x0, 0x0},
25240e0aa64SRichard Purdie 	{18432000, 48000, 384, 0x0, 0x1, 0x0},
25340e0aa64SRichard Purdie 	{12000000, 48000, 250, 0x0, 0x0, 0x1},
25440e0aa64SRichard Purdie 
25540e0aa64SRichard Purdie 	/* 32k */
25640e0aa64SRichard Purdie 	{12288000, 32000, 384, 0x6, 0x0, 0x0},
25740e0aa64SRichard Purdie 	{18432000, 32000, 576, 0x6, 0x1, 0x0},
258298a2c75SFrank Mandarino 	{12000000, 32000, 375, 0x6, 0x0, 0x1},
25940e0aa64SRichard Purdie 
26040e0aa64SRichard Purdie 	/* 8k */
26140e0aa64SRichard Purdie 	{12288000, 8000, 1536, 0x3, 0x0, 0x0},
26240e0aa64SRichard Purdie 	{18432000, 8000, 2304, 0x3, 0x1, 0x0},
26340e0aa64SRichard Purdie 	{11289600, 8000, 1408, 0xb, 0x0, 0x0},
26440e0aa64SRichard Purdie 	{16934400, 8000, 2112, 0xb, 0x1, 0x0},
26540e0aa64SRichard Purdie 	{12000000, 8000, 1500, 0x3, 0x0, 0x1},
26640e0aa64SRichard Purdie 
26740e0aa64SRichard Purdie 	/* 96k */
26840e0aa64SRichard Purdie 	{12288000, 96000, 128, 0x7, 0x0, 0x0},
26940e0aa64SRichard Purdie 	{18432000, 96000, 192, 0x7, 0x1, 0x0},
27040e0aa64SRichard Purdie 	{12000000, 96000, 125, 0x7, 0x0, 0x1},
27140e0aa64SRichard Purdie 
27240e0aa64SRichard Purdie 	/* 44.1k */
27340e0aa64SRichard Purdie 	{11289600, 44100, 256, 0x8, 0x0, 0x0},
27440e0aa64SRichard Purdie 	{16934400, 44100, 384, 0x8, 0x1, 0x0},
27540e0aa64SRichard Purdie 	{12000000, 44100, 272, 0x8, 0x1, 0x1},
27640e0aa64SRichard Purdie 
27740e0aa64SRichard Purdie 	/* 88.2k */
27840e0aa64SRichard Purdie 	{11289600, 88200, 128, 0xf, 0x0, 0x0},
27940e0aa64SRichard Purdie 	{16934400, 88200, 192, 0xf, 0x1, 0x0},
28040e0aa64SRichard Purdie 	{12000000, 88200, 136, 0xf, 0x1, 0x1},
28140e0aa64SRichard Purdie };
28240e0aa64SRichard Purdie 
28340e0aa64SRichard Purdie static inline int get_coeff(int mclk, int rate)
28440e0aa64SRichard Purdie {
28540e0aa64SRichard Purdie 	int i;
28640e0aa64SRichard Purdie 
28740e0aa64SRichard Purdie 	for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
28840e0aa64SRichard Purdie 		if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
28940e0aa64SRichard Purdie 			return i;
29040e0aa64SRichard Purdie 	}
29140e0aa64SRichard Purdie 	return 0;
29240e0aa64SRichard Purdie }
29340e0aa64SRichard Purdie 
294*b36d61d4SFrank Mandarino static int wm8731_hw_params(struct snd_pcm_substream *substream,
295*b36d61d4SFrank Mandarino 	struct snd_pcm_hw_params *params)
29640e0aa64SRichard Purdie {
297*b36d61d4SFrank Mandarino 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
298*b36d61d4SFrank Mandarino 	struct snd_soc_device *socdev = rtd->socdev;
299*b36d61d4SFrank Mandarino 	struct snd_soc_codec *codec = socdev->codec;
300*b36d61d4SFrank Mandarino 	struct wm8731_priv *wm8731 = codec->private_data;
301*b36d61d4SFrank Mandarino 	u16 iface = wm8731_read_reg_cache(codec, WM8731_IFACE) & 0xfff3;
302*b36d61d4SFrank Mandarino 	int i = get_coeff(wm8731->sysclk, params_rate(params));
303*b36d61d4SFrank Mandarino 	u16 srate = (coeff_div[i].sr << 2) |
304*b36d61d4SFrank Mandarino 		(coeff_div[i].bosr << 1) | coeff_div[i].usb;
30540e0aa64SRichard Purdie 
306*b36d61d4SFrank Mandarino 	wm8731_write(codec, WM8731_SRATE, srate);
30740e0aa64SRichard Purdie 
308*b36d61d4SFrank Mandarino 	/* bit size */
309*b36d61d4SFrank Mandarino 	switch (params_format(params)) {
310*b36d61d4SFrank Mandarino 	case SNDRV_PCM_FORMAT_S16_LE:
311*b36d61d4SFrank Mandarino 		break;
312*b36d61d4SFrank Mandarino 	case SNDRV_PCM_FORMAT_S20_3LE:
313*b36d61d4SFrank Mandarino 		iface |= 0x0004;
314*b36d61d4SFrank Mandarino 		break;
315*b36d61d4SFrank Mandarino 	case SNDRV_PCM_FORMAT_S24_LE:
316*b36d61d4SFrank Mandarino 		iface |= 0x0008;
317*b36d61d4SFrank Mandarino 		break;
318*b36d61d4SFrank Mandarino 	}
319*b36d61d4SFrank Mandarino 
320*b36d61d4SFrank Mandarino 	wm8731_write(codec, WM8731_IFACE, iface);
321*b36d61d4SFrank Mandarino 	return 0;
32240e0aa64SRichard Purdie }
32340e0aa64SRichard Purdie 
32440e0aa64SRichard Purdie static int wm8731_pcm_prepare(struct snd_pcm_substream *substream)
32540e0aa64SRichard Purdie {
32640e0aa64SRichard Purdie 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
32740e0aa64SRichard Purdie 	struct snd_soc_device *socdev = rtd->socdev;
32840e0aa64SRichard Purdie 	struct snd_soc_codec *codec = socdev->codec;
32940e0aa64SRichard Purdie 
33040e0aa64SRichard Purdie 	/* set active */
33140e0aa64SRichard Purdie 	wm8731_write(codec, WM8731_ACTIVE, 0x0001);
332*b36d61d4SFrank Mandarino 
33340e0aa64SRichard Purdie 	return 0;
33440e0aa64SRichard Purdie }
33540e0aa64SRichard Purdie 
33640e0aa64SRichard Purdie static void wm8731_shutdown(struct snd_pcm_substream *substream)
33740e0aa64SRichard Purdie {
33840e0aa64SRichard Purdie 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
33940e0aa64SRichard Purdie 	struct snd_soc_device *socdev = rtd->socdev;
34040e0aa64SRichard Purdie 	struct snd_soc_codec *codec = socdev->codec;
34140e0aa64SRichard Purdie 
34240e0aa64SRichard Purdie 	/* deactivate */
34340e0aa64SRichard Purdie 	if (!codec->active) {
34440e0aa64SRichard Purdie 		udelay(50);
34540e0aa64SRichard Purdie 		wm8731_write(codec, WM8731_ACTIVE, 0x0);
34640e0aa64SRichard Purdie 	}
34740e0aa64SRichard Purdie }
34840e0aa64SRichard Purdie 
349*b36d61d4SFrank Mandarino static int wm8731_mute(struct snd_soc_codec_dai *dai, int mute)
35040e0aa64SRichard Purdie {
351*b36d61d4SFrank Mandarino 	struct snd_soc_codec *codec = dai->codec;
35240e0aa64SRichard Purdie 	u16 mute_reg = wm8731_read_reg_cache(codec, WM8731_APDIGI) & 0xfff7;
353*b36d61d4SFrank Mandarino 
35440e0aa64SRichard Purdie 	if (mute)
35540e0aa64SRichard Purdie 		wm8731_write(codec, WM8731_APDIGI, mute_reg | 0x8);
35640e0aa64SRichard Purdie 	else
35740e0aa64SRichard Purdie 		wm8731_write(codec, WM8731_APDIGI, mute_reg);
35840e0aa64SRichard Purdie 	return 0;
35940e0aa64SRichard Purdie }
36040e0aa64SRichard Purdie 
361*b36d61d4SFrank Mandarino static int wm8731_set_dai_sysclk(struct snd_soc_codec_dai *codec_dai,
362*b36d61d4SFrank Mandarino 		int clk_id, unsigned int freq, int dir)
363*b36d61d4SFrank Mandarino {
364*b36d61d4SFrank Mandarino 	struct snd_soc_codec *codec = codec_dai->codec;
365*b36d61d4SFrank Mandarino 	struct wm8731_priv *wm8731 = codec->private_data;
366*b36d61d4SFrank Mandarino 
367*b36d61d4SFrank Mandarino 	switch (freq) {
368*b36d61d4SFrank Mandarino 	case 11289600:
369*b36d61d4SFrank Mandarino 	case 12000000:
370*b36d61d4SFrank Mandarino 	case 12288000:
371*b36d61d4SFrank Mandarino 	case 16934400:
372*b36d61d4SFrank Mandarino 	case 18432000:
373*b36d61d4SFrank Mandarino 		wm8731->sysclk = freq;
374*b36d61d4SFrank Mandarino 		return 0;
375*b36d61d4SFrank Mandarino 	}
376*b36d61d4SFrank Mandarino 	return -EINVAL;
377*b36d61d4SFrank Mandarino }
378*b36d61d4SFrank Mandarino 
379*b36d61d4SFrank Mandarino 
380*b36d61d4SFrank Mandarino static int wm8731_set_dai_fmt(struct snd_soc_codec_dai *codec_dai,
381*b36d61d4SFrank Mandarino 		unsigned int fmt)
382*b36d61d4SFrank Mandarino {
383*b36d61d4SFrank Mandarino 	struct snd_soc_codec *codec = codec_dai->codec;
384*b36d61d4SFrank Mandarino 	u16 iface = 0;
385*b36d61d4SFrank Mandarino 
386*b36d61d4SFrank Mandarino 	/* set master/slave audio interface */
387*b36d61d4SFrank Mandarino 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
388*b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_CBM_CFM:
389*b36d61d4SFrank Mandarino 		iface |= 0x0040;
390*b36d61d4SFrank Mandarino 		break;
391*b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_CBS_CFS:
392*b36d61d4SFrank Mandarino 		break;
393*b36d61d4SFrank Mandarino 	default:
394*b36d61d4SFrank Mandarino 		return -EINVAL;
395*b36d61d4SFrank Mandarino 	}
396*b36d61d4SFrank Mandarino 
397*b36d61d4SFrank Mandarino 	/* interface format */
398*b36d61d4SFrank Mandarino 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
399*b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_I2S:
400*b36d61d4SFrank Mandarino 		iface |= 0x0002;
401*b36d61d4SFrank Mandarino 		break;
402*b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_RIGHT_J:
403*b36d61d4SFrank Mandarino 		break;
404*b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_LEFT_J:
405*b36d61d4SFrank Mandarino 		iface |= 0x0001;
406*b36d61d4SFrank Mandarino 		break;
407*b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_DSP_A:
408*b36d61d4SFrank Mandarino 		iface |= 0x0003;
409*b36d61d4SFrank Mandarino 		break;
410*b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_DSP_B:
411*b36d61d4SFrank Mandarino 		iface |= 0x0013;
412*b36d61d4SFrank Mandarino 		break;
413*b36d61d4SFrank Mandarino 	default:
414*b36d61d4SFrank Mandarino 		return -EINVAL;
415*b36d61d4SFrank Mandarino 	}
416*b36d61d4SFrank Mandarino 
417*b36d61d4SFrank Mandarino 	/* clock inversion */
418*b36d61d4SFrank Mandarino 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
419*b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_NB_NF:
420*b36d61d4SFrank Mandarino 		break;
421*b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_IB_IF:
422*b36d61d4SFrank Mandarino 		iface |= 0x0090;
423*b36d61d4SFrank Mandarino 		break;
424*b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_IB_NF:
425*b36d61d4SFrank Mandarino 		iface |= 0x0080;
426*b36d61d4SFrank Mandarino 		break;
427*b36d61d4SFrank Mandarino 	case SND_SOC_DAIFMT_NB_IF:
428*b36d61d4SFrank Mandarino 		iface |= 0x0010;
429*b36d61d4SFrank Mandarino 		break;
430*b36d61d4SFrank Mandarino 	default:
431*b36d61d4SFrank Mandarino 		return -EINVAL;
432*b36d61d4SFrank Mandarino 	}
433*b36d61d4SFrank Mandarino 
434*b36d61d4SFrank Mandarino 	/* set iface */
435*b36d61d4SFrank Mandarino 	wm8731_write(codec, WM8731_IFACE, iface);
436*b36d61d4SFrank Mandarino 	return 0;
437*b36d61d4SFrank Mandarino }
438*b36d61d4SFrank Mandarino 
43940e0aa64SRichard Purdie static int wm8731_dapm_event(struct snd_soc_codec *codec, int event)
44040e0aa64SRichard Purdie {
44140e0aa64SRichard Purdie 	u16 reg = wm8731_read_reg_cache(codec, WM8731_PWR) & 0xff7f;
44240e0aa64SRichard Purdie 
44340e0aa64SRichard Purdie 	switch (event) {
44440e0aa64SRichard Purdie 	case SNDRV_CTL_POWER_D0: /* full On */
44540e0aa64SRichard Purdie 		/* vref/mid, osc on, dac unmute */
44640e0aa64SRichard Purdie 		wm8731_write(codec, WM8731_PWR, reg);
44740e0aa64SRichard Purdie 		break;
44840e0aa64SRichard Purdie 	case SNDRV_CTL_POWER_D1: /* partial On */
44940e0aa64SRichard Purdie 	case SNDRV_CTL_POWER_D2: /* partial On */
45040e0aa64SRichard Purdie 		break;
45140e0aa64SRichard Purdie 	case SNDRV_CTL_POWER_D3hot: /* Off, with power */
45240e0aa64SRichard Purdie 		/* everything off except vref/vmid, */
45340e0aa64SRichard Purdie 		wm8731_write(codec, WM8731_PWR, reg | 0x0040);
45440e0aa64SRichard Purdie 		break;
45540e0aa64SRichard Purdie 	case SNDRV_CTL_POWER_D3cold: /* Off, without power */
45640e0aa64SRichard Purdie 		/* everything off, dac mute, inactive */
45740e0aa64SRichard Purdie 		wm8731_write(codec, WM8731_ACTIVE, 0x0);
45840e0aa64SRichard Purdie 		wm8731_write(codec, WM8731_PWR, 0xffff);
45940e0aa64SRichard Purdie 		break;
46040e0aa64SRichard Purdie 	}
46140e0aa64SRichard Purdie 	codec->dapm_state = event;
46240e0aa64SRichard Purdie 	return 0;
46340e0aa64SRichard Purdie }
46440e0aa64SRichard Purdie 
465*b36d61d4SFrank Mandarino #define WM8731_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
466*b36d61d4SFrank Mandarino 		SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
467*b36d61d4SFrank Mandarino 		SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\
468*b36d61d4SFrank Mandarino 		SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |\
469*b36d61d4SFrank Mandarino 		SNDRV_PCM_RATE_96000)
470*b36d61d4SFrank Mandarino 
471*b36d61d4SFrank Mandarino #define WM8731_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
472*b36d61d4SFrank Mandarino 	SNDRV_PCM_FMTBIT_S24_LE)
473*b36d61d4SFrank Mandarino 
47440e0aa64SRichard Purdie struct snd_soc_codec_dai wm8731_dai = {
47540e0aa64SRichard Purdie 	.name = "WM8731",
47640e0aa64SRichard Purdie 	.playback = {
47740e0aa64SRichard Purdie 		.stream_name = "Playback",
47840e0aa64SRichard Purdie 		.channels_min = 1,
47940e0aa64SRichard Purdie 		.channels_max = 2,
480*b36d61d4SFrank Mandarino 		.rates = WM8731_RATES,
481*b36d61d4SFrank Mandarino 		.formats = WM8731_FORMATS,},
48240e0aa64SRichard Purdie 	.capture = {
48340e0aa64SRichard Purdie 		.stream_name = "Capture",
48440e0aa64SRichard Purdie 		.channels_min = 1,
48540e0aa64SRichard Purdie 		.channels_max = 2,
486*b36d61d4SFrank Mandarino 		.rates = WM8731_RATES,
487*b36d61d4SFrank Mandarino 		.formats = WM8731_FORMATS,},
48840e0aa64SRichard Purdie 	.ops = {
48940e0aa64SRichard Purdie 		.prepare = wm8731_pcm_prepare,
490*b36d61d4SFrank Mandarino 		.hw_params = wm8731_hw_params,
49140e0aa64SRichard Purdie 		.shutdown = wm8731_shutdown,
49240e0aa64SRichard Purdie 	},
493*b36d61d4SFrank Mandarino 	.dai_ops = {
494*b36d61d4SFrank Mandarino 		.digital_mute = wm8731_mute,
495*b36d61d4SFrank Mandarino 		.set_sysclk = wm8731_set_dai_sysclk,
496*b36d61d4SFrank Mandarino 		.set_fmt = wm8731_set_dai_fmt,
497*b36d61d4SFrank Mandarino 	}
49840e0aa64SRichard Purdie };
49940e0aa64SRichard Purdie EXPORT_SYMBOL_GPL(wm8731_dai);
50040e0aa64SRichard Purdie 
50140e0aa64SRichard Purdie static int wm8731_suspend(struct platform_device *pdev, pm_message_t state)
50240e0aa64SRichard Purdie {
50340e0aa64SRichard Purdie 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
50440e0aa64SRichard Purdie 	struct snd_soc_codec *codec = socdev->codec;
50540e0aa64SRichard Purdie 
50640e0aa64SRichard Purdie 	wm8731_write(codec, WM8731_ACTIVE, 0x0);
50740e0aa64SRichard Purdie 	wm8731_dapm_event(codec, SNDRV_CTL_POWER_D3cold);
50840e0aa64SRichard Purdie 	return 0;
50940e0aa64SRichard Purdie }
51040e0aa64SRichard Purdie 
51140e0aa64SRichard Purdie static int wm8731_resume(struct platform_device *pdev)
51240e0aa64SRichard Purdie {
51340e0aa64SRichard Purdie 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
51440e0aa64SRichard Purdie 	struct snd_soc_codec *codec = socdev->codec;
51540e0aa64SRichard Purdie 	int i;
51640e0aa64SRichard Purdie 	u8 data[2];
51740e0aa64SRichard Purdie 	u16 *cache = codec->reg_cache;
51840e0aa64SRichard Purdie 
51940e0aa64SRichard Purdie 	/* Sync reg_cache with the hardware */
52040e0aa64SRichard Purdie 	for (i = 0; i < ARRAY_SIZE(wm8731_reg); i++) {
52140e0aa64SRichard Purdie 		data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001);
52240e0aa64SRichard Purdie 		data[1] = cache[i] & 0x00ff;
52340e0aa64SRichard Purdie 		codec->hw_write(codec->control_data, data, 2);
52440e0aa64SRichard Purdie 	}
52540e0aa64SRichard Purdie 	wm8731_dapm_event(codec, SNDRV_CTL_POWER_D3hot);
52640e0aa64SRichard Purdie 	wm8731_dapm_event(codec, codec->suspend_dapm_state);
52740e0aa64SRichard Purdie 	return 0;
52840e0aa64SRichard Purdie }
52940e0aa64SRichard Purdie 
53040e0aa64SRichard Purdie /*
53140e0aa64SRichard Purdie  * initialise the WM8731 driver
53240e0aa64SRichard Purdie  * register the mixer and dsp interfaces with the kernel
53340e0aa64SRichard Purdie  */
53440e0aa64SRichard Purdie static int wm8731_init(struct snd_soc_device *socdev)
53540e0aa64SRichard Purdie {
53640e0aa64SRichard Purdie 	struct snd_soc_codec *codec = socdev->codec;
53740e0aa64SRichard Purdie 	int reg, ret = 0;
53840e0aa64SRichard Purdie 
53940e0aa64SRichard Purdie 	codec->name = "WM8731";
54040e0aa64SRichard Purdie 	codec->owner = THIS_MODULE;
54140e0aa64SRichard Purdie 	codec->read = wm8731_read_reg_cache;
54240e0aa64SRichard Purdie 	codec->write = wm8731_write;
54340e0aa64SRichard Purdie 	codec->dapm_event = wm8731_dapm_event;
54440e0aa64SRichard Purdie 	codec->dai = &wm8731_dai;
54540e0aa64SRichard Purdie 	codec->num_dai = 1;
54640e0aa64SRichard Purdie 	codec->reg_cache_size = ARRAY_SIZE(wm8731_reg);
54740e0aa64SRichard Purdie 	codec->reg_cache =
54840e0aa64SRichard Purdie 			kzalloc(sizeof(u16) * ARRAY_SIZE(wm8731_reg), GFP_KERNEL);
54940e0aa64SRichard Purdie 	if (codec->reg_cache == NULL)
55040e0aa64SRichard Purdie 		return -ENOMEM;
55140e0aa64SRichard Purdie 	memcpy(codec->reg_cache,
55240e0aa64SRichard Purdie 		wm8731_reg, sizeof(u16) * ARRAY_SIZE(wm8731_reg));
55340e0aa64SRichard Purdie 	codec->reg_cache_size = sizeof(u16) * ARRAY_SIZE(wm8731_reg);
55440e0aa64SRichard Purdie 
55540e0aa64SRichard Purdie 	wm8731_reset(codec);
55640e0aa64SRichard Purdie 
55740e0aa64SRichard Purdie 	/* register pcms */
55840e0aa64SRichard Purdie 	ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
55940e0aa64SRichard Purdie 	if (ret < 0) {
560e35115a5SLiam Girdwood 		printk(KERN_ERR "wm8731: failed to create pcms\n");
561e35115a5SLiam Girdwood 		goto pcm_err;
56240e0aa64SRichard Purdie 	}
56340e0aa64SRichard Purdie 
56440e0aa64SRichard Purdie 	/* power on device */
56540e0aa64SRichard Purdie 	wm8731_dapm_event(codec, SNDRV_CTL_POWER_D3hot);
56640e0aa64SRichard Purdie 
56740e0aa64SRichard Purdie 	/* set the update bits */
56840e0aa64SRichard Purdie 	reg = wm8731_read_reg_cache(codec, WM8731_LOUT1V);
56940e0aa64SRichard Purdie 	wm8731_write(codec, WM8731_LOUT1V, reg | 0x0100);
57040e0aa64SRichard Purdie 	reg = wm8731_read_reg_cache(codec, WM8731_ROUT1V);
57140e0aa64SRichard Purdie 	wm8731_write(codec, WM8731_ROUT1V, reg | 0x0100);
57240e0aa64SRichard Purdie 	reg = wm8731_read_reg_cache(codec, WM8731_LINVOL);
57340e0aa64SRichard Purdie 	wm8731_write(codec, WM8731_LINVOL, reg | 0x0100);
57440e0aa64SRichard Purdie 	reg = wm8731_read_reg_cache(codec, WM8731_RINVOL);
57540e0aa64SRichard Purdie 	wm8731_write(codec, WM8731_RINVOL, reg | 0x0100);
57640e0aa64SRichard Purdie 
57740e0aa64SRichard Purdie 	wm8731_add_controls(codec);
57840e0aa64SRichard Purdie 	wm8731_add_widgets(codec);
57940e0aa64SRichard Purdie 	ret = snd_soc_register_card(socdev);
58040e0aa64SRichard Purdie 	if (ret < 0) {
581e35115a5SLiam Girdwood 		printk(KERN_ERR "wm8731: failed to register card\n");
582e35115a5SLiam Girdwood 		goto card_err;
58340e0aa64SRichard Purdie 	}
58440e0aa64SRichard Purdie 
58540e0aa64SRichard Purdie 	return ret;
586e35115a5SLiam Girdwood 
587e35115a5SLiam Girdwood card_err:
588e35115a5SLiam Girdwood 	snd_soc_free_pcms(socdev);
589e35115a5SLiam Girdwood 	snd_soc_dapm_free(socdev);
590e35115a5SLiam Girdwood pcm_err:
591e35115a5SLiam Girdwood 	kfree(codec->reg_cache);
592e35115a5SLiam Girdwood 	return ret;
59340e0aa64SRichard Purdie }
59440e0aa64SRichard Purdie 
59540e0aa64SRichard Purdie static struct snd_soc_device *wm8731_socdev;
59640e0aa64SRichard Purdie 
59740e0aa64SRichard Purdie #if defined (CONFIG_I2C) || defined (CONFIG_I2C_MODULE)
59840e0aa64SRichard Purdie 
59940e0aa64SRichard Purdie /*
60040e0aa64SRichard Purdie  * WM8731 2 wire address is determined by GPIO5
60140e0aa64SRichard Purdie  * state during powerup.
60240e0aa64SRichard Purdie  *    low  = 0x1a
60340e0aa64SRichard Purdie  *    high = 0x1b
60440e0aa64SRichard Purdie  */
60540e0aa64SRichard Purdie static unsigned short normal_i2c[] = { 0, I2C_CLIENT_END };
60640e0aa64SRichard Purdie 
60740e0aa64SRichard Purdie /* Magic definition of all other variables and things */
60840e0aa64SRichard Purdie I2C_CLIENT_INSMOD;
60940e0aa64SRichard Purdie 
61040e0aa64SRichard Purdie static struct i2c_driver wm8731_i2c_driver;
61140e0aa64SRichard Purdie static struct i2c_client client_template;
61240e0aa64SRichard Purdie 
61340e0aa64SRichard Purdie /* If the i2c layer weren't so broken, we could pass this kind of data
61440e0aa64SRichard Purdie    around */
61540e0aa64SRichard Purdie 
61640e0aa64SRichard Purdie static int wm8731_codec_probe(struct i2c_adapter *adap, int addr, int kind)
61740e0aa64SRichard Purdie {
61840e0aa64SRichard Purdie 	struct snd_soc_device *socdev = wm8731_socdev;
61940e0aa64SRichard Purdie 	struct wm8731_setup_data *setup = socdev->codec_data;
62040e0aa64SRichard Purdie 	struct snd_soc_codec *codec = socdev->codec;
62140e0aa64SRichard Purdie 	struct i2c_client *i2c;
62240e0aa64SRichard Purdie 	int ret;
62340e0aa64SRichard Purdie 
62440e0aa64SRichard Purdie 	if (addr != setup->i2c_address)
62540e0aa64SRichard Purdie 		return -ENODEV;
62640e0aa64SRichard Purdie 
62740e0aa64SRichard Purdie 	client_template.adapter = adap;
62840e0aa64SRichard Purdie 	client_template.addr = addr;
62940e0aa64SRichard Purdie 
63040e0aa64SRichard Purdie 	i2c = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
63140e0aa64SRichard Purdie 	if (i2c == NULL) {
63240e0aa64SRichard Purdie 		kfree(codec);
63340e0aa64SRichard Purdie 		return -ENOMEM;
63440e0aa64SRichard Purdie 	}
63540e0aa64SRichard Purdie 	memcpy(i2c, &client_template, sizeof(struct i2c_client));
63640e0aa64SRichard Purdie 	i2c_set_clientdata(i2c, codec);
63740e0aa64SRichard Purdie 	codec->control_data = i2c;
63840e0aa64SRichard Purdie 
63940e0aa64SRichard Purdie 	ret = i2c_attach_client(i2c);
64040e0aa64SRichard Purdie 	if (ret < 0) {
64140e0aa64SRichard Purdie 		err("failed to attach codec at addr %x\n", addr);
64240e0aa64SRichard Purdie 		goto err;
64340e0aa64SRichard Purdie 	}
64440e0aa64SRichard Purdie 
64540e0aa64SRichard Purdie 	ret = wm8731_init(socdev);
64640e0aa64SRichard Purdie 	if (ret < 0) {
64740e0aa64SRichard Purdie 		err("failed to initialise WM8731\n");
64840e0aa64SRichard Purdie 		goto err;
64940e0aa64SRichard Purdie 	}
65040e0aa64SRichard Purdie 	return ret;
65140e0aa64SRichard Purdie 
65240e0aa64SRichard Purdie err:
65340e0aa64SRichard Purdie 	kfree(codec);
65440e0aa64SRichard Purdie 	kfree(i2c);
65540e0aa64SRichard Purdie 	return ret;
65640e0aa64SRichard Purdie }
65740e0aa64SRichard Purdie 
65840e0aa64SRichard Purdie static int wm8731_i2c_detach(struct i2c_client *client)
65940e0aa64SRichard Purdie {
66040e0aa64SRichard Purdie 	struct snd_soc_codec* codec = i2c_get_clientdata(client);
66140e0aa64SRichard Purdie 	i2c_detach_client(client);
66240e0aa64SRichard Purdie 	kfree(codec->reg_cache);
66340e0aa64SRichard Purdie 	kfree(client);
66440e0aa64SRichard Purdie 	return 0;
66540e0aa64SRichard Purdie }
66640e0aa64SRichard Purdie 
66740e0aa64SRichard Purdie static int wm8731_i2c_attach(struct i2c_adapter *adap)
66840e0aa64SRichard Purdie {
66940e0aa64SRichard Purdie 	return i2c_probe(adap, &addr_data, wm8731_codec_probe);
67040e0aa64SRichard Purdie }
67140e0aa64SRichard Purdie 
67240e0aa64SRichard Purdie /* corgi i2c codec control layer */
67340e0aa64SRichard Purdie static struct i2c_driver wm8731_i2c_driver = {
67440e0aa64SRichard Purdie 	.driver = {
67540e0aa64SRichard Purdie 		.name = "WM8731 I2C Codec",
67640e0aa64SRichard Purdie 		.owner = THIS_MODULE,
67740e0aa64SRichard Purdie 	},
67840e0aa64SRichard Purdie 	.id =             I2C_DRIVERID_WM8731,
67940e0aa64SRichard Purdie 	.attach_adapter = wm8731_i2c_attach,
68040e0aa64SRichard Purdie 	.detach_client =  wm8731_i2c_detach,
68140e0aa64SRichard Purdie 	.command =        NULL,
68240e0aa64SRichard Purdie };
68340e0aa64SRichard Purdie 
68440e0aa64SRichard Purdie static struct i2c_client client_template = {
68540e0aa64SRichard Purdie 	.name =   "WM8731",
68640e0aa64SRichard Purdie 	.driver = &wm8731_i2c_driver,
68740e0aa64SRichard Purdie };
68840e0aa64SRichard Purdie #endif
68940e0aa64SRichard Purdie 
69040e0aa64SRichard Purdie static int wm8731_probe(struct platform_device *pdev)
69140e0aa64SRichard Purdie {
69240e0aa64SRichard Purdie 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
69340e0aa64SRichard Purdie 	struct wm8731_setup_data *setup;
69440e0aa64SRichard Purdie 	struct snd_soc_codec *codec;
695*b36d61d4SFrank Mandarino 	struct wm8731_priv *wm8731;
69640e0aa64SRichard Purdie 	int ret = 0;
69740e0aa64SRichard Purdie 
69840e0aa64SRichard Purdie 	info("WM8731 Audio Codec %s", WM8731_VERSION);
69940e0aa64SRichard Purdie 
70040e0aa64SRichard Purdie 	setup = socdev->codec_data;
70140e0aa64SRichard Purdie 	codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
70240e0aa64SRichard Purdie 	if (codec == NULL)
70340e0aa64SRichard Purdie 		return -ENOMEM;
70440e0aa64SRichard Purdie 
705*b36d61d4SFrank Mandarino 	wm8731 = kzalloc(sizeof(struct wm8731_priv), GFP_KERNEL);
706*b36d61d4SFrank Mandarino 	if (wm8731 == NULL) {
707*b36d61d4SFrank Mandarino 		kfree(codec);
708*b36d61d4SFrank Mandarino 		return -ENOMEM;
709*b36d61d4SFrank Mandarino 	}
710*b36d61d4SFrank Mandarino 
711*b36d61d4SFrank Mandarino 	codec->private_data = wm8731;
71240e0aa64SRichard Purdie 	socdev->codec = codec;
71340e0aa64SRichard Purdie 	mutex_init(&codec->mutex);
71440e0aa64SRichard Purdie 	INIT_LIST_HEAD(&codec->dapm_widgets);
71540e0aa64SRichard Purdie 	INIT_LIST_HEAD(&codec->dapm_paths);
71640e0aa64SRichard Purdie 
71740e0aa64SRichard Purdie 	wm8731_socdev = socdev;
71840e0aa64SRichard Purdie #if defined (CONFIG_I2C) || defined (CONFIG_I2C_MODULE)
71940e0aa64SRichard Purdie 	if (setup->i2c_address) {
72040e0aa64SRichard Purdie 		normal_i2c[0] = setup->i2c_address;
72140e0aa64SRichard Purdie 		codec->hw_write = (hw_write_t)i2c_master_send;
72240e0aa64SRichard Purdie 		ret = i2c_add_driver(&wm8731_i2c_driver);
72340e0aa64SRichard Purdie 		if (ret != 0)
72440e0aa64SRichard Purdie 			printk(KERN_ERR "can't add i2c driver");
72540e0aa64SRichard Purdie 	}
72640e0aa64SRichard Purdie #else
72740e0aa64SRichard Purdie 	/* Add other interfaces here */
72840e0aa64SRichard Purdie #endif
72940e0aa64SRichard Purdie 	return ret;
73040e0aa64SRichard Purdie }
73140e0aa64SRichard Purdie 
73240e0aa64SRichard Purdie /* power down chip */
73340e0aa64SRichard Purdie static int wm8731_remove(struct platform_device *pdev)
73440e0aa64SRichard Purdie {
73540e0aa64SRichard Purdie 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
73640e0aa64SRichard Purdie 	struct snd_soc_codec *codec = socdev->codec;
73740e0aa64SRichard Purdie 
73840e0aa64SRichard Purdie 	if (codec->control_data)
73940e0aa64SRichard Purdie 		wm8731_dapm_event(codec, SNDRV_CTL_POWER_D3cold);
74040e0aa64SRichard Purdie 
74140e0aa64SRichard Purdie 	snd_soc_free_pcms(socdev);
74240e0aa64SRichard Purdie 	snd_soc_dapm_free(socdev);
74340e0aa64SRichard Purdie #if defined (CONFIG_I2C) || defined (CONFIG_I2C_MODULE)
74440e0aa64SRichard Purdie 	i2c_del_driver(&wm8731_i2c_driver);
74540e0aa64SRichard Purdie #endif
746*b36d61d4SFrank Mandarino 	kfree(codec->private_data);
74740e0aa64SRichard Purdie 	kfree(codec);
74840e0aa64SRichard Purdie 
74940e0aa64SRichard Purdie 	return 0;
75040e0aa64SRichard Purdie }
75140e0aa64SRichard Purdie 
75240e0aa64SRichard Purdie struct snd_soc_codec_device soc_codec_dev_wm8731 = {
75340e0aa64SRichard Purdie 	.probe = 	wm8731_probe,
75440e0aa64SRichard Purdie 	.remove = 	wm8731_remove,
75540e0aa64SRichard Purdie 	.suspend = 	wm8731_suspend,
75640e0aa64SRichard Purdie 	.resume =	wm8731_resume,
75740e0aa64SRichard Purdie };
75840e0aa64SRichard Purdie 
75940e0aa64SRichard Purdie EXPORT_SYMBOL_GPL(soc_codec_dev_wm8731);
76040e0aa64SRichard Purdie 
76140e0aa64SRichard Purdie MODULE_DESCRIPTION("ASoC WM8731 driver");
76240e0aa64SRichard Purdie MODULE_AUTHOR("Richard Purdie");
76340e0aa64SRichard Purdie MODULE_LICENSE("GPL");
764