blob: 4490422fb930bc047af5df27cad62266e3a55689 [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 *
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02005 * Copyright (c) 2000 Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/io.h>
26#include <linux/delay.h>
27#include <linux/interrupt.h>
28#include <linux/init.h>
29#include <linux/pci.h>
30#include <linux/slab.h>
31#include <linux/moduleparam.h>
Ingo Molnar62932df2006-01-16 16:34:20 +010032#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <sound/core.h>
34#include <sound/info.h>
35#include <sound/mpu401.h>
36#include <sound/initval.h>
37
38#include <sound/asoundef.h>
39
40#include "ice1712.h"
41#include "envy24ht.h"
42
43/* lowlevel routines */
44#include "amp.h"
45#include "revo.h"
46#include "aureon.h"
47#include "vt1720_mobo.h"
48#include "pontis.h"
49#include "prodigy192.h"
Julian Scheel6b8d6e52008-01-16 19:50:00 +010050#include "prodigy_hifi.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include "juli.h"
52#include "phase.h"
Clement Guedezf6cdab52007-01-08 10:48:41 +010053#include "wtm.h"
Shin-ya Okadaf31639b2007-10-23 15:08:18 +020054#include "se.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Jaroslav Kyselac1017a42007-10-15 09:50:19 +020056MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070057MODULE_DESCRIPTION("VIA ICEnsemble ICE1724/1720 (Envy24HT/PT)");
58MODULE_LICENSE("GPL");
59MODULE_SUPPORTED_DEVICE("{"
60 REVO_DEVICE_DESC
61 AMP_AUDIO2000_DEVICE_DESC
62 AUREON_DEVICE_DESC
63 VT1720_MOBO_DEVICE_DESC
64 PONTIS_DEVICE_DESC
65 PRODIGY192_DEVICE_DESC
Julian Scheel6b8d6e52008-01-16 19:50:00 +010066 PRODIGY_HIFI_DEVICE_DESC
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 JULI_DEVICE_DESC
68 PHASE_DEVICE_DESC
Clement Guedezf6cdab52007-01-08 10:48:41 +010069 WTM_DEVICE_DESC
Shin-ya Okadaf31639b2007-10-23 15:08:18 +020070 SE_DEVICE_DESC
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 "{VIA,VT1720},"
72 "{VIA,VT1724},"
73 "{ICEnsemble,Generic ICE1724},"
74 "{ICEnsemble,Generic Envy24HT}"
75 "{ICEnsemble,Generic Envy24PT}}");
76
77static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
78static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
79static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
80static char *model[SNDRV_CARDS];
81
82module_param_array(index, int, NULL, 0444);
83MODULE_PARM_DESC(index, "Index value for ICE1724 soundcard.");
84module_param_array(id, charp, NULL, 0444);
85MODULE_PARM_DESC(id, "ID string for ICE1724 soundcard.");
86module_param_array(enable, bool, NULL, 0444);
87MODULE_PARM_DESC(enable, "Enable ICE1724 soundcard.");
88module_param_array(model, charp, NULL, 0444);
89MODULE_PARM_DESC(model, "Use the given board model.");
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92/* Both VT1720 and VT1724 have the same PCI IDs */
Takashi Iwai32b47da2007-01-29 15:26:36 +010093static const struct pci_device_id snd_vt1724_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 { PCI_VENDOR_ID_ICE, PCI_DEVICE_ID_VT1724, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
95 { 0, }
96};
97
98MODULE_DEVICE_TABLE(pci, snd_vt1724_ids);
99
100
101static int PRO_RATE_LOCKED;
102static int PRO_RATE_RESET = 1;
103static unsigned int PRO_RATE_DEFAULT = 44100;
104
105/*
106 * Basic I/O
107 */
108
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100109/*
110 * default rates, default clock routines
111 */
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113/* check whether the clock mode is spdif-in */
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100114static inline int stdclock_is_spdif_master(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
116 return (inb(ICEMT1724(ice, RATE)) & VT1724_SPDIF_MASTER) ? 1 : 0;
117}
118
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100119static inline int is_pro_rate_locked(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100121 return ice->is_spdif_master(ice) || PRO_RATE_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
124/*
125 * ac97 section
126 */
127
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100128static unsigned char snd_vt1724_ac97_ready(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
130 unsigned char old_cmd;
131 int tm;
132 for (tm = 0; tm < 0x10000; tm++) {
133 old_cmd = inb(ICEMT1724(ice, AC97_CMD));
134 if (old_cmd & (VT1724_AC97_WRITE | VT1724_AC97_READ))
135 continue;
136 if (!(old_cmd & VT1724_AC97_READY))
137 continue;
138 return old_cmd;
139 }
140 snd_printd(KERN_ERR "snd_vt1724_ac97_ready: timeout\n");
141 return old_cmd;
142}
143
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100144static int snd_vt1724_ac97_wait_bit(struct snd_ice1712 *ice, unsigned char bit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
146 int tm;
147 for (tm = 0; tm < 0x10000; tm++)
148 if ((inb(ICEMT1724(ice, AC97_CMD)) & bit) == 0)
149 return 0;
150 snd_printd(KERN_ERR "snd_vt1724_ac97_wait_bit: timeout\n");
151 return -EIO;
152}
153
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100154static void snd_vt1724_ac97_write(struct snd_ac97 *ac97,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 unsigned short reg,
156 unsigned short val)
157{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100158 struct snd_ice1712 *ice = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 unsigned char old_cmd;
160
161 old_cmd = snd_vt1724_ac97_ready(ice);
162 old_cmd &= ~VT1724_AC97_ID_MASK;
163 old_cmd |= ac97->num;
164 outb(reg, ICEMT1724(ice, AC97_INDEX));
165 outw(val, ICEMT1724(ice, AC97_DATA));
166 outb(old_cmd | VT1724_AC97_WRITE, ICEMT1724(ice, AC97_CMD));
167 snd_vt1724_ac97_wait_bit(ice, VT1724_AC97_WRITE);
168}
169
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100170static unsigned short snd_vt1724_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100172 struct snd_ice1712 *ice = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 unsigned char old_cmd;
174
175 old_cmd = snd_vt1724_ac97_ready(ice);
176 old_cmd &= ~VT1724_AC97_ID_MASK;
177 old_cmd |= ac97->num;
178 outb(reg, ICEMT1724(ice, AC97_INDEX));
179 outb(old_cmd | VT1724_AC97_READ, ICEMT1724(ice, AC97_CMD));
180 if (snd_vt1724_ac97_wait_bit(ice, VT1724_AC97_READ) < 0)
181 return ~0;
182 return inw(ICEMT1724(ice, AC97_DATA));
183}
184
185
186/*
187 * GPIO operations
188 */
189
190/* set gpio direction 0 = read, 1 = write */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100191static void snd_vt1724_set_gpio_dir(struct snd_ice1712 *ice, unsigned int data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
193 outl(data, ICEREG1724(ice, GPIO_DIRECTION));
194 inw(ICEREG1724(ice, GPIO_DIRECTION)); /* dummy read for pci-posting */
195}
196
197/* set the gpio mask (0 = writable) */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100198static void snd_vt1724_set_gpio_mask(struct snd_ice1712 *ice, unsigned int data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
200 outw(data, ICEREG1724(ice, GPIO_WRITE_MASK));
201 if (! ice->vt1720) /* VT1720 supports only 16 GPIO bits */
202 outb((data >> 16) & 0xff, ICEREG1724(ice, GPIO_WRITE_MASK_22));
203 inw(ICEREG1724(ice, GPIO_WRITE_MASK)); /* dummy read for pci-posting */
204}
205
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100206static void snd_vt1724_set_gpio_data(struct snd_ice1712 *ice, unsigned int data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
208 outw(data, ICEREG1724(ice, GPIO_DATA));
209 if (! ice->vt1720)
210 outb(data >> 16, ICEREG1724(ice, GPIO_DATA_22));
211 inw(ICEREG1724(ice, GPIO_DATA)); /* dummy read for pci-posting */
212}
213
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100214static unsigned int snd_vt1724_get_gpio_data(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
216 unsigned int data;
217 if (! ice->vt1720)
218 data = (unsigned int)inb(ICEREG1724(ice, GPIO_DATA_22));
219 else
220 data = 0;
221 data = (data << 16) | inw(ICEREG1724(ice, GPIO_DATA));
222 return data;
223}
224
225/*
Takashi Iwai3a841d52008-04-23 17:47:28 +0200226 * MPU401 accessor
227 */
228static unsigned char snd_vt1724_mpu401_read(struct snd_mpu401 *mpu,
229 unsigned long addr)
230{
231 /* fix status bits to the standard position */
232 /* only RX_EMPTY and TX_FULL are checked */
233 if (addr == MPU401C(mpu))
234 return (inb(addr) & 0x0c) << 4;
235 else
236 return inb(addr);
237}
238
239static void snd_vt1724_mpu401_write(struct snd_mpu401 *mpu,
240 unsigned char data, unsigned long addr)
241{
242 if (addr == MPU401C(mpu)) {
243 if (data == MPU401_ENTER_UART)
244 outb(0x01, addr);
245 /* what else? */
246 } else
247 outb(data, addr);
248}
249
250
251/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 * Interrupt handler
253 */
254
David Howells7d12e782006-10-05 14:55:46 +0100255static irqreturn_t snd_vt1724_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100257 struct snd_ice1712 *ice = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 unsigned char status;
Takashi Iwai3a841d52008-04-23 17:47:28 +0200259 unsigned char status_mask =
260 VT1724_IRQ_MPU_RX | VT1724_IRQ_MPU_TX | VT1724_IRQ_MTPCM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 int handled = 0;
Takashi Iwai3a841d52008-04-23 17:47:28 +0200262#ifdef CONFIG_SND_DEBUG
263 int timeout = 0;
264#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266 while (1) {
267 status = inb(ICEREG1724(ice, IRQSTAT));
Takashi Iwai3a841d52008-04-23 17:47:28 +0200268 status &= status_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 if (status == 0)
270 break;
Takashi Iwai3a841d52008-04-23 17:47:28 +0200271#ifdef CONFIG_SND_DEBUG
272 if (++timeout > 10) {
273 printk(KERN_ERR
274 "ice1724: Too long irq loop, status = 0x%x\n",
275 status);
276 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 }
Takashi Iwai3a841d52008-04-23 17:47:28 +0200278#endif
279 handled = 1;
280 if (status & VT1724_IRQ_MPU_TX) {
281 if (ice->rmidi[0])
282 snd_mpu401_uart_interrupt_tx(irq,
283 ice->rmidi[0]->private_data);
284 else /* disable TX to be sure */
285 outb(inb(ICEREG1724(ice, IRQMASK)) |
286 VT1724_IRQ_MPU_TX,
287 ICEREG1724(ice, IRQMASK));
288 /* Due to mysterical reasons, MPU_TX is always
289 * generated (and can't be cleared) when a PCM
290 * playback is going. So let's ignore at the
291 * next loop.
292 */
293 status_mask &= ~VT1724_IRQ_MPU_TX;
294 }
295 if (status & VT1724_IRQ_MPU_RX) {
296 if (ice->rmidi[0])
297 snd_mpu401_uart_interrupt(irq,
298 ice->rmidi[0]->private_data);
299 else /* disable RX to be sure */
300 outb(inb(ICEREG1724(ice, IRQMASK)) |
301 VT1724_IRQ_MPU_RX,
302 ICEREG1724(ice, IRQMASK));
303 }
304 /* ack MPU irq */
305 outb(status, ICEREG1724(ice, IRQSTAT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 if (status & VT1724_IRQ_MTPCM) {
307 /*
308 * Multi-track PCM
309 * PCM assignment are:
310 * Playback DMA0 (M/C) = playback_pro_substream
311 * Playback DMA1 = playback_con_substream_ds[0]
312 * Playback DMA2 = playback_con_substream_ds[1]
313 * Playback DMA3 = playback_con_substream_ds[2]
314 * Playback DMA4 (SPDIF) = playback_con_substream
315 * Record DMA0 = capture_pro_substream
316 * Record DMA1 = capture_con_substream
317 */
318 unsigned char mtstat = inb(ICEMT1724(ice, IRQ));
319 if (mtstat & VT1724_MULTI_PDMA0) {
320 if (ice->playback_pro_substream)
321 snd_pcm_period_elapsed(ice->playback_pro_substream);
322 }
323 if (mtstat & VT1724_MULTI_RDMA0) {
324 if (ice->capture_pro_substream)
325 snd_pcm_period_elapsed(ice->capture_pro_substream);
326 }
327 if (mtstat & VT1724_MULTI_PDMA1) {
328 if (ice->playback_con_substream_ds[0])
329 snd_pcm_period_elapsed(ice->playback_con_substream_ds[0]);
330 }
331 if (mtstat & VT1724_MULTI_PDMA2) {
332 if (ice->playback_con_substream_ds[1])
333 snd_pcm_period_elapsed(ice->playback_con_substream_ds[1]);
334 }
335 if (mtstat & VT1724_MULTI_PDMA3) {
336 if (ice->playback_con_substream_ds[2])
337 snd_pcm_period_elapsed(ice->playback_con_substream_ds[2]);
338 }
339 if (mtstat & VT1724_MULTI_PDMA4) {
340 if (ice->playback_con_substream)
341 snd_pcm_period_elapsed(ice->playback_con_substream);
342 }
343 if (mtstat & VT1724_MULTI_RDMA1) {
344 if (ice->capture_con_substream)
345 snd_pcm_period_elapsed(ice->capture_con_substream);
346 }
347 /* ack anyway to avoid freeze */
348 outb(mtstat, ICEMT1724(ice, IRQ));
349 /* ought to really handle this properly */
350 if (mtstat & VT1724_MULTI_FIFO_ERR) {
351 unsigned char fstat = inb(ICEMT1724(ice, DMA_FIFO_ERR));
352 outb(fstat, ICEMT1724(ice, DMA_FIFO_ERR));
353 outb(VT1724_MULTI_FIFO_ERR | inb(ICEMT1724(ice, DMA_INT_MASK)), ICEMT1724(ice, DMA_INT_MASK));
354 /* If I don't do this, I get machine lockup due to continual interrupts */
355 }
356
357 }
358 }
359 return IRQ_RETVAL(handled);
360}
361
362/*
363 * PCM code - professional part (multitrack)
364 */
365
366static unsigned int rates[] = {
367 8000, 9600, 11025, 12000, 16000, 22050, 24000,
368 32000, 44100, 48000, 64000, 88200, 96000,
369 176400, 192000,
370};
371
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100372static struct snd_pcm_hw_constraint_list hw_constraints_rates_96 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 .count = ARRAY_SIZE(rates) - 2, /* up to 96000 */
374 .list = rates,
375 .mask = 0,
376};
377
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100378static struct snd_pcm_hw_constraint_list hw_constraints_rates_48 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 .count = ARRAY_SIZE(rates) - 5, /* up to 48000 */
380 .list = rates,
381 .mask = 0,
382};
383
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100384static struct snd_pcm_hw_constraint_list hw_constraints_rates_192 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 .count = ARRAY_SIZE(rates),
386 .list = rates,
387 .mask = 0,
388};
389
390struct vt1724_pcm_reg {
391 unsigned int addr; /* ADDR register offset */
392 unsigned int size; /* SIZE register offset */
393 unsigned int count; /* COUNT register offset */
394 unsigned int start; /* start & pause bit */
395};
396
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100397static int snd_vt1724_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100399 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 unsigned char what;
401 unsigned char old;
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100402 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 what = 0;
Takashi Iwaief991b92007-02-22 12:52:53 +0100405 snd_pcm_group_for_each_entry(s, substream) {
Clemens Ladisch29998d22007-07-30 08:14:31 +0200406 if (snd_pcm_substream_chip(s) == ice) {
407 const struct vt1724_pcm_reg *reg;
408 reg = s->runtime->private_data;
409 what |= reg->start;
410 snd_pcm_trigger_done(s, substream);
411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
413
414 switch (cmd) {
415 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
416 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
417 spin_lock(&ice->reg_lock);
418 old = inb(ICEMT1724(ice, DMA_PAUSE));
419 if (cmd == SNDRV_PCM_TRIGGER_PAUSE_PUSH)
420 old |= what;
421 else
422 old &= ~what;
423 outb(old, ICEMT1724(ice, DMA_PAUSE));
424 spin_unlock(&ice->reg_lock);
425 break;
426
427 case SNDRV_PCM_TRIGGER_START:
428 case SNDRV_PCM_TRIGGER_STOP:
429 spin_lock(&ice->reg_lock);
430 old = inb(ICEMT1724(ice, DMA_CONTROL));
431 if (cmd == SNDRV_PCM_TRIGGER_START)
432 old |= what;
433 else
434 old &= ~what;
435 outb(old, ICEMT1724(ice, DMA_CONTROL));
436 spin_unlock(&ice->reg_lock);
437 break;
438
439 default:
440 return -EINVAL;
441 }
442 return 0;
443}
444
445/*
446 */
447
448#define DMA_STARTS (VT1724_RDMA0_START|VT1724_PDMA0_START|VT1724_RDMA1_START|\
449 VT1724_PDMA1_START|VT1724_PDMA2_START|VT1724_PDMA3_START|VT1724_PDMA4_START)
450#define DMA_PAUSES (VT1724_RDMA0_PAUSE|VT1724_PDMA0_PAUSE|VT1724_RDMA1_PAUSE|\
451 VT1724_PDMA1_PAUSE|VT1724_PDMA2_PAUSE|VT1724_PDMA3_PAUSE|VT1724_PDMA4_PAUSE)
452
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100453static const unsigned int stdclock_rate_list[16] = {
454 48000, 24000, 12000, 9600, 32000, 16000, 8000, 96000, 44100,
455 22050, 11025, 88200, 176400, 0, 192000, 64000
456};
457
458static unsigned int stdclock_get_rate(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100460 unsigned int rate;
461 rate = stdclock_rate_list[inb(ICEMT1724(ice, RATE)) & 15];
462 return rate;
463}
464
465static void stdclock_set_rate(struct snd_ice1712 *ice, unsigned int rate)
466{
467 int i;
468 for (i = 0; i < ARRAY_SIZE(stdclock_rate_list); i++) {
469 if (stdclock_rate_list[i] == rate) {
470 outb(i, ICEMT1724(ice, RATE));
471 return;
472 }
473 }
474}
475
476static unsigned char stdclock_set_mclk(struct snd_ice1712 *ice,
477 unsigned int rate)
478{
479 unsigned char val, old;
480 /* check MT02 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 if (ice->eeprom.data[ICE_EEP2_ACLINK] & VT1724_CFG_PRO_I2S) {
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100482 val = old = inb(ICEMT1724(ice, I2S_FORMAT));
483 if (rate > 96000)
484 val |= VT1724_MT_I2S_MCLK_128X; /* 128x MCLK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 else
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100486 val &= ~VT1724_MT_I2S_MCLK_128X; /* 256x MCLK */
487 if (val != old) {
488 outb(val, ICEMT1724(ice, I2S_FORMAT));
489 /* master clock changed */
490 return 1;
491 }
492 }
493 /* no change in master clock */
494 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495}
496
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100497static void snd_vt1724_set_pro_rate(struct snd_ice1712 *ice, unsigned int rate,
498 int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
500 unsigned long flags;
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100501 unsigned char mclk_change;
502 unsigned int i, old_rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100504 if (rate > ice->hw_rates->list[ice->hw_rates->count - 1])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 spin_lock_irqsave(&ice->reg_lock, flags);
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100507 if ((inb(ICEMT1724(ice, DMA_CONTROL)) & DMA_STARTS) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 (inb(ICEMT1724(ice, DMA_PAUSE)) & DMA_PAUSES)) {
509 /* running? we cannot change the rate now... */
510 spin_unlock_irqrestore(&ice->reg_lock, flags);
511 return;
512 }
513 if (!force && is_pro_rate_locked(ice)) {
514 spin_unlock_irqrestore(&ice->reg_lock, flags);
515 return;
516 }
517
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100518 old_rate = ice->get_rate(ice);
519 if (force || (old_rate != rate))
520 ice->set_rate(ice, rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 else if (rate == ice->cur_rate) {
522 spin_unlock_irqrestore(&ice->reg_lock, flags);
523 return;
524 }
525
526 ice->cur_rate = rate;
527
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100528 /* setting master clock */
529 mclk_change = ice->set_mclk(ice, rate);
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 spin_unlock_irqrestore(&ice->reg_lock, flags);
532
533 if (mclk_change && ice->gpio.i2s_mclk_changed)
534 ice->gpio.i2s_mclk_changed(ice);
535 if (ice->gpio.set_pro_rate)
536 ice->gpio.set_pro_rate(ice, rate);
537
538 /* set up codecs */
539 for (i = 0; i < ice->akm_codecs; i++) {
540 if (ice->akm[i].ops.set_rate_val)
541 ice->akm[i].ops.set_rate_val(&ice->akm[i], rate);
542 }
543 if (ice->spdif.ops.setup_rate)
544 ice->spdif.ops.setup_rate(ice, rate);
545}
546
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100547static int snd_vt1724_pcm_hw_params(struct snd_pcm_substream *substream,
548 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100550 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 int i, chs;
552
553 chs = params_channels(hw_params);
Ingo Molnar62932df2006-01-16 16:34:20 +0100554 mutex_lock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 /* mark surround channels */
556 if (substream == ice->playback_pro_substream) {
557 /* PDMA0 can be multi-channel up to 8 */
558 chs = chs / 2 - 1;
559 for (i = 0; i < chs; i++) {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100560 if (ice->pcm_reserved[i] &&
561 ice->pcm_reserved[i] != substream) {
Ingo Molnar62932df2006-01-16 16:34:20 +0100562 mutex_unlock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 return -EBUSY;
564 }
565 ice->pcm_reserved[i] = substream;
566 }
567 for (; i < 3; i++) {
568 if (ice->pcm_reserved[i] == substream)
569 ice->pcm_reserved[i] = NULL;
570 }
571 } else {
572 for (i = 0; i < 3; i++) {
573 /* check individual playback stream */
574 if (ice->playback_con_substream_ds[i] == substream) {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100575 if (ice->pcm_reserved[i] &&
576 ice->pcm_reserved[i] != substream) {
Ingo Molnar62932df2006-01-16 16:34:20 +0100577 mutex_unlock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 return -EBUSY;
579 }
580 ice->pcm_reserved[i] = substream;
581 break;
582 }
583 }
584 }
Ingo Molnar62932df2006-01-16 16:34:20 +0100585 mutex_unlock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 snd_vt1724_set_pro_rate(ice, params_rate(hw_params), 0);
587 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
588}
589
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100590static int snd_vt1724_pcm_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100592 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 int i;
594
Ingo Molnar62932df2006-01-16 16:34:20 +0100595 mutex_lock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 /* unmark surround channels */
597 for (i = 0; i < 3; i++)
598 if (ice->pcm_reserved[i] == substream)
599 ice->pcm_reserved[i] = NULL;
Ingo Molnar62932df2006-01-16 16:34:20 +0100600 mutex_unlock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 return snd_pcm_lib_free_pages(substream);
602}
603
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100604static int snd_vt1724_playback_pro_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100606 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 unsigned char val;
608 unsigned int size;
609
610 spin_lock_irq(&ice->reg_lock);
611 val = (8 - substream->runtime->channels) >> 1;
612 outb(val, ICEMT1724(ice, BURST));
613
614 outl(substream->runtime->dma_addr, ICEMT1724(ice, PLAYBACK_ADDR));
615
616 size = (snd_pcm_lib_buffer_bytes(substream) >> 2) - 1;
617 // outl(size, ICEMT1724(ice, PLAYBACK_SIZE));
618 outw(size, ICEMT1724(ice, PLAYBACK_SIZE));
619 outb(size >> 16, ICEMT1724(ice, PLAYBACK_SIZE) + 2);
620 size = (snd_pcm_lib_period_bytes(substream) >> 2) - 1;
621 // outl(size, ICEMT1724(ice, PLAYBACK_COUNT));
622 outw(size, ICEMT1724(ice, PLAYBACK_COUNT));
623 outb(size >> 16, ICEMT1724(ice, PLAYBACK_COUNT) + 2);
624
625 spin_unlock_irq(&ice->reg_lock);
626
627 // 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));
628 return 0;
629}
630
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100631static snd_pcm_uframes_t snd_vt1724_playback_pro_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100633 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 size_t ptr;
635
636 if (!(inl(ICEMT1724(ice, DMA_CONTROL)) & VT1724_PDMA0_START))
637 return 0;
638#if 0 /* read PLAYBACK_ADDR */
639 ptr = inl(ICEMT1724(ice, PLAYBACK_ADDR));
640 if (ptr < substream->runtime->dma_addr) {
641 snd_printd("ice1724: invalid negative ptr\n");
642 return 0;
643 }
644 ptr -= substream->runtime->dma_addr;
645 ptr = bytes_to_frames(substream->runtime, ptr);
646 if (ptr >= substream->runtime->buffer_size) {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100647 snd_printd("ice1724: invalid ptr %d (size=%d)\n",
648 (int)ptr, (int)substream->runtime->period_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return 0;
650 }
651#else /* read PLAYBACK_SIZE */
652 ptr = inl(ICEMT1724(ice, PLAYBACK_SIZE)) & 0xffffff;
653 ptr = (ptr + 1) << 2;
654 ptr = bytes_to_frames(substream->runtime, ptr);
655 if (! ptr)
656 ;
657 else if (ptr <= substream->runtime->buffer_size)
658 ptr = substream->runtime->buffer_size - ptr;
659 else {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100660 snd_printd("ice1724: invalid ptr %d (size=%d)\n",
661 (int)ptr, (int)substream->runtime->buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 ptr = 0;
663 }
664#endif
665 return ptr;
666}
667
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100668static int snd_vt1724_pcm_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100670 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Takashi Iwai32b47da2007-01-29 15:26:36 +0100671 const struct vt1724_pcm_reg *reg = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 spin_lock_irq(&ice->reg_lock);
674 outl(substream->runtime->dma_addr, ice->profi_port + reg->addr);
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100675 outw((snd_pcm_lib_buffer_bytes(substream) >> 2) - 1,
676 ice->profi_port + reg->size);
677 outw((snd_pcm_lib_period_bytes(substream) >> 2) - 1,
678 ice->profi_port + reg->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 spin_unlock_irq(&ice->reg_lock);
680 return 0;
681}
682
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100683static snd_pcm_uframes_t snd_vt1724_pcm_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100685 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Takashi Iwai32b47da2007-01-29 15:26:36 +0100686 const struct vt1724_pcm_reg *reg = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 size_t ptr;
688
689 if (!(inl(ICEMT1724(ice, DMA_CONTROL)) & reg->start))
690 return 0;
691#if 0 /* use ADDR register */
692 ptr = inl(ice->profi_port + reg->addr);
693 ptr -= substream->runtime->dma_addr;
694 return bytes_to_frames(substream->runtime, ptr);
695#else /* use SIZE register */
696 ptr = inw(ice->profi_port + reg->size);
697 ptr = (ptr + 1) << 2;
698 ptr = bytes_to_frames(substream->runtime, ptr);
699 if (! ptr)
700 ;
701 else if (ptr <= substream->runtime->buffer_size)
702 ptr = substream->runtime->buffer_size - ptr;
703 else {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100704 snd_printd("ice1724: invalid ptr %d (size=%d)\n",
705 (int)ptr, (int)substream->runtime->buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 ptr = 0;
707 }
708 return ptr;
709#endif
710}
711
Takashi Iwai32b47da2007-01-29 15:26:36 +0100712static const struct vt1724_pcm_reg vt1724_playback_pro_reg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 .addr = VT1724_MT_PLAYBACK_ADDR,
714 .size = VT1724_MT_PLAYBACK_SIZE,
715 .count = VT1724_MT_PLAYBACK_COUNT,
716 .start = VT1724_PDMA0_START,
717};
718
Takashi Iwai32b47da2007-01-29 15:26:36 +0100719static const struct vt1724_pcm_reg vt1724_capture_pro_reg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 .addr = VT1724_MT_CAPTURE_ADDR,
721 .size = VT1724_MT_CAPTURE_SIZE,
722 .count = VT1724_MT_CAPTURE_COUNT,
723 .start = VT1724_RDMA0_START,
724};
725
Takashi Iwai32b47da2007-01-29 15:26:36 +0100726static const struct snd_pcm_hardware snd_vt1724_playback_pro =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
728 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
729 SNDRV_PCM_INFO_BLOCK_TRANSFER |
730 SNDRV_PCM_INFO_MMAP_VALID |
731 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_SYNC_START),
732 .formats = SNDRV_PCM_FMTBIT_S32_LE,
733 .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_192000,
734 .rate_min = 8000,
735 .rate_max = 192000,
736 .channels_min = 2,
737 .channels_max = 8,
738 .buffer_bytes_max = (1UL << 21), /* 19bits dword */
739 .period_bytes_min = 8 * 4 * 2, /* FIXME: constraints needed */
740 .period_bytes_max = (1UL << 21),
741 .periods_min = 2,
742 .periods_max = 1024,
743};
744
Takashi Iwai32b47da2007-01-29 15:26:36 +0100745static const struct snd_pcm_hardware snd_vt1724_spdif =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
747 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
748 SNDRV_PCM_INFO_BLOCK_TRANSFER |
749 SNDRV_PCM_INFO_MMAP_VALID |
750 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_SYNC_START),
751 .formats = SNDRV_PCM_FMTBIT_S32_LE,
Takashi Iwai2dfbeca2005-10-12 10:04:42 +0200752 .rates = (SNDRV_PCM_RATE_32000|SNDRV_PCM_RATE_44100|
753 SNDRV_PCM_RATE_48000|SNDRV_PCM_RATE_88200|
754 SNDRV_PCM_RATE_96000|SNDRV_PCM_RATE_176400|
755 SNDRV_PCM_RATE_192000),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 .rate_min = 32000,
Takashi Iwai2dfbeca2005-10-12 10:04:42 +0200757 .rate_max = 192000,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 .channels_min = 2,
759 .channels_max = 2,
760 .buffer_bytes_max = (1UL << 18), /* 16bits dword */
761 .period_bytes_min = 2 * 4 * 2,
762 .period_bytes_max = (1UL << 18),
763 .periods_min = 2,
764 .periods_max = 1024,
765};
766
Takashi Iwai32b47da2007-01-29 15:26:36 +0100767static const struct snd_pcm_hardware snd_vt1724_2ch_stereo =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
769 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
770 SNDRV_PCM_INFO_BLOCK_TRANSFER |
771 SNDRV_PCM_INFO_MMAP_VALID |
772 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_SYNC_START),
773 .formats = SNDRV_PCM_FMTBIT_S32_LE,
774 .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_192000,
775 .rate_min = 8000,
776 .rate_max = 192000,
777 .channels_min = 2,
778 .channels_max = 2,
779 .buffer_bytes_max = (1UL << 18), /* 16bits dword */
780 .period_bytes_min = 2 * 4 * 2,
781 .period_bytes_max = (1UL << 18),
782 .periods_min = 2,
783 .periods_max = 1024,
784};
785
786/*
787 * set rate constraints
788 */
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100789static void set_std_hw_rates(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 if (ice->eeprom.data[ICE_EEP2_ACLINK] & VT1724_CFG_PRO_I2S) {
792 /* I2S */
793 /* VT1720 doesn't support more than 96kHz */
794 if ((ice->eeprom.data[ICE_EEP2_I2S] & 0x08) && !ice->vt1720)
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100795 ice->hw_rates = &hw_constraints_rates_192;
796 else
797 ice->hw_rates = &hw_constraints_rates_96;
798 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 /* ACLINK */
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100800 ice->hw_rates = &hw_constraints_rates_48;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 }
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100802}
803
804static int set_rate_constraints(struct snd_ice1712 *ice,
805 struct snd_pcm_substream *substream)
806{
807 struct snd_pcm_runtime *runtime = substream->runtime;
808
809 runtime->hw.rate_min = ice->hw_rates->list[0];
810 runtime->hw.rate_max = ice->hw_rates->list[ice->hw_rates->count - 1];
811 runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
812 return snd_pcm_hw_constraint_list(runtime, 0,
813 SNDRV_PCM_HW_PARAM_RATE,
814 ice->hw_rates);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815}
816
817/* multi-channel playback needs alignment 8x32bit regardless of the channels
818 * actually used
819 */
820#define VT1724_BUFFER_ALIGN 0x20
821
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100822static int snd_vt1724_playback_pro_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100824 struct snd_pcm_runtime *runtime = substream->runtime;
825 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 int chs;
827
Takashi Iwai32b47da2007-01-29 15:26:36 +0100828 runtime->private_data = (void *)&vt1724_playback_pro_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 ice->playback_pro_substream = substream;
830 runtime->hw = snd_vt1724_playback_pro;
831 snd_pcm_set_sync(substream);
832 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
833 set_rate_constraints(ice, substream);
Ingo Molnar62932df2006-01-16 16:34:20 +0100834 mutex_lock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 /* calculate the currently available channels */
836 for (chs = 0; chs < 3; chs++) {
837 if (ice->pcm_reserved[chs])
838 break;
839 }
840 chs = (chs + 1) * 2;
841 runtime->hw.channels_max = chs;
842 if (chs > 2) /* channels must be even */
843 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Ingo Molnar62932df2006-01-16 16:34:20 +0100844 mutex_unlock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
846 VT1724_BUFFER_ALIGN);
847 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
848 VT1724_BUFFER_ALIGN);
849 return 0;
850}
851
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100852static int snd_vt1724_capture_pro_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100854 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
855 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Takashi Iwai32b47da2007-01-29 15:26:36 +0100857 runtime->private_data = (void *)&vt1724_capture_pro_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 ice->capture_pro_substream = substream;
859 runtime->hw = snd_vt1724_2ch_stereo;
860 snd_pcm_set_sync(substream);
861 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
862 set_rate_constraints(ice, substream);
863 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
864 VT1724_BUFFER_ALIGN);
865 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
866 VT1724_BUFFER_ALIGN);
867 return 0;
868}
869
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100870static int snd_vt1724_playback_pro_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100872 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
874 if (PRO_RATE_RESET)
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100875 snd_vt1724_set_pro_rate(ice, ice->pro_rate_default, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 ice->playback_pro_substream = NULL;
877
878 return 0;
879}
880
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100881static int snd_vt1724_capture_pro_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100883 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885 if (PRO_RATE_RESET)
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100886 snd_vt1724_set_pro_rate(ice, ice->pro_rate_default, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 ice->capture_pro_substream = NULL;
888 return 0;
889}
890
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100891static struct snd_pcm_ops snd_vt1724_playback_pro_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 .open = snd_vt1724_playback_pro_open,
893 .close = snd_vt1724_playback_pro_close,
894 .ioctl = snd_pcm_lib_ioctl,
895 .hw_params = snd_vt1724_pcm_hw_params,
896 .hw_free = snd_vt1724_pcm_hw_free,
897 .prepare = snd_vt1724_playback_pro_prepare,
898 .trigger = snd_vt1724_pcm_trigger,
899 .pointer = snd_vt1724_playback_pro_pointer,
900};
901
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100902static struct snd_pcm_ops snd_vt1724_capture_pro_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 .open = snd_vt1724_capture_pro_open,
904 .close = snd_vt1724_capture_pro_close,
905 .ioctl = snd_pcm_lib_ioctl,
906 .hw_params = snd_vt1724_pcm_hw_params,
907 .hw_free = snd_vt1724_pcm_hw_free,
908 .prepare = snd_vt1724_pcm_prepare,
909 .trigger = snd_vt1724_pcm_trigger,
910 .pointer = snd_vt1724_pcm_pointer,
911};
912
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100913static int __devinit snd_vt1724_pcm_profi(struct snd_ice1712 * ice, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100915 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 int err;
917
918 err = snd_pcm_new(ice->card, "ICE1724", device, 1, 1, &pcm);
919 if (err < 0)
920 return err;
921
922 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_vt1724_playback_pro_ops);
923 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_vt1724_capture_pro_ops);
924
925 pcm->private_data = ice;
926 pcm->info_flags = 0;
927 strcpy(pcm->name, "ICE1724");
928
929 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100930 snd_dma_pci_data(ice->pci),
931 256*1024, 256*1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933 ice->pcm_pro = pcm;
934
935 return 0;
936}
937
938
939/*
940 * SPDIF PCM
941 */
942
Takashi Iwai32b47da2007-01-29 15:26:36 +0100943static const struct vt1724_pcm_reg vt1724_playback_spdif_reg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 .addr = VT1724_MT_PDMA4_ADDR,
945 .size = VT1724_MT_PDMA4_SIZE,
946 .count = VT1724_MT_PDMA4_COUNT,
947 .start = VT1724_PDMA4_START,
948};
949
Takashi Iwai32b47da2007-01-29 15:26:36 +0100950static const struct vt1724_pcm_reg vt1724_capture_spdif_reg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 .addr = VT1724_MT_RDMA1_ADDR,
952 .size = VT1724_MT_RDMA1_SIZE,
953 .count = VT1724_MT_RDMA1_COUNT,
954 .start = VT1724_RDMA1_START,
955};
956
957/* update spdif control bits; call with reg_lock */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100958static void update_spdif_bits(struct snd_ice1712 *ice, unsigned int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959{
960 unsigned char cbit, disabled;
961
962 cbit = inb(ICEREG1724(ice, SPDIF_CFG));
963 disabled = cbit & ~VT1724_CFG_SPDIF_OUT_EN;
964 if (cbit != disabled)
965 outb(disabled, ICEREG1724(ice, SPDIF_CFG));
966 outw(val, ICEMT1724(ice, SPDIF_CTRL));
967 if (cbit != disabled)
968 outb(cbit, ICEREG1724(ice, SPDIF_CFG));
969 outw(val, ICEMT1724(ice, SPDIF_CTRL));
970}
971
972/* update SPDIF control bits according to the given rate */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100973static void update_spdif_rate(struct snd_ice1712 *ice, unsigned int rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974{
975 unsigned int val, nval;
976 unsigned long flags;
977
978 spin_lock_irqsave(&ice->reg_lock, flags);
979 nval = val = inw(ICEMT1724(ice, SPDIF_CTRL));
980 nval &= ~(7 << 12);
981 switch (rate) {
982 case 44100: break;
983 case 48000: nval |= 2 << 12; break;
984 case 32000: nval |= 3 << 12; break;
Takashi Iwai2dfbeca2005-10-12 10:04:42 +0200985 case 88200: nval |= 4 << 12; break;
986 case 96000: nval |= 5 << 12; break;
987 case 192000: nval |= 6 << 12; break;
988 case 176400: nval |= 7 << 12; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 }
990 if (val != nval)
991 update_spdif_bits(ice, nval);
992 spin_unlock_irqrestore(&ice->reg_lock, flags);
993}
994
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100995static int snd_vt1724_playback_spdif_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100997 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 if (! ice->force_pdma4)
999 update_spdif_rate(ice, substream->runtime->rate);
1000 return snd_vt1724_pcm_prepare(substream);
1001}
1002
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001003static int snd_vt1724_playback_spdif_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001005 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
1006 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Takashi Iwai32b47da2007-01-29 15:26:36 +01001008 runtime->private_data = (void *)&vt1724_playback_spdif_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 ice->playback_con_substream = substream;
1010 if (ice->force_pdma4) {
1011 runtime->hw = snd_vt1724_2ch_stereo;
1012 set_rate_constraints(ice, substream);
1013 } else
1014 runtime->hw = snd_vt1724_spdif;
1015 snd_pcm_set_sync(substream);
1016 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
1017 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1018 VT1724_BUFFER_ALIGN);
1019 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1020 VT1724_BUFFER_ALIGN);
Takashi Iwaic93f5a12008-03-14 17:17:09 +01001021 if (ice->spdif.ops.open)
1022 ice->spdif.ops.open(ice, substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 return 0;
1024}
1025
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001026static int snd_vt1724_playback_spdif_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001028 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
1030 if (PRO_RATE_RESET)
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001031 snd_vt1724_set_pro_rate(ice, ice->pro_rate_default, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 ice->playback_con_substream = NULL;
Takashi Iwaic93f5a12008-03-14 17:17:09 +01001033 if (ice->spdif.ops.close)
1034 ice->spdif.ops.close(ice, substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
1036 return 0;
1037}
1038
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001039static int snd_vt1724_capture_spdif_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001041 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
1042 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Takashi Iwai32b47da2007-01-29 15:26:36 +01001044 runtime->private_data = (void *)&vt1724_capture_spdif_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 ice->capture_con_substream = substream;
1046 if (ice->force_rdma1) {
1047 runtime->hw = snd_vt1724_2ch_stereo;
1048 set_rate_constraints(ice, substream);
1049 } else
1050 runtime->hw = snd_vt1724_spdif;
1051 snd_pcm_set_sync(substream);
1052 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
1053 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1054 VT1724_BUFFER_ALIGN);
1055 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1056 VT1724_BUFFER_ALIGN);
Takashi Iwaic93f5a12008-03-14 17:17:09 +01001057 if (ice->spdif.ops.open)
1058 ice->spdif.ops.open(ice, substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 return 0;
1060}
1061
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001062static int snd_vt1724_capture_spdif_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001064 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
1066 if (PRO_RATE_RESET)
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001067 snd_vt1724_set_pro_rate(ice, ice->pro_rate_default, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 ice->capture_con_substream = NULL;
Takashi Iwaic93f5a12008-03-14 17:17:09 +01001069 if (ice->spdif.ops.close)
1070 ice->spdif.ops.close(ice, substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
1072 return 0;
1073}
1074
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001075static struct snd_pcm_ops snd_vt1724_playback_spdif_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 .open = snd_vt1724_playback_spdif_open,
1077 .close = snd_vt1724_playback_spdif_close,
1078 .ioctl = snd_pcm_lib_ioctl,
1079 .hw_params = snd_vt1724_pcm_hw_params,
1080 .hw_free = snd_vt1724_pcm_hw_free,
1081 .prepare = snd_vt1724_playback_spdif_prepare,
1082 .trigger = snd_vt1724_pcm_trigger,
1083 .pointer = snd_vt1724_pcm_pointer,
1084};
1085
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001086static struct snd_pcm_ops snd_vt1724_capture_spdif_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 .open = snd_vt1724_capture_spdif_open,
1088 .close = snd_vt1724_capture_spdif_close,
1089 .ioctl = snd_pcm_lib_ioctl,
1090 .hw_params = snd_vt1724_pcm_hw_params,
1091 .hw_free = snd_vt1724_pcm_hw_free,
1092 .prepare = snd_vt1724_pcm_prepare,
1093 .trigger = snd_vt1724_pcm_trigger,
1094 .pointer = snd_vt1724_pcm_pointer,
1095};
1096
1097
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001098static int __devinit snd_vt1724_pcm_spdif(struct snd_ice1712 * ice, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099{
1100 char *name;
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001101 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 int play, capt;
1103 int err;
1104
1105 if (ice->force_pdma4 ||
1106 (ice->eeprom.data[ICE_EEP2_SPDIF] & VT1724_CFG_SPDIF_OUT_INT)) {
1107 play = 1;
1108 ice->has_spdif = 1;
1109 } else
1110 play = 0;
1111 if (ice->force_rdma1 ||
1112 (ice->eeprom.data[ICE_EEP2_SPDIF] & VT1724_CFG_SPDIF_IN)) {
1113 capt = 1;
1114 ice->has_spdif = 1;
1115 } else
1116 capt = 0;
1117 if (! play && ! capt)
1118 return 0; /* no spdif device */
1119
1120 if (ice->force_pdma4 || ice->force_rdma1)
1121 name = "ICE1724 Secondary";
1122 else
1123 name = "IEC1724 IEC958";
1124 err = snd_pcm_new(ice->card, name, device, play, capt, &pcm);
1125 if (err < 0)
1126 return err;
1127
1128 if (play)
1129 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1130 &snd_vt1724_playback_spdif_ops);
1131 if (capt)
1132 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1133 &snd_vt1724_capture_spdif_ops);
1134
1135 pcm->private_data = ice;
1136 pcm->info_flags = 0;
1137 strcpy(pcm->name, name);
1138
1139 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001140 snd_dma_pci_data(ice->pci),
1141 64*1024, 64*1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
1143 ice->pcm = pcm;
1144
1145 return 0;
1146}
1147
1148
1149/*
1150 * independent surround PCMs
1151 */
1152
Takashi Iwai32b47da2007-01-29 15:26:36 +01001153static const struct vt1724_pcm_reg vt1724_playback_dma_regs[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 {
1155 .addr = VT1724_MT_PDMA1_ADDR,
1156 .size = VT1724_MT_PDMA1_SIZE,
1157 .count = VT1724_MT_PDMA1_COUNT,
1158 .start = VT1724_PDMA1_START,
1159 },
1160 {
1161 .addr = VT1724_MT_PDMA2_ADDR,
1162 .size = VT1724_MT_PDMA2_SIZE,
1163 .count = VT1724_MT_PDMA2_COUNT,
1164 .start = VT1724_PDMA2_START,
1165 },
1166 {
1167 .addr = VT1724_MT_PDMA3_ADDR,
1168 .size = VT1724_MT_PDMA3_SIZE,
1169 .count = VT1724_MT_PDMA3_COUNT,
1170 .start = VT1724_PDMA3_START,
1171 },
1172};
1173
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001174static int snd_vt1724_playback_indep_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001176 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 unsigned char val;
1178
1179 spin_lock_irq(&ice->reg_lock);
1180 val = 3 - substream->number;
1181 if (inb(ICEMT1724(ice, BURST)) < val)
1182 outb(val, ICEMT1724(ice, BURST));
1183 spin_unlock_irq(&ice->reg_lock);
1184 return snd_vt1724_pcm_prepare(substream);
1185}
1186
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001187static int snd_vt1724_playback_indep_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001189 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
1190 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Ingo Molnar62932df2006-01-16 16:34:20 +01001192 mutex_lock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 /* already used by PDMA0? */
1194 if (ice->pcm_reserved[substream->number]) {
Ingo Molnar62932df2006-01-16 16:34:20 +01001195 mutex_unlock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 return -EBUSY; /* FIXME: should handle blocking mode properly */
1197 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001198 mutex_unlock(&ice->open_mutex);
Takashi Iwai32b47da2007-01-29 15:26:36 +01001199 runtime->private_data = (void *)&vt1724_playback_dma_regs[substream->number];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 ice->playback_con_substream_ds[substream->number] = substream;
1201 runtime->hw = snd_vt1724_2ch_stereo;
1202 snd_pcm_set_sync(substream);
1203 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
1204 set_rate_constraints(ice, substream);
1205 return 0;
1206}
1207
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001208static int snd_vt1724_playback_indep_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001210 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
1212 if (PRO_RATE_RESET)
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001213 snd_vt1724_set_pro_rate(ice, ice->pro_rate_default, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 ice->playback_con_substream_ds[substream->number] = NULL;
1215 ice->pcm_reserved[substream->number] = NULL;
1216
1217 return 0;
1218}
1219
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001220static struct snd_pcm_ops snd_vt1724_playback_indep_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 .open = snd_vt1724_playback_indep_open,
1222 .close = snd_vt1724_playback_indep_close,
1223 .ioctl = snd_pcm_lib_ioctl,
1224 .hw_params = snd_vt1724_pcm_hw_params,
1225 .hw_free = snd_vt1724_pcm_hw_free,
1226 .prepare = snd_vt1724_playback_indep_prepare,
1227 .trigger = snd_vt1724_pcm_trigger,
1228 .pointer = snd_vt1724_pcm_pointer,
1229};
1230
1231
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001232static int __devinit snd_vt1724_pcm_indep(struct snd_ice1712 * ice, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001234 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 int play;
1236 int err;
1237
1238 play = ice->num_total_dacs / 2 - 1;
1239 if (play <= 0)
1240 return 0;
1241
1242 err = snd_pcm_new(ice->card, "ICE1724 Surrounds", device, play, 0, &pcm);
1243 if (err < 0)
1244 return err;
1245
1246 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1247 &snd_vt1724_playback_indep_ops);
1248
1249 pcm->private_data = ice;
1250 pcm->info_flags = 0;
1251 strcpy(pcm->name, "ICE1724 Surround PCM");
1252
1253 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001254 snd_dma_pci_data(ice->pci),
1255 64*1024, 64*1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
1257 ice->pcm_ds = pcm;
1258
1259 return 0;
1260}
1261
1262
1263/*
1264 * Mixer section
1265 */
1266
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001267static int __devinit snd_vt1724_ac97_mixer(struct snd_ice1712 * ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268{
1269 int err;
1270
1271 if (! (ice->eeprom.data[ICE_EEP2_ACLINK] & VT1724_CFG_PRO_I2S)) {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001272 struct snd_ac97_bus *pbus;
1273 struct snd_ac97_template ac97;
1274 static struct snd_ac97_bus_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 .write = snd_vt1724_ac97_write,
1276 .read = snd_vt1724_ac97_read,
1277 };
1278
1279 /* cold reset */
1280 outb(inb(ICEMT1724(ice, AC97_CMD)) | 0x80, ICEMT1724(ice, AC97_CMD));
1281 mdelay(5); /* FIXME */
1282 outb(inb(ICEMT1724(ice, AC97_CMD)) & ~0x80, ICEMT1724(ice, AC97_CMD));
1283
1284 if ((err = snd_ac97_bus(ice->card, 0, &ops, NULL, &pbus)) < 0)
1285 return err;
1286 memset(&ac97, 0, sizeof(ac97));
1287 ac97.private_data = ice;
1288 if ((err = snd_ac97_mixer(pbus, &ac97, &ice->ac97)) < 0)
1289 printk(KERN_WARNING "ice1712: cannot initialize pro ac97, skipped\n");
1290 else
1291 return 0;
1292 }
1293 /* I2S mixer only */
1294 strcat(ice->card->mixername, "ICE1724 - multitrack");
1295 return 0;
1296}
1297
1298/*
1299 *
1300 */
1301
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001302static inline unsigned int eeprom_triple(struct snd_ice1712 *ice, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303{
1304 return (unsigned int)ice->eeprom.data[idx] | \
1305 ((unsigned int)ice->eeprom.data[idx + 1] << 8) | \
1306 ((unsigned int)ice->eeprom.data[idx + 2] << 16);
1307}
1308
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001309static void snd_vt1724_proc_read(struct snd_info_entry *entry,
1310 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001312 struct snd_ice1712 *ice = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 unsigned int idx;
1314
1315 snd_iprintf(buffer, "%s\n\n", ice->card->longname);
1316 snd_iprintf(buffer, "EEPROM:\n");
1317
1318 snd_iprintf(buffer, " Subvendor : 0x%x\n", ice->eeprom.subvendor);
1319 snd_iprintf(buffer, " Size : %i bytes\n", ice->eeprom.size);
1320 snd_iprintf(buffer, " Version : %i\n", ice->eeprom.version);
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001321 snd_iprintf(buffer, " System Config : 0x%x\n",
1322 ice->eeprom.data[ICE_EEP2_SYSCONF]);
1323 snd_iprintf(buffer, " ACLink : 0x%x\n",
1324 ice->eeprom.data[ICE_EEP2_ACLINK]);
1325 snd_iprintf(buffer, " I2S : 0x%x\n",
1326 ice->eeprom.data[ICE_EEP2_I2S]);
1327 snd_iprintf(buffer, " S/PDIF : 0x%x\n",
1328 ice->eeprom.data[ICE_EEP2_SPDIF]);
1329 snd_iprintf(buffer, " GPIO direction : 0x%x\n",
1330 ice->eeprom.gpiodir);
1331 snd_iprintf(buffer, " GPIO mask : 0x%x\n",
1332 ice->eeprom.gpiomask);
1333 snd_iprintf(buffer, " GPIO state : 0x%x\n",
1334 ice->eeprom.gpiostate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 for (idx = 0x12; idx < ice->eeprom.size; idx++)
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001336 snd_iprintf(buffer, " Extra #%02i : 0x%x\n",
1337 idx, ice->eeprom.data[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
1339 snd_iprintf(buffer, "\nRegisters:\n");
1340
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001341 snd_iprintf(buffer, " PSDOUT03 : 0x%08x\n",
1342 (unsigned)inl(ICEMT1724(ice, ROUTE_PLAYBACK)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 for (idx = 0x0; idx < 0x20 ; idx++)
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001344 snd_iprintf(buffer, " CCS%02x : 0x%02x\n",
1345 idx, inb(ice->port+idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 for (idx = 0x0; idx < 0x30 ; idx++)
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001347 snd_iprintf(buffer, " MT%02x : 0x%02x\n",
1348 idx, inb(ice->profi_port+idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349}
1350
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001351static void __devinit snd_vt1724_proc_init(struct snd_ice1712 * ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001353 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
1355 if (! snd_card_proc_new(ice->card, "ice1724", &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02001356 snd_info_set_text_ops(entry, ice, snd_vt1724_proc_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357}
1358
1359/*
1360 *
1361 */
1362
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001363static int snd_vt1724_eeprom_info(struct snd_kcontrol *kcontrol,
1364 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365{
1366 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001367 uinfo->count = sizeof(struct snd_ice1712_eeprom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 return 0;
1369}
1370
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001371static int snd_vt1724_eeprom_get(struct snd_kcontrol *kcontrol,
1372 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001374 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
1376 memcpy(ucontrol->value.bytes.data, &ice->eeprom, sizeof(ice->eeprom));
1377 return 0;
1378}
1379
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01001380static struct snd_kcontrol_new snd_vt1724_eeprom __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1382 .name = "ICE1724 EEPROM",
1383 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1384 .info = snd_vt1724_eeprom_info,
1385 .get = snd_vt1724_eeprom_get
1386};
1387
1388/*
1389 */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001390static int snd_vt1724_spdif_info(struct snd_kcontrol *kcontrol,
1391 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392{
1393 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1394 uinfo->count = 1;
1395 return 0;
1396}
1397
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001398static unsigned int encode_spdif_bits(struct snd_aes_iec958 *diga)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399{
Takashi Iwai2dfbeca2005-10-12 10:04:42 +02001400 unsigned int val, rbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
1402 val = diga->status[0] & 0x03; /* professional, non-audio */
1403 if (val & 0x01) {
1404 /* professional */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001405 if ((diga->status[0] & IEC958_AES0_PRO_EMPHASIS) ==
1406 IEC958_AES0_PRO_EMPHASIS_5015)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 val |= 1U << 3;
Takashi Iwai2dfbeca2005-10-12 10:04:42 +02001408 rbits = (diga->status[4] >> 3) & 0x0f;
1409 if (rbits) {
1410 switch (rbits) {
1411 case 2: val |= 5 << 12; break; /* 96k */
1412 case 3: val |= 6 << 12; break; /* 192k */
1413 case 10: val |= 4 << 12; break; /* 88.2k */
1414 case 11: val |= 7 << 12; break; /* 176.4k */
1415 }
1416 } else {
1417 switch (diga->status[0] & IEC958_AES0_PRO_FS) {
1418 case IEC958_AES0_PRO_FS_44100:
1419 break;
1420 case IEC958_AES0_PRO_FS_32000:
1421 val |= 3U << 12;
1422 break;
1423 default:
1424 val |= 2U << 12;
1425 break;
1426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 }
1428 } else {
1429 /* consumer */
1430 val |= diga->status[1] & 0x04; /* copyright */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001431 if ((diga->status[0] & IEC958_AES0_CON_EMPHASIS) ==
1432 IEC958_AES0_CON_EMPHASIS_5015)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 val |= 1U << 3;
1434 val |= (unsigned int)(diga->status[1] & 0x3f) << 4; /* category */
1435 val |= (unsigned int)(diga->status[3] & IEC958_AES3_CON_FS) << 12; /* fs */
1436 }
1437 return val;
1438}
1439
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001440static void decode_spdif_bits(struct snd_aes_iec958 *diga, unsigned int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
1442 memset(diga->status, 0, sizeof(diga->status));
1443 diga->status[0] = val & 0x03; /* professional, non-audio */
1444 if (val & 0x01) {
1445 /* professional */
1446 if (val & (1U << 3))
1447 diga->status[0] |= IEC958_AES0_PRO_EMPHASIS_5015;
1448 switch ((val >> 12) & 0x7) {
1449 case 0:
1450 break;
1451 case 2:
1452 diga->status[0] |= IEC958_AES0_PRO_FS_32000;
1453 break;
1454 default:
1455 diga->status[0] |= IEC958_AES0_PRO_FS_48000;
1456 break;
1457 }
1458 } else {
1459 /* consumer */
1460 diga->status[0] |= val & (1U << 2); /* copyright */
1461 if (val & (1U << 3))
1462 diga->status[0] |= IEC958_AES0_CON_EMPHASIS_5015;
1463 diga->status[1] |= (val >> 4) & 0x3f; /* category */
1464 diga->status[3] |= (val >> 12) & 0x07; /* fs */
1465 }
1466}
1467
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001468static int snd_vt1724_spdif_default_get(struct snd_kcontrol *kcontrol,
1469 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001471 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 unsigned int val;
1473 val = inw(ICEMT1724(ice, SPDIF_CTRL));
1474 decode_spdif_bits(&ucontrol->value.iec958, val);
1475 return 0;
1476}
1477
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001478static int snd_vt1724_spdif_default_put(struct snd_kcontrol *kcontrol,
1479 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001481 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 unsigned int val, old;
1483
1484 val = encode_spdif_bits(&ucontrol->value.iec958);
1485 spin_lock_irq(&ice->reg_lock);
1486 old = inw(ICEMT1724(ice, SPDIF_CTRL));
1487 if (val != old)
1488 update_spdif_bits(ice, val);
1489 spin_unlock_irq(&ice->reg_lock);
1490 return (val != old);
1491}
1492
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01001493static struct snd_kcontrol_new snd_vt1724_spdif_default __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494{
1495 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1496 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1497 .info = snd_vt1724_spdif_info,
1498 .get = snd_vt1724_spdif_default_get,
1499 .put = snd_vt1724_spdif_default_put
1500};
1501
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001502static int snd_vt1724_spdif_maskc_get(struct snd_kcontrol *kcontrol,
1503 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504{
1505 ucontrol->value.iec958.status[0] = IEC958_AES0_NONAUDIO |
1506 IEC958_AES0_PROFESSIONAL |
1507 IEC958_AES0_CON_NOT_COPYRIGHT |
1508 IEC958_AES0_CON_EMPHASIS;
1509 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_ORIGINAL |
1510 IEC958_AES1_CON_CATEGORY;
1511 ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS;
1512 return 0;
1513}
1514
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001515static int snd_vt1724_spdif_maskp_get(struct snd_kcontrol *kcontrol,
1516 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517{
1518 ucontrol->value.iec958.status[0] = IEC958_AES0_NONAUDIO |
1519 IEC958_AES0_PROFESSIONAL |
1520 IEC958_AES0_PRO_FS |
1521 IEC958_AES0_PRO_EMPHASIS;
1522 return 0;
1523}
1524
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01001525static struct snd_kcontrol_new snd_vt1724_spdif_maskc __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526{
1527 .access = SNDRV_CTL_ELEM_ACCESS_READ,
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001528 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1530 .info = snd_vt1724_spdif_info,
1531 .get = snd_vt1724_spdif_maskc_get,
1532};
1533
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01001534static struct snd_kcontrol_new snd_vt1724_spdif_maskp __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535{
1536 .access = SNDRV_CTL_ELEM_ACCESS_READ,
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001537 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
1539 .info = snd_vt1724_spdif_info,
1540 .get = snd_vt1724_spdif_maskp_get,
1541};
1542
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001543#define snd_vt1724_spdif_sw_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001545static int snd_vt1724_spdif_sw_get(struct snd_kcontrol *kcontrol,
1546 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001548 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
1549 ucontrol->value.integer.value[0] = inb(ICEREG1724(ice, SPDIF_CFG)) &
1550 VT1724_CFG_SPDIF_OUT_EN ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 return 0;
1552}
1553
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001554static int snd_vt1724_spdif_sw_put(struct snd_kcontrol *kcontrol,
1555 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001557 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 unsigned char old, val;
1559
1560 spin_lock_irq(&ice->reg_lock);
1561 old = val = inb(ICEREG1724(ice, SPDIF_CFG));
1562 val &= ~VT1724_CFG_SPDIF_OUT_EN;
1563 if (ucontrol->value.integer.value[0])
1564 val |= VT1724_CFG_SPDIF_OUT_EN;
1565 if (old != val)
1566 outb(val, ICEREG1724(ice, SPDIF_CFG));
1567 spin_unlock_irq(&ice->reg_lock);
1568 return old != val;
1569}
1570
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01001571static struct snd_kcontrol_new snd_vt1724_spdif_switch __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572{
1573 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1574 /* FIXME: the following conflict with IEC958 Playback Route */
1575 // .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
Clemens Ladisch10e8d782005-08-03 13:40:08 +02001576 .name = SNDRV_CTL_NAME_IEC958("Output ",NONE,SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 .info = snd_vt1724_spdif_sw_info,
1578 .get = snd_vt1724_spdif_sw_get,
1579 .put = snd_vt1724_spdif_sw_put
1580};
1581
1582
1583#if 0 /* NOT USED YET */
1584/*
1585 * GPIO access from extern
1586 */
1587
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001588#define snd_vt1724_gpio_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001590int snd_vt1724_gpio_get(struct snd_kcontrol *kcontrol,
1591 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001593 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 int shift = kcontrol->private_value & 0xff;
1595 int invert = (kcontrol->private_value & (1<<24)) ? 1 : 0;
1596
1597 snd_ice1712_save_gpio_status(ice);
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001598 ucontrol->value.integer.value[0] =
1599 (snd_ice1712_gpio_read(ice) & (1 << shift) ? 1 : 0) ^ invert;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 snd_ice1712_restore_gpio_status(ice);
1601 return 0;
1602}
1603
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001604int snd_ice1712_gpio_put(struct snd_kcontrol *kcontrol,
1605 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001607 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 int shift = kcontrol->private_value & 0xff;
1609 int invert = (kcontrol->private_value & (1<<24)) ? mask : 0;
1610 unsigned int val, nval;
1611
1612 if (kcontrol->private_value & (1 << 31))
1613 return -EPERM;
1614 nval = (ucontrol->value.integer.value[0] ? (1 << shift) : 0) ^ invert;
1615 snd_ice1712_save_gpio_status(ice);
1616 val = snd_ice1712_gpio_read(ice);
1617 nval |= val & ~(1 << shift);
1618 if (val != nval)
1619 snd_ice1712_gpio_write(ice, nval);
1620 snd_ice1712_restore_gpio_status(ice);
1621 return val != nval;
1622}
1623#endif /* NOT USED YET */
1624
1625/*
1626 * rate
1627 */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001628static int snd_vt1724_pro_internal_clock_info(struct snd_kcontrol *kcontrol,
1629 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001631 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
1633 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1634 uinfo->count = 1;
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001635 uinfo->value.enumerated.items = ice->hw_rates->count + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1637 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001638 if (uinfo->value.enumerated.item == uinfo->value.enumerated.items - 1)
1639 strcpy(uinfo->value.enumerated.name, "IEC958 Input");
1640 else
1641 sprintf(uinfo->value.enumerated.name, "%d",
1642 ice->hw_rates->list[uinfo->value.enumerated.item]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 return 0;
1644}
1645
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001646static int snd_vt1724_pro_internal_clock_get(struct snd_kcontrol *kcontrol,
1647 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001649 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001650 unsigned int i, rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
1652 spin_lock_irq(&ice->reg_lock);
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001653 if (ice->is_spdif_master(ice)) {
1654 ucontrol->value.enumerated.item[0] = ice->hw_rates->count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 } else {
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001656 rate = ice->get_rate(ice);
1657 ucontrol->value.enumerated.item[0] = 0;
1658 for (i = 0; i < ice->hw_rates->count; i++) {
1659 if (ice->hw_rates->list[i] == rate) {
1660 ucontrol->value.enumerated.item[0] = i;
1661 break;
1662 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 }
1665 spin_unlock_irq(&ice->reg_lock);
1666 return 0;
1667}
1668
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001669/* setting clock to external - SPDIF */
1670static void stdclock_set_spdif_clock(struct snd_ice1712 *ice)
1671{
1672 unsigned char oval;
1673 unsigned char i2s_oval;
1674 oval = inb(ICEMT1724(ice, RATE));
1675 outb(oval | VT1724_SPDIF_MASTER, ICEMT1724(ice, RATE));
1676 /* setting 256fs */
1677 i2s_oval = inb(ICEMT1724(ice, I2S_FORMAT));
1678 outb(i2s_oval & ~VT1724_MT_I2S_MCLK_128X, ICEMT1724(ice, I2S_FORMAT));
1679}
1680
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001681static int snd_vt1724_pro_internal_clock_put(struct snd_kcontrol *kcontrol,
1682 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001684 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001685 unsigned int old_rate, new_rate;
1686 unsigned int item = ucontrol->value.enumerated.item[0];
1687 unsigned int spdif = ice->hw_rates->count;
1688
1689 if (item > spdif)
1690 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
1692 spin_lock_irq(&ice->reg_lock);
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001693 if (ice->is_spdif_master(ice))
1694 old_rate = 0;
1695 else
1696 old_rate = ice->get_rate(ice);
1697 if (item == spdif) {
1698 /* switching to external clock via SPDIF */
1699 ice->set_spdif_clock(ice);
1700 new_rate = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 } else {
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001702 /* internal on-card clock */
1703 new_rate = ice->hw_rates->list[item];
1704 ice->pro_rate_default = new_rate;
1705 spin_unlock_irq(&ice->reg_lock);
1706 snd_vt1724_set_pro_rate(ice, ice->pro_rate_default, 1);
1707 spin_lock_irq(&ice->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 spin_unlock_irq(&ice->reg_lock);
1710
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001711 /* the first reset to the SPDIF master mode? */
1712 if (old_rate != new_rate && !new_rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 /* notify akm chips as well */
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001714 unsigned int i;
1715 if (ice->gpio.set_pro_rate)
1716 ice->gpio.set_pro_rate(ice, 0);
1717 for (i = 0; i < ice->akm_codecs; i++) {
1718 if (ice->akm[i].ops.set_rate_val)
1719 ice->akm[i].ops.set_rate_val(&ice->akm[i], 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 }
1721 }
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001722 return old_rate != new_rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723}
1724
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01001725static struct snd_kcontrol_new snd_vt1724_pro_internal_clock __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1727 .name = "Multi Track Internal Clock",
1728 .info = snd_vt1724_pro_internal_clock_info,
1729 .get = snd_vt1724_pro_internal_clock_get,
1730 .put = snd_vt1724_pro_internal_clock_put
1731};
1732
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001733#define snd_vt1724_pro_rate_locking_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001735static int snd_vt1724_pro_rate_locking_get(struct snd_kcontrol *kcontrol,
1736 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737{
1738 ucontrol->value.integer.value[0] = PRO_RATE_LOCKED;
1739 return 0;
1740}
1741
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001742static int snd_vt1724_pro_rate_locking_put(struct snd_kcontrol *kcontrol,
1743 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001745 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 int change = 0, nval;
1747
1748 nval = ucontrol->value.integer.value[0] ? 1 : 0;
1749 spin_lock_irq(&ice->reg_lock);
1750 change = PRO_RATE_LOCKED != nval;
1751 PRO_RATE_LOCKED = nval;
1752 spin_unlock_irq(&ice->reg_lock);
1753 return change;
1754}
1755
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01001756static struct snd_kcontrol_new snd_vt1724_pro_rate_locking __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1758 .name = "Multi Track Rate Locking",
1759 .info = snd_vt1724_pro_rate_locking_info,
1760 .get = snd_vt1724_pro_rate_locking_get,
1761 .put = snd_vt1724_pro_rate_locking_put
1762};
1763
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001764#define snd_vt1724_pro_rate_reset_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001766static int snd_vt1724_pro_rate_reset_get(struct snd_kcontrol *kcontrol,
1767 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768{
1769 ucontrol->value.integer.value[0] = PRO_RATE_RESET ? 1 : 0;
1770 return 0;
1771}
1772
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001773static int snd_vt1724_pro_rate_reset_put(struct snd_kcontrol *kcontrol,
1774 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001776 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 int change = 0, nval;
1778
1779 nval = ucontrol->value.integer.value[0] ? 1 : 0;
1780 spin_lock_irq(&ice->reg_lock);
1781 change = PRO_RATE_RESET != nval;
1782 PRO_RATE_RESET = nval;
1783 spin_unlock_irq(&ice->reg_lock);
1784 return change;
1785}
1786
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01001787static struct snd_kcontrol_new snd_vt1724_pro_rate_reset __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1789 .name = "Multi Track Rate Reset",
1790 .info = snd_vt1724_pro_rate_reset_info,
1791 .get = snd_vt1724_pro_rate_reset_get,
1792 .put = snd_vt1724_pro_rate_reset_put
1793};
1794
1795
1796/*
1797 * routing
1798 */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001799static int snd_vt1724_pro_route_info(struct snd_kcontrol *kcontrol,
1800 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801{
1802 static char *texts[] = {
1803 "PCM Out", /* 0 */
1804 "H/W In 0", "H/W In 1", /* 1-2 */
1805 "IEC958 In L", "IEC958 In R", /* 3-4 */
1806 };
1807
1808 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1809 uinfo->count = 1;
1810 uinfo->value.enumerated.items = 5;
1811 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1812 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
1813 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1814 return 0;
1815}
1816
1817static inline int analog_route_shift(int idx)
1818{
1819 return (idx % 2) * 12 + ((idx / 2) * 3) + 8;
1820}
1821
1822static inline int digital_route_shift(int idx)
1823{
1824 return idx * 3;
1825}
1826
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001827static int get_route_val(struct snd_ice1712 *ice, int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828{
1829 unsigned long val;
1830 unsigned char eitem;
Takashi Iwai32b47da2007-01-29 15:26:36 +01001831 static const unsigned char xlate[8] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 0, 255, 1, 2, 255, 255, 3, 4,
1833 };
1834
1835 val = inl(ICEMT1724(ice, ROUTE_PLAYBACK));
1836 val >>= shift;
1837 val &= 7; //we now have 3 bits per output
1838 eitem = xlate[val];
1839 if (eitem == 255) {
1840 snd_BUG();
1841 return 0;
1842 }
1843 return eitem;
1844}
1845
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001846static int put_route_val(struct snd_ice1712 *ice, unsigned int val, int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847{
1848 unsigned int old_val, nval;
1849 int change;
Takashi Iwai32b47da2007-01-29 15:26:36 +01001850 static const unsigned char xroute[8] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 0, /* PCM */
1852 2, /* PSDIN0 Left */
1853 3, /* PSDIN0 Right */
1854 6, /* SPDIN Left */
1855 7, /* SPDIN Right */
1856 };
1857
1858 nval = xroute[val % 5];
1859 val = old_val = inl(ICEMT1724(ice, ROUTE_PLAYBACK));
1860 val &= ~(0x07 << shift);
1861 val |= nval << shift;
1862 change = val != old_val;
1863 if (change)
1864 outl(val, ICEMT1724(ice, ROUTE_PLAYBACK));
1865 return change;
1866}
1867
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001868static int snd_vt1724_pro_route_analog_get(struct snd_kcontrol *kcontrol,
1869 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001871 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001873 ucontrol->value.enumerated.item[0] =
1874 get_route_val(ice, analog_route_shift(idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 return 0;
1876}
1877
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001878static int snd_vt1724_pro_route_analog_put(struct snd_kcontrol *kcontrol,
1879 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001881 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1883 return put_route_val(ice, ucontrol->value.enumerated.item[0],
1884 analog_route_shift(idx));
1885}
1886
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001887static int snd_vt1724_pro_route_spdif_get(struct snd_kcontrol *kcontrol,
1888 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001890 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001892 ucontrol->value.enumerated.item[0] =
1893 get_route_val(ice, digital_route_shift(idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 return 0;
1895}
1896
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001897static int snd_vt1724_pro_route_spdif_put(struct snd_kcontrol *kcontrol,
1898 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001900 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1902 return put_route_val(ice, ucontrol->value.enumerated.item[0],
1903 digital_route_shift(idx));
1904}
1905
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01001906static struct snd_kcontrol_new snd_vt1724_mixer_pro_analog_route __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1908 .name = "H/W Playback Route",
1909 .info = snd_vt1724_pro_route_info,
1910 .get = snd_vt1724_pro_route_analog_get,
1911 .put = snd_vt1724_pro_route_analog_put,
1912};
1913
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01001914static struct snd_kcontrol_new snd_vt1724_mixer_pro_spdif_route __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1916 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Route",
1917 .info = snd_vt1724_pro_route_info,
1918 .get = snd_vt1724_pro_route_spdif_get,
1919 .put = snd_vt1724_pro_route_spdif_put,
1920 .count = 2,
1921};
1922
1923
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001924static int snd_vt1724_pro_peak_info(struct snd_kcontrol *kcontrol,
1925 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926{
1927 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1928 uinfo->count = 22; /* FIXME: for compatibility with ice1712... */
1929 uinfo->value.integer.min = 0;
1930 uinfo->value.integer.max = 255;
1931 return 0;
1932}
1933
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001934static int snd_vt1724_pro_peak_get(struct snd_kcontrol *kcontrol,
1935 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001937 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 int idx;
1939
1940 spin_lock_irq(&ice->reg_lock);
1941 for (idx = 0; idx < 22; idx++) {
1942 outb(idx, ICEMT1724(ice, MONITOR_PEAKINDEX));
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001943 ucontrol->value.integer.value[idx] =
1944 inb(ICEMT1724(ice, MONITOR_PEAKDATA));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 }
1946 spin_unlock_irq(&ice->reg_lock);
1947 return 0;
1948}
1949
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01001950static struct snd_kcontrol_new snd_vt1724_mixer_pro_peak __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1952 .name = "Multi Track Peak",
1953 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
1954 .info = snd_vt1724_pro_peak_info,
1955 .get = snd_vt1724_pro_peak_get
1956};
1957
1958/*
1959 *
1960 */
1961
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01001962static struct snd_ice1712_card_info no_matched __devinitdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01001964static struct snd_ice1712_card_info *card_tables[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 snd_vt1724_revo_cards,
1966 snd_vt1724_amp_cards,
1967 snd_vt1724_aureon_cards,
1968 snd_vt1720_mobo_cards,
1969 snd_vt1720_pontis_cards,
Julian Scheel6b8d6e52008-01-16 19:50:00 +01001970 snd_vt1724_prodigy_hifi_cards,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 snd_vt1724_prodigy192_cards,
1972 snd_vt1724_juli_cards,
1973 snd_vt1724_phase_cards,
Clement Guedezf6cdab52007-01-08 10:48:41 +01001974 snd_vt1724_wtm_cards,
Shin-ya Okadaf31639b2007-10-23 15:08:18 +02001975 snd_vt1724_se_cards,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 NULL,
1977};
1978
1979
1980/*
1981 */
1982
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001983static void wait_i2c_busy(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984{
1985 int t = 0x10000;
1986 while ((inb(ICEREG1724(ice, I2C_CTRL)) & VT1724_I2C_BUSY) && t--)
1987 ;
1988 if (t == -1)
1989 printk(KERN_ERR "ice1724: i2c busy timeout\n");
1990}
1991
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001992unsigned char snd_vt1724_read_i2c(struct snd_ice1712 *ice,
1993 unsigned char dev, unsigned char addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994{
1995 unsigned char val;
1996
Ingo Molnar62932df2006-01-16 16:34:20 +01001997 mutex_lock(&ice->i2c_mutex);
Pavel Hofmanacec30f2007-12-03 12:37:17 +01001998 wait_i2c_busy(ice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 outb(addr, ICEREG1724(ice, I2C_BYTE_ADDR));
2000 outb(dev & ~VT1724_I2C_WRITE, ICEREG1724(ice, I2C_DEV_ADDR));
2001 wait_i2c_busy(ice);
2002 val = inb(ICEREG1724(ice, I2C_DATA));
Ingo Molnar62932df2006-01-16 16:34:20 +01002003 mutex_unlock(&ice->i2c_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 //printk("i2c_read: [0x%x,0x%x] = 0x%x\n", dev, addr, val);
2005 return val;
2006}
2007
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002008void snd_vt1724_write_i2c(struct snd_ice1712 *ice,
2009 unsigned char dev, unsigned char addr, unsigned char data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010{
Ingo Molnar62932df2006-01-16 16:34:20 +01002011 mutex_lock(&ice->i2c_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 wait_i2c_busy(ice);
2013 //printk("i2c_write: [0x%x,0x%x] = 0x%x\n", dev, addr, data);
2014 outb(addr, ICEREG1724(ice, I2C_BYTE_ADDR));
2015 outb(data, ICEREG1724(ice, I2C_DATA));
2016 outb(dev | VT1724_I2C_WRITE, ICEREG1724(ice, I2C_DEV_ADDR));
2017 wait_i2c_busy(ice);
Ingo Molnar62932df2006-01-16 16:34:20 +01002018 mutex_unlock(&ice->i2c_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019}
2020
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002021static int __devinit snd_vt1724_read_eeprom(struct snd_ice1712 *ice,
2022 const char *modelname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023{
2024 const int dev = 0xa0; /* EEPROM device address */
2025 unsigned int i, size;
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01002026 struct snd_ice1712_card_info * const *tbl, *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
2028 if (! modelname || ! *modelname) {
2029 ice->eeprom.subvendor = 0;
2030 if ((inb(ICEREG1724(ice, I2C_CTRL)) & VT1724_I2C_EEPROM) != 0)
2031 ice->eeprom.subvendor =
2032 (snd_vt1724_read_i2c(ice, dev, 0x00) << 0) |
2033 (snd_vt1724_read_i2c(ice, dev, 0x01) << 8) |
2034 (snd_vt1724_read_i2c(ice, dev, 0x02) << 16) |
2035 (snd_vt1724_read_i2c(ice, dev, 0x03) << 24);
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002036 if (ice->eeprom.subvendor == 0 ||
2037 ice->eeprom.subvendor == (unsigned int)-1) {
2038 /* invalid subvendor from EEPROM, try the PCI
2039 * subststem ID instead
2040 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 u16 vendor, device;
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002042 pci_read_config_word(ice->pci, PCI_SUBSYSTEM_VENDOR_ID,
2043 &vendor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 pci_read_config_word(ice->pci, PCI_SUBSYSTEM_ID, &device);
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002045 ice->eeprom.subvendor =
2046 ((unsigned int)swab16(vendor) << 16) | swab16(device);
2047 if (ice->eeprom.subvendor == 0 ||
2048 ice->eeprom.subvendor == (unsigned int)-1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 printk(KERN_ERR "ice1724: No valid ID is found\n");
2050 return -ENXIO;
2051 }
2052 }
2053 }
2054 for (tbl = card_tables; *tbl; tbl++) {
2055 for (c = *tbl; c->subvendor; c++) {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002056 if (modelname && c->model &&
2057 ! strcmp(modelname, c->model)) {
2058 printk(KERN_INFO "ice1724: Using board model %s\n",
2059 c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 ice->eeprom.subvendor = c->subvendor;
2061 } else if (c->subvendor != ice->eeprom.subvendor)
2062 continue;
2063 if (! c->eeprom_size || ! c->eeprom_data)
2064 goto found;
2065 /* if the EEPROM is given by the driver, use it */
2066 snd_printdd("using the defined eeprom..\n");
2067 ice->eeprom.version = 2;
2068 ice->eeprom.size = c->eeprom_size + 6;
2069 memcpy(ice->eeprom.data, c->eeprom_data, c->eeprom_size);
2070 goto read_skipped;
2071 }
2072 }
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002073 printk(KERN_WARNING "ice1724: No matching model found for ID 0x%x\n",
2074 ice->eeprom.subvendor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
2076 found:
2077 ice->eeprom.size = snd_vt1724_read_i2c(ice, dev, 0x04);
2078 if (ice->eeprom.size < 6)
2079 ice->eeprom.size = 32;
2080 else if (ice->eeprom.size > 32) {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002081 printk(KERN_ERR "ice1724: Invalid EEPROM (size = %i)\n",
2082 ice->eeprom.size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 return -EIO;
2084 }
2085 ice->eeprom.version = snd_vt1724_read_i2c(ice, dev, 0x05);
2086 if (ice->eeprom.version != 2)
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002087 printk(KERN_WARNING "ice1724: Invalid EEPROM version %i\n",
2088 ice->eeprom.version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 size = ice->eeprom.size - 6;
2090 for (i = 0; i < size; i++)
2091 ice->eeprom.data[i] = snd_vt1724_read_i2c(ice, dev, i + 6);
2092
2093 read_skipped:
2094 ice->eeprom.gpiomask = eeprom_triple(ice, ICE_EEP2_GPIO_MASK);
2095 ice->eeprom.gpiostate = eeprom_triple(ice, ICE_EEP2_GPIO_STATE);
2096 ice->eeprom.gpiodir = eeprom_triple(ice, ICE_EEP2_GPIO_DIR);
2097
2098 return 0;
2099}
2100
2101
2102
Karsten Wiese988f0662008-04-22 12:52:15 +02002103static void __devinit snd_vt1724_chip_reset(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104{
2105 outb(VT1724_RESET , ICEREG1724(ice, CONTROL));
Karsten Wiese988f0662008-04-22 12:52:15 +02002106 msleep(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 outb(0, ICEREG1724(ice, CONTROL));
Karsten Wiese988f0662008-04-22 12:52:15 +02002108 msleep(10);
2109}
2110
2111static int __devinit snd_vt1724_chip_init(struct snd_ice1712 *ice)
2112{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 outb(ice->eeprom.data[ICE_EEP2_SYSCONF], ICEREG1724(ice, SYS_CFG));
2114 outb(ice->eeprom.data[ICE_EEP2_ACLINK], ICEREG1724(ice, AC97_CFG));
2115 outb(ice->eeprom.data[ICE_EEP2_I2S], ICEREG1724(ice, I2S_FEATURES));
2116 outb(ice->eeprom.data[ICE_EEP2_SPDIF], ICEREG1724(ice, SPDIF_CFG));
2117
2118 ice->gpio.write_mask = ice->eeprom.gpiomask;
2119 ice->gpio.direction = ice->eeprom.gpiodir;
2120 snd_vt1724_set_gpio_mask(ice, ice->eeprom.gpiomask);
2121 snd_vt1724_set_gpio_dir(ice, ice->eeprom.gpiodir);
2122 snd_vt1724_set_gpio_data(ice, ice->eeprom.gpiostate);
2123
2124 outb(0, ICEREG1724(ice, POWERDOWN));
2125
2126 return 0;
2127}
2128
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002129static int __devinit snd_vt1724_spdif_build_controls(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130{
2131 int err;
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002132 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
2134 snd_assert(ice->pcm != NULL, return -EIO);
2135
2136 err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_mixer_pro_spdif_route, ice));
2137 if (err < 0)
2138 return err;
2139
2140 err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_spdif_switch, ice));
2141 if (err < 0)
2142 return err;
2143
2144 err = snd_ctl_add(ice->card, kctl = snd_ctl_new1(&snd_vt1724_spdif_default, ice));
2145 if (err < 0)
2146 return err;
2147 kctl->id.device = ice->pcm->device;
2148 err = snd_ctl_add(ice->card, kctl = snd_ctl_new1(&snd_vt1724_spdif_maskc, ice));
2149 if (err < 0)
2150 return err;
2151 kctl->id.device = ice->pcm->device;
2152 err = snd_ctl_add(ice->card, kctl = snd_ctl_new1(&snd_vt1724_spdif_maskp, ice));
2153 if (err < 0)
2154 return err;
2155 kctl->id.device = ice->pcm->device;
2156#if 0 /* use default only */
2157 err = snd_ctl_add(ice->card, kctl = snd_ctl_new1(&snd_vt1724_spdif_stream, ice));
2158 if (err < 0)
2159 return err;
2160 kctl->id.device = ice->pcm->device;
2161 ice->spdif.stream_ctl = kctl;
2162#endif
2163 return 0;
2164}
2165
2166
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002167static int __devinit snd_vt1724_build_controls(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168{
2169 int err;
2170
2171 err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_eeprom, ice));
2172 if (err < 0)
2173 return err;
2174 err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_pro_internal_clock, ice));
2175 if (err < 0)
2176 return err;
2177
2178 err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_pro_rate_locking, ice));
2179 if (err < 0)
2180 return err;
2181 err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_pro_rate_reset, ice));
2182 if (err < 0)
2183 return err;
2184
2185 if (ice->num_total_dacs > 0) {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002186 struct snd_kcontrol_new tmp = snd_vt1724_mixer_pro_analog_route;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 tmp.count = ice->num_total_dacs;
2188 if (ice->vt1720 && tmp.count > 2)
2189 tmp.count = 2;
2190 err = snd_ctl_add(ice->card, snd_ctl_new1(&tmp, ice));
2191 if (err < 0)
2192 return err;
2193 }
2194
2195 err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_mixer_pro_peak, ice));
2196 if (err < 0)
2197 return err;
2198
2199 return 0;
2200}
2201
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002202static int snd_vt1724_free(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203{
2204 if (! ice->port)
2205 goto __hw_end;
2206 /* mask all interrupts */
2207 outb(0xff, ICEMT1724(ice, DMA_INT_MASK));
2208 outb(0xff, ICEREG1724(ice, IRQMASK));
2209 /* --- */
2210 __hw_end:
Jeff Garzikf000fd82008-04-22 13:50:34 +02002211 if (ice->irq >= 0)
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002212 free_irq(ice->irq, ice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 pci_release_regions(ice->pci);
2214 snd_ice1712_akm4xxx_free(ice);
2215 pci_disable_device(ice->pci);
Takashi Iwai7cda8ba2008-01-18 13:36:07 +01002216 kfree(ice->spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 kfree(ice);
2218 return 0;
2219}
2220
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002221static int snd_vt1724_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002223 struct snd_ice1712 *ice = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 return snd_vt1724_free(ice);
2225}
2226
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002227static int __devinit snd_vt1724_create(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 struct pci_dev *pci,
2229 const char *modelname,
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002230 struct snd_ice1712 ** r_ice1712)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002232 struct snd_ice1712 *ice;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 int err;
2234 unsigned char mask;
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002235 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 .dev_free = snd_vt1724_dev_free,
2237 };
2238
2239 *r_ice1712 = NULL;
2240
2241 /* enable PCI device */
2242 if ((err = pci_enable_device(pci)) < 0)
2243 return err;
2244
Takashi Iwaie560d8d2005-09-09 14:21:46 +02002245 ice = kzalloc(sizeof(*ice), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 if (ice == NULL) {
2247 pci_disable_device(pci);
2248 return -ENOMEM;
2249 }
2250 ice->vt1724 = 1;
2251 spin_lock_init(&ice->reg_lock);
Ingo Molnar62932df2006-01-16 16:34:20 +01002252 mutex_init(&ice->gpio_mutex);
2253 mutex_init(&ice->open_mutex);
2254 mutex_init(&ice->i2c_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 ice->gpio.set_mask = snd_vt1724_set_gpio_mask;
2256 ice->gpio.set_dir = snd_vt1724_set_gpio_dir;
2257 ice->gpio.set_data = snd_vt1724_set_gpio_data;
2258 ice->gpio.get_data = snd_vt1724_get_gpio_data;
2259 ice->card = card;
2260 ice->pci = pci;
2261 ice->irq = -1;
2262 pci_set_master(pci);
2263 snd_vt1724_proc_init(ice);
2264 synchronize_irq(pci->irq);
2265
2266 if ((err = pci_request_regions(pci, "ICE1724")) < 0) {
2267 kfree(ice);
2268 pci_disable_device(pci);
2269 return err;
2270 }
2271 ice->port = pci_resource_start(pci, 0);
2272 ice->profi_port = pci_resource_start(pci, 1);
2273
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002274 if (request_irq(pci->irq, snd_vt1724_interrupt,
Takashi Iwai437a5a42006-11-21 12:14:23 +01002275 IRQF_SHARED, "ICE1724", ice)) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02002276 snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 snd_vt1724_free(ice);
2278 return -EIO;
2279 }
2280
2281 ice->irq = pci->irq;
2282
Karsten Wiese988f0662008-04-22 12:52:15 +02002283 snd_vt1724_chip_reset(ice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 if (snd_vt1724_read_eeprom(ice, modelname) < 0) {
2285 snd_vt1724_free(ice);
2286 return -EIO;
2287 }
2288 if (snd_vt1724_chip_init(ice) < 0) {
2289 snd_vt1724_free(ice);
2290 return -EIO;
2291 }
2292
2293 /* unmask used interrupts */
Takashi Iwai3a841d52008-04-23 17:47:28 +02002294 mask = VT1724_IRQ_MPU_RX | VT1724_IRQ_MPU_TX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 outb(mask, ICEREG1724(ice, IRQMASK));
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002296 /* don't handle FIFO overrun/underruns (just yet),
2297 * since they cause machine lockups
2298 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 outb(VT1724_MULTI_FIFO_ERR, ICEMT1724(ice, DMA_INT_MASK));
2300
2301 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, ice, &ops)) < 0) {
2302 snd_vt1724_free(ice);
2303 return err;
2304 }
2305
2306 snd_card_set_dev(card, &pci->dev);
2307
2308 *r_ice1712 = ice;
2309 return 0;
2310}
2311
2312
2313/*
2314 *
2315 * Registration
2316 *
2317 */
2318
2319static int __devinit snd_vt1724_probe(struct pci_dev *pci,
2320 const struct pci_device_id *pci_id)
2321{
2322 static int dev;
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002323 struct snd_card *card;
2324 struct snd_ice1712 *ice;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 int pcm_dev = 0, err;
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01002326 struct snd_ice1712_card_info * const *tbl, *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
2328 if (dev >= SNDRV_CARDS)
2329 return -ENODEV;
2330 if (!enable[dev]) {
2331 dev++;
2332 return -ENOENT;
2333 }
2334
2335 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
2336 if (card == NULL)
2337 return -ENOMEM;
2338
2339 strcpy(card->driver, "ICE1724");
2340 strcpy(card->shortname, "ICEnsemble ICE1724");
2341
2342 if ((err = snd_vt1724_create(card, pci, model[dev], &ice)) < 0) {
2343 snd_card_free(card);
2344 return err;
2345 }
2346
2347 for (tbl = card_tables; *tbl; tbl++) {
2348 for (c = *tbl; c->subvendor; c++) {
2349 if (c->subvendor == ice->eeprom.subvendor) {
2350 strcpy(card->shortname, c->name);
2351 if (c->driver) /* specific driver? */
2352 strcpy(card->driver, c->driver);
2353 if (c->chip_init) {
2354 if ((err = c->chip_init(ice)) < 0) {
2355 snd_card_free(card);
2356 return err;
2357 }
2358 }
2359 goto __found;
2360 }
2361 }
2362 }
2363 c = &no_matched;
2364 __found:
Takashi Iwaif06934b2007-04-10 11:15:34 +02002365 /*
2366 * VT1724 has separate DMAs for the analog and the SPDIF streams while
2367 * ICE1712 has only one for both (mixed up).
2368 *
2369 * Confusingly the analog PCM is named "professional" here because it
2370 * was called so in ice1712 driver, and vt1724 driver is derived from
2371 * ice1712 driver.
2372 */
Pavel Hofmand16be8e2008-03-20 12:10:27 +01002373 ice->pro_rate_default = PRO_RATE_DEFAULT;
2374 if (!ice->is_spdif_master)
2375 ice->is_spdif_master = stdclock_is_spdif_master;
2376 if (!ice->get_rate)
2377 ice->get_rate = stdclock_get_rate;
2378 if (!ice->set_rate)
2379 ice->set_rate = stdclock_set_rate;
2380 if (!ice->set_mclk)
2381 ice->set_mclk = stdclock_set_mclk;
2382 if (!ice->set_spdif_clock)
2383 ice->set_spdif_clock = stdclock_set_spdif_clock;
2384 if (!ice->hw_rates)
2385 set_std_hw_rates(ice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386
2387 if ((err = snd_vt1724_pcm_profi(ice, pcm_dev++)) < 0) {
2388 snd_card_free(card);
2389 return err;
2390 }
2391
2392 if ((err = snd_vt1724_pcm_spdif(ice, pcm_dev++)) < 0) {
2393 snd_card_free(card);
2394 return err;
2395 }
2396
2397 if ((err = snd_vt1724_pcm_indep(ice, pcm_dev++)) < 0) {
2398 snd_card_free(card);
2399 return err;
2400 }
2401
2402 if ((err = snd_vt1724_ac97_mixer(ice)) < 0) {
2403 snd_card_free(card);
2404 return err;
2405 }
2406
2407 if ((err = snd_vt1724_build_controls(ice)) < 0) {
2408 snd_card_free(card);
2409 return err;
2410 }
2411
2412 if (ice->pcm && ice->has_spdif) { /* has SPDIF I/O */
2413 if ((err = snd_vt1724_spdif_build_controls(ice)) < 0) {
2414 snd_card_free(card);
2415 return err;
2416 }
2417 }
2418
2419 if (c->build_controls) {
2420 if ((err = c->build_controls(ice)) < 0) {
2421 snd_card_free(card);
2422 return err;
2423 }
2424 }
2425
2426 if (! c->no_mpu401) {
2427 if (ice->eeprom.data[ICE_EEP2_SYSCONF] & VT1724_CFG_MPU401) {
Takashi Iwai3a841d52008-04-23 17:47:28 +02002428 struct snd_mpu401 *mpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_ICE1712,
Takashi Iwai302e4c22006-05-23 13:24:30 +02002430 ICEREG1724(ice, MPU_CTRL),
Takashi Iwai3a841d52008-04-23 17:47:28 +02002431 (MPU401_INFO_INTEGRATED |
2432 MPU401_INFO_TX_IRQ),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 ice->irq, 0,
2434 &ice->rmidi[0])) < 0) {
2435 snd_card_free(card);
2436 return err;
2437 }
Takashi Iwai3a841d52008-04-23 17:47:28 +02002438 mpu = ice->rmidi[0]->private_data;
2439 mpu->read = snd_vt1724_mpu401_read;
2440 mpu->write = snd_vt1724_mpu401_write;
2441 /* unmask MPU RX/TX irqs */
2442 outb(inb(ICEREG1724(ice, IRQMASK)) &
2443 ~(VT1724_IRQ_MPU_RX | VT1724_IRQ_MPU_TX),
2444 ICEREG1724(ice, IRQMASK));
2445#if 0 /* for testing */
2446 /* set watermarks */
2447 outb(VT1724_MPU_RX_FIFO | 0x1,
2448 ICEREG1724(ice, MPU_FIFO_WM));
2449 outb(0x1, ICEREG1724(ice, MPU_FIFO_WM));
2450#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 }
2452 }
2453
2454 sprintf(card->longname, "%s at 0x%lx, irq %i",
2455 card->shortname, ice->port, ice->irq);
2456
2457 if ((err = snd_card_register(card)) < 0) {
2458 snd_card_free(card);
2459 return err;
2460 }
2461 pci_set_drvdata(pci, card);
2462 dev++;
2463 return 0;
2464}
2465
2466static void __devexit snd_vt1724_remove(struct pci_dev *pci)
2467{
2468 snd_card_free(pci_get_drvdata(pci));
2469 pci_set_drvdata(pci, NULL);
2470}
2471
2472static struct pci_driver driver = {
2473 .name = "ICE1724",
2474 .id_table = snd_vt1724_ids,
2475 .probe = snd_vt1724_probe,
2476 .remove = __devexit_p(snd_vt1724_remove),
2477};
2478
2479static int __init alsa_card_ice1724_init(void)
2480{
Takashi Iwai01d25d42005-04-11 16:58:24 +02002481 return pci_register_driver(&driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482}
2483
2484static void __exit alsa_card_ice1724_exit(void)
2485{
2486 pci_unregister_driver(&driver);
2487}
2488
2489module_init(alsa_card_ice1724_init)
2490module_exit(alsa_card_ice1724_exit)