2 * ALSA driver for VT1724 ICEnsemble ICE1724 / VIA VT1724 (Envy24HT)
3 * VIA VT1720 (Envy24PT)
5 * Copyright (c) 2000 Jaroslav Kysela <perex@perex.cz>
6 * 2002 James Stafford <jstafford@ampltd.com>
7 * 2003 Takashi Iwai <tiwai@suse.de>
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.
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.
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
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>
38 #include <sound/asoundef.h>
43 /* lowlevel routines */
47 #include "vt1720_mobo.h"
49 #include "prodigy192.h"
50 #include "prodigy_hifi.h"
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("{"
62 AMP_AUDIO2000_DEVICE_DESC
64 VT1720_MOBO_DEVICE_DESC
66 PRODIGY192_DEVICE_DESC
67 PRODIGY_HIFI_DEVICE_DESC
75 "{ICEnsemble,Generic ICE1724},"
76 "{ICEnsemble,Generic Envy24HT}"
77 "{ICEnsemble,Generic Envy24PT}}");
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];
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.");
94 /* Both VT1720 and VT1724 have the same PCI IDs */
95 static const struct pci_device_id snd_vt1724_ids[] = {
96 { PCI_VENDOR_ID_ICE, PCI_DEVICE_ID_VT1724, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
100 MODULE_DEVICE_TABLE(pci, snd_vt1724_ids);
103 static int PRO_RATE_LOCKED;
104 static int PRO_RATE_RESET = 1;
105 static unsigned int PRO_RATE_DEFAULT = 44100;
112 * default rates, default clock routines
115 /* check whether the clock mode is spdif-in */
116 static inline int stdclock_is_spdif_master(struct snd_ice1712 *ice)
118 return (inb(ICEMT1724(ice, RATE)) & VT1724_SPDIF_MASTER) ? 1 : 0;
121 static inline int is_pro_rate_locked(struct snd_ice1712 *ice)
123 return ice->is_spdif_master(ice) || PRO_RATE_LOCKED;
130 static unsigned char snd_vt1724_ac97_ready(struct snd_ice1712 *ice)
132 unsigned char old_cmd;
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))
138 if (!(old_cmd & VT1724_AC97_READY))
142 snd_printd(KERN_ERR "snd_vt1724_ac97_ready: timeout\n");
146 static int snd_vt1724_ac97_wait_bit(struct snd_ice1712 *ice, unsigned char bit)
149 for (tm = 0; tm < 0x10000; tm++)
150 if ((inb(ICEMT1724(ice, AC97_CMD)) & bit) == 0)
152 snd_printd(KERN_ERR "snd_vt1724_ac97_wait_bit: timeout\n");
156 static void snd_vt1724_ac97_write(struct snd_ac97 *ac97,
160 struct snd_ice1712 *ice = ac97->private_data;
161 unsigned char old_cmd;
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);
172 static unsigned short snd_vt1724_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
174 struct snd_ice1712 *ice = ac97->private_data;
175 unsigned char old_cmd;
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)
184 return inw(ICEMT1724(ice, AC97_DATA));
192 /* set gpio direction 0 = read, 1 = write */
193 static void snd_vt1724_set_gpio_dir(struct snd_ice1712 *ice, unsigned int data)
195 outl(data, ICEREG1724(ice, GPIO_DIRECTION));
196 inw(ICEREG1724(ice, GPIO_DIRECTION)); /* dummy read for pci-posting */
199 /* set the gpio mask (0 = writable) */
200 static void snd_vt1724_set_gpio_mask(struct snd_ice1712 *ice, unsigned int data)
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 */
208 static void snd_vt1724_set_gpio_data(struct snd_ice1712 *ice, unsigned int data)
210 outw(data, ICEREG1724(ice, GPIO_DATA));
212 outb(data >> 16, ICEREG1724(ice, GPIO_DATA_22));
213 inw(ICEREG1724(ice, GPIO_DATA)); /* dummy read for pci-posting */
216 static unsigned int snd_vt1724_get_gpio_data(struct snd_ice1712 *ice)
220 data = (unsigned int)inb(ICEREG1724(ice, GPIO_DATA_22));
223 data = (data << 16) | inw(ICEREG1724(ice, GPIO_DATA));
231 static void vt1724_midi_clear_rx(struct snd_ice1712 *ice)
235 for (count = inb(ICEREG1724(ice, MPU_RXFIFO)); count > 0; --count)
236 inb(ICEREG1724(ice, MPU_DATA));
239 static inline struct snd_rawmidi_substream *
240 get_rawmidi_substream(struct snd_ice1712 *ice, unsigned int stream)
242 return list_first_entry(&ice->rmidi[0]->streams[stream].substreams,
243 struct snd_rawmidi_substream, list);
246 static void enable_midi_irq(struct snd_ice1712 *ice, u8 flag, int enable);
248 static void vt1724_midi_write(struct snd_ice1712 *ice)
250 struct snd_rawmidi_substream *s;
254 s = get_rawmidi_substream(ice, SNDRV_RAWMIDI_STREAM_OUTPUT);
255 count = 31 - inb(ICEREG1724(ice, MPU_TXFIFO));
257 count = snd_rawmidi_transmit(s, buffer, count);
258 for (i = 0; i < count; ++i)
259 outb(buffer[i], ICEREG1724(ice, MPU_DATA));
261 /* mask irq when all bytes have been transmitted.
262 * enabled again in output_trigger when the new data comes in.
264 enable_midi_irq(ice, VT1724_IRQ_MPU_TX,
265 !snd_rawmidi_transmit_empty(s));
268 static void vt1724_midi_read(struct snd_ice1712 *ice)
270 struct snd_rawmidi_substream *s;
274 s = get_rawmidi_substream(ice, SNDRV_RAWMIDI_STREAM_INPUT);
275 count = inb(ICEREG1724(ice, MPU_RXFIFO));
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);
284 /* call with ice->reg_lock */
285 static void enable_midi_irq(struct snd_ice1712 *ice, u8 flag, int enable)
287 u8 mask = inb(ICEREG1724(ice, IRQMASK));
292 outb(mask, ICEREG1724(ice, IRQMASK));
295 static void vt1724_enable_midi_irq(struct snd_rawmidi_substream *substream,
298 struct snd_ice1712 *ice = substream->rmidi->private_data;
300 spin_lock_irq(&ice->reg_lock);
301 enable_midi_irq(ice, flag, enable);
302 spin_unlock_irq(&ice->reg_lock);
305 static int vt1724_midi_output_open(struct snd_rawmidi_substream *s)
310 static int vt1724_midi_output_close(struct snd_rawmidi_substream *s)
315 static void vt1724_midi_output_trigger(struct snd_rawmidi_substream *s, int up)
317 struct snd_ice1712 *ice = s->rmidi->private_data;
320 spin_lock_irqsave(&ice->reg_lock, flags);
322 ice->midi_output = 1;
323 vt1724_midi_write(ice);
325 ice->midi_output = 0;
326 enable_midi_irq(ice, VT1724_IRQ_MPU_TX, 0);
328 spin_unlock_irqrestore(&ice->reg_lock, flags);
331 static void vt1724_midi_output_drain(struct snd_rawmidi_substream *s)
333 struct snd_ice1712 *ice = s->rmidi->private_data;
334 unsigned long timeout;
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);
340 if (inb(ICEREG1724(ice, MPU_CTRL)) & VT1724_MPU_TX_EMPTY)
342 schedule_timeout_uninterruptible(1);
343 } while (time_after(timeout, jiffies));
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,
353 static int vt1724_midi_input_open(struct snd_rawmidi_substream *s)
355 vt1724_midi_clear_rx(s->rmidi->private_data);
356 vt1724_enable_midi_irq(s, VT1724_IRQ_MPU_RX, 1);
360 static int vt1724_midi_input_close(struct snd_rawmidi_substream *s)
362 vt1724_enable_midi_irq(s, VT1724_IRQ_MPU_RX, 0);
366 static void vt1724_midi_input_trigger(struct snd_rawmidi_substream *s, int up)
368 struct snd_ice1712 *ice = s->rmidi->private_data;
371 spin_lock_irqsave(&ice->reg_lock, flags);
374 vt1724_midi_read(ice);
378 spin_unlock_irqrestore(&ice->reg_lock, flags);
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,
392 static irqreturn_t snd_vt1724_interrupt(int irq, void *dev_id)
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;
402 status = inb(ICEREG1724(ice, IRQSTAT));
403 status &= status_mask;
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);
415 spin_unlock(&ice->reg_lock);
419 if (status & VT1724_IRQ_MPU_TX) {
420 if (ice->midi_output)
421 vt1724_midi_write(ice);
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
429 status_mask &= ~VT1724_IRQ_MPU_TX;
431 if (status & VT1724_IRQ_MPU_RX) {
433 vt1724_midi_read(ice);
435 vt1724_midi_clear_rx(ice);
438 outb(status, ICEREG1724(ice, IRQSTAT));
439 spin_unlock(&ice->reg_lock);
440 if (status & VT1724_IRQ_MTPCM) {
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
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);
457 if (mtstat & VT1724_MULTI_RDMA0) {
458 if (ice->capture_pro_substream)
459 snd_pcm_period_elapsed(ice->capture_pro_substream);
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]);
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]);
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]);
473 if (mtstat & VT1724_MULTI_PDMA4) {
474 if (ice->playback_con_substream)
475 snd_pcm_period_elapsed(ice->playback_con_substream);
477 if (mtstat & VT1724_MULTI_RDMA1) {
478 if (ice->capture_con_substream)
479 snd_pcm_period_elapsed(ice->capture_con_substream);
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 */
493 return IRQ_RETVAL(handled);
497 * PCM code - professional part (multitrack)
500 static unsigned int rates[] = {
501 8000, 9600, 11025, 12000, 16000, 22050, 24000,
502 32000, 44100, 48000, 64000, 88200, 96000,
506 static struct snd_pcm_hw_constraint_list hw_constraints_rates_96 = {
507 .count = ARRAY_SIZE(rates) - 2, /* up to 96000 */
512 static struct snd_pcm_hw_constraint_list hw_constraints_rates_48 = {
513 .count = ARRAY_SIZE(rates) - 5, /* up to 48000 */
518 static struct snd_pcm_hw_constraint_list hw_constraints_rates_192 = {
519 .count = ARRAY_SIZE(rates),
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 */
531 static int snd_vt1724_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
533 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
536 struct snd_pcm_substream *s;
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;
544 snd_pcm_trigger_done(s, substream);
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)
557 outb(old, ICEMT1724(ice, DMA_PAUSE));
558 spin_unlock(&ice->reg_lock);
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)
569 outb(old, ICEMT1724(ice, DMA_CONTROL));
570 spin_unlock(&ice->reg_lock);
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)
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
592 static unsigned int stdclock_get_rate(struct snd_ice1712 *ice)
595 rate = stdclock_rate_list[inb(ICEMT1724(ice, RATE)) & 15];
599 static void stdclock_set_rate(struct snd_ice1712 *ice, unsigned int rate)
602 for (i = 0; i < ARRAY_SIZE(stdclock_rate_list); i++) {
603 if (stdclock_rate_list[i] == rate) {
604 outb(i, ICEMT1724(ice, RATE));
610 static unsigned char stdclock_set_mclk(struct snd_ice1712 *ice,
613 unsigned char val, old;
615 if (ice->eeprom.data[ICE_EEP2_ACLINK] & VT1724_CFG_PRO_I2S) {
616 val = old = inb(ICEMT1724(ice, I2S_FORMAT));
618 val |= VT1724_MT_I2S_MCLK_128X; /* 128x MCLK */
620 val &= ~VT1724_MT_I2S_MCLK_128X; /* 256x MCLK */
622 outb(val, ICEMT1724(ice, I2S_FORMAT));
623 /* master clock changed */
627 /* no change in master clock */
631 static int snd_vt1724_set_pro_rate(struct snd_ice1712 *ice, unsigned int rate,
635 unsigned char mclk_change;
636 unsigned int i, old_rate;
638 if (rate > ice->hw_rates->list[ice->hw_rates->count - 1])
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);
648 if (!force && is_pro_rate_locked(ice)) {
649 spin_unlock_irqrestore(&ice->reg_lock, flags);
650 return (rate == ice->cur_rate) ? 0 : -EBUSY;
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);
661 ice->cur_rate = rate;
663 /* setting master clock */
664 mclk_change = ice->set_mclk(ice, rate);
666 spin_unlock_irqrestore(&ice->reg_lock, flags);
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);
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);
678 if (ice->spdif.ops.setup_rate)
679 ice->spdif.ops.setup_rate(ice, rate);
684 static int snd_vt1724_pcm_hw_params(struct snd_pcm_substream *substream,
685 struct snd_pcm_hw_params *hw_params)
687 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
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 */
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);
702 ice->pcm_reserved[i] = substream;
705 if (ice->pcm_reserved[i] == substream)
706 ice->pcm_reserved[i] = NULL;
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);
717 ice->pcm_reserved[i] = substream;
722 mutex_unlock(&ice->open_mutex);
724 err = snd_vt1724_set_pro_rate(ice, params_rate(hw_params), 0);
728 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
731 static int snd_vt1724_pcm_hw_free(struct snd_pcm_substream *substream)
733 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
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);
745 static int snd_vt1724_playback_pro_prepare(struct snd_pcm_substream *substream)
747 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
751 spin_lock_irq(&ice->reg_lock);
752 val = (8 - substream->runtime->channels) >> 1;
753 outb(val, ICEMT1724(ice, BURST));
755 outl(substream->runtime->dma_addr, ICEMT1724(ice, PLAYBACK_ADDR));
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);
766 spin_unlock_irq(&ice->reg_lock);
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));
779 static snd_pcm_uframes_t snd_vt1724_playback_pro_pointer(struct snd_pcm_substream *substream)
781 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
784 if (!(inl(ICEMT1724(ice, DMA_CONTROL)) & VT1724_PDMA0_START))
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");
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);
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);
805 else if (ptr <= substream->runtime->buffer_size)
806 ptr = substream->runtime->buffer_size - ptr;
808 snd_printd("ice1724: invalid ptr %d (size=%d)\n",
809 (int)ptr, (int)substream->runtime->buffer_size);
816 static int snd_vt1724_pcm_prepare(struct snd_pcm_substream *substream)
818 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
819 const struct vt1724_pcm_reg *reg = substream->runtime->private_data;
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);
831 static snd_pcm_uframes_t snd_vt1724_pcm_pointer(struct snd_pcm_substream *substream)
833 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
834 const struct vt1724_pcm_reg *reg = substream->runtime->private_data;
837 if (!(inl(ICEMT1724(ice, DMA_CONTROL)) & reg->start))
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);
849 else if (ptr <= substream->runtime->buffer_size)
850 ptr = substream->runtime->buffer_size - ptr;
852 snd_printd("ice1724: invalid ptr %d (size=%d)\n",
853 (int)ptr, (int)substream->runtime->buffer_size);
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,
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,
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,
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,
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
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,
904 .buffer_bytes_max = (1UL << 21), /* 19bits dword */
905 .period_bytes_min = 8 * 4 * 2, /* FIXME: constraints needed */
906 .period_bytes_max = (1UL << 21),
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),
925 .buffer_bytes_max = (1UL << 18), /* 16bits dword */
926 .period_bytes_min = 2 * 4 * 2,
927 .period_bytes_max = (1UL << 18),
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,
943 .buffer_bytes_max = (1UL << 18), /* 16bits dword */
944 .period_bytes_min = 2 * 4 * 2,
945 .period_bytes_max = (1UL << 18),
951 * set rate constraints
953 static void set_std_hw_rates(struct snd_ice1712 *ice)
955 if (ice->eeprom.data[ICE_EEP2_ACLINK] & VT1724_CFG_PRO_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;
961 ice->hw_rates = &hw_constraints_rates_96;
964 ice->hw_rates = &hw_constraints_rates_48;
968 static int set_rate_constraints(struct snd_ice1712 *ice,
969 struct snd_pcm_substream *substream)
971 struct snd_pcm_runtime *runtime = substream->runtime;
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,
981 /* multi-channel playback needs alignment 8x32bit regardless of the channels
984 #define VT1724_BUFFER_ALIGN 0x20
986 static int snd_vt1724_playback_pro_open(struct snd_pcm_substream *substream)
988 struct snd_pcm_runtime *runtime = substream->runtime;
989 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
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])
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);
1017 static int snd_vt1724_capture_pro_open(struct snd_pcm_substream *substream)
1019 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
1020 struct snd_pcm_runtime *runtime = substream->runtime;
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);
1035 static int snd_vt1724_playback_pro_close(struct snd_pcm_substream *substream)
1037 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
1040 snd_vt1724_set_pro_rate(ice, ice->pro_rate_default, 0);
1041 ice->playback_pro_substream = NULL;
1046 static int snd_vt1724_capture_pro_close(struct snd_pcm_substream *substream)
1048 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
1051 snd_vt1724_set_pro_rate(ice, ice->pro_rate_default, 0);
1052 ice->capture_pro_substream = NULL;
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,
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,
1078 static int __devinit snd_vt1724_pcm_profi(struct snd_ice1712 *ice, int device)
1080 struct snd_pcm *pcm;
1083 err = snd_pcm_new(ice->card, "ICE1724", device, 1, 1, &pcm);
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);
1090 pcm->private_data = ice;
1091 pcm->info_flags = 0;
1092 strcpy(pcm->name, "ICE1724");
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);
1108 /* update spdif control bits; call with reg_lock */
1109 static void update_spdif_bits(struct snd_ice1712 *ice, unsigned int val)
1111 unsigned char cbit, disabled;
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));
1123 /* update SPDIF control bits according to the given rate */
1124 static void update_spdif_rate(struct snd_ice1712 *ice, unsigned int rate)
1126 unsigned int val, nval;
1127 unsigned long flags;
1129 spin_lock_irqsave(&ice->reg_lock, flags);
1130 nval = val = inw(ICEMT1724(ice, SPDIF_CTRL));
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;
1142 update_spdif_bits(ice, nval);
1143 spin_unlock_irqrestore(&ice->reg_lock, flags);
1146 static int snd_vt1724_playback_spdif_prepare(struct snd_pcm_substream *substream)
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);
1154 static int snd_vt1724_playback_spdif_open(struct snd_pcm_substream *substream)
1156 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
1157 struct snd_pcm_runtime *runtime = substream->runtime;
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);
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);
1177 static int snd_vt1724_playback_spdif_close(struct snd_pcm_substream *substream)
1179 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
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);
1190 static int snd_vt1724_capture_spdif_open(struct snd_pcm_substream *substream)
1192 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
1193 struct snd_pcm_runtime *runtime = substream->runtime;
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);
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);
1213 static int snd_vt1724_capture_spdif_close(struct snd_pcm_substream *substream)
1215 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
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);
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,
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,
1249 static int __devinit snd_vt1724_pcm_spdif(struct snd_ice1712 *ice, int device)
1252 struct snd_pcm *pcm;
1256 if (ice->force_pdma4 ||
1257 (ice->eeprom.data[ICE_EEP2_SPDIF] & VT1724_CFG_SPDIF_OUT_INT)) {
1262 if (ice->force_rdma1 ||
1263 (ice->eeprom.data[ICE_EEP2_SPDIF] & VT1724_CFG_SPDIF_IN)) {
1269 return 0; /* no spdif device */
1271 if (ice->force_pdma4 || ice->force_rdma1)
1272 name = "ICE1724 Secondary";
1274 name = "ICE1724 IEC958";
1275 err = snd_pcm_new(ice->card, name, device, play, capt, &pcm);
1280 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1281 &snd_vt1724_playback_spdif_ops);
1283 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1284 &snd_vt1724_capture_spdif_ops);
1286 pcm->private_data = ice;
1287 pcm->info_flags = 0;
1288 strcpy(pcm->name, name);
1290 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1291 snd_dma_pci_data(ice->pci),
1301 * independent surround PCMs
1304 static const struct vt1724_pcm_reg vt1724_playback_dma_regs[3] = {
1306 .addr = VT1724_MT_PDMA1_ADDR,
1307 .size = VT1724_MT_PDMA1_SIZE,
1308 .count = VT1724_MT_PDMA1_COUNT,
1309 .start = VT1724_PDMA1_START,
1312 .addr = VT1724_MT_PDMA2_ADDR,
1313 .size = VT1724_MT_PDMA2_SIZE,
1314 .count = VT1724_MT_PDMA2_COUNT,
1315 .start = VT1724_PDMA2_START,
1318 .addr = VT1724_MT_PDMA3_ADDR,
1319 .size = VT1724_MT_PDMA3_SIZE,
1320 .count = VT1724_MT_PDMA3_COUNT,
1321 .start = VT1724_PDMA3_START,
1325 static int snd_vt1724_playback_indep_prepare(struct snd_pcm_substream *substream)
1327 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
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);
1338 static int snd_vt1724_playback_indep_open(struct snd_pcm_substream *substream)
1340 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
1341 struct snd_pcm_runtime *runtime = substream->runtime;
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 */
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);
1359 static int snd_vt1724_playback_indep_close(struct snd_pcm_substream *substream)
1361 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
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;
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,
1383 static int __devinit snd_vt1724_pcm_indep(struct snd_ice1712 *ice, int device)
1385 struct snd_pcm *pcm;
1389 play = ice->num_total_dacs / 2 - 1;
1393 err = snd_pcm_new(ice->card, "ICE1724 Surrounds", device, play, 0, &pcm);
1397 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1398 &snd_vt1724_playback_indep_ops);
1400 pcm->private_data = ice;
1401 pcm->info_flags = 0;
1402 strcpy(pcm->name, "ICE1724 Surround PCM");
1404 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1405 snd_dma_pci_data(ice->pci),
1418 static int __devinit snd_vt1724_ac97_mixer(struct snd_ice1712 *ice)
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,
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));
1435 err = snd_ac97_bus(ice->card, 0, &ops, NULL, &pbus);
1438 memset(&ac97, 0, sizeof(ac97));
1439 ac97.private_data = ice;
1440 err = snd_ac97_mixer(pbus, &ac97, &ice->ac97);
1442 printk(KERN_WARNING "ice1712: cannot initialize pro ac97, skipped\n");
1446 /* I2S mixer only */
1447 strcat(ice->card->mixername, "ICE1724 - multitrack");
1455 static inline unsigned int eeprom_triple(struct snd_ice1712 *ice, int idx)
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);
1462 static void snd_vt1724_proc_read(struct snd_info_entry *entry,
1463 struct snd_info_buffer *buffer)
1465 struct snd_ice1712 *ice = entry->private_data;
1468 snd_iprintf(buffer, "%s\n\n", ice->card->longname);
1469 snd_iprintf(buffer, "EEPROM:\n");
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]);
1492 snd_iprintf(buffer, "\nRegisters:\n");
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));
1504 static void __devinit snd_vt1724_proc_init(struct snd_ice1712 *ice)
1506 struct snd_info_entry *entry;
1508 if (!snd_card_proc_new(ice->card, "ice1724", &entry))
1509 snd_info_set_text_ops(entry, ice, snd_vt1724_proc_read);
1516 static int snd_vt1724_eeprom_info(struct snd_kcontrol *kcontrol,
1517 struct snd_ctl_elem_info *uinfo)
1519 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
1520 uinfo->count = sizeof(struct snd_ice1712_eeprom);
1524 static int snd_vt1724_eeprom_get(struct snd_kcontrol *kcontrol,
1525 struct snd_ctl_elem_value *ucontrol)
1527 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
1529 memcpy(ucontrol->value.bytes.data, &ice->eeprom, sizeof(ice->eeprom));
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
1543 static int snd_vt1724_spdif_info(struct snd_kcontrol *kcontrol,
1544 struct snd_ctl_elem_info *uinfo)
1546 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1551 static unsigned int encode_spdif_bits(struct snd_aes_iec958 *diga)
1553 unsigned int val, rbits;
1555 val = diga->status[0] & 0x03; /* professional, non-audio */
1558 if ((diga->status[0] & IEC958_AES0_PRO_EMPHASIS) ==
1559 IEC958_AES0_PRO_EMPHASIS_5015)
1561 rbits = (diga->status[4] >> 3) & 0x0f;
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 */
1570 switch (diga->status[0] & IEC958_AES0_PRO_FS) {
1571 case IEC958_AES0_PRO_FS_44100:
1573 case IEC958_AES0_PRO_FS_32000:
1583 val |= diga->status[1] & 0x04; /* copyright */
1584 if ((diga->status[0] & IEC958_AES0_CON_EMPHASIS) ==
1585 IEC958_AES0_CON_EMPHASIS_5015)
1587 val |= (unsigned int)(diga->status[1] & 0x3f) << 4; /* category */
1588 val |= (unsigned int)(diga->status[3] & IEC958_AES3_CON_FS) << 12; /* fs */
1593 static void decode_spdif_bits(struct snd_aes_iec958 *diga, unsigned int val)
1595 memset(diga->status, 0, sizeof(diga->status));
1596 diga->status[0] = val & 0x03; /* professional, non-audio */
1599 if (val & (1U << 3))
1600 diga->status[0] |= IEC958_AES0_PRO_EMPHASIS_5015;
1601 switch ((val >> 12) & 0x7) {
1605 diga->status[0] |= IEC958_AES0_PRO_FS_32000;
1608 diga->status[0] |= IEC958_AES0_PRO_FS_48000;
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 */
1621 static int snd_vt1724_spdif_default_get(struct snd_kcontrol *kcontrol,
1622 struct snd_ctl_elem_value *ucontrol)
1624 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
1626 val = inw(ICEMT1724(ice, SPDIF_CTRL));
1627 decode_spdif_bits(&ucontrol->value.iec958, val);
1631 static int snd_vt1724_spdif_default_put(struct snd_kcontrol *kcontrol,
1632 struct snd_ctl_elem_value *ucontrol)
1634 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
1635 unsigned int val, old;
1637 val = encode_spdif_bits(&ucontrol->value.iec958);
1638 spin_lock_irq(&ice->reg_lock);
1639 old = inw(ICEMT1724(ice, SPDIF_CTRL));
1641 update_spdif_bits(ice, val);
1642 spin_unlock_irq(&ice->reg_lock);
1646 static struct snd_kcontrol_new snd_vt1724_spdif_default __devinitdata =
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
1655 static int snd_vt1724_spdif_maskc_get(struct snd_kcontrol *kcontrol,
1656 struct snd_ctl_elem_value *ucontrol)
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;
1668 static int snd_vt1724_spdif_maskp_get(struct snd_kcontrol *kcontrol,
1669 struct snd_ctl_elem_value *ucontrol)
1671 ucontrol->value.iec958.status[0] = IEC958_AES0_NONAUDIO |
1672 IEC958_AES0_PROFESSIONAL |
1673 IEC958_AES0_PRO_FS |
1674 IEC958_AES0_PRO_EMPHASIS;
1678 static struct snd_kcontrol_new snd_vt1724_spdif_maskc __devinitdata =
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,
1687 static struct snd_kcontrol_new snd_vt1724_spdif_maskp __devinitdata =
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,
1696 #define snd_vt1724_spdif_sw_info snd_ctl_boolean_mono_info
1698 static int snd_vt1724_spdif_sw_get(struct snd_kcontrol *kcontrol,
1699 struct snd_ctl_elem_value *ucontrol)
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;
1707 static int snd_vt1724_spdif_sw_put(struct snd_kcontrol *kcontrol,
1708 struct snd_ctl_elem_value *ucontrol)
1710 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
1711 unsigned char old, val;
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;
1719 outb(val, ICEREG1724(ice, SPDIF_CFG));
1720 spin_unlock_irq(&ice->reg_lock);
1724 static struct snd_kcontrol_new snd_vt1724_spdif_switch __devinitdata =
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
1736 #if 0 /* NOT USED YET */
1738 * GPIO access from extern
1741 #define snd_vt1724_gpio_info snd_ctl_boolean_mono_info
1743 int snd_vt1724_gpio_get(struct snd_kcontrol *kcontrol,
1744 struct snd_ctl_elem_value *ucontrol)
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;
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);
1757 int snd_ice1712_gpio_put(struct snd_kcontrol *kcontrol,
1758 struct snd_ctl_elem_value *ucontrol)
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;
1765 if (kcontrol->private_value & (1 << 31))
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);
1772 snd_ice1712_gpio_write(ice, nval);
1773 snd_ice1712_restore_gpio_status(ice);
1776 #endif /* NOT USED YET */
1781 static int snd_vt1724_pro_internal_clock_info(struct snd_kcontrol *kcontrol,
1782 struct snd_ctl_elem_info *uinfo)
1784 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
1786 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
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");
1794 sprintf(uinfo->value.enumerated.name, "%d",
1795 ice->hw_rates->list[uinfo->value.enumerated.item]);
1799 static int snd_vt1724_pro_internal_clock_get(struct snd_kcontrol *kcontrol,
1800 struct snd_ctl_elem_value *ucontrol)
1802 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
1803 unsigned int i, rate;
1805 spin_lock_irq(&ice->reg_lock);
1806 if (ice->is_spdif_master(ice)) {
1807 ucontrol->value.enumerated.item[0] = ice->hw_rates->count;
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;
1818 spin_unlock_irq(&ice->reg_lock);
1822 /* setting clock to external - SPDIF */
1823 static void stdclock_set_spdif_clock(struct snd_ice1712 *ice)
1826 unsigned char i2s_oval;
1827 oval = inb(ICEMT1724(ice, RATE));
1828 outb(oval | VT1724_SPDIF_MASTER, ICEMT1724(ice, RATE));
1830 i2s_oval = inb(ICEMT1724(ice, I2S_FORMAT));
1831 outb(i2s_oval & ~VT1724_MT_I2S_MCLK_128X, ICEMT1724(ice, I2S_FORMAT));
1834 static int snd_vt1724_pro_internal_clock_put(struct snd_kcontrol *kcontrol,
1835 struct snd_ctl_elem_value *ucontrol)
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;
1845 spin_lock_irq(&ice->reg_lock);
1846 if (ice->is_spdif_master(ice))
1849 old_rate = ice->get_rate(ice);
1850 if (item == spdif) {
1851 /* switching to external clock via SPDIF */
1852 ice->set_spdif_clock(ice);
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);
1862 spin_unlock_irq(&ice->reg_lock);
1864 /* the first reset to the SPDIF master mode? */
1865 if (old_rate != new_rate && !new_rate) {
1866 /* notify akm chips as well */
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);
1875 return old_rate != new_rate;
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
1886 #define snd_vt1724_pro_rate_locking_info snd_ctl_boolean_mono_info
1888 static int snd_vt1724_pro_rate_locking_get(struct snd_kcontrol *kcontrol,
1889 struct snd_ctl_elem_value *ucontrol)
1891 ucontrol->value.integer.value[0] = PRO_RATE_LOCKED;
1895 static int snd_vt1724_pro_rate_locking_put(struct snd_kcontrol *kcontrol,
1896 struct snd_ctl_elem_value *ucontrol)
1898 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
1899 int change = 0, nval;
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);
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
1917 #define snd_vt1724_pro_rate_reset_info snd_ctl_boolean_mono_info
1919 static int snd_vt1724_pro_rate_reset_get(struct snd_kcontrol *kcontrol,
1920 struct snd_ctl_elem_value *ucontrol)
1922 ucontrol->value.integer.value[0] = PRO_RATE_RESET ? 1 : 0;
1926 static int snd_vt1724_pro_rate_reset_put(struct snd_kcontrol *kcontrol,
1927 struct snd_ctl_elem_value *ucontrol)
1929 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
1930 int change = 0, nval;
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);
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
1952 static int snd_vt1724_pro_route_info(struct snd_kcontrol *kcontrol,
1953 struct snd_ctl_elem_info *uinfo)
1955 static char *texts[] = {
1957 "H/W In 0", "H/W In 1", /* 1-2 */
1958 "IEC958 In L", "IEC958 In R", /* 3-4 */
1961 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
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]);
1970 static inline int analog_route_shift(int idx)
1972 return (idx % 2) * 12 + ((idx / 2) * 3) + 8;
1975 static inline int digital_route_shift(int idx)
1980 int snd_ice1724_get_route_val(struct snd_ice1712 *ice, int shift)
1983 unsigned char eitem;
1984 static const unsigned char xlate[8] = {
1985 0, 255, 1, 2, 255, 255, 3, 4,
1988 val = inl(ICEMT1724(ice, ROUTE_PLAYBACK));
1990 val &= 7; /* we now have 3 bits per output */
1999 int snd_ice1724_put_route_val(struct snd_ice1712 *ice, unsigned int val,
2002 unsigned int old_val, nval;
2004 static const unsigned char xroute[8] = {
2006 2, /* PSDIN0 Left */
2007 3, /* PSDIN0 Right */
2009 7, /* SPDIN Right */
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;
2018 outl(val, ICEMT1724(ice, ROUTE_PLAYBACK));
2022 static int snd_vt1724_pro_route_analog_get(struct snd_kcontrol *kcontrol,
2023 struct snd_ctl_elem_value *ucontrol)
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));
2032 static int snd_vt1724_pro_route_analog_put(struct snd_kcontrol *kcontrol,
2033 struct snd_ctl_elem_value *ucontrol)
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));
2042 static int snd_vt1724_pro_route_spdif_get(struct snd_kcontrol *kcontrol,
2043 struct snd_ctl_elem_value *ucontrol)
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));
2052 static int snd_vt1724_pro_route_spdif_put(struct snd_kcontrol *kcontrol,
2053 struct snd_ctl_elem_value *ucontrol)
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));
2062 static struct snd_kcontrol_new snd_vt1724_mixer_pro_analog_route __devinitdata =
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,
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,
2081 static int snd_vt1724_pro_peak_info(struct snd_kcontrol *kcontrol,
2082 struct snd_ctl_elem_info *uinfo)
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;
2091 static int snd_vt1724_pro_peak_get(struct snd_kcontrol *kcontrol,
2092 struct snd_ctl_elem_value *ucontrol)
2094 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
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));
2103 spin_unlock_irq(&ice->reg_lock);
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
2119 static struct snd_ice1712_card_info no_matched __devinitdata;
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,
2141 static void wait_i2c_busy(struct snd_ice1712 *ice)
2144 while ((inb(ICEREG1724(ice, I2C_CTRL)) & VT1724_I2C_BUSY) && t--)
2147 printk(KERN_ERR "ice1724: i2c busy timeout\n");
2150 unsigned char snd_vt1724_read_i2c(struct snd_ice1712 *ice,
2151 unsigned char dev, unsigned char addr)
2155 mutex_lock(&ice->i2c_mutex);
2157 outb(addr, ICEREG1724(ice, I2C_BYTE_ADDR));
2158 outb(dev & ~VT1724_I2C_WRITE, ICEREG1724(ice, I2C_DEV_ADDR));
2160 val = inb(ICEREG1724(ice, I2C_DATA));
2161 mutex_unlock(&ice->i2c_mutex);
2163 printk(KERN_DEBUG "i2c_read: [0x%x,0x%x] = 0x%x\n", dev, addr, val);
2168 void snd_vt1724_write_i2c(struct snd_ice1712 *ice,
2169 unsigned char dev, unsigned char addr, unsigned char data)
2171 mutex_lock(&ice->i2c_mutex);
2174 printk(KERN_DEBUG "i2c_write: [0x%x,0x%x] = 0x%x\n", dev, addr, data);
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));
2180 mutex_unlock(&ice->i2c_mutex);
2183 static int __devinit snd_vt1724_read_eeprom(struct snd_ice1712 *ice,
2184 const char *modelname)
2186 const int dev = 0xa0; /* EEPROM device address */
2187 unsigned int i, size;
2188 struct snd_ice1712_card_info * const *tbl, *c;
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
2204 pci_read_config_word(ice->pci, PCI_SUBSYSTEM_VENDOR_ID,
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");
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",
2222 ice->eeprom.subvendor = c->subvendor;
2223 } else if (c->subvendor != ice->eeprom.subvendor)
2225 if (!c->eeprom_size || !c->eeprom_data)
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);
2235 printk(KERN_WARNING "ice1724: No matching model found for ID 0x%x\n",
2236 ice->eeprom.subvendor);
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",
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);
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);
2265 static void __devinit snd_vt1724_chip_reset(struct snd_ice1712 *ice)
2267 outb(VT1724_RESET , ICEREG1724(ice, CONTROL));
2268 inb(ICEREG1724(ice, CONTROL)); /* pci posting flush */
2270 outb(0, ICEREG1724(ice, CONTROL));
2271 inb(ICEREG1724(ice, CONTROL)); /* pci posting flush */
2275 static int __devinit snd_vt1724_chip_init(struct snd_ice1712 *ice)
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));
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);
2288 outb(0, ICEREG1724(ice, POWERDOWN));
2293 static int __devinit snd_vt1724_spdif_build_controls(struct snd_ice1712 *ice)
2296 struct snd_kcontrol *kctl;
2298 if (snd_BUG_ON(!ice->pcm))
2301 if (!ice->own_routing) {
2302 err = snd_ctl_add(ice->card,
2303 snd_ctl_new1(&snd_vt1724_mixer_pro_spdif_route, ice));
2308 err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_spdif_switch, ice));
2312 err = snd_ctl_add(ice->card, kctl = snd_ctl_new1(&snd_vt1724_spdif_default, ice));
2315 kctl->id.device = ice->pcm->device;
2316 err = snd_ctl_add(ice->card, kctl = snd_ctl_new1(&snd_vt1724_spdif_maskc, ice));
2319 kctl->id.device = ice->pcm->device;
2320 err = snd_ctl_add(ice->card, kctl = snd_ctl_new1(&snd_vt1724_spdif_maskp, ice));
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));
2328 kctl->id.device = ice->pcm->device;
2329 ice->spdif.stream_ctl = kctl;
2335 static int __devinit snd_vt1724_build_controls(struct snd_ice1712 *ice)
2339 err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_eeprom, ice));
2342 err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_pro_internal_clock, ice));
2346 err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_pro_rate_locking, ice));
2349 err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_pro_rate_reset, ice));
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)
2358 err = snd_ctl_add(ice->card, snd_ctl_new1(&tmp, ice));
2363 err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_mixer_pro_peak, ice));
2370 static int snd_vt1724_free(struct snd_ice1712 *ice)
2374 /* mask all interrupts */
2375 outb(0xff, ICEMT1724(ice, DMA_INT_MASK));
2376 outb(0xff, ICEREG1724(ice, IRQMASK));
2380 free_irq(ice->irq, ice);
2381 pci_release_regions(ice->pci);
2382 snd_ice1712_akm4xxx_free(ice);
2383 pci_disable_device(ice->pci);
2389 static int snd_vt1724_dev_free(struct snd_device *device)
2391 struct snd_ice1712 *ice = device->device_data;
2392 return snd_vt1724_free(ice);
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)
2400 struct snd_ice1712 *ice;
2402 static struct snd_device_ops ops = {
2403 .dev_free = snd_vt1724_dev_free,
2408 /* enable PCI device */
2409 err = pci_enable_device(pci);
2413 ice = kzalloc(sizeof(*ice), GFP_KERNEL);
2415 pci_disable_device(pci);
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;
2430 pci_set_master(pci);
2431 snd_vt1724_proc_init(ice);
2432 synchronize_irq(pci->irq);
2434 err = pci_request_regions(pci, "ICE1724");
2437 pci_disable_device(pci);
2440 ice->port = pci_resource_start(pci, 0);
2441 ice->profi_port = pci_resource_start(pci, 1);
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);
2450 ice->irq = pci->irq;
2452 snd_vt1724_chip_reset(ice);
2453 if (snd_vt1724_read_eeprom(ice, modelname) < 0) {
2454 snd_vt1724_free(ice);
2457 if (snd_vt1724_chip_init(ice) < 0) {
2458 snd_vt1724_free(ice);
2462 /* MPU_RX and TX irq masks are cleared later dynamically */
2463 outb(VT1724_IRQ_MPU_RX | VT1724_IRQ_MPU_TX , ICEREG1724(ice, IRQMASK));
2465 /* don't handle FIFO overrun/underruns (just yet),
2466 * since they cause machine lockups
2468 outb(VT1724_MULTI_FIFO_ERR, ICEMT1724(ice, DMA_INT_MASK));
2470 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, ice, &ops);
2472 snd_vt1724_free(ice);
2476 snd_card_set_dev(card, &pci->dev);
2489 static int __devinit snd_vt1724_probe(struct pci_dev *pci,
2490 const struct pci_device_id *pci_id)
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;
2498 if (dev >= SNDRV_CARDS)
2505 err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
2509 strcpy(card->driver, "ICE1724");
2510 strcpy(card->shortname, "ICEnsemble ICE1724");
2512 err = snd_vt1724_create(card, pci, model[dev], &ice);
2514 snd_card_free(card);
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);
2525 err = c->chip_init(ice);
2527 snd_card_free(card);
2538 * VT1724 has separate DMAs for the analog and the SPDIF streams while
2539 * ICE1712 has only one for both (mixed up).
2541 * Confusingly the analog PCM is named "professional" here because it
2542 * was called so in ice1712 driver, and vt1724 driver is derived from
2545 ice->pro_rate_default = PRO_RATE_DEFAULT;
2546 if (!ice->is_spdif_master)
2547 ice->is_spdif_master = stdclock_is_spdif_master;
2549 ice->get_rate = stdclock_get_rate;
2551 ice->set_rate = stdclock_set_rate;
2553 ice->set_mclk = stdclock_set_mclk;
2554 if (!ice->set_spdif_clock)
2555 ice->set_spdif_clock = stdclock_set_spdif_clock;
2557 set_std_hw_rates(ice);
2559 err = snd_vt1724_pcm_profi(ice, pcm_dev++);
2561 snd_card_free(card);
2565 err = snd_vt1724_pcm_spdif(ice, pcm_dev++);
2567 snd_card_free(card);
2571 err = snd_vt1724_pcm_indep(ice, pcm_dev++);
2573 snd_card_free(card);
2577 err = snd_vt1724_ac97_mixer(ice);
2579 snd_card_free(card);
2583 err = snd_vt1724_build_controls(ice);
2585 snd_card_free(card);
2589 if (ice->pcm && ice->has_spdif) { /* has SPDIF I/O */
2590 err = snd_vt1724_spdif_build_controls(ice);
2592 snd_card_free(card);
2597 if (c->build_controls) {
2598 err = c->build_controls(ice);
2600 snd_card_free(card);
2605 if (!c->no_mpu401) {
2606 if (ice->eeprom.data[ICE_EEP2_SYSCONF] & VT1724_CFG_MPU401) {
2607 struct snd_rawmidi *rmidi;
2609 err = snd_rawmidi_new(card, "MIDI", 0, 1, 1, &rmidi);
2611 snd_card_free(card);
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);
2625 /* set watermarks */
2626 outb(VT1724_MPU_RX_FIFO | 0x1,
2627 ICEREG1724(ice, MPU_FIFO_WM));
2628 outb(0x1, ICEREG1724(ice, MPU_FIFO_WM));
2630 outb(VT1724_MPU_UART, ICEREG1724(ice, MPU_CTRL));
2634 sprintf(card->longname, "%s at 0x%lx, irq %i",
2635 card->shortname, ice->port, ice->irq);
2637 err = snd_card_register(card);
2639 snd_card_free(card);
2642 pci_set_drvdata(pci, card);
2647 static void __devexit snd_vt1724_remove(struct pci_dev *pci)
2649 snd_card_free(pci_get_drvdata(pci));
2650 pci_set_drvdata(pci, NULL);
2653 static struct pci_driver driver = {
2655 .id_table = snd_vt1724_ids,
2656 .probe = snd_vt1724_probe,
2657 .remove = __devexit_p(snd_vt1724_remove),
2660 static int __init alsa_card_ice1724_init(void)
2662 return pci_register_driver(&driver);
2665 static void __exit alsa_card_ice1724_exit(void)
2667 pci_unregister_driver(&driver);
2670 module_init(alsa_card_ice1724_init)
2671 module_exit(alsa_card_ice1724_exit)