phase.c 25 KB

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