juli.c 20 KB

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