stac9766.c 11 KB

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