blob: 410fed953c541c32d64844ff74c571a1d2fba8e5 [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>
15#include <linux/module.h>
Cliff Cai5f57dc82008-07-29 11:42:34 +010016#include <linux/kernel.h>
17#include <linux/device.h>
18#include <sound/core.h>
19#include <sound/pcm.h>
20#include <sound/ac97_codec.h>
21#include <sound/initval.h>
22#include <sound/soc.h>
23#include <sound/soc-dapm.h>
24
25#include "ad1980.h"
26
27static unsigned int ac97_read(struct snd_soc_codec *codec,
28 unsigned int reg);
29static int ac97_write(struct snd_soc_codec *codec,
30 unsigned int reg, unsigned int val);
31
32/*
33 * AD1980 register cache
34 */
35static const u16 ad1980_reg[] = {
36 0x0090, 0x8000, 0x8000, 0x8000, /* 0 - 6 */
37 0x0000, 0x0000, 0x8008, 0x8008, /* 8 - e */
38 0x8808, 0x8808, 0x0000, 0x8808, /* 10 - 16 */
39 0x8808, 0x0000, 0x8000, 0x0000, /* 18 - 1e */
40 0x0000, 0x0000, 0x0000, 0x0000, /* 20 - 26 */
41 0x03c7, 0x0000, 0xbb80, 0xbb80, /* 28 - 2e */
42 0xbb80, 0xbb80, 0x0000, 0x8080, /* 30 - 36 */
43 0x8080, 0x2000, 0x0000, 0x0000, /* 38 - 3e */
44 0x0000, 0x0000, 0x0000, 0x0000, /* reserved */
45 0x0000, 0x0000, 0x0000, 0x0000, /* reserved */
46 0x0000, 0x0000, 0x0000, 0x0000, /* reserved */
47 0x0000, 0x0000, 0x0000, 0x0000, /* reserved */
48 0x8080, 0x0000, 0x0000, 0x0000, /* 60 - 66 */
49 0x0000, 0x0000, 0x0000, 0x0000, /* reserved */
50 0x0000, 0x0000, 0x1001, 0x0000, /* 70 - 76 */
51 0x0000, 0x0000, 0x4144, 0x5370 /* 78 - 7e */
52};
53
54static const char *ad1980_rec_sel[] = {"Mic", "CD", "NC", "AUX", "Line",
55 "Stereo Mix", "Mono Mix", "Phone"};
56
57static const struct soc_enum ad1980_cap_src =
58 SOC_ENUM_DOUBLE(AC97_REC_SEL, 8, 0, 7, ad1980_rec_sel);
59
60static const struct snd_kcontrol_new ad1980_snd_ac97_controls[] = {
61SOC_DOUBLE("Master Playback Volume", AC97_MASTER, 8, 0, 31, 1),
62SOC_SINGLE("Master Playback Switch", AC97_MASTER, 15, 1, 1),
63
64SOC_DOUBLE("Headphone Playback Volume", AC97_HEADPHONE, 8, 0, 31, 1),
65SOC_SINGLE("Headphone Playback Switch", AC97_HEADPHONE, 15, 1, 1),
66
67SOC_DOUBLE("PCM Playback Volume", AC97_PCM, 8, 0, 31, 1),
68SOC_SINGLE("PCM Playback Switch", AC97_PCM, 15, 1, 1),
69
70SOC_DOUBLE("PCM Capture Volume", AC97_REC_GAIN, 8, 0, 31, 0),
71SOC_SINGLE("PCM Capture Switch", AC97_REC_GAIN, 15, 1, 1),
72
73SOC_SINGLE("Mono Playback Volume", AC97_MASTER_MONO, 0, 31, 1),
74SOC_SINGLE("Mono Playback Switch", AC97_MASTER_MONO, 15, 1, 1),
75
76SOC_SINGLE("Phone Capture Volume", AC97_PHONE, 0, 31, 1),
77SOC_SINGLE("Phone Capture Switch", AC97_PHONE, 15, 1, 1),
78
79SOC_SINGLE("Mic Volume", AC97_MIC, 0, 31, 1),
80SOC_SINGLE("Mic Switch", AC97_MIC, 15, 1, 1),
81
82SOC_SINGLE("Stereo Mic Switch", AC97_AD_MISC, 6, 1, 0),
83SOC_DOUBLE("Line HP Swap Switch", AC97_AD_MISC, 10, 5, 1, 0),
84
85SOC_DOUBLE("Surround Playback Volume", AC97_SURROUND_MASTER, 8, 0, 31, 1),
86SOC_DOUBLE("Surround Playback Switch", AC97_SURROUND_MASTER, 15, 7, 1, 1),
87
Cliff Cai9905ed32008-11-18 16:18:16 +080088SOC_DOUBLE("Center/LFE Playback Volume", AC97_CENTER_LFE_MASTER, 8, 0, 31, 1),
89SOC_DOUBLE("Center/LFE Playback Switch", AC97_CENTER_LFE_MASTER, 15, 7, 1, 1),
90
Cliff Cai5f57dc82008-07-29 11:42:34 +010091SOC_ENUM("Capture Source", ad1980_cap_src),
92
93SOC_SINGLE("Mic Boost Switch", AC97_MIC, 6, 1, 0),
94};
95
96/* add non dapm controls */
97static int ad1980_add_controls(struct snd_soc_codec *codec)
98{
99 int err, i;
100
101 for (i = 0; i < ARRAY_SIZE(ad1980_snd_ac97_controls); i++) {
102 err = snd_ctl_add(codec->card, snd_soc_cnew(
103 &ad1980_snd_ac97_controls[i], codec, NULL));
104 if (err < 0)
105 return err;
106 }
107 return 0;
108}
109
110static unsigned int ac97_read(struct snd_soc_codec *codec,
111 unsigned int reg)
112{
113 u16 *cache = codec->reg_cache;
114
115 switch (reg) {
116 case AC97_RESET:
117 case AC97_INT_PAGING:
118 case AC97_POWERDOWN:
119 case AC97_EXTENDED_STATUS:
120 case AC97_VENDOR_ID1:
121 case AC97_VENDOR_ID2:
122 return soc_ac97_ops.read(codec->ac97, reg);
123 default:
124 reg = reg >> 1;
125
126 if (reg >= (ARRAY_SIZE(ad1980_reg)))
127 return -EINVAL;
128
129 return cache[reg];
130 }
131}
132
133static int ac97_write(struct snd_soc_codec *codec, unsigned int reg,
134 unsigned int val)
135{
136 u16 *cache = codec->reg_cache;
137
138 soc_ac97_ops.write(codec->ac97, reg, val);
139 reg = reg >> 1;
140 if (reg < (ARRAY_SIZE(ad1980_reg)))
141 cache[reg] = val;
142
143 return 0;
144}
145
Cliff Caib1cd9b22008-09-04 16:25:54 +0800146struct snd_soc_dai ad1980_dai = {
Cliff Cai5f57dc82008-07-29 11:42:34 +0100147 .name = "AC97",
148 .playback = {
149 .stream_name = "Playback",
150 .channels_min = 2,
Cliff Cai9905ed32008-11-18 16:18:16 +0800151 .channels_max = 6,
Cliff Cai5f57dc82008-07-29 11:42:34 +0100152 .rates = SNDRV_PCM_RATE_48000,
153 .formats = SNDRV_PCM_FMTBIT_S16_LE, },
154 .capture = {
155 .stream_name = "Capture",
156 .channels_min = 2,
157 .channels_max = 2,
158 .rates = SNDRV_PCM_RATE_48000,
159 .formats = SNDRV_PCM_FMTBIT_S16_LE, },
160};
161EXPORT_SYMBOL_GPL(ad1980_dai);
162
163static int ad1980_reset(struct snd_soc_codec *codec, int try_warm)
164{
165 u16 retry_cnt = 0;
166
167retry:
168 if (try_warm && soc_ac97_ops.warm_reset) {
169 soc_ac97_ops.warm_reset(codec->ac97);
170 if (ac97_read(codec, AC97_RESET) == 0x0090)
171 return 1;
172 }
173
174 soc_ac97_ops.reset(codec->ac97);
175 /* Set bit 16slot in register 74h, then every slot will has only 16
176 * bits. This command is sent out in 20bit mode, in which case the
177 * first nibble of data is eaten by the addr. (Tag is always 16 bit)*/
178 ac97_write(codec, AC97_AD_SERIAL_CFG, 0x9900);
179
180 if (ac97_read(codec, AC97_RESET) != 0x0090)
181 goto err;
182 return 0;
183
184err:
185 while (retry_cnt++ < 10)
186 goto retry;
187
188 printk(KERN_ERR "AD1980 AC97 reset failed\n");
189 return -EIO;
190}
191
192static int ad1980_soc_probe(struct platform_device *pdev)
193{
194 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
195 struct snd_soc_codec *codec;
196 int ret = 0;
197 u16 vendor_id2;
Cliff Cai9905ed32008-11-18 16:18:16 +0800198 u16 ext_status;
Cliff Cai5f57dc82008-07-29 11:42:34 +0100199
200 printk(KERN_INFO "AD1980 SoC Audio Codec\n");
201
202 socdev->codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
203 if (socdev->codec == NULL)
204 return -ENOMEM;
205 codec = socdev->codec;
206 mutex_init(&codec->mutex);
207
208 codec->reg_cache =
209 kzalloc(sizeof(u16) * ARRAY_SIZE(ad1980_reg), GFP_KERNEL);
210 if (codec->reg_cache == NULL) {
211 ret = -ENOMEM;
212 goto cache_err;
213 }
214 memcpy(codec->reg_cache, ad1980_reg, sizeof(u16) * \
215 ARRAY_SIZE(ad1980_reg));
216 codec->reg_cache_size = sizeof(u16) * ARRAY_SIZE(ad1980_reg);
217 codec->reg_cache_step = 2;
218 codec->name = "AD1980";
219 codec->owner = THIS_MODULE;
220 codec->dai = &ad1980_dai;
221 codec->num_dai = 1;
222 codec->write = ac97_write;
223 codec->read = ac97_read;
224 INIT_LIST_HEAD(&codec->dapm_widgets);
225 INIT_LIST_HEAD(&codec->dapm_paths);
226
227 ret = snd_soc_new_ac97_codec(codec, &soc_ac97_ops, 0);
228 if (ret < 0) {
229 printk(KERN_ERR "ad1980: failed to register AC97 codec\n");
230 goto codec_err;
231 }
232
233 /* register pcms */
234 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
235 if (ret < 0)
236 goto pcm_err;
237
238
239 ret = ad1980_reset(codec, 0);
240 if (ret < 0) {
241 printk(KERN_ERR "AC97 link error\n");
242 goto reset_err;
243 }
244
245 /* Read out vendor ID to make sure it is ad1980 */
246 if (ac97_read(codec, AC97_VENDOR_ID1) != 0x4144)
247 goto reset_err;
248
249 vendor_id2 = ac97_read(codec, AC97_VENDOR_ID2);
250
251 if (vendor_id2 != 0x5370) {
252 if (vendor_id2 != 0x5374)
253 goto reset_err;
254 else
255 printk(KERN_WARNING "ad1980: "
256 "Found AD1981 - only 2/2 IN/OUT Channels "
257 "supported\n");
258 }
259
Cliff Cai9905ed32008-11-18 16:18:16 +0800260 /* unmute captures and playbacks volume */
261 ac97_write(codec, AC97_MASTER, 0x0000);
262 ac97_write(codec, AC97_PCM, 0x0000);
263 ac97_write(codec, AC97_REC_GAIN, 0x0000);
264 ac97_write(codec, AC97_CENTER_LFE_MASTER, 0x0000);
265 ac97_write(codec, AC97_SURROUND_MASTER, 0x0000);
266
267 /*power on LFE/CENTER/Surround DACs*/
268 ext_status = ac97_read(codec, AC97_EXTENDED_STATUS);
269 ac97_write(codec, AC97_EXTENDED_STATUS, ext_status&~0x3800);
Cliff Cai5f57dc82008-07-29 11:42:34 +0100270
271 ad1980_add_controls(codec);
272 ret = snd_soc_register_card(socdev);
273 if (ret < 0) {
274 printk(KERN_ERR "ad1980: failed to register card\n");
275 goto reset_err;
276 }
277
278 return 0;
279
280reset_err:
281 snd_soc_free_pcms(socdev);
282
283pcm_err:
284 snd_soc_free_ac97_codec(codec);
285
286codec_err:
287 kfree(codec->reg_cache);
288
289cache_err:
290 kfree(socdev->codec);
291 socdev->codec = NULL;
292 return ret;
293}
294
295static int ad1980_soc_remove(struct platform_device *pdev)
296{
297 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
298 struct snd_soc_codec *codec = socdev->codec;
299
300 if (codec == NULL)
301 return 0;
302
303 snd_soc_dapm_free(socdev);
304 snd_soc_free_pcms(socdev);
305 snd_soc_free_ac97_codec(codec);
306 kfree(codec->reg_cache);
307 kfree(codec);
308 return 0;
309}
310
311struct snd_soc_codec_device soc_codec_dev_ad1980 = {
312 .probe = ad1980_soc_probe,
313 .remove = ad1980_soc_remove,
314};
315EXPORT_SYMBOL_GPL(soc_codec_dev_ad1980);
316
317MODULE_DESCRIPTION("ASoC ad1980 driver");
318MODULE_AUTHOR("Roy Huang, Cliff Cai");
319MODULE_LICENSE("GPL");