ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / sound / pci / ice1712 / ice1724.c
1 /*
2  *   ALSA driver for VT1724 ICEnsemble ICE1724 / VIA VT1724 (Envy24HT)
3  *
4  *      Copyright (c) 2000 Jaroslav Kysela <perex@suse.cz>
5  *                    2002 James Stafford <jstafford@ampltd.com>
6  *                    2003 Takashi Iwai <tiwai@suse.de>
7  *
8  *   This program is free software; you can redistribute it and/or modify
9  *   it under the terms of the GNU General Public License as published by
10  *   the Free Software Foundation; either version 2 of the License, or
11  *   (at your option) any later version.
12  *
13  *   This program is distributed in the hope that it will be useful,
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *   GNU General Public License for more details.
17  *
18  *   You should have received a copy of the GNU General Public License
19  *   along with this program; if not, write to the Free Software
20  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21  *
22  */      
23
24 #include <sound/driver.h>
25 #include <asm/io.h>
26 #include <linux/delay.h>
27 #include <linux/interrupt.h>
28 #include <linux/init.h>
29 #include <linux/pci.h>
30 #include <linux/slab.h>
31 #include <sound/core.h>
32 #include <sound/info.h>
33 #include <sound/mpu401.h>
34 #define SNDRV_GET_ID
35 #include <sound/initval.h>
36
37 #include <sound/asoundef.h>
38
39 #include "ice1712.h"
40 #include "envy24ht.h"
41
42 /* lowlevel routines */
43 #include "amp.h"
44 #include "revo.h"
45 #include "aureon.h"
46 #include "prodigy.h"
47
48
49 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
50 MODULE_DESCRIPTION("ICEnsemble ICE1724 (Envy24HT)");
51 MODULE_LICENSE("GPL");
52 MODULE_CLASSES("{sound}");
53 MODULE_DEVICES("{"
54                REVO_DEVICE_DESC
55                AMP_AUDIO2000_DEVICE_DESC
56                AUREON_DEVICE_DESC
57                PRODIGY_DEVICE_DESC
58                 "{VIA,VT1724},"
59                 "{ICEnsemble,Generic ICE1724},"
60                 "{ICEnsemble,Generic Envy24HT}}");
61
62 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;      /* Index 0-MAX */
63 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;       /* ID for this card */
64 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;              /* Enable this card */
65
66 MODULE_PARM(index, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
67 MODULE_PARM_DESC(index, "Index value for ICE1724 soundcard.");
68 MODULE_PARM_SYNTAX(index, SNDRV_INDEX_DESC);
69 MODULE_PARM(id, "1-" __MODULE_STRING(SNDRV_CARDS) "s");
70 MODULE_PARM_DESC(id, "ID string for ICE1724 soundcard.");
71 MODULE_PARM_SYNTAX(id, SNDRV_ID_DESC);
72 MODULE_PARM(enable, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
73 MODULE_PARM_DESC(enable, "Enable ICE1724 soundcard.");
74 MODULE_PARM_SYNTAX(enable, SNDRV_ENABLE_DESC);
75
76 #ifndef PCI_VENDOR_ID_ICE
77 #define PCI_VENDOR_ID_ICE               0x1412
78 #endif
79 #ifndef PCI_DEVICE_ID_VT1724
80 #define PCI_DEVICE_ID_VT1724            0x1724
81 #endif
82
83 static struct pci_device_id snd_vt1724_ids[] = {
84         { PCI_VENDOR_ID_ICE, PCI_DEVICE_ID_VT1724, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
85         { 0, }
86 };
87
88 MODULE_DEVICE_TABLE(pci, snd_vt1724_ids);
89
90
91 static int PRO_RATE_LOCKED;
92 static int PRO_RATE_RESET = 1;
93 static unsigned int PRO_RATE_DEFAULT = 44100;
94
95 /*
96  *  Basic I/O
97  */
98  
99 /* check whether the clock mode is spdif-in */
100 static inline int is_spdif_master(ice1712_t *ice)
101 {
102         return (inb(ICEMT1724(ice, RATE)) & VT1724_SPDIF_MASTER) ? 1 : 0;
103 }
104
105 static inline int is_pro_rate_locked(ice1712_t *ice)
106 {
107         return is_spdif_master(ice) || PRO_RATE_LOCKED;
108 }
109
110 /*
111  * ac97 section
112  */
113
114 static unsigned char snd_vt1724_ac97_ready(ice1712_t *ice)
115 {
116         unsigned char old_cmd;
117         int tm;
118         for (tm = 0; tm < 0x10000; tm++) {
119                 old_cmd = inb(ICEMT1724(ice, AC97_CMD));
120                 if (old_cmd & (VT1724_AC97_WRITE | VT1724_AC97_READ))
121                         continue;
122                 if (!(old_cmd & VT1724_AC97_READY))
123                         continue;
124                 return old_cmd;
125         }
126         snd_printd(KERN_ERR "snd_vt1724_ac97_ready: timeout\n");
127         return old_cmd;
128 }
129
130 static int snd_vt1724_ac97_wait_bit(ice1712_t *ice, unsigned char bit)
131 {
132         int tm;
133         for (tm = 0; tm < 0x10000; tm++)
134                 if ((inb(ICEMT1724(ice, AC97_CMD)) & bit) == 0)
135                         return 0;
136         snd_printd(KERN_ERR "snd_vt1724_ac97_wait_bit: timeout\n");
137         return -EIO;
138 }
139
140 static void snd_vt1724_ac97_write(ac97_t *ac97,
141                                   unsigned short reg,
142                                   unsigned short val)
143 {
144         ice1712_t *ice = (ice1712_t *)ac97->private_data;
145         unsigned char old_cmd;
146
147         old_cmd = snd_vt1724_ac97_ready(ice);
148         old_cmd &= ~VT1724_AC97_ID_MASK;
149         old_cmd |= ac97->num;
150         outb(reg, ICEMT1724(ice, AC97_INDEX));
151         outw(val, ICEMT1724(ice, AC97_DATA));
152         outb(old_cmd | VT1724_AC97_WRITE, ICEMT1724(ice, AC97_CMD));
153         snd_vt1724_ac97_wait_bit(ice, VT1724_AC97_WRITE);
154 }
155
156 static unsigned short snd_vt1724_ac97_read(ac97_t *ac97, unsigned short reg)
157 {
158         ice1712_t *ice = (ice1712_t *)ac97->private_data;
159         unsigned char old_cmd;
160
161         old_cmd = snd_vt1724_ac97_ready(ice);
162         old_cmd &= ~VT1724_AC97_ID_MASK;
163         old_cmd |= ac97->num;
164         outb(reg, ICEMT1724(ice, AC97_INDEX));
165         outb(old_cmd | VT1724_AC97_READ, ICEMT1724(ice, AC97_CMD));
166         if (snd_vt1724_ac97_wait_bit(ice, VT1724_AC97_READ) < 0)
167                 return ~0;
168         return inw(ICEMT1724(ice, AC97_DATA));
169 }
170
171
172 /*
173  * GPIO operations
174  */
175
176 /* set gpio direction 0 = read, 1 = write */
177 static void snd_vt1724_set_gpio_dir(ice1712_t *ice, unsigned int data)
178 {
179         outl(data, ICEREG1724(ice, GPIO_DIRECTION));
180         inw(ICEREG1724(ice, GPIO_DIRECTION)); /* dummy read for pci-posting */
181 }
182
183 /* set the gpio mask (0 = writable) */
184 static void snd_vt1724_set_gpio_mask(ice1712_t *ice, unsigned int data)
185 {
186         outw(data, ICEREG1724(ice, GPIO_WRITE_MASK));
187         outb((data >> 16) & 0xff, ICEREG1724(ice, GPIO_WRITE_MASK_22));
188         inw(ICEREG1724(ice, GPIO_WRITE_MASK)); /* dummy read for pci-posting */
189 }
190
191 static void snd_vt1724_set_gpio_data(ice1712_t *ice, unsigned int data)
192 {
193         outw(data, ICEREG1724(ice, GPIO_DATA));
194         outb(data >> 16, ICEREG1724(ice, GPIO_DATA_22));
195         inw(ICEREG1724(ice, GPIO_DATA)); /* dummy read for pci-posting */
196 }
197
198 static unsigned int snd_vt1724_get_gpio_data(ice1712_t *ice)
199 {
200         unsigned int data;
201         data = (unsigned int)inb(ICEREG1724(ice, GPIO_DATA_22));
202         data = (data << 16) | inw(ICEREG1724(ice, GPIO_DATA));
203         return data;
204 }
205
206 /*
207  *  Interrupt handler
208  */
209
210 static irqreturn_t snd_vt1724_interrupt(int irq, void *dev_id, struct pt_regs *regs)
211 {
212         ice1712_t *ice = snd_magic_cast(ice1712_t, dev_id, return IRQ_NONE);
213         unsigned char status;
214         int handled = 0;
215
216         while (1) {
217                 status = inb(ICEREG1724(ice, IRQSTAT));
218                 if (status == 0)
219                         break;
220
221                 handled = 1;            
222                 /*  these should probably be separated at some point, 
223                         but as we don't currently have MPU support on the board I will leave it */
224                 if ((status & VT1724_IRQ_MPU_RX)||(status & VT1724_IRQ_MPU_TX)) {
225                         if (ice->rmidi[0])
226                                 snd_mpu401_uart_interrupt(irq, ice->rmidi[0]->private_data, regs);
227                         outb(VT1724_IRQ_MPU_RX|VT1724_IRQ_MPU_TX, ICEREG1724(ice, IRQSTAT));
228                         status &= ~(VT1724_IRQ_MPU_RX|VT1724_IRQ_MPU_TX);
229                 }
230                 if (status & VT1724_IRQ_MTPCM) {
231                         unsigned char mtstat = inb(ICEMT1724(ice, IRQ));
232                         if (mtstat & VT1724_MULTI_PDMA0) {
233                                 if (ice->playback_pro_substream)
234                                         snd_pcm_period_elapsed(ice->playback_pro_substream);
235                         }
236                         if (mtstat & VT1724_MULTI_RDMA0) {
237                                 if (ice->capture_pro_substream)
238                                         snd_pcm_period_elapsed(ice->capture_pro_substream);
239                         }
240                         if (mtstat & VT1724_MULTI_PDMA1) {
241                                 if (ice->playback_con_substream_ds[0])
242                                         snd_pcm_period_elapsed(ice->playback_con_substream_ds[0]);
243                         }
244                         if (mtstat & VT1724_MULTI_PDMA2) {
245                                 if (ice->playback_con_substream_ds[1])
246                                         snd_pcm_period_elapsed(ice->playback_con_substream_ds[1]);
247                         }
248                         if (mtstat & VT1724_MULTI_PDMA3) {
249                                 if (ice->playback_con_substream_ds[2])
250                                         snd_pcm_period_elapsed(ice->playback_con_substream_ds[2]);
251                         }
252                         if (mtstat & VT1724_MULTI_PDMA4) {
253                                 if (ice->playback_con_substream)
254                                         snd_pcm_period_elapsed(ice->playback_con_substream);
255                         }
256                         if (mtstat & VT1724_MULTI_RDMA1) {
257                                 if (ice->capture_con_substream)
258                                         snd_pcm_period_elapsed(ice->capture_con_substream);
259                         }
260                         /* ack anyway to avoid freeze */
261                         outb(mtstat, ICEMT1724(ice, IRQ));
262                         /* ought to really handle this properly */
263                         if (mtstat & VT1724_MULTI_FIFO_ERR) {
264                                 unsigned char fstat = inb(ICEMT1724(ice, DMA_FIFO_ERR));
265                                 outb(fstat, ICEMT1724(ice, DMA_FIFO_ERR));      
266                                 outb(VT1724_MULTI_FIFO_ERR | inb(ICEMT1724(ice, DMA_INT_MASK)), ICEMT1724(ice, DMA_INT_MASK));  
267                                 /* If I don't do this, I get machine lockup due to continual interrupts */
268                         }
269
270                 }
271         }
272         return IRQ_RETVAL(handled);
273 }
274
275 /*
276  *  PCM code - professional part (multitrack)
277  */
278
279 static unsigned int rates[] = {
280         8000, 9600, 11025, 12000, 16000, 22050, 24000,
281         32000, 44100, 48000, 64000, 88200, 96000,
282         176400, 192000,
283 };
284
285 static snd_pcm_hw_constraint_list_t hw_constraints_rates_96 = {
286         .count = ARRAY_SIZE(rates) - 2, /* up to 96000 */
287         .list = rates,
288         .mask = 0,
289 };
290
291 static snd_pcm_hw_constraint_list_t hw_constraints_rates_192 = {
292         .count = ARRAY_SIZE(rates),
293         .list = rates,
294         .mask = 0,
295 };
296
297 static int snd_vt1724_pcm_trigger(snd_pcm_substream_t *substream, int cmd)
298 {
299         ice1712_t *ice = snd_pcm_substream_chip(substream);
300         unsigned char what;
301         unsigned char old;
302         struct list_head *pos;
303         snd_pcm_substream_t *s;
304
305         what = 0;
306         snd_pcm_group_for_each(pos, substream) {
307                 s = snd_pcm_group_substream_entry(pos);
308                 what |= (unsigned long)(s->runtime->private_data);
309                 snd_pcm_trigger_done(s, substream);
310         }
311
312         switch (cmd) {
313         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
314         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
315                 spin_lock(&ice->reg_lock);
316                 old = inb(ICEMT1724(ice, DMA_PAUSE));
317                 if (cmd == SNDRV_PCM_TRIGGER_PAUSE_PUSH)
318                         old |= what;
319                 else
320                         old &= ~what;
321                 outb(old, ICEMT1724(ice, DMA_PAUSE));
322                 spin_unlock(&ice->reg_lock);
323                 break;
324
325         case SNDRV_PCM_TRIGGER_START:
326         case SNDRV_PCM_TRIGGER_STOP:
327                 spin_lock(&ice->reg_lock);
328                 old = inb(ICEMT1724(ice, DMA_CONTROL));
329                 if (cmd == SNDRV_PCM_TRIGGER_START)
330                         old |= what;
331                 else
332                         old &= ~what;
333                 outb(old, ICEMT1724(ice, DMA_CONTROL));
334                 spin_unlock(&ice->reg_lock);
335                 break;
336
337         default:
338                 return -EINVAL;
339         }
340         return 0;
341 }
342
343 /*
344  */
345
346 #define DMA_STARTS      (VT1724_RDMA0_START|VT1724_PDMA0_START|VT1724_RDMA1_START|\
347         VT1724_PDMA1_START|VT1724_PDMA2_START|VT1724_PDMA3_START|VT1724_PDMA4_START)
348 #define DMA_PAUSES      (VT1724_RDMA0_PAUSE|VT1724_PDMA0_PAUSE|VT1724_RDMA1_PAUSE|\
349         VT1724_PDMA1_PAUSE|VT1724_PDMA2_PAUSE|VT1724_PDMA3_PAUSE|VT1724_PDMA4_PAUSE)
350
351 static void snd_vt1724_set_pro_rate(ice1712_t *ice, unsigned int rate, int force)
352 {
353         unsigned long flags;
354         unsigned char val, old;
355         unsigned int i;
356
357         spin_lock_irqsave(&ice->reg_lock, flags);
358         if ((inb(ICEMT1724(ice, DMA_CONTROL)) & DMA_STARTS) || 
359             (inb(ICEMT1724(ice, DMA_PAUSE)) & DMA_PAUSES)) {
360                 /* running? we cannot change the rate now... */
361                 spin_unlock_irqrestore(&ice->reg_lock, flags);
362                 return;
363         }
364         if (!force && is_pro_rate_locked(ice)) {
365                 spin_unlock_irqrestore(&ice->reg_lock, flags);
366                 return;
367         }
368
369         switch (rate) {
370         case 8000: val = 6; break;
371         case 9600: val = 3; break;
372         case 11025: val = 10; break;
373         case 12000: val = 2; break;
374         case 16000: val = 5; break;
375         case 22050: val = 9; break;
376         case 24000: val = 1; break;
377         case 32000: val = 4; break;
378         case 44100: val = 8; break;
379         case 48000: val = 0; break;
380         case 64000: val = 15; break;
381         case 88200: val = 11; break;
382         case 96000: val = 7; break;
383         case 176400: val = 12; break;
384         case 192000: val = 14; break;
385         default:
386                 snd_BUG();
387                 val = 0;
388                 break;
389         }
390         outb(val, ICEMT1724(ice, RATE));
391         if (rate == ice->cur_rate) {
392                 spin_unlock_irqrestore(&ice->reg_lock, flags);
393                 return;
394         }
395
396         ice->cur_rate = rate;
397
398         /* check MT02 */
399         if (ice->eeprom.data[ICE_EEP2_ACLINK] & 0x80) {
400                 val = old = inb(ICEMT1724(ice, I2S_FORMAT));
401                 if (rate > 96000)
402                         val |= VT1724_MT_I2S_MCLK_128X; /* 128x MCLK */
403                 else
404                         val &= ~VT1724_MT_I2S_MCLK_128X; /* 256x MCLK */
405                 if (val != old) {
406                         outb(val, ICEMT1724(ice, I2S_FORMAT));
407                         if (ice->eeprom.subvendor == VT1724_SUBDEVICE_REVOLUTION71) {
408                                 /* FIXME: is this revo only? */
409                                 /* assert PRST# to converters; MT05 bit 7 */
410                                 outb(inb(ICEMT1724(ice, AC97_CMD)) | 0x80, ICEMT1724(ice, AC97_CMD));
411                                 spin_unlock_irqrestore(&ice->reg_lock, flags);
412                                 mdelay(5);
413                                 spin_lock_irqsave(&ice->reg_lock, flags);
414                                 /* deassert PRST# */
415                                 outb(inb(ICEMT1724(ice, AC97_CMD)) & ~0x80, ICEMT1724(ice, AC97_CMD));
416                         }
417                 }
418         }
419         spin_unlock_irqrestore(&ice->reg_lock, flags);
420
421         /* set up codecs */
422         for (i = 0; i < ice->akm_codecs; i++) {
423                 if (ice->akm[i].ops.set_rate_val)
424                         ice->akm[i].ops.set_rate_val(&ice->akm[i], rate);
425         }
426 }
427
428 static int snd_vt1724_pcm_hw_params(snd_pcm_substream_t * substream,
429                                     snd_pcm_hw_params_t * hw_params)
430 {
431         ice1712_t *ice = snd_pcm_substream_chip(substream);
432         int i, chs;
433
434         chs = params_channels(hw_params);
435         down(&ice->open_mutex);
436         /* mark surround channels */
437         if (substream == ice->playback_pro_substream) {
438                 chs = chs / 2 - 1;
439                 for (i = 0; i < chs; i++) {
440                         if (ice->pcm_reserved[i] && ice->pcm_reserved[i] != substream) {
441                                 up(&ice->open_mutex);
442                                 return -EBUSY;
443                         }
444                         ice->pcm_reserved[i] = substream;
445                 }
446                 for (; i < 3; i++) {
447                         if (ice->pcm_reserved[i] == substream)
448                                 ice->pcm_reserved[i] = NULL;
449                 }
450         } else {
451                 for (i = 0; i < 3; i++) {
452                         if (ice->playback_con_substream_ds[i] == substream) {
453                                 if (ice->pcm_reserved[i] && ice->pcm_reserved[i] != substream) {
454                                         up(&ice->open_mutex);
455                                         return -EBUSY;
456                                 }
457                                 ice->pcm_reserved[i] = substream;
458                                 break;
459                         }
460                 }
461         }
462         up(&ice->open_mutex);
463         snd_vt1724_set_pro_rate(ice, params_rate(hw_params), 0);
464         return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
465 }
466
467 static int snd_vt1724_pcm_hw_free(snd_pcm_substream_t * substream)
468 {
469         ice1712_t *ice = snd_pcm_substream_chip(substream);
470         int i;
471
472         down(&ice->open_mutex);
473         /* unmark surround channels */
474         for (i = 0; i < 3; i++)
475                 if (ice->pcm_reserved[i] == substream)
476                         ice->pcm_reserved[i] = NULL;
477         up(&ice->open_mutex);
478         return snd_pcm_lib_free_pages(substream);
479 }
480
481 static int snd_vt1724_playback_pro_prepare(snd_pcm_substream_t * substream)
482 {
483         ice1712_t *ice = snd_pcm_substream_chip(substream);
484         unsigned char val;
485         unsigned int size;
486
487         spin_lock(&ice->reg_lock);
488         val = (8 - substream->runtime->channels) >> 1;
489         outb(val, ICEMT1724(ice, BURST));
490
491         outl(substream->runtime->dma_addr, ICEMT1724(ice, PLAYBACK_ADDR));
492
493         size = (snd_pcm_lib_buffer_bytes(substream) >> 2) - 1;
494         // outl(size, ICEMT1724(ice, PLAYBACK_SIZE));
495         outw(size, ICEMT1724(ice, PLAYBACK_SIZE));
496         outb(size >> 16, ICEMT1724(ice, PLAYBACK_SIZE) + 2);
497         size = (snd_pcm_lib_period_bytes(substream) >> 2) - 1;
498         // outl(size, ICEMT1724(ice, PLAYBACK_COUNT));
499         outw(size, ICEMT1724(ice, PLAYBACK_COUNT));
500         outb(size >> 16, ICEMT1724(ice, PLAYBACK_COUNT) + 2);
501
502         spin_unlock(&ice->reg_lock);
503
504         // printk("pro prepare: ch = %d, addr = 0x%x, buffer = 0x%x, period = 0x%x\n", substream->runtime->channels, (unsigned int)substream->runtime->dma_addr, snd_pcm_lib_buffer_bytes(substream), snd_pcm_lib_period_bytes(substream));
505         return 0;
506 }
507
508 static snd_pcm_uframes_t snd_vt1724_playback_pro_pointer(snd_pcm_substream_t * substream)
509 {
510         ice1712_t *ice = snd_pcm_substream_chip(substream);
511         size_t ptr;
512
513         if (!(inl(ICEMT1724(ice, DMA_CONTROL)) & VT1724_PDMA0_START))
514                 return 0;
515 #if 0 /* read PLAYBACK_ADDR */
516         ptr = inl(ICEMT1724(ice, PLAYBACK_ADDR));
517         if (ptr < substream->runtime->dma_addr) {
518                 snd_printd("ice1724: invalid negative ptr\n");
519                 return 0;
520         }
521         ptr -= substream->runtime->dma_addr;
522         ptr = bytes_to_frames(substream->runtime, ptr);
523         if (ptr >= substream->runtime->buffer_size) {
524                 snd_printd("ice1724: invalid ptr %d (size=%d)\n", (int)ptr, (int)substream->runtime->period_size);
525                 return 0;
526         }
527 #else /* read PLAYBACK_SIZE */
528         ptr = inl(ICEMT1724(ice, PLAYBACK_SIZE)) & 0xffffff;
529         ptr = (ptr + 1) << 2;
530         ptr = bytes_to_frames(substream->runtime, ptr);
531         if (ptr <= substream->runtime->buffer_size)
532                 ptr = substream->runtime->buffer_size - ptr;
533         else {
534                 snd_printd("ice1724: invalid ptr %d (size=%d)\n", (int)ptr, (int)substream->runtime->buffer_size);
535                 ptr = 0;
536         }
537 #endif
538         return ptr;
539 }
540
541 struct vt1724_pcm_reg {
542         unsigned int addr;      /* ADDR register offset */
543         unsigned int size;      /* SIZE register offset */
544         unsigned int count;     /* COUNT register offset */
545         unsigned int start;     /* start bit */
546         unsigned int pause;     /* pause bit */
547 };
548
549 static int snd_vt1724_pcm_prepare(snd_pcm_substream_t *substream, const struct vt1724_pcm_reg *reg)
550 {
551         ice1712_t *ice = snd_pcm_substream_chip(substream);
552
553         spin_lock(&ice->reg_lock);
554         outl(substream->runtime->dma_addr, ice->profi_port + reg->addr);
555         outw((snd_pcm_lib_buffer_bytes(substream) >> 2) - 1, ice->profi_port + reg->size);
556         outw((snd_pcm_lib_period_bytes(substream) >> 2) - 1, ice->profi_port + reg->count);
557         spin_unlock(&ice->reg_lock);
558         return 0;
559 }
560
561 static snd_pcm_uframes_t snd_vt1724_pcm_pointer(snd_pcm_substream_t *substream, const struct vt1724_pcm_reg *reg)
562 {
563         ice1712_t *ice = snd_pcm_substream_chip(substream);
564         size_t ptr;
565
566         if (!(inl(ICEMT1724(ice, DMA_CONTROL)) & reg->start))
567                 return 0;
568 #if 0 /* use ADDR register */
569         ptr = inl(ice->profi_port + reg->addr);
570         ptr -= substream->runtime->dma_addr;
571         return bytes_to_frames(substream->runtime, ptr);
572 #else /* use SIZE register */
573         ptr = inw(ice->profi_port + reg->size);
574         ptr = (ptr + 1) << 2;
575         ptr = bytes_to_frames(substream->runtime, ptr);
576         if (ptr <= substream->runtime->buffer_size)
577                 ptr = substream->runtime->buffer_size - ptr;
578         else {
579                 snd_printd("ice1724: invalid ptr %d (size=%d)\n", (int)ptr, (int)substream->runtime->buffer_size);
580                 ptr = 0;
581         }
582         return ptr;
583 #endif
584 }
585
586 const static struct vt1724_pcm_reg vt1724_capture_pro_reg = {
587         .addr = VT1724_MT_CAPTURE_ADDR,
588         .size = VT1724_MT_CAPTURE_SIZE,
589         .count = VT1724_MT_CAPTURE_COUNT,
590         .start = VT1724_RDMA0_START,
591         .pause = VT1724_RDMA0_PAUSE,
592 };
593
594 static int snd_vt1724_capture_pro_prepare(snd_pcm_substream_t * substream)
595 {
596         return snd_vt1724_pcm_prepare(substream, &vt1724_capture_pro_reg);
597 }
598
599 static snd_pcm_uframes_t snd_vt1724_capture_pro_pointer(snd_pcm_substream_t * substream)
600 {
601         return snd_vt1724_pcm_pointer(substream, &vt1724_capture_pro_reg);
602 }
603
604 static snd_pcm_hardware_t snd_vt1724_playback_pro =
605 {
606         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
607                                  SNDRV_PCM_INFO_BLOCK_TRANSFER |
608                                  SNDRV_PCM_INFO_MMAP_VALID |
609                                  SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_SYNC_START),
610         .formats =              SNDRV_PCM_FMTBIT_S32_LE,
611         .rates =                SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_192000,
612         .rate_min =             4000,
613         .rate_max =             192000,
614         .channels_min =         2,
615         .channels_max =         8,
616         .buffer_bytes_max =     (1UL << 21),    /* 19bits dword */
617         .period_bytes_min =     8 * 4 * 2,      /* FIXME: constraints needed */
618         .period_bytes_max =     (1UL << 21),
619         .periods_min =          2,
620         .periods_max =          1024,
621 };
622
623 static snd_pcm_hardware_t snd_vt1724_2ch_stereo =
624 {
625         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
626                                  SNDRV_PCM_INFO_BLOCK_TRANSFER |
627                                  SNDRV_PCM_INFO_MMAP_VALID |
628                                  SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_SYNC_START),
629         .formats =              SNDRV_PCM_FMTBIT_S32_LE,
630         .rates =                SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_192000,
631         .rate_min =             4000,
632         .rate_max =             192000,
633         .channels_min =         2,
634         .channels_max =         2,
635         .buffer_bytes_max =     (1UL << 18),    /* 16bits dword */
636         .period_bytes_min =     2 * 4 * 2,
637         .period_bytes_max =     (1UL << 18),
638         .periods_min =          2,
639         .periods_max =          1024,
640 };
641
642 /* multi-channel playback needs alignment 8x32bit regarless of the channels
643  * actually used
644  */
645 #define VT1724_BUFFER_ALIGN     0x20
646
647 static int snd_vt1724_playback_pro_open(snd_pcm_substream_t * substream)
648 {
649         snd_pcm_runtime_t *runtime = substream->runtime;
650         ice1712_t *ice = snd_pcm_substream_chip(substream);
651         int chs;
652
653         runtime->private_data = (void*)VT1724_PDMA0_START; /* irq/status/trigger bit */
654         ice->playback_pro_substream = substream;
655         runtime->hw = snd_vt1724_playback_pro;
656         snd_pcm_set_sync(substream);
657         snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
658         if ((ice->eeprom.data[ICE_EEP2_ACLINK] & 0x80) &&
659             (ice->eeprom.data[ICE_EEP2_I2S] & 0x08))
660                 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates_192);
661         else
662                 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates_96);
663
664         down(&ice->open_mutex);
665         /* calculate the currently available channels */
666         for (chs = 0; chs < 3; chs++) {
667                 if (ice->pcm_reserved[chs])
668                         break;
669         }
670         chs = (chs + 1) * 2;
671         runtime->hw.channels_max = chs;
672         if (chs > 2) /* channels must be even */
673                 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, 2);
674         up(&ice->open_mutex);
675         snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
676                                    VT1724_BUFFER_ALIGN);
677         snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
678                                    VT1724_BUFFER_ALIGN);
679         return 0;
680 }
681
682 static int snd_vt1724_capture_pro_open(snd_pcm_substream_t * substream)
683 {
684         ice1712_t *ice = snd_pcm_substream_chip(substream);
685         snd_pcm_runtime_t *runtime = substream->runtime;
686
687         runtime->private_data = (void*)VT1724_RDMA0_START; /* irq/status/trigger bit */
688         ice->capture_pro_substream = substream;
689         runtime->hw = snd_vt1724_2ch_stereo;
690         snd_pcm_set_sync(substream);
691         snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
692         if ((ice->eeprom.data[ICE_EEP2_ACLINK] & 0x80) &&
693             (ice->eeprom.data[ICE_EEP2_I2S] & 0x08))
694                 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates_192);
695         else
696                 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates_96);
697         return 0;
698 }
699
700 static int snd_vt1724_playback_pro_close(snd_pcm_substream_t * substream)
701 {
702         ice1712_t *ice = snd_pcm_substream_chip(substream);
703
704         if (PRO_RATE_RESET)
705                 snd_vt1724_set_pro_rate(ice, PRO_RATE_DEFAULT, 0);
706         ice->playback_pro_substream = NULL;
707
708         return 0;
709 }
710
711 static int snd_vt1724_capture_pro_close(snd_pcm_substream_t * substream)
712 {
713         ice1712_t *ice = snd_pcm_substream_chip(substream);
714
715         if (PRO_RATE_RESET)
716                 snd_vt1724_set_pro_rate(ice, PRO_RATE_DEFAULT, 0);
717         ice->capture_pro_substream = NULL;
718         return 0;
719 }
720
721 static snd_pcm_ops_t snd_vt1724_playback_pro_ops = {
722         .open =         snd_vt1724_playback_pro_open,
723         .close =        snd_vt1724_playback_pro_close,
724         .ioctl =        snd_pcm_lib_ioctl,
725         .hw_params =    snd_vt1724_pcm_hw_params,
726         .hw_free =      snd_vt1724_pcm_hw_free,
727         .prepare =      snd_vt1724_playback_pro_prepare,
728         .trigger =      snd_vt1724_pcm_trigger,
729         .pointer =      snd_vt1724_playback_pro_pointer,
730 };
731
732 static snd_pcm_ops_t snd_vt1724_capture_pro_ops = {
733         .open =         snd_vt1724_capture_pro_open,
734         .close =        snd_vt1724_capture_pro_close,
735         .ioctl =        snd_pcm_lib_ioctl,
736         .hw_params =    snd_vt1724_pcm_hw_params,
737         .hw_free =      snd_vt1724_pcm_hw_free,
738         .prepare =      snd_vt1724_capture_pro_prepare,
739         .trigger =      snd_vt1724_pcm_trigger,
740         .pointer =      snd_vt1724_capture_pro_pointer,
741 };
742
743 static int __devinit snd_vt1724_pcm_profi(ice1712_t * ice, int device)
744 {
745         snd_pcm_t *pcm;
746         int err;
747
748         err = snd_pcm_new(ice->card, "ICE1724", device, 1, 1, &pcm);
749         if (err < 0)
750                 return err;
751
752         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_vt1724_playback_pro_ops);
753         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_vt1724_capture_pro_ops);
754
755         pcm->private_data = ice;
756         pcm->info_flags = 0;
757         strcpy(pcm->name, "ICE1724");
758
759         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
760                                               snd_dma_pci_data(ice->pci), 256*1024, 256*1024);
761
762         ice->pcm_pro = pcm;
763
764         return 0;
765 }
766
767
768 /*
769  * SPDIF PCM
770  */
771
772 const static struct vt1724_pcm_reg vt1724_playback_spdif_reg = {
773         .addr = VT1724_MT_PDMA4_ADDR,
774         .size = VT1724_MT_PDMA4_SIZE,
775         .count = VT1724_MT_PDMA4_COUNT,
776         .start = VT1724_PDMA4_START,
777         .pause = VT1724_PDMA4_PAUSE,
778 };
779
780 const static struct vt1724_pcm_reg vt1724_capture_spdif_reg = {
781         .addr = VT1724_MT_RDMA1_ADDR,
782         .size = VT1724_MT_RDMA1_SIZE,
783         .count = VT1724_MT_RDMA1_COUNT,
784         .start = VT1724_RDMA1_START,
785         .pause = VT1724_RDMA1_PAUSE,
786 };
787
788 static int snd_vt1724_playback_spdif_prepare(snd_pcm_substream_t * substream)
789 {
790         return snd_vt1724_pcm_prepare(substream, &vt1724_playback_spdif_reg);
791 }
792
793 static snd_pcm_uframes_t snd_vt1724_playback_spdif_pointer(snd_pcm_substream_t * substream)
794 {
795         return snd_vt1724_pcm_pointer(substream, &vt1724_playback_spdif_reg);
796 }
797
798 static int snd_vt1724_capture_spdif_prepare(snd_pcm_substream_t * substream)
799 {
800         return snd_vt1724_pcm_prepare(substream, &vt1724_capture_spdif_reg);
801 }
802
803 static snd_pcm_uframes_t snd_vt1724_capture_spdif_pointer(snd_pcm_substream_t * substream)
804 {
805         return snd_vt1724_pcm_pointer(substream, &vt1724_capture_spdif_reg);
806 }
807
808 static int snd_vt1724_playback_spdif_open(snd_pcm_substream_t *substream)
809 {
810         ice1712_t *ice = snd_pcm_substream_chip(substream);
811         snd_pcm_runtime_t *runtime = substream->runtime;
812
813         runtime->private_data = (void*)VT1724_PDMA4_START; /* irq/status/trigger bit */
814         ice->playback_con_substream = substream;
815         runtime->hw = snd_vt1724_2ch_stereo;
816         snd_pcm_set_sync(substream);
817         snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
818
819         snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates_192);
820         return 0;
821 }
822
823 static int snd_vt1724_playback_spdif_close(snd_pcm_substream_t * substream)
824 {
825         ice1712_t *ice = snd_pcm_substream_chip(substream);
826
827         if (PRO_RATE_RESET)
828                 snd_vt1724_set_pro_rate(ice, PRO_RATE_DEFAULT, 0);
829         ice->playback_con_substream = NULL;
830
831         return 0;
832 }
833
834 static int snd_vt1724_capture_spdif_open(snd_pcm_substream_t *substream)
835 {
836         ice1712_t *ice = snd_pcm_substream_chip(substream);
837         snd_pcm_runtime_t *runtime = substream->runtime;
838
839         runtime->private_data = (void*)VT1724_RDMA1_START; /* irq/status/trigger bit */
840         ice->capture_con_substream = substream;
841         runtime->hw = snd_vt1724_2ch_stereo;
842         snd_pcm_set_sync(substream);
843         snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
844         snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates_96);
845         return 0;
846 }
847
848 static int snd_vt1724_capture_spdif_close(snd_pcm_substream_t * substream)
849 {
850         ice1712_t *ice = snd_pcm_substream_chip(substream);
851
852         if (PRO_RATE_RESET)
853                 snd_vt1724_set_pro_rate(ice, PRO_RATE_DEFAULT, 0);
854         ice->capture_con_substream = NULL;
855
856         return 0;
857 }
858
859 static snd_pcm_ops_t snd_vt1724_playback_spdif_ops = {
860         .open =         snd_vt1724_playback_spdif_open,
861         .close =        snd_vt1724_playback_spdif_close,
862         .ioctl =        snd_pcm_lib_ioctl,
863         .hw_params =    snd_vt1724_pcm_hw_params,
864         .hw_free =      snd_vt1724_pcm_hw_free,
865         .prepare =      snd_vt1724_playback_spdif_prepare,
866         .trigger =      snd_vt1724_pcm_trigger,
867         .pointer =      snd_vt1724_playback_spdif_pointer,
868 };
869
870 static snd_pcm_ops_t snd_vt1724_capture_spdif_ops = {
871         .open =         snd_vt1724_capture_spdif_open,
872         .close =        snd_vt1724_capture_spdif_close,
873         .ioctl =        snd_pcm_lib_ioctl,
874         .hw_params =    snd_vt1724_pcm_hw_params,
875         .hw_free =      snd_vt1724_pcm_hw_free,
876         .prepare =      snd_vt1724_capture_spdif_prepare,
877         .trigger =      snd_vt1724_pcm_trigger,
878         .pointer =      snd_vt1724_capture_spdif_pointer,
879 };
880
881
882 static int __devinit snd_vt1724_pcm_spdif(ice1712_t * ice, int device)
883 {
884         snd_pcm_t *pcm;
885         int play, capt;
886         int err;
887
888         if (ice->eeprom.data[ICE_EEP2_SPDIF] & VT1724_CFG_SPDIF_OUT_INT)
889                 play = 1;
890         else
891                 play = 0;
892         if (ice->eeprom.data[ICE_EEP2_SPDIF] & VT1724_CFG_SPDIF_IN)
893                 capt = 1;
894         else
895                 capt = 0;
896         if (! play && ! capt)
897                 return 0; /* no spdif device */
898
899         err = snd_pcm_new(ice->card, "ICE1724 IEC958", device, play, capt, &pcm);
900         if (err < 0)
901                 return err;
902
903         if (play)
904                 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
905                                 &snd_vt1724_playback_spdif_ops);
906         if (capt)
907                 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
908                                 &snd_vt1724_capture_spdif_ops);
909
910         pcm->private_data = ice;
911         pcm->info_flags = 0;
912         strcpy(pcm->name, "ICE1724 IEC958");
913
914         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
915                                               snd_dma_pci_data(ice->pci), 64*1024, 64*1024);
916
917         ice->pcm = pcm;
918
919         return 0;
920 }
921
922
923 /*
924  * independent surround PCMs
925  */
926
927 const static struct vt1724_pcm_reg vt1724_playback_dma_regs[3] = {
928         {
929                 .addr = VT1724_MT_PDMA1_ADDR,
930                 .size = VT1724_MT_PDMA1_SIZE,
931                 .count = VT1724_MT_PDMA1_COUNT,
932                 .start = VT1724_PDMA1_START,
933                 .pause = VT1724_PDMA1_PAUSE,
934         },
935         {
936                 .addr = VT1724_MT_PDMA2_ADDR,
937                 .size = VT1724_MT_PDMA2_SIZE,
938                 .count = VT1724_MT_PDMA2_COUNT,
939                 .start = VT1724_PDMA2_START,
940                 .pause = VT1724_PDMA2_PAUSE,
941         },
942         {
943                 .addr = VT1724_MT_PDMA3_ADDR,
944                 .size = VT1724_MT_PDMA3_SIZE,
945                 .count = VT1724_MT_PDMA3_COUNT,
946                 .start = VT1724_PDMA3_START,
947                 .pause = VT1724_PDMA3_PAUSE,
948         },
949 };
950
951 static int snd_vt1724_playback_indep_prepare(snd_pcm_substream_t * substream)
952 {
953         ice1712_t *ice = snd_pcm_substream_chip(substream);
954         unsigned char val;
955
956         spin_lock(&ice->reg_lock);
957         val = 3 - substream->number;
958         if (inb(ICEMT1724(ice, BURST)) < val)
959                 outb(val, ICEMT1724(ice, BURST));
960         spin_unlock(&ice->reg_lock);
961         return snd_vt1724_pcm_prepare(substream, &vt1724_playback_dma_regs[substream->number]);
962 }
963
964 static snd_pcm_uframes_t snd_vt1724_playback_indep_pointer(snd_pcm_substream_t * substream)
965 {
966         return snd_vt1724_pcm_pointer(substream, &vt1724_playback_dma_regs[substream->number]);
967 }
968
969 static int snd_vt1724_playback_indep_open(snd_pcm_substream_t *substream)
970 {
971         ice1712_t *ice = snd_pcm_substream_chip(substream);
972         snd_pcm_runtime_t *runtime = substream->runtime;
973
974         down(&ice->open_mutex);
975         /* already used by PDMA0? */
976         if (ice->pcm_reserved[substream->number]) {
977                 up(&ice->open_mutex);
978                 return -EBUSY; /* FIXME: should handle blocking mode properly */
979         }
980         up(&ice->open_mutex);
981         runtime->private_data = (void*)(1 << (substream->number + 4));
982         ice->playback_con_substream_ds[substream->number] = substream;
983         runtime->hw = snd_vt1724_2ch_stereo;
984         snd_pcm_set_sync(substream);
985         snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
986
987         snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates_192);
988         return 0;
989 }
990
991 static int snd_vt1724_playback_indep_close(snd_pcm_substream_t * substream)
992 {
993         ice1712_t *ice = snd_pcm_substream_chip(substream);
994
995         if (PRO_RATE_RESET)
996                 snd_vt1724_set_pro_rate(ice, PRO_RATE_DEFAULT, 0);
997         ice->playback_con_substream_ds[substream->number] = NULL;
998         ice->pcm_reserved[substream->number] = NULL;
999
1000         return 0;
1001 }
1002
1003 static snd_pcm_ops_t snd_vt1724_playback_indep_ops = {
1004         .open =         snd_vt1724_playback_indep_open,
1005         .close =        snd_vt1724_playback_indep_close,
1006         .ioctl =        snd_pcm_lib_ioctl,
1007         .hw_params =    snd_vt1724_pcm_hw_params,
1008         .hw_free =      snd_vt1724_pcm_hw_free,
1009         .prepare =      snd_vt1724_playback_indep_prepare,
1010         .trigger =      snd_vt1724_pcm_trigger,
1011         .pointer =      snd_vt1724_playback_indep_pointer,
1012 };
1013
1014
1015 static int __devinit snd_vt1724_pcm_indep(ice1712_t * ice, int device)
1016 {
1017         snd_pcm_t *pcm;
1018         int play;
1019         int err;
1020
1021         play = ice->num_total_dacs / 2 - 1;
1022         if (play <= 0)
1023                 return 0;
1024
1025         err = snd_pcm_new(ice->card, "ICE1724 Surrounds", device, play, 0, &pcm);
1026         if (err < 0)
1027                 return err;
1028
1029         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1030                         &snd_vt1724_playback_indep_ops);
1031
1032         pcm->private_data = ice;
1033         pcm->info_flags = 0;
1034         strcpy(pcm->name, "ICE1724 Surround PCM");
1035
1036         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1037                                               snd_dma_pci_data(ice->pci), 64*1024, 64*1024);
1038
1039         ice->pcm_ds = pcm;
1040
1041         return 0;
1042 }
1043
1044
1045 /*
1046  *  Mixer section
1047  */
1048
1049 static int __devinit snd_vt1724_ac97_mixer(ice1712_t * ice)
1050 {
1051         int err;
1052
1053         if (! (ice->eeprom.data[ICE_EEP2_ACLINK] & VT1724_CFG_PRO_I2S)) {
1054                 ac97_bus_t bus, *pbus;
1055                 ac97_t ac97;
1056                 /* cold reset */
1057                 outb(inb(ICEMT1724(ice, AC97_CMD)) | 0x80, ICEMT1724(ice, AC97_CMD));
1058                 mdelay(5); /* FIXME */
1059                 outb(inb(ICEMT1724(ice, AC97_CMD)) & ~0x80, ICEMT1724(ice, AC97_CMD));
1060
1061                 memset(&bus, 0, sizeof(bus));
1062                 bus.write = snd_vt1724_ac97_write;
1063                 bus.read = snd_vt1724_ac97_read;
1064                 if ((err = snd_ac97_bus(ice->card, &bus, &pbus)) < 0)
1065                         return err;
1066                 memset(&ac97, 0, sizeof(ac97));
1067                 ac97.private_data = ice;
1068                 if ((err = snd_ac97_mixer(pbus, &ac97, &ice->ac97)) < 0)
1069                         printk(KERN_WARNING "ice1712: cannot initialize pro ac97, skipped\n");
1070                 else
1071                         return 0;
1072         }
1073         /* I2S mixer only */
1074         strcat(ice->card->mixername, "ICE1724 - multitrack");
1075         return 0;
1076 }
1077
1078 /*
1079  *
1080  */
1081
1082 static inline unsigned int eeprom_triple(ice1712_t *ice, int idx)
1083 {
1084         return (unsigned int)ice->eeprom.data[idx] | \
1085                 ((unsigned int)ice->eeprom.data[idx + 1] << 8) | \
1086                 ((unsigned int)ice->eeprom.data[idx + 2] << 16);
1087 }
1088
1089 static void snd_vt1724_proc_read(snd_info_entry_t *entry, 
1090                                  snd_info_buffer_t * buffer)
1091 {
1092         ice1712_t *ice = snd_magic_cast(ice1712_t, entry->private_data, return);
1093         unsigned int idx;
1094
1095         snd_iprintf(buffer, "%s\n\n", ice->card->longname);
1096         snd_iprintf(buffer, "EEPROM:\n");
1097
1098         snd_iprintf(buffer, "  Subvendor        : 0x%x\n", ice->eeprom.subvendor);
1099         snd_iprintf(buffer, "  Size             : %i bytes\n", ice->eeprom.size);
1100         snd_iprintf(buffer, "  Version          : %i\n", ice->eeprom.version);
1101         snd_iprintf(buffer, "  System Config    : 0x%x\n", ice->eeprom.data[ICE_EEP2_SYSCONF]);
1102         snd_iprintf(buffer, "  ACLink           : 0x%x\n", ice->eeprom.data[ICE_EEP2_ACLINK]);
1103         snd_iprintf(buffer, "  I2S              : 0x%x\n", ice->eeprom.data[ICE_EEP2_I2S]);
1104         snd_iprintf(buffer, "  S/PDIF           : 0x%x\n", ice->eeprom.data[ICE_EEP2_SPDIF]);
1105         snd_iprintf(buffer, "  GPIO direction   : 0x%x\n", ice->eeprom.gpiodir);
1106         snd_iprintf(buffer, "  GPIO mask        : 0x%x\n", ice->eeprom.gpiomask);
1107         snd_iprintf(buffer, "  GPIO state       : 0x%x\n", ice->eeprom.gpiostate);
1108         for (idx = 0x12; idx < ice->eeprom.size; idx++)
1109                 snd_iprintf(buffer, "  Extra #%02i        : 0x%x\n", idx, ice->eeprom.data[idx]);
1110
1111         snd_iprintf(buffer, "\nRegisters:\n");
1112
1113         snd_iprintf(buffer, "  PSDOUT03 : 0x%08x\n", (unsigned)inl(ICEMT1724(ice, ROUTE_PLAYBACK)));
1114         for (idx = 0x0; idx < 0x20 ; idx++)
1115                 snd_iprintf(buffer, "  CCS%02x    : 0x%02x\n", idx, inb(ice->port+idx));
1116         for (idx = 0x0; idx < 0x30 ; idx++)
1117                 snd_iprintf(buffer, "  MT%02x     : 0x%02x\n", idx, inb(ice->profi_port+idx));
1118 }
1119
1120 static void __devinit snd_vt1724_proc_init(ice1712_t * ice)
1121 {
1122         snd_info_entry_t *entry;
1123
1124         if (! snd_card_proc_new(ice->card, "ice1724", &entry))
1125                 snd_info_set_text_ops(entry, ice, 1024, snd_vt1724_proc_read);
1126 }
1127
1128 /*
1129  *
1130  */
1131
1132 static int snd_vt1724_eeprom_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1133 {
1134         uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
1135         uinfo->count = sizeof(ice1712_eeprom_t);
1136         return 0;
1137 }
1138
1139 static int snd_vt1724_eeprom_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1140 {
1141         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1142         
1143         memcpy(ucontrol->value.bytes.data, &ice->eeprom, sizeof(ice->eeprom));
1144         return 0;
1145 }
1146
1147 static snd_kcontrol_new_t snd_vt1724_eeprom __devinitdata = {
1148         .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1149         .name = "ICE1724 EEPROM",
1150         .access = SNDRV_CTL_ELEM_ACCESS_READ,
1151         .info = snd_vt1724_eeprom_info,
1152         .get = snd_vt1724_eeprom_get
1153 };
1154
1155 /*
1156  */
1157 static int snd_vt1724_spdif_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1158 {
1159         uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1160         uinfo->count = 1;
1161         return 0;
1162 }
1163
1164 static unsigned int encode_spdif_bits(snd_aes_iec958_t *diga)
1165 {
1166         unsigned int val;
1167
1168         val = diga->status[0] & 0x03; /* professional, non-audio */
1169         if (val & 0x01) {
1170                 /* professional */
1171                 if ((diga->status[0] & IEC958_AES0_PRO_EMPHASIS) == IEC958_AES0_PRO_EMPHASIS_5015)
1172                         val |= 1U << 3;
1173                 switch (diga->status[0] & IEC958_AES0_PRO_FS) {
1174                 case IEC958_AES0_PRO_FS_44100:
1175                         break;
1176                 case IEC958_AES0_PRO_FS_32000:
1177                         val |= 3U << 12;
1178                         break;
1179                 default:
1180                         val |= 2U << 12;
1181                         break;
1182                 }
1183         } else {
1184                 /* consumer */
1185                 val |= diga->status[0] & 0x04; /* copyright */
1186                 if ((diga->status[0] & IEC958_AES0_CON_EMPHASIS)== IEC958_AES0_CON_EMPHASIS_5015)
1187                         val |= 1U << 3;
1188                 val |= (unsigned int)(diga->status[1] & 0x3f) << 4; /* category */
1189                 val |= (unsigned int)(diga->status[3] & IEC958_AES3_CON_FS) << 12; /* fs */
1190         }
1191         return val;
1192 }
1193
1194 static void decode_spdif_bits(snd_aes_iec958_t *diga, unsigned int val)
1195 {
1196         memset(diga->status, 0, sizeof(diga->status));
1197         diga->status[0] = val & 0x03; /* professional, non-audio */
1198         if (val & 0x01) {
1199                 /* professional */
1200                 if (val & (1U << 3))
1201                         diga->status[0] |= IEC958_AES0_PRO_EMPHASIS_5015;
1202                 switch ((val >> 12) & 0x7) {
1203                 case 0:
1204                         break;
1205                 case 2:
1206                         diga->status[0] |= IEC958_AES0_PRO_FS_32000;
1207                         break;
1208                 default:
1209                         diga->status[0] |= IEC958_AES0_PRO_FS_48000;
1210                         break;
1211                 }
1212         } else {
1213                 /* consumer */
1214                 diga->status[0] |= val & (1U << 2); /* copyright */
1215                 if (val & (1U << 3))
1216                         diga->status[0] |= IEC958_AES0_CON_EMPHASIS_5015;
1217                 diga->status[1] |= (val >> 4) & 0x3f; /* category */
1218                 diga->status[3] |= (val >> 12) & 0x07; /* fs */
1219         }
1220 }
1221
1222 static int snd_vt1724_spdif_default_get(snd_kcontrol_t * kcontrol,
1223                                         snd_ctl_elem_value_t * ucontrol)
1224 {
1225         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1226         unsigned int val;
1227         val = inw(ICEMT1724(ice, SPDIF_CTRL));
1228         decode_spdif_bits(&ucontrol->value.iec958, val);
1229         return 0;
1230 }
1231
1232 static int snd_vt1724_spdif_default_put(snd_kcontrol_t * kcontrol,
1233                                          snd_ctl_elem_value_t * ucontrol)
1234 {
1235         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1236         unsigned int val, old;
1237         unsigned long flags;
1238
1239         val = encode_spdif_bits(&ucontrol->value.iec958);
1240         spin_lock_irqsave(&ice->reg_lock, flags);
1241         old = inw(ICEMT1724(ice, SPDIF_CTRL));
1242         if (val != old) {
1243                 unsigned char cbit, disabled;
1244                 cbit = inb(ICEREG1724(ice, SPDIF_CFG));
1245                 disabled = cbit & ~VT1724_CFG_SPDIF_OUT_EN;
1246                 if (cbit != disabled)
1247                         outb(disabled, ICEREG1724(ice, SPDIF_CFG));
1248                 outw(val, ICEMT1724(ice, SPDIF_CTRL));
1249                 if (cbit != disabled)
1250                         outb(cbit, ICEREG1724(ice, SPDIF_CFG));
1251         }
1252         spin_unlock_irqrestore(&ice->reg_lock, flags);
1253         return (val != old);
1254 }
1255
1256 static snd_kcontrol_new_t snd_vt1724_spdif_default __devinitdata =
1257 {
1258         .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
1259         .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1260         .info =         snd_vt1724_spdif_info,
1261         .get =          snd_vt1724_spdif_default_get,
1262         .put =          snd_vt1724_spdif_default_put
1263 };
1264
1265 static int snd_vt1724_spdif_maskc_get(snd_kcontrol_t * kcontrol,
1266                                        snd_ctl_elem_value_t * ucontrol)
1267 {
1268         ucontrol->value.iec958.status[0] = IEC958_AES0_NONAUDIO |
1269                                                      IEC958_AES0_PROFESSIONAL |
1270                                                      IEC958_AES0_CON_NOT_COPYRIGHT |
1271                                                      IEC958_AES0_CON_EMPHASIS;
1272         ucontrol->value.iec958.status[1] = IEC958_AES1_CON_ORIGINAL |
1273                                                      IEC958_AES1_CON_CATEGORY;
1274         ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS;
1275         return 0;
1276 }
1277
1278 static int snd_vt1724_spdif_maskp_get(snd_kcontrol_t * kcontrol,
1279                                        snd_ctl_elem_value_t * ucontrol)
1280 {
1281         ucontrol->value.iec958.status[0] = IEC958_AES0_NONAUDIO |
1282                                                      IEC958_AES0_PROFESSIONAL |
1283                                                      IEC958_AES0_PRO_FS |
1284                                                      IEC958_AES0_PRO_EMPHASIS;
1285         return 0;
1286 }
1287
1288 static snd_kcontrol_new_t snd_vt1724_spdif_maskc __devinitdata =
1289 {
1290         .access =       SNDRV_CTL_ELEM_ACCESS_READ,
1291         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
1292         .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1293         .info =         snd_vt1724_spdif_info,
1294         .get =          snd_vt1724_spdif_maskc_get,
1295 };
1296
1297 static snd_kcontrol_new_t snd_vt1724_spdif_maskp __devinitdata =
1298 {
1299         .access =       SNDRV_CTL_ELEM_ACCESS_READ,
1300         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
1301         .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
1302         .info =         snd_vt1724_spdif_info,
1303         .get =          snd_vt1724_spdif_maskp_get,
1304 };
1305
1306 static int snd_vt1724_spdif_sw_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1307 {
1308         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1309         uinfo->count = 1;
1310         uinfo->value.integer.min = 0;
1311         uinfo->value.integer.max = 1;
1312         return 0;
1313 }
1314
1315 static int snd_vt1724_spdif_sw_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1316 {
1317         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1318         ucontrol->value.integer.value[0] = inb(ICEREG1724(ice, SPDIF_CFG)) & VT1724_CFG_SPDIF_OUT_EN ? 1 : 0;
1319         return 0;
1320 }
1321
1322 static int snd_vt1724_spdif_sw_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1323 {
1324         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1325         unsigned char old, val;
1326         unsigned long flags;
1327         spin_lock_irqsave(&ice->reg_lock, flags);
1328         old = val = inb(ICEREG1724(ice, SPDIF_CFG));
1329         val &= ~VT1724_CFG_SPDIF_OUT_EN;
1330         if (ucontrol->value.integer.value[0])
1331                 val |= VT1724_CFG_SPDIF_OUT_EN;
1332         if (old != val)
1333                 outb(val, ICEREG1724(ice, SPDIF_CFG));
1334         spin_unlock_irqrestore(&ice->reg_lock, flags);
1335         return old != val;
1336 }
1337
1338 static snd_kcontrol_new_t snd_vt1724_spdif_switch __devinitdata =
1339 {
1340         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
1341         /* FIXME: the following conflict with IEC958 Playback Route */
1342         // .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
1343         .name =         "IEC958 Output Switch",
1344         .info =         snd_vt1724_spdif_sw_info,
1345         .get =          snd_vt1724_spdif_sw_get,
1346         .put =          snd_vt1724_spdif_sw_put
1347 };
1348
1349
1350 #if 0 /* NOT USED YET */
1351 /*
1352  * GPIO access from extern
1353  */
1354
1355 int snd_vt1724_gpio_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1356 {
1357         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1358         uinfo->count = 1;
1359         uinfo->value.integer.min = 0;
1360         uinfo->value.integer.max = 1;
1361         return 0;
1362 }
1363
1364 int snd_vt1724_gpio_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1365 {
1366         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1367         int shift = kcontrol->private_value & 0xff;
1368         int invert = (kcontrol->private_value & (1<<24)) ? 1 : 0;
1369         
1370         snd_ice1712_save_gpio_status(ice);
1371         ucontrol->value.integer.value[0] = (snd_ice1712_gpio_read(ice) & (1 << shift) ? 1 : 0) ^ invert;
1372         snd_ice1712_restore_gpio_status(ice);
1373         return 0;
1374 }
1375
1376 int snd_ice1712_gpio_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1377 {
1378         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1379         int shift = kcontrol->private_value & 0xff;
1380         int invert = (kcontrol->private_value & (1<<24)) ? mask : 0;
1381         unsigned int val, nval;
1382
1383         if (kcontrol->private_value & (1 << 31))
1384                 return -EPERM;
1385         nval = (ucontrol->value.integer.value[0] ? (1 << shift) : 0) ^ invert;
1386         snd_ice1712_save_gpio_status(ice);
1387         val = snd_ice1712_gpio_read(ice);
1388         nval |= val & ~(1 << shift);
1389         if (val != nval)
1390                 snd_ice1712_gpio_write(ice, nval);
1391         snd_ice1712_restore_gpio_status(ice);
1392         return val != nval;
1393 }
1394 #endif /* NOT USED YET */
1395
1396 /*
1397  *  rate
1398  */
1399 static int snd_vt1724_pro_internal_clock_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1400 {
1401         static char *texts[] = {
1402                 "8000",         /* 0: 6 */
1403                 "9600",         /* 1: 3 */
1404                 "11025",        /* 2: 10 */
1405                 "12000",        /* 3: 2 */
1406                 "16000",        /* 4: 5 */
1407                 "22050",        /* 5: 9 */
1408                 "24000",        /* 6: 1 */
1409                 "32000",        /* 7: 4 */
1410                 "44100",        /* 8: 8 */
1411                 "48000",        /* 9: 0 */
1412                 "64000",        /* 10: 15 */
1413                 "88200",        /* 11: 11 */
1414                 "96000",        /* 12: 7 */
1415                 "176400",       /* 13: 12 */
1416                 "192000",       /* 14: 14 */
1417                 "IEC958 Input", /* 15: -- */
1418         };
1419         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1420         uinfo->count = 1;
1421         uinfo->value.enumerated.items = 16;
1422         if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1423                 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
1424         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1425         return 0;
1426 }
1427
1428 static int snd_vt1724_pro_internal_clock_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1429 {
1430         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1431         static unsigned char xlate[16] = {
1432                 9, 6, 3, 1, 7, 4, 0, 12, 8, 5, 2, 11, 13, 255, 14, 10
1433         };
1434         unsigned char val;
1435         
1436         spin_lock_irq(&ice->reg_lock);
1437         if (is_spdif_master(ice)) {
1438                 ucontrol->value.enumerated.item[0] = 15;
1439         } else {
1440                 val = xlate[inb(ICEMT1724(ice, RATE)) & 15];
1441                 if (val == 255) {
1442                         snd_BUG();
1443                         val = 0;
1444                 }
1445                 ucontrol->value.enumerated.item[0] = val;
1446         }
1447         spin_unlock_irq(&ice->reg_lock);
1448         return 0;
1449 }
1450
1451 static int snd_vt1724_pro_internal_clock_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1452 {
1453         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1454         unsigned char oval;
1455         int change = 0;
1456
1457         spin_lock_irq(&ice->reg_lock);
1458         oval = inb(ICEMT1724(ice, RATE));
1459         if (ucontrol->value.enumerated.item[0] == 15) {
1460                 outb(oval | VT1724_SPDIF_MASTER, ICEMT1724(ice, RATE));
1461         } else {
1462                 PRO_RATE_DEFAULT = rates[ucontrol->value.integer.value[0] % 15];
1463                 spin_unlock_irq(&ice->reg_lock);
1464                 snd_vt1724_set_pro_rate(ice, PRO_RATE_DEFAULT, 1);
1465                 spin_lock_irq(&ice->reg_lock);
1466         }
1467         change = inb(ICEMT1724(ice, RATE)) != oval;
1468         spin_unlock_irq(&ice->reg_lock);
1469
1470         if ((oval & VT1724_SPDIF_MASTER) != (inb(ICEMT1724(ice, RATE)) & VT1724_SPDIF_MASTER)) {
1471                 /* notify akm chips as well */
1472                 if (is_spdif_master(ice)) {
1473                         unsigned int i;
1474                         for (i = 0; i < ice->akm_codecs; i++) {
1475                                 if (ice->akm[i].ops.set_rate_val)
1476                                         ice->akm[i].ops.set_rate_val(&ice->akm[i], 0);
1477                         }
1478                 }
1479         }
1480         return change;
1481 }
1482
1483 static snd_kcontrol_new_t snd_vt1724_pro_internal_clock __devinitdata = {
1484         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1485         .name = "Multi Track Internal Clock",
1486         .info = snd_vt1724_pro_internal_clock_info,
1487         .get = snd_vt1724_pro_internal_clock_get,
1488         .put = snd_vt1724_pro_internal_clock_put
1489 };
1490
1491 static int snd_vt1724_pro_rate_locking_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1492 {
1493         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1494         uinfo->count = 1;
1495         uinfo->value.integer.min = 0;
1496         uinfo->value.integer.max = 1;
1497         return 0;
1498 }
1499
1500 static int snd_vt1724_pro_rate_locking_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1501 {
1502         ucontrol->value.integer.value[0] = PRO_RATE_LOCKED;
1503         return 0;
1504 }
1505
1506 static int snd_vt1724_pro_rate_locking_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1507 {
1508         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1509         int change = 0, nval;
1510
1511         nval = ucontrol->value.integer.value[0] ? 1 : 0;
1512         spin_lock_irq(&ice->reg_lock);
1513         change = PRO_RATE_LOCKED != nval;
1514         PRO_RATE_LOCKED = nval;
1515         spin_unlock_irq(&ice->reg_lock);
1516         return change;
1517 }
1518
1519 static snd_kcontrol_new_t snd_vt1724_pro_rate_locking __devinitdata = {
1520         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1521         .name = "Multi Track Rate Locking",
1522         .info = snd_vt1724_pro_rate_locking_info,
1523         .get = snd_vt1724_pro_rate_locking_get,
1524         .put = snd_vt1724_pro_rate_locking_put
1525 };
1526
1527 static int snd_vt1724_pro_rate_reset_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1528 {
1529         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1530         uinfo->count = 1;
1531         uinfo->value.integer.min = 0;
1532         uinfo->value.integer.max = 1;
1533         return 0;
1534 }
1535
1536 static int snd_vt1724_pro_rate_reset_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1537 {
1538         ucontrol->value.integer.value[0] = PRO_RATE_RESET ? 1 : 0;
1539         return 0;
1540 }
1541
1542 static int snd_vt1724_pro_rate_reset_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1543 {
1544         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1545         int change = 0, nval;
1546
1547         nval = ucontrol->value.integer.value[0] ? 1 : 0;
1548         spin_lock_irq(&ice->reg_lock);
1549         change = PRO_RATE_RESET != nval;
1550         PRO_RATE_RESET = nval;
1551         spin_unlock_irq(&ice->reg_lock);
1552         return change;
1553 }
1554
1555 static snd_kcontrol_new_t snd_vt1724_pro_rate_reset __devinitdata = {
1556         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1557         .name = "Multi Track Rate Reset",
1558         .info = snd_vt1724_pro_rate_reset_info,
1559         .get = snd_vt1724_pro_rate_reset_get,
1560         .put = snd_vt1724_pro_rate_reset_put
1561 };
1562
1563
1564 /*
1565  * routing
1566  */
1567 static int snd_vt1724_pro_route_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1568 {
1569         static char *texts[] = {
1570                 "PCM Out", /* 0 */
1571                 "H/W In 0", "H/W In 1", /* 1-2 */
1572                 "IEC958 In L", "IEC958 In R", /* 3-4 */
1573         };
1574         
1575         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1576         uinfo->count = 1;
1577         uinfo->value.enumerated.items = 5;
1578         if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1579                 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
1580         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1581         return 0;
1582 }
1583
1584 static inline int analog_route_shift(int idx)
1585 {
1586         return (idx % 2) * 12 + ((idx / 2) * 3) + 8;
1587 }
1588
1589 static inline int digital_route_shift(int idx)
1590 {
1591         return idx * 3;
1592 }
1593
1594 static int get_route_val(ice1712_t *ice, int shift)
1595 {
1596         unsigned long val;
1597         unsigned char eitem;
1598         static unsigned char xlate[8] = {
1599                 0, 255, 1, 2, 255, 255, 3, 4,
1600         };
1601
1602         val = inl(ICEMT1724(ice, ROUTE_PLAYBACK));
1603         val >>= shift;
1604         val &= 7;       //we now have 3 bits per output
1605         eitem = xlate[val];
1606         if (eitem == 255) {
1607                 snd_BUG();
1608                 return 0;
1609         }
1610         return eitem;
1611 }
1612
1613 static int put_route_val(ice1712_t *ice, unsigned int val, int shift)
1614 {
1615         unsigned int old_val, nval;
1616         int change;
1617         static unsigned char xroute[8] = {
1618                 0, /* PCM */
1619                 2, /* PSDIN0 Left */
1620                 3, /* PSDIN0 Right */
1621                 6, /* SPDIN Left */
1622                 7, /* SPDIN Right */
1623         };
1624
1625         nval = xroute[val % 5];
1626         val = old_val = inl(ICEMT1724(ice, ROUTE_PLAYBACK));
1627         val &= ~(0x07 << shift);
1628         val |= nval << shift;
1629         change = val != old_val;
1630         if (change)
1631                 outl(val, ICEMT1724(ice, ROUTE_PLAYBACK));
1632         return change;
1633 }
1634
1635 static int snd_vt1724_pro_route_analog_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t *ucontrol)
1636 {
1637         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1638         int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1639         ucontrol->value.enumerated.item[0] = get_route_val(ice, analog_route_shift(idx));
1640         return 0;
1641 }
1642
1643 static int snd_vt1724_pro_route_analog_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t *ucontrol)
1644 {
1645         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1646         int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1647         return put_route_val(ice, ucontrol->value.enumerated.item[0],
1648                              analog_route_shift(idx));
1649 }
1650
1651 static int snd_vt1724_pro_route_spdif_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t *ucontrol)
1652 {
1653         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1654         int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1655         ucontrol->value.enumerated.item[0] = get_route_val(ice, digital_route_shift(idx));
1656         return 0;
1657 }
1658
1659 static int snd_vt1724_pro_route_spdif_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t *ucontrol)
1660 {
1661         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1662         int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1663         return put_route_val(ice, ucontrol->value.enumerated.item[0],
1664                              digital_route_shift(idx));
1665 }
1666
1667 static snd_kcontrol_new_t snd_vt1724_mixer_pro_analog_route __devinitdata = {
1668         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1669         .name = "H/W Playback Route",
1670         .info = snd_vt1724_pro_route_info,
1671         .get = snd_vt1724_pro_route_analog_get,
1672         .put = snd_vt1724_pro_route_analog_put,
1673 };
1674
1675 static snd_kcontrol_new_t snd_vt1724_mixer_pro_spdif_route __devinitdata = {
1676         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1677         .name = "IEC958 Playback Route",
1678         .info = snd_vt1724_pro_route_info,
1679         .get = snd_vt1724_pro_route_spdif_get,
1680         .put = snd_vt1724_pro_route_spdif_put,
1681         .count = 2,
1682 };
1683
1684
1685 static int snd_vt1724_pro_peak_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1686 {
1687         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1688         uinfo->count = 22; /* FIXME: for compatibility with ice1712... */
1689         uinfo->value.integer.min = 0;
1690         uinfo->value.integer.max = 255;
1691         return 0;
1692 }
1693
1694 static int snd_vt1724_pro_peak_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1695 {
1696         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1697         int idx;
1698         
1699         spin_lock_irq(&ice->reg_lock);
1700         for (idx = 0; idx < 22; idx++) {
1701                 outb(idx, ICEMT1724(ice, MONITOR_PEAKINDEX));
1702                 ucontrol->value.integer.value[idx] = inb(ICEMT1724(ice, MONITOR_PEAKDATA));
1703         }
1704         spin_unlock_irq(&ice->reg_lock);
1705         return 0;
1706 }
1707
1708 static snd_kcontrol_new_t snd_vt1724_mixer_pro_peak __devinitdata = {
1709         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1710         .name = "Multi Track Peak",
1711         .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
1712         .info = snd_vt1724_pro_peak_info,
1713         .get = snd_vt1724_pro_peak_get
1714 };
1715
1716 /*
1717  *
1718  */
1719
1720 static struct snd_ice1712_card_info no_matched __devinitdata;
1721
1722 static struct snd_ice1712_card_info *card_tables[] __devinitdata = {
1723         snd_vt1724_revo_cards,
1724         snd_vt1724_amp_cards, 
1725         snd_vt1724_aureon_cards,
1726         snd_vt1724_prodigy_cards,
1727         0,
1728 };
1729
1730
1731 /*
1732  */
1733
1734 static unsigned char __devinit snd_vt1724_read_i2c(ice1712_t *ice,
1735                                                  unsigned char dev,
1736                                                  unsigned char addr)
1737 {
1738         long t = 0x10000;
1739
1740         outb(addr, ICEREG1724(ice, I2C_BYTE_ADDR));
1741         outb(dev & ~VT1724_I2C_WRITE, ICEREG1724(ice, I2C_DEV_ADDR));
1742         while (t-- > 0 && (inb(ICEREG1724(ice, I2C_CTRL)) & VT1724_I2C_BUSY)) ;
1743         return inb(ICEREG1724(ice, I2C_DATA));
1744 }
1745
1746 static int __devinit snd_vt1724_read_eeprom(ice1712_t *ice)
1747 {
1748         int dev = 0xa0;         /* EEPROM device address */
1749         unsigned int i, size;
1750         struct snd_ice1712_card_info **tbl, *c;
1751
1752         if ((inb(ICEREG1724(ice, I2C_CTRL)) & VT1724_I2C_EEPROM) == 0) {
1753                 snd_printk(KERN_WARNING "ICE1724 has not detected EEPROM\n");
1754                 // return -EIO;
1755         }
1756         ice->eeprom.subvendor = (snd_vt1724_read_i2c(ice, dev, 0x00) << 0) |
1757                                 (snd_vt1724_read_i2c(ice, dev, 0x01) << 8) | 
1758                                 (snd_vt1724_read_i2c(ice, dev, 0x02) << 16) | 
1759                                 (snd_vt1724_read_i2c(ice, dev, 0x03) << 24);
1760
1761         /* if the EEPROM is given by the driver, use it */
1762         for (tbl = card_tables; *tbl; tbl++) {
1763                 for (c = *tbl; c->subvendor; c++) {
1764                         if (c->subvendor == ice->eeprom.subvendor) {
1765                                 if (! c->eeprom_size || ! c->eeprom_data)
1766                                         goto found;
1767                                 snd_printdd("using the defined eeprom..\n");
1768                                 ice->eeprom.version = 2;
1769                                 ice->eeprom.size = c->eeprom_size + 6;
1770                                 memcpy(ice->eeprom.data, c->eeprom_data, c->eeprom_size);
1771                                 goto read_skipped;
1772                         }
1773                 }
1774         }
1775
1776  found:
1777         ice->eeprom.size = snd_vt1724_read_i2c(ice, dev, 0x04);
1778         if (ice->eeprom.size < 6)
1779                 ice->eeprom.size = 32;
1780         else if (ice->eeprom.size > 32) {
1781                 snd_printk("invalid EEPROM (size = %i)\n", ice->eeprom.size);
1782                 return -EIO;
1783         }
1784         ice->eeprom.version = snd_vt1724_read_i2c(ice, dev, 0x05);
1785         if (ice->eeprom.version != 2) {
1786                 snd_printk("invalid EEPROM version %i\n", ice->eeprom.version);
1787                 // return -EIO;
1788         }
1789         size = ice->eeprom.size - 6;
1790         for (i = 0; i < size; i++)
1791                 ice->eeprom.data[i] = snd_vt1724_read_i2c(ice, dev, i + 6);
1792
1793  read_skipped:
1794         ice->eeprom.gpiomask = eeprom_triple(ice, ICE_EEP2_GPIO_MASK);
1795         ice->eeprom.gpiostate = eeprom_triple(ice, ICE_EEP2_GPIO_STATE);
1796         ice->eeprom.gpiodir = eeprom_triple(ice, ICE_EEP2_GPIO_DIR);
1797
1798         return 0;
1799 }
1800
1801
1802
1803 static int __devinit snd_vt1724_chip_init(ice1712_t *ice)
1804 {
1805         outb(VT1724_RESET , ICEREG1724(ice, CONTROL));
1806         udelay(200);
1807         outb(0, ICEREG1724(ice, CONTROL));
1808         udelay(200);
1809         outb(ice->eeprom.data[ICE_EEP2_SYSCONF], ICEREG1724(ice, SYS_CFG));
1810         outb(ice->eeprom.data[ICE_EEP2_ACLINK], ICEREG1724(ice, AC97_CFG));
1811         outb(ice->eeprom.data[ICE_EEP2_I2S], ICEREG1724(ice, I2S_FEATURES));
1812         outb(ice->eeprom.data[ICE_EEP2_SPDIF], ICEREG1724(ice, SPDIF_CFG));
1813
1814         ice->gpio.write_mask = ice->eeprom.gpiomask;
1815         ice->gpio.direction = ice->eeprom.gpiodir;
1816         snd_vt1724_set_gpio_mask(ice, ice->eeprom.gpiomask);
1817         snd_vt1724_set_gpio_dir(ice, ice->eeprom.gpiodir);
1818         snd_vt1724_set_gpio_data(ice, ice->eeprom.gpiostate);
1819
1820         outb(0, ICEREG1724(ice, POWERDOWN));
1821
1822         return 0;
1823 }
1824
1825 static int __devinit snd_vt1724_spdif_build_controls(ice1712_t *ice)
1826 {
1827         int err;
1828         snd_kcontrol_t *kctl;
1829
1830         snd_assert(ice->pcm != NULL, return -EIO);
1831
1832         err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_mixer_pro_spdif_route, ice));
1833         if (err < 0)
1834                 return err;
1835
1836         err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_spdif_switch, ice));
1837         if (err < 0)
1838                 return err;
1839
1840         err = snd_ctl_add(ice->card, kctl = snd_ctl_new1(&snd_vt1724_spdif_default, ice));
1841         if (err < 0)
1842                 return err;
1843         kctl->id.device = ice->pcm->device;
1844         err = snd_ctl_add(ice->card, kctl = snd_ctl_new1(&snd_vt1724_spdif_maskc, ice));
1845         if (err < 0)
1846                 return err;
1847         kctl->id.device = ice->pcm->device;
1848         err = snd_ctl_add(ice->card, kctl = snd_ctl_new1(&snd_vt1724_spdif_maskp, ice));
1849         if (err < 0)
1850                 return err;
1851         kctl->id.device = ice->pcm->device;
1852 #if 0 /* use default only */
1853         err = snd_ctl_add(ice->card, kctl = snd_ctl_new1(&snd_vt1724_spdif_stream, ice));
1854         if (err < 0)
1855                 return err;
1856         kctl->id.device = ice->pcm->device;
1857         ice->spdif.stream_ctl = kctl;
1858 #endif
1859         return 0;
1860 }
1861
1862
1863 static int __devinit snd_vt1724_build_controls(ice1712_t *ice)
1864 {
1865         int err;
1866
1867         err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_eeprom, ice));
1868         if (err < 0)
1869                 return err;
1870         err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_pro_internal_clock, ice));
1871         if (err < 0)
1872                 return err;
1873
1874         err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_pro_rate_locking, ice));
1875         if (err < 0)
1876                 return err;
1877         err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_pro_rate_reset, ice));
1878         if (err < 0)
1879                 return err;
1880
1881         if (ice->num_total_dacs > 0) {
1882                 snd_kcontrol_new_t tmp = snd_vt1724_mixer_pro_analog_route;
1883                 tmp.count = ice->num_total_dacs;
1884                 err = snd_ctl_add(ice->card, snd_ctl_new1(&tmp, ice));
1885                 if (err < 0)
1886                         return err;
1887         }
1888
1889         err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_mixer_pro_peak, ice));
1890         if (err < 0)
1891                 return err;
1892
1893         return 0;
1894 }
1895
1896 static int snd_vt1724_free(ice1712_t *ice)
1897 {
1898         if (ice->res_port == NULL)
1899                 goto __hw_end;
1900         /* mask all interrupts */
1901         outb(0xff, ICEMT1724(ice, DMA_INT_MASK));
1902         outb(0xff, ICEREG1724(ice, IRQMASK));
1903         /* --- */
1904       __hw_end:
1905         if (ice->irq >= 0) {
1906                 synchronize_irq(ice->irq);
1907                 free_irq(ice->irq, (void *) ice);
1908         }
1909         if (ice->res_port) {
1910                 release_resource(ice->res_port);
1911                 kfree_nocheck(ice->res_port);
1912         }
1913         if (ice->res_profi_port) {
1914                 release_resource(ice->res_profi_port);
1915                 kfree_nocheck(ice->res_profi_port);
1916         }
1917         snd_ice1712_akm4xxx_free(ice);
1918         snd_magic_kfree(ice);
1919         return 0;
1920 }
1921
1922 static int snd_vt1724_dev_free(snd_device_t *device)
1923 {
1924         ice1712_t *ice = snd_magic_cast(ice1712_t, device->device_data, return -ENXIO);
1925         return snd_vt1724_free(ice);
1926 }
1927
1928 static int __devinit snd_vt1724_create(snd_card_t * card,
1929                                        struct pci_dev *pci,
1930                                        ice1712_t ** r_ice1712)
1931 {
1932         ice1712_t *ice;
1933         int err;
1934         unsigned char mask;
1935         static snd_device_ops_t ops = {
1936                 .dev_free =     snd_vt1724_dev_free,
1937         };
1938
1939         *r_ice1712 = NULL;
1940
1941         /* enable PCI device */
1942         if ((err = pci_enable_device(pci)) < 0)
1943                 return err;
1944
1945         ice = snd_magic_kcalloc(ice1712_t, 0, GFP_KERNEL);
1946         if (ice == NULL)
1947                 return -ENOMEM;
1948         ice->vt1724 = 1;
1949         spin_lock_init(&ice->reg_lock);
1950         init_MUTEX(&ice->gpio_mutex);
1951         init_MUTEX(&ice->open_mutex);
1952         ice->gpio.set_mask = snd_vt1724_set_gpio_mask;
1953         ice->gpio.set_dir = snd_vt1724_set_gpio_dir;
1954         ice->gpio.set_data = snd_vt1724_set_gpio_data;
1955         ice->gpio.get_data = snd_vt1724_get_gpio_data;
1956         ice->card = card;
1957         ice->pci = pci;
1958         ice->irq = -1;
1959         ice->port = pci_resource_start(pci, 0);
1960         ice->profi_port = pci_resource_start(pci, 1);
1961         pci_set_master(pci);
1962         snd_vt1724_proc_init(ice);
1963         synchronize_irq(pci->irq);
1964
1965         if ((ice->res_port = request_region(ice->port, 32, "ICE1724 - Controller")) == NULL) {
1966                 snd_printk("unable to grab ports 0x%lx-0x%lx\n", ice->port, ice->port + 32 - 1);
1967                 snd_vt1724_free(ice);
1968                 return -EIO;
1969         }
1970
1971         if ((ice->res_profi_port = request_region(ice->profi_port, 128, "ICE1724 - Professional")) == NULL) {
1972                 snd_printk("unable to grab ports 0x%lx-0x%lx\n", ice->profi_port, ice->profi_port + 16 - 1);
1973                 snd_vt1724_free(ice);
1974                 return -EIO;
1975         }
1976                 
1977         if (request_irq(pci->irq, snd_vt1724_interrupt, SA_INTERRUPT|SA_SHIRQ, "ICE1724", (void *) ice)) {
1978                 snd_printk("unable to grab IRQ %d\n", pci->irq);
1979                 snd_vt1724_free(ice);
1980                 return -EIO;
1981         }
1982
1983         ice->irq = pci->irq;
1984
1985         if (snd_vt1724_read_eeprom(ice) < 0) {
1986                 snd_vt1724_free(ice);
1987                 return -EIO;
1988         }
1989         if (snd_vt1724_chip_init(ice) < 0) {
1990                 snd_vt1724_free(ice);
1991                 return -EIO;
1992         }
1993
1994         /* unmask used interrupts */
1995         if (! (ice->eeprom.data[ICE_EEP2_SYSCONF] & VT1724_CFG_MPU401))
1996                 mask = VT1724_IRQ_MPU_RX | VT1724_IRQ_MPU_TX;
1997         else
1998                 mask = 0;
1999         outb(mask, ICEREG1724(ice, IRQMASK));
2000         /* don't handle FIFO overrun/underruns (just yet), since they cause machine lockups */
2001         outb(VT1724_MULTI_FIFO_ERR, ICEMT1724(ice, DMA_INT_MASK));
2002
2003         if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, ice, &ops)) < 0) {
2004                 snd_vt1724_free(ice);
2005                 return err;
2006         }
2007
2008         snd_card_set_dev(card, &pci->dev);
2009
2010         *r_ice1712 = ice;
2011         return 0;
2012 }
2013
2014
2015 /*
2016  *
2017  * Registration
2018  *
2019  */
2020
2021 static int __devinit snd_vt1724_probe(struct pci_dev *pci,
2022                                       const struct pci_device_id *pci_id)
2023 {
2024         static int dev;
2025         snd_card_t *card;
2026         ice1712_t *ice;
2027         int pcm_dev = 0, err;
2028         struct snd_ice1712_card_info **tbl, *c;
2029
2030         if (dev >= SNDRV_CARDS)
2031                 return -ENODEV;
2032         if (!enable[dev]) {
2033                 dev++;
2034                 return -ENOENT;
2035         }
2036
2037         card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
2038         if (card == NULL)
2039                 return -ENOMEM;
2040
2041         strcpy(card->driver, "ICE1724");
2042         strcpy(card->shortname, "ICEnsemble ICE1724");
2043         
2044         if ((err = snd_vt1724_create(card, pci, &ice)) < 0) {
2045                 snd_card_free(card);
2046                 return err;
2047         }
2048
2049         for (tbl = card_tables; *tbl; tbl++) {
2050                 for (c = *tbl; c->subvendor; c++) {
2051                         if (c->subvendor == ice->eeprom.subvendor) {
2052                                 strcpy(card->shortname, c->name);
2053                                 if (c->chip_init) {
2054                                         if ((err = c->chip_init(ice)) < 0) {
2055                                                 snd_card_free(card);
2056                                                 return err;
2057                                         }
2058                                 }
2059                                 goto __found;
2060                         }
2061                 }
2062         }
2063         c = &no_matched;
2064  __found:
2065
2066         if ((err = snd_vt1724_pcm_profi(ice, pcm_dev++)) < 0) {
2067                 snd_card_free(card);
2068                 return err;
2069         }
2070         
2071         if ((err = snd_vt1724_pcm_spdif(ice, pcm_dev++)) < 0) {
2072                 snd_card_free(card);
2073                 return err;
2074         }
2075         
2076         if ((err = snd_vt1724_pcm_indep(ice, pcm_dev++)) < 0) {
2077                 snd_card_free(card);
2078                 return err;
2079         }
2080
2081         if ((err = snd_vt1724_ac97_mixer(ice)) < 0) {
2082                 snd_card_free(card);
2083                 return err;
2084         }
2085
2086         if ((err = snd_vt1724_build_controls(ice)) < 0) {
2087                 snd_card_free(card);
2088                 return err;
2089         }
2090
2091         if (ice->pcm) { /* has SPDIF I/O */
2092                 if ((err = snd_vt1724_spdif_build_controls(ice)) < 0) {
2093                         snd_card_free(card);
2094                         return err;
2095                 }
2096         }
2097
2098         if (c->build_controls) {
2099                 if ((err = c->build_controls(ice)) < 0) {
2100                         snd_card_free(card);
2101                         return err;
2102                 }
2103         }
2104
2105         if (! c->no_mpu401) {
2106                 if (ice->eeprom.data[ICE_EEP2_SYSCONF] & VT1724_CFG_MPU401) {
2107                         if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_ICE1712,
2108                                                        ICEREG1724(ice, MPU_CTRL), 1,
2109                                                        ice->irq, 0,
2110                                                        &ice->rmidi[0])) < 0) {
2111                                 snd_card_free(card);
2112                                 return err;
2113                         }
2114                 }
2115         }
2116
2117         sprintf(card->longname, "%s at 0x%lx, irq %i",
2118                 card->shortname, ice->port, ice->irq);
2119
2120         if ((err = snd_card_register(card)) < 0) {
2121                 snd_card_free(card);
2122                 return err;
2123         }
2124         pci_set_drvdata(pci, card);
2125         dev++;
2126         return 0;
2127 }
2128
2129 static void __devexit snd_vt1724_remove(struct pci_dev *pci)
2130 {
2131         snd_card_free(pci_get_drvdata(pci));
2132         pci_set_drvdata(pci, NULL);
2133 }
2134
2135 static struct pci_driver driver = {
2136         .name = "ICE1724",
2137         .id_table = snd_vt1724_ids,
2138         .probe = snd_vt1724_probe,
2139         .remove = __devexit_p(snd_vt1724_remove),
2140 };
2141
2142 static int __init alsa_card_ice1724_init(void)
2143 {
2144         int err;
2145
2146         if ((err = pci_module_init(&driver)) < 0) {
2147 #ifdef MODULE
2148                 printk(KERN_ERR "ICE1724 soundcard not found or device busy\n");
2149 #endif
2150                 return err;
2151         }
2152         return 0;
2153 }
2154
2155 static void __exit alsa_card_ice1724_exit(void)
2156 {
2157         pci_unregister_driver(&driver);
2158 }
2159
2160 module_init(alsa_card_ice1724_init)
2161 module_exit(alsa_card_ice1724_exit)
2162
2163 #ifndef MODULE
2164
2165 /* format is: snd-ice1724=enable,index,id */
2166
2167 static int __init alsa_card_ice1724_setup(char *str)
2168 {
2169         static unsigned __initdata nr_dev = 0;
2170
2171         if (nr_dev >= SNDRV_CARDS)
2172                 return 0;
2173         (void)(get_option(&str,&enable[nr_dev]) == 2 &&
2174                get_option(&str,&index[nr_dev]) == 2 &&
2175                get_id(&str,&id[nr_dev]) == 2);
2176         nr_dev++;
2177         return 1;
2178 }
2179
2180 __setup("snd-ice1724=", alsa_card_ice1724_setup);
2181
2182 #endif /* ifndef MODULE */