blob: 2213beec009a3760b5f57567d8448cb6a76847d9 [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 *
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +040023 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +040025#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#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>
Clemens Ladischaea3bfb2008-05-20 14:22:44 +020035#include <sound/rawmidi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#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"
Takashi Iwai72cbfd42009-05-06 17:33:19 +020052#include "maya44.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include "phase.h"
Clement Guedezf6cdab52007-01-08 10:48:41 +010054#include "wtm.h"
Shin-ya Okadaf31639b2007-10-23 15:08:18 +020055#include "se.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Jaroslav Kyselac1017a42007-10-15 09:50:19 +020057MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070058MODULE_DESCRIPTION("VIA ICEnsemble ICE1724/1720 (Envy24HT/PT)");
59MODULE_LICENSE("GPL");
60MODULE_SUPPORTED_DEVICE("{"
61 REVO_DEVICE_DESC
62 AMP_AUDIO2000_DEVICE_DESC
63 AUREON_DEVICE_DESC
64 VT1720_MOBO_DEVICE_DESC
65 PONTIS_DEVICE_DESC
66 PRODIGY192_DEVICE_DESC
Julian Scheel6b8d6e52008-01-16 19:50:00 +010067 PRODIGY_HIFI_DEVICE_DESC
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 JULI_DEVICE_DESC
Takashi Iwai72cbfd42009-05-06 17:33:19 +020069 MAYA44_DEVICE_DESC
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 PHASE_DEVICE_DESC
Clement Guedezf6cdab52007-01-08 10:48:41 +010071 WTM_DEVICE_DESC
Shin-ya Okadaf31639b2007-10-23 15:08:18 +020072 SE_DEVICE_DESC
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 "{VIA,VT1720},"
74 "{VIA,VT1724},"
75 "{ICEnsemble,Generic ICE1724},"
76 "{ICEnsemble,Generic Envy24HT}"
77 "{ICEnsemble,Generic Envy24PT}}");
78
79static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
80static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
81static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
82static char *model[SNDRV_CARDS];
83
84module_param_array(index, int, NULL, 0444);
85MODULE_PARM_DESC(index, "Index value for ICE1724 soundcard.");
86module_param_array(id, charp, NULL, 0444);
87MODULE_PARM_DESC(id, "ID string for ICE1724 soundcard.");
88module_param_array(enable, bool, NULL, 0444);
89MODULE_PARM_DESC(enable, "Enable ICE1724 soundcard.");
90module_param_array(model, charp, NULL, 0444);
91MODULE_PARM_DESC(model, "Use the given board model.");
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94/* Both VT1720 and VT1724 have the same PCI IDs */
Takashi Iwai32b47da2007-01-29 15:26:36 +010095static const struct pci_device_id snd_vt1724_ids[] = {
Joe Perches28d27aa2009-06-24 22:13:35 -070096 { PCI_VDEVICE(ICE, PCI_DEVICE_ID_VT1724), 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 { 0, }
98};
99
100MODULE_DEVICE_TABLE(pci, snd_vt1724_ids);
101
102
103static int PRO_RATE_LOCKED;
104static int PRO_RATE_RESET = 1;
105static unsigned int PRO_RATE_DEFAULT = 44100;
106
107/*
108 * Basic I/O
109 */
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +0400110
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100111/*
112 * default rates, default clock routines
113 */
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115/* check whether the clock mode is spdif-in */
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100116static inline int stdclock_is_spdif_master(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
118 return (inb(ICEMT1724(ice, RATE)) & VT1724_SPDIF_MASTER) ? 1 : 0;
119}
120
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100121static inline int is_pro_rate_locked(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100123 return ice->is_spdif_master(ice) || PRO_RATE_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
125
126/*
127 * ac97 section
128 */
129
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100130static unsigned char snd_vt1724_ac97_ready(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
132 unsigned char old_cmd;
133 int tm;
134 for (tm = 0; tm < 0x10000; tm++) {
135 old_cmd = inb(ICEMT1724(ice, AC97_CMD));
136 if (old_cmd & (VT1724_AC97_WRITE | VT1724_AC97_READ))
137 continue;
138 if (!(old_cmd & VT1724_AC97_READY))
139 continue;
140 return old_cmd;
141 }
142 snd_printd(KERN_ERR "snd_vt1724_ac97_ready: timeout\n");
143 return old_cmd;
144}
145
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100146static int snd_vt1724_ac97_wait_bit(struct snd_ice1712 *ice, unsigned char bit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
148 int tm;
149 for (tm = 0; tm < 0x10000; tm++)
150 if ((inb(ICEMT1724(ice, AC97_CMD)) & bit) == 0)
151 return 0;
152 snd_printd(KERN_ERR "snd_vt1724_ac97_wait_bit: timeout\n");
153 return -EIO;
154}
155
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100156static void snd_vt1724_ac97_write(struct snd_ac97 *ac97,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 unsigned short reg,
158 unsigned short val)
159{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100160 struct snd_ice1712 *ice = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 unsigned char old_cmd;
162
163 old_cmd = snd_vt1724_ac97_ready(ice);
164 old_cmd &= ~VT1724_AC97_ID_MASK;
165 old_cmd |= ac97->num;
166 outb(reg, ICEMT1724(ice, AC97_INDEX));
167 outw(val, ICEMT1724(ice, AC97_DATA));
168 outb(old_cmd | VT1724_AC97_WRITE, ICEMT1724(ice, AC97_CMD));
169 snd_vt1724_ac97_wait_bit(ice, VT1724_AC97_WRITE);
170}
171
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100172static unsigned short snd_vt1724_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100174 struct snd_ice1712 *ice = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 unsigned char old_cmd;
176
177 old_cmd = snd_vt1724_ac97_ready(ice);
178 old_cmd &= ~VT1724_AC97_ID_MASK;
179 old_cmd |= ac97->num;
180 outb(reg, ICEMT1724(ice, AC97_INDEX));
181 outb(old_cmd | VT1724_AC97_READ, ICEMT1724(ice, AC97_CMD));
182 if (snd_vt1724_ac97_wait_bit(ice, VT1724_AC97_READ) < 0)
183 return ~0;
184 return inw(ICEMT1724(ice, AC97_DATA));
185}
186
187
188/*
189 * GPIO operations
190 */
191
192/* set gpio direction 0 = read, 1 = write */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100193static void snd_vt1724_set_gpio_dir(struct snd_ice1712 *ice, unsigned int data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
195 outl(data, ICEREG1724(ice, GPIO_DIRECTION));
196 inw(ICEREG1724(ice, GPIO_DIRECTION)); /* dummy read for pci-posting */
197}
198
Pavel Hofman49470302009-09-16 22:25:38 +0200199/* get gpio direction 0 = read, 1 = write */
200static unsigned int snd_vt1724_get_gpio_dir(struct snd_ice1712 *ice)
201{
202 return inl(ICEREG1724(ice, GPIO_DIRECTION));
203}
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205/* set the gpio mask (0 = writable) */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100206static void snd_vt1724_set_gpio_mask(struct snd_ice1712 *ice, unsigned int data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
208 outw(data, ICEREG1724(ice, GPIO_WRITE_MASK));
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +0400209 if (!ice->vt1720) /* VT1720 supports only 16 GPIO bits */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 outb((data >> 16) & 0xff, ICEREG1724(ice, GPIO_WRITE_MASK_22));
211 inw(ICEREG1724(ice, GPIO_WRITE_MASK)); /* dummy read for pci-posting */
212}
213
Pavel Hofman49470302009-09-16 22:25:38 +0200214static unsigned int snd_vt1724_get_gpio_mask(struct snd_ice1712 *ice)
215{
216 unsigned int mask;
217 if (!ice->vt1720)
218 mask = (unsigned int)inb(ICEREG1724(ice, GPIO_WRITE_MASK_22));
219 else
220 mask = 0;
221 mask = (mask << 16) | inw(ICEREG1724(ice, GPIO_WRITE_MASK));
222 return mask;
223}
224
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100225static void snd_vt1724_set_gpio_data(struct snd_ice1712 *ice, unsigned int data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
227 outw(data, ICEREG1724(ice, GPIO_DATA));
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +0400228 if (!ice->vt1720)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 outb(data >> 16, ICEREG1724(ice, GPIO_DATA_22));
230 inw(ICEREG1724(ice, GPIO_DATA)); /* dummy read for pci-posting */
231}
232
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100233static unsigned int snd_vt1724_get_gpio_data(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
235 unsigned int data;
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +0400236 if (!ice->vt1720)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 data = (unsigned int)inb(ICEREG1724(ice, GPIO_DATA_22));
238 else
239 data = 0;
240 data = (data << 16) | inw(ICEREG1724(ice, GPIO_DATA));
241 return data;
242}
243
244/*
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200245 * MIDI
Takashi Iwai3a841d52008-04-23 17:47:28 +0200246 */
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200247
248static void vt1724_midi_clear_rx(struct snd_ice1712 *ice)
Takashi Iwai3a841d52008-04-23 17:47:28 +0200249{
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200250 unsigned int count;
251
252 for (count = inb(ICEREG1724(ice, MPU_RXFIFO)); count > 0; --count)
253 inb(ICEREG1724(ice, MPU_DATA));
Takashi Iwai3a841d52008-04-23 17:47:28 +0200254}
255
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200256static inline struct snd_rawmidi_substream *
257get_rawmidi_substream(struct snd_ice1712 *ice, unsigned int stream)
Takashi Iwai3a841d52008-04-23 17:47:28 +0200258{
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200259 return list_first_entry(&ice->rmidi[0]->streams[stream].substreams,
260 struct snd_rawmidi_substream, list);
Takashi Iwai3a841d52008-04-23 17:47:28 +0200261}
262
Takashi Iwaie683ec42008-11-12 16:42:44 +0100263static void enable_midi_irq(struct snd_ice1712 *ice, u8 flag, int enable);
264
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200265static void vt1724_midi_write(struct snd_ice1712 *ice)
266{
267 struct snd_rawmidi_substream *s;
268 int count, i;
269 u8 buffer[32];
270
271 s = get_rawmidi_substream(ice, SNDRV_RAWMIDI_STREAM_OUTPUT);
272 count = 31 - inb(ICEREG1724(ice, MPU_TXFIFO));
273 if (count > 0) {
274 count = snd_rawmidi_transmit(s, buffer, count);
275 for (i = 0; i < count; ++i)
276 outb(buffer[i], ICEREG1724(ice, MPU_DATA));
277 }
Takashi Iwaie683ec42008-11-12 16:42:44 +0100278 /* mask irq when all bytes have been transmitted.
279 * enabled again in output_trigger when the new data comes in.
280 */
281 enable_midi_irq(ice, VT1724_IRQ_MPU_TX,
282 !snd_rawmidi_transmit_empty(s));
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200283}
284
285static void vt1724_midi_read(struct snd_ice1712 *ice)
286{
287 struct snd_rawmidi_substream *s;
288 int count, i;
289 u8 buffer[32];
290
291 s = get_rawmidi_substream(ice, SNDRV_RAWMIDI_STREAM_INPUT);
292 count = inb(ICEREG1724(ice, MPU_RXFIFO));
293 if (count > 0) {
294 count = min(count, 32);
295 for (i = 0; i < count; ++i)
296 buffer[i] = inb(ICEREG1724(ice, MPU_DATA));
297 snd_rawmidi_receive(s, buffer, count);
298 }
299}
300
Takashi Iwaie683ec42008-11-12 16:42:44 +0100301/* call with ice->reg_lock */
302static void enable_midi_irq(struct snd_ice1712 *ice, u8 flag, int enable)
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200303{
Takashi Iwaie683ec42008-11-12 16:42:44 +0100304 u8 mask = inb(ICEREG1724(ice, IRQMASK));
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200305 if (enable)
306 mask &= ~flag;
307 else
308 mask |= flag;
309 outb(mask, ICEREG1724(ice, IRQMASK));
Takashi Iwaie683ec42008-11-12 16:42:44 +0100310}
311
312static void vt1724_enable_midi_irq(struct snd_rawmidi_substream *substream,
313 u8 flag, int enable)
314{
315 struct snd_ice1712 *ice = substream->rmidi->private_data;
316
317 spin_lock_irq(&ice->reg_lock);
318 enable_midi_irq(ice, flag, enable);
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200319 spin_unlock_irq(&ice->reg_lock);
320}
321
322static int vt1724_midi_output_open(struct snd_rawmidi_substream *s)
323{
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200324 return 0;
325}
326
327static int vt1724_midi_output_close(struct snd_rawmidi_substream *s)
328{
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200329 return 0;
330}
331
332static void vt1724_midi_output_trigger(struct snd_rawmidi_substream *s, int up)
333{
334 struct snd_ice1712 *ice = s->rmidi->private_data;
335 unsigned long flags;
336
337 spin_lock_irqsave(&ice->reg_lock, flags);
338 if (up) {
339 ice->midi_output = 1;
340 vt1724_midi_write(ice);
341 } else {
342 ice->midi_output = 0;
Takashi Iwaie683ec42008-11-12 16:42:44 +0100343 enable_midi_irq(ice, VT1724_IRQ_MPU_TX, 0);
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200344 }
345 spin_unlock_irqrestore(&ice->reg_lock, flags);
346}
347
348static void vt1724_midi_output_drain(struct snd_rawmidi_substream *s)
349{
350 struct snd_ice1712 *ice = s->rmidi->private_data;
351 unsigned long timeout;
352
Takashi Iwaie683ec42008-11-12 16:42:44 +0100353 vt1724_enable_midi_irq(s, VT1724_IRQ_MPU_TX, 0);
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200354 /* 32 bytes should be transmitted in less than about 12 ms */
355 timeout = jiffies + msecs_to_jiffies(15);
356 do {
357 if (inb(ICEREG1724(ice, MPU_CTRL)) & VT1724_MPU_TX_EMPTY)
358 break;
359 schedule_timeout_uninterruptible(1);
360 } while (time_after(timeout, jiffies));
361}
362
363static struct snd_rawmidi_ops vt1724_midi_output_ops = {
364 .open = vt1724_midi_output_open,
365 .close = vt1724_midi_output_close,
366 .trigger = vt1724_midi_output_trigger,
367 .drain = vt1724_midi_output_drain,
368};
369
370static int vt1724_midi_input_open(struct snd_rawmidi_substream *s)
371{
372 vt1724_midi_clear_rx(s->rmidi->private_data);
373 vt1724_enable_midi_irq(s, VT1724_IRQ_MPU_RX, 1);
374 return 0;
375}
376
377static int vt1724_midi_input_close(struct snd_rawmidi_substream *s)
378{
379 vt1724_enable_midi_irq(s, VT1724_IRQ_MPU_RX, 0);
380 return 0;
381}
382
383static void vt1724_midi_input_trigger(struct snd_rawmidi_substream *s, int up)
384{
385 struct snd_ice1712 *ice = s->rmidi->private_data;
386 unsigned long flags;
387
388 spin_lock_irqsave(&ice->reg_lock, flags);
389 if (up) {
390 ice->midi_input = 1;
391 vt1724_midi_read(ice);
392 } else {
393 ice->midi_input = 0;
394 }
395 spin_unlock_irqrestore(&ice->reg_lock, flags);
396}
397
398static struct snd_rawmidi_ops vt1724_midi_input_ops = {
399 .open = vt1724_midi_input_open,
400 .close = vt1724_midi_input_close,
401 .trigger = vt1724_midi_input_trigger,
402};
403
Takashi Iwai3a841d52008-04-23 17:47:28 +0200404
405/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 * Interrupt handler
407 */
408
David Howells7d12e782006-10-05 14:55:46 +0100409static irqreturn_t snd_vt1724_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100411 struct snd_ice1712 *ice = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 unsigned char status;
Takashi Iwai3a841d52008-04-23 17:47:28 +0200413 unsigned char status_mask =
414 VT1724_IRQ_MPU_RX | VT1724_IRQ_MPU_TX | VT1724_IRQ_MTPCM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 int handled = 0;
Takashi Iwai3a841d52008-04-23 17:47:28 +0200416 int timeout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418 while (1) {
419 status = inb(ICEREG1724(ice, IRQSTAT));
Takashi Iwai3a841d52008-04-23 17:47:28 +0200420 status &= status_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 if (status == 0)
422 break;
Takashi Iwaie683ec42008-11-12 16:42:44 +0100423 spin_lock(&ice->reg_lock);
Takashi Iwai3a841d52008-04-23 17:47:28 +0200424 if (++timeout > 10) {
Takashi Iwai10832062008-08-11 10:18:39 +0200425 status = inb(ICEREG1724(ice, IRQSTAT));
426 printk(KERN_ERR "ice1724: Too long irq loop, "
427 "status = 0x%x\n", status);
428 if (status & VT1724_IRQ_MPU_TX) {
429 printk(KERN_ERR "ice1724: Disabling MPU_TX\n");
Takashi Iwaie683ec42008-11-12 16:42:44 +0100430 enable_midi_irq(ice, VT1724_IRQ_MPU_TX, 0);
Takashi Iwai10832062008-08-11 10:18:39 +0200431 }
Takashi Iwaie683ec42008-11-12 16:42:44 +0100432 spin_unlock(&ice->reg_lock);
Takashi Iwai3a841d52008-04-23 17:47:28 +0200433 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 }
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +0400435 handled = 1;
Takashi Iwai3a841d52008-04-23 17:47:28 +0200436 if (status & VT1724_IRQ_MPU_TX) {
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200437 if (ice->midi_output)
438 vt1724_midi_write(ice);
Takashi Iwaie683ec42008-11-12 16:42:44 +0100439 else
440 enable_midi_irq(ice, VT1724_IRQ_MPU_TX, 0);
Takashi Iwai3a841d52008-04-23 17:47:28 +0200441 /* Due to mysterical reasons, MPU_TX is always
442 * generated (and can't be cleared) when a PCM
443 * playback is going. So let's ignore at the
444 * next loop.
445 */
446 status_mask &= ~VT1724_IRQ_MPU_TX;
447 }
448 if (status & VT1724_IRQ_MPU_RX) {
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200449 if (ice->midi_input)
450 vt1724_midi_read(ice);
451 else
452 vt1724_midi_clear_rx(ice);
Takashi Iwai3a841d52008-04-23 17:47:28 +0200453 }
454 /* ack MPU irq */
455 outb(status, ICEREG1724(ice, IRQSTAT));
Takashi Iwaie683ec42008-11-12 16:42:44 +0100456 spin_unlock(&ice->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 if (status & VT1724_IRQ_MTPCM) {
458 /*
459 * Multi-track PCM
460 * PCM assignment are:
461 * Playback DMA0 (M/C) = playback_pro_substream
462 * Playback DMA1 = playback_con_substream_ds[0]
463 * Playback DMA2 = playback_con_substream_ds[1]
464 * Playback DMA3 = playback_con_substream_ds[2]
465 * Playback DMA4 (SPDIF) = playback_con_substream
466 * Record DMA0 = capture_pro_substream
467 * Record DMA1 = capture_con_substream
468 */
469 unsigned char mtstat = inb(ICEMT1724(ice, IRQ));
470 if (mtstat & VT1724_MULTI_PDMA0) {
471 if (ice->playback_pro_substream)
472 snd_pcm_period_elapsed(ice->playback_pro_substream);
473 }
474 if (mtstat & VT1724_MULTI_RDMA0) {
475 if (ice->capture_pro_substream)
476 snd_pcm_period_elapsed(ice->capture_pro_substream);
477 }
478 if (mtstat & VT1724_MULTI_PDMA1) {
479 if (ice->playback_con_substream_ds[0])
480 snd_pcm_period_elapsed(ice->playback_con_substream_ds[0]);
481 }
482 if (mtstat & VT1724_MULTI_PDMA2) {
483 if (ice->playback_con_substream_ds[1])
484 snd_pcm_period_elapsed(ice->playback_con_substream_ds[1]);
485 }
486 if (mtstat & VT1724_MULTI_PDMA3) {
487 if (ice->playback_con_substream_ds[2])
488 snd_pcm_period_elapsed(ice->playback_con_substream_ds[2]);
489 }
490 if (mtstat & VT1724_MULTI_PDMA4) {
491 if (ice->playback_con_substream)
492 snd_pcm_period_elapsed(ice->playback_con_substream);
493 }
494 if (mtstat & VT1724_MULTI_RDMA1) {
495 if (ice->capture_con_substream)
496 snd_pcm_period_elapsed(ice->capture_con_substream);
497 }
498 /* ack anyway to avoid freeze */
499 outb(mtstat, ICEMT1724(ice, IRQ));
500 /* ought to really handle this properly */
501 if (mtstat & VT1724_MULTI_FIFO_ERR) {
502 unsigned char fstat = inb(ICEMT1724(ice, DMA_FIFO_ERR));
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +0400503 outb(fstat, ICEMT1724(ice, DMA_FIFO_ERR));
504 outb(VT1724_MULTI_FIFO_ERR | inb(ICEMT1724(ice, DMA_INT_MASK)), ICEMT1724(ice, DMA_INT_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 /* If I don't do this, I get machine lockup due to continual interrupts */
506 }
507
508 }
509 }
510 return IRQ_RETVAL(handled);
511}
512
513/*
514 * PCM code - professional part (multitrack)
515 */
516
517static unsigned int rates[] = {
518 8000, 9600, 11025, 12000, 16000, 22050, 24000,
519 32000, 44100, 48000, 64000, 88200, 96000,
520 176400, 192000,
521};
522
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100523static struct snd_pcm_hw_constraint_list hw_constraints_rates_96 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 .count = ARRAY_SIZE(rates) - 2, /* up to 96000 */
525 .list = rates,
526 .mask = 0,
527};
528
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100529static struct snd_pcm_hw_constraint_list hw_constraints_rates_48 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 .count = ARRAY_SIZE(rates) - 5, /* up to 48000 */
531 .list = rates,
532 .mask = 0,
533};
534
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100535static struct snd_pcm_hw_constraint_list hw_constraints_rates_192 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 .count = ARRAY_SIZE(rates),
537 .list = rates,
538 .mask = 0,
539};
540
541struct vt1724_pcm_reg {
542 unsigned int addr; /* ADDR register offset */
543 unsigned int size; /* SIZE register offset */
544 unsigned int count; /* COUNT register offset */
545 unsigned int start; /* start & pause bit */
546};
547
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100548static int snd_vt1724_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
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 unsigned char what;
552 unsigned char old;
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100553 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 what = 0;
Takashi Iwaief991b92007-02-22 12:52:53 +0100556 snd_pcm_group_for_each_entry(s, substream) {
Clemens Ladisch29998d22007-07-30 08:14:31 +0200557 if (snd_pcm_substream_chip(s) == ice) {
558 const struct vt1724_pcm_reg *reg;
559 reg = s->runtime->private_data;
560 what |= reg->start;
561 snd_pcm_trigger_done(s, substream);
562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 }
564
565 switch (cmd) {
566 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
567 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
568 spin_lock(&ice->reg_lock);
569 old = inb(ICEMT1724(ice, DMA_PAUSE));
570 if (cmd == SNDRV_PCM_TRIGGER_PAUSE_PUSH)
571 old |= what;
572 else
573 old &= ~what;
574 outb(old, ICEMT1724(ice, DMA_PAUSE));
575 spin_unlock(&ice->reg_lock);
576 break;
577
578 case SNDRV_PCM_TRIGGER_START:
579 case SNDRV_PCM_TRIGGER_STOP:
Igor Chernyshevb40e9532009-06-25 09:31:07 +0200580 case SNDRV_PCM_TRIGGER_SUSPEND:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 spin_lock(&ice->reg_lock);
582 old = inb(ICEMT1724(ice, DMA_CONTROL));
583 if (cmd == SNDRV_PCM_TRIGGER_START)
584 old |= what;
585 else
586 old &= ~what;
587 outb(old, ICEMT1724(ice, DMA_CONTROL));
588 spin_unlock(&ice->reg_lock);
589 break;
590
Igor Chernyshevb40e9532009-06-25 09:31:07 +0200591 case SNDRV_PCM_TRIGGER_RESUME:
592 /* apps will have to restart stream */
593 break;
594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 default:
596 return -EINVAL;
597 }
598 return 0;
599}
600
601/*
602 */
603
604#define DMA_STARTS (VT1724_RDMA0_START|VT1724_PDMA0_START|VT1724_RDMA1_START|\
605 VT1724_PDMA1_START|VT1724_PDMA2_START|VT1724_PDMA3_START|VT1724_PDMA4_START)
606#define DMA_PAUSES (VT1724_RDMA0_PAUSE|VT1724_PDMA0_PAUSE|VT1724_RDMA1_PAUSE|\
607 VT1724_PDMA1_PAUSE|VT1724_PDMA2_PAUSE|VT1724_PDMA3_PAUSE|VT1724_PDMA4_PAUSE)
608
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100609static const unsigned int stdclock_rate_list[16] = {
610 48000, 24000, 12000, 9600, 32000, 16000, 8000, 96000, 44100,
611 22050, 11025, 88200, 176400, 0, 192000, 64000
612};
613
614static unsigned int stdclock_get_rate(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100616 unsigned int rate;
617 rate = stdclock_rate_list[inb(ICEMT1724(ice, RATE)) & 15];
618 return rate;
619}
620
621static void stdclock_set_rate(struct snd_ice1712 *ice, unsigned int rate)
622{
623 int i;
624 for (i = 0; i < ARRAY_SIZE(stdclock_rate_list); i++) {
625 if (stdclock_rate_list[i] == rate) {
626 outb(i, ICEMT1724(ice, RATE));
627 return;
628 }
629 }
630}
631
632static unsigned char stdclock_set_mclk(struct snd_ice1712 *ice,
633 unsigned int rate)
634{
635 unsigned char val, old;
636 /* check MT02 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 if (ice->eeprom.data[ICE_EEP2_ACLINK] & VT1724_CFG_PRO_I2S) {
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100638 val = old = inb(ICEMT1724(ice, I2S_FORMAT));
639 if (rate > 96000)
640 val |= VT1724_MT_I2S_MCLK_128X; /* 128x MCLK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 else
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100642 val &= ~VT1724_MT_I2S_MCLK_128X; /* 256x MCLK */
643 if (val != old) {
644 outb(val, ICEMT1724(ice, I2S_FORMAT));
645 /* master clock changed */
646 return 1;
647 }
648 }
649 /* no change in master clock */
650 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651}
652
Takashi Iwai92d71002009-05-06 17:18:34 +0200653static int snd_vt1724_set_pro_rate(struct snd_ice1712 *ice, unsigned int rate,
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100654 int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
656 unsigned long flags;
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100657 unsigned char mclk_change;
658 unsigned int i, old_rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100660 if (rate > ice->hw_rates->list[ice->hw_rates->count - 1])
Takashi Iwai92d71002009-05-06 17:18:34 +0200661 return -EINVAL;
662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 spin_lock_irqsave(&ice->reg_lock, flags);
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100664 if ((inb(ICEMT1724(ice, DMA_CONTROL)) & DMA_STARTS) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 (inb(ICEMT1724(ice, DMA_PAUSE)) & DMA_PAUSES)) {
666 /* running? we cannot change the rate now... */
667 spin_unlock_irqrestore(&ice->reg_lock, flags);
Takashi Iwai92d71002009-05-06 17:18:34 +0200668 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 }
670 if (!force && is_pro_rate_locked(ice)) {
671 spin_unlock_irqrestore(&ice->reg_lock, flags);
Takashi Iwai92d71002009-05-06 17:18:34 +0200672 return (rate == ice->cur_rate) ? 0 : -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 }
674
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100675 old_rate = ice->get_rate(ice);
676 if (force || (old_rate != rate))
677 ice->set_rate(ice, rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 else if (rate == ice->cur_rate) {
679 spin_unlock_irqrestore(&ice->reg_lock, flags);
Takashi Iwai92d71002009-05-06 17:18:34 +0200680 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 }
682
683 ice->cur_rate = rate;
684
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100685 /* setting master clock */
686 mclk_change = ice->set_mclk(ice, rate);
687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 spin_unlock_irqrestore(&ice->reg_lock, flags);
689
690 if (mclk_change && ice->gpio.i2s_mclk_changed)
691 ice->gpio.i2s_mclk_changed(ice);
692 if (ice->gpio.set_pro_rate)
693 ice->gpio.set_pro_rate(ice, rate);
694
695 /* set up codecs */
696 for (i = 0; i < ice->akm_codecs; i++) {
697 if (ice->akm[i].ops.set_rate_val)
698 ice->akm[i].ops.set_rate_val(&ice->akm[i], rate);
699 }
700 if (ice->spdif.ops.setup_rate)
701 ice->spdif.ops.setup_rate(ice, rate);
Takashi Iwai92d71002009-05-06 17:18:34 +0200702
703 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704}
705
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100706static int snd_vt1724_pcm_hw_params(struct snd_pcm_substream *substream,
707 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100709 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Takashi Iwai92d71002009-05-06 17:18:34 +0200710 int i, chs, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
712 chs = params_channels(hw_params);
Ingo Molnar62932df2006-01-16 16:34:20 +0100713 mutex_lock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 /* mark surround channels */
715 if (substream == ice->playback_pro_substream) {
716 /* PDMA0 can be multi-channel up to 8 */
717 chs = chs / 2 - 1;
718 for (i = 0; i < chs; i++) {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100719 if (ice->pcm_reserved[i] &&
720 ice->pcm_reserved[i] != substream) {
Ingo Molnar62932df2006-01-16 16:34:20 +0100721 mutex_unlock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 return -EBUSY;
723 }
724 ice->pcm_reserved[i] = substream;
725 }
726 for (; i < 3; i++) {
727 if (ice->pcm_reserved[i] == substream)
728 ice->pcm_reserved[i] = NULL;
729 }
730 } else {
731 for (i = 0; i < 3; i++) {
732 /* check individual playback stream */
733 if (ice->playback_con_substream_ds[i] == substream) {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100734 if (ice->pcm_reserved[i] &&
735 ice->pcm_reserved[i] != substream) {
Ingo Molnar62932df2006-01-16 16:34:20 +0100736 mutex_unlock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 return -EBUSY;
738 }
739 ice->pcm_reserved[i] = substream;
740 break;
741 }
742 }
743 }
Ingo Molnar62932df2006-01-16 16:34:20 +0100744 mutex_unlock(&ice->open_mutex);
Takashi Iwai92d71002009-05-06 17:18:34 +0200745
746 err = snd_vt1724_set_pro_rate(ice, params_rate(hw_params), 0);
747 if (err < 0)
748 return err;
749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
751}
752
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100753static int snd_vt1724_pcm_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100755 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 int i;
757
Ingo Molnar62932df2006-01-16 16:34:20 +0100758 mutex_lock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 /* unmark surround channels */
760 for (i = 0; i < 3; i++)
761 if (ice->pcm_reserved[i] == substream)
762 ice->pcm_reserved[i] = NULL;
Ingo Molnar62932df2006-01-16 16:34:20 +0100763 mutex_unlock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 return snd_pcm_lib_free_pages(substream);
765}
766
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100767static int snd_vt1724_playback_pro_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100769 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 unsigned char val;
771 unsigned int size;
772
773 spin_lock_irq(&ice->reg_lock);
774 val = (8 - substream->runtime->channels) >> 1;
775 outb(val, ICEMT1724(ice, BURST));
776
777 outl(substream->runtime->dma_addr, ICEMT1724(ice, PLAYBACK_ADDR));
778
779 size = (snd_pcm_lib_buffer_bytes(substream) >> 2) - 1;
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +0400780 /* outl(size, ICEMT1724(ice, PLAYBACK_SIZE)); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 outw(size, ICEMT1724(ice, PLAYBACK_SIZE));
782 outb(size >> 16, ICEMT1724(ice, PLAYBACK_SIZE) + 2);
783 size = (snd_pcm_lib_period_bytes(substream) >> 2) - 1;
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +0400784 /* outl(size, ICEMT1724(ice, PLAYBACK_COUNT)); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 outw(size, ICEMT1724(ice, PLAYBACK_COUNT));
786 outb(size >> 16, ICEMT1724(ice, PLAYBACK_COUNT) + 2);
787
788 spin_unlock_irq(&ice->reg_lock);
789
Takashi Iwaie2ea7cfc2009-02-05 16:07:02 +0100790 /*
791 printk(KERN_DEBUG "pro prepare: ch = %d, addr = 0x%x, "
792 "buffer = 0x%x, period = 0x%x\n",
793 substream->runtime->channels,
794 (unsigned int)substream->runtime->dma_addr,
795 snd_pcm_lib_buffer_bytes(substream),
796 snd_pcm_lib_period_bytes(substream));
797 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 return 0;
799}
800
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100801static snd_pcm_uframes_t snd_vt1724_playback_pro_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100803 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 size_t ptr;
805
806 if (!(inl(ICEMT1724(ice, DMA_CONTROL)) & VT1724_PDMA0_START))
807 return 0;
808#if 0 /* read PLAYBACK_ADDR */
809 ptr = inl(ICEMT1724(ice, PLAYBACK_ADDR));
810 if (ptr < substream->runtime->dma_addr) {
811 snd_printd("ice1724: invalid negative ptr\n");
812 return 0;
813 }
814 ptr -= substream->runtime->dma_addr;
815 ptr = bytes_to_frames(substream->runtime, ptr);
816 if (ptr >= substream->runtime->buffer_size) {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100817 snd_printd("ice1724: invalid ptr %d (size=%d)\n",
818 (int)ptr, (int)substream->runtime->period_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 return 0;
820 }
821#else /* read PLAYBACK_SIZE */
822 ptr = inl(ICEMT1724(ice, PLAYBACK_SIZE)) & 0xffffff;
823 ptr = (ptr + 1) << 2;
824 ptr = bytes_to_frames(substream->runtime, ptr);
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +0400825 if (!ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 ;
827 else if (ptr <= substream->runtime->buffer_size)
828 ptr = substream->runtime->buffer_size - ptr;
829 else {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100830 snd_printd("ice1724: invalid ptr %d (size=%d)\n",
831 (int)ptr, (int)substream->runtime->buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 ptr = 0;
833 }
834#endif
835 return ptr;
836}
837
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100838static int snd_vt1724_pcm_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100840 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Takashi Iwai32b47da2007-01-29 15:26:36 +0100841 const struct vt1724_pcm_reg *reg = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843 spin_lock_irq(&ice->reg_lock);
844 outl(substream->runtime->dma_addr, ice->profi_port + reg->addr);
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100845 outw((snd_pcm_lib_buffer_bytes(substream) >> 2) - 1,
846 ice->profi_port + reg->size);
847 outw((snd_pcm_lib_period_bytes(substream) >> 2) - 1,
848 ice->profi_port + reg->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 spin_unlock_irq(&ice->reg_lock);
850 return 0;
851}
852
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100853static snd_pcm_uframes_t snd_vt1724_pcm_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100855 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Takashi Iwai32b47da2007-01-29 15:26:36 +0100856 const struct vt1724_pcm_reg *reg = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 size_t ptr;
858
859 if (!(inl(ICEMT1724(ice, DMA_CONTROL)) & reg->start))
860 return 0;
861#if 0 /* use ADDR register */
862 ptr = inl(ice->profi_port + reg->addr);
863 ptr -= substream->runtime->dma_addr;
864 return bytes_to_frames(substream->runtime, ptr);
865#else /* use SIZE register */
866 ptr = inw(ice->profi_port + reg->size);
867 ptr = (ptr + 1) << 2;
868 ptr = bytes_to_frames(substream->runtime, ptr);
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +0400869 if (!ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 ;
871 else if (ptr <= substream->runtime->buffer_size)
872 ptr = substream->runtime->buffer_size - ptr;
873 else {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100874 snd_printd("ice1724: invalid ptr %d (size=%d)\n",
875 (int)ptr, (int)substream->runtime->buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 ptr = 0;
877 }
878 return ptr;
879#endif
880}
881
Takashi Iwaia5b7b5c2009-05-06 17:22:07 +0200882static const struct vt1724_pcm_reg vt1724_pdma0_reg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 .addr = VT1724_MT_PLAYBACK_ADDR,
884 .size = VT1724_MT_PLAYBACK_SIZE,
885 .count = VT1724_MT_PLAYBACK_COUNT,
886 .start = VT1724_PDMA0_START,
887};
888
Takashi Iwaia5b7b5c2009-05-06 17:22:07 +0200889static const struct vt1724_pcm_reg vt1724_pdma4_reg = {
890 .addr = VT1724_MT_PDMA4_ADDR,
891 .size = VT1724_MT_PDMA4_SIZE,
892 .count = VT1724_MT_PDMA4_COUNT,
893 .start = VT1724_PDMA4_START,
894};
895
896static const struct vt1724_pcm_reg vt1724_rdma0_reg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 .addr = VT1724_MT_CAPTURE_ADDR,
898 .size = VT1724_MT_CAPTURE_SIZE,
899 .count = VT1724_MT_CAPTURE_COUNT,
900 .start = VT1724_RDMA0_START,
901};
902
Takashi Iwaia5b7b5c2009-05-06 17:22:07 +0200903static const struct vt1724_pcm_reg vt1724_rdma1_reg = {
904 .addr = VT1724_MT_RDMA1_ADDR,
905 .size = VT1724_MT_RDMA1_SIZE,
906 .count = VT1724_MT_RDMA1_COUNT,
907 .start = VT1724_RDMA1_START,
908};
909
910#define vt1724_playback_pro_reg vt1724_pdma0_reg
911#define vt1724_playback_spdif_reg vt1724_pdma4_reg
912#define vt1724_capture_pro_reg vt1724_rdma0_reg
913#define vt1724_capture_spdif_reg vt1724_rdma1_reg
914
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +0400915static const struct snd_pcm_hardware snd_vt1724_playback_pro = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
917 SNDRV_PCM_INFO_BLOCK_TRANSFER |
918 SNDRV_PCM_INFO_MMAP_VALID |
919 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_SYNC_START),
920 .formats = SNDRV_PCM_FMTBIT_S32_LE,
921 .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_192000,
922 .rate_min = 8000,
923 .rate_max = 192000,
924 .channels_min = 2,
925 .channels_max = 8,
926 .buffer_bytes_max = (1UL << 21), /* 19bits dword */
927 .period_bytes_min = 8 * 4 * 2, /* FIXME: constraints needed */
928 .period_bytes_max = (1UL << 21),
929 .periods_min = 2,
930 .periods_max = 1024,
931};
932
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +0400933static const struct snd_pcm_hardware snd_vt1724_spdif = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
935 SNDRV_PCM_INFO_BLOCK_TRANSFER |
936 SNDRV_PCM_INFO_MMAP_VALID |
937 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_SYNC_START),
938 .formats = SNDRV_PCM_FMTBIT_S32_LE,
Takashi Iwai2dfbeca2005-10-12 10:04:42 +0200939 .rates = (SNDRV_PCM_RATE_32000|SNDRV_PCM_RATE_44100|
940 SNDRV_PCM_RATE_48000|SNDRV_PCM_RATE_88200|
941 SNDRV_PCM_RATE_96000|SNDRV_PCM_RATE_176400|
942 SNDRV_PCM_RATE_192000),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 .rate_min = 32000,
Takashi Iwai2dfbeca2005-10-12 10:04:42 +0200944 .rate_max = 192000,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 .channels_min = 2,
946 .channels_max = 2,
947 .buffer_bytes_max = (1UL << 18), /* 16bits dword */
948 .period_bytes_min = 2 * 4 * 2,
949 .period_bytes_max = (1UL << 18),
950 .periods_min = 2,
951 .periods_max = 1024,
952};
953
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +0400954static const struct snd_pcm_hardware snd_vt1724_2ch_stereo = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
956 SNDRV_PCM_INFO_BLOCK_TRANSFER |
957 SNDRV_PCM_INFO_MMAP_VALID |
958 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_SYNC_START),
959 .formats = SNDRV_PCM_FMTBIT_S32_LE,
960 .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_192000,
961 .rate_min = 8000,
962 .rate_max = 192000,
963 .channels_min = 2,
964 .channels_max = 2,
965 .buffer_bytes_max = (1UL << 18), /* 16bits dword */
966 .period_bytes_min = 2 * 4 * 2,
967 .period_bytes_max = (1UL << 18),
968 .periods_min = 2,
969 .periods_max = 1024,
970};
971
972/*
973 * set rate constraints
974 */
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100975static void set_std_hw_rates(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 if (ice->eeprom.data[ICE_EEP2_ACLINK] & VT1724_CFG_PRO_I2S) {
978 /* I2S */
979 /* VT1720 doesn't support more than 96kHz */
980 if ((ice->eeprom.data[ICE_EEP2_I2S] & 0x08) && !ice->vt1720)
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100981 ice->hw_rates = &hw_constraints_rates_192;
982 else
983 ice->hw_rates = &hw_constraints_rates_96;
984 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 /* ACLINK */
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100986 ice->hw_rates = &hw_constraints_rates_48;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 }
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100988}
989
990static int set_rate_constraints(struct snd_ice1712 *ice,
991 struct snd_pcm_substream *substream)
992{
993 struct snd_pcm_runtime *runtime = substream->runtime;
994
995 runtime->hw.rate_min = ice->hw_rates->list[0];
996 runtime->hw.rate_max = ice->hw_rates->list[ice->hw_rates->count - 1];
997 runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
998 return snd_pcm_hw_constraint_list(runtime, 0,
999 SNDRV_PCM_HW_PARAM_RATE,
1000 ice->hw_rates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001}
1002
1003/* multi-channel playback needs alignment 8x32bit regardless of the channels
1004 * actually used
1005 */
1006#define VT1724_BUFFER_ALIGN 0x20
1007
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001008static int snd_vt1724_playback_pro_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001010 struct snd_pcm_runtime *runtime = substream->runtime;
1011 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Takashi Iwaia6b936b2008-08-29 16:17:25 +02001012 int chs, num_indeps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Takashi Iwai32b47da2007-01-29 15:26:36 +01001014 runtime->private_data = (void *)&vt1724_playback_pro_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 ice->playback_pro_substream = substream;
1016 runtime->hw = snd_vt1724_playback_pro;
1017 snd_pcm_set_sync(substream);
1018 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
1019 set_rate_constraints(ice, substream);
Ingo Molnar62932df2006-01-16 16:34:20 +01001020 mutex_lock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 /* calculate the currently available channels */
Takashi Iwaia6b936b2008-08-29 16:17:25 +02001022 num_indeps = ice->num_total_dacs / 2 - 1;
1023 for (chs = 0; chs < num_indeps; chs++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 if (ice->pcm_reserved[chs])
1025 break;
1026 }
1027 chs = (chs + 1) * 2;
1028 runtime->hw.channels_max = chs;
1029 if (chs > 2) /* channels must be even */
1030 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Ingo Molnar62932df2006-01-16 16:34:20 +01001031 mutex_unlock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1033 VT1724_BUFFER_ALIGN);
1034 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1035 VT1724_BUFFER_ALIGN);
1036 return 0;
1037}
1038
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001039static int snd_vt1724_capture_pro_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_pro_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 ice->capture_pro_substream = substream;
1046 runtime->hw = snd_vt1724_2ch_stereo;
1047 snd_pcm_set_sync(substream);
1048 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
1049 set_rate_constraints(ice, substream);
1050 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1051 VT1724_BUFFER_ALIGN);
1052 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1053 VT1724_BUFFER_ALIGN);
1054 return 0;
1055}
1056
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001057static int snd_vt1724_playback_pro_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001059 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
1061 if (PRO_RATE_RESET)
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001062 snd_vt1724_set_pro_rate(ice, ice->pro_rate_default, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 ice->playback_pro_substream = NULL;
1064
1065 return 0;
1066}
1067
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001068static int snd_vt1724_capture_pro_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001070 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
1072 if (PRO_RATE_RESET)
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001073 snd_vt1724_set_pro_rate(ice, ice->pro_rate_default, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 ice->capture_pro_substream = NULL;
1075 return 0;
1076}
1077
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001078static struct snd_pcm_ops snd_vt1724_playback_pro_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 .open = snd_vt1724_playback_pro_open,
1080 .close = snd_vt1724_playback_pro_close,
1081 .ioctl = snd_pcm_lib_ioctl,
1082 .hw_params = snd_vt1724_pcm_hw_params,
1083 .hw_free = snd_vt1724_pcm_hw_free,
1084 .prepare = snd_vt1724_playback_pro_prepare,
1085 .trigger = snd_vt1724_pcm_trigger,
1086 .pointer = snd_vt1724_playback_pro_pointer,
1087};
1088
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001089static struct snd_pcm_ops snd_vt1724_capture_pro_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 .open = snd_vt1724_capture_pro_open,
1091 .close = snd_vt1724_capture_pro_close,
1092 .ioctl = snd_pcm_lib_ioctl,
1093 .hw_params = snd_vt1724_pcm_hw_params,
1094 .hw_free = snd_vt1724_pcm_hw_free,
1095 .prepare = snd_vt1724_pcm_prepare,
1096 .trigger = snd_vt1724_pcm_trigger,
1097 .pointer = snd_vt1724_pcm_pointer,
1098};
1099
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001100static int __devinit snd_vt1724_pcm_profi(struct snd_ice1712 *ice, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001102 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 int err;
1104
1105 err = snd_pcm_new(ice->card, "ICE1724", device, 1, 1, &pcm);
1106 if (err < 0)
1107 return err;
1108
1109 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_vt1724_playback_pro_ops);
1110 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_vt1724_capture_pro_ops);
1111
1112 pcm->private_data = ice;
1113 pcm->info_flags = 0;
1114 strcpy(pcm->name, "ICE1724");
1115
1116 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001117 snd_dma_pci_data(ice->pci),
1118 256*1024, 256*1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
1120 ice->pcm_pro = pcm;
1121
1122 return 0;
1123}
1124
1125
1126/*
1127 * SPDIF PCM
1128 */
1129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130/* update spdif control bits; call with reg_lock */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001131static void update_spdif_bits(struct snd_ice1712 *ice, unsigned int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132{
1133 unsigned char cbit, disabled;
1134
1135 cbit = inb(ICEREG1724(ice, SPDIF_CFG));
1136 disabled = cbit & ~VT1724_CFG_SPDIF_OUT_EN;
1137 if (cbit != disabled)
1138 outb(disabled, ICEREG1724(ice, SPDIF_CFG));
1139 outw(val, ICEMT1724(ice, SPDIF_CTRL));
1140 if (cbit != disabled)
1141 outb(cbit, ICEREG1724(ice, SPDIF_CFG));
1142 outw(val, ICEMT1724(ice, SPDIF_CTRL));
1143}
1144
1145/* update SPDIF control bits according to the given rate */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001146static void update_spdif_rate(struct snd_ice1712 *ice, unsigned int rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147{
1148 unsigned int val, nval;
1149 unsigned long flags;
1150
1151 spin_lock_irqsave(&ice->reg_lock, flags);
1152 nval = val = inw(ICEMT1724(ice, SPDIF_CTRL));
1153 nval &= ~(7 << 12);
1154 switch (rate) {
1155 case 44100: break;
1156 case 48000: nval |= 2 << 12; break;
1157 case 32000: nval |= 3 << 12; break;
Takashi Iwai2dfbeca2005-10-12 10:04:42 +02001158 case 88200: nval |= 4 << 12; break;
1159 case 96000: nval |= 5 << 12; break;
1160 case 192000: nval |= 6 << 12; break;
1161 case 176400: nval |= 7 << 12; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 }
1163 if (val != nval)
1164 update_spdif_bits(ice, nval);
1165 spin_unlock_irqrestore(&ice->reg_lock, flags);
1166}
1167
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001168static int snd_vt1724_playback_spdif_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001170 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001171 if (!ice->force_pdma4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 update_spdif_rate(ice, substream->runtime->rate);
1173 return snd_vt1724_pcm_prepare(substream);
1174}
1175
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001176static int snd_vt1724_playback_spdif_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001178 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
1179 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
Takashi Iwai32b47da2007-01-29 15:26:36 +01001181 runtime->private_data = (void *)&vt1724_playback_spdif_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 ice->playback_con_substream = substream;
1183 if (ice->force_pdma4) {
1184 runtime->hw = snd_vt1724_2ch_stereo;
1185 set_rate_constraints(ice, substream);
1186 } else
1187 runtime->hw = snd_vt1724_spdif;
1188 snd_pcm_set_sync(substream);
1189 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
1190 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1191 VT1724_BUFFER_ALIGN);
1192 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1193 VT1724_BUFFER_ALIGN);
Takashi Iwaic93f5a12008-03-14 17:17:09 +01001194 if (ice->spdif.ops.open)
1195 ice->spdif.ops.open(ice, substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 return 0;
1197}
1198
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001199static int snd_vt1724_playback_spdif_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001201 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
1203 if (PRO_RATE_RESET)
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001204 snd_vt1724_set_pro_rate(ice, ice->pro_rate_default, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 ice->playback_con_substream = NULL;
Takashi Iwaic93f5a12008-03-14 17:17:09 +01001206 if (ice->spdif.ops.close)
1207 ice->spdif.ops.close(ice, substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
1209 return 0;
1210}
1211
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001212static int snd_vt1724_capture_spdif_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001214 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
1215 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
Takashi Iwai32b47da2007-01-29 15:26:36 +01001217 runtime->private_data = (void *)&vt1724_capture_spdif_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 ice->capture_con_substream = substream;
1219 if (ice->force_rdma1) {
1220 runtime->hw = snd_vt1724_2ch_stereo;
1221 set_rate_constraints(ice, substream);
1222 } else
1223 runtime->hw = snd_vt1724_spdif;
1224 snd_pcm_set_sync(substream);
1225 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
1226 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1227 VT1724_BUFFER_ALIGN);
1228 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1229 VT1724_BUFFER_ALIGN);
Takashi Iwaic93f5a12008-03-14 17:17:09 +01001230 if (ice->spdif.ops.open)
1231 ice->spdif.ops.open(ice, substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 return 0;
1233}
1234
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001235static int snd_vt1724_capture_spdif_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001237 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
1239 if (PRO_RATE_RESET)
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001240 snd_vt1724_set_pro_rate(ice, ice->pro_rate_default, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 ice->capture_con_substream = NULL;
Takashi Iwaic93f5a12008-03-14 17:17:09 +01001242 if (ice->spdif.ops.close)
1243 ice->spdif.ops.close(ice, substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
1245 return 0;
1246}
1247
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001248static struct snd_pcm_ops snd_vt1724_playback_spdif_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 .open = snd_vt1724_playback_spdif_open,
1250 .close = snd_vt1724_playback_spdif_close,
1251 .ioctl = snd_pcm_lib_ioctl,
1252 .hw_params = snd_vt1724_pcm_hw_params,
1253 .hw_free = snd_vt1724_pcm_hw_free,
1254 .prepare = snd_vt1724_playback_spdif_prepare,
1255 .trigger = snd_vt1724_pcm_trigger,
1256 .pointer = snd_vt1724_pcm_pointer,
1257};
1258
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001259static struct snd_pcm_ops snd_vt1724_capture_spdif_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 .open = snd_vt1724_capture_spdif_open,
1261 .close = snd_vt1724_capture_spdif_close,
1262 .ioctl = snd_pcm_lib_ioctl,
1263 .hw_params = snd_vt1724_pcm_hw_params,
1264 .hw_free = snd_vt1724_pcm_hw_free,
1265 .prepare = snd_vt1724_pcm_prepare,
1266 .trigger = snd_vt1724_pcm_trigger,
1267 .pointer = snd_vt1724_pcm_pointer,
1268};
1269
1270
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001271static int __devinit snd_vt1724_pcm_spdif(struct snd_ice1712 *ice, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272{
1273 char *name;
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001274 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 int play, capt;
1276 int err;
1277
1278 if (ice->force_pdma4 ||
1279 (ice->eeprom.data[ICE_EEP2_SPDIF] & VT1724_CFG_SPDIF_OUT_INT)) {
1280 play = 1;
1281 ice->has_spdif = 1;
1282 } else
1283 play = 0;
1284 if (ice->force_rdma1 ||
1285 (ice->eeprom.data[ICE_EEP2_SPDIF] & VT1724_CFG_SPDIF_IN)) {
1286 capt = 1;
1287 ice->has_spdif = 1;
1288 } else
1289 capt = 0;
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001290 if (!play && !capt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 return 0; /* no spdif device */
1292
1293 if (ice->force_pdma4 || ice->force_rdma1)
1294 name = "ICE1724 Secondary";
1295 else
Alan Horstmann8eca7532009-01-05 18:30:04 +01001296 name = "ICE1724 IEC958";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 err = snd_pcm_new(ice->card, name, device, play, capt, &pcm);
1298 if (err < 0)
1299 return err;
1300
1301 if (play)
1302 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1303 &snd_vt1724_playback_spdif_ops);
1304 if (capt)
1305 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1306 &snd_vt1724_capture_spdif_ops);
1307
1308 pcm->private_data = ice;
1309 pcm->info_flags = 0;
1310 strcpy(pcm->name, name);
1311
1312 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001313 snd_dma_pci_data(ice->pci),
1314 64*1024, 64*1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
1316 ice->pcm = pcm;
1317
1318 return 0;
1319}
1320
1321
1322/*
1323 * independent surround PCMs
1324 */
1325
Takashi Iwai32b47da2007-01-29 15:26:36 +01001326static const struct vt1724_pcm_reg vt1724_playback_dma_regs[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 {
1328 .addr = VT1724_MT_PDMA1_ADDR,
1329 .size = VT1724_MT_PDMA1_SIZE,
1330 .count = VT1724_MT_PDMA1_COUNT,
1331 .start = VT1724_PDMA1_START,
1332 },
1333 {
1334 .addr = VT1724_MT_PDMA2_ADDR,
1335 .size = VT1724_MT_PDMA2_SIZE,
1336 .count = VT1724_MT_PDMA2_COUNT,
1337 .start = VT1724_PDMA2_START,
1338 },
1339 {
1340 .addr = VT1724_MT_PDMA3_ADDR,
1341 .size = VT1724_MT_PDMA3_SIZE,
1342 .count = VT1724_MT_PDMA3_COUNT,
1343 .start = VT1724_PDMA3_START,
1344 },
1345};
1346
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001347static int snd_vt1724_playback_indep_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001349 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 unsigned char val;
1351
1352 spin_lock_irq(&ice->reg_lock);
1353 val = 3 - substream->number;
1354 if (inb(ICEMT1724(ice, BURST)) < val)
1355 outb(val, ICEMT1724(ice, BURST));
1356 spin_unlock_irq(&ice->reg_lock);
1357 return snd_vt1724_pcm_prepare(substream);
1358}
1359
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001360static int snd_vt1724_playback_indep_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001362 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
1363 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Ingo Molnar62932df2006-01-16 16:34:20 +01001365 mutex_lock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 /* already used by PDMA0? */
1367 if (ice->pcm_reserved[substream->number]) {
Ingo Molnar62932df2006-01-16 16:34:20 +01001368 mutex_unlock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 return -EBUSY; /* FIXME: should handle blocking mode properly */
1370 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001371 mutex_unlock(&ice->open_mutex);
Takashi Iwai32b47da2007-01-29 15:26:36 +01001372 runtime->private_data = (void *)&vt1724_playback_dma_regs[substream->number];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 ice->playback_con_substream_ds[substream->number] = substream;
1374 runtime->hw = snd_vt1724_2ch_stereo;
1375 snd_pcm_set_sync(substream);
1376 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
1377 set_rate_constraints(ice, substream);
1378 return 0;
1379}
1380
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001381static int snd_vt1724_playback_indep_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001383 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
1385 if (PRO_RATE_RESET)
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001386 snd_vt1724_set_pro_rate(ice, ice->pro_rate_default, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 ice->playback_con_substream_ds[substream->number] = NULL;
1388 ice->pcm_reserved[substream->number] = NULL;
1389
1390 return 0;
1391}
1392
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001393static struct snd_pcm_ops snd_vt1724_playback_indep_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 .open = snd_vt1724_playback_indep_open,
1395 .close = snd_vt1724_playback_indep_close,
1396 .ioctl = snd_pcm_lib_ioctl,
1397 .hw_params = snd_vt1724_pcm_hw_params,
1398 .hw_free = snd_vt1724_pcm_hw_free,
1399 .prepare = snd_vt1724_playback_indep_prepare,
1400 .trigger = snd_vt1724_pcm_trigger,
1401 .pointer = snd_vt1724_pcm_pointer,
1402};
1403
1404
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001405static int __devinit snd_vt1724_pcm_indep(struct snd_ice1712 *ice, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001407 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 int play;
1409 int err;
1410
1411 play = ice->num_total_dacs / 2 - 1;
1412 if (play <= 0)
1413 return 0;
1414
1415 err = snd_pcm_new(ice->card, "ICE1724 Surrounds", device, play, 0, &pcm);
1416 if (err < 0)
1417 return err;
1418
1419 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1420 &snd_vt1724_playback_indep_ops);
1421
1422 pcm->private_data = ice;
1423 pcm->info_flags = 0;
1424 strcpy(pcm->name, "ICE1724 Surround PCM");
1425
1426 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001427 snd_dma_pci_data(ice->pci),
1428 64*1024, 64*1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
1430 ice->pcm_ds = pcm;
1431
1432 return 0;
1433}
1434
1435
1436/*
1437 * Mixer section
1438 */
1439
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001440static int __devinit snd_vt1724_ac97_mixer(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
1442 int err;
1443
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001444 if (!(ice->eeprom.data[ICE_EEP2_ACLINK] & VT1724_CFG_PRO_I2S)) {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001445 struct snd_ac97_bus *pbus;
1446 struct snd_ac97_template ac97;
1447 static struct snd_ac97_bus_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 .write = snd_vt1724_ac97_write,
1449 .read = snd_vt1724_ac97_read,
1450 };
1451
1452 /* cold reset */
1453 outb(inb(ICEMT1724(ice, AC97_CMD)) | 0x80, ICEMT1724(ice, AC97_CMD));
1454 mdelay(5); /* FIXME */
1455 outb(inb(ICEMT1724(ice, AC97_CMD)) & ~0x80, ICEMT1724(ice, AC97_CMD));
1456
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001457 err = snd_ac97_bus(ice->card, 0, &ops, NULL, &pbus);
1458 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 return err;
1460 memset(&ac97, 0, sizeof(ac97));
1461 ac97.private_data = ice;
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001462 err = snd_ac97_mixer(pbus, &ac97, &ice->ac97);
1463 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 printk(KERN_WARNING "ice1712: cannot initialize pro ac97, skipped\n");
1465 else
1466 return 0;
1467 }
1468 /* I2S mixer only */
1469 strcat(ice->card->mixername, "ICE1724 - multitrack");
1470 return 0;
1471}
1472
1473/*
1474 *
1475 */
1476
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001477static inline unsigned int eeprom_triple(struct snd_ice1712 *ice, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478{
1479 return (unsigned int)ice->eeprom.data[idx] | \
1480 ((unsigned int)ice->eeprom.data[idx + 1] << 8) | \
1481 ((unsigned int)ice->eeprom.data[idx + 2] << 16);
1482}
1483
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001484static void snd_vt1724_proc_read(struct snd_info_entry *entry,
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001485 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001487 struct snd_ice1712 *ice = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 unsigned int idx;
1489
1490 snd_iprintf(buffer, "%s\n\n", ice->card->longname);
1491 snd_iprintf(buffer, "EEPROM:\n");
1492
1493 snd_iprintf(buffer, " Subvendor : 0x%x\n", ice->eeprom.subvendor);
1494 snd_iprintf(buffer, " Size : %i bytes\n", ice->eeprom.size);
1495 snd_iprintf(buffer, " Version : %i\n", ice->eeprom.version);
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001496 snd_iprintf(buffer, " System Config : 0x%x\n",
1497 ice->eeprom.data[ICE_EEP2_SYSCONF]);
1498 snd_iprintf(buffer, " ACLink : 0x%x\n",
1499 ice->eeprom.data[ICE_EEP2_ACLINK]);
1500 snd_iprintf(buffer, " I2S : 0x%x\n",
1501 ice->eeprom.data[ICE_EEP2_I2S]);
1502 snd_iprintf(buffer, " S/PDIF : 0x%x\n",
1503 ice->eeprom.data[ICE_EEP2_SPDIF]);
1504 snd_iprintf(buffer, " GPIO direction : 0x%x\n",
1505 ice->eeprom.gpiodir);
1506 snd_iprintf(buffer, " GPIO mask : 0x%x\n",
1507 ice->eeprom.gpiomask);
1508 snd_iprintf(buffer, " GPIO state : 0x%x\n",
1509 ice->eeprom.gpiostate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 for (idx = 0x12; idx < ice->eeprom.size; idx++)
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001511 snd_iprintf(buffer, " Extra #%02i : 0x%x\n",
1512 idx, ice->eeprom.data[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
1514 snd_iprintf(buffer, "\nRegisters:\n");
1515
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001516 snd_iprintf(buffer, " PSDOUT03 : 0x%08x\n",
1517 (unsigned)inl(ICEMT1724(ice, ROUTE_PLAYBACK)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 for (idx = 0x0; idx < 0x20 ; idx++)
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001519 snd_iprintf(buffer, " CCS%02x : 0x%02x\n",
1520 idx, inb(ice->port+idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 for (idx = 0x0; idx < 0x30 ; idx++)
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001522 snd_iprintf(buffer, " MT%02x : 0x%02x\n",
1523 idx, inb(ice->profi_port+idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524}
1525
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001526static void __devinit snd_vt1724_proc_init(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001528 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001530 if (!snd_card_proc_new(ice->card, "ice1724", &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02001531 snd_info_set_text_ops(entry, ice, snd_vt1724_proc_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532}
1533
1534/*
1535 *
1536 */
1537
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001538static int snd_vt1724_eeprom_info(struct snd_kcontrol *kcontrol,
1539 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540{
1541 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001542 uinfo->count = sizeof(struct snd_ice1712_eeprom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 return 0;
1544}
1545
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001546static int snd_vt1724_eeprom_get(struct snd_kcontrol *kcontrol,
1547 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001549 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001550
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 memcpy(ucontrol->value.bytes.data, &ice->eeprom, sizeof(ice->eeprom));
1552 return 0;
1553}
1554
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01001555static struct snd_kcontrol_new snd_vt1724_eeprom __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1557 .name = "ICE1724 EEPROM",
1558 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1559 .info = snd_vt1724_eeprom_info,
1560 .get = snd_vt1724_eeprom_get
1561};
1562
1563/*
1564 */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001565static int snd_vt1724_spdif_info(struct snd_kcontrol *kcontrol,
1566 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567{
1568 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1569 uinfo->count = 1;
1570 return 0;
1571}
1572
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001573static unsigned int encode_spdif_bits(struct snd_aes_iec958 *diga)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574{
Takashi Iwai2dfbeca2005-10-12 10:04:42 +02001575 unsigned int val, rbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
1577 val = diga->status[0] & 0x03; /* professional, non-audio */
1578 if (val & 0x01) {
1579 /* professional */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001580 if ((diga->status[0] & IEC958_AES0_PRO_EMPHASIS) ==
1581 IEC958_AES0_PRO_EMPHASIS_5015)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 val |= 1U << 3;
Takashi Iwai2dfbeca2005-10-12 10:04:42 +02001583 rbits = (diga->status[4] >> 3) & 0x0f;
1584 if (rbits) {
1585 switch (rbits) {
1586 case 2: val |= 5 << 12; break; /* 96k */
1587 case 3: val |= 6 << 12; break; /* 192k */
1588 case 10: val |= 4 << 12; break; /* 88.2k */
1589 case 11: val |= 7 << 12; break; /* 176.4k */
1590 }
1591 } else {
1592 switch (diga->status[0] & IEC958_AES0_PRO_FS) {
1593 case IEC958_AES0_PRO_FS_44100:
1594 break;
1595 case IEC958_AES0_PRO_FS_32000:
1596 val |= 3U << 12;
1597 break;
1598 default:
1599 val |= 2U << 12;
1600 break;
1601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 }
1603 } else {
1604 /* consumer */
1605 val |= diga->status[1] & 0x04; /* copyright */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001606 if ((diga->status[0] & IEC958_AES0_CON_EMPHASIS) ==
1607 IEC958_AES0_CON_EMPHASIS_5015)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 val |= 1U << 3;
1609 val |= (unsigned int)(diga->status[1] & 0x3f) << 4; /* category */
1610 val |= (unsigned int)(diga->status[3] & IEC958_AES3_CON_FS) << 12; /* fs */
1611 }
1612 return val;
1613}
1614
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001615static void decode_spdif_bits(struct snd_aes_iec958 *diga, unsigned int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616{
1617 memset(diga->status, 0, sizeof(diga->status));
1618 diga->status[0] = val & 0x03; /* professional, non-audio */
1619 if (val & 0x01) {
1620 /* professional */
1621 if (val & (1U << 3))
1622 diga->status[0] |= IEC958_AES0_PRO_EMPHASIS_5015;
1623 switch ((val >> 12) & 0x7) {
1624 case 0:
1625 break;
1626 case 2:
1627 diga->status[0] |= IEC958_AES0_PRO_FS_32000;
1628 break;
1629 default:
1630 diga->status[0] |= IEC958_AES0_PRO_FS_48000;
1631 break;
1632 }
1633 } else {
1634 /* consumer */
1635 diga->status[0] |= val & (1U << 2); /* copyright */
1636 if (val & (1U << 3))
1637 diga->status[0] |= IEC958_AES0_CON_EMPHASIS_5015;
1638 diga->status[1] |= (val >> 4) & 0x3f; /* category */
1639 diga->status[3] |= (val >> 12) & 0x07; /* fs */
1640 }
1641}
1642
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001643static int snd_vt1724_spdif_default_get(struct snd_kcontrol *kcontrol,
1644 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001646 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 unsigned int val;
1648 val = inw(ICEMT1724(ice, SPDIF_CTRL));
1649 decode_spdif_bits(&ucontrol->value.iec958, val);
1650 return 0;
1651}
1652
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001653static int snd_vt1724_spdif_default_put(struct snd_kcontrol *kcontrol,
1654 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001656 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 unsigned int val, old;
1658
1659 val = encode_spdif_bits(&ucontrol->value.iec958);
1660 spin_lock_irq(&ice->reg_lock);
1661 old = inw(ICEMT1724(ice, SPDIF_CTRL));
1662 if (val != old)
1663 update_spdif_bits(ice, val);
1664 spin_unlock_irq(&ice->reg_lock);
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001665 return val != old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666}
1667
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01001668static struct snd_kcontrol_new snd_vt1724_spdif_default __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669{
1670 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001671 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 .info = snd_vt1724_spdif_info,
1673 .get = snd_vt1724_spdif_default_get,
1674 .put = snd_vt1724_spdif_default_put
1675};
1676
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001677static int snd_vt1724_spdif_maskc_get(struct snd_kcontrol *kcontrol,
1678 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679{
1680 ucontrol->value.iec958.status[0] = IEC958_AES0_NONAUDIO |
1681 IEC958_AES0_PROFESSIONAL |
1682 IEC958_AES0_CON_NOT_COPYRIGHT |
1683 IEC958_AES0_CON_EMPHASIS;
1684 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_ORIGINAL |
1685 IEC958_AES1_CON_CATEGORY;
1686 ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS;
1687 return 0;
1688}
1689
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001690static int snd_vt1724_spdif_maskp_get(struct snd_kcontrol *kcontrol,
1691 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692{
1693 ucontrol->value.iec958.status[0] = IEC958_AES0_NONAUDIO |
1694 IEC958_AES0_PROFESSIONAL |
1695 IEC958_AES0_PRO_FS |
1696 IEC958_AES0_PRO_EMPHASIS;
1697 return 0;
1698}
1699
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01001700static struct snd_kcontrol_new snd_vt1724_spdif_maskc __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701{
1702 .access = SNDRV_CTL_ELEM_ACCESS_READ,
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001703 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001704 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 .info = snd_vt1724_spdif_info,
1706 .get = snd_vt1724_spdif_maskc_get,
1707};
1708
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01001709static struct snd_kcontrol_new snd_vt1724_spdif_maskp __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710{
1711 .access = SNDRV_CTL_ELEM_ACCESS_READ,
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001712 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001713 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 .info = snd_vt1724_spdif_info,
1715 .get = snd_vt1724_spdif_maskp_get,
1716};
1717
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001718#define snd_vt1724_spdif_sw_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001720static int snd_vt1724_spdif_sw_get(struct snd_kcontrol *kcontrol,
1721 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001723 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
1724 ucontrol->value.integer.value[0] = inb(ICEREG1724(ice, SPDIF_CFG)) &
1725 VT1724_CFG_SPDIF_OUT_EN ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 return 0;
1727}
1728
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001729static int snd_vt1724_spdif_sw_put(struct snd_kcontrol *kcontrol,
1730 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001732 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 unsigned char old, val;
1734
1735 spin_lock_irq(&ice->reg_lock);
1736 old = val = inb(ICEREG1724(ice, SPDIF_CFG));
1737 val &= ~VT1724_CFG_SPDIF_OUT_EN;
1738 if (ucontrol->value.integer.value[0])
1739 val |= VT1724_CFG_SPDIF_OUT_EN;
1740 if (old != val)
1741 outb(val, ICEREG1724(ice, SPDIF_CFG));
1742 spin_unlock_irq(&ice->reg_lock);
1743 return old != val;
1744}
1745
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01001746static struct snd_kcontrol_new snd_vt1724_spdif_switch __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747{
1748 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1749 /* FIXME: the following conflict with IEC958 Playback Route */
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001750 /* .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH), */
1751 .name = SNDRV_CTL_NAME_IEC958("Output ", NONE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 .info = snd_vt1724_spdif_sw_info,
1753 .get = snd_vt1724_spdif_sw_get,
1754 .put = snd_vt1724_spdif_sw_put
1755};
1756
1757
1758#if 0 /* NOT USED YET */
1759/*
1760 * GPIO access from extern
1761 */
1762
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001763#define snd_vt1724_gpio_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001765int snd_vt1724_gpio_get(struct snd_kcontrol *kcontrol,
1766 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001768 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 int shift = kcontrol->private_value & 0xff;
1770 int invert = (kcontrol->private_value & (1<<24)) ? 1 : 0;
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001771
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 snd_ice1712_save_gpio_status(ice);
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001773 ucontrol->value.integer.value[0] =
1774 (snd_ice1712_gpio_read(ice) & (1 << shift) ? 1 : 0) ^ invert;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 snd_ice1712_restore_gpio_status(ice);
1776 return 0;
1777}
1778
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001779int snd_ice1712_gpio_put(struct snd_kcontrol *kcontrol,
1780 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001782 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 int shift = kcontrol->private_value & 0xff;
1784 int invert = (kcontrol->private_value & (1<<24)) ? mask : 0;
1785 unsigned int val, nval;
1786
1787 if (kcontrol->private_value & (1 << 31))
1788 return -EPERM;
1789 nval = (ucontrol->value.integer.value[0] ? (1 << shift) : 0) ^ invert;
1790 snd_ice1712_save_gpio_status(ice);
1791 val = snd_ice1712_gpio_read(ice);
1792 nval |= val & ~(1 << shift);
1793 if (val != nval)
1794 snd_ice1712_gpio_write(ice, nval);
1795 snd_ice1712_restore_gpio_status(ice);
1796 return val != nval;
1797}
1798#endif /* NOT USED YET */
1799
1800/*
1801 * rate
1802 */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001803static int snd_vt1724_pro_internal_clock_info(struct snd_kcontrol *kcontrol,
1804 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001806 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
1808 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1809 uinfo->count = 1;
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001810 uinfo->value.enumerated.items = ice->hw_rates->count + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1812 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001813 if (uinfo->value.enumerated.item == uinfo->value.enumerated.items - 1)
1814 strcpy(uinfo->value.enumerated.name, "IEC958 Input");
1815 else
1816 sprintf(uinfo->value.enumerated.name, "%d",
1817 ice->hw_rates->list[uinfo->value.enumerated.item]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 return 0;
1819}
1820
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001821static int snd_vt1724_pro_internal_clock_get(struct snd_kcontrol *kcontrol,
1822 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001824 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001825 unsigned int i, rate;
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001826
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 spin_lock_irq(&ice->reg_lock);
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001828 if (ice->is_spdif_master(ice)) {
1829 ucontrol->value.enumerated.item[0] = ice->hw_rates->count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 } else {
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001831 rate = ice->get_rate(ice);
1832 ucontrol->value.enumerated.item[0] = 0;
1833 for (i = 0; i < ice->hw_rates->count; i++) {
1834 if (ice->hw_rates->list[i] == rate) {
1835 ucontrol->value.enumerated.item[0] = i;
1836 break;
1837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 }
1840 spin_unlock_irq(&ice->reg_lock);
1841 return 0;
1842}
1843
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001844/* setting clock to external - SPDIF */
1845static void stdclock_set_spdif_clock(struct snd_ice1712 *ice)
1846{
1847 unsigned char oval;
1848 unsigned char i2s_oval;
1849 oval = inb(ICEMT1724(ice, RATE));
1850 outb(oval | VT1724_SPDIF_MASTER, ICEMT1724(ice, RATE));
1851 /* setting 256fs */
1852 i2s_oval = inb(ICEMT1724(ice, I2S_FORMAT));
1853 outb(i2s_oval & ~VT1724_MT_I2S_MCLK_128X, ICEMT1724(ice, I2S_FORMAT));
1854}
1855
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001856static int snd_vt1724_pro_internal_clock_put(struct snd_kcontrol *kcontrol,
1857 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001859 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001860 unsigned int old_rate, new_rate;
1861 unsigned int item = ucontrol->value.enumerated.item[0];
1862 unsigned int spdif = ice->hw_rates->count;
1863
1864 if (item > spdif)
1865 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
1867 spin_lock_irq(&ice->reg_lock);
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001868 if (ice->is_spdif_master(ice))
1869 old_rate = 0;
1870 else
1871 old_rate = ice->get_rate(ice);
1872 if (item == spdif) {
1873 /* switching to external clock via SPDIF */
1874 ice->set_spdif_clock(ice);
1875 new_rate = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 } else {
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001877 /* internal on-card clock */
1878 new_rate = ice->hw_rates->list[item];
1879 ice->pro_rate_default = new_rate;
1880 spin_unlock_irq(&ice->reg_lock);
1881 snd_vt1724_set_pro_rate(ice, ice->pro_rate_default, 1);
1882 spin_lock_irq(&ice->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 spin_unlock_irq(&ice->reg_lock);
1885
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001886 /* the first reset to the SPDIF master mode? */
1887 if (old_rate != new_rate && !new_rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 /* notify akm chips as well */
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001889 unsigned int i;
1890 if (ice->gpio.set_pro_rate)
1891 ice->gpio.set_pro_rate(ice, 0);
1892 for (i = 0; i < ice->akm_codecs; i++) {
1893 if (ice->akm[i].ops.set_rate_val)
1894 ice->akm[i].ops.set_rate_val(&ice->akm[i], 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 }
1896 }
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001897 return old_rate != new_rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898}
1899
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01001900static struct snd_kcontrol_new snd_vt1724_pro_internal_clock __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1902 .name = "Multi Track Internal Clock",
1903 .info = snd_vt1724_pro_internal_clock_info,
1904 .get = snd_vt1724_pro_internal_clock_get,
1905 .put = snd_vt1724_pro_internal_clock_put
1906};
1907
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001908#define snd_vt1724_pro_rate_locking_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001910static int snd_vt1724_pro_rate_locking_get(struct snd_kcontrol *kcontrol,
1911 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912{
1913 ucontrol->value.integer.value[0] = PRO_RATE_LOCKED;
1914 return 0;
1915}
1916
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001917static int snd_vt1724_pro_rate_locking_put(struct snd_kcontrol *kcontrol,
1918 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001920 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 int change = 0, nval;
1922
1923 nval = ucontrol->value.integer.value[0] ? 1 : 0;
1924 spin_lock_irq(&ice->reg_lock);
1925 change = PRO_RATE_LOCKED != nval;
1926 PRO_RATE_LOCKED = nval;
1927 spin_unlock_irq(&ice->reg_lock);
1928 return change;
1929}
1930
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01001931static struct snd_kcontrol_new snd_vt1724_pro_rate_locking __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1933 .name = "Multi Track Rate Locking",
1934 .info = snd_vt1724_pro_rate_locking_info,
1935 .get = snd_vt1724_pro_rate_locking_get,
1936 .put = snd_vt1724_pro_rate_locking_put
1937};
1938
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001939#define snd_vt1724_pro_rate_reset_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001941static int snd_vt1724_pro_rate_reset_get(struct snd_kcontrol *kcontrol,
1942 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943{
1944 ucontrol->value.integer.value[0] = PRO_RATE_RESET ? 1 : 0;
1945 return 0;
1946}
1947
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001948static int snd_vt1724_pro_rate_reset_put(struct snd_kcontrol *kcontrol,
1949 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001951 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 int change = 0, nval;
1953
1954 nval = ucontrol->value.integer.value[0] ? 1 : 0;
1955 spin_lock_irq(&ice->reg_lock);
1956 change = PRO_RATE_RESET != nval;
1957 PRO_RATE_RESET = nval;
1958 spin_unlock_irq(&ice->reg_lock);
1959 return change;
1960}
1961
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01001962static struct snd_kcontrol_new snd_vt1724_pro_rate_reset __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1964 .name = "Multi Track Rate Reset",
1965 .info = snd_vt1724_pro_rate_reset_info,
1966 .get = snd_vt1724_pro_rate_reset_get,
1967 .put = snd_vt1724_pro_rate_reset_put
1968};
1969
1970
1971/*
1972 * routing
1973 */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001974static int snd_vt1724_pro_route_info(struct snd_kcontrol *kcontrol,
1975 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976{
1977 static char *texts[] = {
1978 "PCM Out", /* 0 */
1979 "H/W In 0", "H/W In 1", /* 1-2 */
1980 "IEC958 In L", "IEC958 In R", /* 3-4 */
1981 };
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001982
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1984 uinfo->count = 1;
1985 uinfo->value.enumerated.items = 5;
1986 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1987 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
1988 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1989 return 0;
1990}
1991
1992static inline int analog_route_shift(int idx)
1993{
1994 return (idx % 2) * 12 + ((idx / 2) * 3) + 8;
1995}
1996
1997static inline int digital_route_shift(int idx)
1998{
1999 return idx * 3;
2000}
2001
Takashi Iwai2bf864a2009-05-06 17:30:17 +02002002int snd_ice1724_get_route_val(struct snd_ice1712 *ice, int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003{
2004 unsigned long val;
2005 unsigned char eitem;
Takashi Iwai32b47da2007-01-29 15:26:36 +01002006 static const unsigned char xlate[8] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 0, 255, 1, 2, 255, 255, 3, 4,
2008 };
2009
2010 val = inl(ICEMT1724(ice, ROUTE_PLAYBACK));
2011 val >>= shift;
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002012 val &= 7; /* we now have 3 bits per output */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 eitem = xlate[val];
2014 if (eitem == 255) {
2015 snd_BUG();
2016 return 0;
2017 }
2018 return eitem;
2019}
2020
Takashi Iwai2bf864a2009-05-06 17:30:17 +02002021int snd_ice1724_put_route_val(struct snd_ice1712 *ice, unsigned int val,
2022 int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023{
2024 unsigned int old_val, nval;
2025 int change;
Takashi Iwai32b47da2007-01-29 15:26:36 +01002026 static const unsigned char xroute[8] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 0, /* PCM */
2028 2, /* PSDIN0 Left */
2029 3, /* PSDIN0 Right */
2030 6, /* SPDIN Left */
2031 7, /* SPDIN Right */
2032 };
2033
2034 nval = xroute[val % 5];
2035 val = old_val = inl(ICEMT1724(ice, ROUTE_PLAYBACK));
2036 val &= ~(0x07 << shift);
2037 val |= nval << shift;
2038 change = val != old_val;
2039 if (change)
2040 outl(val, ICEMT1724(ice, ROUTE_PLAYBACK));
2041 return change;
2042}
2043
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002044static int snd_vt1724_pro_route_analog_get(struct snd_kcontrol *kcontrol,
2045 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002047 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002049 ucontrol->value.enumerated.item[0] =
Takashi Iwai2bf864a2009-05-06 17:30:17 +02002050 snd_ice1724_get_route_val(ice, analog_route_shift(idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 return 0;
2052}
2053
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002054static int snd_vt1724_pro_route_analog_put(struct snd_kcontrol *kcontrol,
2055 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002057 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai2bf864a2009-05-06 17:30:17 +02002059 return snd_ice1724_put_route_val(ice,
2060 ucontrol->value.enumerated.item[0],
2061 analog_route_shift(idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062}
2063
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002064static int snd_vt1724_pro_route_spdif_get(struct snd_kcontrol *kcontrol,
2065 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002067 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002069 ucontrol->value.enumerated.item[0] =
Takashi Iwai2bf864a2009-05-06 17:30:17 +02002070 snd_ice1724_get_route_val(ice, digital_route_shift(idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 return 0;
2072}
2073
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002074static int snd_vt1724_pro_route_spdif_put(struct snd_kcontrol *kcontrol,
2075 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002077 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai2bf864a2009-05-06 17:30:17 +02002079 return snd_ice1724_put_route_val(ice,
2080 ucontrol->value.enumerated.item[0],
2081 digital_route_shift(idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082}
2083
Takashi Iwai2bf864a2009-05-06 17:30:17 +02002084static struct snd_kcontrol_new snd_vt1724_mixer_pro_analog_route __devinitdata =
2085{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2087 .name = "H/W Playback Route",
2088 .info = snd_vt1724_pro_route_info,
2089 .get = snd_vt1724_pro_route_analog_get,
2090 .put = snd_vt1724_pro_route_analog_put,
2091};
2092
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01002093static struct snd_kcontrol_new snd_vt1724_mixer_pro_spdif_route __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002095 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, NONE) "Route",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 .info = snd_vt1724_pro_route_info,
2097 .get = snd_vt1724_pro_route_spdif_get,
2098 .put = snd_vt1724_pro_route_spdif_put,
2099 .count = 2,
2100};
2101
2102
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002103static int snd_vt1724_pro_peak_info(struct snd_kcontrol *kcontrol,
2104 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105{
2106 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2107 uinfo->count = 22; /* FIXME: for compatibility with ice1712... */
2108 uinfo->value.integer.min = 0;
2109 uinfo->value.integer.max = 255;
2110 return 0;
2111}
2112
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002113static int snd_vt1724_pro_peak_get(struct snd_kcontrol *kcontrol,
2114 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002116 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 int idx;
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002118
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 spin_lock_irq(&ice->reg_lock);
2120 for (idx = 0; idx < 22; idx++) {
2121 outb(idx, ICEMT1724(ice, MONITOR_PEAKINDEX));
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002122 ucontrol->value.integer.value[idx] =
2123 inb(ICEMT1724(ice, MONITOR_PEAKDATA));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 }
2125 spin_unlock_irq(&ice->reg_lock);
2126 return 0;
2127}
2128
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01002129static struct snd_kcontrol_new snd_vt1724_mixer_pro_peak __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2131 .name = "Multi Track Peak",
2132 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2133 .info = snd_vt1724_pro_peak_info,
2134 .get = snd_vt1724_pro_peak_get
2135};
2136
2137/*
2138 *
2139 */
2140
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01002141static struct snd_ice1712_card_info no_matched __devinitdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01002143static struct snd_ice1712_card_info *card_tables[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 snd_vt1724_revo_cards,
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002145 snd_vt1724_amp_cards,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 snd_vt1724_aureon_cards,
2147 snd_vt1720_mobo_cards,
2148 snd_vt1720_pontis_cards,
Julian Scheel6b8d6e52008-01-16 19:50:00 +01002149 snd_vt1724_prodigy_hifi_cards,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 snd_vt1724_prodigy192_cards,
2151 snd_vt1724_juli_cards,
Takashi Iwai72cbfd42009-05-06 17:33:19 +02002152 snd_vt1724_maya44_cards,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 snd_vt1724_phase_cards,
Clement Guedezf6cdab52007-01-08 10:48:41 +01002154 snd_vt1724_wtm_cards,
Shin-ya Okadaf31639b2007-10-23 15:08:18 +02002155 snd_vt1724_se_cards,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 NULL,
2157};
2158
2159
2160/*
2161 */
2162
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002163static void wait_i2c_busy(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164{
2165 int t = 0x10000;
2166 while ((inb(ICEREG1724(ice, I2C_CTRL)) & VT1724_I2C_BUSY) && t--)
2167 ;
2168 if (t == -1)
2169 printk(KERN_ERR "ice1724: i2c busy timeout\n");
2170}
2171
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002172unsigned char snd_vt1724_read_i2c(struct snd_ice1712 *ice,
2173 unsigned char dev, unsigned char addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174{
2175 unsigned char val;
2176
Ingo Molnar62932df2006-01-16 16:34:20 +01002177 mutex_lock(&ice->i2c_mutex);
Pavel Hofmanacec30f2007-12-03 12:37:17 +01002178 wait_i2c_busy(ice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 outb(addr, ICEREG1724(ice, I2C_BYTE_ADDR));
2180 outb(dev & ~VT1724_I2C_WRITE, ICEREG1724(ice, I2C_DEV_ADDR));
2181 wait_i2c_busy(ice);
2182 val = inb(ICEREG1724(ice, I2C_DATA));
Ingo Molnar62932df2006-01-16 16:34:20 +01002183 mutex_unlock(&ice->i2c_mutex);
Takashi Iwaie2ea7cfc2009-02-05 16:07:02 +01002184 /*
2185 printk(KERN_DEBUG "i2c_read: [0x%x,0x%x] = 0x%x\n", dev, addr, val);
2186 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 return val;
2188}
2189
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002190void snd_vt1724_write_i2c(struct snd_ice1712 *ice,
2191 unsigned char dev, unsigned char addr, unsigned char data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192{
Ingo Molnar62932df2006-01-16 16:34:20 +01002193 mutex_lock(&ice->i2c_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 wait_i2c_busy(ice);
Takashi Iwaie2ea7cfc2009-02-05 16:07:02 +01002195 /*
2196 printk(KERN_DEBUG "i2c_write: [0x%x,0x%x] = 0x%x\n", dev, addr, data);
2197 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 outb(addr, ICEREG1724(ice, I2C_BYTE_ADDR));
2199 outb(data, ICEREG1724(ice, I2C_DATA));
2200 outb(dev | VT1724_I2C_WRITE, ICEREG1724(ice, I2C_DEV_ADDR));
2201 wait_i2c_busy(ice);
Ingo Molnar62932df2006-01-16 16:34:20 +01002202 mutex_unlock(&ice->i2c_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203}
2204
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002205static int __devinit snd_vt1724_read_eeprom(struct snd_ice1712 *ice,
2206 const char *modelname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207{
2208 const int dev = 0xa0; /* EEPROM device address */
2209 unsigned int i, size;
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01002210 struct snd_ice1712_card_info * const *tbl, *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002212 if (!modelname || !*modelname) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 ice->eeprom.subvendor = 0;
2214 if ((inb(ICEREG1724(ice, I2C_CTRL)) & VT1724_I2C_EEPROM) != 0)
2215 ice->eeprom.subvendor =
2216 (snd_vt1724_read_i2c(ice, dev, 0x00) << 0) |
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002217 (snd_vt1724_read_i2c(ice, dev, 0x01) << 8) |
2218 (snd_vt1724_read_i2c(ice, dev, 0x02) << 16) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 (snd_vt1724_read_i2c(ice, dev, 0x03) << 24);
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002220 if (ice->eeprom.subvendor == 0 ||
2221 ice->eeprom.subvendor == (unsigned int)-1) {
2222 /* invalid subvendor from EEPROM, try the PCI
2223 * subststem ID instead
2224 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 u16 vendor, device;
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002226 pci_read_config_word(ice->pci, PCI_SUBSYSTEM_VENDOR_ID,
2227 &vendor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 pci_read_config_word(ice->pci, PCI_SUBSYSTEM_ID, &device);
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002229 ice->eeprom.subvendor =
2230 ((unsigned int)swab16(vendor) << 16) | swab16(device);
2231 if (ice->eeprom.subvendor == 0 ||
2232 ice->eeprom.subvendor == (unsigned int)-1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 printk(KERN_ERR "ice1724: No valid ID is found\n");
2234 return -ENXIO;
2235 }
2236 }
2237 }
2238 for (tbl = card_tables; *tbl; tbl++) {
2239 for (c = *tbl; c->subvendor; c++) {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002240 if (modelname && c->model &&
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002241 !strcmp(modelname, c->model)) {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002242 printk(KERN_INFO "ice1724: Using board model %s\n",
2243 c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 ice->eeprom.subvendor = c->subvendor;
2245 } else if (c->subvendor != ice->eeprom.subvendor)
2246 continue;
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002247 if (!c->eeprom_size || !c->eeprom_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 goto found;
2249 /* if the EEPROM is given by the driver, use it */
2250 snd_printdd("using the defined eeprom..\n");
2251 ice->eeprom.version = 2;
2252 ice->eeprom.size = c->eeprom_size + 6;
2253 memcpy(ice->eeprom.data, c->eeprom_data, c->eeprom_size);
2254 goto read_skipped;
2255 }
2256 }
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002257 printk(KERN_WARNING "ice1724: No matching model found for ID 0x%x\n",
2258 ice->eeprom.subvendor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
2260 found:
2261 ice->eeprom.size = snd_vt1724_read_i2c(ice, dev, 0x04);
2262 if (ice->eeprom.size < 6)
2263 ice->eeprom.size = 32;
2264 else if (ice->eeprom.size > 32) {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002265 printk(KERN_ERR "ice1724: Invalid EEPROM (size = %i)\n",
2266 ice->eeprom.size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 return -EIO;
2268 }
2269 ice->eeprom.version = snd_vt1724_read_i2c(ice, dev, 0x05);
2270 if (ice->eeprom.version != 2)
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002271 printk(KERN_WARNING "ice1724: Invalid EEPROM version %i\n",
2272 ice->eeprom.version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 size = ice->eeprom.size - 6;
2274 for (i = 0; i < size; i++)
2275 ice->eeprom.data[i] = snd_vt1724_read_i2c(ice, dev, i + 6);
2276
2277 read_skipped:
2278 ice->eeprom.gpiomask = eeprom_triple(ice, ICE_EEP2_GPIO_MASK);
2279 ice->eeprom.gpiostate = eeprom_triple(ice, ICE_EEP2_GPIO_STATE);
2280 ice->eeprom.gpiodir = eeprom_triple(ice, ICE_EEP2_GPIO_DIR);
2281
2282 return 0;
2283}
2284
2285
2286
Takashi Iwaicd775382009-07-02 10:28:56 +02002287static void snd_vt1724_chip_reset(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288{
2289 outb(VT1724_RESET , ICEREG1724(ice, CONTROL));
Takashi Iwaid82b64f2009-05-06 17:25:42 +02002290 inb(ICEREG1724(ice, CONTROL)); /* pci posting flush */
Karsten Wiese988f0662008-04-22 12:52:15 +02002291 msleep(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 outb(0, ICEREG1724(ice, CONTROL));
Takashi Iwaid82b64f2009-05-06 17:25:42 +02002293 inb(ICEREG1724(ice, CONTROL)); /* pci posting flush */
Karsten Wiese988f0662008-04-22 12:52:15 +02002294 msleep(10);
2295}
2296
Igor Chernyshevb40e9532009-06-25 09:31:07 +02002297static int snd_vt1724_chip_init(struct snd_ice1712 *ice)
Karsten Wiese988f0662008-04-22 12:52:15 +02002298{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 outb(ice->eeprom.data[ICE_EEP2_SYSCONF], ICEREG1724(ice, SYS_CFG));
2300 outb(ice->eeprom.data[ICE_EEP2_ACLINK], ICEREG1724(ice, AC97_CFG));
2301 outb(ice->eeprom.data[ICE_EEP2_I2S], ICEREG1724(ice, I2S_FEATURES));
2302 outb(ice->eeprom.data[ICE_EEP2_SPDIF], ICEREG1724(ice, SPDIF_CFG));
2303
2304 ice->gpio.write_mask = ice->eeprom.gpiomask;
2305 ice->gpio.direction = ice->eeprom.gpiodir;
2306 snd_vt1724_set_gpio_mask(ice, ice->eeprom.gpiomask);
2307 snd_vt1724_set_gpio_dir(ice, ice->eeprom.gpiodir);
2308 snd_vt1724_set_gpio_data(ice, ice->eeprom.gpiostate);
2309
2310 outb(0, ICEREG1724(ice, POWERDOWN));
2311
Igor Chernyshevb40e9532009-06-25 09:31:07 +02002312 /* MPU_RX and TX irq masks are cleared later dynamically */
2313 outb(VT1724_IRQ_MPU_RX | VT1724_IRQ_MPU_TX , ICEREG1724(ice, IRQMASK));
2314
2315 /* don't handle FIFO overrun/underruns (just yet),
2316 * since they cause machine lockups
2317 */
2318 outb(VT1724_MULTI_FIFO_ERR, ICEMT1724(ice, DMA_INT_MASK));
2319
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 return 0;
2321}
2322
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002323static int __devinit snd_vt1724_spdif_build_controls(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324{
2325 int err;
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002326 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
Takashi Iwaida3cec32008-08-08 17:12:14 +02002328 if (snd_BUG_ON(!ice->pcm))
2329 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330
Takashi Iwai2bf864a2009-05-06 17:30:17 +02002331 if (!ice->own_routing) {
2332 err = snd_ctl_add(ice->card,
2333 snd_ctl_new1(&snd_vt1724_mixer_pro_spdif_route, ice));
2334 if (err < 0)
2335 return err;
2336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337
2338 err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_spdif_switch, ice));
2339 if (err < 0)
2340 return err;
2341
2342 err = snd_ctl_add(ice->card, kctl = snd_ctl_new1(&snd_vt1724_spdif_default, ice));
2343 if (err < 0)
2344 return err;
2345 kctl->id.device = ice->pcm->device;
2346 err = snd_ctl_add(ice->card, kctl = snd_ctl_new1(&snd_vt1724_spdif_maskc, ice));
2347 if (err < 0)
2348 return err;
2349 kctl->id.device = ice->pcm->device;
2350 err = snd_ctl_add(ice->card, kctl = snd_ctl_new1(&snd_vt1724_spdif_maskp, ice));
2351 if (err < 0)
2352 return err;
2353 kctl->id.device = ice->pcm->device;
2354#if 0 /* use default only */
2355 err = snd_ctl_add(ice->card, kctl = snd_ctl_new1(&snd_vt1724_spdif_stream, ice));
2356 if (err < 0)
2357 return err;
2358 kctl->id.device = ice->pcm->device;
2359 ice->spdif.stream_ctl = kctl;
2360#endif
2361 return 0;
2362}
2363
2364
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002365static int __devinit snd_vt1724_build_controls(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366{
2367 int err;
2368
2369 err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_eeprom, ice));
2370 if (err < 0)
2371 return err;
2372 err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_pro_internal_clock, ice));
2373 if (err < 0)
2374 return err;
2375
2376 err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_pro_rate_locking, ice));
2377 if (err < 0)
2378 return err;
2379 err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_pro_rate_reset, ice));
2380 if (err < 0)
2381 return err;
2382
Takashi Iwai2bf864a2009-05-06 17:30:17 +02002383 if (!ice->own_routing && ice->num_total_dacs > 0) {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002384 struct snd_kcontrol_new tmp = snd_vt1724_mixer_pro_analog_route;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 tmp.count = ice->num_total_dacs;
2386 if (ice->vt1720 && tmp.count > 2)
2387 tmp.count = 2;
2388 err = snd_ctl_add(ice->card, snd_ctl_new1(&tmp, ice));
2389 if (err < 0)
2390 return err;
2391 }
2392
2393 err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_mixer_pro_peak, ice));
2394 if (err < 0)
2395 return err;
2396
2397 return 0;
2398}
2399
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002400static int snd_vt1724_free(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401{
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002402 if (!ice->port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 goto __hw_end;
2404 /* mask all interrupts */
2405 outb(0xff, ICEMT1724(ice, DMA_INT_MASK));
2406 outb(0xff, ICEREG1724(ice, IRQMASK));
2407 /* --- */
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002408__hw_end:
Jeff Garzikf000fd82008-04-22 13:50:34 +02002409 if (ice->irq >= 0)
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002410 free_irq(ice->irq, ice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 pci_release_regions(ice->pci);
2412 snd_ice1712_akm4xxx_free(ice);
2413 pci_disable_device(ice->pci);
Takashi Iwai7cda8ba2008-01-18 13:36:07 +01002414 kfree(ice->spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 kfree(ice);
2416 return 0;
2417}
2418
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002419static int snd_vt1724_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002421 struct snd_ice1712 *ice = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 return snd_vt1724_free(ice);
2423}
2424
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002425static int __devinit snd_vt1724_create(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 struct pci_dev *pci,
2427 const char *modelname,
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002428 struct snd_ice1712 **r_ice1712)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002430 struct snd_ice1712 *ice;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 int err;
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002432 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 .dev_free = snd_vt1724_dev_free,
2434 };
2435
2436 *r_ice1712 = NULL;
2437
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002438 /* enable PCI device */
2439 err = pci_enable_device(pci);
2440 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 return err;
2442
Takashi Iwaie560d8d2005-09-09 14:21:46 +02002443 ice = kzalloc(sizeof(*ice), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 if (ice == NULL) {
2445 pci_disable_device(pci);
2446 return -ENOMEM;
2447 }
2448 ice->vt1724 = 1;
2449 spin_lock_init(&ice->reg_lock);
Ingo Molnar62932df2006-01-16 16:34:20 +01002450 mutex_init(&ice->gpio_mutex);
2451 mutex_init(&ice->open_mutex);
2452 mutex_init(&ice->i2c_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 ice->gpio.set_mask = snd_vt1724_set_gpio_mask;
Pavel Hofman49470302009-09-16 22:25:38 +02002454 ice->gpio.get_mask = snd_vt1724_get_gpio_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 ice->gpio.set_dir = snd_vt1724_set_gpio_dir;
Pavel Hofman49470302009-09-16 22:25:38 +02002456 ice->gpio.get_dir = snd_vt1724_get_gpio_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 ice->gpio.set_data = snd_vt1724_set_gpio_data;
2458 ice->gpio.get_data = snd_vt1724_get_gpio_data;
2459 ice->card = card;
2460 ice->pci = pci;
2461 ice->irq = -1;
2462 pci_set_master(pci);
2463 snd_vt1724_proc_init(ice);
2464 synchronize_irq(pci->irq);
2465
Igor Chernyshevb40e9532009-06-25 09:31:07 +02002466 card->private_data = ice;
2467
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002468 err = pci_request_regions(pci, "ICE1724");
2469 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 kfree(ice);
2471 pci_disable_device(pci);
2472 return err;
2473 }
2474 ice->port = pci_resource_start(pci, 0);
2475 ice->profi_port = pci_resource_start(pci, 1);
2476
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002477 if (request_irq(pci->irq, snd_vt1724_interrupt,
Takashi Iwai437a5a42006-11-21 12:14:23 +01002478 IRQF_SHARED, "ICE1724", ice)) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02002479 snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 snd_vt1724_free(ice);
2481 return -EIO;
2482 }
2483
2484 ice->irq = pci->irq;
2485
Karsten Wiese988f0662008-04-22 12:52:15 +02002486 snd_vt1724_chip_reset(ice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 if (snd_vt1724_read_eeprom(ice, modelname) < 0) {
2488 snd_vt1724_free(ice);
2489 return -EIO;
2490 }
2491 if (snd_vt1724_chip_init(ice) < 0) {
2492 snd_vt1724_free(ice);
2493 return -EIO;
2494 }
2495
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002496 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, ice, &ops);
2497 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 snd_vt1724_free(ice);
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002499 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 }
2501
2502 snd_card_set_dev(card, &pci->dev);
2503
2504 *r_ice1712 = ice;
2505 return 0;
2506}
2507
2508
2509/*
2510 *
2511 * Registration
2512 *
2513 */
2514
2515static int __devinit snd_vt1724_probe(struct pci_dev *pci,
2516 const struct pci_device_id *pci_id)
2517{
2518 static int dev;
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002519 struct snd_card *card;
2520 struct snd_ice1712 *ice;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 int pcm_dev = 0, err;
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01002522 struct snd_ice1712_card_info * const *tbl, *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523
2524 if (dev >= SNDRV_CARDS)
2525 return -ENODEV;
2526 if (!enable[dev]) {
2527 dev++;
2528 return -ENOENT;
2529 }
2530
Takashi Iwaie58de7b2008-12-28 16:44:30 +01002531 err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
2532 if (err < 0)
2533 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534
2535 strcpy(card->driver, "ICE1724");
2536 strcpy(card->shortname, "ICEnsemble ICE1724");
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002537
2538 err = snd_vt1724_create(card, pci, model[dev], &ice);
2539 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 snd_card_free(card);
2541 return err;
2542 }
2543
2544 for (tbl = card_tables; *tbl; tbl++) {
2545 for (c = *tbl; c->subvendor; c++) {
2546 if (c->subvendor == ice->eeprom.subvendor) {
2547 strcpy(card->shortname, c->name);
2548 if (c->driver) /* specific driver? */
2549 strcpy(card->driver, c->driver);
2550 if (c->chip_init) {
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002551 err = c->chip_init(ice);
2552 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 snd_card_free(card);
2554 return err;
2555 }
2556 }
2557 goto __found;
2558 }
2559 }
2560 }
2561 c = &no_matched;
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002562__found:
2563 /*
2564 * VT1724 has separate DMAs for the analog and the SPDIF streams while
2565 * ICE1712 has only one for both (mixed up).
2566 *
2567 * Confusingly the analog PCM is named "professional" here because it
2568 * was called so in ice1712 driver, and vt1724 driver is derived from
2569 * ice1712 driver.
2570 */
Pavel Hofmand16be8e2008-03-20 12:10:27 +01002571 ice->pro_rate_default = PRO_RATE_DEFAULT;
2572 if (!ice->is_spdif_master)
2573 ice->is_spdif_master = stdclock_is_spdif_master;
2574 if (!ice->get_rate)
2575 ice->get_rate = stdclock_get_rate;
2576 if (!ice->set_rate)
2577 ice->set_rate = stdclock_set_rate;
2578 if (!ice->set_mclk)
2579 ice->set_mclk = stdclock_set_mclk;
2580 if (!ice->set_spdif_clock)
2581 ice->set_spdif_clock = stdclock_set_spdif_clock;
2582 if (!ice->hw_rates)
2583 set_std_hw_rates(ice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002585 err = snd_vt1724_pcm_profi(ice, pcm_dev++);
2586 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 snd_card_free(card);
2588 return err;
2589 }
2590
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002591 err = snd_vt1724_pcm_spdif(ice, pcm_dev++);
2592 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 snd_card_free(card);
2594 return err;
2595 }
2596
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002597 err = snd_vt1724_pcm_indep(ice, pcm_dev++);
2598 if (err < 0) {
2599 snd_card_free(card);
2600 return err;
2601 }
2602
2603 err = snd_vt1724_ac97_mixer(ice);
2604 if (err < 0) {
2605 snd_card_free(card);
2606 return err;
2607 }
2608
2609 err = snd_vt1724_build_controls(ice);
2610 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 snd_card_free(card);
2612 return err;
2613 }
2614
2615 if (ice->pcm && ice->has_spdif) { /* has SPDIF I/O */
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002616 err = snd_vt1724_spdif_build_controls(ice);
2617 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 snd_card_free(card);
2619 return err;
2620 }
2621 }
2622
2623 if (c->build_controls) {
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002624 err = c->build_controls(ice);
2625 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 snd_card_free(card);
2627 return err;
2628 }
2629 }
2630
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002631 if (!c->no_mpu401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 if (ice->eeprom.data[ICE_EEP2_SYSCONF] & VT1724_CFG_MPU401) {
Clemens Ladischaea3bfb2008-05-20 14:22:44 +02002633 struct snd_rawmidi *rmidi;
2634
2635 err = snd_rawmidi_new(card, "MIDI", 0, 1, 1, &rmidi);
2636 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 snd_card_free(card);
2638 return err;
2639 }
Clemens Ladischaea3bfb2008-05-20 14:22:44 +02002640 ice->rmidi[0] = rmidi;
2641 rmidi->private_data = ice;
2642 strcpy(rmidi->name, "ICE1724 MIDI");
2643 rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
2644 SNDRV_RAWMIDI_INFO_INPUT |
2645 SNDRV_RAWMIDI_INFO_DUPLEX;
2646 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
2647 &vt1724_midi_output_ops);
2648 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT,
2649 &vt1724_midi_input_ops);
2650
Takashi Iwai3a841d52008-04-23 17:47:28 +02002651 /* set watermarks */
2652 outb(VT1724_MPU_RX_FIFO | 0x1,
2653 ICEREG1724(ice, MPU_FIFO_WM));
2654 outb(0x1, ICEREG1724(ice, MPU_FIFO_WM));
Clemens Ladischaea3bfb2008-05-20 14:22:44 +02002655 /* set UART mode */
2656 outb(VT1724_MPU_UART, ICEREG1724(ice, MPU_CTRL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 }
2658 }
2659
2660 sprintf(card->longname, "%s at 0x%lx, irq %i",
2661 card->shortname, ice->port, ice->irq);
2662
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002663 err = snd_card_register(card);
2664 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 snd_card_free(card);
2666 return err;
2667 }
2668 pci_set_drvdata(pci, card);
2669 dev++;
2670 return 0;
2671}
2672
2673static void __devexit snd_vt1724_remove(struct pci_dev *pci)
2674{
2675 snd_card_free(pci_get_drvdata(pci));
2676 pci_set_drvdata(pci, NULL);
2677}
2678
Igor Chernyshevb40e9532009-06-25 09:31:07 +02002679#ifdef CONFIG_PM
2680static int snd_vt1724_suspend(struct pci_dev *pci, pm_message_t state)
2681{
2682 struct snd_card *card = pci_get_drvdata(pci);
2683 struct snd_ice1712 *ice = card->private_data;
2684
2685 if (!ice->pm_suspend_enabled)
2686 return 0;
2687
2688 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
2689
2690 snd_pcm_suspend_all(ice->pcm);
2691 snd_pcm_suspend_all(ice->pcm_pro);
2692 snd_pcm_suspend_all(ice->pcm_ds);
2693 snd_ac97_suspend(ice->ac97);
2694
2695 spin_lock_irq(&ice->reg_lock);
2696 ice->pm_saved_is_spdif_master = ice->is_spdif_master(ice);
2697 ice->pm_saved_spdif_ctrl = inw(ICEMT1724(ice, SPDIF_CTRL));
2698 ice->pm_saved_spdif_cfg = inb(ICEREG1724(ice, SPDIF_CFG));
2699 ice->pm_saved_route = inl(ICEMT1724(ice, ROUTE_PLAYBACK));
2700 spin_unlock_irq(&ice->reg_lock);
2701
2702 if (ice->pm_suspend)
2703 ice->pm_suspend(ice);
2704
2705 pci_disable_device(pci);
2706 pci_save_state(pci);
2707 pci_set_power_state(pci, pci_choose_state(pci, state));
2708 return 0;
2709}
2710
2711static int snd_vt1724_resume(struct pci_dev *pci)
2712{
2713 struct snd_card *card = pci_get_drvdata(pci);
2714 struct snd_ice1712 *ice = card->private_data;
2715
2716 if (!ice->pm_suspend_enabled)
2717 return 0;
2718
2719 pci_set_power_state(pci, PCI_D0);
2720 pci_restore_state(pci);
2721
2722 if (pci_enable_device(pci) < 0) {
2723 snd_card_disconnect(card);
2724 return -EIO;
2725 }
2726
2727 pci_set_master(pci);
2728
2729 snd_vt1724_chip_reset(ice);
2730
2731 if (snd_vt1724_chip_init(ice) < 0) {
2732 snd_card_disconnect(card);
2733 return -EIO;
2734 }
2735
2736 if (ice->pm_resume)
2737 ice->pm_resume(ice);
2738
2739 if (ice->pm_saved_is_spdif_master) {
2740 /* switching to external clock via SPDIF */
2741 ice->set_spdif_clock(ice);
2742 } else {
2743 /* internal on-card clock */
2744 snd_vt1724_set_pro_rate(ice, ice->pro_rate_default, 1);
2745 }
2746
2747 update_spdif_bits(ice, ice->pm_saved_spdif_ctrl);
2748
2749 outb(ice->pm_saved_spdif_cfg, ICEREG1724(ice, SPDIF_CFG));
2750 outl(ice->pm_saved_route, ICEMT1724(ice, ROUTE_PLAYBACK));
2751
2752 if (ice->ac97)
2753 snd_ac97_resume(ice->ac97);
2754
2755 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
2756 return 0;
2757}
2758#endif
2759
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760static struct pci_driver driver = {
2761 .name = "ICE1724",
2762 .id_table = snd_vt1724_ids,
2763 .probe = snd_vt1724_probe,
2764 .remove = __devexit_p(snd_vt1724_remove),
Igor Chernyshevb40e9532009-06-25 09:31:07 +02002765#ifdef CONFIG_PM
2766 .suspend = snd_vt1724_suspend,
2767 .resume = snd_vt1724_resume,
2768#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769};
2770
2771static int __init alsa_card_ice1724_init(void)
2772{
Takashi Iwai01d25d42005-04-11 16:58:24 +02002773 return pci_register_driver(&driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774}
2775
2776static void __exit alsa_card_ice1724_exit(void)
2777{
2778 pci_unregister_driver(&driver);
2779}
2780
2781module_init(alsa_card_ice1724_init)
2782module_exit(alsa_card_ice1724_exit)