ad1980.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. * WARNING:
  15. *
  16. * Because Analog Devices Inc. discontinued the ad1980 sound chip since
  17. * Sep. 2009, this ad1980 driver is not maintained, tested and supported
  18. * by ADI now.
  19. */
  20. #include <linux/init.h>
  21. #include <linux/slab.h>
  22. #include <linux/module.h>
  23. #include <linux/kernel.h>
  24. #include <linux/device.h>
  25. #include <sound/core.h>
  26. #include <sound/pcm.h>
  27. #include <sound/ac97_codec.h>
  28. #include <sound/initval.h>
  29. #include <sound/soc.h>
  30. #include "ad1980.h"
  31. /*
  32. * AD1980 register cache
  33. */
  34. static const u16 ad1980_reg[] = {
  35. 0x0090, 0x8000, 0x8000, 0x8000, /* 0 - 6 */
  36. 0x0000, 0x0000, 0x8008, 0x8008, /* 8 - e */
  37. 0x8808, 0x8808, 0x0000, 0x8808, /* 10 - 16 */
  38. 0x8808, 0x0000, 0x8000, 0x0000, /* 18 - 1e */
  39. 0x0000, 0x0000, 0x0000, 0x0000, /* 20 - 26 */
  40. 0x03c7, 0x0000, 0xbb80, 0xbb80, /* 28 - 2e */
  41. 0xbb80, 0xbb80, 0x0000, 0x8080, /* 30 - 36 */
  42. 0x8080, 0x2000, 0x0000, 0x0000, /* 38 - 3e */
  43. 0x0000, 0x0000, 0x0000, 0x0000, /* reserved */
  44. 0x0000, 0x0000, 0x0000, 0x0000, /* reserved */
  45. 0x0000, 0x0000, 0x0000, 0x0000, /* reserved */
  46. 0x0000, 0x0000, 0x0000, 0x0000, /* reserved */
  47. 0x8080, 0x0000, 0x0000, 0x0000, /* 60 - 66 */
  48. 0x0000, 0x0000, 0x0000, 0x0000, /* reserved */
  49. 0x0000, 0x0000, 0x1001, 0x0000, /* 70 - 76 */
  50. 0x0000, 0x0000, 0x4144, 0x5370 /* 78 - 7e */
  51. };
  52. static const char *ad1980_rec_sel[] = {"Mic", "CD", "NC", "AUX", "Line",
  53. "Stereo Mix", "Mono Mix", "Phone"};
  54. static const struct soc_enum ad1980_cap_src =
  55. SOC_ENUM_DOUBLE(AC97_REC_SEL, 8, 0, 7, ad1980_rec_sel);
  56. static const struct snd_kcontrol_new ad1980_snd_ac97_controls[] = {
  57. SOC_DOUBLE("Master Playback Volume", AC97_MASTER, 8, 0, 31, 1),
  58. SOC_SINGLE("Master Playback Switch", AC97_MASTER, 15, 1, 1),
  59. SOC_DOUBLE("Headphone Playback Volume", AC97_HEADPHONE, 8, 0, 31, 1),
  60. SOC_SINGLE("Headphone Playback Switch", AC97_HEADPHONE, 15, 1, 1),
  61. SOC_DOUBLE("PCM Playback Volume", AC97_PCM, 8, 0, 31, 1),
  62. SOC_SINGLE("PCM Playback Switch", AC97_PCM, 15, 1, 1),
  63. SOC_DOUBLE("PCM Capture Volume", AC97_REC_GAIN, 8, 0, 31, 0),
  64. SOC_SINGLE("PCM Capture Switch", AC97_REC_GAIN, 15, 1, 1),
  65. SOC_SINGLE("Mono Playback Volume", AC97_MASTER_MONO, 0, 31, 1),
  66. SOC_SINGLE("Mono Playback Switch", AC97_MASTER_MONO, 15, 1, 1),
  67. SOC_SINGLE("Phone Capture Volume", AC97_PHONE, 0, 31, 1),
  68. SOC_SINGLE("Phone Capture Switch", AC97_PHONE, 15, 1, 1),
  69. SOC_SINGLE("Mic Volume", AC97_MIC, 0, 31, 1),
  70. SOC_SINGLE("Mic Switch", AC97_MIC, 15, 1, 1),
  71. SOC_SINGLE("Stereo Mic Switch", AC97_AD_MISC, 6, 1, 0),
  72. SOC_DOUBLE("Line HP Swap Switch", AC97_AD_MISC, 10, 5, 1, 0),
  73. SOC_DOUBLE("Surround Playback Volume", AC97_SURROUND_MASTER, 8, 0, 31, 1),
  74. SOC_DOUBLE("Surround Playback Switch", AC97_SURROUND_MASTER, 15, 7, 1, 1),
  75. SOC_DOUBLE("Center/LFE Playback Volume", AC97_CENTER_LFE_MASTER, 8, 0, 31, 1),
  76. SOC_DOUBLE("Center/LFE Playback Switch", AC97_CENTER_LFE_MASTER, 15, 7, 1, 1),
  77. SOC_ENUM("Capture Source", ad1980_cap_src),
  78. SOC_SINGLE("Mic Boost Switch", AC97_MIC, 6, 1, 0),
  79. };
  80. static unsigned int ac97_read(struct snd_soc_codec *codec,
  81. unsigned int reg)
  82. {
  83. u16 *cache = codec->reg_cache;
  84. switch (reg) {
  85. case AC97_RESET:
  86. case AC97_INT_PAGING:
  87. case AC97_POWERDOWN:
  88. case AC97_EXTENDED_STATUS:
  89. case AC97_VENDOR_ID1:
  90. case AC97_VENDOR_ID2:
  91. return soc_ac97_ops.read(codec->ac97, reg);
  92. default:
  93. reg = reg >> 1;
  94. if (reg >= ARRAY_SIZE(ad1980_reg))
  95. return -EINVAL;
  96. return cache[reg];
  97. }
  98. }
  99. static int ac97_write(struct snd_soc_codec *codec, unsigned int reg,
  100. unsigned int val)
  101. {
  102. u16 *cache = codec->reg_cache;
  103. soc_ac97_ops.write(codec->ac97, reg, val);
  104. reg = reg >> 1;
  105. if (reg < ARRAY_SIZE(ad1980_reg))
  106. cache[reg] = val;
  107. return 0;
  108. }
  109. static struct snd_soc_dai_driver ad1980_dai = {
  110. .name = "ad1980-hifi",
  111. .ac97_control = 1,
  112. .playback = {
  113. .stream_name = "Playback",
  114. .channels_min = 2,
  115. .channels_max = 6,
  116. .rates = SNDRV_PCM_RATE_48000,
  117. .formats = SND_SOC_STD_AC97_FMTS, },
  118. .capture = {
  119. .stream_name = "Capture",
  120. .channels_min = 2,
  121. .channels_max = 2,
  122. .rates = SNDRV_PCM_RATE_48000,
  123. .formats = SND_SOC_STD_AC97_FMTS, },
  124. };
  125. EXPORT_SYMBOL_GPL(ad1980_dai);
  126. static int ad1980_reset(struct snd_soc_codec *codec, int try_warm)
  127. {
  128. u16 retry_cnt = 0;
  129. retry:
  130. if (try_warm && soc_ac97_ops.warm_reset) {
  131. soc_ac97_ops.warm_reset(codec->ac97);
  132. if (ac97_read(codec, AC97_RESET) == 0x0090)
  133. return 1;
  134. }
  135. soc_ac97_ops.reset(codec->ac97);
  136. /* Set bit 16slot in register 74h, then every slot will has only 16
  137. * bits. This command is sent out in 20bit mode, in which case the
  138. * first nibble of data is eaten by the addr. (Tag is always 16 bit)*/
  139. ac97_write(codec, AC97_AD_SERIAL_CFG, 0x9900);
  140. if (ac97_read(codec, AC97_RESET) != 0x0090)
  141. goto err;
  142. return 0;
  143. err:
  144. while (retry_cnt++ < 10)
  145. goto retry;
  146. printk(KERN_ERR "AD1980 AC97 reset failed\n");
  147. return -EIO;
  148. }
  149. static int ad1980_soc_probe(struct snd_soc_codec *codec)
  150. {
  151. int ret;
  152. u16 vendor_id2;
  153. u16 ext_status;
  154. printk(KERN_INFO "AD1980 SoC Audio Codec\n");
  155. ret = snd_soc_new_ac97_codec(codec, &soc_ac97_ops, 0);
  156. if (ret < 0) {
  157. printk(KERN_ERR "ad1980: failed to register AC97 codec\n");
  158. return ret;
  159. }
  160. ret = ad1980_reset(codec, 0);
  161. if (ret < 0) {
  162. printk(KERN_ERR "Failed to reset AD1980: AC97 link error\n");
  163. goto reset_err;
  164. }
  165. /* Read out vendor ID to make sure it is ad1980 */
  166. if (ac97_read(codec, AC97_VENDOR_ID1) != 0x4144)
  167. goto reset_err;
  168. vendor_id2 = ac97_read(codec, AC97_VENDOR_ID2);
  169. if (vendor_id2 != 0x5370) {
  170. if (vendor_id2 != 0x5374)
  171. goto reset_err;
  172. else
  173. printk(KERN_WARNING "ad1980: "
  174. "Found AD1981 - only 2/2 IN/OUT Channels "
  175. "supported\n");
  176. }
  177. /* unmute captures and playbacks volume */
  178. ac97_write(codec, AC97_MASTER, 0x0000);
  179. ac97_write(codec, AC97_PCM, 0x0000);
  180. ac97_write(codec, AC97_REC_GAIN, 0x0000);
  181. ac97_write(codec, AC97_CENTER_LFE_MASTER, 0x0000);
  182. ac97_write(codec, AC97_SURROUND_MASTER, 0x0000);
  183. /*power on LFE/CENTER/Surround DACs*/
  184. ext_status = ac97_read(codec, AC97_EXTENDED_STATUS);
  185. ac97_write(codec, AC97_EXTENDED_STATUS, ext_status&~0x3800);
  186. snd_soc_add_controls(codec, ad1980_snd_ac97_controls,
  187. ARRAY_SIZE(ad1980_snd_ac97_controls));
  188. return 0;
  189. reset_err:
  190. snd_soc_free_ac97_codec(codec);
  191. return ret;
  192. }
  193. static int ad1980_soc_remove(struct snd_soc_codec *codec)
  194. {
  195. snd_soc_free_ac97_codec(codec);
  196. return 0;
  197. }
  198. static struct snd_soc_codec_driver soc_codec_dev_ad1980 = {
  199. .probe = ad1980_soc_probe,
  200. .remove = ad1980_soc_remove,
  201. .reg_cache_size = ARRAY_SIZE(ad1980_reg),
  202. .reg_word_size = sizeof(u16),
  203. .reg_cache_default = ad1980_reg,
  204. .reg_cache_step = 2,
  205. .write = ac97_write,
  206. .read = ac97_read,
  207. };
  208. static __devinit int ad1980_probe(struct platform_device *pdev)
  209. {
  210. return snd_soc_register_codec(&pdev->dev,
  211. &soc_codec_dev_ad1980, &ad1980_dai, 1);
  212. }
  213. static int __devexit ad1980_remove(struct platform_device *pdev)
  214. {
  215. snd_soc_unregister_codec(&pdev->dev);
  216. return 0;
  217. }
  218. static struct platform_driver ad1980_codec_driver = {
  219. .driver = {
  220. .name = "ad1980",
  221. .owner = THIS_MODULE,
  222. },
  223. .probe = ad1980_probe,
  224. .remove = __devexit_p(ad1980_remove),
  225. };
  226. static int __init ad1980_init(void)
  227. {
  228. return platform_driver_register(&ad1980_codec_driver);
  229. }
  230. module_init(ad1980_init);
  231. static void __exit ad1980_exit(void)
  232. {
  233. platform_driver_unregister(&ad1980_codec_driver);
  234. }
  235. module_exit(ad1980_exit);
  236. MODULE_DESCRIPTION("ASoC ad1980 driver (Obsolete)");
  237. MODULE_AUTHOR("Roy Huang, Cliff Cai");
  238. MODULE_LICENSE("GPL");