phase.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. /*
  2. * ALSA driver for ICEnsemble ICE1724 (Envy24)
  3. *
  4. * Lowlevel functions for Terratec PHASE 22
  5. *
  6. * Copyright (c) 2005 Misha Zhilin <misha@epiphan.com>
  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. /* PHASE 22 overview:
  24. * Audio controller: VIA Envy24HT-S (slightly trimmed down version of Envy24HT)
  25. * Analog chip: AK4524 (partially via Philip's 74HCT125)
  26. * Digital receiver: CS8414-CS (not supported in this release)
  27. *
  28. * Envy connects to AK4524
  29. * - CS directly from GPIO 10
  30. * - CCLK via 74HCT125's gate #4 from GPIO 4
  31. * - CDTI via 74HCT125's gate #2 from GPIO 5
  32. * CDTI may be completely blocked by 74HCT125's gate #1 controlled by GPIO 3
  33. */
  34. #include <sound/driver.h>
  35. #include <asm/io.h>
  36. #include <linux/delay.h>
  37. #include <linux/interrupt.h>
  38. #include <linux/init.h>
  39. #include <linux/slab.h>
  40. #include <linux/mutex.h>
  41. #include <sound/core.h>
  42. #include "ice1712.h"
  43. #include "envy24ht.h"
  44. #include "phase.h"
  45. #include <sound/tlv.h>
  46. /* WM8770 registers */
  47. #define WM_DAC_ATTEN 0x00 /* DAC1-8 analog attenuation */
  48. #define WM_DAC_MASTER_ATTEN 0x08 /* DAC master analog attenuation */
  49. #define WM_DAC_DIG_ATTEN 0x09 /* DAC1-8 digital attenuation */
  50. #define WM_DAC_DIG_MASTER_ATTEN 0x11 /* DAC master digital attenuation */
  51. #define WM_PHASE_SWAP 0x12 /* DAC phase */
  52. #define WM_DAC_CTRL1 0x13 /* DAC control bits */
  53. #define WM_MUTE 0x14 /* mute controls */
  54. #define WM_DAC_CTRL2 0x15 /* de-emphasis and zefo-flag */
  55. #define WM_INT_CTRL 0x16 /* interface control */
  56. #define WM_MASTER 0x17 /* master clock and mode */
  57. #define WM_POWERDOWN 0x18 /* power-down controls */
  58. #define WM_ADC_GAIN 0x19 /* ADC gain L(19)/R(1a) */
  59. #define WM_ADC_MUX 0x1b /* input MUX */
  60. #define WM_OUT_MUX1 0x1c /* output MUX */
  61. #define WM_OUT_MUX2 0x1e /* output MUX */
  62. #define WM_RESET 0x1f /* software reset */
  63. /*
  64. * Logarithmic volume values for WM8770
  65. * Computed as 20 * Log10(255 / x)
  66. */
  67. static const unsigned char wm_vol[256] = {
  68. 127, 48, 42, 39, 36, 34, 33, 31, 30, 29, 28, 27, 27, 26, 25, 25, 24, 24, 23,
  69. 23, 22, 22, 21, 21, 21, 20, 20, 20, 19, 19, 19, 18, 18, 18, 18, 17, 17, 17,
  70. 17, 16, 16, 16, 16, 15, 15, 15, 15, 15, 15, 14, 14, 14, 14, 14, 13, 13, 13,
  71. 13, 13, 13, 13, 12, 12, 12, 12, 12, 12, 12, 11, 11, 11, 11, 11, 11, 11, 11,
  72. 11, 10, 10, 10, 10, 10, 10, 10, 10, 10, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 8, 8,
  73. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 6, 6, 6,
  74. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  75. 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3,
  76. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  77. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  78. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  79. 0, 0
  80. };
  81. #define WM_VOL_MAX (sizeof(wm_vol) - 1)
  82. #define WM_VOL_MUTE 0x8000
  83. static struct snd_akm4xxx akm_phase22 __devinitdata = {
  84. .type = SND_AK4524,
  85. .num_dacs = 2,
  86. .num_adcs = 2,
  87. };
  88. static struct snd_ak4xxx_private akm_phase22_priv __devinitdata = {
  89. .caddr = 2,
  90. .cif = 1,
  91. .data_mask = 1 << 4,
  92. .clk_mask = 1 << 5,
  93. .cs_mask = 1 << 10,
  94. .cs_addr = 1 << 10,
  95. .cs_none = 0,
  96. .add_flags = 1 << 3,
  97. .mask_flags = 0,
  98. };
  99. static int __devinit phase22_init(struct snd_ice1712 *ice)
  100. {
  101. struct snd_akm4xxx *ak;
  102. int err;
  103. // Configure DAC/ADC description for generic part of ice1724
  104. switch (ice->eeprom.subvendor) {
  105. case VT1724_SUBDEVICE_PHASE22:
  106. ice->num_total_dacs = 2;
  107. ice->num_total_adcs = 2;
  108. ice->vt1720 = 1; // Envy24HT-S have 16 bit wide GPIO
  109. break;
  110. default:
  111. snd_BUG();
  112. return -EINVAL;
  113. }
  114. // Initialize analog chips
  115. ak = ice->akm = kzalloc(sizeof(struct snd_akm4xxx), GFP_KERNEL);
  116. if (! ak)
  117. return -ENOMEM;
  118. ice->akm_codecs = 1;
  119. switch (ice->eeprom.subvendor) {
  120. case VT1724_SUBDEVICE_PHASE22:
  121. if ((err = snd_ice1712_akm4xxx_init(ak, &akm_phase22, &akm_phase22_priv, ice)) < 0)
  122. return err;
  123. break;
  124. }
  125. return 0;
  126. }
  127. static int __devinit phase22_add_controls(struct snd_ice1712 *ice)
  128. {
  129. int err = 0;
  130. switch (ice->eeprom.subvendor) {
  131. case VT1724_SUBDEVICE_PHASE22:
  132. err = snd_ice1712_akm4xxx_build_controls(ice);
  133. if (err < 0)
  134. return err;
  135. }
  136. return 0;
  137. }
  138. static unsigned char phase22_eeprom[] __devinitdata = {
  139. [ICE_EEP2_SYSCONF] = 0x00, /* 1xADC, 1xDACs */
  140. [ICE_EEP2_ACLINK] = 0x80, /* I2S */
  141. [ICE_EEP2_I2S] = 0xf8, /* vol, 96k, 24bit */
  142. [ICE_EEP2_SPDIF] = 0xc3, /* out-en, out-int, spdif-in */
  143. [ICE_EEP2_GPIO_DIR] = 0xff,
  144. [ICE_EEP2_GPIO_DIR1] = 0xff,
  145. [ICE_EEP2_GPIO_DIR2] = 0xff,
  146. [ICE_EEP2_GPIO_MASK] = 0x00,
  147. [ICE_EEP2_GPIO_MASK1] = 0x00,
  148. [ICE_EEP2_GPIO_MASK2] = 0x00,
  149. [ICE_EEP2_GPIO_STATE] = 0x00,
  150. [ICE_EEP2_GPIO_STATE1] = 0x00,
  151. [ICE_EEP2_GPIO_STATE2] = 0x00,
  152. };
  153. static unsigned char phase28_eeprom[] __devinitdata = {
  154. [ICE_EEP2_SYSCONF] = 0x0b, /* clock 512, spdif-in/ADC, 4DACs */
  155. [ICE_EEP2_ACLINK] = 0x80, /* I2S */
  156. [ICE_EEP2_I2S] = 0xfc, /* vol, 96k, 24bit, 192k */
  157. [ICE_EEP2_SPDIF] = 0xc3, /* out-en, out-int, spdif-in */
  158. [ICE_EEP2_GPIO_DIR] = 0xff,
  159. [ICE_EEP2_GPIO_DIR1] = 0xff,
  160. [ICE_EEP2_GPIO_DIR2] = 0x5f,
  161. [ICE_EEP2_GPIO_MASK] = 0x00,
  162. [ICE_EEP2_GPIO_MASK1] = 0x00,
  163. [ICE_EEP2_GPIO_MASK2] = 0x00,
  164. [ICE_EEP2_GPIO_STATE] = 0x00,
  165. [ICE_EEP2_GPIO_STATE1] = 0x00,
  166. [ICE_EEP2_GPIO_STATE2] = 0x00,
  167. };
  168. /*
  169. * write data in the SPI mode
  170. */
  171. static void phase28_spi_write(struct snd_ice1712 *ice, unsigned int cs, unsigned int data, int bits)
  172. {
  173. unsigned int tmp;
  174. int i;
  175. tmp = snd_ice1712_gpio_read(ice);
  176. snd_ice1712_gpio_set_mask(ice, ~(PHASE28_WM_RW|PHASE28_SPI_MOSI|PHASE28_SPI_CLK|
  177. PHASE28_WM_CS));
  178. tmp |= PHASE28_WM_RW;
  179. tmp &= ~cs;
  180. snd_ice1712_gpio_write(ice, tmp);
  181. udelay(1);
  182. for (i = bits - 1; i >= 0; i--) {
  183. tmp &= ~PHASE28_SPI_CLK;
  184. snd_ice1712_gpio_write(ice, tmp);
  185. udelay(1);
  186. if (data & (1 << i))
  187. tmp |= PHASE28_SPI_MOSI;
  188. else
  189. tmp &= ~PHASE28_SPI_MOSI;
  190. snd_ice1712_gpio_write(ice, tmp);
  191. udelay(1);
  192. tmp |= PHASE28_SPI_CLK;
  193. snd_ice1712_gpio_write(ice, tmp);
  194. udelay(1);
  195. }
  196. tmp &= ~PHASE28_SPI_CLK;
  197. tmp |= cs;
  198. snd_ice1712_gpio_write(ice, tmp);
  199. udelay(1);
  200. tmp |= PHASE28_SPI_CLK;
  201. snd_ice1712_gpio_write(ice, tmp);
  202. udelay(1);
  203. }
  204. /*
  205. * get the current register value of WM codec
  206. */
  207. static unsigned short wm_get(struct snd_ice1712 *ice, int reg)
  208. {
  209. reg <<= 1;
  210. return ((unsigned short)ice->akm[0].images[reg] << 8) |
  211. ice->akm[0].images[reg + 1];
  212. }
  213. /*
  214. * set the register value of WM codec
  215. */
  216. static void wm_put_nocache(struct snd_ice1712 *ice, int reg, unsigned short val)
  217. {
  218. phase28_spi_write(ice, PHASE28_WM_CS, (reg << 9) | (val & 0x1ff), 16);
  219. }
  220. /*
  221. * set the register value of WM codec and remember it
  222. */
  223. static void wm_put(struct snd_ice1712 *ice, int reg, unsigned short val)
  224. {
  225. wm_put_nocache(ice, reg, val);
  226. reg <<= 1;
  227. ice->akm[0].images[reg] = val >> 8;
  228. ice->akm[0].images[reg + 1] = val;
  229. }
  230. static void wm_set_vol(struct snd_ice1712 *ice, unsigned int index, unsigned short vol, unsigned short master)
  231. {
  232. unsigned char nvol;
  233. if ((master & WM_VOL_MUTE) || (vol & WM_VOL_MUTE))
  234. nvol = 0;
  235. else
  236. nvol = 127 - wm_vol[(((vol & ~WM_VOL_MUTE) * (master & ~WM_VOL_MUTE)) / 127) & WM_VOL_MAX];
  237. wm_put(ice, index, nvol);
  238. wm_put_nocache(ice, index, 0x180 | nvol);
  239. }
  240. /*
  241. * DAC mute control
  242. */
  243. #define wm_pcm_mute_info snd_ctl_boolean_mono_info
  244. static int wm_pcm_mute_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  245. {
  246. struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
  247. mutex_lock(&ice->gpio_mutex);
  248. ucontrol->value.integer.value[0] = (wm_get(ice, WM_MUTE) & 0x10) ? 0 : 1;
  249. mutex_unlock(&ice->gpio_mutex);
  250. return 0;
  251. }
  252. static int wm_pcm_mute_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  253. {
  254. struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
  255. unsigned short nval, oval;
  256. int change;
  257. snd_ice1712_save_gpio_status(ice);
  258. oval = wm_get(ice, WM_MUTE);
  259. nval = (oval & ~0x10) | (ucontrol->value.integer.value[0] ? 0 : 0x10);
  260. if ((change = (nval != oval)))
  261. wm_put(ice, WM_MUTE, nval);
  262. snd_ice1712_restore_gpio_status(ice);
  263. return change;
  264. }
  265. /*
  266. * Master volume attenuation mixer control
  267. */
  268. static int wm_master_vol_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  269. {
  270. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  271. uinfo->count = 2;
  272. uinfo->value.integer.min = 0;
  273. uinfo->value.integer.max = WM_VOL_MAX;
  274. return 0;
  275. }
  276. static int wm_master_vol_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  277. {
  278. struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
  279. int i;
  280. for (i=0; i<2; i++)
  281. ucontrol->value.integer.value[i] = ice->spec.phase28.master[i] & ~WM_VOL_MUTE;
  282. return 0;
  283. }
  284. static int wm_master_vol_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  285. {
  286. struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
  287. int ch, change = 0;
  288. snd_ice1712_save_gpio_status(ice);
  289. for (ch = 0; ch < 2; ch++) {
  290. if (ucontrol->value.integer.value[ch] != ice->spec.phase28.master[ch]) {
  291. int dac;
  292. ice->spec.phase28.master[ch] &= WM_VOL_MUTE;
  293. ice->spec.phase28.master[ch] |= ucontrol->value.integer.value[ch];
  294. for (dac = 0; dac < ice->num_total_dacs; dac += 2)
  295. wm_set_vol(ice, WM_DAC_ATTEN + dac + ch,
  296. ice->spec.phase28.vol[dac + ch],
  297. ice->spec.phase28.master[ch]);
  298. change = 1;
  299. }
  300. }
  301. snd_ice1712_restore_gpio_status(ice);
  302. return change;
  303. }
  304. static int __devinit phase28_init(struct snd_ice1712 *ice)
  305. {
  306. static const unsigned short wm_inits_phase28[] = {
  307. /* These come first to reduce init pop noise */
  308. 0x1b, 0x044, /* ADC Mux (AC'97 source) */
  309. 0x1c, 0x00B, /* Out Mux1 (VOUT1 = DAC+AUX, VOUT2 = DAC) */
  310. 0x1d, 0x009, /* Out Mux2 (VOUT2 = DAC, VOUT3 = DAC) */
  311. 0x18, 0x000, /* All power-up */
  312. 0x16, 0x122, /* I2S, normal polarity, 24bit */
  313. 0x17, 0x022, /* 256fs, slave mode */
  314. 0x00, 0, /* DAC1 analog mute */
  315. 0x01, 0, /* DAC2 analog mute */
  316. 0x02, 0, /* DAC3 analog mute */
  317. 0x03, 0, /* DAC4 analog mute */
  318. 0x04, 0, /* DAC5 analog mute */
  319. 0x05, 0, /* DAC6 analog mute */
  320. 0x06, 0, /* DAC7 analog mute */
  321. 0x07, 0, /* DAC8 analog mute */
  322. 0x08, 0x100, /* master analog mute */
  323. 0x09, 0xff, /* DAC1 digital full */
  324. 0x0a, 0xff, /* DAC2 digital full */
  325. 0x0b, 0xff, /* DAC3 digital full */
  326. 0x0c, 0xff, /* DAC4 digital full */
  327. 0x0d, 0xff, /* DAC5 digital full */
  328. 0x0e, 0xff, /* DAC6 digital full */
  329. 0x0f, 0xff, /* DAC7 digital full */
  330. 0x10, 0xff, /* DAC8 digital full */
  331. 0x11, 0x1ff, /* master digital full */
  332. 0x12, 0x000, /* phase normal */
  333. 0x13, 0x090, /* unmute DAC L/R */
  334. 0x14, 0x000, /* all unmute */
  335. 0x15, 0x000, /* no deemphasis, no ZFLG */
  336. 0x19, 0x000, /* -12dB ADC/L */
  337. 0x1a, 0x000, /* -12dB ADC/R */
  338. (unsigned short)-1
  339. };
  340. unsigned int tmp;
  341. struct snd_akm4xxx *ak;
  342. const unsigned short *p;
  343. int i;
  344. ice->num_total_dacs = 8;
  345. ice->num_total_adcs = 2;
  346. // Initialize analog chips
  347. ak = ice->akm = kzalloc(sizeof(struct snd_akm4xxx), GFP_KERNEL);
  348. if (!ak)
  349. return -ENOMEM;
  350. ice->akm_codecs = 1;
  351. snd_ice1712_gpio_set_dir(ice, 0x5fffff); /* fix this for the time being */
  352. /* reset the wm codec as the SPI mode */
  353. snd_ice1712_save_gpio_status(ice);
  354. snd_ice1712_gpio_set_mask(ice, ~(PHASE28_WM_RESET|PHASE28_WM_CS|PHASE28_HP_SEL));
  355. tmp = snd_ice1712_gpio_read(ice);
  356. tmp &= ~PHASE28_WM_RESET;
  357. snd_ice1712_gpio_write(ice, tmp);
  358. udelay(1);
  359. tmp |= PHASE28_WM_CS;
  360. snd_ice1712_gpio_write(ice, tmp);
  361. udelay(1);
  362. tmp |= PHASE28_WM_RESET;
  363. snd_ice1712_gpio_write(ice, tmp);
  364. udelay(1);
  365. p = wm_inits_phase28;
  366. for (; *p != (unsigned short)-1; p += 2)
  367. wm_put(ice, p[0], p[1]);
  368. snd_ice1712_restore_gpio_status(ice);
  369. ice->spec.phase28.master[0] = WM_VOL_MUTE;
  370. ice->spec.phase28.master[1] = WM_VOL_MUTE;
  371. for (i = 0; i < ice->num_total_dacs; i++) {
  372. ice->spec.phase28.vol[i] = WM_VOL_MUTE;
  373. wm_set_vol(ice, i, ice->spec.phase28.vol[i], ice->spec.phase28.master[i % 2]);
  374. }
  375. return 0;
  376. }
  377. /*
  378. * DAC volume attenuation mixer control
  379. */
  380. static int wm_vol_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  381. {
  382. int voices = kcontrol->private_value >> 8;
  383. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  384. uinfo->count = voices;
  385. uinfo->value.integer.min = 0; /* mute (-101dB) */
  386. uinfo->value.integer.max = 0x7F; /* 0dB */
  387. return 0;
  388. }
  389. static int wm_vol_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  390. {
  391. struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
  392. int i, ofs, voices;
  393. voices = kcontrol->private_value >> 8;
  394. ofs = kcontrol->private_value & 0xff;
  395. for (i = 0; i < voices; i++)
  396. ucontrol->value.integer.value[i] = ice->spec.phase28.vol[ofs+i] & ~WM_VOL_MUTE;
  397. return 0;
  398. }
  399. static int wm_vol_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  400. {
  401. struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
  402. int i, idx, ofs, voices;
  403. int change = 0;
  404. voices = kcontrol->private_value >> 8;
  405. ofs = kcontrol->private_value & 0xff;
  406. snd_ice1712_save_gpio_status(ice);
  407. for (i = 0; i < voices; i++) {
  408. idx = WM_DAC_ATTEN + ofs + i;
  409. if (ucontrol->value.integer.value[i] != ice->spec.phase28.vol[ofs+i]) {
  410. ice->spec.phase28.vol[ofs+i] &= WM_VOL_MUTE;
  411. ice->spec.phase28.vol[ofs+i] |= ucontrol->value.integer.value[i];
  412. wm_set_vol(ice, idx, ice->spec.phase28.vol[ofs+i],
  413. ice->spec.phase28.master[i]);
  414. change = 1;
  415. }
  416. }
  417. snd_ice1712_restore_gpio_status(ice);
  418. return change;
  419. }
  420. /*
  421. * WM8770 mute control
  422. */
  423. static int wm_mute_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) {
  424. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  425. uinfo->count = kcontrol->private_value >> 8;
  426. uinfo->value.integer.min = 0;
  427. uinfo->value.integer.max = 1;
  428. return 0;
  429. }
  430. static int wm_mute_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  431. {
  432. struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
  433. int voices, ofs, i;
  434. voices = kcontrol->private_value >> 8;
  435. ofs = kcontrol->private_value & 0xFF;
  436. for (i = 0; i < voices; i++)
  437. ucontrol->value.integer.value[i] = (ice->spec.phase28.vol[ofs+i] & WM_VOL_MUTE) ? 0 : 1;
  438. return 0;
  439. }
  440. static int wm_mute_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  441. {
  442. struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
  443. int change = 0, voices, ofs, i;
  444. voices = kcontrol->private_value >> 8;
  445. ofs = kcontrol->private_value & 0xFF;
  446. snd_ice1712_save_gpio_status(ice);
  447. for (i = 0; i < voices; i++) {
  448. int val = (ice->spec.phase28.vol[ofs + i] & WM_VOL_MUTE) ? 0 : 1;
  449. if (ucontrol->value.integer.value[i] != val) {
  450. ice->spec.phase28.vol[ofs + i] &= ~WM_VOL_MUTE;
  451. ice->spec.phase28.vol[ofs + i] |=
  452. ucontrol->value.integer.value[i] ? 0 : WM_VOL_MUTE;
  453. wm_set_vol(ice, ofs + i, ice->spec.phase28.vol[ofs + i],
  454. ice->spec.phase28.master[i]);
  455. change = 1;
  456. }
  457. }
  458. snd_ice1712_restore_gpio_status(ice);
  459. return change;
  460. }
  461. /*
  462. * WM8770 master mute control
  463. */
  464. #define wm_master_mute_info snd_ctl_boolean_stereo_info
  465. static int wm_master_mute_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  466. {
  467. struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
  468. ucontrol->value.integer.value[0] = (ice->spec.phase28.master[0] & WM_VOL_MUTE) ? 0 : 1;
  469. ucontrol->value.integer.value[1] = (ice->spec.phase28.master[1] & WM_VOL_MUTE) ? 0 : 1;
  470. return 0;
  471. }
  472. static int wm_master_mute_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  473. {
  474. struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
  475. int change = 0, i;
  476. snd_ice1712_save_gpio_status(ice);
  477. for (i = 0; i < 2; i++) {
  478. int val = (ice->spec.phase28.master[i] & WM_VOL_MUTE) ? 0 : 1;
  479. if (ucontrol->value.integer.value[i] != val) {
  480. int dac;
  481. ice->spec.phase28.master[i] &= ~WM_VOL_MUTE;
  482. ice->spec.phase28.master[i] |=
  483. ucontrol->value.integer.value[i] ? 0 : WM_VOL_MUTE;
  484. for (dac = 0; dac < ice->num_total_dacs; dac += 2)
  485. wm_set_vol(ice, WM_DAC_ATTEN + dac + i,
  486. ice->spec.phase28.vol[dac + i],
  487. ice->spec.phase28.master[i]);
  488. change = 1;
  489. }
  490. }
  491. snd_ice1712_restore_gpio_status(ice);
  492. return change;
  493. }
  494. /* digital master volume */
  495. #define PCM_0dB 0xff
  496. #define PCM_RES 128 /* -64dB */
  497. #define PCM_MIN (PCM_0dB - PCM_RES)
  498. static int wm_pcm_vol_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  499. {
  500. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  501. uinfo->count = 1;
  502. uinfo->value.integer.min = 0; /* mute (-64dB) */
  503. uinfo->value.integer.max = PCM_RES; /* 0dB */
  504. return 0;
  505. }
  506. static int wm_pcm_vol_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  507. {
  508. struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
  509. unsigned short val;
  510. mutex_lock(&ice->gpio_mutex);
  511. val = wm_get(ice, WM_DAC_DIG_MASTER_ATTEN) & 0xff;
  512. val = val > PCM_MIN ? (val - PCM_MIN) : 0;
  513. ucontrol->value.integer.value[0] = val;
  514. mutex_unlock(&ice->gpio_mutex);
  515. return 0;
  516. }
  517. static int wm_pcm_vol_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  518. {
  519. struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
  520. unsigned short ovol, nvol;
  521. int change = 0;
  522. snd_ice1712_save_gpio_status(ice);
  523. nvol = ucontrol->value.integer.value[0];
  524. nvol = (nvol ? (nvol + PCM_MIN) : 0) & 0xff;
  525. ovol = wm_get(ice, WM_DAC_DIG_MASTER_ATTEN) & 0xff;
  526. if (ovol != nvol) {
  527. wm_put(ice, WM_DAC_DIG_MASTER_ATTEN, nvol); /* prelatch */
  528. wm_put_nocache(ice, WM_DAC_DIG_MASTER_ATTEN, nvol | 0x100); /* update */
  529. change = 1;
  530. }
  531. snd_ice1712_restore_gpio_status(ice);
  532. return change;
  533. }
  534. /*
  535. * Deemphasis
  536. */
  537. #define phase28_deemp_info snd_ctl_boolean_mono_info
  538. static int phase28_deemp_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  539. {
  540. struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
  541. ucontrol->value.integer.value[0] = (wm_get(ice, WM_DAC_CTRL2) & 0xf) == 0xf;
  542. return 0;
  543. }
  544. static int phase28_deemp_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  545. {
  546. struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
  547. int temp, temp2;
  548. temp2 = temp = wm_get(ice, WM_DAC_CTRL2);
  549. if (ucontrol->value.integer.value[0])
  550. temp |= 0xf;
  551. else
  552. temp &= ~0xf;
  553. if (temp != temp2) {
  554. wm_put(ice, WM_DAC_CTRL2, temp);
  555. return 1;
  556. }
  557. return 0;
  558. }
  559. /*
  560. * ADC Oversampling
  561. */
  562. static int phase28_oversampling_info(struct snd_kcontrol *k, struct snd_ctl_elem_info *uinfo)
  563. {
  564. static char *texts[2] = { "128x", "64x" };
  565. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  566. uinfo->count = 1;
  567. uinfo->value.enumerated.items = 2;
  568. if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
  569. uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
  570. strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
  571. return 0;
  572. }
  573. static int phase28_oversampling_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  574. {
  575. struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
  576. ucontrol->value.enumerated.item[0] = (wm_get(ice, WM_MASTER) & 0x8) == 0x8;
  577. return 0;
  578. }
  579. static int phase28_oversampling_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  580. {
  581. int temp, temp2;
  582. struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
  583. temp2 = temp = wm_get(ice, WM_MASTER);
  584. if (ucontrol->value.enumerated.item[0])
  585. temp |= 0x8;
  586. else
  587. temp &= ~0x8;
  588. if (temp != temp2) {
  589. wm_put(ice, WM_MASTER, temp);
  590. return 1;
  591. }
  592. return 0;
  593. }
  594. static const DECLARE_TLV_DB_SCALE(db_scale_wm_dac, -12700, 100, 1);
  595. static const DECLARE_TLV_DB_SCALE(db_scale_wm_pcm, -6400, 50, 1);
  596. static struct snd_kcontrol_new phase28_dac_controls[] __devinitdata = {
  597. {
  598. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  599. .name = "Master Playback Switch",
  600. .info = wm_master_mute_info,
  601. .get = wm_master_mute_get,
  602. .put = wm_master_mute_put
  603. },
  604. {
  605. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  606. .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
  607. SNDRV_CTL_ELEM_ACCESS_TLV_READ),
  608. .name = "Master Playback Volume",
  609. .info = wm_master_vol_info,
  610. .get = wm_master_vol_get,
  611. .put = wm_master_vol_put,
  612. .tlv = { .p = db_scale_wm_dac }
  613. },
  614. {
  615. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  616. .name = "Front Playback Switch",
  617. .info = wm_mute_info,
  618. .get = wm_mute_get,
  619. .put = wm_mute_put,
  620. .private_value = (2 << 8) | 0
  621. },
  622. {
  623. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  624. .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
  625. SNDRV_CTL_ELEM_ACCESS_TLV_READ),
  626. .name = "Front Playback Volume",
  627. .info = wm_vol_info,
  628. .get = wm_vol_get,
  629. .put = wm_vol_put,
  630. .private_value = (2 << 8) | 0,
  631. .tlv = { .p = db_scale_wm_dac }
  632. },
  633. {
  634. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  635. .name = "Rear Playback Switch",
  636. .info = wm_mute_info,
  637. .get = wm_mute_get,
  638. .put = wm_mute_put,
  639. .private_value = (2 << 8) | 2
  640. },
  641. {
  642. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  643. .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
  644. SNDRV_CTL_ELEM_ACCESS_TLV_READ),
  645. .name = "Rear Playback Volume",
  646. .info = wm_vol_info,
  647. .get = wm_vol_get,
  648. .put = wm_vol_put,
  649. .private_value = (2 << 8) | 2,
  650. .tlv = { .p = db_scale_wm_dac }
  651. },
  652. {
  653. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  654. .name = "Center Playback Switch",
  655. .info = wm_mute_info,
  656. .get = wm_mute_get,
  657. .put = wm_mute_put,
  658. .private_value = (1 << 8) | 4
  659. },
  660. {
  661. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  662. .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
  663. SNDRV_CTL_ELEM_ACCESS_TLV_READ),
  664. .name = "Center Playback Volume",
  665. .info = wm_vol_info,
  666. .get = wm_vol_get,
  667. .put = wm_vol_put,
  668. .private_value = (1 << 8) | 4,
  669. .tlv = { .p = db_scale_wm_dac }
  670. },
  671. {
  672. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  673. .name = "LFE Playback Switch",
  674. .info = wm_mute_info,
  675. .get = wm_mute_get,
  676. .put = wm_mute_put,
  677. .private_value = (1 << 8) | 5
  678. },
  679. {
  680. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  681. .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
  682. SNDRV_CTL_ELEM_ACCESS_TLV_READ),
  683. .name = "LFE Playback Volume",
  684. .info = wm_vol_info,
  685. .get = wm_vol_get,
  686. .put = wm_vol_put,
  687. .private_value = (1 << 8) | 5,
  688. .tlv = { .p = db_scale_wm_dac }
  689. },
  690. {
  691. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  692. .name = "Side Playback Switch",
  693. .info = wm_mute_info,
  694. .get = wm_mute_get,
  695. .put = wm_mute_put,
  696. .private_value = (2 << 8) | 6
  697. },
  698. {
  699. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  700. .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
  701. SNDRV_CTL_ELEM_ACCESS_TLV_READ),
  702. .name = "Side Playback Volume",
  703. .info = wm_vol_info,
  704. .get = wm_vol_get,
  705. .put = wm_vol_put,
  706. .private_value = (2 << 8) | 6,
  707. .tlv = { .p = db_scale_wm_dac }
  708. }
  709. };
  710. static struct snd_kcontrol_new wm_controls[] __devinitdata = {
  711. {
  712. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  713. .name = "PCM Playback Switch",
  714. .info = wm_pcm_mute_info,
  715. .get = wm_pcm_mute_get,
  716. .put = wm_pcm_mute_put
  717. },
  718. {
  719. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  720. .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
  721. SNDRV_CTL_ELEM_ACCESS_TLV_READ),
  722. .name = "PCM Playback Volume",
  723. .info = wm_pcm_vol_info,
  724. .get = wm_pcm_vol_get,
  725. .put = wm_pcm_vol_put,
  726. .tlv = { .p = db_scale_wm_pcm }
  727. },
  728. {
  729. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  730. .name = "DAC Deemphasis Switch",
  731. .info = phase28_deemp_info,
  732. .get = phase28_deemp_get,
  733. .put = phase28_deemp_put
  734. },
  735. {
  736. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  737. .name = "ADC Oversampling",
  738. .info = phase28_oversampling_info,
  739. .get = phase28_oversampling_get,
  740. .put = phase28_oversampling_put
  741. }
  742. };
  743. static int __devinit phase28_add_controls(struct snd_ice1712 *ice)
  744. {
  745. unsigned int i, counts;
  746. int err;
  747. counts = ARRAY_SIZE(phase28_dac_controls);
  748. for (i = 0; i < counts; i++) {
  749. err = snd_ctl_add(ice->card, snd_ctl_new1(&phase28_dac_controls[i], ice));
  750. if (err < 0)
  751. return err;
  752. }
  753. for (i = 0; i < ARRAY_SIZE(wm_controls); i++) {
  754. err = snd_ctl_add(ice->card, snd_ctl_new1(&wm_controls[i], ice));
  755. if (err < 0)
  756. return err;
  757. }
  758. return 0;
  759. }
  760. struct snd_ice1712_card_info snd_vt1724_phase_cards[] __devinitdata = {
  761. {
  762. .subvendor = VT1724_SUBDEVICE_PHASE22,
  763. .name = "Terratec PHASE 22",
  764. .model = "phase22",
  765. .chip_init = phase22_init,
  766. .build_controls = phase22_add_controls,
  767. .eeprom_size = sizeof(phase22_eeprom),
  768. .eeprom_data = phase22_eeprom,
  769. },
  770. {
  771. .subvendor = VT1724_SUBDEVICE_PHASE28,
  772. .name = "Terratec PHASE 28",
  773. .model = "phase28",
  774. .chip_init = phase28_init,
  775. .build_controls = phase28_add_controls,
  776. .eeprom_size = sizeof(phase28_eeprom),
  777. .eeprom_data = phase28_eeprom,
  778. },
  779. { } /* terminator */
  780. };