wm9713.c 41 KB

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