xref: /openbmc/linux/sound/soc/codecs/wm9705.c (revision a22eaf4c)
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>
132aceefefSIan Molton #include <linux/module.h>
142aceefefSIan Molton #include <linux/kernel.h>
152aceefefSIan Molton #include <linux/device.h>
162aceefefSIan Molton #include <sound/core.h>
172aceefefSIan Molton #include <sound/pcm.h>
182aceefefSIan Molton #include <sound/ac97_codec.h>
192aceefefSIan Molton #include <sound/initval.h>
202aceefefSIan Molton #include <sound/soc.h>
212aceefefSIan Molton #include <sound/soc-dapm.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 
702aceefefSIan Molton static const struct soc_enum wm9705_enum_mic =
712aceefefSIan Molton 	SOC_ENUM_SINGLE(AC97_GENERAL_PURPOSE, 8, 2, wm9705_mic);
722aceefefSIan Molton static const struct soc_enum wm9705_enum_rec_l =
732aceefefSIan Molton 	SOC_ENUM_SINGLE(AC97_REC_SEL, 8, 8, wm9705_rec_sel);
742aceefefSIan Molton static const struct soc_enum wm9705_enum_rec_r =
752aceefefSIan Molton 	SOC_ENUM_SINGLE(AC97_REC_SEL, 0, 8, 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  */
1452aceefefSIan Molton static const struct snd_soc_dapm_route 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 static int wm9705_add_widgets(struct snd_soc_codec *codec)
2042aceefefSIan Molton {
2052aceefefSIan Molton 	snd_soc_dapm_new_controls(codec, wm9705_dapm_widgets,
2062aceefefSIan Molton 					ARRAY_SIZE(wm9705_dapm_widgets));
2072aceefefSIan Molton 	snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
2082aceefefSIan Molton 
2092aceefefSIan Molton 	return 0;
2102aceefefSIan Molton }
2112aceefefSIan Molton 
2122aceefefSIan Molton /* We use a register cache to enhance read performance. */
2132aceefefSIan Molton static unsigned int ac97_read(struct snd_soc_codec *codec, unsigned int reg)
2142aceefefSIan Molton {
2152aceefefSIan Molton 	u16 *cache = codec->reg_cache;
2162aceefefSIan Molton 
2172aceefefSIan Molton 	switch (reg) {
2182aceefefSIan Molton 	case AC97_RESET:
2192aceefefSIan Molton 	case AC97_VENDOR_ID1:
2202aceefefSIan Molton 	case AC97_VENDOR_ID2:
2212aceefefSIan Molton 		return soc_ac97_ops.read(codec->ac97, reg);
2222aceefefSIan Molton 	default:
2232aceefefSIan Molton 		reg = reg >> 1;
2242aceefefSIan Molton 
2252aceefefSIan Molton 		if (reg >= (ARRAY_SIZE(wm9705_reg)))
2262aceefefSIan Molton 			return -EIO;
2272aceefefSIan Molton 
2282aceefefSIan Molton 		return cache[reg];
2292aceefefSIan Molton 	}
2302aceefefSIan Molton }
2312aceefefSIan Molton 
2322aceefefSIan Molton static int ac97_write(struct snd_soc_codec *codec, unsigned int reg,
2332aceefefSIan Molton 	unsigned int val)
2342aceefefSIan Molton {
2352aceefefSIan Molton 	u16 *cache = codec->reg_cache;
2362aceefefSIan Molton 
2372aceefefSIan Molton 	soc_ac97_ops.write(codec->ac97, reg, val);
2382aceefefSIan Molton 	reg = reg >> 1;
2392aceefefSIan Molton 	if (reg < (ARRAY_SIZE(wm9705_reg)))
2402aceefefSIan Molton 		cache[reg] = val;
2412aceefefSIan Molton 
2422aceefefSIan Molton 	return 0;
2432aceefefSIan Molton }
2442aceefefSIan Molton 
2452aceefefSIan Molton static int ac97_prepare(struct snd_pcm_substream *substream,
2462aceefefSIan Molton 			struct snd_soc_dai *dai)
2472aceefefSIan Molton {
2482aceefefSIan Molton 	struct snd_pcm_runtime *runtime = substream->runtime;
2492aceefefSIan Molton 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
2502aceefefSIan Molton 	struct snd_soc_device *socdev = rtd->socdev;
2516627a653SMark Brown 	struct snd_soc_codec *codec = socdev->card->codec;
2522aceefefSIan Molton 	int reg;
2532aceefefSIan Molton 	u16 vra;
2542aceefefSIan Molton 
2552aceefefSIan Molton 	vra = ac97_read(codec, AC97_EXTENDED_STATUS);
2562aceefefSIan Molton 	ac97_write(codec, AC97_EXTENDED_STATUS, vra | 0x1);
2572aceefefSIan Molton 
2582aceefefSIan Molton 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2592aceefefSIan Molton 		reg = AC97_PCM_FRONT_DAC_RATE;
2602aceefefSIan Molton 	else
2612aceefefSIan Molton 		reg = AC97_PCM_LR_ADC_RATE;
2622aceefefSIan Molton 
2632aceefefSIan Molton 	return ac97_write(codec, reg, runtime->rate);
2642aceefefSIan Molton }
2652aceefefSIan Molton 
2662aceefefSIan Molton #define WM9705_AC97_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 | \
2672aceefefSIan Molton 			SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 | \
2682aceefefSIan Molton 			SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
2692aceefefSIan Molton 			SNDRV_PCM_RATE_48000)
2702aceefefSIan Molton 
2716335d055SEric Miao static struct snd_soc_dai_ops wm9705_dai_ops = {
2726335d055SEric Miao 	.prepare	= ac97_prepare,
2736335d055SEric Miao };
2746335d055SEric Miao 
2752aceefefSIan Molton struct snd_soc_dai wm9705_dai[] = {
2762aceefefSIan Molton 	{
2772aceefefSIan Molton 		.name = "AC97 HiFi",
2782aceefefSIan Molton 		.ac97_control = 1,
2792aceefefSIan Molton 		.playback = {
2802aceefefSIan Molton 			.stream_name = "HiFi Playback",
2812aceefefSIan Molton 			.channels_min = 1,
2822aceefefSIan Molton 			.channels_max = 2,
2832aceefefSIan Molton 			.rates = WM9705_AC97_RATES,
28433f503c9SMark Brown 			.formats = SND_SOC_STD_AC97_FMTS,
2852aceefefSIan Molton 		},
2862aceefefSIan Molton 		.capture = {
2872aceefefSIan Molton 			.stream_name = "HiFi Capture",
2882aceefefSIan Molton 			.channels_min = 1,
2892aceefefSIan Molton 			.channels_max = 2,
2902aceefefSIan Molton 			.rates = WM9705_AC97_RATES,
29133f503c9SMark Brown 			.formats = SND_SOC_STD_AC97_FMTS,
2922aceefefSIan Molton 		},
2936335d055SEric Miao 		.ops = &wm9705_dai_ops,
2942aceefefSIan Molton 	},
2952aceefefSIan Molton 	{
2962aceefefSIan Molton 		.name = "AC97 Aux",
2972aceefefSIan Molton 		.playback = {
2982aceefefSIan Molton 			.stream_name = "Aux Playback",
2992aceefefSIan Molton 			.channels_min = 1,
3002aceefefSIan Molton 			.channels_max = 1,
3012aceefefSIan Molton 			.rates = WM9705_AC97_RATES,
3022aceefefSIan Molton 			.formats = SNDRV_PCM_FMTBIT_S16_LE,
3032aceefefSIan Molton 		},
3042aceefefSIan Molton 	}
3052aceefefSIan Molton };
3062aceefefSIan Molton EXPORT_SYMBOL_GPL(wm9705_dai);
3072aceefefSIan Molton 
3082aceefefSIan Molton static int wm9705_reset(struct snd_soc_codec *codec)
3092aceefefSIan Molton {
3102aceefefSIan Molton 	if (soc_ac97_ops.reset) {
3112aceefefSIan Molton 		soc_ac97_ops.reset(codec->ac97);
3122aceefefSIan Molton 		if (ac97_read(codec, 0) == wm9705_reg[0])
3132aceefefSIan Molton 			return 0; /* Success */
3142aceefefSIan Molton 	}
3152aceefefSIan Molton 
3162aceefefSIan Molton 	return -EIO;
3172aceefefSIan Molton }
3182aceefefSIan Molton 
3190a11b168SMark Brown #ifdef CONFIG_PM
320516ef69fSRussell King - ARM Linux static int wm9705_soc_suspend(struct platform_device *pdev, pm_message_t msg)
3210a11b168SMark Brown {
3220a11b168SMark Brown 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
3230a11b168SMark Brown 	struct snd_soc_codec *codec = socdev->card->codec;
3240a11b168SMark Brown 
3250a11b168SMark Brown 	soc_ac97_ops.write(codec->ac97, AC97_POWERDOWN, 0xffff);
3260a11b168SMark Brown 
3270a11b168SMark Brown 	return 0;
3280a11b168SMark Brown }
3290a11b168SMark Brown 
3300a11b168SMark Brown static int wm9705_soc_resume(struct platform_device *pdev)
3310a11b168SMark Brown {
3320a11b168SMark Brown 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
3330a11b168SMark Brown 	struct snd_soc_codec *codec = socdev->card->codec;
3340a11b168SMark Brown 	int i, ret;
3350a11b168SMark Brown 	u16 *cache = codec->reg_cache;
3360a11b168SMark Brown 
3370a11b168SMark Brown 	ret = wm9705_reset(codec);
3380a11b168SMark Brown 	if (ret < 0) {
3390a11b168SMark Brown 		printk(KERN_ERR "could not reset AC97 codec\n");
3400a11b168SMark Brown 		return ret;
3410a11b168SMark Brown 	}
3420a11b168SMark Brown 
3430a11b168SMark Brown 	for (i = 2; i < ARRAY_SIZE(wm9705_reg) << 1; i += 2) {
3440a11b168SMark Brown 		soc_ac97_ops.write(codec->ac97, i, cache[i>>1]);
3450a11b168SMark Brown 	}
3460a11b168SMark Brown 
3470a11b168SMark Brown 	return 0;
3480a11b168SMark Brown }
3490a11b168SMark Brown #else
3500a11b168SMark Brown #define wm9705_soc_suspend NULL
3510a11b168SMark Brown #define wm9705_soc_resume NULL
3520a11b168SMark Brown #endif
3530a11b168SMark Brown 
3542aceefefSIan Molton static int wm9705_soc_probe(struct platform_device *pdev)
3552aceefefSIan Molton {
3562aceefefSIan Molton 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
3572aceefefSIan Molton 	struct snd_soc_codec *codec;
3582aceefefSIan Molton 	int ret = 0;
3592aceefefSIan Molton 
3602aceefefSIan Molton 	printk(KERN_INFO "WM9705 SoC Audio Codec\n");
3612aceefefSIan Molton 
3626627a653SMark Brown 	socdev->card->codec = kzalloc(sizeof(struct snd_soc_codec),
3636627a653SMark Brown 				      GFP_KERNEL);
3646627a653SMark Brown 	if (socdev->card->codec == NULL)
3652aceefefSIan Molton 		return -ENOMEM;
3666627a653SMark Brown 	codec = socdev->card->codec;
3672aceefefSIan Molton 	mutex_init(&codec->mutex);
3682aceefefSIan Molton 
3692aceefefSIan Molton 	codec->reg_cache = kmemdup(wm9705_reg, sizeof(wm9705_reg), GFP_KERNEL);
3702aceefefSIan Molton 	if (codec->reg_cache == NULL) {
3712aceefefSIan Molton 		ret = -ENOMEM;
3722aceefefSIan Molton 		goto cache_err;
3732aceefefSIan Molton 	}
3742aceefefSIan Molton 	codec->reg_cache_size = sizeof(wm9705_reg);
3752aceefefSIan Molton 	codec->reg_cache_step = 2;
3762aceefefSIan Molton 
3772aceefefSIan Molton 	codec->name = "WM9705";
3782aceefefSIan Molton 	codec->owner = THIS_MODULE;
3792aceefefSIan Molton 	codec->dai = wm9705_dai;
3802aceefefSIan Molton 	codec->num_dai = ARRAY_SIZE(wm9705_dai);
3812aceefefSIan Molton 	codec->write = ac97_write;
3822aceefefSIan Molton 	codec->read = ac97_read;
3832aceefefSIan Molton 	INIT_LIST_HEAD(&codec->dapm_widgets);
3842aceefefSIan Molton 	INIT_LIST_HEAD(&codec->dapm_paths);
3852aceefefSIan Molton 
3862aceefefSIan Molton 	ret = snd_soc_new_ac97_codec(codec, &soc_ac97_ops, 0);
3872aceefefSIan Molton 	if (ret < 0) {
3882aceefefSIan Molton 		printk(KERN_ERR "wm9705: failed to register AC97 codec\n");
3892aceefefSIan Molton 		goto codec_err;
3902aceefefSIan Molton 	}
3912aceefefSIan Molton 
3922aceefefSIan Molton 	/* register pcms */
3932aceefefSIan Molton 	ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
3942aceefefSIan Molton 	if (ret < 0)
3952aceefefSIan Molton 		goto pcm_err;
3962aceefefSIan Molton 
3972aceefefSIan Molton 	ret = wm9705_reset(codec);
3982aceefefSIan Molton 	if (ret)
3992aceefefSIan Molton 		goto reset_err;
4002aceefefSIan Molton 
4012aceefefSIan Molton 	snd_soc_add_controls(codec, wm9705_snd_ac97_controls,
4022aceefefSIan Molton 				ARRAY_SIZE(wm9705_snd_ac97_controls));
4032aceefefSIan Molton 	wm9705_add_widgets(codec);
4042aceefefSIan Molton 
4052aceefefSIan Molton 	return 0;
4062aceefefSIan Molton 
407a22eaf4cSTakashi Iwai reset_err:
408a22eaf4cSTakashi Iwai 	snd_soc_free_pcms(socdev);
4092aceefefSIan Molton pcm_err:
4102aceefefSIan Molton 	snd_soc_free_ac97_codec(codec);
4112aceefefSIan Molton codec_err:
4122aceefefSIan Molton 	kfree(codec->reg_cache);
4132aceefefSIan Molton cache_err:
4146627a653SMark Brown 	kfree(socdev->card->codec);
4156627a653SMark Brown 	socdev->card->codec = NULL;
4162aceefefSIan Molton 	return ret;
4172aceefefSIan Molton }
4182aceefefSIan Molton 
4192aceefefSIan Molton static int wm9705_soc_remove(struct platform_device *pdev)
4202aceefefSIan Molton {
4212aceefefSIan Molton 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
4226627a653SMark Brown 	struct snd_soc_codec *codec = socdev->card->codec;
4232aceefefSIan Molton 
4242aceefefSIan Molton 	if (codec == NULL)
4252aceefefSIan Molton 		return 0;
4262aceefefSIan Molton 
4272aceefefSIan Molton 	snd_soc_dapm_free(socdev);
4282aceefefSIan Molton 	snd_soc_free_pcms(socdev);
4292aceefefSIan Molton 	snd_soc_free_ac97_codec(codec);
4302aceefefSIan Molton 	kfree(codec->reg_cache);
4312aceefefSIan Molton 	kfree(codec);
4322aceefefSIan Molton 	return 0;
4332aceefefSIan Molton }
4342aceefefSIan Molton 
4352aceefefSIan Molton struct snd_soc_codec_device soc_codec_dev_wm9705 = {
4362aceefefSIan Molton 	.probe = 	wm9705_soc_probe,
4372aceefefSIan Molton 	.remove = 	wm9705_soc_remove,
4380a11b168SMark Brown 	.suspend =	wm9705_soc_suspend,
4390a11b168SMark Brown 	.resume =	wm9705_soc_resume,
4402aceefefSIan Molton };
4412aceefefSIan Molton EXPORT_SYMBOL_GPL(soc_codec_dev_wm9705);
4422aceefefSIan Molton 
4432aceefefSIan Molton MODULE_DESCRIPTION("ASoC WM9705 driver");
4442aceefefSIan Molton MODULE_AUTHOR("Ian Molton");
4452aceefefSIan Molton MODULE_LICENSE("GPL v2");
446