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