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