ASoC: wm8904: fix DSP mode B configuration
[pandora-kernel.git] / sound / soc / codecs / ad1980.c
1 /*
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 /*
15  * WARNING:
16  *
17  * Because Analog Devices Inc. discontinued the ad1980 sound chip since
18  * Sep. 2009, this ad1980 driver is not maintained, tested and supported
19  * by ADI now.
20  */
21
22 #include <linux/init.h>
23 #include <linux/slab.h>
24 #include <linux/module.h>
25 #include <linux/kernel.h>
26 #include <linux/device.h>
27 #include <sound/core.h>
28 #include <sound/pcm.h>
29 #include <sound/ac97_codec.h>
30 #include <sound/initval.h>
31 #include <sound/soc.h>
32
33 #include "ad1980.h"
34
35 /*
36  * AD1980 register cache
37  */
38 static const u16 ad1980_reg[] = {
39         0x0090, 0x8000, 0x8000, 0x8000, /* 0 - 6  */
40         0x0000, 0x0000, 0x8008, 0x8008, /* 8 - e  */
41         0x8808, 0x8808, 0x0000, 0x8808, /* 10 - 16 */
42         0x8808, 0x0000, 0x8000, 0x0000, /* 18 - 1e */
43         0x0000, 0x0000, 0x0000, 0x0000, /* 20 - 26 */
44         0x03c7, 0x0000, 0xbb80, 0xbb80, /* 28 - 2e */
45         0xbb80, 0xbb80, 0x0000, 0x8080, /* 30 - 36 */
46         0x8080, 0x2000, 0x0000, 0x0000, /* 38 - 3e */
47         0x0000, 0x0000, 0x0000, 0x0000, /* reserved */
48         0x0000, 0x0000, 0x0000, 0x0000, /* reserved */
49         0x0000, 0x0000, 0x0000, 0x0000, /* reserved */
50         0x0000, 0x0000, 0x0000, 0x0000, /* reserved */
51         0x8080, 0x0000, 0x0000, 0x0000, /* 60 - 66 */
52         0x0000, 0x0000, 0x0000, 0x0000, /* reserved */
53         0x0000, 0x0000, 0x1001, 0x0000, /* 70 - 76 */
54         0x0000, 0x0000, 0x4144, 0x5370  /* 78 - 7e */
55 };
56
57 static const char *ad1980_rec_sel[] = {"Mic", "CD", "NC", "AUX", "Line",
58                 "Stereo Mix", "Mono Mix", "Phone"};
59
60 static const struct soc_enum ad1980_cap_src =
61         SOC_ENUM_DOUBLE(AC97_REC_SEL, 8, 0, 7, ad1980_rec_sel);
62
63 static const struct snd_kcontrol_new ad1980_snd_ac97_controls[] = {
64 SOC_DOUBLE("Master Playback Volume", AC97_MASTER, 8, 0, 31, 1),
65 SOC_SINGLE("Master Playback Switch", AC97_MASTER, 15, 1, 1),
66
67 SOC_DOUBLE("Headphone Playback Volume", AC97_HEADPHONE, 8, 0, 31, 1),
68 SOC_SINGLE("Headphone Playback Switch", AC97_HEADPHONE, 15, 1, 1),
69
70 SOC_DOUBLE("PCM Playback Volume", AC97_PCM, 8, 0, 31, 1),
71 SOC_SINGLE("PCM Playback Switch", AC97_PCM, 15, 1, 1),
72
73 SOC_DOUBLE("PCM Capture Volume", AC97_REC_GAIN, 8, 0, 31, 0),
74 SOC_SINGLE("PCM Capture Switch", AC97_REC_GAIN, 15, 1, 1),
75
76 SOC_SINGLE("Mono Playback Volume", AC97_MASTER_MONO, 0, 31, 1),
77 SOC_SINGLE("Mono Playback Switch", AC97_MASTER_MONO, 15, 1, 1),
78
79 SOC_SINGLE("Phone Capture Volume", AC97_PHONE, 0, 31, 1),
80 SOC_SINGLE("Phone Capture Switch", AC97_PHONE, 15, 1, 1),
81
82 SOC_SINGLE("Mic Volume", AC97_MIC, 0, 31, 1),
83 SOC_SINGLE("Mic Switch", AC97_MIC, 15, 1, 1),
84
85 SOC_SINGLE("Stereo Mic Switch", AC97_AD_MISC, 6, 1, 0),
86 SOC_DOUBLE("Line HP Swap Switch", AC97_AD_MISC, 10, 5, 1, 0),
87
88 SOC_DOUBLE("Surround Playback Volume", AC97_SURROUND_MASTER, 8, 0, 31, 1),
89 SOC_DOUBLE("Surround Playback Switch", AC97_SURROUND_MASTER, 15, 7, 1, 1),
90
91 SOC_DOUBLE("Center/LFE Playback Volume", AC97_CENTER_LFE_MASTER, 8, 0, 31, 1),
92 SOC_DOUBLE("Center/LFE Playback Switch", AC97_CENTER_LFE_MASTER, 15, 7, 1, 1),
93
94 SOC_ENUM("Capture Source", ad1980_cap_src),
95
96 SOC_SINGLE("Mic Boost Switch", AC97_MIC, 6, 1, 0),
97 };
98
99 static unsigned int ac97_read(struct snd_soc_codec *codec,
100         unsigned int reg)
101 {
102         u16 *cache = codec->reg_cache;
103
104         switch (reg) {
105         case AC97_RESET:
106         case AC97_INT_PAGING:
107         case AC97_POWERDOWN:
108         case AC97_EXTENDED_STATUS:
109         case AC97_VENDOR_ID1:
110         case AC97_VENDOR_ID2:
111                 return soc_ac97_ops.read(codec->ac97, reg);
112         default:
113                 reg = reg >> 1;
114
115                 if (reg >= ARRAY_SIZE(ad1980_reg))
116                         return -EINVAL;
117
118                 return cache[reg];
119         }
120 }
121
122 static int ac97_write(struct snd_soc_codec *codec, unsigned int reg,
123         unsigned int val)
124 {
125         u16 *cache = codec->reg_cache;
126
127         soc_ac97_ops.write(codec->ac97, reg, val);
128         reg = reg >> 1;
129         if (reg < ARRAY_SIZE(ad1980_reg))
130                 cache[reg] = val;
131
132         return 0;
133 }
134
135 static struct snd_soc_dai_driver ad1980_dai = {
136         .name = "ad1980-hifi",
137         .ac97_control = 1,
138         .playback = {
139                 .stream_name = "Playback",
140                 .channels_min = 2,
141                 .channels_max = 6,
142                 .rates = SNDRV_PCM_RATE_48000,
143                 .formats = SND_SOC_STD_AC97_FMTS, },
144         .capture = {
145                 .stream_name = "Capture",
146                 .channels_min = 2,
147                 .channels_max = 2,
148                 .rates = SNDRV_PCM_RATE_48000,
149                 .formats = SND_SOC_STD_AC97_FMTS, },
150 };
151
152 static int ad1980_reset(struct snd_soc_codec *codec, int try_warm)
153 {
154         u16 retry_cnt = 0;
155
156 retry:
157         if (try_warm && soc_ac97_ops.warm_reset) {
158                 soc_ac97_ops.warm_reset(codec->ac97);
159                 if (ac97_read(codec, AC97_RESET) == 0x0090)
160                         return 1;
161         }
162
163         soc_ac97_ops.reset(codec->ac97);
164         /* Set bit 16slot in register 74h, then every slot will has only 16
165          * bits. This command is sent out in 20bit mode, in which case the
166          * first nibble of data is eaten by the addr. (Tag is always 16 bit)*/
167         ac97_write(codec, AC97_AD_SERIAL_CFG, 0x9900);
168
169         if (ac97_read(codec, AC97_RESET)  != 0x0090)
170                 goto err;
171         return 0;
172
173 err:
174         while (retry_cnt++ < 10)
175                 goto retry;
176
177         printk(KERN_ERR "AD1980 AC97 reset failed\n");
178         return -EIO;
179 }
180
181 static int ad1980_soc_probe(struct snd_soc_codec *codec)
182 {
183         int ret;
184         u16 vendor_id2;
185         u16 ext_status;
186
187         printk(KERN_INFO "AD1980 SoC Audio Codec\n");
188
189         ret = snd_soc_new_ac97_codec(codec, &soc_ac97_ops, 0);
190         if (ret < 0) {
191                 printk(KERN_ERR "ad1980: failed to register AC97 codec\n");
192                 return ret;
193         }
194
195         ret = ad1980_reset(codec, 0);
196         if (ret < 0) {
197                 printk(KERN_ERR "Failed to reset AD1980: AC97 link error\n");
198                 goto reset_err;
199         }
200
201         /* Read out vendor ID to make sure it is ad1980 */
202         if (ac97_read(codec, AC97_VENDOR_ID1) != 0x4144) {
203                 ret = -ENODEV;
204                 goto reset_err;
205         }
206
207         vendor_id2 = ac97_read(codec, AC97_VENDOR_ID2);
208
209         if (vendor_id2 != 0x5370) {
210                 if (vendor_id2 != 0x5374) {
211                         ret = -ENODEV;
212                         goto reset_err;
213                 } else {
214                         printk(KERN_WARNING "ad1980: "
215                                 "Found AD1981 - only 2/2 IN/OUT Channels "
216                                 "supported\n");
217                 }
218         }
219
220         /* unmute captures and playbacks volume */
221         ac97_write(codec, AC97_MASTER, 0x0000);
222         ac97_write(codec, AC97_PCM, 0x0000);
223         ac97_write(codec, AC97_REC_GAIN, 0x0000);
224         ac97_write(codec, AC97_CENTER_LFE_MASTER, 0x0000);
225         ac97_write(codec, AC97_SURROUND_MASTER, 0x0000);
226
227         /*power on LFE/CENTER/Surround DACs*/
228         ext_status = ac97_read(codec, AC97_EXTENDED_STATUS);
229         ac97_write(codec, AC97_EXTENDED_STATUS, ext_status&~0x3800);
230
231         snd_soc_add_controls(codec, ad1980_snd_ac97_controls,
232                                 ARRAY_SIZE(ad1980_snd_ac97_controls));
233
234         return 0;
235
236 reset_err:
237         snd_soc_free_ac97_codec(codec);
238         return ret;
239 }
240
241 static int ad1980_soc_remove(struct snd_soc_codec *codec)
242 {
243         snd_soc_free_ac97_codec(codec);
244         return 0;
245 }
246
247 static struct snd_soc_codec_driver soc_codec_dev_ad1980 = {
248         .probe =        ad1980_soc_probe,
249         .remove =       ad1980_soc_remove,
250         .reg_cache_size = ARRAY_SIZE(ad1980_reg),
251         .reg_word_size = sizeof(u16),
252         .reg_cache_default = ad1980_reg,
253         .reg_cache_step = 2,
254         .write = ac97_write,
255         .read = ac97_read,
256 };
257
258 static __devinit int ad1980_probe(struct platform_device *pdev)
259 {
260         return snd_soc_register_codec(&pdev->dev,
261                         &soc_codec_dev_ad1980, &ad1980_dai, 1);
262 }
263
264 static int __devexit ad1980_remove(struct platform_device *pdev)
265 {
266         snd_soc_unregister_codec(&pdev->dev);
267         return 0;
268 }
269
270 static struct platform_driver ad1980_codec_driver = {
271         .driver = {
272                         .name = "ad1980",
273                         .owner = THIS_MODULE,
274         },
275
276         .probe = ad1980_probe,
277         .remove = __devexit_p(ad1980_remove),
278 };
279
280 static int __init ad1980_init(void)
281 {
282         return platform_driver_register(&ad1980_codec_driver);
283 }
284 module_init(ad1980_init);
285
286 static void __exit ad1980_exit(void)
287 {
288         platform_driver_unregister(&ad1980_codec_driver);
289 }
290 module_exit(ad1980_exit);
291
292 MODULE_DESCRIPTION("ASoC ad1980 driver (Obsolete)");
293 MODULE_AUTHOR("Roy Huang, Cliff Cai");
294 MODULE_LICENSE("GPL");