wm8776.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. /*
  2. * ALSA driver for ICEnsemble VT17xx
  3. *
  4. * Lowlevel functions for WM8776 codec
  5. *
  6. * Copyright (c) 2012 Ondrej Zary <linux@rainbow-software.org>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. #include <linux/delay.h>
  24. #include <sound/core.h>
  25. #include <sound/control.h>
  26. #include <sound/tlv.h>
  27. #include "wm8776.h"
  28. /* low-level access */
  29. static void snd_wm8776_write(struct snd_wm8776 *wm, u16 addr, u16 data)
  30. {
  31. u8 bus_addr = addr << 1 | data >> 8; /* addr + 9th data bit */
  32. u8 bus_data = data & 0xff; /* remaining 8 data bits */
  33. if (addr < WM8776_REG_RESET)
  34. wm->regs[addr] = data;
  35. wm->ops.write(wm, bus_addr, bus_data);
  36. }
  37. /* register-level functions */
  38. static void snd_wm8776_activate_ctl(struct snd_wm8776 *wm,
  39. const char *ctl_name,
  40. bool active)
  41. {
  42. struct snd_card *card = wm->card;
  43. struct snd_kcontrol *kctl;
  44. struct snd_kcontrol_volatile *vd;
  45. struct snd_ctl_elem_id elem_id;
  46. unsigned int index_offset;
  47. memset(&elem_id, 0, sizeof(elem_id));
  48. strlcpy(elem_id.name, ctl_name, sizeof(elem_id.name));
  49. elem_id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  50. kctl = snd_ctl_find_id(card, &elem_id);
  51. if (!kctl)
  52. return;
  53. index_offset = snd_ctl_get_ioff(kctl, &kctl->id);
  54. vd = &kctl->vd[index_offset];
  55. if (active)
  56. vd->access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  57. else
  58. vd->access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  59. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO, &kctl->id);
  60. }
  61. static void snd_wm8776_update_agc_ctl(struct snd_wm8776 *wm)
  62. {
  63. int i, flags_on = 0, flags_off = 0;
  64. switch (wm->agc_mode) {
  65. case WM8776_AGC_OFF:
  66. flags_off = WM8776_FLAG_LIM | WM8776_FLAG_ALC;
  67. break;
  68. case WM8776_AGC_LIM:
  69. flags_off = WM8776_FLAG_ALC;
  70. flags_on = WM8776_FLAG_LIM;
  71. break;
  72. case WM8776_AGC_ALC_R:
  73. case WM8776_AGC_ALC_L:
  74. case WM8776_AGC_ALC_STEREO:
  75. flags_off = WM8776_FLAG_LIM;
  76. flags_on = WM8776_FLAG_ALC;
  77. break;
  78. }
  79. for (i = 0; i < WM8776_CTL_COUNT; i++)
  80. if (wm->ctl[i].flags & flags_off)
  81. snd_wm8776_activate_ctl(wm, wm->ctl[i].name, false);
  82. else if (wm->ctl[i].flags & flags_on)
  83. snd_wm8776_activate_ctl(wm, wm->ctl[i].name, true);
  84. }
  85. static void snd_wm8776_set_agc(struct snd_wm8776 *wm, u16 agc, u16 nothing)
  86. {
  87. u16 alc1 = wm->regs[WM8776_REG_ALCCTRL1] & ~WM8776_ALC1_LCT_MASK;
  88. u16 alc2 = wm->regs[WM8776_REG_ALCCTRL2] & ~WM8776_ALC2_LCEN;
  89. switch (agc) {
  90. case 0: /* Off */
  91. wm->agc_mode = WM8776_AGC_OFF;
  92. break;
  93. case 1: /* Limiter */
  94. alc2 |= WM8776_ALC2_LCEN;
  95. wm->agc_mode = WM8776_AGC_LIM;
  96. break;
  97. case 2: /* ALC Right */
  98. alc1 |= WM8776_ALC1_LCSEL_ALCR;
  99. alc2 |= WM8776_ALC2_LCEN;
  100. wm->agc_mode = WM8776_AGC_ALC_R;
  101. break;
  102. case 3: /* ALC Left */
  103. alc1 |= WM8776_ALC1_LCSEL_ALCL;
  104. alc2 |= WM8776_ALC2_LCEN;
  105. wm->agc_mode = WM8776_AGC_ALC_L;
  106. break;
  107. case 4: /* ALC Stereo */
  108. alc1 |= WM8776_ALC1_LCSEL_ALCSTEREO;
  109. alc2 |= WM8776_ALC2_LCEN;
  110. wm->agc_mode = WM8776_AGC_ALC_STEREO;
  111. break;
  112. }
  113. snd_wm8776_write(wm, WM8776_REG_ALCCTRL1, alc1);
  114. snd_wm8776_write(wm, WM8776_REG_ALCCTRL2, alc2);
  115. snd_wm8776_update_agc_ctl(wm);
  116. }
  117. static void snd_wm8776_get_agc(struct snd_wm8776 *wm, u16 *mode, u16 *nothing)
  118. {
  119. *mode = wm->agc_mode;
  120. }
  121. /* mixer controls */
  122. static const DECLARE_TLV_DB_SCALE(wm8776_hp_tlv, -7400, 100, 1);
  123. static const DECLARE_TLV_DB_SCALE(wm8776_dac_tlv, -12750, 50, 1);
  124. static const DECLARE_TLV_DB_SCALE(wm8776_adc_tlv, -10350, 50, 1);
  125. static const DECLARE_TLV_DB_SCALE(wm8776_lct_tlv, -1600, 100, 0);
  126. static const DECLARE_TLV_DB_SCALE(wm8776_maxgain_tlv, 0, 400, 0);
  127. static const DECLARE_TLV_DB_SCALE(wm8776_ngth_tlv, -7800, 600, 0);
  128. static const DECLARE_TLV_DB_SCALE(wm8776_maxatten_lim_tlv, -1200, 100, 0);
  129. static const DECLARE_TLV_DB_SCALE(wm8776_maxatten_alc_tlv, -2100, 400, 0);
  130. static struct snd_wm8776_ctl snd_wm8776_default_ctl[WM8776_CTL_COUNT] = {
  131. [WM8776_CTL_DAC_VOL] = {
  132. .name = "Master Playback Volume",
  133. .type = SNDRV_CTL_ELEM_TYPE_INTEGER,
  134. .tlv = wm8776_dac_tlv,
  135. .reg1 = WM8776_REG_DACLVOL,
  136. .reg2 = WM8776_REG_DACRVOL,
  137. .mask1 = WM8776_DACVOL_MASK,
  138. .mask2 = WM8776_DACVOL_MASK,
  139. .max = 0xff,
  140. .flags = WM8776_FLAG_STEREO | WM8776_FLAG_VOL_UPDATE,
  141. },
  142. [WM8776_CTL_DAC_SW] = {
  143. .name = "Master Playback Switch",
  144. .type = SNDRV_CTL_ELEM_TYPE_BOOLEAN,
  145. .reg1 = WM8776_REG_DACCTRL1,
  146. .reg2 = WM8776_REG_DACCTRL1,
  147. .mask1 = WM8776_DAC_PL_LL,
  148. .mask2 = WM8776_DAC_PL_RR,
  149. .flags = WM8776_FLAG_STEREO,
  150. },
  151. [WM8776_CTL_DAC_ZC_SW] = {
  152. .name = "Master Zero Cross Detect Playback Switch",
  153. .type = SNDRV_CTL_ELEM_TYPE_BOOLEAN,
  154. .reg1 = WM8776_REG_DACCTRL1,
  155. .mask1 = WM8776_DAC_DZCEN,
  156. },
  157. [WM8776_CTL_HP_VOL] = {
  158. .name = "Headphone Playback Volume",
  159. .type = SNDRV_CTL_ELEM_TYPE_INTEGER,
  160. .tlv = wm8776_hp_tlv,
  161. .reg1 = WM8776_REG_HPLVOL,
  162. .reg2 = WM8776_REG_HPRVOL,
  163. .mask1 = WM8776_HPVOL_MASK,
  164. .mask2 = WM8776_HPVOL_MASK,
  165. .min = 0x2f,
  166. .max = 0x7f,
  167. .flags = WM8776_FLAG_STEREO | WM8776_FLAG_VOL_UPDATE,
  168. },
  169. [WM8776_CTL_HP_SW] = {
  170. .name = "Headphone Playback Switch",
  171. .type = SNDRV_CTL_ELEM_TYPE_BOOLEAN,
  172. .reg1 = WM8776_REG_PWRDOWN,
  173. .mask1 = WM8776_PWR_HPPD,
  174. .flags = WM8776_FLAG_INVERT,
  175. },
  176. [WM8776_CTL_HP_ZC_SW] = {
  177. .name = "Headphone Zero Cross Detect Playback Switch",
  178. .type = SNDRV_CTL_ELEM_TYPE_BOOLEAN,
  179. .reg1 = WM8776_REG_HPLVOL,
  180. .reg2 = WM8776_REG_HPRVOL,
  181. .mask1 = WM8776_VOL_HPZCEN,
  182. .mask2 = WM8776_VOL_HPZCEN,
  183. .flags = WM8776_FLAG_STEREO,
  184. },
  185. [WM8776_CTL_AUX_SW] = {
  186. .name = "AUX Playback Switch",
  187. .type = SNDRV_CTL_ELEM_TYPE_BOOLEAN,
  188. .reg1 = WM8776_REG_OUTMUX,
  189. .mask1 = WM8776_OUTMUX_AUX,
  190. },
  191. [WM8776_CTL_BYPASS_SW] = {
  192. .name = "Bypass Playback Switch",
  193. .type = SNDRV_CTL_ELEM_TYPE_BOOLEAN,
  194. .reg1 = WM8776_REG_OUTMUX,
  195. .mask1 = WM8776_OUTMUX_BYPASS,
  196. },
  197. [WM8776_CTL_DAC_IZD_SW] = {
  198. .name = "Infinite Zero Detect Playback Switch",
  199. .type = SNDRV_CTL_ELEM_TYPE_BOOLEAN,
  200. .reg1 = WM8776_REG_DACCTRL1,
  201. .mask1 = WM8776_DAC_IZD,
  202. },
  203. [WM8776_CTL_PHASE_SW] = {
  204. .name = "Phase Invert Playback Switch",
  205. .type = SNDRV_CTL_ELEM_TYPE_BOOLEAN,
  206. .reg1 = WM8776_REG_PHASESWAP,
  207. .reg2 = WM8776_REG_PHASESWAP,
  208. .mask1 = WM8776_PHASE_INVERTL,
  209. .mask2 = WM8776_PHASE_INVERTR,
  210. .flags = WM8776_FLAG_STEREO,
  211. },
  212. [WM8776_CTL_DEEMPH_SW] = {
  213. .name = "Deemphasis Playback Switch",
  214. .type = SNDRV_CTL_ELEM_TYPE_BOOLEAN,
  215. .reg1 = WM8776_REG_DACCTRL2,
  216. .mask1 = WM8776_DAC2_DEEMPH,
  217. },
  218. [WM8776_CTL_ADC_VOL] = {
  219. .name = "Input Capture Volume",
  220. .type = SNDRV_CTL_ELEM_TYPE_INTEGER,
  221. .tlv = wm8776_adc_tlv,
  222. .reg1 = WM8776_REG_ADCLVOL,
  223. .reg2 = WM8776_REG_ADCRVOL,
  224. .mask1 = WM8776_ADC_GAIN_MASK,
  225. .mask2 = WM8776_ADC_GAIN_MASK,
  226. .max = 0xff,
  227. .flags = WM8776_FLAG_STEREO | WM8776_FLAG_VOL_UPDATE,
  228. },
  229. [WM8776_CTL_ADC_SW] = {
  230. .name = "Input Capture Switch",
  231. .type = SNDRV_CTL_ELEM_TYPE_BOOLEAN,
  232. .reg1 = WM8776_REG_ADCMUX,
  233. .reg2 = WM8776_REG_ADCMUX,
  234. .mask1 = WM8776_ADC_MUTEL,
  235. .mask2 = WM8776_ADC_MUTER,
  236. .flags = WM8776_FLAG_STEREO | WM8776_FLAG_INVERT,
  237. },
  238. [WM8776_CTL_INPUT1_SW] = {
  239. .name = "AIN1 Capture Switch",
  240. .type = SNDRV_CTL_ELEM_TYPE_BOOLEAN,
  241. .reg1 = WM8776_REG_ADCMUX,
  242. .mask1 = WM8776_ADC_MUX_AIN1,
  243. },
  244. [WM8776_CTL_INPUT2_SW] = {
  245. .name = "AIN2 Capture Switch",
  246. .type = SNDRV_CTL_ELEM_TYPE_BOOLEAN,
  247. .reg1 = WM8776_REG_ADCMUX,
  248. .mask1 = WM8776_ADC_MUX_AIN2,
  249. },
  250. [WM8776_CTL_INPUT3_SW] = {
  251. .name = "AIN3 Capture Switch",
  252. .type = SNDRV_CTL_ELEM_TYPE_BOOLEAN,
  253. .reg1 = WM8776_REG_ADCMUX,
  254. .mask1 = WM8776_ADC_MUX_AIN3,
  255. },
  256. [WM8776_CTL_INPUT4_SW] = {
  257. .name = "AIN4 Capture Switch",
  258. .type = SNDRV_CTL_ELEM_TYPE_BOOLEAN,
  259. .reg1 = WM8776_REG_ADCMUX,
  260. .mask1 = WM8776_ADC_MUX_AIN4,
  261. },
  262. [WM8776_CTL_INPUT5_SW] = {
  263. .name = "AIN5 Capture Switch",
  264. .type = SNDRV_CTL_ELEM_TYPE_BOOLEAN,
  265. .reg1 = WM8776_REG_ADCMUX,
  266. .mask1 = WM8776_ADC_MUX_AIN5,
  267. },
  268. [WM8776_CTL_AGC_SEL] = {
  269. .name = "AGC Select Capture Enum",
  270. .type = SNDRV_CTL_ELEM_TYPE_ENUMERATED,
  271. .enum_names = { "Off", "Limiter", "ALC Right", "ALC Left",
  272. "ALC Stereo" },
  273. .max = 5, /* .enum_names item count */
  274. .set = snd_wm8776_set_agc,
  275. .get = snd_wm8776_get_agc,
  276. },
  277. [WM8776_CTL_LIM_THR] = {
  278. .name = "Limiter Threshold Capture Volume",
  279. .type = SNDRV_CTL_ELEM_TYPE_INTEGER,
  280. .tlv = wm8776_lct_tlv,
  281. .reg1 = WM8776_REG_ALCCTRL1,
  282. .mask1 = WM8776_ALC1_LCT_MASK,
  283. .max = 15,
  284. .flags = WM8776_FLAG_LIM,
  285. },
  286. [WM8776_CTL_LIM_ATK] = {
  287. .name = "Limiter Attack Time Capture Enum",
  288. .type = SNDRV_CTL_ELEM_TYPE_ENUMERATED,
  289. .enum_names = { "0.25 ms", "0.5 ms", "1 ms", "2 ms", "4 ms",
  290. "8 ms", "16 ms", "32 ms", "64 ms", "128 ms", "256 ms" },
  291. .max = 11, /* .enum_names item count */
  292. .reg1 = WM8776_REG_ALCCTRL3,
  293. .mask1 = WM8776_ALC3_ATK_MASK,
  294. .flags = WM8776_FLAG_LIM,
  295. },
  296. [WM8776_CTL_LIM_DCY] = {
  297. .name = "Limiter Decay Time Capture Enum",
  298. .type = SNDRV_CTL_ELEM_TYPE_ENUMERATED,
  299. .enum_names = { "1.2 ms", "2.4 ms", "4.8 ms", "9.6 ms",
  300. "19.2 ms", "38.4 ms", "76.8 ms", "154 ms", "307 ms",
  301. "614 ms", "1.23 s" },
  302. .max = 11, /* .enum_names item count */
  303. .reg1 = WM8776_REG_ALCCTRL3,
  304. .mask1 = WM8776_ALC3_DCY_MASK,
  305. .flags = WM8776_FLAG_LIM,
  306. },
  307. [WM8776_CTL_LIM_TRANWIN] = {
  308. .name = "Limiter Transient Window Capture Enum",
  309. .type = SNDRV_CTL_ELEM_TYPE_ENUMERATED,
  310. .enum_names = { "0 us", "62.5 us", "125 us", "250 us", "500 us",
  311. "1 ms", "2 ms", "4 ms" },
  312. .max = 8, /* .enum_names item count */
  313. .reg1 = WM8776_REG_LIMITER,
  314. .mask1 = WM8776_LIM_TRANWIN_MASK,
  315. .flags = WM8776_FLAG_LIM,
  316. },
  317. [WM8776_CTL_LIM_MAXATTN] = {
  318. .name = "Limiter Maximum Attenuation Capture Volume",
  319. .type = SNDRV_CTL_ELEM_TYPE_INTEGER,
  320. .tlv = wm8776_maxatten_lim_tlv,
  321. .reg1 = WM8776_REG_LIMITER,
  322. .mask1 = WM8776_LIM_MAXATTEN_MASK,
  323. .min = 3,
  324. .max = 12,
  325. .flags = WM8776_FLAG_LIM | WM8776_FLAG_INVERT,
  326. },
  327. [WM8776_CTL_ALC_TGT] = {
  328. .name = "ALC Target Level Capture Volume",
  329. .type = SNDRV_CTL_ELEM_TYPE_INTEGER,
  330. .tlv = wm8776_lct_tlv,
  331. .reg1 = WM8776_REG_ALCCTRL1,
  332. .mask1 = WM8776_ALC1_LCT_MASK,
  333. .max = 15,
  334. .flags = WM8776_FLAG_ALC,
  335. },
  336. [WM8776_CTL_ALC_ATK] = {
  337. .name = "ALC Attack Time Capture Enum",
  338. .type = SNDRV_CTL_ELEM_TYPE_ENUMERATED,
  339. .enum_names = { "8.40 ms", "16.8 ms", "33.6 ms", "67.2 ms",
  340. "134 ms", "269 ms", "538 ms", "1.08 s", "2.15 s",
  341. "4.3 s", "8.6 s" },
  342. .max = 11, /* .enum_names item count */
  343. .reg1 = WM8776_REG_ALCCTRL3,
  344. .mask1 = WM8776_ALC3_ATK_MASK,
  345. .flags = WM8776_FLAG_ALC,
  346. },
  347. [WM8776_CTL_ALC_DCY] = {
  348. .name = "ALC Decay Time Capture Enum",
  349. .type = SNDRV_CTL_ELEM_TYPE_ENUMERATED,
  350. .enum_names = { "33.5 ms", "67.0 ms", "134 ms", "268 ms",
  351. "536 ms", "1.07 s", "2.14 s", "4.29 s", "8.58 s",
  352. "17.2 s", "34.3 s" },
  353. .max = 11, /* .enum_names item count */
  354. .reg1 = WM8776_REG_ALCCTRL3,
  355. .mask1 = WM8776_ALC3_DCY_MASK,
  356. .flags = WM8776_FLAG_ALC,
  357. },
  358. [WM8776_CTL_ALC_MAXGAIN] = {
  359. .name = "ALC Maximum Gain Capture Volume",
  360. .type = SNDRV_CTL_ELEM_TYPE_INTEGER,
  361. .tlv = wm8776_maxgain_tlv,
  362. .reg1 = WM8776_REG_ALCCTRL1,
  363. .mask1 = WM8776_ALC1_MAXGAIN_MASK,
  364. .min = 1,
  365. .max = 7,
  366. .flags = WM8776_FLAG_ALC,
  367. },
  368. [WM8776_CTL_ALC_MAXATTN] = {
  369. .name = "ALC Maximum Attenuation Capture Volume",
  370. .type = SNDRV_CTL_ELEM_TYPE_INTEGER,
  371. .tlv = wm8776_maxatten_alc_tlv,
  372. .reg1 = WM8776_REG_LIMITER,
  373. .mask1 = WM8776_LIM_MAXATTEN_MASK,
  374. .min = 10,
  375. .max = 15,
  376. .flags = WM8776_FLAG_ALC | WM8776_FLAG_INVERT,
  377. },
  378. [WM8776_CTL_ALC_HLD] = {
  379. .name = "ALC Hold Time Capture Enum",
  380. .type = SNDRV_CTL_ELEM_TYPE_ENUMERATED,
  381. .enum_names = { "0 ms", "2.67 ms", "5.33 ms", "10.6 ms",
  382. "21.3 ms", "42.7 ms", "85.3 ms", "171 ms", "341 ms",
  383. "683 ms", "1.37 s", "2.73 s", "5.46 s", "10.9 s",
  384. "21.8 s", "43.7 s" },
  385. .max = 16, /* .enum_names item count */
  386. .reg1 = WM8776_REG_ALCCTRL2,
  387. .mask1 = WM8776_ALC2_HOLD_MASK,
  388. .flags = WM8776_FLAG_ALC,
  389. },
  390. [WM8776_CTL_NGT_SW] = {
  391. .name = "Noise Gate Capture Switch",
  392. .type = SNDRV_CTL_ELEM_TYPE_BOOLEAN,
  393. .reg1 = WM8776_REG_NOISEGATE,
  394. .mask1 = WM8776_NGAT_ENABLE,
  395. .flags = WM8776_FLAG_ALC,
  396. },
  397. [WM8776_CTL_NGT_THR] = {
  398. .name = "Noise Gate Threshold Capture Volume",
  399. .type = SNDRV_CTL_ELEM_TYPE_INTEGER,
  400. .tlv = wm8776_ngth_tlv,
  401. .reg1 = WM8776_REG_NOISEGATE,
  402. .mask1 = WM8776_NGAT_THR_MASK,
  403. .max = 7,
  404. .flags = WM8776_FLAG_ALC,
  405. },
  406. };
  407. /* exported functions */
  408. void snd_wm8776_init(struct snd_wm8776 *wm)
  409. {
  410. int i;
  411. static const u16 default_values[] = {
  412. 0x000, 0x100, 0x000,
  413. 0x000, 0x100, 0x000,
  414. 0x000, 0x090, 0x000, 0x000,
  415. 0x022, 0x022, 0x022,
  416. 0x008, 0x0cf, 0x0cf, 0x07b, 0x000,
  417. 0x032, 0x000, 0x0a6, 0x001, 0x001
  418. };
  419. memcpy(wm->ctl, snd_wm8776_default_ctl, sizeof(wm->ctl));
  420. snd_wm8776_write(wm, WM8776_REG_RESET, 0x00); /* reset */
  421. udelay(10);
  422. /* load defaults */
  423. for (i = 0; i < ARRAY_SIZE(default_values); i++)
  424. snd_wm8776_write(wm, i, default_values[i]);
  425. }
  426. void snd_wm8776_resume(struct snd_wm8776 *wm)
  427. {
  428. int i;
  429. for (i = 0; i < WM8776_REG_COUNT; i++)
  430. snd_wm8776_write(wm, i, wm->regs[i]);
  431. }
  432. void snd_wm8776_set_power(struct snd_wm8776 *wm, u16 power)
  433. {
  434. snd_wm8776_write(wm, WM8776_REG_PWRDOWN, power);
  435. }
  436. void snd_wm8776_volume_restore(struct snd_wm8776 *wm)
  437. {
  438. u16 val = wm->regs[WM8776_REG_DACRVOL];
  439. /* restore volume after MCLK stopped */
  440. snd_wm8776_write(wm, WM8776_REG_DACRVOL, val | WM8776_VOL_UPDATE);
  441. }
  442. /* mixer callbacks */
  443. static int snd_wm8776_volume_info(struct snd_kcontrol *kcontrol,
  444. struct snd_ctl_elem_info *uinfo)
  445. {
  446. struct snd_wm8776 *wm = snd_kcontrol_chip(kcontrol);
  447. int n = kcontrol->private_value;
  448. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  449. uinfo->count = (wm->ctl[n].flags & WM8776_FLAG_STEREO) ? 2 : 1;
  450. uinfo->value.integer.min = wm->ctl[n].min;
  451. uinfo->value.integer.max = wm->ctl[n].max;
  452. return 0;
  453. }
  454. static int snd_wm8776_enum_info(struct snd_kcontrol *kcontrol,
  455. struct snd_ctl_elem_info *uinfo)
  456. {
  457. struct snd_wm8776 *wm = snd_kcontrol_chip(kcontrol);
  458. int n = kcontrol->private_value;
  459. return snd_ctl_enum_info(uinfo, 1, wm->ctl[n].max,
  460. wm->ctl[n].enum_names);
  461. }
  462. static int snd_wm8776_ctl_get(struct snd_kcontrol *kcontrol,
  463. struct snd_ctl_elem_value *ucontrol)
  464. {
  465. struct snd_wm8776 *wm = snd_kcontrol_chip(kcontrol);
  466. int n = kcontrol->private_value;
  467. u16 val1, val2;
  468. if (wm->ctl[n].get)
  469. wm->ctl[n].get(wm, &val1, &val2);
  470. else {
  471. val1 = wm->regs[wm->ctl[n].reg1] & wm->ctl[n].mask1;
  472. val1 >>= __ffs(wm->ctl[n].mask1);
  473. if (wm->ctl[n].flags & WM8776_FLAG_STEREO) {
  474. val2 = wm->regs[wm->ctl[n].reg2] & wm->ctl[n].mask2;
  475. val2 >>= __ffs(wm->ctl[n].mask2);
  476. if (wm->ctl[n].flags & WM8776_FLAG_VOL_UPDATE)
  477. val2 &= ~WM8776_VOL_UPDATE;
  478. }
  479. }
  480. if (wm->ctl[n].flags & WM8776_FLAG_INVERT) {
  481. val1 = wm->ctl[n].max - (val1 - wm->ctl[n].min);
  482. if (wm->ctl[n].flags & WM8776_FLAG_STEREO)
  483. val2 = wm->ctl[n].max - (val2 - wm->ctl[n].min);
  484. }
  485. ucontrol->value.integer.value[0] = val1;
  486. if (wm->ctl[n].flags & WM8776_FLAG_STEREO)
  487. ucontrol->value.integer.value[1] = val2;
  488. return 0;
  489. }
  490. static int snd_wm8776_ctl_put(struct snd_kcontrol *kcontrol,
  491. struct snd_ctl_elem_value *ucontrol)
  492. {
  493. struct snd_wm8776 *wm = snd_kcontrol_chip(kcontrol);
  494. int n = kcontrol->private_value;
  495. u16 val, regval1, regval2;
  496. /* this also works for enum because value is an union */
  497. regval1 = ucontrol->value.integer.value[0];
  498. regval2 = ucontrol->value.integer.value[1];
  499. if (wm->ctl[n].flags & WM8776_FLAG_INVERT) {
  500. regval1 = wm->ctl[n].max - (regval1 - wm->ctl[n].min);
  501. regval2 = wm->ctl[n].max - (regval2 - wm->ctl[n].min);
  502. }
  503. if (wm->ctl[n].set)
  504. wm->ctl[n].set(wm, regval1, regval2);
  505. else {
  506. val = wm->regs[wm->ctl[n].reg1] & ~wm->ctl[n].mask1;
  507. val |= regval1 << __ffs(wm->ctl[n].mask1);
  508. /* both stereo controls in one register */
  509. if (wm->ctl[n].flags & WM8776_FLAG_STEREO &&
  510. wm->ctl[n].reg1 == wm->ctl[n].reg2) {
  511. val &= ~wm->ctl[n].mask2;
  512. val |= regval2 << __ffs(wm->ctl[n].mask2);
  513. }
  514. snd_wm8776_write(wm, wm->ctl[n].reg1, val);
  515. /* stereo controls in different registers */
  516. if (wm->ctl[n].flags & WM8776_FLAG_STEREO &&
  517. wm->ctl[n].reg1 != wm->ctl[n].reg2) {
  518. val = wm->regs[wm->ctl[n].reg2] & ~wm->ctl[n].mask2;
  519. val |= regval2 << __ffs(wm->ctl[n].mask2);
  520. if (wm->ctl[n].flags & WM8776_FLAG_VOL_UPDATE)
  521. val |= WM8776_VOL_UPDATE;
  522. snd_wm8776_write(wm, wm->ctl[n].reg2, val);
  523. }
  524. }
  525. return 0;
  526. }
  527. static int snd_wm8776_add_control(struct snd_wm8776 *wm, int num)
  528. {
  529. struct snd_kcontrol_new cont;
  530. struct snd_kcontrol *ctl;
  531. memset(&cont, 0, sizeof(cont));
  532. cont.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  533. cont.private_value = num;
  534. cont.name = wm->ctl[num].name;
  535. cont.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
  536. if (wm->ctl[num].flags & WM8776_FLAG_LIM ||
  537. wm->ctl[num].flags & WM8776_FLAG_ALC)
  538. cont.access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  539. cont.tlv.p = NULL;
  540. cont.get = snd_wm8776_ctl_get;
  541. cont.put = snd_wm8776_ctl_put;
  542. switch (wm->ctl[num].type) {
  543. case SNDRV_CTL_ELEM_TYPE_INTEGER:
  544. cont.info = snd_wm8776_volume_info;
  545. cont.access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
  546. cont.tlv.p = wm->ctl[num].tlv;
  547. break;
  548. case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
  549. wm->ctl[num].max = 1;
  550. if (wm->ctl[num].flags & WM8776_FLAG_STEREO)
  551. cont.info = snd_ctl_boolean_stereo_info;
  552. else
  553. cont.info = snd_ctl_boolean_mono_info;
  554. break;
  555. case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
  556. cont.info = snd_wm8776_enum_info;
  557. break;
  558. default:
  559. return -EINVAL;
  560. }
  561. ctl = snd_ctl_new1(&cont, wm);
  562. if (!ctl)
  563. return -ENOMEM;
  564. return snd_ctl_add(wm->card, ctl);
  565. }
  566. int snd_wm8776_build_controls(struct snd_wm8776 *wm)
  567. {
  568. int err, i;
  569. for (i = 0; i < WM8776_CTL_COUNT; i++)
  570. if (wm->ctl[i].name) {
  571. err = snd_wm8776_add_control(wm, i);
  572. if (err < 0)
  573. return err;
  574. }
  575. return 0;
  576. }