blob: 304d3003339a63bcedc4db0b3de821edd133ce05 [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
Sonic Zhang2e2211a2010-08-07 12:06:07 -040014/*
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
Cliff Cai5f57dc82008-07-29 11:42:34 +010022#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Cliff Cai5f57dc82008-07-29 11:42:34 +010024#include <linux/module.h>
Cliff Cai5f57dc82008-07-29 11:42:34 +010025#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>
Cliff Cai5f57dc82008-07-29 11:42:34 +010032
33#include "ad1980.h"
34
Cliff Cai5f57dc82008-07-29 11:42:34 +010035/*
36 * AD1980 register cache
37 */
38static 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
57static const char *ad1980_rec_sel[] = {"Mic", "CD", "NC", "AUX", "Line",
58 "Stereo Mix", "Mono Mix", "Phone"};
59
Takashi Iwai901bb6c2014-02-18 09:56:40 +010060static SOC_ENUM_DOUBLE_DECL(ad1980_cap_src,
61 AC97_REC_SEL, 8, 0, ad1980_rec_sel);
Cliff Cai5f57dc82008-07-29 11:42:34 +010062
63static const struct snd_kcontrol_new ad1980_snd_ac97_controls[] = {
64SOC_DOUBLE("Master Playback Volume", AC97_MASTER, 8, 0, 31, 1),
65SOC_SINGLE("Master Playback Switch", AC97_MASTER, 15, 1, 1),
66
67SOC_DOUBLE("Headphone Playback Volume", AC97_HEADPHONE, 8, 0, 31, 1),
68SOC_SINGLE("Headphone Playback Switch", AC97_HEADPHONE, 15, 1, 1),
69
70SOC_DOUBLE("PCM Playback Volume", AC97_PCM, 8, 0, 31, 1),
71SOC_SINGLE("PCM Playback Switch", AC97_PCM, 15, 1, 1),
72
73SOC_DOUBLE("PCM Capture Volume", AC97_REC_GAIN, 8, 0, 31, 0),
74SOC_SINGLE("PCM Capture Switch", AC97_REC_GAIN, 15, 1, 1),
75
76SOC_SINGLE("Mono Playback Volume", AC97_MASTER_MONO, 0, 31, 1),
77SOC_SINGLE("Mono Playback Switch", AC97_MASTER_MONO, 15, 1, 1),
78
79SOC_SINGLE("Phone Capture Volume", AC97_PHONE, 0, 31, 1),
80SOC_SINGLE("Phone Capture Switch", AC97_PHONE, 15, 1, 1),
81
82SOC_SINGLE("Mic Volume", AC97_MIC, 0, 31, 1),
83SOC_SINGLE("Mic Switch", AC97_MIC, 15, 1, 1),
84
85SOC_SINGLE("Stereo Mic Switch", AC97_AD_MISC, 6, 1, 0),
86SOC_DOUBLE("Line HP Swap Switch", AC97_AD_MISC, 10, 5, 1, 0),
87
88SOC_DOUBLE("Surround Playback Volume", AC97_SURROUND_MASTER, 8, 0, 31, 1),
89SOC_DOUBLE("Surround Playback Switch", AC97_SURROUND_MASTER, 15, 7, 1, 1),
90
Cliff Cai9905ed32008-11-18 16:18:16 +080091SOC_DOUBLE("Center/LFE Playback Volume", AC97_CENTER_LFE_MASTER, 8, 0, 31, 1),
92SOC_DOUBLE("Center/LFE Playback Switch", AC97_CENTER_LFE_MASTER, 15, 7, 1, 1),
93
Cliff Cai5f57dc82008-07-29 11:42:34 +010094SOC_ENUM("Capture Source", ad1980_cap_src),
95
96SOC_SINGLE("Mic Boost Switch", AC97_MIC, 6, 1, 0),
97};
98
Mark Brown9e7e4742013-08-06 23:51:12 +010099static const struct snd_soc_dapm_widget ad1980_dapm_widgets[] = {
100SND_SOC_DAPM_INPUT("MIC1"),
101SND_SOC_DAPM_INPUT("MIC2"),
102SND_SOC_DAPM_INPUT("CD_L"),
103SND_SOC_DAPM_INPUT("CD_R"),
104SND_SOC_DAPM_INPUT("AUX_L"),
105SND_SOC_DAPM_INPUT("AUX_R"),
106SND_SOC_DAPM_INPUT("LINE_IN_L"),
107SND_SOC_DAPM_INPUT("LINE_IN_R"),
108
109SND_SOC_DAPM_OUTPUT("LFE_OUT"),
110SND_SOC_DAPM_OUTPUT("CENTER_OUT"),
111SND_SOC_DAPM_OUTPUT("LINE_OUT_L"),
112SND_SOC_DAPM_OUTPUT("LINE_OUT_R"),
113SND_SOC_DAPM_OUTPUT("MONO_OUT"),
114SND_SOC_DAPM_OUTPUT("HP_OUT_L"),
115SND_SOC_DAPM_OUTPUT("HP_OUT_R"),
116};
117
118static const struct snd_soc_dapm_route ad1980_dapm_routes[] = {
119 { "Capture", NULL, "MIC1" },
120 { "Capture", NULL, "MIC2" },
121 { "Capture", NULL, "CD_L" },
122 { "Capture", NULL, "CD_R" },
123 { "Capture", NULL, "AUX_L" },
124 { "Capture", NULL, "AUX_R" },
125 { "Capture", NULL, "LINE_IN_L" },
126 { "Capture", NULL, "LINE_IN_R" },
127
128 { "LFE_OUT", NULL, "Playback" },
129 { "CENTER_OUT", NULL, "Playback" },
130 { "LINE_OUT_L", NULL, "Playback" },
131 { "LINE_OUT_R", NULL, "Playback" },
132 { "MONO_OUT", NULL, "Playback" },
133 { "HP_OUT_L", NULL, "Playback" },
134 { "HP_OUT_R", NULL, "Playback" },
135};
136
Cliff Cai5f57dc82008-07-29 11:42:34 +0100137static unsigned int ac97_read(struct snd_soc_codec *codec,
138 unsigned int reg)
139{
140 u16 *cache = codec->reg_cache;
141
142 switch (reg) {
143 case AC97_RESET:
144 case AC97_INT_PAGING:
145 case AC97_POWERDOWN:
146 case AC97_EXTENDED_STATUS:
147 case AC97_VENDOR_ID1:
148 case AC97_VENDOR_ID2:
Mark Brownb047e1c2013-06-26 12:45:59 +0100149 return soc_ac97_ops->read(codec->ac97, reg);
Cliff Cai5f57dc82008-07-29 11:42:34 +0100150 default:
151 reg = reg >> 1;
152
Ian Molton91432e92009-01-17 17:44:23 +0000153 if (reg >= ARRAY_SIZE(ad1980_reg))
Cliff Cai5f57dc82008-07-29 11:42:34 +0100154 return -EINVAL;
155
156 return cache[reg];
157 }
158}
159
160static int ac97_write(struct snd_soc_codec *codec, unsigned int reg,
161 unsigned int val)
162{
163 u16 *cache = codec->reg_cache;
164
Mark Brownb047e1c2013-06-26 12:45:59 +0100165 soc_ac97_ops->write(codec->ac97, reg, val);
Cliff Cai5f57dc82008-07-29 11:42:34 +0100166 reg = reg >> 1;
Ian Molton91432e92009-01-17 17:44:23 +0000167 if (reg < ARRAY_SIZE(ad1980_reg))
Cliff Cai5f57dc82008-07-29 11:42:34 +0100168 cache[reg] = val;
169
170 return 0;
171}
172
Mark Brownde535a52010-10-06 16:20:07 -0700173static struct snd_soc_dai_driver ad1980_dai = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000174 .name = "ad1980-hifi",
Mark Brown67c91512008-11-24 17:45:26 +0000175 .ac97_control = 1,
Cliff Cai5f57dc82008-07-29 11:42:34 +0100176 .playback = {
177 .stream_name = "Playback",
178 .channels_min = 2,
Cliff Cai9905ed32008-11-18 16:18:16 +0800179 .channels_max = 6,
Cliff Cai5f57dc82008-07-29 11:42:34 +0100180 .rates = SNDRV_PCM_RATE_48000,
Mark Brown33f503c2009-05-02 12:24:55 +0100181 .formats = SND_SOC_STD_AC97_FMTS, },
Cliff Cai5f57dc82008-07-29 11:42:34 +0100182 .capture = {
183 .stream_name = "Capture",
184 .channels_min = 2,
185 .channels_max = 2,
186 .rates = SNDRV_PCM_RATE_48000,
Mark Brown33f503c2009-05-02 12:24:55 +0100187 .formats = SND_SOC_STD_AC97_FMTS, },
Cliff Cai5f57dc82008-07-29 11:42:34 +0100188};
Cliff Cai5f57dc82008-07-29 11:42:34 +0100189
190static int ad1980_reset(struct snd_soc_codec *codec, int try_warm)
191{
Lars-Peter Clausencf7b71f42014-05-20 14:23:10 +0200192 unsigned int retry_cnt = 0;
Cliff Cai5f57dc82008-07-29 11:42:34 +0100193
Lars-Peter Clausencf7b71f42014-05-20 14:23:10 +0200194 do {
195 if (try_warm && soc_ac97_ops->warm_reset) {
196 soc_ac97_ops->warm_reset(codec->ac97);
197 if (ac97_read(codec, AC97_RESET) == 0x0090)
198 return 1;
199 }
Cliff Cai5f57dc82008-07-29 11:42:34 +0100200
Lars-Peter Clausencf7b71f42014-05-20 14:23:10 +0200201 soc_ac97_ops->reset(codec->ac97);
202 /*
203 * Set bit 16slot in register 74h, then every slot will has only
204 * 16 bits. This command is sent out in 20bit mode, in which
205 * case the first nibble of data is eaten by the addr. (Tag is
206 * always 16 bit)
207 */
208 ac97_write(codec, AC97_AD_SERIAL_CFG, 0x9900);
Cliff Cai5f57dc82008-07-29 11:42:34 +0100209
Lars-Peter Clausencf7b71f42014-05-20 14:23:10 +0200210 if (ac97_read(codec, AC97_RESET) == 0x0090)
211 return 0;
212 } while (retry_cnt++ < 10);
Cliff Cai5f57dc82008-07-29 11:42:34 +0100213
214 printk(KERN_ERR "AD1980 AC97 reset failed\n");
215 return -EIO;
216}
217
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000218static int ad1980_soc_probe(struct snd_soc_codec *codec)
Cliff Cai5f57dc82008-07-29 11:42:34 +0100219{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000220 int ret;
Cliff Cai5f57dc82008-07-29 11:42:34 +0100221 u16 vendor_id2;
Cliff Cai9905ed32008-11-18 16:18:16 +0800222 u16 ext_status;
Cliff Cai5f57dc82008-07-29 11:42:34 +0100223
224 printk(KERN_INFO "AD1980 SoC Audio Codec\n");
225
Mark Brownb047e1c2013-06-26 12:45:59 +0100226 ret = snd_soc_new_ac97_codec(codec, soc_ac97_ops, 0);
Cliff Cai5f57dc82008-07-29 11:42:34 +0100227 if (ret < 0) {
228 printk(KERN_ERR "ad1980: failed to register AC97 codec\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000229 return ret;
Cliff Cai5f57dc82008-07-29 11:42:34 +0100230 }
231
Cliff Cai5f57dc82008-07-29 11:42:34 +0100232 ret = ad1980_reset(codec, 0);
233 if (ret < 0) {
Mark Brown39639fa2008-11-21 14:28:49 +0000234 printk(KERN_ERR "Failed to reset AD1980: AC97 link error\n");
Cliff Cai5f57dc82008-07-29 11:42:34 +0100235 goto reset_err;
236 }
237
238 /* Read out vendor ID to make sure it is ad1980 */
Axel Lin0f736442011-09-06 10:37:48 +0800239 if (ac97_read(codec, AC97_VENDOR_ID1) != 0x4144) {
240 ret = -ENODEV;
Cliff Cai5f57dc82008-07-29 11:42:34 +0100241 goto reset_err;
Axel Lin0f736442011-09-06 10:37:48 +0800242 }
Cliff Cai5f57dc82008-07-29 11:42:34 +0100243
244 vendor_id2 = ac97_read(codec, AC97_VENDOR_ID2);
245
246 if (vendor_id2 != 0x5370) {
Axel Lin0f736442011-09-06 10:37:48 +0800247 if (vendor_id2 != 0x5374) {
248 ret = -ENODEV;
Cliff Cai5f57dc82008-07-29 11:42:34 +0100249 goto reset_err;
Axel Lin0f736442011-09-06 10:37:48 +0800250 } else {
Cliff Cai5f57dc82008-07-29 11:42:34 +0100251 printk(KERN_WARNING "ad1980: "
252 "Found AD1981 - only 2/2 IN/OUT Channels "
253 "supported\n");
Axel Lin0f736442011-09-06 10:37:48 +0800254 }
Cliff Cai5f57dc82008-07-29 11:42:34 +0100255 }
256
Cliff Cai9905ed32008-11-18 16:18:16 +0800257 /* unmute captures and playbacks volume */
258 ac97_write(codec, AC97_MASTER, 0x0000);
259 ac97_write(codec, AC97_PCM, 0x0000);
260 ac97_write(codec, AC97_REC_GAIN, 0x0000);
261 ac97_write(codec, AC97_CENTER_LFE_MASTER, 0x0000);
262 ac97_write(codec, AC97_SURROUND_MASTER, 0x0000);
263
264 /*power on LFE/CENTER/Surround DACs*/
265 ext_status = ac97_read(codec, AC97_EXTENDED_STATUS);
266 ac97_write(codec, AC97_EXTENDED_STATUS, ext_status&~0x3800);
Cliff Cai5f57dc82008-07-29 11:42:34 +0100267
Liam Girdwood022658b2012-02-03 17:43:09 +0000268 snd_soc_add_codec_controls(codec, ad1980_snd_ac97_controls,
Ian Molton3e8e1952009-01-09 00:23:21 +0000269 ARRAY_SIZE(ad1980_snd_ac97_controls));
Cliff Cai5f57dc82008-07-29 11:42:34 +0100270
271 return 0;
272
273reset_err:
Cliff Cai5f57dc82008-07-29 11:42:34 +0100274 snd_soc_free_ac97_codec(codec);
Cliff Cai5f57dc82008-07-29 11:42:34 +0100275 return ret;
276}
277
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000278static int ad1980_soc_remove(struct snd_soc_codec *codec)
Cliff Cai5f57dc82008-07-29 11:42:34 +0100279{
Cliff Cai5f57dc82008-07-29 11:42:34 +0100280 snd_soc_free_ac97_codec(codec);
Cliff Cai5f57dc82008-07-29 11:42:34 +0100281 return 0;
282}
283
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000284static struct snd_soc_codec_driver soc_codec_dev_ad1980 = {
Cliff Cai5f57dc82008-07-29 11:42:34 +0100285 .probe = ad1980_soc_probe,
286 .remove = ad1980_soc_remove,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000287 .reg_cache_size = ARRAY_SIZE(ad1980_reg),
288 .reg_word_size = sizeof(u16),
Axel Linc7bad062010-09-09 17:50:56 +0800289 .reg_cache_default = ad1980_reg,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000290 .reg_cache_step = 2,
291 .write = ac97_write,
292 .read = ac97_read,
Mark Brown9e7e4742013-08-06 23:51:12 +0100293
294 .dapm_widgets = ad1980_dapm_widgets,
295 .num_dapm_widgets = ARRAY_SIZE(ad1980_dapm_widgets),
296 .dapm_routes = ad1980_dapm_routes,
297 .num_dapm_routes = ARRAY_SIZE(ad1980_dapm_routes),
Cliff Cai5f57dc82008-07-29 11:42:34 +0100298};
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000299
Bill Pemberton7a79e942012-12-07 09:26:37 -0500300static int ad1980_probe(struct platform_device *pdev)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000301{
302 return snd_soc_register_codec(&pdev->dev,
303 &soc_codec_dev_ad1980, &ad1980_dai, 1);
304}
305
Bill Pemberton7a79e942012-12-07 09:26:37 -0500306static int ad1980_remove(struct platform_device *pdev)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000307{
308 snd_soc_unregister_codec(&pdev->dev);
309 return 0;
310}
311
312static struct platform_driver ad1980_codec_driver = {
313 .driver = {
Mike Frysinger1afa98b2011-03-27 00:44:13 -0400314 .name = "ad1980",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000315 .owner = THIS_MODULE,
316 },
317
318 .probe = ad1980_probe,
Bill Pemberton7a79e942012-12-07 09:26:37 -0500319 .remove = ad1980_remove,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000320};
321
Mark Brown5bbcc3c2011-11-23 22:52:08 +0000322module_platform_driver(ad1980_codec_driver);
Cliff Cai5f57dc82008-07-29 11:42:34 +0100323
Sonic Zhang2e2211a2010-08-07 12:06:07 -0400324MODULE_DESCRIPTION("ASoC ad1980 driver (Obsolete)");
Cliff Cai5f57dc82008-07-29 11:42:34 +0100325MODULE_AUTHOR("Roy Huang, Cliff Cai");
326MODULE_LICENSE("GPL");