juli.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. /*
  2. * ALSA driver for ICEnsemble VT1724 (Envy24HT)
  3. *
  4. * Lowlevel functions for ESI Juli@ cards
  5. *
  6. * Copyright (c) 2004 Jaroslav Kysela <perex@perex.cz>
  7. * 2008 Pavel Hofman <dustin@seznam.cz>
  8. *
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. */
  25. #include <asm/io.h>
  26. #include <linux/delay.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/init.h>
  29. #include <linux/slab.h>
  30. #include <sound/core.h>
  31. #include <sound/tlv.h>
  32. #include "ice1712.h"
  33. #include "envy24ht.h"
  34. #include "juli.h"
  35. struct juli_spec {
  36. struct ak4114 *ak4114;
  37. unsigned int analog:1;
  38. };
  39. /*
  40. * chip addresses on I2C bus
  41. */
  42. #define AK4114_ADDR 0x20 /* S/PDIF receiver */
  43. #define AK4358_ADDR 0x22 /* DAC */
  44. /*
  45. * Juli does not use the standard ICE1724 clock scheme. Juli's ice1724 chip is
  46. * supplied by external clock provided by Xilinx array and MK73-1 PLL frequency
  47. * multiplier. Actual frequency is set by ice1724 GPIOs hooked to the Xilinx.
  48. *
  49. * The clock circuitry is supplied by the two ice1724 crystals. This
  50. * arrangement allows to generate independent clock signal for AK4114's input
  51. * rate detection circuit. As a result, Juli, unlike most other
  52. * ice1724+ak4114-based cards, detects spdif input rate correctly.
  53. * This fact is applied in the driver, allowing to modify PCM stream rate
  54. * parameter according to the actual input rate.
  55. *
  56. * Juli uses the remaining three stereo-channels of its DAC to optionally
  57. * monitor analog input, digital input, and digital output. The corresponding
  58. * I2S signals are routed by Xilinx, controlled by GPIOs.
  59. *
  60. * The master mute is implemented using output muting transistors (GPIO) in
  61. * combination with smuting the DAC.
  62. *
  63. * The card itself has no HW master volume control, implemented using the
  64. * vmaster control.
  65. *
  66. * TODO:
  67. * researching and fixing the input monitors
  68. */
  69. /*
  70. * GPIO pins
  71. */
  72. #define GPIO_FREQ_MASK (3<<0)
  73. #define GPIO_FREQ_32KHZ (0<<0)
  74. #define GPIO_FREQ_44KHZ (1<<0)
  75. #define GPIO_FREQ_48KHZ (2<<0)
  76. #define GPIO_MULTI_MASK (3<<2)
  77. #define GPIO_MULTI_4X (0<<2)
  78. #define GPIO_MULTI_2X (1<<2)
  79. #define GPIO_MULTI_1X (2<<2) /* also external */
  80. #define GPIO_MULTI_HALF (3<<2)
  81. #define GPIO_INTERNAL_CLOCK (1<<4) /* 0 = external, 1 = internal */
  82. #define GPIO_CLOCK_MASK (1<<4)
  83. #define GPIO_ANALOG_PRESENT (1<<5) /* RO only: 0 = present */
  84. #define GPIO_RXMCLK_SEL (1<<7) /* must be 0 */
  85. #define GPIO_AK5385A_CKS0 (1<<8)
  86. #define GPIO_AK5385A_DFS1 (1<<9)
  87. #define GPIO_AK5385A_DFS0 (1<<10)
  88. #define GPIO_DIGOUT_MONITOR (1<<11) /* 1 = active */
  89. #define GPIO_DIGIN_MONITOR (1<<12) /* 1 = active */
  90. #define GPIO_ANAIN_MONITOR (1<<13) /* 1 = active */
  91. #define GPIO_AK5385A_CKS1 (1<<14) /* must be 0 */
  92. #define GPIO_MUTE_CONTROL (1<<15) /* output mute, 1 = muted */
  93. #define GPIO_RATE_MASK (GPIO_FREQ_MASK | GPIO_MULTI_MASK | \
  94. GPIO_CLOCK_MASK)
  95. #define GPIO_AK5385A_MASK (GPIO_AK5385A_CKS0 | GPIO_AK5385A_DFS0 | \
  96. GPIO_AK5385A_DFS1 | GPIO_AK5385A_CKS1)
  97. #define JULI_PCM_RATE (SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 | \
  98. SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
  99. SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_64000 | \
  100. SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | \
  101. SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000)
  102. #define GPIO_RATE_16000 (GPIO_FREQ_32KHZ | GPIO_MULTI_HALF | \
  103. GPIO_INTERNAL_CLOCK)
  104. #define GPIO_RATE_22050 (GPIO_FREQ_44KHZ | GPIO_MULTI_HALF | \
  105. GPIO_INTERNAL_CLOCK)
  106. #define GPIO_RATE_24000 (GPIO_FREQ_48KHZ | GPIO_MULTI_HALF | \
  107. GPIO_INTERNAL_CLOCK)
  108. #define GPIO_RATE_32000 (GPIO_FREQ_32KHZ | GPIO_MULTI_1X | \
  109. GPIO_INTERNAL_CLOCK)
  110. #define GPIO_RATE_44100 (GPIO_FREQ_44KHZ | GPIO_MULTI_1X | \
  111. GPIO_INTERNAL_CLOCK)
  112. #define GPIO_RATE_48000 (GPIO_FREQ_48KHZ | GPIO_MULTI_1X | \
  113. GPIO_INTERNAL_CLOCK)
  114. #define GPIO_RATE_64000 (GPIO_FREQ_32KHZ | GPIO_MULTI_2X | \
  115. GPIO_INTERNAL_CLOCK)
  116. #define GPIO_RATE_88200 (GPIO_FREQ_44KHZ | GPIO_MULTI_2X | \
  117. GPIO_INTERNAL_CLOCK)
  118. #define GPIO_RATE_96000 (GPIO_FREQ_48KHZ | GPIO_MULTI_2X | \
  119. GPIO_INTERNAL_CLOCK)
  120. #define GPIO_RATE_176400 (GPIO_FREQ_44KHZ | GPIO_MULTI_4X | \
  121. GPIO_INTERNAL_CLOCK)
  122. #define GPIO_RATE_192000 (GPIO_FREQ_48KHZ | GPIO_MULTI_4X | \
  123. GPIO_INTERNAL_CLOCK)
  124. /*
  125. * Initial setup of the conversion array GPIO <-> rate
  126. */
  127. static unsigned int juli_rates[] = {
  128. 16000, 22050, 24000, 32000,
  129. 44100, 48000, 64000, 88200,
  130. 96000, 176400, 192000,
  131. };
  132. static unsigned int gpio_vals[] = {
  133. GPIO_RATE_16000, GPIO_RATE_22050, GPIO_RATE_24000, GPIO_RATE_32000,
  134. GPIO_RATE_44100, GPIO_RATE_48000, GPIO_RATE_64000, GPIO_RATE_88200,
  135. GPIO_RATE_96000, GPIO_RATE_176400, GPIO_RATE_192000,
  136. };
  137. static struct snd_pcm_hw_constraint_list juli_rates_info = {
  138. .count = ARRAY_SIZE(juli_rates),
  139. .list = juli_rates,
  140. .mask = 0,
  141. };
  142. static int get_gpio_val(int rate)
  143. {
  144. int i;
  145. for (i = 0; i < ARRAY_SIZE(juli_rates); i++)
  146. if (juli_rates[i] == rate)
  147. return gpio_vals[i];
  148. return 0;
  149. }
  150. static void juli_ak4114_write(void *private_data, unsigned char reg,
  151. unsigned char val)
  152. {
  153. snd_vt1724_write_i2c((struct snd_ice1712 *)private_data, AK4114_ADDR,
  154. reg, val);
  155. }
  156. static unsigned char juli_ak4114_read(void *private_data, unsigned char reg)
  157. {
  158. return snd_vt1724_read_i2c((struct snd_ice1712 *)private_data,
  159. AK4114_ADDR, reg);
  160. }
  161. /*
  162. * If SPDIF capture and slaved to SPDIF-IN, setting runtime rate
  163. * to the external rate
  164. */
  165. static void juli_spdif_in_open(struct snd_ice1712 *ice,
  166. struct snd_pcm_substream *substream)
  167. {
  168. struct juli_spec *spec = ice->spec;
  169. struct snd_pcm_runtime *runtime = substream->runtime;
  170. int rate;
  171. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK ||
  172. !ice->is_spdif_master(ice))
  173. return;
  174. rate = snd_ak4114_external_rate(spec->ak4114);
  175. if (rate >= runtime->hw.rate_min && rate <= runtime->hw.rate_max) {
  176. runtime->hw.rate_min = rate;
  177. runtime->hw.rate_max = rate;
  178. }
  179. }
  180. /*
  181. * AK4358 section
  182. */
  183. static void juli_akm_lock(struct snd_akm4xxx *ak, int chip)
  184. {
  185. }
  186. static void juli_akm_unlock(struct snd_akm4xxx *ak, int chip)
  187. {
  188. }
  189. static void juli_akm_write(struct snd_akm4xxx *ak, int chip,
  190. unsigned char addr, unsigned char data)
  191. {
  192. struct snd_ice1712 *ice = ak->private_data[0];
  193. if (snd_BUG_ON(chip))
  194. return;
  195. snd_vt1724_write_i2c(ice, AK4358_ADDR, addr, data);
  196. }
  197. /*
  198. * change the rate of envy24HT, AK4358, AK5385
  199. */
  200. static void juli_akm_set_rate_val(struct snd_akm4xxx *ak, unsigned int rate)
  201. {
  202. unsigned char old, tmp, ak4358_dfs;
  203. unsigned int ak5385_pins, old_gpio, new_gpio;
  204. struct snd_ice1712 *ice = ak->private_data[0];
  205. struct juli_spec *spec = ice->spec;
  206. if (rate == 0) /* no hint - S/PDIF input is master or the new spdif
  207. input rate undetected, simply return */
  208. return;
  209. /* adjust DFS on codecs */
  210. if (rate > 96000) {
  211. ak4358_dfs = 2;
  212. ak5385_pins = GPIO_AK5385A_DFS1 | GPIO_AK5385A_CKS0;
  213. } else if (rate > 48000) {
  214. ak4358_dfs = 1;
  215. ak5385_pins = GPIO_AK5385A_DFS0;
  216. } else {
  217. ak4358_dfs = 0;
  218. ak5385_pins = 0;
  219. }
  220. /* AK5385 first, since it requires cold reset affecting both codecs */
  221. old_gpio = ice->gpio.get_data(ice);
  222. new_gpio = (old_gpio & ~GPIO_AK5385A_MASK) | ak5385_pins;
  223. /* printk(KERN_DEBUG "JULI - ak5385 set_rate_val: new gpio 0x%x\n",
  224. new_gpio); */
  225. ice->gpio.set_data(ice, new_gpio);
  226. /* cold reset */
  227. old = inb(ICEMT1724(ice, AC97_CMD));
  228. outb(old | VT1724_AC97_COLD, ICEMT1724(ice, AC97_CMD));
  229. udelay(1);
  230. outb(old & ~VT1724_AC97_COLD, ICEMT1724(ice, AC97_CMD));
  231. /* AK4358 */
  232. /* set new value, reset DFS */
  233. tmp = snd_akm4xxx_get(ak, 0, 2);
  234. snd_akm4xxx_reset(ak, 1);
  235. tmp = snd_akm4xxx_get(ak, 0, 2);
  236. tmp &= ~(0x03 << 4);
  237. tmp |= ak4358_dfs << 4;
  238. snd_akm4xxx_set(ak, 0, 2, tmp);
  239. snd_akm4xxx_reset(ak, 0);
  240. /* reinit ak4114 */
  241. snd_ak4114_reinit(spec->ak4114);
  242. }
  243. #define AK_DAC(xname, xch) { .name = xname, .num_channels = xch }
  244. #define PCM_VOLUME "PCM Playback Volume"
  245. #define MONITOR_AN_IN_VOLUME "Monitor Analog In Volume"
  246. #define MONITOR_DIG_IN_VOLUME "Monitor Digital In Volume"
  247. #define MONITOR_DIG_OUT_VOLUME "Monitor Digital Out Volume"
  248. static const struct snd_akm4xxx_dac_channel juli_dac[] = {
  249. AK_DAC(PCM_VOLUME, 2),
  250. AK_DAC(MONITOR_AN_IN_VOLUME, 2),
  251. AK_DAC(MONITOR_DIG_OUT_VOLUME, 2),
  252. AK_DAC(MONITOR_DIG_IN_VOLUME, 2),
  253. };
  254. static struct snd_akm4xxx akm_juli_dac __devinitdata = {
  255. .type = SND_AK4358,
  256. .num_dacs = 8, /* DAC1 - analog out
  257. DAC2 - analog in monitor
  258. DAC3 - digital out monitor
  259. DAC4 - digital in monitor
  260. */
  261. .ops = {
  262. .lock = juli_akm_lock,
  263. .unlock = juli_akm_unlock,
  264. .write = juli_akm_write,
  265. .set_rate_val = juli_akm_set_rate_val
  266. },
  267. .dac_info = juli_dac,
  268. };
  269. #define juli_mute_info snd_ctl_boolean_mono_info
  270. static int juli_mute_get(struct snd_kcontrol *kcontrol,
  271. struct snd_ctl_elem_value *ucontrol)
  272. {
  273. struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
  274. unsigned int val;
  275. val = ice->gpio.get_data(ice) & (unsigned int) kcontrol->private_value;
  276. if (kcontrol->private_value == GPIO_MUTE_CONTROL)
  277. /* val 0 = signal on */
  278. ucontrol->value.integer.value[0] = (val) ? 0 : 1;
  279. else
  280. /* val 1 = signal on */
  281. ucontrol->value.integer.value[0] = (val) ? 1 : 0;
  282. return 0;
  283. }
  284. static int juli_mute_put(struct snd_kcontrol *kcontrol,
  285. struct snd_ctl_elem_value *ucontrol)
  286. {
  287. struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
  288. unsigned int old_gpio, new_gpio;
  289. old_gpio = ice->gpio.get_data(ice);
  290. if (ucontrol->value.integer.value[0]) {
  291. /* unmute */
  292. if (kcontrol->private_value == GPIO_MUTE_CONTROL) {
  293. /* 0 = signal on */
  294. new_gpio = old_gpio & ~GPIO_MUTE_CONTROL;
  295. /* un-smuting DAC */
  296. snd_akm4xxx_write(ice->akm, 0, 0x01, 0x01);
  297. } else
  298. /* 1 = signal on */
  299. new_gpio = old_gpio |
  300. (unsigned int) kcontrol->private_value;
  301. } else {
  302. /* mute */
  303. if (kcontrol->private_value == GPIO_MUTE_CONTROL) {
  304. /* 1 = signal off */
  305. new_gpio = old_gpio | GPIO_MUTE_CONTROL;
  306. /* smuting DAC */
  307. snd_akm4xxx_write(ice->akm, 0, 0x01, 0x03);
  308. } else
  309. /* 0 = signal off */
  310. new_gpio = old_gpio &
  311. ~((unsigned int) kcontrol->private_value);
  312. }
  313. /* printk(KERN_DEBUG
  314. "JULI - mute/unmute: control_value: 0x%x, old_gpio: 0x%x, "
  315. "new_gpio 0x%x\n",
  316. (unsigned int)ucontrol->value.integer.value[0], old_gpio,
  317. new_gpio); */
  318. if (old_gpio != new_gpio) {
  319. ice->gpio.set_data(ice, new_gpio);
  320. return 1;
  321. }
  322. /* no change */
  323. return 0;
  324. }
  325. static struct snd_kcontrol_new juli_mute_controls[] __devinitdata = {
  326. {
  327. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  328. .name = "Master Playback Switch",
  329. .info = juli_mute_info,
  330. .get = juli_mute_get,
  331. .put = juli_mute_put,
  332. .private_value = GPIO_MUTE_CONTROL,
  333. },
  334. /* Although the following functionality respects the succint NDA'd
  335. * documentation from the card manufacturer, and the same way of
  336. * operation is coded in OSS Juli driver, only Digital Out monitor
  337. * seems to work. Surprisingly, Analog input monitor outputs Digital
  338. * output data. The two are independent, as enabling both doubles
  339. * volume of the monitor sound.
  340. *
  341. * Checking traces on the board suggests the functionality described
  342. * by the manufacturer is correct - I2S from ADC and AK4114
  343. * go to ICE as well as to Xilinx, I2S inputs of DAC2,3,4 (the monitor
  344. * inputs) are fed from Xilinx.
  345. *
  346. * I even checked traces on board and coded a support in driver for
  347. * an alternative possiblity - the unused I2S ICE output channels
  348. * switched to HW-IN/SPDIF-IN and providing the monitoring signal to
  349. * the DAC - to no avail. The I2S outputs seem to be unconnected.
  350. *
  351. * The windows driver supports the monitoring correctly.
  352. */
  353. {
  354. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  355. .name = "Monitor Analog In Switch",
  356. .info = juli_mute_info,
  357. .get = juli_mute_get,
  358. .put = juli_mute_put,
  359. .private_value = GPIO_ANAIN_MONITOR,
  360. },
  361. {
  362. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  363. .name = "Monitor Digital Out Switch",
  364. .info = juli_mute_info,
  365. .get = juli_mute_get,
  366. .put = juli_mute_put,
  367. .private_value = GPIO_DIGOUT_MONITOR,
  368. },
  369. {
  370. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  371. .name = "Monitor Digital In Switch",
  372. .info = juli_mute_info,
  373. .get = juli_mute_get,
  374. .put = juli_mute_put,
  375. .private_value = GPIO_DIGIN_MONITOR,
  376. },
  377. };
  378. static void ak4358_proc_regs_read(struct snd_info_entry *entry,
  379. struct snd_info_buffer *buffer)
  380. {
  381. struct snd_ice1712 *ice = (struct snd_ice1712 *)entry->private_data;
  382. int reg, val;
  383. for (reg = 0; reg <= 0xf; reg++) {
  384. val = snd_akm4xxx_get(ice->akm, 0, reg);
  385. snd_iprintf(buffer, "0x%02x = 0x%02x\n", reg, val);
  386. }
  387. }
  388. static void ak4358_proc_init(struct snd_ice1712 *ice)
  389. {
  390. struct snd_info_entry *entry;
  391. if (!snd_card_proc_new(ice->card, "ak4358_codec", &entry))
  392. snd_info_set_text_ops(entry, ice, ak4358_proc_regs_read);
  393. }
  394. static char *slave_vols[] __devinitdata = {
  395. PCM_VOLUME,
  396. MONITOR_AN_IN_VOLUME,
  397. MONITOR_DIG_IN_VOLUME,
  398. MONITOR_DIG_OUT_VOLUME,
  399. NULL
  400. };
  401. static __devinitdata
  402. DECLARE_TLV_DB_SCALE(juli_master_db_scale, -6350, 50, 1);
  403. static struct snd_kcontrol __devinit *ctl_find(struct snd_card *card,
  404. const char *name)
  405. {
  406. struct snd_ctl_elem_id sid;
  407. memset(&sid, 0, sizeof(sid));
  408. /* FIXME: strcpy is bad. */
  409. strcpy(sid.name, name);
  410. sid.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  411. return snd_ctl_find_id(card, &sid);
  412. }
  413. static void __devinit add_slaves(struct snd_card *card,
  414. struct snd_kcontrol *master, char **list)
  415. {
  416. for (; *list; list++) {
  417. struct snd_kcontrol *slave = ctl_find(card, *list);
  418. /* printk(KERN_DEBUG "add_slaves - %s\n", *list); */
  419. if (slave) {
  420. /* printk(KERN_DEBUG "slave %s found\n", *list); */
  421. snd_ctl_add_slave(master, slave);
  422. }
  423. }
  424. }
  425. static int __devinit juli_add_controls(struct snd_ice1712 *ice)
  426. {
  427. struct juli_spec *spec = ice->spec;
  428. int err;
  429. unsigned int i;
  430. struct snd_kcontrol *vmaster;
  431. err = snd_ice1712_akm4xxx_build_controls(ice);
  432. if (err < 0)
  433. return err;
  434. for (i = 0; i < ARRAY_SIZE(juli_mute_controls); i++) {
  435. err = snd_ctl_add(ice->card,
  436. snd_ctl_new1(&juli_mute_controls[i], ice));
  437. if (err < 0)
  438. return err;
  439. }
  440. /* Create virtual master control */
  441. vmaster = snd_ctl_make_virtual_master("Master Playback Volume",
  442. juli_master_db_scale);
  443. if (!vmaster)
  444. return -ENOMEM;
  445. add_slaves(ice->card, vmaster, slave_vols);
  446. err = snd_ctl_add(ice->card, vmaster);
  447. if (err < 0)
  448. return err;
  449. /* only capture SPDIF over AK4114 */
  450. err = snd_ak4114_build(spec->ak4114, NULL,
  451. ice->pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream);
  452. ak4358_proc_init(ice);
  453. if (err < 0)
  454. return err;
  455. return 0;
  456. }
  457. /*
  458. * initialize the chip
  459. */
  460. static inline int juli_is_spdif_master(struct snd_ice1712 *ice)
  461. {
  462. return (ice->gpio.get_data(ice) & GPIO_INTERNAL_CLOCK) ? 0 : 1;
  463. }
  464. static unsigned int juli_get_rate(struct snd_ice1712 *ice)
  465. {
  466. int i;
  467. unsigned char result;
  468. result = ice->gpio.get_data(ice) & GPIO_RATE_MASK;
  469. for (i = 0; i < ARRAY_SIZE(gpio_vals); i++)
  470. if (gpio_vals[i] == result)
  471. return juli_rates[i];
  472. return 0;
  473. }
  474. /* setting new rate */
  475. static void juli_set_rate(struct snd_ice1712 *ice, unsigned int rate)
  476. {
  477. unsigned int old, new;
  478. unsigned char val;
  479. old = ice->gpio.get_data(ice);
  480. new = (old & ~GPIO_RATE_MASK) | get_gpio_val(rate);
  481. /* printk(KERN_DEBUG "JULI - set_rate: old %x, new %x\n",
  482. old & GPIO_RATE_MASK,
  483. new & GPIO_RATE_MASK); */
  484. ice->gpio.set_data(ice, new);
  485. /* switching to external clock - supplied by external circuits */
  486. val = inb(ICEMT1724(ice, RATE));
  487. outb(val | VT1724_SPDIF_MASTER, ICEMT1724(ice, RATE));
  488. }
  489. static inline unsigned char juli_set_mclk(struct snd_ice1712 *ice,
  490. unsigned int rate)
  491. {
  492. /* no change in master clock */
  493. return 0;
  494. }
  495. /* setting clock to external - SPDIF */
  496. static void juli_set_spdif_clock(struct snd_ice1712 *ice)
  497. {
  498. unsigned int old;
  499. old = ice->gpio.get_data(ice);
  500. /* external clock (= 0), multiply 1x, 48kHz */
  501. ice->gpio.set_data(ice, (old & ~GPIO_RATE_MASK) | GPIO_MULTI_1X |
  502. GPIO_FREQ_48KHZ);
  503. }
  504. /* Called when ak4114 detects change in the input SPDIF stream */
  505. static void juli_ak4114_change(struct ak4114 *ak4114, unsigned char c0,
  506. unsigned char c1)
  507. {
  508. struct snd_ice1712 *ice = ak4114->change_callback_private;
  509. int rate;
  510. if (ice->is_spdif_master(ice) && c1) {
  511. /* only for SPDIF master mode, rate was changed */
  512. rate = snd_ak4114_external_rate(ak4114);
  513. /* printk(KERN_DEBUG "ak4114 - input rate changed to %d\n",
  514. rate); */
  515. juli_akm_set_rate_val(ice->akm, rate);
  516. }
  517. }
  518. static int __devinit juli_init(struct snd_ice1712 *ice)
  519. {
  520. static const unsigned char ak4114_init_vals[] = {
  521. /* AK4117_REG_PWRDN */ AK4114_RST | AK4114_PWN |
  522. AK4114_OCKS0 | AK4114_OCKS1,
  523. /* AK4114_REQ_FORMAT */ AK4114_DIF_I24I2S,
  524. /* AK4114_REG_IO0 */ AK4114_TX1E,
  525. /* AK4114_REG_IO1 */ AK4114_EFH_1024 | AK4114_DIT |
  526. AK4114_IPS(1),
  527. /* AK4114_REG_INT0_MASK */ 0,
  528. /* AK4114_REG_INT1_MASK */ 0
  529. };
  530. static const unsigned char ak4114_init_txcsb[] = {
  531. 0x41, 0x02, 0x2c, 0x00, 0x00
  532. };
  533. int err;
  534. struct juli_spec *spec;
  535. struct snd_akm4xxx *ak;
  536. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  537. if (!spec)
  538. return -ENOMEM;
  539. ice->spec = spec;
  540. err = snd_ak4114_create(ice->card,
  541. juli_ak4114_read,
  542. juli_ak4114_write,
  543. ak4114_init_vals, ak4114_init_txcsb,
  544. ice, &spec->ak4114);
  545. if (err < 0)
  546. return err;
  547. /* callback for codecs rate setting */
  548. spec->ak4114->change_callback = juli_ak4114_change;
  549. spec->ak4114->change_callback_private = ice;
  550. /* AK4114 in Juli can detect external rate correctly */
  551. spec->ak4114->check_flags = 0;
  552. #if 0
  553. /*
  554. * it seems that the analog doughter board detection does not work reliably, so
  555. * force the analog flag; it should be very rare (if ever) to come at Juli@
  556. * used without the analog daughter board
  557. */
  558. spec->analog = (ice->gpio.get_data(ice) & GPIO_ANALOG_PRESENT) ? 0 : 1;
  559. #else
  560. spec->analog = 1;
  561. #endif
  562. if (spec->analog) {
  563. printk(KERN_INFO "juli@: analog I/O detected\n");
  564. ice->num_total_dacs = 2;
  565. ice->num_total_adcs = 2;
  566. ice->akm = kzalloc(sizeof(struct snd_akm4xxx), GFP_KERNEL);
  567. ak = ice->akm;
  568. if (!ak)
  569. return -ENOMEM;
  570. ice->akm_codecs = 1;
  571. err = snd_ice1712_akm4xxx_init(ak, &akm_juli_dac, NULL, ice);
  572. if (err < 0)
  573. return err;
  574. }
  575. /* juli is clocked by Xilinx array */
  576. ice->hw_rates = &juli_rates_info;
  577. ice->is_spdif_master = juli_is_spdif_master;
  578. ice->get_rate = juli_get_rate;
  579. ice->set_rate = juli_set_rate;
  580. ice->set_mclk = juli_set_mclk;
  581. ice->set_spdif_clock = juli_set_spdif_clock;
  582. ice->spdif.ops.open = juli_spdif_in_open;
  583. return 0;
  584. }
  585. /*
  586. * Juli@ boards don't provide the EEPROM data except for the vendor IDs.
  587. * hence the driver needs to sets up it properly.
  588. */
  589. static unsigned char juli_eeprom[] __devinitdata = {
  590. [ICE_EEP2_SYSCONF] = 0x2b, /* clock 512, mpu401, 1xADC, 1xDACs,
  591. SPDIF in */
  592. [ICE_EEP2_ACLINK] = 0x80, /* I2S */
  593. [ICE_EEP2_I2S] = 0xf8, /* vol, 96k, 24bit, 192k */
  594. [ICE_EEP2_SPDIF] = 0xc3, /* out-en, out-int, spdif-in */
  595. [ICE_EEP2_GPIO_DIR] = 0x9f, /* 5, 6:inputs; 7, 4-0 outputs*/
  596. [ICE_EEP2_GPIO_DIR1] = 0xff,
  597. [ICE_EEP2_GPIO_DIR2] = 0x7f,
  598. [ICE_EEP2_GPIO_MASK] = 0x60, /* 5, 6: locked; 7, 4-0 writable */
  599. [ICE_EEP2_GPIO_MASK1] = 0x00, /* 0-7 writable */
  600. [ICE_EEP2_GPIO_MASK2] = 0x7f,
  601. [ICE_EEP2_GPIO_STATE] = GPIO_FREQ_48KHZ | GPIO_MULTI_1X |
  602. GPIO_INTERNAL_CLOCK, /* internal clock, multiple 1x, 48kHz*/
  603. [ICE_EEP2_GPIO_STATE1] = 0x00, /* unmuted */
  604. [ICE_EEP2_GPIO_STATE2] = 0x00,
  605. };
  606. /* entry point */
  607. struct snd_ice1712_card_info snd_vt1724_juli_cards[] __devinitdata = {
  608. {
  609. .subvendor = VT1724_SUBDEVICE_JULI,
  610. .name = "ESI Juli@",
  611. .model = "juli",
  612. .chip_init = juli_init,
  613. .build_controls = juli_add_controls,
  614. .eeprom_size = sizeof(juli_eeprom),
  615. .eeprom_data = juli_eeprom,
  616. },
  617. { } /* terminator */
  618. };