wm9712.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. /*
  2. * wm9712.c -- ALSA Soc WM9712 codec support
  3. *
  4. * Copyright 2006 Wolfson Microelectronics PLC.
  5. * Author: Liam Girdwood
  6. * liam.girdwood@wolfsonmicro.com or linux@wolfsonmicro.com
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. * Revision history
  14. * 4th Feb 2006 Initial version.
  15. */
  16. #include <linux/init.h>
  17. #include <linux/module.h>
  18. #include <linux/version.h>
  19. #include <linux/kernel.h>
  20. #include <linux/device.h>
  21. #include <sound/driver.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. #include <sound/soc-dapm.h>
  28. #define WM9712_VERSION "0.4"
  29. static unsigned int ac97_read(struct snd_soc_codec *codec,
  30. unsigned int reg);
  31. static int ac97_write(struct snd_soc_codec *codec,
  32. unsigned int reg, unsigned int val);
  33. #define AC97_DIR \
  34. (SND_SOC_DAIDIR_PLAYBACK | SND_SOC_DAIDIR_CAPTURE)
  35. #define AC97_RATES \
  36. (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 | SNDRV_PCM_RATE_16000 | \
  37. SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
  38. SNDRV_PCM_RATE_48000)
  39. /* may need to expand this */
  40. static struct snd_soc_dai_mode ac97_modes[] = {
  41. {
  42. .pcmfmt = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S18_3LE,
  43. .pcmrate = AC97_RATES,
  44. .pcmdir = AC97_DIR,
  45. },
  46. };
  47. /*
  48. * WM9712 register cache
  49. */
  50. static const u16 wm9712_reg[] = {
  51. 0x6174, 0x8000, 0x8000, 0x8000, // 6
  52. 0xf0f0, 0xaaa0, 0xc008, 0x6808, // e
  53. 0xe808, 0xaaa0, 0xad00, 0x8000, // 16
  54. 0xe808, 0x3000, 0x8000, 0x0000, // 1e
  55. 0x0000, 0x0000, 0x0000, 0x000f, // 26
  56. 0x0405, 0x0410, 0xbb80, 0xbb80, // 2e
  57. 0x0000, 0xbb80, 0x0000, 0x0000, // 36
  58. 0x0000, 0x2000, 0x0000, 0x0000, // 3e
  59. 0x0000, 0x0000, 0x0000, 0x0000, // 46
  60. 0x0000, 0x0000, 0xf83e, 0xffff, // 4e
  61. 0x0000, 0x0000, 0x0000, 0xf83e, // 56
  62. 0x0008, 0x0000, 0x0000, 0x0000, // 5e
  63. 0xb032, 0x3e00, 0x0000, 0x0000, // 66
  64. 0x0000, 0x0000, 0x0000, 0x0000, // 6e
  65. 0x0000, 0x0000, 0x0000, 0x0006, // 76
  66. 0x0001, 0x0000, 0x574d, 0x4c12, // 7e
  67. 0x0000, 0x0000 // virtual hp mixers
  68. };
  69. /* virtual HP mixers regs */
  70. #define HPL_MIXER 0x80
  71. #define HPR_MIXER 0x82
  72. static const char *wm9712_alc_select[] = {"None", "Left", "Right", "Stereo"};
  73. static const char *wm9712_alc_mux[] = {"Stereo", "Left", "Right", "None"};
  74. static const char *wm9712_out3_src[] = {"Left", "VREF", "Left + Right",
  75. "Mono"};
  76. static const char *wm9712_spk_src[] = {"Speaker Mix", "Headphone Mix"};
  77. static const char *wm9712_rec_adc[] = {"Stereo", "Left", "Right", "Mute"};
  78. static const char *wm9712_base[] = {"Linear Control", "Adaptive Boost"};
  79. static const char *wm9712_rec_gain[] = {"+1.5dB Steps", "+0.75dB Steps"};
  80. static const char *wm9712_mic[] = {"Mic 1", "Differential", "Mic 2",
  81. "Stereo"};
  82. static const char *wm9712_rec_sel[] = {"Mic", "NC", "NC", "Speaker Mixer",
  83. "Line", "Headphone Mixer", "Phone Mixer", "Phone"};
  84. static const char *wm9712_ng_type[] = {"Constant Gain", "Mute"};
  85. static const char *wm9712_diff_sel[] = {"Mic", "Line"};
  86. static const struct soc_enum wm9712_enum[] = {
  87. SOC_ENUM_SINGLE(AC97_PCI_SVID, 14, 4, wm9712_alc_select),
  88. SOC_ENUM_SINGLE(AC97_VIDEO, 12, 4, wm9712_alc_mux),
  89. SOC_ENUM_SINGLE(AC97_AUX, 9, 4, wm9712_out3_src),
  90. SOC_ENUM_SINGLE(AC97_AUX, 8, 2, wm9712_spk_src),
  91. SOC_ENUM_SINGLE(AC97_REC_SEL, 12, 4, wm9712_rec_adc),
  92. SOC_ENUM_SINGLE(AC97_MASTER_TONE, 15, 2, wm9712_base),
  93. SOC_ENUM_DOUBLE(AC97_REC_GAIN, 14, 6, 2, wm9712_rec_gain),
  94. SOC_ENUM_SINGLE(AC97_MIC, 5, 4, wm9712_mic),
  95. SOC_ENUM_SINGLE(AC97_REC_SEL, 8, 8, wm9712_rec_sel),
  96. SOC_ENUM_SINGLE(AC97_REC_SEL, 0, 8, wm9712_rec_sel),
  97. SOC_ENUM_SINGLE(AC97_PCI_SVID, 5, 2, wm9712_ng_type),
  98. SOC_ENUM_SINGLE(0x5c, 8, 2, wm9712_diff_sel),
  99. };
  100. static const struct snd_kcontrol_new wm9712_snd_ac97_controls[] = {
  101. SOC_DOUBLE("Speaker Playback Volume", AC97_MASTER, 8, 0, 31, 1),
  102. SOC_SINGLE("Speaker 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_SINGLE("Speaker Playback ZC Switch", AC97_MASTER, 7, 1, 0),
  106. SOC_SINGLE("Speaker Playback Invert Switch", AC97_MASTER, 6, 1, 0),
  107. SOC_SINGLE("Headphone Playback ZC Switch", AC97_HEADPHONE, 7, 1, 0),
  108. SOC_SINGLE("Mono Playback ZC Switch", AC97_MASTER_MONO, 7, 1, 0),
  109. SOC_SINGLE("Mono Playback Volume", AC97_MASTER_MONO, 0, 31, 0),
  110. SOC_SINGLE("ALC Target Volume", AC97_CODEC_CLASS_REV, 12, 15, 0),
  111. SOC_SINGLE("ALC Hold Time", AC97_CODEC_CLASS_REV, 8, 15, 0),
  112. SOC_SINGLE("ALC Decay Time", AC97_CODEC_CLASS_REV, 4, 15, 0),
  113. SOC_SINGLE("ALC Attack Time", AC97_CODEC_CLASS_REV, 0, 15, 0),
  114. SOC_ENUM("ALC Function", wm9712_enum[0]),
  115. SOC_SINGLE("ALC Max Volume", AC97_PCI_SVID, 11, 7, 0),
  116. SOC_SINGLE("ALC ZC Timeout", AC97_PCI_SVID, 9, 3, 1),
  117. SOC_SINGLE("ALC ZC Switch", AC97_PCI_SVID, 8, 1, 0),
  118. SOC_SINGLE("ALC NG Switch", AC97_PCI_SVID, 7, 1, 0),
  119. SOC_ENUM("ALC NG Type", wm9712_enum[10]),
  120. SOC_SINGLE("ALC NG Threshold", AC97_PCI_SVID, 0, 31, 1),
  121. SOC_SINGLE("Mic Headphone Volume", AC97_VIDEO, 12, 7, 1),
  122. SOC_SINGLE("ALC Headphone Volume", AC97_VIDEO, 7, 7, 1),
  123. SOC_SINGLE("Out3 Switch", AC97_AUX, 15, 1, 1),
  124. SOC_SINGLE("Out3 ZC Switch", AC97_AUX, 7, 1, 1),
  125. SOC_SINGLE("Out3 Volume", AC97_AUX, 0, 31, 1),
  126. SOC_SINGLE("PCBeep Bypass Headphone Volume", AC97_PC_BEEP, 12, 7, 1),
  127. SOC_SINGLE("PCBeep Bypass Speaker Volume", AC97_PC_BEEP, 8, 7, 1),
  128. SOC_SINGLE("PCBeep Bypass Phone Volume", AC97_PC_BEEP, 4, 7, 1),
  129. SOC_SINGLE("Aux Playback Headphone Volume", AC97_CD, 12, 7, 1),
  130. SOC_SINGLE("Aux Playback Speaker Volume", AC97_CD, 8, 7, 1),
  131. SOC_SINGLE("Aux Playback Phone Volume", AC97_CD, 4, 7, 1),
  132. SOC_SINGLE("Phone Volume", AC97_PHONE, 0, 15, 0),
  133. SOC_DOUBLE("Line Capture Volume", AC97_LINE, 8, 0, 31, 1),
  134. SOC_SINGLE("Capture 20dB Boost Switch", AC97_REC_SEL, 14, 1, 0),
  135. SOC_SINGLE("Capture to Phone 20dB Boost Switch", AC97_REC_SEL, 11, 1, 1),
  136. SOC_SINGLE("3D Upper Cut-off Switch", AC97_3D_CONTROL, 5, 1, 1),
  137. SOC_SINGLE("3D Lower Cut-off Switch", AC97_3D_CONTROL, 4, 1, 1),
  138. SOC_SINGLE("3D Playback Volume", AC97_3D_CONTROL, 0, 15, 0),
  139. SOC_ENUM("Bass Control", wm9712_enum[5]),
  140. SOC_SINGLE("Bass Cut-off Switch", AC97_MASTER_TONE, 12, 1, 1),
  141. SOC_SINGLE("Tone Cut-off Switch", AC97_MASTER_TONE, 4, 1, 1),
  142. SOC_SINGLE("Playback Attenuate (-6dB) Switch", AC97_MASTER_TONE, 6, 1, 0),
  143. SOC_SINGLE("Bass Volume", AC97_MASTER_TONE, 8, 15, 0),
  144. SOC_SINGLE("Treble Volume", AC97_MASTER_TONE, 0, 15, 0),
  145. SOC_SINGLE("Capture ADC Switch", AC97_REC_GAIN, 15, 1, 1),
  146. SOC_ENUM("Capture Volume Steps", wm9712_enum[6]),
  147. SOC_DOUBLE("Capture Volume", AC97_REC_GAIN, 8, 0, 63, 1),
  148. SOC_SINGLE("Capture ZC Switch", AC97_REC_GAIN, 7, 1, 0),
  149. SOC_SINGLE("Mic 1 Volume", AC97_MIC, 8, 31, 1),
  150. SOC_SINGLE("Mic 2 Volume", AC97_MIC, 0, 31, 1),
  151. SOC_SINGLE("Mic 20dB Boost Switch", AC97_MIC, 7, 1, 0),
  152. };
  153. /* add non dapm controls */
  154. static int wm9712_add_controls(struct snd_soc_codec *codec)
  155. {
  156. int err, i;
  157. for (i = 0; i < ARRAY_SIZE(wm9712_snd_ac97_controls); i++) {
  158. err = snd_ctl_add(codec->card,
  159. snd_soc_cnew(&wm9712_snd_ac97_controls[i],codec, NULL));
  160. if (err < 0)
  161. return err;
  162. }
  163. return 0;
  164. }
  165. /* We have to create a fake left and right HP mixers because
  166. * the codec only has a single control that is shared by both channels.
  167. * This makes it impossible to determine the audio path.
  168. */
  169. static int mixer_event (struct snd_soc_dapm_widget *w, int event)
  170. {
  171. u16 l, r, beep, line, phone, mic, pcm, aux;
  172. l = ac97_read(w->codec, HPL_MIXER);
  173. r = ac97_read(w->codec, HPR_MIXER);
  174. beep = ac97_read(w->codec, AC97_PC_BEEP);
  175. mic = ac97_read(w->codec, AC97_VIDEO);
  176. phone = ac97_read(w->codec, AC97_PHONE);
  177. line = ac97_read(w->codec, AC97_LINE);
  178. pcm = ac97_read(w->codec, AC97_PCM);
  179. aux = ac97_read(w->codec, AC97_CD);
  180. if (l & 0x1 || r & 0x1)
  181. ac97_write(w->codec, AC97_VIDEO, mic & 0x7fff);
  182. else
  183. ac97_write(w->codec, AC97_VIDEO, mic | 0x8000);
  184. if (l & 0x2 || r & 0x2)
  185. ac97_write(w->codec, AC97_PCM, pcm & 0x7fff);
  186. else
  187. ac97_write(w->codec, AC97_PCM, pcm | 0x8000);
  188. if (l & 0x4 || r & 0x4)
  189. ac97_write(w->codec, AC97_LINE, line & 0x7fff);
  190. else
  191. ac97_write(w->codec, AC97_LINE, line | 0x8000);
  192. if (l & 0x8 || r & 0x8)
  193. ac97_write(w->codec, AC97_PHONE, phone & 0x7fff);
  194. else
  195. ac97_write(w->codec, AC97_PHONE, phone | 0x8000);
  196. if (l & 0x10 || r & 0x10)
  197. ac97_write(w->codec, AC97_CD, aux & 0x7fff);
  198. else
  199. ac97_write(w->codec, AC97_CD, aux | 0x8000);
  200. if (l & 0x20 || r & 0x20)
  201. ac97_write(w->codec, AC97_PC_BEEP, beep & 0x7fff);
  202. else
  203. ac97_write(w->codec, AC97_PC_BEEP, beep | 0x8000);
  204. return 0;
  205. }
  206. /* Left Headphone Mixers */
  207. static const struct snd_kcontrol_new wm9712_hpl_mixer_controls[] = {
  208. SOC_DAPM_SINGLE("PCBeep Bypass Switch", HPL_MIXER, 5, 1, 0),
  209. SOC_DAPM_SINGLE("Aux Playback Switch", HPL_MIXER, 4, 1, 0),
  210. SOC_DAPM_SINGLE("Phone Bypass Switch", HPL_MIXER, 3, 1, 0),
  211. SOC_DAPM_SINGLE("Line Bypass Switch", HPL_MIXER, 2, 1, 0),
  212. SOC_DAPM_SINGLE("PCM Playback Switch", HPL_MIXER, 1, 1, 0),
  213. SOC_DAPM_SINGLE("Mic Sidetone Switch", HPL_MIXER, 0, 1, 0),
  214. };
  215. /* Right Headphone Mixers */
  216. static const struct snd_kcontrol_new wm9712_hpr_mixer_controls[] = {
  217. SOC_DAPM_SINGLE("PCBeep Bypass Switch", HPR_MIXER, 5, 1, 0),
  218. SOC_DAPM_SINGLE("Aux Playback Switch", HPR_MIXER, 4, 1, 0),
  219. SOC_DAPM_SINGLE("Phone Bypass Switch", HPR_MIXER, 3, 1, 0),
  220. SOC_DAPM_SINGLE("Line Bypass Switch", HPR_MIXER, 2, 1, 0),
  221. SOC_DAPM_SINGLE("PCM Playback Switch", HPR_MIXER, 1, 1, 0),
  222. SOC_DAPM_SINGLE("Mic Sidetone Switch", HPR_MIXER, 0, 1, 0),
  223. };
  224. /* Speaker Mixer */
  225. static const struct snd_kcontrol_new wm9712_speaker_mixer_controls[] = {
  226. SOC_DAPM_SINGLE("PCBeep Bypass Switch", AC97_PC_BEEP, 11, 1, 1),
  227. SOC_DAPM_SINGLE("Aux Playback Switch", AC97_CD, 11, 1, 1),
  228. SOC_DAPM_SINGLE("Phone Bypass Switch", AC97_PHONE, 14, 1, 1),
  229. SOC_DAPM_SINGLE("Line Bypass Switch", AC97_LINE, 14, 1, 1),
  230. SOC_DAPM_SINGLE("PCM Playback Switch", AC97_PCM, 14, 1, 1),
  231. };
  232. /* Phone Mixer */
  233. static const struct snd_kcontrol_new wm9712_phone_mixer_controls[] = {
  234. SOC_DAPM_SINGLE("PCBeep Bypass Switch", AC97_PC_BEEP, 7, 1, 1),
  235. SOC_DAPM_SINGLE("Aux Playback Switch", AC97_CD, 7, 1, 1),
  236. SOC_DAPM_SINGLE("Line Bypass Switch", AC97_LINE, 13, 1, 1),
  237. SOC_DAPM_SINGLE("PCM Playback Switch", AC97_PCM, 13, 1, 1),
  238. SOC_DAPM_SINGLE("Mic 1 Sidetone Switch", AC97_MIC, 14, 1, 1),
  239. SOC_DAPM_SINGLE("Mic 2 Sidetone Switch", AC97_MIC, 13, 1, 1),
  240. };
  241. /* ALC headphone mux */
  242. static const struct snd_kcontrol_new wm9712_alc_mux_controls =
  243. SOC_DAPM_ENUM("Route", wm9712_enum[1]);
  244. /* out 3 mux */
  245. static const struct snd_kcontrol_new wm9712_out3_mux_controls =
  246. SOC_DAPM_ENUM("Route", wm9712_enum[2]);
  247. /* spk mux */
  248. static const struct snd_kcontrol_new wm9712_spk_mux_controls =
  249. SOC_DAPM_ENUM("Route", wm9712_enum[3]);
  250. /* Capture to Phone mux */
  251. static const struct snd_kcontrol_new wm9712_capture_phone_mux_controls =
  252. SOC_DAPM_ENUM("Route", wm9712_enum[4]);
  253. /* Capture left select */
  254. static const struct snd_kcontrol_new wm9712_capture_selectl_controls =
  255. SOC_DAPM_ENUM("Route", wm9712_enum[8]);
  256. /* Capture right select */
  257. static const struct snd_kcontrol_new wm9712_capture_selectr_controls =
  258. SOC_DAPM_ENUM("Route", wm9712_enum[9]);
  259. /* Mic select */
  260. static const struct snd_kcontrol_new wm9712_mic_src_controls =
  261. SOC_DAPM_ENUM("Route", wm9712_enum[7]);
  262. /* diff select */
  263. static const struct snd_kcontrol_new wm9712_diff_sel_controls =
  264. SOC_DAPM_ENUM("Route", wm9712_enum[11]);
  265. static const struct snd_soc_dapm_widget wm9712_dapm_widgets[] = {
  266. SND_SOC_DAPM_MUX("ALC Sidetone Mux", SND_SOC_NOPM, 0, 0,
  267. &wm9712_alc_mux_controls),
  268. SND_SOC_DAPM_MUX("Out3 Mux", SND_SOC_NOPM, 0, 0,
  269. &wm9712_out3_mux_controls),
  270. SND_SOC_DAPM_MUX("Speaker Mux", SND_SOC_NOPM, 0, 0,
  271. &wm9712_spk_mux_controls),
  272. SND_SOC_DAPM_MUX("Capture Phone Mux", SND_SOC_NOPM, 0, 0,
  273. &wm9712_capture_phone_mux_controls),
  274. SND_SOC_DAPM_MUX("Left Capture Select", SND_SOC_NOPM, 0, 0,
  275. &wm9712_capture_selectl_controls),
  276. SND_SOC_DAPM_MUX("Right Capture Select", SND_SOC_NOPM, 0, 0,
  277. &wm9712_capture_selectr_controls),
  278. SND_SOC_DAPM_MUX("Mic Select Source", SND_SOC_NOPM, 0, 0,
  279. &wm9712_mic_src_controls),
  280. SND_SOC_DAPM_MUX("Differential Source", SND_SOC_NOPM, 0, 0,
  281. &wm9712_diff_sel_controls),
  282. SND_SOC_DAPM_MIXER("AC97 Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
  283. SND_SOC_DAPM_MIXER_E("Left HP Mixer", AC97_INT_PAGING, 9, 1,
  284. &wm9712_hpl_mixer_controls[0], ARRAY_SIZE(wm9712_hpl_mixer_controls),
  285. mixer_event, SND_SOC_DAPM_POST_REG),
  286. SND_SOC_DAPM_MIXER_E("Right HP Mixer", AC97_INT_PAGING, 8, 1,
  287. &wm9712_hpr_mixer_controls[0], ARRAY_SIZE(wm9712_hpr_mixer_controls),
  288. mixer_event, SND_SOC_DAPM_POST_REG),
  289. SND_SOC_DAPM_MIXER("Phone Mixer", AC97_INT_PAGING, 6, 1,
  290. &wm9712_phone_mixer_controls[0], ARRAY_SIZE(wm9712_phone_mixer_controls)),
  291. SND_SOC_DAPM_MIXER("Speaker Mixer", AC97_INT_PAGING, 7, 1,
  292. &wm9712_speaker_mixer_controls[0],
  293. ARRAY_SIZE(wm9712_speaker_mixer_controls)),
  294. SND_SOC_DAPM_MIXER("Mono Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
  295. SND_SOC_DAPM_DAC("Left DAC", "Left HiFi Playback", AC97_INT_PAGING, 14, 1),
  296. SND_SOC_DAPM_DAC("Right DAC", "Right HiFi Playback", AC97_INT_PAGING, 13, 1),
  297. SND_SOC_DAPM_DAC("Aux DAC", "Aux Playback", SND_SOC_NOPM, 0, 0),
  298. SND_SOC_DAPM_ADC("Left ADC", "Left HiFi Capture", AC97_INT_PAGING, 12, 1),
  299. SND_SOC_DAPM_ADC("Right ADC", "Right HiFi Capture", AC97_INT_PAGING, 11, 1),
  300. SND_SOC_DAPM_PGA("Headphone PGA", AC97_INT_PAGING, 4, 1, NULL, 0),
  301. SND_SOC_DAPM_PGA("Speaker PGA", AC97_INT_PAGING, 3, 1, NULL, 0),
  302. SND_SOC_DAPM_PGA("Out 3 PGA", AC97_INT_PAGING, 5, 1, NULL, 0),
  303. SND_SOC_DAPM_PGA("Line PGA", AC97_INT_PAGING, 2, 1, NULL, 0),
  304. SND_SOC_DAPM_PGA("Phone PGA", AC97_INT_PAGING, 1, 1, NULL, 0),
  305. SND_SOC_DAPM_PGA("Mic PGA", AC97_INT_PAGING, 0, 1, NULL, 0),
  306. SND_SOC_DAPM_MICBIAS("Mic Bias", AC97_INT_PAGING, 10, 1),
  307. SND_SOC_DAPM_OUTPUT("MONOOUT"),
  308. SND_SOC_DAPM_OUTPUT("HPOUTL"),
  309. SND_SOC_DAPM_OUTPUT("HPOUTR"),
  310. SND_SOC_DAPM_OUTPUT("LOUT2"),
  311. SND_SOC_DAPM_OUTPUT("ROUT2"),
  312. SND_SOC_DAPM_OUTPUT("OUT3"),
  313. SND_SOC_DAPM_INPUT("LINEINL"),
  314. SND_SOC_DAPM_INPUT("LINEINR"),
  315. SND_SOC_DAPM_INPUT("PHONE"),
  316. SND_SOC_DAPM_INPUT("PCBEEP"),
  317. SND_SOC_DAPM_INPUT("MIC1"),
  318. SND_SOC_DAPM_INPUT("MIC2"),
  319. };
  320. static const char *audio_map[][3] = {
  321. /* virtual mixer - mixes left & right channels for spk and mono */
  322. {"AC97 Mixer", NULL, "Left DAC"},
  323. {"AC97 Mixer", NULL, "Right DAC"},
  324. /* Left HP mixer */
  325. {"Left HP Mixer", "PCBeep Bypass Switch", "PCBEEP"},
  326. {"Left HP Mixer", "Aux Playback Switch", "Aux DAC"},
  327. {"Left HP Mixer", "Phone Bypass Switch", "Phone PGA"},
  328. {"Left HP Mixer", "Line Bypass Switch", "Line PGA"},
  329. {"Left HP Mixer", "PCM Playback Switch", "Left DAC"},
  330. {"Left HP Mixer", "Mic Sidetone Switch", "Mic PGA"},
  331. {"Left HP Mixer", NULL, "ALC Sidetone Mux"},
  332. //{"Right HP Mixer", NULL, "HP Mixer"},
  333. /* Right HP mixer */
  334. {"Right HP Mixer", "PCBeep Bypass Switch", "PCBEEP"},
  335. {"Right HP Mixer", "Aux Playback Switch", "Aux DAC"},
  336. {"Right HP Mixer", "Phone Bypass Switch", "Phone PGA"},
  337. {"Right HP Mixer", "Line Bypass Switch", "Line PGA"},
  338. {"Right HP Mixer", "PCM Playback Switch", "Right DAC"},
  339. {"Right HP Mixer", "Mic Sidetone Switch", "Mic PGA"},
  340. {"Right HP Mixer", NULL, "ALC Sidetone Mux"},
  341. /* speaker mixer */
  342. {"Speaker Mixer", "PCBeep Bypass Switch", "PCBEEP"},
  343. {"Speaker Mixer", "Line Bypass Switch", "Line PGA"},
  344. {"Speaker Mixer", "PCM Playback Switch", "AC97 Mixer"},
  345. {"Speaker Mixer", "Phone Bypass Switch", "Phone PGA"},
  346. {"Speaker Mixer", "Aux Playback Switch", "Aux DAC"},
  347. /* Phone mixer */
  348. {"Phone Mixer", "PCBeep Bypass Switch", "PCBEEP"},
  349. {"Phone Mixer", "Line Bypass Switch", "Line PGA"},
  350. {"Phone Mixer", "Aux Playback Switch", "Aux DAC"},
  351. {"Phone Mixer", "PCM Playback Switch", "AC97 Mixer"},
  352. {"Phone Mixer", "Mic 1 Sidetone Switch", "Mic PGA"},
  353. {"Phone Mixer", "Mic 2 Sidetone Switch", "Mic PGA"},
  354. /* inputs */
  355. {"Line PGA", NULL, "LINEINL"},
  356. {"Line PGA", NULL, "LINEINR"},
  357. {"Phone PGA", NULL, "PHONE"},
  358. {"Mic PGA", NULL, "MIC1"},
  359. {"Mic PGA", NULL, "MIC2"},
  360. /* left capture selector */
  361. {"Left Capture Select", "Mic", "MIC1"},
  362. {"Left Capture Select", "Speaker Mixer", "Speaker Mixer"},
  363. {"Left Capture Select", "Line", "LINEINL"},
  364. {"Left Capture Select", "Headphone Mixer", "Left HP Mixer"},
  365. {"Left Capture Select", "Phone Mixer", "Phone Mixer"},
  366. {"Left Capture Select", "Phone", "PHONE"},
  367. /* right capture selector */
  368. {"Right Capture Select", "Mic", "MIC2"},
  369. {"Right Capture Select", "Speaker Mixer", "Speaker Mixer"},
  370. {"Right Capture Select", "Line", "LINEINR"},
  371. {"Right Capture Select", "Headphone Mixer", "Right HP Mixer"},
  372. {"Right Capture Select", "Phone Mixer", "Phone Mixer"},
  373. {"Right Capture Select", "Phone", "PHONE"},
  374. /* ALC Sidetone */
  375. {"ALC Sidetone Mux", "Stereo", "Left Capture Select"},
  376. {"ALC Sidetone Mux", "Stereo", "Right Capture Select"},
  377. {"ALC Sidetone Mux", "Left", "Left Capture Select"},
  378. {"ALC Sidetone Mux", "Right", "Right Capture Select"},
  379. /* ADC's */
  380. {"Left ADC", NULL, "Left Capture Select"},
  381. {"Right ADC", NULL, "Right Capture Select"},
  382. /* outputs */
  383. {"MONOOUT", NULL, "Phone Mixer"},
  384. {"HPOUTL", NULL, "Headphone PGA"},
  385. {"Headphone PGA", NULL, "Left HP Mixer"},
  386. {"HPOUTR", NULL, "Headphone PGA"},
  387. {"Headphone PGA", NULL, "Right HP Mixer"},
  388. /* mono hp mixer */
  389. {"Mono HP Mixer", NULL, "Left HP Mixer"},
  390. {"Mono HP Mixer", NULL, "Right HP Mixer"},
  391. /* Out3 Mux */
  392. {"Out3 Mux", "Left", "Left HP Mixer"},
  393. {"Out3 Mux", "Mono", "Phone Mixer"},
  394. {"Out3 Mux", "Left + Right", "Mono HP Mixer"},
  395. {"Out 3 PGA", NULL, "Out3 Mux"},
  396. {"OUT3", NULL, "Out 3 PGA"},
  397. /* speaker Mux */
  398. {"Speaker Mux", "Speaker Mix", "Speaker Mixer"},
  399. {"Speaker Mux", "Headphone Mix", "Mono HP Mixer"},
  400. {"Speaker PGA", NULL, "Speaker Mux"},
  401. {"LOUT2", NULL, "Speaker PGA"},
  402. {"ROUT2", NULL, "Speaker PGA"},
  403. {NULL, NULL, NULL},
  404. };
  405. static int wm9712_add_widgets(struct snd_soc_codec *codec)
  406. {
  407. int i;
  408. for(i = 0; i < ARRAY_SIZE(wm9712_dapm_widgets); i++) {
  409. snd_soc_dapm_new_control(codec, &wm9712_dapm_widgets[i]);
  410. }
  411. /* set up audio path audio_mapnects */
  412. for(i = 0; audio_map[i][0] != NULL; i++) {
  413. snd_soc_dapm_connect_input(codec, audio_map[i][0],
  414. audio_map[i][1], audio_map[i][2]);
  415. }
  416. snd_soc_dapm_new_widgets(codec);
  417. return 0;
  418. }
  419. static unsigned int ac97_read(struct snd_soc_codec *codec,
  420. unsigned int reg)
  421. {
  422. u16 *cache = codec->reg_cache;
  423. if (reg == AC97_RESET || reg == AC97_GPIO_STATUS ||
  424. reg == AC97_VENDOR_ID1 || reg == AC97_VENDOR_ID2 ||
  425. reg == AC97_REC_GAIN)
  426. return soc_ac97_ops.read(codec->ac97, reg);
  427. else {
  428. reg = reg >> 1;
  429. if (reg > (ARRAY_SIZE(wm9712_reg)))
  430. return -EIO;
  431. return cache[reg];
  432. }
  433. }
  434. static int ac97_write(struct snd_soc_codec *codec, unsigned int reg,
  435. unsigned int val)
  436. {
  437. u16 *cache = codec->reg_cache;
  438. soc_ac97_ops.write(codec->ac97, reg, val);
  439. reg = reg >> 1;
  440. if (reg <= (ARRAY_SIZE(wm9712_reg)))
  441. cache[reg] = val;
  442. return 0;
  443. }
  444. static int ac97_prepare(struct snd_pcm_substream *substream)
  445. {
  446. struct snd_pcm_runtime *runtime = substream->runtime;
  447. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  448. struct snd_soc_device *socdev = rtd->socdev;
  449. struct snd_soc_codec *codec = socdev->codec;
  450. int reg;
  451. u16 vra;
  452. vra = ac97_read(codec, AC97_EXTENDED_STATUS);
  453. ac97_write(codec, AC97_EXTENDED_STATUS, vra | 0x1);
  454. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  455. reg = AC97_PCM_FRONT_DAC_RATE;
  456. else
  457. reg = AC97_PCM_LR_ADC_RATE;
  458. return ac97_write(codec, reg, runtime->rate);
  459. }
  460. static int ac97_aux_prepare(struct snd_pcm_substream *substream)
  461. {
  462. struct snd_pcm_runtime *runtime = substream->runtime;
  463. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  464. struct snd_soc_device *socdev = rtd->socdev;
  465. struct snd_soc_codec *codec = socdev->codec;
  466. u16 vra, xsle;
  467. vra = ac97_read(codec, AC97_EXTENDED_STATUS);
  468. ac97_write(codec, AC97_EXTENDED_STATUS, vra | 0x1);
  469. xsle = ac97_read(codec, AC97_PCI_SID);
  470. ac97_write(codec, AC97_PCI_SID, xsle | 0x8000);
  471. if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
  472. return -ENODEV;
  473. return ac97_write(codec, AC97_PCM_SURR_DAC_RATE, runtime->rate);
  474. }
  475. struct snd_soc_codec_dai wm9712_dai[] = {
  476. {
  477. .name = "AC97 HiFi",
  478. .playback = {
  479. .stream_name = "HiFi Playback",
  480. .channels_min = 1,
  481. .channels_max = 2,},
  482. .capture = {
  483. .stream_name = "HiFi Capture",
  484. .channels_min = 1,
  485. .channels_max = 2,},
  486. .ops = {
  487. .prepare = ac97_prepare,},
  488. .caps = {
  489. .num_modes = ARRAY_SIZE(ac97_modes),
  490. .mode = ac97_modes,},
  491. },
  492. {
  493. .name = "AC97 Aux",
  494. .playback = {
  495. .stream_name = "Aux Playback",
  496. .channels_min = 1,
  497. .channels_max = 1,},
  498. .ops = {
  499. .prepare = ac97_aux_prepare,},
  500. .caps = {
  501. .num_modes = ARRAY_SIZE(ac97_modes),
  502. .mode = ac97_modes,},
  503. },
  504. };
  505. EXPORT_SYMBOL_GPL(wm9712_dai);
  506. static int wm9712_dapm_event(struct snd_soc_codec *codec, int event)
  507. {
  508. u16 reg;
  509. switch (event) {
  510. case SNDRV_CTL_POWER_D0: /* full On */
  511. /* liam - maybe enable thermal shutdown */
  512. reg = ac97_read(codec, AC97_EXTENDED_MID) & 0xdfff;
  513. ac97_write(codec, AC97_EXTENDED_MID, reg);
  514. break;
  515. case SNDRV_CTL_POWER_D1: /* partial On */
  516. case SNDRV_CTL_POWER_D2: /* partial On */
  517. break;
  518. case SNDRV_CTL_POWER_D3hot: /* Off, with power */
  519. /* enable master bias and vmid */
  520. reg = ac97_read(codec, AC97_EXTENDED_MID) & 0xbbff;
  521. ac97_write(codec, AC97_EXTENDED_MID, reg);
  522. ac97_write(codec, AC97_POWERDOWN, 0x0000);
  523. break;
  524. case SNDRV_CTL_POWER_D3cold: /* Off, without power */
  525. /* disable everything including AC link */
  526. ac97_write(codec, AC97_EXTENDED_MID, 0xffff);
  527. ac97_write(codec, AC97_EXTENDED_MSTATUS, 0xffff);
  528. ac97_write(codec, AC97_POWERDOWN, 0xffff);
  529. break;
  530. }
  531. codec->dapm_state = event;
  532. return 0;
  533. }
  534. static int wm9712_reset(struct snd_soc_codec *codec, int try_warm)
  535. {
  536. if (try_warm && soc_ac97_ops.warm_reset) {
  537. soc_ac97_ops.warm_reset(codec->ac97);
  538. if (!(ac97_read(codec, 0) & 0x8000))
  539. return 1;
  540. }
  541. soc_ac97_ops.reset(codec->ac97);
  542. if (ac97_read(codec, 0) & 0x8000)
  543. goto err;
  544. return 0;
  545. err:
  546. printk(KERN_ERR "WM9712 AC97 reset failed\n");
  547. return -EIO;
  548. }
  549. static int wm9712_soc_suspend(struct platform_device *pdev,
  550. pm_message_t state)
  551. {
  552. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  553. struct snd_soc_codec *codec = socdev->codec;
  554. wm9712_dapm_event(codec, SNDRV_CTL_POWER_D3cold);
  555. return 0;
  556. }
  557. static int wm9712_soc_resume(struct platform_device *pdev)
  558. {
  559. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  560. struct snd_soc_codec *codec = socdev->codec;
  561. int i, ret;
  562. u16 *cache = codec->reg_cache;
  563. ret = wm9712_reset(codec, 1);
  564. if (ret < 0){
  565. printk(KERN_ERR "could not reset AC97 codec\n");
  566. return ret;
  567. }
  568. wm9712_dapm_event(codec, SNDRV_CTL_POWER_D3hot);
  569. if (ret == 0) {
  570. /* Sync reg_cache with the hardware after cold reset */
  571. for (i = 2; i < ARRAY_SIZE(wm9712_reg) << 1; i+=2) {
  572. if (i == AC97_INT_PAGING || i == AC97_POWERDOWN ||
  573. (i > 0x58 && i != 0x5c))
  574. continue;
  575. soc_ac97_ops.write(codec->ac97, i, cache[i>>1]);
  576. }
  577. }
  578. if (codec->suspend_dapm_state == SNDRV_CTL_POWER_D0)
  579. wm9712_dapm_event(codec, SNDRV_CTL_POWER_D0);
  580. return ret;
  581. }
  582. static int wm9712_soc_probe(struct platform_device *pdev)
  583. {
  584. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  585. struct snd_soc_codec *codec;
  586. int ret = 0;
  587. printk(KERN_INFO "WM9711/WM9712 SoC Audio Codec %s\n", WM9712_VERSION);
  588. socdev->codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
  589. if (socdev->codec == NULL)
  590. return -ENOMEM;
  591. codec = socdev->codec;
  592. mutex_init(&codec->mutex);
  593. codec->reg_cache =
  594. kzalloc(sizeof(u16) * ARRAY_SIZE(wm9712_reg), GFP_KERNEL);
  595. if (codec->reg_cache == NULL) {
  596. kfree(codec->ac97);
  597. kfree(socdev->codec);
  598. socdev->codec = NULL;
  599. return -ENOMEM;
  600. }
  601. memcpy(codec->reg_cache, wm9712_reg, sizeof(u16) * ARRAY_SIZE(wm9712_reg));
  602. codec->reg_cache_size = sizeof(u16) * ARRAY_SIZE(wm9712_reg);
  603. codec->reg_cache_step = 2;
  604. codec->name = "WM9712";
  605. codec->owner = THIS_MODULE;
  606. codec->dai = wm9712_dai;
  607. codec->num_dai = ARRAY_SIZE(wm9712_dai);
  608. codec->write = ac97_write;
  609. codec->read = ac97_read;
  610. codec->dapm_event = wm9712_dapm_event;
  611. INIT_LIST_HEAD(&codec->dapm_widgets);
  612. INIT_LIST_HEAD(&codec->dapm_paths);
  613. ret = snd_soc_new_ac97_codec(codec, &soc_ac97_ops, 0);
  614. if (ret < 0)
  615. goto err;
  616. /* register pcms */
  617. ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
  618. if (ret < 0)
  619. goto pcm_err;
  620. ret = wm9712_reset(codec, 0);
  621. if (ret < 0) {
  622. printk(KERN_ERR "AC97 link error\n");
  623. goto reset_err;
  624. }
  625. /* set alc mux to none */
  626. ac97_write(codec, AC97_VIDEO, ac97_read(codec, AC97_VIDEO) | 0x3000);
  627. wm9712_dapm_event(codec, SNDRV_CTL_POWER_D3hot);
  628. wm9712_add_controls(codec);
  629. wm9712_add_widgets(codec);
  630. ret = snd_soc_register_card(socdev);
  631. if (ret < 0)
  632. goto reset_err;
  633. return 0;
  634. reset_err:
  635. snd_soc_free_pcms(socdev);
  636. pcm_err:
  637. snd_soc_free_ac97_codec(codec);
  638. err:
  639. kfree(socdev->codec->reg_cache);
  640. kfree(socdev->codec);
  641. socdev->codec = NULL;
  642. return ret;
  643. }
  644. static int wm9712_soc_remove(struct platform_device *pdev)
  645. {
  646. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  647. struct snd_soc_codec *codec = socdev->codec;
  648. if (codec == NULL)
  649. return 0;
  650. snd_soc_dapm_free(socdev);
  651. snd_soc_free_pcms(socdev);
  652. snd_soc_free_ac97_codec(codec);
  653. kfree(codec->reg_cache);
  654. kfree(codec);
  655. return 0;
  656. }
  657. struct snd_soc_codec_device soc_codec_dev_wm9712 = {
  658. .probe = wm9712_soc_probe,
  659. .remove = wm9712_soc_remove,
  660. .suspend = wm9712_soc_suspend,
  661. .resume = wm9712_soc_resume,
  662. };
  663. EXPORT_SYMBOL_GPL(soc_codec_dev_wm9712);
  664. MODULE_DESCRIPTION("ASoC WM9711/WM9712 driver");
  665. MODULE_AUTHOR("Liam Girdwood");
  666. MODULE_LICENSE("GPL");