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