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