ad1980.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 <linux/regmap.h>
  26. #include <sound/core.h>
  27. #include <sound/pcm.h>
  28. #include <sound/ac97_codec.h>
  29. #include <sound/initval.h>
  30. #include <sound/soc.h>
  31. static const struct reg_default ad1980_reg_defaults[] = {
  32. { 0x02, 0x8000 },
  33. { 0x04, 0x8000 },
  34. { 0x06, 0x8000 },
  35. { 0x0c, 0x8008 },
  36. { 0x0e, 0x8008 },
  37. { 0x10, 0x8808 },
  38. { 0x12, 0x8808 },
  39. { 0x16, 0x8808 },
  40. { 0x18, 0x8808 },
  41. { 0x1a, 0x0000 },
  42. { 0x1c, 0x8000 },
  43. { 0x20, 0x0000 },
  44. { 0x28, 0x03c7 },
  45. { 0x2c, 0xbb80 },
  46. { 0x2e, 0xbb80 },
  47. { 0x30, 0xbb80 },
  48. { 0x32, 0xbb80 },
  49. { 0x36, 0x8080 },
  50. { 0x38, 0x8080 },
  51. { 0x3a, 0x2000 },
  52. { 0x60, 0x0000 },
  53. { 0x62, 0x0000 },
  54. { 0x72, 0x0000 },
  55. { 0x74, 0x1001 },
  56. { 0x76, 0x0000 },
  57. };
  58. static bool ad1980_readable_reg(struct device *dev, unsigned int reg)
  59. {
  60. switch (reg) {
  61. case AC97_RESET ... AC97_MASTER_MONO:
  62. case AC97_PHONE ... AC97_CD:
  63. case AC97_AUX ... AC97_GENERAL_PURPOSE:
  64. case AC97_POWERDOWN ... AC97_PCM_LR_ADC_RATE:
  65. case AC97_SPDIF:
  66. case AC97_CODEC_CLASS_REV:
  67. case AC97_PCI_SVID:
  68. case AC97_AD_CODEC_CFG:
  69. case AC97_AD_JACK_SPDIF:
  70. case AC97_AD_SERIAL_CFG:
  71. case AC97_VENDOR_ID1:
  72. case AC97_VENDOR_ID2:
  73. return true;
  74. default:
  75. return false;
  76. }
  77. }
  78. static bool ad1980_writeable_reg(struct device *dev, unsigned int reg)
  79. {
  80. switch (reg) {
  81. case AC97_VENDOR_ID1:
  82. case AC97_VENDOR_ID2:
  83. return false;
  84. default:
  85. return ad1980_readable_reg(dev, reg);
  86. }
  87. }
  88. static const struct regmap_config ad1980_regmap_config = {
  89. .reg_bits = 16,
  90. .reg_stride = 2,
  91. .val_bits = 16,
  92. .max_register = 0x7e,
  93. .cache_type = REGCACHE_RBTREE,
  94. .volatile_reg = regmap_ac97_default_volatile,
  95. .readable_reg = ad1980_readable_reg,
  96. .writeable_reg = ad1980_writeable_reg,
  97. .reg_defaults = ad1980_reg_defaults,
  98. .num_reg_defaults = ARRAY_SIZE(ad1980_reg_defaults),
  99. };
  100. static const char *ad1980_rec_sel[] = {"Mic", "CD", "NC", "AUX", "Line",
  101. "Stereo Mix", "Mono Mix", "Phone"};
  102. static SOC_ENUM_DOUBLE_DECL(ad1980_cap_src,
  103. AC97_REC_SEL, 8, 0, ad1980_rec_sel);
  104. static const struct snd_kcontrol_new ad1980_snd_ac97_controls[] = {
  105. SOC_DOUBLE("Master Playback Volume", AC97_MASTER, 8, 0, 31, 1),
  106. SOC_SINGLE("Master Playback Switch", AC97_MASTER, 15, 1, 1),
  107. SOC_DOUBLE("Headphone Playback Volume", AC97_HEADPHONE, 8, 0, 31, 1),
  108. SOC_SINGLE("Headphone Playback Switch", AC97_HEADPHONE, 15, 1, 1),
  109. SOC_DOUBLE("PCM Playback Volume", AC97_PCM, 8, 0, 31, 1),
  110. SOC_SINGLE("PCM Playback Switch", AC97_PCM, 15, 1, 1),
  111. SOC_DOUBLE("PCM Capture Volume", AC97_REC_GAIN, 8, 0, 31, 0),
  112. SOC_SINGLE("PCM Capture Switch", AC97_REC_GAIN, 15, 1, 1),
  113. SOC_SINGLE("Mono Playback Volume", AC97_MASTER_MONO, 0, 31, 1),
  114. SOC_SINGLE("Mono Playback Switch", AC97_MASTER_MONO, 15, 1, 1),
  115. SOC_SINGLE("Phone Capture Volume", AC97_PHONE, 0, 31, 1),
  116. SOC_SINGLE("Phone Capture Switch", AC97_PHONE, 15, 1, 1),
  117. SOC_SINGLE("Mic Volume", AC97_MIC, 0, 31, 1),
  118. SOC_SINGLE("Mic Switch", AC97_MIC, 15, 1, 1),
  119. SOC_SINGLE("Stereo Mic Switch", AC97_AD_MISC, 6, 1, 0),
  120. SOC_DOUBLE("Line HP Swap Switch", AC97_AD_MISC, 10, 5, 1, 0),
  121. SOC_DOUBLE("Surround Playback Volume", AC97_SURROUND_MASTER, 8, 0, 31, 1),
  122. SOC_DOUBLE("Surround Playback Switch", AC97_SURROUND_MASTER, 15, 7, 1, 1),
  123. SOC_DOUBLE("Center/LFE Playback Volume", AC97_CENTER_LFE_MASTER, 8, 0, 31, 1),
  124. SOC_DOUBLE("Center/LFE Playback Switch", AC97_CENTER_LFE_MASTER, 15, 7, 1, 1),
  125. SOC_ENUM("Capture Source", ad1980_cap_src),
  126. SOC_SINGLE("Mic Boost Switch", AC97_MIC, 6, 1, 0),
  127. };
  128. static const struct snd_soc_dapm_widget ad1980_dapm_widgets[] = {
  129. SND_SOC_DAPM_INPUT("MIC1"),
  130. SND_SOC_DAPM_INPUT("MIC2"),
  131. SND_SOC_DAPM_INPUT("CD_L"),
  132. SND_SOC_DAPM_INPUT("CD_R"),
  133. SND_SOC_DAPM_INPUT("AUX_L"),
  134. SND_SOC_DAPM_INPUT("AUX_R"),
  135. SND_SOC_DAPM_INPUT("LINE_IN_L"),
  136. SND_SOC_DAPM_INPUT("LINE_IN_R"),
  137. SND_SOC_DAPM_OUTPUT("LFE_OUT"),
  138. SND_SOC_DAPM_OUTPUT("CENTER_OUT"),
  139. SND_SOC_DAPM_OUTPUT("LINE_OUT_L"),
  140. SND_SOC_DAPM_OUTPUT("LINE_OUT_R"),
  141. SND_SOC_DAPM_OUTPUT("MONO_OUT"),
  142. SND_SOC_DAPM_OUTPUT("HP_OUT_L"),
  143. SND_SOC_DAPM_OUTPUT("HP_OUT_R"),
  144. };
  145. static const struct snd_soc_dapm_route ad1980_dapm_routes[] = {
  146. { "Capture", NULL, "MIC1" },
  147. { "Capture", NULL, "MIC2" },
  148. { "Capture", NULL, "CD_L" },
  149. { "Capture", NULL, "CD_R" },
  150. { "Capture", NULL, "AUX_L" },
  151. { "Capture", NULL, "AUX_R" },
  152. { "Capture", NULL, "LINE_IN_L" },
  153. { "Capture", NULL, "LINE_IN_R" },
  154. { "LFE_OUT", NULL, "Playback" },
  155. { "CENTER_OUT", NULL, "Playback" },
  156. { "LINE_OUT_L", NULL, "Playback" },
  157. { "LINE_OUT_R", NULL, "Playback" },
  158. { "MONO_OUT", NULL, "Playback" },
  159. { "HP_OUT_L", NULL, "Playback" },
  160. { "HP_OUT_R", NULL, "Playback" },
  161. };
  162. static struct snd_soc_dai_driver ad1980_dai = {
  163. .name = "ad1980-hifi",
  164. .playback = {
  165. .stream_name = "Playback",
  166. .channels_min = 2,
  167. .channels_max = 6,
  168. .rates = SNDRV_PCM_RATE_48000,
  169. .formats = SND_SOC_STD_AC97_FMTS, },
  170. .capture = {
  171. .stream_name = "Capture",
  172. .channels_min = 2,
  173. .channels_max = 2,
  174. .rates = SNDRV_PCM_RATE_48000,
  175. .formats = SND_SOC_STD_AC97_FMTS, },
  176. };
  177. #define AD1980_VENDOR_ID 0x41445300
  178. #define AD1980_VENDOR_MASK 0xffffff00
  179. static int ad1980_reset(struct snd_soc_codec *codec, int try_warm)
  180. {
  181. struct snd_ac97 *ac97 = snd_soc_codec_get_drvdata(codec);
  182. unsigned int retry_cnt = 0;
  183. int ret;
  184. do {
  185. ret = snd_ac97_reset(ac97, true, AD1980_VENDOR_ID,
  186. AD1980_VENDOR_MASK);
  187. if (ret >= 0)
  188. return 0;
  189. /*
  190. * Set bit 16slot in register 74h, then every slot will has only
  191. * 16 bits. This command is sent out in 20bit mode, in which
  192. * case the first nibble of data is eaten by the addr. (Tag is
  193. * always 16 bit)
  194. */
  195. snd_soc_write(codec, AC97_AD_SERIAL_CFG, 0x9900);
  196. } while (retry_cnt++ < 10);
  197. dev_err(codec->dev, "Failed to reset: AC97 link error\n");
  198. return -EIO;
  199. }
  200. static int ad1980_soc_probe(struct snd_soc_codec *codec)
  201. {
  202. struct snd_ac97 *ac97;
  203. struct regmap *regmap;
  204. int ret;
  205. u16 vendor_id2;
  206. u16 ext_status;
  207. ac97 = snd_soc_new_ac97_codec(codec, 0, 0);
  208. if (IS_ERR(ac97)) {
  209. ret = PTR_ERR(ac97);
  210. dev_err(codec->dev, "Failed to register AC97 codec: %d\n", ret);
  211. return ret;
  212. }
  213. regmap = regmap_init_ac97(ac97, &ad1980_regmap_config);
  214. if (IS_ERR(regmap)) {
  215. ret = PTR_ERR(regmap);
  216. goto err_free_ac97;
  217. }
  218. snd_soc_codec_init_regmap(codec, regmap);
  219. snd_soc_codec_set_drvdata(codec, ac97);
  220. ret = ad1980_reset(codec, 0);
  221. if (ret < 0)
  222. goto reset_err;
  223. vendor_id2 = snd_soc_read(codec, AC97_VENDOR_ID2);
  224. if (vendor_id2 == 0x5374) {
  225. dev_warn(codec->dev,
  226. "Found AD1981 - only 2/2 IN/OUT Channels supported\n");
  227. }
  228. /* unmute captures and playbacks volume */
  229. snd_soc_write(codec, AC97_MASTER, 0x0000);
  230. snd_soc_write(codec, AC97_PCM, 0x0000);
  231. snd_soc_write(codec, AC97_REC_GAIN, 0x0000);
  232. snd_soc_write(codec, AC97_CENTER_LFE_MASTER, 0x0000);
  233. snd_soc_write(codec, AC97_SURROUND_MASTER, 0x0000);
  234. /*power on LFE/CENTER/Surround DACs*/
  235. ext_status = snd_soc_read(codec, AC97_EXTENDED_STATUS);
  236. snd_soc_write(codec, AC97_EXTENDED_STATUS, ext_status&~0x3800);
  237. return 0;
  238. reset_err:
  239. snd_soc_codec_exit_regmap(codec);
  240. err_free_ac97:
  241. snd_soc_free_ac97_codec(ac97);
  242. return ret;
  243. }
  244. static int ad1980_soc_remove(struct snd_soc_codec *codec)
  245. {
  246. struct snd_ac97 *ac97 = snd_soc_codec_get_drvdata(codec);
  247. snd_soc_codec_exit_regmap(codec);
  248. snd_soc_free_ac97_codec(ac97);
  249. return 0;
  250. }
  251. static struct snd_soc_codec_driver soc_codec_dev_ad1980 = {
  252. .probe = ad1980_soc_probe,
  253. .remove = ad1980_soc_remove,
  254. .component_driver = {
  255. .controls = ad1980_snd_ac97_controls,
  256. .num_controls = ARRAY_SIZE(ad1980_snd_ac97_controls),
  257. .dapm_widgets = ad1980_dapm_widgets,
  258. .num_dapm_widgets = ARRAY_SIZE(ad1980_dapm_widgets),
  259. .dapm_routes = ad1980_dapm_routes,
  260. .num_dapm_routes = ARRAY_SIZE(ad1980_dapm_routes),
  261. },
  262. };
  263. static int ad1980_probe(struct platform_device *pdev)
  264. {
  265. return snd_soc_register_codec(&pdev->dev,
  266. &soc_codec_dev_ad1980, &ad1980_dai, 1);
  267. }
  268. static int ad1980_remove(struct platform_device *pdev)
  269. {
  270. snd_soc_unregister_codec(&pdev->dev);
  271. return 0;
  272. }
  273. static struct platform_driver ad1980_codec_driver = {
  274. .driver = {
  275. .name = "ad1980",
  276. },
  277. .probe = ad1980_probe,
  278. .remove = ad1980_remove,
  279. };
  280. module_platform_driver(ad1980_codec_driver);
  281. MODULE_DESCRIPTION("ASoC ad1980 driver (Obsolete)");
  282. MODULE_AUTHOR("Roy Huang, Cliff Cai");
  283. MODULE_LICENSE("GPL");