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