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