juli.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  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("JULI - mute/unmute: control_value: 0x%x, old_gpio: 0x%x, \
  314. new_gpio 0x%x\n",
  315. (unsigned int)ucontrol->value.integer.value[0], old_gpio,
  316. new_gpio); */
  317. if (old_gpio != new_gpio) {
  318. ice->gpio.set_data(ice, new_gpio);
  319. return 1;
  320. }
  321. /* no change */
  322. return 0;
  323. }
  324. static struct snd_kcontrol_new juli_mute_controls[] __devinitdata = {
  325. {
  326. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  327. .name = "Master Playback Switch",
  328. .info = juli_mute_info,
  329. .get = juli_mute_get,
  330. .put = juli_mute_put,
  331. .private_value = GPIO_MUTE_CONTROL,
  332. },
  333. /* Although the following functionality respects the succint NDA'd
  334. * documentation from the card manufacturer, and the same way of
  335. * operation is coded in OSS Juli driver, only Digital Out monitor
  336. * seems to work. Surprisingly, Analog input monitor outputs Digital
  337. * output data. The two are independent, as enabling both doubles
  338. * volume of the monitor sound.
  339. *
  340. * Checking traces on the board suggests the functionality described
  341. * by the manufacturer is correct - I2S from ADC and AK4114
  342. * go to ICE as well as to Xilinx, I2S inputs of DAC2,3,4 (the monitor
  343. * inputs) are fed from Xilinx.
  344. *
  345. * I even checked traces on board and coded a support in driver for
  346. * an alternative possiblity - the unused I2S ICE output channels
  347. * switched to HW-IN/SPDIF-IN and providing the monitoring signal to
  348. * the DAC - to no avail. The I2S outputs seem to be unconnected.
  349. *
  350. * The windows driver supports the monitoring correctly.
  351. */
  352. {
  353. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  354. .name = "Monitor Analog In Switch",
  355. .info = juli_mute_info,
  356. .get = juli_mute_get,
  357. .put = juli_mute_put,
  358. .private_value = GPIO_ANAIN_MONITOR,
  359. },
  360. {
  361. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  362. .name = "Monitor Digital Out Switch",
  363. .info = juli_mute_info,
  364. .get = juli_mute_get,
  365. .put = juli_mute_put,
  366. .private_value = GPIO_DIGOUT_MONITOR,
  367. },
  368. {
  369. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  370. .name = "Monitor Digital In Switch",
  371. .info = juli_mute_info,
  372. .get = juli_mute_get,
  373. .put = juli_mute_put,
  374. .private_value = GPIO_DIGIN_MONITOR,
  375. },
  376. };
  377. static void ak4358_proc_regs_read(struct snd_info_entry *entry,
  378. struct snd_info_buffer *buffer)
  379. {
  380. struct snd_ice1712 *ice = (struct snd_ice1712 *)entry->private_data;
  381. int reg, val;
  382. for (reg = 0; reg <= 0xf; reg++) {
  383. val = snd_akm4xxx_get(ice->akm, 0, reg);
  384. snd_iprintf(buffer, "0x%02x = 0x%02x\n", reg, val);
  385. }
  386. }
  387. static void ak4358_proc_init(struct snd_ice1712 *ice)
  388. {
  389. struct snd_info_entry *entry;
  390. if (!snd_card_proc_new(ice->card, "ak4358_codec", &entry))
  391. snd_info_set_text_ops(entry, ice, ak4358_proc_regs_read);
  392. }
  393. static char *slave_vols[] __devinitdata = {
  394. PCM_VOLUME,
  395. MONITOR_AN_IN_VOLUME,
  396. MONITOR_DIG_IN_VOLUME,
  397. MONITOR_DIG_OUT_VOLUME,
  398. NULL
  399. };
  400. static __devinitdata
  401. DECLARE_TLV_DB_SCALE(juli_master_db_scale, -6350, 50, 1);
  402. static struct snd_kcontrol __devinit *ctl_find(struct snd_card *card,
  403. const char *name)
  404. {
  405. struct snd_ctl_elem_id sid;
  406. memset(&sid, 0, sizeof(sid));
  407. /* FIXME: strcpy is bad. */
  408. strcpy(sid.name, name);
  409. sid.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  410. return snd_ctl_find_id(card, &sid);
  411. }
  412. static void __devinit add_slaves(struct snd_card *card,
  413. struct snd_kcontrol *master, char **list)
  414. {
  415. for (; *list; list++) {
  416. struct snd_kcontrol *slave = ctl_find(card, *list);
  417. /* printk(KERN_DEBUG "add_slaves - %s\n", *list); */
  418. if (slave) {
  419. /* printk(KERN_DEBUG "slave %s found\n", *list); */
  420. snd_ctl_add_slave(master, slave);
  421. }
  422. }
  423. }
  424. static int __devinit juli_add_controls(struct snd_ice1712 *ice)
  425. {
  426. struct juli_spec *spec = ice->spec;
  427. int err;
  428. unsigned int i;
  429. struct snd_kcontrol *vmaster;
  430. err = snd_ice1712_akm4xxx_build_controls(ice);
  431. if (err < 0)
  432. return err;
  433. for (i = 0; i < ARRAY_SIZE(juli_mute_controls); i++) {
  434. err = snd_ctl_add(ice->card,
  435. snd_ctl_new1(&juli_mute_controls[i], ice));
  436. if (err < 0)
  437. return err;
  438. }
  439. /* Create virtual master control */
  440. vmaster = snd_ctl_make_virtual_master("Master Playback Volume",
  441. juli_master_db_scale);
  442. if (!vmaster)
  443. return -ENOMEM;
  444. add_slaves(ice->card, vmaster, slave_vols);
  445. err = snd_ctl_add(ice->card, vmaster);
  446. if (err < 0)
  447. return err;
  448. /* only capture SPDIF over AK4114 */
  449. err = snd_ak4114_build(spec->ak4114, NULL,
  450. ice->pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream);
  451. ak4358_proc_init(ice);
  452. if (err < 0)
  453. return err;
  454. return 0;
  455. }
  456. /*
  457. * initialize the chip
  458. */
  459. static inline int juli_is_spdif_master(struct snd_ice1712 *ice)
  460. {
  461. return (ice->gpio.get_data(ice) & GPIO_INTERNAL_CLOCK) ? 0 : 1;
  462. }
  463. static unsigned int juli_get_rate(struct snd_ice1712 *ice)
  464. {
  465. int i;
  466. unsigned char result;
  467. result = ice->gpio.get_data(ice) & GPIO_RATE_MASK;
  468. for (i = 0; i < ARRAY_SIZE(gpio_vals); i++)
  469. if (gpio_vals[i] == result)
  470. return juli_rates[i];
  471. return 0;
  472. }
  473. /* setting new rate */
  474. static void juli_set_rate(struct snd_ice1712 *ice, unsigned int rate)
  475. {
  476. unsigned int old, new;
  477. unsigned char val;
  478. old = ice->gpio.get_data(ice);
  479. new = (old & ~GPIO_RATE_MASK) | get_gpio_val(rate);
  480. /* printk(KERN_DEBUG "JULI - set_rate: old %x, new %x\n",
  481. old & GPIO_RATE_MASK,
  482. new & GPIO_RATE_MASK); */
  483. ice->gpio.set_data(ice, new);
  484. /* switching to external clock - supplied by external circuits */
  485. val = inb(ICEMT1724(ice, RATE));
  486. outb(val | VT1724_SPDIF_MASTER, ICEMT1724(ice, RATE));
  487. }
  488. static inline unsigned char juli_set_mclk(struct snd_ice1712 *ice,
  489. unsigned int rate)
  490. {
  491. /* no change in master clock */
  492. return 0;
  493. }
  494. /* setting clock to external - SPDIF */
  495. static void juli_set_spdif_clock(struct snd_ice1712 *ice)
  496. {
  497. unsigned int old;
  498. old = ice->gpio.get_data(ice);
  499. /* external clock (= 0), multiply 1x, 48kHz */
  500. ice->gpio.set_data(ice, (old & ~GPIO_RATE_MASK) | GPIO_MULTI_1X |
  501. GPIO_FREQ_48KHZ);
  502. }
  503. /* Called when ak4114 detects change in the input SPDIF stream */
  504. static void juli_ak4114_change(struct ak4114 *ak4114, unsigned char c0,
  505. unsigned char c1)
  506. {
  507. struct snd_ice1712 *ice = ak4114->change_callback_private;
  508. int rate;
  509. if (ice->is_spdif_master(ice) && c1) {
  510. /* only for SPDIF master mode, rate was changed */
  511. rate = snd_ak4114_external_rate(ak4114);
  512. /* printk(KERN_DEBUG "ak4114 - input rate changed to %d\n",
  513. rate); */
  514. juli_akm_set_rate_val(ice->akm, rate);
  515. }
  516. }
  517. static int __devinit juli_init(struct snd_ice1712 *ice)
  518. {
  519. static const unsigned char ak4114_init_vals[] = {
  520. /* AK4117_REG_PWRDN */ AK4114_RST | AK4114_PWN |
  521. AK4114_OCKS0 | AK4114_OCKS1,
  522. /* AK4114_REQ_FORMAT */ AK4114_DIF_I24I2S,
  523. /* AK4114_REG_IO0 */ AK4114_TX1E,
  524. /* AK4114_REG_IO1 */ AK4114_EFH_1024 | AK4114_DIT |
  525. AK4114_IPS(1),
  526. /* AK4114_REG_INT0_MASK */ 0,
  527. /* AK4114_REG_INT1_MASK */ 0
  528. };
  529. static const unsigned char ak4114_init_txcsb[] = {
  530. 0x41, 0x02, 0x2c, 0x00, 0x00
  531. };
  532. int err;
  533. struct juli_spec *spec;
  534. struct snd_akm4xxx *ak;
  535. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  536. if (!spec)
  537. return -ENOMEM;
  538. ice->spec = spec;
  539. err = snd_ak4114_create(ice->card,
  540. juli_ak4114_read,
  541. juli_ak4114_write,
  542. ak4114_init_vals, ak4114_init_txcsb,
  543. ice, &spec->ak4114);
  544. if (err < 0)
  545. return err;
  546. /* callback for codecs rate setting */
  547. spec->ak4114->change_callback = juli_ak4114_change;
  548. spec->ak4114->change_callback_private = ice;
  549. /* AK4114 in Juli can detect external rate correctly */
  550. spec->ak4114->check_flags = 0;
  551. #if 0
  552. /*
  553. * it seems that the analog doughter board detection does not work reliably, so
  554. * force the analog flag; it should be very rare (if ever) to come at Juli@
  555. * used without the analog daughter board
  556. */
  557. spec->analog = (ice->gpio.get_data(ice) & GPIO_ANALOG_PRESENT) ? 0 : 1;
  558. #else
  559. spec->analog = 1;
  560. #endif
  561. if (spec->analog) {
  562. printk(KERN_INFO "juli@: analog I/O detected\n");
  563. ice->num_total_dacs = 2;
  564. ice->num_total_adcs = 2;
  565. ice->akm = kzalloc(sizeof(struct snd_akm4xxx), GFP_KERNEL);
  566. ak = ice->akm;
  567. if (!ak)
  568. return -ENOMEM;
  569. ice->akm_codecs = 1;
  570. err = snd_ice1712_akm4xxx_init(ak, &akm_juli_dac, NULL, ice);
  571. if (err < 0)
  572. return err;
  573. }
  574. /* juli is clocked by Xilinx array */
  575. ice->hw_rates = &juli_rates_info;
  576. ice->is_spdif_master = juli_is_spdif_master;
  577. ice->get_rate = juli_get_rate;
  578. ice->set_rate = juli_set_rate;
  579. ice->set_mclk = juli_set_mclk;
  580. ice->set_spdif_clock = juli_set_spdif_clock;
  581. ice->spdif.ops.open = juli_spdif_in_open;
  582. return 0;
  583. }
  584. /*
  585. * Juli@ boards don't provide the EEPROM data except for the vendor IDs.
  586. * hence the driver needs to sets up it properly.
  587. */
  588. static unsigned char juli_eeprom[] __devinitdata = {
  589. [ICE_EEP2_SYSCONF] = 0x2b, /* clock 512, mpu401, 1xADC, 1xDACs,
  590. SPDIF in */
  591. [ICE_EEP2_ACLINK] = 0x80, /* I2S */
  592. [ICE_EEP2_I2S] = 0xf8, /* vol, 96k, 24bit, 192k */
  593. [ICE_EEP2_SPDIF] = 0xc3, /* out-en, out-int, spdif-in */
  594. [ICE_EEP2_GPIO_DIR] = 0x9f, /* 5, 6:inputs; 7, 4-0 outputs*/
  595. [ICE_EEP2_GPIO_DIR1] = 0xff,
  596. [ICE_EEP2_GPIO_DIR2] = 0x7f,
  597. [ICE_EEP2_GPIO_MASK] = 0x60, /* 5, 6: locked; 7, 4-0 writable */
  598. [ICE_EEP2_GPIO_MASK1] = 0x00, /* 0-7 writable */
  599. [ICE_EEP2_GPIO_MASK2] = 0x7f,
  600. [ICE_EEP2_GPIO_STATE] = GPIO_FREQ_48KHZ | GPIO_MULTI_1X |
  601. GPIO_INTERNAL_CLOCK, /* internal clock, multiple 1x, 48kHz*/
  602. [ICE_EEP2_GPIO_STATE1] = 0x00, /* unmuted */
  603. [ICE_EEP2_GPIO_STATE2] = 0x00,
  604. };
  605. /* entry point */
  606. struct snd_ice1712_card_info snd_vt1724_juli_cards[] __devinitdata = {
  607. {
  608. .subvendor = VT1724_SUBDEVICE_JULI,
  609. .name = "ESI Juli@",
  610. .model = "juli",
  611. .chip_init = juli_init,
  612. .build_controls = juli_add_controls,
  613. .eeprom_size = sizeof(juli_eeprom),
  614. .eeprom_data = juli_eeprom,
  615. },
  616. { } /* terminator */
  617. };