blob: cc84a831eb21f19ee30099b6aece9e7f021c32d4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ALSA driver for VT1724 ICEnsemble ICE1724 / VIA VT1724 (Envy24HT)
3 * VIA VT1720 (Envy24PT)
4 *
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02005 * Copyright (c) 2000 Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * 2002 James Stafford <jstafford@ampltd.com>
7 * 2003 Takashi Iwai <tiwai@suse.de>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +040023 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +040025#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/delay.h>
27#include <linux/interrupt.h>
28#include <linux/init.h>
29#include <linux/pci.h>
30#include <linux/slab.h>
31#include <linux/moduleparam.h>
Ingo Molnar62932df2006-01-16 16:34:20 +010032#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <sound/core.h>
34#include <sound/info.h>
Clemens Ladischaea3bfb2008-05-20 14:22:44 +020035#include <sound/rawmidi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <sound/initval.h>
37
38#include <sound/asoundef.h>
39
40#include "ice1712.h"
41#include "envy24ht.h"
42
43/* lowlevel routines */
44#include "amp.h"
45#include "revo.h"
46#include "aureon.h"
47#include "vt1720_mobo.h"
48#include "pontis.h"
49#include "prodigy192.h"
Julian Scheel6b8d6e52008-01-16 19:50:00 +010050#include "prodigy_hifi.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include "juli.h"
Takashi Iwai72cbfd42009-05-06 17:33:19 +020052#include "maya44.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include "phase.h"
Clement Guedezf6cdab52007-01-08 10:48:41 +010054#include "wtm.h"
Shin-ya Okadaf31639b2007-10-23 15:08:18 +020055#include "se.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Jaroslav Kyselac1017a42007-10-15 09:50:19 +020057MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070058MODULE_DESCRIPTION("VIA ICEnsemble ICE1724/1720 (Envy24HT/PT)");
59MODULE_LICENSE("GPL");
60MODULE_SUPPORTED_DEVICE("{"
61 REVO_DEVICE_DESC
62 AMP_AUDIO2000_DEVICE_DESC
63 AUREON_DEVICE_DESC
64 VT1720_MOBO_DEVICE_DESC
65 PONTIS_DEVICE_DESC
66 PRODIGY192_DEVICE_DESC
Julian Scheel6b8d6e52008-01-16 19:50:00 +010067 PRODIGY_HIFI_DEVICE_DESC
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 JULI_DEVICE_DESC
Takashi Iwai72cbfd42009-05-06 17:33:19 +020069 MAYA44_DEVICE_DESC
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 PHASE_DEVICE_DESC
Clement Guedezf6cdab52007-01-08 10:48:41 +010071 WTM_DEVICE_DESC
Shin-ya Okadaf31639b2007-10-23 15:08:18 +020072 SE_DEVICE_DESC
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 "{VIA,VT1720},"
74 "{VIA,VT1724},"
75 "{ICEnsemble,Generic ICE1724},"
76 "{ICEnsemble,Generic Envy24HT}"
77 "{ICEnsemble,Generic Envy24PT}}");
78
79static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
80static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
81static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
82static char *model[SNDRV_CARDS];
83
84module_param_array(index, int, NULL, 0444);
85MODULE_PARM_DESC(index, "Index value for ICE1724 soundcard.");
86module_param_array(id, charp, NULL, 0444);
87MODULE_PARM_DESC(id, "ID string for ICE1724 soundcard.");
88module_param_array(enable, bool, NULL, 0444);
89MODULE_PARM_DESC(enable, "Enable ICE1724 soundcard.");
90module_param_array(model, charp, NULL, 0444);
91MODULE_PARM_DESC(model, "Use the given board model.");
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94/* Both VT1720 and VT1724 have the same PCI IDs */
Takashi Iwai32b47da2007-01-29 15:26:36 +010095static const struct pci_device_id snd_vt1724_ids[] = {
Joe Perches28d27aa2009-06-24 22:13:35 -070096 { PCI_VDEVICE(ICE, PCI_DEVICE_ID_VT1724), 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 { 0, }
98};
99
100MODULE_DEVICE_TABLE(pci, snd_vt1724_ids);
101
102
103static int PRO_RATE_LOCKED;
104static int PRO_RATE_RESET = 1;
105static unsigned int PRO_RATE_DEFAULT = 44100;
106
107/*
108 * Basic I/O
109 */
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +0400110
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100111/*
112 * default rates, default clock routines
113 */
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115/* check whether the clock mode is spdif-in */
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100116static inline int stdclock_is_spdif_master(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
118 return (inb(ICEMT1724(ice, RATE)) & VT1724_SPDIF_MASTER) ? 1 : 0;
119}
120
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100121static inline int is_pro_rate_locked(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100123 return ice->is_spdif_master(ice) || PRO_RATE_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
125
126/*
127 * ac97 section
128 */
129
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100130static unsigned char snd_vt1724_ac97_ready(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
132 unsigned char old_cmd;
133 int tm;
134 for (tm = 0; tm < 0x10000; tm++) {
135 old_cmd = inb(ICEMT1724(ice, AC97_CMD));
136 if (old_cmd & (VT1724_AC97_WRITE | VT1724_AC97_READ))
137 continue;
138 if (!(old_cmd & VT1724_AC97_READY))
139 continue;
140 return old_cmd;
141 }
142 snd_printd(KERN_ERR "snd_vt1724_ac97_ready: timeout\n");
143 return old_cmd;
144}
145
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100146static int snd_vt1724_ac97_wait_bit(struct snd_ice1712 *ice, unsigned char bit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
148 int tm;
149 for (tm = 0; tm < 0x10000; tm++)
150 if ((inb(ICEMT1724(ice, AC97_CMD)) & bit) == 0)
151 return 0;
152 snd_printd(KERN_ERR "snd_vt1724_ac97_wait_bit: timeout\n");
153 return -EIO;
154}
155
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100156static void snd_vt1724_ac97_write(struct snd_ac97 *ac97,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 unsigned short reg,
158 unsigned short val)
159{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100160 struct snd_ice1712 *ice = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 unsigned char old_cmd;
162
163 old_cmd = snd_vt1724_ac97_ready(ice);
164 old_cmd &= ~VT1724_AC97_ID_MASK;
165 old_cmd |= ac97->num;
166 outb(reg, ICEMT1724(ice, AC97_INDEX));
167 outw(val, ICEMT1724(ice, AC97_DATA));
168 outb(old_cmd | VT1724_AC97_WRITE, ICEMT1724(ice, AC97_CMD));
169 snd_vt1724_ac97_wait_bit(ice, VT1724_AC97_WRITE);
170}
171
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100172static unsigned short snd_vt1724_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100174 struct snd_ice1712 *ice = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 unsigned char old_cmd;
176
177 old_cmd = snd_vt1724_ac97_ready(ice);
178 old_cmd &= ~VT1724_AC97_ID_MASK;
179 old_cmd |= ac97->num;
180 outb(reg, ICEMT1724(ice, AC97_INDEX));
181 outb(old_cmd | VT1724_AC97_READ, ICEMT1724(ice, AC97_CMD));
182 if (snd_vt1724_ac97_wait_bit(ice, VT1724_AC97_READ) < 0)
183 return ~0;
184 return inw(ICEMT1724(ice, AC97_DATA));
185}
186
187
188/*
189 * GPIO operations
190 */
191
192/* set gpio direction 0 = read, 1 = write */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100193static void snd_vt1724_set_gpio_dir(struct snd_ice1712 *ice, unsigned int data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
195 outl(data, ICEREG1724(ice, GPIO_DIRECTION));
196 inw(ICEREG1724(ice, GPIO_DIRECTION)); /* dummy read for pci-posting */
197}
198
199/* set the gpio mask (0 = writable) */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100200static void snd_vt1724_set_gpio_mask(struct snd_ice1712 *ice, unsigned int data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
202 outw(data, ICEREG1724(ice, GPIO_WRITE_MASK));
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +0400203 if (!ice->vt1720) /* VT1720 supports only 16 GPIO bits */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 outb((data >> 16) & 0xff, ICEREG1724(ice, GPIO_WRITE_MASK_22));
205 inw(ICEREG1724(ice, GPIO_WRITE_MASK)); /* dummy read for pci-posting */
206}
207
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100208static void snd_vt1724_set_gpio_data(struct snd_ice1712 *ice, unsigned int data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
210 outw(data, ICEREG1724(ice, GPIO_DATA));
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +0400211 if (!ice->vt1720)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 outb(data >> 16, ICEREG1724(ice, GPIO_DATA_22));
213 inw(ICEREG1724(ice, GPIO_DATA)); /* dummy read for pci-posting */
214}
215
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100216static unsigned int snd_vt1724_get_gpio_data(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
218 unsigned int data;
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +0400219 if (!ice->vt1720)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 data = (unsigned int)inb(ICEREG1724(ice, GPIO_DATA_22));
221 else
222 data = 0;
223 data = (data << 16) | inw(ICEREG1724(ice, GPIO_DATA));
224 return data;
225}
226
227/*
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200228 * MIDI
Takashi Iwai3a841d52008-04-23 17:47:28 +0200229 */
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200230
231static void vt1724_midi_clear_rx(struct snd_ice1712 *ice)
Takashi Iwai3a841d52008-04-23 17:47:28 +0200232{
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200233 unsigned int count;
234
235 for (count = inb(ICEREG1724(ice, MPU_RXFIFO)); count > 0; --count)
236 inb(ICEREG1724(ice, MPU_DATA));
Takashi Iwai3a841d52008-04-23 17:47:28 +0200237}
238
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200239static inline struct snd_rawmidi_substream *
240get_rawmidi_substream(struct snd_ice1712 *ice, unsigned int stream)
Takashi Iwai3a841d52008-04-23 17:47:28 +0200241{
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200242 return list_first_entry(&ice->rmidi[0]->streams[stream].substreams,
243 struct snd_rawmidi_substream, list);
Takashi Iwai3a841d52008-04-23 17:47:28 +0200244}
245
Takashi Iwaie683ec42008-11-12 16:42:44 +0100246static void enable_midi_irq(struct snd_ice1712 *ice, u8 flag, int enable);
247
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200248static void vt1724_midi_write(struct snd_ice1712 *ice)
249{
250 struct snd_rawmidi_substream *s;
251 int count, i;
252 u8 buffer[32];
253
254 s = get_rawmidi_substream(ice, SNDRV_RAWMIDI_STREAM_OUTPUT);
255 count = 31 - inb(ICEREG1724(ice, MPU_TXFIFO));
256 if (count > 0) {
257 count = snd_rawmidi_transmit(s, buffer, count);
258 for (i = 0; i < count; ++i)
259 outb(buffer[i], ICEREG1724(ice, MPU_DATA));
260 }
Takashi Iwaie683ec42008-11-12 16:42:44 +0100261 /* mask irq when all bytes have been transmitted.
262 * enabled again in output_trigger when the new data comes in.
263 */
264 enable_midi_irq(ice, VT1724_IRQ_MPU_TX,
265 !snd_rawmidi_transmit_empty(s));
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200266}
267
268static void vt1724_midi_read(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_INPUT);
275 count = inb(ICEREG1724(ice, MPU_RXFIFO));
276 if (count > 0) {
277 count = min(count, 32);
278 for (i = 0; i < count; ++i)
279 buffer[i] = inb(ICEREG1724(ice, MPU_DATA));
280 snd_rawmidi_receive(s, buffer, count);
281 }
282}
283
Takashi Iwaie683ec42008-11-12 16:42:44 +0100284/* call with ice->reg_lock */
285static void enable_midi_irq(struct snd_ice1712 *ice, u8 flag, int enable)
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200286{
Takashi Iwaie683ec42008-11-12 16:42:44 +0100287 u8 mask = inb(ICEREG1724(ice, IRQMASK));
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200288 if (enable)
289 mask &= ~flag;
290 else
291 mask |= flag;
292 outb(mask, ICEREG1724(ice, IRQMASK));
Takashi Iwaie683ec42008-11-12 16:42:44 +0100293}
294
295static void vt1724_enable_midi_irq(struct snd_rawmidi_substream *substream,
296 u8 flag, int enable)
297{
298 struct snd_ice1712 *ice = substream->rmidi->private_data;
299
300 spin_lock_irq(&ice->reg_lock);
301 enable_midi_irq(ice, flag, enable);
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200302 spin_unlock_irq(&ice->reg_lock);
303}
304
305static int vt1724_midi_output_open(struct snd_rawmidi_substream *s)
306{
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200307 return 0;
308}
309
310static int vt1724_midi_output_close(struct snd_rawmidi_substream *s)
311{
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200312 return 0;
313}
314
315static void vt1724_midi_output_trigger(struct snd_rawmidi_substream *s, int up)
316{
317 struct snd_ice1712 *ice = s->rmidi->private_data;
318 unsigned long flags;
319
320 spin_lock_irqsave(&ice->reg_lock, flags);
321 if (up) {
322 ice->midi_output = 1;
323 vt1724_midi_write(ice);
324 } else {
325 ice->midi_output = 0;
Takashi Iwaie683ec42008-11-12 16:42:44 +0100326 enable_midi_irq(ice, VT1724_IRQ_MPU_TX, 0);
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200327 }
328 spin_unlock_irqrestore(&ice->reg_lock, flags);
329}
330
331static void vt1724_midi_output_drain(struct snd_rawmidi_substream *s)
332{
333 struct snd_ice1712 *ice = s->rmidi->private_data;
334 unsigned long timeout;
335
Takashi Iwaie683ec42008-11-12 16:42:44 +0100336 vt1724_enable_midi_irq(s, VT1724_IRQ_MPU_TX, 0);
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200337 /* 32 bytes should be transmitted in less than about 12 ms */
338 timeout = jiffies + msecs_to_jiffies(15);
339 do {
340 if (inb(ICEREG1724(ice, MPU_CTRL)) & VT1724_MPU_TX_EMPTY)
341 break;
342 schedule_timeout_uninterruptible(1);
343 } while (time_after(timeout, jiffies));
344}
345
346static struct snd_rawmidi_ops vt1724_midi_output_ops = {
347 .open = vt1724_midi_output_open,
348 .close = vt1724_midi_output_close,
349 .trigger = vt1724_midi_output_trigger,
350 .drain = vt1724_midi_output_drain,
351};
352
353static int vt1724_midi_input_open(struct snd_rawmidi_substream *s)
354{
355 vt1724_midi_clear_rx(s->rmidi->private_data);
356 vt1724_enable_midi_irq(s, VT1724_IRQ_MPU_RX, 1);
357 return 0;
358}
359
360static int vt1724_midi_input_close(struct snd_rawmidi_substream *s)
361{
362 vt1724_enable_midi_irq(s, VT1724_IRQ_MPU_RX, 0);
363 return 0;
364}
365
366static void vt1724_midi_input_trigger(struct snd_rawmidi_substream *s, int up)
367{
368 struct snd_ice1712 *ice = s->rmidi->private_data;
369 unsigned long flags;
370
371 spin_lock_irqsave(&ice->reg_lock, flags);
372 if (up) {
373 ice->midi_input = 1;
374 vt1724_midi_read(ice);
375 } else {
376 ice->midi_input = 0;
377 }
378 spin_unlock_irqrestore(&ice->reg_lock, flags);
379}
380
381static struct snd_rawmidi_ops vt1724_midi_input_ops = {
382 .open = vt1724_midi_input_open,
383 .close = vt1724_midi_input_close,
384 .trigger = vt1724_midi_input_trigger,
385};
386
Takashi Iwai3a841d52008-04-23 17:47:28 +0200387
388/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 * Interrupt handler
390 */
391
David Howells7d12e782006-10-05 14:55:46 +0100392static irqreturn_t snd_vt1724_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100394 struct snd_ice1712 *ice = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 unsigned char status;
Takashi Iwai3a841d52008-04-23 17:47:28 +0200396 unsigned char status_mask =
397 VT1724_IRQ_MPU_RX | VT1724_IRQ_MPU_TX | VT1724_IRQ_MTPCM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 int handled = 0;
Takashi Iwai3a841d52008-04-23 17:47:28 +0200399 int timeout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 while (1) {
402 status = inb(ICEREG1724(ice, IRQSTAT));
Takashi Iwai3a841d52008-04-23 17:47:28 +0200403 status &= status_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 if (status == 0)
405 break;
Takashi Iwaie683ec42008-11-12 16:42:44 +0100406 spin_lock(&ice->reg_lock);
Takashi Iwai3a841d52008-04-23 17:47:28 +0200407 if (++timeout > 10) {
Takashi Iwai10832062008-08-11 10:18:39 +0200408 status = inb(ICEREG1724(ice, IRQSTAT));
409 printk(KERN_ERR "ice1724: Too long irq loop, "
410 "status = 0x%x\n", status);
411 if (status & VT1724_IRQ_MPU_TX) {
412 printk(KERN_ERR "ice1724: Disabling MPU_TX\n");
Takashi Iwaie683ec42008-11-12 16:42:44 +0100413 enable_midi_irq(ice, VT1724_IRQ_MPU_TX, 0);
Takashi Iwai10832062008-08-11 10:18:39 +0200414 }
Takashi Iwaie683ec42008-11-12 16:42:44 +0100415 spin_unlock(&ice->reg_lock);
Takashi Iwai3a841d52008-04-23 17:47:28 +0200416 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +0400418 handled = 1;
Takashi Iwai3a841d52008-04-23 17:47:28 +0200419 if (status & VT1724_IRQ_MPU_TX) {
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200420 if (ice->midi_output)
421 vt1724_midi_write(ice);
Takashi Iwaie683ec42008-11-12 16:42:44 +0100422 else
423 enable_midi_irq(ice, VT1724_IRQ_MPU_TX, 0);
Takashi Iwai3a841d52008-04-23 17:47:28 +0200424 /* Due to mysterical reasons, MPU_TX is always
425 * generated (and can't be cleared) when a PCM
426 * playback is going. So let's ignore at the
427 * next loop.
428 */
429 status_mask &= ~VT1724_IRQ_MPU_TX;
430 }
431 if (status & VT1724_IRQ_MPU_RX) {
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200432 if (ice->midi_input)
433 vt1724_midi_read(ice);
434 else
435 vt1724_midi_clear_rx(ice);
Takashi Iwai3a841d52008-04-23 17:47:28 +0200436 }
437 /* ack MPU irq */
438 outb(status, ICEREG1724(ice, IRQSTAT));
Takashi Iwaie683ec42008-11-12 16:42:44 +0100439 spin_unlock(&ice->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 if (status & VT1724_IRQ_MTPCM) {
441 /*
442 * Multi-track PCM
443 * PCM assignment are:
444 * Playback DMA0 (M/C) = playback_pro_substream
445 * Playback DMA1 = playback_con_substream_ds[0]
446 * Playback DMA2 = playback_con_substream_ds[1]
447 * Playback DMA3 = playback_con_substream_ds[2]
448 * Playback DMA4 (SPDIF) = playback_con_substream
449 * Record DMA0 = capture_pro_substream
450 * Record DMA1 = capture_con_substream
451 */
452 unsigned char mtstat = inb(ICEMT1724(ice, IRQ));
453 if (mtstat & VT1724_MULTI_PDMA0) {
454 if (ice->playback_pro_substream)
455 snd_pcm_period_elapsed(ice->playback_pro_substream);
456 }
457 if (mtstat & VT1724_MULTI_RDMA0) {
458 if (ice->capture_pro_substream)
459 snd_pcm_period_elapsed(ice->capture_pro_substream);
460 }
461 if (mtstat & VT1724_MULTI_PDMA1) {
462 if (ice->playback_con_substream_ds[0])
463 snd_pcm_period_elapsed(ice->playback_con_substream_ds[0]);
464 }
465 if (mtstat & VT1724_MULTI_PDMA2) {
466 if (ice->playback_con_substream_ds[1])
467 snd_pcm_period_elapsed(ice->playback_con_substream_ds[1]);
468 }
469 if (mtstat & VT1724_MULTI_PDMA3) {
470 if (ice->playback_con_substream_ds[2])
471 snd_pcm_period_elapsed(ice->playback_con_substream_ds[2]);
472 }
473 if (mtstat & VT1724_MULTI_PDMA4) {
474 if (ice->playback_con_substream)
475 snd_pcm_period_elapsed(ice->playback_con_substream);
476 }
477 if (mtstat & VT1724_MULTI_RDMA1) {
478 if (ice->capture_con_substream)
479 snd_pcm_period_elapsed(ice->capture_con_substream);
480 }
481 /* ack anyway to avoid freeze */
482 outb(mtstat, ICEMT1724(ice, IRQ));
483 /* ought to really handle this properly */
484 if (mtstat & VT1724_MULTI_FIFO_ERR) {
485 unsigned char fstat = inb(ICEMT1724(ice, DMA_FIFO_ERR));
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +0400486 outb(fstat, ICEMT1724(ice, DMA_FIFO_ERR));
487 outb(VT1724_MULTI_FIFO_ERR | inb(ICEMT1724(ice, DMA_INT_MASK)), ICEMT1724(ice, DMA_INT_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 /* If I don't do this, I get machine lockup due to continual interrupts */
489 }
490
491 }
492 }
493 return IRQ_RETVAL(handled);
494}
495
496/*
497 * PCM code - professional part (multitrack)
498 */
499
500static unsigned int rates[] = {
501 8000, 9600, 11025, 12000, 16000, 22050, 24000,
502 32000, 44100, 48000, 64000, 88200, 96000,
503 176400, 192000,
504};
505
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100506static struct snd_pcm_hw_constraint_list hw_constraints_rates_96 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 .count = ARRAY_SIZE(rates) - 2, /* up to 96000 */
508 .list = rates,
509 .mask = 0,
510};
511
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100512static struct snd_pcm_hw_constraint_list hw_constraints_rates_48 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 .count = ARRAY_SIZE(rates) - 5, /* up to 48000 */
514 .list = rates,
515 .mask = 0,
516};
517
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100518static struct snd_pcm_hw_constraint_list hw_constraints_rates_192 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 .count = ARRAY_SIZE(rates),
520 .list = rates,
521 .mask = 0,
522};
523
524struct vt1724_pcm_reg {
525 unsigned int addr; /* ADDR register offset */
526 unsigned int size; /* SIZE register offset */
527 unsigned int count; /* COUNT register offset */
528 unsigned int start; /* start & pause bit */
529};
530
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100531static int snd_vt1724_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100533 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 unsigned char what;
535 unsigned char old;
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100536 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 what = 0;
Takashi Iwaief991b92007-02-22 12:52:53 +0100539 snd_pcm_group_for_each_entry(s, substream) {
Clemens Ladisch29998d22007-07-30 08:14:31 +0200540 if (snd_pcm_substream_chip(s) == ice) {
541 const struct vt1724_pcm_reg *reg;
542 reg = s->runtime->private_data;
543 what |= reg->start;
544 snd_pcm_trigger_done(s, substream);
545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 }
547
548 switch (cmd) {
549 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
550 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
551 spin_lock(&ice->reg_lock);
552 old = inb(ICEMT1724(ice, DMA_PAUSE));
553 if (cmd == SNDRV_PCM_TRIGGER_PAUSE_PUSH)
554 old |= what;
555 else
556 old &= ~what;
557 outb(old, ICEMT1724(ice, DMA_PAUSE));
558 spin_unlock(&ice->reg_lock);
559 break;
560
561 case SNDRV_PCM_TRIGGER_START:
562 case SNDRV_PCM_TRIGGER_STOP:
563 spin_lock(&ice->reg_lock);
564 old = inb(ICEMT1724(ice, DMA_CONTROL));
565 if (cmd == SNDRV_PCM_TRIGGER_START)
566 old |= what;
567 else
568 old &= ~what;
569 outb(old, ICEMT1724(ice, DMA_CONTROL));
570 spin_unlock(&ice->reg_lock);
571 break;
572
573 default:
574 return -EINVAL;
575 }
576 return 0;
577}
578
579/*
580 */
581
582#define DMA_STARTS (VT1724_RDMA0_START|VT1724_PDMA0_START|VT1724_RDMA1_START|\
583 VT1724_PDMA1_START|VT1724_PDMA2_START|VT1724_PDMA3_START|VT1724_PDMA4_START)
584#define DMA_PAUSES (VT1724_RDMA0_PAUSE|VT1724_PDMA0_PAUSE|VT1724_RDMA1_PAUSE|\
585 VT1724_PDMA1_PAUSE|VT1724_PDMA2_PAUSE|VT1724_PDMA3_PAUSE|VT1724_PDMA4_PAUSE)
586
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100587static const unsigned int stdclock_rate_list[16] = {
588 48000, 24000, 12000, 9600, 32000, 16000, 8000, 96000, 44100,
589 22050, 11025, 88200, 176400, 0, 192000, 64000
590};
591
592static unsigned int stdclock_get_rate(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100594 unsigned int rate;
595 rate = stdclock_rate_list[inb(ICEMT1724(ice, RATE)) & 15];
596 return rate;
597}
598
599static void stdclock_set_rate(struct snd_ice1712 *ice, unsigned int rate)
600{
601 int i;
602 for (i = 0; i < ARRAY_SIZE(stdclock_rate_list); i++) {
603 if (stdclock_rate_list[i] == rate) {
604 outb(i, ICEMT1724(ice, RATE));
605 return;
606 }
607 }
608}
609
610static unsigned char stdclock_set_mclk(struct snd_ice1712 *ice,
611 unsigned int rate)
612{
613 unsigned char val, old;
614 /* check MT02 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 if (ice->eeprom.data[ICE_EEP2_ACLINK] & VT1724_CFG_PRO_I2S) {
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100616 val = old = inb(ICEMT1724(ice, I2S_FORMAT));
617 if (rate > 96000)
618 val |= VT1724_MT_I2S_MCLK_128X; /* 128x MCLK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 else
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100620 val &= ~VT1724_MT_I2S_MCLK_128X; /* 256x MCLK */
621 if (val != old) {
622 outb(val, ICEMT1724(ice, I2S_FORMAT));
623 /* master clock changed */
624 return 1;
625 }
626 }
627 /* no change in master clock */
628 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
630
Takashi Iwai92d71002009-05-06 17:18:34 +0200631static int snd_vt1724_set_pro_rate(struct snd_ice1712 *ice, unsigned int rate,
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100632 int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
634 unsigned long flags;
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100635 unsigned char mclk_change;
636 unsigned int i, old_rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100638 if (rate > ice->hw_rates->list[ice->hw_rates->count - 1])
Takashi Iwai92d71002009-05-06 17:18:34 +0200639 return -EINVAL;
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 spin_lock_irqsave(&ice->reg_lock, flags);
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100642 if ((inb(ICEMT1724(ice, DMA_CONTROL)) & DMA_STARTS) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 (inb(ICEMT1724(ice, DMA_PAUSE)) & DMA_PAUSES)) {
644 /* running? we cannot change the rate now... */
645 spin_unlock_irqrestore(&ice->reg_lock, flags);
Takashi Iwai92d71002009-05-06 17:18:34 +0200646 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
648 if (!force && is_pro_rate_locked(ice)) {
649 spin_unlock_irqrestore(&ice->reg_lock, flags);
Takashi Iwai92d71002009-05-06 17:18:34 +0200650 return (rate == ice->cur_rate) ? 0 : -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 }
652
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100653 old_rate = ice->get_rate(ice);
654 if (force || (old_rate != rate))
655 ice->set_rate(ice, rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 else if (rate == ice->cur_rate) {
657 spin_unlock_irqrestore(&ice->reg_lock, flags);
Takashi Iwai92d71002009-05-06 17:18:34 +0200658 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 }
660
661 ice->cur_rate = rate;
662
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100663 /* setting master clock */
664 mclk_change = ice->set_mclk(ice, rate);
665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 spin_unlock_irqrestore(&ice->reg_lock, flags);
667
668 if (mclk_change && ice->gpio.i2s_mclk_changed)
669 ice->gpio.i2s_mclk_changed(ice);
670 if (ice->gpio.set_pro_rate)
671 ice->gpio.set_pro_rate(ice, rate);
672
673 /* set up codecs */
674 for (i = 0; i < ice->akm_codecs; i++) {
675 if (ice->akm[i].ops.set_rate_val)
676 ice->akm[i].ops.set_rate_val(&ice->akm[i], rate);
677 }
678 if (ice->spdif.ops.setup_rate)
679 ice->spdif.ops.setup_rate(ice, rate);
Takashi Iwai92d71002009-05-06 17:18:34 +0200680
681 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682}
683
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100684static int snd_vt1724_pcm_hw_params(struct snd_pcm_substream *substream,
685 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100687 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Takashi Iwai92d71002009-05-06 17:18:34 +0200688 int i, chs, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 chs = params_channels(hw_params);
Ingo Molnar62932df2006-01-16 16:34:20 +0100691 mutex_lock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 /* mark surround channels */
693 if (substream == ice->playback_pro_substream) {
694 /* PDMA0 can be multi-channel up to 8 */
695 chs = chs / 2 - 1;
696 for (i = 0; i < chs; i++) {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100697 if (ice->pcm_reserved[i] &&
698 ice->pcm_reserved[i] != substream) {
Ingo Molnar62932df2006-01-16 16:34:20 +0100699 mutex_unlock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return -EBUSY;
701 }
702 ice->pcm_reserved[i] = substream;
703 }
704 for (; i < 3; i++) {
705 if (ice->pcm_reserved[i] == substream)
706 ice->pcm_reserved[i] = NULL;
707 }
708 } else {
709 for (i = 0; i < 3; i++) {
710 /* check individual playback stream */
711 if (ice->playback_con_substream_ds[i] == substream) {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100712 if (ice->pcm_reserved[i] &&
713 ice->pcm_reserved[i] != substream) {
Ingo Molnar62932df2006-01-16 16:34:20 +0100714 mutex_unlock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 return -EBUSY;
716 }
717 ice->pcm_reserved[i] = substream;
718 break;
719 }
720 }
721 }
Ingo Molnar62932df2006-01-16 16:34:20 +0100722 mutex_unlock(&ice->open_mutex);
Takashi Iwai92d71002009-05-06 17:18:34 +0200723
724 err = snd_vt1724_set_pro_rate(ice, params_rate(hw_params), 0);
725 if (err < 0)
726 return err;
727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
729}
730
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100731static int snd_vt1724_pcm_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100733 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 int i;
735
Ingo Molnar62932df2006-01-16 16:34:20 +0100736 mutex_lock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 /* unmark surround channels */
738 for (i = 0; i < 3; i++)
739 if (ice->pcm_reserved[i] == substream)
740 ice->pcm_reserved[i] = NULL;
Ingo Molnar62932df2006-01-16 16:34:20 +0100741 mutex_unlock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 return snd_pcm_lib_free_pages(substream);
743}
744
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100745static int snd_vt1724_playback_pro_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100747 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 unsigned char val;
749 unsigned int size;
750
751 spin_lock_irq(&ice->reg_lock);
752 val = (8 - substream->runtime->channels) >> 1;
753 outb(val, ICEMT1724(ice, BURST));
754
755 outl(substream->runtime->dma_addr, ICEMT1724(ice, PLAYBACK_ADDR));
756
757 size = (snd_pcm_lib_buffer_bytes(substream) >> 2) - 1;
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +0400758 /* outl(size, ICEMT1724(ice, PLAYBACK_SIZE)); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 outw(size, ICEMT1724(ice, PLAYBACK_SIZE));
760 outb(size >> 16, ICEMT1724(ice, PLAYBACK_SIZE) + 2);
761 size = (snd_pcm_lib_period_bytes(substream) >> 2) - 1;
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +0400762 /* outl(size, ICEMT1724(ice, PLAYBACK_COUNT)); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 outw(size, ICEMT1724(ice, PLAYBACK_COUNT));
764 outb(size >> 16, ICEMT1724(ice, PLAYBACK_COUNT) + 2);
765
766 spin_unlock_irq(&ice->reg_lock);
767
Takashi Iwaie2ea7cfc2009-02-05 16:07:02 +0100768 /*
769 printk(KERN_DEBUG "pro prepare: ch = %d, addr = 0x%x, "
770 "buffer = 0x%x, period = 0x%x\n",
771 substream->runtime->channels,
772 (unsigned int)substream->runtime->dma_addr,
773 snd_pcm_lib_buffer_bytes(substream),
774 snd_pcm_lib_period_bytes(substream));
775 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 return 0;
777}
778
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100779static snd_pcm_uframes_t snd_vt1724_playback_pro_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100781 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 size_t ptr;
783
784 if (!(inl(ICEMT1724(ice, DMA_CONTROL)) & VT1724_PDMA0_START))
785 return 0;
786#if 0 /* read PLAYBACK_ADDR */
787 ptr = inl(ICEMT1724(ice, PLAYBACK_ADDR));
788 if (ptr < substream->runtime->dma_addr) {
789 snd_printd("ice1724: invalid negative ptr\n");
790 return 0;
791 }
792 ptr -= substream->runtime->dma_addr;
793 ptr = bytes_to_frames(substream->runtime, ptr);
794 if (ptr >= substream->runtime->buffer_size) {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100795 snd_printd("ice1724: invalid ptr %d (size=%d)\n",
796 (int)ptr, (int)substream->runtime->period_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 return 0;
798 }
799#else /* read PLAYBACK_SIZE */
800 ptr = inl(ICEMT1724(ice, PLAYBACK_SIZE)) & 0xffffff;
801 ptr = (ptr + 1) << 2;
802 ptr = bytes_to_frames(substream->runtime, ptr);
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +0400803 if (!ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 ;
805 else if (ptr <= substream->runtime->buffer_size)
806 ptr = substream->runtime->buffer_size - ptr;
807 else {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100808 snd_printd("ice1724: invalid ptr %d (size=%d)\n",
809 (int)ptr, (int)substream->runtime->buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 ptr = 0;
811 }
812#endif
813 return ptr;
814}
815
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100816static int snd_vt1724_pcm_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100818 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Takashi Iwai32b47da2007-01-29 15:26:36 +0100819 const struct vt1724_pcm_reg *reg = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 spin_lock_irq(&ice->reg_lock);
822 outl(substream->runtime->dma_addr, ice->profi_port + reg->addr);
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100823 outw((snd_pcm_lib_buffer_bytes(substream) >> 2) - 1,
824 ice->profi_port + reg->size);
825 outw((snd_pcm_lib_period_bytes(substream) >> 2) - 1,
826 ice->profi_port + reg->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 spin_unlock_irq(&ice->reg_lock);
828 return 0;
829}
830
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100831static snd_pcm_uframes_t snd_vt1724_pcm_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100833 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Takashi Iwai32b47da2007-01-29 15:26:36 +0100834 const struct vt1724_pcm_reg *reg = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 size_t ptr;
836
837 if (!(inl(ICEMT1724(ice, DMA_CONTROL)) & reg->start))
838 return 0;
839#if 0 /* use ADDR register */
840 ptr = inl(ice->profi_port + reg->addr);
841 ptr -= substream->runtime->dma_addr;
842 return bytes_to_frames(substream->runtime, ptr);
843#else /* use SIZE register */
844 ptr = inw(ice->profi_port + reg->size);
845 ptr = (ptr + 1) << 2;
846 ptr = bytes_to_frames(substream->runtime, ptr);
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +0400847 if (!ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 ;
849 else if (ptr <= substream->runtime->buffer_size)
850 ptr = substream->runtime->buffer_size - ptr;
851 else {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100852 snd_printd("ice1724: invalid ptr %d (size=%d)\n",
853 (int)ptr, (int)substream->runtime->buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 ptr = 0;
855 }
856 return ptr;
857#endif
858}
859
Takashi Iwaia5b7b5c2009-05-06 17:22:07 +0200860static const struct vt1724_pcm_reg vt1724_pdma0_reg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 .addr = VT1724_MT_PLAYBACK_ADDR,
862 .size = VT1724_MT_PLAYBACK_SIZE,
863 .count = VT1724_MT_PLAYBACK_COUNT,
864 .start = VT1724_PDMA0_START,
865};
866
Takashi Iwaia5b7b5c2009-05-06 17:22:07 +0200867static const struct vt1724_pcm_reg vt1724_pdma4_reg = {
868 .addr = VT1724_MT_PDMA4_ADDR,
869 .size = VT1724_MT_PDMA4_SIZE,
870 .count = VT1724_MT_PDMA4_COUNT,
871 .start = VT1724_PDMA4_START,
872};
873
874static const struct vt1724_pcm_reg vt1724_rdma0_reg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 .addr = VT1724_MT_CAPTURE_ADDR,
876 .size = VT1724_MT_CAPTURE_SIZE,
877 .count = VT1724_MT_CAPTURE_COUNT,
878 .start = VT1724_RDMA0_START,
879};
880
Takashi Iwaia5b7b5c2009-05-06 17:22:07 +0200881static const struct vt1724_pcm_reg vt1724_rdma1_reg = {
882 .addr = VT1724_MT_RDMA1_ADDR,
883 .size = VT1724_MT_RDMA1_SIZE,
884 .count = VT1724_MT_RDMA1_COUNT,
885 .start = VT1724_RDMA1_START,
886};
887
888#define vt1724_playback_pro_reg vt1724_pdma0_reg
889#define vt1724_playback_spdif_reg vt1724_pdma4_reg
890#define vt1724_capture_pro_reg vt1724_rdma0_reg
891#define vt1724_capture_spdif_reg vt1724_rdma1_reg
892
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +0400893static const struct snd_pcm_hardware snd_vt1724_playback_pro = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
895 SNDRV_PCM_INFO_BLOCK_TRANSFER |
896 SNDRV_PCM_INFO_MMAP_VALID |
897 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_SYNC_START),
898 .formats = SNDRV_PCM_FMTBIT_S32_LE,
899 .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_192000,
900 .rate_min = 8000,
901 .rate_max = 192000,
902 .channels_min = 2,
903 .channels_max = 8,
904 .buffer_bytes_max = (1UL << 21), /* 19bits dword */
905 .period_bytes_min = 8 * 4 * 2, /* FIXME: constraints needed */
906 .period_bytes_max = (1UL << 21),
907 .periods_min = 2,
908 .periods_max = 1024,
909};
910
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +0400911static const struct snd_pcm_hardware snd_vt1724_spdif = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
913 SNDRV_PCM_INFO_BLOCK_TRANSFER |
914 SNDRV_PCM_INFO_MMAP_VALID |
915 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_SYNC_START),
916 .formats = SNDRV_PCM_FMTBIT_S32_LE,
Takashi Iwai2dfbeca2005-10-12 10:04:42 +0200917 .rates = (SNDRV_PCM_RATE_32000|SNDRV_PCM_RATE_44100|
918 SNDRV_PCM_RATE_48000|SNDRV_PCM_RATE_88200|
919 SNDRV_PCM_RATE_96000|SNDRV_PCM_RATE_176400|
920 SNDRV_PCM_RATE_192000),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 .rate_min = 32000,
Takashi Iwai2dfbeca2005-10-12 10:04:42 +0200922 .rate_max = 192000,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 .channels_min = 2,
924 .channels_max = 2,
925 .buffer_bytes_max = (1UL << 18), /* 16bits dword */
926 .period_bytes_min = 2 * 4 * 2,
927 .period_bytes_max = (1UL << 18),
928 .periods_min = 2,
929 .periods_max = 1024,
930};
931
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +0400932static const struct snd_pcm_hardware snd_vt1724_2ch_stereo = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
934 SNDRV_PCM_INFO_BLOCK_TRANSFER |
935 SNDRV_PCM_INFO_MMAP_VALID |
936 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_SYNC_START),
937 .formats = SNDRV_PCM_FMTBIT_S32_LE,
938 .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_192000,
939 .rate_min = 8000,
940 .rate_max = 192000,
941 .channels_min = 2,
942 .channels_max = 2,
943 .buffer_bytes_max = (1UL << 18), /* 16bits dword */
944 .period_bytes_min = 2 * 4 * 2,
945 .period_bytes_max = (1UL << 18),
946 .periods_min = 2,
947 .periods_max = 1024,
948};
949
950/*
951 * set rate constraints
952 */
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100953static void set_std_hw_rates(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 if (ice->eeprom.data[ICE_EEP2_ACLINK] & VT1724_CFG_PRO_I2S) {
956 /* I2S */
957 /* VT1720 doesn't support more than 96kHz */
958 if ((ice->eeprom.data[ICE_EEP2_I2S] & 0x08) && !ice->vt1720)
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100959 ice->hw_rates = &hw_constraints_rates_192;
960 else
961 ice->hw_rates = &hw_constraints_rates_96;
962 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 /* ACLINK */
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100964 ice->hw_rates = &hw_constraints_rates_48;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 }
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100966}
967
968static int set_rate_constraints(struct snd_ice1712 *ice,
969 struct snd_pcm_substream *substream)
970{
971 struct snd_pcm_runtime *runtime = substream->runtime;
972
973 runtime->hw.rate_min = ice->hw_rates->list[0];
974 runtime->hw.rate_max = ice->hw_rates->list[ice->hw_rates->count - 1];
975 runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
976 return snd_pcm_hw_constraint_list(runtime, 0,
977 SNDRV_PCM_HW_PARAM_RATE,
978 ice->hw_rates);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979}
980
981/* multi-channel playback needs alignment 8x32bit regardless of the channels
982 * actually used
983 */
984#define VT1724_BUFFER_ALIGN 0x20
985
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100986static int snd_vt1724_playback_pro_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100988 struct snd_pcm_runtime *runtime = substream->runtime;
989 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Takashi Iwaia6b936b2008-08-29 16:17:25 +0200990 int chs, num_indeps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Takashi Iwai32b47da2007-01-29 15:26:36 +0100992 runtime->private_data = (void *)&vt1724_playback_pro_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 ice->playback_pro_substream = substream;
994 runtime->hw = snd_vt1724_playback_pro;
995 snd_pcm_set_sync(substream);
996 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
997 set_rate_constraints(ice, substream);
Ingo Molnar62932df2006-01-16 16:34:20 +0100998 mutex_lock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 /* calculate the currently available channels */
Takashi Iwaia6b936b2008-08-29 16:17:25 +02001000 num_indeps = ice->num_total_dacs / 2 - 1;
1001 for (chs = 0; chs < num_indeps; chs++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 if (ice->pcm_reserved[chs])
1003 break;
1004 }
1005 chs = (chs + 1) * 2;
1006 runtime->hw.channels_max = chs;
1007 if (chs > 2) /* channels must be even */
1008 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Ingo Molnar62932df2006-01-16 16:34:20 +01001009 mutex_unlock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1011 VT1724_BUFFER_ALIGN);
1012 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1013 VT1724_BUFFER_ALIGN);
1014 return 0;
1015}
1016
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001017static int snd_vt1724_capture_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_ice1712 *ice = snd_pcm_substream_chip(substream);
1020 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Takashi Iwai32b47da2007-01-29 15:26:36 +01001022 runtime->private_data = (void *)&vt1724_capture_pro_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 ice->capture_pro_substream = substream;
1024 runtime->hw = snd_vt1724_2ch_stereo;
1025 snd_pcm_set_sync(substream);
1026 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
1027 set_rate_constraints(ice, substream);
1028 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1029 VT1724_BUFFER_ALIGN);
1030 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1031 VT1724_BUFFER_ALIGN);
1032 return 0;
1033}
1034
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001035static int snd_vt1724_playback_pro_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001037 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
1039 if (PRO_RATE_RESET)
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001040 snd_vt1724_set_pro_rate(ice, ice->pro_rate_default, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 ice->playback_pro_substream = NULL;
1042
1043 return 0;
1044}
1045
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001046static int snd_vt1724_capture_pro_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001048 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
1050 if (PRO_RATE_RESET)
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001051 snd_vt1724_set_pro_rate(ice, ice->pro_rate_default, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 ice->capture_pro_substream = NULL;
1053 return 0;
1054}
1055
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001056static struct snd_pcm_ops snd_vt1724_playback_pro_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 .open = snd_vt1724_playback_pro_open,
1058 .close = snd_vt1724_playback_pro_close,
1059 .ioctl = snd_pcm_lib_ioctl,
1060 .hw_params = snd_vt1724_pcm_hw_params,
1061 .hw_free = snd_vt1724_pcm_hw_free,
1062 .prepare = snd_vt1724_playback_pro_prepare,
1063 .trigger = snd_vt1724_pcm_trigger,
1064 .pointer = snd_vt1724_playback_pro_pointer,
1065};
1066
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001067static struct snd_pcm_ops snd_vt1724_capture_pro_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 .open = snd_vt1724_capture_pro_open,
1069 .close = snd_vt1724_capture_pro_close,
1070 .ioctl = snd_pcm_lib_ioctl,
1071 .hw_params = snd_vt1724_pcm_hw_params,
1072 .hw_free = snd_vt1724_pcm_hw_free,
1073 .prepare = snd_vt1724_pcm_prepare,
1074 .trigger = snd_vt1724_pcm_trigger,
1075 .pointer = snd_vt1724_pcm_pointer,
1076};
1077
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001078static int __devinit snd_vt1724_pcm_profi(struct snd_ice1712 *ice, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001080 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 int err;
1082
1083 err = snd_pcm_new(ice->card, "ICE1724", device, 1, 1, &pcm);
1084 if (err < 0)
1085 return err;
1086
1087 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_vt1724_playback_pro_ops);
1088 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_vt1724_capture_pro_ops);
1089
1090 pcm->private_data = ice;
1091 pcm->info_flags = 0;
1092 strcpy(pcm->name, "ICE1724");
1093
1094 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001095 snd_dma_pci_data(ice->pci),
1096 256*1024, 256*1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
1098 ice->pcm_pro = pcm;
1099
1100 return 0;
1101}
1102
1103
1104/*
1105 * SPDIF PCM
1106 */
1107
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108/* update spdif control bits; call with reg_lock */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001109static void update_spdif_bits(struct snd_ice1712 *ice, unsigned int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110{
1111 unsigned char cbit, disabled;
1112
1113 cbit = inb(ICEREG1724(ice, SPDIF_CFG));
1114 disabled = cbit & ~VT1724_CFG_SPDIF_OUT_EN;
1115 if (cbit != disabled)
1116 outb(disabled, ICEREG1724(ice, SPDIF_CFG));
1117 outw(val, ICEMT1724(ice, SPDIF_CTRL));
1118 if (cbit != disabled)
1119 outb(cbit, ICEREG1724(ice, SPDIF_CFG));
1120 outw(val, ICEMT1724(ice, SPDIF_CTRL));
1121}
1122
1123/* update SPDIF control bits according to the given rate */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001124static void update_spdif_rate(struct snd_ice1712 *ice, unsigned int rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125{
1126 unsigned int val, nval;
1127 unsigned long flags;
1128
1129 spin_lock_irqsave(&ice->reg_lock, flags);
1130 nval = val = inw(ICEMT1724(ice, SPDIF_CTRL));
1131 nval &= ~(7 << 12);
1132 switch (rate) {
1133 case 44100: break;
1134 case 48000: nval |= 2 << 12; break;
1135 case 32000: nval |= 3 << 12; break;
Takashi Iwai2dfbeca2005-10-12 10:04:42 +02001136 case 88200: nval |= 4 << 12; break;
1137 case 96000: nval |= 5 << 12; break;
1138 case 192000: nval |= 6 << 12; break;
1139 case 176400: nval |= 7 << 12; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 }
1141 if (val != nval)
1142 update_spdif_bits(ice, nval);
1143 spin_unlock_irqrestore(&ice->reg_lock, flags);
1144}
1145
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001146static int snd_vt1724_playback_spdif_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001148 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001149 if (!ice->force_pdma4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 update_spdif_rate(ice, substream->runtime->rate);
1151 return snd_vt1724_pcm_prepare(substream);
1152}
1153
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001154static int snd_vt1724_playback_spdif_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001156 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
1157 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Takashi Iwai32b47da2007-01-29 15:26:36 +01001159 runtime->private_data = (void *)&vt1724_playback_spdif_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 ice->playback_con_substream = substream;
1161 if (ice->force_pdma4) {
1162 runtime->hw = snd_vt1724_2ch_stereo;
1163 set_rate_constraints(ice, substream);
1164 } else
1165 runtime->hw = snd_vt1724_spdif;
1166 snd_pcm_set_sync(substream);
1167 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
1168 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1169 VT1724_BUFFER_ALIGN);
1170 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1171 VT1724_BUFFER_ALIGN);
Takashi Iwaic93f5a12008-03-14 17:17:09 +01001172 if (ice->spdif.ops.open)
1173 ice->spdif.ops.open(ice, substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 return 0;
1175}
1176
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001177static int snd_vt1724_playback_spdif_close(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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
1181 if (PRO_RATE_RESET)
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001182 snd_vt1724_set_pro_rate(ice, ice->pro_rate_default, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 ice->playback_con_substream = NULL;
Takashi Iwaic93f5a12008-03-14 17:17:09 +01001184 if (ice->spdif.ops.close)
1185 ice->spdif.ops.close(ice, substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
1187 return 0;
1188}
1189
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001190static int snd_vt1724_capture_spdif_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001192 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
1193 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Takashi Iwai32b47da2007-01-29 15:26:36 +01001195 runtime->private_data = (void *)&vt1724_capture_spdif_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 ice->capture_con_substream = substream;
1197 if (ice->force_rdma1) {
1198 runtime->hw = snd_vt1724_2ch_stereo;
1199 set_rate_constraints(ice, substream);
1200 } else
1201 runtime->hw = snd_vt1724_spdif;
1202 snd_pcm_set_sync(substream);
1203 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
1204 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1205 VT1724_BUFFER_ALIGN);
1206 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1207 VT1724_BUFFER_ALIGN);
Takashi Iwaic93f5a12008-03-14 17:17:09 +01001208 if (ice->spdif.ops.open)
1209 ice->spdif.ops.open(ice, substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 return 0;
1211}
1212
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001213static int snd_vt1724_capture_spdif_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001215 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
1217 if (PRO_RATE_RESET)
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001218 snd_vt1724_set_pro_rate(ice, ice->pro_rate_default, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 ice->capture_con_substream = NULL;
Takashi Iwaic93f5a12008-03-14 17:17:09 +01001220 if (ice->spdif.ops.close)
1221 ice->spdif.ops.close(ice, substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
1223 return 0;
1224}
1225
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001226static struct snd_pcm_ops snd_vt1724_playback_spdif_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 .open = snd_vt1724_playback_spdif_open,
1228 .close = snd_vt1724_playback_spdif_close,
1229 .ioctl = snd_pcm_lib_ioctl,
1230 .hw_params = snd_vt1724_pcm_hw_params,
1231 .hw_free = snd_vt1724_pcm_hw_free,
1232 .prepare = snd_vt1724_playback_spdif_prepare,
1233 .trigger = snd_vt1724_pcm_trigger,
1234 .pointer = snd_vt1724_pcm_pointer,
1235};
1236
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001237static struct snd_pcm_ops snd_vt1724_capture_spdif_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 .open = snd_vt1724_capture_spdif_open,
1239 .close = snd_vt1724_capture_spdif_close,
1240 .ioctl = snd_pcm_lib_ioctl,
1241 .hw_params = snd_vt1724_pcm_hw_params,
1242 .hw_free = snd_vt1724_pcm_hw_free,
1243 .prepare = snd_vt1724_pcm_prepare,
1244 .trigger = snd_vt1724_pcm_trigger,
1245 .pointer = snd_vt1724_pcm_pointer,
1246};
1247
1248
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001249static int __devinit snd_vt1724_pcm_spdif(struct snd_ice1712 *ice, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
1251 char *name;
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001252 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 int play, capt;
1254 int err;
1255
1256 if (ice->force_pdma4 ||
1257 (ice->eeprom.data[ICE_EEP2_SPDIF] & VT1724_CFG_SPDIF_OUT_INT)) {
1258 play = 1;
1259 ice->has_spdif = 1;
1260 } else
1261 play = 0;
1262 if (ice->force_rdma1 ||
1263 (ice->eeprom.data[ICE_EEP2_SPDIF] & VT1724_CFG_SPDIF_IN)) {
1264 capt = 1;
1265 ice->has_spdif = 1;
1266 } else
1267 capt = 0;
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001268 if (!play && !capt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 return 0; /* no spdif device */
1270
1271 if (ice->force_pdma4 || ice->force_rdma1)
1272 name = "ICE1724 Secondary";
1273 else
Alan Horstmann8eca7532009-01-05 18:30:04 +01001274 name = "ICE1724 IEC958";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 err = snd_pcm_new(ice->card, name, device, play, capt, &pcm);
1276 if (err < 0)
1277 return err;
1278
1279 if (play)
1280 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1281 &snd_vt1724_playback_spdif_ops);
1282 if (capt)
1283 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1284 &snd_vt1724_capture_spdif_ops);
1285
1286 pcm->private_data = ice;
1287 pcm->info_flags = 0;
1288 strcpy(pcm->name, name);
1289
1290 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001291 snd_dma_pci_data(ice->pci),
1292 64*1024, 64*1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
1294 ice->pcm = pcm;
1295
1296 return 0;
1297}
1298
1299
1300/*
1301 * independent surround PCMs
1302 */
1303
Takashi Iwai32b47da2007-01-29 15:26:36 +01001304static const struct vt1724_pcm_reg vt1724_playback_dma_regs[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 {
1306 .addr = VT1724_MT_PDMA1_ADDR,
1307 .size = VT1724_MT_PDMA1_SIZE,
1308 .count = VT1724_MT_PDMA1_COUNT,
1309 .start = VT1724_PDMA1_START,
1310 },
1311 {
1312 .addr = VT1724_MT_PDMA2_ADDR,
1313 .size = VT1724_MT_PDMA2_SIZE,
1314 .count = VT1724_MT_PDMA2_COUNT,
1315 .start = VT1724_PDMA2_START,
1316 },
1317 {
1318 .addr = VT1724_MT_PDMA3_ADDR,
1319 .size = VT1724_MT_PDMA3_SIZE,
1320 .count = VT1724_MT_PDMA3_COUNT,
1321 .start = VT1724_PDMA3_START,
1322 },
1323};
1324
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001325static int snd_vt1724_playback_indep_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001327 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 unsigned char val;
1329
1330 spin_lock_irq(&ice->reg_lock);
1331 val = 3 - substream->number;
1332 if (inb(ICEMT1724(ice, BURST)) < val)
1333 outb(val, ICEMT1724(ice, BURST));
1334 spin_unlock_irq(&ice->reg_lock);
1335 return snd_vt1724_pcm_prepare(substream);
1336}
1337
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001338static int snd_vt1724_playback_indep_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001340 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
1341 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
Ingo Molnar62932df2006-01-16 16:34:20 +01001343 mutex_lock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 /* already used by PDMA0? */
1345 if (ice->pcm_reserved[substream->number]) {
Ingo Molnar62932df2006-01-16 16:34:20 +01001346 mutex_unlock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 return -EBUSY; /* FIXME: should handle blocking mode properly */
1348 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001349 mutex_unlock(&ice->open_mutex);
Takashi Iwai32b47da2007-01-29 15:26:36 +01001350 runtime->private_data = (void *)&vt1724_playback_dma_regs[substream->number];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 ice->playback_con_substream_ds[substream->number] = substream;
1352 runtime->hw = snd_vt1724_2ch_stereo;
1353 snd_pcm_set_sync(substream);
1354 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
1355 set_rate_constraints(ice, substream);
1356 return 0;
1357}
1358
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001359static int snd_vt1724_playback_indep_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001361 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
1363 if (PRO_RATE_RESET)
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001364 snd_vt1724_set_pro_rate(ice, ice->pro_rate_default, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 ice->playback_con_substream_ds[substream->number] = NULL;
1366 ice->pcm_reserved[substream->number] = NULL;
1367
1368 return 0;
1369}
1370
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001371static struct snd_pcm_ops snd_vt1724_playback_indep_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 .open = snd_vt1724_playback_indep_open,
1373 .close = snd_vt1724_playback_indep_close,
1374 .ioctl = snd_pcm_lib_ioctl,
1375 .hw_params = snd_vt1724_pcm_hw_params,
1376 .hw_free = snd_vt1724_pcm_hw_free,
1377 .prepare = snd_vt1724_playback_indep_prepare,
1378 .trigger = snd_vt1724_pcm_trigger,
1379 .pointer = snd_vt1724_pcm_pointer,
1380};
1381
1382
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001383static int __devinit snd_vt1724_pcm_indep(struct snd_ice1712 *ice, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001385 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 int play;
1387 int err;
1388
1389 play = ice->num_total_dacs / 2 - 1;
1390 if (play <= 0)
1391 return 0;
1392
1393 err = snd_pcm_new(ice->card, "ICE1724 Surrounds", device, play, 0, &pcm);
1394 if (err < 0)
1395 return err;
1396
1397 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1398 &snd_vt1724_playback_indep_ops);
1399
1400 pcm->private_data = ice;
1401 pcm->info_flags = 0;
1402 strcpy(pcm->name, "ICE1724 Surround PCM");
1403
1404 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001405 snd_dma_pci_data(ice->pci),
1406 64*1024, 64*1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
1408 ice->pcm_ds = pcm;
1409
1410 return 0;
1411}
1412
1413
1414/*
1415 * Mixer section
1416 */
1417
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001418static int __devinit snd_vt1724_ac97_mixer(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419{
1420 int err;
1421
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001422 if (!(ice->eeprom.data[ICE_EEP2_ACLINK] & VT1724_CFG_PRO_I2S)) {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001423 struct snd_ac97_bus *pbus;
1424 struct snd_ac97_template ac97;
1425 static struct snd_ac97_bus_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 .write = snd_vt1724_ac97_write,
1427 .read = snd_vt1724_ac97_read,
1428 };
1429
1430 /* cold reset */
1431 outb(inb(ICEMT1724(ice, AC97_CMD)) | 0x80, ICEMT1724(ice, AC97_CMD));
1432 mdelay(5); /* FIXME */
1433 outb(inb(ICEMT1724(ice, AC97_CMD)) & ~0x80, ICEMT1724(ice, AC97_CMD));
1434
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001435 err = snd_ac97_bus(ice->card, 0, &ops, NULL, &pbus);
1436 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 return err;
1438 memset(&ac97, 0, sizeof(ac97));
1439 ac97.private_data = ice;
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001440 err = snd_ac97_mixer(pbus, &ac97, &ice->ac97);
1441 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 printk(KERN_WARNING "ice1712: cannot initialize pro ac97, skipped\n");
1443 else
1444 return 0;
1445 }
1446 /* I2S mixer only */
1447 strcat(ice->card->mixername, "ICE1724 - multitrack");
1448 return 0;
1449}
1450
1451/*
1452 *
1453 */
1454
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001455static inline unsigned int eeprom_triple(struct snd_ice1712 *ice, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456{
1457 return (unsigned int)ice->eeprom.data[idx] | \
1458 ((unsigned int)ice->eeprom.data[idx + 1] << 8) | \
1459 ((unsigned int)ice->eeprom.data[idx + 2] << 16);
1460}
1461
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001462static void snd_vt1724_proc_read(struct snd_info_entry *entry,
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001463 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001465 struct snd_ice1712 *ice = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 unsigned int idx;
1467
1468 snd_iprintf(buffer, "%s\n\n", ice->card->longname);
1469 snd_iprintf(buffer, "EEPROM:\n");
1470
1471 snd_iprintf(buffer, " Subvendor : 0x%x\n", ice->eeprom.subvendor);
1472 snd_iprintf(buffer, " Size : %i bytes\n", ice->eeprom.size);
1473 snd_iprintf(buffer, " Version : %i\n", ice->eeprom.version);
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001474 snd_iprintf(buffer, " System Config : 0x%x\n",
1475 ice->eeprom.data[ICE_EEP2_SYSCONF]);
1476 snd_iprintf(buffer, " ACLink : 0x%x\n",
1477 ice->eeprom.data[ICE_EEP2_ACLINK]);
1478 snd_iprintf(buffer, " I2S : 0x%x\n",
1479 ice->eeprom.data[ICE_EEP2_I2S]);
1480 snd_iprintf(buffer, " S/PDIF : 0x%x\n",
1481 ice->eeprom.data[ICE_EEP2_SPDIF]);
1482 snd_iprintf(buffer, " GPIO direction : 0x%x\n",
1483 ice->eeprom.gpiodir);
1484 snd_iprintf(buffer, " GPIO mask : 0x%x\n",
1485 ice->eeprom.gpiomask);
1486 snd_iprintf(buffer, " GPIO state : 0x%x\n",
1487 ice->eeprom.gpiostate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 for (idx = 0x12; idx < ice->eeprom.size; idx++)
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001489 snd_iprintf(buffer, " Extra #%02i : 0x%x\n",
1490 idx, ice->eeprom.data[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
1492 snd_iprintf(buffer, "\nRegisters:\n");
1493
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001494 snd_iprintf(buffer, " PSDOUT03 : 0x%08x\n",
1495 (unsigned)inl(ICEMT1724(ice, ROUTE_PLAYBACK)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 for (idx = 0x0; idx < 0x20 ; idx++)
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001497 snd_iprintf(buffer, " CCS%02x : 0x%02x\n",
1498 idx, inb(ice->port+idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 for (idx = 0x0; idx < 0x30 ; idx++)
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001500 snd_iprintf(buffer, " MT%02x : 0x%02x\n",
1501 idx, inb(ice->profi_port+idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502}
1503
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001504static void __devinit snd_vt1724_proc_init(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001506 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001508 if (!snd_card_proc_new(ice->card, "ice1724", &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02001509 snd_info_set_text_ops(entry, ice, snd_vt1724_proc_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510}
1511
1512/*
1513 *
1514 */
1515
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001516static int snd_vt1724_eeprom_info(struct snd_kcontrol *kcontrol,
1517 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518{
1519 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001520 uinfo->count = sizeof(struct snd_ice1712_eeprom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 return 0;
1522}
1523
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001524static int snd_vt1724_eeprom_get(struct snd_kcontrol *kcontrol,
1525 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001527 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 memcpy(ucontrol->value.bytes.data, &ice->eeprom, sizeof(ice->eeprom));
1530 return 0;
1531}
1532
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01001533static struct snd_kcontrol_new snd_vt1724_eeprom __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1535 .name = "ICE1724 EEPROM",
1536 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1537 .info = snd_vt1724_eeprom_info,
1538 .get = snd_vt1724_eeprom_get
1539};
1540
1541/*
1542 */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001543static int snd_vt1724_spdif_info(struct snd_kcontrol *kcontrol,
1544 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545{
1546 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1547 uinfo->count = 1;
1548 return 0;
1549}
1550
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001551static unsigned int encode_spdif_bits(struct snd_aes_iec958 *diga)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552{
Takashi Iwai2dfbeca2005-10-12 10:04:42 +02001553 unsigned int val, rbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
1555 val = diga->status[0] & 0x03; /* professional, non-audio */
1556 if (val & 0x01) {
1557 /* professional */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001558 if ((diga->status[0] & IEC958_AES0_PRO_EMPHASIS) ==
1559 IEC958_AES0_PRO_EMPHASIS_5015)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 val |= 1U << 3;
Takashi Iwai2dfbeca2005-10-12 10:04:42 +02001561 rbits = (diga->status[4] >> 3) & 0x0f;
1562 if (rbits) {
1563 switch (rbits) {
1564 case 2: val |= 5 << 12; break; /* 96k */
1565 case 3: val |= 6 << 12; break; /* 192k */
1566 case 10: val |= 4 << 12; break; /* 88.2k */
1567 case 11: val |= 7 << 12; break; /* 176.4k */
1568 }
1569 } else {
1570 switch (diga->status[0] & IEC958_AES0_PRO_FS) {
1571 case IEC958_AES0_PRO_FS_44100:
1572 break;
1573 case IEC958_AES0_PRO_FS_32000:
1574 val |= 3U << 12;
1575 break;
1576 default:
1577 val |= 2U << 12;
1578 break;
1579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 }
1581 } else {
1582 /* consumer */
1583 val |= diga->status[1] & 0x04; /* copyright */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001584 if ((diga->status[0] & IEC958_AES0_CON_EMPHASIS) ==
1585 IEC958_AES0_CON_EMPHASIS_5015)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 val |= 1U << 3;
1587 val |= (unsigned int)(diga->status[1] & 0x3f) << 4; /* category */
1588 val |= (unsigned int)(diga->status[3] & IEC958_AES3_CON_FS) << 12; /* fs */
1589 }
1590 return val;
1591}
1592
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001593static void decode_spdif_bits(struct snd_aes_iec958 *diga, unsigned int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594{
1595 memset(diga->status, 0, sizeof(diga->status));
1596 diga->status[0] = val & 0x03; /* professional, non-audio */
1597 if (val & 0x01) {
1598 /* professional */
1599 if (val & (1U << 3))
1600 diga->status[0] |= IEC958_AES0_PRO_EMPHASIS_5015;
1601 switch ((val >> 12) & 0x7) {
1602 case 0:
1603 break;
1604 case 2:
1605 diga->status[0] |= IEC958_AES0_PRO_FS_32000;
1606 break;
1607 default:
1608 diga->status[0] |= IEC958_AES0_PRO_FS_48000;
1609 break;
1610 }
1611 } else {
1612 /* consumer */
1613 diga->status[0] |= val & (1U << 2); /* copyright */
1614 if (val & (1U << 3))
1615 diga->status[0] |= IEC958_AES0_CON_EMPHASIS_5015;
1616 diga->status[1] |= (val >> 4) & 0x3f; /* category */
1617 diga->status[3] |= (val >> 12) & 0x07; /* fs */
1618 }
1619}
1620
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001621static int snd_vt1724_spdif_default_get(struct snd_kcontrol *kcontrol,
1622 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001624 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 unsigned int val;
1626 val = inw(ICEMT1724(ice, SPDIF_CTRL));
1627 decode_spdif_bits(&ucontrol->value.iec958, val);
1628 return 0;
1629}
1630
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001631static int snd_vt1724_spdif_default_put(struct snd_kcontrol *kcontrol,
1632 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001634 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 unsigned int val, old;
1636
1637 val = encode_spdif_bits(&ucontrol->value.iec958);
1638 spin_lock_irq(&ice->reg_lock);
1639 old = inw(ICEMT1724(ice, SPDIF_CTRL));
1640 if (val != old)
1641 update_spdif_bits(ice, val);
1642 spin_unlock_irq(&ice->reg_lock);
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001643 return val != old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644}
1645
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01001646static struct snd_kcontrol_new snd_vt1724_spdif_default __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647{
1648 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001649 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 .info = snd_vt1724_spdif_info,
1651 .get = snd_vt1724_spdif_default_get,
1652 .put = snd_vt1724_spdif_default_put
1653};
1654
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001655static int snd_vt1724_spdif_maskc_get(struct snd_kcontrol *kcontrol,
1656 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657{
1658 ucontrol->value.iec958.status[0] = IEC958_AES0_NONAUDIO |
1659 IEC958_AES0_PROFESSIONAL |
1660 IEC958_AES0_CON_NOT_COPYRIGHT |
1661 IEC958_AES0_CON_EMPHASIS;
1662 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_ORIGINAL |
1663 IEC958_AES1_CON_CATEGORY;
1664 ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS;
1665 return 0;
1666}
1667
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001668static int snd_vt1724_spdif_maskp_get(struct snd_kcontrol *kcontrol,
1669 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670{
1671 ucontrol->value.iec958.status[0] = IEC958_AES0_NONAUDIO |
1672 IEC958_AES0_PROFESSIONAL |
1673 IEC958_AES0_PRO_FS |
1674 IEC958_AES0_PRO_EMPHASIS;
1675 return 0;
1676}
1677
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01001678static struct snd_kcontrol_new snd_vt1724_spdif_maskc __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679{
1680 .access = SNDRV_CTL_ELEM_ACCESS_READ,
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001681 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001682 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 .info = snd_vt1724_spdif_info,
1684 .get = snd_vt1724_spdif_maskc_get,
1685};
1686
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01001687static struct snd_kcontrol_new snd_vt1724_spdif_maskp __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688{
1689 .access = SNDRV_CTL_ELEM_ACCESS_READ,
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001690 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001691 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 .info = snd_vt1724_spdif_info,
1693 .get = snd_vt1724_spdif_maskp_get,
1694};
1695
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001696#define snd_vt1724_spdif_sw_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001698static int snd_vt1724_spdif_sw_get(struct snd_kcontrol *kcontrol,
1699 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001701 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
1702 ucontrol->value.integer.value[0] = inb(ICEREG1724(ice, SPDIF_CFG)) &
1703 VT1724_CFG_SPDIF_OUT_EN ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 return 0;
1705}
1706
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001707static int snd_vt1724_spdif_sw_put(struct snd_kcontrol *kcontrol,
1708 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001710 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 unsigned char old, val;
1712
1713 spin_lock_irq(&ice->reg_lock);
1714 old = val = inb(ICEREG1724(ice, SPDIF_CFG));
1715 val &= ~VT1724_CFG_SPDIF_OUT_EN;
1716 if (ucontrol->value.integer.value[0])
1717 val |= VT1724_CFG_SPDIF_OUT_EN;
1718 if (old != val)
1719 outb(val, ICEREG1724(ice, SPDIF_CFG));
1720 spin_unlock_irq(&ice->reg_lock);
1721 return old != val;
1722}
1723
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01001724static struct snd_kcontrol_new snd_vt1724_spdif_switch __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725{
1726 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1727 /* FIXME: the following conflict with IEC958 Playback Route */
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001728 /* .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH), */
1729 .name = SNDRV_CTL_NAME_IEC958("Output ", NONE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 .info = snd_vt1724_spdif_sw_info,
1731 .get = snd_vt1724_spdif_sw_get,
1732 .put = snd_vt1724_spdif_sw_put
1733};
1734
1735
1736#if 0 /* NOT USED YET */
1737/*
1738 * GPIO access from extern
1739 */
1740
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001741#define snd_vt1724_gpio_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001743int snd_vt1724_gpio_get(struct snd_kcontrol *kcontrol,
1744 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001746 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 int shift = kcontrol->private_value & 0xff;
1748 int invert = (kcontrol->private_value & (1<<24)) ? 1 : 0;
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001749
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 snd_ice1712_save_gpio_status(ice);
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001751 ucontrol->value.integer.value[0] =
1752 (snd_ice1712_gpio_read(ice) & (1 << shift) ? 1 : 0) ^ invert;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 snd_ice1712_restore_gpio_status(ice);
1754 return 0;
1755}
1756
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001757int snd_ice1712_gpio_put(struct snd_kcontrol *kcontrol,
1758 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001760 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 int shift = kcontrol->private_value & 0xff;
1762 int invert = (kcontrol->private_value & (1<<24)) ? mask : 0;
1763 unsigned int val, nval;
1764
1765 if (kcontrol->private_value & (1 << 31))
1766 return -EPERM;
1767 nval = (ucontrol->value.integer.value[0] ? (1 << shift) : 0) ^ invert;
1768 snd_ice1712_save_gpio_status(ice);
1769 val = snd_ice1712_gpio_read(ice);
1770 nval |= val & ~(1 << shift);
1771 if (val != nval)
1772 snd_ice1712_gpio_write(ice, nval);
1773 snd_ice1712_restore_gpio_status(ice);
1774 return val != nval;
1775}
1776#endif /* NOT USED YET */
1777
1778/*
1779 * rate
1780 */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001781static int snd_vt1724_pro_internal_clock_info(struct snd_kcontrol *kcontrol,
1782 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001784 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
1786 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1787 uinfo->count = 1;
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001788 uinfo->value.enumerated.items = ice->hw_rates->count + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1790 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001791 if (uinfo->value.enumerated.item == uinfo->value.enumerated.items - 1)
1792 strcpy(uinfo->value.enumerated.name, "IEC958 Input");
1793 else
1794 sprintf(uinfo->value.enumerated.name, "%d",
1795 ice->hw_rates->list[uinfo->value.enumerated.item]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 return 0;
1797}
1798
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001799static int snd_vt1724_pro_internal_clock_get(struct snd_kcontrol *kcontrol,
1800 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001802 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001803 unsigned int i, rate;
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001804
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 spin_lock_irq(&ice->reg_lock);
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001806 if (ice->is_spdif_master(ice)) {
1807 ucontrol->value.enumerated.item[0] = ice->hw_rates->count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 } else {
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001809 rate = ice->get_rate(ice);
1810 ucontrol->value.enumerated.item[0] = 0;
1811 for (i = 0; i < ice->hw_rates->count; i++) {
1812 if (ice->hw_rates->list[i] == rate) {
1813 ucontrol->value.enumerated.item[0] = i;
1814 break;
1815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 }
1818 spin_unlock_irq(&ice->reg_lock);
1819 return 0;
1820}
1821
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001822/* setting clock to external - SPDIF */
1823static void stdclock_set_spdif_clock(struct snd_ice1712 *ice)
1824{
1825 unsigned char oval;
1826 unsigned char i2s_oval;
1827 oval = inb(ICEMT1724(ice, RATE));
1828 outb(oval | VT1724_SPDIF_MASTER, ICEMT1724(ice, RATE));
1829 /* setting 256fs */
1830 i2s_oval = inb(ICEMT1724(ice, I2S_FORMAT));
1831 outb(i2s_oval & ~VT1724_MT_I2S_MCLK_128X, ICEMT1724(ice, I2S_FORMAT));
1832}
1833
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001834static int snd_vt1724_pro_internal_clock_put(struct snd_kcontrol *kcontrol,
1835 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001837 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001838 unsigned int old_rate, new_rate;
1839 unsigned int item = ucontrol->value.enumerated.item[0];
1840 unsigned int spdif = ice->hw_rates->count;
1841
1842 if (item > spdif)
1843 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
1845 spin_lock_irq(&ice->reg_lock);
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001846 if (ice->is_spdif_master(ice))
1847 old_rate = 0;
1848 else
1849 old_rate = ice->get_rate(ice);
1850 if (item == spdif) {
1851 /* switching to external clock via SPDIF */
1852 ice->set_spdif_clock(ice);
1853 new_rate = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 } else {
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001855 /* internal on-card clock */
1856 new_rate = ice->hw_rates->list[item];
1857 ice->pro_rate_default = new_rate;
1858 spin_unlock_irq(&ice->reg_lock);
1859 snd_vt1724_set_pro_rate(ice, ice->pro_rate_default, 1);
1860 spin_lock_irq(&ice->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 spin_unlock_irq(&ice->reg_lock);
1863
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001864 /* the first reset to the SPDIF master mode? */
1865 if (old_rate != new_rate && !new_rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 /* notify akm chips as well */
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001867 unsigned int i;
1868 if (ice->gpio.set_pro_rate)
1869 ice->gpio.set_pro_rate(ice, 0);
1870 for (i = 0; i < ice->akm_codecs; i++) {
1871 if (ice->akm[i].ops.set_rate_val)
1872 ice->akm[i].ops.set_rate_val(&ice->akm[i], 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 }
1874 }
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001875 return old_rate != new_rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876}
1877
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01001878static struct snd_kcontrol_new snd_vt1724_pro_internal_clock __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1880 .name = "Multi Track Internal Clock",
1881 .info = snd_vt1724_pro_internal_clock_info,
1882 .get = snd_vt1724_pro_internal_clock_get,
1883 .put = snd_vt1724_pro_internal_clock_put
1884};
1885
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001886#define snd_vt1724_pro_rate_locking_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001888static int snd_vt1724_pro_rate_locking_get(struct snd_kcontrol *kcontrol,
1889 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890{
1891 ucontrol->value.integer.value[0] = PRO_RATE_LOCKED;
1892 return 0;
1893}
1894
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001895static int snd_vt1724_pro_rate_locking_put(struct snd_kcontrol *kcontrol,
1896 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001898 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 int change = 0, nval;
1900
1901 nval = ucontrol->value.integer.value[0] ? 1 : 0;
1902 spin_lock_irq(&ice->reg_lock);
1903 change = PRO_RATE_LOCKED != nval;
1904 PRO_RATE_LOCKED = nval;
1905 spin_unlock_irq(&ice->reg_lock);
1906 return change;
1907}
1908
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01001909static struct snd_kcontrol_new snd_vt1724_pro_rate_locking __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1911 .name = "Multi Track Rate Locking",
1912 .info = snd_vt1724_pro_rate_locking_info,
1913 .get = snd_vt1724_pro_rate_locking_get,
1914 .put = snd_vt1724_pro_rate_locking_put
1915};
1916
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001917#define snd_vt1724_pro_rate_reset_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_reset_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_RESET ? 1 : 0;
1923 return 0;
1924}
1925
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001926static int snd_vt1724_pro_rate_reset_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_RESET != nval;
1935 PRO_RATE_RESET = 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_reset __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1942 .name = "Multi Track Rate Reset",
1943 .info = snd_vt1724_pro_rate_reset_info,
1944 .get = snd_vt1724_pro_rate_reset_get,
1945 .put = snd_vt1724_pro_rate_reset_put
1946};
1947
1948
1949/*
1950 * routing
1951 */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001952static int snd_vt1724_pro_route_info(struct snd_kcontrol *kcontrol,
1953 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954{
1955 static char *texts[] = {
1956 "PCM Out", /* 0 */
1957 "H/W In 0", "H/W In 1", /* 1-2 */
1958 "IEC958 In L", "IEC958 In R", /* 3-4 */
1959 };
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001960
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1962 uinfo->count = 1;
1963 uinfo->value.enumerated.items = 5;
1964 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1965 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
1966 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1967 return 0;
1968}
1969
1970static inline int analog_route_shift(int idx)
1971{
1972 return (idx % 2) * 12 + ((idx / 2) * 3) + 8;
1973}
1974
1975static inline int digital_route_shift(int idx)
1976{
1977 return idx * 3;
1978}
1979
Takashi Iwai2bf864a2009-05-06 17:30:17 +02001980int snd_ice1724_get_route_val(struct snd_ice1712 *ice, int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981{
1982 unsigned long val;
1983 unsigned char eitem;
Takashi Iwai32b47da2007-01-29 15:26:36 +01001984 static const unsigned char xlate[8] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 0, 255, 1, 2, 255, 255, 3, 4,
1986 };
1987
1988 val = inl(ICEMT1724(ice, ROUTE_PLAYBACK));
1989 val >>= shift;
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001990 val &= 7; /* we now have 3 bits per output */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 eitem = xlate[val];
1992 if (eitem == 255) {
1993 snd_BUG();
1994 return 0;
1995 }
1996 return eitem;
1997}
1998
Takashi Iwai2bf864a2009-05-06 17:30:17 +02001999int snd_ice1724_put_route_val(struct snd_ice1712 *ice, unsigned int val,
2000 int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001{
2002 unsigned int old_val, nval;
2003 int change;
Takashi Iwai32b47da2007-01-29 15:26:36 +01002004 static const unsigned char xroute[8] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 0, /* PCM */
2006 2, /* PSDIN0 Left */
2007 3, /* PSDIN0 Right */
2008 6, /* SPDIN Left */
2009 7, /* SPDIN Right */
2010 };
2011
2012 nval = xroute[val % 5];
2013 val = old_val = inl(ICEMT1724(ice, ROUTE_PLAYBACK));
2014 val &= ~(0x07 << shift);
2015 val |= nval << shift;
2016 change = val != old_val;
2017 if (change)
2018 outl(val, ICEMT1724(ice, ROUTE_PLAYBACK));
2019 return change;
2020}
2021
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002022static int snd_vt1724_pro_route_analog_get(struct snd_kcontrol *kcontrol,
2023 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002025 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002027 ucontrol->value.enumerated.item[0] =
Takashi Iwai2bf864a2009-05-06 17:30:17 +02002028 snd_ice1724_get_route_val(ice, analog_route_shift(idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 return 0;
2030}
2031
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002032static int snd_vt1724_pro_route_analog_put(struct snd_kcontrol *kcontrol,
2033 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002035 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai2bf864a2009-05-06 17:30:17 +02002037 return snd_ice1724_put_route_val(ice,
2038 ucontrol->value.enumerated.item[0],
2039 analog_route_shift(idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040}
2041
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002042static int snd_vt1724_pro_route_spdif_get(struct snd_kcontrol *kcontrol,
2043 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002045 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002047 ucontrol->value.enumerated.item[0] =
Takashi Iwai2bf864a2009-05-06 17:30:17 +02002048 snd_ice1724_get_route_val(ice, digital_route_shift(idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 return 0;
2050}
2051
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002052static int snd_vt1724_pro_route_spdif_put(struct snd_kcontrol *kcontrol,
2053 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002055 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai2bf864a2009-05-06 17:30:17 +02002057 return snd_ice1724_put_route_val(ice,
2058 ucontrol->value.enumerated.item[0],
2059 digital_route_shift(idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060}
2061
Takashi Iwai2bf864a2009-05-06 17:30:17 +02002062static struct snd_kcontrol_new snd_vt1724_mixer_pro_analog_route __devinitdata =
2063{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2065 .name = "H/W Playback Route",
2066 .info = snd_vt1724_pro_route_info,
2067 .get = snd_vt1724_pro_route_analog_get,
2068 .put = snd_vt1724_pro_route_analog_put,
2069};
2070
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01002071static struct snd_kcontrol_new snd_vt1724_mixer_pro_spdif_route __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002073 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, NONE) "Route",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 .info = snd_vt1724_pro_route_info,
2075 .get = snd_vt1724_pro_route_spdif_get,
2076 .put = snd_vt1724_pro_route_spdif_put,
2077 .count = 2,
2078};
2079
2080
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002081static int snd_vt1724_pro_peak_info(struct snd_kcontrol *kcontrol,
2082 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083{
2084 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2085 uinfo->count = 22; /* FIXME: for compatibility with ice1712... */
2086 uinfo->value.integer.min = 0;
2087 uinfo->value.integer.max = 255;
2088 return 0;
2089}
2090
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002091static int snd_vt1724_pro_peak_get(struct snd_kcontrol *kcontrol,
2092 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002094 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 int idx;
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002096
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 spin_lock_irq(&ice->reg_lock);
2098 for (idx = 0; idx < 22; idx++) {
2099 outb(idx, ICEMT1724(ice, MONITOR_PEAKINDEX));
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002100 ucontrol->value.integer.value[idx] =
2101 inb(ICEMT1724(ice, MONITOR_PEAKDATA));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 }
2103 spin_unlock_irq(&ice->reg_lock);
2104 return 0;
2105}
2106
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01002107static struct snd_kcontrol_new snd_vt1724_mixer_pro_peak __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2109 .name = "Multi Track Peak",
2110 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2111 .info = snd_vt1724_pro_peak_info,
2112 .get = snd_vt1724_pro_peak_get
2113};
2114
2115/*
2116 *
2117 */
2118
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01002119static struct snd_ice1712_card_info no_matched __devinitdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01002121static struct snd_ice1712_card_info *card_tables[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 snd_vt1724_revo_cards,
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002123 snd_vt1724_amp_cards,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 snd_vt1724_aureon_cards,
2125 snd_vt1720_mobo_cards,
2126 snd_vt1720_pontis_cards,
Julian Scheel6b8d6e52008-01-16 19:50:00 +01002127 snd_vt1724_prodigy_hifi_cards,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 snd_vt1724_prodigy192_cards,
2129 snd_vt1724_juli_cards,
Takashi Iwai72cbfd42009-05-06 17:33:19 +02002130 snd_vt1724_maya44_cards,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 snd_vt1724_phase_cards,
Clement Guedezf6cdab52007-01-08 10:48:41 +01002132 snd_vt1724_wtm_cards,
Shin-ya Okadaf31639b2007-10-23 15:08:18 +02002133 snd_vt1724_se_cards,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 NULL,
2135};
2136
2137
2138/*
2139 */
2140
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002141static void wait_i2c_busy(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142{
2143 int t = 0x10000;
2144 while ((inb(ICEREG1724(ice, I2C_CTRL)) & VT1724_I2C_BUSY) && t--)
2145 ;
2146 if (t == -1)
2147 printk(KERN_ERR "ice1724: i2c busy timeout\n");
2148}
2149
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002150unsigned char snd_vt1724_read_i2c(struct snd_ice1712 *ice,
2151 unsigned char dev, unsigned char addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152{
2153 unsigned char val;
2154
Ingo Molnar62932df2006-01-16 16:34:20 +01002155 mutex_lock(&ice->i2c_mutex);
Pavel Hofmanacec30f2007-12-03 12:37:17 +01002156 wait_i2c_busy(ice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 outb(addr, ICEREG1724(ice, I2C_BYTE_ADDR));
2158 outb(dev & ~VT1724_I2C_WRITE, ICEREG1724(ice, I2C_DEV_ADDR));
2159 wait_i2c_busy(ice);
2160 val = inb(ICEREG1724(ice, I2C_DATA));
Ingo Molnar62932df2006-01-16 16:34:20 +01002161 mutex_unlock(&ice->i2c_mutex);
Takashi Iwaie2ea7cfc2009-02-05 16:07:02 +01002162 /*
2163 printk(KERN_DEBUG "i2c_read: [0x%x,0x%x] = 0x%x\n", dev, addr, val);
2164 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 return val;
2166}
2167
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002168void snd_vt1724_write_i2c(struct snd_ice1712 *ice,
2169 unsigned char dev, unsigned char addr, unsigned char data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170{
Ingo Molnar62932df2006-01-16 16:34:20 +01002171 mutex_lock(&ice->i2c_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 wait_i2c_busy(ice);
Takashi Iwaie2ea7cfc2009-02-05 16:07:02 +01002173 /*
2174 printk(KERN_DEBUG "i2c_write: [0x%x,0x%x] = 0x%x\n", dev, addr, data);
2175 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 outb(addr, ICEREG1724(ice, I2C_BYTE_ADDR));
2177 outb(data, ICEREG1724(ice, I2C_DATA));
2178 outb(dev | VT1724_I2C_WRITE, ICEREG1724(ice, I2C_DEV_ADDR));
2179 wait_i2c_busy(ice);
Ingo Molnar62932df2006-01-16 16:34:20 +01002180 mutex_unlock(&ice->i2c_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181}
2182
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002183static int __devinit snd_vt1724_read_eeprom(struct snd_ice1712 *ice,
2184 const char *modelname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185{
2186 const int dev = 0xa0; /* EEPROM device address */
2187 unsigned int i, size;
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01002188 struct snd_ice1712_card_info * const *tbl, *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002190 if (!modelname || !*modelname) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 ice->eeprom.subvendor = 0;
2192 if ((inb(ICEREG1724(ice, I2C_CTRL)) & VT1724_I2C_EEPROM) != 0)
2193 ice->eeprom.subvendor =
2194 (snd_vt1724_read_i2c(ice, dev, 0x00) << 0) |
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002195 (snd_vt1724_read_i2c(ice, dev, 0x01) << 8) |
2196 (snd_vt1724_read_i2c(ice, dev, 0x02) << 16) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 (snd_vt1724_read_i2c(ice, dev, 0x03) << 24);
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002198 if (ice->eeprom.subvendor == 0 ||
2199 ice->eeprom.subvendor == (unsigned int)-1) {
2200 /* invalid subvendor from EEPROM, try the PCI
2201 * subststem ID instead
2202 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 u16 vendor, device;
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002204 pci_read_config_word(ice->pci, PCI_SUBSYSTEM_VENDOR_ID,
2205 &vendor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 pci_read_config_word(ice->pci, PCI_SUBSYSTEM_ID, &device);
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002207 ice->eeprom.subvendor =
2208 ((unsigned int)swab16(vendor) << 16) | swab16(device);
2209 if (ice->eeprom.subvendor == 0 ||
2210 ice->eeprom.subvendor == (unsigned int)-1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 printk(KERN_ERR "ice1724: No valid ID is found\n");
2212 return -ENXIO;
2213 }
2214 }
2215 }
2216 for (tbl = card_tables; *tbl; tbl++) {
2217 for (c = *tbl; c->subvendor; c++) {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002218 if (modelname && c->model &&
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002219 !strcmp(modelname, c->model)) {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002220 printk(KERN_INFO "ice1724: Using board model %s\n",
2221 c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 ice->eeprom.subvendor = c->subvendor;
2223 } else if (c->subvendor != ice->eeprom.subvendor)
2224 continue;
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002225 if (!c->eeprom_size || !c->eeprom_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 goto found;
2227 /* if the EEPROM is given by the driver, use it */
2228 snd_printdd("using the defined eeprom..\n");
2229 ice->eeprom.version = 2;
2230 ice->eeprom.size = c->eeprom_size + 6;
2231 memcpy(ice->eeprom.data, c->eeprom_data, c->eeprom_size);
2232 goto read_skipped;
2233 }
2234 }
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002235 printk(KERN_WARNING "ice1724: No matching model found for ID 0x%x\n",
2236 ice->eeprom.subvendor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
2238 found:
2239 ice->eeprom.size = snd_vt1724_read_i2c(ice, dev, 0x04);
2240 if (ice->eeprom.size < 6)
2241 ice->eeprom.size = 32;
2242 else if (ice->eeprom.size > 32) {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002243 printk(KERN_ERR "ice1724: Invalid EEPROM (size = %i)\n",
2244 ice->eeprom.size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 return -EIO;
2246 }
2247 ice->eeprom.version = snd_vt1724_read_i2c(ice, dev, 0x05);
2248 if (ice->eeprom.version != 2)
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002249 printk(KERN_WARNING "ice1724: Invalid EEPROM version %i\n",
2250 ice->eeprom.version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 size = ice->eeprom.size - 6;
2252 for (i = 0; i < size; i++)
2253 ice->eeprom.data[i] = snd_vt1724_read_i2c(ice, dev, i + 6);
2254
2255 read_skipped:
2256 ice->eeprom.gpiomask = eeprom_triple(ice, ICE_EEP2_GPIO_MASK);
2257 ice->eeprom.gpiostate = eeprom_triple(ice, ICE_EEP2_GPIO_STATE);
2258 ice->eeprom.gpiodir = eeprom_triple(ice, ICE_EEP2_GPIO_DIR);
2259
2260 return 0;
2261}
2262
2263
2264
Karsten Wiese988f0662008-04-22 12:52:15 +02002265static void __devinit snd_vt1724_chip_reset(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266{
2267 outb(VT1724_RESET , ICEREG1724(ice, CONTROL));
Takashi Iwaid82b64f2009-05-06 17:25:42 +02002268 inb(ICEREG1724(ice, CONTROL)); /* pci posting flush */
Karsten Wiese988f0662008-04-22 12:52:15 +02002269 msleep(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 outb(0, ICEREG1724(ice, CONTROL));
Takashi Iwaid82b64f2009-05-06 17:25:42 +02002271 inb(ICEREG1724(ice, CONTROL)); /* pci posting flush */
Karsten Wiese988f0662008-04-22 12:52:15 +02002272 msleep(10);
2273}
2274
2275static int __devinit snd_vt1724_chip_init(struct snd_ice1712 *ice)
2276{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 outb(ice->eeprom.data[ICE_EEP2_SYSCONF], ICEREG1724(ice, SYS_CFG));
2278 outb(ice->eeprom.data[ICE_EEP2_ACLINK], ICEREG1724(ice, AC97_CFG));
2279 outb(ice->eeprom.data[ICE_EEP2_I2S], ICEREG1724(ice, I2S_FEATURES));
2280 outb(ice->eeprom.data[ICE_EEP2_SPDIF], ICEREG1724(ice, SPDIF_CFG));
2281
2282 ice->gpio.write_mask = ice->eeprom.gpiomask;
2283 ice->gpio.direction = ice->eeprom.gpiodir;
2284 snd_vt1724_set_gpio_mask(ice, ice->eeprom.gpiomask);
2285 snd_vt1724_set_gpio_dir(ice, ice->eeprom.gpiodir);
2286 snd_vt1724_set_gpio_data(ice, ice->eeprom.gpiostate);
2287
2288 outb(0, ICEREG1724(ice, POWERDOWN));
2289
2290 return 0;
2291}
2292
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002293static int __devinit snd_vt1724_spdif_build_controls(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294{
2295 int err;
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002296 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
Takashi Iwaida3cec32008-08-08 17:12:14 +02002298 if (snd_BUG_ON(!ice->pcm))
2299 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300
Takashi Iwai2bf864a2009-05-06 17:30:17 +02002301 if (!ice->own_routing) {
2302 err = snd_ctl_add(ice->card,
2303 snd_ctl_new1(&snd_vt1724_mixer_pro_spdif_route, ice));
2304 if (err < 0)
2305 return err;
2306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307
2308 err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_spdif_switch, ice));
2309 if (err < 0)
2310 return err;
2311
2312 err = snd_ctl_add(ice->card, kctl = snd_ctl_new1(&snd_vt1724_spdif_default, ice));
2313 if (err < 0)
2314 return err;
2315 kctl->id.device = ice->pcm->device;
2316 err = snd_ctl_add(ice->card, kctl = snd_ctl_new1(&snd_vt1724_spdif_maskc, ice));
2317 if (err < 0)
2318 return err;
2319 kctl->id.device = ice->pcm->device;
2320 err = snd_ctl_add(ice->card, kctl = snd_ctl_new1(&snd_vt1724_spdif_maskp, ice));
2321 if (err < 0)
2322 return err;
2323 kctl->id.device = ice->pcm->device;
2324#if 0 /* use default only */
2325 err = snd_ctl_add(ice->card, kctl = snd_ctl_new1(&snd_vt1724_spdif_stream, ice));
2326 if (err < 0)
2327 return err;
2328 kctl->id.device = ice->pcm->device;
2329 ice->spdif.stream_ctl = kctl;
2330#endif
2331 return 0;
2332}
2333
2334
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002335static int __devinit snd_vt1724_build_controls(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336{
2337 int err;
2338
2339 err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_eeprom, ice));
2340 if (err < 0)
2341 return err;
2342 err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_pro_internal_clock, ice));
2343 if (err < 0)
2344 return err;
2345
2346 err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_pro_rate_locking, ice));
2347 if (err < 0)
2348 return err;
2349 err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_pro_rate_reset, ice));
2350 if (err < 0)
2351 return err;
2352
Takashi Iwai2bf864a2009-05-06 17:30:17 +02002353 if (!ice->own_routing && ice->num_total_dacs > 0) {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002354 struct snd_kcontrol_new tmp = snd_vt1724_mixer_pro_analog_route;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 tmp.count = ice->num_total_dacs;
2356 if (ice->vt1720 && tmp.count > 2)
2357 tmp.count = 2;
2358 err = snd_ctl_add(ice->card, snd_ctl_new1(&tmp, ice));
2359 if (err < 0)
2360 return err;
2361 }
2362
2363 err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_mixer_pro_peak, ice));
2364 if (err < 0)
2365 return err;
2366
2367 return 0;
2368}
2369
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002370static int snd_vt1724_free(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371{
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002372 if (!ice->port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 goto __hw_end;
2374 /* mask all interrupts */
2375 outb(0xff, ICEMT1724(ice, DMA_INT_MASK));
2376 outb(0xff, ICEREG1724(ice, IRQMASK));
2377 /* --- */
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002378__hw_end:
Jeff Garzikf000fd82008-04-22 13:50:34 +02002379 if (ice->irq >= 0)
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002380 free_irq(ice->irq, ice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 pci_release_regions(ice->pci);
2382 snd_ice1712_akm4xxx_free(ice);
2383 pci_disable_device(ice->pci);
Takashi Iwai7cda8ba2008-01-18 13:36:07 +01002384 kfree(ice->spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 kfree(ice);
2386 return 0;
2387}
2388
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002389static int snd_vt1724_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002391 struct snd_ice1712 *ice = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 return snd_vt1724_free(ice);
2393}
2394
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002395static int __devinit snd_vt1724_create(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 struct pci_dev *pci,
2397 const char *modelname,
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002398 struct snd_ice1712 **r_ice1712)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002400 struct snd_ice1712 *ice;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 int err;
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002402 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 .dev_free = snd_vt1724_dev_free,
2404 };
2405
2406 *r_ice1712 = NULL;
2407
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002408 /* enable PCI device */
2409 err = pci_enable_device(pci);
2410 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 return err;
2412
Takashi Iwaie560d8d2005-09-09 14:21:46 +02002413 ice = kzalloc(sizeof(*ice), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 if (ice == NULL) {
2415 pci_disable_device(pci);
2416 return -ENOMEM;
2417 }
2418 ice->vt1724 = 1;
2419 spin_lock_init(&ice->reg_lock);
Ingo Molnar62932df2006-01-16 16:34:20 +01002420 mutex_init(&ice->gpio_mutex);
2421 mutex_init(&ice->open_mutex);
2422 mutex_init(&ice->i2c_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 ice->gpio.set_mask = snd_vt1724_set_gpio_mask;
2424 ice->gpio.set_dir = snd_vt1724_set_gpio_dir;
2425 ice->gpio.set_data = snd_vt1724_set_gpio_data;
2426 ice->gpio.get_data = snd_vt1724_get_gpio_data;
2427 ice->card = card;
2428 ice->pci = pci;
2429 ice->irq = -1;
2430 pci_set_master(pci);
2431 snd_vt1724_proc_init(ice);
2432 synchronize_irq(pci->irq);
2433
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002434 err = pci_request_regions(pci, "ICE1724");
2435 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 kfree(ice);
2437 pci_disable_device(pci);
2438 return err;
2439 }
2440 ice->port = pci_resource_start(pci, 0);
2441 ice->profi_port = pci_resource_start(pci, 1);
2442
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002443 if (request_irq(pci->irq, snd_vt1724_interrupt,
Takashi Iwai437a5a42006-11-21 12:14:23 +01002444 IRQF_SHARED, "ICE1724", ice)) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02002445 snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 snd_vt1724_free(ice);
2447 return -EIO;
2448 }
2449
2450 ice->irq = pci->irq;
2451
Karsten Wiese988f0662008-04-22 12:52:15 +02002452 snd_vt1724_chip_reset(ice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 if (snd_vt1724_read_eeprom(ice, modelname) < 0) {
2454 snd_vt1724_free(ice);
2455 return -EIO;
2456 }
2457 if (snd_vt1724_chip_init(ice) < 0) {
2458 snd_vt1724_free(ice);
2459 return -EIO;
2460 }
2461
Takashi Iwai6834d7c2008-11-05 17:41:23 +01002462 /* MPU_RX and TX irq masks are cleared later dynamically */
2463 outb(VT1724_IRQ_MPU_RX | VT1724_IRQ_MPU_TX , ICEREG1724(ice, IRQMASK));
Takashi Iwai4074ea22008-11-01 11:01:50 +01002464
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002465 /* don't handle FIFO overrun/underruns (just yet),
2466 * since they cause machine lockups
2467 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 outb(VT1724_MULTI_FIFO_ERR, ICEMT1724(ice, DMA_INT_MASK));
2469
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002470 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, ice, &ops);
2471 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 snd_vt1724_free(ice);
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002473 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 }
2475
2476 snd_card_set_dev(card, &pci->dev);
2477
2478 *r_ice1712 = ice;
2479 return 0;
2480}
2481
2482
2483/*
2484 *
2485 * Registration
2486 *
2487 */
2488
2489static int __devinit snd_vt1724_probe(struct pci_dev *pci,
2490 const struct pci_device_id *pci_id)
2491{
2492 static int dev;
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002493 struct snd_card *card;
2494 struct snd_ice1712 *ice;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 int pcm_dev = 0, err;
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01002496 struct snd_ice1712_card_info * const *tbl, *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
2498 if (dev >= SNDRV_CARDS)
2499 return -ENODEV;
2500 if (!enable[dev]) {
2501 dev++;
2502 return -ENOENT;
2503 }
2504
Takashi Iwaie58de7b2008-12-28 16:44:30 +01002505 err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
2506 if (err < 0)
2507 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508
2509 strcpy(card->driver, "ICE1724");
2510 strcpy(card->shortname, "ICEnsemble ICE1724");
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002511
2512 err = snd_vt1724_create(card, pci, model[dev], &ice);
2513 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 snd_card_free(card);
2515 return err;
2516 }
2517
2518 for (tbl = card_tables; *tbl; tbl++) {
2519 for (c = *tbl; c->subvendor; c++) {
2520 if (c->subvendor == ice->eeprom.subvendor) {
2521 strcpy(card->shortname, c->name);
2522 if (c->driver) /* specific driver? */
2523 strcpy(card->driver, c->driver);
2524 if (c->chip_init) {
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002525 err = c->chip_init(ice);
2526 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 snd_card_free(card);
2528 return err;
2529 }
2530 }
2531 goto __found;
2532 }
2533 }
2534 }
2535 c = &no_matched;
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002536__found:
2537 /*
2538 * VT1724 has separate DMAs for the analog and the SPDIF streams while
2539 * ICE1712 has only one for both (mixed up).
2540 *
2541 * Confusingly the analog PCM is named "professional" here because it
2542 * was called so in ice1712 driver, and vt1724 driver is derived from
2543 * ice1712 driver.
2544 */
Pavel Hofmand16be8e2008-03-20 12:10:27 +01002545 ice->pro_rate_default = PRO_RATE_DEFAULT;
2546 if (!ice->is_spdif_master)
2547 ice->is_spdif_master = stdclock_is_spdif_master;
2548 if (!ice->get_rate)
2549 ice->get_rate = stdclock_get_rate;
2550 if (!ice->set_rate)
2551 ice->set_rate = stdclock_set_rate;
2552 if (!ice->set_mclk)
2553 ice->set_mclk = stdclock_set_mclk;
2554 if (!ice->set_spdif_clock)
2555 ice->set_spdif_clock = stdclock_set_spdif_clock;
2556 if (!ice->hw_rates)
2557 set_std_hw_rates(ice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002559 err = snd_vt1724_pcm_profi(ice, pcm_dev++);
2560 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 snd_card_free(card);
2562 return err;
2563 }
2564
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002565 err = snd_vt1724_pcm_spdif(ice, pcm_dev++);
2566 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 snd_card_free(card);
2568 return err;
2569 }
2570
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002571 err = snd_vt1724_pcm_indep(ice, pcm_dev++);
2572 if (err < 0) {
2573 snd_card_free(card);
2574 return err;
2575 }
2576
2577 err = snd_vt1724_ac97_mixer(ice);
2578 if (err < 0) {
2579 snd_card_free(card);
2580 return err;
2581 }
2582
2583 err = snd_vt1724_build_controls(ice);
2584 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 snd_card_free(card);
2586 return err;
2587 }
2588
2589 if (ice->pcm && ice->has_spdif) { /* has SPDIF I/O */
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002590 err = snd_vt1724_spdif_build_controls(ice);
2591 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 snd_card_free(card);
2593 return err;
2594 }
2595 }
2596
2597 if (c->build_controls) {
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002598 err = c->build_controls(ice);
2599 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 snd_card_free(card);
2601 return err;
2602 }
2603 }
2604
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002605 if (!c->no_mpu401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 if (ice->eeprom.data[ICE_EEP2_SYSCONF] & VT1724_CFG_MPU401) {
Clemens Ladischaea3bfb2008-05-20 14:22:44 +02002607 struct snd_rawmidi *rmidi;
2608
2609 err = snd_rawmidi_new(card, "MIDI", 0, 1, 1, &rmidi);
2610 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 snd_card_free(card);
2612 return err;
2613 }
Clemens Ladischaea3bfb2008-05-20 14:22:44 +02002614 ice->rmidi[0] = rmidi;
2615 rmidi->private_data = ice;
2616 strcpy(rmidi->name, "ICE1724 MIDI");
2617 rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
2618 SNDRV_RAWMIDI_INFO_INPUT |
2619 SNDRV_RAWMIDI_INFO_DUPLEX;
2620 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
2621 &vt1724_midi_output_ops);
2622 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT,
2623 &vt1724_midi_input_ops);
2624
Takashi Iwai3a841d52008-04-23 17:47:28 +02002625 /* set watermarks */
2626 outb(VT1724_MPU_RX_FIFO | 0x1,
2627 ICEREG1724(ice, MPU_FIFO_WM));
2628 outb(0x1, ICEREG1724(ice, MPU_FIFO_WM));
Clemens Ladischaea3bfb2008-05-20 14:22:44 +02002629 /* set UART mode */
2630 outb(VT1724_MPU_UART, ICEREG1724(ice, MPU_CTRL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 }
2632 }
2633
2634 sprintf(card->longname, "%s at 0x%lx, irq %i",
2635 card->shortname, ice->port, ice->irq);
2636
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002637 err = snd_card_register(card);
2638 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 snd_card_free(card);
2640 return err;
2641 }
2642 pci_set_drvdata(pci, card);
2643 dev++;
2644 return 0;
2645}
2646
2647static void __devexit snd_vt1724_remove(struct pci_dev *pci)
2648{
2649 snd_card_free(pci_get_drvdata(pci));
2650 pci_set_drvdata(pci, NULL);
2651}
2652
2653static struct pci_driver driver = {
2654 .name = "ICE1724",
2655 .id_table = snd_vt1724_ids,
2656 .probe = snd_vt1724_probe,
2657 .remove = __devexit_p(snd_vt1724_remove),
2658};
2659
2660static int __init alsa_card_ice1724_init(void)
2661{
Takashi Iwai01d25d42005-04-11 16:58:24 +02002662 return pci_register_driver(&driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663}
2664
2665static void __exit alsa_card_ice1724_exit(void)
2666{
2667 pci_unregister_driver(&driver);
2668}
2669
2670module_init(alsa_card_ice1724_init)
2671module_exit(alsa_card_ice1724_exit)