ALSA: ASoC: AD1980 audio codec driver
[deliverable/linux.git] / sound / soc / codecs / ad1980.c
CommitLineData
5f57dc8b
CC
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#include <linux/init.h>
15#include <linux/module.h>
16#include <linux/version.h>
17#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
89SOC_ENUM("Capture Source", ad1980_cap_src),
90
91SOC_SINGLE("Mic Boost Switch", AC97_MIC, 6, 1, 0),
92};
93
94/* add non dapm controls */
95static int ad1980_add_controls(struct snd_soc_codec *codec)
96{
97 int err, i;
98
99 for (i = 0; i < ARRAY_SIZE(ad1980_snd_ac97_controls); i++) {
100 err = snd_ctl_add(codec->card, snd_soc_cnew(
101 &ad1980_snd_ac97_controls[i], codec, NULL));
102 if (err < 0)
103 return err;
104 }
105 return 0;
106}
107
108static unsigned int ac97_read(struct snd_soc_codec *codec,
109 unsigned int reg)
110{
111 u16 *cache = codec->reg_cache;
112
113 switch (reg) {
114 case AC97_RESET:
115 case AC97_INT_PAGING:
116 case AC97_POWERDOWN:
117 case AC97_EXTENDED_STATUS:
118 case AC97_VENDOR_ID1:
119 case AC97_VENDOR_ID2:
120 return soc_ac97_ops.read(codec->ac97, reg);
121 default:
122 reg = reg >> 1;
123
124 if (reg >= (ARRAY_SIZE(ad1980_reg)))
125 return -EINVAL;
126
127 return cache[reg];
128 }
129}
130
131static int ac97_write(struct snd_soc_codec *codec, unsigned int reg,
132 unsigned int val)
133{
134 u16 *cache = codec->reg_cache;
135
136 soc_ac97_ops.write(codec->ac97, reg, val);
137 reg = reg >> 1;
138 if (reg < (ARRAY_SIZE(ad1980_reg)))
139 cache[reg] = val;
140
141 return 0;
142}
143
144struct snd_soc_codec_dai ad1980_dai = {
145 .name = "AC97",
146 .playback = {
147 .stream_name = "Playback",
148 .channels_min = 2,
149 .channels_max = 2,
150 .rates = SNDRV_PCM_RATE_48000,
151 .formats = SNDRV_PCM_FMTBIT_S16_LE, },
152 .capture = {
153 .stream_name = "Capture",
154 .channels_min = 2,
155 .channels_max = 2,
156 .rates = SNDRV_PCM_RATE_48000,
157 .formats = SNDRV_PCM_FMTBIT_S16_LE, },
158};
159EXPORT_SYMBOL_GPL(ad1980_dai);
160
161static int ad1980_reset(struct snd_soc_codec *codec, int try_warm)
162{
163 u16 retry_cnt = 0;
164
165retry:
166 if (try_warm && soc_ac97_ops.warm_reset) {
167 soc_ac97_ops.warm_reset(codec->ac97);
168 if (ac97_read(codec, AC97_RESET) == 0x0090)
169 return 1;
170 }
171
172 soc_ac97_ops.reset(codec->ac97);
173 /* Set bit 16slot in register 74h, then every slot will has only 16
174 * bits. This command is sent out in 20bit mode, in which case the
175 * first nibble of data is eaten by the addr. (Tag is always 16 bit)*/
176 ac97_write(codec, AC97_AD_SERIAL_CFG, 0x9900);
177
178 if (ac97_read(codec, AC97_RESET) != 0x0090)
179 goto err;
180 return 0;
181
182err:
183 while (retry_cnt++ < 10)
184 goto retry;
185
186 printk(KERN_ERR "AD1980 AC97 reset failed\n");
187 return -EIO;
188}
189
190static int ad1980_soc_probe(struct platform_device *pdev)
191{
192 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
193 struct snd_soc_codec *codec;
194 int ret = 0;
195 u16 vendor_id2;
196
197 printk(KERN_INFO "AD1980 SoC Audio Codec\n");
198
199 socdev->codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
200 if (socdev->codec == NULL)
201 return -ENOMEM;
202 codec = socdev->codec;
203 mutex_init(&codec->mutex);
204
205 codec->reg_cache =
206 kzalloc(sizeof(u16) * ARRAY_SIZE(ad1980_reg), GFP_KERNEL);
207 if (codec->reg_cache == NULL) {
208 ret = -ENOMEM;
209 goto cache_err;
210 }
211 memcpy(codec->reg_cache, ad1980_reg, sizeof(u16) * \
212 ARRAY_SIZE(ad1980_reg));
213 codec->reg_cache_size = sizeof(u16) * ARRAY_SIZE(ad1980_reg);
214 codec->reg_cache_step = 2;
215 codec->name = "AD1980";
216 codec->owner = THIS_MODULE;
217 codec->dai = &ad1980_dai;
218 codec->num_dai = 1;
219 codec->write = ac97_write;
220 codec->read = ac97_read;
221 INIT_LIST_HEAD(&codec->dapm_widgets);
222 INIT_LIST_HEAD(&codec->dapm_paths);
223
224 ret = snd_soc_new_ac97_codec(codec, &soc_ac97_ops, 0);
225 if (ret < 0) {
226 printk(KERN_ERR "ad1980: failed to register AC97 codec\n");
227 goto codec_err;
228 }
229
230 /* register pcms */
231 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
232 if (ret < 0)
233 goto pcm_err;
234
235
236 ret = ad1980_reset(codec, 0);
237 if (ret < 0) {
238 printk(KERN_ERR "AC97 link error\n");
239 goto reset_err;
240 }
241
242 /* Read out vendor ID to make sure it is ad1980 */
243 if (ac97_read(codec, AC97_VENDOR_ID1) != 0x4144)
244 goto reset_err;
245
246 vendor_id2 = ac97_read(codec, AC97_VENDOR_ID2);
247
248 if (vendor_id2 != 0x5370) {
249 if (vendor_id2 != 0x5374)
250 goto reset_err;
251 else
252 printk(KERN_WARNING "ad1980: "
253 "Found AD1981 - only 2/2 IN/OUT Channels "
254 "supported\n");
255 }
256
257 ac97_write(codec, AC97_MASTER, 0x0000); /* unmute line out volume */
258 ac97_write(codec, AC97_PCM, 0x0000); /* unmute PCM out volume */
259 ac97_write(codec, AC97_REC_GAIN, 0x0000);/* unmute record volume */
260
261 ad1980_add_controls(codec);
262 ret = snd_soc_register_card(socdev);
263 if (ret < 0) {
264 printk(KERN_ERR "ad1980: failed to register card\n");
265 goto reset_err;
266 }
267
268 return 0;
269
270reset_err:
271 snd_soc_free_pcms(socdev);
272
273pcm_err:
274 snd_soc_free_ac97_codec(codec);
275
276codec_err:
277 kfree(codec->reg_cache);
278
279cache_err:
280 kfree(socdev->codec);
281 socdev->codec = NULL;
282 return ret;
283}
284
285static int ad1980_soc_remove(struct platform_device *pdev)
286{
287 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
288 struct snd_soc_codec *codec = socdev->codec;
289
290 if (codec == NULL)
291 return 0;
292
293 snd_soc_dapm_free(socdev);
294 snd_soc_free_pcms(socdev);
295 snd_soc_free_ac97_codec(codec);
296 kfree(codec->reg_cache);
297 kfree(codec);
298 return 0;
299}
300
301struct snd_soc_codec_device soc_codec_dev_ad1980 = {
302 .probe = ad1980_soc_probe,
303 .remove = ad1980_soc_remove,
304};
305EXPORT_SYMBOL_GPL(soc_codec_dev_ad1980);
306
307MODULE_DESCRIPTION("ASoC ad1980 driver");
308MODULE_AUTHOR("Roy Huang, Cliff Cai");
309MODULE_LICENSE("GPL");
This page took 0.037766 seconds and 5 git commands to generate.