ad1980.c 8.2 KB

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