blob: c51659b9caf6cfd5fa44a47337c664b729246194 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ALSA driver for ICEnsemble VT1724 (Envy24HT)
3 *
4 * Lowlevel functions for ESI Juli@ cards
5 *
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02006 * Copyright (c) 2004 Jaroslav Kysela <perex@perex.cz>
Pavel Hofmand16be8e2008-03-20 12:10:27 +01007 * 2008 Pavel Hofman <dustin@seznam.cz>
8 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
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 *
Vedran Mileticcc67b7f2008-09-07 12:00:02 +020024 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <asm/io.h>
27#include <linux/delay.h>
28#include <linux/interrupt.h>
29#include <linux/init.h>
30#include <linux/slab.h>
31#include <sound/core.h>
Pavel Hofmand16be8e2008-03-20 12:10:27 +010032#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34#include "ice1712.h"
35#include "envy24ht.h"
36#include "juli.h"
Vedran Mileticcc67b7f2008-09-07 12:00:02 +020037
Takashi Iwai7cda8ba2008-01-18 13:36:07 +010038struct juli_spec {
39 struct ak4114 *ak4114;
Vedran Mileticcc67b7f2008-09-07 12:00:02 +020040 unsigned int analog:1;
Takashi Iwai7cda8ba2008-01-18 13:36:07 +010041};
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/*
44 * chip addresses on I2C bus
45 */
46#define AK4114_ADDR 0x20 /* S/PDIF receiver */
47#define AK4358_ADDR 0x22 /* DAC */
48
49/*
Pavel Hofmand16be8e2008-03-20 12:10:27 +010050 * Juli does not use the standard ICE1724 clock scheme. Juli's ice1724 chip is
51 * supplied by external clock provided by Xilinx array and MK73-1 PLL frequency
52 * multiplier. Actual frequency is set by ice1724 GPIOs hooked to the Xilinx.
53 *
54 * The clock circuitry is supplied by the two ice1724 crystals. This
55 * arrangement allows to generate independent clock signal for AK4114's input
56 * rate detection circuit. As a result, Juli, unlike most other
57 * ice1724+ak4114-based cards, detects spdif input rate correctly.
58 * This fact is applied in the driver, allowing to modify PCM stream rate
59 * parameter according to the actual input rate.
60 *
61 * Juli uses the remaining three stereo-channels of its DAC to optionally
62 * monitor analog input, digital input, and digital output. The corresponding
63 * I2S signals are routed by Xilinx, controlled by GPIOs.
64 *
65 * The master mute is implemented using output muting transistors (GPIO) in
66 * combination with smuting the DAC.
67 *
68 * The card itself has no HW master volume control, implemented using the
69 * vmaster control.
70 *
71 * TODO:
72 * researching and fixing the input monitors
73 */
74
75/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 * GPIO pins
77 */
78#define GPIO_FREQ_MASK (3<<0)
79#define GPIO_FREQ_32KHZ (0<<0)
80#define GPIO_FREQ_44KHZ (1<<0)
81#define GPIO_FREQ_48KHZ (2<<0)
82#define GPIO_MULTI_MASK (3<<2)
83#define GPIO_MULTI_4X (0<<2)
84#define GPIO_MULTI_2X (1<<2)
85#define GPIO_MULTI_1X (2<<2) /* also external */
86#define GPIO_MULTI_HALF (3<<2)
Pavel Hofmand16be8e2008-03-20 12:10:27 +010087#define GPIO_INTERNAL_CLOCK (1<<4) /* 0 = external, 1 = internal */
88#define GPIO_CLOCK_MASK (1<<4)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#define GPIO_ANALOG_PRESENT (1<<5) /* RO only: 0 = present */
90#define GPIO_RXMCLK_SEL (1<<7) /* must be 0 */
91#define GPIO_AK5385A_CKS0 (1<<8)
Pavel Hofmand16be8e2008-03-20 12:10:27 +010092#define GPIO_AK5385A_DFS1 (1<<9)
93#define GPIO_AK5385A_DFS0 (1<<10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094#define GPIO_DIGOUT_MONITOR (1<<11) /* 1 = active */
95#define GPIO_DIGIN_MONITOR (1<<12) /* 1 = active */
96#define GPIO_ANAIN_MONITOR (1<<13) /* 1 = active */
Pavel Hofmand16be8e2008-03-20 12:10:27 +010097#define GPIO_AK5385A_CKS1 (1<<14) /* must be 0 */
98#define GPIO_MUTE_CONTROL (1<<15) /* output mute, 1 = muted */
99
100#define GPIO_RATE_MASK (GPIO_FREQ_MASK | GPIO_MULTI_MASK | \
101 GPIO_CLOCK_MASK)
102#define GPIO_AK5385A_MASK (GPIO_AK5385A_CKS0 | GPIO_AK5385A_DFS0 | \
103 GPIO_AK5385A_DFS1 | GPIO_AK5385A_CKS1)
104
105#define JULI_PCM_RATE (SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 | \
106 SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
107 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_64000 | \
108 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | \
109 SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000)
110
111#define GPIO_RATE_16000 (GPIO_FREQ_32KHZ | GPIO_MULTI_HALF | \
112 GPIO_INTERNAL_CLOCK)
113#define GPIO_RATE_22050 (GPIO_FREQ_44KHZ | GPIO_MULTI_HALF | \
114 GPIO_INTERNAL_CLOCK)
115#define GPIO_RATE_24000 (GPIO_FREQ_48KHZ | GPIO_MULTI_HALF | \
116 GPIO_INTERNAL_CLOCK)
117#define GPIO_RATE_32000 (GPIO_FREQ_32KHZ | GPIO_MULTI_1X | \
118 GPIO_INTERNAL_CLOCK)
119#define GPIO_RATE_44100 (GPIO_FREQ_44KHZ | GPIO_MULTI_1X | \
120 GPIO_INTERNAL_CLOCK)
121#define GPIO_RATE_48000 (GPIO_FREQ_48KHZ | GPIO_MULTI_1X | \
122 GPIO_INTERNAL_CLOCK)
123#define GPIO_RATE_64000 (GPIO_FREQ_32KHZ | GPIO_MULTI_2X | \
124 GPIO_INTERNAL_CLOCK)
125#define GPIO_RATE_88200 (GPIO_FREQ_44KHZ | GPIO_MULTI_2X | \
126 GPIO_INTERNAL_CLOCK)
127#define GPIO_RATE_96000 (GPIO_FREQ_48KHZ | GPIO_MULTI_2X | \
128 GPIO_INTERNAL_CLOCK)
129#define GPIO_RATE_176400 (GPIO_FREQ_44KHZ | GPIO_MULTI_4X | \
130 GPIO_INTERNAL_CLOCK)
131#define GPIO_RATE_192000 (GPIO_FREQ_48KHZ | GPIO_MULTI_4X | \
132 GPIO_INTERNAL_CLOCK)
133
134/*
135 * Initial setup of the conversion array GPIO <-> rate
136 */
137static unsigned int juli_rates[] = {
138 16000, 22050, 24000, 32000,
139 44100, 48000, 64000, 88200,
140 96000, 176400, 192000,
141};
142
143static unsigned int gpio_vals[] = {
144 GPIO_RATE_16000, GPIO_RATE_22050, GPIO_RATE_24000, GPIO_RATE_32000,
145 GPIO_RATE_44100, GPIO_RATE_48000, GPIO_RATE_64000, GPIO_RATE_88200,
146 GPIO_RATE_96000, GPIO_RATE_176400, GPIO_RATE_192000,
147};
148
149static struct snd_pcm_hw_constraint_list juli_rates_info = {
150 .count = ARRAY_SIZE(juli_rates),
151 .list = juli_rates,
152 .mask = 0,
153};
154
155static int get_gpio_val(int rate)
156{
157 int i;
158 for (i = 0; i < ARRAY_SIZE(juli_rates); i++)
159 if (juli_rates[i] == rate)
160 return gpio_vals[i];
161 return 0;
162}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Vedran Mileticcc67b7f2008-09-07 12:00:02 +0200164static void juli_ak4114_write(void *private_data, unsigned char reg,
165 unsigned char val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
Vedran Mileticcc67b7f2008-09-07 12:00:02 +0200167 snd_vt1724_write_i2c((struct snd_ice1712 *)private_data, AK4114_ADDR,
168 reg, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
Vedran Mileticcc67b7f2008-09-07 12:00:02 +0200170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171static unsigned char juli_ak4114_read(void *private_data, unsigned char reg)
172{
Vedran Mileticcc67b7f2008-09-07 12:00:02 +0200173 return snd_vt1724_read_i2c((struct snd_ice1712 *)private_data,
174 AK4114_ADDR, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100177/*
178 * If SPDIF capture and slaved to SPDIF-IN, setting runtime rate
179 * to the external rate
180 */
Takashi Iwaic93f5a12008-03-14 17:17:09 +0100181static void juli_spdif_in_open(struct snd_ice1712 *ice,
Vedran Mileticcc67b7f2008-09-07 12:00:02 +0200182 struct snd_pcm_substream *substream)
Takashi Iwaic93f5a12008-03-14 17:17:09 +0100183{
184 struct juli_spec *spec = ice->spec;
185 struct snd_pcm_runtime *runtime = substream->runtime;
186 int rate;
187
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100188 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK ||
189 !ice->is_spdif_master(ice))
Takashi Iwaic93f5a12008-03-14 17:17:09 +0100190 return;
191 rate = snd_ak4114_external_rate(spec->ak4114);
192 if (rate >= runtime->hw.rate_min && rate <= runtime->hw.rate_max) {
193 runtime->hw.rate_min = rate;
194 runtime->hw.rate_max = rate;
195 }
196}
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198/*
199 * AK4358 section
200 */
201
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100202static void juli_akm_lock(struct snd_akm4xxx *ak, int chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
204}
205
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100206static void juli_akm_unlock(struct snd_akm4xxx *ak, int chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
208}
209
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100210static void juli_akm_write(struct snd_akm4xxx *ak, int chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 unsigned char addr, unsigned char data)
212{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100213 struct snd_ice1712 *ice = ak->private_data[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Takashi Iwaida3cec32008-08-08 17:12:14 +0200215 if (snd_BUG_ON(chip))
216 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 snd_vt1724_write_i2c(ice, AK4358_ADDR, addr, data);
218}
219
220/*
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100221 * change the rate of envy24HT, AK4358, AK5385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100223static void juli_akm_set_rate_val(struct snd_akm4xxx *ak, unsigned int rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100225 unsigned char old, tmp, ak4358_dfs;
226 unsigned int ak5385_pins, old_gpio, new_gpio;
227 struct snd_ice1712 *ice = ak->private_data[0];
228 struct juli_spec *spec = ice->spec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100230 if (rate == 0) /* no hint - S/PDIF input is master or the new spdif
231 input rate undetected, simply return */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 return;
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 /* adjust DFS on codecs */
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100235 if (rate > 96000) {
236 ak4358_dfs = 2;
237 ak5385_pins = GPIO_AK5385A_DFS1 | GPIO_AK5385A_CKS0;
238 } else if (rate > 48000) {
239 ak4358_dfs = 1;
240 ak5385_pins = GPIO_AK5385A_DFS0;
241 } else {
242 ak4358_dfs = 0;
243 ak5385_pins = 0;
244 }
245 /* AK5385 first, since it requires cold reset affecting both codecs */
246 old_gpio = ice->gpio.get_data(ice);
247 new_gpio = (old_gpio & ~GPIO_AK5385A_MASK) | ak5385_pins;
248 /* printk(KERN_DEBUG "JULI - ak5385 set_rate_val: new gpio 0x%x\n",
249 new_gpio); */
250 ice->gpio.set_data(ice, new_gpio);
251
252 /* cold reset */
253 old = inb(ICEMT1724(ice, AC97_CMD));
254 outb(old | VT1724_AC97_COLD, ICEMT1724(ice, AC97_CMD));
255 udelay(1);
256 outb(old & ~VT1724_AC97_COLD, ICEMT1724(ice, AC97_CMD));
257
258 /* AK4358 */
259 /* set new value, reset DFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 tmp = snd_akm4xxx_get(ak, 0, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 snd_akm4xxx_reset(ak, 1);
262 tmp = snd_akm4xxx_get(ak, 0, 2);
263 tmp &= ~(0x03 << 4);
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100264 tmp |= ak4358_dfs << 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 snd_akm4xxx_set(ak, 0, 2, tmp);
266 snd_akm4xxx_reset(ak, 0);
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100267
268 /* reinit ak4114 */
269 snd_ak4114_reinit(spec->ak4114);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
271
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100272#define AK_DAC(xname, xch) { .name = xname, .num_channels = xch }
273#define PCM_VOLUME "PCM Playback Volume"
274#define MONITOR_AN_IN_VOLUME "Monitor Analog In Volume"
275#define MONITOR_DIG_IN_VOLUME "Monitor Digital In Volume"
276#define MONITOR_DIG_OUT_VOLUME "Monitor Digital Out Volume"
277
278static const struct snd_akm4xxx_dac_channel juli_dac[] = {
279 AK_DAC(PCM_VOLUME, 2),
280 AK_DAC(MONITOR_AN_IN_VOLUME, 2),
281 AK_DAC(MONITOR_DIG_OUT_VOLUME, 2),
282 AK_DAC(MONITOR_DIG_IN_VOLUME, 2),
283};
284
285
Takashi Iwai1b60f6b2007-03-13 22:13:47 +0100286static struct snd_akm4xxx akm_juli_dac __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 .type = SND_AK4358,
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100288 .num_dacs = 8, /* DAC1 - analog out
289 DAC2 - analog in monitor
290 DAC3 - digital out monitor
291 DAC4 - digital in monitor
292 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 .ops = {
294 .lock = juli_akm_lock,
295 .unlock = juli_akm_unlock,
296 .write = juli_akm_write,
297 .set_rate_val = juli_akm_set_rate_val
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100298 },
299 .dac_info = juli_dac,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300};
301
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100302#define juli_mute_info snd_ctl_boolean_mono_info
303
304static int juli_mute_get(struct snd_kcontrol *kcontrol,
305 struct snd_ctl_elem_value *ucontrol)
306{
307 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
308 unsigned int val;
309 val = ice->gpio.get_data(ice) & (unsigned int) kcontrol->private_value;
310 if (kcontrol->private_value == GPIO_MUTE_CONTROL)
311 /* val 0 = signal on */
312 ucontrol->value.integer.value[0] = (val) ? 0 : 1;
313 else
314 /* val 1 = signal on */
315 ucontrol->value.integer.value[0] = (val) ? 1 : 0;
316 return 0;
317}
318
319static int juli_mute_put(struct snd_kcontrol *kcontrol,
320 struct snd_ctl_elem_value *ucontrol)
321{
322 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
323 unsigned int old_gpio, new_gpio;
324 old_gpio = ice->gpio.get_data(ice);
325 if (ucontrol->value.integer.value[0]) {
326 /* unmute */
327 if (kcontrol->private_value == GPIO_MUTE_CONTROL) {
328 /* 0 = signal on */
329 new_gpio = old_gpio & ~GPIO_MUTE_CONTROL;
330 /* un-smuting DAC */
331 snd_akm4xxx_write(ice->akm, 0, 0x01, 0x01);
332 } else
333 /* 1 = signal on */
334 new_gpio = old_gpio |
335 (unsigned int) kcontrol->private_value;
336 } else {
337 /* mute */
338 if (kcontrol->private_value == GPIO_MUTE_CONTROL) {
339 /* 1 = signal off */
340 new_gpio = old_gpio | GPIO_MUTE_CONTROL;
341 /* smuting DAC */
342 snd_akm4xxx_write(ice->akm, 0, 0x01, 0x03);
343 } else
344 /* 0 = signal off */
345 new_gpio = old_gpio &
346 ~((unsigned int) kcontrol->private_value);
347 }
348 /* printk("JULI - mute/unmute: control_value: 0x%x, old_gpio: 0x%x, \
349 new_gpio 0x%x\n",
350 (unsigned int)ucontrol->value.integer.value[0], old_gpio,
351 new_gpio); */
352 if (old_gpio != new_gpio) {
353 ice->gpio.set_data(ice, new_gpio);
354 return 1;
355 }
356 /* no change */
357 return 0;
358}
359
360static struct snd_kcontrol_new juli_mute_controls[] __devinitdata = {
361 {
362 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
363 .name = "Master Playback Switch",
364 .info = juli_mute_info,
365 .get = juli_mute_get,
366 .put = juli_mute_put,
367 .private_value = GPIO_MUTE_CONTROL,
368 },
369 /* Although the following functionality respects the succint NDA'd
370 * documentation from the card manufacturer, and the same way of
371 * operation is coded in OSS Juli driver, only Digital Out monitor
372 * seems to work. Surprisingly, Analog input monitor outputs Digital
373 * output data. The two are independent, as enabling both doubles
374 * volume of the monitor sound.
375 *
376 * Checking traces on the board suggests the functionality described
377 * by the manufacturer is correct - I2S from ADC and AK4114
378 * go to ICE as well as to Xilinx, I2S inputs of DAC2,3,4 (the monitor
379 * inputs) are fed from Xilinx.
380 *
381 * I even checked traces on board and coded a support in driver for
382 * an alternative possiblity - the unused I2S ICE output channels
383 * switched to HW-IN/SPDIF-IN and providing the monitoring signal to
384 * the DAC - to no avail. The I2S outputs seem to be unconnected.
385 *
386 * The windows driver supports the monitoring correctly.
387 */
388 {
389 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
390 .name = "Monitor Analog In Switch",
391 .info = juli_mute_info,
392 .get = juli_mute_get,
393 .put = juli_mute_put,
394 .private_value = GPIO_ANAIN_MONITOR,
395 },
396 {
397 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
398 .name = "Monitor Digital Out Switch",
399 .info = juli_mute_info,
400 .get = juli_mute_get,
401 .put = juli_mute_put,
402 .private_value = GPIO_DIGOUT_MONITOR,
403 },
404 {
405 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
406 .name = "Monitor Digital In Switch",
407 .info = juli_mute_info,
408 .get = juli_mute_get,
409 .put = juli_mute_put,
410 .private_value = GPIO_DIGIN_MONITOR,
411 },
412};
413
414
415static void ak4358_proc_regs_read(struct snd_info_entry *entry,
416 struct snd_info_buffer *buffer)
417{
418 struct snd_ice1712 *ice = (struct snd_ice1712 *)entry->private_data;
419 int reg, val;
420 for (reg = 0; reg <= 0xf; reg++) {
421 val = snd_akm4xxx_get(ice->akm, 0, reg);
422 snd_iprintf(buffer, "0x%02x = 0x%02x\n", reg, val);
423 }
424}
425
426static void ak4358_proc_init(struct snd_ice1712 *ice)
427{
428 struct snd_info_entry *entry;
429 if (!snd_card_proc_new(ice->card, "ak4358_codec", &entry))
430 snd_info_set_text_ops(entry, ice, ak4358_proc_regs_read);
431}
432
433static char *slave_vols[] __devinitdata = {
434 PCM_VOLUME,
435 MONITOR_AN_IN_VOLUME,
436 MONITOR_DIG_IN_VOLUME,
437 MONITOR_DIG_OUT_VOLUME,
438 NULL
439};
440
441static __devinitdata
442DECLARE_TLV_DB_SCALE(juli_master_db_scale, -6350, 50, 1);
443
444static struct snd_kcontrol __devinit *ctl_find(struct snd_card *card,
445 const char *name)
446{
447 struct snd_ctl_elem_id sid;
448 memset(&sid, 0, sizeof(sid));
449 /* FIXME: strcpy is bad. */
450 strcpy(sid.name, name);
451 sid.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
452 return snd_ctl_find_id(card, &sid);
453}
454
455static void __devinit add_slaves(struct snd_card *card,
456 struct snd_kcontrol *master, char **list)
457{
458 for (; *list; list++) {
459 struct snd_kcontrol *slave = ctl_find(card, *list);
460 /* printk(KERN_DEBUG "add_slaves - %s\n", *list); */
461 if (slave) {
462 /* printk(KERN_DEBUG "slave %s found\n", *list); */
463 snd_ctl_add_slave(master, slave);
464 }
465 }
466}
467
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100468static int __devinit juli_add_controls(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
Takashi Iwai7cda8ba2008-01-18 13:36:07 +0100470 struct juli_spec *spec = ice->spec;
Takashi Iwaifdd4bb42007-04-05 17:08:57 +0200471 int err;
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100472 unsigned int i;
473 struct snd_kcontrol *vmaster;
474
Takashi Iwaifdd4bb42007-04-05 17:08:57 +0200475 err = snd_ice1712_akm4xxx_build_controls(ice);
476 if (err < 0)
477 return err;
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100478
479 for (i = 0; i < ARRAY_SIZE(juli_mute_controls); i++) {
480 err = snd_ctl_add(ice->card,
481 snd_ctl_new1(&juli_mute_controls[i], ice));
482 if (err < 0)
483 return err;
484 }
485 /* Create virtual master control */
486 vmaster = snd_ctl_make_virtual_master("Master Playback Volume",
487 juli_master_db_scale);
488 if (!vmaster)
489 return -ENOMEM;
490 add_slaves(ice->card, vmaster, slave_vols);
491 err = snd_ctl_add(ice->card, vmaster);
492 if (err < 0)
493 return err;
494
Takashi Iwaifdd4bb42007-04-05 17:08:57 +0200495 /* only capture SPDIF over AK4114 */
Takashi Iwai7cda8ba2008-01-18 13:36:07 +0100496 err = snd_ak4114_build(spec->ak4114, NULL,
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100497 ice->pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream);
498
499 ak4358_proc_init(ice);
Takashi Iwaifdd4bb42007-04-05 17:08:57 +0200500 if (err < 0)
501 return err;
502 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
504
505/*
506 * initialize the chip
507 */
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100508
509static inline int juli_is_spdif_master(struct snd_ice1712 *ice)
510{
511 return (ice->gpio.get_data(ice) & GPIO_INTERNAL_CLOCK) ? 0 : 1;
512}
513
514static unsigned int juli_get_rate(struct snd_ice1712 *ice)
515{
516 int i;
517 unsigned char result;
518
519 result = ice->gpio.get_data(ice) & GPIO_RATE_MASK;
520 for (i = 0; i < ARRAY_SIZE(gpio_vals); i++)
521 if (gpio_vals[i] == result)
522 return juli_rates[i];
523 return 0;
524}
525
526/* setting new rate */
527static void juli_set_rate(struct snd_ice1712 *ice, unsigned int rate)
528{
529 unsigned int old, new;
530 unsigned char val;
531
532 old = ice->gpio.get_data(ice);
533 new = (old & ~GPIO_RATE_MASK) | get_gpio_val(rate);
534 /* printk(KERN_DEBUG "JULI - set_rate: old %x, new %x\n",
535 old & GPIO_RATE_MASK,
536 new & GPIO_RATE_MASK); */
537
538 ice->gpio.set_data(ice, new);
539 /* switching to external clock - supplied by external circuits */
540 val = inb(ICEMT1724(ice, RATE));
541 outb(val | VT1724_SPDIF_MASTER, ICEMT1724(ice, RATE));
542}
543
544static inline unsigned char juli_set_mclk(struct snd_ice1712 *ice,
545 unsigned int rate)
546{
547 /* no change in master clock */
548 return 0;
549}
550
551/* setting clock to external - SPDIF */
552static void juli_set_spdif_clock(struct snd_ice1712 *ice)
553{
554 unsigned int old;
555 old = ice->gpio.get_data(ice);
556 /* external clock (= 0), multiply 1x, 48kHz */
557 ice->gpio.set_data(ice, (old & ~GPIO_RATE_MASK) | GPIO_MULTI_1X |
558 GPIO_FREQ_48KHZ);
559}
560
561/* Called when ak4114 detects change in the input SPDIF stream */
562static void juli_ak4114_change(struct ak4114 *ak4114, unsigned char c0,
563 unsigned char c1)
564{
565 struct snd_ice1712 *ice = ak4114->change_callback_private;
566 int rate;
567 if (ice->is_spdif_master(ice) && c1) {
568 /* only for SPDIF master mode, rate was changed */
569 rate = snd_ak4114_external_rate(ak4114);
570 /* printk(KERN_DEBUG "ak4114 - input rate changed to %d\n",
571 rate); */
572 juli_akm_set_rate_val(ice->akm, rate);
573 }
574}
575
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100576static int __devinit juli_init(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
Takashi Iwai517400c2007-01-29 15:27:56 +0100578 static const unsigned char ak4114_init_vals[] = {
Vedran Mileticcc67b7f2008-09-07 12:00:02 +0200579 /* AK4117_REG_PWRDN */ AK4114_RST | AK4114_PWN |
580 AK4114_OCKS0 | AK4114_OCKS1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 /* AK4114_REQ_FORMAT */ AK4114_DIF_I24I2S,
582 /* AK4114_REG_IO0 */ AK4114_TX1E,
Vedran Mileticcc67b7f2008-09-07 12:00:02 +0200583 /* AK4114_REG_IO1 */ AK4114_EFH_1024 | AK4114_DIT |
584 AK4114_IPS(1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 /* AK4114_REG_INT0_MASK */ 0,
586 /* AK4114_REG_INT1_MASK */ 0
587 };
Takashi Iwai517400c2007-01-29 15:27:56 +0100588 static const unsigned char ak4114_init_txcsb[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 0x41, 0x02, 0x2c, 0x00, 0x00
590 };
591 int err;
Takashi Iwai7cda8ba2008-01-18 13:36:07 +0100592 struct juli_spec *spec;
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100593 struct snd_akm4xxx *ak;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Takashi Iwai7cda8ba2008-01-18 13:36:07 +0100595 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
596 if (!spec)
597 return -ENOMEM;
598 ice->spec = spec;
599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 err = snd_ak4114_create(ice->card,
601 juli_ak4114_read,
602 juli_ak4114_write,
603 ak4114_init_vals, ak4114_init_txcsb,
Takashi Iwai7cda8ba2008-01-18 13:36:07 +0100604 ice, &spec->ak4114);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 if (err < 0)
606 return err;
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100607 /* callback for codecs rate setting */
608 spec->ak4114->change_callback = juli_ak4114_change;
609 spec->ak4114->change_callback_private = ice;
610 /* AK4114 in Juli can detect external rate correctly */
611 spec->ak4114->check_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Jaroslav Kyselafd6715e2005-11-10 07:51:31 +0100613#if 0
Vedran Mileticcc67b7f2008-09-07 12:00:02 +0200614/*
615 * it seems that the analog doughter board detection does not work reliably, so
616 * force the analog flag; it should be very rare (if ever) to come at Juli@
617 * used without the analog daughter board
618 */
Takashi Iwai7cda8ba2008-01-18 13:36:07 +0100619 spec->analog = (ice->gpio.get_data(ice) & GPIO_ANALOG_PRESENT) ? 0 : 1;
Jaroslav Kyselafd6715e2005-11-10 07:51:31 +0100620#else
Vedran Mileticcc67b7f2008-09-07 12:00:02 +0200621 spec->analog = 1;
Jaroslav Kyselafd6715e2005-11-10 07:51:31 +0100622#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Takashi Iwai7cda8ba2008-01-18 13:36:07 +0100624 if (spec->analog) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 printk(KERN_INFO "juli@: analog I/O detected\n");
626 ice->num_total_dacs = 2;
627 ice->num_total_adcs = 2;
628
Vedran Mileticcc67b7f2008-09-07 12:00:02 +0200629 ice->akm = kzalloc(sizeof(struct snd_akm4xxx), GFP_KERNEL);
630 ak = ice->akm;
631 if (!ak)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 return -ENOMEM;
633 ice->akm_codecs = 1;
Vedran Mileticcc67b7f2008-09-07 12:00:02 +0200634 err = snd_ice1712_akm4xxx_init(ak, &akm_juli_dac, NULL, ice);
635 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 return err;
637 }
Vedran Mileticcc67b7f2008-09-07 12:00:02 +0200638
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100639 /* juli is clocked by Xilinx array */
640 ice->hw_rates = &juli_rates_info;
641 ice->is_spdif_master = juli_is_spdif_master;
642 ice->get_rate = juli_get_rate;
643 ice->set_rate = juli_set_rate;
644 ice->set_mclk = juli_set_mclk;
645 ice->set_spdif_clock = juli_set_spdif_clock;
646
Takashi Iwaic93f5a12008-03-14 17:17:09 +0100647 ice->spdif.ops.open = juli_spdif_in_open;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 return 0;
649}
650
651
652/*
653 * Juli@ boards don't provide the EEPROM data except for the vendor IDs.
654 * hence the driver needs to sets up it properly.
655 */
656
Takashi Iwai1b60f6b2007-03-13 22:13:47 +0100657static unsigned char juli_eeprom[] __devinitdata = {
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100658 [ICE_EEP2_SYSCONF] = 0x2b, /* clock 512, mpu401, 1xADC, 1xDACs,
659 SPDIF in */
Takashi Iwai189bc172007-01-29 15:25:40 +0100660 [ICE_EEP2_ACLINK] = 0x80, /* I2S */
661 [ICE_EEP2_I2S] = 0xf8, /* vol, 96k, 24bit, 192k */
662 [ICE_EEP2_SPDIF] = 0xc3, /* out-en, out-int, spdif-in */
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100663 [ICE_EEP2_GPIO_DIR] = 0x9f, /* 5, 6:inputs; 7, 4-0 outputs*/
Takashi Iwai189bc172007-01-29 15:25:40 +0100664 [ICE_EEP2_GPIO_DIR1] = 0xff,
665 [ICE_EEP2_GPIO_DIR2] = 0x7f,
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100666 [ICE_EEP2_GPIO_MASK] = 0x60, /* 5, 6: locked; 7, 4-0 writable */
667 [ICE_EEP2_GPIO_MASK1] = 0x00, /* 0-7 writable */
Takashi Iwai189bc172007-01-29 15:25:40 +0100668 [ICE_EEP2_GPIO_MASK2] = 0x7f,
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100669 [ICE_EEP2_GPIO_STATE] = GPIO_FREQ_48KHZ | GPIO_MULTI_1X |
670 GPIO_INTERNAL_CLOCK, /* internal clock, multiple 1x, 48kHz*/
671 [ICE_EEP2_GPIO_STATE1] = 0x00, /* unmuted */
Takashi Iwai189bc172007-01-29 15:25:40 +0100672 [ICE_EEP2_GPIO_STATE2] = 0x00,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673};
674
675/* entry point */
Takashi Iwai1b60f6b2007-03-13 22:13:47 +0100676struct snd_ice1712_card_info snd_vt1724_juli_cards[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 {
678 .subvendor = VT1724_SUBDEVICE_JULI,
679 .name = "ESI Juli@",
680 .model = "juli",
681 .chip_init = juli_init,
682 .build_controls = juli_add_controls,
683 .eeprom_size = sizeof(juli_eeprom),
684 .eeprom_data = juli_eeprom,
685 },
686 { } /* terminator */
687};