ad1980.c 8.0 KB

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