phase.c 25 KB

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