ad1980.c 8.7 KB

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