juli.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  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 <linux/delay.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/init.h>
  28. #include <linux/slab.h>
  29. #include <linux/string.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 const unsigned int juli_rates[] = {
  128. 16000, 22050, 24000, 32000,
  129. 44100, 48000, 64000, 88200,
  130. 96000, 176400, 192000,
  131. };
  132. static const 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 const 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. /* dev_dbg(ice->card->dev, "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 const struct snd_akm4xxx akm_juli_dac = {
  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. /* dev_dbg(ice->card->dev,
  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[] = {
  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 possibility - 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 char *slave_vols[] = {
  379. PCM_VOLUME,
  380. MONITOR_AN_IN_VOLUME,
  381. MONITOR_DIG_IN_VOLUME,
  382. MONITOR_DIG_OUT_VOLUME,
  383. NULL
  384. };
  385. static
  386. DECLARE_TLV_DB_SCALE(juli_master_db_scale, -6350, 50, 1);
  387. static struct snd_kcontrol *ctl_find(struct snd_card *card,
  388. const char *name)
  389. {
  390. struct snd_ctl_elem_id sid = {0};
  391. strlcpy(sid.name, name, sizeof(sid.name));
  392. sid.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  393. return snd_ctl_find_id(card, &sid);
  394. }
  395. static void add_slaves(struct snd_card *card,
  396. struct snd_kcontrol *master,
  397. char * const *list)
  398. {
  399. for (; *list; list++) {
  400. struct snd_kcontrol *slave = ctl_find(card, *list);
  401. /* dev_dbg(card->dev, "add_slaves - %s\n", *list); */
  402. if (slave) {
  403. /* dev_dbg(card->dev, "slave %s found\n", *list); */
  404. snd_ctl_add_slave(master, slave);
  405. }
  406. }
  407. }
  408. static int juli_add_controls(struct snd_ice1712 *ice)
  409. {
  410. struct juli_spec *spec = ice->spec;
  411. int err;
  412. unsigned int i;
  413. struct snd_kcontrol *vmaster;
  414. err = snd_ice1712_akm4xxx_build_controls(ice);
  415. if (err < 0)
  416. return err;
  417. for (i = 0; i < ARRAY_SIZE(juli_mute_controls); i++) {
  418. err = snd_ctl_add(ice->card,
  419. snd_ctl_new1(&juli_mute_controls[i], ice));
  420. if (err < 0)
  421. return err;
  422. }
  423. /* Create virtual master control */
  424. vmaster = snd_ctl_make_virtual_master("Master Playback Volume",
  425. juli_master_db_scale);
  426. if (!vmaster)
  427. return -ENOMEM;
  428. add_slaves(ice->card, vmaster, slave_vols);
  429. err = snd_ctl_add(ice->card, vmaster);
  430. if (err < 0)
  431. return err;
  432. /* only capture SPDIF over AK4114 */
  433. return snd_ak4114_build(spec->ak4114, NULL,
  434. ice->pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream);
  435. }
  436. /*
  437. * suspend/resume
  438. * */
  439. #ifdef CONFIG_PM_SLEEP
  440. static int juli_resume(struct snd_ice1712 *ice)
  441. {
  442. struct snd_akm4xxx *ak = ice->akm;
  443. struct juli_spec *spec = ice->spec;
  444. /* akm4358 un-reset, un-mute */
  445. snd_akm4xxx_reset(ak, 0);
  446. /* reinit ak4114 */
  447. snd_ak4114_resume(spec->ak4114);
  448. return 0;
  449. }
  450. static int juli_suspend(struct snd_ice1712 *ice)
  451. {
  452. struct snd_akm4xxx *ak = ice->akm;
  453. struct juli_spec *spec = ice->spec;
  454. /* akm4358 reset and soft-mute */
  455. snd_akm4xxx_reset(ak, 1);
  456. snd_ak4114_suspend(spec->ak4114);
  457. return 0;
  458. }
  459. #endif
  460. /*
  461. * initialize the chip
  462. */
  463. static inline int juli_is_spdif_master(struct snd_ice1712 *ice)
  464. {
  465. return (ice->gpio.get_data(ice) & GPIO_INTERNAL_CLOCK) ? 0 : 1;
  466. }
  467. static unsigned int juli_get_rate(struct snd_ice1712 *ice)
  468. {
  469. int i;
  470. unsigned char result;
  471. result = ice->gpio.get_data(ice) & GPIO_RATE_MASK;
  472. for (i = 0; i < ARRAY_SIZE(gpio_vals); i++)
  473. if (gpio_vals[i] == result)
  474. return juli_rates[i];
  475. return 0;
  476. }
  477. /* setting new rate */
  478. static void juli_set_rate(struct snd_ice1712 *ice, unsigned int rate)
  479. {
  480. unsigned int old, new;
  481. unsigned char val;
  482. old = ice->gpio.get_data(ice);
  483. new = (old & ~GPIO_RATE_MASK) | get_gpio_val(rate);
  484. /* dev_dbg(ice->card->dev, "JULI - set_rate: old %x, new %x\n",
  485. old & GPIO_RATE_MASK,
  486. new & GPIO_RATE_MASK); */
  487. ice->gpio.set_data(ice, new);
  488. /* switching to external clock - supplied by external circuits */
  489. val = inb(ICEMT1724(ice, RATE));
  490. outb(val | VT1724_SPDIF_MASTER, ICEMT1724(ice, RATE));
  491. }
  492. static inline unsigned char juli_set_mclk(struct snd_ice1712 *ice,
  493. unsigned int rate)
  494. {
  495. /* no change in master clock */
  496. return 0;
  497. }
  498. /* setting clock to external - SPDIF */
  499. static int juli_set_spdif_clock(struct snd_ice1712 *ice, int type)
  500. {
  501. unsigned int old;
  502. old = ice->gpio.get_data(ice);
  503. /* external clock (= 0), multiply 1x, 48kHz */
  504. ice->gpio.set_data(ice, (old & ~GPIO_RATE_MASK) | GPIO_MULTI_1X |
  505. GPIO_FREQ_48KHZ);
  506. return 0;
  507. }
  508. /* Called when ak4114 detects change in the input SPDIF stream */
  509. static void juli_ak4114_change(struct ak4114 *ak4114, unsigned char c0,
  510. unsigned char c1)
  511. {
  512. struct snd_ice1712 *ice = ak4114->change_callback_private;
  513. int rate;
  514. if (ice->is_spdif_master(ice) && c1) {
  515. /* only for SPDIF master mode, rate was changed */
  516. rate = snd_ak4114_external_rate(ak4114);
  517. /* dev_dbg(ice->card->dev, "ak4114 - input rate changed to %d\n",
  518. rate); */
  519. juli_akm_set_rate_val(ice->akm, rate);
  520. }
  521. }
  522. static int juli_init(struct snd_ice1712 *ice)
  523. {
  524. static const unsigned char ak4114_init_vals[] = {
  525. /* AK4117_REG_PWRDN */ AK4114_RST | AK4114_PWN |
  526. AK4114_OCKS0 | AK4114_OCKS1,
  527. /* AK4114_REQ_FORMAT */ AK4114_DIF_I24I2S,
  528. /* AK4114_REG_IO0 */ AK4114_TX1E,
  529. /* AK4114_REG_IO1 */ AK4114_EFH_1024 | AK4114_DIT |
  530. AK4114_IPS(1),
  531. /* AK4114_REG_INT0_MASK */ 0,
  532. /* AK4114_REG_INT1_MASK */ 0
  533. };
  534. static const unsigned char ak4114_init_txcsb[] = {
  535. 0x41, 0x02, 0x2c, 0x00, 0x00
  536. };
  537. int err;
  538. struct juli_spec *spec;
  539. struct snd_akm4xxx *ak;
  540. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  541. if (!spec)
  542. return -ENOMEM;
  543. ice->spec = spec;
  544. err = snd_ak4114_create(ice->card,
  545. juli_ak4114_read,
  546. juli_ak4114_write,
  547. ak4114_init_vals, ak4114_init_txcsb,
  548. ice, &spec->ak4114);
  549. if (err < 0)
  550. return err;
  551. /* callback for codecs rate setting */
  552. spec->ak4114->change_callback = juli_ak4114_change;
  553. spec->ak4114->change_callback_private = ice;
  554. /* AK4114 in Juli can detect external rate correctly */
  555. spec->ak4114->check_flags = 0;
  556. #if 0
  557. /*
  558. * it seems that the analog doughter board detection does not work reliably, so
  559. * force the analog flag; it should be very rare (if ever) to come at Juli@
  560. * used without the analog daughter board
  561. */
  562. spec->analog = (ice->gpio.get_data(ice) & GPIO_ANALOG_PRESENT) ? 0 : 1;
  563. #else
  564. spec->analog = 1;
  565. #endif
  566. if (spec->analog) {
  567. dev_info(ice->card->dev, "juli@: analog I/O detected\n");
  568. ice->num_total_dacs = 2;
  569. ice->num_total_adcs = 2;
  570. ice->akm = kzalloc(sizeof(struct snd_akm4xxx), GFP_KERNEL);
  571. ak = ice->akm;
  572. if (!ak)
  573. return -ENOMEM;
  574. ice->akm_codecs = 1;
  575. err = snd_ice1712_akm4xxx_init(ak, &akm_juli_dac, NULL, ice);
  576. if (err < 0)
  577. return err;
  578. }
  579. /* juli is clocked by Xilinx array */
  580. ice->hw_rates = &juli_rates_info;
  581. ice->is_spdif_master = juli_is_spdif_master;
  582. ice->get_rate = juli_get_rate;
  583. ice->set_rate = juli_set_rate;
  584. ice->set_mclk = juli_set_mclk;
  585. ice->set_spdif_clock = juli_set_spdif_clock;
  586. ice->spdif.ops.open = juli_spdif_in_open;
  587. #ifdef CONFIG_PM_SLEEP
  588. ice->pm_resume = juli_resume;
  589. ice->pm_suspend = juli_suspend;
  590. ice->pm_suspend_enabled = 1;
  591. #endif
  592. return 0;
  593. }
  594. /*
  595. * Juli@ boards don't provide the EEPROM data except for the vendor IDs.
  596. * hence the driver needs to sets up it properly.
  597. */
  598. static unsigned char juli_eeprom[] = {
  599. [ICE_EEP2_SYSCONF] = 0x2b, /* clock 512, mpu401, 1xADC, 1xDACs,
  600. SPDIF in */
  601. [ICE_EEP2_ACLINK] = 0x80, /* I2S */
  602. [ICE_EEP2_I2S] = 0xf8, /* vol, 96k, 24bit, 192k */
  603. [ICE_EEP2_SPDIF] = 0xc3, /* out-en, out-int, spdif-in */
  604. [ICE_EEP2_GPIO_DIR] = 0x9f, /* 5, 6:inputs; 7, 4-0 outputs*/
  605. [ICE_EEP2_GPIO_DIR1] = 0xff,
  606. [ICE_EEP2_GPIO_DIR2] = 0x7f,
  607. [ICE_EEP2_GPIO_MASK] = 0x60, /* 5, 6: locked; 7, 4-0 writable */
  608. [ICE_EEP2_GPIO_MASK1] = 0x00, /* 0-7 writable */
  609. [ICE_EEP2_GPIO_MASK2] = 0x7f,
  610. [ICE_EEP2_GPIO_STATE] = GPIO_FREQ_48KHZ | GPIO_MULTI_1X |
  611. GPIO_INTERNAL_CLOCK, /* internal clock, multiple 1x, 48kHz*/
  612. [ICE_EEP2_GPIO_STATE1] = 0x00, /* unmuted */
  613. [ICE_EEP2_GPIO_STATE2] = 0x00,
  614. };
  615. /* entry point */
  616. struct snd_ice1712_card_info snd_vt1724_juli_cards[] = {
  617. {
  618. .subvendor = VT1724_SUBDEVICE_JULI,
  619. .name = "ESI Juli@",
  620. .model = "juli",
  621. .chip_init = juli_init,
  622. .build_controls = juli_add_controls,
  623. .eeprom_size = sizeof(juli_eeprom),
  624. .eeprom_data = juli_eeprom,
  625. },
  626. { } /* terminator */
  627. };