blob: 1127ebdf5fec45971a41869d797229300fc2ba90 [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;
340 struct list_head *pos;
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100341 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343 what = 0;
344 snd_pcm_group_for_each(pos, substream) {
Takashi Iwai32b47da2007-01-29 15:26:36 +0100345 const struct vt1724_pcm_reg *reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 s = snd_pcm_group_substream_entry(pos);
347 reg = s->runtime->private_data;
348 what |= reg->start;
349 snd_pcm_trigger_done(s, substream);
350 }
351
352 switch (cmd) {
353 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
354 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
355 spin_lock(&ice->reg_lock);
356 old = inb(ICEMT1724(ice, DMA_PAUSE));
357 if (cmd == SNDRV_PCM_TRIGGER_PAUSE_PUSH)
358 old |= what;
359 else
360 old &= ~what;
361 outb(old, ICEMT1724(ice, DMA_PAUSE));
362 spin_unlock(&ice->reg_lock);
363 break;
364
365 case SNDRV_PCM_TRIGGER_START:
366 case SNDRV_PCM_TRIGGER_STOP:
367 spin_lock(&ice->reg_lock);
368 old = inb(ICEMT1724(ice, DMA_CONTROL));
369 if (cmd == SNDRV_PCM_TRIGGER_START)
370 old |= what;
371 else
372 old &= ~what;
373 outb(old, ICEMT1724(ice, DMA_CONTROL));
374 spin_unlock(&ice->reg_lock);
375 break;
376
377 default:
378 return -EINVAL;
379 }
380 return 0;
381}
382
383/*
384 */
385
386#define DMA_STARTS (VT1724_RDMA0_START|VT1724_PDMA0_START|VT1724_RDMA1_START|\
387 VT1724_PDMA1_START|VT1724_PDMA2_START|VT1724_PDMA3_START|VT1724_PDMA4_START)
388#define DMA_PAUSES (VT1724_RDMA0_PAUSE|VT1724_PDMA0_PAUSE|VT1724_RDMA1_PAUSE|\
389 VT1724_PDMA1_PAUSE|VT1724_PDMA2_PAUSE|VT1724_PDMA3_PAUSE|VT1724_PDMA4_PAUSE)
390
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100391static int get_max_rate(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
393 if (ice->eeprom.data[ICE_EEP2_ACLINK] & VT1724_CFG_PRO_I2S) {
394 if ((ice->eeprom.data[ICE_EEP2_I2S] & 0x08) && !ice->vt1720)
395 return 192000;
396 else
397 return 96000;
398 } else
399 return 48000;
400}
401
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100402static void snd_vt1724_set_pro_rate(struct snd_ice1712 *ice, unsigned int rate,
403 int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
405 unsigned long flags;
406 unsigned char val, old;
407 unsigned int i, mclk_change;
408
409 if (rate > get_max_rate(ice))
410 return;
411
412 switch (rate) {
413 case 8000: val = 6; break;
414 case 9600: val = 3; break;
415 case 11025: val = 10; break;
416 case 12000: val = 2; break;
417 case 16000: val = 5; break;
418 case 22050: val = 9; break;
419 case 24000: val = 1; break;
420 case 32000: val = 4; break;
421 case 44100: val = 8; break;
422 case 48000: val = 0; break;
423 case 64000: val = 15; break;
424 case 88200: val = 11; break;
425 case 96000: val = 7; break;
426 case 176400: val = 12; break;
427 case 192000: val = 14; break;
428 default:
429 snd_BUG();
430 val = 0;
431 break;
432 }
433
434 spin_lock_irqsave(&ice->reg_lock, flags);
435 if ((inb(ICEMT1724(ice, DMA_CONTROL)) & DMA_STARTS) ||
436 (inb(ICEMT1724(ice, DMA_PAUSE)) & DMA_PAUSES)) {
437 /* running? we cannot change the rate now... */
438 spin_unlock_irqrestore(&ice->reg_lock, flags);
439 return;
440 }
441 if (!force && is_pro_rate_locked(ice)) {
442 spin_unlock_irqrestore(&ice->reg_lock, flags);
443 return;
444 }
445
446 old = inb(ICEMT1724(ice, RATE));
447 if (force || old != val)
448 outb(val, ICEMT1724(ice, RATE));
449 else if (rate == ice->cur_rate) {
450 spin_unlock_irqrestore(&ice->reg_lock, flags);
451 return;
452 }
453
454 ice->cur_rate = rate;
455
456 /* check MT02 */
457 mclk_change = 0;
458 if (ice->eeprom.data[ICE_EEP2_ACLINK] & VT1724_CFG_PRO_I2S) {
459 val = old = inb(ICEMT1724(ice, I2S_FORMAT));
460 if (rate > 96000)
461 val |= VT1724_MT_I2S_MCLK_128X; /* 128x MCLK */
462 else
463 val &= ~VT1724_MT_I2S_MCLK_128X; /* 256x MCLK */
464 if (val != old) {
465 outb(val, ICEMT1724(ice, I2S_FORMAT));
466 mclk_change = 1;
467 }
468 }
469 spin_unlock_irqrestore(&ice->reg_lock, flags);
470
471 if (mclk_change && ice->gpio.i2s_mclk_changed)
472 ice->gpio.i2s_mclk_changed(ice);
473 if (ice->gpio.set_pro_rate)
474 ice->gpio.set_pro_rate(ice, rate);
475
476 /* set up codecs */
477 for (i = 0; i < ice->akm_codecs; i++) {
478 if (ice->akm[i].ops.set_rate_val)
479 ice->akm[i].ops.set_rate_val(&ice->akm[i], rate);
480 }
481 if (ice->spdif.ops.setup_rate)
482 ice->spdif.ops.setup_rate(ice, rate);
483}
484
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100485static int snd_vt1724_pcm_hw_params(struct snd_pcm_substream *substream,
486 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100488 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 int i, chs;
490
491 chs = params_channels(hw_params);
Ingo Molnar62932df2006-01-16 16:34:20 +0100492 mutex_lock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 /* mark surround channels */
494 if (substream == ice->playback_pro_substream) {
495 /* PDMA0 can be multi-channel up to 8 */
496 chs = chs / 2 - 1;
497 for (i = 0; i < chs; i++) {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100498 if (ice->pcm_reserved[i] &&
499 ice->pcm_reserved[i] != substream) {
Ingo Molnar62932df2006-01-16 16:34:20 +0100500 mutex_unlock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 return -EBUSY;
502 }
503 ice->pcm_reserved[i] = substream;
504 }
505 for (; i < 3; i++) {
506 if (ice->pcm_reserved[i] == substream)
507 ice->pcm_reserved[i] = NULL;
508 }
509 } else {
510 for (i = 0; i < 3; i++) {
511 /* check individual playback stream */
512 if (ice->playback_con_substream_ds[i] == substream) {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100513 if (ice->pcm_reserved[i] &&
514 ice->pcm_reserved[i] != substream) {
Ingo Molnar62932df2006-01-16 16:34:20 +0100515 mutex_unlock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 return -EBUSY;
517 }
518 ice->pcm_reserved[i] = substream;
519 break;
520 }
521 }
522 }
Ingo Molnar62932df2006-01-16 16:34:20 +0100523 mutex_unlock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 snd_vt1724_set_pro_rate(ice, params_rate(hw_params), 0);
525 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
526}
527
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100528static int snd_vt1724_pcm_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100530 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 int i;
532
Ingo Molnar62932df2006-01-16 16:34:20 +0100533 mutex_lock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 /* unmark surround channels */
535 for (i = 0; i < 3; i++)
536 if (ice->pcm_reserved[i] == substream)
537 ice->pcm_reserved[i] = NULL;
Ingo Molnar62932df2006-01-16 16:34:20 +0100538 mutex_unlock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 return snd_pcm_lib_free_pages(substream);
540}
541
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100542static int snd_vt1724_playback_pro_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100544 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 unsigned char val;
546 unsigned int size;
547
548 spin_lock_irq(&ice->reg_lock);
549 val = (8 - substream->runtime->channels) >> 1;
550 outb(val, ICEMT1724(ice, BURST));
551
552 outl(substream->runtime->dma_addr, ICEMT1724(ice, PLAYBACK_ADDR));
553
554 size = (snd_pcm_lib_buffer_bytes(substream) >> 2) - 1;
555 // outl(size, ICEMT1724(ice, PLAYBACK_SIZE));
556 outw(size, ICEMT1724(ice, PLAYBACK_SIZE));
557 outb(size >> 16, ICEMT1724(ice, PLAYBACK_SIZE) + 2);
558 size = (snd_pcm_lib_period_bytes(substream) >> 2) - 1;
559 // outl(size, ICEMT1724(ice, PLAYBACK_COUNT));
560 outw(size, ICEMT1724(ice, PLAYBACK_COUNT));
561 outb(size >> 16, ICEMT1724(ice, PLAYBACK_COUNT) + 2);
562
563 spin_unlock_irq(&ice->reg_lock);
564
565 // 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));
566 return 0;
567}
568
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100569static snd_pcm_uframes_t snd_vt1724_playback_pro_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100571 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 size_t ptr;
573
574 if (!(inl(ICEMT1724(ice, DMA_CONTROL)) & VT1724_PDMA0_START))
575 return 0;
576#if 0 /* read PLAYBACK_ADDR */
577 ptr = inl(ICEMT1724(ice, PLAYBACK_ADDR));
578 if (ptr < substream->runtime->dma_addr) {
579 snd_printd("ice1724: invalid negative ptr\n");
580 return 0;
581 }
582 ptr -= substream->runtime->dma_addr;
583 ptr = bytes_to_frames(substream->runtime, ptr);
584 if (ptr >= substream->runtime->buffer_size) {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100585 snd_printd("ice1724: invalid ptr %d (size=%d)\n",
586 (int)ptr, (int)substream->runtime->period_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 return 0;
588 }
589#else /* read PLAYBACK_SIZE */
590 ptr = inl(ICEMT1724(ice, PLAYBACK_SIZE)) & 0xffffff;
591 ptr = (ptr + 1) << 2;
592 ptr = bytes_to_frames(substream->runtime, ptr);
593 if (! ptr)
594 ;
595 else if (ptr <= substream->runtime->buffer_size)
596 ptr = substream->runtime->buffer_size - ptr;
597 else {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100598 snd_printd("ice1724: invalid ptr %d (size=%d)\n",
599 (int)ptr, (int)substream->runtime->buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 ptr = 0;
601 }
602#endif
603 return ptr;
604}
605
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100606static int snd_vt1724_pcm_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100608 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Takashi Iwai32b47da2007-01-29 15:26:36 +0100609 const struct vt1724_pcm_reg *reg = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 spin_lock_irq(&ice->reg_lock);
612 outl(substream->runtime->dma_addr, ice->profi_port + reg->addr);
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100613 outw((snd_pcm_lib_buffer_bytes(substream) >> 2) - 1,
614 ice->profi_port + reg->size);
615 outw((snd_pcm_lib_period_bytes(substream) >> 2) - 1,
616 ice->profi_port + reg->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 spin_unlock_irq(&ice->reg_lock);
618 return 0;
619}
620
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100621static snd_pcm_uframes_t snd_vt1724_pcm_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100623 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Takashi Iwai32b47da2007-01-29 15:26:36 +0100624 const struct vt1724_pcm_reg *reg = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 size_t ptr;
626
627 if (!(inl(ICEMT1724(ice, DMA_CONTROL)) & reg->start))
628 return 0;
629#if 0 /* use ADDR register */
630 ptr = inl(ice->profi_port + reg->addr);
631 ptr -= substream->runtime->dma_addr;
632 return bytes_to_frames(substream->runtime, ptr);
633#else /* use SIZE register */
634 ptr = inw(ice->profi_port + reg->size);
635 ptr = (ptr + 1) << 2;
636 ptr = bytes_to_frames(substream->runtime, ptr);
637 if (! ptr)
638 ;
639 else if (ptr <= substream->runtime->buffer_size)
640 ptr = substream->runtime->buffer_size - ptr;
641 else {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100642 snd_printd("ice1724: invalid ptr %d (size=%d)\n",
643 (int)ptr, (int)substream->runtime->buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 ptr = 0;
645 }
646 return ptr;
647#endif
648}
649
Takashi Iwai32b47da2007-01-29 15:26:36 +0100650static const struct vt1724_pcm_reg vt1724_playback_pro_reg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 .addr = VT1724_MT_PLAYBACK_ADDR,
652 .size = VT1724_MT_PLAYBACK_SIZE,
653 .count = VT1724_MT_PLAYBACK_COUNT,
654 .start = VT1724_PDMA0_START,
655};
656
Takashi Iwai32b47da2007-01-29 15:26:36 +0100657static const struct vt1724_pcm_reg vt1724_capture_pro_reg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 .addr = VT1724_MT_CAPTURE_ADDR,
659 .size = VT1724_MT_CAPTURE_SIZE,
660 .count = VT1724_MT_CAPTURE_COUNT,
661 .start = VT1724_RDMA0_START,
662};
663
Takashi Iwai32b47da2007-01-29 15:26:36 +0100664static const struct snd_pcm_hardware snd_vt1724_playback_pro =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
666 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
667 SNDRV_PCM_INFO_BLOCK_TRANSFER |
668 SNDRV_PCM_INFO_MMAP_VALID |
669 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_SYNC_START),
670 .formats = SNDRV_PCM_FMTBIT_S32_LE,
671 .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_192000,
672 .rate_min = 8000,
673 .rate_max = 192000,
674 .channels_min = 2,
675 .channels_max = 8,
676 .buffer_bytes_max = (1UL << 21), /* 19bits dword */
677 .period_bytes_min = 8 * 4 * 2, /* FIXME: constraints needed */
678 .period_bytes_max = (1UL << 21),
679 .periods_min = 2,
680 .periods_max = 1024,
681};
682
Takashi Iwai32b47da2007-01-29 15:26:36 +0100683static const struct snd_pcm_hardware snd_vt1724_spdif =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
685 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
686 SNDRV_PCM_INFO_BLOCK_TRANSFER |
687 SNDRV_PCM_INFO_MMAP_VALID |
688 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_SYNC_START),
689 .formats = SNDRV_PCM_FMTBIT_S32_LE,
Takashi Iwai2dfbeca2005-10-12 10:04:42 +0200690 .rates = (SNDRV_PCM_RATE_32000|SNDRV_PCM_RATE_44100|
691 SNDRV_PCM_RATE_48000|SNDRV_PCM_RATE_88200|
692 SNDRV_PCM_RATE_96000|SNDRV_PCM_RATE_176400|
693 SNDRV_PCM_RATE_192000),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 .rate_min = 32000,
Takashi Iwai2dfbeca2005-10-12 10:04:42 +0200695 .rate_max = 192000,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 .channels_min = 2,
697 .channels_max = 2,
698 .buffer_bytes_max = (1UL << 18), /* 16bits dword */
699 .period_bytes_min = 2 * 4 * 2,
700 .period_bytes_max = (1UL << 18),
701 .periods_min = 2,
702 .periods_max = 1024,
703};
704
Takashi Iwai32b47da2007-01-29 15:26:36 +0100705static const struct snd_pcm_hardware snd_vt1724_2ch_stereo =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
707 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
708 SNDRV_PCM_INFO_BLOCK_TRANSFER |
709 SNDRV_PCM_INFO_MMAP_VALID |
710 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_SYNC_START),
711 .formats = SNDRV_PCM_FMTBIT_S32_LE,
712 .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_192000,
713 .rate_min = 8000,
714 .rate_max = 192000,
715 .channels_min = 2,
716 .channels_max = 2,
717 .buffer_bytes_max = (1UL << 18), /* 16bits dword */
718 .period_bytes_min = 2 * 4 * 2,
719 .period_bytes_max = (1UL << 18),
720 .periods_min = 2,
721 .periods_max = 1024,
722};
723
724/*
725 * set rate constraints
726 */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100727static int set_rate_constraints(struct snd_ice1712 *ice,
728 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100730 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 if (ice->hw_rates) {
732 /* hardware specific */
733 runtime->hw.rate_min = ice->hw_rates->list[0];
734 runtime->hw.rate_max = ice->hw_rates->list[ice->hw_rates->count - 1];
735 runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100736 return snd_pcm_hw_constraint_list(runtime, 0,
737 SNDRV_PCM_HW_PARAM_RATE,
738 ice->hw_rates);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 }
740 if (ice->eeprom.data[ICE_EEP2_ACLINK] & VT1724_CFG_PRO_I2S) {
741 /* I2S */
742 /* VT1720 doesn't support more than 96kHz */
743 if ((ice->eeprom.data[ICE_EEP2_I2S] & 0x08) && !ice->vt1720)
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100744 return snd_pcm_hw_constraint_list(runtime, 0,
745 SNDRV_PCM_HW_PARAM_RATE,
746 &hw_constraints_rates_192);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 else {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100748 runtime->hw.rates = SNDRV_PCM_RATE_KNOT |
749 SNDRV_PCM_RATE_8000_96000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 runtime->hw.rate_max = 96000;
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100751 return snd_pcm_hw_constraint_list(runtime, 0,
752 SNDRV_PCM_HW_PARAM_RATE,
753 &hw_constraints_rates_96);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 }
755 } else if (ice->ac97) {
756 /* ACLINK */
757 runtime->hw.rate_max = 48000;
758 runtime->hw.rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000;
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100759 return snd_pcm_hw_constraint_list(runtime, 0,
760 SNDRV_PCM_HW_PARAM_RATE,
761 &hw_constraints_rates_48);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 }
763 return 0;
764}
765
766/* multi-channel playback needs alignment 8x32bit regardless of the channels
767 * actually used
768 */
769#define VT1724_BUFFER_ALIGN 0x20
770
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100771static int snd_vt1724_playback_pro_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100773 struct snd_pcm_runtime *runtime = substream->runtime;
774 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 int chs;
776
Takashi Iwai32b47da2007-01-29 15:26:36 +0100777 runtime->private_data = (void *)&vt1724_playback_pro_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 ice->playback_pro_substream = substream;
779 runtime->hw = snd_vt1724_playback_pro;
780 snd_pcm_set_sync(substream);
781 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
782 set_rate_constraints(ice, substream);
Ingo Molnar62932df2006-01-16 16:34:20 +0100783 mutex_lock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 /* calculate the currently available channels */
785 for (chs = 0; chs < 3; chs++) {
786 if (ice->pcm_reserved[chs])
787 break;
788 }
789 chs = (chs + 1) * 2;
790 runtime->hw.channels_max = chs;
791 if (chs > 2) /* channels must be even */
792 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Ingo Molnar62932df2006-01-16 16:34:20 +0100793 mutex_unlock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
795 VT1724_BUFFER_ALIGN);
796 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
797 VT1724_BUFFER_ALIGN);
798 return 0;
799}
800
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100801static int snd_vt1724_capture_pro_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100803 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
804 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Takashi Iwai32b47da2007-01-29 15:26:36 +0100806 runtime->private_data = (void *)&vt1724_capture_pro_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 ice->capture_pro_substream = substream;
808 runtime->hw = snd_vt1724_2ch_stereo;
809 snd_pcm_set_sync(substream);
810 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
811 set_rate_constraints(ice, substream);
812 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
813 VT1724_BUFFER_ALIGN);
814 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
815 VT1724_BUFFER_ALIGN);
816 return 0;
817}
818
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100819static int snd_vt1724_playback_pro_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100821 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 if (PRO_RATE_RESET)
824 snd_vt1724_set_pro_rate(ice, PRO_RATE_DEFAULT, 0);
825 ice->playback_pro_substream = NULL;
826
827 return 0;
828}
829
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100830static int snd_vt1724_capture_pro_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100832 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
834 if (PRO_RATE_RESET)
835 snd_vt1724_set_pro_rate(ice, PRO_RATE_DEFAULT, 0);
836 ice->capture_pro_substream = NULL;
837 return 0;
838}
839
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100840static struct snd_pcm_ops snd_vt1724_playback_pro_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 .open = snd_vt1724_playback_pro_open,
842 .close = snd_vt1724_playback_pro_close,
843 .ioctl = snd_pcm_lib_ioctl,
844 .hw_params = snd_vt1724_pcm_hw_params,
845 .hw_free = snd_vt1724_pcm_hw_free,
846 .prepare = snd_vt1724_playback_pro_prepare,
847 .trigger = snd_vt1724_pcm_trigger,
848 .pointer = snd_vt1724_playback_pro_pointer,
849};
850
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100851static struct snd_pcm_ops snd_vt1724_capture_pro_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 .open = snd_vt1724_capture_pro_open,
853 .close = snd_vt1724_capture_pro_close,
854 .ioctl = snd_pcm_lib_ioctl,
855 .hw_params = snd_vt1724_pcm_hw_params,
856 .hw_free = snd_vt1724_pcm_hw_free,
857 .prepare = snd_vt1724_pcm_prepare,
858 .trigger = snd_vt1724_pcm_trigger,
859 .pointer = snd_vt1724_pcm_pointer,
860};
861
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100862static int __devinit snd_vt1724_pcm_profi(struct snd_ice1712 * ice, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100864 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 int err;
866
867 err = snd_pcm_new(ice->card, "ICE1724", device, 1, 1, &pcm);
868 if (err < 0)
869 return err;
870
871 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_vt1724_playback_pro_ops);
872 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_vt1724_capture_pro_ops);
873
874 pcm->private_data = ice;
875 pcm->info_flags = 0;
876 strcpy(pcm->name, "ICE1724");
877
878 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100879 snd_dma_pci_data(ice->pci),
880 256*1024, 256*1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
882 ice->pcm_pro = pcm;
883
884 return 0;
885}
886
887
888/*
889 * SPDIF PCM
890 */
891
Takashi Iwai32b47da2007-01-29 15:26:36 +0100892static const struct vt1724_pcm_reg vt1724_playback_spdif_reg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 .addr = VT1724_MT_PDMA4_ADDR,
894 .size = VT1724_MT_PDMA4_SIZE,
895 .count = VT1724_MT_PDMA4_COUNT,
896 .start = VT1724_PDMA4_START,
897};
898
Takashi Iwai32b47da2007-01-29 15:26:36 +0100899static const struct vt1724_pcm_reg vt1724_capture_spdif_reg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 .addr = VT1724_MT_RDMA1_ADDR,
901 .size = VT1724_MT_RDMA1_SIZE,
902 .count = VT1724_MT_RDMA1_COUNT,
903 .start = VT1724_RDMA1_START,
904};
905
906/* update spdif control bits; call with reg_lock */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100907static void update_spdif_bits(struct snd_ice1712 *ice, unsigned int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
909 unsigned char cbit, disabled;
910
911 cbit = inb(ICEREG1724(ice, SPDIF_CFG));
912 disabled = cbit & ~VT1724_CFG_SPDIF_OUT_EN;
913 if (cbit != disabled)
914 outb(disabled, ICEREG1724(ice, SPDIF_CFG));
915 outw(val, ICEMT1724(ice, SPDIF_CTRL));
916 if (cbit != disabled)
917 outb(cbit, ICEREG1724(ice, SPDIF_CFG));
918 outw(val, ICEMT1724(ice, SPDIF_CTRL));
919}
920
921/* update SPDIF control bits according to the given rate */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100922static void update_spdif_rate(struct snd_ice1712 *ice, unsigned int rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
924 unsigned int val, nval;
925 unsigned long flags;
926
927 spin_lock_irqsave(&ice->reg_lock, flags);
928 nval = val = inw(ICEMT1724(ice, SPDIF_CTRL));
929 nval &= ~(7 << 12);
930 switch (rate) {
931 case 44100: break;
932 case 48000: nval |= 2 << 12; break;
933 case 32000: nval |= 3 << 12; break;
Takashi Iwai2dfbeca2005-10-12 10:04:42 +0200934 case 88200: nval |= 4 << 12; break;
935 case 96000: nval |= 5 << 12; break;
936 case 192000: nval |= 6 << 12; break;
937 case 176400: nval |= 7 << 12; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 }
939 if (val != nval)
940 update_spdif_bits(ice, nval);
941 spin_unlock_irqrestore(&ice->reg_lock, flags);
942}
943
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100944static int snd_vt1724_playback_spdif_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100946 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 if (! ice->force_pdma4)
948 update_spdif_rate(ice, substream->runtime->rate);
949 return snd_vt1724_pcm_prepare(substream);
950}
951
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100952static int snd_vt1724_playback_spdif_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100954 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
955 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
Takashi Iwai32b47da2007-01-29 15:26:36 +0100957 runtime->private_data = (void *)&vt1724_playback_spdif_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 ice->playback_con_substream = substream;
959 if (ice->force_pdma4) {
960 runtime->hw = snd_vt1724_2ch_stereo;
961 set_rate_constraints(ice, substream);
962 } else
963 runtime->hw = snd_vt1724_spdif;
964 snd_pcm_set_sync(substream);
965 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
966 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
967 VT1724_BUFFER_ALIGN);
968 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
969 VT1724_BUFFER_ALIGN);
970 return 0;
971}
972
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100973static int snd_vt1724_playback_spdif_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100975 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
977 if (PRO_RATE_RESET)
978 snd_vt1724_set_pro_rate(ice, PRO_RATE_DEFAULT, 0);
979 ice->playback_con_substream = NULL;
980
981 return 0;
982}
983
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100984static int snd_vt1724_capture_spdif_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100986 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
987 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Takashi Iwai32b47da2007-01-29 15:26:36 +0100989 runtime->private_data = (void *)&vt1724_capture_spdif_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 ice->capture_con_substream = substream;
991 if (ice->force_rdma1) {
992 runtime->hw = snd_vt1724_2ch_stereo;
993 set_rate_constraints(ice, substream);
994 } else
995 runtime->hw = snd_vt1724_spdif;
996 snd_pcm_set_sync(substream);
997 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
998 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
999 VT1724_BUFFER_ALIGN);
1000 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1001 VT1724_BUFFER_ALIGN);
1002 return 0;
1003}
1004
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001005static int snd_vt1724_capture_spdif_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001007 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
1009 if (PRO_RATE_RESET)
1010 snd_vt1724_set_pro_rate(ice, PRO_RATE_DEFAULT, 0);
1011 ice->capture_con_substream = NULL;
1012
1013 return 0;
1014}
1015
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001016static struct snd_pcm_ops snd_vt1724_playback_spdif_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 .open = snd_vt1724_playback_spdif_open,
1018 .close = snd_vt1724_playback_spdif_close,
1019 .ioctl = snd_pcm_lib_ioctl,
1020 .hw_params = snd_vt1724_pcm_hw_params,
1021 .hw_free = snd_vt1724_pcm_hw_free,
1022 .prepare = snd_vt1724_playback_spdif_prepare,
1023 .trigger = snd_vt1724_pcm_trigger,
1024 .pointer = snd_vt1724_pcm_pointer,
1025};
1026
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001027static struct snd_pcm_ops snd_vt1724_capture_spdif_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 .open = snd_vt1724_capture_spdif_open,
1029 .close = snd_vt1724_capture_spdif_close,
1030 .ioctl = snd_pcm_lib_ioctl,
1031 .hw_params = snd_vt1724_pcm_hw_params,
1032 .hw_free = snd_vt1724_pcm_hw_free,
1033 .prepare = snd_vt1724_pcm_prepare,
1034 .trigger = snd_vt1724_pcm_trigger,
1035 .pointer = snd_vt1724_pcm_pointer,
1036};
1037
1038
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001039static int __devinit snd_vt1724_pcm_spdif(struct snd_ice1712 * ice, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
1041 char *name;
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001042 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 int play, capt;
1044 int err;
1045
1046 if (ice->force_pdma4 ||
1047 (ice->eeprom.data[ICE_EEP2_SPDIF] & VT1724_CFG_SPDIF_OUT_INT)) {
1048 play = 1;
1049 ice->has_spdif = 1;
1050 } else
1051 play = 0;
1052 if (ice->force_rdma1 ||
1053 (ice->eeprom.data[ICE_EEP2_SPDIF] & VT1724_CFG_SPDIF_IN)) {
1054 capt = 1;
1055 ice->has_spdif = 1;
1056 } else
1057 capt = 0;
1058 if (! play && ! capt)
1059 return 0; /* no spdif device */
1060
1061 if (ice->force_pdma4 || ice->force_rdma1)
1062 name = "ICE1724 Secondary";
1063 else
1064 name = "IEC1724 IEC958";
1065 err = snd_pcm_new(ice->card, name, device, play, capt, &pcm);
1066 if (err < 0)
1067 return err;
1068
1069 if (play)
1070 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1071 &snd_vt1724_playback_spdif_ops);
1072 if (capt)
1073 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1074 &snd_vt1724_capture_spdif_ops);
1075
1076 pcm->private_data = ice;
1077 pcm->info_flags = 0;
1078 strcpy(pcm->name, name);
1079
1080 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001081 snd_dma_pci_data(ice->pci),
1082 64*1024, 64*1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
1084 ice->pcm = pcm;
1085
1086 return 0;
1087}
1088
1089
1090/*
1091 * independent surround PCMs
1092 */
1093
Takashi Iwai32b47da2007-01-29 15:26:36 +01001094static const struct vt1724_pcm_reg vt1724_playback_dma_regs[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 {
1096 .addr = VT1724_MT_PDMA1_ADDR,
1097 .size = VT1724_MT_PDMA1_SIZE,
1098 .count = VT1724_MT_PDMA1_COUNT,
1099 .start = VT1724_PDMA1_START,
1100 },
1101 {
1102 .addr = VT1724_MT_PDMA2_ADDR,
1103 .size = VT1724_MT_PDMA2_SIZE,
1104 .count = VT1724_MT_PDMA2_COUNT,
1105 .start = VT1724_PDMA2_START,
1106 },
1107 {
1108 .addr = VT1724_MT_PDMA3_ADDR,
1109 .size = VT1724_MT_PDMA3_SIZE,
1110 .count = VT1724_MT_PDMA3_COUNT,
1111 .start = VT1724_PDMA3_START,
1112 },
1113};
1114
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001115static int snd_vt1724_playback_indep_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001117 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 unsigned char val;
1119
1120 spin_lock_irq(&ice->reg_lock);
1121 val = 3 - substream->number;
1122 if (inb(ICEMT1724(ice, BURST)) < val)
1123 outb(val, ICEMT1724(ice, BURST));
1124 spin_unlock_irq(&ice->reg_lock);
1125 return snd_vt1724_pcm_prepare(substream);
1126}
1127
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001128static int snd_vt1724_playback_indep_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001130 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
1131 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Ingo Molnar62932df2006-01-16 16:34:20 +01001133 mutex_lock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 /* already used by PDMA0? */
1135 if (ice->pcm_reserved[substream->number]) {
Ingo Molnar62932df2006-01-16 16:34:20 +01001136 mutex_unlock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 return -EBUSY; /* FIXME: should handle blocking mode properly */
1138 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001139 mutex_unlock(&ice->open_mutex);
Takashi Iwai32b47da2007-01-29 15:26:36 +01001140 runtime->private_data = (void *)&vt1724_playback_dma_regs[substream->number];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 ice->playback_con_substream_ds[substream->number] = substream;
1142 runtime->hw = snd_vt1724_2ch_stereo;
1143 snd_pcm_set_sync(substream);
1144 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
1145 set_rate_constraints(ice, substream);
1146 return 0;
1147}
1148
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001149static int snd_vt1724_playback_indep_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001151 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
1153 if (PRO_RATE_RESET)
1154 snd_vt1724_set_pro_rate(ice, PRO_RATE_DEFAULT, 0);
1155 ice->playback_con_substream_ds[substream->number] = NULL;
1156 ice->pcm_reserved[substream->number] = NULL;
1157
1158 return 0;
1159}
1160
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001161static struct snd_pcm_ops snd_vt1724_playback_indep_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 .open = snd_vt1724_playback_indep_open,
1163 .close = snd_vt1724_playback_indep_close,
1164 .ioctl = snd_pcm_lib_ioctl,
1165 .hw_params = snd_vt1724_pcm_hw_params,
1166 .hw_free = snd_vt1724_pcm_hw_free,
1167 .prepare = snd_vt1724_playback_indep_prepare,
1168 .trigger = snd_vt1724_pcm_trigger,
1169 .pointer = snd_vt1724_pcm_pointer,
1170};
1171
1172
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001173static int __devinit snd_vt1724_pcm_indep(struct snd_ice1712 * ice, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001175 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 int play;
1177 int err;
1178
1179 play = ice->num_total_dacs / 2 - 1;
1180 if (play <= 0)
1181 return 0;
1182
1183 err = snd_pcm_new(ice->card, "ICE1724 Surrounds", device, play, 0, &pcm);
1184 if (err < 0)
1185 return err;
1186
1187 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1188 &snd_vt1724_playback_indep_ops);
1189
1190 pcm->private_data = ice;
1191 pcm->info_flags = 0;
1192 strcpy(pcm->name, "ICE1724 Surround PCM");
1193
1194 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001195 snd_dma_pci_data(ice->pci),
1196 64*1024, 64*1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
1198 ice->pcm_ds = pcm;
1199
1200 return 0;
1201}
1202
1203
1204/*
1205 * Mixer section
1206 */
1207
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001208static int __devinit snd_vt1724_ac97_mixer(struct snd_ice1712 * ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209{
1210 int err;
1211
1212 if (! (ice->eeprom.data[ICE_EEP2_ACLINK] & VT1724_CFG_PRO_I2S)) {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001213 struct snd_ac97_bus *pbus;
1214 struct snd_ac97_template ac97;
1215 static struct snd_ac97_bus_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 .write = snd_vt1724_ac97_write,
1217 .read = snd_vt1724_ac97_read,
1218 };
1219
1220 /* cold reset */
1221 outb(inb(ICEMT1724(ice, AC97_CMD)) | 0x80, ICEMT1724(ice, AC97_CMD));
1222 mdelay(5); /* FIXME */
1223 outb(inb(ICEMT1724(ice, AC97_CMD)) & ~0x80, ICEMT1724(ice, AC97_CMD));
1224
1225 if ((err = snd_ac97_bus(ice->card, 0, &ops, NULL, &pbus)) < 0)
1226 return err;
1227 memset(&ac97, 0, sizeof(ac97));
1228 ac97.private_data = ice;
1229 if ((err = snd_ac97_mixer(pbus, &ac97, &ice->ac97)) < 0)
1230 printk(KERN_WARNING "ice1712: cannot initialize pro ac97, skipped\n");
1231 else
1232 return 0;
1233 }
1234 /* I2S mixer only */
1235 strcat(ice->card->mixername, "ICE1724 - multitrack");
1236 return 0;
1237}
1238
1239/*
1240 *
1241 */
1242
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001243static inline unsigned int eeprom_triple(struct snd_ice1712 *ice, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244{
1245 return (unsigned int)ice->eeprom.data[idx] | \
1246 ((unsigned int)ice->eeprom.data[idx + 1] << 8) | \
1247 ((unsigned int)ice->eeprom.data[idx + 2] << 16);
1248}
1249
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001250static void snd_vt1724_proc_read(struct snd_info_entry *entry,
1251 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001253 struct snd_ice1712 *ice = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 unsigned int idx;
1255
1256 snd_iprintf(buffer, "%s\n\n", ice->card->longname);
1257 snd_iprintf(buffer, "EEPROM:\n");
1258
1259 snd_iprintf(buffer, " Subvendor : 0x%x\n", ice->eeprom.subvendor);
1260 snd_iprintf(buffer, " Size : %i bytes\n", ice->eeprom.size);
1261 snd_iprintf(buffer, " Version : %i\n", ice->eeprom.version);
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001262 snd_iprintf(buffer, " System Config : 0x%x\n",
1263 ice->eeprom.data[ICE_EEP2_SYSCONF]);
1264 snd_iprintf(buffer, " ACLink : 0x%x\n",
1265 ice->eeprom.data[ICE_EEP2_ACLINK]);
1266 snd_iprintf(buffer, " I2S : 0x%x\n",
1267 ice->eeprom.data[ICE_EEP2_I2S]);
1268 snd_iprintf(buffer, " S/PDIF : 0x%x\n",
1269 ice->eeprom.data[ICE_EEP2_SPDIF]);
1270 snd_iprintf(buffer, " GPIO direction : 0x%x\n",
1271 ice->eeprom.gpiodir);
1272 snd_iprintf(buffer, " GPIO mask : 0x%x\n",
1273 ice->eeprom.gpiomask);
1274 snd_iprintf(buffer, " GPIO state : 0x%x\n",
1275 ice->eeprom.gpiostate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 for (idx = 0x12; idx < ice->eeprom.size; idx++)
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001277 snd_iprintf(buffer, " Extra #%02i : 0x%x\n",
1278 idx, ice->eeprom.data[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
1280 snd_iprintf(buffer, "\nRegisters:\n");
1281
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001282 snd_iprintf(buffer, " PSDOUT03 : 0x%08x\n",
1283 (unsigned)inl(ICEMT1724(ice, ROUTE_PLAYBACK)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 for (idx = 0x0; idx < 0x20 ; idx++)
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001285 snd_iprintf(buffer, " CCS%02x : 0x%02x\n",
1286 idx, inb(ice->port+idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 for (idx = 0x0; idx < 0x30 ; idx++)
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001288 snd_iprintf(buffer, " MT%02x : 0x%02x\n",
1289 idx, inb(ice->profi_port+idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290}
1291
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001292static void __devinit snd_vt1724_proc_init(struct snd_ice1712 * ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001294 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296 if (! snd_card_proc_new(ice->card, "ice1724", &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02001297 snd_info_set_text_ops(entry, ice, snd_vt1724_proc_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298}
1299
1300/*
1301 *
1302 */
1303
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001304static int snd_vt1724_eeprom_info(struct snd_kcontrol *kcontrol,
1305 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306{
1307 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001308 uinfo->count = sizeof(struct snd_ice1712_eeprom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 return 0;
1310}
1311
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001312static int snd_vt1724_eeprom_get(struct snd_kcontrol *kcontrol,
1313 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001315 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
1317 memcpy(ucontrol->value.bytes.data, &ice->eeprom, sizeof(ice->eeprom));
1318 return 0;
1319}
1320
Takashi Iwai32b47da2007-01-29 15:26:36 +01001321static const struct snd_kcontrol_new snd_vt1724_eeprom __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1323 .name = "ICE1724 EEPROM",
1324 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1325 .info = snd_vt1724_eeprom_info,
1326 .get = snd_vt1724_eeprom_get
1327};
1328
1329/*
1330 */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001331static int snd_vt1724_spdif_info(struct snd_kcontrol *kcontrol,
1332 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333{
1334 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1335 uinfo->count = 1;
1336 return 0;
1337}
1338
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001339static unsigned int encode_spdif_bits(struct snd_aes_iec958 *diga)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340{
Takashi Iwai2dfbeca2005-10-12 10:04:42 +02001341 unsigned int val, rbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
1343 val = diga->status[0] & 0x03; /* professional, non-audio */
1344 if (val & 0x01) {
1345 /* professional */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001346 if ((diga->status[0] & IEC958_AES0_PRO_EMPHASIS) ==
1347 IEC958_AES0_PRO_EMPHASIS_5015)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 val |= 1U << 3;
Takashi Iwai2dfbeca2005-10-12 10:04:42 +02001349 rbits = (diga->status[4] >> 3) & 0x0f;
1350 if (rbits) {
1351 switch (rbits) {
1352 case 2: val |= 5 << 12; break; /* 96k */
1353 case 3: val |= 6 << 12; break; /* 192k */
1354 case 10: val |= 4 << 12; break; /* 88.2k */
1355 case 11: val |= 7 << 12; break; /* 176.4k */
1356 }
1357 } else {
1358 switch (diga->status[0] & IEC958_AES0_PRO_FS) {
1359 case IEC958_AES0_PRO_FS_44100:
1360 break;
1361 case IEC958_AES0_PRO_FS_32000:
1362 val |= 3U << 12;
1363 break;
1364 default:
1365 val |= 2U << 12;
1366 break;
1367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 }
1369 } else {
1370 /* consumer */
1371 val |= diga->status[1] & 0x04; /* copyright */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001372 if ((diga->status[0] & IEC958_AES0_CON_EMPHASIS) ==
1373 IEC958_AES0_CON_EMPHASIS_5015)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 val |= 1U << 3;
1375 val |= (unsigned int)(diga->status[1] & 0x3f) << 4; /* category */
1376 val |= (unsigned int)(diga->status[3] & IEC958_AES3_CON_FS) << 12; /* fs */
1377 }
1378 return val;
1379}
1380
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001381static void decode_spdif_bits(struct snd_aes_iec958 *diga, unsigned int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382{
1383 memset(diga->status, 0, sizeof(diga->status));
1384 diga->status[0] = val & 0x03; /* professional, non-audio */
1385 if (val & 0x01) {
1386 /* professional */
1387 if (val & (1U << 3))
1388 diga->status[0] |= IEC958_AES0_PRO_EMPHASIS_5015;
1389 switch ((val >> 12) & 0x7) {
1390 case 0:
1391 break;
1392 case 2:
1393 diga->status[0] |= IEC958_AES0_PRO_FS_32000;
1394 break;
1395 default:
1396 diga->status[0] |= IEC958_AES0_PRO_FS_48000;
1397 break;
1398 }
1399 } else {
1400 /* consumer */
1401 diga->status[0] |= val & (1U << 2); /* copyright */
1402 if (val & (1U << 3))
1403 diga->status[0] |= IEC958_AES0_CON_EMPHASIS_5015;
1404 diga->status[1] |= (val >> 4) & 0x3f; /* category */
1405 diga->status[3] |= (val >> 12) & 0x07; /* fs */
1406 }
1407}
1408
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001409static int snd_vt1724_spdif_default_get(struct snd_kcontrol *kcontrol,
1410 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001412 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 unsigned int val;
1414 val = inw(ICEMT1724(ice, SPDIF_CTRL));
1415 decode_spdif_bits(&ucontrol->value.iec958, val);
1416 return 0;
1417}
1418
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001419static int snd_vt1724_spdif_default_put(struct snd_kcontrol *kcontrol,
1420 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001422 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 unsigned int val, old;
1424
1425 val = encode_spdif_bits(&ucontrol->value.iec958);
1426 spin_lock_irq(&ice->reg_lock);
1427 old = inw(ICEMT1724(ice, SPDIF_CTRL));
1428 if (val != old)
1429 update_spdif_bits(ice, val);
1430 spin_unlock_irq(&ice->reg_lock);
1431 return (val != old);
1432}
1433
Takashi Iwai32b47da2007-01-29 15:26:36 +01001434static const struct snd_kcontrol_new snd_vt1724_spdif_default __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435{
1436 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1437 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1438 .info = snd_vt1724_spdif_info,
1439 .get = snd_vt1724_spdif_default_get,
1440 .put = snd_vt1724_spdif_default_put
1441};
1442
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001443static int snd_vt1724_spdif_maskc_get(struct snd_kcontrol *kcontrol,
1444 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
1446 ucontrol->value.iec958.status[0] = IEC958_AES0_NONAUDIO |
1447 IEC958_AES0_PROFESSIONAL |
1448 IEC958_AES0_CON_NOT_COPYRIGHT |
1449 IEC958_AES0_CON_EMPHASIS;
1450 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_ORIGINAL |
1451 IEC958_AES1_CON_CATEGORY;
1452 ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS;
1453 return 0;
1454}
1455
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001456static int snd_vt1724_spdif_maskp_get(struct snd_kcontrol *kcontrol,
1457 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458{
1459 ucontrol->value.iec958.status[0] = IEC958_AES0_NONAUDIO |
1460 IEC958_AES0_PROFESSIONAL |
1461 IEC958_AES0_PRO_FS |
1462 IEC958_AES0_PRO_EMPHASIS;
1463 return 0;
1464}
1465
Takashi Iwai32b47da2007-01-29 15:26:36 +01001466static const struct snd_kcontrol_new snd_vt1724_spdif_maskc __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467{
1468 .access = SNDRV_CTL_ELEM_ACCESS_READ,
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001469 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1471 .info = snd_vt1724_spdif_info,
1472 .get = snd_vt1724_spdif_maskc_get,
1473};
1474
Takashi Iwai32b47da2007-01-29 15:26:36 +01001475static const struct snd_kcontrol_new snd_vt1724_spdif_maskp __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476{
1477 .access = SNDRV_CTL_ELEM_ACCESS_READ,
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001478 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
1480 .info = snd_vt1724_spdif_info,
1481 .get = snd_vt1724_spdif_maskp_get,
1482};
1483
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001484static int snd_vt1724_spdif_sw_info(struct snd_kcontrol *kcontrol,
1485 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486{
1487 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1488 uinfo->count = 1;
1489 uinfo->value.integer.min = 0;
1490 uinfo->value.integer.max = 1;
1491 return 0;
1492}
1493
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001494static int snd_vt1724_spdif_sw_get(struct snd_kcontrol *kcontrol,
1495 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001497 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
1498 ucontrol->value.integer.value[0] = inb(ICEREG1724(ice, SPDIF_CFG)) &
1499 VT1724_CFG_SPDIF_OUT_EN ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 return 0;
1501}
1502
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001503static int snd_vt1724_spdif_sw_put(struct snd_kcontrol *kcontrol,
1504 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001506 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 unsigned char old, val;
1508
1509 spin_lock_irq(&ice->reg_lock);
1510 old = val = inb(ICEREG1724(ice, SPDIF_CFG));
1511 val &= ~VT1724_CFG_SPDIF_OUT_EN;
1512 if (ucontrol->value.integer.value[0])
1513 val |= VT1724_CFG_SPDIF_OUT_EN;
1514 if (old != val)
1515 outb(val, ICEREG1724(ice, SPDIF_CFG));
1516 spin_unlock_irq(&ice->reg_lock);
1517 return old != val;
1518}
1519
Takashi Iwai32b47da2007-01-29 15:26:36 +01001520static const struct snd_kcontrol_new snd_vt1724_spdif_switch __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521{
1522 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1523 /* FIXME: the following conflict with IEC958 Playback Route */
1524 // .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
Clemens Ladisch10e8d782005-08-03 13:40:08 +02001525 .name = SNDRV_CTL_NAME_IEC958("Output ",NONE,SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 .info = snd_vt1724_spdif_sw_info,
1527 .get = snd_vt1724_spdif_sw_get,
1528 .put = snd_vt1724_spdif_sw_put
1529};
1530
1531
1532#if 0 /* NOT USED YET */
1533/*
1534 * GPIO access from extern
1535 */
1536
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001537int snd_vt1724_gpio_info(struct snd_kcontrol *kcontrol,
1538 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539{
1540 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1541 uinfo->count = 1;
1542 uinfo->value.integer.min = 0;
1543 uinfo->value.integer.max = 1;
1544 return 0;
1545}
1546
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001547int snd_vt1724_gpio_get(struct snd_kcontrol *kcontrol,
1548 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001550 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 int shift = kcontrol->private_value & 0xff;
1552 int invert = (kcontrol->private_value & (1<<24)) ? 1 : 0;
1553
1554 snd_ice1712_save_gpio_status(ice);
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001555 ucontrol->value.integer.value[0] =
1556 (snd_ice1712_gpio_read(ice) & (1 << shift) ? 1 : 0) ^ invert;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 snd_ice1712_restore_gpio_status(ice);
1558 return 0;
1559}
1560
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001561int snd_ice1712_gpio_put(struct snd_kcontrol *kcontrol,
1562 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001564 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 int shift = kcontrol->private_value & 0xff;
1566 int invert = (kcontrol->private_value & (1<<24)) ? mask : 0;
1567 unsigned int val, nval;
1568
1569 if (kcontrol->private_value & (1 << 31))
1570 return -EPERM;
1571 nval = (ucontrol->value.integer.value[0] ? (1 << shift) : 0) ^ invert;
1572 snd_ice1712_save_gpio_status(ice);
1573 val = snd_ice1712_gpio_read(ice);
1574 nval |= val & ~(1 << shift);
1575 if (val != nval)
1576 snd_ice1712_gpio_write(ice, nval);
1577 snd_ice1712_restore_gpio_status(ice);
1578 return val != nval;
1579}
1580#endif /* NOT USED YET */
1581
1582/*
1583 * rate
1584 */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001585static int snd_vt1724_pro_internal_clock_info(struct snd_kcontrol *kcontrol,
1586 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587{
Takashi Iwai32b47da2007-01-29 15:26:36 +01001588 static const char * const texts_1724[] = {
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 "176400", /* 13: 12 */
1603 "192000", /* 14: 14 */
1604 "IEC958 Input", /* 15: -- */
1605 };
Takashi Iwai32b47da2007-01-29 15:26:36 +01001606 static const char * const texts_1720[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 "8000", /* 0: 6 */
1608 "9600", /* 1: 3 */
1609 "11025", /* 2: 10 */
1610 "12000", /* 3: 2 */
1611 "16000", /* 4: 5 */
1612 "22050", /* 5: 9 */
1613 "24000", /* 6: 1 */
1614 "32000", /* 7: 4 */
1615 "44100", /* 8: 8 */
1616 "48000", /* 9: 0 */
1617 "64000", /* 10: 15 */
1618 "88200", /* 11: 11 */
1619 "96000", /* 12: 7 */
1620 "IEC958 Input", /* 13: -- */
1621 };
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001622 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
1624 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1625 uinfo->count = 1;
1626 uinfo->value.enumerated.items = ice->vt1720 ? 14 : 16;
1627 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1628 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
1629 strcpy(uinfo->value.enumerated.name,
1630 ice->vt1720 ? texts_1720[uinfo->value.enumerated.item] :
1631 texts_1724[uinfo->value.enumerated.item]);
1632 return 0;
1633}
1634
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001635static int snd_vt1724_pro_internal_clock_get(struct snd_kcontrol *kcontrol,
1636 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001638 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Takashi Iwai32b47da2007-01-29 15:26:36 +01001639 static const unsigned char xlate[16] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 9, 6, 3, 1, 7, 4, 0, 12, 8, 5, 2, 11, 13, 255, 14, 10
1641 };
1642 unsigned char val;
1643
1644 spin_lock_irq(&ice->reg_lock);
1645 if (is_spdif_master(ice)) {
1646 ucontrol->value.enumerated.item[0] = ice->vt1720 ? 13 : 15;
1647 } else {
1648 val = xlate[inb(ICEMT1724(ice, RATE)) & 15];
1649 if (val == 255) {
1650 snd_BUG();
1651 val = 0;
1652 }
1653 ucontrol->value.enumerated.item[0] = val;
1654 }
1655 spin_unlock_irq(&ice->reg_lock);
1656 return 0;
1657}
1658
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001659static int snd_vt1724_pro_internal_clock_put(struct snd_kcontrol *kcontrol,
1660 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001662 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 unsigned char oval;
1664 int rate;
1665 int change = 0;
1666 int spdif = ice->vt1720 ? 13 : 15;
1667
1668 spin_lock_irq(&ice->reg_lock);
1669 oval = inb(ICEMT1724(ice, RATE));
1670 if (ucontrol->value.enumerated.item[0] == spdif) {
1671 outb(oval | VT1724_SPDIF_MASTER, ICEMT1724(ice, RATE));
1672 } else {
1673 rate = rates[ucontrol->value.integer.value[0] % 15];
1674 if (rate <= get_max_rate(ice)) {
1675 PRO_RATE_DEFAULT = rate;
1676 spin_unlock_irq(&ice->reg_lock);
1677 snd_vt1724_set_pro_rate(ice, PRO_RATE_DEFAULT, 1);
1678 spin_lock_irq(&ice->reg_lock);
1679 }
1680 }
1681 change = inb(ICEMT1724(ice, RATE)) != oval;
1682 spin_unlock_irq(&ice->reg_lock);
1683
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001684 if ((oval & VT1724_SPDIF_MASTER) !=
1685 (inb(ICEMT1724(ice, RATE)) & VT1724_SPDIF_MASTER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 /* notify akm chips as well */
1687 if (is_spdif_master(ice)) {
1688 unsigned int i;
1689 for (i = 0; i < ice->akm_codecs; i++) {
1690 if (ice->akm[i].ops.set_rate_val)
1691 ice->akm[i].ops.set_rate_val(&ice->akm[i], 0);
1692 }
1693 }
1694 }
1695 return change;
1696}
1697
Takashi Iwai32b47da2007-01-29 15:26:36 +01001698static const struct snd_kcontrol_new snd_vt1724_pro_internal_clock __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1700 .name = "Multi Track Internal Clock",
1701 .info = snd_vt1724_pro_internal_clock_info,
1702 .get = snd_vt1724_pro_internal_clock_get,
1703 .put = snd_vt1724_pro_internal_clock_put
1704};
1705
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001706static int snd_vt1724_pro_rate_locking_info(struct snd_kcontrol *kcontrol,
1707 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708{
1709 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1710 uinfo->count = 1;
1711 uinfo->value.integer.min = 0;
1712 uinfo->value.integer.max = 1;
1713 return 0;
1714}
1715
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001716static int snd_vt1724_pro_rate_locking_get(struct snd_kcontrol *kcontrol,
1717 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718{
1719 ucontrol->value.integer.value[0] = PRO_RATE_LOCKED;
1720 return 0;
1721}
1722
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001723static int snd_vt1724_pro_rate_locking_put(struct snd_kcontrol *kcontrol,
1724 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001726 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 int change = 0, nval;
1728
1729 nval = ucontrol->value.integer.value[0] ? 1 : 0;
1730 spin_lock_irq(&ice->reg_lock);
1731 change = PRO_RATE_LOCKED != nval;
1732 PRO_RATE_LOCKED = nval;
1733 spin_unlock_irq(&ice->reg_lock);
1734 return change;
1735}
1736
Takashi Iwai32b47da2007-01-29 15:26:36 +01001737static const struct snd_kcontrol_new snd_vt1724_pro_rate_locking __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1739 .name = "Multi Track Rate Locking",
1740 .info = snd_vt1724_pro_rate_locking_info,
1741 .get = snd_vt1724_pro_rate_locking_get,
1742 .put = snd_vt1724_pro_rate_locking_put
1743};
1744
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001745static int snd_vt1724_pro_rate_reset_info(struct snd_kcontrol *kcontrol,
1746 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747{
1748 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1749 uinfo->count = 1;
1750 uinfo->value.integer.min = 0;
1751 uinfo->value.integer.max = 1;
1752 return 0;
1753}
1754
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001755static int snd_vt1724_pro_rate_reset_get(struct snd_kcontrol *kcontrol,
1756 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757{
1758 ucontrol->value.integer.value[0] = PRO_RATE_RESET ? 1 : 0;
1759 return 0;
1760}
1761
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001762static int snd_vt1724_pro_rate_reset_put(struct snd_kcontrol *kcontrol,
1763 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001765 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 int change = 0, nval;
1767
1768 nval = ucontrol->value.integer.value[0] ? 1 : 0;
1769 spin_lock_irq(&ice->reg_lock);
1770 change = PRO_RATE_RESET != nval;
1771 PRO_RATE_RESET = nval;
1772 spin_unlock_irq(&ice->reg_lock);
1773 return change;
1774}
1775
Takashi Iwai32b47da2007-01-29 15:26:36 +01001776static const struct snd_kcontrol_new snd_vt1724_pro_rate_reset __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1778 .name = "Multi Track Rate Reset",
1779 .info = snd_vt1724_pro_rate_reset_info,
1780 .get = snd_vt1724_pro_rate_reset_get,
1781 .put = snd_vt1724_pro_rate_reset_put
1782};
1783
1784
1785/*
1786 * routing
1787 */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001788static int snd_vt1724_pro_route_info(struct snd_kcontrol *kcontrol,
1789 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790{
1791 static char *texts[] = {
1792 "PCM Out", /* 0 */
1793 "H/W In 0", "H/W In 1", /* 1-2 */
1794 "IEC958 In L", "IEC958 In R", /* 3-4 */
1795 };
1796
1797 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1798 uinfo->count = 1;
1799 uinfo->value.enumerated.items = 5;
1800 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1801 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
1802 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1803 return 0;
1804}
1805
1806static inline int analog_route_shift(int idx)
1807{
1808 return (idx % 2) * 12 + ((idx / 2) * 3) + 8;
1809}
1810
1811static inline int digital_route_shift(int idx)
1812{
1813 return idx * 3;
1814}
1815
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001816static int get_route_val(struct snd_ice1712 *ice, int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817{
1818 unsigned long val;
1819 unsigned char eitem;
Takashi Iwai32b47da2007-01-29 15:26:36 +01001820 static const unsigned char xlate[8] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 0, 255, 1, 2, 255, 255, 3, 4,
1822 };
1823
1824 val = inl(ICEMT1724(ice, ROUTE_PLAYBACK));
1825 val >>= shift;
1826 val &= 7; //we now have 3 bits per output
1827 eitem = xlate[val];
1828 if (eitem == 255) {
1829 snd_BUG();
1830 return 0;
1831 }
1832 return eitem;
1833}
1834
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001835static int put_route_val(struct snd_ice1712 *ice, unsigned int val, int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836{
1837 unsigned int old_val, nval;
1838 int change;
Takashi Iwai32b47da2007-01-29 15:26:36 +01001839 static const unsigned char xroute[8] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 0, /* PCM */
1841 2, /* PSDIN0 Left */
1842 3, /* PSDIN0 Right */
1843 6, /* SPDIN Left */
1844 7, /* SPDIN Right */
1845 };
1846
1847 nval = xroute[val % 5];
1848 val = old_val = inl(ICEMT1724(ice, ROUTE_PLAYBACK));
1849 val &= ~(0x07 << shift);
1850 val |= nval << shift;
1851 change = val != old_val;
1852 if (change)
1853 outl(val, ICEMT1724(ice, ROUTE_PLAYBACK));
1854 return change;
1855}
1856
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001857static int snd_vt1724_pro_route_analog_get(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);
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001862 ucontrol->value.enumerated.item[0] =
1863 get_route_val(ice, analog_route_shift(idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 return 0;
1865}
1866
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001867static int snd_vt1724_pro_route_analog_put(struct snd_kcontrol *kcontrol,
1868 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001870 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1872 return put_route_val(ice, ucontrol->value.enumerated.item[0],
1873 analog_route_shift(idx));
1874}
1875
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001876static int snd_vt1724_pro_route_spdif_get(struct snd_kcontrol *kcontrol,
1877 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001879 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001881 ucontrol->value.enumerated.item[0] =
1882 get_route_val(ice, digital_route_shift(idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 return 0;
1884}
1885
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001886static int snd_vt1724_pro_route_spdif_put(struct snd_kcontrol *kcontrol,
1887 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001889 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1891 return put_route_val(ice, ucontrol->value.enumerated.item[0],
1892 digital_route_shift(idx));
1893}
1894
Takashi Iwai32b47da2007-01-29 15:26:36 +01001895static const struct snd_kcontrol_new snd_vt1724_mixer_pro_analog_route __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1897 .name = "H/W Playback Route",
1898 .info = snd_vt1724_pro_route_info,
1899 .get = snd_vt1724_pro_route_analog_get,
1900 .put = snd_vt1724_pro_route_analog_put,
1901};
1902
Takashi Iwai32b47da2007-01-29 15:26:36 +01001903static const struct snd_kcontrol_new snd_vt1724_mixer_pro_spdif_route __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1905 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Route",
1906 .info = snd_vt1724_pro_route_info,
1907 .get = snd_vt1724_pro_route_spdif_get,
1908 .put = snd_vt1724_pro_route_spdif_put,
1909 .count = 2,
1910};
1911
1912
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001913static int snd_vt1724_pro_peak_info(struct snd_kcontrol *kcontrol,
1914 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915{
1916 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1917 uinfo->count = 22; /* FIXME: for compatibility with ice1712... */
1918 uinfo->value.integer.min = 0;
1919 uinfo->value.integer.max = 255;
1920 return 0;
1921}
1922
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001923static int snd_vt1724_pro_peak_get(struct snd_kcontrol *kcontrol,
1924 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001926 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 int idx;
1928
1929 spin_lock_irq(&ice->reg_lock);
1930 for (idx = 0; idx < 22; idx++) {
1931 outb(idx, ICEMT1724(ice, MONITOR_PEAKINDEX));
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001932 ucontrol->value.integer.value[idx] =
1933 inb(ICEMT1724(ice, MONITOR_PEAKDATA));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 }
1935 spin_unlock_irq(&ice->reg_lock);
1936 return 0;
1937}
1938
Takashi Iwai32b47da2007-01-29 15:26:36 +01001939static const struct snd_kcontrol_new snd_vt1724_mixer_pro_peak __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1941 .name = "Multi Track Peak",
1942 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
1943 .info = snd_vt1724_pro_peak_info,
1944 .get = snd_vt1724_pro_peak_get
1945};
1946
1947/*
1948 *
1949 */
1950
Takashi Iwai32b47da2007-01-29 15:26:36 +01001951static const struct snd_ice1712_card_info no_matched __devinitdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
Takashi Iwai32b47da2007-01-29 15:26:36 +01001953static const struct snd_ice1712_card_info *card_tables[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 snd_vt1724_revo_cards,
1955 snd_vt1724_amp_cards,
1956 snd_vt1724_aureon_cards,
1957 snd_vt1720_mobo_cards,
1958 snd_vt1720_pontis_cards,
1959 snd_vt1724_prodigy192_cards,
1960 snd_vt1724_juli_cards,
1961 snd_vt1724_phase_cards,
Clement Guedezf6cdab52007-01-08 10:48:41 +01001962 snd_vt1724_wtm_cards,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 NULL,
1964};
1965
1966
1967/*
1968 */
1969
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001970static void wait_i2c_busy(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971{
1972 int t = 0x10000;
1973 while ((inb(ICEREG1724(ice, I2C_CTRL)) & VT1724_I2C_BUSY) && t--)
1974 ;
1975 if (t == -1)
1976 printk(KERN_ERR "ice1724: i2c busy timeout\n");
1977}
1978
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001979unsigned char snd_vt1724_read_i2c(struct snd_ice1712 *ice,
1980 unsigned char dev, unsigned char addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981{
1982 unsigned char val;
1983
Ingo Molnar62932df2006-01-16 16:34:20 +01001984 mutex_lock(&ice->i2c_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 outb(addr, ICEREG1724(ice, I2C_BYTE_ADDR));
1986 outb(dev & ~VT1724_I2C_WRITE, ICEREG1724(ice, I2C_DEV_ADDR));
1987 wait_i2c_busy(ice);
1988 val = inb(ICEREG1724(ice, I2C_DATA));
Ingo Molnar62932df2006-01-16 16:34:20 +01001989 mutex_unlock(&ice->i2c_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 //printk("i2c_read: [0x%x,0x%x] = 0x%x\n", dev, addr, val);
1991 return val;
1992}
1993
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001994void snd_vt1724_write_i2c(struct snd_ice1712 *ice,
1995 unsigned char dev, unsigned char addr, unsigned char data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996{
Ingo Molnar62932df2006-01-16 16:34:20 +01001997 mutex_lock(&ice->i2c_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 wait_i2c_busy(ice);
1999 //printk("i2c_write: [0x%x,0x%x] = 0x%x\n", dev, addr, data);
2000 outb(addr, ICEREG1724(ice, I2C_BYTE_ADDR));
2001 outb(data, ICEREG1724(ice, I2C_DATA));
2002 outb(dev | VT1724_I2C_WRITE, ICEREG1724(ice, I2C_DEV_ADDR));
2003 wait_i2c_busy(ice);
Ingo Molnar62932df2006-01-16 16:34:20 +01002004 mutex_unlock(&ice->i2c_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005}
2006
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002007static int __devinit snd_vt1724_read_eeprom(struct snd_ice1712 *ice,
2008 const char *modelname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009{
2010 const int dev = 0xa0; /* EEPROM device address */
2011 unsigned int i, size;
Takashi Iwai32b47da2007-01-29 15:26:36 +01002012 const struct snd_ice1712_card_info **tbl, *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
2014 if (! modelname || ! *modelname) {
2015 ice->eeprom.subvendor = 0;
2016 if ((inb(ICEREG1724(ice, I2C_CTRL)) & VT1724_I2C_EEPROM) != 0)
2017 ice->eeprom.subvendor =
2018 (snd_vt1724_read_i2c(ice, dev, 0x00) << 0) |
2019 (snd_vt1724_read_i2c(ice, dev, 0x01) << 8) |
2020 (snd_vt1724_read_i2c(ice, dev, 0x02) << 16) |
2021 (snd_vt1724_read_i2c(ice, dev, 0x03) << 24);
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002022 if (ice->eeprom.subvendor == 0 ||
2023 ice->eeprom.subvendor == (unsigned int)-1) {
2024 /* invalid subvendor from EEPROM, try the PCI
2025 * subststem ID instead
2026 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 u16 vendor, device;
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002028 pci_read_config_word(ice->pci, PCI_SUBSYSTEM_VENDOR_ID,
2029 &vendor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 pci_read_config_word(ice->pci, PCI_SUBSYSTEM_ID, &device);
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002031 ice->eeprom.subvendor =
2032 ((unsigned int)swab16(vendor) << 16) | swab16(device);
2033 if (ice->eeprom.subvendor == 0 ||
2034 ice->eeprom.subvendor == (unsigned int)-1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 printk(KERN_ERR "ice1724: No valid ID is found\n");
2036 return -ENXIO;
2037 }
2038 }
2039 }
2040 for (tbl = card_tables; *tbl; tbl++) {
2041 for (c = *tbl; c->subvendor; c++) {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002042 if (modelname && c->model &&
2043 ! strcmp(modelname, c->model)) {
2044 printk(KERN_INFO "ice1724: Using board model %s\n",
2045 c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 ice->eeprom.subvendor = c->subvendor;
2047 } else if (c->subvendor != ice->eeprom.subvendor)
2048 continue;
2049 if (! c->eeprom_size || ! c->eeprom_data)
2050 goto found;
2051 /* if the EEPROM is given by the driver, use it */
2052 snd_printdd("using the defined eeprom..\n");
2053 ice->eeprom.version = 2;
2054 ice->eeprom.size = c->eeprom_size + 6;
2055 memcpy(ice->eeprom.data, c->eeprom_data, c->eeprom_size);
2056 goto read_skipped;
2057 }
2058 }
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002059 printk(KERN_WARNING "ice1724: No matching model found for ID 0x%x\n",
2060 ice->eeprom.subvendor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
2062 found:
2063 ice->eeprom.size = snd_vt1724_read_i2c(ice, dev, 0x04);
2064 if (ice->eeprom.size < 6)
2065 ice->eeprom.size = 32;
2066 else if (ice->eeprom.size > 32) {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002067 printk(KERN_ERR "ice1724: Invalid EEPROM (size = %i)\n",
2068 ice->eeprom.size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 return -EIO;
2070 }
2071 ice->eeprom.version = snd_vt1724_read_i2c(ice, dev, 0x05);
2072 if (ice->eeprom.version != 2)
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002073 printk(KERN_WARNING "ice1724: Invalid EEPROM version %i\n",
2074 ice->eeprom.version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 size = ice->eeprom.size - 6;
2076 for (i = 0; i < size; i++)
2077 ice->eeprom.data[i] = snd_vt1724_read_i2c(ice, dev, i + 6);
2078
2079 read_skipped:
2080 ice->eeprom.gpiomask = eeprom_triple(ice, ICE_EEP2_GPIO_MASK);
2081 ice->eeprom.gpiostate = eeprom_triple(ice, ICE_EEP2_GPIO_STATE);
2082 ice->eeprom.gpiodir = eeprom_triple(ice, ICE_EEP2_GPIO_DIR);
2083
2084 return 0;
2085}
2086
2087
2088
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002089static int __devinit snd_vt1724_chip_init(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090{
2091 outb(VT1724_RESET , ICEREG1724(ice, CONTROL));
2092 udelay(200);
2093 outb(0, ICEREG1724(ice, CONTROL));
2094 udelay(200);
2095 outb(ice->eeprom.data[ICE_EEP2_SYSCONF], ICEREG1724(ice, SYS_CFG));
2096 outb(ice->eeprom.data[ICE_EEP2_ACLINK], ICEREG1724(ice, AC97_CFG));
2097 outb(ice->eeprom.data[ICE_EEP2_I2S], ICEREG1724(ice, I2S_FEATURES));
2098 outb(ice->eeprom.data[ICE_EEP2_SPDIF], ICEREG1724(ice, SPDIF_CFG));
2099
2100 ice->gpio.write_mask = ice->eeprom.gpiomask;
2101 ice->gpio.direction = ice->eeprom.gpiodir;
2102 snd_vt1724_set_gpio_mask(ice, ice->eeprom.gpiomask);
2103 snd_vt1724_set_gpio_dir(ice, ice->eeprom.gpiodir);
2104 snd_vt1724_set_gpio_data(ice, ice->eeprom.gpiostate);
2105
2106 outb(0, ICEREG1724(ice, POWERDOWN));
2107
2108 return 0;
2109}
2110
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002111static int __devinit snd_vt1724_spdif_build_controls(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112{
2113 int err;
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002114 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
2116 snd_assert(ice->pcm != NULL, return -EIO);
2117
2118 err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_mixer_pro_spdif_route, ice));
2119 if (err < 0)
2120 return err;
2121
2122 err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_spdif_switch, ice));
2123 if (err < 0)
2124 return err;
2125
2126 err = snd_ctl_add(ice->card, kctl = snd_ctl_new1(&snd_vt1724_spdif_default, ice));
2127 if (err < 0)
2128 return err;
2129 kctl->id.device = ice->pcm->device;
2130 err = snd_ctl_add(ice->card, kctl = snd_ctl_new1(&snd_vt1724_spdif_maskc, ice));
2131 if (err < 0)
2132 return err;
2133 kctl->id.device = ice->pcm->device;
2134 err = snd_ctl_add(ice->card, kctl = snd_ctl_new1(&snd_vt1724_spdif_maskp, ice));
2135 if (err < 0)
2136 return err;
2137 kctl->id.device = ice->pcm->device;
2138#if 0 /* use default only */
2139 err = snd_ctl_add(ice->card, kctl = snd_ctl_new1(&snd_vt1724_spdif_stream, ice));
2140 if (err < 0)
2141 return err;
2142 kctl->id.device = ice->pcm->device;
2143 ice->spdif.stream_ctl = kctl;
2144#endif
2145 return 0;
2146}
2147
2148
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002149static int __devinit snd_vt1724_build_controls(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150{
2151 int err;
2152
2153 err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_eeprom, ice));
2154 if (err < 0)
2155 return err;
2156 err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_pro_internal_clock, ice));
2157 if (err < 0)
2158 return err;
2159
2160 err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_pro_rate_locking, ice));
2161 if (err < 0)
2162 return err;
2163 err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_pro_rate_reset, ice));
2164 if (err < 0)
2165 return err;
2166
2167 if (ice->num_total_dacs > 0) {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002168 struct snd_kcontrol_new tmp = snd_vt1724_mixer_pro_analog_route;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 tmp.count = ice->num_total_dacs;
2170 if (ice->vt1720 && tmp.count > 2)
2171 tmp.count = 2;
2172 err = snd_ctl_add(ice->card, snd_ctl_new1(&tmp, ice));
2173 if (err < 0)
2174 return err;
2175 }
2176
2177 err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_mixer_pro_peak, ice));
2178 if (err < 0)
2179 return err;
2180
2181 return 0;
2182}
2183
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002184static int snd_vt1724_free(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185{
2186 if (! ice->port)
2187 goto __hw_end;
2188 /* mask all interrupts */
2189 outb(0xff, ICEMT1724(ice, DMA_INT_MASK));
2190 outb(0xff, ICEREG1724(ice, IRQMASK));
2191 /* --- */
2192 __hw_end:
2193 if (ice->irq >= 0) {
2194 synchronize_irq(ice->irq);
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002195 free_irq(ice->irq, ice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 }
2197 pci_release_regions(ice->pci);
2198 snd_ice1712_akm4xxx_free(ice);
2199 pci_disable_device(ice->pci);
2200 kfree(ice);
2201 return 0;
2202}
2203
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002204static int snd_vt1724_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002206 struct snd_ice1712 *ice = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 return snd_vt1724_free(ice);
2208}
2209
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002210static int __devinit snd_vt1724_create(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 struct pci_dev *pci,
2212 const char *modelname,
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002213 struct snd_ice1712 ** r_ice1712)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002215 struct snd_ice1712 *ice;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 int err;
2217 unsigned char mask;
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002218 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 .dev_free = snd_vt1724_dev_free,
2220 };
2221
2222 *r_ice1712 = NULL;
2223
2224 /* enable PCI device */
2225 if ((err = pci_enable_device(pci)) < 0)
2226 return err;
2227
Takashi Iwaie560d8d2005-09-09 14:21:46 +02002228 ice = kzalloc(sizeof(*ice), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 if (ice == NULL) {
2230 pci_disable_device(pci);
2231 return -ENOMEM;
2232 }
2233 ice->vt1724 = 1;
2234 spin_lock_init(&ice->reg_lock);
Ingo Molnar62932df2006-01-16 16:34:20 +01002235 mutex_init(&ice->gpio_mutex);
2236 mutex_init(&ice->open_mutex);
2237 mutex_init(&ice->i2c_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 ice->gpio.set_mask = snd_vt1724_set_gpio_mask;
2239 ice->gpio.set_dir = snd_vt1724_set_gpio_dir;
2240 ice->gpio.set_data = snd_vt1724_set_gpio_data;
2241 ice->gpio.get_data = snd_vt1724_get_gpio_data;
2242 ice->card = card;
2243 ice->pci = pci;
2244 ice->irq = -1;
2245 pci_set_master(pci);
2246 snd_vt1724_proc_init(ice);
2247 synchronize_irq(pci->irq);
2248
2249 if ((err = pci_request_regions(pci, "ICE1724")) < 0) {
2250 kfree(ice);
2251 pci_disable_device(pci);
2252 return err;
2253 }
2254 ice->port = pci_resource_start(pci, 0);
2255 ice->profi_port = pci_resource_start(pci, 1);
2256
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002257 if (request_irq(pci->irq, snd_vt1724_interrupt,
Takashi Iwai437a5a42006-11-21 12:14:23 +01002258 IRQF_SHARED, "ICE1724", ice)) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02002259 snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 snd_vt1724_free(ice);
2261 return -EIO;
2262 }
2263
2264 ice->irq = pci->irq;
2265
2266 if (snd_vt1724_read_eeprom(ice, modelname) < 0) {
2267 snd_vt1724_free(ice);
2268 return -EIO;
2269 }
2270 if (snd_vt1724_chip_init(ice) < 0) {
2271 snd_vt1724_free(ice);
2272 return -EIO;
2273 }
2274
2275 /* unmask used interrupts */
2276 if (! (ice->eeprom.data[ICE_EEP2_SYSCONF] & VT1724_CFG_MPU401))
2277 mask = VT1724_IRQ_MPU_RX | VT1724_IRQ_MPU_TX;
2278 else
2279 mask = 0;
2280 outb(mask, ICEREG1724(ice, IRQMASK));
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002281 /* don't handle FIFO overrun/underruns (just yet),
2282 * since they cause machine lockups
2283 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 outb(VT1724_MULTI_FIFO_ERR, ICEMT1724(ice, DMA_INT_MASK));
2285
2286 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, ice, &ops)) < 0) {
2287 snd_vt1724_free(ice);
2288 return err;
2289 }
2290
2291 snd_card_set_dev(card, &pci->dev);
2292
2293 *r_ice1712 = ice;
2294 return 0;
2295}
2296
2297
2298/*
2299 *
2300 * Registration
2301 *
2302 */
2303
2304static int __devinit snd_vt1724_probe(struct pci_dev *pci,
2305 const struct pci_device_id *pci_id)
2306{
2307 static int dev;
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002308 struct snd_card *card;
2309 struct snd_ice1712 *ice;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 int pcm_dev = 0, err;
Takashi Iwai32b47da2007-01-29 15:26:36 +01002311 const struct snd_ice1712_card_info **tbl, *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
2313 if (dev >= SNDRV_CARDS)
2314 return -ENODEV;
2315 if (!enable[dev]) {
2316 dev++;
2317 return -ENOENT;
2318 }
2319
2320 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
2321 if (card == NULL)
2322 return -ENOMEM;
2323
2324 strcpy(card->driver, "ICE1724");
2325 strcpy(card->shortname, "ICEnsemble ICE1724");
2326
2327 if ((err = snd_vt1724_create(card, pci, model[dev], &ice)) < 0) {
2328 snd_card_free(card);
2329 return err;
2330 }
2331
2332 for (tbl = card_tables; *tbl; tbl++) {
2333 for (c = *tbl; c->subvendor; c++) {
2334 if (c->subvendor == ice->eeprom.subvendor) {
2335 strcpy(card->shortname, c->name);
2336 if (c->driver) /* specific driver? */
2337 strcpy(card->driver, c->driver);
2338 if (c->chip_init) {
2339 if ((err = c->chip_init(ice)) < 0) {
2340 snd_card_free(card);
2341 return err;
2342 }
2343 }
2344 goto __found;
2345 }
2346 }
2347 }
2348 c = &no_matched;
2349 __found:
2350
2351 if ((err = snd_vt1724_pcm_profi(ice, pcm_dev++)) < 0) {
2352 snd_card_free(card);
2353 return err;
2354 }
2355
2356 if ((err = snd_vt1724_pcm_spdif(ice, pcm_dev++)) < 0) {
2357 snd_card_free(card);
2358 return err;
2359 }
2360
2361 if ((err = snd_vt1724_pcm_indep(ice, pcm_dev++)) < 0) {
2362 snd_card_free(card);
2363 return err;
2364 }
2365
2366 if ((err = snd_vt1724_ac97_mixer(ice)) < 0) {
2367 snd_card_free(card);
2368 return err;
2369 }
2370
2371 if ((err = snd_vt1724_build_controls(ice)) < 0) {
2372 snd_card_free(card);
2373 return err;
2374 }
2375
2376 if (ice->pcm && ice->has_spdif) { /* has SPDIF I/O */
2377 if ((err = snd_vt1724_spdif_build_controls(ice)) < 0) {
2378 snd_card_free(card);
2379 return err;
2380 }
2381 }
2382
2383 if (c->build_controls) {
2384 if ((err = c->build_controls(ice)) < 0) {
2385 snd_card_free(card);
2386 return err;
2387 }
2388 }
2389
2390 if (! c->no_mpu401) {
2391 if (ice->eeprom.data[ICE_EEP2_SYSCONF] & VT1724_CFG_MPU401) {
2392 if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_ICE1712,
Takashi Iwai302e4c22006-05-23 13:24:30 +02002393 ICEREG1724(ice, MPU_CTRL),
2394 MPU401_INFO_INTEGRATED,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 ice->irq, 0,
2396 &ice->rmidi[0])) < 0) {
2397 snd_card_free(card);
2398 return err;
2399 }
2400 }
2401 }
2402
2403 sprintf(card->longname, "%s at 0x%lx, irq %i",
2404 card->shortname, ice->port, ice->irq);
2405
2406 if ((err = snd_card_register(card)) < 0) {
2407 snd_card_free(card);
2408 return err;
2409 }
2410 pci_set_drvdata(pci, card);
2411 dev++;
2412 return 0;
2413}
2414
2415static void __devexit snd_vt1724_remove(struct pci_dev *pci)
2416{
2417 snd_card_free(pci_get_drvdata(pci));
2418 pci_set_drvdata(pci, NULL);
2419}
2420
2421static struct pci_driver driver = {
2422 .name = "ICE1724",
2423 .id_table = snd_vt1724_ids,
2424 .probe = snd_vt1724_probe,
2425 .remove = __devexit_p(snd_vt1724_remove),
2426};
2427
2428static int __init alsa_card_ice1724_init(void)
2429{
Takashi Iwai01d25d42005-04-11 16:58:24 +02002430 return pci_register_driver(&driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431}
2432
2433static void __exit alsa_card_ice1724_exit(void)
2434{
2435 pci_unregister_driver(&driver);
2436}
2437
2438module_init(alsa_card_ice1724_init)
2439module_exit(alsa_card_ice1724_exit)