stac9766.c 12 KB

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