juli.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  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, unsigned char val)
  151. {
  152. snd_vt1724_write_i2c((struct snd_ice1712 *)private_data, AK4114_ADDR, reg, val);
  153. }
  154. static unsigned char juli_ak4114_read(void *private_data, unsigned char reg)
  155. {
  156. return snd_vt1724_read_i2c((struct snd_ice1712 *)private_data, AK4114_ADDR, reg);
  157. }
  158. /*
  159. * If SPDIF capture and slaved to SPDIF-IN, setting runtime rate
  160. * to the external rate
  161. */
  162. static void juli_spdif_in_open(struct snd_ice1712 *ice,
  163. struct snd_pcm_substream *substream)
  164. {
  165. struct juli_spec *spec = ice->spec;
  166. struct snd_pcm_runtime *runtime = substream->runtime;
  167. int rate;
  168. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK ||
  169. !ice->is_spdif_master(ice))
  170. return;
  171. rate = snd_ak4114_external_rate(spec->ak4114);
  172. if (rate >= runtime->hw.rate_min && rate <= runtime->hw.rate_max) {
  173. runtime->hw.rate_min = rate;
  174. runtime->hw.rate_max = rate;
  175. }
  176. }
  177. /*
  178. * AK4358 section
  179. */
  180. static void juli_akm_lock(struct snd_akm4xxx *ak, int chip)
  181. {
  182. }
  183. static void juli_akm_unlock(struct snd_akm4xxx *ak, int chip)
  184. {
  185. }
  186. static void juli_akm_write(struct snd_akm4xxx *ak, int chip,
  187. unsigned char addr, unsigned char data)
  188. {
  189. struct snd_ice1712 *ice = ak->private_data[0];
  190. snd_assert(chip == 0, return);
  191. snd_vt1724_write_i2c(ice, AK4358_ADDR, addr, data);
  192. }
  193. /*
  194. * change the rate of envy24HT, AK4358, AK5385
  195. */
  196. static void juli_akm_set_rate_val(struct snd_akm4xxx *ak, unsigned int rate)
  197. {
  198. unsigned char old, tmp, ak4358_dfs;
  199. unsigned int ak5385_pins, old_gpio, new_gpio;
  200. struct snd_ice1712 *ice = ak->private_data[0];
  201. struct juli_spec *spec = ice->spec;
  202. if (rate == 0) /* no hint - S/PDIF input is master or the new spdif
  203. input rate undetected, simply return */
  204. return;
  205. /* adjust DFS on codecs */
  206. if (rate > 96000) {
  207. ak4358_dfs = 2;
  208. ak5385_pins = GPIO_AK5385A_DFS1 | GPIO_AK5385A_CKS0;
  209. } else if (rate > 48000) {
  210. ak4358_dfs = 1;
  211. ak5385_pins = GPIO_AK5385A_DFS0;
  212. } else {
  213. ak4358_dfs = 0;
  214. ak5385_pins = 0;
  215. }
  216. /* AK5385 first, since it requires cold reset affecting both codecs */
  217. old_gpio = ice->gpio.get_data(ice);
  218. new_gpio = (old_gpio & ~GPIO_AK5385A_MASK) | ak5385_pins;
  219. /* printk(KERN_DEBUG "JULI - ak5385 set_rate_val: new gpio 0x%x\n",
  220. new_gpio); */
  221. ice->gpio.set_data(ice, new_gpio);
  222. /* cold reset */
  223. old = inb(ICEMT1724(ice, AC97_CMD));
  224. outb(old | VT1724_AC97_COLD, ICEMT1724(ice, AC97_CMD));
  225. udelay(1);
  226. outb(old & ~VT1724_AC97_COLD, ICEMT1724(ice, AC97_CMD));
  227. /* AK4358 */
  228. /* set new value, reset DFS */
  229. tmp = snd_akm4xxx_get(ak, 0, 2);
  230. snd_akm4xxx_reset(ak, 1);
  231. tmp = snd_akm4xxx_get(ak, 0, 2);
  232. tmp &= ~(0x03 << 4);
  233. tmp |= ak4358_dfs << 4;
  234. snd_akm4xxx_set(ak, 0, 2, tmp);
  235. snd_akm4xxx_reset(ak, 0);
  236. /* reinit ak4114 */
  237. snd_ak4114_reinit(spec->ak4114);
  238. }
  239. #define AK_DAC(xname, xch) { .name = xname, .num_channels = xch }
  240. #define PCM_VOLUME "PCM Playback Volume"
  241. #define MONITOR_AN_IN_VOLUME "Monitor Analog In Volume"
  242. #define MONITOR_DIG_IN_VOLUME "Monitor Digital In Volume"
  243. #define MONITOR_DIG_OUT_VOLUME "Monitor Digital Out Volume"
  244. static const struct snd_akm4xxx_dac_channel juli_dac[] = {
  245. AK_DAC(PCM_VOLUME, 2),
  246. AK_DAC(MONITOR_AN_IN_VOLUME, 2),
  247. AK_DAC(MONITOR_DIG_OUT_VOLUME, 2),
  248. AK_DAC(MONITOR_DIG_IN_VOLUME, 2),
  249. };
  250. static struct snd_akm4xxx akm_juli_dac __devinitdata = {
  251. .type = SND_AK4358,
  252. .num_dacs = 8, /* DAC1 - analog out
  253. DAC2 - analog in monitor
  254. DAC3 - digital out monitor
  255. DAC4 - digital in monitor
  256. */
  257. .ops = {
  258. .lock = juli_akm_lock,
  259. .unlock = juli_akm_unlock,
  260. .write = juli_akm_write,
  261. .set_rate_val = juli_akm_set_rate_val
  262. },
  263. .dac_info = juli_dac,
  264. };
  265. #define juli_mute_info snd_ctl_boolean_mono_info
  266. static int juli_mute_get(struct snd_kcontrol *kcontrol,
  267. struct snd_ctl_elem_value *ucontrol)
  268. {
  269. struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
  270. unsigned int val;
  271. val = ice->gpio.get_data(ice) & (unsigned int) kcontrol->private_value;
  272. if (kcontrol->private_value == GPIO_MUTE_CONTROL)
  273. /* val 0 = signal on */
  274. ucontrol->value.integer.value[0] = (val) ? 0 : 1;
  275. else
  276. /* val 1 = signal on */
  277. ucontrol->value.integer.value[0] = (val) ? 1 : 0;
  278. return 0;
  279. }
  280. static int juli_mute_put(struct snd_kcontrol *kcontrol,
  281. struct snd_ctl_elem_value *ucontrol)
  282. {
  283. struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
  284. unsigned int old_gpio, new_gpio;
  285. old_gpio = ice->gpio.get_data(ice);
  286. if (ucontrol->value.integer.value[0]) {
  287. /* unmute */
  288. if (kcontrol->private_value == GPIO_MUTE_CONTROL) {
  289. /* 0 = signal on */
  290. new_gpio = old_gpio & ~GPIO_MUTE_CONTROL;
  291. /* un-smuting DAC */
  292. snd_akm4xxx_write(ice->akm, 0, 0x01, 0x01);
  293. } else
  294. /* 1 = signal on */
  295. new_gpio = old_gpio |
  296. (unsigned int) kcontrol->private_value;
  297. } else {
  298. /* mute */
  299. if (kcontrol->private_value == GPIO_MUTE_CONTROL) {
  300. /* 1 = signal off */
  301. new_gpio = old_gpio | GPIO_MUTE_CONTROL;
  302. /* smuting DAC */
  303. snd_akm4xxx_write(ice->akm, 0, 0x01, 0x03);
  304. } else
  305. /* 0 = signal off */
  306. new_gpio = old_gpio &
  307. ~((unsigned int) kcontrol->private_value);
  308. }
  309. /* printk("JULI - mute/unmute: control_value: 0x%x, old_gpio: 0x%x, \
  310. new_gpio 0x%x\n",
  311. (unsigned int)ucontrol->value.integer.value[0], old_gpio,
  312. new_gpio); */
  313. if (old_gpio != new_gpio) {
  314. ice->gpio.set_data(ice, new_gpio);
  315. return 1;
  316. }
  317. /* no change */
  318. return 0;
  319. }
  320. static struct snd_kcontrol_new juli_mute_controls[] __devinitdata = {
  321. {
  322. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  323. .name = "Master Playback Switch",
  324. .info = juli_mute_info,
  325. .get = juli_mute_get,
  326. .put = juli_mute_put,
  327. .private_value = GPIO_MUTE_CONTROL,
  328. },
  329. /* Although the following functionality respects the succint NDA'd
  330. * documentation from the card manufacturer, and the same way of
  331. * operation is coded in OSS Juli driver, only Digital Out monitor
  332. * seems to work. Surprisingly, Analog input monitor outputs Digital
  333. * output data. The two are independent, as enabling both doubles
  334. * volume of the monitor sound.
  335. *
  336. * Checking traces on the board suggests the functionality described
  337. * by the manufacturer is correct - I2S from ADC and AK4114
  338. * go to ICE as well as to Xilinx, I2S inputs of DAC2,3,4 (the monitor
  339. * inputs) are fed from Xilinx.
  340. *
  341. * I even checked traces on board and coded a support in driver for
  342. * an alternative possiblity - the unused I2S ICE output channels
  343. * switched to HW-IN/SPDIF-IN and providing the monitoring signal to
  344. * the DAC - to no avail. The I2S outputs seem to be unconnected.
  345. *
  346. * The windows driver supports the monitoring correctly.
  347. */
  348. {
  349. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  350. .name = "Monitor Analog In Switch",
  351. .info = juli_mute_info,
  352. .get = juli_mute_get,
  353. .put = juli_mute_put,
  354. .private_value = GPIO_ANAIN_MONITOR,
  355. },
  356. {
  357. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  358. .name = "Monitor Digital Out Switch",
  359. .info = juli_mute_info,
  360. .get = juli_mute_get,
  361. .put = juli_mute_put,
  362. .private_value = GPIO_DIGOUT_MONITOR,
  363. },
  364. {
  365. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  366. .name = "Monitor Digital In Switch",
  367. .info = juli_mute_info,
  368. .get = juli_mute_get,
  369. .put = juli_mute_put,
  370. .private_value = GPIO_DIGIN_MONITOR,
  371. },
  372. };
  373. static void ak4358_proc_regs_read(struct snd_info_entry *entry,
  374. struct snd_info_buffer *buffer)
  375. {
  376. struct snd_ice1712 *ice = (struct snd_ice1712 *)entry->private_data;
  377. int reg, val;
  378. for (reg = 0; reg <= 0xf; reg++) {
  379. val = snd_akm4xxx_get(ice->akm, 0, reg);
  380. snd_iprintf(buffer, "0x%02x = 0x%02x\n", reg, val);
  381. }
  382. }
  383. static void ak4358_proc_init(struct snd_ice1712 *ice)
  384. {
  385. struct snd_info_entry *entry;
  386. if (!snd_card_proc_new(ice->card, "ak4358_codec", &entry))
  387. snd_info_set_text_ops(entry, ice, ak4358_proc_regs_read);
  388. }
  389. static char *slave_vols[] __devinitdata = {
  390. PCM_VOLUME,
  391. MONITOR_AN_IN_VOLUME,
  392. MONITOR_DIG_IN_VOLUME,
  393. MONITOR_DIG_OUT_VOLUME,
  394. NULL
  395. };
  396. static __devinitdata
  397. DECLARE_TLV_DB_SCALE(juli_master_db_scale, -6350, 50, 1);
  398. static struct snd_kcontrol __devinit *ctl_find(struct snd_card *card,
  399. const char *name)
  400. {
  401. struct snd_ctl_elem_id sid;
  402. memset(&sid, 0, sizeof(sid));
  403. /* FIXME: strcpy is bad. */
  404. strcpy(sid.name, name);
  405. sid.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  406. return snd_ctl_find_id(card, &sid);
  407. }
  408. static void __devinit add_slaves(struct snd_card *card,
  409. struct snd_kcontrol *master, char **list)
  410. {
  411. for (; *list; list++) {
  412. struct snd_kcontrol *slave = ctl_find(card, *list);
  413. /* printk(KERN_DEBUG "add_slaves - %s\n", *list); */
  414. if (slave) {
  415. /* printk(KERN_DEBUG "slave %s found\n", *list); */
  416. snd_ctl_add_slave(master, slave);
  417. }
  418. }
  419. }
  420. static int __devinit juli_add_controls(struct snd_ice1712 *ice)
  421. {
  422. struct juli_spec *spec = ice->spec;
  423. int err;
  424. unsigned int i;
  425. struct snd_kcontrol *vmaster;
  426. err = snd_ice1712_akm4xxx_build_controls(ice);
  427. if (err < 0)
  428. return err;
  429. for (i = 0; i < ARRAY_SIZE(juli_mute_controls); i++) {
  430. err = snd_ctl_add(ice->card,
  431. snd_ctl_new1(&juli_mute_controls[i], ice));
  432. if (err < 0)
  433. return err;
  434. }
  435. /* Create virtual master control */
  436. vmaster = snd_ctl_make_virtual_master("Master Playback Volume",
  437. juli_master_db_scale);
  438. if (!vmaster)
  439. return -ENOMEM;
  440. add_slaves(ice->card, vmaster, slave_vols);
  441. err = snd_ctl_add(ice->card, vmaster);
  442. if (err < 0)
  443. return err;
  444. /* only capture SPDIF over AK4114 */
  445. err = snd_ak4114_build(spec->ak4114, NULL,
  446. ice->pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream);
  447. ak4358_proc_init(ice);
  448. if (err < 0)
  449. return err;
  450. return 0;
  451. }
  452. /*
  453. * initialize the chip
  454. */
  455. static inline int juli_is_spdif_master(struct snd_ice1712 *ice)
  456. {
  457. return (ice->gpio.get_data(ice) & GPIO_INTERNAL_CLOCK) ? 0 : 1;
  458. }
  459. static unsigned int juli_get_rate(struct snd_ice1712 *ice)
  460. {
  461. int i;
  462. unsigned char result;
  463. result = ice->gpio.get_data(ice) & GPIO_RATE_MASK;
  464. for (i = 0; i < ARRAY_SIZE(gpio_vals); i++)
  465. if (gpio_vals[i] == result)
  466. return juli_rates[i];
  467. return 0;
  468. }
  469. /* setting new rate */
  470. static void juli_set_rate(struct snd_ice1712 *ice, unsigned int rate)
  471. {
  472. unsigned int old, new;
  473. unsigned char val;
  474. old = ice->gpio.get_data(ice);
  475. new = (old & ~GPIO_RATE_MASK) | get_gpio_val(rate);
  476. /* printk(KERN_DEBUG "JULI - set_rate: old %x, new %x\n",
  477. old & GPIO_RATE_MASK,
  478. new & GPIO_RATE_MASK); */
  479. ice->gpio.set_data(ice, new);
  480. /* switching to external clock - supplied by external circuits */
  481. val = inb(ICEMT1724(ice, RATE));
  482. outb(val | VT1724_SPDIF_MASTER, ICEMT1724(ice, RATE));
  483. }
  484. static inline unsigned char juli_set_mclk(struct snd_ice1712 *ice,
  485. unsigned int rate)
  486. {
  487. /* no change in master clock */
  488. return 0;
  489. }
  490. /* setting clock to external - SPDIF */
  491. static void juli_set_spdif_clock(struct snd_ice1712 *ice)
  492. {
  493. unsigned int old;
  494. old = ice->gpio.get_data(ice);
  495. /* external clock (= 0), multiply 1x, 48kHz */
  496. ice->gpio.set_data(ice, (old & ~GPIO_RATE_MASK) | GPIO_MULTI_1X |
  497. GPIO_FREQ_48KHZ);
  498. }
  499. /* Called when ak4114 detects change in the input SPDIF stream */
  500. static void juli_ak4114_change(struct ak4114 *ak4114, unsigned char c0,
  501. unsigned char c1)
  502. {
  503. struct snd_ice1712 *ice = ak4114->change_callback_private;
  504. int rate;
  505. if (ice->is_spdif_master(ice) && c1) {
  506. /* only for SPDIF master mode, rate was changed */
  507. rate = snd_ak4114_external_rate(ak4114);
  508. /* printk(KERN_DEBUG "ak4114 - input rate changed to %d\n",
  509. rate); */
  510. juli_akm_set_rate_val(ice->akm, rate);
  511. }
  512. }
  513. static int __devinit juli_init(struct snd_ice1712 *ice)
  514. {
  515. static const unsigned char ak4114_init_vals[] = {
  516. /* AK4117_REG_PWRDN */ AK4114_RST | AK4114_PWN | AK4114_OCKS0 | AK4114_OCKS1,
  517. /* AK4114_REQ_FORMAT */ AK4114_DIF_I24I2S,
  518. /* AK4114_REG_IO0 */ AK4114_TX1E,
  519. /* AK4114_REG_IO1 */ AK4114_EFH_1024 | AK4114_DIT | AK4114_IPS(1),
  520. /* AK4114_REG_INT0_MASK */ 0,
  521. /* AK4114_REG_INT1_MASK */ 0
  522. };
  523. static const unsigned char ak4114_init_txcsb[] = {
  524. 0x41, 0x02, 0x2c, 0x00, 0x00
  525. };
  526. int err;
  527. struct juli_spec *spec;
  528. struct snd_akm4xxx *ak;
  529. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  530. if (!spec)
  531. return -ENOMEM;
  532. ice->spec = spec;
  533. err = snd_ak4114_create(ice->card,
  534. juli_ak4114_read,
  535. juli_ak4114_write,
  536. ak4114_init_vals, ak4114_init_txcsb,
  537. ice, &spec->ak4114);
  538. if (err < 0)
  539. return err;
  540. /* callback for codecs rate setting */
  541. spec->ak4114->change_callback = juli_ak4114_change;
  542. spec->ak4114->change_callback_private = ice;
  543. /* AK4114 in Juli can detect external rate correctly */
  544. spec->ak4114->check_flags = 0;
  545. #if 0
  546. /* it seems that the analog doughter board detection does not work
  547. reliably, so force the analog flag; it should be very rare
  548. to use Juli@ without the analog doughter board */
  549. spec->analog = (ice->gpio.get_data(ice) & GPIO_ANALOG_PRESENT) ? 0 : 1;
  550. #else
  551. spec->analog = 1;
  552. #endif
  553. if (spec->analog) {
  554. printk(KERN_INFO "juli@: analog I/O detected\n");
  555. ice->num_total_dacs = 2;
  556. ice->num_total_adcs = 2;
  557. ak = ice->akm = kzalloc(sizeof(struct snd_akm4xxx), GFP_KERNEL);
  558. if (! ak)
  559. return -ENOMEM;
  560. ice->akm_codecs = 1;
  561. if ((err = snd_ice1712_akm4xxx_init(ak, &akm_juli_dac, NULL, ice)) < 0)
  562. return err;
  563. }
  564. /* juli is clocked by Xilinx array */
  565. ice->hw_rates = &juli_rates_info;
  566. ice->is_spdif_master = juli_is_spdif_master;
  567. ice->get_rate = juli_get_rate;
  568. ice->set_rate = juli_set_rate;
  569. ice->set_mclk = juli_set_mclk;
  570. ice->set_spdif_clock = juli_set_spdif_clock;
  571. ice->spdif.ops.open = juli_spdif_in_open;
  572. return 0;
  573. }
  574. /*
  575. * Juli@ boards don't provide the EEPROM data except for the vendor IDs.
  576. * hence the driver needs to sets up it properly.
  577. */
  578. static unsigned char juli_eeprom[] __devinitdata = {
  579. [ICE_EEP2_SYSCONF] = 0x2b, /* clock 512, mpu401, 1xADC, 1xDACs,
  580. SPDIF in */
  581. [ICE_EEP2_ACLINK] = 0x80, /* I2S */
  582. [ICE_EEP2_I2S] = 0xf8, /* vol, 96k, 24bit, 192k */
  583. [ICE_EEP2_SPDIF] = 0xc3, /* out-en, out-int, spdif-in */
  584. [ICE_EEP2_GPIO_DIR] = 0x9f, /* 5, 6:inputs; 7, 4-0 outputs*/
  585. [ICE_EEP2_GPIO_DIR1] = 0xff,
  586. [ICE_EEP2_GPIO_DIR2] = 0x7f,
  587. [ICE_EEP2_GPIO_MASK] = 0x60, /* 5, 6: locked; 7, 4-0 writable */
  588. [ICE_EEP2_GPIO_MASK1] = 0x00, /* 0-7 writable */
  589. [ICE_EEP2_GPIO_MASK2] = 0x7f,
  590. [ICE_EEP2_GPIO_STATE] = GPIO_FREQ_48KHZ | GPIO_MULTI_1X |
  591. GPIO_INTERNAL_CLOCK, /* internal clock, multiple 1x, 48kHz*/
  592. [ICE_EEP2_GPIO_STATE1] = 0x00, /* unmuted */
  593. [ICE_EEP2_GPIO_STATE2] = 0x00,
  594. };
  595. /* entry point */
  596. struct snd_ice1712_card_info snd_vt1724_juli_cards[] __devinitdata = {
  597. {
  598. .subvendor = VT1724_SUBDEVICE_JULI,
  599. .name = "ESI Juli@",
  600. .model = "juli",
  601. .chip_init = juli_init,
  602. .build_controls = juli_add_controls,
  603. .eeprom_size = sizeof(juli_eeprom),
  604. .eeprom_data = juli_eeprom,
  605. },
  606. { } /* terminator */
  607. };