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