blob: 1371afac657b842835b7337e1129ef29bc231708 [file] [log] [blame]
Cliff Cai5f57dc82008-07-29 11:42:34 +01001/*
2 * ad1980.c -- ALSA Soc AD1980 codec support
3 *
4 * Copyright: Analog Device Inc.
5 * Author: Roy Huang <roy.huang@analog.com>
6 * Cliff Cai <cliff.cai@analog.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
12 */
13
14#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Cliff Cai5f57dc82008-07-29 11:42:34 +010016#include <linux/module.h>
Cliff Cai5f57dc82008-07-29 11:42:34 +010017#include <linux/kernel.h>
18#include <linux/device.h>
19#include <sound/core.h>
20#include <sound/pcm.h>
21#include <sound/ac97_codec.h>
22#include <sound/initval.h>
23#include <sound/soc.h>
24#include <sound/soc-dapm.h>
25
26#include "ad1980.h"
27
28static unsigned int ac97_read(struct snd_soc_codec *codec,
29 unsigned int reg);
30static int ac97_write(struct snd_soc_codec *codec,
31 unsigned int reg, unsigned int val);
32
33/*
34 * AD1980 register cache
35 */
36static const u16 ad1980_reg[] = {
37 0x0090, 0x8000, 0x8000, 0x8000, /* 0 - 6 */
38 0x0000, 0x0000, 0x8008, 0x8008, /* 8 - e */
39 0x8808, 0x8808, 0x0000, 0x8808, /* 10 - 16 */
40 0x8808, 0x0000, 0x8000, 0x0000, /* 18 - 1e */
41 0x0000, 0x0000, 0x0000, 0x0000, /* 20 - 26 */
42 0x03c7, 0x0000, 0xbb80, 0xbb80, /* 28 - 2e */
43 0xbb80, 0xbb80, 0x0000, 0x8080, /* 30 - 36 */
44 0x8080, 0x2000, 0x0000, 0x0000, /* 38 - 3e */
45 0x0000, 0x0000, 0x0000, 0x0000, /* reserved */
46 0x0000, 0x0000, 0x0000, 0x0000, /* reserved */
47 0x0000, 0x0000, 0x0000, 0x0000, /* reserved */
48 0x0000, 0x0000, 0x0000, 0x0000, /* reserved */
49 0x8080, 0x0000, 0x0000, 0x0000, /* 60 - 66 */
50 0x0000, 0x0000, 0x0000, 0x0000, /* reserved */
51 0x0000, 0x0000, 0x1001, 0x0000, /* 70 - 76 */
52 0x0000, 0x0000, 0x4144, 0x5370 /* 78 - 7e */
53};
54
55static const char *ad1980_rec_sel[] = {"Mic", "CD", "NC", "AUX", "Line",
56 "Stereo Mix", "Mono Mix", "Phone"};
57
58static const struct soc_enum ad1980_cap_src =
59 SOC_ENUM_DOUBLE(AC97_REC_SEL, 8, 0, 7, ad1980_rec_sel);
60
61static const struct snd_kcontrol_new ad1980_snd_ac97_controls[] = {
62SOC_DOUBLE("Master Playback Volume", AC97_MASTER, 8, 0, 31, 1),
63SOC_SINGLE("Master Playback Switch", AC97_MASTER, 15, 1, 1),
64
65SOC_DOUBLE("Headphone Playback Volume", AC97_HEADPHONE, 8, 0, 31, 1),
66SOC_SINGLE("Headphone Playback Switch", AC97_HEADPHONE, 15, 1, 1),
67
68SOC_DOUBLE("PCM Playback Volume", AC97_PCM, 8, 0, 31, 1),
69SOC_SINGLE("PCM Playback Switch", AC97_PCM, 15, 1, 1),
70
71SOC_DOUBLE("PCM Capture Volume", AC97_REC_GAIN, 8, 0, 31, 0),
72SOC_SINGLE("PCM Capture Switch", AC97_REC_GAIN, 15, 1, 1),
73
74SOC_SINGLE("Mono Playback Volume", AC97_MASTER_MONO, 0, 31, 1),
75SOC_SINGLE("Mono Playback Switch", AC97_MASTER_MONO, 15, 1, 1),
76
77SOC_SINGLE("Phone Capture Volume", AC97_PHONE, 0, 31, 1),
78SOC_SINGLE("Phone Capture Switch", AC97_PHONE, 15, 1, 1),
79
80SOC_SINGLE("Mic Volume", AC97_MIC, 0, 31, 1),
81SOC_SINGLE("Mic Switch", AC97_MIC, 15, 1, 1),
82
83SOC_SINGLE("Stereo Mic Switch", AC97_AD_MISC, 6, 1, 0),
84SOC_DOUBLE("Line HP Swap Switch", AC97_AD_MISC, 10, 5, 1, 0),
85
86SOC_DOUBLE("Surround Playback Volume", AC97_SURROUND_MASTER, 8, 0, 31, 1),
87SOC_DOUBLE("Surround Playback Switch", AC97_SURROUND_MASTER, 15, 7, 1, 1),
88
Cliff Cai9905ed32008-11-18 16:18:16 +080089SOC_DOUBLE("Center/LFE Playback Volume", AC97_CENTER_LFE_MASTER, 8, 0, 31, 1),
90SOC_DOUBLE("Center/LFE Playback Switch", AC97_CENTER_LFE_MASTER, 15, 7, 1, 1),
91
Cliff Cai5f57dc82008-07-29 11:42:34 +010092SOC_ENUM("Capture Source", ad1980_cap_src),
93
94SOC_SINGLE("Mic Boost Switch", AC97_MIC, 6, 1, 0),
95};
96
Cliff Cai5f57dc82008-07-29 11:42:34 +010097static unsigned int ac97_read(struct snd_soc_codec *codec,
98 unsigned int reg)
99{
100 u16 *cache = codec->reg_cache;
101
102 switch (reg) {
103 case AC97_RESET:
104 case AC97_INT_PAGING:
105 case AC97_POWERDOWN:
106 case AC97_EXTENDED_STATUS:
107 case AC97_VENDOR_ID1:
108 case AC97_VENDOR_ID2:
109 return soc_ac97_ops.read(codec->ac97, reg);
110 default:
111 reg = reg >> 1;
112
Ian Molton91432e92009-01-17 17:44:23 +0000113 if (reg >= ARRAY_SIZE(ad1980_reg))
Cliff Cai5f57dc82008-07-29 11:42:34 +0100114 return -EINVAL;
115
116 return cache[reg];
117 }
118}
119
120static int ac97_write(struct snd_soc_codec *codec, unsigned int reg,
121 unsigned int val)
122{
123 u16 *cache = codec->reg_cache;
124
125 soc_ac97_ops.write(codec->ac97, reg, val);
126 reg = reg >> 1;
Ian Molton91432e92009-01-17 17:44:23 +0000127 if (reg < ARRAY_SIZE(ad1980_reg))
Cliff Cai5f57dc82008-07-29 11:42:34 +0100128 cache[reg] = val;
129
130 return 0;
131}
132
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000133struct snd_soc_dai_driver ad1980_dai = {
134 .name = "ad1980-hifi",
Mark Brown67c91512008-11-24 17:45:26 +0000135 .ac97_control = 1,
Cliff Cai5f57dc82008-07-29 11:42:34 +0100136 .playback = {
137 .stream_name = "Playback",
138 .channels_min = 2,
Cliff Cai9905ed32008-11-18 16:18:16 +0800139 .channels_max = 6,
Cliff Cai5f57dc82008-07-29 11:42:34 +0100140 .rates = SNDRV_PCM_RATE_48000,
Mark Brown33f503c2009-05-02 12:24:55 +0100141 .formats = SND_SOC_STD_AC97_FMTS, },
Cliff Cai5f57dc82008-07-29 11:42:34 +0100142 .capture = {
143 .stream_name = "Capture",
144 .channels_min = 2,
145 .channels_max = 2,
146 .rates = SNDRV_PCM_RATE_48000,
Mark Brown33f503c2009-05-02 12:24:55 +0100147 .formats = SND_SOC_STD_AC97_FMTS, },
Cliff Cai5f57dc82008-07-29 11:42:34 +0100148};
149EXPORT_SYMBOL_GPL(ad1980_dai);
150
151static int ad1980_reset(struct snd_soc_codec *codec, int try_warm)
152{
153 u16 retry_cnt = 0;
154
155retry:
156 if (try_warm && soc_ac97_ops.warm_reset) {
157 soc_ac97_ops.warm_reset(codec->ac97);
158 if (ac97_read(codec, AC97_RESET) == 0x0090)
159 return 1;
160 }
161
162 soc_ac97_ops.reset(codec->ac97);
163 /* Set bit 16slot in register 74h, then every slot will has only 16
164 * bits. This command is sent out in 20bit mode, in which case the
165 * first nibble of data is eaten by the addr. (Tag is always 16 bit)*/
166 ac97_write(codec, AC97_AD_SERIAL_CFG, 0x9900);
167
168 if (ac97_read(codec, AC97_RESET) != 0x0090)
169 goto err;
170 return 0;
171
172err:
173 while (retry_cnt++ < 10)
174 goto retry;
175
176 printk(KERN_ERR "AD1980 AC97 reset failed\n");
177 return -EIO;
178}
179
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000180static int ad1980_soc_probe(struct snd_soc_codec *codec)
Cliff Cai5f57dc82008-07-29 11:42:34 +0100181{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000182 int ret;
Cliff Cai5f57dc82008-07-29 11:42:34 +0100183 u16 vendor_id2;
Cliff Cai9905ed32008-11-18 16:18:16 +0800184 u16 ext_status;
Cliff Cai5f57dc82008-07-29 11:42:34 +0100185
186 printk(KERN_INFO "AD1980 SoC Audio Codec\n");
187
Cliff Cai5f57dc82008-07-29 11:42:34 +0100188 ret = snd_soc_new_ac97_codec(codec, &soc_ac97_ops, 0);
189 if (ret < 0) {
190 printk(KERN_ERR "ad1980: failed to register AC97 codec\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000191 return ret;
Cliff Cai5f57dc82008-07-29 11:42:34 +0100192 }
193
Cliff Cai5f57dc82008-07-29 11:42:34 +0100194 ret = ad1980_reset(codec, 0);
195 if (ret < 0) {
Mark Brown39639fa2008-11-21 14:28:49 +0000196 printk(KERN_ERR "Failed to reset AD1980: AC97 link error\n");
Cliff Cai5f57dc82008-07-29 11:42:34 +0100197 goto reset_err;
198 }
199
200 /* Read out vendor ID to make sure it is ad1980 */
201 if (ac97_read(codec, AC97_VENDOR_ID1) != 0x4144)
202 goto reset_err;
203
204 vendor_id2 = ac97_read(codec, AC97_VENDOR_ID2);
205
206 if (vendor_id2 != 0x5370) {
207 if (vendor_id2 != 0x5374)
208 goto reset_err;
209 else
210 printk(KERN_WARNING "ad1980: "
211 "Found AD1981 - only 2/2 IN/OUT Channels "
212 "supported\n");
213 }
214
Cliff Cai9905ed32008-11-18 16:18:16 +0800215 /* unmute captures and playbacks volume */
216 ac97_write(codec, AC97_MASTER, 0x0000);
217 ac97_write(codec, AC97_PCM, 0x0000);
218 ac97_write(codec, AC97_REC_GAIN, 0x0000);
219 ac97_write(codec, AC97_CENTER_LFE_MASTER, 0x0000);
220 ac97_write(codec, AC97_SURROUND_MASTER, 0x0000);
221
222 /*power on LFE/CENTER/Surround DACs*/
223 ext_status = ac97_read(codec, AC97_EXTENDED_STATUS);
224 ac97_write(codec, AC97_EXTENDED_STATUS, ext_status&~0x3800);
Cliff Cai5f57dc82008-07-29 11:42:34 +0100225
Ian Molton3e8e1952009-01-09 00:23:21 +0000226 snd_soc_add_controls(codec, ad1980_snd_ac97_controls,
227 ARRAY_SIZE(ad1980_snd_ac97_controls));
Cliff Cai5f57dc82008-07-29 11:42:34 +0100228
229 return 0;
230
231reset_err:
Cliff Cai5f57dc82008-07-29 11:42:34 +0100232 snd_soc_free_ac97_codec(codec);
Cliff Cai5f57dc82008-07-29 11:42:34 +0100233 return ret;
234}
235
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000236static int ad1980_soc_remove(struct snd_soc_codec *codec)
Cliff Cai5f57dc82008-07-29 11:42:34 +0100237{
Cliff Cai5f57dc82008-07-29 11:42:34 +0100238 snd_soc_free_ac97_codec(codec);
Cliff Cai5f57dc82008-07-29 11:42:34 +0100239 return 0;
240}
241
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000242static struct snd_soc_codec_driver soc_codec_dev_ad1980 = {
Cliff Cai5f57dc82008-07-29 11:42:34 +0100243 .probe = ad1980_soc_probe,
244 .remove = ad1980_soc_remove,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000245 .reg_cache_size = ARRAY_SIZE(ad1980_reg),
246 .reg_word_size = sizeof(u16),
247 .reg_cache_step = 2,
248 .write = ac97_write,
249 .read = ac97_read,
Cliff Cai5f57dc82008-07-29 11:42:34 +0100250};
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000251
252static __devinit int ad1980_probe(struct platform_device *pdev)
253{
254 return snd_soc_register_codec(&pdev->dev,
255 &soc_codec_dev_ad1980, &ad1980_dai, 1);
256}
257
258static int __devexit ad1980_remove(struct platform_device *pdev)
259{
260 snd_soc_unregister_codec(&pdev->dev);
261 return 0;
262}
263
264static struct platform_driver ad1980_codec_driver = {
265 .driver = {
266 .name = "ad1980-codec",
267 .owner = THIS_MODULE,
268 },
269
270 .probe = ad1980_probe,
271 .remove = __devexit_p(ad1980_remove),
272};
273
274static int __init ad1980_init(void)
275{
276 return platform_driver_register(&ad1980_codec_driver);
277}
278module_init(ad1980_init);
279
280static void __exit ad1980_exit(void)
281{
282 platform_driver_unregister(&ad1980_codec_driver);
283}
284module_exit(ad1980_exit);
Cliff Cai5f57dc82008-07-29 11:42:34 +0100285
286MODULE_DESCRIPTION("ASoC ad1980 driver");
287MODULE_AUTHOR("Roy Huang, Cliff Cai");
288MODULE_LICENSE("GPL");