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