stac9766.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * stac9766.c -- ALSA SoC STAC9766 codec support
  4. *
  5. * Copyright 2009 Jon Smirl, Digispeaker
  6. * Author: Jon Smirl <[email protected]>
  7. *
  8. * Features:-
  9. *
  10. * o Support for AC97 Codec, S/PDIF
  11. */
  12. #include <linux/init.h>
  13. #include <linux/slab.h>
  14. #include <linux/module.h>
  15. #include <linux/device.h>
  16. #include <linux/regmap.h>
  17. #include <sound/core.h>
  18. #include <sound/pcm.h>
  19. #include <sound/ac97_codec.h>
  20. #include <sound/initval.h>
  21. #include <sound/pcm_params.h>
  22. #include <sound/soc.h>
  23. #include <sound/tlv.h>
  24. #define STAC9766_VENDOR_ID 0x83847666
  25. #define STAC9766_VENDOR_ID_MASK 0xffffffff
  26. #define AC97_STAC_DA_CONTROL 0x6A
  27. #define AC97_STAC_ANALOG_SPECIAL 0x6E
  28. #define AC97_STAC_STEREO_MIC 0x78
  29. static const struct reg_default stac9766_reg_defaults[] = {
  30. { 0x02, 0x8000 },
  31. { 0x04, 0x8000 },
  32. { 0x06, 0x8000 },
  33. { 0x0a, 0x0000 },
  34. { 0x0c, 0x8008 },
  35. { 0x0e, 0x8008 },
  36. { 0x10, 0x8808 },
  37. { 0x12, 0x8808 },
  38. { 0x14, 0x8808 },
  39. { 0x16, 0x8808 },
  40. { 0x18, 0x8808 },
  41. { 0x1a, 0x0000 },
  42. { 0x1c, 0x8000 },
  43. { 0x20, 0x0000 },
  44. { 0x22, 0x0000 },
  45. { 0x28, 0x0a05 },
  46. { 0x2c, 0xbb80 },
  47. { 0x32, 0xbb80 },
  48. { 0x3a, 0x2000 },
  49. { 0x3e, 0x0100 },
  50. { 0x4c, 0x0300 },
  51. { 0x4e, 0xffff },
  52. { 0x50, 0x0000 },
  53. { 0x52, 0x0000 },
  54. { 0x54, 0x0000 },
  55. { 0x6a, 0x0000 },
  56. { 0x6e, 0x1000 },
  57. { 0x72, 0x0000 },
  58. { 0x78, 0x0000 },
  59. };
  60. static const struct regmap_config stac9766_regmap_config = {
  61. .reg_bits = 16,
  62. .reg_stride = 2,
  63. .val_bits = 16,
  64. .max_register = 0x78,
  65. .cache_type = REGCACHE_RBTREE,
  66. .volatile_reg = regmap_ac97_default_volatile,
  67. .reg_defaults = stac9766_reg_defaults,
  68. .num_reg_defaults = ARRAY_SIZE(stac9766_reg_defaults),
  69. };
  70. static const char *stac9766_record_mux[] = {"Mic", "CD", "Video", "AUX",
  71. "Line", "Stereo Mix", "Mono Mix", "Phone"};
  72. static const char *stac9766_mono_mux[] = {"Mix", "Mic"};
  73. static const char *stac9766_mic_mux[] = {"Mic1", "Mic2"};
  74. static const char *stac9766_SPDIF_mux[] = {"PCM", "ADC Record"};
  75. static const char *stac9766_popbypass_mux[] = {"Normal", "Bypass Mixer"};
  76. static const char *stac9766_record_all_mux[] = {"All analog",
  77. "Analog plus DAC"};
  78. static const char *stac9766_boost1[] = {"0dB", "10dB"};
  79. static const char *stac9766_boost2[] = {"0dB", "20dB"};
  80. static const char *stac9766_stereo_mic[] = {"Off", "On"};
  81. static SOC_ENUM_DOUBLE_DECL(stac9766_record_enum,
  82. AC97_REC_SEL, 8, 0, stac9766_record_mux);
  83. static SOC_ENUM_SINGLE_DECL(stac9766_mono_enum,
  84. AC97_GENERAL_PURPOSE, 9, stac9766_mono_mux);
  85. static SOC_ENUM_SINGLE_DECL(stac9766_mic_enum,
  86. AC97_GENERAL_PURPOSE, 8, stac9766_mic_mux);
  87. static SOC_ENUM_SINGLE_DECL(stac9766_SPDIF_enum,
  88. AC97_STAC_DA_CONTROL, 1, stac9766_SPDIF_mux);
  89. static SOC_ENUM_SINGLE_DECL(stac9766_popbypass_enum,
  90. AC97_GENERAL_PURPOSE, 15, stac9766_popbypass_mux);
  91. static SOC_ENUM_SINGLE_DECL(stac9766_record_all_enum,
  92. AC97_STAC_ANALOG_SPECIAL, 12,
  93. stac9766_record_all_mux);
  94. static SOC_ENUM_SINGLE_DECL(stac9766_boost1_enum,
  95. AC97_MIC, 6, stac9766_boost1); /* 0/10dB */
  96. static SOC_ENUM_SINGLE_DECL(stac9766_boost2_enum,
  97. AC97_STAC_ANALOG_SPECIAL, 2, stac9766_boost2); /* 0/20dB */
  98. static SOC_ENUM_SINGLE_DECL(stac9766_stereo_mic_enum,
  99. AC97_STAC_STEREO_MIC, 2, stac9766_stereo_mic);
  100. static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(master_tlv, -4650, 150, 0);
  101. static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(record_tlv, 0, 150, 0);
  102. static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(beep_tlv, -4500, 300, 0);
  103. static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(mix_tlv, -3450, 150, 0);
  104. static const struct snd_kcontrol_new stac9766_snd_ac97_controls[] = {
  105. SOC_DOUBLE_TLV("Speaker Volume", AC97_MASTER, 8, 0, 31, 1, master_tlv),
  106. SOC_SINGLE("Speaker Switch", AC97_MASTER, 15, 1, 1),
  107. SOC_DOUBLE_TLV("Headphone Volume", AC97_HEADPHONE, 8, 0, 31, 1,
  108. master_tlv),
  109. SOC_SINGLE("Headphone Switch", AC97_HEADPHONE, 15, 1, 1),
  110. SOC_SINGLE_TLV("Mono Out Volume", AC97_MASTER_MONO, 0, 31, 1,
  111. master_tlv),
  112. SOC_SINGLE("Mono Out Switch", AC97_MASTER_MONO, 15, 1, 1),
  113. SOC_DOUBLE_TLV("Record Volume", AC97_REC_GAIN, 8, 0, 15, 0, record_tlv),
  114. SOC_SINGLE("Record Switch", AC97_REC_GAIN, 15, 1, 1),
  115. SOC_SINGLE_TLV("Beep Volume", AC97_PC_BEEP, 1, 15, 1, beep_tlv),
  116. SOC_SINGLE("Beep Switch", AC97_PC_BEEP, 15, 1, 1),
  117. SOC_SINGLE("Beep Frequency", AC97_PC_BEEP, 5, 127, 1),
  118. SOC_SINGLE_TLV("Phone Volume", AC97_PHONE, 0, 31, 1, mix_tlv),
  119. SOC_SINGLE("Phone Switch", AC97_PHONE, 15, 1, 1),
  120. SOC_ENUM("Mic Boost1", stac9766_boost1_enum),
  121. SOC_ENUM("Mic Boost2", stac9766_boost2_enum),
  122. SOC_SINGLE_TLV("Mic Volume", AC97_MIC, 0, 31, 1, mix_tlv),
  123. SOC_SINGLE("Mic Switch", AC97_MIC, 15, 1, 1),
  124. SOC_ENUM("Stereo Mic", stac9766_stereo_mic_enum),
  125. SOC_DOUBLE_TLV("Line Volume", AC97_LINE, 8, 0, 31, 1, mix_tlv),
  126. SOC_SINGLE("Line Switch", AC97_LINE, 15, 1, 1),
  127. SOC_DOUBLE_TLV("CD Volume", AC97_CD, 8, 0, 31, 1, mix_tlv),
  128. SOC_SINGLE("CD Switch", AC97_CD, 15, 1, 1),
  129. SOC_DOUBLE_TLV("AUX Volume", AC97_AUX, 8, 0, 31, 1, mix_tlv),
  130. SOC_SINGLE("AUX Switch", AC97_AUX, 15, 1, 1),
  131. SOC_DOUBLE_TLV("Video Volume", AC97_VIDEO, 8, 0, 31, 1, mix_tlv),
  132. SOC_SINGLE("Video Switch", AC97_VIDEO, 15, 1, 1),
  133. SOC_DOUBLE_TLV("DAC Volume", AC97_PCM, 8, 0, 31, 1, mix_tlv),
  134. SOC_SINGLE("DAC Switch", AC97_PCM, 15, 1, 1),
  135. SOC_SINGLE("Loopback Test Switch", AC97_GENERAL_PURPOSE, 7, 1, 0),
  136. SOC_SINGLE("3D Volume", AC97_3D_CONTROL, 3, 2, 1),
  137. SOC_SINGLE("3D Switch", AC97_GENERAL_PURPOSE, 13, 1, 0),
  138. SOC_ENUM("SPDIF Mux", stac9766_SPDIF_enum),
  139. SOC_ENUM("Mic1/2 Mux", stac9766_mic_enum),
  140. SOC_ENUM("Record All Mux", stac9766_record_all_enum),
  141. SOC_ENUM("Record Mux", stac9766_record_enum),
  142. SOC_ENUM("Mono Mux", stac9766_mono_enum),
  143. SOC_ENUM("Pop Bypass Mux", stac9766_popbypass_enum),
  144. };
  145. static int ac97_analog_prepare(struct snd_pcm_substream *substream,
  146. struct snd_soc_dai *dai)
  147. {
  148. struct snd_soc_component *component = dai->component;
  149. struct snd_pcm_runtime *runtime = substream->runtime;
  150. unsigned short reg;
  151. /* enable variable rate audio, disable SPDIF output */
  152. snd_soc_component_update_bits(component, AC97_EXTENDED_STATUS, 0x5, 0x1);
  153. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  154. reg = AC97_PCM_FRONT_DAC_RATE;
  155. else
  156. reg = AC97_PCM_LR_ADC_RATE;
  157. return snd_soc_component_write(component, reg, runtime->rate);
  158. }
  159. static int ac97_digital_prepare(struct snd_pcm_substream *substream,
  160. struct snd_soc_dai *dai)
  161. {
  162. struct snd_soc_component *component = dai->component;
  163. struct snd_pcm_runtime *runtime = substream->runtime;
  164. unsigned short reg;
  165. snd_soc_component_write(component, AC97_SPDIF, 0x2002);
  166. /* Enable VRA and SPDIF out */
  167. snd_soc_component_update_bits(component, AC97_EXTENDED_STATUS, 0x5, 0x5);
  168. reg = AC97_PCM_FRONT_DAC_RATE;
  169. return snd_soc_component_write(component, reg, runtime->rate);
  170. }
  171. static int stac9766_set_bias_level(struct snd_soc_component *component,
  172. enum snd_soc_bias_level level)
  173. {
  174. switch (level) {
  175. case SND_SOC_BIAS_ON: /* full On */
  176. case SND_SOC_BIAS_PREPARE: /* partial On */
  177. case SND_SOC_BIAS_STANDBY: /* Off, with power */
  178. snd_soc_component_write(component, AC97_POWERDOWN, 0x0000);
  179. break;
  180. case SND_SOC_BIAS_OFF: /* Off, without power */
  181. /* disable everything including AC link */
  182. snd_soc_component_write(component, AC97_POWERDOWN, 0xffff);
  183. break;
  184. }
  185. return 0;
  186. }
  187. static int stac9766_component_resume(struct snd_soc_component *component)
  188. {
  189. struct snd_ac97 *ac97 = snd_soc_component_get_drvdata(component);
  190. return snd_ac97_reset(ac97, true, STAC9766_VENDOR_ID,
  191. STAC9766_VENDOR_ID_MASK);
  192. }
  193. static const struct snd_soc_dai_ops stac9766_dai_ops_analog = {
  194. .prepare = ac97_analog_prepare,
  195. };
  196. static const struct snd_soc_dai_ops stac9766_dai_ops_digital = {
  197. .prepare = ac97_digital_prepare,
  198. };
  199. static struct snd_soc_dai_driver stac9766_dai[] = {
  200. {
  201. .name = "stac9766-hifi-analog",
  202. /* stream cababilities */
  203. .playback = {
  204. .stream_name = "stac9766 analog",
  205. .channels_min = 1,
  206. .channels_max = 2,
  207. .rates = SNDRV_PCM_RATE_8000_48000,
  208. .formats = SND_SOC_STD_AC97_FMTS,
  209. },
  210. .capture = {
  211. .stream_name = "stac9766 analog",
  212. .channels_min = 1,
  213. .channels_max = 2,
  214. .rates = SNDRV_PCM_RATE_8000_48000,
  215. .formats = SND_SOC_STD_AC97_FMTS,
  216. },
  217. /* alsa ops */
  218. .ops = &stac9766_dai_ops_analog,
  219. },
  220. {
  221. .name = "stac9766-hifi-IEC958",
  222. /* stream cababilities */
  223. .playback = {
  224. .stream_name = "stac9766 IEC958",
  225. .channels_min = 1,
  226. .channels_max = 2,
  227. .rates = SNDRV_PCM_RATE_32000 | \
  228. SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
  229. .formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
  230. },
  231. /* alsa ops */
  232. .ops = &stac9766_dai_ops_digital,
  233. }
  234. };
  235. static int stac9766_component_probe(struct snd_soc_component *component)
  236. {
  237. struct snd_ac97 *ac97;
  238. struct regmap *regmap;
  239. int ret;
  240. ac97 = snd_soc_new_ac97_component(component, STAC9766_VENDOR_ID,
  241. STAC9766_VENDOR_ID_MASK);
  242. if (IS_ERR(ac97))
  243. return PTR_ERR(ac97);
  244. regmap = regmap_init_ac97(ac97, &stac9766_regmap_config);
  245. if (IS_ERR(regmap)) {
  246. ret = PTR_ERR(regmap);
  247. goto err_free_ac97;
  248. }
  249. snd_soc_component_init_regmap(component, regmap);
  250. snd_soc_component_set_drvdata(component, ac97);
  251. return 0;
  252. err_free_ac97:
  253. snd_soc_free_ac97_component(ac97);
  254. return ret;
  255. }
  256. static void stac9766_component_remove(struct snd_soc_component *component)
  257. {
  258. struct snd_ac97 *ac97 = snd_soc_component_get_drvdata(component);
  259. snd_soc_component_exit_regmap(component);
  260. snd_soc_free_ac97_component(ac97);
  261. }
  262. static const struct snd_soc_component_driver soc_component_dev_stac9766 = {
  263. .controls = stac9766_snd_ac97_controls,
  264. .num_controls = ARRAY_SIZE(stac9766_snd_ac97_controls),
  265. .set_bias_level = stac9766_set_bias_level,
  266. .probe = stac9766_component_probe,
  267. .remove = stac9766_component_remove,
  268. .resume = stac9766_component_resume,
  269. .suspend_bias_off = 1,
  270. .idle_bias_on = 1,
  271. .use_pmdown_time = 1,
  272. .endianness = 1,
  273. };
  274. static int stac9766_probe(struct platform_device *pdev)
  275. {
  276. return devm_snd_soc_register_component(&pdev->dev,
  277. &soc_component_dev_stac9766, stac9766_dai, ARRAY_SIZE(stac9766_dai));
  278. }
  279. static struct platform_driver stac9766_codec_driver = {
  280. .driver = {
  281. .name = "stac9766-codec",
  282. },
  283. .probe = stac9766_probe,
  284. };
  285. module_platform_driver(stac9766_codec_driver);
  286. MODULE_DESCRIPTION("ASoC stac9766 driver");
  287. MODULE_AUTHOR("Jon Smirl <[email protected]>");
  288. MODULE_LICENSE("GPL");