Merge branch 'master'
[linux-2.6] / sound / pci / ice1712 / ice1712.c
1 /*
2  *   ALSA driver for ICEnsemble ICE1712 (Envy24)
3  *
4  *      Copyright (c) 2000 Jaroslav Kysela <perex@suse.cz>
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19  *
20  */      
21
22 /*
23   NOTES:
24   - spdif nonaudio consumer mode does not work (at least with my
25     Sony STR-DB830)
26 */
27
28 /*
29  * Changes:
30  *
31  *  2002.09.09  Takashi Iwai <tiwai@suse.de>
32  *      split the code to several files.  each low-level routine
33  *      is stored in the local file and called from registration
34  *      function from card_info struct.
35  *
36  *  2002.11.26  James Stafford <jstafford@ampltd.com>
37  *      Added support for VT1724 (Envy24HT)
38  *      I have left out support for 176.4 and 192 KHz for the moment. 
39  *  I also haven't done anything with the internal S/PDIF transmitter or the MPU-401
40  *
41  *  2003.02.20  Taksahi Iwai <tiwai@suse.de>
42  *      Split vt1724 part to an independent driver.
43  *      The GPIO is accessed through the callback functions now.
44  *
45  * 2004.03.31 Doug McLain <nostar@comcast.net>
46  *    Added support for Event Electronics EZ8 card to hoontech.c.
47  */
48
49
50 #include <sound/driver.h>
51 #include <asm/io.h>
52 #include <linux/delay.h>
53 #include <linux/interrupt.h>
54 #include <linux/init.h>
55 #include <linux/pci.h>
56 #include <linux/slab.h>
57 #include <linux/moduleparam.h>
58 #include <sound/core.h>
59 #include <sound/cs8427.h>
60 #include <sound/info.h>
61 #include <sound/mpu401.h>
62 #include <sound/initval.h>
63
64 #include <sound/asoundef.h>
65
66 #include "ice1712.h"
67
68 /* lowlevel routines */
69 #include "delta.h"
70 #include "ews.h"
71 #include "hoontech.h"
72
73 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
74 MODULE_DESCRIPTION("ICEnsemble ICE1712 (Envy24)");
75 MODULE_LICENSE("GPL");
76 MODULE_SUPPORTED_DEVICE("{"
77                HOONTECH_DEVICE_DESC
78                DELTA_DEVICE_DESC
79                EWS_DEVICE_DESC
80                "{ICEnsemble,Generic ICE1712},"
81                "{ICEnsemble,Generic Envy24}}");
82
83 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;      /* Index 0-MAX */
84 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;       /* ID for this card */
85 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;              /* Enable this card */
86 static char *model[SNDRV_CARDS];
87 static int omni[SNDRV_CARDS];   /* Delta44 & 66 Omni I/O support */
88 static int cs8427_timeout[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 500}; /* CS8427 S/PDIF transciever reset timeout value in msec */
89
90 module_param_array(index, int, NULL, 0444);
91 MODULE_PARM_DESC(index, "Index value for ICE1712 soundcard.");
92 module_param_array(id, charp, NULL, 0444);
93 MODULE_PARM_DESC(id, "ID string for ICE1712 soundcard.");
94 module_param_array(enable, bool, NULL, 0444);
95 MODULE_PARM_DESC(enable, "Enable ICE1712 soundcard.");
96 module_param_array(omni, bool, NULL, 0444);
97 MODULE_PARM_DESC(omni, "Enable Midiman M-Audio Delta Omni I/O support.");
98 module_param_array(cs8427_timeout, int, NULL, 0444);
99 MODULE_PARM_DESC(cs8427_timeout, "Define reset timeout for cs8427 chip in msec resolution.");
100 module_param_array(model, charp, NULL, 0444);
101 MODULE_PARM_DESC(model, "Use the given board model.");
102
103
104 static struct pci_device_id snd_ice1712_ids[] = {
105         { PCI_VENDOR_ID_ICE, PCI_DEVICE_ID_ICE_1712, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },   /* ICE1712 */
106         { 0, }
107 };
108
109 MODULE_DEVICE_TABLE(pci, snd_ice1712_ids);
110
111 static int snd_ice1712_build_pro_mixer(ice1712_t *ice);
112 static int snd_ice1712_build_controls(ice1712_t *ice);
113
114 static int PRO_RATE_LOCKED;
115 static int PRO_RATE_RESET = 1;
116 static unsigned int PRO_RATE_DEFAULT = 44100;
117
118 /*
119  *  Basic I/O
120  */
121  
122 /* check whether the clock mode is spdif-in */
123 static inline int is_spdif_master(ice1712_t *ice)
124 {
125         return (inb(ICEMT(ice, RATE)) & ICE1712_SPDIF_MASTER) ? 1 : 0;
126 }
127
128 static inline int is_pro_rate_locked(ice1712_t *ice)
129 {
130         return is_spdif_master(ice) || PRO_RATE_LOCKED;
131 }
132
133 static inline void snd_ice1712_ds_write(ice1712_t * ice, u8 channel, u8 addr, u32 data)
134 {
135         outb((channel << 4) | addr, ICEDS(ice, INDEX));
136         outl(data, ICEDS(ice, DATA));
137 }
138
139 static inline u32 snd_ice1712_ds_read(ice1712_t * ice, u8 channel, u8 addr)
140 {
141         outb((channel << 4) | addr, ICEDS(ice, INDEX));
142         return inl(ICEDS(ice, DATA));
143 }
144
145 static void snd_ice1712_ac97_write(ac97_t *ac97,
146                                    unsigned short reg,
147                                    unsigned short val)
148 {
149         ice1712_t *ice = (ice1712_t *)ac97->private_data;
150         int tm;
151         unsigned char old_cmd = 0;
152
153         for (tm = 0; tm < 0x10000; tm++) {
154                 old_cmd = inb(ICEREG(ice, AC97_CMD));
155                 if (old_cmd & (ICE1712_AC97_WRITE | ICE1712_AC97_READ))
156                         continue;
157                 if (!(old_cmd & ICE1712_AC97_READY))
158                         continue;
159                 break;
160         }
161         outb(reg, ICEREG(ice, AC97_INDEX));
162         outw(val, ICEREG(ice, AC97_DATA));
163         old_cmd &= ~(ICE1712_AC97_PBK_VSR | ICE1712_AC97_CAP_VSR);
164         outb(old_cmd | ICE1712_AC97_WRITE, ICEREG(ice, AC97_CMD));
165         for (tm = 0; tm < 0x10000; tm++)
166                 if ((inb(ICEREG(ice, AC97_CMD)) & ICE1712_AC97_WRITE) == 0)
167                         break;
168 }
169
170 static unsigned short snd_ice1712_ac97_read(ac97_t *ac97,
171                                             unsigned short reg)
172 {
173         ice1712_t *ice = (ice1712_t *)ac97->private_data;
174         int tm;
175         unsigned char old_cmd = 0;
176
177         for (tm = 0; tm < 0x10000; tm++) {
178                 old_cmd = inb(ICEREG(ice, AC97_CMD));
179                 if (old_cmd & (ICE1712_AC97_WRITE | ICE1712_AC97_READ))
180                         continue;
181                 if (!(old_cmd & ICE1712_AC97_READY))
182                         continue;
183                 break;
184         }
185         outb(reg, ICEREG(ice, AC97_INDEX));
186         outb(old_cmd | ICE1712_AC97_READ, ICEREG(ice, AC97_CMD));
187         for (tm = 0; tm < 0x10000; tm++)
188                 if ((inb(ICEREG(ice, AC97_CMD)) & ICE1712_AC97_READ) == 0)
189                         break;
190         if (tm >= 0x10000)              /* timeout */
191                 return ~0;
192         return inw(ICEREG(ice, AC97_DATA));
193 }
194
195 /*
196  * pro ac97 section
197  */
198
199 static void snd_ice1712_pro_ac97_write(ac97_t *ac97,
200                                        unsigned short reg,
201                                        unsigned short val)
202 {
203         ice1712_t *ice = (ice1712_t *)ac97->private_data;
204         int tm;
205         unsigned char old_cmd = 0;
206
207         for (tm = 0; tm < 0x10000; tm++) {
208                 old_cmd = inb(ICEMT(ice, AC97_CMD));
209                 if (old_cmd & (ICE1712_AC97_WRITE | ICE1712_AC97_READ))
210                         continue;
211                 if (!(old_cmd & ICE1712_AC97_READY))
212                         continue;
213                 break;
214         }
215         outb(reg, ICEMT(ice, AC97_INDEX));
216         outw(val, ICEMT(ice, AC97_DATA));
217         old_cmd &= ~(ICE1712_AC97_PBK_VSR | ICE1712_AC97_CAP_VSR);
218         outb(old_cmd | ICE1712_AC97_WRITE, ICEMT(ice, AC97_CMD));
219         for (tm = 0; tm < 0x10000; tm++)
220                 if ((inb(ICEMT(ice, AC97_CMD)) & ICE1712_AC97_WRITE) == 0)
221                         break;
222 }
223
224
225 static unsigned short snd_ice1712_pro_ac97_read(ac97_t *ac97,
226                                                 unsigned short reg)
227 {
228         ice1712_t *ice = (ice1712_t *)ac97->private_data;
229         int tm;
230         unsigned char old_cmd = 0;
231
232         for (tm = 0; tm < 0x10000; tm++) {
233                 old_cmd = inb(ICEMT(ice, AC97_CMD));
234                 if (old_cmd & (ICE1712_AC97_WRITE | ICE1712_AC97_READ))
235                         continue;
236                 if (!(old_cmd & ICE1712_AC97_READY))
237                         continue;
238                 break;
239         }
240         outb(reg, ICEMT(ice, AC97_INDEX));
241         outb(old_cmd | ICE1712_AC97_READ, ICEMT(ice, AC97_CMD));
242         for (tm = 0; tm < 0x10000; tm++)
243                 if ((inb(ICEMT(ice, AC97_CMD)) & ICE1712_AC97_READ) == 0)
244                         break;
245         if (tm >= 0x10000)              /* timeout */
246                 return ~0;
247         return inw(ICEMT(ice, AC97_DATA));
248 }
249
250 /*
251  * consumer ac97 digital mix
252  */
253 static int snd_ice1712_digmix_route_ac97_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
254 {
255         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
256         uinfo->count = 1;
257         uinfo->value.integer.min = 0;
258         uinfo->value.integer.max = 1;
259         return 0;
260 }
261
262 static int snd_ice1712_digmix_route_ac97_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
263 {
264         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
265         
266         ucontrol->value.integer.value[0] = inb(ICEMT(ice, MONITOR_ROUTECTRL)) & ICE1712_ROUTE_AC97 ? 1 : 0;
267         return 0;
268 }
269
270 static int snd_ice1712_digmix_route_ac97_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
271 {
272         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
273         unsigned char val, nval;
274         
275         spin_lock_irq(&ice->reg_lock);
276         val = inb(ICEMT(ice, MONITOR_ROUTECTRL));
277         nval = val & ~ICE1712_ROUTE_AC97;
278         if (ucontrol->value.integer.value[0]) nval |= ICE1712_ROUTE_AC97;
279         outb(nval, ICEMT(ice, MONITOR_ROUTECTRL));
280         spin_unlock_irq(&ice->reg_lock);
281         return val != nval;
282 }
283
284 static snd_kcontrol_new_t snd_ice1712_mixer_digmix_route_ac97 __devinitdata = {
285         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
286         .name = "Digital Mixer To AC97",
287         .info = snd_ice1712_digmix_route_ac97_info,
288         .get = snd_ice1712_digmix_route_ac97_get,
289         .put = snd_ice1712_digmix_route_ac97_put,
290 };
291
292
293 /*
294  * gpio operations
295  */
296 static void snd_ice1712_set_gpio_dir(ice1712_t *ice, unsigned int data)
297 {
298         snd_ice1712_write(ice, ICE1712_IREG_GPIO_DIRECTION, data);
299         inb(ICEREG(ice, DATA)); /* dummy read for pci-posting */
300 }
301
302 static void snd_ice1712_set_gpio_mask(ice1712_t *ice, unsigned int data)
303 {
304         snd_ice1712_write(ice, ICE1712_IREG_GPIO_WRITE_MASK, data);
305         inb(ICEREG(ice, DATA)); /* dummy read for pci-posting */
306 }
307
308 static unsigned int snd_ice1712_get_gpio_data(ice1712_t *ice)
309 {
310         return snd_ice1712_read(ice, ICE1712_IREG_GPIO_DATA);
311 }
312
313 static void snd_ice1712_set_gpio_data(ice1712_t *ice, unsigned int val)
314 {
315         snd_ice1712_write(ice, ICE1712_IREG_GPIO_DATA, val);
316         inb(ICEREG(ice, DATA)); /* dummy read for pci-posting */
317 }
318
319
320 /*
321  *
322  * CS8427 interface
323  *
324  */
325
326 /*
327  * change the input clock selection
328  * spdif_clock = 1 - IEC958 input, 0 - Envy24
329  */
330 static int snd_ice1712_cs8427_set_input_clock(ice1712_t *ice, int spdif_clock)
331 {
332         unsigned char reg[2] = { 0x80 | 4, 0 };   /* CS8427 auto increment | register number 4 + data */
333         unsigned char val, nval;
334         int res = 0;
335         
336         snd_i2c_lock(ice->i2c);
337         if (snd_i2c_sendbytes(ice->cs8427, reg, 1) != 1) {
338                 snd_i2c_unlock(ice->i2c);
339                 return -EIO;
340         }
341         if (snd_i2c_readbytes(ice->cs8427, &val, 1) != 1) {
342                 snd_i2c_unlock(ice->i2c);
343                 return -EIO;
344         }
345         nval = val & 0xf0;
346         if (spdif_clock)
347                 nval |= 0x01;
348         else
349                 nval |= 0x04;
350         if (val != nval) {
351                 reg[1] = nval;
352                 if (snd_i2c_sendbytes(ice->cs8427, reg, 2) != 2) {
353                         res = -EIO;
354                 } else {
355                         res++;
356                 }
357         }
358         snd_i2c_unlock(ice->i2c);
359         return res;
360 }
361
362 /*
363  * spdif callbacks
364  */
365 static void open_cs8427(ice1712_t *ice, snd_pcm_substream_t * substream)
366 {
367         snd_cs8427_iec958_active(ice->cs8427, 1);
368 }
369
370 static void close_cs8427(ice1712_t *ice, snd_pcm_substream_t * substream)
371 {
372         snd_cs8427_iec958_active(ice->cs8427, 0);
373 }
374
375 static void setup_cs8427(ice1712_t *ice, int rate)
376 {
377         snd_cs8427_iec958_pcm(ice->cs8427, rate);
378 }
379
380 /*
381  * create and initialize callbacks for cs8427 interface
382  */
383 int __devinit snd_ice1712_init_cs8427(ice1712_t *ice, int addr)
384 {
385         int err;
386
387         if ((err = snd_cs8427_create(ice->i2c, addr,
388                                      (ice->cs8427_timeout * HZ) / 1000,
389                                      &ice->cs8427)) < 0) {
390                 snd_printk(KERN_ERR "CS8427 initialization failed\n");
391                 return err;
392         }
393         ice->spdif.ops.open = open_cs8427;
394         ice->spdif.ops.close = close_cs8427;
395         ice->spdif.ops.setup_rate = setup_cs8427;
396         return 0;
397 }
398
399
400 /*
401  *  Interrupt handler
402  */
403
404 static irqreturn_t snd_ice1712_interrupt(int irq, void *dev_id, struct pt_regs *regs)
405 {
406         ice1712_t *ice = dev_id;
407         unsigned char status;
408         int handled = 0;
409
410         while (1) {
411                 status = inb(ICEREG(ice, IRQSTAT));
412                 if (status == 0)
413                         break;
414                 handled = 1;
415                 if (status & ICE1712_IRQ_MPU1) {
416                         if (ice->rmidi[0])
417                                 snd_mpu401_uart_interrupt(irq, ice->rmidi[0]->private_data, regs);
418                         outb(ICE1712_IRQ_MPU1, ICEREG(ice, IRQSTAT));
419                         status &= ~ICE1712_IRQ_MPU1;
420                 }
421                 if (status & ICE1712_IRQ_TIMER)
422                         outb(ICE1712_IRQ_TIMER, ICEREG(ice, IRQSTAT));
423                 if (status & ICE1712_IRQ_MPU2) {
424                         if (ice->rmidi[1])
425                                 snd_mpu401_uart_interrupt(irq, ice->rmidi[1]->private_data, regs);
426                         outb(ICE1712_IRQ_MPU2, ICEREG(ice, IRQSTAT));
427                         status &= ~ICE1712_IRQ_MPU2;
428                 }
429                 if (status & ICE1712_IRQ_PROPCM) {
430                         unsigned char mtstat = inb(ICEMT(ice, IRQ));
431                         if (mtstat & ICE1712_MULTI_PBKSTATUS) {
432                                 if (ice->playback_pro_substream)
433                                         snd_pcm_period_elapsed(ice->playback_pro_substream);
434                                 outb(ICE1712_MULTI_PBKSTATUS, ICEMT(ice, IRQ));
435                         }
436                         if (mtstat & ICE1712_MULTI_CAPSTATUS) {
437                                 if (ice->capture_pro_substream)
438                                         snd_pcm_period_elapsed(ice->capture_pro_substream);
439                                 outb(ICE1712_MULTI_CAPSTATUS, ICEMT(ice, IRQ));
440                         }
441                 }
442                 if (status & ICE1712_IRQ_FM)
443                         outb(ICE1712_IRQ_FM, ICEREG(ice, IRQSTAT));
444                 if (status & ICE1712_IRQ_PBKDS) {
445                         u32 idx;
446                         u16 pbkstatus;
447                         snd_pcm_substream_t *substream;
448                         pbkstatus = inw(ICEDS(ice, INTSTAT));
449                         //printk("pbkstatus = 0x%x\n", pbkstatus);
450                         for (idx = 0; idx < 6; idx++) {
451                                 if ((pbkstatus & (3 << (idx * 2))) == 0)
452                                         continue;
453                                 if ((substream = ice->playback_con_substream_ds[idx]) != NULL)
454                                         snd_pcm_period_elapsed(substream);
455                                 outw(3 << (idx * 2), ICEDS(ice, INTSTAT));
456                         }
457                         outb(ICE1712_IRQ_PBKDS, ICEREG(ice, IRQSTAT));
458                 }
459                 if (status & ICE1712_IRQ_CONCAP) {
460                         if (ice->capture_con_substream)
461                                 snd_pcm_period_elapsed(ice->capture_con_substream);
462                         outb(ICE1712_IRQ_CONCAP, ICEREG(ice, IRQSTAT));
463                 }
464                 if (status & ICE1712_IRQ_CONPBK) {
465                         if (ice->playback_con_substream)
466                                 snd_pcm_period_elapsed(ice->playback_con_substream);
467                         outb(ICE1712_IRQ_CONPBK, ICEREG(ice, IRQSTAT));
468                 }
469         }
470         return IRQ_RETVAL(handled);
471 }
472
473
474 /*
475  *  PCM part - misc
476  */
477
478 static int snd_ice1712_hw_params(snd_pcm_substream_t * substream,
479                                  snd_pcm_hw_params_t * hw_params)
480 {
481         return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
482 }
483
484 static int snd_ice1712_hw_free(snd_pcm_substream_t * substream)
485 {
486         return snd_pcm_lib_free_pages(substream);
487 }
488
489 /*
490  *  PCM part - consumer I/O
491  */
492
493 static int snd_ice1712_playback_trigger(snd_pcm_substream_t * substream,
494                                         int cmd)
495 {
496         ice1712_t *ice = snd_pcm_substream_chip(substream);
497         int result = 0;
498         u32 tmp;
499         
500         spin_lock(&ice->reg_lock);
501         tmp = snd_ice1712_read(ice, ICE1712_IREG_PBK_CTRL);
502         if (cmd == SNDRV_PCM_TRIGGER_START) {
503                 tmp |= 1;
504         } else if (cmd == SNDRV_PCM_TRIGGER_STOP) {
505                 tmp &= ~1;
506         } else if (cmd == SNDRV_PCM_TRIGGER_PAUSE_PUSH) {
507                 tmp |= 2;
508         } else if (cmd == SNDRV_PCM_TRIGGER_PAUSE_RELEASE) {
509                 tmp &= ~2;
510         } else {
511                 result = -EINVAL;
512         }
513         snd_ice1712_write(ice, ICE1712_IREG_PBK_CTRL, tmp);
514         spin_unlock(&ice->reg_lock);
515         return result;
516 }
517
518 static int snd_ice1712_playback_ds_trigger(snd_pcm_substream_t * substream,
519                                            int cmd)
520 {
521         ice1712_t *ice = snd_pcm_substream_chip(substream);
522         int result = 0;
523         u32 tmp;
524         
525         spin_lock(&ice->reg_lock);
526         tmp = snd_ice1712_ds_read(ice, substream->number * 2, ICE1712_DSC_CONTROL);
527         if (cmd == SNDRV_PCM_TRIGGER_START) {
528                 tmp |= 1;
529         } else if (cmd == SNDRV_PCM_TRIGGER_STOP) {
530                 tmp &= ~1;
531         } else if (cmd == SNDRV_PCM_TRIGGER_PAUSE_PUSH) {
532                 tmp |= 2;
533         } else if (cmd == SNDRV_PCM_TRIGGER_PAUSE_RELEASE) {
534                 tmp &= ~2;
535         } else {
536                 result = -EINVAL;
537         }
538         snd_ice1712_ds_write(ice, substream->number * 2, ICE1712_DSC_CONTROL, tmp);
539         spin_unlock(&ice->reg_lock);
540         return result;
541 }
542
543 static int snd_ice1712_capture_trigger(snd_pcm_substream_t * substream,
544                                        int cmd)
545 {
546         ice1712_t *ice = snd_pcm_substream_chip(substream);
547         int result = 0;
548         u8 tmp;
549         
550         spin_lock(&ice->reg_lock);
551         tmp = snd_ice1712_read(ice, ICE1712_IREG_CAP_CTRL);
552         if (cmd == SNDRV_PCM_TRIGGER_START) {
553                 tmp |= 1;
554         } else if (cmd == SNDRV_PCM_TRIGGER_STOP) {
555                 tmp &= ~1;
556         } else {
557                 result = -EINVAL;
558         }
559         snd_ice1712_write(ice, ICE1712_IREG_CAP_CTRL, tmp);
560         spin_unlock(&ice->reg_lock);
561         return result;
562 }
563
564 static int snd_ice1712_playback_prepare(snd_pcm_substream_t * substream)
565 {
566         ice1712_t *ice = snd_pcm_substream_chip(substream);
567         snd_pcm_runtime_t *runtime = substream->runtime;
568         u32 period_size, buf_size, rate, tmp;
569
570         period_size = (snd_pcm_lib_period_bytes(substream) >> 2) - 1;
571         buf_size = snd_pcm_lib_buffer_bytes(substream) - 1;
572         tmp = 0x0000;
573         if (snd_pcm_format_width(runtime->format) == 16)
574                 tmp |= 0x10;
575         if (runtime->channels == 2)
576                 tmp |= 0x08;
577         rate = (runtime->rate * 8192) / 375;
578         if (rate > 0x000fffff)
579                 rate = 0x000fffff;
580         spin_lock_irq(&ice->reg_lock);
581         outb(0, ice->ddma_port + 15);
582         outb(ICE1712_DMA_MODE_WRITE | ICE1712_DMA_AUTOINIT, ice->ddma_port + 0x0b);
583         outl(runtime->dma_addr, ice->ddma_port + 0);
584         outw(buf_size, ice->ddma_port + 4);
585         snd_ice1712_write(ice, ICE1712_IREG_PBK_RATE_LO, rate & 0xff);
586         snd_ice1712_write(ice, ICE1712_IREG_PBK_RATE_MID, (rate >> 8) & 0xff);
587         snd_ice1712_write(ice, ICE1712_IREG_PBK_RATE_HI, (rate >> 16) & 0xff);
588         snd_ice1712_write(ice, ICE1712_IREG_PBK_CTRL, tmp);
589         snd_ice1712_write(ice, ICE1712_IREG_PBK_COUNT_LO, period_size & 0xff);
590         snd_ice1712_write(ice, ICE1712_IREG_PBK_COUNT_HI, period_size >> 8);
591         snd_ice1712_write(ice, ICE1712_IREG_PBK_LEFT, 0);
592         snd_ice1712_write(ice, ICE1712_IREG_PBK_RIGHT, 0);
593         spin_unlock_irq(&ice->reg_lock);
594         return 0;
595 }
596
597 static int snd_ice1712_playback_ds_prepare(snd_pcm_substream_t * substream)
598 {
599         ice1712_t *ice = snd_pcm_substream_chip(substream);
600         snd_pcm_runtime_t *runtime = substream->runtime;
601         u32 period_size, buf_size, rate, tmp, chn;
602
603         period_size = snd_pcm_lib_period_bytes(substream) - 1;
604         buf_size = snd_pcm_lib_buffer_bytes(substream) - 1;
605         tmp = 0x0064;
606         if (snd_pcm_format_width(runtime->format) == 16)
607                 tmp &= ~0x04;
608         if (runtime->channels == 2)
609                 tmp |= 0x08;
610         rate = (runtime->rate * 8192) / 375;
611         if (rate > 0x000fffff)
612                 rate = 0x000fffff;
613         ice->playback_con_active_buf[substream->number] = 0;
614         ice->playback_con_virt_addr[substream->number] = runtime->dma_addr;
615         chn = substream->number * 2;
616         spin_lock_irq(&ice->reg_lock);
617         snd_ice1712_ds_write(ice, chn, ICE1712_DSC_ADDR0, runtime->dma_addr);
618         snd_ice1712_ds_write(ice, chn, ICE1712_DSC_COUNT0, period_size);
619         snd_ice1712_ds_write(ice, chn, ICE1712_DSC_ADDR1, runtime->dma_addr + (runtime->periods > 1 ? period_size + 1 : 0));
620         snd_ice1712_ds_write(ice, chn, ICE1712_DSC_COUNT1, period_size);
621         snd_ice1712_ds_write(ice, chn, ICE1712_DSC_RATE, rate);
622         snd_ice1712_ds_write(ice, chn, ICE1712_DSC_VOLUME, 0);
623         snd_ice1712_ds_write(ice, chn, ICE1712_DSC_CONTROL, tmp);
624         if (runtime->channels == 2) {
625                 snd_ice1712_ds_write(ice, chn + 1, ICE1712_DSC_RATE, rate);
626                 snd_ice1712_ds_write(ice, chn + 1, ICE1712_DSC_VOLUME, 0);
627         }
628         spin_unlock_irq(&ice->reg_lock);
629         return 0;
630 }
631
632 static int snd_ice1712_capture_prepare(snd_pcm_substream_t * substream)
633 {
634         ice1712_t *ice = snd_pcm_substream_chip(substream);
635         snd_pcm_runtime_t *runtime = substream->runtime;
636         u32 period_size, buf_size;
637         u8 tmp;
638
639         period_size = (snd_pcm_lib_period_bytes(substream) >> 2) - 1;
640         buf_size = snd_pcm_lib_buffer_bytes(substream) - 1;
641         tmp = 0x06;
642         if (snd_pcm_format_width(runtime->format) == 16)
643                 tmp &= ~0x04;
644         if (runtime->channels == 2)
645                 tmp &= ~0x02;
646         spin_lock_irq(&ice->reg_lock);
647         outl(ice->capture_con_virt_addr = runtime->dma_addr, ICEREG(ice, CONCAP_ADDR));
648         outw(buf_size, ICEREG(ice, CONCAP_COUNT));
649         snd_ice1712_write(ice, ICE1712_IREG_CAP_COUNT_HI, period_size >> 8);
650         snd_ice1712_write(ice, ICE1712_IREG_CAP_COUNT_LO, period_size & 0xff);
651         snd_ice1712_write(ice, ICE1712_IREG_CAP_CTRL, tmp);
652         spin_unlock_irq(&ice->reg_lock);
653         snd_ac97_set_rate(ice->ac97, AC97_PCM_LR_ADC_RATE, runtime->rate);
654         return 0;
655 }
656
657 static snd_pcm_uframes_t snd_ice1712_playback_pointer(snd_pcm_substream_t * substream)
658 {
659         ice1712_t *ice = snd_pcm_substream_chip(substream);
660         snd_pcm_runtime_t *runtime = substream->runtime;
661         size_t ptr;
662
663         if (!(snd_ice1712_read(ice, ICE1712_IREG_PBK_CTRL) & 1))
664                 return 0;
665         ptr = runtime->buffer_size - inw(ice->ddma_port + 4);
666         if (ptr == runtime->buffer_size)
667                 ptr = 0;
668         return bytes_to_frames(substream->runtime, ptr);
669 }
670
671 static snd_pcm_uframes_t snd_ice1712_playback_ds_pointer(snd_pcm_substream_t * substream)
672 {
673         ice1712_t *ice = snd_pcm_substream_chip(substream);
674         u8 addr;
675         size_t ptr;
676
677         if (!(snd_ice1712_ds_read(ice, substream->number * 2, ICE1712_DSC_CONTROL) & 1))
678                 return 0;
679         if (ice->playback_con_active_buf[substream->number])
680                 addr = ICE1712_DSC_ADDR1;
681         else
682                 addr = ICE1712_DSC_ADDR0;
683         ptr = snd_ice1712_ds_read(ice, substream->number * 2, addr) -
684                 ice->playback_con_virt_addr[substream->number];
685         if (ptr == substream->runtime->buffer_size)
686                 ptr = 0;
687         return bytes_to_frames(substream->runtime, ptr);
688 }
689
690 static snd_pcm_uframes_t snd_ice1712_capture_pointer(snd_pcm_substream_t * substream)
691 {
692         ice1712_t *ice = snd_pcm_substream_chip(substream);
693         size_t ptr;
694
695         if (!(snd_ice1712_read(ice, ICE1712_IREG_CAP_CTRL) & 1))
696                 return 0;
697         ptr = inl(ICEREG(ice, CONCAP_ADDR)) - ice->capture_con_virt_addr;
698         if (ptr == substream->runtime->buffer_size)
699                 ptr = 0;
700         return bytes_to_frames(substream->runtime, ptr);
701 }
702
703 static snd_pcm_hardware_t snd_ice1712_playback =
704 {
705         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
706                                  SNDRV_PCM_INFO_BLOCK_TRANSFER |
707                                  SNDRV_PCM_INFO_MMAP_VALID |
708                                  SNDRV_PCM_INFO_PAUSE),
709         .formats =              SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
710         .rates =                SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
711         .rate_min =             4000,
712         .rate_max =             48000,
713         .channels_min =         1,
714         .channels_max =         2,
715         .buffer_bytes_max =     (64*1024),
716         .period_bytes_min =     64,
717         .period_bytes_max =     (64*1024),
718         .periods_min =          1,
719         .periods_max =          1024,
720         .fifo_size =            0,
721 };
722
723 static snd_pcm_hardware_t snd_ice1712_playback_ds =
724 {
725         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
726                                  SNDRV_PCM_INFO_BLOCK_TRANSFER |
727                                  SNDRV_PCM_INFO_MMAP_VALID |
728                                  SNDRV_PCM_INFO_PAUSE),
729         .formats =              SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
730         .rates =                SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
731         .rate_min =             4000,
732         .rate_max =             48000,
733         .channels_min =         1,
734         .channels_max =         2,
735         .buffer_bytes_max =     (128*1024),
736         .period_bytes_min =     64,
737         .period_bytes_max =     (128*1024),
738         .periods_min =          2,
739         .periods_max =          2,
740         .fifo_size =            0,
741 };
742
743 static snd_pcm_hardware_t snd_ice1712_capture =
744 {
745         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
746                                  SNDRV_PCM_INFO_BLOCK_TRANSFER |
747                                  SNDRV_PCM_INFO_MMAP_VALID),
748         .formats =              SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
749         .rates =                SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
750         .rate_min =             4000,
751         .rate_max =             48000,
752         .channels_min =         1,
753         .channels_max =         2,
754         .buffer_bytes_max =     (64*1024),
755         .period_bytes_min =     64,
756         .period_bytes_max =     (64*1024),
757         .periods_min =          1,
758         .periods_max =          1024,
759         .fifo_size =            0,
760 };
761
762 static int snd_ice1712_playback_open(snd_pcm_substream_t * substream)
763 {
764         snd_pcm_runtime_t *runtime = substream->runtime;
765         ice1712_t *ice = snd_pcm_substream_chip(substream);
766
767         ice->playback_con_substream = substream;
768         runtime->hw = snd_ice1712_playback;
769         return 0;
770 }
771
772 static int snd_ice1712_playback_ds_open(snd_pcm_substream_t * substream)
773 {
774         snd_pcm_runtime_t *runtime = substream->runtime;
775         ice1712_t *ice = snd_pcm_substream_chip(substream);
776         u32 tmp;
777
778         ice->playback_con_substream_ds[substream->number] = substream;
779         runtime->hw = snd_ice1712_playback_ds;
780         spin_lock_irq(&ice->reg_lock); 
781         tmp = inw(ICEDS(ice, INTMASK)) & ~(1 << (substream->number * 2));
782         outw(tmp, ICEDS(ice, INTMASK));
783         spin_unlock_irq(&ice->reg_lock);
784         return 0;
785 }
786
787 static int snd_ice1712_capture_open(snd_pcm_substream_t * substream)
788 {
789         snd_pcm_runtime_t *runtime = substream->runtime;
790         ice1712_t *ice = snd_pcm_substream_chip(substream);
791
792         ice->capture_con_substream = substream;
793         runtime->hw = snd_ice1712_capture;
794         runtime->hw.rates = ice->ac97->rates[AC97_RATES_ADC];
795         if (!(runtime->hw.rates & SNDRV_PCM_RATE_8000))
796                 runtime->hw.rate_min = 48000;
797         return 0;
798 }
799
800 static int snd_ice1712_playback_close(snd_pcm_substream_t * substream)
801 {
802         ice1712_t *ice = snd_pcm_substream_chip(substream);
803
804         ice->playback_con_substream = NULL;
805         return 0;
806 }
807
808 static int snd_ice1712_playback_ds_close(snd_pcm_substream_t * substream)
809 {
810         ice1712_t *ice = snd_pcm_substream_chip(substream);
811         u32 tmp;
812
813         spin_lock_irq(&ice->reg_lock); 
814         tmp = inw(ICEDS(ice, INTMASK)) | (3 << (substream->number * 2));
815         outw(tmp, ICEDS(ice, INTMASK));
816         spin_unlock_irq(&ice->reg_lock);
817         ice->playback_con_substream_ds[substream->number] = NULL;
818         return 0;
819 }
820
821 static int snd_ice1712_capture_close(snd_pcm_substream_t * substream)
822 {
823         ice1712_t *ice = snd_pcm_substream_chip(substream);
824
825         ice->capture_con_substream = NULL;
826         return 0;
827 }
828
829 static snd_pcm_ops_t snd_ice1712_playback_ops = {
830         .open =         snd_ice1712_playback_open,
831         .close =        snd_ice1712_playback_close,
832         .ioctl =        snd_pcm_lib_ioctl,
833         .hw_params =    snd_ice1712_hw_params,
834         .hw_free =      snd_ice1712_hw_free,
835         .prepare =      snd_ice1712_playback_prepare,
836         .trigger =      snd_ice1712_playback_trigger,
837         .pointer =      snd_ice1712_playback_pointer,
838 };
839
840 static snd_pcm_ops_t snd_ice1712_playback_ds_ops = {
841         .open =         snd_ice1712_playback_ds_open,
842         .close =        snd_ice1712_playback_ds_close,
843         .ioctl =        snd_pcm_lib_ioctl,
844         .hw_params =    snd_ice1712_hw_params,
845         .hw_free =      snd_ice1712_hw_free,
846         .prepare =      snd_ice1712_playback_ds_prepare,
847         .trigger =      snd_ice1712_playback_ds_trigger,
848         .pointer =      snd_ice1712_playback_ds_pointer,
849 };
850
851 static snd_pcm_ops_t snd_ice1712_capture_ops = {
852         .open =         snd_ice1712_capture_open,
853         .close =        snd_ice1712_capture_close,
854         .ioctl =        snd_pcm_lib_ioctl,
855         .hw_params =    snd_ice1712_hw_params,
856         .hw_free =      snd_ice1712_hw_free,
857         .prepare =      snd_ice1712_capture_prepare,
858         .trigger =      snd_ice1712_capture_trigger,
859         .pointer =      snd_ice1712_capture_pointer,
860 };
861
862 static void snd_ice1712_pcm_free(snd_pcm_t *pcm)
863 {
864         ice1712_t *ice = pcm->private_data;
865         ice->pcm = NULL;
866         snd_pcm_lib_preallocate_free_for_all(pcm);
867 }
868
869 static int __devinit snd_ice1712_pcm(ice1712_t * ice, int device, snd_pcm_t ** rpcm)
870 {
871         snd_pcm_t *pcm;
872         int err;
873
874         if (rpcm)
875                 *rpcm = NULL;
876         err = snd_pcm_new(ice->card, "ICE1712 consumer", device, 1, 1, &pcm);
877         if (err < 0)
878                 return err;
879
880         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ice1712_playback_ops);
881         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ice1712_capture_ops);
882
883         pcm->private_data = ice;
884         pcm->private_free = snd_ice1712_pcm_free;
885         pcm->info_flags = 0;
886         strcpy(pcm->name, "ICE1712 consumer");
887         ice->pcm = pcm;
888
889         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
890                                               snd_dma_pci_data(ice->pci), 64*1024, 64*1024);
891
892         if (rpcm)
893                 *rpcm = pcm;
894
895         printk(KERN_WARNING "Consumer PCM code does not work well at the moment --jk\n");
896
897         return 0;
898 }
899
900 static void snd_ice1712_pcm_free_ds(snd_pcm_t *pcm)
901 {
902         ice1712_t *ice = pcm->private_data;
903         ice->pcm_ds = NULL;
904         snd_pcm_lib_preallocate_free_for_all(pcm);
905 }
906
907 static int __devinit snd_ice1712_pcm_ds(ice1712_t * ice, int device, snd_pcm_t ** rpcm)
908 {
909         snd_pcm_t *pcm;
910         int err;
911
912         if (rpcm)
913                 *rpcm = NULL;
914         err = snd_pcm_new(ice->card, "ICE1712 consumer (DS)", device, 6, 0, &pcm);
915         if (err < 0)
916                 return err;
917
918         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ice1712_playback_ds_ops);
919
920         pcm->private_data = ice;
921         pcm->private_free = snd_ice1712_pcm_free_ds;
922         pcm->info_flags = 0;
923         strcpy(pcm->name, "ICE1712 consumer (DS)");
924         ice->pcm_ds = pcm;
925
926         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
927                                               snd_dma_pci_data(ice->pci), 64*1024, 128*1024);
928
929         if (rpcm)
930                 *rpcm = pcm;
931
932         return 0;
933 }
934
935 /*
936  *  PCM code - professional part (multitrack)
937  */
938
939 static unsigned int rates[] = { 8000, 9600, 11025, 12000, 16000, 22050, 24000,
940                                 32000, 44100, 48000, 64000, 88200, 96000 };
941
942 static snd_pcm_hw_constraint_list_t hw_constraints_rates = {
943         .count = ARRAY_SIZE(rates),
944         .list = rates,
945         .mask = 0,
946 };
947
948 static int snd_ice1712_pro_trigger(snd_pcm_substream_t *substream,
949                                    int cmd)
950 {
951         ice1712_t *ice = snd_pcm_substream_chip(substream);
952         switch (cmd) {
953         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
954         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
955         {
956                 unsigned int what;
957                 unsigned int old;
958                 if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
959                         return -EINVAL;
960                 what = ICE1712_PLAYBACK_PAUSE;
961                 snd_pcm_trigger_done(substream, substream);
962                 spin_lock(&ice->reg_lock);
963                 old = inl(ICEMT(ice, PLAYBACK_CONTROL));
964                 if (cmd == SNDRV_PCM_TRIGGER_PAUSE_PUSH)
965                         old |= what;
966                 else
967                         old &= ~what;
968                 outl(old, ICEMT(ice, PLAYBACK_CONTROL));
969                 spin_unlock(&ice->reg_lock);
970                 break;
971         }
972         case SNDRV_PCM_TRIGGER_START:
973         case SNDRV_PCM_TRIGGER_STOP:
974         {
975                 unsigned int what = 0;
976                 unsigned int old;
977                 struct list_head *pos;
978                 snd_pcm_substream_t *s;
979
980                 snd_pcm_group_for_each(pos, substream) {
981                         s = snd_pcm_group_substream_entry(pos);
982                         if (s == ice->playback_pro_substream) {
983                                 what |= ICE1712_PLAYBACK_START;
984                                 snd_pcm_trigger_done(s, substream);
985                         } else if (s == ice->capture_pro_substream) {
986                                 what |= ICE1712_CAPTURE_START_SHADOW;
987                                 snd_pcm_trigger_done(s, substream);
988                         }
989                 }
990                 spin_lock(&ice->reg_lock);
991                 old = inl(ICEMT(ice, PLAYBACK_CONTROL));
992                 if (cmd == SNDRV_PCM_TRIGGER_START)
993                         old |= what;
994                 else
995                         old &= ~what;
996                 outl(old, ICEMT(ice, PLAYBACK_CONTROL));
997                 spin_unlock(&ice->reg_lock);
998                 break;
999         }
1000         default:
1001                 return -EINVAL;
1002         }
1003         return 0;
1004 }
1005
1006 /*
1007  */
1008 static void snd_ice1712_set_pro_rate(ice1712_t *ice, unsigned int rate, int force)
1009 {
1010         unsigned long flags;
1011         unsigned char val, old;
1012         unsigned int i;
1013
1014         switch (rate) {
1015         case 8000: val = 6; break;
1016         case 9600: val = 3; break;
1017         case 11025: val = 10; break;
1018         case 12000: val = 2; break;
1019         case 16000: val = 5; break;
1020         case 22050: val = 9; break;
1021         case 24000: val = 1; break;
1022         case 32000: val = 4; break;
1023         case 44100: val = 8; break;
1024         case 48000: val = 0; break;
1025         case 64000: val = 15; break;
1026         case 88200: val = 11; break;
1027         case 96000: val = 7; break;
1028         default:
1029                 snd_BUG();
1030                 val = 0;
1031                 rate = 48000;
1032                 break;
1033         }
1034
1035         spin_lock_irqsave(&ice->reg_lock, flags);
1036         if (inb(ICEMT(ice, PLAYBACK_CONTROL)) & (ICE1712_CAPTURE_START_SHADOW|
1037                                                  ICE1712_PLAYBACK_PAUSE|
1038                                                  ICE1712_PLAYBACK_START)) {
1039               __out:
1040                 spin_unlock_irqrestore(&ice->reg_lock, flags);
1041                 return;
1042         }
1043         if (!force && is_pro_rate_locked(ice))
1044                 goto __out;
1045
1046         old = inb(ICEMT(ice, RATE));
1047         if (!force && old == val)
1048                 goto __out;
1049         outb(val, ICEMT(ice, RATE));
1050         spin_unlock_irqrestore(&ice->reg_lock, flags);
1051
1052         if (ice->gpio.set_pro_rate)
1053                 ice->gpio.set_pro_rate(ice, rate);
1054         for (i = 0; i < ice->akm_codecs; i++) {
1055                 if (ice->akm[i].ops.set_rate_val)
1056                         ice->akm[i].ops.set_rate_val(&ice->akm[i], rate);
1057         }
1058         if (ice->spdif.ops.setup_rate)
1059                 ice->spdif.ops.setup_rate(ice, rate);
1060 }
1061
1062 static int snd_ice1712_playback_pro_prepare(snd_pcm_substream_t * substream)
1063 {
1064         ice1712_t *ice = snd_pcm_substream_chip(substream);
1065
1066         ice->playback_pro_size = snd_pcm_lib_buffer_bytes(substream);
1067         spin_lock_irq(&ice->reg_lock);
1068         outl(substream->runtime->dma_addr, ICEMT(ice, PLAYBACK_ADDR));
1069         outw((ice->playback_pro_size >> 2) - 1, ICEMT(ice, PLAYBACK_SIZE));
1070         outw((snd_pcm_lib_period_bytes(substream) >> 2) - 1, ICEMT(ice, PLAYBACK_COUNT));
1071         spin_unlock_irq(&ice->reg_lock);
1072
1073         return 0;
1074 }
1075
1076 static int snd_ice1712_playback_pro_hw_params(snd_pcm_substream_t * substream,
1077                                               snd_pcm_hw_params_t * hw_params)
1078 {
1079         ice1712_t *ice = snd_pcm_substream_chip(substream);
1080
1081         snd_ice1712_set_pro_rate(ice, params_rate(hw_params), 0);
1082         return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
1083 }
1084
1085 static int snd_ice1712_capture_pro_prepare(snd_pcm_substream_t * substream)
1086 {
1087         ice1712_t *ice = snd_pcm_substream_chip(substream);
1088
1089         ice->capture_pro_size = snd_pcm_lib_buffer_bytes(substream);
1090         spin_lock_irq(&ice->reg_lock);
1091         outl(substream->runtime->dma_addr, ICEMT(ice, CAPTURE_ADDR));
1092         outw((ice->capture_pro_size >> 2) - 1, ICEMT(ice, CAPTURE_SIZE));
1093         outw((snd_pcm_lib_period_bytes(substream) >> 2) - 1, ICEMT(ice, CAPTURE_COUNT));
1094         spin_unlock_irq(&ice->reg_lock);
1095         return 0;
1096 }
1097
1098 static int snd_ice1712_capture_pro_hw_params(snd_pcm_substream_t * substream,
1099                                              snd_pcm_hw_params_t * hw_params)
1100 {
1101         ice1712_t *ice = snd_pcm_substream_chip(substream);
1102
1103         snd_ice1712_set_pro_rate(ice, params_rate(hw_params), 0);
1104         return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
1105 }
1106
1107 static snd_pcm_uframes_t snd_ice1712_playback_pro_pointer(snd_pcm_substream_t * substream)
1108 {
1109         ice1712_t *ice = snd_pcm_substream_chip(substream);
1110         size_t ptr;
1111
1112         if (!(inl(ICEMT(ice, PLAYBACK_CONTROL)) & ICE1712_PLAYBACK_START))
1113                 return 0;
1114         ptr = ice->playback_pro_size - (inw(ICEMT(ice, PLAYBACK_SIZE)) << 2);
1115         if (ptr == substream->runtime->buffer_size)
1116                 ptr = 0;
1117         return bytes_to_frames(substream->runtime, ptr);
1118 }
1119
1120 static snd_pcm_uframes_t snd_ice1712_capture_pro_pointer(snd_pcm_substream_t * substream)
1121 {
1122         ice1712_t *ice = snd_pcm_substream_chip(substream);
1123         size_t ptr;
1124
1125         if (!(inl(ICEMT(ice, PLAYBACK_CONTROL)) & ICE1712_CAPTURE_START_SHADOW))
1126                 return 0;
1127         ptr = ice->capture_pro_size - (inw(ICEMT(ice, CAPTURE_SIZE)) << 2);
1128         if (ptr == substream->runtime->buffer_size)
1129                 ptr = 0;
1130         return bytes_to_frames(substream->runtime, ptr);
1131 }
1132
1133 static snd_pcm_hardware_t snd_ice1712_playback_pro =
1134 {
1135         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1136                                  SNDRV_PCM_INFO_BLOCK_TRANSFER |
1137                                  SNDRV_PCM_INFO_MMAP_VALID |
1138                                  SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_SYNC_START),
1139         .formats =              SNDRV_PCM_FMTBIT_S32_LE,
1140         .rates =                SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_96000,
1141         .rate_min =             4000,
1142         .rate_max =             96000,
1143         .channels_min =         10,
1144         .channels_max =         10,
1145         .buffer_bytes_max =     (256*1024),
1146         .period_bytes_min =     10 * 4 * 2,
1147         .period_bytes_max =     131040,
1148         .periods_min =          1,
1149         .periods_max =          1024,
1150         .fifo_size =            0,
1151 };
1152
1153 static snd_pcm_hardware_t snd_ice1712_capture_pro =
1154 {
1155         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1156                                  SNDRV_PCM_INFO_BLOCK_TRANSFER |
1157                                  SNDRV_PCM_INFO_MMAP_VALID |
1158                                  SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_SYNC_START),
1159         .formats =              SNDRV_PCM_FMTBIT_S32_LE,
1160         .rates =                SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_96000,
1161         .rate_min =             4000,
1162         .rate_max =             96000,
1163         .channels_min =         12,
1164         .channels_max =         12,
1165         .buffer_bytes_max =     (256*1024),
1166         .period_bytes_min =     12 * 4 * 2,
1167         .period_bytes_max =     131040,
1168         .periods_min =          1,
1169         .periods_max =          1024,
1170         .fifo_size =            0,
1171 };
1172
1173 static int snd_ice1712_playback_pro_open(snd_pcm_substream_t * substream)
1174 {
1175         snd_pcm_runtime_t *runtime = substream->runtime;
1176         ice1712_t *ice = snd_pcm_substream_chip(substream);
1177
1178         ice->playback_pro_substream = substream;
1179         runtime->hw = snd_ice1712_playback_pro;
1180         snd_pcm_set_sync(substream);
1181         snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
1182         snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
1183
1184         if (ice->spdif.ops.open)
1185                 ice->spdif.ops.open(ice, substream);
1186
1187         return 0;
1188 }
1189
1190 static int snd_ice1712_capture_pro_open(snd_pcm_substream_t * substream)
1191 {
1192         ice1712_t *ice = snd_pcm_substream_chip(substream);
1193         snd_pcm_runtime_t *runtime = substream->runtime;
1194
1195         ice->capture_pro_substream = substream;
1196         runtime->hw = snd_ice1712_capture_pro;
1197         snd_pcm_set_sync(substream);
1198         snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
1199         snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
1200         return 0;
1201 }
1202
1203 static int snd_ice1712_playback_pro_close(snd_pcm_substream_t * substream)
1204 {
1205         ice1712_t *ice = snd_pcm_substream_chip(substream);
1206
1207         if (PRO_RATE_RESET)
1208                 snd_ice1712_set_pro_rate(ice, PRO_RATE_DEFAULT, 0);
1209         ice->playback_pro_substream = NULL;
1210         if (ice->spdif.ops.close)
1211                 ice->spdif.ops.close(ice, substream);
1212
1213         return 0;
1214 }
1215
1216 static int snd_ice1712_capture_pro_close(snd_pcm_substream_t * substream)
1217 {
1218         ice1712_t *ice = snd_pcm_substream_chip(substream);
1219
1220         if (PRO_RATE_RESET)
1221                 snd_ice1712_set_pro_rate(ice, PRO_RATE_DEFAULT, 0);
1222         ice->capture_pro_substream = NULL;
1223         return 0;
1224 }
1225
1226 static void snd_ice1712_pcm_profi_free(snd_pcm_t *pcm)
1227 {
1228         ice1712_t *ice = pcm->private_data;
1229         ice->pcm_pro = NULL;
1230         snd_pcm_lib_preallocate_free_for_all(pcm);
1231 }
1232
1233 static snd_pcm_ops_t snd_ice1712_playback_pro_ops = {
1234         .open =         snd_ice1712_playback_pro_open,
1235         .close =        snd_ice1712_playback_pro_close,
1236         .ioctl =        snd_pcm_lib_ioctl,
1237         .hw_params =    snd_ice1712_playback_pro_hw_params,
1238         .hw_free =      snd_ice1712_hw_free,
1239         .prepare =      snd_ice1712_playback_pro_prepare,
1240         .trigger =      snd_ice1712_pro_trigger,
1241         .pointer =      snd_ice1712_playback_pro_pointer,
1242 };
1243
1244 static snd_pcm_ops_t snd_ice1712_capture_pro_ops = {
1245         .open =         snd_ice1712_capture_pro_open,
1246         .close =        snd_ice1712_capture_pro_close,
1247         .ioctl =        snd_pcm_lib_ioctl,
1248         .hw_params =    snd_ice1712_capture_pro_hw_params,
1249         .hw_free =      snd_ice1712_hw_free,
1250         .prepare =      snd_ice1712_capture_pro_prepare,
1251         .trigger =      snd_ice1712_pro_trigger,
1252         .pointer =      snd_ice1712_capture_pro_pointer,
1253 };
1254
1255 static int __devinit snd_ice1712_pcm_profi(ice1712_t * ice, int device, snd_pcm_t ** rpcm)
1256 {
1257         snd_pcm_t *pcm;
1258         int err;
1259
1260         if (rpcm)
1261                 *rpcm = NULL;
1262         err = snd_pcm_new(ice->card, "ICE1712 multi", device, 1, 1, &pcm);
1263         if (err < 0)
1264                 return err;
1265
1266         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ice1712_playback_pro_ops);
1267         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ice1712_capture_pro_ops);
1268
1269         pcm->private_data = ice;
1270         pcm->private_free = snd_ice1712_pcm_profi_free;
1271         pcm->info_flags = 0;
1272         strcpy(pcm->name, "ICE1712 multi");
1273
1274         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1275                                               snd_dma_pci_data(ice->pci), 256*1024, 256*1024);
1276
1277         ice->pcm_pro = pcm;
1278         if (rpcm)
1279                 *rpcm = pcm;
1280         
1281         if (ice->cs8427) {
1282                 /* assign channels to iec958 */
1283                 err = snd_cs8427_iec958_build(ice->cs8427,
1284                                               pcm->streams[0].substream,
1285                                               pcm->streams[1].substream);
1286                 if (err < 0)
1287                         return err;
1288         }
1289
1290         if ((err = snd_ice1712_build_pro_mixer(ice)) < 0)
1291                 return err;
1292         return 0;
1293 }
1294
1295 /*
1296  *  Mixer section
1297  */
1298
1299 static void snd_ice1712_update_volume(ice1712_t *ice, int index)
1300 {
1301         unsigned int vol = ice->pro_volumes[index];
1302         unsigned short val = 0;
1303
1304         val |= (vol & 0x8000) == 0 ? (96 - (vol & 0x7f)) : 0x7f;
1305         val |= ((vol & 0x80000000) == 0 ? (96 - ((vol >> 16) & 0x7f)) : 0x7f) << 8;
1306         outb(index, ICEMT(ice, MONITOR_INDEX));
1307         outw(val, ICEMT(ice, MONITOR_VOLUME));
1308 }
1309
1310 static int snd_ice1712_pro_mixer_switch_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1311 {
1312         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1313         uinfo->count = 2;
1314         uinfo->value.integer.min = 0;
1315         uinfo->value.integer.max = 1;
1316         return 0;
1317 }
1318
1319 static int snd_ice1712_pro_mixer_switch_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1320 {
1321         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1322         int index = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id) + kcontrol->private_value;
1323         
1324         spin_lock_irq(&ice->reg_lock);
1325         ucontrol->value.integer.value[0] = !((ice->pro_volumes[index] >> 15) & 1);
1326         ucontrol->value.integer.value[1] = !((ice->pro_volumes[index] >> 31) & 1);
1327         spin_unlock_irq(&ice->reg_lock);
1328         return 0;
1329 }
1330
1331 static int snd_ice1712_pro_mixer_switch_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1332 {
1333         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1334         int index = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id) + kcontrol->private_value;
1335         unsigned int nval, change;
1336
1337         nval = (ucontrol->value.integer.value[0] ? 0 : 0x00008000) |
1338                (ucontrol->value.integer.value[1] ? 0 : 0x80000000);
1339         spin_lock_irq(&ice->reg_lock);
1340         nval |= ice->pro_volumes[index] & ~0x80008000;
1341         change = nval != ice->pro_volumes[index];
1342         ice->pro_volumes[index] = nval;
1343         snd_ice1712_update_volume(ice, index);
1344         spin_unlock_irq(&ice->reg_lock);
1345         return change;
1346 }
1347
1348 static int snd_ice1712_pro_mixer_volume_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1349 {
1350         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1351         uinfo->count = 2;
1352         uinfo->value.integer.min = 0;
1353         uinfo->value.integer.max = 96;
1354         return 0;
1355 }
1356
1357 static int snd_ice1712_pro_mixer_volume_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1358 {
1359         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1360         int index = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id) + kcontrol->private_value;
1361         
1362         spin_lock_irq(&ice->reg_lock);
1363         ucontrol->value.integer.value[0] = (ice->pro_volumes[index] >> 0) & 127;
1364         ucontrol->value.integer.value[1] = (ice->pro_volumes[index] >> 16) & 127;
1365         spin_unlock_irq(&ice->reg_lock);
1366         return 0;
1367 }
1368
1369 static int snd_ice1712_pro_mixer_volume_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1370 {
1371         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1372         int index = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id) + kcontrol->private_value;
1373         unsigned int nval, change;
1374
1375         nval = (ucontrol->value.integer.value[0] & 127) |
1376                ((ucontrol->value.integer.value[1] & 127) << 16);
1377         spin_lock_irq(&ice->reg_lock);
1378         nval |= ice->pro_volumes[index] & ~0x007f007f;
1379         change = nval != ice->pro_volumes[index];
1380         ice->pro_volumes[index] = nval;
1381         snd_ice1712_update_volume(ice, index);
1382         spin_unlock_irq(&ice->reg_lock);
1383         return change;
1384 }
1385
1386
1387 static snd_kcontrol_new_t snd_ice1712_multi_playback_ctrls[] __devinitdata = {
1388         {
1389                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1390                 .name = "Multi Playback Switch",
1391                 .info = snd_ice1712_pro_mixer_switch_info,
1392                 .get = snd_ice1712_pro_mixer_switch_get,
1393                 .put = snd_ice1712_pro_mixer_switch_put,
1394                 .private_value = 0,
1395                 .count = 10,
1396         },
1397         {
1398                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1399                 .name = "Multi Playback Volume",
1400                 .info = snd_ice1712_pro_mixer_volume_info,
1401                 .get = snd_ice1712_pro_mixer_volume_get,
1402                 .put = snd_ice1712_pro_mixer_volume_put,
1403                 .private_value = 0,
1404                 .count = 10,
1405         },
1406 };
1407
1408 static snd_kcontrol_new_t snd_ice1712_multi_capture_analog_switch __devinitdata = {
1409         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1410         .name = "H/W Multi Capture Switch",
1411         .info = snd_ice1712_pro_mixer_switch_info,
1412         .get = snd_ice1712_pro_mixer_switch_get,
1413         .put = snd_ice1712_pro_mixer_switch_put,
1414         .private_value = 10,
1415 };
1416
1417 static snd_kcontrol_new_t snd_ice1712_multi_capture_spdif_switch __devinitdata = {
1418         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1419         .name = SNDRV_CTL_NAME_IEC958("Multi ",CAPTURE,SWITCH),
1420         .info = snd_ice1712_pro_mixer_switch_info,
1421         .get = snd_ice1712_pro_mixer_switch_get,
1422         .put = snd_ice1712_pro_mixer_switch_put,
1423         .private_value = 18,
1424         .count = 2,
1425 };
1426
1427 static snd_kcontrol_new_t snd_ice1712_multi_capture_analog_volume __devinitdata = {
1428         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1429         .name = "H/W Multi Capture Volume",
1430         .info = snd_ice1712_pro_mixer_volume_info,
1431         .get = snd_ice1712_pro_mixer_volume_get,
1432         .put = snd_ice1712_pro_mixer_volume_put,
1433         .private_value = 10,
1434 };
1435
1436 static snd_kcontrol_new_t snd_ice1712_multi_capture_spdif_volume __devinitdata = {
1437         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1438         .name = SNDRV_CTL_NAME_IEC958("Multi ",CAPTURE,VOLUME),
1439         .info = snd_ice1712_pro_mixer_volume_info,
1440         .get = snd_ice1712_pro_mixer_volume_get,
1441         .put = snd_ice1712_pro_mixer_volume_put,
1442         .private_value = 18,
1443         .count = 2,
1444 };
1445
1446 static int __devinit snd_ice1712_build_pro_mixer(ice1712_t *ice)
1447 {
1448         snd_card_t * card = ice->card;
1449         unsigned int idx;
1450         int err;
1451
1452         /* multi-channel mixer */
1453         for (idx = 0; idx < ARRAY_SIZE(snd_ice1712_multi_playback_ctrls); idx++) {
1454                 err = snd_ctl_add(card, snd_ctl_new1(&snd_ice1712_multi_playback_ctrls[idx], ice));
1455                 if (err < 0)
1456                         return err;
1457         }
1458         
1459         if (ice->num_total_adcs > 0) {
1460                 snd_kcontrol_new_t tmp = snd_ice1712_multi_capture_analog_switch;
1461                 tmp.count = ice->num_total_adcs;
1462                 err = snd_ctl_add(card, snd_ctl_new1(&tmp, ice));
1463                 if (err < 0)
1464                         return err;
1465         }
1466
1467         err = snd_ctl_add(card, snd_ctl_new1(&snd_ice1712_multi_capture_spdif_switch, ice));
1468         if (err < 0)
1469                 return err;
1470
1471         if (ice->num_total_adcs > 0) {
1472                 snd_kcontrol_new_t tmp = snd_ice1712_multi_capture_analog_volume;
1473                 tmp.count = ice->num_total_adcs;
1474                 err = snd_ctl_add(card, snd_ctl_new1(&tmp, ice));
1475                 if (err < 0)
1476                         return err;
1477         }
1478
1479         err = snd_ctl_add(card, snd_ctl_new1(&snd_ice1712_multi_capture_spdif_volume, ice));
1480         if (err < 0)
1481                 return err;
1482
1483         /* initialize volumes */
1484         for (idx = 0; idx < 10; idx++) {
1485                 ice->pro_volumes[idx] = 0x80008000;     /* mute */
1486                 snd_ice1712_update_volume(ice, idx);
1487         }
1488         for (idx = 10; idx < 10 + ice->num_total_adcs; idx++) {
1489                 ice->pro_volumes[idx] = 0x80008000;     /* mute */
1490                 snd_ice1712_update_volume(ice, idx);
1491         }
1492         for (idx = 18; idx < 20; idx++) {
1493                 ice->pro_volumes[idx] = 0x80008000;     /* mute */
1494                 snd_ice1712_update_volume(ice, idx);
1495         }
1496         return 0;
1497 }
1498
1499 static void snd_ice1712_mixer_free_ac97(ac97_t *ac97)
1500 {
1501         ice1712_t *ice = ac97->private_data;
1502         ice->ac97 = NULL;
1503 }
1504
1505 static int __devinit snd_ice1712_ac97_mixer(ice1712_t * ice)
1506 {
1507         int err, bus_num = 0;
1508         ac97_template_t ac97;
1509         ac97_bus_t *pbus;
1510         static ac97_bus_ops_t con_ops = {
1511                 .write = snd_ice1712_ac97_write,
1512                 .read = snd_ice1712_ac97_read,
1513         };
1514         static ac97_bus_ops_t pro_ops = {
1515                 .write = snd_ice1712_pro_ac97_write,
1516                 .read = snd_ice1712_pro_ac97_read,
1517         };
1518
1519         if (ice_has_con_ac97(ice)) {
1520                 if ((err = snd_ac97_bus(ice->card, bus_num++, &con_ops, NULL, &pbus)) < 0)
1521                         return err;
1522                 memset(&ac97, 0, sizeof(ac97));
1523                 ac97.private_data = ice;
1524                 ac97.private_free = snd_ice1712_mixer_free_ac97;
1525                 if ((err = snd_ac97_mixer(pbus, &ac97, &ice->ac97)) < 0)
1526                         printk(KERN_WARNING "ice1712: cannot initialize ac97 for consumer, skipped\n");
1527                 else {
1528                         if ((err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_ice1712_mixer_digmix_route_ac97, ice))) < 0)
1529                                 return err;
1530                         return 0;
1531                 }
1532         }
1533
1534         if (! (ice->eeprom.data[ICE_EEP1_ACLINK] & ICE1712_CFG_PRO_I2S)) {
1535                 if ((err = snd_ac97_bus(ice->card, bus_num, &pro_ops, NULL, &pbus)) < 0)
1536                         return err;
1537                 memset(&ac97, 0, sizeof(ac97));
1538                 ac97.private_data = ice;
1539                 ac97.private_free = snd_ice1712_mixer_free_ac97;
1540                 if ((err = snd_ac97_mixer(pbus, &ac97, &ice->ac97)) < 0)
1541                         printk(KERN_WARNING "ice1712: cannot initialize pro ac97, skipped\n");
1542                 else
1543                         return 0;
1544         }
1545         /* I2S mixer only */
1546         strcat(ice->card->mixername, "ICE1712 - multitrack");
1547         return 0;
1548 }
1549
1550 /*
1551  *
1552  */
1553
1554 static inline unsigned int eeprom_double(ice1712_t *ice, int idx)
1555 {
1556         return (unsigned int)ice->eeprom.data[idx] | ((unsigned int)ice->eeprom.data[idx + 1] << 8);
1557 }
1558
1559 static void snd_ice1712_proc_read(snd_info_entry_t *entry, 
1560                                   snd_info_buffer_t * buffer)
1561 {
1562         ice1712_t *ice = entry->private_data;
1563         unsigned int idx;
1564
1565         snd_iprintf(buffer, "%s\n\n", ice->card->longname);
1566         snd_iprintf(buffer, "EEPROM:\n");
1567
1568         snd_iprintf(buffer, "  Subvendor        : 0x%x\n", ice->eeprom.subvendor);
1569         snd_iprintf(buffer, "  Size             : %i bytes\n", ice->eeprom.size);
1570         snd_iprintf(buffer, "  Version          : %i\n", ice->eeprom.version);
1571         snd_iprintf(buffer, "  Codec            : 0x%x\n", ice->eeprom.data[ICE_EEP1_CODEC]);
1572         snd_iprintf(buffer, "  ACLink           : 0x%x\n", ice->eeprom.data[ICE_EEP1_ACLINK]);
1573         snd_iprintf(buffer, "  I2S ID           : 0x%x\n", ice->eeprom.data[ICE_EEP1_I2SID]);
1574         snd_iprintf(buffer, "  S/PDIF           : 0x%x\n", ice->eeprom.data[ICE_EEP1_SPDIF]);
1575         snd_iprintf(buffer, "  GPIO mask        : 0x%x\n", ice->eeprom.gpiomask);
1576         snd_iprintf(buffer, "  GPIO state       : 0x%x\n", ice->eeprom.gpiostate);
1577         snd_iprintf(buffer, "  GPIO direction   : 0x%x\n", ice->eeprom.gpiodir);
1578         snd_iprintf(buffer, "  AC'97 main       : 0x%x\n", eeprom_double(ice, ICE_EEP1_AC97_MAIN_LO));
1579         snd_iprintf(buffer, "  AC'97 pcm        : 0x%x\n", eeprom_double(ice, ICE_EEP1_AC97_PCM_LO));
1580         snd_iprintf(buffer, "  AC'97 record     : 0x%x\n", eeprom_double(ice, ICE_EEP1_AC97_REC_LO));
1581         snd_iprintf(buffer, "  AC'97 record src : 0x%x\n", ice->eeprom.data[ICE_EEP1_AC97_RECSRC]);
1582         for (idx = 0; idx < 4; idx++)
1583                 snd_iprintf(buffer, "  DAC ID #%i        : 0x%x\n", idx, ice->eeprom.data[ICE_EEP1_DAC_ID + idx]);
1584         for (idx = 0; idx < 4; idx++)
1585                 snd_iprintf(buffer, "  ADC ID #%i        : 0x%x\n", idx, ice->eeprom.data[ICE_EEP1_ADC_ID + idx]);
1586         for (idx = 0x1c; idx < ice->eeprom.size; idx++)
1587                 snd_iprintf(buffer, "  Extra #%02i        : 0x%x\n", idx, ice->eeprom.data[idx]);
1588
1589         snd_iprintf(buffer, "\nRegisters:\n");
1590         snd_iprintf(buffer, "  PSDOUT03         : 0x%04x\n", (unsigned)inw(ICEMT(ice, ROUTE_PSDOUT03)));
1591         snd_iprintf(buffer, "  CAPTURE          : 0x%08x\n", inl(ICEMT(ice, ROUTE_CAPTURE)));
1592         snd_iprintf(buffer, "  SPDOUT           : 0x%04x\n", (unsigned)inw(ICEMT(ice, ROUTE_SPDOUT)));
1593         snd_iprintf(buffer, "  RATE             : 0x%02x\n", (unsigned)inb(ICEMT(ice, RATE)));
1594 }
1595
1596 static void __devinit snd_ice1712_proc_init(ice1712_t * ice)
1597 {
1598         snd_info_entry_t *entry;
1599
1600         if (! snd_card_proc_new(ice->card, "ice1712", &entry))
1601                 snd_info_set_text_ops(entry, ice, 1024, snd_ice1712_proc_read);
1602 }
1603
1604 /*
1605  *
1606  */
1607
1608 static int snd_ice1712_eeprom_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1609 {
1610         uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
1611         uinfo->count = sizeof(ice1712_eeprom_t);
1612         return 0;
1613 }
1614
1615 static int snd_ice1712_eeprom_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1616 {
1617         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1618         
1619         memcpy(ucontrol->value.bytes.data, &ice->eeprom, sizeof(ice->eeprom));
1620         return 0;
1621 }
1622
1623 static snd_kcontrol_new_t snd_ice1712_eeprom __devinitdata = {
1624         .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1625         .name = "ICE1712 EEPROM",
1626         .access = SNDRV_CTL_ELEM_ACCESS_READ,
1627         .info = snd_ice1712_eeprom_info,
1628         .get = snd_ice1712_eeprom_get
1629 };
1630
1631 /*
1632  */
1633 static int snd_ice1712_spdif_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1634 {
1635         uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1636         uinfo->count = 1;
1637         return 0;
1638 }
1639
1640 static int snd_ice1712_spdif_default_get(snd_kcontrol_t * kcontrol,
1641                                          snd_ctl_elem_value_t * ucontrol)
1642 {
1643         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1644         if (ice->spdif.ops.default_get)
1645                 ice->spdif.ops.default_get(ice, ucontrol); 
1646         return 0;
1647 }
1648
1649 static int snd_ice1712_spdif_default_put(snd_kcontrol_t * kcontrol,
1650                                          snd_ctl_elem_value_t * ucontrol)
1651 {
1652         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1653         if (ice->spdif.ops.default_put)
1654                 return ice->spdif.ops.default_put(ice, ucontrol);
1655         return 0;
1656 }
1657
1658 static snd_kcontrol_new_t snd_ice1712_spdif_default __devinitdata =
1659 {
1660         .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
1661         .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1662         .info =         snd_ice1712_spdif_info,
1663         .get =          snd_ice1712_spdif_default_get,
1664         .put =          snd_ice1712_spdif_default_put
1665 };
1666
1667 static int snd_ice1712_spdif_maskc_get(snd_kcontrol_t * kcontrol,
1668                                        snd_ctl_elem_value_t * ucontrol)
1669 {
1670         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1671         if (ice->spdif.ops.default_get) {
1672                 ucontrol->value.iec958.status[0] = IEC958_AES0_NONAUDIO |
1673                                                      IEC958_AES0_PROFESSIONAL |
1674                                                      IEC958_AES0_CON_NOT_COPYRIGHT |
1675                                                      IEC958_AES0_CON_EMPHASIS;
1676                 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_ORIGINAL |
1677                                                      IEC958_AES1_CON_CATEGORY;
1678                 ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS;
1679         } else {
1680                 ucontrol->value.iec958.status[0] = 0xff;
1681                 ucontrol->value.iec958.status[1] = 0xff;
1682                 ucontrol->value.iec958.status[2] = 0xff;
1683                 ucontrol->value.iec958.status[3] = 0xff;
1684                 ucontrol->value.iec958.status[4] = 0xff;
1685         }
1686         return 0;
1687 }
1688
1689 static int snd_ice1712_spdif_maskp_get(snd_kcontrol_t * kcontrol,
1690                                        snd_ctl_elem_value_t * ucontrol)
1691 {
1692         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1693         if (ice->spdif.ops.default_get) {
1694                 ucontrol->value.iec958.status[0] = IEC958_AES0_NONAUDIO |
1695                                                      IEC958_AES0_PROFESSIONAL |
1696                                                      IEC958_AES0_PRO_FS |
1697                                                      IEC958_AES0_PRO_EMPHASIS;
1698                 ucontrol->value.iec958.status[1] = IEC958_AES1_PRO_MODE;
1699         } else {
1700                 ucontrol->value.iec958.status[0] = 0xff;
1701                 ucontrol->value.iec958.status[1] = 0xff;
1702                 ucontrol->value.iec958.status[2] = 0xff;
1703                 ucontrol->value.iec958.status[3] = 0xff;
1704                 ucontrol->value.iec958.status[4] = 0xff;
1705         }
1706         return 0;
1707 }
1708
1709 static snd_kcontrol_new_t snd_ice1712_spdif_maskc __devinitdata =
1710 {
1711         .access =       SNDRV_CTL_ELEM_ACCESS_READ,
1712         .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
1713         .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1714         .info =         snd_ice1712_spdif_info,
1715         .get =          snd_ice1712_spdif_maskc_get,
1716 };
1717
1718 static snd_kcontrol_new_t snd_ice1712_spdif_maskp __devinitdata =
1719 {
1720         .access =       SNDRV_CTL_ELEM_ACCESS_READ,
1721         .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
1722         .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
1723         .info =         snd_ice1712_spdif_info,
1724         .get =          snd_ice1712_spdif_maskp_get,
1725 };
1726
1727 static int snd_ice1712_spdif_stream_get(snd_kcontrol_t * kcontrol,
1728                                         snd_ctl_elem_value_t * ucontrol)
1729 {
1730         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1731         if (ice->spdif.ops.stream_get)
1732                 ice->spdif.ops.stream_get(ice, ucontrol);
1733         return 0;
1734 }
1735
1736 static int snd_ice1712_spdif_stream_put(snd_kcontrol_t * kcontrol,
1737                                         snd_ctl_elem_value_t * ucontrol)
1738 {
1739         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1740         if (ice->spdif.ops.stream_put)
1741                 return ice->spdif.ops.stream_put(ice, ucontrol);
1742         return 0;
1743 }
1744
1745 static snd_kcontrol_new_t snd_ice1712_spdif_stream __devinitdata =
1746 {
1747         .access =       SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
1748         .iface =        SNDRV_CTL_ELEM_IFACE_PCM,
1749         .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
1750         .info =         snd_ice1712_spdif_info,
1751         .get =          snd_ice1712_spdif_stream_get,
1752         .put =          snd_ice1712_spdif_stream_put
1753 };
1754
1755 int snd_ice1712_gpio_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1756 {
1757         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1758         uinfo->count = 1;
1759         uinfo->value.integer.min = 0;
1760         uinfo->value.integer.max = 1;
1761         return 0;
1762 }
1763
1764 int snd_ice1712_gpio_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1765 {
1766         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1767         unsigned char mask = kcontrol->private_value & 0xff;
1768         int invert = (kcontrol->private_value & (1<<24)) ? 1 : 0;
1769         
1770         snd_ice1712_save_gpio_status(ice);
1771         ucontrol->value.integer.value[0] = (snd_ice1712_gpio_read(ice) & mask ? 1 : 0) ^ invert;
1772         snd_ice1712_restore_gpio_status(ice);
1773         return 0;
1774 }
1775
1776 int snd_ice1712_gpio_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1777 {
1778         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1779         unsigned char mask = kcontrol->private_value & 0xff;
1780         int invert = (kcontrol->private_value & (1<<24)) ? mask : 0;
1781         unsigned int val, nval;
1782
1783         if (kcontrol->private_value & (1 << 31))
1784                 return -EPERM;
1785         nval = (ucontrol->value.integer.value[0] ? mask : 0) ^ invert;
1786         snd_ice1712_save_gpio_status(ice);
1787         val = snd_ice1712_gpio_read(ice);
1788         nval |= val & ~mask;
1789         if (val != nval)
1790                 snd_ice1712_gpio_write(ice, nval);
1791         snd_ice1712_restore_gpio_status(ice);
1792         return val != nval;
1793 }
1794
1795 /*
1796  *  rate
1797  */
1798 static int snd_ice1712_pro_internal_clock_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1799 {
1800         static char *texts[] = {
1801                 "8000",         /* 0: 6 */
1802                 "9600",         /* 1: 3 */
1803                 "11025",        /* 2: 10 */
1804                 "12000",        /* 3: 2 */
1805                 "16000",        /* 4: 5 */
1806                 "22050",        /* 5: 9 */
1807                 "24000",        /* 6: 1 */
1808                 "32000",        /* 7: 4 */
1809                 "44100",        /* 8: 8 */
1810                 "48000",        /* 9: 0 */
1811                 "64000",        /* 10: 15 */
1812                 "88200",        /* 11: 11 */
1813                 "96000",        /* 12: 7 */
1814                 "IEC958 Input", /* 13: -- */
1815         };
1816         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1817         uinfo->count = 1;
1818         uinfo->value.enumerated.items = 14;
1819         if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1820                 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
1821         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1822         return 0;
1823 }
1824
1825 static int snd_ice1712_pro_internal_clock_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1826 {
1827         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1828         static unsigned char xlate[16] = {
1829                 9, 6, 3, 1, 7, 4, 0, 12, 8, 5, 2, 11, 255, 255, 255, 10
1830         };
1831         unsigned char val;
1832         
1833         spin_lock_irq(&ice->reg_lock);
1834         if (is_spdif_master(ice)) {
1835                 ucontrol->value.enumerated.item[0] = 13;
1836         } else {
1837                 val = xlate[inb(ICEMT(ice, RATE)) & 15];
1838                 if (val == 255) {
1839                         snd_BUG();
1840                         val = 0;
1841                 }
1842                 ucontrol->value.enumerated.item[0] = val;
1843         }
1844         spin_unlock_irq(&ice->reg_lock);
1845         return 0;
1846 }
1847
1848 static int snd_ice1712_pro_internal_clock_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1849 {
1850         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1851         static unsigned int xrate[13] = {
1852                 8000, 9600, 11025, 12000, 1600, 22050, 24000,
1853                 32000, 44100, 48000, 64000, 88200, 96000
1854         };
1855         unsigned char oval;
1856         int change = 0;
1857
1858         spin_lock_irq(&ice->reg_lock);
1859         oval = inb(ICEMT(ice, RATE));
1860         if (ucontrol->value.enumerated.item[0] == 13) {
1861                 outb(oval | ICE1712_SPDIF_MASTER, ICEMT(ice, RATE));
1862         } else {
1863                 PRO_RATE_DEFAULT = xrate[ucontrol->value.integer.value[0] % 13];
1864                 spin_unlock_irq(&ice->reg_lock);
1865                 snd_ice1712_set_pro_rate(ice, PRO_RATE_DEFAULT, 1);
1866                 spin_lock_irq(&ice->reg_lock);
1867         }
1868         change = inb(ICEMT(ice, RATE)) != oval;
1869         spin_unlock_irq(&ice->reg_lock);
1870
1871         if ((oval & ICE1712_SPDIF_MASTER) != (inb(ICEMT(ice, RATE)) & ICE1712_SPDIF_MASTER)) {
1872                 /* change CS8427 clock source too */
1873                 if (ice->cs8427) {
1874                         snd_ice1712_cs8427_set_input_clock(ice, is_spdif_master(ice));
1875                 }
1876                 /* notify ak4524 chip as well */
1877                 if (is_spdif_master(ice)) {
1878                         unsigned int i;
1879                         for (i = 0; i < ice->akm_codecs; i++) {
1880                                 if (ice->akm[i].ops.set_rate_val)
1881                                         ice->akm[i].ops.set_rate_val(&ice->akm[i], 0);
1882                         }
1883                 }
1884         }
1885
1886         return change;
1887 }
1888
1889 static snd_kcontrol_new_t snd_ice1712_pro_internal_clock __devinitdata = {
1890         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1891         .name = "Multi Track Internal Clock",
1892         .info = snd_ice1712_pro_internal_clock_info,
1893         .get = snd_ice1712_pro_internal_clock_get,
1894         .put = snd_ice1712_pro_internal_clock_put
1895 };
1896
1897 static int snd_ice1712_pro_internal_clock_default_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1898 {
1899         static char *texts[] = {
1900                 "8000",         /* 0: 6 */
1901                 "9600",         /* 1: 3 */
1902                 "11025",        /* 2: 10 */
1903                 "12000",        /* 3: 2 */
1904                 "16000",        /* 4: 5 */
1905                 "22050",        /* 5: 9 */
1906                 "24000",        /* 6: 1 */
1907                 "32000",        /* 7: 4 */
1908                 "44100",        /* 8: 8 */
1909                 "48000",        /* 9: 0 */
1910                 "64000",        /* 10: 15 */
1911                 "88200",        /* 11: 11 */
1912                 "96000",        /* 12: 7 */
1913                 // "IEC958 Input",      /* 13: -- */
1914         };
1915         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1916         uinfo->count = 1;
1917         uinfo->value.enumerated.items = 13;
1918         if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1919                 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
1920         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1921         return 0;
1922 }
1923
1924 static int snd_ice1712_pro_internal_clock_default_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1925 {
1926         int val;
1927         static unsigned int xrate[13] = {
1928                 8000, 9600, 11025, 12000, 1600, 22050, 24000,
1929                 32000, 44100, 48000, 64000, 88200, 96000
1930         };
1931
1932         for (val = 0; val < 13; val++) {
1933                 if (xrate[val] == PRO_RATE_DEFAULT)
1934                         break;
1935         }
1936
1937         ucontrol->value.enumerated.item[0] = val;
1938         return 0;
1939 }
1940
1941 static int snd_ice1712_pro_internal_clock_default_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1942 {
1943         static unsigned int xrate[13] = {
1944                 8000, 9600, 11025, 12000, 1600, 22050, 24000,
1945                 32000, 44100, 48000, 64000, 88200, 96000
1946         };
1947         unsigned char oval;
1948         int change = 0;
1949
1950         oval = PRO_RATE_DEFAULT;
1951         PRO_RATE_DEFAULT = xrate[ucontrol->value.integer.value[0] % 13];
1952         change = PRO_RATE_DEFAULT != oval;
1953
1954         return change;
1955 }
1956
1957 static snd_kcontrol_new_t snd_ice1712_pro_internal_clock_default __devinitdata = {
1958         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1959         .name = "Multi Track Internal Clock Default",
1960         .info = snd_ice1712_pro_internal_clock_default_info,
1961         .get = snd_ice1712_pro_internal_clock_default_get,
1962         .put = snd_ice1712_pro_internal_clock_default_put
1963 };
1964
1965 static int snd_ice1712_pro_rate_locking_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1966 {
1967         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1968         uinfo->count = 1;
1969         uinfo->value.integer.min = 0;
1970         uinfo->value.integer.max = 1;
1971         return 0;
1972 }
1973
1974 static int snd_ice1712_pro_rate_locking_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1975 {
1976         ucontrol->value.integer.value[0] = PRO_RATE_LOCKED;
1977         return 0;
1978 }
1979
1980 static int snd_ice1712_pro_rate_locking_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1981 {
1982         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
1983         int change = 0, nval;
1984
1985         nval = ucontrol->value.integer.value[0] ? 1 : 0;
1986         spin_lock_irq(&ice->reg_lock);
1987         change = PRO_RATE_LOCKED != nval;
1988         PRO_RATE_LOCKED = nval;
1989         spin_unlock_irq(&ice->reg_lock);
1990         return change;
1991 }
1992
1993 static snd_kcontrol_new_t snd_ice1712_pro_rate_locking __devinitdata = {
1994         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1995         .name = "Multi Track Rate Locking",
1996         .info = snd_ice1712_pro_rate_locking_info,
1997         .get = snd_ice1712_pro_rate_locking_get,
1998         .put = snd_ice1712_pro_rate_locking_put
1999 };
2000
2001 static int snd_ice1712_pro_rate_reset_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
2002 {
2003         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2004         uinfo->count = 1;
2005         uinfo->value.integer.min = 0;
2006         uinfo->value.integer.max = 1;
2007         return 0;
2008 }
2009
2010 static int snd_ice1712_pro_rate_reset_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
2011 {
2012         ucontrol->value.integer.value[0] = PRO_RATE_RESET;
2013         return 0;
2014 }
2015
2016 static int snd_ice1712_pro_rate_reset_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
2017 {
2018         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
2019         int change = 0, nval;
2020
2021         nval = ucontrol->value.integer.value[0] ? 1 : 0;
2022         spin_lock_irq(&ice->reg_lock);
2023         change = PRO_RATE_RESET != nval;
2024         PRO_RATE_RESET = nval;
2025         spin_unlock_irq(&ice->reg_lock);
2026         return change;
2027 }
2028
2029 static snd_kcontrol_new_t snd_ice1712_pro_rate_reset __devinitdata = {
2030         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2031         .name = "Multi Track Rate Reset",
2032         .info = snd_ice1712_pro_rate_reset_info,
2033         .get = snd_ice1712_pro_rate_reset_get,
2034         .put = snd_ice1712_pro_rate_reset_put
2035 };
2036
2037 /*
2038  * routing
2039  */
2040 static int snd_ice1712_pro_route_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
2041 {
2042         static char *texts[] = {
2043                 "PCM Out", /* 0 */
2044                 "H/W In 0", "H/W In 1", "H/W In 2", "H/W In 3", /* 1-4 */
2045                 "H/W In 4", "H/W In 5", "H/W In 6", "H/W In 7", /* 5-8 */
2046                 "IEC958 In L", "IEC958 In R", /* 9-10 */
2047                 "Digital Mixer", /* 11 - optional */
2048         };
2049         
2050         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2051         uinfo->count = 1;
2052         uinfo->value.enumerated.items = snd_ctl_get_ioffidx(kcontrol, &uinfo->id) < 2 ? 12 : 11;
2053         if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2054                 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2055         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2056         return 0;
2057 }
2058
2059 static int snd_ice1712_pro_route_analog_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t *ucontrol)
2060 {
2061         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
2062         int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2063         unsigned int val, cval;
2064
2065         spin_lock_irq(&ice->reg_lock);
2066         val = inw(ICEMT(ice, ROUTE_PSDOUT03));
2067         cval = inl(ICEMT(ice, ROUTE_CAPTURE));
2068         spin_unlock_irq(&ice->reg_lock);
2069
2070         val >>= ((idx % 2) * 8) + ((idx / 2) * 2);
2071         val &= 3;
2072         cval >>= ((idx / 2) * 8) + ((idx % 2) * 4);
2073         if (val == 1 && idx < 2)
2074                 ucontrol->value.enumerated.item[0] = 11;
2075         else if (val == 2)
2076                 ucontrol->value.enumerated.item[0] = (cval & 7) + 1;
2077         else if (val == 3)
2078                 ucontrol->value.enumerated.item[0] = ((cval >> 3) & 1) + 9;
2079         else
2080                 ucontrol->value.enumerated.item[0] = 0;
2081         return 0;
2082 }
2083
2084 static int snd_ice1712_pro_route_analog_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t *ucontrol)
2085 {
2086         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
2087         int change, shift;
2088         int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2089         unsigned int val, old_val, nval;
2090         
2091         /* update PSDOUT */
2092         if (ucontrol->value.enumerated.item[0] >= 11)
2093                 nval = idx < 2 ? 1 : 0; /* dig mixer (or pcm) */
2094         else if (ucontrol->value.enumerated.item[0] >= 9)
2095                 nval = 3; /* spdif in */
2096         else if (ucontrol->value.enumerated.item[0] >= 1)
2097                 nval = 2; /* analog in */
2098         else
2099                 nval = 0; /* pcm */
2100         shift = ((idx % 2) * 8) + ((idx / 2) * 2);
2101         spin_lock_irq(&ice->reg_lock);
2102         val = old_val = inw(ICEMT(ice, ROUTE_PSDOUT03));
2103         val &= ~(0x03 << shift);
2104         val |= nval << shift;
2105         change = val != old_val;
2106         if (change)
2107                 outw(val, ICEMT(ice, ROUTE_PSDOUT03));
2108         spin_unlock_irq(&ice->reg_lock);
2109         if (nval < 2) /* dig mixer of pcm */
2110                 return change;
2111
2112         /* update CAPTURE */
2113         spin_lock_irq(&ice->reg_lock);
2114         val = old_val = inl(ICEMT(ice, ROUTE_CAPTURE));
2115         shift = ((idx / 2) * 8) + ((idx % 2) * 4);
2116         if (nval == 2) { /* analog in */
2117                 nval = ucontrol->value.enumerated.item[0] - 1;
2118                 val &= ~(0x07 << shift);
2119                 val |= nval << shift;
2120         } else { /* spdif in */
2121                 nval = (ucontrol->value.enumerated.item[0] - 9) << 3;
2122                 val &= ~(0x08 << shift);
2123                 val |= nval << shift;
2124         }
2125         if (val != old_val) {
2126                 change = 1;
2127                 outl(val, ICEMT(ice, ROUTE_CAPTURE));
2128         }
2129         spin_unlock_irq(&ice->reg_lock);
2130         return change;
2131 }
2132
2133 static int snd_ice1712_pro_route_spdif_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t *ucontrol)
2134 {
2135         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
2136         int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2137         unsigned int val, cval;
2138         val = inw(ICEMT(ice, ROUTE_SPDOUT));
2139         cval = (val >> (idx * 4 + 8)) & 0x0f;
2140         val = (val >> (idx * 2)) & 0x03;
2141         if (val == 1)
2142                 ucontrol->value.enumerated.item[0] = 11;
2143         else if (val == 2)
2144                 ucontrol->value.enumerated.item[0] = (cval & 7) + 1;
2145         else if (val == 3)
2146                 ucontrol->value.enumerated.item[0] = ((cval >> 3) & 1) + 9;
2147         else
2148                 ucontrol->value.enumerated.item[0] = 0;
2149         return 0;
2150 }
2151
2152 static int snd_ice1712_pro_route_spdif_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t *ucontrol)
2153 {
2154         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
2155         int change, shift;
2156         int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2157         unsigned int val, old_val, nval;
2158         
2159         /* update SPDOUT */
2160         spin_lock_irq(&ice->reg_lock);
2161         val = old_val = inw(ICEMT(ice, ROUTE_SPDOUT));
2162         if (ucontrol->value.enumerated.item[0] >= 11)
2163                 nval = 1;
2164         else if (ucontrol->value.enumerated.item[0] >= 9)
2165                 nval = 3;
2166         else if (ucontrol->value.enumerated.item[0] >= 1)
2167                 nval = 2;
2168         else
2169                 nval = 0;
2170         shift = idx * 2;
2171         val &= ~(0x03 << shift);
2172         val |= nval << shift;
2173         shift = idx * 4 + 8;
2174         if (nval == 2) {
2175                 nval = ucontrol->value.enumerated.item[0] - 1;
2176                 val &= ~(0x07 << shift);
2177                 val |= nval << shift;
2178         } else if (nval == 3) {
2179                 nval = (ucontrol->value.enumerated.item[0] - 9) << 3;
2180                 val &= ~(0x08 << shift);
2181                 val |= nval << shift;
2182         }
2183         change = val != old_val;
2184         if (change)
2185                 outw(val, ICEMT(ice, ROUTE_SPDOUT));
2186         spin_unlock_irq(&ice->reg_lock);
2187         return change;
2188 }
2189
2190 static snd_kcontrol_new_t snd_ice1712_mixer_pro_analog_route __devinitdata = {
2191         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2192         .name = "H/W Playback Route",
2193         .info = snd_ice1712_pro_route_info,
2194         .get = snd_ice1712_pro_route_analog_get,
2195         .put = snd_ice1712_pro_route_analog_put,
2196 };
2197
2198 static snd_kcontrol_new_t snd_ice1712_mixer_pro_spdif_route __devinitdata = {
2199         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2200         .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Route",
2201         .info = snd_ice1712_pro_route_info,
2202         .get = snd_ice1712_pro_route_spdif_get,
2203         .put = snd_ice1712_pro_route_spdif_put,
2204         .count = 2,
2205 };
2206
2207
2208 static int snd_ice1712_pro_volume_rate_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
2209 {
2210         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2211         uinfo->count = 1;
2212         uinfo->value.integer.min = 0;
2213         uinfo->value.integer.max = 255;
2214         return 0;
2215 }
2216
2217 static int snd_ice1712_pro_volume_rate_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
2218 {
2219         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
2220         
2221         ucontrol->value.integer.value[0] = inb(ICEMT(ice, MONITOR_RATE));
2222         return 0;
2223 }
2224
2225 static int snd_ice1712_pro_volume_rate_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
2226 {
2227         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
2228         int change;
2229
2230         spin_lock_irq(&ice->reg_lock);
2231         change = inb(ICEMT(ice, MONITOR_RATE)) != ucontrol->value.integer.value[0];
2232         outb(ucontrol->value.integer.value[0], ICEMT(ice, MONITOR_RATE));
2233         spin_unlock_irq(&ice->reg_lock);
2234         return change;
2235 }
2236
2237 static snd_kcontrol_new_t snd_ice1712_mixer_pro_volume_rate __devinitdata = {
2238         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2239         .name = "Multi Track Volume Rate",
2240         .info = snd_ice1712_pro_volume_rate_info,
2241         .get = snd_ice1712_pro_volume_rate_get,
2242         .put = snd_ice1712_pro_volume_rate_put
2243 };
2244
2245 static int snd_ice1712_pro_peak_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
2246 {
2247         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2248         uinfo->count = 22;
2249         uinfo->value.integer.min = 0;
2250         uinfo->value.integer.max = 255;
2251         return 0;
2252 }
2253
2254 static int snd_ice1712_pro_peak_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
2255 {
2256         ice1712_t *ice = snd_kcontrol_chip(kcontrol);
2257         int idx;
2258         
2259         spin_lock_irq(&ice->reg_lock);
2260         for (idx = 0; idx < 22; idx++) {
2261                 outb(idx, ICEMT(ice, MONITOR_PEAKINDEX));
2262                 ucontrol->value.integer.value[idx] = inb(ICEMT(ice, MONITOR_PEAKDATA));
2263         }
2264         spin_unlock_irq(&ice->reg_lock);
2265         return 0;
2266 }
2267
2268 static snd_kcontrol_new_t snd_ice1712_mixer_pro_peak __devinitdata = {
2269         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2270         .name = "Multi Track Peak",
2271         .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2272         .info = snd_ice1712_pro_peak_info,
2273         .get = snd_ice1712_pro_peak_get
2274 };
2275
2276 /*
2277  *
2278  */
2279
2280 /*
2281  * list of available boards
2282  */
2283 static struct snd_ice1712_card_info *card_tables[] __devinitdata = {
2284         snd_ice1712_hoontech_cards,
2285         snd_ice1712_delta_cards,
2286         snd_ice1712_ews_cards,
2287         NULL,
2288 };
2289
2290 static unsigned char __devinit snd_ice1712_read_i2c(ice1712_t *ice,
2291                                                  unsigned char dev,
2292                                                  unsigned char addr)
2293 {
2294         long t = 0x10000;
2295
2296         outb(addr, ICEREG(ice, I2C_BYTE_ADDR));
2297         outb(dev & ~ICE1712_I2C_WRITE, ICEREG(ice, I2C_DEV_ADDR));
2298         while (t-- > 0 && (inb(ICEREG(ice, I2C_CTRL)) & ICE1712_I2C_BUSY)) ;
2299         return inb(ICEREG(ice, I2C_DATA));
2300 }
2301
2302 static int __devinit snd_ice1712_read_eeprom(ice1712_t *ice, const char *modelname)
2303 {
2304         int dev = 0xa0;         /* EEPROM device address */
2305         unsigned int i, size;
2306         struct snd_ice1712_card_info **tbl, *c;
2307
2308         if (! modelname || ! *modelname) {
2309                 ice->eeprom.subvendor = 0;
2310                 if ((inb(ICEREG(ice, I2C_CTRL)) & ICE1712_I2C_EEPROM) != 0)
2311                         ice->eeprom.subvendor = (snd_ice1712_read_i2c(ice, dev, 0x00) << 0) |
2312                                 (snd_ice1712_read_i2c(ice, dev, 0x01) << 8) | 
2313                                 (snd_ice1712_read_i2c(ice, dev, 0x02) << 16) | 
2314                                 (snd_ice1712_read_i2c(ice, dev, 0x03) << 24);
2315                 if (ice->eeprom.subvendor == 0 || ice->eeprom.subvendor == (unsigned int)-1) {
2316                         /* invalid subvendor from EEPROM, try the PCI subststem ID instead */
2317                         u16 vendor, device;
2318                         pci_read_config_word(ice->pci, PCI_SUBSYSTEM_VENDOR_ID, &vendor);
2319                         pci_read_config_word(ice->pci, PCI_SUBSYSTEM_ID, &device);
2320                         ice->eeprom.subvendor = ((unsigned int)swab16(vendor) << 16) | swab16(device);
2321                         if (ice->eeprom.subvendor == 0 || ice->eeprom.subvendor == (unsigned int)-1) {
2322                                 printk(KERN_ERR "ice1712: No valid ID is found\n");
2323                                 return -ENXIO;
2324                         }
2325                 }
2326         }
2327         for (tbl = card_tables; *tbl; tbl++) {
2328                 for (c = *tbl; c->subvendor; c++) {
2329                         if (modelname && c->model && ! strcmp(modelname, c->model)) {
2330                                 printk(KERN_INFO "ice1712: Using board model %s\n", c->name);
2331                                 ice->eeprom.subvendor = c->subvendor;
2332                         } else if (c->subvendor != ice->eeprom.subvendor)
2333                                 continue;
2334                         if (! c->eeprom_size || ! c->eeprom_data)
2335                                 goto found;
2336                         /* if the EEPROM is given by the driver, use it */
2337                         snd_printdd("using the defined eeprom..\n");
2338                         ice->eeprom.version = 1;
2339                         ice->eeprom.size = c->eeprom_size + 6;
2340                         memcpy(ice->eeprom.data, c->eeprom_data, c->eeprom_size);
2341                         goto read_skipped;
2342                 }
2343         }
2344         printk(KERN_WARNING "ice1712: No matching model found for ID 0x%x\n", ice->eeprom.subvendor);
2345
2346  found:
2347         ice->eeprom.size = snd_ice1712_read_i2c(ice, dev, 0x04);
2348         if (ice->eeprom.size < 6)
2349                 ice->eeprom.size = 32; /* FIXME: any cards without the correct size? */
2350         else if (ice->eeprom.size > 32) {
2351                 snd_printk(KERN_ERR "invalid EEPROM (size = %i)\n", ice->eeprom.size);
2352                 return -EIO;
2353         }
2354         ice->eeprom.version = snd_ice1712_read_i2c(ice, dev, 0x05);
2355         if (ice->eeprom.version != 1) {
2356                 snd_printk(KERN_ERR "invalid EEPROM version %i\n", ice->eeprom.version);
2357                 /* return -EIO; */
2358         }
2359         size = ice->eeprom.size - 6;
2360         for (i = 0; i < size; i++)
2361                 ice->eeprom.data[i] = snd_ice1712_read_i2c(ice, dev, i + 6);
2362
2363  read_skipped:
2364         ice->eeprom.gpiomask = ice->eeprom.data[ICE_EEP1_GPIO_MASK];
2365         ice->eeprom.gpiostate = ice->eeprom.data[ICE_EEP1_GPIO_STATE];
2366         ice->eeprom.gpiodir = ice->eeprom.data[ICE_EEP1_GPIO_DIR];
2367
2368         return 0;
2369 }
2370
2371
2372
2373 static int __devinit snd_ice1712_chip_init(ice1712_t *ice)
2374 {
2375         outb(ICE1712_RESET | ICE1712_NATIVE, ICEREG(ice, CONTROL));
2376         udelay(200);
2377         outb(ICE1712_NATIVE, ICEREG(ice, CONTROL));
2378         udelay(200);
2379         pci_write_config_byte(ice->pci, 0x60, ice->eeprom.data[ICE_EEP1_CODEC]);
2380         pci_write_config_byte(ice->pci, 0x61, ice->eeprom.data[ICE_EEP1_ACLINK]);
2381         pci_write_config_byte(ice->pci, 0x62, ice->eeprom.data[ICE_EEP1_I2SID]);
2382         pci_write_config_byte(ice->pci, 0x63, ice->eeprom.data[ICE_EEP1_SPDIF]);
2383         if (ice->eeprom.subvendor != ICE1712_SUBDEVICE_STDSP24) {
2384                 ice->gpio.write_mask = ice->eeprom.gpiomask;
2385                 ice->gpio.direction = ice->eeprom.gpiodir;
2386                 snd_ice1712_write(ice, ICE1712_IREG_GPIO_WRITE_MASK, ice->eeprom.gpiomask);
2387                 snd_ice1712_write(ice, ICE1712_IREG_GPIO_DIRECTION, ice->eeprom.gpiodir);
2388                 snd_ice1712_write(ice, ICE1712_IREG_GPIO_DATA, ice->eeprom.gpiostate);
2389         } else {
2390                 ice->gpio.write_mask = 0xc0;
2391                 ice->gpio.direction = 0xff;
2392                 snd_ice1712_write(ice, ICE1712_IREG_GPIO_WRITE_MASK, 0xc0);
2393                 snd_ice1712_write(ice, ICE1712_IREG_GPIO_DIRECTION, 0xff);
2394                 snd_ice1712_write(ice, ICE1712_IREG_GPIO_DATA, ICE1712_STDSP24_CLOCK_BIT);
2395         }
2396         snd_ice1712_write(ice, ICE1712_IREG_PRO_POWERDOWN, 0);
2397         if (!(ice->eeprom.data[ICE_EEP1_CODEC] & ICE1712_CFG_NO_CON_AC97)) {
2398                 outb(ICE1712_AC97_WARM, ICEREG(ice, AC97_CMD));
2399                 udelay(100);
2400                 outb(0, ICEREG(ice, AC97_CMD));
2401                 udelay(200);
2402                 snd_ice1712_write(ice, ICE1712_IREG_CONSUMER_POWERDOWN, 0);
2403         }
2404         snd_ice1712_set_pro_rate(ice, 48000, 1);
2405
2406         return 0;
2407 }
2408
2409 int __devinit snd_ice1712_spdif_build_controls(ice1712_t *ice)
2410 {
2411         int err;
2412         snd_kcontrol_t *kctl;
2413
2414         snd_assert(ice->pcm_pro != NULL, return -EIO);
2415         err = snd_ctl_add(ice->card, kctl = snd_ctl_new1(&snd_ice1712_spdif_default, ice));
2416         if (err < 0)
2417                 return err;
2418         kctl->id.device = ice->pcm_pro->device;
2419         err = snd_ctl_add(ice->card, kctl = snd_ctl_new1(&snd_ice1712_spdif_maskc, ice));
2420         if (err < 0)
2421                 return err;
2422         kctl->id.device = ice->pcm_pro->device;
2423         err = snd_ctl_add(ice->card, kctl = snd_ctl_new1(&snd_ice1712_spdif_maskp, ice));
2424         if (err < 0)
2425                 return err;
2426         kctl->id.device = ice->pcm_pro->device;
2427         err = snd_ctl_add(ice->card, kctl = snd_ctl_new1(&snd_ice1712_spdif_stream, ice));
2428         if (err < 0)
2429                 return err;
2430         kctl->id.device = ice->pcm_pro->device;
2431         ice->spdif.stream_ctl = kctl;
2432         return 0;
2433 }
2434
2435
2436 static int __devinit snd_ice1712_build_controls(ice1712_t *ice)
2437 {
2438         int err;
2439
2440         err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_ice1712_eeprom, ice));
2441         if (err < 0)
2442                 return err;
2443         err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_ice1712_pro_internal_clock, ice));
2444         if (err < 0)
2445                 return err;
2446         err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_ice1712_pro_internal_clock_default, ice));
2447         if (err < 0)
2448                 return err;
2449
2450         err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_ice1712_pro_rate_locking, ice));
2451         if (err < 0)
2452                 return err;
2453         err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_ice1712_pro_rate_reset, ice));
2454         if (err < 0)
2455                 return err;
2456
2457         if (ice->num_total_dacs > 0) {
2458                 snd_kcontrol_new_t tmp = snd_ice1712_mixer_pro_analog_route;
2459                 tmp.count = ice->num_total_dacs;
2460                 err = snd_ctl_add(ice->card, snd_ctl_new1(&tmp, ice));
2461                 if (err < 0)
2462                         return err;
2463         }
2464
2465         err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_ice1712_mixer_pro_spdif_route, ice));
2466         if (err < 0)
2467                 return err;
2468
2469         err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_ice1712_mixer_pro_volume_rate, ice));
2470         if (err < 0)
2471                 return err;
2472         err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_ice1712_mixer_pro_peak, ice));
2473         if (err < 0)
2474                 return err;
2475
2476         return 0;
2477 }
2478
2479 static int snd_ice1712_free(ice1712_t *ice)
2480 {
2481         if (! ice->port)
2482                 goto __hw_end;
2483         /* mask all interrupts */
2484         outb(0xc0, ICEMT(ice, IRQ));
2485         outb(0xff, ICEREG(ice, IRQMASK));
2486         /* --- */
2487       __hw_end:
2488         if (ice->irq >= 0) {
2489                 synchronize_irq(ice->irq);
2490                 free_irq(ice->irq, (void *) ice);
2491         }
2492         if (ice->port)
2493                 pci_release_regions(ice->pci);
2494         snd_ice1712_akm4xxx_free(ice);
2495         pci_disable_device(ice->pci);
2496         kfree(ice);
2497         return 0;
2498 }
2499
2500 static int snd_ice1712_dev_free(snd_device_t *device)
2501 {
2502         ice1712_t *ice = device->device_data;
2503         return snd_ice1712_free(ice);
2504 }
2505
2506 static int __devinit snd_ice1712_create(snd_card_t * card,
2507                                         struct pci_dev *pci,
2508                                         const char *modelname,
2509                                         int omni,
2510                                         int cs8427_timeout,
2511                                         ice1712_t ** r_ice1712)
2512 {
2513         ice1712_t *ice;
2514         int err;
2515         static snd_device_ops_t ops = {
2516                 .dev_free =     snd_ice1712_dev_free,
2517         };
2518
2519         *r_ice1712 = NULL;
2520
2521         /* enable PCI device */
2522         if ((err = pci_enable_device(pci)) < 0)
2523                 return err;
2524         /* check, if we can restrict PCI DMA transfers to 28 bits */
2525         if (pci_set_dma_mask(pci, 0x0fffffff) < 0 ||
2526             pci_set_consistent_dma_mask(pci, 0x0fffffff) < 0) {
2527                 snd_printk(KERN_ERR "architecture does not support 28bit PCI busmaster DMA\n");
2528                 pci_disable_device(pci);
2529                 return -ENXIO;
2530         }
2531
2532         ice = kzalloc(sizeof(*ice), GFP_KERNEL);
2533         if (ice == NULL) {
2534                 pci_disable_device(pci);
2535                 return -ENOMEM;
2536         }
2537         ice->omni = omni ? 1 : 0;
2538         if (cs8427_timeout < 1)
2539                 cs8427_timeout = 1;
2540         else if (cs8427_timeout > 1000)
2541                 cs8427_timeout = 1000;
2542         ice->cs8427_timeout = cs8427_timeout;
2543         spin_lock_init(&ice->reg_lock);
2544         init_MUTEX(&ice->gpio_mutex);
2545         init_MUTEX(&ice->i2c_mutex);
2546         init_MUTEX(&ice->open_mutex);
2547         ice->gpio.set_mask = snd_ice1712_set_gpio_mask;
2548         ice->gpio.set_dir = snd_ice1712_set_gpio_dir;
2549         ice->gpio.set_data = snd_ice1712_set_gpio_data;
2550         ice->gpio.get_data = snd_ice1712_get_gpio_data;
2551
2552         ice->spdif.cs8403_bits =
2553                 ice->spdif.cs8403_stream_bits = (0x01 | /* consumer format */
2554                                                  0x10 | /* no emphasis */
2555                                                  0x20); /* PCM encoder/decoder */
2556         ice->card = card;
2557         ice->pci = pci;
2558         ice->irq = -1;
2559         pci_set_master(pci);
2560         pci_write_config_word(ice->pci, 0x40, 0x807f);
2561         pci_write_config_word(ice->pci, 0x42, 0x0006);
2562         snd_ice1712_proc_init(ice);
2563         synchronize_irq(pci->irq);
2564
2565         if ((err = pci_request_regions(pci, "ICE1712")) < 0) {
2566                 kfree(ice);
2567                 pci_disable_device(pci);
2568                 return err;
2569         }
2570         ice->port = pci_resource_start(pci, 0);
2571         ice->ddma_port = pci_resource_start(pci, 1);
2572         ice->dmapath_port = pci_resource_start(pci, 2);
2573         ice->profi_port = pci_resource_start(pci, 3);
2574
2575         if (request_irq(pci->irq, snd_ice1712_interrupt, SA_INTERRUPT|SA_SHIRQ, "ICE1712", (void *) ice)) {
2576                 snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
2577                 snd_ice1712_free(ice);
2578                 return -EIO;
2579         }
2580         
2581         ice->irq = pci->irq;
2582
2583         if (snd_ice1712_read_eeprom(ice, modelname) < 0) {
2584                 snd_ice1712_free(ice);
2585                 return -EIO;
2586         }
2587         if (snd_ice1712_chip_init(ice) < 0) {
2588                 snd_ice1712_free(ice);
2589                 return -EIO;
2590         }
2591
2592         /* unmask used interrupts */
2593         outb((ice->eeprom.data[ICE_EEP1_CODEC] & ICE1712_CFG_2xMPU401) == 0 ? ICE1712_IRQ_MPU2 : 0 |
2594              (ice->eeprom.data[ICE_EEP1_CODEC] & ICE1712_CFG_NO_CON_AC97) ? ICE1712_IRQ_PBKDS | ICE1712_IRQ_CONCAP | ICE1712_IRQ_CONPBK : 0,
2595              ICEREG(ice, IRQMASK));
2596         outb(0x00, ICEMT(ice, IRQ));
2597
2598         if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, ice, &ops)) < 0) {
2599                 snd_ice1712_free(ice);
2600                 return err;
2601         }
2602
2603         snd_card_set_dev(card, &pci->dev);
2604
2605         *r_ice1712 = ice;
2606         return 0;
2607 }
2608
2609
2610 /*
2611  *
2612  * Registration
2613  *
2614  */
2615
2616 static struct snd_ice1712_card_info no_matched __devinitdata;
2617
2618 static int __devinit snd_ice1712_probe(struct pci_dev *pci,
2619                                        const struct pci_device_id *pci_id)
2620 {
2621         static int dev;
2622         snd_card_t *card;
2623         ice1712_t *ice;
2624         int pcm_dev = 0, err;
2625         struct snd_ice1712_card_info **tbl, *c;
2626
2627         if (dev >= SNDRV_CARDS)
2628                 return -ENODEV;
2629         if (!enable[dev]) {
2630                 dev++;
2631                 return -ENOENT;
2632         }
2633
2634         card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
2635         if (card == NULL)
2636                 return -ENOMEM;
2637
2638         strcpy(card->driver, "ICE1712");
2639         strcpy(card->shortname, "ICEnsemble ICE1712");
2640         
2641         if ((err = snd_ice1712_create(card, pci, model[dev], omni[dev], cs8427_timeout[dev], &ice)) < 0) {
2642                 snd_card_free(card);
2643                 return err;
2644         }
2645
2646         for (tbl = card_tables; *tbl; tbl++) {
2647                 for (c = *tbl; c->subvendor; c++) {
2648                         if (c->subvendor == ice->eeprom.subvendor) {
2649                                 strcpy(card->shortname, c->name);
2650                                 if (c->driver) /* specific driver? */
2651                                         strcpy(card->driver, c->driver);
2652                                 if (c->chip_init) {
2653                                         if ((err = c->chip_init(ice)) < 0) {
2654                                                 snd_card_free(card);
2655                                                 return err;
2656                                         }
2657                                 }
2658                                 goto __found;
2659                         }
2660                 }
2661         }
2662         c = &no_matched;
2663  __found:
2664
2665         if ((err = snd_ice1712_pcm_profi(ice, pcm_dev++, NULL)) < 0) {
2666                 snd_card_free(card);
2667                 return err;
2668         }
2669         
2670         if (ice_has_con_ac97(ice))
2671                 if ((err = snd_ice1712_pcm(ice, pcm_dev++, NULL)) < 0) {
2672                         snd_card_free(card);
2673                         return err;
2674                 }
2675
2676         if ((err = snd_ice1712_ac97_mixer(ice)) < 0) {
2677                 snd_card_free(card);
2678                 return err;
2679         }
2680
2681         if ((err = snd_ice1712_build_controls(ice)) < 0) {
2682                 snd_card_free(card);
2683                 return err;
2684         }
2685
2686         if (c->build_controls) {
2687                 if ((err = c->build_controls(ice)) < 0) {
2688                         snd_card_free(card);
2689                         return err;
2690                 }
2691         }
2692
2693         if (ice_has_con_ac97(ice))
2694                 if ((err = snd_ice1712_pcm_ds(ice, pcm_dev++, NULL)) < 0) {
2695                         snd_card_free(card);
2696                         return err;
2697                 }
2698
2699         if (! c->no_mpu401) {
2700                 if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_ICE1712,
2701                                                ICEREG(ice, MPU1_CTRL), 1,
2702                                                ice->irq, 0,
2703                                                &ice->rmidi[0])) < 0) {
2704                         snd_card_free(card);
2705                         return err;
2706                 }
2707
2708                 if (ice->eeprom.data[ICE_EEP1_CODEC] & ICE1712_CFG_2xMPU401)
2709                         if ((err = snd_mpu401_uart_new(card, 1, MPU401_HW_ICE1712,
2710                                                        ICEREG(ice, MPU2_CTRL), 1,
2711                                                        ice->irq, 0,
2712                                                        &ice->rmidi[1])) < 0) {
2713                                 snd_card_free(card);
2714                                 return err;
2715                         }
2716         }
2717
2718         sprintf(card->longname, "%s at 0x%lx, irq %i",
2719                 card->shortname, ice->port, ice->irq);
2720
2721         if ((err = snd_card_register(card)) < 0) {
2722                 snd_card_free(card);
2723                 return err;
2724         }
2725         pci_set_drvdata(pci, card);
2726         dev++;
2727         return 0;
2728 }
2729
2730 static void __devexit snd_ice1712_remove(struct pci_dev *pci)
2731 {
2732         snd_card_free(pci_get_drvdata(pci));
2733         pci_set_drvdata(pci, NULL);
2734 }
2735
2736 static struct pci_driver driver = {
2737         .name = "ICE1712",
2738         .owner = THIS_MODULE,
2739         .id_table = snd_ice1712_ids,
2740         .probe = snd_ice1712_probe,
2741         .remove = __devexit_p(snd_ice1712_remove),
2742 };
2743
2744 static int __init alsa_card_ice1712_init(void)
2745 {
2746         return pci_register_driver(&driver);
2747 }
2748
2749 static void __exit alsa_card_ice1712_exit(void)
2750 {
2751         pci_unregister_driver(&driver);
2752 }
2753
2754 module_init(alsa_card_ice1712_init)
2755 module_exit(alsa_card_ice1712_exit)