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