stac9766.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /*
  2. * stac9766.c -- ALSA SoC STAC9766 codec support
  3. *
  4. * Copyright 2009 Jon Smirl, Digispeaker
  5. * Author: Jon Smirl <jonsmirl@gmail.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2 of the License, or (at your
  10. * option) any later version.
  11. *
  12. * Features:-
  13. *
  14. * o Support for AC97 Codec, S/PDIF
  15. */
  16. #include <linux/init.h>
  17. #include <linux/slab.h>
  18. #include <linux/module.h>
  19. #include <linux/device.h>
  20. #include <sound/core.h>
  21. #include <sound/pcm.h>
  22. #include <sound/ac97_codec.h>
  23. #include <sound/initval.h>
  24. #include <sound/pcm_params.h>
  25. #include <sound/soc.h>
  26. #include <sound/tlv.h>
  27. #include "stac9766.h"
  28. #define STAC9766_VERSION "0.10"
  29. /*
  30. * STAC9766 register cache
  31. */
  32. static const u16 stac9766_reg[] = {
  33. 0x6A90, 0x8000, 0x8000, 0x8000, /* 6 */
  34. 0x0000, 0x0000, 0x8008, 0x8008, /* e */
  35. 0x8808, 0x8808, 0x8808, 0x8808, /* 16 */
  36. 0x8808, 0x0000, 0x8000, 0x0000, /* 1e */
  37. 0x0000, 0x0000, 0x0000, 0x000f, /* 26 */
  38. 0x0a05, 0x0400, 0xbb80, 0x0000, /* 2e */
  39. 0x0000, 0xbb80, 0x0000, 0x0000, /* 36 */
  40. 0x0000, 0x2000, 0x0000, 0x0100, /* 3e */
  41. 0x0000, 0x0000, 0x0080, 0x0000, /* 46 */
  42. 0x0000, 0x0000, 0x0003, 0xffff, /* 4e */
  43. 0x0000, 0x0000, 0x0000, 0x0000, /* 56 */
  44. 0x4000, 0x0000, 0x0000, 0x0000, /* 5e */
  45. 0x1201, 0xFFFF, 0xFFFF, 0x0000, /* 66 */
  46. 0x0000, 0x0000, 0x0000, 0x0000, /* 6e */
  47. 0x0000, 0x0000, 0x0000, 0x0006, /* 76 */
  48. 0x0000, 0x0000, 0x0000, 0x0000, /* 7e */
  49. };
  50. static const char *stac9766_record_mux[] = {"Mic", "CD", "Video", "AUX",
  51. "Line", "Stereo Mix", "Mono Mix", "Phone"};
  52. static const char *stac9766_mono_mux[] = {"Mix", "Mic"};
  53. static const char *stac9766_mic_mux[] = {"Mic1", "Mic2"};
  54. static const char *stac9766_SPDIF_mux[] = {"PCM", "ADC Record"};
  55. static const char *stac9766_popbypass_mux[] = {"Normal", "Bypass Mixer"};
  56. static const char *stac9766_record_all_mux[] = {"All analog",
  57. "Analog plus DAC"};
  58. static const char *stac9766_boost1[] = {"0dB", "10dB"};
  59. static const char *stac9766_boost2[] = {"0dB", "20dB"};
  60. static const char *stac9766_stereo_mic[] = {"Off", "On"};
  61. static const struct soc_enum stac9766_record_enum =
  62. SOC_ENUM_DOUBLE(AC97_REC_SEL, 8, 0, 8, stac9766_record_mux);
  63. static const struct soc_enum stac9766_mono_enum =
  64. SOC_ENUM_SINGLE(AC97_GENERAL_PURPOSE, 9, 2, stac9766_mono_mux);
  65. static const struct soc_enum stac9766_mic_enum =
  66. SOC_ENUM_SINGLE(AC97_GENERAL_PURPOSE, 8, 2, stac9766_mic_mux);
  67. static const struct soc_enum stac9766_SPDIF_enum =
  68. SOC_ENUM_SINGLE(AC97_STAC_DA_CONTROL, 1, 2, stac9766_SPDIF_mux);
  69. static const struct soc_enum stac9766_popbypass_enum =
  70. SOC_ENUM_SINGLE(AC97_GENERAL_PURPOSE, 15, 2, stac9766_popbypass_mux);
  71. static const struct soc_enum stac9766_record_all_enum =
  72. SOC_ENUM_SINGLE(AC97_STAC_ANALOG_SPECIAL, 12, 2,
  73. stac9766_record_all_mux);
  74. static const struct soc_enum stac9766_boost1_enum =
  75. SOC_ENUM_SINGLE(AC97_MIC, 6, 2, stac9766_boost1); /* 0/10dB */
  76. static const struct soc_enum stac9766_boost2_enum =
  77. SOC_ENUM_SINGLE(AC97_STAC_ANALOG_SPECIAL, 2, 2, stac9766_boost2); /* 0/20dB */
  78. static const struct soc_enum stac9766_stereo_mic_enum =
  79. SOC_ENUM_SINGLE(AC97_STAC_STEREO_MIC, 2, 1, stac9766_stereo_mic);
  80. static const DECLARE_TLV_DB_LINEAR(master_tlv, -4600, 0);
  81. static const DECLARE_TLV_DB_LINEAR(record_tlv, 0, 2250);
  82. static const DECLARE_TLV_DB_LINEAR(beep_tlv, -4500, 0);
  83. static const DECLARE_TLV_DB_LINEAR(mix_tlv, -3450, 1200);
  84. static const struct snd_kcontrol_new stac9766_snd_ac97_controls[] = {
  85. SOC_DOUBLE_TLV("Speaker Volume", AC97_MASTER, 8, 0, 31, 1, master_tlv),
  86. SOC_SINGLE("Speaker Switch", AC97_MASTER, 15, 1, 1),
  87. SOC_DOUBLE_TLV("Headphone Volume", AC97_HEADPHONE, 8, 0, 31, 1,
  88. master_tlv),
  89. SOC_SINGLE("Headphone Switch", AC97_HEADPHONE, 15, 1, 1),
  90. SOC_SINGLE_TLV("Mono Out Volume", AC97_MASTER_MONO, 0, 31, 1,
  91. master_tlv),
  92. SOC_SINGLE("Mono Out Switch", AC97_MASTER_MONO, 15, 1, 1),
  93. SOC_DOUBLE_TLV("Record Volume", AC97_REC_GAIN, 8, 0, 15, 0, record_tlv),
  94. SOC_SINGLE("Record Switch", AC97_REC_GAIN, 15, 1, 1),
  95. SOC_SINGLE_TLV("Beep Volume", AC97_PC_BEEP, 1, 15, 1, beep_tlv),
  96. SOC_SINGLE("Beep Switch", AC97_PC_BEEP, 15, 1, 1),
  97. SOC_SINGLE("Beep Frequency", AC97_PC_BEEP, 5, 127, 1),
  98. SOC_SINGLE_TLV("Phone Volume", AC97_PHONE, 0, 31, 1, mix_tlv),
  99. SOC_SINGLE("Phone Switch", AC97_PHONE, 15, 1, 1),
  100. SOC_ENUM("Mic Boost1", stac9766_boost1_enum),
  101. SOC_ENUM("Mic Boost2", stac9766_boost2_enum),
  102. SOC_SINGLE_TLV("Mic Volume", AC97_MIC, 0, 31, 1, mix_tlv),
  103. SOC_SINGLE("Mic Switch", AC97_MIC, 15, 1, 1),
  104. SOC_ENUM("Stereo Mic", stac9766_stereo_mic_enum),
  105. SOC_DOUBLE_TLV("Line Volume", AC97_LINE, 8, 0, 31, 1, mix_tlv),
  106. SOC_SINGLE("Line Switch", AC97_LINE, 15, 1, 1),
  107. SOC_DOUBLE_TLV("CD Volume", AC97_CD, 8, 0, 31, 1, mix_tlv),
  108. SOC_SINGLE("CD Switch", AC97_CD, 15, 1, 1),
  109. SOC_DOUBLE_TLV("AUX Volume", AC97_AUX, 8, 0, 31, 1, mix_tlv),
  110. SOC_SINGLE("AUX Switch", AC97_AUX, 15, 1, 1),
  111. SOC_DOUBLE_TLV("Video Volume", AC97_VIDEO, 8, 0, 31, 1, mix_tlv),
  112. SOC_SINGLE("Video Switch", AC97_VIDEO, 15, 1, 1),
  113. SOC_DOUBLE_TLV("DAC Volume", AC97_PCM, 8, 0, 31, 1, mix_tlv),
  114. SOC_SINGLE("DAC Switch", AC97_PCM, 15, 1, 1),
  115. SOC_SINGLE("Loopback Test Switch", AC97_GENERAL_PURPOSE, 7, 1, 0),
  116. SOC_SINGLE("3D Volume", AC97_3D_CONTROL, 3, 2, 1),
  117. SOC_SINGLE("3D Switch", AC97_GENERAL_PURPOSE, 13, 1, 0),
  118. SOC_ENUM("SPDIF Mux", stac9766_SPDIF_enum),
  119. SOC_ENUM("Mic1/2 Mux", stac9766_mic_enum),
  120. SOC_ENUM("Record All Mux", stac9766_record_all_enum),
  121. SOC_ENUM("Record Mux", stac9766_record_enum),
  122. SOC_ENUM("Mono Mux", stac9766_mono_enum),
  123. SOC_ENUM("Pop Bypass Mux", stac9766_popbypass_enum),
  124. };
  125. static int stac9766_ac97_write(struct snd_soc_codec *codec, unsigned int reg,
  126. unsigned int val)
  127. {
  128. u16 *cache = codec->reg_cache;
  129. if (reg > AC97_STAC_PAGE0) {
  130. stac9766_ac97_write(codec, AC97_INT_PAGING, 0);
  131. soc_ac97_ops.write(codec->ac97, reg, val);
  132. stac9766_ac97_write(codec, AC97_INT_PAGING, 1);
  133. return 0;
  134. }
  135. if (reg / 2 >= ARRAY_SIZE(stac9766_reg))
  136. return -EIO;
  137. soc_ac97_ops.write(codec->ac97, reg, val);
  138. cache[reg / 2] = val;
  139. return 0;
  140. }
  141. static unsigned int stac9766_ac97_read(struct snd_soc_codec *codec,
  142. unsigned int reg)
  143. {
  144. u16 val = 0, *cache = codec->reg_cache;
  145. if (reg > AC97_STAC_PAGE0) {
  146. stac9766_ac97_write(codec, AC97_INT_PAGING, 0);
  147. val = soc_ac97_ops.read(codec->ac97, reg - AC97_STAC_PAGE0);
  148. stac9766_ac97_write(codec, AC97_INT_PAGING, 1);
  149. return val;
  150. }
  151. if (reg / 2 >= ARRAY_SIZE(stac9766_reg))
  152. return -EIO;
  153. if (reg == AC97_RESET || reg == AC97_GPIO_STATUS ||
  154. reg == AC97_INT_PAGING || reg == AC97_VENDOR_ID1 ||
  155. reg == AC97_VENDOR_ID2) {
  156. val = soc_ac97_ops.read(codec->ac97, reg);
  157. return val;
  158. }
  159. return cache[reg / 2];
  160. }
  161. static int ac97_analog_prepare(struct snd_pcm_substream *substream,
  162. struct snd_soc_dai *dai)
  163. {
  164. struct snd_soc_codec *codec = dai->codec;
  165. struct snd_pcm_runtime *runtime = substream->runtime;
  166. unsigned short reg, vra;
  167. vra = stac9766_ac97_read(codec, AC97_EXTENDED_STATUS);
  168. vra |= 0x1; /* enable variable rate audio */
  169. vra &= ~0x4; /* disable SPDIF output */
  170. stac9766_ac97_write(codec, AC97_EXTENDED_STATUS, vra);
  171. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  172. reg = AC97_PCM_FRONT_DAC_RATE;
  173. else
  174. reg = AC97_PCM_LR_ADC_RATE;
  175. return stac9766_ac97_write(codec, reg, runtime->rate);
  176. }
  177. static int ac97_digital_prepare(struct snd_pcm_substream *substream,
  178. struct snd_soc_dai *dai)
  179. {
  180. struct snd_soc_codec *codec = dai->codec;
  181. struct snd_pcm_runtime *runtime = substream->runtime;
  182. unsigned short reg, vra;
  183. stac9766_ac97_write(codec, AC97_SPDIF, 0x2002);
  184. vra = stac9766_ac97_read(codec, AC97_EXTENDED_STATUS);
  185. vra |= 0x5; /* Enable VRA and SPDIF out */
  186. stac9766_ac97_write(codec, AC97_EXTENDED_STATUS, vra);
  187. reg = AC97_PCM_FRONT_DAC_RATE;
  188. return stac9766_ac97_write(codec, reg, runtime->rate);
  189. }
  190. static int stac9766_set_bias_level(struct snd_soc_codec *codec,
  191. enum snd_soc_bias_level level)
  192. {
  193. switch (level) {
  194. case SND_SOC_BIAS_ON: /* full On */
  195. case SND_SOC_BIAS_PREPARE: /* partial On */
  196. case SND_SOC_BIAS_STANDBY: /* Off, with power */
  197. stac9766_ac97_write(codec, AC97_POWERDOWN, 0x0000);
  198. break;
  199. case SND_SOC_BIAS_OFF: /* Off, without power */
  200. /* disable everything including AC link */
  201. stac9766_ac97_write(codec, AC97_POWERDOWN, 0xffff);
  202. break;
  203. }
  204. codec->dapm.bias_level = level;
  205. return 0;
  206. }
  207. static int stac9766_reset(struct snd_soc_codec *codec, int try_warm)
  208. {
  209. if (try_warm && soc_ac97_ops.warm_reset) {
  210. soc_ac97_ops.warm_reset(codec->ac97);
  211. if (stac9766_ac97_read(codec, 0) == stac9766_reg[0])
  212. return 1;
  213. }
  214. soc_ac97_ops.reset(codec->ac97);
  215. if (soc_ac97_ops.warm_reset)
  216. soc_ac97_ops.warm_reset(codec->ac97);
  217. if (stac9766_ac97_read(codec, 0) != stac9766_reg[0])
  218. return -EIO;
  219. return 0;
  220. }
  221. static int stac9766_codec_suspend(struct snd_soc_codec *codec,
  222. pm_message_t state)
  223. {
  224. stac9766_set_bias_level(codec, SND_SOC_BIAS_OFF);
  225. return 0;
  226. }
  227. static int stac9766_codec_resume(struct snd_soc_codec *codec)
  228. {
  229. u16 id, reset;
  230. reset = 0;
  231. /* give the codec an AC97 warm reset to start the link */
  232. reset:
  233. if (reset > 5) {
  234. printk(KERN_ERR "stac9766 failed to resume");
  235. return -EIO;
  236. }
  237. codec->ac97->bus->ops->warm_reset(codec->ac97);
  238. id = soc_ac97_ops.read(codec->ac97, AC97_VENDOR_ID2);
  239. if (id != 0x4c13) {
  240. stac9766_reset(codec, 0);
  241. reset++;
  242. goto reset;
  243. }
  244. stac9766_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  245. return 0;
  246. }
  247. static struct snd_soc_dai_ops stac9766_dai_ops_analog = {
  248. .prepare = ac97_analog_prepare,
  249. };
  250. static struct snd_soc_dai_ops stac9766_dai_ops_digital = {
  251. .prepare = ac97_digital_prepare,
  252. };
  253. static struct snd_soc_dai_driver stac9766_dai[] = {
  254. {
  255. .name = "stac9766-hifi-analog",
  256. .ac97_control = 1,
  257. /* stream cababilities */
  258. .playback = {
  259. .stream_name = "stac9766 analog",
  260. .channels_min = 1,
  261. .channels_max = 2,
  262. .rates = SNDRV_PCM_RATE_8000_48000,
  263. .formats = SND_SOC_STD_AC97_FMTS,
  264. },
  265. .capture = {
  266. .stream_name = "stac9766 analog",
  267. .channels_min = 1,
  268. .channels_max = 2,
  269. .rates = SNDRV_PCM_RATE_8000_48000,
  270. .formats = SND_SOC_STD_AC97_FMTS,
  271. },
  272. /* alsa ops */
  273. .ops = &stac9766_dai_ops_analog,
  274. },
  275. {
  276. .name = "stac9766-hifi-IEC958",
  277. .ac97_control = 1,
  278. /* stream cababilities */
  279. .playback = {
  280. .stream_name = "stac9766 IEC958",
  281. .channels_min = 1,
  282. .channels_max = 2,
  283. .rates = SNDRV_PCM_RATE_32000 | \
  284. SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
  285. .formats = SNDRV_PCM_FORMAT_IEC958_SUBFRAME_BE,
  286. },
  287. /* alsa ops */
  288. .ops = &stac9766_dai_ops_digital,
  289. }
  290. };
  291. static int stac9766_codec_probe(struct snd_soc_codec *codec)
  292. {
  293. int ret = 0;
  294. printk(KERN_INFO "STAC9766 SoC Audio Codec %s\n", STAC9766_VERSION);
  295. ret = snd_soc_new_ac97_codec(codec, &soc_ac97_ops, 0);
  296. if (ret < 0)
  297. goto codec_err;
  298. /* do a cold reset for the controller and then try
  299. * a warm reset followed by an optional cold reset for codec */
  300. stac9766_reset(codec, 0);
  301. ret = stac9766_reset(codec, 1);
  302. if (ret < 0) {
  303. printk(KERN_ERR "Failed to reset STAC9766: AC97 link error\n");
  304. goto codec_err;
  305. }
  306. stac9766_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  307. snd_soc_add_controls(codec, stac9766_snd_ac97_controls,
  308. ARRAY_SIZE(stac9766_snd_ac97_controls));
  309. return 0;
  310. codec_err:
  311. snd_soc_free_ac97_codec(codec);
  312. return ret;
  313. }
  314. static int stac9766_codec_remove(struct snd_soc_codec *codec)
  315. {
  316. snd_soc_free_ac97_codec(codec);
  317. return 0;
  318. }
  319. static struct snd_soc_codec_driver soc_codec_dev_stac9766 = {
  320. .write = stac9766_ac97_write,
  321. .read = stac9766_ac97_read,
  322. .set_bias_level = stac9766_set_bias_level,
  323. .probe = stac9766_codec_probe,
  324. .remove = stac9766_codec_remove,
  325. .suspend = stac9766_codec_suspend,
  326. .resume = stac9766_codec_resume,
  327. .reg_cache_size = sizeof(stac9766_reg),
  328. .reg_word_size = sizeof(u16),
  329. .reg_cache_step = 2,
  330. .reg_cache_default = stac9766_reg,
  331. };
  332. static __devinit int stac9766_probe(struct platform_device *pdev)
  333. {
  334. return snd_soc_register_codec(&pdev->dev,
  335. &soc_codec_dev_stac9766, stac9766_dai, ARRAY_SIZE(stac9766_dai));
  336. }
  337. static int __devexit stac9766_remove(struct platform_device *pdev)
  338. {
  339. snd_soc_unregister_codec(&pdev->dev);
  340. return 0;
  341. }
  342. static struct platform_driver stac9766_codec_driver = {
  343. .driver = {
  344. .name = "stac9766-codec",
  345. .owner = THIS_MODULE,
  346. },
  347. .probe = stac9766_probe,
  348. .remove = __devexit_p(stac9766_remove),
  349. };
  350. static int __init stac9766_init(void)
  351. {
  352. return platform_driver_register(&stac9766_codec_driver);
  353. }
  354. module_init(stac9766_init);
  355. static void __exit stac9766_exit(void)
  356. {
  357. platform_driver_unregister(&stac9766_codec_driver);
  358. }
  359. module_exit(stac9766_exit);
  360. MODULE_DESCRIPTION("ASoC stac9766 driver");
  361. MODULE_AUTHOR("Jon Smirl <jonsmirl@gmail.com>");
  362. MODULE_LICENSE("GPL");