phase.c 25 KB

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