blob: 514e15385f7ab1ce25465914835e9fbc3d634b63 [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
Pavel Hofman6796d5a2009-09-16 22:25:39 +0200121/*
122 * locking rate makes sense only for internal clock mode
123 */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100124static inline int is_pro_rate_locked(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Pavel Hofman6796d5a2009-09-16 22:25:39 +0200126 return (!ice->is_spdif_master(ice)) && PRO_RATE_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
128
129/*
130 * ac97 section
131 */
132
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100133static unsigned char snd_vt1724_ac97_ready(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
135 unsigned char old_cmd;
136 int tm;
137 for (tm = 0; tm < 0x10000; tm++) {
138 old_cmd = inb(ICEMT1724(ice, AC97_CMD));
139 if (old_cmd & (VT1724_AC97_WRITE | VT1724_AC97_READ))
140 continue;
141 if (!(old_cmd & VT1724_AC97_READY))
142 continue;
143 return old_cmd;
144 }
145 snd_printd(KERN_ERR "snd_vt1724_ac97_ready: timeout\n");
146 return old_cmd;
147}
148
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100149static int snd_vt1724_ac97_wait_bit(struct snd_ice1712 *ice, unsigned char bit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
151 int tm;
152 for (tm = 0; tm < 0x10000; tm++)
153 if ((inb(ICEMT1724(ice, AC97_CMD)) & bit) == 0)
154 return 0;
155 snd_printd(KERN_ERR "snd_vt1724_ac97_wait_bit: timeout\n");
156 return -EIO;
157}
158
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100159static void snd_vt1724_ac97_write(struct snd_ac97 *ac97,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 unsigned short reg,
161 unsigned short val)
162{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100163 struct snd_ice1712 *ice = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 unsigned char old_cmd;
165
166 old_cmd = snd_vt1724_ac97_ready(ice);
167 old_cmd &= ~VT1724_AC97_ID_MASK;
168 old_cmd |= ac97->num;
169 outb(reg, ICEMT1724(ice, AC97_INDEX));
170 outw(val, ICEMT1724(ice, AC97_DATA));
171 outb(old_cmd | VT1724_AC97_WRITE, ICEMT1724(ice, AC97_CMD));
172 snd_vt1724_ac97_wait_bit(ice, VT1724_AC97_WRITE);
173}
174
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100175static unsigned short snd_vt1724_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100177 struct snd_ice1712 *ice = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 unsigned char old_cmd;
179
180 old_cmd = snd_vt1724_ac97_ready(ice);
181 old_cmd &= ~VT1724_AC97_ID_MASK;
182 old_cmd |= ac97->num;
183 outb(reg, ICEMT1724(ice, AC97_INDEX));
184 outb(old_cmd | VT1724_AC97_READ, ICEMT1724(ice, AC97_CMD));
185 if (snd_vt1724_ac97_wait_bit(ice, VT1724_AC97_READ) < 0)
186 return ~0;
187 return inw(ICEMT1724(ice, AC97_DATA));
188}
189
190
191/*
192 * GPIO operations
193 */
194
195/* set gpio direction 0 = read, 1 = write */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100196static void snd_vt1724_set_gpio_dir(struct snd_ice1712 *ice, unsigned int data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
198 outl(data, ICEREG1724(ice, GPIO_DIRECTION));
199 inw(ICEREG1724(ice, GPIO_DIRECTION)); /* dummy read for pci-posting */
200}
201
Pavel Hofman49470302009-09-16 22:25:38 +0200202/* get gpio direction 0 = read, 1 = write */
203static unsigned int snd_vt1724_get_gpio_dir(struct snd_ice1712 *ice)
204{
205 return inl(ICEREG1724(ice, GPIO_DIRECTION));
206}
207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208/* set the gpio mask (0 = writable) */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100209static void snd_vt1724_set_gpio_mask(struct snd_ice1712 *ice, unsigned int data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
211 outw(data, ICEREG1724(ice, GPIO_WRITE_MASK));
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +0400212 if (!ice->vt1720) /* VT1720 supports only 16 GPIO bits */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 outb((data >> 16) & 0xff, ICEREG1724(ice, GPIO_WRITE_MASK_22));
214 inw(ICEREG1724(ice, GPIO_WRITE_MASK)); /* dummy read for pci-posting */
215}
216
Pavel Hofman49470302009-09-16 22:25:38 +0200217static unsigned int snd_vt1724_get_gpio_mask(struct snd_ice1712 *ice)
218{
219 unsigned int mask;
220 if (!ice->vt1720)
221 mask = (unsigned int)inb(ICEREG1724(ice, GPIO_WRITE_MASK_22));
222 else
223 mask = 0;
224 mask = (mask << 16) | inw(ICEREG1724(ice, GPIO_WRITE_MASK));
225 return mask;
226}
227
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100228static void snd_vt1724_set_gpio_data(struct snd_ice1712 *ice, unsigned int data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
230 outw(data, ICEREG1724(ice, GPIO_DATA));
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +0400231 if (!ice->vt1720)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 outb(data >> 16, ICEREG1724(ice, GPIO_DATA_22));
233 inw(ICEREG1724(ice, GPIO_DATA)); /* dummy read for pci-posting */
234}
235
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100236static unsigned int snd_vt1724_get_gpio_data(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
238 unsigned int data;
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +0400239 if (!ice->vt1720)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 data = (unsigned int)inb(ICEREG1724(ice, GPIO_DATA_22));
241 else
242 data = 0;
243 data = (data << 16) | inw(ICEREG1724(ice, GPIO_DATA));
244 return data;
245}
246
247/*
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200248 * MIDI
Takashi Iwai3a841d52008-04-23 17:47:28 +0200249 */
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200250
251static void vt1724_midi_clear_rx(struct snd_ice1712 *ice)
Takashi Iwai3a841d52008-04-23 17:47:28 +0200252{
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200253 unsigned int count;
254
255 for (count = inb(ICEREG1724(ice, MPU_RXFIFO)); count > 0; --count)
256 inb(ICEREG1724(ice, MPU_DATA));
Takashi Iwai3a841d52008-04-23 17:47:28 +0200257}
258
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200259static inline struct snd_rawmidi_substream *
260get_rawmidi_substream(struct snd_ice1712 *ice, unsigned int stream)
Takashi Iwai3a841d52008-04-23 17:47:28 +0200261{
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200262 return list_first_entry(&ice->rmidi[0]->streams[stream].substreams,
263 struct snd_rawmidi_substream, list);
Takashi Iwai3a841d52008-04-23 17:47:28 +0200264}
265
Takashi Iwaie683ec42008-11-12 16:42:44 +0100266static void enable_midi_irq(struct snd_ice1712 *ice, u8 flag, int enable);
267
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200268static void vt1724_midi_write(struct snd_ice1712 *ice)
269{
270 struct snd_rawmidi_substream *s;
271 int count, i;
272 u8 buffer[32];
273
274 s = get_rawmidi_substream(ice, SNDRV_RAWMIDI_STREAM_OUTPUT);
275 count = 31 - inb(ICEREG1724(ice, MPU_TXFIFO));
276 if (count > 0) {
277 count = snd_rawmidi_transmit(s, buffer, count);
278 for (i = 0; i < count; ++i)
279 outb(buffer[i], ICEREG1724(ice, MPU_DATA));
280 }
Takashi Iwaie683ec42008-11-12 16:42:44 +0100281 /* mask irq when all bytes have been transmitted.
282 * enabled again in output_trigger when the new data comes in.
283 */
284 enable_midi_irq(ice, VT1724_IRQ_MPU_TX,
285 !snd_rawmidi_transmit_empty(s));
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200286}
287
288static void vt1724_midi_read(struct snd_ice1712 *ice)
289{
290 struct snd_rawmidi_substream *s;
291 int count, i;
292 u8 buffer[32];
293
294 s = get_rawmidi_substream(ice, SNDRV_RAWMIDI_STREAM_INPUT);
295 count = inb(ICEREG1724(ice, MPU_RXFIFO));
296 if (count > 0) {
297 count = min(count, 32);
298 for (i = 0; i < count; ++i)
299 buffer[i] = inb(ICEREG1724(ice, MPU_DATA));
300 snd_rawmidi_receive(s, buffer, count);
301 }
302}
303
Takashi Iwaie683ec42008-11-12 16:42:44 +0100304/* call with ice->reg_lock */
305static void enable_midi_irq(struct snd_ice1712 *ice, u8 flag, int enable)
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200306{
Takashi Iwaie683ec42008-11-12 16:42:44 +0100307 u8 mask = inb(ICEREG1724(ice, IRQMASK));
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200308 if (enable)
309 mask &= ~flag;
310 else
311 mask |= flag;
312 outb(mask, ICEREG1724(ice, IRQMASK));
Takashi Iwaie683ec42008-11-12 16:42:44 +0100313}
314
315static void vt1724_enable_midi_irq(struct snd_rawmidi_substream *substream,
316 u8 flag, int enable)
317{
318 struct snd_ice1712 *ice = substream->rmidi->private_data;
319
320 spin_lock_irq(&ice->reg_lock);
321 enable_midi_irq(ice, flag, enable);
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200322 spin_unlock_irq(&ice->reg_lock);
323}
324
325static int vt1724_midi_output_open(struct snd_rawmidi_substream *s)
326{
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200327 return 0;
328}
329
330static int vt1724_midi_output_close(struct snd_rawmidi_substream *s)
331{
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200332 return 0;
333}
334
335static void vt1724_midi_output_trigger(struct snd_rawmidi_substream *s, int up)
336{
337 struct snd_ice1712 *ice = s->rmidi->private_data;
338 unsigned long flags;
339
340 spin_lock_irqsave(&ice->reg_lock, flags);
341 if (up) {
342 ice->midi_output = 1;
343 vt1724_midi_write(ice);
344 } else {
345 ice->midi_output = 0;
Takashi Iwaie683ec42008-11-12 16:42:44 +0100346 enable_midi_irq(ice, VT1724_IRQ_MPU_TX, 0);
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200347 }
348 spin_unlock_irqrestore(&ice->reg_lock, flags);
349}
350
351static void vt1724_midi_output_drain(struct snd_rawmidi_substream *s)
352{
353 struct snd_ice1712 *ice = s->rmidi->private_data;
354 unsigned long timeout;
355
Takashi Iwaie683ec42008-11-12 16:42:44 +0100356 vt1724_enable_midi_irq(s, VT1724_IRQ_MPU_TX, 0);
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200357 /* 32 bytes should be transmitted in less than about 12 ms */
358 timeout = jiffies + msecs_to_jiffies(15);
359 do {
360 if (inb(ICEREG1724(ice, MPU_CTRL)) & VT1724_MPU_TX_EMPTY)
361 break;
362 schedule_timeout_uninterruptible(1);
363 } while (time_after(timeout, jiffies));
364}
365
366static struct snd_rawmidi_ops vt1724_midi_output_ops = {
367 .open = vt1724_midi_output_open,
368 .close = vt1724_midi_output_close,
369 .trigger = vt1724_midi_output_trigger,
370 .drain = vt1724_midi_output_drain,
371};
372
373static int vt1724_midi_input_open(struct snd_rawmidi_substream *s)
374{
375 vt1724_midi_clear_rx(s->rmidi->private_data);
376 vt1724_enable_midi_irq(s, VT1724_IRQ_MPU_RX, 1);
377 return 0;
378}
379
380static int vt1724_midi_input_close(struct snd_rawmidi_substream *s)
381{
382 vt1724_enable_midi_irq(s, VT1724_IRQ_MPU_RX, 0);
383 return 0;
384}
385
386static void vt1724_midi_input_trigger(struct snd_rawmidi_substream *s, int up)
387{
388 struct snd_ice1712 *ice = s->rmidi->private_data;
389 unsigned long flags;
390
391 spin_lock_irqsave(&ice->reg_lock, flags);
392 if (up) {
393 ice->midi_input = 1;
394 vt1724_midi_read(ice);
395 } else {
396 ice->midi_input = 0;
397 }
398 spin_unlock_irqrestore(&ice->reg_lock, flags);
399}
400
401static struct snd_rawmidi_ops vt1724_midi_input_ops = {
402 .open = vt1724_midi_input_open,
403 .close = vt1724_midi_input_close,
404 .trigger = vt1724_midi_input_trigger,
405};
406
Takashi Iwai3a841d52008-04-23 17:47:28 +0200407
408/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 * Interrupt handler
410 */
411
David Howells7d12e782006-10-05 14:55:46 +0100412static irqreturn_t snd_vt1724_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100414 struct snd_ice1712 *ice = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 unsigned char status;
Takashi Iwai3a841d52008-04-23 17:47:28 +0200416 unsigned char status_mask =
417 VT1724_IRQ_MPU_RX | VT1724_IRQ_MPU_TX | VT1724_IRQ_MTPCM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 int handled = 0;
Takashi Iwai3a841d52008-04-23 17:47:28 +0200419 int timeout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 while (1) {
422 status = inb(ICEREG1724(ice, IRQSTAT));
Takashi Iwai3a841d52008-04-23 17:47:28 +0200423 status &= status_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 if (status == 0)
425 break;
Takashi Iwaie683ec42008-11-12 16:42:44 +0100426 spin_lock(&ice->reg_lock);
Takashi Iwai3a841d52008-04-23 17:47:28 +0200427 if (++timeout > 10) {
Takashi Iwai10832062008-08-11 10:18:39 +0200428 status = inb(ICEREG1724(ice, IRQSTAT));
429 printk(KERN_ERR "ice1724: Too long irq loop, "
430 "status = 0x%x\n", status);
431 if (status & VT1724_IRQ_MPU_TX) {
432 printk(KERN_ERR "ice1724: Disabling MPU_TX\n");
Takashi Iwaie683ec42008-11-12 16:42:44 +0100433 enable_midi_irq(ice, VT1724_IRQ_MPU_TX, 0);
Takashi Iwai10832062008-08-11 10:18:39 +0200434 }
Takashi Iwaie683ec42008-11-12 16:42:44 +0100435 spin_unlock(&ice->reg_lock);
Takashi Iwai3a841d52008-04-23 17:47:28 +0200436 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 }
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +0400438 handled = 1;
Takashi Iwai3a841d52008-04-23 17:47:28 +0200439 if (status & VT1724_IRQ_MPU_TX) {
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200440 if (ice->midi_output)
441 vt1724_midi_write(ice);
Takashi Iwaie683ec42008-11-12 16:42:44 +0100442 else
443 enable_midi_irq(ice, VT1724_IRQ_MPU_TX, 0);
Takashi Iwai3a841d52008-04-23 17:47:28 +0200444 /* Due to mysterical reasons, MPU_TX is always
445 * generated (and can't be cleared) when a PCM
446 * playback is going. So let's ignore at the
447 * next loop.
448 */
449 status_mask &= ~VT1724_IRQ_MPU_TX;
450 }
451 if (status & VT1724_IRQ_MPU_RX) {
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200452 if (ice->midi_input)
453 vt1724_midi_read(ice);
454 else
455 vt1724_midi_clear_rx(ice);
Takashi Iwai3a841d52008-04-23 17:47:28 +0200456 }
457 /* ack MPU irq */
458 outb(status, ICEREG1724(ice, IRQSTAT));
Takashi Iwaie683ec42008-11-12 16:42:44 +0100459 spin_unlock(&ice->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 if (status & VT1724_IRQ_MTPCM) {
461 /*
462 * Multi-track PCM
463 * PCM assignment are:
464 * Playback DMA0 (M/C) = playback_pro_substream
465 * Playback DMA1 = playback_con_substream_ds[0]
466 * Playback DMA2 = playback_con_substream_ds[1]
467 * Playback DMA3 = playback_con_substream_ds[2]
468 * Playback DMA4 (SPDIF) = playback_con_substream
469 * Record DMA0 = capture_pro_substream
470 * Record DMA1 = capture_con_substream
471 */
472 unsigned char mtstat = inb(ICEMT1724(ice, IRQ));
473 if (mtstat & VT1724_MULTI_PDMA0) {
474 if (ice->playback_pro_substream)
475 snd_pcm_period_elapsed(ice->playback_pro_substream);
476 }
477 if (mtstat & VT1724_MULTI_RDMA0) {
478 if (ice->capture_pro_substream)
479 snd_pcm_period_elapsed(ice->capture_pro_substream);
480 }
481 if (mtstat & VT1724_MULTI_PDMA1) {
482 if (ice->playback_con_substream_ds[0])
483 snd_pcm_period_elapsed(ice->playback_con_substream_ds[0]);
484 }
485 if (mtstat & VT1724_MULTI_PDMA2) {
486 if (ice->playback_con_substream_ds[1])
487 snd_pcm_period_elapsed(ice->playback_con_substream_ds[1]);
488 }
489 if (mtstat & VT1724_MULTI_PDMA3) {
490 if (ice->playback_con_substream_ds[2])
491 snd_pcm_period_elapsed(ice->playback_con_substream_ds[2]);
492 }
493 if (mtstat & VT1724_MULTI_PDMA4) {
494 if (ice->playback_con_substream)
495 snd_pcm_period_elapsed(ice->playback_con_substream);
496 }
497 if (mtstat & VT1724_MULTI_RDMA1) {
498 if (ice->capture_con_substream)
499 snd_pcm_period_elapsed(ice->capture_con_substream);
500 }
501 /* ack anyway to avoid freeze */
502 outb(mtstat, ICEMT1724(ice, IRQ));
503 /* ought to really handle this properly */
504 if (mtstat & VT1724_MULTI_FIFO_ERR) {
505 unsigned char fstat = inb(ICEMT1724(ice, DMA_FIFO_ERR));
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +0400506 outb(fstat, ICEMT1724(ice, DMA_FIFO_ERR));
507 outb(VT1724_MULTI_FIFO_ERR | inb(ICEMT1724(ice, DMA_INT_MASK)), ICEMT1724(ice, DMA_INT_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 /* If I don't do this, I get machine lockup due to continual interrupts */
509 }
510
511 }
512 }
513 return IRQ_RETVAL(handled);
514}
515
516/*
517 * PCM code - professional part (multitrack)
518 */
519
520static unsigned int rates[] = {
521 8000, 9600, 11025, 12000, 16000, 22050, 24000,
522 32000, 44100, 48000, 64000, 88200, 96000,
523 176400, 192000,
524};
525
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100526static struct snd_pcm_hw_constraint_list hw_constraints_rates_96 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 .count = ARRAY_SIZE(rates) - 2, /* up to 96000 */
528 .list = rates,
529 .mask = 0,
530};
531
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100532static struct snd_pcm_hw_constraint_list hw_constraints_rates_48 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 .count = ARRAY_SIZE(rates) - 5, /* up to 48000 */
534 .list = rates,
535 .mask = 0,
536};
537
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100538static struct snd_pcm_hw_constraint_list hw_constraints_rates_192 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 .count = ARRAY_SIZE(rates),
540 .list = rates,
541 .mask = 0,
542};
543
544struct vt1724_pcm_reg {
545 unsigned int addr; /* ADDR register offset */
546 unsigned int size; /* SIZE register offset */
547 unsigned int count; /* COUNT register offset */
548 unsigned int start; /* start & pause bit */
549};
550
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100551static int snd_vt1724_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100553 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 unsigned char what;
555 unsigned char old;
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100556 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 what = 0;
Takashi Iwaief991b92007-02-22 12:52:53 +0100559 snd_pcm_group_for_each_entry(s, substream) {
Clemens Ladisch29998d22007-07-30 08:14:31 +0200560 if (snd_pcm_substream_chip(s) == ice) {
561 const struct vt1724_pcm_reg *reg;
562 reg = s->runtime->private_data;
563 what |= reg->start;
564 snd_pcm_trigger_done(s, substream);
565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 }
567
568 switch (cmd) {
569 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
570 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
571 spin_lock(&ice->reg_lock);
572 old = inb(ICEMT1724(ice, DMA_PAUSE));
573 if (cmd == SNDRV_PCM_TRIGGER_PAUSE_PUSH)
574 old |= what;
575 else
576 old &= ~what;
577 outb(old, ICEMT1724(ice, DMA_PAUSE));
578 spin_unlock(&ice->reg_lock);
579 break;
580
581 case SNDRV_PCM_TRIGGER_START:
582 case SNDRV_PCM_TRIGGER_STOP:
Igor Chernyshevb40e9532009-06-25 09:31:07 +0200583 case SNDRV_PCM_TRIGGER_SUSPEND:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 spin_lock(&ice->reg_lock);
585 old = inb(ICEMT1724(ice, DMA_CONTROL));
586 if (cmd == SNDRV_PCM_TRIGGER_START)
587 old |= what;
588 else
589 old &= ~what;
590 outb(old, ICEMT1724(ice, DMA_CONTROL));
591 spin_unlock(&ice->reg_lock);
592 break;
593
Igor Chernyshevb40e9532009-06-25 09:31:07 +0200594 case SNDRV_PCM_TRIGGER_RESUME:
595 /* apps will have to restart stream */
596 break;
597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 default:
599 return -EINVAL;
600 }
601 return 0;
602}
603
604/*
605 */
606
607#define DMA_STARTS (VT1724_RDMA0_START|VT1724_PDMA0_START|VT1724_RDMA1_START|\
608 VT1724_PDMA1_START|VT1724_PDMA2_START|VT1724_PDMA3_START|VT1724_PDMA4_START)
609#define DMA_PAUSES (VT1724_RDMA0_PAUSE|VT1724_PDMA0_PAUSE|VT1724_RDMA1_PAUSE|\
610 VT1724_PDMA1_PAUSE|VT1724_PDMA2_PAUSE|VT1724_PDMA3_PAUSE|VT1724_PDMA4_PAUSE)
611
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100612static const unsigned int stdclock_rate_list[16] = {
613 48000, 24000, 12000, 9600, 32000, 16000, 8000, 96000, 44100,
614 22050, 11025, 88200, 176400, 0, 192000, 64000
615};
616
617static unsigned int stdclock_get_rate(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100619 unsigned int rate;
620 rate = stdclock_rate_list[inb(ICEMT1724(ice, RATE)) & 15];
621 return rate;
622}
623
624static void stdclock_set_rate(struct snd_ice1712 *ice, unsigned int rate)
625{
626 int i;
627 for (i = 0; i < ARRAY_SIZE(stdclock_rate_list); i++) {
628 if (stdclock_rate_list[i] == rate) {
629 outb(i, ICEMT1724(ice, RATE));
630 return;
631 }
632 }
633}
634
635static unsigned char stdclock_set_mclk(struct snd_ice1712 *ice,
636 unsigned int rate)
637{
638 unsigned char val, old;
639 /* check MT02 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 if (ice->eeprom.data[ICE_EEP2_ACLINK] & VT1724_CFG_PRO_I2S) {
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100641 val = old = inb(ICEMT1724(ice, I2S_FORMAT));
642 if (rate > 96000)
643 val |= VT1724_MT_I2S_MCLK_128X; /* 128x MCLK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 else
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100645 val &= ~VT1724_MT_I2S_MCLK_128X; /* 256x MCLK */
646 if (val != old) {
647 outb(val, ICEMT1724(ice, I2S_FORMAT));
648 /* master clock changed */
649 return 1;
650 }
651 }
652 /* no change in master clock */
653 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
Takashi Iwai92d71002009-05-06 17:18:34 +0200656static int snd_vt1724_set_pro_rate(struct snd_ice1712 *ice, unsigned int rate,
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100657 int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
659 unsigned long flags;
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100660 unsigned char mclk_change;
661 unsigned int i, old_rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100663 if (rate > ice->hw_rates->list[ice->hw_rates->count - 1])
Takashi Iwai92d71002009-05-06 17:18:34 +0200664 return -EINVAL;
665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 spin_lock_irqsave(&ice->reg_lock, flags);
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100667 if ((inb(ICEMT1724(ice, DMA_CONTROL)) & DMA_STARTS) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 (inb(ICEMT1724(ice, DMA_PAUSE)) & DMA_PAUSES)) {
669 /* running? we cannot change the rate now... */
670 spin_unlock_irqrestore(&ice->reg_lock, flags);
Takashi Iwai92d71002009-05-06 17:18:34 +0200671 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 }
673 if (!force && is_pro_rate_locked(ice)) {
Pavel Hofman6796d5a2009-09-16 22:25:39 +0200674 /* comparing required and current rate - makes sense for
675 * internal clock only */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 spin_unlock_irqrestore(&ice->reg_lock, flags);
Takashi Iwai92d71002009-05-06 17:18:34 +0200677 return (rate == ice->cur_rate) ? 0 : -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 }
679
Pavel Hofman6796d5a2009-09-16 22:25:39 +0200680 if (force || !ice->is_spdif_master(ice)) {
681 /* force means the rate was switched by ucontrol, otherwise
682 * setting clock rate for internal clock mode */
683 old_rate = ice->get_rate(ice);
684 if (force || (old_rate != rate))
685 ice->set_rate(ice, rate);
686 else if (rate == ice->cur_rate) {
687 spin_unlock_irqrestore(&ice->reg_lock, flags);
688 return 0;
689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 }
691
692 ice->cur_rate = rate;
693
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100694 /* setting master clock */
695 mclk_change = ice->set_mclk(ice, rate);
696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 spin_unlock_irqrestore(&ice->reg_lock, flags);
698
699 if (mclk_change && ice->gpio.i2s_mclk_changed)
700 ice->gpio.i2s_mclk_changed(ice);
701 if (ice->gpio.set_pro_rate)
702 ice->gpio.set_pro_rate(ice, rate);
703
704 /* set up codecs */
705 for (i = 0; i < ice->akm_codecs; i++) {
706 if (ice->akm[i].ops.set_rate_val)
707 ice->akm[i].ops.set_rate_val(&ice->akm[i], rate);
708 }
709 if (ice->spdif.ops.setup_rate)
710 ice->spdif.ops.setup_rate(ice, rate);
Takashi Iwai92d71002009-05-06 17:18:34 +0200711
712 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713}
714
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100715static int snd_vt1724_pcm_hw_params(struct snd_pcm_substream *substream,
716 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100718 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Takashi Iwai92d71002009-05-06 17:18:34 +0200719 int i, chs, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
721 chs = params_channels(hw_params);
Ingo Molnar62932df2006-01-16 16:34:20 +0100722 mutex_lock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 /* mark surround channels */
724 if (substream == ice->playback_pro_substream) {
725 /* PDMA0 can be multi-channel up to 8 */
726 chs = chs / 2 - 1;
727 for (i = 0; i < chs; i++) {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100728 if (ice->pcm_reserved[i] &&
729 ice->pcm_reserved[i] != substream) {
Ingo Molnar62932df2006-01-16 16:34:20 +0100730 mutex_unlock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 return -EBUSY;
732 }
733 ice->pcm_reserved[i] = substream;
734 }
735 for (; i < 3; i++) {
736 if (ice->pcm_reserved[i] == substream)
737 ice->pcm_reserved[i] = NULL;
738 }
739 } else {
740 for (i = 0; i < 3; i++) {
741 /* check individual playback stream */
742 if (ice->playback_con_substream_ds[i] == substream) {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100743 if (ice->pcm_reserved[i] &&
744 ice->pcm_reserved[i] != substream) {
Ingo Molnar62932df2006-01-16 16:34:20 +0100745 mutex_unlock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 return -EBUSY;
747 }
748 ice->pcm_reserved[i] = substream;
749 break;
750 }
751 }
752 }
Ingo Molnar62932df2006-01-16 16:34:20 +0100753 mutex_unlock(&ice->open_mutex);
Takashi Iwai92d71002009-05-06 17:18:34 +0200754
755 err = snd_vt1724_set_pro_rate(ice, params_rate(hw_params), 0);
756 if (err < 0)
757 return err;
758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
760}
761
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100762static int snd_vt1724_pcm_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100764 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 int i;
766
Ingo Molnar62932df2006-01-16 16:34:20 +0100767 mutex_lock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 /* unmark surround channels */
769 for (i = 0; i < 3; i++)
770 if (ice->pcm_reserved[i] == substream)
771 ice->pcm_reserved[i] = NULL;
Ingo Molnar62932df2006-01-16 16:34:20 +0100772 mutex_unlock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 return snd_pcm_lib_free_pages(substream);
774}
775
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100776static int snd_vt1724_playback_pro_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100778 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 unsigned char val;
780 unsigned int size;
781
782 spin_lock_irq(&ice->reg_lock);
783 val = (8 - substream->runtime->channels) >> 1;
784 outb(val, ICEMT1724(ice, BURST));
785
786 outl(substream->runtime->dma_addr, ICEMT1724(ice, PLAYBACK_ADDR));
787
788 size = (snd_pcm_lib_buffer_bytes(substream) >> 2) - 1;
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +0400789 /* outl(size, ICEMT1724(ice, PLAYBACK_SIZE)); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 outw(size, ICEMT1724(ice, PLAYBACK_SIZE));
791 outb(size >> 16, ICEMT1724(ice, PLAYBACK_SIZE) + 2);
792 size = (snd_pcm_lib_period_bytes(substream) >> 2) - 1;
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +0400793 /* outl(size, ICEMT1724(ice, PLAYBACK_COUNT)); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 outw(size, ICEMT1724(ice, PLAYBACK_COUNT));
795 outb(size >> 16, ICEMT1724(ice, PLAYBACK_COUNT) + 2);
796
797 spin_unlock_irq(&ice->reg_lock);
798
Takashi Iwaie2ea7cfc2009-02-05 16:07:02 +0100799 /*
800 printk(KERN_DEBUG "pro prepare: ch = %d, addr = 0x%x, "
801 "buffer = 0x%x, period = 0x%x\n",
802 substream->runtime->channels,
803 (unsigned int)substream->runtime->dma_addr,
804 snd_pcm_lib_buffer_bytes(substream),
805 snd_pcm_lib_period_bytes(substream));
806 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 return 0;
808}
809
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100810static snd_pcm_uframes_t snd_vt1724_playback_pro_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100812 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 size_t ptr;
814
815 if (!(inl(ICEMT1724(ice, DMA_CONTROL)) & VT1724_PDMA0_START))
816 return 0;
817#if 0 /* read PLAYBACK_ADDR */
818 ptr = inl(ICEMT1724(ice, PLAYBACK_ADDR));
819 if (ptr < substream->runtime->dma_addr) {
820 snd_printd("ice1724: invalid negative ptr\n");
821 return 0;
822 }
823 ptr -= substream->runtime->dma_addr;
824 ptr = bytes_to_frames(substream->runtime, ptr);
825 if (ptr >= substream->runtime->buffer_size) {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100826 snd_printd("ice1724: invalid ptr %d (size=%d)\n",
827 (int)ptr, (int)substream->runtime->period_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 return 0;
829 }
830#else /* read PLAYBACK_SIZE */
831 ptr = inl(ICEMT1724(ice, PLAYBACK_SIZE)) & 0xffffff;
832 ptr = (ptr + 1) << 2;
833 ptr = bytes_to_frames(substream->runtime, ptr);
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +0400834 if (!ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 ;
836 else if (ptr <= substream->runtime->buffer_size)
837 ptr = substream->runtime->buffer_size - ptr;
838 else {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100839 snd_printd("ice1724: invalid ptr %d (size=%d)\n",
840 (int)ptr, (int)substream->runtime->buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 ptr = 0;
842 }
843#endif
844 return ptr;
845}
846
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100847static int snd_vt1724_pcm_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100849 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Takashi Iwai32b47da2007-01-29 15:26:36 +0100850 const struct vt1724_pcm_reg *reg = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 spin_lock_irq(&ice->reg_lock);
853 outl(substream->runtime->dma_addr, ice->profi_port + reg->addr);
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100854 outw((snd_pcm_lib_buffer_bytes(substream) >> 2) - 1,
855 ice->profi_port + reg->size);
856 outw((snd_pcm_lib_period_bytes(substream) >> 2) - 1,
857 ice->profi_port + reg->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 spin_unlock_irq(&ice->reg_lock);
859 return 0;
860}
861
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100862static snd_pcm_uframes_t snd_vt1724_pcm_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100864 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Takashi Iwai32b47da2007-01-29 15:26:36 +0100865 const struct vt1724_pcm_reg *reg = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 size_t ptr;
867
868 if (!(inl(ICEMT1724(ice, DMA_CONTROL)) & reg->start))
869 return 0;
870#if 0 /* use ADDR register */
871 ptr = inl(ice->profi_port + reg->addr);
872 ptr -= substream->runtime->dma_addr;
873 return bytes_to_frames(substream->runtime, ptr);
874#else /* use SIZE register */
875 ptr = inw(ice->profi_port + reg->size);
876 ptr = (ptr + 1) << 2;
877 ptr = bytes_to_frames(substream->runtime, ptr);
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +0400878 if (!ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 ;
880 else if (ptr <= substream->runtime->buffer_size)
881 ptr = substream->runtime->buffer_size - ptr;
882 else {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100883 snd_printd("ice1724: invalid ptr %d (size=%d)\n",
884 (int)ptr, (int)substream->runtime->buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 ptr = 0;
886 }
887 return ptr;
888#endif
889}
890
Takashi Iwaia5b7b5c2009-05-06 17:22:07 +0200891static const struct vt1724_pcm_reg vt1724_pdma0_reg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 .addr = VT1724_MT_PLAYBACK_ADDR,
893 .size = VT1724_MT_PLAYBACK_SIZE,
894 .count = VT1724_MT_PLAYBACK_COUNT,
895 .start = VT1724_PDMA0_START,
896};
897
Takashi Iwaia5b7b5c2009-05-06 17:22:07 +0200898static const struct vt1724_pcm_reg vt1724_pdma4_reg = {
899 .addr = VT1724_MT_PDMA4_ADDR,
900 .size = VT1724_MT_PDMA4_SIZE,
901 .count = VT1724_MT_PDMA4_COUNT,
902 .start = VT1724_PDMA4_START,
903};
904
905static const struct vt1724_pcm_reg vt1724_rdma0_reg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 .addr = VT1724_MT_CAPTURE_ADDR,
907 .size = VT1724_MT_CAPTURE_SIZE,
908 .count = VT1724_MT_CAPTURE_COUNT,
909 .start = VT1724_RDMA0_START,
910};
911
Takashi Iwaia5b7b5c2009-05-06 17:22:07 +0200912static const struct vt1724_pcm_reg vt1724_rdma1_reg = {
913 .addr = VT1724_MT_RDMA1_ADDR,
914 .size = VT1724_MT_RDMA1_SIZE,
915 .count = VT1724_MT_RDMA1_COUNT,
916 .start = VT1724_RDMA1_START,
917};
918
919#define vt1724_playback_pro_reg vt1724_pdma0_reg
920#define vt1724_playback_spdif_reg vt1724_pdma4_reg
921#define vt1724_capture_pro_reg vt1724_rdma0_reg
922#define vt1724_capture_spdif_reg vt1724_rdma1_reg
923
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +0400924static const struct snd_pcm_hardware snd_vt1724_playback_pro = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
926 SNDRV_PCM_INFO_BLOCK_TRANSFER |
927 SNDRV_PCM_INFO_MMAP_VALID |
928 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_SYNC_START),
929 .formats = SNDRV_PCM_FMTBIT_S32_LE,
930 .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_192000,
931 .rate_min = 8000,
932 .rate_max = 192000,
933 .channels_min = 2,
934 .channels_max = 8,
935 .buffer_bytes_max = (1UL << 21), /* 19bits dword */
936 .period_bytes_min = 8 * 4 * 2, /* FIXME: constraints needed */
937 .period_bytes_max = (1UL << 21),
938 .periods_min = 2,
939 .periods_max = 1024,
940};
941
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +0400942static const struct snd_pcm_hardware snd_vt1724_spdif = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
944 SNDRV_PCM_INFO_BLOCK_TRANSFER |
945 SNDRV_PCM_INFO_MMAP_VALID |
946 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_SYNC_START),
947 .formats = SNDRV_PCM_FMTBIT_S32_LE,
Takashi Iwai2dfbeca2005-10-12 10:04:42 +0200948 .rates = (SNDRV_PCM_RATE_32000|SNDRV_PCM_RATE_44100|
949 SNDRV_PCM_RATE_48000|SNDRV_PCM_RATE_88200|
950 SNDRV_PCM_RATE_96000|SNDRV_PCM_RATE_176400|
951 SNDRV_PCM_RATE_192000),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 .rate_min = 32000,
Takashi Iwai2dfbeca2005-10-12 10:04:42 +0200953 .rate_max = 192000,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 .channels_min = 2,
955 .channels_max = 2,
956 .buffer_bytes_max = (1UL << 18), /* 16bits dword */
957 .period_bytes_min = 2 * 4 * 2,
958 .period_bytes_max = (1UL << 18),
959 .periods_min = 2,
960 .periods_max = 1024,
961};
962
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +0400963static const struct snd_pcm_hardware snd_vt1724_2ch_stereo = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
965 SNDRV_PCM_INFO_BLOCK_TRANSFER |
966 SNDRV_PCM_INFO_MMAP_VALID |
967 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_SYNC_START),
968 .formats = SNDRV_PCM_FMTBIT_S32_LE,
969 .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_192000,
970 .rate_min = 8000,
971 .rate_max = 192000,
972 .channels_min = 2,
973 .channels_max = 2,
974 .buffer_bytes_max = (1UL << 18), /* 16bits dword */
975 .period_bytes_min = 2 * 4 * 2,
976 .period_bytes_max = (1UL << 18),
977 .periods_min = 2,
978 .periods_max = 1024,
979};
980
981/*
982 * set rate constraints
983 */
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100984static void set_std_hw_rates(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 if (ice->eeprom.data[ICE_EEP2_ACLINK] & VT1724_CFG_PRO_I2S) {
987 /* I2S */
988 /* VT1720 doesn't support more than 96kHz */
989 if ((ice->eeprom.data[ICE_EEP2_I2S] & 0x08) && !ice->vt1720)
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100990 ice->hw_rates = &hw_constraints_rates_192;
991 else
992 ice->hw_rates = &hw_constraints_rates_96;
993 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 /* ACLINK */
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100995 ice->hw_rates = &hw_constraints_rates_48;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 }
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100997}
998
999static int set_rate_constraints(struct snd_ice1712 *ice,
1000 struct snd_pcm_substream *substream)
1001{
1002 struct snd_pcm_runtime *runtime = substream->runtime;
1003
1004 runtime->hw.rate_min = ice->hw_rates->list[0];
1005 runtime->hw.rate_max = ice->hw_rates->list[ice->hw_rates->count - 1];
1006 runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
1007 return snd_pcm_hw_constraint_list(runtime, 0,
1008 SNDRV_PCM_HW_PARAM_RATE,
1009 ice->hw_rates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010}
1011
1012/* multi-channel playback needs alignment 8x32bit regardless of the channels
1013 * actually used
1014 */
1015#define VT1724_BUFFER_ALIGN 0x20
1016
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001017static int snd_vt1724_playback_pro_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001019 struct snd_pcm_runtime *runtime = substream->runtime;
1020 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Takashi Iwaia6b936b2008-08-29 16:17:25 +02001021 int chs, num_indeps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Takashi Iwai32b47da2007-01-29 15:26:36 +01001023 runtime->private_data = (void *)&vt1724_playback_pro_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 ice->playback_pro_substream = substream;
1025 runtime->hw = snd_vt1724_playback_pro;
1026 snd_pcm_set_sync(substream);
1027 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
1028 set_rate_constraints(ice, substream);
Ingo Molnar62932df2006-01-16 16:34:20 +01001029 mutex_lock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 /* calculate the currently available channels */
Takashi Iwaia6b936b2008-08-29 16:17:25 +02001031 num_indeps = ice->num_total_dacs / 2 - 1;
1032 for (chs = 0; chs < num_indeps; chs++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 if (ice->pcm_reserved[chs])
1034 break;
1035 }
1036 chs = (chs + 1) * 2;
1037 runtime->hw.channels_max = chs;
1038 if (chs > 2) /* channels must be even */
1039 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Ingo Molnar62932df2006-01-16 16:34:20 +01001040 mutex_unlock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1042 VT1724_BUFFER_ALIGN);
1043 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1044 VT1724_BUFFER_ALIGN);
1045 return 0;
1046}
1047
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001048static int snd_vt1724_capture_pro_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001050 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
1051 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
Takashi Iwai32b47da2007-01-29 15:26:36 +01001053 runtime->private_data = (void *)&vt1724_capture_pro_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 ice->capture_pro_substream = substream;
1055 runtime->hw = snd_vt1724_2ch_stereo;
1056 snd_pcm_set_sync(substream);
1057 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
1058 set_rate_constraints(ice, substream);
1059 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1060 VT1724_BUFFER_ALIGN);
1061 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1062 VT1724_BUFFER_ALIGN);
1063 return 0;
1064}
1065
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001066static int snd_vt1724_playback_pro_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001068 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
1070 if (PRO_RATE_RESET)
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001071 snd_vt1724_set_pro_rate(ice, ice->pro_rate_default, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 ice->playback_pro_substream = NULL;
1073
1074 return 0;
1075}
1076
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001077static int snd_vt1724_capture_pro_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001079 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
1081 if (PRO_RATE_RESET)
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001082 snd_vt1724_set_pro_rate(ice, ice->pro_rate_default, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 ice->capture_pro_substream = NULL;
1084 return 0;
1085}
1086
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001087static struct snd_pcm_ops snd_vt1724_playback_pro_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 .open = snd_vt1724_playback_pro_open,
1089 .close = snd_vt1724_playback_pro_close,
1090 .ioctl = snd_pcm_lib_ioctl,
1091 .hw_params = snd_vt1724_pcm_hw_params,
1092 .hw_free = snd_vt1724_pcm_hw_free,
1093 .prepare = snd_vt1724_playback_pro_prepare,
1094 .trigger = snd_vt1724_pcm_trigger,
1095 .pointer = snd_vt1724_playback_pro_pointer,
1096};
1097
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001098static struct snd_pcm_ops snd_vt1724_capture_pro_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 .open = snd_vt1724_capture_pro_open,
1100 .close = snd_vt1724_capture_pro_close,
1101 .ioctl = snd_pcm_lib_ioctl,
1102 .hw_params = snd_vt1724_pcm_hw_params,
1103 .hw_free = snd_vt1724_pcm_hw_free,
1104 .prepare = snd_vt1724_pcm_prepare,
1105 .trigger = snd_vt1724_pcm_trigger,
1106 .pointer = snd_vt1724_pcm_pointer,
1107};
1108
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001109static int __devinit snd_vt1724_pcm_profi(struct snd_ice1712 *ice, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001111 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 int err;
1113
1114 err = snd_pcm_new(ice->card, "ICE1724", device, 1, 1, &pcm);
1115 if (err < 0)
1116 return err;
1117
1118 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_vt1724_playback_pro_ops);
1119 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_vt1724_capture_pro_ops);
1120
1121 pcm->private_data = ice;
1122 pcm->info_flags = 0;
1123 strcpy(pcm->name, "ICE1724");
1124
1125 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001126 snd_dma_pci_data(ice->pci),
1127 256*1024, 256*1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
1129 ice->pcm_pro = pcm;
1130
1131 return 0;
1132}
1133
1134
1135/*
1136 * SPDIF PCM
1137 */
1138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139/* update spdif control bits; call with reg_lock */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001140static void update_spdif_bits(struct snd_ice1712 *ice, unsigned int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141{
1142 unsigned char cbit, disabled;
1143
1144 cbit = inb(ICEREG1724(ice, SPDIF_CFG));
1145 disabled = cbit & ~VT1724_CFG_SPDIF_OUT_EN;
1146 if (cbit != disabled)
1147 outb(disabled, ICEREG1724(ice, SPDIF_CFG));
1148 outw(val, ICEMT1724(ice, SPDIF_CTRL));
1149 if (cbit != disabled)
1150 outb(cbit, ICEREG1724(ice, SPDIF_CFG));
1151 outw(val, ICEMT1724(ice, SPDIF_CTRL));
1152}
1153
1154/* update SPDIF control bits according to the given rate */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001155static void update_spdif_rate(struct snd_ice1712 *ice, unsigned int rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156{
1157 unsigned int val, nval;
1158 unsigned long flags;
1159
1160 spin_lock_irqsave(&ice->reg_lock, flags);
1161 nval = val = inw(ICEMT1724(ice, SPDIF_CTRL));
1162 nval &= ~(7 << 12);
1163 switch (rate) {
1164 case 44100: break;
1165 case 48000: nval |= 2 << 12; break;
1166 case 32000: nval |= 3 << 12; break;
Takashi Iwai2dfbeca2005-10-12 10:04:42 +02001167 case 88200: nval |= 4 << 12; break;
1168 case 96000: nval |= 5 << 12; break;
1169 case 192000: nval |= 6 << 12; break;
1170 case 176400: nval |= 7 << 12; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 }
1172 if (val != nval)
1173 update_spdif_bits(ice, nval);
1174 spin_unlock_irqrestore(&ice->reg_lock, flags);
1175}
1176
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001177static int snd_vt1724_playback_spdif_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001179 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001180 if (!ice->force_pdma4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 update_spdif_rate(ice, substream->runtime->rate);
1182 return snd_vt1724_pcm_prepare(substream);
1183}
1184
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001185static int snd_vt1724_playback_spdif_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001187 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
1188 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
Takashi Iwai32b47da2007-01-29 15:26:36 +01001190 runtime->private_data = (void *)&vt1724_playback_spdif_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 ice->playback_con_substream = substream;
1192 if (ice->force_pdma4) {
1193 runtime->hw = snd_vt1724_2ch_stereo;
1194 set_rate_constraints(ice, substream);
1195 } else
1196 runtime->hw = snd_vt1724_spdif;
1197 snd_pcm_set_sync(substream);
1198 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
1199 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1200 VT1724_BUFFER_ALIGN);
1201 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1202 VT1724_BUFFER_ALIGN);
Takashi Iwaic93f5a12008-03-14 17:17:09 +01001203 if (ice->spdif.ops.open)
1204 ice->spdif.ops.open(ice, substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 return 0;
1206}
1207
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001208static int snd_vt1724_playback_spdif_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001210 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
1212 if (PRO_RATE_RESET)
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001213 snd_vt1724_set_pro_rate(ice, ice->pro_rate_default, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 ice->playback_con_substream = NULL;
Takashi Iwaic93f5a12008-03-14 17:17:09 +01001215 if (ice->spdif.ops.close)
1216 ice->spdif.ops.close(ice, substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
1218 return 0;
1219}
1220
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001221static int snd_vt1724_capture_spdif_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001223 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
1224 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Takashi Iwai32b47da2007-01-29 15:26:36 +01001226 runtime->private_data = (void *)&vt1724_capture_spdif_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 ice->capture_con_substream = substream;
1228 if (ice->force_rdma1) {
1229 runtime->hw = snd_vt1724_2ch_stereo;
1230 set_rate_constraints(ice, substream);
1231 } else
1232 runtime->hw = snd_vt1724_spdif;
1233 snd_pcm_set_sync(substream);
1234 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
1235 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1236 VT1724_BUFFER_ALIGN);
1237 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1238 VT1724_BUFFER_ALIGN);
Takashi Iwaic93f5a12008-03-14 17:17:09 +01001239 if (ice->spdif.ops.open)
1240 ice->spdif.ops.open(ice, substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 return 0;
1242}
1243
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001244static int snd_vt1724_capture_spdif_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001246 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
1248 if (PRO_RATE_RESET)
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001249 snd_vt1724_set_pro_rate(ice, ice->pro_rate_default, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 ice->capture_con_substream = NULL;
Takashi Iwaic93f5a12008-03-14 17:17:09 +01001251 if (ice->spdif.ops.close)
1252 ice->spdif.ops.close(ice, substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
1254 return 0;
1255}
1256
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001257static struct snd_pcm_ops snd_vt1724_playback_spdif_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 .open = snd_vt1724_playback_spdif_open,
1259 .close = snd_vt1724_playback_spdif_close,
1260 .ioctl = snd_pcm_lib_ioctl,
1261 .hw_params = snd_vt1724_pcm_hw_params,
1262 .hw_free = snd_vt1724_pcm_hw_free,
1263 .prepare = snd_vt1724_playback_spdif_prepare,
1264 .trigger = snd_vt1724_pcm_trigger,
1265 .pointer = snd_vt1724_pcm_pointer,
1266};
1267
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001268static struct snd_pcm_ops snd_vt1724_capture_spdif_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 .open = snd_vt1724_capture_spdif_open,
1270 .close = snd_vt1724_capture_spdif_close,
1271 .ioctl = snd_pcm_lib_ioctl,
1272 .hw_params = snd_vt1724_pcm_hw_params,
1273 .hw_free = snd_vt1724_pcm_hw_free,
1274 .prepare = snd_vt1724_pcm_prepare,
1275 .trigger = snd_vt1724_pcm_trigger,
1276 .pointer = snd_vt1724_pcm_pointer,
1277};
1278
1279
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001280static int __devinit snd_vt1724_pcm_spdif(struct snd_ice1712 *ice, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281{
1282 char *name;
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001283 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 int play, capt;
1285 int err;
1286
1287 if (ice->force_pdma4 ||
1288 (ice->eeprom.data[ICE_EEP2_SPDIF] & VT1724_CFG_SPDIF_OUT_INT)) {
1289 play = 1;
1290 ice->has_spdif = 1;
1291 } else
1292 play = 0;
1293 if (ice->force_rdma1 ||
1294 (ice->eeprom.data[ICE_EEP2_SPDIF] & VT1724_CFG_SPDIF_IN)) {
1295 capt = 1;
1296 ice->has_spdif = 1;
1297 } else
1298 capt = 0;
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001299 if (!play && !capt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 return 0; /* no spdif device */
1301
1302 if (ice->force_pdma4 || ice->force_rdma1)
1303 name = "ICE1724 Secondary";
1304 else
Alan Horstmann8eca7532009-01-05 18:30:04 +01001305 name = "ICE1724 IEC958";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 err = snd_pcm_new(ice->card, name, device, play, capt, &pcm);
1307 if (err < 0)
1308 return err;
1309
1310 if (play)
1311 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1312 &snd_vt1724_playback_spdif_ops);
1313 if (capt)
1314 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1315 &snd_vt1724_capture_spdif_ops);
1316
1317 pcm->private_data = ice;
1318 pcm->info_flags = 0;
1319 strcpy(pcm->name, name);
1320
1321 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001322 snd_dma_pci_data(ice->pci),
1323 64*1024, 64*1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
1325 ice->pcm = pcm;
1326
1327 return 0;
1328}
1329
1330
1331/*
1332 * independent surround PCMs
1333 */
1334
Takashi Iwai32b47da2007-01-29 15:26:36 +01001335static const struct vt1724_pcm_reg vt1724_playback_dma_regs[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 {
1337 .addr = VT1724_MT_PDMA1_ADDR,
1338 .size = VT1724_MT_PDMA1_SIZE,
1339 .count = VT1724_MT_PDMA1_COUNT,
1340 .start = VT1724_PDMA1_START,
1341 },
1342 {
1343 .addr = VT1724_MT_PDMA2_ADDR,
1344 .size = VT1724_MT_PDMA2_SIZE,
1345 .count = VT1724_MT_PDMA2_COUNT,
1346 .start = VT1724_PDMA2_START,
1347 },
1348 {
1349 .addr = VT1724_MT_PDMA3_ADDR,
1350 .size = VT1724_MT_PDMA3_SIZE,
1351 .count = VT1724_MT_PDMA3_COUNT,
1352 .start = VT1724_PDMA3_START,
1353 },
1354};
1355
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001356static int snd_vt1724_playback_indep_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001358 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 unsigned char val;
1360
1361 spin_lock_irq(&ice->reg_lock);
1362 val = 3 - substream->number;
1363 if (inb(ICEMT1724(ice, BURST)) < val)
1364 outb(val, ICEMT1724(ice, BURST));
1365 spin_unlock_irq(&ice->reg_lock);
1366 return snd_vt1724_pcm_prepare(substream);
1367}
1368
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001369static int snd_vt1724_playback_indep_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001371 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
1372 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Ingo Molnar62932df2006-01-16 16:34:20 +01001374 mutex_lock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 /* already used by PDMA0? */
1376 if (ice->pcm_reserved[substream->number]) {
Ingo Molnar62932df2006-01-16 16:34:20 +01001377 mutex_unlock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 return -EBUSY; /* FIXME: should handle blocking mode properly */
1379 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001380 mutex_unlock(&ice->open_mutex);
Takashi Iwai32b47da2007-01-29 15:26:36 +01001381 runtime->private_data = (void *)&vt1724_playback_dma_regs[substream->number];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 ice->playback_con_substream_ds[substream->number] = substream;
1383 runtime->hw = snd_vt1724_2ch_stereo;
1384 snd_pcm_set_sync(substream);
1385 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
1386 set_rate_constraints(ice, substream);
1387 return 0;
1388}
1389
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001390static int snd_vt1724_playback_indep_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001392 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
1394 if (PRO_RATE_RESET)
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001395 snd_vt1724_set_pro_rate(ice, ice->pro_rate_default, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 ice->playback_con_substream_ds[substream->number] = NULL;
1397 ice->pcm_reserved[substream->number] = NULL;
1398
1399 return 0;
1400}
1401
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001402static struct snd_pcm_ops snd_vt1724_playback_indep_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 .open = snd_vt1724_playback_indep_open,
1404 .close = snd_vt1724_playback_indep_close,
1405 .ioctl = snd_pcm_lib_ioctl,
1406 .hw_params = snd_vt1724_pcm_hw_params,
1407 .hw_free = snd_vt1724_pcm_hw_free,
1408 .prepare = snd_vt1724_playback_indep_prepare,
1409 .trigger = snd_vt1724_pcm_trigger,
1410 .pointer = snd_vt1724_pcm_pointer,
1411};
1412
1413
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001414static int __devinit snd_vt1724_pcm_indep(struct snd_ice1712 *ice, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001416 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 int play;
1418 int err;
1419
1420 play = ice->num_total_dacs / 2 - 1;
1421 if (play <= 0)
1422 return 0;
1423
1424 err = snd_pcm_new(ice->card, "ICE1724 Surrounds", device, play, 0, &pcm);
1425 if (err < 0)
1426 return err;
1427
1428 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1429 &snd_vt1724_playback_indep_ops);
1430
1431 pcm->private_data = ice;
1432 pcm->info_flags = 0;
1433 strcpy(pcm->name, "ICE1724 Surround PCM");
1434
1435 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001436 snd_dma_pci_data(ice->pci),
1437 64*1024, 64*1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
1439 ice->pcm_ds = pcm;
1440
1441 return 0;
1442}
1443
1444
1445/*
1446 * Mixer section
1447 */
1448
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001449static int __devinit snd_vt1724_ac97_mixer(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450{
1451 int err;
1452
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001453 if (!(ice->eeprom.data[ICE_EEP2_ACLINK] & VT1724_CFG_PRO_I2S)) {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001454 struct snd_ac97_bus *pbus;
1455 struct snd_ac97_template ac97;
1456 static struct snd_ac97_bus_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 .write = snd_vt1724_ac97_write,
1458 .read = snd_vt1724_ac97_read,
1459 };
1460
1461 /* cold reset */
1462 outb(inb(ICEMT1724(ice, AC97_CMD)) | 0x80, ICEMT1724(ice, AC97_CMD));
1463 mdelay(5); /* FIXME */
1464 outb(inb(ICEMT1724(ice, AC97_CMD)) & ~0x80, ICEMT1724(ice, AC97_CMD));
1465
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001466 err = snd_ac97_bus(ice->card, 0, &ops, NULL, &pbus);
1467 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 return err;
1469 memset(&ac97, 0, sizeof(ac97));
1470 ac97.private_data = ice;
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001471 err = snd_ac97_mixer(pbus, &ac97, &ice->ac97);
1472 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 printk(KERN_WARNING "ice1712: cannot initialize pro ac97, skipped\n");
1474 else
1475 return 0;
1476 }
1477 /* I2S mixer only */
1478 strcat(ice->card->mixername, "ICE1724 - multitrack");
1479 return 0;
1480}
1481
1482/*
1483 *
1484 */
1485
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001486static inline unsigned int eeprom_triple(struct snd_ice1712 *ice, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487{
1488 return (unsigned int)ice->eeprom.data[idx] | \
1489 ((unsigned int)ice->eeprom.data[idx + 1] << 8) | \
1490 ((unsigned int)ice->eeprom.data[idx + 2] << 16);
1491}
1492
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001493static void snd_vt1724_proc_read(struct snd_info_entry *entry,
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001494 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001496 struct snd_ice1712 *ice = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 unsigned int idx;
1498
1499 snd_iprintf(buffer, "%s\n\n", ice->card->longname);
1500 snd_iprintf(buffer, "EEPROM:\n");
1501
1502 snd_iprintf(buffer, " Subvendor : 0x%x\n", ice->eeprom.subvendor);
1503 snd_iprintf(buffer, " Size : %i bytes\n", ice->eeprom.size);
1504 snd_iprintf(buffer, " Version : %i\n", ice->eeprom.version);
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001505 snd_iprintf(buffer, " System Config : 0x%x\n",
1506 ice->eeprom.data[ICE_EEP2_SYSCONF]);
1507 snd_iprintf(buffer, " ACLink : 0x%x\n",
1508 ice->eeprom.data[ICE_EEP2_ACLINK]);
1509 snd_iprintf(buffer, " I2S : 0x%x\n",
1510 ice->eeprom.data[ICE_EEP2_I2S]);
1511 snd_iprintf(buffer, " S/PDIF : 0x%x\n",
1512 ice->eeprom.data[ICE_EEP2_SPDIF]);
1513 snd_iprintf(buffer, " GPIO direction : 0x%x\n",
1514 ice->eeprom.gpiodir);
1515 snd_iprintf(buffer, " GPIO mask : 0x%x\n",
1516 ice->eeprom.gpiomask);
1517 snd_iprintf(buffer, " GPIO state : 0x%x\n",
1518 ice->eeprom.gpiostate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 for (idx = 0x12; idx < ice->eeprom.size; idx++)
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001520 snd_iprintf(buffer, " Extra #%02i : 0x%x\n",
1521 idx, ice->eeprom.data[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
1523 snd_iprintf(buffer, "\nRegisters:\n");
1524
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001525 snd_iprintf(buffer, " PSDOUT03 : 0x%08x\n",
1526 (unsigned)inl(ICEMT1724(ice, ROUTE_PLAYBACK)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 for (idx = 0x0; idx < 0x20 ; idx++)
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001528 snd_iprintf(buffer, " CCS%02x : 0x%02x\n",
1529 idx, inb(ice->port+idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 for (idx = 0x0; idx < 0x30 ; idx++)
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001531 snd_iprintf(buffer, " MT%02x : 0x%02x\n",
1532 idx, inb(ice->profi_port+idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533}
1534
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001535static void __devinit snd_vt1724_proc_init(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001537 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001539 if (!snd_card_proc_new(ice->card, "ice1724", &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02001540 snd_info_set_text_ops(entry, ice, snd_vt1724_proc_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541}
1542
1543/*
1544 *
1545 */
1546
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001547static int snd_vt1724_eeprom_info(struct snd_kcontrol *kcontrol,
1548 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549{
1550 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001551 uinfo->count = sizeof(struct snd_ice1712_eeprom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 return 0;
1553}
1554
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001555static int snd_vt1724_eeprom_get(struct snd_kcontrol *kcontrol,
1556 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001558 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001559
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 memcpy(ucontrol->value.bytes.data, &ice->eeprom, sizeof(ice->eeprom));
1561 return 0;
1562}
1563
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01001564static struct snd_kcontrol_new snd_vt1724_eeprom __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1566 .name = "ICE1724 EEPROM",
1567 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1568 .info = snd_vt1724_eeprom_info,
1569 .get = snd_vt1724_eeprom_get
1570};
1571
1572/*
1573 */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001574static int snd_vt1724_spdif_info(struct snd_kcontrol *kcontrol,
1575 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576{
1577 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1578 uinfo->count = 1;
1579 return 0;
1580}
1581
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001582static unsigned int encode_spdif_bits(struct snd_aes_iec958 *diga)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583{
Takashi Iwai2dfbeca2005-10-12 10:04:42 +02001584 unsigned int val, rbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
1586 val = diga->status[0] & 0x03; /* professional, non-audio */
1587 if (val & 0x01) {
1588 /* professional */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001589 if ((diga->status[0] & IEC958_AES0_PRO_EMPHASIS) ==
1590 IEC958_AES0_PRO_EMPHASIS_5015)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 val |= 1U << 3;
Takashi Iwai2dfbeca2005-10-12 10:04:42 +02001592 rbits = (diga->status[4] >> 3) & 0x0f;
1593 if (rbits) {
1594 switch (rbits) {
1595 case 2: val |= 5 << 12; break; /* 96k */
1596 case 3: val |= 6 << 12; break; /* 192k */
1597 case 10: val |= 4 << 12; break; /* 88.2k */
1598 case 11: val |= 7 << 12; break; /* 176.4k */
1599 }
1600 } else {
1601 switch (diga->status[0] & IEC958_AES0_PRO_FS) {
1602 case IEC958_AES0_PRO_FS_44100:
1603 break;
1604 case IEC958_AES0_PRO_FS_32000:
1605 val |= 3U << 12;
1606 break;
1607 default:
1608 val |= 2U << 12;
1609 break;
1610 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 }
1612 } else {
1613 /* consumer */
1614 val |= diga->status[1] & 0x04; /* copyright */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001615 if ((diga->status[0] & IEC958_AES0_CON_EMPHASIS) ==
1616 IEC958_AES0_CON_EMPHASIS_5015)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 val |= 1U << 3;
1618 val |= (unsigned int)(diga->status[1] & 0x3f) << 4; /* category */
1619 val |= (unsigned int)(diga->status[3] & IEC958_AES3_CON_FS) << 12; /* fs */
1620 }
1621 return val;
1622}
1623
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001624static void decode_spdif_bits(struct snd_aes_iec958 *diga, unsigned int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625{
1626 memset(diga->status, 0, sizeof(diga->status));
1627 diga->status[0] = val & 0x03; /* professional, non-audio */
1628 if (val & 0x01) {
1629 /* professional */
1630 if (val & (1U << 3))
1631 diga->status[0] |= IEC958_AES0_PRO_EMPHASIS_5015;
1632 switch ((val >> 12) & 0x7) {
1633 case 0:
1634 break;
1635 case 2:
1636 diga->status[0] |= IEC958_AES0_PRO_FS_32000;
1637 break;
1638 default:
1639 diga->status[0] |= IEC958_AES0_PRO_FS_48000;
1640 break;
1641 }
1642 } else {
1643 /* consumer */
1644 diga->status[0] |= val & (1U << 2); /* copyright */
1645 if (val & (1U << 3))
1646 diga->status[0] |= IEC958_AES0_CON_EMPHASIS_5015;
1647 diga->status[1] |= (val >> 4) & 0x3f; /* category */
1648 diga->status[3] |= (val >> 12) & 0x07; /* fs */
1649 }
1650}
1651
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001652static int snd_vt1724_spdif_default_get(struct snd_kcontrol *kcontrol,
1653 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001655 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 unsigned int val;
1657 val = inw(ICEMT1724(ice, SPDIF_CTRL));
1658 decode_spdif_bits(&ucontrol->value.iec958, val);
1659 return 0;
1660}
1661
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001662static int snd_vt1724_spdif_default_put(struct snd_kcontrol *kcontrol,
1663 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001665 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 unsigned int val, old;
1667
1668 val = encode_spdif_bits(&ucontrol->value.iec958);
1669 spin_lock_irq(&ice->reg_lock);
1670 old = inw(ICEMT1724(ice, SPDIF_CTRL));
1671 if (val != old)
1672 update_spdif_bits(ice, val);
1673 spin_unlock_irq(&ice->reg_lock);
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001674 return val != old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675}
1676
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01001677static struct snd_kcontrol_new snd_vt1724_spdif_default __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678{
1679 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001680 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 .info = snd_vt1724_spdif_info,
1682 .get = snd_vt1724_spdif_default_get,
1683 .put = snd_vt1724_spdif_default_put
1684};
1685
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001686static int snd_vt1724_spdif_maskc_get(struct snd_kcontrol *kcontrol,
1687 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688{
1689 ucontrol->value.iec958.status[0] = IEC958_AES0_NONAUDIO |
1690 IEC958_AES0_PROFESSIONAL |
1691 IEC958_AES0_CON_NOT_COPYRIGHT |
1692 IEC958_AES0_CON_EMPHASIS;
1693 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_ORIGINAL |
1694 IEC958_AES1_CON_CATEGORY;
1695 ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS;
1696 return 0;
1697}
1698
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001699static int snd_vt1724_spdif_maskp_get(struct snd_kcontrol *kcontrol,
1700 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701{
1702 ucontrol->value.iec958.status[0] = IEC958_AES0_NONAUDIO |
1703 IEC958_AES0_PROFESSIONAL |
1704 IEC958_AES0_PRO_FS |
1705 IEC958_AES0_PRO_EMPHASIS;
1706 return 0;
1707}
1708
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01001709static struct snd_kcontrol_new snd_vt1724_spdif_maskc __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, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 .info = snd_vt1724_spdif_info,
1715 .get = snd_vt1724_spdif_maskc_get,
1716};
1717
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01001718static struct snd_kcontrol_new snd_vt1724_spdif_maskp __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719{
1720 .access = SNDRV_CTL_ELEM_ACCESS_READ,
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001721 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001722 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 .info = snd_vt1724_spdif_info,
1724 .get = snd_vt1724_spdif_maskp_get,
1725};
1726
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001727#define snd_vt1724_spdif_sw_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001729static int snd_vt1724_spdif_sw_get(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);
1733 ucontrol->value.integer.value[0] = inb(ICEREG1724(ice, SPDIF_CFG)) &
1734 VT1724_CFG_SPDIF_OUT_EN ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 return 0;
1736}
1737
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001738static int snd_vt1724_spdif_sw_put(struct snd_kcontrol *kcontrol,
1739 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001741 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 unsigned char old, val;
1743
1744 spin_lock_irq(&ice->reg_lock);
1745 old = val = inb(ICEREG1724(ice, SPDIF_CFG));
1746 val &= ~VT1724_CFG_SPDIF_OUT_EN;
1747 if (ucontrol->value.integer.value[0])
1748 val |= VT1724_CFG_SPDIF_OUT_EN;
1749 if (old != val)
1750 outb(val, ICEREG1724(ice, SPDIF_CFG));
1751 spin_unlock_irq(&ice->reg_lock);
1752 return old != val;
1753}
1754
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01001755static struct snd_kcontrol_new snd_vt1724_spdif_switch __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756{
1757 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1758 /* FIXME: the following conflict with IEC958 Playback Route */
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001759 /* .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH), */
1760 .name = SNDRV_CTL_NAME_IEC958("Output ", NONE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 .info = snd_vt1724_spdif_sw_info,
1762 .get = snd_vt1724_spdif_sw_get,
1763 .put = snd_vt1724_spdif_sw_put
1764};
1765
1766
1767#if 0 /* NOT USED YET */
1768/*
1769 * GPIO access from extern
1770 */
1771
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001772#define snd_vt1724_gpio_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001774int snd_vt1724_gpio_get(struct snd_kcontrol *kcontrol,
1775 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001777 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 int shift = kcontrol->private_value & 0xff;
1779 int invert = (kcontrol->private_value & (1<<24)) ? 1 : 0;
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001780
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 snd_ice1712_save_gpio_status(ice);
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001782 ucontrol->value.integer.value[0] =
1783 (snd_ice1712_gpio_read(ice) & (1 << shift) ? 1 : 0) ^ invert;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 snd_ice1712_restore_gpio_status(ice);
1785 return 0;
1786}
1787
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001788int snd_ice1712_gpio_put(struct snd_kcontrol *kcontrol,
1789 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001791 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 int shift = kcontrol->private_value & 0xff;
1793 int invert = (kcontrol->private_value & (1<<24)) ? mask : 0;
1794 unsigned int val, nval;
1795
1796 if (kcontrol->private_value & (1 << 31))
1797 return -EPERM;
1798 nval = (ucontrol->value.integer.value[0] ? (1 << shift) : 0) ^ invert;
1799 snd_ice1712_save_gpio_status(ice);
1800 val = snd_ice1712_gpio_read(ice);
1801 nval |= val & ~(1 << shift);
1802 if (val != nval)
1803 snd_ice1712_gpio_write(ice, nval);
1804 snd_ice1712_restore_gpio_status(ice);
1805 return val != nval;
1806}
1807#endif /* NOT USED YET */
1808
1809/*
1810 * rate
1811 */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001812static int snd_vt1724_pro_internal_clock_info(struct snd_kcontrol *kcontrol,
1813 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001815 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
1817 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1818 uinfo->count = 1;
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001819 uinfo->value.enumerated.items = ice->hw_rates->count + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1821 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001822 if (uinfo->value.enumerated.item == uinfo->value.enumerated.items - 1)
1823 strcpy(uinfo->value.enumerated.name, "IEC958 Input");
1824 else
1825 sprintf(uinfo->value.enumerated.name, "%d",
1826 ice->hw_rates->list[uinfo->value.enumerated.item]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 return 0;
1828}
1829
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001830static int snd_vt1724_pro_internal_clock_get(struct snd_kcontrol *kcontrol,
1831 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001833 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001834 unsigned int i, rate;
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001835
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 spin_lock_irq(&ice->reg_lock);
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001837 if (ice->is_spdif_master(ice)) {
1838 ucontrol->value.enumerated.item[0] = ice->hw_rates->count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 } else {
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001840 rate = ice->get_rate(ice);
1841 ucontrol->value.enumerated.item[0] = 0;
1842 for (i = 0; i < ice->hw_rates->count; i++) {
1843 if (ice->hw_rates->list[i] == rate) {
1844 ucontrol->value.enumerated.item[0] = i;
1845 break;
1846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 }
1849 spin_unlock_irq(&ice->reg_lock);
1850 return 0;
1851}
1852
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001853/* setting clock to external - SPDIF */
1854static void stdclock_set_spdif_clock(struct snd_ice1712 *ice)
1855{
1856 unsigned char oval;
1857 unsigned char i2s_oval;
1858 oval = inb(ICEMT1724(ice, RATE));
1859 outb(oval | VT1724_SPDIF_MASTER, ICEMT1724(ice, RATE));
1860 /* setting 256fs */
1861 i2s_oval = inb(ICEMT1724(ice, I2S_FORMAT));
1862 outb(i2s_oval & ~VT1724_MT_I2S_MCLK_128X, ICEMT1724(ice, I2S_FORMAT));
1863}
1864
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001865static int snd_vt1724_pro_internal_clock_put(struct snd_kcontrol *kcontrol,
1866 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001868 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001869 unsigned int old_rate, new_rate;
1870 unsigned int item = ucontrol->value.enumerated.item[0];
1871 unsigned int spdif = ice->hw_rates->count;
1872
1873 if (item > spdif)
1874 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
1876 spin_lock_irq(&ice->reg_lock);
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001877 if (ice->is_spdif_master(ice))
1878 old_rate = 0;
1879 else
1880 old_rate = ice->get_rate(ice);
1881 if (item == spdif) {
1882 /* switching to external clock via SPDIF */
1883 ice->set_spdif_clock(ice);
1884 new_rate = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 } else {
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001886 /* internal on-card clock */
1887 new_rate = ice->hw_rates->list[item];
1888 ice->pro_rate_default = new_rate;
1889 spin_unlock_irq(&ice->reg_lock);
1890 snd_vt1724_set_pro_rate(ice, ice->pro_rate_default, 1);
1891 spin_lock_irq(&ice->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 spin_unlock_irq(&ice->reg_lock);
1894
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001895 /* the first reset to the SPDIF master mode? */
1896 if (old_rate != new_rate && !new_rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 /* notify akm chips as well */
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001898 unsigned int i;
1899 if (ice->gpio.set_pro_rate)
1900 ice->gpio.set_pro_rate(ice, 0);
1901 for (i = 0; i < ice->akm_codecs; i++) {
1902 if (ice->akm[i].ops.set_rate_val)
1903 ice->akm[i].ops.set_rate_val(&ice->akm[i], 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 }
1905 }
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001906 return old_rate != new_rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907}
1908
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01001909static struct snd_kcontrol_new snd_vt1724_pro_internal_clock __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1911 .name = "Multi Track Internal Clock",
1912 .info = snd_vt1724_pro_internal_clock_info,
1913 .get = snd_vt1724_pro_internal_clock_get,
1914 .put = snd_vt1724_pro_internal_clock_put
1915};
1916
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001917#define snd_vt1724_pro_rate_locking_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001919static int snd_vt1724_pro_rate_locking_get(struct snd_kcontrol *kcontrol,
1920 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921{
1922 ucontrol->value.integer.value[0] = PRO_RATE_LOCKED;
1923 return 0;
1924}
1925
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001926static int snd_vt1724_pro_rate_locking_put(struct snd_kcontrol *kcontrol,
1927 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001929 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 int change = 0, nval;
1931
1932 nval = ucontrol->value.integer.value[0] ? 1 : 0;
1933 spin_lock_irq(&ice->reg_lock);
1934 change = PRO_RATE_LOCKED != nval;
1935 PRO_RATE_LOCKED = nval;
1936 spin_unlock_irq(&ice->reg_lock);
1937 return change;
1938}
1939
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01001940static struct snd_kcontrol_new snd_vt1724_pro_rate_locking __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1942 .name = "Multi Track Rate Locking",
1943 .info = snd_vt1724_pro_rate_locking_info,
1944 .get = snd_vt1724_pro_rate_locking_get,
1945 .put = snd_vt1724_pro_rate_locking_put
1946};
1947
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001948#define snd_vt1724_pro_rate_reset_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001950static int snd_vt1724_pro_rate_reset_get(struct snd_kcontrol *kcontrol,
1951 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952{
1953 ucontrol->value.integer.value[0] = PRO_RATE_RESET ? 1 : 0;
1954 return 0;
1955}
1956
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001957static int snd_vt1724_pro_rate_reset_put(struct snd_kcontrol *kcontrol,
1958 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001960 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 int change = 0, nval;
1962
1963 nval = ucontrol->value.integer.value[0] ? 1 : 0;
1964 spin_lock_irq(&ice->reg_lock);
1965 change = PRO_RATE_RESET != nval;
1966 PRO_RATE_RESET = nval;
1967 spin_unlock_irq(&ice->reg_lock);
1968 return change;
1969}
1970
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01001971static struct snd_kcontrol_new snd_vt1724_pro_rate_reset __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1973 .name = "Multi Track Rate Reset",
1974 .info = snd_vt1724_pro_rate_reset_info,
1975 .get = snd_vt1724_pro_rate_reset_get,
1976 .put = snd_vt1724_pro_rate_reset_put
1977};
1978
1979
1980/*
1981 * routing
1982 */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001983static int snd_vt1724_pro_route_info(struct snd_kcontrol *kcontrol,
1984 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985{
1986 static char *texts[] = {
1987 "PCM Out", /* 0 */
1988 "H/W In 0", "H/W In 1", /* 1-2 */
1989 "IEC958 In L", "IEC958 In R", /* 3-4 */
1990 };
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001991
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1993 uinfo->count = 1;
1994 uinfo->value.enumerated.items = 5;
1995 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1996 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
1997 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1998 return 0;
1999}
2000
2001static inline int analog_route_shift(int idx)
2002{
2003 return (idx % 2) * 12 + ((idx / 2) * 3) + 8;
2004}
2005
2006static inline int digital_route_shift(int idx)
2007{
2008 return idx * 3;
2009}
2010
Takashi Iwai2bf864a2009-05-06 17:30:17 +02002011int snd_ice1724_get_route_val(struct snd_ice1712 *ice, int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012{
2013 unsigned long val;
2014 unsigned char eitem;
Takashi Iwai32b47da2007-01-29 15:26:36 +01002015 static const unsigned char xlate[8] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 0, 255, 1, 2, 255, 255, 3, 4,
2017 };
2018
2019 val = inl(ICEMT1724(ice, ROUTE_PLAYBACK));
2020 val >>= shift;
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002021 val &= 7; /* we now have 3 bits per output */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 eitem = xlate[val];
2023 if (eitem == 255) {
2024 snd_BUG();
2025 return 0;
2026 }
2027 return eitem;
2028}
2029
Takashi Iwai2bf864a2009-05-06 17:30:17 +02002030int snd_ice1724_put_route_val(struct snd_ice1712 *ice, unsigned int val,
2031 int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032{
2033 unsigned int old_val, nval;
2034 int change;
Takashi Iwai32b47da2007-01-29 15:26:36 +01002035 static const unsigned char xroute[8] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 0, /* PCM */
2037 2, /* PSDIN0 Left */
2038 3, /* PSDIN0 Right */
2039 6, /* SPDIN Left */
2040 7, /* SPDIN Right */
2041 };
2042
2043 nval = xroute[val % 5];
2044 val = old_val = inl(ICEMT1724(ice, ROUTE_PLAYBACK));
2045 val &= ~(0x07 << shift);
2046 val |= nval << shift;
2047 change = val != old_val;
2048 if (change)
2049 outl(val, ICEMT1724(ice, ROUTE_PLAYBACK));
2050 return change;
2051}
2052
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002053static int snd_vt1724_pro_route_analog_get(struct snd_kcontrol *kcontrol,
2054 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002056 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002058 ucontrol->value.enumerated.item[0] =
Takashi Iwai2bf864a2009-05-06 17:30:17 +02002059 snd_ice1724_get_route_val(ice, analog_route_shift(idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 return 0;
2061}
2062
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002063static int snd_vt1724_pro_route_analog_put(struct snd_kcontrol *kcontrol,
2064 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002066 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai2bf864a2009-05-06 17:30:17 +02002068 return snd_ice1724_put_route_val(ice,
2069 ucontrol->value.enumerated.item[0],
2070 analog_route_shift(idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071}
2072
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002073static int snd_vt1724_pro_route_spdif_get(struct snd_kcontrol *kcontrol,
2074 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002076 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002078 ucontrol->value.enumerated.item[0] =
Takashi Iwai2bf864a2009-05-06 17:30:17 +02002079 snd_ice1724_get_route_val(ice, digital_route_shift(idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 return 0;
2081}
2082
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002083static int snd_vt1724_pro_route_spdif_put(struct snd_kcontrol *kcontrol,
2084 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002086 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai2bf864a2009-05-06 17:30:17 +02002088 return snd_ice1724_put_route_val(ice,
2089 ucontrol->value.enumerated.item[0],
2090 digital_route_shift(idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091}
2092
Takashi Iwai2bf864a2009-05-06 17:30:17 +02002093static struct snd_kcontrol_new snd_vt1724_mixer_pro_analog_route __devinitdata =
2094{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2096 .name = "H/W Playback Route",
2097 .info = snd_vt1724_pro_route_info,
2098 .get = snd_vt1724_pro_route_analog_get,
2099 .put = snd_vt1724_pro_route_analog_put,
2100};
2101
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01002102static struct snd_kcontrol_new snd_vt1724_mixer_pro_spdif_route __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002104 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, NONE) "Route",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 .info = snd_vt1724_pro_route_info,
2106 .get = snd_vt1724_pro_route_spdif_get,
2107 .put = snd_vt1724_pro_route_spdif_put,
2108 .count = 2,
2109};
2110
2111
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002112static int snd_vt1724_pro_peak_info(struct snd_kcontrol *kcontrol,
2113 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114{
2115 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2116 uinfo->count = 22; /* FIXME: for compatibility with ice1712... */
2117 uinfo->value.integer.min = 0;
2118 uinfo->value.integer.max = 255;
2119 return 0;
2120}
2121
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002122static int snd_vt1724_pro_peak_get(struct snd_kcontrol *kcontrol,
2123 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002125 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 int idx;
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002127
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 spin_lock_irq(&ice->reg_lock);
2129 for (idx = 0; idx < 22; idx++) {
2130 outb(idx, ICEMT1724(ice, MONITOR_PEAKINDEX));
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002131 ucontrol->value.integer.value[idx] =
2132 inb(ICEMT1724(ice, MONITOR_PEAKDATA));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 }
2134 spin_unlock_irq(&ice->reg_lock);
2135 return 0;
2136}
2137
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01002138static struct snd_kcontrol_new snd_vt1724_mixer_pro_peak __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2140 .name = "Multi Track Peak",
2141 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2142 .info = snd_vt1724_pro_peak_info,
2143 .get = snd_vt1724_pro_peak_get
2144};
2145
2146/*
2147 *
2148 */
2149
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01002150static struct snd_ice1712_card_info no_matched __devinitdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01002152static struct snd_ice1712_card_info *card_tables[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 snd_vt1724_revo_cards,
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002154 snd_vt1724_amp_cards,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 snd_vt1724_aureon_cards,
2156 snd_vt1720_mobo_cards,
2157 snd_vt1720_pontis_cards,
Julian Scheel6b8d6e52008-01-16 19:50:00 +01002158 snd_vt1724_prodigy_hifi_cards,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 snd_vt1724_prodigy192_cards,
2160 snd_vt1724_juli_cards,
Takashi Iwai72cbfd42009-05-06 17:33:19 +02002161 snd_vt1724_maya44_cards,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 snd_vt1724_phase_cards,
Clement Guedezf6cdab52007-01-08 10:48:41 +01002163 snd_vt1724_wtm_cards,
Shin-ya Okadaf31639b2007-10-23 15:08:18 +02002164 snd_vt1724_se_cards,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 NULL,
2166};
2167
2168
2169/*
2170 */
2171
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002172static void wait_i2c_busy(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173{
2174 int t = 0x10000;
2175 while ((inb(ICEREG1724(ice, I2C_CTRL)) & VT1724_I2C_BUSY) && t--)
2176 ;
2177 if (t == -1)
2178 printk(KERN_ERR "ice1724: i2c busy timeout\n");
2179}
2180
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002181unsigned char snd_vt1724_read_i2c(struct snd_ice1712 *ice,
2182 unsigned char dev, unsigned char addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183{
2184 unsigned char val;
2185
Ingo Molnar62932df2006-01-16 16:34:20 +01002186 mutex_lock(&ice->i2c_mutex);
Pavel Hofmanacec30f2007-12-03 12:37:17 +01002187 wait_i2c_busy(ice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 outb(addr, ICEREG1724(ice, I2C_BYTE_ADDR));
2189 outb(dev & ~VT1724_I2C_WRITE, ICEREG1724(ice, I2C_DEV_ADDR));
2190 wait_i2c_busy(ice);
2191 val = inb(ICEREG1724(ice, I2C_DATA));
Ingo Molnar62932df2006-01-16 16:34:20 +01002192 mutex_unlock(&ice->i2c_mutex);
Takashi Iwaie2ea7cfc2009-02-05 16:07:02 +01002193 /*
2194 printk(KERN_DEBUG "i2c_read: [0x%x,0x%x] = 0x%x\n", dev, addr, val);
2195 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 return val;
2197}
2198
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002199void snd_vt1724_write_i2c(struct snd_ice1712 *ice,
2200 unsigned char dev, unsigned char addr, unsigned char data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201{
Ingo Molnar62932df2006-01-16 16:34:20 +01002202 mutex_lock(&ice->i2c_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 wait_i2c_busy(ice);
Takashi Iwaie2ea7cfc2009-02-05 16:07:02 +01002204 /*
2205 printk(KERN_DEBUG "i2c_write: [0x%x,0x%x] = 0x%x\n", dev, addr, data);
2206 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 outb(addr, ICEREG1724(ice, I2C_BYTE_ADDR));
2208 outb(data, ICEREG1724(ice, I2C_DATA));
2209 outb(dev | VT1724_I2C_WRITE, ICEREG1724(ice, I2C_DEV_ADDR));
2210 wait_i2c_busy(ice);
Ingo Molnar62932df2006-01-16 16:34:20 +01002211 mutex_unlock(&ice->i2c_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212}
2213
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002214static int __devinit snd_vt1724_read_eeprom(struct snd_ice1712 *ice,
2215 const char *modelname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216{
2217 const int dev = 0xa0; /* EEPROM device address */
2218 unsigned int i, size;
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01002219 struct snd_ice1712_card_info * const *tbl, *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002221 if (!modelname || !*modelname) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 ice->eeprom.subvendor = 0;
2223 if ((inb(ICEREG1724(ice, I2C_CTRL)) & VT1724_I2C_EEPROM) != 0)
2224 ice->eeprom.subvendor =
2225 (snd_vt1724_read_i2c(ice, dev, 0x00) << 0) |
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002226 (snd_vt1724_read_i2c(ice, dev, 0x01) << 8) |
2227 (snd_vt1724_read_i2c(ice, dev, 0x02) << 16) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 (snd_vt1724_read_i2c(ice, dev, 0x03) << 24);
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002229 if (ice->eeprom.subvendor == 0 ||
2230 ice->eeprom.subvendor == (unsigned int)-1) {
2231 /* invalid subvendor from EEPROM, try the PCI
2232 * subststem ID instead
2233 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 u16 vendor, device;
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002235 pci_read_config_word(ice->pci, PCI_SUBSYSTEM_VENDOR_ID,
2236 &vendor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 pci_read_config_word(ice->pci, PCI_SUBSYSTEM_ID, &device);
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002238 ice->eeprom.subvendor =
2239 ((unsigned int)swab16(vendor) << 16) | swab16(device);
2240 if (ice->eeprom.subvendor == 0 ||
2241 ice->eeprom.subvendor == (unsigned int)-1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 printk(KERN_ERR "ice1724: No valid ID is found\n");
2243 return -ENXIO;
2244 }
2245 }
2246 }
2247 for (tbl = card_tables; *tbl; tbl++) {
2248 for (c = *tbl; c->subvendor; c++) {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002249 if (modelname && c->model &&
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002250 !strcmp(modelname, c->model)) {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002251 printk(KERN_INFO "ice1724: Using board model %s\n",
2252 c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 ice->eeprom.subvendor = c->subvendor;
2254 } else if (c->subvendor != ice->eeprom.subvendor)
2255 continue;
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002256 if (!c->eeprom_size || !c->eeprom_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 goto found;
2258 /* if the EEPROM is given by the driver, use it */
2259 snd_printdd("using the defined eeprom..\n");
2260 ice->eeprom.version = 2;
2261 ice->eeprom.size = c->eeprom_size + 6;
2262 memcpy(ice->eeprom.data, c->eeprom_data, c->eeprom_size);
2263 goto read_skipped;
2264 }
2265 }
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002266 printk(KERN_WARNING "ice1724: No matching model found for ID 0x%x\n",
2267 ice->eeprom.subvendor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
2269 found:
2270 ice->eeprom.size = snd_vt1724_read_i2c(ice, dev, 0x04);
2271 if (ice->eeprom.size < 6)
2272 ice->eeprom.size = 32;
2273 else if (ice->eeprom.size > 32) {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002274 printk(KERN_ERR "ice1724: Invalid EEPROM (size = %i)\n",
2275 ice->eeprom.size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 return -EIO;
2277 }
2278 ice->eeprom.version = snd_vt1724_read_i2c(ice, dev, 0x05);
2279 if (ice->eeprom.version != 2)
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002280 printk(KERN_WARNING "ice1724: Invalid EEPROM version %i\n",
2281 ice->eeprom.version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 size = ice->eeprom.size - 6;
2283 for (i = 0; i < size; i++)
2284 ice->eeprom.data[i] = snd_vt1724_read_i2c(ice, dev, i + 6);
2285
2286 read_skipped:
2287 ice->eeprom.gpiomask = eeprom_triple(ice, ICE_EEP2_GPIO_MASK);
2288 ice->eeprom.gpiostate = eeprom_triple(ice, ICE_EEP2_GPIO_STATE);
2289 ice->eeprom.gpiodir = eeprom_triple(ice, ICE_EEP2_GPIO_DIR);
2290
2291 return 0;
2292}
2293
2294
2295
Takashi Iwaicd775382009-07-02 10:28:56 +02002296static void snd_vt1724_chip_reset(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297{
2298 outb(VT1724_RESET , ICEREG1724(ice, CONTROL));
Takashi Iwaid82b64f2009-05-06 17:25:42 +02002299 inb(ICEREG1724(ice, CONTROL)); /* pci posting flush */
Karsten Wiese988f0662008-04-22 12:52:15 +02002300 msleep(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 outb(0, ICEREG1724(ice, CONTROL));
Takashi Iwaid82b64f2009-05-06 17:25:42 +02002302 inb(ICEREG1724(ice, CONTROL)); /* pci posting flush */
Karsten Wiese988f0662008-04-22 12:52:15 +02002303 msleep(10);
2304}
2305
Igor Chernyshevb40e9532009-06-25 09:31:07 +02002306static int snd_vt1724_chip_init(struct snd_ice1712 *ice)
Karsten Wiese988f0662008-04-22 12:52:15 +02002307{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 outb(ice->eeprom.data[ICE_EEP2_SYSCONF], ICEREG1724(ice, SYS_CFG));
2309 outb(ice->eeprom.data[ICE_EEP2_ACLINK], ICEREG1724(ice, AC97_CFG));
2310 outb(ice->eeprom.data[ICE_EEP2_I2S], ICEREG1724(ice, I2S_FEATURES));
2311 outb(ice->eeprom.data[ICE_EEP2_SPDIF], ICEREG1724(ice, SPDIF_CFG));
2312
2313 ice->gpio.write_mask = ice->eeprom.gpiomask;
2314 ice->gpio.direction = ice->eeprom.gpiodir;
2315 snd_vt1724_set_gpio_mask(ice, ice->eeprom.gpiomask);
2316 snd_vt1724_set_gpio_dir(ice, ice->eeprom.gpiodir);
2317 snd_vt1724_set_gpio_data(ice, ice->eeprom.gpiostate);
2318
2319 outb(0, ICEREG1724(ice, POWERDOWN));
2320
Igor Chernyshevb40e9532009-06-25 09:31:07 +02002321 /* MPU_RX and TX irq masks are cleared later dynamically */
2322 outb(VT1724_IRQ_MPU_RX | VT1724_IRQ_MPU_TX , ICEREG1724(ice, IRQMASK));
2323
2324 /* don't handle FIFO overrun/underruns (just yet),
2325 * since they cause machine lockups
2326 */
2327 outb(VT1724_MULTI_FIFO_ERR, ICEMT1724(ice, DMA_INT_MASK));
2328
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 return 0;
2330}
2331
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002332static int __devinit snd_vt1724_spdif_build_controls(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333{
2334 int err;
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002335 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
Takashi Iwaida3cec32008-08-08 17:12:14 +02002337 if (snd_BUG_ON(!ice->pcm))
2338 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339
Takashi Iwai2bf864a2009-05-06 17:30:17 +02002340 if (!ice->own_routing) {
2341 err = snd_ctl_add(ice->card,
2342 snd_ctl_new1(&snd_vt1724_mixer_pro_spdif_route, ice));
2343 if (err < 0)
2344 return err;
2345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346
2347 err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_spdif_switch, ice));
2348 if (err < 0)
2349 return err;
2350
2351 err = snd_ctl_add(ice->card, kctl = snd_ctl_new1(&snd_vt1724_spdif_default, ice));
2352 if (err < 0)
2353 return err;
2354 kctl->id.device = ice->pcm->device;
2355 err = snd_ctl_add(ice->card, kctl = snd_ctl_new1(&snd_vt1724_spdif_maskc, ice));
2356 if (err < 0)
2357 return err;
2358 kctl->id.device = ice->pcm->device;
2359 err = snd_ctl_add(ice->card, kctl = snd_ctl_new1(&snd_vt1724_spdif_maskp, ice));
2360 if (err < 0)
2361 return err;
2362 kctl->id.device = ice->pcm->device;
2363#if 0 /* use default only */
2364 err = snd_ctl_add(ice->card, kctl = snd_ctl_new1(&snd_vt1724_spdif_stream, ice));
2365 if (err < 0)
2366 return err;
2367 kctl->id.device = ice->pcm->device;
2368 ice->spdif.stream_ctl = kctl;
2369#endif
2370 return 0;
2371}
2372
2373
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002374static int __devinit snd_vt1724_build_controls(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375{
2376 int err;
2377
2378 err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_eeprom, ice));
2379 if (err < 0)
2380 return err;
2381 err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_pro_internal_clock, ice));
2382 if (err < 0)
2383 return err;
2384
2385 err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_pro_rate_locking, ice));
2386 if (err < 0)
2387 return err;
2388 err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_pro_rate_reset, ice));
2389 if (err < 0)
2390 return err;
2391
Takashi Iwai2bf864a2009-05-06 17:30:17 +02002392 if (!ice->own_routing && ice->num_total_dacs > 0) {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002393 struct snd_kcontrol_new tmp = snd_vt1724_mixer_pro_analog_route;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 tmp.count = ice->num_total_dacs;
2395 if (ice->vt1720 && tmp.count > 2)
2396 tmp.count = 2;
2397 err = snd_ctl_add(ice->card, snd_ctl_new1(&tmp, ice));
2398 if (err < 0)
2399 return err;
2400 }
2401
2402 err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_mixer_pro_peak, ice));
2403 if (err < 0)
2404 return err;
2405
2406 return 0;
2407}
2408
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002409static int snd_vt1724_free(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410{
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002411 if (!ice->port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 goto __hw_end;
2413 /* mask all interrupts */
2414 outb(0xff, ICEMT1724(ice, DMA_INT_MASK));
2415 outb(0xff, ICEREG1724(ice, IRQMASK));
2416 /* --- */
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002417__hw_end:
Jeff Garzikf000fd82008-04-22 13:50:34 +02002418 if (ice->irq >= 0)
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002419 free_irq(ice->irq, ice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 pci_release_regions(ice->pci);
2421 snd_ice1712_akm4xxx_free(ice);
2422 pci_disable_device(ice->pci);
Takashi Iwai7cda8ba2008-01-18 13:36:07 +01002423 kfree(ice->spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 kfree(ice);
2425 return 0;
2426}
2427
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002428static int snd_vt1724_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002430 struct snd_ice1712 *ice = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 return snd_vt1724_free(ice);
2432}
2433
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002434static int __devinit snd_vt1724_create(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 struct pci_dev *pci,
2436 const char *modelname,
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002437 struct snd_ice1712 **r_ice1712)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002439 struct snd_ice1712 *ice;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 int err;
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002441 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 .dev_free = snd_vt1724_dev_free,
2443 };
2444
2445 *r_ice1712 = NULL;
2446
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002447 /* enable PCI device */
2448 err = pci_enable_device(pci);
2449 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 return err;
2451
Takashi Iwaie560d8d2005-09-09 14:21:46 +02002452 ice = kzalloc(sizeof(*ice), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 if (ice == NULL) {
2454 pci_disable_device(pci);
2455 return -ENOMEM;
2456 }
2457 ice->vt1724 = 1;
2458 spin_lock_init(&ice->reg_lock);
Ingo Molnar62932df2006-01-16 16:34:20 +01002459 mutex_init(&ice->gpio_mutex);
2460 mutex_init(&ice->open_mutex);
2461 mutex_init(&ice->i2c_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 ice->gpio.set_mask = snd_vt1724_set_gpio_mask;
Pavel Hofman49470302009-09-16 22:25:38 +02002463 ice->gpio.get_mask = snd_vt1724_get_gpio_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 ice->gpio.set_dir = snd_vt1724_set_gpio_dir;
Pavel Hofman49470302009-09-16 22:25:38 +02002465 ice->gpio.get_dir = snd_vt1724_get_gpio_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 ice->gpio.set_data = snd_vt1724_set_gpio_data;
2467 ice->gpio.get_data = snd_vt1724_get_gpio_data;
2468 ice->card = card;
2469 ice->pci = pci;
2470 ice->irq = -1;
2471 pci_set_master(pci);
2472 snd_vt1724_proc_init(ice);
2473 synchronize_irq(pci->irq);
2474
Igor Chernyshevb40e9532009-06-25 09:31:07 +02002475 card->private_data = ice;
2476
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002477 err = pci_request_regions(pci, "ICE1724");
2478 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 kfree(ice);
2480 pci_disable_device(pci);
2481 return err;
2482 }
2483 ice->port = pci_resource_start(pci, 0);
2484 ice->profi_port = pci_resource_start(pci, 1);
2485
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002486 if (request_irq(pci->irq, snd_vt1724_interrupt,
Takashi Iwai437a5a42006-11-21 12:14:23 +01002487 IRQF_SHARED, "ICE1724", ice)) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02002488 snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 snd_vt1724_free(ice);
2490 return -EIO;
2491 }
2492
2493 ice->irq = pci->irq;
2494
Karsten Wiese988f0662008-04-22 12:52:15 +02002495 snd_vt1724_chip_reset(ice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 if (snd_vt1724_read_eeprom(ice, modelname) < 0) {
2497 snd_vt1724_free(ice);
2498 return -EIO;
2499 }
2500 if (snd_vt1724_chip_init(ice) < 0) {
2501 snd_vt1724_free(ice);
2502 return -EIO;
2503 }
2504
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002505 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, ice, &ops);
2506 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 snd_vt1724_free(ice);
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002508 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 }
2510
2511 snd_card_set_dev(card, &pci->dev);
2512
2513 *r_ice1712 = ice;
2514 return 0;
2515}
2516
2517
2518/*
2519 *
2520 * Registration
2521 *
2522 */
2523
2524static int __devinit snd_vt1724_probe(struct pci_dev *pci,
2525 const struct pci_device_id *pci_id)
2526{
2527 static int dev;
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002528 struct snd_card *card;
2529 struct snd_ice1712 *ice;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 int pcm_dev = 0, err;
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01002531 struct snd_ice1712_card_info * const *tbl, *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532
2533 if (dev >= SNDRV_CARDS)
2534 return -ENODEV;
2535 if (!enable[dev]) {
2536 dev++;
2537 return -ENOENT;
2538 }
2539
Takashi Iwaie58de7b2008-12-28 16:44:30 +01002540 err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
2541 if (err < 0)
2542 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543
2544 strcpy(card->driver, "ICE1724");
2545 strcpy(card->shortname, "ICEnsemble ICE1724");
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002546
2547 err = snd_vt1724_create(card, pci, model[dev], &ice);
2548 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 snd_card_free(card);
2550 return err;
2551 }
2552
2553 for (tbl = card_tables; *tbl; tbl++) {
2554 for (c = *tbl; c->subvendor; c++) {
2555 if (c->subvendor == ice->eeprom.subvendor) {
2556 strcpy(card->shortname, c->name);
2557 if (c->driver) /* specific driver? */
2558 strcpy(card->driver, c->driver);
2559 if (c->chip_init) {
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002560 err = c->chip_init(ice);
2561 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 snd_card_free(card);
2563 return err;
2564 }
2565 }
2566 goto __found;
2567 }
2568 }
2569 }
2570 c = &no_matched;
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002571__found:
2572 /*
2573 * VT1724 has separate DMAs for the analog and the SPDIF streams while
2574 * ICE1712 has only one for both (mixed up).
2575 *
2576 * Confusingly the analog PCM is named "professional" here because it
2577 * was called so in ice1712 driver, and vt1724 driver is derived from
2578 * ice1712 driver.
2579 */
Pavel Hofmand16be8e2008-03-20 12:10:27 +01002580 ice->pro_rate_default = PRO_RATE_DEFAULT;
2581 if (!ice->is_spdif_master)
2582 ice->is_spdif_master = stdclock_is_spdif_master;
2583 if (!ice->get_rate)
2584 ice->get_rate = stdclock_get_rate;
2585 if (!ice->set_rate)
2586 ice->set_rate = stdclock_set_rate;
2587 if (!ice->set_mclk)
2588 ice->set_mclk = stdclock_set_mclk;
2589 if (!ice->set_spdif_clock)
2590 ice->set_spdif_clock = stdclock_set_spdif_clock;
2591 if (!ice->hw_rates)
2592 set_std_hw_rates(ice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002594 err = snd_vt1724_pcm_profi(ice, pcm_dev++);
2595 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 snd_card_free(card);
2597 return err;
2598 }
2599
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002600 err = snd_vt1724_pcm_spdif(ice, pcm_dev++);
2601 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 snd_card_free(card);
2603 return err;
2604 }
2605
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002606 err = snd_vt1724_pcm_indep(ice, pcm_dev++);
2607 if (err < 0) {
2608 snd_card_free(card);
2609 return err;
2610 }
2611
2612 err = snd_vt1724_ac97_mixer(ice);
2613 if (err < 0) {
2614 snd_card_free(card);
2615 return err;
2616 }
2617
2618 err = snd_vt1724_build_controls(ice);
2619 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 snd_card_free(card);
2621 return err;
2622 }
2623
2624 if (ice->pcm && ice->has_spdif) { /* has SPDIF I/O */
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002625 err = snd_vt1724_spdif_build_controls(ice);
2626 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 snd_card_free(card);
2628 return err;
2629 }
2630 }
2631
2632 if (c->build_controls) {
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002633 err = c->build_controls(ice);
2634 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 snd_card_free(card);
2636 return err;
2637 }
2638 }
2639
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002640 if (!c->no_mpu401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 if (ice->eeprom.data[ICE_EEP2_SYSCONF] & VT1724_CFG_MPU401) {
Clemens Ladischaea3bfb2008-05-20 14:22:44 +02002642 struct snd_rawmidi *rmidi;
2643
2644 err = snd_rawmidi_new(card, "MIDI", 0, 1, 1, &rmidi);
2645 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 snd_card_free(card);
2647 return err;
2648 }
Clemens Ladischaea3bfb2008-05-20 14:22:44 +02002649 ice->rmidi[0] = rmidi;
2650 rmidi->private_data = ice;
2651 strcpy(rmidi->name, "ICE1724 MIDI");
2652 rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
2653 SNDRV_RAWMIDI_INFO_INPUT |
2654 SNDRV_RAWMIDI_INFO_DUPLEX;
2655 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
2656 &vt1724_midi_output_ops);
2657 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT,
2658 &vt1724_midi_input_ops);
2659
Takashi Iwai3a841d52008-04-23 17:47:28 +02002660 /* set watermarks */
2661 outb(VT1724_MPU_RX_FIFO | 0x1,
2662 ICEREG1724(ice, MPU_FIFO_WM));
2663 outb(0x1, ICEREG1724(ice, MPU_FIFO_WM));
Clemens Ladischaea3bfb2008-05-20 14:22:44 +02002664 /* set UART mode */
2665 outb(VT1724_MPU_UART, ICEREG1724(ice, MPU_CTRL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 }
2667 }
2668
2669 sprintf(card->longname, "%s at 0x%lx, irq %i",
2670 card->shortname, ice->port, ice->irq);
2671
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002672 err = snd_card_register(card);
2673 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 snd_card_free(card);
2675 return err;
2676 }
2677 pci_set_drvdata(pci, card);
2678 dev++;
2679 return 0;
2680}
2681
2682static void __devexit snd_vt1724_remove(struct pci_dev *pci)
2683{
2684 snd_card_free(pci_get_drvdata(pci));
2685 pci_set_drvdata(pci, NULL);
2686}
2687
Igor Chernyshevb40e9532009-06-25 09:31:07 +02002688#ifdef CONFIG_PM
2689static int snd_vt1724_suspend(struct pci_dev *pci, pm_message_t state)
2690{
2691 struct snd_card *card = pci_get_drvdata(pci);
2692 struct snd_ice1712 *ice = card->private_data;
2693
2694 if (!ice->pm_suspend_enabled)
2695 return 0;
2696
2697 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
2698
2699 snd_pcm_suspend_all(ice->pcm);
2700 snd_pcm_suspend_all(ice->pcm_pro);
2701 snd_pcm_suspend_all(ice->pcm_ds);
2702 snd_ac97_suspend(ice->ac97);
2703
2704 spin_lock_irq(&ice->reg_lock);
2705 ice->pm_saved_is_spdif_master = ice->is_spdif_master(ice);
2706 ice->pm_saved_spdif_ctrl = inw(ICEMT1724(ice, SPDIF_CTRL));
2707 ice->pm_saved_spdif_cfg = inb(ICEREG1724(ice, SPDIF_CFG));
2708 ice->pm_saved_route = inl(ICEMT1724(ice, ROUTE_PLAYBACK));
2709 spin_unlock_irq(&ice->reg_lock);
2710
2711 if (ice->pm_suspend)
2712 ice->pm_suspend(ice);
2713
2714 pci_disable_device(pci);
2715 pci_save_state(pci);
2716 pci_set_power_state(pci, pci_choose_state(pci, state));
2717 return 0;
2718}
2719
2720static int snd_vt1724_resume(struct pci_dev *pci)
2721{
2722 struct snd_card *card = pci_get_drvdata(pci);
2723 struct snd_ice1712 *ice = card->private_data;
2724
2725 if (!ice->pm_suspend_enabled)
2726 return 0;
2727
2728 pci_set_power_state(pci, PCI_D0);
2729 pci_restore_state(pci);
2730
2731 if (pci_enable_device(pci) < 0) {
2732 snd_card_disconnect(card);
2733 return -EIO;
2734 }
2735
2736 pci_set_master(pci);
2737
2738 snd_vt1724_chip_reset(ice);
2739
2740 if (snd_vt1724_chip_init(ice) < 0) {
2741 snd_card_disconnect(card);
2742 return -EIO;
2743 }
2744
2745 if (ice->pm_resume)
2746 ice->pm_resume(ice);
2747
2748 if (ice->pm_saved_is_spdif_master) {
2749 /* switching to external clock via SPDIF */
2750 ice->set_spdif_clock(ice);
2751 } else {
2752 /* internal on-card clock */
2753 snd_vt1724_set_pro_rate(ice, ice->pro_rate_default, 1);
2754 }
2755
2756 update_spdif_bits(ice, ice->pm_saved_spdif_ctrl);
2757
2758 outb(ice->pm_saved_spdif_cfg, ICEREG1724(ice, SPDIF_CFG));
2759 outl(ice->pm_saved_route, ICEMT1724(ice, ROUTE_PLAYBACK));
2760
2761 if (ice->ac97)
2762 snd_ac97_resume(ice->ac97);
2763
2764 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
2765 return 0;
2766}
2767#endif
2768
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769static struct pci_driver driver = {
2770 .name = "ICE1724",
2771 .id_table = snd_vt1724_ids,
2772 .probe = snd_vt1724_probe,
2773 .remove = __devexit_p(snd_vt1724_remove),
Igor Chernyshevb40e9532009-06-25 09:31:07 +02002774#ifdef CONFIG_PM
2775 .suspend = snd_vt1724_suspend,
2776 .resume = snd_vt1724_resume,
2777#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778};
2779
2780static int __init alsa_card_ice1724_init(void)
2781{
Takashi Iwai01d25d42005-04-11 16:58:24 +02002782 return pci_register_driver(&driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783}
2784
2785static void __exit alsa_card_ice1724_exit(void)
2786{
2787 pci_unregister_driver(&driver);
2788}
2789
2790module_init(alsa_card_ice1724_init)
2791module_exit(alsa_card_ice1724_exit)