xref: /openbmc/linux/sound/soc/codecs/wm9705.c (revision d7cabb08)
12aceefefSIan Molton /*
22aceefefSIan Molton  * wm9705.c  --  ALSA Soc WM9705 codec support
32aceefefSIan Molton  *
42aceefefSIan Molton  * Copyright 2008 Ian Molton <spyro@f2s.com>
52aceefefSIan Molton  *
62aceefefSIan Molton  *  This program is free software; you can redistribute  it and/or modify it
72aceefefSIan Molton  *  under  the terms of  the GNU General  Public License as published by the
82aceefefSIan Molton  *  Free Software Foundation; Version 2 of the  License only.
92aceefefSIan Molton  *
102aceefefSIan Molton  */
112aceefefSIan Molton 
122aceefefSIan Molton #include <linux/init.h>
135a0e3ad6STejun Heo #include <linux/slab.h>
142aceefefSIan Molton #include <linux/module.h>
152aceefefSIan Molton #include <linux/kernel.h>
162aceefefSIan Molton #include <linux/device.h>
172aceefefSIan Molton #include <sound/core.h>
182aceefefSIan Molton #include <sound/pcm.h>
192aceefefSIan Molton #include <sound/ac97_codec.h>
202aceefefSIan Molton #include <sound/initval.h>
212aceefefSIan Molton #include <sound/soc.h>
222aceefefSIan Molton 
2301e097d6SMark Brown #include "wm9705.h"
2401e097d6SMark Brown 
252aceefefSIan Molton /*
262aceefefSIan Molton  * WM9705 register cache
272aceefefSIan Molton  */
282aceefefSIan Molton static const u16 wm9705_reg[] = {
292aceefefSIan Molton 	0x6150, 0x8000, 0x8000, 0x8000, /* 0x0  */
302aceefefSIan Molton 	0x0000, 0x8000, 0x8008, 0x8008, /* 0x8  */
312aceefefSIan Molton 	0x8808, 0x8808, 0x8808, 0x8808, /* 0x10 */
322aceefefSIan Molton 	0x8808, 0x0000, 0x8000, 0x0000, /* 0x18 */
332aceefefSIan Molton 	0x0000, 0x0000, 0x0000, 0x000f, /* 0x20 */
342aceefefSIan Molton 	0x0605, 0x0000, 0xbb80, 0x0000, /* 0x28 */
352aceefefSIan Molton 	0x0000, 0xbb80, 0x0000, 0x0000, /* 0x30 */
362aceefefSIan Molton 	0x0000, 0x2000, 0x0000, 0x0000, /* 0x38 */
372aceefefSIan Molton 	0x0000, 0x0000, 0x0000, 0x0000, /* 0x40 */
382aceefefSIan Molton 	0x0000, 0x0000, 0x0000, 0x0000, /* 0x48 */
392aceefefSIan Molton 	0x0000, 0x0000, 0x0000, 0x0000, /* 0x50 */
402aceefefSIan Molton 	0x0000, 0x0000, 0x0000, 0x0000, /* 0x58 */
412aceefefSIan Molton 	0x0000, 0x0000, 0x0000, 0x0000, /* 0x60 */
422aceefefSIan Molton 	0x0000, 0x0000, 0x0000, 0x0000, /* 0x68 */
432aceefefSIan Molton 	0x0000, 0x0808, 0x0000, 0x0006, /* 0x70 */
442aceefefSIan Molton 	0x0000, 0x0000, 0x574d, 0x4c05, /* 0x78 */
452aceefefSIan Molton };
462aceefefSIan Molton 
472aceefefSIan Molton static const struct snd_kcontrol_new wm9705_snd_ac97_controls[] = {
482aceefefSIan Molton 	SOC_DOUBLE("Master Playback Volume", AC97_MASTER, 8, 0, 31, 1),
492aceefefSIan Molton 	SOC_SINGLE("Master Playback Switch", AC97_MASTER, 15, 1, 1),
502aceefefSIan Molton 	SOC_DOUBLE("Headphone Playback Volume", AC97_HEADPHONE, 8, 0, 31, 1),
512aceefefSIan Molton 	SOC_SINGLE("Headphone Playback Switch", AC97_HEADPHONE, 15, 1, 1),
522aceefefSIan Molton 	SOC_DOUBLE("PCM Playback Volume", AC97_PCM, 8, 0, 31, 1),
532aceefefSIan Molton 	SOC_SINGLE("PCM Playback Switch", AC97_PCM, 15, 1, 1),
542aceefefSIan Molton 	SOC_SINGLE("Mono Playback Volume", AC97_MASTER_MONO, 0, 31, 1),
552aceefefSIan Molton 	SOC_SINGLE("Mono Playback Switch", AC97_MASTER_MONO, 15, 1, 1),
562aceefefSIan Molton 	SOC_SINGLE("PCBeep Playback Volume", AC97_PC_BEEP, 1, 15, 1),
572aceefefSIan Molton 	SOC_SINGLE("Phone Playback Volume", AC97_PHONE, 0, 31, 1),
582aceefefSIan Molton 	SOC_DOUBLE("Line Playback Volume", AC97_LINE, 8, 0, 31, 1),
592aceefefSIan Molton 	SOC_DOUBLE("CD Playback Volume", AC97_CD, 8, 0, 31, 1),
602aceefefSIan Molton 	SOC_SINGLE("Mic Playback Volume", AC97_MIC, 0, 31, 1),
612aceefefSIan Molton 	SOC_SINGLE("Mic 20dB Boost Switch", AC97_MIC, 6, 1, 0),
62927b0aeaSIan Molton 	SOC_DOUBLE("Capture Volume", AC97_REC_GAIN, 8, 0, 15, 0),
63927b0aeaSIan Molton 	SOC_SINGLE("Capture Switch", AC97_REC_GAIN, 15, 1, 1),
642aceefefSIan Molton };
652aceefefSIan Molton 
662aceefefSIan Molton static const char *wm9705_mic[] = {"Mic 1", "Mic 2"};
672aceefefSIan Molton static const char *wm9705_rec_sel[] = {"Mic", "CD", "NC", "NC",
682aceefefSIan Molton 	"Line", "Stereo Mix", "Mono Mix", "Phone"};
692aceefefSIan Molton 
7009981dcbSTakashi Iwai static SOC_ENUM_SINGLE_DECL(wm9705_enum_mic,
7109981dcbSTakashi Iwai 			    AC97_GENERAL_PURPOSE, 8, wm9705_mic);
7209981dcbSTakashi Iwai static SOC_ENUM_SINGLE_DECL(wm9705_enum_rec_l,
7309981dcbSTakashi Iwai 			    AC97_REC_SEL, 8, wm9705_rec_sel);
7409981dcbSTakashi Iwai static SOC_ENUM_SINGLE_DECL(wm9705_enum_rec_r,
7509981dcbSTakashi Iwai 			    AC97_REC_SEL, 0, wm9705_rec_sel);
762aceefefSIan Molton 
772aceefefSIan Molton /* Headphone Mixer */
782aceefefSIan Molton static const struct snd_kcontrol_new wm9705_hp_mixer_controls[] = {
792aceefefSIan Molton 	SOC_DAPM_SINGLE("PCBeep Playback Switch", AC97_PC_BEEP, 15, 1, 1),
802aceefefSIan Molton 	SOC_DAPM_SINGLE("CD Playback Switch", AC97_CD, 15, 1, 1),
812aceefefSIan Molton 	SOC_DAPM_SINGLE("Mic Playback Switch", AC97_MIC, 15, 1, 1),
822aceefefSIan Molton 	SOC_DAPM_SINGLE("Phone Playback Switch", AC97_PHONE, 15, 1, 1),
832aceefefSIan Molton 	SOC_DAPM_SINGLE("Line Playback Switch", AC97_LINE, 15, 1, 1),
842aceefefSIan Molton };
852aceefefSIan Molton 
862aceefefSIan Molton /* Mic source */
872aceefefSIan Molton static const struct snd_kcontrol_new wm9705_mic_src_controls =
882aceefefSIan Molton 	SOC_DAPM_ENUM("Route", wm9705_enum_mic);
892aceefefSIan Molton 
902aceefefSIan Molton /* Capture source */
912aceefefSIan Molton static const struct snd_kcontrol_new wm9705_capture_selectl_controls =
922aceefefSIan Molton 	SOC_DAPM_ENUM("Route", wm9705_enum_rec_l);
932aceefefSIan Molton static const struct snd_kcontrol_new wm9705_capture_selectr_controls =
942aceefefSIan Molton 	SOC_DAPM_ENUM("Route", wm9705_enum_rec_r);
952aceefefSIan Molton 
962aceefefSIan Molton /* DAPM widgets */
972aceefefSIan Molton static const struct snd_soc_dapm_widget wm9705_dapm_widgets[] = {
982aceefefSIan Molton 	SND_SOC_DAPM_MUX("Mic Source", SND_SOC_NOPM, 0, 0,
992aceefefSIan Molton 		&wm9705_mic_src_controls),
1002aceefefSIan Molton 	SND_SOC_DAPM_MUX("Left Capture Source", SND_SOC_NOPM, 0, 0,
1012aceefefSIan Molton 		&wm9705_capture_selectl_controls),
1022aceefefSIan Molton 	SND_SOC_DAPM_MUX("Right Capture Source", SND_SOC_NOPM, 0, 0,
1032aceefefSIan Molton 		&wm9705_capture_selectr_controls),
1042aceefefSIan Molton 	SND_SOC_DAPM_DAC("Left DAC", "Left HiFi Playback",
1052aceefefSIan Molton 		SND_SOC_NOPM, 0, 0),
1062aceefefSIan Molton 	SND_SOC_DAPM_DAC("Right DAC", "Right HiFi Playback",
1072aceefefSIan Molton 		SND_SOC_NOPM, 0, 0),
1082aceefefSIan Molton 	SND_SOC_DAPM_MIXER_NAMED_CTL("HP Mixer", SND_SOC_NOPM, 0, 0,
1092aceefefSIan Molton 		&wm9705_hp_mixer_controls[0],
1102aceefefSIan Molton 		ARRAY_SIZE(wm9705_hp_mixer_controls)),
1112aceefefSIan Molton 	SND_SOC_DAPM_MIXER("Mono Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
1122aceefefSIan Molton 	SND_SOC_DAPM_ADC("Left ADC", "Left HiFi Capture", SND_SOC_NOPM, 0, 0),
1132aceefefSIan Molton 	SND_SOC_DAPM_ADC("Right ADC", "Right HiFi Capture", SND_SOC_NOPM, 0, 0),
1142aceefefSIan Molton 	SND_SOC_DAPM_PGA("Headphone PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
1152aceefefSIan Molton 	SND_SOC_DAPM_PGA("Speaker PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
1162aceefefSIan Molton 	SND_SOC_DAPM_PGA("Line PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
1172aceefefSIan Molton 	SND_SOC_DAPM_PGA("Line out PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
1182aceefefSIan Molton 	SND_SOC_DAPM_PGA("Mono PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
1192aceefefSIan Molton 	SND_SOC_DAPM_PGA("Phone PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
1202aceefefSIan Molton 	SND_SOC_DAPM_PGA("Mic PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
1212aceefefSIan Molton 	SND_SOC_DAPM_PGA("PCBEEP PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
1222aceefefSIan Molton 	SND_SOC_DAPM_PGA("CD PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
1232aceefefSIan Molton 	SND_SOC_DAPM_PGA("ADC PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
1242aceefefSIan Molton 	SND_SOC_DAPM_OUTPUT("HPOUTL"),
1252aceefefSIan Molton 	SND_SOC_DAPM_OUTPUT("HPOUTR"),
1262aceefefSIan Molton 	SND_SOC_DAPM_OUTPUT("LOUT"),
1272aceefefSIan Molton 	SND_SOC_DAPM_OUTPUT("ROUT"),
1282aceefefSIan Molton 	SND_SOC_DAPM_OUTPUT("MONOOUT"),
1292aceefefSIan Molton 	SND_SOC_DAPM_INPUT("PHONE"),
1302aceefefSIan Molton 	SND_SOC_DAPM_INPUT("LINEINL"),
1312aceefefSIan Molton 	SND_SOC_DAPM_INPUT("LINEINR"),
1322aceefefSIan Molton 	SND_SOC_DAPM_INPUT("CDINL"),
1332aceefefSIan Molton 	SND_SOC_DAPM_INPUT("CDINR"),
1342aceefefSIan Molton 	SND_SOC_DAPM_INPUT("PCBEEP"),
1352aceefefSIan Molton 	SND_SOC_DAPM_INPUT("MIC1"),
1362aceefefSIan Molton 	SND_SOC_DAPM_INPUT("MIC2"),
1372aceefefSIan Molton };
1382aceefefSIan Molton 
1392aceefefSIan Molton /* Audio map
1402aceefefSIan Molton  * WM9705 has no switches to disable the route from the inputs to the HP mixer
1412aceefefSIan Molton  * so in order to prevent active inputs from forcing the audio outputs to be
1422aceefefSIan Molton  * constantly enabled, we use the mutes on those inputs to simulate such
1432aceefefSIan Molton  * controls.
1442aceefefSIan Molton  */
14597a58d6eSLu Guanqun static const struct snd_soc_dapm_route wm9705_audio_map[] = {
1462aceefefSIan Molton 	/* HP mixer */
1472aceefefSIan Molton 	{"HP Mixer", "PCBeep Playback Switch", "PCBEEP PGA"},
1482aceefefSIan Molton 	{"HP Mixer", "CD Playback Switch", "CD PGA"},
1492aceefefSIan Molton 	{"HP Mixer", "Mic Playback Switch", "Mic PGA"},
1502aceefefSIan Molton 	{"HP Mixer", "Phone Playback Switch", "Phone PGA"},
1512aceefefSIan Molton 	{"HP Mixer", "Line Playback Switch", "Line PGA"},
1522aceefefSIan Molton 	{"HP Mixer", NULL, "Left DAC"},
1532aceefefSIan Molton 	{"HP Mixer", NULL, "Right DAC"},
1542aceefefSIan Molton 
1552aceefefSIan Molton 	/* mono mixer */
1562aceefefSIan Molton 	{"Mono Mixer", NULL, "HP Mixer"},
1572aceefefSIan Molton 
1582aceefefSIan Molton 	/* outputs */
1592aceefefSIan Molton 	{"Headphone PGA", NULL, "HP Mixer"},
1602aceefefSIan Molton 	{"HPOUTL", NULL, "Headphone PGA"},
1612aceefefSIan Molton 	{"HPOUTR", NULL, "Headphone PGA"},
1622aceefefSIan Molton 	{"Line out PGA", NULL, "HP Mixer"},
1632aceefefSIan Molton 	{"LOUT", NULL, "Line out PGA"},
1642aceefefSIan Molton 	{"ROUT", NULL, "Line out PGA"},
1652aceefefSIan Molton 	{"Mono PGA", NULL, "Mono Mixer"},
1662aceefefSIan Molton 	{"MONOOUT", NULL, "Mono PGA"},
1672aceefefSIan Molton 
1682aceefefSIan Molton 	/* inputs */
1692aceefefSIan Molton 	{"CD PGA", NULL, "CDINL"},
1702aceefefSIan Molton 	{"CD PGA", NULL, "CDINR"},
1712aceefefSIan Molton 	{"Line PGA", NULL, "LINEINL"},
1722aceefefSIan Molton 	{"Line PGA", NULL, "LINEINR"},
1732aceefefSIan Molton 	{"Phone PGA", NULL, "PHONE"},
1742aceefefSIan Molton 	{"Mic Source", "Mic 1", "MIC1"},
1752aceefefSIan Molton 	{"Mic Source", "Mic 2", "MIC2"},
1762aceefefSIan Molton 	{"Mic PGA", NULL, "Mic Source"},
1772aceefefSIan Molton 	{"PCBEEP PGA", NULL, "PCBEEP"},
1782aceefefSIan Molton 
1792aceefefSIan Molton 	/* Left capture selector */
1802aceefefSIan Molton 	{"Left Capture Source", "Mic", "Mic Source"},
1812aceefefSIan Molton 	{"Left Capture Source", "CD", "CDINL"},
1822aceefefSIan Molton 	{"Left Capture Source", "Line", "LINEINL"},
1832aceefefSIan Molton 	{"Left Capture Source", "Stereo Mix", "HP Mixer"},
1842aceefefSIan Molton 	{"Left Capture Source", "Mono Mix", "HP Mixer"},
1852aceefefSIan Molton 	{"Left Capture Source", "Phone", "PHONE"},
1862aceefefSIan Molton 
1872aceefefSIan Molton 	/* Right capture source */
1882aceefefSIan Molton 	{"Right Capture Source", "Mic", "Mic Source"},
1892aceefefSIan Molton 	{"Right Capture Source", "CD", "CDINR"},
1902aceefefSIan Molton 	{"Right Capture Source", "Line", "LINEINR"},
1912aceefefSIan Molton 	{"Right Capture Source", "Stereo Mix", "HP Mixer"},
1922aceefefSIan Molton 	{"Right Capture Source", "Mono Mix", "HP Mixer"},
1932aceefefSIan Molton 	{"Right Capture Source", "Phone", "PHONE"},
1942aceefefSIan Molton 
1952aceefefSIan Molton 	{"ADC PGA", NULL, "Left Capture Source"},
1962aceefefSIan Molton 	{"ADC PGA", NULL, "Right Capture Source"},
1972aceefefSIan Molton 
1982aceefefSIan Molton 	/* ADC's */
1992aceefefSIan Molton 	{"Left ADC",  NULL, "ADC PGA"},
2002aceefefSIan Molton 	{"Right ADC", NULL, "ADC PGA"},
2012aceefefSIan Molton };
2022aceefefSIan Molton 
2032aceefefSIan Molton /* We use a register cache to enhance read performance. */
2042aceefefSIan Molton static unsigned int ac97_read(struct snd_soc_codec *codec, unsigned int reg)
2052aceefefSIan Molton {
2062aceefefSIan Molton 	u16 *cache = codec->reg_cache;
2072aceefefSIan Molton 
2082aceefefSIan Molton 	switch (reg) {
2092aceefefSIan Molton 	case AC97_RESET:
2102aceefefSIan Molton 	case AC97_VENDOR_ID1:
2112aceefefSIan Molton 	case AC97_VENDOR_ID2:
212b047e1ccSMark Brown 		return soc_ac97_ops->read(codec->ac97, reg);
2132aceefefSIan Molton 	default:
2142aceefefSIan Molton 		reg = reg >> 1;
2152aceefefSIan Molton 
2162aceefefSIan Molton 		if (reg >= (ARRAY_SIZE(wm9705_reg)))
2172aceefefSIan Molton 			return -EIO;
2182aceefefSIan Molton 
2192aceefefSIan Molton 		return cache[reg];
2202aceefefSIan Molton 	}
2212aceefefSIan Molton }
2222aceefefSIan Molton 
2232aceefefSIan Molton static int ac97_write(struct snd_soc_codec *codec, unsigned int reg,
2242aceefefSIan Molton 	unsigned int val)
2252aceefefSIan Molton {
2262aceefefSIan Molton 	u16 *cache = codec->reg_cache;
2272aceefefSIan Molton 
228b047e1ccSMark Brown 	soc_ac97_ops->write(codec->ac97, reg, val);
2292aceefefSIan Molton 	reg = reg >> 1;
2302aceefefSIan Molton 	if (reg < (ARRAY_SIZE(wm9705_reg)))
2312aceefefSIan Molton 		cache[reg] = val;
2322aceefefSIan Molton 
2332aceefefSIan Molton 	return 0;
2342aceefefSIan Molton }
2352aceefefSIan Molton 
2362aceefefSIan Molton static int ac97_prepare(struct snd_pcm_substream *substream,
2372aceefefSIan Molton 			struct snd_soc_dai *dai)
2382aceefefSIan Molton {
239e6968a17SMark Brown 	struct snd_soc_codec *codec = dai->codec;
2402aceefefSIan Molton 	int reg;
2412aceefefSIan Molton 	u16 vra;
2422aceefefSIan Molton 
2432aceefefSIan Molton 	vra = ac97_read(codec, AC97_EXTENDED_STATUS);
2442aceefefSIan Molton 	ac97_write(codec, AC97_EXTENDED_STATUS, vra | 0x1);
2452aceefefSIan Molton 
2462aceefefSIan Molton 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2472aceefefSIan Molton 		reg = AC97_PCM_FRONT_DAC_RATE;
2482aceefefSIan Molton 	else
2492aceefefSIan Molton 		reg = AC97_PCM_LR_ADC_RATE;
2502aceefefSIan Molton 
251019ec505SFabio Estevam 	return ac97_write(codec, reg, substream->runtime->rate);
2522aceefefSIan Molton }
2532aceefefSIan Molton 
2542aceefefSIan Molton #define WM9705_AC97_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 | \
2552aceefefSIan Molton 			SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 | \
2562aceefefSIan Molton 			SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
2572aceefefSIan Molton 			SNDRV_PCM_RATE_48000)
2582aceefefSIan Molton 
25985e7652dSLars-Peter Clausen static const struct snd_soc_dai_ops wm9705_dai_ops = {
2606335d055SEric Miao 	.prepare	= ac97_prepare,
2616335d055SEric Miao };
2626335d055SEric Miao 
263f0fba2adSLiam Girdwood static struct snd_soc_dai_driver wm9705_dai[] = {
2642aceefefSIan Molton 	{
265f0fba2adSLiam Girdwood 		.name = "wm9705-hifi",
2662aceefefSIan Molton 		.ac97_control = 1,
2672aceefefSIan Molton 		.playback = {
2682aceefefSIan Molton 			.stream_name = "HiFi Playback",
2692aceefefSIan Molton 			.channels_min = 1,
2702aceefefSIan Molton 			.channels_max = 2,
2712aceefefSIan Molton 			.rates = WM9705_AC97_RATES,
27233f503c9SMark Brown 			.formats = SND_SOC_STD_AC97_FMTS,
2732aceefefSIan Molton 		},
2742aceefefSIan Molton 		.capture = {
2752aceefefSIan Molton 			.stream_name = "HiFi Capture",
2762aceefefSIan Molton 			.channels_min = 1,
2772aceefefSIan Molton 			.channels_max = 2,
2782aceefefSIan Molton 			.rates = WM9705_AC97_RATES,
27933f503c9SMark Brown 			.formats = SND_SOC_STD_AC97_FMTS,
2802aceefefSIan Molton 		},
2816335d055SEric Miao 		.ops = &wm9705_dai_ops,
2822aceefefSIan Molton 	},
2832aceefefSIan Molton 	{
284f0fba2adSLiam Girdwood 		.name = "wm9705-aux",
2852aceefefSIan Molton 		.playback = {
2862aceefefSIan Molton 			.stream_name = "Aux Playback",
2872aceefefSIan Molton 			.channels_min = 1,
2882aceefefSIan Molton 			.channels_max = 1,
2892aceefefSIan Molton 			.rates = WM9705_AC97_RATES,
2902aceefefSIan Molton 			.formats = SNDRV_PCM_FMTBIT_S16_LE,
2912aceefefSIan Molton 		},
2922aceefefSIan Molton 	}
2932aceefefSIan Molton };
2942aceefefSIan Molton 
2952aceefefSIan Molton static int wm9705_reset(struct snd_soc_codec *codec)
2962aceefefSIan Molton {
297b047e1ccSMark Brown 	if (soc_ac97_ops->reset) {
298b047e1ccSMark Brown 		soc_ac97_ops->reset(codec->ac97);
2992aceefefSIan Molton 		if (ac97_read(codec, 0) == wm9705_reg[0])
3002aceefefSIan Molton 			return 0; /* Success */
3012aceefefSIan Molton 	}
3022aceefefSIan Molton 
3039cf766f6SLars-Peter Clausen 	dev_err(codec->dev, "Failed to reset: AC97 link error\n");
3049cf766f6SLars-Peter Clausen 
3052aceefefSIan Molton 	return -EIO;
3062aceefefSIan Molton }
3072aceefefSIan Molton 
3080a11b168SMark Brown #ifdef CONFIG_PM
30984b315eeSLars-Peter Clausen static int wm9705_soc_suspend(struct snd_soc_codec *codec)
3100a11b168SMark Brown {
311b047e1ccSMark Brown 	soc_ac97_ops->write(codec->ac97, AC97_POWERDOWN, 0xffff);
3120a11b168SMark Brown 
3130a11b168SMark Brown 	return 0;
3140a11b168SMark Brown }
3150a11b168SMark Brown 
316f0fba2adSLiam Girdwood static int wm9705_soc_resume(struct snd_soc_codec *codec)
3170a11b168SMark Brown {
3180a11b168SMark Brown 	int i, ret;
3190a11b168SMark Brown 	u16 *cache = codec->reg_cache;
3200a11b168SMark Brown 
3210a11b168SMark Brown 	ret = wm9705_reset(codec);
3229cf766f6SLars-Peter Clausen 	if (ret < 0)
3230a11b168SMark Brown 		return ret;
3240a11b168SMark Brown 
3250a11b168SMark Brown 	for (i = 2; i < ARRAY_SIZE(wm9705_reg) << 1; i += 2) {
326b047e1ccSMark Brown 		soc_ac97_ops->write(codec->ac97, i, cache[i>>1]);
3270a11b168SMark Brown 	}
3280a11b168SMark Brown 
3290a11b168SMark Brown 	return 0;
3300a11b168SMark Brown }
3310a11b168SMark Brown #else
3320a11b168SMark Brown #define wm9705_soc_suspend NULL
3330a11b168SMark Brown #define wm9705_soc_resume NULL
3340a11b168SMark Brown #endif
3350a11b168SMark Brown 
336f0fba2adSLiam Girdwood static int wm9705_soc_probe(struct snd_soc_codec *codec)
3372aceefefSIan Molton {
3382aceefefSIan Molton 	int ret = 0;
3392aceefefSIan Molton 
340b047e1ccSMark Brown 	ret = snd_soc_new_ac97_codec(codec, soc_ac97_ops, 0);
3412aceefefSIan Molton 	if (ret < 0) {
3429cf766f6SLars-Peter Clausen 		dev_err(codec->dev, "Failed to register AC97 codec\n");
343f0fba2adSLiam Girdwood 		return ret;
3442aceefefSIan Molton 	}
3452aceefefSIan Molton 
3462aceefefSIan Molton 	ret = wm9705_reset(codec);
3472aceefefSIan Molton 	if (ret)
3482aceefefSIan Molton 		goto reset_err;
3492aceefefSIan Molton 
3502aceefefSIan Molton 	return 0;
3512aceefefSIan Molton 
352a22eaf4cSTakashi Iwai reset_err:
3532aceefefSIan Molton 	snd_soc_free_ac97_codec(codec);
3542aceefefSIan Molton 	return ret;
3552aceefefSIan Molton }
3562aceefefSIan Molton 
357f0fba2adSLiam Girdwood static int wm9705_soc_remove(struct snd_soc_codec *codec)
3582aceefefSIan Molton {
3592aceefefSIan Molton 	snd_soc_free_ac97_codec(codec);
3602aceefefSIan Molton 	return 0;
3612aceefefSIan Molton }
3622aceefefSIan Molton 
363f0fba2adSLiam Girdwood static struct snd_soc_codec_driver soc_codec_dev_wm9705 = {
3642aceefefSIan Molton 	.probe = 	wm9705_soc_probe,
3652aceefefSIan Molton 	.remove = 	wm9705_soc_remove,
3660a11b168SMark Brown 	.suspend =	wm9705_soc_suspend,
3670a11b168SMark Brown 	.resume =	wm9705_soc_resume,
368f0fba2adSLiam Girdwood 	.read = ac97_read,
369f0fba2adSLiam Girdwood 	.write = ac97_write,
370e5eec34cSDimitris Papastamos 	.reg_cache_size = ARRAY_SIZE(wm9705_reg),
371f0fba2adSLiam Girdwood 	.reg_word_size = sizeof(u16),
372f0fba2adSLiam Girdwood 	.reg_cache_step = 2,
373f0fba2adSLiam Girdwood 	.reg_cache_default = wm9705_reg,
374d7cabb08SLars-Peter Clausen 
375d7cabb08SLars-Peter Clausen 	.controls = wm9705_snd_ac97_controls,
376d7cabb08SLars-Peter Clausen 	.num_controls = ARRAY_SIZE(wm9705_snd_ac97_controls),
37797a58d6eSLu Guanqun 	.dapm_widgets = wm9705_dapm_widgets,
37897a58d6eSLu Guanqun 	.num_dapm_widgets = ARRAY_SIZE(wm9705_dapm_widgets),
37997a58d6eSLu Guanqun 	.dapm_routes = wm9705_audio_map,
38097a58d6eSLu Guanqun 	.num_dapm_routes = ARRAY_SIZE(wm9705_audio_map),
3812aceefefSIan Molton };
382f0fba2adSLiam Girdwood 
3837a79e94eSBill Pemberton static int wm9705_probe(struct platform_device *pdev)
384f0fba2adSLiam Girdwood {
385f0fba2adSLiam Girdwood 	return snd_soc_register_codec(&pdev->dev,
386f0fba2adSLiam Girdwood 			&soc_codec_dev_wm9705, wm9705_dai, ARRAY_SIZE(wm9705_dai));
387f0fba2adSLiam Girdwood }
388f0fba2adSLiam Girdwood 
3897a79e94eSBill Pemberton static int wm9705_remove(struct platform_device *pdev)
390f0fba2adSLiam Girdwood {
391f0fba2adSLiam Girdwood 	snd_soc_unregister_codec(&pdev->dev);
392f0fba2adSLiam Girdwood 	return 0;
393f0fba2adSLiam Girdwood }
394f0fba2adSLiam Girdwood 
395f0fba2adSLiam Girdwood static struct platform_driver wm9705_codec_driver = {
396f0fba2adSLiam Girdwood 	.driver = {
397f0fba2adSLiam Girdwood 			.name = "wm9705-codec",
398f0fba2adSLiam Girdwood 			.owner = THIS_MODULE,
399f0fba2adSLiam Girdwood 	},
400f0fba2adSLiam Girdwood 
401f0fba2adSLiam Girdwood 	.probe = wm9705_probe,
4027a79e94eSBill Pemberton 	.remove = wm9705_remove,
403f0fba2adSLiam Girdwood };
404f0fba2adSLiam Girdwood 
4055bbcc3c0SMark Brown module_platform_driver(wm9705_codec_driver);
4062aceefefSIan Molton 
4072aceefefSIan Molton MODULE_DESCRIPTION("ASoC WM9705 driver");
4082aceefefSIan Molton MODULE_AUTHOR("Ian Molton");
4092aceefefSIan Molton MODULE_LICENSE("GPL v2");
410