wm9713.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295
  1. /*
  2. * wm9713.c -- ALSA Soc WM9713 codec support
  3. *
  4. * Copyright 2006 Wolfson Microelectronics PLC.
  5. * Author: Liam Girdwood <lrg@slimlogic.co.uk>
  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, Voice DAC and Aux DAC
  15. * o Support for DAPM
  16. */
  17. #include <linux/init.h>
  18. #include <linux/slab.h>
  19. #include <linux/module.h>
  20. #include <linux/device.h>
  21. #include <sound/core.h>
  22. #include <sound/pcm.h>
  23. #include <sound/ac97_codec.h>
  24. #include <sound/initval.h>
  25. #include <sound/pcm_params.h>
  26. #include <sound/tlv.h>
  27. #include <sound/soc.h>
  28. #include "wm9713.h"
  29. struct wm9713_priv {
  30. u32 pll_in; /* PLL input frequency */
  31. };
  32. static unsigned int ac97_read(struct snd_soc_codec *codec,
  33. unsigned int reg);
  34. static int ac97_write(struct snd_soc_codec *codec,
  35. unsigned int reg, unsigned int val);
  36. /*
  37. * WM9713 register cache
  38. * Reg 0x3c bit 15 is used by touch driver.
  39. */
  40. static const u16 wm9713_reg[] = {
  41. 0x6174, 0x8080, 0x8080, 0x8080,
  42. 0xc880, 0xe808, 0xe808, 0x0808,
  43. 0x00da, 0x8000, 0xd600, 0xaaa0,
  44. 0xaaa0, 0xaaa0, 0x0000, 0x0000,
  45. 0x0f0f, 0x0040, 0x0000, 0x7f00,
  46. 0x0405, 0x0410, 0xbb80, 0xbb80,
  47. 0x0000, 0xbb80, 0x0000, 0x4523,
  48. 0x0000, 0x2000, 0x7eff, 0xffff,
  49. 0x0000, 0x0000, 0x0080, 0x0000,
  50. 0x0000, 0x0000, 0xfffe, 0xffff,
  51. 0x0000, 0x0000, 0x0000, 0xfffe,
  52. 0x4000, 0x0000, 0x0000, 0x0000,
  53. 0xb032, 0x3e00, 0x0000, 0x0000,
  54. 0x0000, 0x0000, 0x0000, 0x0000,
  55. 0x0000, 0x0000, 0x0000, 0x0006,
  56. 0x0001, 0x0000, 0x574d, 0x4c13,
  57. 0x0000, 0x0000, 0x0000
  58. };
  59. /* virtual HP mixers regs */
  60. #define HPL_MIXER 0x80
  61. #define HPR_MIXER 0x82
  62. #define MICB_MUX 0x82
  63. static const char *wm9713_mic_mixer[] = {"Stereo", "Mic 1", "Mic 2", "Mute"};
  64. static const char *wm9713_rec_mux[] = {"Stereo", "Left", "Right", "Mute"};
  65. static const char *wm9713_rec_src[] =
  66. {"Mic 1", "Mic 2", "Line", "Mono In", "Headphone", "Speaker",
  67. "Mono Out", "Zh"};
  68. static const char *wm9713_rec_gain[] = {"+1.5dB Steps", "+0.75dB Steps"};
  69. static const char *wm9713_alc_select[] = {"None", "Left", "Right", "Stereo"};
  70. static const char *wm9713_mono_pga[] = {"Vmid", "Zh", "Mono", "Inv",
  71. "Mono Vmid", "Inv Vmid"};
  72. static const char *wm9713_spk_pga[] =
  73. {"Vmid", "Zh", "Headphone", "Speaker", "Inv", "Headphone Vmid",
  74. "Speaker Vmid", "Inv Vmid"};
  75. static const char *wm9713_hp_pga[] = {"Vmid", "Zh", "Headphone",
  76. "Headphone Vmid"};
  77. static const char *wm9713_out3_pga[] = {"Vmid", "Zh", "Inv 1", "Inv 1 Vmid"};
  78. static const char *wm9713_out4_pga[] = {"Vmid", "Zh", "Inv 2", "Inv 2 Vmid"};
  79. static const char *wm9713_dac_inv[] =
  80. {"Off", "Mono", "Speaker", "Left Headphone", "Right Headphone",
  81. "Headphone Mono", "NC", "Vmid"};
  82. static const char *wm9713_bass[] = {"Linear Control", "Adaptive Boost"};
  83. static const char *wm9713_ng_type[] = {"Constant Gain", "Mute"};
  84. static const char *wm9713_mic_select[] = {"Mic 1", "Mic 2 A", "Mic 2 B"};
  85. static const char *wm9713_micb_select[] = {"MPB", "MPA"};
  86. static const struct soc_enum wm9713_enum[] = {
  87. SOC_ENUM_SINGLE(AC97_LINE, 3, 4, wm9713_mic_mixer), /* record mic mixer 0 */
  88. SOC_ENUM_SINGLE(AC97_VIDEO, 14, 4, wm9713_rec_mux), /* record mux hp 1 */
  89. SOC_ENUM_SINGLE(AC97_VIDEO, 9, 4, wm9713_rec_mux), /* record mux mono 2 */
  90. SOC_ENUM_SINGLE(AC97_VIDEO, 3, 8, wm9713_rec_src), /* record mux left 3 */
  91. SOC_ENUM_SINGLE(AC97_VIDEO, 0, 8, wm9713_rec_src), /* record mux right 4*/
  92. SOC_ENUM_DOUBLE(AC97_CD, 14, 6, 2, wm9713_rec_gain), /* record step size 5 */
  93. SOC_ENUM_SINGLE(AC97_PCI_SVID, 14, 4, wm9713_alc_select), /* alc source select 6*/
  94. SOC_ENUM_SINGLE(AC97_REC_GAIN, 14, 4, wm9713_mono_pga), /* mono input select 7 */
  95. SOC_ENUM_SINGLE(AC97_REC_GAIN, 11, 8, wm9713_spk_pga), /* speaker left input select 8 */
  96. SOC_ENUM_SINGLE(AC97_REC_GAIN, 8, 8, wm9713_spk_pga), /* speaker right input select 9 */
  97. SOC_ENUM_SINGLE(AC97_REC_GAIN, 6, 3, wm9713_hp_pga), /* headphone left input 10 */
  98. SOC_ENUM_SINGLE(AC97_REC_GAIN, 4, 3, wm9713_hp_pga), /* headphone right input 11 */
  99. SOC_ENUM_SINGLE(AC97_REC_GAIN, 2, 4, wm9713_out3_pga), /* out 3 source 12 */
  100. SOC_ENUM_SINGLE(AC97_REC_GAIN, 0, 4, wm9713_out4_pga), /* out 4 source 13 */
  101. SOC_ENUM_SINGLE(AC97_REC_GAIN_MIC, 13, 8, wm9713_dac_inv), /* dac invert 1 14 */
  102. SOC_ENUM_SINGLE(AC97_REC_GAIN_MIC, 10, 8, wm9713_dac_inv), /* dac invert 2 15 */
  103. SOC_ENUM_SINGLE(AC97_GENERAL_PURPOSE, 15, 2, wm9713_bass), /* bass control 16 */
  104. SOC_ENUM_SINGLE(AC97_PCI_SVID, 5, 2, wm9713_ng_type), /* noise gate type 17 */
  105. SOC_ENUM_SINGLE(AC97_3D_CONTROL, 12, 3, wm9713_mic_select), /* mic selection 18 */
  106. SOC_ENUM_SINGLE(MICB_MUX, 0, 2, wm9713_micb_select), /* mic selection 19 */
  107. };
  108. static const DECLARE_TLV_DB_SCALE(out_tlv, -4650, 150, 0);
  109. static const DECLARE_TLV_DB_SCALE(main_tlv, -3450, 150, 0);
  110. static const DECLARE_TLV_DB_SCALE(misc_tlv, -1500, 300, 0);
  111. static unsigned int mic_tlv[] = {
  112. TLV_DB_RANGE_HEAD(2),
  113. 0, 2, TLV_DB_SCALE_ITEM(1200, 600, 0),
  114. 3, 3, TLV_DB_SCALE_ITEM(3000, 0, 0),
  115. };
  116. static const struct snd_kcontrol_new wm9713_snd_ac97_controls[] = {
  117. SOC_DOUBLE_TLV("Speaker Playback Volume", AC97_MASTER, 8, 0, 31, 1, out_tlv),
  118. SOC_DOUBLE("Speaker Playback Switch", AC97_MASTER, 15, 7, 1, 1),
  119. SOC_DOUBLE_TLV("Headphone Playback Volume", AC97_HEADPHONE, 8, 0, 31, 1,
  120. out_tlv),
  121. SOC_DOUBLE("Headphone Playback Switch", AC97_HEADPHONE, 15, 7, 1, 1),
  122. SOC_DOUBLE_TLV("Line In Volume", AC97_PC_BEEP, 8, 0, 31, 1, main_tlv),
  123. SOC_DOUBLE_TLV("PCM Playback Volume", AC97_PHONE, 8, 0, 31, 1, main_tlv),
  124. SOC_SINGLE_TLV("Mic 1 Volume", AC97_MIC, 8, 31, 1, main_tlv),
  125. SOC_SINGLE_TLV("Mic 2 Volume", AC97_MIC, 0, 31, 1, main_tlv),
  126. SOC_SINGLE_TLV("Mic 1 Preamp Volume", AC97_3D_CONTROL, 10, 3, 0, mic_tlv),
  127. SOC_SINGLE_TLV("Mic 2 Preamp Volume", AC97_3D_CONTROL, 12, 3, 0, mic_tlv),
  128. SOC_SINGLE("Mic Boost (+20dB) Switch", AC97_LINE, 5, 1, 0),
  129. SOC_SINGLE("Mic Headphone Mixer Volume", AC97_LINE, 0, 7, 1),
  130. SOC_SINGLE("Capture Switch", AC97_CD, 15, 1, 1),
  131. SOC_ENUM("Capture Volume Steps", wm9713_enum[5]),
  132. SOC_DOUBLE("Capture Volume", AC97_CD, 8, 0, 31, 0),
  133. SOC_SINGLE("Capture ZC Switch", AC97_CD, 7, 1, 0),
  134. SOC_SINGLE_TLV("Capture to Headphone Volume", AC97_VIDEO, 11, 7, 1, misc_tlv),
  135. SOC_SINGLE("Capture to Mono Boost (+20dB) Switch", AC97_VIDEO, 8, 1, 0),
  136. SOC_SINGLE("Capture ADC Boost (+20dB) Switch", AC97_VIDEO, 6, 1, 0),
  137. SOC_SINGLE("ALC Target Volume", AC97_CODEC_CLASS_REV, 12, 15, 0),
  138. SOC_SINGLE("ALC Hold Time", AC97_CODEC_CLASS_REV, 8, 15, 0),
  139. SOC_SINGLE("ALC Decay Time", AC97_CODEC_CLASS_REV, 4, 15, 0),
  140. SOC_SINGLE("ALC Attack Time", AC97_CODEC_CLASS_REV, 0, 15, 0),
  141. SOC_ENUM("ALC Function", wm9713_enum[6]),
  142. SOC_SINGLE("ALC Max Volume", AC97_PCI_SVID, 11, 7, 0),
  143. SOC_SINGLE("ALC ZC Timeout", AC97_PCI_SVID, 9, 3, 0),
  144. SOC_SINGLE("ALC ZC Switch", AC97_PCI_SVID, 8, 1, 0),
  145. SOC_SINGLE("ALC NG Switch", AC97_PCI_SVID, 7, 1, 0),
  146. SOC_ENUM("ALC NG Type", wm9713_enum[17]),
  147. SOC_SINGLE("ALC NG Threshold", AC97_PCI_SVID, 0, 31, 0),
  148. SOC_DOUBLE("Speaker Playback ZC Switch", AC97_MASTER, 14, 6, 1, 0),
  149. SOC_DOUBLE("Headphone Playback ZC Switch", AC97_HEADPHONE, 14, 6, 1, 0),
  150. SOC_SINGLE("Out4 Playback Switch", AC97_MASTER_MONO, 15, 1, 1),
  151. SOC_SINGLE("Out4 Playback ZC Switch", AC97_MASTER_MONO, 14, 1, 0),
  152. SOC_SINGLE_TLV("Out4 Playback Volume", AC97_MASTER_MONO, 8, 31, 1, out_tlv),
  153. SOC_SINGLE("Out3 Playback Switch", AC97_MASTER_MONO, 7, 1, 1),
  154. SOC_SINGLE("Out3 Playback ZC Switch", AC97_MASTER_MONO, 6, 1, 0),
  155. SOC_SINGLE_TLV("Out3 Playback Volume", AC97_MASTER_MONO, 0, 31, 1, out_tlv),
  156. SOC_SINGLE_TLV("Mono Capture Volume", AC97_MASTER_TONE, 8, 31, 1, main_tlv),
  157. SOC_SINGLE("Mono Playback Switch", AC97_MASTER_TONE, 7, 1, 1),
  158. SOC_SINGLE("Mono Playback ZC Switch", AC97_MASTER_TONE, 6, 1, 0),
  159. SOC_SINGLE_TLV("Mono Playback Volume", AC97_MASTER_TONE, 0, 31, 1, out_tlv),
  160. SOC_SINGLE_TLV("Headphone Mixer Beep Playback Volume", AC97_AUX, 12, 7, 1,
  161. misc_tlv),
  162. SOC_SINGLE_TLV("Speaker Mixer Beep Playback Volume", AC97_AUX, 8, 7, 1,
  163. misc_tlv),
  164. SOC_SINGLE_TLV("Mono Mixer Beep Playback Volume", AC97_AUX, 4, 7, 1, misc_tlv),
  165. SOC_SINGLE_TLV("Voice Playback Headphone Volume", AC97_PCM, 12, 7, 1,
  166. misc_tlv),
  167. SOC_SINGLE("Voice Playback Master Volume", AC97_PCM, 8, 7, 1),
  168. SOC_SINGLE("Voice Playback Mono Volume", AC97_PCM, 4, 7, 1),
  169. SOC_SINGLE_TLV("Headphone Mixer Aux Playback Volume", AC97_REC_SEL, 12, 7, 1,
  170. misc_tlv),
  171. SOC_SINGLE_TLV("Speaker Mixer Voice Playback Volume", AC97_PCM, 8, 7, 1,
  172. misc_tlv),
  173. SOC_SINGLE_TLV("Speaker Mixer Aux Playback Volume", AC97_REC_SEL, 8, 7, 1,
  174. misc_tlv),
  175. SOC_SINGLE_TLV("Mono Mixer Voice Playback Volume", AC97_PCM, 4, 7, 1,
  176. misc_tlv),
  177. SOC_SINGLE_TLV("Mono Mixer Aux Playback Volume", AC97_REC_SEL, 4, 7, 1,
  178. misc_tlv),
  179. SOC_SINGLE("Aux Playback Headphone Volume", AC97_REC_SEL, 12, 7, 1),
  180. SOC_SINGLE("Aux Playback Master Volume", AC97_REC_SEL, 8, 7, 1),
  181. SOC_ENUM("Bass Control", wm9713_enum[16]),
  182. SOC_SINGLE("Bass Cut-off Switch", AC97_GENERAL_PURPOSE, 12, 1, 1),
  183. SOC_SINGLE("Tone Cut-off Switch", AC97_GENERAL_PURPOSE, 4, 1, 1),
  184. SOC_SINGLE("Playback Attenuate (-6dB) Switch", AC97_GENERAL_PURPOSE, 6, 1, 0),
  185. SOC_SINGLE("Bass Volume", AC97_GENERAL_PURPOSE, 8, 15, 1),
  186. SOC_SINGLE("Tone Volume", AC97_GENERAL_PURPOSE, 0, 15, 1),
  187. SOC_SINGLE("3D Upper Cut-off Switch", AC97_REC_GAIN_MIC, 5, 1, 0),
  188. SOC_SINGLE("3D Lower Cut-off Switch", AC97_REC_GAIN_MIC, 4, 1, 0),
  189. SOC_SINGLE("3D Depth", AC97_REC_GAIN_MIC, 0, 15, 1),
  190. };
  191. static int wm9713_voice_shutdown(struct snd_soc_dapm_widget *w,
  192. struct snd_kcontrol *kcontrol, int event)
  193. {
  194. struct snd_soc_codec *codec = w->codec;
  195. u16 status, rate;
  196. BUG_ON(event != SND_SOC_DAPM_PRE_PMD);
  197. /* Gracefully shut down the voice interface. */
  198. status = ac97_read(codec, AC97_EXTENDED_MID) | 0x1000;
  199. rate = ac97_read(codec, AC97_HANDSET_RATE) & 0xF0FF;
  200. ac97_write(codec, AC97_HANDSET_RATE, rate | 0x0200);
  201. schedule_timeout_interruptible(msecs_to_jiffies(1));
  202. ac97_write(codec, AC97_HANDSET_RATE, rate | 0x0F00);
  203. ac97_write(codec, AC97_EXTENDED_MID, status);
  204. return 0;
  205. }
  206. /* We have to create a fake left and right HP mixers because
  207. * the codec only has a single control that is shared by both channels.
  208. * This makes it impossible to determine the audio path using the current
  209. * register map, thus we add a new (virtual) register to help determine the
  210. * audio route within the device.
  211. */
  212. static int mixer_event(struct snd_soc_dapm_widget *w,
  213. struct snd_kcontrol *kcontrol, int event)
  214. {
  215. u16 l, r, beep, tone, phone, rec, pcm, aux;
  216. l = ac97_read(w->codec, HPL_MIXER);
  217. r = ac97_read(w->codec, HPR_MIXER);
  218. beep = ac97_read(w->codec, AC97_PC_BEEP);
  219. tone = ac97_read(w->codec, AC97_MASTER_TONE);
  220. phone = ac97_read(w->codec, AC97_PHONE);
  221. rec = ac97_read(w->codec, AC97_REC_SEL);
  222. pcm = ac97_read(w->codec, AC97_PCM);
  223. aux = ac97_read(w->codec, AC97_AUX);
  224. if (event & SND_SOC_DAPM_PRE_REG)
  225. return 0;
  226. if ((l & 0x1) || (r & 0x1))
  227. ac97_write(w->codec, AC97_PC_BEEP, beep & 0x7fff);
  228. else
  229. ac97_write(w->codec, AC97_PC_BEEP, beep | 0x8000);
  230. if ((l & 0x2) || (r & 0x2))
  231. ac97_write(w->codec, AC97_MASTER_TONE, tone & 0x7fff);
  232. else
  233. ac97_write(w->codec, AC97_MASTER_TONE, tone | 0x8000);
  234. if ((l & 0x4) || (r & 0x4))
  235. ac97_write(w->codec, AC97_PHONE, phone & 0x7fff);
  236. else
  237. ac97_write(w->codec, AC97_PHONE, phone | 0x8000);
  238. if ((l & 0x8) || (r & 0x8))
  239. ac97_write(w->codec, AC97_REC_SEL, rec & 0x7fff);
  240. else
  241. ac97_write(w->codec, AC97_REC_SEL, rec | 0x8000);
  242. if ((l & 0x10) || (r & 0x10))
  243. ac97_write(w->codec, AC97_PCM, pcm & 0x7fff);
  244. else
  245. ac97_write(w->codec, AC97_PCM, pcm | 0x8000);
  246. if ((l & 0x20) || (r & 0x20))
  247. ac97_write(w->codec, AC97_AUX, aux & 0x7fff);
  248. else
  249. ac97_write(w->codec, AC97_AUX, aux | 0x8000);
  250. return 0;
  251. }
  252. /* Left Headphone Mixers */
  253. static const struct snd_kcontrol_new wm9713_hpl_mixer_controls[] = {
  254. SOC_DAPM_SINGLE("Beep Playback Switch", HPL_MIXER, 5, 1, 0),
  255. SOC_DAPM_SINGLE("Voice Playback Switch", HPL_MIXER, 4, 1, 0),
  256. SOC_DAPM_SINGLE("Aux Playback Switch", HPL_MIXER, 3, 1, 0),
  257. SOC_DAPM_SINGLE("PCM Playback Switch", HPL_MIXER, 2, 1, 0),
  258. SOC_DAPM_SINGLE("MonoIn Playback Switch", HPL_MIXER, 1, 1, 0),
  259. SOC_DAPM_SINGLE("Bypass Playback Switch", HPL_MIXER, 0, 1, 0),
  260. };
  261. /* Right Headphone Mixers */
  262. static const struct snd_kcontrol_new wm9713_hpr_mixer_controls[] = {
  263. SOC_DAPM_SINGLE("Beep Playback Switch", HPR_MIXER, 5, 1, 0),
  264. SOC_DAPM_SINGLE("Voice Playback Switch", HPR_MIXER, 4, 1, 0),
  265. SOC_DAPM_SINGLE("Aux Playback Switch", HPR_MIXER, 3, 1, 0),
  266. SOC_DAPM_SINGLE("PCM Playback Switch", HPR_MIXER, 2, 1, 0),
  267. SOC_DAPM_SINGLE("MonoIn Playback Switch", HPR_MIXER, 1, 1, 0),
  268. SOC_DAPM_SINGLE("Bypass Playback Switch", HPR_MIXER, 0, 1, 0),
  269. };
  270. /* headphone capture mux */
  271. static const struct snd_kcontrol_new wm9713_hp_rec_mux_controls =
  272. SOC_DAPM_ENUM("Route", wm9713_enum[1]);
  273. /* headphone mic mux */
  274. static const struct snd_kcontrol_new wm9713_hp_mic_mux_controls =
  275. SOC_DAPM_ENUM("Route", wm9713_enum[0]);
  276. /* Speaker Mixer */
  277. static const struct snd_kcontrol_new wm9713_speaker_mixer_controls[] = {
  278. SOC_DAPM_SINGLE("Beep Playback Switch", AC97_AUX, 11, 1, 1),
  279. SOC_DAPM_SINGLE("Voice Playback Switch", AC97_PCM, 11, 1, 1),
  280. SOC_DAPM_SINGLE("Aux Playback Switch", AC97_REC_SEL, 11, 1, 1),
  281. SOC_DAPM_SINGLE("PCM Playback Switch", AC97_PHONE, 14, 1, 1),
  282. SOC_DAPM_SINGLE("MonoIn Playback Switch", AC97_MASTER_TONE, 14, 1, 1),
  283. SOC_DAPM_SINGLE("Bypass Playback Switch", AC97_PC_BEEP, 14, 1, 1),
  284. };
  285. /* Mono Mixer */
  286. static const struct snd_kcontrol_new wm9713_mono_mixer_controls[] = {
  287. SOC_DAPM_SINGLE("Beep Playback Switch", AC97_AUX, 7, 1, 1),
  288. SOC_DAPM_SINGLE("Voice Playback Switch", AC97_PCM, 7, 1, 1),
  289. SOC_DAPM_SINGLE("Aux Playback Switch", AC97_REC_SEL, 7, 1, 1),
  290. SOC_DAPM_SINGLE("PCM Playback Switch", AC97_PHONE, 13, 1, 1),
  291. SOC_DAPM_SINGLE("MonoIn Playback Switch", AC97_MASTER_TONE, 13, 1, 1),
  292. SOC_DAPM_SINGLE("Bypass Playback Switch", AC97_PC_BEEP, 13, 1, 1),
  293. SOC_DAPM_SINGLE("Mic 1 Sidetone Switch", AC97_LINE, 7, 1, 1),
  294. SOC_DAPM_SINGLE("Mic 2 Sidetone Switch", AC97_LINE, 6, 1, 1),
  295. };
  296. /* mono mic mux */
  297. static const struct snd_kcontrol_new wm9713_mono_mic_mux_controls =
  298. SOC_DAPM_ENUM("Route", wm9713_enum[2]);
  299. /* mono output mux */
  300. static const struct snd_kcontrol_new wm9713_mono_mux_controls =
  301. SOC_DAPM_ENUM("Route", wm9713_enum[7]);
  302. /* speaker left output mux */
  303. static const struct snd_kcontrol_new wm9713_hp_spkl_mux_controls =
  304. SOC_DAPM_ENUM("Route", wm9713_enum[8]);
  305. /* speaker right output mux */
  306. static const struct snd_kcontrol_new wm9713_hp_spkr_mux_controls =
  307. SOC_DAPM_ENUM("Route", wm9713_enum[9]);
  308. /* headphone left output mux */
  309. static const struct snd_kcontrol_new wm9713_hpl_out_mux_controls =
  310. SOC_DAPM_ENUM("Route", wm9713_enum[10]);
  311. /* headphone right output mux */
  312. static const struct snd_kcontrol_new wm9713_hpr_out_mux_controls =
  313. SOC_DAPM_ENUM("Route", wm9713_enum[11]);
  314. /* Out3 mux */
  315. static const struct snd_kcontrol_new wm9713_out3_mux_controls =
  316. SOC_DAPM_ENUM("Route", wm9713_enum[12]);
  317. /* Out4 mux */
  318. static const struct snd_kcontrol_new wm9713_out4_mux_controls =
  319. SOC_DAPM_ENUM("Route", wm9713_enum[13]);
  320. /* DAC inv mux 1 */
  321. static const struct snd_kcontrol_new wm9713_dac_inv1_mux_controls =
  322. SOC_DAPM_ENUM("Route", wm9713_enum[14]);
  323. /* DAC inv mux 2 */
  324. static const struct snd_kcontrol_new wm9713_dac_inv2_mux_controls =
  325. SOC_DAPM_ENUM("Route", wm9713_enum[15]);
  326. /* Capture source left */
  327. static const struct snd_kcontrol_new wm9713_rec_srcl_mux_controls =
  328. SOC_DAPM_ENUM("Route", wm9713_enum[3]);
  329. /* Capture source right */
  330. static const struct snd_kcontrol_new wm9713_rec_srcr_mux_controls =
  331. SOC_DAPM_ENUM("Route", wm9713_enum[4]);
  332. /* mic source */
  333. static const struct snd_kcontrol_new wm9713_mic_sel_mux_controls =
  334. SOC_DAPM_ENUM("Route", wm9713_enum[18]);
  335. /* mic source B virtual control */
  336. static const struct snd_kcontrol_new wm9713_micb_sel_mux_controls =
  337. SOC_DAPM_ENUM("Route", wm9713_enum[19]);
  338. static const struct snd_soc_dapm_widget wm9713_dapm_widgets[] = {
  339. SND_SOC_DAPM_MUX("Capture Headphone Mux", SND_SOC_NOPM, 0, 0,
  340. &wm9713_hp_rec_mux_controls),
  341. SND_SOC_DAPM_MUX("Sidetone Mux", SND_SOC_NOPM, 0, 0,
  342. &wm9713_hp_mic_mux_controls),
  343. SND_SOC_DAPM_MUX("Capture Mono Mux", SND_SOC_NOPM, 0, 0,
  344. &wm9713_mono_mic_mux_controls),
  345. SND_SOC_DAPM_MUX("Mono Out Mux", SND_SOC_NOPM, 0, 0,
  346. &wm9713_mono_mux_controls),
  347. SND_SOC_DAPM_MUX("Left Speaker Out Mux", SND_SOC_NOPM, 0, 0,
  348. &wm9713_hp_spkl_mux_controls),
  349. SND_SOC_DAPM_MUX("Right Speaker Out Mux", SND_SOC_NOPM, 0, 0,
  350. &wm9713_hp_spkr_mux_controls),
  351. SND_SOC_DAPM_MUX("Left Headphone Out Mux", SND_SOC_NOPM, 0, 0,
  352. &wm9713_hpl_out_mux_controls),
  353. SND_SOC_DAPM_MUX("Right Headphone Out Mux", SND_SOC_NOPM, 0, 0,
  354. &wm9713_hpr_out_mux_controls),
  355. SND_SOC_DAPM_MUX("Out 3 Mux", SND_SOC_NOPM, 0, 0,
  356. &wm9713_out3_mux_controls),
  357. SND_SOC_DAPM_MUX("Out 4 Mux", SND_SOC_NOPM, 0, 0,
  358. &wm9713_out4_mux_controls),
  359. SND_SOC_DAPM_MUX("DAC Inv Mux 1", SND_SOC_NOPM, 0, 0,
  360. &wm9713_dac_inv1_mux_controls),
  361. SND_SOC_DAPM_MUX("DAC Inv Mux 2", SND_SOC_NOPM, 0, 0,
  362. &wm9713_dac_inv2_mux_controls),
  363. SND_SOC_DAPM_MUX("Left Capture Source", SND_SOC_NOPM, 0, 0,
  364. &wm9713_rec_srcl_mux_controls),
  365. SND_SOC_DAPM_MUX("Right Capture Source", SND_SOC_NOPM, 0, 0,
  366. &wm9713_rec_srcr_mux_controls),
  367. SND_SOC_DAPM_MUX("Mic A Source", SND_SOC_NOPM, 0, 0,
  368. &wm9713_mic_sel_mux_controls),
  369. SND_SOC_DAPM_MUX("Mic B Source", SND_SOC_NOPM, 0, 0,
  370. &wm9713_micb_sel_mux_controls),
  371. SND_SOC_DAPM_MIXER_E("Left HP Mixer", AC97_EXTENDED_MID, 3, 1,
  372. &wm9713_hpl_mixer_controls[0], ARRAY_SIZE(wm9713_hpl_mixer_controls),
  373. mixer_event, SND_SOC_DAPM_POST_REG),
  374. SND_SOC_DAPM_MIXER_E("Right HP Mixer", AC97_EXTENDED_MID, 2, 1,
  375. &wm9713_hpr_mixer_controls[0], ARRAY_SIZE(wm9713_hpr_mixer_controls),
  376. mixer_event, SND_SOC_DAPM_POST_REG),
  377. SND_SOC_DAPM_MIXER("Mono Mixer", AC97_EXTENDED_MID, 0, 1,
  378. &wm9713_mono_mixer_controls[0], ARRAY_SIZE(wm9713_mono_mixer_controls)),
  379. SND_SOC_DAPM_MIXER("Speaker Mixer", AC97_EXTENDED_MID, 1, 1,
  380. &wm9713_speaker_mixer_controls[0],
  381. ARRAY_SIZE(wm9713_speaker_mixer_controls)),
  382. SND_SOC_DAPM_DAC("Left DAC", "Left HiFi Playback", AC97_EXTENDED_MID, 7, 1),
  383. SND_SOC_DAPM_DAC("Right DAC", "Right HiFi Playback", AC97_EXTENDED_MID, 6, 1),
  384. SND_SOC_DAPM_MIXER("AC97 Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
  385. SND_SOC_DAPM_MIXER("HP Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
  386. SND_SOC_DAPM_MIXER("Line Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
  387. SND_SOC_DAPM_MIXER("Capture Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
  388. SND_SOC_DAPM_DAC_E("Voice DAC", "Voice Playback", AC97_EXTENDED_MID, 12, 1,
  389. wm9713_voice_shutdown, SND_SOC_DAPM_PRE_PMD),
  390. SND_SOC_DAPM_DAC("Aux DAC", "Aux Playback", AC97_EXTENDED_MID, 11, 1),
  391. SND_SOC_DAPM_PGA("Left ADC", AC97_EXTENDED_MID, 5, 1, NULL, 0),
  392. SND_SOC_DAPM_PGA("Right ADC", AC97_EXTENDED_MID, 4, 1, NULL, 0),
  393. SND_SOC_DAPM_ADC("Left HiFi ADC", "Left HiFi Capture", SND_SOC_NOPM, 0, 0),
  394. SND_SOC_DAPM_ADC("Right HiFi ADC", "Right HiFi Capture", SND_SOC_NOPM, 0, 0),
  395. SND_SOC_DAPM_ADC("Left Voice ADC", "Left Voice Capture", SND_SOC_NOPM, 0, 0),
  396. SND_SOC_DAPM_ADC("Right Voice ADC", "Right Voice Capture", SND_SOC_NOPM, 0, 0),
  397. SND_SOC_DAPM_PGA("Left Headphone", AC97_EXTENDED_MSTATUS, 10, 1, NULL, 0),
  398. SND_SOC_DAPM_PGA("Right Headphone", AC97_EXTENDED_MSTATUS, 9, 1, NULL, 0),
  399. SND_SOC_DAPM_PGA("Left Speaker", AC97_EXTENDED_MSTATUS, 8, 1, NULL, 0),
  400. SND_SOC_DAPM_PGA("Right Speaker", AC97_EXTENDED_MSTATUS, 7, 1, NULL, 0),
  401. SND_SOC_DAPM_PGA("Out 3", AC97_EXTENDED_MSTATUS, 11, 1, NULL, 0),
  402. SND_SOC_DAPM_PGA("Out 4", AC97_EXTENDED_MSTATUS, 12, 1, NULL, 0),
  403. SND_SOC_DAPM_PGA("Mono Out", AC97_EXTENDED_MSTATUS, 13, 1, NULL, 0),
  404. SND_SOC_DAPM_PGA("Left Line In", AC97_EXTENDED_MSTATUS, 6, 1, NULL, 0),
  405. SND_SOC_DAPM_PGA("Right Line In", AC97_EXTENDED_MSTATUS, 5, 1, NULL, 0),
  406. SND_SOC_DAPM_PGA("Mono In", AC97_EXTENDED_MSTATUS, 4, 1, NULL, 0),
  407. SND_SOC_DAPM_PGA("Mic A PGA", AC97_EXTENDED_MSTATUS, 3, 1, NULL, 0),
  408. SND_SOC_DAPM_PGA("Mic B PGA", AC97_EXTENDED_MSTATUS, 2, 1, NULL, 0),
  409. SND_SOC_DAPM_PGA("Mic A Pre Amp", AC97_EXTENDED_MSTATUS, 1, 1, NULL, 0),
  410. SND_SOC_DAPM_PGA("Mic B Pre Amp", AC97_EXTENDED_MSTATUS, 0, 1, NULL, 0),
  411. SND_SOC_DAPM_MICBIAS("Mic Bias", AC97_EXTENDED_MSTATUS, 14, 1),
  412. SND_SOC_DAPM_OUTPUT("MONO"),
  413. SND_SOC_DAPM_OUTPUT("HPL"),
  414. SND_SOC_DAPM_OUTPUT("HPR"),
  415. SND_SOC_DAPM_OUTPUT("SPKL"),
  416. SND_SOC_DAPM_OUTPUT("SPKR"),
  417. SND_SOC_DAPM_OUTPUT("OUT3"),
  418. SND_SOC_DAPM_OUTPUT("OUT4"),
  419. SND_SOC_DAPM_INPUT("LINEL"),
  420. SND_SOC_DAPM_INPUT("LINER"),
  421. SND_SOC_DAPM_INPUT("MONOIN"),
  422. SND_SOC_DAPM_INPUT("PCBEEP"),
  423. SND_SOC_DAPM_INPUT("MIC1"),
  424. SND_SOC_DAPM_INPUT("MIC2A"),
  425. SND_SOC_DAPM_INPUT("MIC2B"),
  426. SND_SOC_DAPM_VMID("VMID"),
  427. };
  428. static const struct snd_soc_dapm_route wm9713_audio_map[] = {
  429. /* left HP mixer */
  430. {"Left HP Mixer", "Beep Playback Switch", "PCBEEP"},
  431. {"Left HP Mixer", "Voice Playback Switch", "Voice DAC"},
  432. {"Left HP Mixer", "Aux Playback Switch", "Aux DAC"},
  433. {"Left HP Mixer", "Bypass Playback Switch", "Left Line In"},
  434. {"Left HP Mixer", "PCM Playback Switch", "Left DAC"},
  435. {"Left HP Mixer", "MonoIn Playback Switch", "Mono In"},
  436. {"Left HP Mixer", NULL, "Capture Headphone Mux"},
  437. /* right HP mixer */
  438. {"Right HP Mixer", "Beep Playback Switch", "PCBEEP"},
  439. {"Right HP Mixer", "Voice Playback Switch", "Voice DAC"},
  440. {"Right HP Mixer", "Aux Playback Switch", "Aux DAC"},
  441. {"Right HP Mixer", "Bypass Playback Switch", "Right Line In"},
  442. {"Right HP Mixer", "PCM Playback Switch", "Right DAC"},
  443. {"Right HP Mixer", "MonoIn Playback Switch", "Mono In"},
  444. {"Right HP Mixer", NULL, "Capture Headphone Mux"},
  445. /* virtual mixer - mixes left & right channels for spk and mono */
  446. {"AC97 Mixer", NULL, "Left DAC"},
  447. {"AC97 Mixer", NULL, "Right DAC"},
  448. {"Line Mixer", NULL, "Right Line In"},
  449. {"Line Mixer", NULL, "Left Line In"},
  450. {"HP Mixer", NULL, "Left HP Mixer"},
  451. {"HP Mixer", NULL, "Right HP Mixer"},
  452. {"Capture Mixer", NULL, "Left Capture Source"},
  453. {"Capture Mixer", NULL, "Right Capture Source"},
  454. /* speaker mixer */
  455. {"Speaker Mixer", "Beep Playback Switch", "PCBEEP"},
  456. {"Speaker Mixer", "Voice Playback Switch", "Voice DAC"},
  457. {"Speaker Mixer", "Aux Playback Switch", "Aux DAC"},
  458. {"Speaker Mixer", "Bypass Playback Switch", "Line Mixer"},
  459. {"Speaker Mixer", "PCM Playback Switch", "AC97 Mixer"},
  460. {"Speaker Mixer", "MonoIn Playback Switch", "Mono In"},
  461. /* mono mixer */
  462. {"Mono Mixer", "Beep Playback Switch", "PCBEEP"},
  463. {"Mono Mixer", "Voice Playback Switch", "Voice DAC"},
  464. {"Mono Mixer", "Aux Playback Switch", "Aux DAC"},
  465. {"Mono Mixer", "Bypass Playback Switch", "Line Mixer"},
  466. {"Mono Mixer", "PCM Playback Switch", "AC97 Mixer"},
  467. {"Mono Mixer", "Mic 1 Sidetone Switch", "Mic A PGA"},
  468. {"Mono Mixer", "Mic 2 Sidetone Switch", "Mic B PGA"},
  469. {"Mono Mixer", NULL, "Capture Mono Mux"},
  470. /* DAC inv mux 1 */
  471. {"DAC Inv Mux 1", "Mono", "Mono Mixer"},
  472. {"DAC Inv Mux 1", "Speaker", "Speaker Mixer"},
  473. {"DAC Inv Mux 1", "Left Headphone", "Left HP Mixer"},
  474. {"DAC Inv Mux 1", "Right Headphone", "Right HP Mixer"},
  475. {"DAC Inv Mux 1", "Headphone Mono", "HP Mixer"},
  476. /* DAC inv mux 2 */
  477. {"DAC Inv Mux 2", "Mono", "Mono Mixer"},
  478. {"DAC Inv Mux 2", "Speaker", "Speaker Mixer"},
  479. {"DAC Inv Mux 2", "Left Headphone", "Left HP Mixer"},
  480. {"DAC Inv Mux 2", "Right Headphone", "Right HP Mixer"},
  481. {"DAC Inv Mux 2", "Headphone Mono", "HP Mixer"},
  482. /* headphone left mux */
  483. {"Left Headphone Out Mux", "Headphone", "Left HP Mixer"},
  484. /* headphone right mux */
  485. {"Right Headphone Out Mux", "Headphone", "Right HP Mixer"},
  486. /* speaker left mux */
  487. {"Left Speaker Out Mux", "Headphone", "Left HP Mixer"},
  488. {"Left Speaker Out Mux", "Speaker", "Speaker Mixer"},
  489. {"Left Speaker Out Mux", "Inv", "DAC Inv Mux 1"},
  490. /* speaker right mux */
  491. {"Right Speaker Out Mux", "Headphone", "Right HP Mixer"},
  492. {"Right Speaker Out Mux", "Speaker", "Speaker Mixer"},
  493. {"Right Speaker Out Mux", "Inv", "DAC Inv Mux 2"},
  494. /* mono mux */
  495. {"Mono Out Mux", "Mono", "Mono Mixer"},
  496. {"Mono Out Mux", "Inv", "DAC Inv Mux 1"},
  497. /* out 3 mux */
  498. {"Out 3 Mux", "Inv 1", "DAC Inv Mux 1"},
  499. /* out 4 mux */
  500. {"Out 4 Mux", "Inv 2", "DAC Inv Mux 2"},
  501. /* output pga */
  502. {"HPL", NULL, "Left Headphone"},
  503. {"Left Headphone", NULL, "Left Headphone Out Mux"},
  504. {"HPR", NULL, "Right Headphone"},
  505. {"Right Headphone", NULL, "Right Headphone Out Mux"},
  506. {"OUT3", NULL, "Out 3"},
  507. {"Out 3", NULL, "Out 3 Mux"},
  508. {"OUT4", NULL, "Out 4"},
  509. {"Out 4", NULL, "Out 4 Mux"},
  510. {"SPKL", NULL, "Left Speaker"},
  511. {"Left Speaker", NULL, "Left Speaker Out Mux"},
  512. {"SPKR", NULL, "Right Speaker"},
  513. {"Right Speaker", NULL, "Right Speaker Out Mux"},
  514. {"MONO", NULL, "Mono Out"},
  515. {"Mono Out", NULL, "Mono Out Mux"},
  516. /* input pga */
  517. {"Left Line In", NULL, "LINEL"},
  518. {"Right Line In", NULL, "LINER"},
  519. {"Mono In", NULL, "MONOIN"},
  520. {"Mic A PGA", NULL, "Mic A Pre Amp"},
  521. {"Mic B PGA", NULL, "Mic B Pre Amp"},
  522. /* left capture select */
  523. {"Left Capture Source", "Mic 1", "Mic A Pre Amp"},
  524. {"Left Capture Source", "Mic 2", "Mic B Pre Amp"},
  525. {"Left Capture Source", "Line", "LINEL"},
  526. {"Left Capture Source", "Mono In", "MONOIN"},
  527. {"Left Capture Source", "Headphone", "Left HP Mixer"},
  528. {"Left Capture Source", "Speaker", "Speaker Mixer"},
  529. {"Left Capture Source", "Mono Out", "Mono Mixer"},
  530. /* right capture select */
  531. {"Right Capture Source", "Mic 1", "Mic A Pre Amp"},
  532. {"Right Capture Source", "Mic 2", "Mic B Pre Amp"},
  533. {"Right Capture Source", "Line", "LINER"},
  534. {"Right Capture Source", "Mono In", "MONOIN"},
  535. {"Right Capture Source", "Headphone", "Right HP Mixer"},
  536. {"Right Capture Source", "Speaker", "Speaker Mixer"},
  537. {"Right Capture Source", "Mono Out", "Mono Mixer"},
  538. /* left ADC */
  539. {"Left ADC", NULL, "Left Capture Source"},
  540. {"Left Voice ADC", NULL, "Left ADC"},
  541. {"Left HiFi ADC", NULL, "Left ADC"},
  542. /* right ADC */
  543. {"Right ADC", NULL, "Right Capture Source"},
  544. {"Right Voice ADC", NULL, "Right ADC"},
  545. {"Right HiFi ADC", NULL, "Right ADC"},
  546. /* mic */
  547. {"Mic A Pre Amp", NULL, "Mic A Source"},
  548. {"Mic A Source", "Mic 1", "MIC1"},
  549. {"Mic A Source", "Mic 2 A", "MIC2A"},
  550. {"Mic A Source", "Mic 2 B", "Mic B Source"},
  551. {"Mic B Pre Amp", "MPB", "Mic B Source"},
  552. {"Mic B Source", NULL, "MIC2B"},
  553. /* headphone capture */
  554. {"Capture Headphone Mux", "Stereo", "Capture Mixer"},
  555. {"Capture Headphone Mux", "Left", "Left Capture Source"},
  556. {"Capture Headphone Mux", "Right", "Right Capture Source"},
  557. /* mono capture */
  558. {"Capture Mono Mux", "Stereo", "Capture Mixer"},
  559. {"Capture Mono Mux", "Left", "Left Capture Source"},
  560. {"Capture Mono Mux", "Right", "Right Capture Source"},
  561. };
  562. static unsigned int ac97_read(struct snd_soc_codec *codec,
  563. unsigned int reg)
  564. {
  565. u16 *cache = codec->reg_cache;
  566. if (reg == AC97_RESET || reg == AC97_GPIO_STATUS ||
  567. reg == AC97_VENDOR_ID1 || reg == AC97_VENDOR_ID2 ||
  568. reg == AC97_CD)
  569. return soc_ac97_ops.read(codec->ac97, reg);
  570. else {
  571. reg = reg >> 1;
  572. if (reg >= (ARRAY_SIZE(wm9713_reg)))
  573. return -EIO;
  574. return cache[reg];
  575. }
  576. }
  577. static int ac97_write(struct snd_soc_codec *codec, unsigned int reg,
  578. unsigned int val)
  579. {
  580. u16 *cache = codec->reg_cache;
  581. if (reg < 0x7c)
  582. soc_ac97_ops.write(codec->ac97, reg, val);
  583. reg = reg >> 1;
  584. if (reg < (ARRAY_SIZE(wm9713_reg)))
  585. cache[reg] = val;
  586. return 0;
  587. }
  588. /* PLL divisors */
  589. struct _pll_div {
  590. u32 divsel:1;
  591. u32 divctl:1;
  592. u32 lf:1;
  593. u32 n:4;
  594. u32 k:24;
  595. };
  596. /* The size in bits of the PLL divide multiplied by 10
  597. * to allow rounding later */
  598. #define FIXED_PLL_SIZE ((1 << 22) * 10)
  599. static void pll_factors(struct _pll_div *pll_div, unsigned int source)
  600. {
  601. u64 Kpart;
  602. unsigned int K, Ndiv, Nmod, target;
  603. /* The the PLL output is always 98.304MHz. */
  604. target = 98304000;
  605. /* If the input frequency is over 14.4MHz then scale it down. */
  606. if (source > 14400000) {
  607. source >>= 1;
  608. pll_div->divsel = 1;
  609. if (source > 14400000) {
  610. source >>= 1;
  611. pll_div->divctl = 1;
  612. } else
  613. pll_div->divctl = 0;
  614. } else {
  615. pll_div->divsel = 0;
  616. pll_div->divctl = 0;
  617. }
  618. /* Low frequency sources require an additional divide in the
  619. * loop.
  620. */
  621. if (source < 8192000) {
  622. pll_div->lf = 1;
  623. target >>= 2;
  624. } else
  625. pll_div->lf = 0;
  626. Ndiv = target / source;
  627. if ((Ndiv < 5) || (Ndiv > 12))
  628. printk(KERN_WARNING
  629. "WM9713 PLL N value %u out of recommended range!\n",
  630. Ndiv);
  631. pll_div->n = Ndiv;
  632. Nmod = target % source;
  633. Kpart = FIXED_PLL_SIZE * (long long)Nmod;
  634. do_div(Kpart, source);
  635. K = Kpart & 0xFFFFFFFF;
  636. /* Check if we need to round */
  637. if ((K % 10) >= 5)
  638. K += 5;
  639. /* Move down to proper range now rounding is done */
  640. K /= 10;
  641. pll_div->k = K;
  642. }
  643. /**
  644. * Please note that changing the PLL input frequency may require
  645. * resynchronisation with the AC97 controller.
  646. */
  647. static int wm9713_set_pll(struct snd_soc_codec *codec,
  648. int pll_id, unsigned int freq_in, unsigned int freq_out)
  649. {
  650. struct wm9713_priv *wm9713 = snd_soc_codec_get_drvdata(codec);
  651. u16 reg, reg2;
  652. struct _pll_div pll_div;
  653. /* turn PLL off ? */
  654. if (freq_in == 0) {
  655. /* disable PLL power and select ext source */
  656. reg = ac97_read(codec, AC97_HANDSET_RATE);
  657. ac97_write(codec, AC97_HANDSET_RATE, reg | 0x0080);
  658. reg = ac97_read(codec, AC97_EXTENDED_MID);
  659. ac97_write(codec, AC97_EXTENDED_MID, reg | 0x0200);
  660. wm9713->pll_in = 0;
  661. return 0;
  662. }
  663. pll_factors(&pll_div, freq_in);
  664. if (pll_div.k == 0) {
  665. reg = (pll_div.n << 12) | (pll_div.lf << 11) |
  666. (pll_div.divsel << 9) | (pll_div.divctl << 8);
  667. ac97_write(codec, AC97_LINE1_LEVEL, reg);
  668. } else {
  669. /* write the fractional k to the reg 0x46 pages */
  670. reg2 = (pll_div.n << 12) | (pll_div.lf << 11) | (1 << 10) |
  671. (pll_div.divsel << 9) | (pll_div.divctl << 8);
  672. /* K [21:20] */
  673. reg = reg2 | (0x5 << 4) | (pll_div.k >> 20);
  674. ac97_write(codec, AC97_LINE1_LEVEL, reg);
  675. /* K [19:16] */
  676. reg = reg2 | (0x4 << 4) | ((pll_div.k >> 16) & 0xf);
  677. ac97_write(codec, AC97_LINE1_LEVEL, reg);
  678. /* K [15:12] */
  679. reg = reg2 | (0x3 << 4) | ((pll_div.k >> 12) & 0xf);
  680. ac97_write(codec, AC97_LINE1_LEVEL, reg);
  681. /* K [11:8] */
  682. reg = reg2 | (0x2 << 4) | ((pll_div.k >> 8) & 0xf);
  683. ac97_write(codec, AC97_LINE1_LEVEL, reg);
  684. /* K [7:4] */
  685. reg = reg2 | (0x1 << 4) | ((pll_div.k >> 4) & 0xf);
  686. ac97_write(codec, AC97_LINE1_LEVEL, reg);
  687. reg = reg2 | (0x0 << 4) | (pll_div.k & 0xf); /* K [3:0] */
  688. ac97_write(codec, AC97_LINE1_LEVEL, reg);
  689. }
  690. /* turn PLL on and select as source */
  691. reg = ac97_read(codec, AC97_EXTENDED_MID);
  692. ac97_write(codec, AC97_EXTENDED_MID, reg & 0xfdff);
  693. reg = ac97_read(codec, AC97_HANDSET_RATE);
  694. ac97_write(codec, AC97_HANDSET_RATE, reg & 0xff7f);
  695. wm9713->pll_in = freq_in;
  696. /* wait 10ms AC97 link frames for the link to stabilise */
  697. schedule_timeout_interruptible(msecs_to_jiffies(10));
  698. return 0;
  699. }
  700. static int wm9713_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id,
  701. int source, unsigned int freq_in, unsigned int freq_out)
  702. {
  703. struct snd_soc_codec *codec = codec_dai->codec;
  704. return wm9713_set_pll(codec, pll_id, freq_in, freq_out);
  705. }
  706. /*
  707. * Tristate the PCM DAI lines, tristate can be disabled by calling
  708. * wm9713_set_dai_fmt()
  709. */
  710. static int wm9713_set_dai_tristate(struct snd_soc_dai *codec_dai,
  711. int tristate)
  712. {
  713. struct snd_soc_codec *codec = codec_dai->codec;
  714. u16 reg = ac97_read(codec, AC97_CENTER_LFE_MASTER) & 0x9fff;
  715. if (tristate)
  716. ac97_write(codec, AC97_CENTER_LFE_MASTER, reg);
  717. return 0;
  718. }
  719. /*
  720. * Configure WM9713 clock dividers.
  721. * Voice DAC needs 256 FS
  722. */
  723. static int wm9713_set_dai_clkdiv(struct snd_soc_dai *codec_dai,
  724. int div_id, int div)
  725. {
  726. struct snd_soc_codec *codec = codec_dai->codec;
  727. u16 reg;
  728. switch (div_id) {
  729. case WM9713_PCMCLK_DIV:
  730. reg = ac97_read(codec, AC97_HANDSET_RATE) & 0xf0ff;
  731. ac97_write(codec, AC97_HANDSET_RATE, reg | div);
  732. break;
  733. case WM9713_CLKA_MULT:
  734. reg = ac97_read(codec, AC97_HANDSET_RATE) & 0xfffd;
  735. ac97_write(codec, AC97_HANDSET_RATE, reg | div);
  736. break;
  737. case WM9713_CLKB_MULT:
  738. reg = ac97_read(codec, AC97_HANDSET_RATE) & 0xfffb;
  739. ac97_write(codec, AC97_HANDSET_RATE, reg | div);
  740. break;
  741. case WM9713_HIFI_DIV:
  742. reg = ac97_read(codec, AC97_HANDSET_RATE) & 0x8fff;
  743. ac97_write(codec, AC97_HANDSET_RATE, reg | div);
  744. break;
  745. case WM9713_PCMBCLK_DIV:
  746. reg = ac97_read(codec, AC97_CENTER_LFE_MASTER) & 0xf1ff;
  747. ac97_write(codec, AC97_CENTER_LFE_MASTER, reg | div);
  748. break;
  749. case WM9713_PCMCLK_PLL_DIV:
  750. reg = ac97_read(codec, AC97_LINE1_LEVEL) & 0xff80;
  751. ac97_write(codec, AC97_LINE1_LEVEL, reg | 0x60 | div);
  752. break;
  753. case WM9713_HIFI_PLL_DIV:
  754. reg = ac97_read(codec, AC97_LINE1_LEVEL) & 0xff80;
  755. ac97_write(codec, AC97_LINE1_LEVEL, reg | 0x70 | div);
  756. break;
  757. default:
  758. return -EINVAL;
  759. }
  760. return 0;
  761. }
  762. static int wm9713_set_dai_fmt(struct snd_soc_dai *codec_dai,
  763. unsigned int fmt)
  764. {
  765. struct snd_soc_codec *codec = codec_dai->codec;
  766. u16 gpio = ac97_read(codec, AC97_GPIO_CFG) & 0xffc5;
  767. u16 reg = 0x8000;
  768. /* clock masters */
  769. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  770. case SND_SOC_DAIFMT_CBM_CFM:
  771. reg |= 0x4000;
  772. gpio |= 0x0010;
  773. break;
  774. case SND_SOC_DAIFMT_CBM_CFS:
  775. reg |= 0x6000;
  776. gpio |= 0x0018;
  777. break;
  778. case SND_SOC_DAIFMT_CBS_CFS:
  779. reg |= 0x2000;
  780. gpio |= 0x001a;
  781. break;
  782. case SND_SOC_DAIFMT_CBS_CFM:
  783. gpio |= 0x0012;
  784. break;
  785. }
  786. /* clock inversion */
  787. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  788. case SND_SOC_DAIFMT_IB_IF:
  789. reg |= 0x00c0;
  790. break;
  791. case SND_SOC_DAIFMT_IB_NF:
  792. reg |= 0x0080;
  793. break;
  794. case SND_SOC_DAIFMT_NB_IF:
  795. reg |= 0x0040;
  796. break;
  797. }
  798. /* DAI format */
  799. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  800. case SND_SOC_DAIFMT_I2S:
  801. reg |= 0x0002;
  802. break;
  803. case SND_SOC_DAIFMT_RIGHT_J:
  804. break;
  805. case SND_SOC_DAIFMT_LEFT_J:
  806. reg |= 0x0001;
  807. break;
  808. case SND_SOC_DAIFMT_DSP_A:
  809. reg |= 0x0003;
  810. break;
  811. case SND_SOC_DAIFMT_DSP_B:
  812. reg |= 0x0043;
  813. break;
  814. }
  815. ac97_write(codec, AC97_GPIO_CFG, gpio);
  816. ac97_write(codec, AC97_CENTER_LFE_MASTER, reg);
  817. return 0;
  818. }
  819. static int wm9713_pcm_hw_params(struct snd_pcm_substream *substream,
  820. struct snd_pcm_hw_params *params,
  821. struct snd_soc_dai *dai)
  822. {
  823. struct snd_soc_codec *codec = dai->codec;
  824. u16 reg = ac97_read(codec, AC97_CENTER_LFE_MASTER) & 0xfff3;
  825. switch (params_format(params)) {
  826. case SNDRV_PCM_FORMAT_S16_LE:
  827. break;
  828. case SNDRV_PCM_FORMAT_S20_3LE:
  829. reg |= 0x0004;
  830. break;
  831. case SNDRV_PCM_FORMAT_S24_LE:
  832. reg |= 0x0008;
  833. break;
  834. case SNDRV_PCM_FORMAT_S32_LE:
  835. reg |= 0x000c;
  836. break;
  837. }
  838. /* enable PCM interface in master mode */
  839. ac97_write(codec, AC97_CENTER_LFE_MASTER, reg);
  840. return 0;
  841. }
  842. static int ac97_hifi_prepare(struct snd_pcm_substream *substream,
  843. struct snd_soc_dai *dai)
  844. {
  845. struct snd_soc_codec *codec = dai->codec;
  846. struct snd_pcm_runtime *runtime = substream->runtime;
  847. int reg;
  848. u16 vra;
  849. vra = ac97_read(codec, AC97_EXTENDED_STATUS);
  850. ac97_write(codec, AC97_EXTENDED_STATUS, vra | 0x1);
  851. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  852. reg = AC97_PCM_FRONT_DAC_RATE;
  853. else
  854. reg = AC97_PCM_LR_ADC_RATE;
  855. return ac97_write(codec, reg, runtime->rate);
  856. }
  857. static int ac97_aux_prepare(struct snd_pcm_substream *substream,
  858. struct snd_soc_dai *dai)
  859. {
  860. struct snd_soc_codec *codec = dai->codec;
  861. struct snd_pcm_runtime *runtime = substream->runtime;
  862. u16 vra, xsle;
  863. vra = ac97_read(codec, AC97_EXTENDED_STATUS);
  864. ac97_write(codec, AC97_EXTENDED_STATUS, vra | 0x1);
  865. xsle = ac97_read(codec, AC97_PCI_SID);
  866. ac97_write(codec, AC97_PCI_SID, xsle | 0x8000);
  867. if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
  868. return -ENODEV;
  869. return ac97_write(codec, AC97_PCM_SURR_DAC_RATE, runtime->rate);
  870. }
  871. #define WM9713_RATES (SNDRV_PCM_RATE_8000 | \
  872. SNDRV_PCM_RATE_11025 | \
  873. SNDRV_PCM_RATE_22050 | \
  874. SNDRV_PCM_RATE_44100 | \
  875. SNDRV_PCM_RATE_48000)
  876. #define WM9713_PCM_RATES (SNDRV_PCM_RATE_8000 | \
  877. SNDRV_PCM_RATE_11025 | \
  878. SNDRV_PCM_RATE_16000 | \
  879. SNDRV_PCM_RATE_22050 | \
  880. SNDRV_PCM_RATE_44100 | \
  881. SNDRV_PCM_RATE_48000)
  882. #define WM9713_PCM_FORMATS \
  883. (SNDRV_PCM_FORMAT_S16_LE | SNDRV_PCM_FORMAT_S20_3LE | \
  884. SNDRV_PCM_FORMAT_S24_LE)
  885. static struct snd_soc_dai_ops wm9713_dai_ops_hifi = {
  886. .prepare = ac97_hifi_prepare,
  887. .set_clkdiv = wm9713_set_dai_clkdiv,
  888. .set_pll = wm9713_set_dai_pll,
  889. };
  890. static struct snd_soc_dai_ops wm9713_dai_ops_aux = {
  891. .prepare = ac97_aux_prepare,
  892. .set_clkdiv = wm9713_set_dai_clkdiv,
  893. .set_pll = wm9713_set_dai_pll,
  894. };
  895. static struct snd_soc_dai_ops wm9713_dai_ops_voice = {
  896. .hw_params = wm9713_pcm_hw_params,
  897. .set_clkdiv = wm9713_set_dai_clkdiv,
  898. .set_pll = wm9713_set_dai_pll,
  899. .set_fmt = wm9713_set_dai_fmt,
  900. .set_tristate = wm9713_set_dai_tristate,
  901. };
  902. static struct snd_soc_dai_driver wm9713_dai[] = {
  903. {
  904. .name = "wm9713-hifi",
  905. .ac97_control = 1,
  906. .playback = {
  907. .stream_name = "HiFi Playback",
  908. .channels_min = 1,
  909. .channels_max = 2,
  910. .rates = WM9713_RATES,
  911. .formats = SND_SOC_STD_AC97_FMTS,},
  912. .capture = {
  913. .stream_name = "HiFi Capture",
  914. .channels_min = 1,
  915. .channels_max = 2,
  916. .rates = WM9713_RATES,
  917. .formats = SND_SOC_STD_AC97_FMTS,},
  918. .ops = &wm9713_dai_ops_hifi,
  919. },
  920. {
  921. .name = "wm9713-aux",
  922. .playback = {
  923. .stream_name = "Aux Playback",
  924. .channels_min = 1,
  925. .channels_max = 1,
  926. .rates = WM9713_RATES,
  927. .formats = SND_SOC_STD_AC97_FMTS,},
  928. .ops = &wm9713_dai_ops_aux,
  929. },
  930. {
  931. .name = "wm9713-voice",
  932. .playback = {
  933. .stream_name = "Voice Playback",
  934. .channels_min = 1,
  935. .channels_max = 1,
  936. .rates = WM9713_PCM_RATES,
  937. .formats = WM9713_PCM_FORMATS,},
  938. .capture = {
  939. .stream_name = "Voice Capture",
  940. .channels_min = 1,
  941. .channels_max = 2,
  942. .rates = WM9713_PCM_RATES,
  943. .formats = WM9713_PCM_FORMATS,},
  944. .ops = &wm9713_dai_ops_voice,
  945. .symmetric_rates = 1,
  946. },
  947. };
  948. int wm9713_reset(struct snd_soc_codec *codec, int try_warm)
  949. {
  950. if (try_warm && soc_ac97_ops.warm_reset) {
  951. soc_ac97_ops.warm_reset(codec->ac97);
  952. if (ac97_read(codec, 0) == wm9713_reg[0])
  953. return 1;
  954. }
  955. soc_ac97_ops.reset(codec->ac97);
  956. if (soc_ac97_ops.warm_reset)
  957. soc_ac97_ops.warm_reset(codec->ac97);
  958. if (ac97_read(codec, 0) != wm9713_reg[0])
  959. return -EIO;
  960. return 0;
  961. }
  962. EXPORT_SYMBOL_GPL(wm9713_reset);
  963. static int wm9713_set_bias_level(struct snd_soc_codec *codec,
  964. enum snd_soc_bias_level level)
  965. {
  966. u16 reg;
  967. switch (level) {
  968. case SND_SOC_BIAS_ON:
  969. /* enable thermal shutdown */
  970. reg = ac97_read(codec, AC97_EXTENDED_MID) & 0x1bff;
  971. ac97_write(codec, AC97_EXTENDED_MID, reg);
  972. break;
  973. case SND_SOC_BIAS_PREPARE:
  974. break;
  975. case SND_SOC_BIAS_STANDBY:
  976. /* enable master bias and vmid */
  977. reg = ac97_read(codec, AC97_EXTENDED_MID) & 0x3bff;
  978. ac97_write(codec, AC97_EXTENDED_MID, reg);
  979. ac97_write(codec, AC97_POWERDOWN, 0x0000);
  980. break;
  981. case SND_SOC_BIAS_OFF:
  982. /* disable everything including AC link */
  983. ac97_write(codec, AC97_EXTENDED_MID, 0xffff);
  984. ac97_write(codec, AC97_EXTENDED_MSTATUS, 0xffff);
  985. ac97_write(codec, AC97_POWERDOWN, 0xffff);
  986. break;
  987. }
  988. codec->dapm.bias_level = level;
  989. return 0;
  990. }
  991. static int wm9713_soc_suspend(struct snd_soc_codec *codec,
  992. pm_message_t state)
  993. {
  994. u16 reg;
  995. /* Disable everything except touchpanel - that will be handled
  996. * by the touch driver and left disabled if touch is not in
  997. * use. */
  998. reg = ac97_read(codec, AC97_EXTENDED_MID);
  999. ac97_write(codec, AC97_EXTENDED_MID, reg | 0x7fff);
  1000. ac97_write(codec, AC97_EXTENDED_MSTATUS, 0xffff);
  1001. ac97_write(codec, AC97_POWERDOWN, 0x6f00);
  1002. ac97_write(codec, AC97_POWERDOWN, 0xffff);
  1003. return 0;
  1004. }
  1005. static int wm9713_soc_resume(struct snd_soc_codec *codec)
  1006. {
  1007. struct wm9713_priv *wm9713 = snd_soc_codec_get_drvdata(codec);
  1008. int i, ret;
  1009. u16 *cache = codec->reg_cache;
  1010. ret = wm9713_reset(codec, 1);
  1011. if (ret < 0) {
  1012. printk(KERN_ERR "could not reset AC97 codec\n");
  1013. return ret;
  1014. }
  1015. wm9713_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  1016. /* do we need to re-start the PLL ? */
  1017. if (wm9713->pll_in)
  1018. wm9713_set_pll(codec, 0, wm9713->pll_in, 0);
  1019. /* only synchronise the codec if warm reset failed */
  1020. if (ret == 0) {
  1021. for (i = 2; i < ARRAY_SIZE(wm9713_reg) << 1; i += 2) {
  1022. if (i == AC97_POWERDOWN || i == AC97_EXTENDED_MID ||
  1023. i == AC97_EXTENDED_MSTATUS || i > 0x66)
  1024. continue;
  1025. soc_ac97_ops.write(codec->ac97, i, cache[i>>1]);
  1026. }
  1027. }
  1028. return ret;
  1029. }
  1030. static int wm9713_soc_probe(struct snd_soc_codec *codec)
  1031. {
  1032. struct wm9713_priv *wm9713;
  1033. int ret = 0, reg;
  1034. wm9713 = kzalloc(sizeof(struct wm9713_priv), GFP_KERNEL);
  1035. if (wm9713 == NULL)
  1036. return -ENOMEM;
  1037. snd_soc_codec_set_drvdata(codec, wm9713);
  1038. ret = snd_soc_new_ac97_codec(codec, &soc_ac97_ops, 0);
  1039. if (ret < 0)
  1040. goto codec_err;
  1041. /* do a cold reset for the controller and then try
  1042. * a warm reset followed by an optional cold reset for codec */
  1043. wm9713_reset(codec, 0);
  1044. ret = wm9713_reset(codec, 1);
  1045. if (ret < 0) {
  1046. printk(KERN_ERR "Failed to reset WM9713: AC97 link error\n");
  1047. goto reset_err;
  1048. }
  1049. wm9713_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  1050. /* unmute the adc - move to kcontrol */
  1051. reg = ac97_read(codec, AC97_CD) & 0x7fff;
  1052. ac97_write(codec, AC97_CD, reg);
  1053. snd_soc_add_controls(codec, wm9713_snd_ac97_controls,
  1054. ARRAY_SIZE(wm9713_snd_ac97_controls));
  1055. return 0;
  1056. reset_err:
  1057. snd_soc_free_ac97_codec(codec);
  1058. codec_err:
  1059. kfree(wm9713);
  1060. return ret;
  1061. }
  1062. static int wm9713_soc_remove(struct snd_soc_codec *codec)
  1063. {
  1064. struct wm9713_priv *wm9713 = snd_soc_codec_get_drvdata(codec);
  1065. snd_soc_free_ac97_codec(codec);
  1066. kfree(wm9713);
  1067. return 0;
  1068. }
  1069. static struct snd_soc_codec_driver soc_codec_dev_wm9713 = {
  1070. .probe = wm9713_soc_probe,
  1071. .remove = wm9713_soc_remove,
  1072. .suspend = wm9713_soc_suspend,
  1073. .resume = wm9713_soc_resume,
  1074. .read = ac97_read,
  1075. .write = ac97_write,
  1076. .set_bias_level = wm9713_set_bias_level,
  1077. .reg_cache_size = ARRAY_SIZE(wm9713_reg),
  1078. .reg_word_size = sizeof(u16),
  1079. .reg_cache_step = 2,
  1080. .reg_cache_default = wm9713_reg,
  1081. .dapm_widgets = wm9713_dapm_widgets,
  1082. .num_dapm_widgets = ARRAY_SIZE(wm9713_dapm_widgets),
  1083. .dapm_routes = wm9713_audio_map,
  1084. .num_dapm_routes = ARRAY_SIZE(wm9713_audio_map),
  1085. };
  1086. static __devinit int wm9713_probe(struct platform_device *pdev)
  1087. {
  1088. return snd_soc_register_codec(&pdev->dev,
  1089. &soc_codec_dev_wm9713, wm9713_dai, ARRAY_SIZE(wm9713_dai));
  1090. }
  1091. static int __devexit wm9713_remove(struct platform_device *pdev)
  1092. {
  1093. snd_soc_unregister_codec(&pdev->dev);
  1094. return 0;
  1095. }
  1096. static struct platform_driver wm9713_codec_driver = {
  1097. .driver = {
  1098. .name = "wm9713-codec",
  1099. .owner = THIS_MODULE,
  1100. },
  1101. .probe = wm9713_probe,
  1102. .remove = __devexit_p(wm9713_remove),
  1103. };
  1104. static int __init wm9713_init(void)
  1105. {
  1106. return platform_driver_register(&wm9713_codec_driver);
  1107. }
  1108. module_init(wm9713_init);
  1109. static void __exit wm9713_exit(void)
  1110. {
  1111. platform_driver_unregister(&wm9713_codec_driver);
  1112. }
  1113. module_exit(wm9713_exit);
  1114. MODULE_DESCRIPTION("ASoC WM9713/WM9714 driver");
  1115. MODULE_AUTHOR("Liam Girdwood");
  1116. MODULE_LICENSE("GPL");