phase.c 26 KB

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