blob: 32560cfb6c3fcafdfe8bb210ea34d2b3c6d66008 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Ingo Molnar62932df2006-01-16 16:34:20 +010033#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#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"
Clement Guedezf6cdab52007-01-08 10:48:41 +010053#include "wtm.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
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
Clement Guedezf6cdab52007-01-08 10:48:41 +010067 WTM_DEVICE_DESC
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 "{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
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89/* Both VT1720 and VT1724 have the same PCI IDs */
Takashi Iwai32b47da2007-01-29 15:26:36 +010090static const struct pci_device_id snd_vt1724_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 { 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 */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100107static inline int is_spdif_master(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
109 return (inb(ICEMT1724(ice, RATE)) & VT1724_SPDIF_MASTER) ? 1 : 0;
110}
111
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100112static inline int is_pro_rate_locked(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
114 return is_spdif_master(ice) || PRO_RATE_LOCKED;
115}
116
117/*
118 * ac97 section
119 */
120
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100121static unsigned char snd_vt1724_ac97_ready(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
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
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100137static int snd_vt1724_ac97_wait_bit(struct snd_ice1712 *ice, unsigned char bit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
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
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100147static void snd_vt1724_ac97_write(struct snd_ac97 *ac97,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 unsigned short reg,
149 unsigned short val)
150{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100151 struct snd_ice1712 *ice = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 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
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100163static unsigned short snd_vt1724_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100165 struct snd_ice1712 *ice = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 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 */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100184static void snd_vt1724_set_gpio_dir(struct snd_ice1712 *ice, unsigned int data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
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) */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100191static void snd_vt1724_set_gpio_mask(struct snd_ice1712 *ice, unsigned int data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
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
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100199static void snd_vt1724_set_gpio_data(struct snd_ice1712 *ice, unsigned int data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
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
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100207static unsigned int snd_vt1724_get_gpio_data(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
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
David Howells7d12e782006-10-05 14:55:46 +0100222static irqreturn_t snd_vt1724_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100224 struct snd_ice1712 *ice = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 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;
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100234 /* 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 if ((status & VT1724_IRQ_MPU_RX)||(status & VT1724_IRQ_MPU_TX)) {
239 if (ice->rmidi[0])
David Howells7d12e782006-10-05 14:55:46 +0100240 snd_mpu401_uart_interrupt(irq, ice->rmidi[0]->private_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 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
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100310static struct snd_pcm_hw_constraint_list hw_constraints_rates_96 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 .count = ARRAY_SIZE(rates) - 2, /* up to 96000 */
312 .list = rates,
313 .mask = 0,
314};
315
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100316static struct snd_pcm_hw_constraint_list hw_constraints_rates_48 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 .count = ARRAY_SIZE(rates) - 5, /* up to 48000 */
318 .list = rates,
319 .mask = 0,
320};
321
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100322static struct snd_pcm_hw_constraint_list hw_constraints_rates_192 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 .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
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100335static int snd_vt1724_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100337 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 unsigned char what;
339 unsigned char old;
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100340 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 what = 0;
Takashi Iwaief991b92007-02-22 12:52:53 +0100343 snd_pcm_group_for_each_entry(s, substream) {
Takashi Iwai32b47da2007-01-29 15:26:36 +0100344 const struct vt1724_pcm_reg *reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 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
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100389static int get_max_rate(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
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
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100400static void snd_vt1724_set_pro_rate(struct snd_ice1712 *ice, unsigned int rate,
401 int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
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
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100483static int snd_vt1724_pcm_hw_params(struct snd_pcm_substream *substream,
484 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100486 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 int i, chs;
488
489 chs = params_channels(hw_params);
Ingo Molnar62932df2006-01-16 16:34:20 +0100490 mutex_lock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 /* 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++) {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100496 if (ice->pcm_reserved[i] &&
497 ice->pcm_reserved[i] != substream) {
Ingo Molnar62932df2006-01-16 16:34:20 +0100498 mutex_unlock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 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) {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100511 if (ice->pcm_reserved[i] &&
512 ice->pcm_reserved[i] != substream) {
Ingo Molnar62932df2006-01-16 16:34:20 +0100513 mutex_unlock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 return -EBUSY;
515 }
516 ice->pcm_reserved[i] = substream;
517 break;
518 }
519 }
520 }
Ingo Molnar62932df2006-01-16 16:34:20 +0100521 mutex_unlock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 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
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100526static int snd_vt1724_pcm_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100528 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 int i;
530
Ingo Molnar62932df2006-01-16 16:34:20 +0100531 mutex_lock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 /* unmark surround channels */
533 for (i = 0; i < 3; i++)
534 if (ice->pcm_reserved[i] == substream)
535 ice->pcm_reserved[i] = NULL;
Ingo Molnar62932df2006-01-16 16:34:20 +0100536 mutex_unlock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 return snd_pcm_lib_free_pages(substream);
538}
539
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100540static int snd_vt1724_playback_pro_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100542 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 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
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100567static snd_pcm_uframes_t snd_vt1724_playback_pro_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100569 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 size_t ptr;
571
572 if (!(inl(ICEMT1724(ice, DMA_CONTROL)) & VT1724_PDMA0_START))
573 return 0;
574#if 0 /* read PLAYBACK_ADDR */
575 ptr = inl(ICEMT1724(ice, PLAYBACK_ADDR));
576 if (ptr < substream->runtime->dma_addr) {
577 snd_printd("ice1724: invalid negative ptr\n");
578 return 0;
579 }
580 ptr -= substream->runtime->dma_addr;
581 ptr = bytes_to_frames(substream->runtime, ptr);
582 if (ptr >= substream->runtime->buffer_size) {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100583 snd_printd("ice1724: invalid ptr %d (size=%d)\n",
584 (int)ptr, (int)substream->runtime->period_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 return 0;
586 }
587#else /* read PLAYBACK_SIZE */
588 ptr = inl(ICEMT1724(ice, PLAYBACK_SIZE)) & 0xffffff;
589 ptr = (ptr + 1) << 2;
590 ptr = bytes_to_frames(substream->runtime, ptr);
591 if (! ptr)
592 ;
593 else if (ptr <= substream->runtime->buffer_size)
594 ptr = substream->runtime->buffer_size - ptr;
595 else {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100596 snd_printd("ice1724: invalid ptr %d (size=%d)\n",
597 (int)ptr, (int)substream->runtime->buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 ptr = 0;
599 }
600#endif
601 return ptr;
602}
603
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100604static int snd_vt1724_pcm_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100606 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Takashi Iwai32b47da2007-01-29 15:26:36 +0100607 const struct vt1724_pcm_reg *reg = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 spin_lock_irq(&ice->reg_lock);
610 outl(substream->runtime->dma_addr, ice->profi_port + reg->addr);
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100611 outw((snd_pcm_lib_buffer_bytes(substream) >> 2) - 1,
612 ice->profi_port + reg->size);
613 outw((snd_pcm_lib_period_bytes(substream) >> 2) - 1,
614 ice->profi_port + reg->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 spin_unlock_irq(&ice->reg_lock);
616 return 0;
617}
618
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100619static snd_pcm_uframes_t snd_vt1724_pcm_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100621 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Takashi Iwai32b47da2007-01-29 15:26:36 +0100622 const struct vt1724_pcm_reg *reg = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 size_t ptr;
624
625 if (!(inl(ICEMT1724(ice, DMA_CONTROL)) & reg->start))
626 return 0;
627#if 0 /* use ADDR register */
628 ptr = inl(ice->profi_port + reg->addr);
629 ptr -= substream->runtime->dma_addr;
630 return bytes_to_frames(substream->runtime, ptr);
631#else /* use SIZE register */
632 ptr = inw(ice->profi_port + reg->size);
633 ptr = (ptr + 1) << 2;
634 ptr = bytes_to_frames(substream->runtime, ptr);
635 if (! ptr)
636 ;
637 else if (ptr <= substream->runtime->buffer_size)
638 ptr = substream->runtime->buffer_size - ptr;
639 else {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100640 snd_printd("ice1724: invalid ptr %d (size=%d)\n",
641 (int)ptr, (int)substream->runtime->buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 ptr = 0;
643 }
644 return ptr;
645#endif
646}
647
Takashi Iwai32b47da2007-01-29 15:26:36 +0100648static const struct vt1724_pcm_reg vt1724_playback_pro_reg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 .addr = VT1724_MT_PLAYBACK_ADDR,
650 .size = VT1724_MT_PLAYBACK_SIZE,
651 .count = VT1724_MT_PLAYBACK_COUNT,
652 .start = VT1724_PDMA0_START,
653};
654
Takashi Iwai32b47da2007-01-29 15:26:36 +0100655static const struct vt1724_pcm_reg vt1724_capture_pro_reg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 .addr = VT1724_MT_CAPTURE_ADDR,
657 .size = VT1724_MT_CAPTURE_SIZE,
658 .count = VT1724_MT_CAPTURE_COUNT,
659 .start = VT1724_RDMA0_START,
660};
661
Takashi Iwai32b47da2007-01-29 15:26:36 +0100662static const struct snd_pcm_hardware snd_vt1724_playback_pro =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
664 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
665 SNDRV_PCM_INFO_BLOCK_TRANSFER |
666 SNDRV_PCM_INFO_MMAP_VALID |
667 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_SYNC_START),
668 .formats = SNDRV_PCM_FMTBIT_S32_LE,
669 .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_192000,
670 .rate_min = 8000,
671 .rate_max = 192000,
672 .channels_min = 2,
673 .channels_max = 8,
674 .buffer_bytes_max = (1UL << 21), /* 19bits dword */
675 .period_bytes_min = 8 * 4 * 2, /* FIXME: constraints needed */
676 .period_bytes_max = (1UL << 21),
677 .periods_min = 2,
678 .periods_max = 1024,
679};
680
Takashi Iwai32b47da2007-01-29 15:26:36 +0100681static const struct snd_pcm_hardware snd_vt1724_spdif =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
683 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
684 SNDRV_PCM_INFO_BLOCK_TRANSFER |
685 SNDRV_PCM_INFO_MMAP_VALID |
686 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_SYNC_START),
687 .formats = SNDRV_PCM_FMTBIT_S32_LE,
Takashi Iwai2dfbeca2005-10-12 10:04:42 +0200688 .rates = (SNDRV_PCM_RATE_32000|SNDRV_PCM_RATE_44100|
689 SNDRV_PCM_RATE_48000|SNDRV_PCM_RATE_88200|
690 SNDRV_PCM_RATE_96000|SNDRV_PCM_RATE_176400|
691 SNDRV_PCM_RATE_192000),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 .rate_min = 32000,
Takashi Iwai2dfbeca2005-10-12 10:04:42 +0200693 .rate_max = 192000,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 .channels_min = 2,
695 .channels_max = 2,
696 .buffer_bytes_max = (1UL << 18), /* 16bits dword */
697 .period_bytes_min = 2 * 4 * 2,
698 .period_bytes_max = (1UL << 18),
699 .periods_min = 2,
700 .periods_max = 1024,
701};
702
Takashi Iwai32b47da2007-01-29 15:26:36 +0100703static const struct snd_pcm_hardware snd_vt1724_2ch_stereo =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
705 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
706 SNDRV_PCM_INFO_BLOCK_TRANSFER |
707 SNDRV_PCM_INFO_MMAP_VALID |
708 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_SYNC_START),
709 .formats = SNDRV_PCM_FMTBIT_S32_LE,
710 .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_192000,
711 .rate_min = 8000,
712 .rate_max = 192000,
713 .channels_min = 2,
714 .channels_max = 2,
715 .buffer_bytes_max = (1UL << 18), /* 16bits dword */
716 .period_bytes_min = 2 * 4 * 2,
717 .period_bytes_max = (1UL << 18),
718 .periods_min = 2,
719 .periods_max = 1024,
720};
721
722/*
723 * set rate constraints
724 */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100725static int set_rate_constraints(struct snd_ice1712 *ice,
726 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100728 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 if (ice->hw_rates) {
730 /* hardware specific */
731 runtime->hw.rate_min = ice->hw_rates->list[0];
732 runtime->hw.rate_max = ice->hw_rates->list[ice->hw_rates->count - 1];
733 runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100734 return snd_pcm_hw_constraint_list(runtime, 0,
735 SNDRV_PCM_HW_PARAM_RATE,
736 ice->hw_rates);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 }
738 if (ice->eeprom.data[ICE_EEP2_ACLINK] & VT1724_CFG_PRO_I2S) {
739 /* I2S */
740 /* VT1720 doesn't support more than 96kHz */
741 if ((ice->eeprom.data[ICE_EEP2_I2S] & 0x08) && !ice->vt1720)
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100742 return snd_pcm_hw_constraint_list(runtime, 0,
743 SNDRV_PCM_HW_PARAM_RATE,
744 &hw_constraints_rates_192);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 else {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100746 runtime->hw.rates = SNDRV_PCM_RATE_KNOT |
747 SNDRV_PCM_RATE_8000_96000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 runtime->hw.rate_max = 96000;
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100749 return snd_pcm_hw_constraint_list(runtime, 0,
750 SNDRV_PCM_HW_PARAM_RATE,
751 &hw_constraints_rates_96);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 }
753 } else if (ice->ac97) {
754 /* ACLINK */
755 runtime->hw.rate_max = 48000;
756 runtime->hw.rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000;
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100757 return snd_pcm_hw_constraint_list(runtime, 0,
758 SNDRV_PCM_HW_PARAM_RATE,
759 &hw_constraints_rates_48);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 }
761 return 0;
762}
763
764/* multi-channel playback needs alignment 8x32bit regardless of the channels
765 * actually used
766 */
767#define VT1724_BUFFER_ALIGN 0x20
768
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100769static int snd_vt1724_playback_pro_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100771 struct snd_pcm_runtime *runtime = substream->runtime;
772 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 int chs;
774
Takashi Iwai32b47da2007-01-29 15:26:36 +0100775 runtime->private_data = (void *)&vt1724_playback_pro_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 ice->playback_pro_substream = substream;
777 runtime->hw = snd_vt1724_playback_pro;
778 snd_pcm_set_sync(substream);
779 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
780 set_rate_constraints(ice, substream);
Ingo Molnar62932df2006-01-16 16:34:20 +0100781 mutex_lock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 /* calculate the currently available channels */
783 for (chs = 0; chs < 3; chs++) {
784 if (ice->pcm_reserved[chs])
785 break;
786 }
787 chs = (chs + 1) * 2;
788 runtime->hw.channels_max = chs;
789 if (chs > 2) /* channels must be even */
790 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Ingo Molnar62932df2006-01-16 16:34:20 +0100791 mutex_unlock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
793 VT1724_BUFFER_ALIGN);
794 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
795 VT1724_BUFFER_ALIGN);
796 return 0;
797}
798
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100799static int snd_vt1724_capture_pro_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100801 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
802 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Takashi Iwai32b47da2007-01-29 15:26:36 +0100804 runtime->private_data = (void *)&vt1724_capture_pro_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 ice->capture_pro_substream = substream;
806 runtime->hw = snd_vt1724_2ch_stereo;
807 snd_pcm_set_sync(substream);
808 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
809 set_rate_constraints(ice, substream);
810 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
811 VT1724_BUFFER_ALIGN);
812 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
813 VT1724_BUFFER_ALIGN);
814 return 0;
815}
816
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100817static int snd_vt1724_playback_pro_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100819 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 if (PRO_RATE_RESET)
822 snd_vt1724_set_pro_rate(ice, PRO_RATE_DEFAULT, 0);
823 ice->playback_pro_substream = NULL;
824
825 return 0;
826}
827
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100828static int snd_vt1724_capture_pro_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100830 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
832 if (PRO_RATE_RESET)
833 snd_vt1724_set_pro_rate(ice, PRO_RATE_DEFAULT, 0);
834 ice->capture_pro_substream = NULL;
835 return 0;
836}
837
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100838static struct snd_pcm_ops snd_vt1724_playback_pro_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 .open = snd_vt1724_playback_pro_open,
840 .close = snd_vt1724_playback_pro_close,
841 .ioctl = snd_pcm_lib_ioctl,
842 .hw_params = snd_vt1724_pcm_hw_params,
843 .hw_free = snd_vt1724_pcm_hw_free,
844 .prepare = snd_vt1724_playback_pro_prepare,
845 .trigger = snd_vt1724_pcm_trigger,
846 .pointer = snd_vt1724_playback_pro_pointer,
847};
848
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100849static struct snd_pcm_ops snd_vt1724_capture_pro_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 .open = snd_vt1724_capture_pro_open,
851 .close = snd_vt1724_capture_pro_close,
852 .ioctl = snd_pcm_lib_ioctl,
853 .hw_params = snd_vt1724_pcm_hw_params,
854 .hw_free = snd_vt1724_pcm_hw_free,
855 .prepare = snd_vt1724_pcm_prepare,
856 .trigger = snd_vt1724_pcm_trigger,
857 .pointer = snd_vt1724_pcm_pointer,
858};
859
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100860static int __devinit snd_vt1724_pcm_profi(struct snd_ice1712 * ice, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100862 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 int err;
864
865 err = snd_pcm_new(ice->card, "ICE1724", device, 1, 1, &pcm);
866 if (err < 0)
867 return err;
868
869 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_vt1724_playback_pro_ops);
870 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_vt1724_capture_pro_ops);
871
872 pcm->private_data = ice;
873 pcm->info_flags = 0;
874 strcpy(pcm->name, "ICE1724");
875
876 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100877 snd_dma_pci_data(ice->pci),
878 256*1024, 256*1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
880 ice->pcm_pro = pcm;
881
882 return 0;
883}
884
885
886/*
887 * SPDIF PCM
888 */
889
Takashi Iwai32b47da2007-01-29 15:26:36 +0100890static const struct vt1724_pcm_reg vt1724_playback_spdif_reg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 .addr = VT1724_MT_PDMA4_ADDR,
892 .size = VT1724_MT_PDMA4_SIZE,
893 .count = VT1724_MT_PDMA4_COUNT,
894 .start = VT1724_PDMA4_START,
895};
896
Takashi Iwai32b47da2007-01-29 15:26:36 +0100897static const struct vt1724_pcm_reg vt1724_capture_spdif_reg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 .addr = VT1724_MT_RDMA1_ADDR,
899 .size = VT1724_MT_RDMA1_SIZE,
900 .count = VT1724_MT_RDMA1_COUNT,
901 .start = VT1724_RDMA1_START,
902};
903
904/* update spdif control bits; call with reg_lock */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100905static void update_spdif_bits(struct snd_ice1712 *ice, unsigned int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
907 unsigned char cbit, disabled;
908
909 cbit = inb(ICEREG1724(ice, SPDIF_CFG));
910 disabled = cbit & ~VT1724_CFG_SPDIF_OUT_EN;
911 if (cbit != disabled)
912 outb(disabled, ICEREG1724(ice, SPDIF_CFG));
913 outw(val, ICEMT1724(ice, SPDIF_CTRL));
914 if (cbit != disabled)
915 outb(cbit, ICEREG1724(ice, SPDIF_CFG));
916 outw(val, ICEMT1724(ice, SPDIF_CTRL));
917}
918
919/* update SPDIF control bits according to the given rate */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100920static void update_spdif_rate(struct snd_ice1712 *ice, unsigned int rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
922 unsigned int val, nval;
923 unsigned long flags;
924
925 spin_lock_irqsave(&ice->reg_lock, flags);
926 nval = val = inw(ICEMT1724(ice, SPDIF_CTRL));
927 nval &= ~(7 << 12);
928 switch (rate) {
929 case 44100: break;
930 case 48000: nval |= 2 << 12; break;
931 case 32000: nval |= 3 << 12; break;
Takashi Iwai2dfbeca2005-10-12 10:04:42 +0200932 case 88200: nval |= 4 << 12; break;
933 case 96000: nval |= 5 << 12; break;
934 case 192000: nval |= 6 << 12; break;
935 case 176400: nval |= 7 << 12; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 }
937 if (val != nval)
938 update_spdif_bits(ice, nval);
939 spin_unlock_irqrestore(&ice->reg_lock, flags);
940}
941
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100942static int snd_vt1724_playback_spdif_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100944 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 if (! ice->force_pdma4)
946 update_spdif_rate(ice, substream->runtime->rate);
947 return snd_vt1724_pcm_prepare(substream);
948}
949
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100950static int snd_vt1724_playback_spdif_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100952 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
953 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Takashi Iwai32b47da2007-01-29 15:26:36 +0100955 runtime->private_data = (void *)&vt1724_playback_spdif_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 ice->playback_con_substream = substream;
957 if (ice->force_pdma4) {
958 runtime->hw = snd_vt1724_2ch_stereo;
959 set_rate_constraints(ice, substream);
960 } else
961 runtime->hw = snd_vt1724_spdif;
962 snd_pcm_set_sync(substream);
963 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
964 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
965 VT1724_BUFFER_ALIGN);
966 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
967 VT1724_BUFFER_ALIGN);
968 return 0;
969}
970
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100971static int snd_vt1724_playback_spdif_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100973 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
975 if (PRO_RATE_RESET)
976 snd_vt1724_set_pro_rate(ice, PRO_RATE_DEFAULT, 0);
977 ice->playback_con_substream = NULL;
978
979 return 0;
980}
981
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100982static int snd_vt1724_capture_spdif_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100984 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
985 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Takashi Iwai32b47da2007-01-29 15:26:36 +0100987 runtime->private_data = (void *)&vt1724_capture_spdif_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 ice->capture_con_substream = substream;
989 if (ice->force_rdma1) {
990 runtime->hw = snd_vt1724_2ch_stereo;
991 set_rate_constraints(ice, substream);
992 } else
993 runtime->hw = snd_vt1724_spdif;
994 snd_pcm_set_sync(substream);
995 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
996 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
997 VT1724_BUFFER_ALIGN);
998 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
999 VT1724_BUFFER_ALIGN);
1000 return 0;
1001}
1002
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001003static int snd_vt1724_capture_spdif_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001005 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
1007 if (PRO_RATE_RESET)
1008 snd_vt1724_set_pro_rate(ice, PRO_RATE_DEFAULT, 0);
1009 ice->capture_con_substream = NULL;
1010
1011 return 0;
1012}
1013
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001014static struct snd_pcm_ops snd_vt1724_playback_spdif_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 .open = snd_vt1724_playback_spdif_open,
1016 .close = snd_vt1724_playback_spdif_close,
1017 .ioctl = snd_pcm_lib_ioctl,
1018 .hw_params = snd_vt1724_pcm_hw_params,
1019 .hw_free = snd_vt1724_pcm_hw_free,
1020 .prepare = snd_vt1724_playback_spdif_prepare,
1021 .trigger = snd_vt1724_pcm_trigger,
1022 .pointer = snd_vt1724_pcm_pointer,
1023};
1024
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001025static struct snd_pcm_ops snd_vt1724_capture_spdif_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 .open = snd_vt1724_capture_spdif_open,
1027 .close = snd_vt1724_capture_spdif_close,
1028 .ioctl = snd_pcm_lib_ioctl,
1029 .hw_params = snd_vt1724_pcm_hw_params,
1030 .hw_free = snd_vt1724_pcm_hw_free,
1031 .prepare = snd_vt1724_pcm_prepare,
1032 .trigger = snd_vt1724_pcm_trigger,
1033 .pointer = snd_vt1724_pcm_pointer,
1034};
1035
1036
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001037static int __devinit snd_vt1724_pcm_spdif(struct snd_ice1712 * ice, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038{
1039 char *name;
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001040 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 int play, capt;
1042 int err;
1043
1044 if (ice->force_pdma4 ||
1045 (ice->eeprom.data[ICE_EEP2_SPDIF] & VT1724_CFG_SPDIF_OUT_INT)) {
1046 play = 1;
1047 ice->has_spdif = 1;
1048 } else
1049 play = 0;
1050 if (ice->force_rdma1 ||
1051 (ice->eeprom.data[ICE_EEP2_SPDIF] & VT1724_CFG_SPDIF_IN)) {
1052 capt = 1;
1053 ice->has_spdif = 1;
1054 } else
1055 capt = 0;
1056 if (! play && ! capt)
1057 return 0; /* no spdif device */
1058
1059 if (ice->force_pdma4 || ice->force_rdma1)
1060 name = "ICE1724 Secondary";
1061 else
1062 name = "IEC1724 IEC958";
1063 err = snd_pcm_new(ice->card, name, device, play, capt, &pcm);
1064 if (err < 0)
1065 return err;
1066
1067 if (play)
1068 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1069 &snd_vt1724_playback_spdif_ops);
1070 if (capt)
1071 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1072 &snd_vt1724_capture_spdif_ops);
1073
1074 pcm->private_data = ice;
1075 pcm->info_flags = 0;
1076 strcpy(pcm->name, name);
1077
1078 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001079 snd_dma_pci_data(ice->pci),
1080 64*1024, 64*1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082 ice->pcm = pcm;
1083
1084 return 0;
1085}
1086
1087
1088/*
1089 * independent surround PCMs
1090 */
1091
Takashi Iwai32b47da2007-01-29 15:26:36 +01001092static const struct vt1724_pcm_reg vt1724_playback_dma_regs[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 {
1094 .addr = VT1724_MT_PDMA1_ADDR,
1095 .size = VT1724_MT_PDMA1_SIZE,
1096 .count = VT1724_MT_PDMA1_COUNT,
1097 .start = VT1724_PDMA1_START,
1098 },
1099 {
1100 .addr = VT1724_MT_PDMA2_ADDR,
1101 .size = VT1724_MT_PDMA2_SIZE,
1102 .count = VT1724_MT_PDMA2_COUNT,
1103 .start = VT1724_PDMA2_START,
1104 },
1105 {
1106 .addr = VT1724_MT_PDMA3_ADDR,
1107 .size = VT1724_MT_PDMA3_SIZE,
1108 .count = VT1724_MT_PDMA3_COUNT,
1109 .start = VT1724_PDMA3_START,
1110 },
1111};
1112
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001113static int snd_vt1724_playback_indep_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001115 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 unsigned char val;
1117
1118 spin_lock_irq(&ice->reg_lock);
1119 val = 3 - substream->number;
1120 if (inb(ICEMT1724(ice, BURST)) < val)
1121 outb(val, ICEMT1724(ice, BURST));
1122 spin_unlock_irq(&ice->reg_lock);
1123 return snd_vt1724_pcm_prepare(substream);
1124}
1125
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001126static int snd_vt1724_playback_indep_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001128 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
1129 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
Ingo Molnar62932df2006-01-16 16:34:20 +01001131 mutex_lock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 /* already used by PDMA0? */
1133 if (ice->pcm_reserved[substream->number]) {
Ingo Molnar62932df2006-01-16 16:34:20 +01001134 mutex_unlock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 return -EBUSY; /* FIXME: should handle blocking mode properly */
1136 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001137 mutex_unlock(&ice->open_mutex);
Takashi Iwai32b47da2007-01-29 15:26:36 +01001138 runtime->private_data = (void *)&vt1724_playback_dma_regs[substream->number];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 ice->playback_con_substream_ds[substream->number] = substream;
1140 runtime->hw = snd_vt1724_2ch_stereo;
1141 snd_pcm_set_sync(substream);
1142 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
1143 set_rate_constraints(ice, substream);
1144 return 0;
1145}
1146
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001147static int snd_vt1724_playback_indep_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001149 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
1151 if (PRO_RATE_RESET)
1152 snd_vt1724_set_pro_rate(ice, PRO_RATE_DEFAULT, 0);
1153 ice->playback_con_substream_ds[substream->number] = NULL;
1154 ice->pcm_reserved[substream->number] = NULL;
1155
1156 return 0;
1157}
1158
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001159static struct snd_pcm_ops snd_vt1724_playback_indep_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 .open = snd_vt1724_playback_indep_open,
1161 .close = snd_vt1724_playback_indep_close,
1162 .ioctl = snd_pcm_lib_ioctl,
1163 .hw_params = snd_vt1724_pcm_hw_params,
1164 .hw_free = snd_vt1724_pcm_hw_free,
1165 .prepare = snd_vt1724_playback_indep_prepare,
1166 .trigger = snd_vt1724_pcm_trigger,
1167 .pointer = snd_vt1724_pcm_pointer,
1168};
1169
1170
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001171static int __devinit snd_vt1724_pcm_indep(struct snd_ice1712 * ice, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001173 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 int play;
1175 int err;
1176
1177 play = ice->num_total_dacs / 2 - 1;
1178 if (play <= 0)
1179 return 0;
1180
1181 err = snd_pcm_new(ice->card, "ICE1724 Surrounds", device, play, 0, &pcm);
1182 if (err < 0)
1183 return err;
1184
1185 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1186 &snd_vt1724_playback_indep_ops);
1187
1188 pcm->private_data = ice;
1189 pcm->info_flags = 0;
1190 strcpy(pcm->name, "ICE1724 Surround PCM");
1191
1192 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001193 snd_dma_pci_data(ice->pci),
1194 64*1024, 64*1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
1196 ice->pcm_ds = pcm;
1197
1198 return 0;
1199}
1200
1201
1202/*
1203 * Mixer section
1204 */
1205
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001206static int __devinit snd_vt1724_ac97_mixer(struct snd_ice1712 * ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207{
1208 int err;
1209
1210 if (! (ice->eeprom.data[ICE_EEP2_ACLINK] & VT1724_CFG_PRO_I2S)) {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001211 struct snd_ac97_bus *pbus;
1212 struct snd_ac97_template ac97;
1213 static struct snd_ac97_bus_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 .write = snd_vt1724_ac97_write,
1215 .read = snd_vt1724_ac97_read,
1216 };
1217
1218 /* cold reset */
1219 outb(inb(ICEMT1724(ice, AC97_CMD)) | 0x80, ICEMT1724(ice, AC97_CMD));
1220 mdelay(5); /* FIXME */
1221 outb(inb(ICEMT1724(ice, AC97_CMD)) & ~0x80, ICEMT1724(ice, AC97_CMD));
1222
1223 if ((err = snd_ac97_bus(ice->card, 0, &ops, NULL, &pbus)) < 0)
1224 return err;
1225 memset(&ac97, 0, sizeof(ac97));
1226 ac97.private_data = ice;
1227 if ((err = snd_ac97_mixer(pbus, &ac97, &ice->ac97)) < 0)
1228 printk(KERN_WARNING "ice1712: cannot initialize pro ac97, skipped\n");
1229 else
1230 return 0;
1231 }
1232 /* I2S mixer only */
1233 strcat(ice->card->mixername, "ICE1724 - multitrack");
1234 return 0;
1235}
1236
1237/*
1238 *
1239 */
1240
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001241static inline unsigned int eeprom_triple(struct snd_ice1712 *ice, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242{
1243 return (unsigned int)ice->eeprom.data[idx] | \
1244 ((unsigned int)ice->eeprom.data[idx + 1] << 8) | \
1245 ((unsigned int)ice->eeprom.data[idx + 2] << 16);
1246}
1247
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001248static void snd_vt1724_proc_read(struct snd_info_entry *entry,
1249 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001251 struct snd_ice1712 *ice = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 unsigned int idx;
1253
1254 snd_iprintf(buffer, "%s\n\n", ice->card->longname);
1255 snd_iprintf(buffer, "EEPROM:\n");
1256
1257 snd_iprintf(buffer, " Subvendor : 0x%x\n", ice->eeprom.subvendor);
1258 snd_iprintf(buffer, " Size : %i bytes\n", ice->eeprom.size);
1259 snd_iprintf(buffer, " Version : %i\n", ice->eeprom.version);
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001260 snd_iprintf(buffer, " System Config : 0x%x\n",
1261 ice->eeprom.data[ICE_EEP2_SYSCONF]);
1262 snd_iprintf(buffer, " ACLink : 0x%x\n",
1263 ice->eeprom.data[ICE_EEP2_ACLINK]);
1264 snd_iprintf(buffer, " I2S : 0x%x\n",
1265 ice->eeprom.data[ICE_EEP2_I2S]);
1266 snd_iprintf(buffer, " S/PDIF : 0x%x\n",
1267 ice->eeprom.data[ICE_EEP2_SPDIF]);
1268 snd_iprintf(buffer, " GPIO direction : 0x%x\n",
1269 ice->eeprom.gpiodir);
1270 snd_iprintf(buffer, " GPIO mask : 0x%x\n",
1271 ice->eeprom.gpiomask);
1272 snd_iprintf(buffer, " GPIO state : 0x%x\n",
1273 ice->eeprom.gpiostate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 for (idx = 0x12; idx < ice->eeprom.size; idx++)
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001275 snd_iprintf(buffer, " Extra #%02i : 0x%x\n",
1276 idx, ice->eeprom.data[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
1278 snd_iprintf(buffer, "\nRegisters:\n");
1279
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001280 snd_iprintf(buffer, " PSDOUT03 : 0x%08x\n",
1281 (unsigned)inl(ICEMT1724(ice, ROUTE_PLAYBACK)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 for (idx = 0x0; idx < 0x20 ; idx++)
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001283 snd_iprintf(buffer, " CCS%02x : 0x%02x\n",
1284 idx, inb(ice->port+idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 for (idx = 0x0; idx < 0x30 ; idx++)
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001286 snd_iprintf(buffer, " MT%02x : 0x%02x\n",
1287 idx, inb(ice->profi_port+idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288}
1289
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001290static void __devinit snd_vt1724_proc_init(struct snd_ice1712 * ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001292 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
1294 if (! snd_card_proc_new(ice->card, "ice1724", &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02001295 snd_info_set_text_ops(entry, ice, snd_vt1724_proc_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296}
1297
1298/*
1299 *
1300 */
1301
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001302static int snd_vt1724_eeprom_info(struct snd_kcontrol *kcontrol,
1303 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304{
1305 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001306 uinfo->count = sizeof(struct snd_ice1712_eeprom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 return 0;
1308}
1309
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001310static int snd_vt1724_eeprom_get(struct snd_kcontrol *kcontrol,
1311 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001313 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
1315 memcpy(ucontrol->value.bytes.data, &ice->eeprom, sizeof(ice->eeprom));
1316 return 0;
1317}
1318
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01001319static struct snd_kcontrol_new snd_vt1724_eeprom __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1321 .name = "ICE1724 EEPROM",
1322 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1323 .info = snd_vt1724_eeprom_info,
1324 .get = snd_vt1724_eeprom_get
1325};
1326
1327/*
1328 */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001329static int snd_vt1724_spdif_info(struct snd_kcontrol *kcontrol,
1330 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331{
1332 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1333 uinfo->count = 1;
1334 return 0;
1335}
1336
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001337static unsigned int encode_spdif_bits(struct snd_aes_iec958 *diga)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
Takashi Iwai2dfbeca2005-10-12 10:04:42 +02001339 unsigned int val, rbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
1341 val = diga->status[0] & 0x03; /* professional, non-audio */
1342 if (val & 0x01) {
1343 /* professional */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001344 if ((diga->status[0] & IEC958_AES0_PRO_EMPHASIS) ==
1345 IEC958_AES0_PRO_EMPHASIS_5015)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 val |= 1U << 3;
Takashi Iwai2dfbeca2005-10-12 10:04:42 +02001347 rbits = (diga->status[4] >> 3) & 0x0f;
1348 if (rbits) {
1349 switch (rbits) {
1350 case 2: val |= 5 << 12; break; /* 96k */
1351 case 3: val |= 6 << 12; break; /* 192k */
1352 case 10: val |= 4 << 12; break; /* 88.2k */
1353 case 11: val |= 7 << 12; break; /* 176.4k */
1354 }
1355 } else {
1356 switch (diga->status[0] & IEC958_AES0_PRO_FS) {
1357 case IEC958_AES0_PRO_FS_44100:
1358 break;
1359 case IEC958_AES0_PRO_FS_32000:
1360 val |= 3U << 12;
1361 break;
1362 default:
1363 val |= 2U << 12;
1364 break;
1365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 }
1367 } else {
1368 /* consumer */
1369 val |= diga->status[1] & 0x04; /* copyright */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001370 if ((diga->status[0] & IEC958_AES0_CON_EMPHASIS) ==
1371 IEC958_AES0_CON_EMPHASIS_5015)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 val |= 1U << 3;
1373 val |= (unsigned int)(diga->status[1] & 0x3f) << 4; /* category */
1374 val |= (unsigned int)(diga->status[3] & IEC958_AES3_CON_FS) << 12; /* fs */
1375 }
1376 return val;
1377}
1378
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001379static void decode_spdif_bits(struct snd_aes_iec958 *diga, unsigned int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380{
1381 memset(diga->status, 0, sizeof(diga->status));
1382 diga->status[0] = val & 0x03; /* professional, non-audio */
1383 if (val & 0x01) {
1384 /* professional */
1385 if (val & (1U << 3))
1386 diga->status[0] |= IEC958_AES0_PRO_EMPHASIS_5015;
1387 switch ((val >> 12) & 0x7) {
1388 case 0:
1389 break;
1390 case 2:
1391 diga->status[0] |= IEC958_AES0_PRO_FS_32000;
1392 break;
1393 default:
1394 diga->status[0] |= IEC958_AES0_PRO_FS_48000;
1395 break;
1396 }
1397 } else {
1398 /* consumer */
1399 diga->status[0] |= val & (1U << 2); /* copyright */
1400 if (val & (1U << 3))
1401 diga->status[0] |= IEC958_AES0_CON_EMPHASIS_5015;
1402 diga->status[1] |= (val >> 4) & 0x3f; /* category */
1403 diga->status[3] |= (val >> 12) & 0x07; /* fs */
1404 }
1405}
1406
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001407static int snd_vt1724_spdif_default_get(struct snd_kcontrol *kcontrol,
1408 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001410 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 unsigned int val;
1412 val = inw(ICEMT1724(ice, SPDIF_CTRL));
1413 decode_spdif_bits(&ucontrol->value.iec958, val);
1414 return 0;
1415}
1416
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001417static int snd_vt1724_spdif_default_put(struct snd_kcontrol *kcontrol,
1418 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001420 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 unsigned int val, old;
1422
1423 val = encode_spdif_bits(&ucontrol->value.iec958);
1424 spin_lock_irq(&ice->reg_lock);
1425 old = inw(ICEMT1724(ice, SPDIF_CTRL));
1426 if (val != old)
1427 update_spdif_bits(ice, val);
1428 spin_unlock_irq(&ice->reg_lock);
1429 return (val != old);
1430}
1431
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01001432static struct snd_kcontrol_new snd_vt1724_spdif_default __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
1434 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1435 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1436 .info = snd_vt1724_spdif_info,
1437 .get = snd_vt1724_spdif_default_get,
1438 .put = snd_vt1724_spdif_default_put
1439};
1440
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001441static int snd_vt1724_spdif_maskc_get(struct snd_kcontrol *kcontrol,
1442 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443{
1444 ucontrol->value.iec958.status[0] = IEC958_AES0_NONAUDIO |
1445 IEC958_AES0_PROFESSIONAL |
1446 IEC958_AES0_CON_NOT_COPYRIGHT |
1447 IEC958_AES0_CON_EMPHASIS;
1448 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_ORIGINAL |
1449 IEC958_AES1_CON_CATEGORY;
1450 ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS;
1451 return 0;
1452}
1453
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001454static int snd_vt1724_spdif_maskp_get(struct snd_kcontrol *kcontrol,
1455 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456{
1457 ucontrol->value.iec958.status[0] = IEC958_AES0_NONAUDIO |
1458 IEC958_AES0_PROFESSIONAL |
1459 IEC958_AES0_PRO_FS |
1460 IEC958_AES0_PRO_EMPHASIS;
1461 return 0;
1462}
1463
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01001464static struct snd_kcontrol_new snd_vt1724_spdif_maskc __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465{
1466 .access = SNDRV_CTL_ELEM_ACCESS_READ,
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001467 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1469 .info = snd_vt1724_spdif_info,
1470 .get = snd_vt1724_spdif_maskc_get,
1471};
1472
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01001473static struct snd_kcontrol_new snd_vt1724_spdif_maskp __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474{
1475 .access = SNDRV_CTL_ELEM_ACCESS_READ,
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001476 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
1478 .info = snd_vt1724_spdif_info,
1479 .get = snd_vt1724_spdif_maskp_get,
1480};
1481
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001482#define snd_vt1724_spdif_sw_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001484static int snd_vt1724_spdif_sw_get(struct snd_kcontrol *kcontrol,
1485 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001487 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
1488 ucontrol->value.integer.value[0] = inb(ICEREG1724(ice, SPDIF_CFG)) &
1489 VT1724_CFG_SPDIF_OUT_EN ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 return 0;
1491}
1492
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001493static int snd_vt1724_spdif_sw_put(struct snd_kcontrol *kcontrol,
1494 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001496 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 unsigned char old, val;
1498
1499 spin_lock_irq(&ice->reg_lock);
1500 old = val = inb(ICEREG1724(ice, SPDIF_CFG));
1501 val &= ~VT1724_CFG_SPDIF_OUT_EN;
1502 if (ucontrol->value.integer.value[0])
1503 val |= VT1724_CFG_SPDIF_OUT_EN;
1504 if (old != val)
1505 outb(val, ICEREG1724(ice, SPDIF_CFG));
1506 spin_unlock_irq(&ice->reg_lock);
1507 return old != val;
1508}
1509
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01001510static struct snd_kcontrol_new snd_vt1724_spdif_switch __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511{
1512 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1513 /* FIXME: the following conflict with IEC958 Playback Route */
1514 // .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
Clemens Ladisch10e8d782005-08-03 13:40:08 +02001515 .name = SNDRV_CTL_NAME_IEC958("Output ",NONE,SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 .info = snd_vt1724_spdif_sw_info,
1517 .get = snd_vt1724_spdif_sw_get,
1518 .put = snd_vt1724_spdif_sw_put
1519};
1520
1521
1522#if 0 /* NOT USED YET */
1523/*
1524 * GPIO access from extern
1525 */
1526
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001527#define snd_vt1724_gpio_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001529int snd_vt1724_gpio_get(struct snd_kcontrol *kcontrol,
1530 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001532 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 int shift = kcontrol->private_value & 0xff;
1534 int invert = (kcontrol->private_value & (1<<24)) ? 1 : 0;
1535
1536 snd_ice1712_save_gpio_status(ice);
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001537 ucontrol->value.integer.value[0] =
1538 (snd_ice1712_gpio_read(ice) & (1 << shift) ? 1 : 0) ^ invert;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 snd_ice1712_restore_gpio_status(ice);
1540 return 0;
1541}
1542
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001543int snd_ice1712_gpio_put(struct snd_kcontrol *kcontrol,
1544 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001546 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 int shift = kcontrol->private_value & 0xff;
1548 int invert = (kcontrol->private_value & (1<<24)) ? mask : 0;
1549 unsigned int val, nval;
1550
1551 if (kcontrol->private_value & (1 << 31))
1552 return -EPERM;
1553 nval = (ucontrol->value.integer.value[0] ? (1 << shift) : 0) ^ invert;
1554 snd_ice1712_save_gpio_status(ice);
1555 val = snd_ice1712_gpio_read(ice);
1556 nval |= val & ~(1 << shift);
1557 if (val != nval)
1558 snd_ice1712_gpio_write(ice, nval);
1559 snd_ice1712_restore_gpio_status(ice);
1560 return val != nval;
1561}
1562#endif /* NOT USED YET */
1563
1564/*
1565 * rate
1566 */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001567static int snd_vt1724_pro_internal_clock_info(struct snd_kcontrol *kcontrol,
1568 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569{
Takashi Iwai32b47da2007-01-29 15:26:36 +01001570 static const char * const texts_1724[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 "8000", /* 0: 6 */
1572 "9600", /* 1: 3 */
1573 "11025", /* 2: 10 */
1574 "12000", /* 3: 2 */
1575 "16000", /* 4: 5 */
1576 "22050", /* 5: 9 */
1577 "24000", /* 6: 1 */
1578 "32000", /* 7: 4 */
1579 "44100", /* 8: 8 */
1580 "48000", /* 9: 0 */
1581 "64000", /* 10: 15 */
1582 "88200", /* 11: 11 */
1583 "96000", /* 12: 7 */
1584 "176400", /* 13: 12 */
1585 "192000", /* 14: 14 */
1586 "IEC958 Input", /* 15: -- */
1587 };
Takashi Iwai32b47da2007-01-29 15:26:36 +01001588 static const char * const texts_1720[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 "8000", /* 0: 6 */
1590 "9600", /* 1: 3 */
1591 "11025", /* 2: 10 */
1592 "12000", /* 3: 2 */
1593 "16000", /* 4: 5 */
1594 "22050", /* 5: 9 */
1595 "24000", /* 6: 1 */
1596 "32000", /* 7: 4 */
1597 "44100", /* 8: 8 */
1598 "48000", /* 9: 0 */
1599 "64000", /* 10: 15 */
1600 "88200", /* 11: 11 */
1601 "96000", /* 12: 7 */
1602 "IEC958 Input", /* 13: -- */
1603 };
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001604 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
1606 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1607 uinfo->count = 1;
1608 uinfo->value.enumerated.items = ice->vt1720 ? 14 : 16;
1609 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1610 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
1611 strcpy(uinfo->value.enumerated.name,
1612 ice->vt1720 ? texts_1720[uinfo->value.enumerated.item] :
1613 texts_1724[uinfo->value.enumerated.item]);
1614 return 0;
1615}
1616
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001617static int snd_vt1724_pro_internal_clock_get(struct snd_kcontrol *kcontrol,
1618 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001620 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Takashi Iwai32b47da2007-01-29 15:26:36 +01001621 static const unsigned char xlate[16] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 9, 6, 3, 1, 7, 4, 0, 12, 8, 5, 2, 11, 13, 255, 14, 10
1623 };
1624 unsigned char val;
1625
1626 spin_lock_irq(&ice->reg_lock);
1627 if (is_spdif_master(ice)) {
1628 ucontrol->value.enumerated.item[0] = ice->vt1720 ? 13 : 15;
1629 } else {
1630 val = xlate[inb(ICEMT1724(ice, RATE)) & 15];
1631 if (val == 255) {
1632 snd_BUG();
1633 val = 0;
1634 }
1635 ucontrol->value.enumerated.item[0] = val;
1636 }
1637 spin_unlock_irq(&ice->reg_lock);
1638 return 0;
1639}
1640
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001641static int snd_vt1724_pro_internal_clock_put(struct snd_kcontrol *kcontrol,
1642 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001644 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 unsigned char oval;
1646 int rate;
1647 int change = 0;
1648 int spdif = ice->vt1720 ? 13 : 15;
1649
1650 spin_lock_irq(&ice->reg_lock);
1651 oval = inb(ICEMT1724(ice, RATE));
1652 if (ucontrol->value.enumerated.item[0] == spdif) {
Pavel Hofmanc5a30f82007-04-24 12:27:36 +02001653 unsigned char i2s_oval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 outb(oval | VT1724_SPDIF_MASTER, ICEMT1724(ice, RATE));
Pavel Hofmanc5a30f82007-04-24 12:27:36 +02001655 /* setting 256fs */
1656 i2s_oval = inb(ICEMT1724(ice, I2S_FORMAT));
1657 outb(i2s_oval & ~VT1724_MT_I2S_MCLK_128X,
1658 ICEMT1724(ice, I2S_FORMAT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 } else {
1660 rate = rates[ucontrol->value.integer.value[0] % 15];
1661 if (rate <= get_max_rate(ice)) {
1662 PRO_RATE_DEFAULT = rate;
1663 spin_unlock_irq(&ice->reg_lock);
1664 snd_vt1724_set_pro_rate(ice, PRO_RATE_DEFAULT, 1);
1665 spin_lock_irq(&ice->reg_lock);
1666 }
1667 }
1668 change = inb(ICEMT1724(ice, RATE)) != oval;
1669 spin_unlock_irq(&ice->reg_lock);
1670
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001671 if ((oval & VT1724_SPDIF_MASTER) !=
1672 (inb(ICEMT1724(ice, RATE)) & VT1724_SPDIF_MASTER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 /* notify akm chips as well */
1674 if (is_spdif_master(ice)) {
1675 unsigned int i;
1676 for (i = 0; i < ice->akm_codecs; i++) {
1677 if (ice->akm[i].ops.set_rate_val)
1678 ice->akm[i].ops.set_rate_val(&ice->akm[i], 0);
1679 }
1680 }
1681 }
1682 return change;
1683}
1684
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01001685static struct snd_kcontrol_new snd_vt1724_pro_internal_clock __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1687 .name = "Multi Track Internal Clock",
1688 .info = snd_vt1724_pro_internal_clock_info,
1689 .get = snd_vt1724_pro_internal_clock_get,
1690 .put = snd_vt1724_pro_internal_clock_put
1691};
1692
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001693#define snd_vt1724_pro_rate_locking_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001695static int snd_vt1724_pro_rate_locking_get(struct snd_kcontrol *kcontrol,
1696 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697{
1698 ucontrol->value.integer.value[0] = PRO_RATE_LOCKED;
1699 return 0;
1700}
1701
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001702static int snd_vt1724_pro_rate_locking_put(struct snd_kcontrol *kcontrol,
1703 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001705 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 int change = 0, nval;
1707
1708 nval = ucontrol->value.integer.value[0] ? 1 : 0;
1709 spin_lock_irq(&ice->reg_lock);
1710 change = PRO_RATE_LOCKED != nval;
1711 PRO_RATE_LOCKED = nval;
1712 spin_unlock_irq(&ice->reg_lock);
1713 return change;
1714}
1715
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01001716static struct snd_kcontrol_new snd_vt1724_pro_rate_locking __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1718 .name = "Multi Track Rate Locking",
1719 .info = snd_vt1724_pro_rate_locking_info,
1720 .get = snd_vt1724_pro_rate_locking_get,
1721 .put = snd_vt1724_pro_rate_locking_put
1722};
1723
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001724#define snd_vt1724_pro_rate_reset_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001726static int snd_vt1724_pro_rate_reset_get(struct snd_kcontrol *kcontrol,
1727 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728{
1729 ucontrol->value.integer.value[0] = PRO_RATE_RESET ? 1 : 0;
1730 return 0;
1731}
1732
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001733static int snd_vt1724_pro_rate_reset_put(struct snd_kcontrol *kcontrol,
1734 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001736 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 int change = 0, nval;
1738
1739 nval = ucontrol->value.integer.value[0] ? 1 : 0;
1740 spin_lock_irq(&ice->reg_lock);
1741 change = PRO_RATE_RESET != nval;
1742 PRO_RATE_RESET = nval;
1743 spin_unlock_irq(&ice->reg_lock);
1744 return change;
1745}
1746
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01001747static struct snd_kcontrol_new snd_vt1724_pro_rate_reset __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1749 .name = "Multi Track Rate Reset",
1750 .info = snd_vt1724_pro_rate_reset_info,
1751 .get = snd_vt1724_pro_rate_reset_get,
1752 .put = snd_vt1724_pro_rate_reset_put
1753};
1754
1755
1756/*
1757 * routing
1758 */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001759static int snd_vt1724_pro_route_info(struct snd_kcontrol *kcontrol,
1760 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761{
1762 static char *texts[] = {
1763 "PCM Out", /* 0 */
1764 "H/W In 0", "H/W In 1", /* 1-2 */
1765 "IEC958 In L", "IEC958 In R", /* 3-4 */
1766 };
1767
1768 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1769 uinfo->count = 1;
1770 uinfo->value.enumerated.items = 5;
1771 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1772 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
1773 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1774 return 0;
1775}
1776
1777static inline int analog_route_shift(int idx)
1778{
1779 return (idx % 2) * 12 + ((idx / 2) * 3) + 8;
1780}
1781
1782static inline int digital_route_shift(int idx)
1783{
1784 return idx * 3;
1785}
1786
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001787static int get_route_val(struct snd_ice1712 *ice, int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788{
1789 unsigned long val;
1790 unsigned char eitem;
Takashi Iwai32b47da2007-01-29 15:26:36 +01001791 static const unsigned char xlate[8] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 0, 255, 1, 2, 255, 255, 3, 4,
1793 };
1794
1795 val = inl(ICEMT1724(ice, ROUTE_PLAYBACK));
1796 val >>= shift;
1797 val &= 7; //we now have 3 bits per output
1798 eitem = xlate[val];
1799 if (eitem == 255) {
1800 snd_BUG();
1801 return 0;
1802 }
1803 return eitem;
1804}
1805
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001806static int put_route_val(struct snd_ice1712 *ice, unsigned int val, int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807{
1808 unsigned int old_val, nval;
1809 int change;
Takashi Iwai32b47da2007-01-29 15:26:36 +01001810 static const unsigned char xroute[8] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 0, /* PCM */
1812 2, /* PSDIN0 Left */
1813 3, /* PSDIN0 Right */
1814 6, /* SPDIN Left */
1815 7, /* SPDIN Right */
1816 };
1817
1818 nval = xroute[val % 5];
1819 val = old_val = inl(ICEMT1724(ice, ROUTE_PLAYBACK));
1820 val &= ~(0x07 << shift);
1821 val |= nval << shift;
1822 change = val != old_val;
1823 if (change)
1824 outl(val, ICEMT1724(ice, ROUTE_PLAYBACK));
1825 return change;
1826}
1827
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001828static int snd_vt1724_pro_route_analog_get(struct snd_kcontrol *kcontrol,
1829 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001831 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001833 ucontrol->value.enumerated.item[0] =
1834 get_route_val(ice, analog_route_shift(idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 return 0;
1836}
1837
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001838static int snd_vt1724_pro_route_analog_put(struct snd_kcontrol *kcontrol,
1839 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001841 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1843 return put_route_val(ice, ucontrol->value.enumerated.item[0],
1844 analog_route_shift(idx));
1845}
1846
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001847static int snd_vt1724_pro_route_spdif_get(struct snd_kcontrol *kcontrol,
1848 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001850 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001852 ucontrol->value.enumerated.item[0] =
1853 get_route_val(ice, digital_route_shift(idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 return 0;
1855}
1856
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001857static int snd_vt1724_pro_route_spdif_put(struct snd_kcontrol *kcontrol,
1858 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001860 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1862 return put_route_val(ice, ucontrol->value.enumerated.item[0],
1863 digital_route_shift(idx));
1864}
1865
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01001866static struct snd_kcontrol_new snd_vt1724_mixer_pro_analog_route __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1868 .name = "H/W Playback Route",
1869 .info = snd_vt1724_pro_route_info,
1870 .get = snd_vt1724_pro_route_analog_get,
1871 .put = snd_vt1724_pro_route_analog_put,
1872};
1873
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01001874static struct snd_kcontrol_new snd_vt1724_mixer_pro_spdif_route __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1876 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Route",
1877 .info = snd_vt1724_pro_route_info,
1878 .get = snd_vt1724_pro_route_spdif_get,
1879 .put = snd_vt1724_pro_route_spdif_put,
1880 .count = 2,
1881};
1882
1883
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001884static int snd_vt1724_pro_peak_info(struct snd_kcontrol *kcontrol,
1885 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886{
1887 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1888 uinfo->count = 22; /* FIXME: for compatibility with ice1712... */
1889 uinfo->value.integer.min = 0;
1890 uinfo->value.integer.max = 255;
1891 return 0;
1892}
1893
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001894static int snd_vt1724_pro_peak_get(struct snd_kcontrol *kcontrol,
1895 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001897 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 int idx;
1899
1900 spin_lock_irq(&ice->reg_lock);
1901 for (idx = 0; idx < 22; idx++) {
1902 outb(idx, ICEMT1724(ice, MONITOR_PEAKINDEX));
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001903 ucontrol->value.integer.value[idx] =
1904 inb(ICEMT1724(ice, MONITOR_PEAKDATA));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 }
1906 spin_unlock_irq(&ice->reg_lock);
1907 return 0;
1908}
1909
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01001910static struct snd_kcontrol_new snd_vt1724_mixer_pro_peak __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1912 .name = "Multi Track Peak",
1913 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
1914 .info = snd_vt1724_pro_peak_info,
1915 .get = snd_vt1724_pro_peak_get
1916};
1917
1918/*
1919 *
1920 */
1921
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01001922static struct snd_ice1712_card_info no_matched __devinitdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01001924static struct snd_ice1712_card_info *card_tables[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 snd_vt1724_revo_cards,
1926 snd_vt1724_amp_cards,
1927 snd_vt1724_aureon_cards,
1928 snd_vt1720_mobo_cards,
1929 snd_vt1720_pontis_cards,
1930 snd_vt1724_prodigy192_cards,
1931 snd_vt1724_juli_cards,
1932 snd_vt1724_phase_cards,
Clement Guedezf6cdab52007-01-08 10:48:41 +01001933 snd_vt1724_wtm_cards,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 NULL,
1935};
1936
1937
1938/*
1939 */
1940
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001941static void wait_i2c_busy(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942{
1943 int t = 0x10000;
1944 while ((inb(ICEREG1724(ice, I2C_CTRL)) & VT1724_I2C_BUSY) && t--)
1945 ;
1946 if (t == -1)
1947 printk(KERN_ERR "ice1724: i2c busy timeout\n");
1948}
1949
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001950unsigned char snd_vt1724_read_i2c(struct snd_ice1712 *ice,
1951 unsigned char dev, unsigned char addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952{
1953 unsigned char val;
1954
Ingo Molnar62932df2006-01-16 16:34:20 +01001955 mutex_lock(&ice->i2c_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 outb(addr, ICEREG1724(ice, I2C_BYTE_ADDR));
1957 outb(dev & ~VT1724_I2C_WRITE, ICEREG1724(ice, I2C_DEV_ADDR));
1958 wait_i2c_busy(ice);
1959 val = inb(ICEREG1724(ice, I2C_DATA));
Ingo Molnar62932df2006-01-16 16:34:20 +01001960 mutex_unlock(&ice->i2c_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 //printk("i2c_read: [0x%x,0x%x] = 0x%x\n", dev, addr, val);
1962 return val;
1963}
1964
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001965void snd_vt1724_write_i2c(struct snd_ice1712 *ice,
1966 unsigned char dev, unsigned char addr, unsigned char data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967{
Ingo Molnar62932df2006-01-16 16:34:20 +01001968 mutex_lock(&ice->i2c_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 wait_i2c_busy(ice);
1970 //printk("i2c_write: [0x%x,0x%x] = 0x%x\n", dev, addr, data);
1971 outb(addr, ICEREG1724(ice, I2C_BYTE_ADDR));
1972 outb(data, ICEREG1724(ice, I2C_DATA));
1973 outb(dev | VT1724_I2C_WRITE, ICEREG1724(ice, I2C_DEV_ADDR));
1974 wait_i2c_busy(ice);
Ingo Molnar62932df2006-01-16 16:34:20 +01001975 mutex_unlock(&ice->i2c_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976}
1977
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001978static int __devinit snd_vt1724_read_eeprom(struct snd_ice1712 *ice,
1979 const char *modelname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980{
1981 const int dev = 0xa0; /* EEPROM device address */
1982 unsigned int i, size;
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01001983 struct snd_ice1712_card_info * const *tbl, *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
1985 if (! modelname || ! *modelname) {
1986 ice->eeprom.subvendor = 0;
1987 if ((inb(ICEREG1724(ice, I2C_CTRL)) & VT1724_I2C_EEPROM) != 0)
1988 ice->eeprom.subvendor =
1989 (snd_vt1724_read_i2c(ice, dev, 0x00) << 0) |
1990 (snd_vt1724_read_i2c(ice, dev, 0x01) << 8) |
1991 (snd_vt1724_read_i2c(ice, dev, 0x02) << 16) |
1992 (snd_vt1724_read_i2c(ice, dev, 0x03) << 24);
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001993 if (ice->eeprom.subvendor == 0 ||
1994 ice->eeprom.subvendor == (unsigned int)-1) {
1995 /* invalid subvendor from EEPROM, try the PCI
1996 * subststem ID instead
1997 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 u16 vendor, device;
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001999 pci_read_config_word(ice->pci, PCI_SUBSYSTEM_VENDOR_ID,
2000 &vendor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 pci_read_config_word(ice->pci, PCI_SUBSYSTEM_ID, &device);
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002002 ice->eeprom.subvendor =
2003 ((unsigned int)swab16(vendor) << 16) | swab16(device);
2004 if (ice->eeprom.subvendor == 0 ||
2005 ice->eeprom.subvendor == (unsigned int)-1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 printk(KERN_ERR "ice1724: No valid ID is found\n");
2007 return -ENXIO;
2008 }
2009 }
2010 }
2011 for (tbl = card_tables; *tbl; tbl++) {
2012 for (c = *tbl; c->subvendor; c++) {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002013 if (modelname && c->model &&
2014 ! strcmp(modelname, c->model)) {
2015 printk(KERN_INFO "ice1724: Using board model %s\n",
2016 c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 ice->eeprom.subvendor = c->subvendor;
2018 } else if (c->subvendor != ice->eeprom.subvendor)
2019 continue;
2020 if (! c->eeprom_size || ! c->eeprom_data)
2021 goto found;
2022 /* if the EEPROM is given by the driver, use it */
2023 snd_printdd("using the defined eeprom..\n");
2024 ice->eeprom.version = 2;
2025 ice->eeprom.size = c->eeprom_size + 6;
2026 memcpy(ice->eeprom.data, c->eeprom_data, c->eeprom_size);
2027 goto read_skipped;
2028 }
2029 }
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002030 printk(KERN_WARNING "ice1724: No matching model found for ID 0x%x\n",
2031 ice->eeprom.subvendor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032
2033 found:
2034 ice->eeprom.size = snd_vt1724_read_i2c(ice, dev, 0x04);
2035 if (ice->eeprom.size < 6)
2036 ice->eeprom.size = 32;
2037 else if (ice->eeprom.size > 32) {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002038 printk(KERN_ERR "ice1724: Invalid EEPROM (size = %i)\n",
2039 ice->eeprom.size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 return -EIO;
2041 }
2042 ice->eeprom.version = snd_vt1724_read_i2c(ice, dev, 0x05);
2043 if (ice->eeprom.version != 2)
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002044 printk(KERN_WARNING "ice1724: Invalid EEPROM version %i\n",
2045 ice->eeprom.version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 size = ice->eeprom.size - 6;
2047 for (i = 0; i < size; i++)
2048 ice->eeprom.data[i] = snd_vt1724_read_i2c(ice, dev, i + 6);
2049
2050 read_skipped:
2051 ice->eeprom.gpiomask = eeprom_triple(ice, ICE_EEP2_GPIO_MASK);
2052 ice->eeprom.gpiostate = eeprom_triple(ice, ICE_EEP2_GPIO_STATE);
2053 ice->eeprom.gpiodir = eeprom_triple(ice, ICE_EEP2_GPIO_DIR);
2054
2055 return 0;
2056}
2057
2058
2059
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002060static int __devinit snd_vt1724_chip_init(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061{
2062 outb(VT1724_RESET , ICEREG1724(ice, CONTROL));
2063 udelay(200);
2064 outb(0, ICEREG1724(ice, CONTROL));
2065 udelay(200);
2066 outb(ice->eeprom.data[ICE_EEP2_SYSCONF], ICEREG1724(ice, SYS_CFG));
2067 outb(ice->eeprom.data[ICE_EEP2_ACLINK], ICEREG1724(ice, AC97_CFG));
2068 outb(ice->eeprom.data[ICE_EEP2_I2S], ICEREG1724(ice, I2S_FEATURES));
2069 outb(ice->eeprom.data[ICE_EEP2_SPDIF], ICEREG1724(ice, SPDIF_CFG));
2070
2071 ice->gpio.write_mask = ice->eeprom.gpiomask;
2072 ice->gpio.direction = ice->eeprom.gpiodir;
2073 snd_vt1724_set_gpio_mask(ice, ice->eeprom.gpiomask);
2074 snd_vt1724_set_gpio_dir(ice, ice->eeprom.gpiodir);
2075 snd_vt1724_set_gpio_data(ice, ice->eeprom.gpiostate);
2076
2077 outb(0, ICEREG1724(ice, POWERDOWN));
2078
2079 return 0;
2080}
2081
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002082static int __devinit snd_vt1724_spdif_build_controls(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083{
2084 int err;
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002085 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
2087 snd_assert(ice->pcm != NULL, return -EIO);
2088
2089 err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_mixer_pro_spdif_route, ice));
2090 if (err < 0)
2091 return err;
2092
2093 err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_spdif_switch, ice));
2094 if (err < 0)
2095 return err;
2096
2097 err = snd_ctl_add(ice->card, kctl = snd_ctl_new1(&snd_vt1724_spdif_default, ice));
2098 if (err < 0)
2099 return err;
2100 kctl->id.device = ice->pcm->device;
2101 err = snd_ctl_add(ice->card, kctl = snd_ctl_new1(&snd_vt1724_spdif_maskc, ice));
2102 if (err < 0)
2103 return err;
2104 kctl->id.device = ice->pcm->device;
2105 err = snd_ctl_add(ice->card, kctl = snd_ctl_new1(&snd_vt1724_spdif_maskp, ice));
2106 if (err < 0)
2107 return err;
2108 kctl->id.device = ice->pcm->device;
2109#if 0 /* use default only */
2110 err = snd_ctl_add(ice->card, kctl = snd_ctl_new1(&snd_vt1724_spdif_stream, ice));
2111 if (err < 0)
2112 return err;
2113 kctl->id.device = ice->pcm->device;
2114 ice->spdif.stream_ctl = kctl;
2115#endif
2116 return 0;
2117}
2118
2119
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002120static int __devinit snd_vt1724_build_controls(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121{
2122 int err;
2123
2124 err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_eeprom, ice));
2125 if (err < 0)
2126 return err;
2127 err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_pro_internal_clock, ice));
2128 if (err < 0)
2129 return err;
2130
2131 err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_pro_rate_locking, ice));
2132 if (err < 0)
2133 return err;
2134 err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_pro_rate_reset, ice));
2135 if (err < 0)
2136 return err;
2137
2138 if (ice->num_total_dacs > 0) {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002139 struct snd_kcontrol_new tmp = snd_vt1724_mixer_pro_analog_route;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 tmp.count = ice->num_total_dacs;
2141 if (ice->vt1720 && tmp.count > 2)
2142 tmp.count = 2;
2143 err = snd_ctl_add(ice->card, snd_ctl_new1(&tmp, ice));
2144 if (err < 0)
2145 return err;
2146 }
2147
2148 err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_mixer_pro_peak, ice));
2149 if (err < 0)
2150 return err;
2151
2152 return 0;
2153}
2154
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002155static int snd_vt1724_free(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156{
2157 if (! ice->port)
2158 goto __hw_end;
2159 /* mask all interrupts */
2160 outb(0xff, ICEMT1724(ice, DMA_INT_MASK));
2161 outb(0xff, ICEREG1724(ice, IRQMASK));
2162 /* --- */
2163 __hw_end:
2164 if (ice->irq >= 0) {
2165 synchronize_irq(ice->irq);
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002166 free_irq(ice->irq, ice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 }
2168 pci_release_regions(ice->pci);
2169 snd_ice1712_akm4xxx_free(ice);
2170 pci_disable_device(ice->pci);
2171 kfree(ice);
2172 return 0;
2173}
2174
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002175static int snd_vt1724_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002177 struct snd_ice1712 *ice = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 return snd_vt1724_free(ice);
2179}
2180
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002181static int __devinit snd_vt1724_create(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 struct pci_dev *pci,
2183 const char *modelname,
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002184 struct snd_ice1712 ** r_ice1712)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002186 struct snd_ice1712 *ice;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 int err;
2188 unsigned char mask;
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002189 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 .dev_free = snd_vt1724_dev_free,
2191 };
2192
2193 *r_ice1712 = NULL;
2194
2195 /* enable PCI device */
2196 if ((err = pci_enable_device(pci)) < 0)
2197 return err;
2198
Takashi Iwaie560d8d2005-09-09 14:21:46 +02002199 ice = kzalloc(sizeof(*ice), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 if (ice == NULL) {
2201 pci_disable_device(pci);
2202 return -ENOMEM;
2203 }
2204 ice->vt1724 = 1;
2205 spin_lock_init(&ice->reg_lock);
Ingo Molnar62932df2006-01-16 16:34:20 +01002206 mutex_init(&ice->gpio_mutex);
2207 mutex_init(&ice->open_mutex);
2208 mutex_init(&ice->i2c_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 ice->gpio.set_mask = snd_vt1724_set_gpio_mask;
2210 ice->gpio.set_dir = snd_vt1724_set_gpio_dir;
2211 ice->gpio.set_data = snd_vt1724_set_gpio_data;
2212 ice->gpio.get_data = snd_vt1724_get_gpio_data;
2213 ice->card = card;
2214 ice->pci = pci;
2215 ice->irq = -1;
2216 pci_set_master(pci);
2217 snd_vt1724_proc_init(ice);
2218 synchronize_irq(pci->irq);
2219
2220 if ((err = pci_request_regions(pci, "ICE1724")) < 0) {
2221 kfree(ice);
2222 pci_disable_device(pci);
2223 return err;
2224 }
2225 ice->port = pci_resource_start(pci, 0);
2226 ice->profi_port = pci_resource_start(pci, 1);
2227
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002228 if (request_irq(pci->irq, snd_vt1724_interrupt,
Takashi Iwai437a5a42006-11-21 12:14:23 +01002229 IRQF_SHARED, "ICE1724", ice)) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02002230 snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 snd_vt1724_free(ice);
2232 return -EIO;
2233 }
2234
2235 ice->irq = pci->irq;
2236
2237 if (snd_vt1724_read_eeprom(ice, modelname) < 0) {
2238 snd_vt1724_free(ice);
2239 return -EIO;
2240 }
2241 if (snd_vt1724_chip_init(ice) < 0) {
2242 snd_vt1724_free(ice);
2243 return -EIO;
2244 }
2245
2246 /* unmask used interrupts */
2247 if (! (ice->eeprom.data[ICE_EEP2_SYSCONF] & VT1724_CFG_MPU401))
2248 mask = VT1724_IRQ_MPU_RX | VT1724_IRQ_MPU_TX;
2249 else
2250 mask = 0;
2251 outb(mask, ICEREG1724(ice, IRQMASK));
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002252 /* don't handle FIFO overrun/underruns (just yet),
2253 * since they cause machine lockups
2254 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 outb(VT1724_MULTI_FIFO_ERR, ICEMT1724(ice, DMA_INT_MASK));
2256
2257 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, ice, &ops)) < 0) {
2258 snd_vt1724_free(ice);
2259 return err;
2260 }
2261
2262 snd_card_set_dev(card, &pci->dev);
2263
2264 *r_ice1712 = ice;
2265 return 0;
2266}
2267
2268
2269/*
2270 *
2271 * Registration
2272 *
2273 */
2274
2275static int __devinit snd_vt1724_probe(struct pci_dev *pci,
2276 const struct pci_device_id *pci_id)
2277{
2278 static int dev;
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002279 struct snd_card *card;
2280 struct snd_ice1712 *ice;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 int pcm_dev = 0, err;
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01002282 struct snd_ice1712_card_info * const *tbl, *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
2284 if (dev >= SNDRV_CARDS)
2285 return -ENODEV;
2286 if (!enable[dev]) {
2287 dev++;
2288 return -ENOENT;
2289 }
2290
2291 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
2292 if (card == NULL)
2293 return -ENOMEM;
2294
2295 strcpy(card->driver, "ICE1724");
2296 strcpy(card->shortname, "ICEnsemble ICE1724");
2297
2298 if ((err = snd_vt1724_create(card, pci, model[dev], &ice)) < 0) {
2299 snd_card_free(card);
2300 return err;
2301 }
2302
2303 for (tbl = card_tables; *tbl; tbl++) {
2304 for (c = *tbl; c->subvendor; c++) {
2305 if (c->subvendor == ice->eeprom.subvendor) {
2306 strcpy(card->shortname, c->name);
2307 if (c->driver) /* specific driver? */
2308 strcpy(card->driver, c->driver);
2309 if (c->chip_init) {
2310 if ((err = c->chip_init(ice)) < 0) {
2311 snd_card_free(card);
2312 return err;
2313 }
2314 }
2315 goto __found;
2316 }
2317 }
2318 }
2319 c = &no_matched;
2320 __found:
Takashi Iwaif06934b2007-04-10 11:15:34 +02002321 /*
2322 * VT1724 has separate DMAs for the analog and the SPDIF streams while
2323 * ICE1712 has only one for both (mixed up).
2324 *
2325 * Confusingly the analog PCM is named "professional" here because it
2326 * was called so in ice1712 driver, and vt1724 driver is derived from
2327 * ice1712 driver.
2328 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
2330 if ((err = snd_vt1724_pcm_profi(ice, pcm_dev++)) < 0) {
2331 snd_card_free(card);
2332 return err;
2333 }
2334
2335 if ((err = snd_vt1724_pcm_spdif(ice, pcm_dev++)) < 0) {
2336 snd_card_free(card);
2337 return err;
2338 }
2339
2340 if ((err = snd_vt1724_pcm_indep(ice, pcm_dev++)) < 0) {
2341 snd_card_free(card);
2342 return err;
2343 }
2344
2345 if ((err = snd_vt1724_ac97_mixer(ice)) < 0) {
2346 snd_card_free(card);
2347 return err;
2348 }
2349
2350 if ((err = snd_vt1724_build_controls(ice)) < 0) {
2351 snd_card_free(card);
2352 return err;
2353 }
2354
2355 if (ice->pcm && ice->has_spdif) { /* has SPDIF I/O */
2356 if ((err = snd_vt1724_spdif_build_controls(ice)) < 0) {
2357 snd_card_free(card);
2358 return err;
2359 }
2360 }
2361
2362 if (c->build_controls) {
2363 if ((err = c->build_controls(ice)) < 0) {
2364 snd_card_free(card);
2365 return err;
2366 }
2367 }
2368
2369 if (! c->no_mpu401) {
2370 if (ice->eeprom.data[ICE_EEP2_SYSCONF] & VT1724_CFG_MPU401) {
2371 if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_ICE1712,
Takashi Iwai302e4c22006-05-23 13:24:30 +02002372 ICEREG1724(ice, MPU_CTRL),
2373 MPU401_INFO_INTEGRATED,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 ice->irq, 0,
2375 &ice->rmidi[0])) < 0) {
2376 snd_card_free(card);
2377 return err;
2378 }
2379 }
2380 }
2381
2382 sprintf(card->longname, "%s at 0x%lx, irq %i",
2383 card->shortname, ice->port, ice->irq);
2384
2385 if ((err = snd_card_register(card)) < 0) {
2386 snd_card_free(card);
2387 return err;
2388 }
2389 pci_set_drvdata(pci, card);
2390 dev++;
2391 return 0;
2392}
2393
2394static void __devexit snd_vt1724_remove(struct pci_dev *pci)
2395{
2396 snd_card_free(pci_get_drvdata(pci));
2397 pci_set_drvdata(pci, NULL);
2398}
2399
2400static struct pci_driver driver = {
2401 .name = "ICE1724",
2402 .id_table = snd_vt1724_ids,
2403 .probe = snd_vt1724_probe,
2404 .remove = __devexit_p(snd_vt1724_remove),
2405};
2406
2407static int __init alsa_card_ice1724_init(void)
2408{
Takashi Iwai01d25d42005-04-11 16:58:24 +02002409 return pci_register_driver(&driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410}
2411
2412static void __exit alsa_card_ice1724_exit(void)
2413{
2414 pci_unregister_driver(&driver);
2415}
2416
2417module_init(alsa_card_ice1724_init)
2418module_exit(alsa_card_ice1724_exit)