blob: e7369f4cca76499dc6f4b5191261368ee7916665 [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>
Paul Gortmaker65a77212011-07-15 13:13:37 -040031#include <linux/module.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"
Pavel Hofman6ef80702009-09-16 22:25:41 +020056#include "quartet.h"
Ondrej Zary77b0b252012-10-14 21:09:22 +020057#include "psc724.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Jaroslav Kyselac1017a42007-10-15 09:50:19 +020059MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070060MODULE_DESCRIPTION("VIA ICEnsemble ICE1724/1720 (Envy24HT/PT)");
61MODULE_LICENSE("GPL");
62MODULE_SUPPORTED_DEVICE("{"
63 REVO_DEVICE_DESC
64 AMP_AUDIO2000_DEVICE_DESC
65 AUREON_DEVICE_DESC
66 VT1720_MOBO_DEVICE_DESC
67 PONTIS_DEVICE_DESC
68 PRODIGY192_DEVICE_DESC
Julian Scheel6b8d6e52008-01-16 19:50:00 +010069 PRODIGY_HIFI_DEVICE_DESC
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 JULI_DEVICE_DESC
Takashi Iwai72cbfd42009-05-06 17:33:19 +020071 MAYA44_DEVICE_DESC
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 PHASE_DEVICE_DESC
Clement Guedezf6cdab52007-01-08 10:48:41 +010073 WTM_DEVICE_DESC
Shin-ya Okadaf31639b2007-10-23 15:08:18 +020074 SE_DEVICE_DESC
Pavel Hofman6ef80702009-09-16 22:25:41 +020075 QTET_DEVICE_DESC
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 "{VIA,VT1720},"
77 "{VIA,VT1724},"
78 "{ICEnsemble,Generic ICE1724},"
79 "{ICEnsemble,Generic Envy24HT}"
80 "{ICEnsemble,Generic Envy24PT}}");
81
82static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
83static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
Rusty Russella67ff6a2011-12-15 13:49:36 +103084static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
Linus Torvalds1da177e2005-04-16 15:20:36 -070085static char *model[SNDRV_CARDS];
86
87module_param_array(index, int, NULL, 0444);
88MODULE_PARM_DESC(index, "Index value for ICE1724 soundcard.");
89module_param_array(id, charp, NULL, 0444);
90MODULE_PARM_DESC(id, "ID string for ICE1724 soundcard.");
91module_param_array(enable, bool, NULL, 0444);
92MODULE_PARM_DESC(enable, "Enable ICE1724 soundcard.");
93module_param_array(model, charp, NULL, 0444);
94MODULE_PARM_DESC(model, "Use the given board model.");
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97/* Both VT1720 and VT1724 have the same PCI IDs */
Alexey Dobriyancebe41d2010-02-06 00:21:03 +020098static DEFINE_PCI_DEVICE_TABLE(snd_vt1724_ids) = {
Joe Perches28d27aa2009-06-24 22:13:35 -070099 { PCI_VDEVICE(ICE, PCI_DEVICE_ID_VT1724), 0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 { 0, }
101};
102
103MODULE_DEVICE_TABLE(pci, snd_vt1724_ids);
104
105
106static int PRO_RATE_LOCKED;
107static int PRO_RATE_RESET = 1;
108static unsigned int PRO_RATE_DEFAULT = 44100;
109
Takashi Iwaia2af0502012-10-17 09:21:48 +0200110static const char * const ext_clock_names[1] = { "IEC958 In" };
Pavel Hofman1ff97cb2009-09-16 22:25:40 +0200111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112/*
113 * Basic I/O
114 */
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +0400115
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100116/*
117 * default rates, default clock routines
118 */
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120/* check whether the clock mode is spdif-in */
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100121static inline int stdclock_is_spdif_master(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
123 return (inb(ICEMT1724(ice, RATE)) & VT1724_SPDIF_MASTER) ? 1 : 0;
124}
125
Pavel Hofman6796d5a2009-09-16 22:25:39 +0200126/*
127 * locking rate makes sense only for internal clock mode
128 */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100129static inline int is_pro_rate_locked(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
Pavel Hofman6796d5a2009-09-16 22:25:39 +0200131 return (!ice->is_spdif_master(ice)) && PRO_RATE_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
134/*
135 * ac97 section
136 */
137
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100138static unsigned char snd_vt1724_ac97_ready(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
140 unsigned char old_cmd;
141 int tm;
142 for (tm = 0; tm < 0x10000; tm++) {
143 old_cmd = inb(ICEMT1724(ice, AC97_CMD));
144 if (old_cmd & (VT1724_AC97_WRITE | VT1724_AC97_READ))
145 continue;
146 if (!(old_cmd & VT1724_AC97_READY))
147 continue;
148 return old_cmd;
149 }
150 snd_printd(KERN_ERR "snd_vt1724_ac97_ready: timeout\n");
151 return old_cmd;
152}
153
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100154static int snd_vt1724_ac97_wait_bit(struct snd_ice1712 *ice, unsigned char bit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
156 int tm;
157 for (tm = 0; tm < 0x10000; tm++)
158 if ((inb(ICEMT1724(ice, AC97_CMD)) & bit) == 0)
159 return 0;
160 snd_printd(KERN_ERR "snd_vt1724_ac97_wait_bit: timeout\n");
161 return -EIO;
162}
163
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100164static void snd_vt1724_ac97_write(struct snd_ac97 *ac97,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 unsigned short reg,
166 unsigned short val)
167{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100168 struct snd_ice1712 *ice = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 unsigned char old_cmd;
170
171 old_cmd = snd_vt1724_ac97_ready(ice);
172 old_cmd &= ~VT1724_AC97_ID_MASK;
173 old_cmd |= ac97->num;
174 outb(reg, ICEMT1724(ice, AC97_INDEX));
175 outw(val, ICEMT1724(ice, AC97_DATA));
176 outb(old_cmd | VT1724_AC97_WRITE, ICEMT1724(ice, AC97_CMD));
177 snd_vt1724_ac97_wait_bit(ice, VT1724_AC97_WRITE);
178}
179
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100180static unsigned short snd_vt1724_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100182 struct snd_ice1712 *ice = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 unsigned char old_cmd;
184
185 old_cmd = snd_vt1724_ac97_ready(ice);
186 old_cmd &= ~VT1724_AC97_ID_MASK;
187 old_cmd |= ac97->num;
188 outb(reg, ICEMT1724(ice, AC97_INDEX));
189 outb(old_cmd | VT1724_AC97_READ, ICEMT1724(ice, AC97_CMD));
190 if (snd_vt1724_ac97_wait_bit(ice, VT1724_AC97_READ) < 0)
191 return ~0;
192 return inw(ICEMT1724(ice, AC97_DATA));
193}
194
195
196/*
197 * GPIO operations
198 */
199
200/* set gpio direction 0 = read, 1 = write */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100201static void snd_vt1724_set_gpio_dir(struct snd_ice1712 *ice, unsigned int data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
203 outl(data, ICEREG1724(ice, GPIO_DIRECTION));
204 inw(ICEREG1724(ice, GPIO_DIRECTION)); /* dummy read for pci-posting */
205}
206
Pavel Hofman49470302009-09-16 22:25:38 +0200207/* get gpio direction 0 = read, 1 = write */
208static unsigned int snd_vt1724_get_gpio_dir(struct snd_ice1712 *ice)
209{
210 return inl(ICEREG1724(ice, GPIO_DIRECTION));
211}
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213/* set the gpio mask (0 = writable) */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100214static void snd_vt1724_set_gpio_mask(struct snd_ice1712 *ice, unsigned int data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
216 outw(data, ICEREG1724(ice, GPIO_WRITE_MASK));
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +0400217 if (!ice->vt1720) /* VT1720 supports only 16 GPIO bits */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 outb((data >> 16) & 0xff, ICEREG1724(ice, GPIO_WRITE_MASK_22));
219 inw(ICEREG1724(ice, GPIO_WRITE_MASK)); /* dummy read for pci-posting */
220}
221
Pavel Hofman49470302009-09-16 22:25:38 +0200222static unsigned int snd_vt1724_get_gpio_mask(struct snd_ice1712 *ice)
223{
224 unsigned int mask;
225 if (!ice->vt1720)
226 mask = (unsigned int)inb(ICEREG1724(ice, GPIO_WRITE_MASK_22));
227 else
228 mask = 0;
229 mask = (mask << 16) | inw(ICEREG1724(ice, GPIO_WRITE_MASK));
230 return mask;
231}
232
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100233static void snd_vt1724_set_gpio_data(struct snd_ice1712 *ice, unsigned int data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
235 outw(data, ICEREG1724(ice, GPIO_DATA));
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +0400236 if (!ice->vt1720)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 outb(data >> 16, ICEREG1724(ice, GPIO_DATA_22));
238 inw(ICEREG1724(ice, GPIO_DATA)); /* dummy read for pci-posting */
239}
240
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100241static unsigned int snd_vt1724_get_gpio_data(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
243 unsigned int data;
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +0400244 if (!ice->vt1720)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 data = (unsigned int)inb(ICEREG1724(ice, GPIO_DATA_22));
246 else
247 data = 0;
248 data = (data << 16) | inw(ICEREG1724(ice, GPIO_DATA));
249 return data;
250}
251
252/*
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200253 * MIDI
Takashi Iwai3a841d52008-04-23 17:47:28 +0200254 */
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200255
256static void vt1724_midi_clear_rx(struct snd_ice1712 *ice)
Takashi Iwai3a841d52008-04-23 17:47:28 +0200257{
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200258 unsigned int count;
259
260 for (count = inb(ICEREG1724(ice, MPU_RXFIFO)); count > 0; --count)
261 inb(ICEREG1724(ice, MPU_DATA));
Takashi Iwai3a841d52008-04-23 17:47:28 +0200262}
263
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200264static inline struct snd_rawmidi_substream *
265get_rawmidi_substream(struct snd_ice1712 *ice, unsigned int stream)
Takashi Iwai3a841d52008-04-23 17:47:28 +0200266{
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200267 return list_first_entry(&ice->rmidi[0]->streams[stream].substreams,
268 struct snd_rawmidi_substream, list);
Takashi Iwai3a841d52008-04-23 17:47:28 +0200269}
270
Takashi Iwaie683ec42008-11-12 16:42:44 +0100271static void enable_midi_irq(struct snd_ice1712 *ice, u8 flag, int enable);
272
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200273static void vt1724_midi_write(struct snd_ice1712 *ice)
274{
275 struct snd_rawmidi_substream *s;
276 int count, i;
277 u8 buffer[32];
278
279 s = get_rawmidi_substream(ice, SNDRV_RAWMIDI_STREAM_OUTPUT);
280 count = 31 - inb(ICEREG1724(ice, MPU_TXFIFO));
281 if (count > 0) {
282 count = snd_rawmidi_transmit(s, buffer, count);
283 for (i = 0; i < count; ++i)
284 outb(buffer[i], ICEREG1724(ice, MPU_DATA));
285 }
Takashi Iwaie683ec42008-11-12 16:42:44 +0100286 /* mask irq when all bytes have been transmitted.
287 * enabled again in output_trigger when the new data comes in.
288 */
289 enable_midi_irq(ice, VT1724_IRQ_MPU_TX,
290 !snd_rawmidi_transmit_empty(s));
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200291}
292
293static void vt1724_midi_read(struct snd_ice1712 *ice)
294{
295 struct snd_rawmidi_substream *s;
296 int count, i;
297 u8 buffer[32];
298
299 s = get_rawmidi_substream(ice, SNDRV_RAWMIDI_STREAM_INPUT);
300 count = inb(ICEREG1724(ice, MPU_RXFIFO));
301 if (count > 0) {
302 count = min(count, 32);
303 for (i = 0; i < count; ++i)
304 buffer[i] = inb(ICEREG1724(ice, MPU_DATA));
305 snd_rawmidi_receive(s, buffer, count);
306 }
307}
308
Takashi Iwaie683ec42008-11-12 16:42:44 +0100309/* call with ice->reg_lock */
310static void enable_midi_irq(struct snd_ice1712 *ice, u8 flag, int enable)
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200311{
Takashi Iwaie683ec42008-11-12 16:42:44 +0100312 u8 mask = inb(ICEREG1724(ice, IRQMASK));
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200313 if (enable)
314 mask &= ~flag;
315 else
316 mask |= flag;
317 outb(mask, ICEREG1724(ice, IRQMASK));
Takashi Iwaie683ec42008-11-12 16:42:44 +0100318}
319
320static void vt1724_enable_midi_irq(struct snd_rawmidi_substream *substream,
321 u8 flag, int enable)
322{
323 struct snd_ice1712 *ice = substream->rmidi->private_data;
324
325 spin_lock_irq(&ice->reg_lock);
326 enable_midi_irq(ice, flag, enable);
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200327 spin_unlock_irq(&ice->reg_lock);
328}
329
330static int vt1724_midi_output_open(struct snd_rawmidi_substream *s)
331{
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200332 return 0;
333}
334
335static int vt1724_midi_output_close(struct snd_rawmidi_substream *s)
336{
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200337 return 0;
338}
339
340static void vt1724_midi_output_trigger(struct snd_rawmidi_substream *s, int up)
341{
342 struct snd_ice1712 *ice = s->rmidi->private_data;
343 unsigned long flags;
344
345 spin_lock_irqsave(&ice->reg_lock, flags);
346 if (up) {
347 ice->midi_output = 1;
348 vt1724_midi_write(ice);
349 } else {
350 ice->midi_output = 0;
Takashi Iwaie683ec42008-11-12 16:42:44 +0100351 enable_midi_irq(ice, VT1724_IRQ_MPU_TX, 0);
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200352 }
353 spin_unlock_irqrestore(&ice->reg_lock, flags);
354}
355
356static void vt1724_midi_output_drain(struct snd_rawmidi_substream *s)
357{
358 struct snd_ice1712 *ice = s->rmidi->private_data;
359 unsigned long timeout;
360
Takashi Iwaie683ec42008-11-12 16:42:44 +0100361 vt1724_enable_midi_irq(s, VT1724_IRQ_MPU_TX, 0);
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200362 /* 32 bytes should be transmitted in less than about 12 ms */
363 timeout = jiffies + msecs_to_jiffies(15);
364 do {
365 if (inb(ICEREG1724(ice, MPU_CTRL)) & VT1724_MPU_TX_EMPTY)
366 break;
367 schedule_timeout_uninterruptible(1);
368 } while (time_after(timeout, jiffies));
369}
370
371static struct snd_rawmidi_ops vt1724_midi_output_ops = {
372 .open = vt1724_midi_output_open,
373 .close = vt1724_midi_output_close,
374 .trigger = vt1724_midi_output_trigger,
375 .drain = vt1724_midi_output_drain,
376};
377
378static int vt1724_midi_input_open(struct snd_rawmidi_substream *s)
379{
380 vt1724_midi_clear_rx(s->rmidi->private_data);
381 vt1724_enable_midi_irq(s, VT1724_IRQ_MPU_RX, 1);
382 return 0;
383}
384
385static int vt1724_midi_input_close(struct snd_rawmidi_substream *s)
386{
387 vt1724_enable_midi_irq(s, VT1724_IRQ_MPU_RX, 0);
388 return 0;
389}
390
391static void vt1724_midi_input_trigger(struct snd_rawmidi_substream *s, int up)
392{
393 struct snd_ice1712 *ice = s->rmidi->private_data;
394 unsigned long flags;
395
396 spin_lock_irqsave(&ice->reg_lock, flags);
397 if (up) {
398 ice->midi_input = 1;
399 vt1724_midi_read(ice);
400 } else {
401 ice->midi_input = 0;
402 }
403 spin_unlock_irqrestore(&ice->reg_lock, flags);
404}
405
406static struct snd_rawmidi_ops vt1724_midi_input_ops = {
407 .open = vt1724_midi_input_open,
408 .close = vt1724_midi_input_close,
409 .trigger = vt1724_midi_input_trigger,
410};
411
Takashi Iwai3a841d52008-04-23 17:47:28 +0200412
413/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 * Interrupt handler
415 */
416
David Howells7d12e782006-10-05 14:55:46 +0100417static irqreturn_t snd_vt1724_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100419 struct snd_ice1712 *ice = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 unsigned char status;
Takashi Iwai3a841d52008-04-23 17:47:28 +0200421 unsigned char status_mask =
422 VT1724_IRQ_MPU_RX | VT1724_IRQ_MPU_TX | VT1724_IRQ_MTPCM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 int handled = 0;
Takashi Iwai3a841d52008-04-23 17:47:28 +0200424 int timeout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 while (1) {
427 status = inb(ICEREG1724(ice, IRQSTAT));
Takashi Iwai3a841d52008-04-23 17:47:28 +0200428 status &= status_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 if (status == 0)
430 break;
Takashi Iwaie683ec42008-11-12 16:42:44 +0100431 spin_lock(&ice->reg_lock);
Takashi Iwai3a841d52008-04-23 17:47:28 +0200432 if (++timeout > 10) {
Takashi Iwai10832062008-08-11 10:18:39 +0200433 status = inb(ICEREG1724(ice, IRQSTAT));
434 printk(KERN_ERR "ice1724: Too long irq loop, "
435 "status = 0x%x\n", status);
436 if (status & VT1724_IRQ_MPU_TX) {
437 printk(KERN_ERR "ice1724: Disabling MPU_TX\n");
Takashi Iwaie683ec42008-11-12 16:42:44 +0100438 enable_midi_irq(ice, VT1724_IRQ_MPU_TX, 0);
Takashi Iwai10832062008-08-11 10:18:39 +0200439 }
Takashi Iwaie683ec42008-11-12 16:42:44 +0100440 spin_unlock(&ice->reg_lock);
Takashi Iwai3a841d52008-04-23 17:47:28 +0200441 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 }
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +0400443 handled = 1;
Takashi Iwai3a841d52008-04-23 17:47:28 +0200444 if (status & VT1724_IRQ_MPU_TX) {
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200445 if (ice->midi_output)
446 vt1724_midi_write(ice);
Takashi Iwaie683ec42008-11-12 16:42:44 +0100447 else
448 enable_midi_irq(ice, VT1724_IRQ_MPU_TX, 0);
Takashi Iwai3a841d52008-04-23 17:47:28 +0200449 /* Due to mysterical reasons, MPU_TX is always
450 * generated (and can't be cleared) when a PCM
451 * playback is going. So let's ignore at the
452 * next loop.
453 */
454 status_mask &= ~VT1724_IRQ_MPU_TX;
455 }
456 if (status & VT1724_IRQ_MPU_RX) {
Clemens Ladischaea3bfb2008-05-20 14:22:44 +0200457 if (ice->midi_input)
458 vt1724_midi_read(ice);
459 else
460 vt1724_midi_clear_rx(ice);
Takashi Iwai3a841d52008-04-23 17:47:28 +0200461 }
462 /* ack MPU irq */
463 outb(status, ICEREG1724(ice, IRQSTAT));
Takashi Iwaie683ec42008-11-12 16:42:44 +0100464 spin_unlock(&ice->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 if (status & VT1724_IRQ_MTPCM) {
466 /*
467 * Multi-track PCM
468 * PCM assignment are:
469 * Playback DMA0 (M/C) = playback_pro_substream
470 * Playback DMA1 = playback_con_substream_ds[0]
471 * Playback DMA2 = playback_con_substream_ds[1]
472 * Playback DMA3 = playback_con_substream_ds[2]
473 * Playback DMA4 (SPDIF) = playback_con_substream
474 * Record DMA0 = capture_pro_substream
475 * Record DMA1 = capture_con_substream
476 */
477 unsigned char mtstat = inb(ICEMT1724(ice, IRQ));
478 if (mtstat & VT1724_MULTI_PDMA0) {
479 if (ice->playback_pro_substream)
480 snd_pcm_period_elapsed(ice->playback_pro_substream);
481 }
482 if (mtstat & VT1724_MULTI_RDMA0) {
483 if (ice->capture_pro_substream)
484 snd_pcm_period_elapsed(ice->capture_pro_substream);
485 }
486 if (mtstat & VT1724_MULTI_PDMA1) {
487 if (ice->playback_con_substream_ds[0])
488 snd_pcm_period_elapsed(ice->playback_con_substream_ds[0]);
489 }
490 if (mtstat & VT1724_MULTI_PDMA2) {
491 if (ice->playback_con_substream_ds[1])
492 snd_pcm_period_elapsed(ice->playback_con_substream_ds[1]);
493 }
494 if (mtstat & VT1724_MULTI_PDMA3) {
495 if (ice->playback_con_substream_ds[2])
496 snd_pcm_period_elapsed(ice->playback_con_substream_ds[2]);
497 }
498 if (mtstat & VT1724_MULTI_PDMA4) {
499 if (ice->playback_con_substream)
500 snd_pcm_period_elapsed(ice->playback_con_substream);
501 }
502 if (mtstat & VT1724_MULTI_RDMA1) {
503 if (ice->capture_con_substream)
504 snd_pcm_period_elapsed(ice->capture_con_substream);
505 }
506 /* ack anyway to avoid freeze */
507 outb(mtstat, ICEMT1724(ice, IRQ));
508 /* ought to really handle this properly */
509 if (mtstat & VT1724_MULTI_FIFO_ERR) {
510 unsigned char fstat = inb(ICEMT1724(ice, DMA_FIFO_ERR));
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +0400511 outb(fstat, ICEMT1724(ice, DMA_FIFO_ERR));
512 outb(VT1724_MULTI_FIFO_ERR | inb(ICEMT1724(ice, DMA_INT_MASK)), ICEMT1724(ice, DMA_INT_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 /* If I don't do this, I get machine lockup due to continual interrupts */
514 }
515
516 }
517 }
518 return IRQ_RETVAL(handled);
519}
520
521/*
522 * PCM code - professional part (multitrack)
523 */
524
525static unsigned int rates[] = {
526 8000, 9600, 11025, 12000, 16000, 22050, 24000,
527 32000, 44100, 48000, 64000, 88200, 96000,
528 176400, 192000,
529};
530
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100531static struct snd_pcm_hw_constraint_list hw_constraints_rates_96 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 .count = ARRAY_SIZE(rates) - 2, /* up to 96000 */
533 .list = rates,
534 .mask = 0,
535};
536
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100537static struct snd_pcm_hw_constraint_list hw_constraints_rates_48 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 .count = ARRAY_SIZE(rates) - 5, /* up to 48000 */
539 .list = rates,
540 .mask = 0,
541};
542
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100543static struct snd_pcm_hw_constraint_list hw_constraints_rates_192 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 .count = ARRAY_SIZE(rates),
545 .list = rates,
546 .mask = 0,
547};
548
549struct vt1724_pcm_reg {
550 unsigned int addr; /* ADDR register offset */
551 unsigned int size; /* SIZE register offset */
552 unsigned int count; /* COUNT register offset */
553 unsigned int start; /* start & pause bit */
554};
555
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100556static int snd_vt1724_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100558 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 unsigned char what;
560 unsigned char old;
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100561 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563 what = 0;
Takashi Iwaief991b92007-02-22 12:52:53 +0100564 snd_pcm_group_for_each_entry(s, substream) {
Clemens Ladisch29998d22007-07-30 08:14:31 +0200565 if (snd_pcm_substream_chip(s) == ice) {
566 const struct vt1724_pcm_reg *reg;
567 reg = s->runtime->private_data;
568 what |= reg->start;
569 snd_pcm_trigger_done(s, substream);
570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 }
572
573 switch (cmd) {
574 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
575 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
576 spin_lock(&ice->reg_lock);
577 old = inb(ICEMT1724(ice, DMA_PAUSE));
578 if (cmd == SNDRV_PCM_TRIGGER_PAUSE_PUSH)
579 old |= what;
580 else
581 old &= ~what;
582 outb(old, ICEMT1724(ice, DMA_PAUSE));
583 spin_unlock(&ice->reg_lock);
584 break;
585
586 case SNDRV_PCM_TRIGGER_START:
587 case SNDRV_PCM_TRIGGER_STOP:
Igor Chernyshevb40e9532009-06-25 09:31:07 +0200588 case SNDRV_PCM_TRIGGER_SUSPEND:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 spin_lock(&ice->reg_lock);
590 old = inb(ICEMT1724(ice, DMA_CONTROL));
591 if (cmd == SNDRV_PCM_TRIGGER_START)
592 old |= what;
593 else
594 old &= ~what;
595 outb(old, ICEMT1724(ice, DMA_CONTROL));
596 spin_unlock(&ice->reg_lock);
597 break;
598
Igor Chernyshevb40e9532009-06-25 09:31:07 +0200599 case SNDRV_PCM_TRIGGER_RESUME:
600 /* apps will have to restart stream */
601 break;
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 default:
604 return -EINVAL;
605 }
606 return 0;
607}
608
609/*
610 */
611
612#define DMA_STARTS (VT1724_RDMA0_START|VT1724_PDMA0_START|VT1724_RDMA1_START|\
613 VT1724_PDMA1_START|VT1724_PDMA2_START|VT1724_PDMA3_START|VT1724_PDMA4_START)
614#define DMA_PAUSES (VT1724_RDMA0_PAUSE|VT1724_PDMA0_PAUSE|VT1724_RDMA1_PAUSE|\
615 VT1724_PDMA1_PAUSE|VT1724_PDMA2_PAUSE|VT1724_PDMA3_PAUSE|VT1724_PDMA4_PAUSE)
616
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100617static const unsigned int stdclock_rate_list[16] = {
618 48000, 24000, 12000, 9600, 32000, 16000, 8000, 96000, 44100,
619 22050, 11025, 88200, 176400, 0, 192000, 64000
620};
621
622static unsigned int stdclock_get_rate(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100624 unsigned int rate;
625 rate = stdclock_rate_list[inb(ICEMT1724(ice, RATE)) & 15];
626 return rate;
627}
628
629static void stdclock_set_rate(struct snd_ice1712 *ice, unsigned int rate)
630{
631 int i;
632 for (i = 0; i < ARRAY_SIZE(stdclock_rate_list); i++) {
633 if (stdclock_rate_list[i] == rate) {
634 outb(i, ICEMT1724(ice, RATE));
635 return;
636 }
637 }
638}
639
640static unsigned char stdclock_set_mclk(struct snd_ice1712 *ice,
641 unsigned int rate)
642{
643 unsigned char val, old;
644 /* check MT02 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 if (ice->eeprom.data[ICE_EEP2_ACLINK] & VT1724_CFG_PRO_I2S) {
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100646 val = old = inb(ICEMT1724(ice, I2S_FORMAT));
647 if (rate > 96000)
648 val |= VT1724_MT_I2S_MCLK_128X; /* 128x MCLK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 else
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100650 val &= ~VT1724_MT_I2S_MCLK_128X; /* 256x MCLK */
651 if (val != old) {
652 outb(val, ICEMT1724(ice, I2S_FORMAT));
653 /* master clock changed */
654 return 1;
655 }
656 }
657 /* no change in master clock */
658 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659}
660
Takashi Iwai92d71002009-05-06 17:18:34 +0200661static int snd_vt1724_set_pro_rate(struct snd_ice1712 *ice, unsigned int rate,
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100662 int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
664 unsigned long flags;
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100665 unsigned char mclk_change;
666 unsigned int i, old_rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100668 if (rate > ice->hw_rates->list[ice->hw_rates->count - 1])
Takashi Iwai92d71002009-05-06 17:18:34 +0200669 return -EINVAL;
670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 spin_lock_irqsave(&ice->reg_lock, flags);
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100672 if ((inb(ICEMT1724(ice, DMA_CONTROL)) & DMA_STARTS) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 (inb(ICEMT1724(ice, DMA_PAUSE)) & DMA_PAUSES)) {
674 /* running? we cannot change the rate now... */
675 spin_unlock_irqrestore(&ice->reg_lock, flags);
David Henningssonbd3c2002009-10-11 11:37:22 +0200676 return ((rate == ice->cur_rate) && !force) ? 0 : -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 }
678 if (!force && is_pro_rate_locked(ice)) {
Pavel Hofman6796d5a2009-09-16 22:25:39 +0200679 /* comparing required and current rate - makes sense for
680 * internal clock only */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 spin_unlock_irqrestore(&ice->reg_lock, flags);
Takashi Iwai92d71002009-05-06 17:18:34 +0200682 return (rate == ice->cur_rate) ? 0 : -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
684
Pavel Hofman6796d5a2009-09-16 22:25:39 +0200685 if (force || !ice->is_spdif_master(ice)) {
686 /* force means the rate was switched by ucontrol, otherwise
687 * setting clock rate for internal clock mode */
688 old_rate = ice->get_rate(ice);
689 if (force || (old_rate != rate))
690 ice->set_rate(ice, rate);
691 else if (rate == ice->cur_rate) {
692 spin_unlock_irqrestore(&ice->reg_lock, flags);
693 return 0;
694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 }
696
697 ice->cur_rate = rate;
698
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100699 /* setting master clock */
700 mclk_change = ice->set_mclk(ice, rate);
701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 spin_unlock_irqrestore(&ice->reg_lock, flags);
703
704 if (mclk_change && ice->gpio.i2s_mclk_changed)
705 ice->gpio.i2s_mclk_changed(ice);
706 if (ice->gpio.set_pro_rate)
707 ice->gpio.set_pro_rate(ice, rate);
708
709 /* set up codecs */
710 for (i = 0; i < ice->akm_codecs; i++) {
711 if (ice->akm[i].ops.set_rate_val)
712 ice->akm[i].ops.set_rate_val(&ice->akm[i], rate);
713 }
714 if (ice->spdif.ops.setup_rate)
715 ice->spdif.ops.setup_rate(ice, rate);
Takashi Iwai92d71002009-05-06 17:18:34 +0200716
717 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718}
719
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100720static int snd_vt1724_pcm_hw_params(struct snd_pcm_substream *substream,
721 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100723 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Takashi Iwai92d71002009-05-06 17:18:34 +0200724 int i, chs, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
726 chs = params_channels(hw_params);
Ingo Molnar62932df2006-01-16 16:34:20 +0100727 mutex_lock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 /* mark surround channels */
729 if (substream == ice->playback_pro_substream) {
730 /* PDMA0 can be multi-channel up to 8 */
731 chs = chs / 2 - 1;
732 for (i = 0; i < chs; i++) {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100733 if (ice->pcm_reserved[i] &&
734 ice->pcm_reserved[i] != substream) {
Ingo Molnar62932df2006-01-16 16:34:20 +0100735 mutex_unlock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 return -EBUSY;
737 }
738 ice->pcm_reserved[i] = substream;
739 }
740 for (; i < 3; i++) {
741 if (ice->pcm_reserved[i] == substream)
742 ice->pcm_reserved[i] = NULL;
743 }
744 } else {
745 for (i = 0; i < 3; i++) {
746 /* check individual playback stream */
747 if (ice->playback_con_substream_ds[i] == substream) {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100748 if (ice->pcm_reserved[i] &&
749 ice->pcm_reserved[i] != substream) {
Ingo Molnar62932df2006-01-16 16:34:20 +0100750 mutex_unlock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 return -EBUSY;
752 }
753 ice->pcm_reserved[i] = substream;
754 break;
755 }
756 }
757 }
Ingo Molnar62932df2006-01-16 16:34:20 +0100758 mutex_unlock(&ice->open_mutex);
Takashi Iwai92d71002009-05-06 17:18:34 +0200759
760 err = snd_vt1724_set_pro_rate(ice, params_rate(hw_params), 0);
761 if (err < 0)
762 return err;
763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
765}
766
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100767static int snd_vt1724_pcm_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100769 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 int i;
771
Ingo Molnar62932df2006-01-16 16:34:20 +0100772 mutex_lock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 /* unmark surround channels */
774 for (i = 0; i < 3; i++)
775 if (ice->pcm_reserved[i] == substream)
776 ice->pcm_reserved[i] = NULL;
Ingo Molnar62932df2006-01-16 16:34:20 +0100777 mutex_unlock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 return snd_pcm_lib_free_pages(substream);
779}
780
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100781static int snd_vt1724_playback_pro_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100783 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 unsigned char val;
785 unsigned int size;
786
787 spin_lock_irq(&ice->reg_lock);
788 val = (8 - substream->runtime->channels) >> 1;
789 outb(val, ICEMT1724(ice, BURST));
790
791 outl(substream->runtime->dma_addr, ICEMT1724(ice, PLAYBACK_ADDR));
792
793 size = (snd_pcm_lib_buffer_bytes(substream) >> 2) - 1;
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +0400794 /* outl(size, ICEMT1724(ice, PLAYBACK_SIZE)); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 outw(size, ICEMT1724(ice, PLAYBACK_SIZE));
796 outb(size >> 16, ICEMT1724(ice, PLAYBACK_SIZE) + 2);
797 size = (snd_pcm_lib_period_bytes(substream) >> 2) - 1;
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +0400798 /* outl(size, ICEMT1724(ice, PLAYBACK_COUNT)); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 outw(size, ICEMT1724(ice, PLAYBACK_COUNT));
800 outb(size >> 16, ICEMT1724(ice, PLAYBACK_COUNT) + 2);
801
802 spin_unlock_irq(&ice->reg_lock);
803
Takashi Iwaie2ea7cfc2009-02-05 16:07:02 +0100804 /*
805 printk(KERN_DEBUG "pro prepare: ch = %d, addr = 0x%x, "
806 "buffer = 0x%x, period = 0x%x\n",
807 substream->runtime->channels,
808 (unsigned int)substream->runtime->dma_addr,
809 snd_pcm_lib_buffer_bytes(substream),
810 snd_pcm_lib_period_bytes(substream));
811 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 return 0;
813}
814
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100815static snd_pcm_uframes_t snd_vt1724_playback_pro_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100817 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 size_t ptr;
819
820 if (!(inl(ICEMT1724(ice, DMA_CONTROL)) & VT1724_PDMA0_START))
821 return 0;
822#if 0 /* read PLAYBACK_ADDR */
823 ptr = inl(ICEMT1724(ice, PLAYBACK_ADDR));
824 if (ptr < substream->runtime->dma_addr) {
825 snd_printd("ice1724: invalid negative ptr\n");
826 return 0;
827 }
828 ptr -= substream->runtime->dma_addr;
829 ptr = bytes_to_frames(substream->runtime, ptr);
830 if (ptr >= substream->runtime->buffer_size) {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100831 snd_printd("ice1724: invalid ptr %d (size=%d)\n",
832 (int)ptr, (int)substream->runtime->period_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 return 0;
834 }
835#else /* read PLAYBACK_SIZE */
836 ptr = inl(ICEMT1724(ice, PLAYBACK_SIZE)) & 0xffffff;
837 ptr = (ptr + 1) << 2;
838 ptr = bytes_to_frames(substream->runtime, ptr);
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +0400839 if (!ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 ;
841 else if (ptr <= substream->runtime->buffer_size)
842 ptr = substream->runtime->buffer_size - ptr;
843 else {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100844 snd_printd("ice1724: invalid ptr %d (size=%d)\n",
845 (int)ptr, (int)substream->runtime->buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 ptr = 0;
847 }
848#endif
849 return ptr;
850}
851
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100852static int snd_vt1724_pcm_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100854 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Takashi Iwai32b47da2007-01-29 15:26:36 +0100855 const struct vt1724_pcm_reg *reg = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
857 spin_lock_irq(&ice->reg_lock);
858 outl(substream->runtime->dma_addr, ice->profi_port + reg->addr);
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100859 outw((snd_pcm_lib_buffer_bytes(substream) >> 2) - 1,
860 ice->profi_port + reg->size);
861 outw((snd_pcm_lib_period_bytes(substream) >> 2) - 1,
862 ice->profi_port + reg->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 spin_unlock_irq(&ice->reg_lock);
864 return 0;
865}
866
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100867static snd_pcm_uframes_t snd_vt1724_pcm_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100869 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Takashi Iwai32b47da2007-01-29 15:26:36 +0100870 const struct vt1724_pcm_reg *reg = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 size_t ptr;
872
873 if (!(inl(ICEMT1724(ice, DMA_CONTROL)) & reg->start))
874 return 0;
875#if 0 /* use ADDR register */
876 ptr = inl(ice->profi_port + reg->addr);
877 ptr -= substream->runtime->dma_addr;
878 return bytes_to_frames(substream->runtime, ptr);
879#else /* use SIZE register */
880 ptr = inw(ice->profi_port + reg->size);
881 ptr = (ptr + 1) << 2;
882 ptr = bytes_to_frames(substream->runtime, ptr);
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +0400883 if (!ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 ;
885 else if (ptr <= substream->runtime->buffer_size)
886 ptr = substream->runtime->buffer_size - ptr;
887 else {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +0100888 snd_printd("ice1724: invalid ptr %d (size=%d)\n",
889 (int)ptr, (int)substream->runtime->buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 ptr = 0;
891 }
892 return ptr;
893#endif
894}
895
Takashi Iwaia5b7b5c2009-05-06 17:22:07 +0200896static const struct vt1724_pcm_reg vt1724_pdma0_reg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 .addr = VT1724_MT_PLAYBACK_ADDR,
898 .size = VT1724_MT_PLAYBACK_SIZE,
899 .count = VT1724_MT_PLAYBACK_COUNT,
900 .start = VT1724_PDMA0_START,
901};
902
Takashi Iwaia5b7b5c2009-05-06 17:22:07 +0200903static const struct vt1724_pcm_reg vt1724_pdma4_reg = {
904 .addr = VT1724_MT_PDMA4_ADDR,
905 .size = VT1724_MT_PDMA4_SIZE,
906 .count = VT1724_MT_PDMA4_COUNT,
907 .start = VT1724_PDMA4_START,
908};
909
910static const struct vt1724_pcm_reg vt1724_rdma0_reg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 .addr = VT1724_MT_CAPTURE_ADDR,
912 .size = VT1724_MT_CAPTURE_SIZE,
913 .count = VT1724_MT_CAPTURE_COUNT,
914 .start = VT1724_RDMA0_START,
915};
916
Takashi Iwaia5b7b5c2009-05-06 17:22:07 +0200917static const struct vt1724_pcm_reg vt1724_rdma1_reg = {
918 .addr = VT1724_MT_RDMA1_ADDR,
919 .size = VT1724_MT_RDMA1_SIZE,
920 .count = VT1724_MT_RDMA1_COUNT,
921 .start = VT1724_RDMA1_START,
922};
923
924#define vt1724_playback_pro_reg vt1724_pdma0_reg
925#define vt1724_playback_spdif_reg vt1724_pdma4_reg
926#define vt1724_capture_pro_reg vt1724_rdma0_reg
927#define vt1724_capture_spdif_reg vt1724_rdma1_reg
928
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +0400929static const struct snd_pcm_hardware snd_vt1724_playback_pro = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
931 SNDRV_PCM_INFO_BLOCK_TRANSFER |
932 SNDRV_PCM_INFO_MMAP_VALID |
933 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_SYNC_START),
934 .formats = SNDRV_PCM_FMTBIT_S32_LE,
935 .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_192000,
936 .rate_min = 8000,
937 .rate_max = 192000,
938 .channels_min = 2,
939 .channels_max = 8,
940 .buffer_bytes_max = (1UL << 21), /* 19bits dword */
941 .period_bytes_min = 8 * 4 * 2, /* FIXME: constraints needed */
942 .period_bytes_max = (1UL << 21),
943 .periods_min = 2,
944 .periods_max = 1024,
945};
946
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +0400947static const struct snd_pcm_hardware snd_vt1724_spdif = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
949 SNDRV_PCM_INFO_BLOCK_TRANSFER |
950 SNDRV_PCM_INFO_MMAP_VALID |
951 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_SYNC_START),
952 .formats = SNDRV_PCM_FMTBIT_S32_LE,
Takashi Iwai2dfbeca2005-10-12 10:04:42 +0200953 .rates = (SNDRV_PCM_RATE_32000|SNDRV_PCM_RATE_44100|
954 SNDRV_PCM_RATE_48000|SNDRV_PCM_RATE_88200|
955 SNDRV_PCM_RATE_96000|SNDRV_PCM_RATE_176400|
956 SNDRV_PCM_RATE_192000),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 .rate_min = 32000,
Takashi Iwai2dfbeca2005-10-12 10:04:42 +0200958 .rate_max = 192000,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 .channels_min = 2,
960 .channels_max = 2,
961 .buffer_bytes_max = (1UL << 18), /* 16bits dword */
962 .period_bytes_min = 2 * 4 * 2,
963 .period_bytes_max = (1UL << 18),
964 .periods_min = 2,
965 .periods_max = 1024,
966};
967
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +0400968static const struct snd_pcm_hardware snd_vt1724_2ch_stereo = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
970 SNDRV_PCM_INFO_BLOCK_TRANSFER |
971 SNDRV_PCM_INFO_MMAP_VALID |
972 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_SYNC_START),
973 .formats = SNDRV_PCM_FMTBIT_S32_LE,
974 .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_192000,
975 .rate_min = 8000,
976 .rate_max = 192000,
977 .channels_min = 2,
978 .channels_max = 2,
979 .buffer_bytes_max = (1UL << 18), /* 16bits dword */
980 .period_bytes_min = 2 * 4 * 2,
981 .period_bytes_max = (1UL << 18),
982 .periods_min = 2,
983 .periods_max = 1024,
984};
985
986/*
987 * set rate constraints
988 */
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100989static void set_std_hw_rates(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 if (ice->eeprom.data[ICE_EEP2_ACLINK] & VT1724_CFG_PRO_I2S) {
992 /* I2S */
993 /* VT1720 doesn't support more than 96kHz */
994 if ((ice->eeprom.data[ICE_EEP2_I2S] & 0x08) && !ice->vt1720)
Pavel Hofmand16be8e2008-03-20 12:10:27 +0100995 ice->hw_rates = &hw_constraints_rates_192;
996 else
997 ice->hw_rates = &hw_constraints_rates_96;
998 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 /* ACLINK */
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001000 ice->hw_rates = &hw_constraints_rates_48;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 }
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001002}
1003
1004static int set_rate_constraints(struct snd_ice1712 *ice,
1005 struct snd_pcm_substream *substream)
1006{
1007 struct snd_pcm_runtime *runtime = substream->runtime;
1008
1009 runtime->hw.rate_min = ice->hw_rates->list[0];
1010 runtime->hw.rate_max = ice->hw_rates->list[ice->hw_rates->count - 1];
1011 runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
1012 return snd_pcm_hw_constraint_list(runtime, 0,
1013 SNDRV_PCM_HW_PARAM_RATE,
1014 ice->hw_rates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015}
1016
Pavel Hofmandb058282012-03-02 22:09:39 +01001017/* if the card has the internal rate locked (is_pro_locked), limit runtime
1018 hw rates to the current internal rate only.
1019*/
1020static void constrain_rate_if_locked(struct snd_pcm_substream *substream)
1021{
1022 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
1023 struct snd_pcm_runtime *runtime = substream->runtime;
1024 unsigned int rate;
1025 if (is_pro_rate_locked(ice)) {
1026 rate = ice->get_rate(ice);
1027 if (rate >= runtime->hw.rate_min
1028 && rate <= runtime->hw.rate_max) {
1029 runtime->hw.rate_min = rate;
1030 runtime->hw.rate_max = rate;
1031 }
1032 }
1033}
1034
1035
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036/* multi-channel playback needs alignment 8x32bit regardless of the channels
1037 * actually used
1038 */
1039#define VT1724_BUFFER_ALIGN 0x20
1040
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001041static int snd_vt1724_playback_pro_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001043 struct snd_pcm_runtime *runtime = substream->runtime;
1044 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Takashi Iwaia6b936b2008-08-29 16:17:25 +02001045 int chs, num_indeps;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Takashi Iwai32b47da2007-01-29 15:26:36 +01001047 runtime->private_data = (void *)&vt1724_playback_pro_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 ice->playback_pro_substream = substream;
1049 runtime->hw = snd_vt1724_playback_pro;
1050 snd_pcm_set_sync(substream);
1051 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
1052 set_rate_constraints(ice, substream);
Ingo Molnar62932df2006-01-16 16:34:20 +01001053 mutex_lock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 /* calculate the currently available channels */
Takashi Iwaia6b936b2008-08-29 16:17:25 +02001055 num_indeps = ice->num_total_dacs / 2 - 1;
1056 for (chs = 0; chs < num_indeps; chs++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 if (ice->pcm_reserved[chs])
1058 break;
1059 }
1060 chs = (chs + 1) * 2;
1061 runtime->hw.channels_max = chs;
1062 if (chs > 2) /* channels must be even */
1063 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, 2);
Ingo Molnar62932df2006-01-16 16:34:20 +01001064 mutex_unlock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1066 VT1724_BUFFER_ALIGN);
1067 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1068 VT1724_BUFFER_ALIGN);
Pavel Hofmandb058282012-03-02 22:09:39 +01001069 constrain_rate_if_locked(substream);
Pavel Hofman1ff97cb2009-09-16 22:25:40 +02001070 if (ice->pro_open)
1071 ice->pro_open(ice, substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 return 0;
1073}
1074
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001075static int snd_vt1724_capture_pro_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001077 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
1078 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Takashi Iwai32b47da2007-01-29 15:26:36 +01001080 runtime->private_data = (void *)&vt1724_capture_pro_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 ice->capture_pro_substream = substream;
1082 runtime->hw = snd_vt1724_2ch_stereo;
1083 snd_pcm_set_sync(substream);
1084 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
1085 set_rate_constraints(ice, substream);
1086 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1087 VT1724_BUFFER_ALIGN);
1088 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1089 VT1724_BUFFER_ALIGN);
Pavel Hofmandb058282012-03-02 22:09:39 +01001090 constrain_rate_if_locked(substream);
Pavel Hofman1ff97cb2009-09-16 22:25:40 +02001091 if (ice->pro_open)
1092 ice->pro_open(ice, substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 return 0;
1094}
1095
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001096static int snd_vt1724_playback_pro_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001098 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
1100 if (PRO_RATE_RESET)
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001101 snd_vt1724_set_pro_rate(ice, ice->pro_rate_default, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 ice->playback_pro_substream = NULL;
1103
1104 return 0;
1105}
1106
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001107static int snd_vt1724_capture_pro_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001109 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
1111 if (PRO_RATE_RESET)
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001112 snd_vt1724_set_pro_rate(ice, ice->pro_rate_default, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 ice->capture_pro_substream = NULL;
1114 return 0;
1115}
1116
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001117static struct snd_pcm_ops snd_vt1724_playback_pro_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 .open = snd_vt1724_playback_pro_open,
1119 .close = snd_vt1724_playback_pro_close,
1120 .ioctl = snd_pcm_lib_ioctl,
1121 .hw_params = snd_vt1724_pcm_hw_params,
1122 .hw_free = snd_vt1724_pcm_hw_free,
1123 .prepare = snd_vt1724_playback_pro_prepare,
1124 .trigger = snd_vt1724_pcm_trigger,
1125 .pointer = snd_vt1724_playback_pro_pointer,
1126};
1127
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001128static struct snd_pcm_ops snd_vt1724_capture_pro_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 .open = snd_vt1724_capture_pro_open,
1130 .close = snd_vt1724_capture_pro_close,
1131 .ioctl = snd_pcm_lib_ioctl,
1132 .hw_params = snd_vt1724_pcm_hw_params,
1133 .hw_free = snd_vt1724_pcm_hw_free,
1134 .prepare = snd_vt1724_pcm_prepare,
1135 .trigger = snd_vt1724_pcm_trigger,
1136 .pointer = snd_vt1724_pcm_pointer,
1137};
1138
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001139static int __devinit snd_vt1724_pcm_profi(struct snd_ice1712 *ice, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001141 struct snd_pcm *pcm;
Pavel Hofmanffd364d2012-01-10 20:45:28 +01001142 int capt, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Pavel Hofmanffd364d2012-01-10 20:45:28 +01001144 if ((ice->eeprom.data[ICE_EEP2_SYSCONF] & VT1724_CFG_ADC_MASK) ==
1145 VT1724_CFG_ADC_NONE)
1146 capt = 0;
1147 else
1148 capt = 1;
1149 err = snd_pcm_new(ice->card, "ICE1724", device, 1, capt, &pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 if (err < 0)
1151 return err;
1152
1153 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_vt1724_playback_pro_ops);
Pavel Hofmanffd364d2012-01-10 20:45:28 +01001154 if (capt)
1155 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1156 &snd_vt1724_capture_pro_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
1158 pcm->private_data = ice;
1159 pcm->info_flags = 0;
1160 strcpy(pcm->name, "ICE1724");
1161
1162 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001163 snd_dma_pci_data(ice->pci),
1164 256*1024, 256*1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
1166 ice->pcm_pro = pcm;
1167
1168 return 0;
1169}
1170
1171
1172/*
1173 * SPDIF PCM
1174 */
1175
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176/* update spdif control bits; call with reg_lock */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001177static void update_spdif_bits(struct snd_ice1712 *ice, unsigned int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178{
1179 unsigned char cbit, disabled;
1180
1181 cbit = inb(ICEREG1724(ice, SPDIF_CFG));
1182 disabled = cbit & ~VT1724_CFG_SPDIF_OUT_EN;
1183 if (cbit != disabled)
1184 outb(disabled, ICEREG1724(ice, SPDIF_CFG));
1185 outw(val, ICEMT1724(ice, SPDIF_CTRL));
1186 if (cbit != disabled)
1187 outb(cbit, ICEREG1724(ice, SPDIF_CFG));
1188 outw(val, ICEMT1724(ice, SPDIF_CTRL));
1189}
1190
1191/* update SPDIF control bits according to the given rate */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001192static void update_spdif_rate(struct snd_ice1712 *ice, unsigned int rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193{
1194 unsigned int val, nval;
1195 unsigned long flags;
1196
1197 spin_lock_irqsave(&ice->reg_lock, flags);
1198 nval = val = inw(ICEMT1724(ice, SPDIF_CTRL));
1199 nval &= ~(7 << 12);
1200 switch (rate) {
1201 case 44100: break;
1202 case 48000: nval |= 2 << 12; break;
1203 case 32000: nval |= 3 << 12; break;
Takashi Iwai2dfbeca2005-10-12 10:04:42 +02001204 case 88200: nval |= 4 << 12; break;
1205 case 96000: nval |= 5 << 12; break;
1206 case 192000: nval |= 6 << 12; break;
1207 case 176400: nval |= 7 << 12; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 }
1209 if (val != nval)
1210 update_spdif_bits(ice, nval);
1211 spin_unlock_irqrestore(&ice->reg_lock, flags);
1212}
1213
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001214static int snd_vt1724_playback_spdif_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001216 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001217 if (!ice->force_pdma4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 update_spdif_rate(ice, substream->runtime->rate);
1219 return snd_vt1724_pcm_prepare(substream);
1220}
1221
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001222static int snd_vt1724_playback_spdif_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001224 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
1225 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Takashi Iwai32b47da2007-01-29 15:26:36 +01001227 runtime->private_data = (void *)&vt1724_playback_spdif_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 ice->playback_con_substream = substream;
1229 if (ice->force_pdma4) {
1230 runtime->hw = snd_vt1724_2ch_stereo;
1231 set_rate_constraints(ice, substream);
1232 } else
1233 runtime->hw = snd_vt1724_spdif;
1234 snd_pcm_set_sync(substream);
1235 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
1236 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1237 VT1724_BUFFER_ALIGN);
1238 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1239 VT1724_BUFFER_ALIGN);
Pavel Hofmandb058282012-03-02 22:09:39 +01001240 constrain_rate_if_locked(substream);
Takashi Iwaic93f5a12008-03-14 17:17:09 +01001241 if (ice->spdif.ops.open)
1242 ice->spdif.ops.open(ice, substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 return 0;
1244}
1245
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001246static int snd_vt1724_playback_spdif_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001248 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
1250 if (PRO_RATE_RESET)
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001251 snd_vt1724_set_pro_rate(ice, ice->pro_rate_default, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 ice->playback_con_substream = NULL;
Takashi Iwaic93f5a12008-03-14 17:17:09 +01001253 if (ice->spdif.ops.close)
1254 ice->spdif.ops.close(ice, substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
1256 return 0;
1257}
1258
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001259static int snd_vt1724_capture_spdif_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001261 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
1262 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Takashi Iwai32b47da2007-01-29 15:26:36 +01001264 runtime->private_data = (void *)&vt1724_capture_spdif_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 ice->capture_con_substream = substream;
1266 if (ice->force_rdma1) {
1267 runtime->hw = snd_vt1724_2ch_stereo;
1268 set_rate_constraints(ice, substream);
1269 } else
1270 runtime->hw = snd_vt1724_spdif;
1271 snd_pcm_set_sync(substream);
1272 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
1273 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1274 VT1724_BUFFER_ALIGN);
1275 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1276 VT1724_BUFFER_ALIGN);
Pavel Hofmandb058282012-03-02 22:09:39 +01001277 constrain_rate_if_locked(substream);
Takashi Iwaic93f5a12008-03-14 17:17:09 +01001278 if (ice->spdif.ops.open)
1279 ice->spdif.ops.open(ice, substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 return 0;
1281}
1282
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001283static int snd_vt1724_capture_spdif_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001285 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
1287 if (PRO_RATE_RESET)
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001288 snd_vt1724_set_pro_rate(ice, ice->pro_rate_default, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 ice->capture_con_substream = NULL;
Takashi Iwaic93f5a12008-03-14 17:17:09 +01001290 if (ice->spdif.ops.close)
1291 ice->spdif.ops.close(ice, substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
1293 return 0;
1294}
1295
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001296static struct snd_pcm_ops snd_vt1724_playback_spdif_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 .open = snd_vt1724_playback_spdif_open,
1298 .close = snd_vt1724_playback_spdif_close,
1299 .ioctl = snd_pcm_lib_ioctl,
1300 .hw_params = snd_vt1724_pcm_hw_params,
1301 .hw_free = snd_vt1724_pcm_hw_free,
1302 .prepare = snd_vt1724_playback_spdif_prepare,
1303 .trigger = snd_vt1724_pcm_trigger,
1304 .pointer = snd_vt1724_pcm_pointer,
1305};
1306
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001307static struct snd_pcm_ops snd_vt1724_capture_spdif_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 .open = snd_vt1724_capture_spdif_open,
1309 .close = snd_vt1724_capture_spdif_close,
1310 .ioctl = snd_pcm_lib_ioctl,
1311 .hw_params = snd_vt1724_pcm_hw_params,
1312 .hw_free = snd_vt1724_pcm_hw_free,
1313 .prepare = snd_vt1724_pcm_prepare,
1314 .trigger = snd_vt1724_pcm_trigger,
1315 .pointer = snd_vt1724_pcm_pointer,
1316};
1317
1318
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001319static int __devinit snd_vt1724_pcm_spdif(struct snd_ice1712 *ice, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320{
1321 char *name;
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001322 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 int play, capt;
1324 int err;
1325
1326 if (ice->force_pdma4 ||
1327 (ice->eeprom.data[ICE_EEP2_SPDIF] & VT1724_CFG_SPDIF_OUT_INT)) {
1328 play = 1;
1329 ice->has_spdif = 1;
1330 } else
1331 play = 0;
1332 if (ice->force_rdma1 ||
1333 (ice->eeprom.data[ICE_EEP2_SPDIF] & VT1724_CFG_SPDIF_IN)) {
1334 capt = 1;
1335 ice->has_spdif = 1;
1336 } else
1337 capt = 0;
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001338 if (!play && !capt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 return 0; /* no spdif device */
1340
1341 if (ice->force_pdma4 || ice->force_rdma1)
1342 name = "ICE1724 Secondary";
1343 else
Alan Horstmann8eca7532009-01-05 18:30:04 +01001344 name = "ICE1724 IEC958";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 err = snd_pcm_new(ice->card, name, device, play, capt, &pcm);
1346 if (err < 0)
1347 return err;
1348
1349 if (play)
1350 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1351 &snd_vt1724_playback_spdif_ops);
1352 if (capt)
1353 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1354 &snd_vt1724_capture_spdif_ops);
1355
1356 pcm->private_data = ice;
1357 pcm->info_flags = 0;
1358 strcpy(pcm->name, name);
1359
1360 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001361 snd_dma_pci_data(ice->pci),
Robert Hancock1d4efa62009-10-07 20:19:21 -06001362 256*1024, 256*1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
1364 ice->pcm = pcm;
1365
1366 return 0;
1367}
1368
1369
1370/*
1371 * independent surround PCMs
1372 */
1373
Takashi Iwai32b47da2007-01-29 15:26:36 +01001374static const struct vt1724_pcm_reg vt1724_playback_dma_regs[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 {
1376 .addr = VT1724_MT_PDMA1_ADDR,
1377 .size = VT1724_MT_PDMA1_SIZE,
1378 .count = VT1724_MT_PDMA1_COUNT,
1379 .start = VT1724_PDMA1_START,
1380 },
1381 {
1382 .addr = VT1724_MT_PDMA2_ADDR,
1383 .size = VT1724_MT_PDMA2_SIZE,
1384 .count = VT1724_MT_PDMA2_COUNT,
1385 .start = VT1724_PDMA2_START,
1386 },
1387 {
1388 .addr = VT1724_MT_PDMA3_ADDR,
1389 .size = VT1724_MT_PDMA3_SIZE,
1390 .count = VT1724_MT_PDMA3_COUNT,
1391 .start = VT1724_PDMA3_START,
1392 },
1393};
1394
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001395static int snd_vt1724_playback_indep_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001397 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 unsigned char val;
1399
1400 spin_lock_irq(&ice->reg_lock);
1401 val = 3 - substream->number;
1402 if (inb(ICEMT1724(ice, BURST)) < val)
1403 outb(val, ICEMT1724(ice, BURST));
1404 spin_unlock_irq(&ice->reg_lock);
1405 return snd_vt1724_pcm_prepare(substream);
1406}
1407
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001408static int snd_vt1724_playback_indep_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001410 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
1411 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Ingo Molnar62932df2006-01-16 16:34:20 +01001413 mutex_lock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 /* already used by PDMA0? */
1415 if (ice->pcm_reserved[substream->number]) {
Ingo Molnar62932df2006-01-16 16:34:20 +01001416 mutex_unlock(&ice->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 return -EBUSY; /* FIXME: should handle blocking mode properly */
1418 }
Ingo Molnar62932df2006-01-16 16:34:20 +01001419 mutex_unlock(&ice->open_mutex);
Takashi Iwai32b47da2007-01-29 15:26:36 +01001420 runtime->private_data = (void *)&vt1724_playback_dma_regs[substream->number];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 ice->playback_con_substream_ds[substream->number] = substream;
1422 runtime->hw = snd_vt1724_2ch_stereo;
1423 snd_pcm_set_sync(substream);
1424 snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
1425 set_rate_constraints(ice, substream);
1426 return 0;
1427}
1428
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001429static int snd_vt1724_playback_indep_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001431 struct snd_ice1712 *ice = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
1433 if (PRO_RATE_RESET)
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001434 snd_vt1724_set_pro_rate(ice, ice->pro_rate_default, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 ice->playback_con_substream_ds[substream->number] = NULL;
1436 ice->pcm_reserved[substream->number] = NULL;
1437
1438 return 0;
1439}
1440
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001441static struct snd_pcm_ops snd_vt1724_playback_indep_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 .open = snd_vt1724_playback_indep_open,
1443 .close = snd_vt1724_playback_indep_close,
1444 .ioctl = snd_pcm_lib_ioctl,
1445 .hw_params = snd_vt1724_pcm_hw_params,
1446 .hw_free = snd_vt1724_pcm_hw_free,
1447 .prepare = snd_vt1724_playback_indep_prepare,
1448 .trigger = snd_vt1724_pcm_trigger,
1449 .pointer = snd_vt1724_pcm_pointer,
1450};
1451
1452
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001453static int __devinit snd_vt1724_pcm_indep(struct snd_ice1712 *ice, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001455 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 int play;
1457 int err;
1458
1459 play = ice->num_total_dacs / 2 - 1;
1460 if (play <= 0)
1461 return 0;
1462
1463 err = snd_pcm_new(ice->card, "ICE1724 Surrounds", device, play, 0, &pcm);
1464 if (err < 0)
1465 return err;
1466
1467 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1468 &snd_vt1724_playback_indep_ops);
1469
1470 pcm->private_data = ice;
1471 pcm->info_flags = 0;
1472 strcpy(pcm->name, "ICE1724 Surround PCM");
1473
1474 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001475 snd_dma_pci_data(ice->pci),
Robert Hancock1d4efa62009-10-07 20:19:21 -06001476 256*1024, 256*1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
1478 ice->pcm_ds = pcm;
1479
1480 return 0;
1481}
1482
1483
1484/*
1485 * Mixer section
1486 */
1487
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001488static int __devinit snd_vt1724_ac97_mixer(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489{
1490 int err;
1491
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001492 if (!(ice->eeprom.data[ICE_EEP2_ACLINK] & VT1724_CFG_PRO_I2S)) {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001493 struct snd_ac97_bus *pbus;
1494 struct snd_ac97_template ac97;
1495 static struct snd_ac97_bus_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 .write = snd_vt1724_ac97_write,
1497 .read = snd_vt1724_ac97_read,
1498 };
1499
1500 /* cold reset */
1501 outb(inb(ICEMT1724(ice, AC97_CMD)) | 0x80, ICEMT1724(ice, AC97_CMD));
1502 mdelay(5); /* FIXME */
1503 outb(inb(ICEMT1724(ice, AC97_CMD)) & ~0x80, ICEMT1724(ice, AC97_CMD));
1504
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001505 err = snd_ac97_bus(ice->card, 0, &ops, NULL, &pbus);
1506 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 return err;
1508 memset(&ac97, 0, sizeof(ac97));
1509 ac97.private_data = ice;
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001510 err = snd_ac97_mixer(pbus, &ac97, &ice->ac97);
1511 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 printk(KERN_WARNING "ice1712: cannot initialize pro ac97, skipped\n");
1513 else
1514 return 0;
1515 }
1516 /* I2S mixer only */
1517 strcat(ice->card->mixername, "ICE1724 - multitrack");
1518 return 0;
1519}
1520
1521/*
1522 *
1523 */
1524
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001525static inline unsigned int eeprom_triple(struct snd_ice1712 *ice, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526{
1527 return (unsigned int)ice->eeprom.data[idx] | \
1528 ((unsigned int)ice->eeprom.data[idx + 1] << 8) | \
1529 ((unsigned int)ice->eeprom.data[idx + 2] << 16);
1530}
1531
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001532static void snd_vt1724_proc_read(struct snd_info_entry *entry,
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001533 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001535 struct snd_ice1712 *ice = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 unsigned int idx;
1537
1538 snd_iprintf(buffer, "%s\n\n", ice->card->longname);
1539 snd_iprintf(buffer, "EEPROM:\n");
1540
1541 snd_iprintf(buffer, " Subvendor : 0x%x\n", ice->eeprom.subvendor);
1542 snd_iprintf(buffer, " Size : %i bytes\n", ice->eeprom.size);
1543 snd_iprintf(buffer, " Version : %i\n", ice->eeprom.version);
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001544 snd_iprintf(buffer, " System Config : 0x%x\n",
1545 ice->eeprom.data[ICE_EEP2_SYSCONF]);
1546 snd_iprintf(buffer, " ACLink : 0x%x\n",
1547 ice->eeprom.data[ICE_EEP2_ACLINK]);
1548 snd_iprintf(buffer, " I2S : 0x%x\n",
1549 ice->eeprom.data[ICE_EEP2_I2S]);
1550 snd_iprintf(buffer, " S/PDIF : 0x%x\n",
1551 ice->eeprom.data[ICE_EEP2_SPDIF]);
1552 snd_iprintf(buffer, " GPIO direction : 0x%x\n",
1553 ice->eeprom.gpiodir);
1554 snd_iprintf(buffer, " GPIO mask : 0x%x\n",
1555 ice->eeprom.gpiomask);
1556 snd_iprintf(buffer, " GPIO state : 0x%x\n",
1557 ice->eeprom.gpiostate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 for (idx = 0x12; idx < ice->eeprom.size; idx++)
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001559 snd_iprintf(buffer, " Extra #%02i : 0x%x\n",
1560 idx, ice->eeprom.data[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
1562 snd_iprintf(buffer, "\nRegisters:\n");
1563
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001564 snd_iprintf(buffer, " PSDOUT03 : 0x%08x\n",
1565 (unsigned)inl(ICEMT1724(ice, ROUTE_PLAYBACK)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 for (idx = 0x0; idx < 0x20 ; idx++)
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001567 snd_iprintf(buffer, " CCS%02x : 0x%02x\n",
1568 idx, inb(ice->port+idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 for (idx = 0x0; idx < 0x30 ; idx++)
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001570 snd_iprintf(buffer, " MT%02x : 0x%02x\n",
1571 idx, inb(ice->profi_port+idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572}
1573
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001574static void __devinit snd_vt1724_proc_init(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001576 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001578 if (!snd_card_proc_new(ice->card, "ice1724", &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +02001579 snd_info_set_text_ops(entry, ice, snd_vt1724_proc_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580}
1581
1582/*
1583 *
1584 */
1585
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001586static int snd_vt1724_eeprom_info(struct snd_kcontrol *kcontrol,
1587 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588{
1589 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001590 uinfo->count = sizeof(struct snd_ice1712_eeprom);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 return 0;
1592}
1593
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001594static int snd_vt1724_eeprom_get(struct snd_kcontrol *kcontrol,
1595 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001597 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001598
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 memcpy(ucontrol->value.bytes.data, &ice->eeprom, sizeof(ice->eeprom));
1600 return 0;
1601}
1602
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01001603static struct snd_kcontrol_new snd_vt1724_eeprom __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1605 .name = "ICE1724 EEPROM",
1606 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1607 .info = snd_vt1724_eeprom_info,
1608 .get = snd_vt1724_eeprom_get
1609};
1610
1611/*
1612 */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001613static int snd_vt1724_spdif_info(struct snd_kcontrol *kcontrol,
1614 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615{
1616 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1617 uinfo->count = 1;
1618 return 0;
1619}
1620
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001621static unsigned int encode_spdif_bits(struct snd_aes_iec958 *diga)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622{
Takashi Iwai2dfbeca2005-10-12 10:04:42 +02001623 unsigned int val, rbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
1625 val = diga->status[0] & 0x03; /* professional, non-audio */
1626 if (val & 0x01) {
1627 /* professional */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001628 if ((diga->status[0] & IEC958_AES0_PRO_EMPHASIS) ==
1629 IEC958_AES0_PRO_EMPHASIS_5015)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 val |= 1U << 3;
Takashi Iwai2dfbeca2005-10-12 10:04:42 +02001631 rbits = (diga->status[4] >> 3) & 0x0f;
1632 if (rbits) {
1633 switch (rbits) {
1634 case 2: val |= 5 << 12; break; /* 96k */
1635 case 3: val |= 6 << 12; break; /* 192k */
1636 case 10: val |= 4 << 12; break; /* 88.2k */
1637 case 11: val |= 7 << 12; break; /* 176.4k */
1638 }
1639 } else {
1640 switch (diga->status[0] & IEC958_AES0_PRO_FS) {
1641 case IEC958_AES0_PRO_FS_44100:
1642 break;
1643 case IEC958_AES0_PRO_FS_32000:
1644 val |= 3U << 12;
1645 break;
1646 default:
1647 val |= 2U << 12;
1648 break;
1649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 }
1651 } else {
1652 /* consumer */
1653 val |= diga->status[1] & 0x04; /* copyright */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001654 if ((diga->status[0] & IEC958_AES0_CON_EMPHASIS) ==
1655 IEC958_AES0_CON_EMPHASIS_5015)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 val |= 1U << 3;
1657 val |= (unsigned int)(diga->status[1] & 0x3f) << 4; /* category */
1658 val |= (unsigned int)(diga->status[3] & IEC958_AES3_CON_FS) << 12; /* fs */
1659 }
1660 return val;
1661}
1662
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001663static void decode_spdif_bits(struct snd_aes_iec958 *diga, unsigned int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664{
1665 memset(diga->status, 0, sizeof(diga->status));
1666 diga->status[0] = val & 0x03; /* professional, non-audio */
1667 if (val & 0x01) {
1668 /* professional */
1669 if (val & (1U << 3))
1670 diga->status[0] |= IEC958_AES0_PRO_EMPHASIS_5015;
1671 switch ((val >> 12) & 0x7) {
1672 case 0:
1673 break;
1674 case 2:
1675 diga->status[0] |= IEC958_AES0_PRO_FS_32000;
1676 break;
1677 default:
1678 diga->status[0] |= IEC958_AES0_PRO_FS_48000;
1679 break;
1680 }
1681 } else {
1682 /* consumer */
1683 diga->status[0] |= val & (1U << 2); /* copyright */
1684 if (val & (1U << 3))
1685 diga->status[0] |= IEC958_AES0_CON_EMPHASIS_5015;
1686 diga->status[1] |= (val >> 4) & 0x3f; /* category */
1687 diga->status[3] |= (val >> 12) & 0x07; /* fs */
1688 }
1689}
1690
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001691static int snd_vt1724_spdif_default_get(struct snd_kcontrol *kcontrol,
1692 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001694 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 unsigned int val;
1696 val = inw(ICEMT1724(ice, SPDIF_CTRL));
1697 decode_spdif_bits(&ucontrol->value.iec958, val);
1698 return 0;
1699}
1700
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001701static int snd_vt1724_spdif_default_put(struct snd_kcontrol *kcontrol,
1702 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001704 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 unsigned int val, old;
1706
1707 val = encode_spdif_bits(&ucontrol->value.iec958);
1708 spin_lock_irq(&ice->reg_lock);
1709 old = inw(ICEMT1724(ice, SPDIF_CTRL));
1710 if (val != old)
1711 update_spdif_bits(ice, val);
1712 spin_unlock_irq(&ice->reg_lock);
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001713 return val != old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714}
1715
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01001716static struct snd_kcontrol_new snd_vt1724_spdif_default __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717{
1718 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001719 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 .info = snd_vt1724_spdif_info,
1721 .get = snd_vt1724_spdif_default_get,
1722 .put = snd_vt1724_spdif_default_put
1723};
1724
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001725static int snd_vt1724_spdif_maskc_get(struct snd_kcontrol *kcontrol,
1726 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727{
1728 ucontrol->value.iec958.status[0] = IEC958_AES0_NONAUDIO |
1729 IEC958_AES0_PROFESSIONAL |
1730 IEC958_AES0_CON_NOT_COPYRIGHT |
1731 IEC958_AES0_CON_EMPHASIS;
1732 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_ORIGINAL |
1733 IEC958_AES1_CON_CATEGORY;
1734 ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS;
1735 return 0;
1736}
1737
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001738static int snd_vt1724_spdif_maskp_get(struct snd_kcontrol *kcontrol,
1739 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740{
1741 ucontrol->value.iec958.status[0] = IEC958_AES0_NONAUDIO |
1742 IEC958_AES0_PROFESSIONAL |
1743 IEC958_AES0_PRO_FS |
1744 IEC958_AES0_PRO_EMPHASIS;
1745 return 0;
1746}
1747
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01001748static struct snd_kcontrol_new snd_vt1724_spdif_maskc __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749{
1750 .access = SNDRV_CTL_ELEM_ACCESS_READ,
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001751 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001752 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 .info = snd_vt1724_spdif_info,
1754 .get = snd_vt1724_spdif_maskc_get,
1755};
1756
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01001757static struct snd_kcontrol_new snd_vt1724_spdif_maskp __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758{
1759 .access = SNDRV_CTL_ELEM_ACCESS_READ,
Clemens Ladisch67ed4162005-07-29 15:32:58 +02001760 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001761 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 .info = snd_vt1724_spdif_info,
1763 .get = snd_vt1724_spdif_maskp_get,
1764};
1765
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001766#define snd_vt1724_spdif_sw_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001768static int snd_vt1724_spdif_sw_get(struct snd_kcontrol *kcontrol,
1769 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001771 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
1772 ucontrol->value.integer.value[0] = inb(ICEREG1724(ice, SPDIF_CFG)) &
1773 VT1724_CFG_SPDIF_OUT_EN ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 return 0;
1775}
1776
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001777static int snd_vt1724_spdif_sw_put(struct snd_kcontrol *kcontrol,
1778 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001780 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 unsigned char old, val;
1782
1783 spin_lock_irq(&ice->reg_lock);
1784 old = val = inb(ICEREG1724(ice, SPDIF_CFG));
1785 val &= ~VT1724_CFG_SPDIF_OUT_EN;
1786 if (ucontrol->value.integer.value[0])
1787 val |= VT1724_CFG_SPDIF_OUT_EN;
1788 if (old != val)
1789 outb(val, ICEREG1724(ice, SPDIF_CFG));
1790 spin_unlock_irq(&ice->reg_lock);
1791 return old != val;
1792}
1793
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01001794static struct snd_kcontrol_new snd_vt1724_spdif_switch __devinitdata =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795{
1796 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1797 /* FIXME: the following conflict with IEC958 Playback Route */
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001798 /* .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH), */
1799 .name = SNDRV_CTL_NAME_IEC958("Output ", NONE, SWITCH),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 .info = snd_vt1724_spdif_sw_info,
1801 .get = snd_vt1724_spdif_sw_get,
1802 .put = snd_vt1724_spdif_sw_put
1803};
1804
1805
1806#if 0 /* NOT USED YET */
1807/*
1808 * GPIO access from extern
1809 */
1810
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001811#define snd_vt1724_gpio_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001813int snd_vt1724_gpio_get(struct snd_kcontrol *kcontrol,
1814 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001816 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 int shift = kcontrol->private_value & 0xff;
1818 int invert = (kcontrol->private_value & (1<<24)) ? 1 : 0;
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001819
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 snd_ice1712_save_gpio_status(ice);
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001821 ucontrol->value.integer.value[0] =
1822 (snd_ice1712_gpio_read(ice) & (1 << shift) ? 1 : 0) ^ invert;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 snd_ice1712_restore_gpio_status(ice);
1824 return 0;
1825}
1826
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001827int snd_ice1712_gpio_put(struct snd_kcontrol *kcontrol,
1828 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001830 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 int shift = kcontrol->private_value & 0xff;
1832 int invert = (kcontrol->private_value & (1<<24)) ? mask : 0;
1833 unsigned int val, nval;
1834
1835 if (kcontrol->private_value & (1 << 31))
1836 return -EPERM;
1837 nval = (ucontrol->value.integer.value[0] ? (1 << shift) : 0) ^ invert;
1838 snd_ice1712_save_gpio_status(ice);
1839 val = snd_ice1712_gpio_read(ice);
1840 nval |= val & ~(1 << shift);
1841 if (val != nval)
1842 snd_ice1712_gpio_write(ice, nval);
1843 snd_ice1712_restore_gpio_status(ice);
1844 return val != nval;
1845}
1846#endif /* NOT USED YET */
1847
1848/*
1849 * rate
1850 */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001851static int snd_vt1724_pro_internal_clock_info(struct snd_kcontrol *kcontrol,
1852 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001854 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Pavel Hofman1ff97cb2009-09-16 22:25:40 +02001855 int hw_rates_count = ice->hw_rates->count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1857 uinfo->count = 1;
Pavel Hofman1ff97cb2009-09-16 22:25:40 +02001858
Pavel Hofman219e2cd2012-01-05 22:01:56 +01001859 /* internal clocks */
1860 uinfo->value.enumerated.items = hw_rates_count;
1861 /* external clocks */
1862 if (ice->force_rdma1 ||
1863 (ice->eeprom.data[ICE_EEP2_SPDIF] & VT1724_CFG_SPDIF_IN))
1864 uinfo->value.enumerated.items += ice->ext_clock_count;
Pavel Hofman1ff97cb2009-09-16 22:25:40 +02001865 /* upper limit - keep at top */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1867 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
Pavel Hofman1ff97cb2009-09-16 22:25:40 +02001868 if (uinfo->value.enumerated.item >= hw_rates_count)
1869 /* ext_clock items */
1870 strcpy(uinfo->value.enumerated.name,
1871 ice->ext_clock_names[
1872 uinfo->value.enumerated.item - hw_rates_count]);
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001873 else
Pavel Hofman1ff97cb2009-09-16 22:25:40 +02001874 /* int clock items */
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001875 sprintf(uinfo->value.enumerated.name, "%d",
1876 ice->hw_rates->list[uinfo->value.enumerated.item]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 return 0;
1878}
1879
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001880static int snd_vt1724_pro_internal_clock_get(struct snd_kcontrol *kcontrol,
1881 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001883 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001884 unsigned int i, rate;
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04001885
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 spin_lock_irq(&ice->reg_lock);
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001887 if (ice->is_spdif_master(ice)) {
Pavel Hofman1ff97cb2009-09-16 22:25:40 +02001888 ucontrol->value.enumerated.item[0] = ice->hw_rates->count +
1889 ice->get_spdif_master_type(ice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 } else {
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001891 rate = ice->get_rate(ice);
1892 ucontrol->value.enumerated.item[0] = 0;
1893 for (i = 0; i < ice->hw_rates->count; i++) {
1894 if (ice->hw_rates->list[i] == rate) {
1895 ucontrol->value.enumerated.item[0] = i;
1896 break;
1897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 }
1900 spin_unlock_irq(&ice->reg_lock);
1901 return 0;
1902}
1903
Pavel Hofman1ff97cb2009-09-16 22:25:40 +02001904static int stdclock_get_spdif_master_type(struct snd_ice1712 *ice)
1905{
1906 /* standard external clock - only single type - SPDIF IN */
1907 return 0;
1908}
1909
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001910/* setting clock to external - SPDIF */
Pavel Hofman1ff97cb2009-09-16 22:25:40 +02001911static int stdclock_set_spdif_clock(struct snd_ice1712 *ice, int type)
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001912{
1913 unsigned char oval;
1914 unsigned char i2s_oval;
1915 oval = inb(ICEMT1724(ice, RATE));
1916 outb(oval | VT1724_SPDIF_MASTER, ICEMT1724(ice, RATE));
1917 /* setting 256fs */
1918 i2s_oval = inb(ICEMT1724(ice, I2S_FORMAT));
1919 outb(i2s_oval & ~VT1724_MT_I2S_MCLK_128X, ICEMT1724(ice, I2S_FORMAT));
Pavel Hofman1ff97cb2009-09-16 22:25:40 +02001920 return 0;
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001921}
1922
Pavel Hofman1ff97cb2009-09-16 22:25:40 +02001923
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001924static int snd_vt1724_pro_internal_clock_put(struct snd_kcontrol *kcontrol,
1925 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001927 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001928 unsigned int old_rate, new_rate;
1929 unsigned int item = ucontrol->value.enumerated.item[0];
Pavel Hofman1ff97cb2009-09-16 22:25:40 +02001930 unsigned int first_ext_clock = ice->hw_rates->count;
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001931
Pavel Hofman1ff97cb2009-09-16 22:25:40 +02001932 if (item > first_ext_clock + ice->ext_clock_count - 1)
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001933 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
Pavel Hofman1ff97cb2009-09-16 22:25:40 +02001935 /* if rate = 0 => external clock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 spin_lock_irq(&ice->reg_lock);
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001937 if (ice->is_spdif_master(ice))
1938 old_rate = 0;
1939 else
1940 old_rate = ice->get_rate(ice);
Pavel Hofman1ff97cb2009-09-16 22:25:40 +02001941 if (item >= first_ext_clock) {
1942 /* switching to external clock */
1943 ice->set_spdif_clock(ice, item - first_ext_clock);
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001944 new_rate = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 } else {
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001946 /* internal on-card clock */
1947 new_rate = ice->hw_rates->list[item];
1948 ice->pro_rate_default = new_rate;
1949 spin_unlock_irq(&ice->reg_lock);
1950 snd_vt1724_set_pro_rate(ice, ice->pro_rate_default, 1);
1951 spin_lock_irq(&ice->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 spin_unlock_irq(&ice->reg_lock);
1954
Pavel Hofman1ff97cb2009-09-16 22:25:40 +02001955 /* the first switch to the ext. clock mode? */
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001956 if (old_rate != new_rate && !new_rate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 /* notify akm chips as well */
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001958 unsigned int i;
1959 if (ice->gpio.set_pro_rate)
1960 ice->gpio.set_pro_rate(ice, 0);
1961 for (i = 0; i < ice->akm_codecs; i++) {
1962 if (ice->akm[i].ops.set_rate_val)
1963 ice->akm[i].ops.set_rate_val(&ice->akm[i], 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 }
1965 }
Pavel Hofmand16be8e2008-03-20 12:10:27 +01001966 return old_rate != new_rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967}
1968
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01001969static struct snd_kcontrol_new snd_vt1724_pro_internal_clock __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1971 .name = "Multi Track Internal Clock",
1972 .info = snd_vt1724_pro_internal_clock_info,
1973 .get = snd_vt1724_pro_internal_clock_get,
1974 .put = snd_vt1724_pro_internal_clock_put
1975};
1976
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001977#define snd_vt1724_pro_rate_locking_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001979static int snd_vt1724_pro_rate_locking_get(struct snd_kcontrol *kcontrol,
1980 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981{
1982 ucontrol->value.integer.value[0] = PRO_RATE_LOCKED;
1983 return 0;
1984}
1985
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001986static int snd_vt1724_pro_rate_locking_put(struct snd_kcontrol *kcontrol,
1987 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01001989 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 int change = 0, nval;
1991
1992 nval = ucontrol->value.integer.value[0] ? 1 : 0;
1993 spin_lock_irq(&ice->reg_lock);
1994 change = PRO_RATE_LOCKED != nval;
1995 PRO_RATE_LOCKED = nval;
1996 spin_unlock_irq(&ice->reg_lock);
1997 return change;
1998}
1999
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01002000static struct snd_kcontrol_new snd_vt1724_pro_rate_locking __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2002 .name = "Multi Track Rate Locking",
2003 .info = snd_vt1724_pro_rate_locking_info,
2004 .get = snd_vt1724_pro_rate_locking_get,
2005 .put = snd_vt1724_pro_rate_locking_put
2006};
2007
Takashi Iwaia5ce8892007-07-23 15:42:26 +02002008#define snd_vt1724_pro_rate_reset_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002010static int snd_vt1724_pro_rate_reset_get(struct snd_kcontrol *kcontrol,
2011 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012{
2013 ucontrol->value.integer.value[0] = PRO_RATE_RESET ? 1 : 0;
2014 return 0;
2015}
2016
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002017static int snd_vt1724_pro_rate_reset_put(struct snd_kcontrol *kcontrol,
2018 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002020 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 int change = 0, nval;
2022
2023 nval = ucontrol->value.integer.value[0] ? 1 : 0;
2024 spin_lock_irq(&ice->reg_lock);
2025 change = PRO_RATE_RESET != nval;
2026 PRO_RATE_RESET = nval;
2027 spin_unlock_irq(&ice->reg_lock);
2028 return change;
2029}
2030
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01002031static struct snd_kcontrol_new snd_vt1724_pro_rate_reset __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2033 .name = "Multi Track Rate Reset",
2034 .info = snd_vt1724_pro_rate_reset_info,
2035 .get = snd_vt1724_pro_rate_reset_get,
2036 .put = snd_vt1724_pro_rate_reset_put
2037};
2038
2039
2040/*
2041 * routing
2042 */
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002043static int snd_vt1724_pro_route_info(struct snd_kcontrol *kcontrol,
2044 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045{
Takashi Iwaia2af0502012-10-17 09:21:48 +02002046 static const char * const texts[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 "PCM Out", /* 0 */
2048 "H/W In 0", "H/W In 1", /* 1-2 */
2049 "IEC958 In L", "IEC958 In R", /* 3-4 */
2050 };
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002051
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2053 uinfo->count = 1;
2054 uinfo->value.enumerated.items = 5;
2055 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2056 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2057 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2058 return 0;
2059}
2060
2061static inline int analog_route_shift(int idx)
2062{
2063 return (idx % 2) * 12 + ((idx / 2) * 3) + 8;
2064}
2065
2066static inline int digital_route_shift(int idx)
2067{
2068 return idx * 3;
2069}
2070
Takashi Iwai2bf864a2009-05-06 17:30:17 +02002071int snd_ice1724_get_route_val(struct snd_ice1712 *ice, int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072{
2073 unsigned long val;
2074 unsigned char eitem;
Takashi Iwai32b47da2007-01-29 15:26:36 +01002075 static const unsigned char xlate[8] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 0, 255, 1, 2, 255, 255, 3, 4,
2077 };
2078
2079 val = inl(ICEMT1724(ice, ROUTE_PLAYBACK));
2080 val >>= shift;
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002081 val &= 7; /* we now have 3 bits per output */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 eitem = xlate[val];
2083 if (eitem == 255) {
2084 snd_BUG();
2085 return 0;
2086 }
2087 return eitem;
2088}
2089
Takashi Iwai2bf864a2009-05-06 17:30:17 +02002090int snd_ice1724_put_route_val(struct snd_ice1712 *ice, unsigned int val,
2091 int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092{
2093 unsigned int old_val, nval;
2094 int change;
Takashi Iwai32b47da2007-01-29 15:26:36 +01002095 static const unsigned char xroute[8] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 0, /* PCM */
2097 2, /* PSDIN0 Left */
2098 3, /* PSDIN0 Right */
2099 6, /* SPDIN Left */
2100 7, /* SPDIN Right */
2101 };
2102
2103 nval = xroute[val % 5];
2104 val = old_val = inl(ICEMT1724(ice, ROUTE_PLAYBACK));
2105 val &= ~(0x07 << shift);
2106 val |= nval << shift;
2107 change = val != old_val;
2108 if (change)
2109 outl(val, ICEMT1724(ice, ROUTE_PLAYBACK));
2110 return change;
2111}
2112
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002113static int snd_vt1724_pro_route_analog_get(struct snd_kcontrol *kcontrol,
2114 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002116 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002118 ucontrol->value.enumerated.item[0] =
Takashi Iwai2bf864a2009-05-06 17:30:17 +02002119 snd_ice1724_get_route_val(ice, analog_route_shift(idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 return 0;
2121}
2122
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002123static int snd_vt1724_pro_route_analog_put(struct snd_kcontrol *kcontrol,
2124 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002126 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai2bf864a2009-05-06 17:30:17 +02002128 return snd_ice1724_put_route_val(ice,
2129 ucontrol->value.enumerated.item[0],
2130 analog_route_shift(idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131}
2132
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002133static int snd_vt1724_pro_route_spdif_get(struct snd_kcontrol *kcontrol,
2134 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002136 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002138 ucontrol->value.enumerated.item[0] =
Takashi Iwai2bf864a2009-05-06 17:30:17 +02002139 snd_ice1724_get_route_val(ice, digital_route_shift(idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 return 0;
2141}
2142
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002143static int snd_vt1724_pro_route_spdif_put(struct snd_kcontrol *kcontrol,
2144 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002146 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
Takashi Iwai2bf864a2009-05-06 17:30:17 +02002148 return snd_ice1724_put_route_val(ice,
2149 ucontrol->value.enumerated.item[0],
2150 digital_route_shift(idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151}
2152
Takashi Iwai2bf864a2009-05-06 17:30:17 +02002153static struct snd_kcontrol_new snd_vt1724_mixer_pro_analog_route __devinitdata =
2154{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2156 .name = "H/W Playback Route",
2157 .info = snd_vt1724_pro_route_info,
2158 .get = snd_vt1724_pro_route_analog_get,
2159 .put = snd_vt1724_pro_route_analog_put,
2160};
2161
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01002162static struct snd_kcontrol_new snd_vt1724_mixer_pro_spdif_route __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002164 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, NONE) "Route",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 .info = snd_vt1724_pro_route_info,
2166 .get = snd_vt1724_pro_route_spdif_get,
2167 .put = snd_vt1724_pro_route_spdif_put,
2168 .count = 2,
2169};
2170
2171
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002172static int snd_vt1724_pro_peak_info(struct snd_kcontrol *kcontrol,
2173 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174{
2175 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2176 uinfo->count = 22; /* FIXME: for compatibility with ice1712... */
2177 uinfo->value.integer.min = 0;
2178 uinfo->value.integer.max = 255;
2179 return 0;
2180}
2181
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002182static int snd_vt1724_pro_peak_get(struct snd_kcontrol *kcontrol,
2183 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002185 struct snd_ice1712 *ice = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 int idx;
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002187
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 spin_lock_irq(&ice->reg_lock);
2189 for (idx = 0; idx < 22; idx++) {
2190 outb(idx, ICEMT1724(ice, MONITOR_PEAKINDEX));
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002191 ucontrol->value.integer.value[idx] =
2192 inb(ICEMT1724(ice, MONITOR_PEAKDATA));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 }
2194 spin_unlock_irq(&ice->reg_lock);
2195 return 0;
2196}
2197
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01002198static struct snd_kcontrol_new snd_vt1724_mixer_pro_peak __devinitdata = {
Pavel Hofman2bdf6632009-10-06 16:04:11 +02002199 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 .name = "Multi Track Peak",
2201 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
2202 .info = snd_vt1724_pro_peak_info,
2203 .get = snd_vt1724_pro_peak_get
2204};
2205
2206/*
2207 *
2208 */
2209
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01002210static struct snd_ice1712_card_info no_matched __devinitdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
Pavel Hofman52cd0a72012-01-10 20:28:48 +01002212
2213/*
2214 ooAoo cards with no controls
2215*/
2216static unsigned char ooaoo_sq210_eeprom[] __devinitdata = {
2217 [ICE_EEP2_SYSCONF] = 0x4c, /* 49MHz crystal, no mpu401, no ADC,
2218 1xDACs */
2219 [ICE_EEP2_ACLINK] = 0x80, /* I2S */
2220 [ICE_EEP2_I2S] = 0x78, /* no volume, 96k, 24bit, 192k */
2221 [ICE_EEP2_SPDIF] = 0xc1, /* out-en, out-int, out-ext */
2222 [ICE_EEP2_GPIO_DIR] = 0x00, /* no GPIOs are used */
2223 [ICE_EEP2_GPIO_DIR1] = 0x00,
2224 [ICE_EEP2_GPIO_DIR2] = 0x00,
2225 [ICE_EEP2_GPIO_MASK] = 0xff,
2226 [ICE_EEP2_GPIO_MASK1] = 0xff,
2227 [ICE_EEP2_GPIO_MASK2] = 0xff,
2228
2229 [ICE_EEP2_GPIO_STATE] = 0x00, /* inputs */
2230 [ICE_EEP2_GPIO_STATE1] = 0x00, /* all 1, but GPIO_CPLD_RW
2231 and GPIO15 always zero */
2232 [ICE_EEP2_GPIO_STATE2] = 0x00, /* inputs */
2233};
2234
2235
Takashi Iwaia2af0502012-10-17 09:21:48 +02002236static struct snd_ice1712_card_info snd_vt1724_ooaoo_cards[] __devinitdata = {
Pavel Hofman52cd0a72012-01-10 20:28:48 +01002237 {
2238 .name = "ooAoo SQ210a",
2239 .model = "sq210a",
2240 .eeprom_size = sizeof(ooaoo_sq210_eeprom),
2241 .eeprom_data = ooaoo_sq210_eeprom,
2242 },
2243 { } /* terminator */
2244};
2245
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01002246static struct snd_ice1712_card_info *card_tables[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 snd_vt1724_revo_cards,
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002248 snd_vt1724_amp_cards,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 snd_vt1724_aureon_cards,
2250 snd_vt1720_mobo_cards,
2251 snd_vt1720_pontis_cards,
Julian Scheel6b8d6e52008-01-16 19:50:00 +01002252 snd_vt1724_prodigy_hifi_cards,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 snd_vt1724_prodigy192_cards,
2254 snd_vt1724_juli_cards,
Takashi Iwai72cbfd42009-05-06 17:33:19 +02002255 snd_vt1724_maya44_cards,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 snd_vt1724_phase_cards,
Clement Guedezf6cdab52007-01-08 10:48:41 +01002257 snd_vt1724_wtm_cards,
Shin-ya Okadaf31639b2007-10-23 15:08:18 +02002258 snd_vt1724_se_cards,
Pavel Hofman6ef80702009-09-16 22:25:41 +02002259 snd_vt1724_qtet_cards,
Pavel Hofman52cd0a72012-01-10 20:28:48 +01002260 snd_vt1724_ooaoo_cards,
Ondrej Zary77b0b252012-10-14 21:09:22 +02002261 snd_vt1724_psc724_cards,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 NULL,
2263};
2264
2265
2266/*
2267 */
2268
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002269static void wait_i2c_busy(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270{
2271 int t = 0x10000;
2272 while ((inb(ICEREG1724(ice, I2C_CTRL)) & VT1724_I2C_BUSY) && t--)
2273 ;
2274 if (t == -1)
2275 printk(KERN_ERR "ice1724: i2c busy timeout\n");
2276}
2277
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002278unsigned char snd_vt1724_read_i2c(struct snd_ice1712 *ice,
2279 unsigned char dev, unsigned char addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280{
2281 unsigned char val;
2282
Ingo Molnar62932df2006-01-16 16:34:20 +01002283 mutex_lock(&ice->i2c_mutex);
Pavel Hofmanacec30f2007-12-03 12:37:17 +01002284 wait_i2c_busy(ice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 outb(addr, ICEREG1724(ice, I2C_BYTE_ADDR));
2286 outb(dev & ~VT1724_I2C_WRITE, ICEREG1724(ice, I2C_DEV_ADDR));
2287 wait_i2c_busy(ice);
2288 val = inb(ICEREG1724(ice, I2C_DATA));
Ingo Molnar62932df2006-01-16 16:34:20 +01002289 mutex_unlock(&ice->i2c_mutex);
Takashi Iwaie2ea7cfc2009-02-05 16:07:02 +01002290 /*
2291 printk(KERN_DEBUG "i2c_read: [0x%x,0x%x] = 0x%x\n", dev, addr, val);
2292 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 return val;
2294}
2295
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002296void snd_vt1724_write_i2c(struct snd_ice1712 *ice,
2297 unsigned char dev, unsigned char addr, unsigned char data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298{
Ingo Molnar62932df2006-01-16 16:34:20 +01002299 mutex_lock(&ice->i2c_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 wait_i2c_busy(ice);
Takashi Iwaie2ea7cfc2009-02-05 16:07:02 +01002301 /*
2302 printk(KERN_DEBUG "i2c_write: [0x%x,0x%x] = 0x%x\n", dev, addr, data);
2303 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 outb(addr, ICEREG1724(ice, I2C_BYTE_ADDR));
2305 outb(data, ICEREG1724(ice, I2C_DATA));
2306 outb(dev | VT1724_I2C_WRITE, ICEREG1724(ice, I2C_DEV_ADDR));
2307 wait_i2c_busy(ice);
Ingo Molnar62932df2006-01-16 16:34:20 +01002308 mutex_unlock(&ice->i2c_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309}
2310
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002311static int __devinit snd_vt1724_read_eeprom(struct snd_ice1712 *ice,
2312 const char *modelname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313{
2314 const int dev = 0xa0; /* EEPROM device address */
2315 unsigned int i, size;
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01002316 struct snd_ice1712_card_info * const *tbl, *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002318 if (!modelname || !*modelname) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 ice->eeprom.subvendor = 0;
2320 if ((inb(ICEREG1724(ice, I2C_CTRL)) & VT1724_I2C_EEPROM) != 0)
2321 ice->eeprom.subvendor =
2322 (snd_vt1724_read_i2c(ice, dev, 0x00) << 0) |
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002323 (snd_vt1724_read_i2c(ice, dev, 0x01) << 8) |
2324 (snd_vt1724_read_i2c(ice, dev, 0x02) << 16) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 (snd_vt1724_read_i2c(ice, dev, 0x03) << 24);
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002326 if (ice->eeprom.subvendor == 0 ||
2327 ice->eeprom.subvendor == (unsigned int)-1) {
2328 /* invalid subvendor from EEPROM, try the PCI
2329 * subststem ID instead
2330 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 u16 vendor, device;
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002332 pci_read_config_word(ice->pci, PCI_SUBSYSTEM_VENDOR_ID,
2333 &vendor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 pci_read_config_word(ice->pci, PCI_SUBSYSTEM_ID, &device);
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002335 ice->eeprom.subvendor =
2336 ((unsigned int)swab16(vendor) << 16) | swab16(device);
2337 if (ice->eeprom.subvendor == 0 ||
2338 ice->eeprom.subvendor == (unsigned int)-1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 printk(KERN_ERR "ice1724: No valid ID is found\n");
2340 return -ENXIO;
2341 }
2342 }
2343 }
2344 for (tbl = card_tables; *tbl; tbl++) {
Pavel Hofman2b151ef2012-01-10 20:28:47 +01002345 for (c = *tbl; c->name; c++) {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002346 if (modelname && c->model &&
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002347 !strcmp(modelname, c->model)) {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002348 printk(KERN_INFO "ice1724: Using board model %s\n",
2349 c->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 ice->eeprom.subvendor = c->subvendor;
2351 } else if (c->subvendor != ice->eeprom.subvendor)
2352 continue;
Ondrej Zary267bcca2012-10-14 21:09:19 +02002353 ice->card_info = c;
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002354 if (!c->eeprom_size || !c->eeprom_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 goto found;
2356 /* if the EEPROM is given by the driver, use it */
2357 snd_printdd("using the defined eeprom..\n");
2358 ice->eeprom.version = 2;
2359 ice->eeprom.size = c->eeprom_size + 6;
2360 memcpy(ice->eeprom.data, c->eeprom_data, c->eeprom_size);
2361 goto read_skipped;
2362 }
2363 }
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002364 printk(KERN_WARNING "ice1724: No matching model found for ID 0x%x\n",
2365 ice->eeprom.subvendor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
2367 found:
2368 ice->eeprom.size = snd_vt1724_read_i2c(ice, dev, 0x04);
2369 if (ice->eeprom.size < 6)
2370 ice->eeprom.size = 32;
2371 else if (ice->eeprom.size > 32) {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002372 printk(KERN_ERR "ice1724: Invalid EEPROM (size = %i)\n",
2373 ice->eeprom.size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 return -EIO;
2375 }
2376 ice->eeprom.version = snd_vt1724_read_i2c(ice, dev, 0x05);
Ondrej Zary77b0b252012-10-14 21:09:22 +02002377 if (ice->eeprom.version != 1 && ice->eeprom.version != 2)
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002378 printk(KERN_WARNING "ice1724: Invalid EEPROM version %i\n",
2379 ice->eeprom.version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 size = ice->eeprom.size - 6;
2381 for (i = 0; i < size; i++)
2382 ice->eeprom.data[i] = snd_vt1724_read_i2c(ice, dev, i + 6);
2383
2384 read_skipped:
2385 ice->eeprom.gpiomask = eeprom_triple(ice, ICE_EEP2_GPIO_MASK);
2386 ice->eeprom.gpiostate = eeprom_triple(ice, ICE_EEP2_GPIO_STATE);
2387 ice->eeprom.gpiodir = eeprom_triple(ice, ICE_EEP2_GPIO_DIR);
2388
2389 return 0;
2390}
2391
2392
2393
Takashi Iwaicd775382009-07-02 10:28:56 +02002394static void snd_vt1724_chip_reset(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395{
2396 outb(VT1724_RESET , ICEREG1724(ice, CONTROL));
Takashi Iwaid82b64f2009-05-06 17:25:42 +02002397 inb(ICEREG1724(ice, CONTROL)); /* pci posting flush */
Karsten Wiese988f0662008-04-22 12:52:15 +02002398 msleep(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 outb(0, ICEREG1724(ice, CONTROL));
Takashi Iwaid82b64f2009-05-06 17:25:42 +02002400 inb(ICEREG1724(ice, CONTROL)); /* pci posting flush */
Karsten Wiese988f0662008-04-22 12:52:15 +02002401 msleep(10);
2402}
2403
Igor Chernyshevb40e9532009-06-25 09:31:07 +02002404static int snd_vt1724_chip_init(struct snd_ice1712 *ice)
Karsten Wiese988f0662008-04-22 12:52:15 +02002405{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 outb(ice->eeprom.data[ICE_EEP2_SYSCONF], ICEREG1724(ice, SYS_CFG));
2407 outb(ice->eeprom.data[ICE_EEP2_ACLINK], ICEREG1724(ice, AC97_CFG));
2408 outb(ice->eeprom.data[ICE_EEP2_I2S], ICEREG1724(ice, I2S_FEATURES));
2409 outb(ice->eeprom.data[ICE_EEP2_SPDIF], ICEREG1724(ice, SPDIF_CFG));
2410
2411 ice->gpio.write_mask = ice->eeprom.gpiomask;
2412 ice->gpio.direction = ice->eeprom.gpiodir;
2413 snd_vt1724_set_gpio_mask(ice, ice->eeprom.gpiomask);
2414 snd_vt1724_set_gpio_dir(ice, ice->eeprom.gpiodir);
2415 snd_vt1724_set_gpio_data(ice, ice->eeprom.gpiostate);
2416
2417 outb(0, ICEREG1724(ice, POWERDOWN));
2418
Igor Chernyshevb40e9532009-06-25 09:31:07 +02002419 /* MPU_RX and TX irq masks are cleared later dynamically */
2420 outb(VT1724_IRQ_MPU_RX | VT1724_IRQ_MPU_TX , ICEREG1724(ice, IRQMASK));
2421
2422 /* don't handle FIFO overrun/underruns (just yet),
2423 * since they cause machine lockups
2424 */
2425 outb(VT1724_MULTI_FIFO_ERR, ICEMT1724(ice, DMA_INT_MASK));
2426
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 return 0;
2428}
2429
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002430static int __devinit snd_vt1724_spdif_build_controls(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431{
2432 int err;
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002433 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434
Takashi Iwaida3cec32008-08-08 17:12:14 +02002435 if (snd_BUG_ON(!ice->pcm))
2436 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437
Takashi Iwai2bf864a2009-05-06 17:30:17 +02002438 if (!ice->own_routing) {
2439 err = snd_ctl_add(ice->card,
2440 snd_ctl_new1(&snd_vt1724_mixer_pro_spdif_route, ice));
2441 if (err < 0)
2442 return err;
2443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
2445 err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_spdif_switch, ice));
2446 if (err < 0)
2447 return err;
2448
2449 err = snd_ctl_add(ice->card, kctl = snd_ctl_new1(&snd_vt1724_spdif_default, ice));
2450 if (err < 0)
2451 return err;
2452 kctl->id.device = ice->pcm->device;
2453 err = snd_ctl_add(ice->card, kctl = snd_ctl_new1(&snd_vt1724_spdif_maskc, ice));
2454 if (err < 0)
2455 return err;
2456 kctl->id.device = ice->pcm->device;
2457 err = snd_ctl_add(ice->card, kctl = snd_ctl_new1(&snd_vt1724_spdif_maskp, ice));
2458 if (err < 0)
2459 return err;
2460 kctl->id.device = ice->pcm->device;
2461#if 0 /* use default only */
2462 err = snd_ctl_add(ice->card, kctl = snd_ctl_new1(&snd_vt1724_spdif_stream, ice));
2463 if (err < 0)
2464 return err;
2465 kctl->id.device = ice->pcm->device;
2466 ice->spdif.stream_ctl = kctl;
2467#endif
2468 return 0;
2469}
2470
2471
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002472static int __devinit snd_vt1724_build_controls(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473{
2474 int err;
2475
2476 err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_eeprom, ice));
2477 if (err < 0)
2478 return err;
2479 err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_pro_internal_clock, ice));
2480 if (err < 0)
2481 return err;
2482
2483 err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_pro_rate_locking, ice));
2484 if (err < 0)
2485 return err;
2486 err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_pro_rate_reset, ice));
2487 if (err < 0)
2488 return err;
2489
Takashi Iwai2bf864a2009-05-06 17:30:17 +02002490 if (!ice->own_routing && ice->num_total_dacs > 0) {
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002491 struct snd_kcontrol_new tmp = snd_vt1724_mixer_pro_analog_route;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 tmp.count = ice->num_total_dacs;
2493 if (ice->vt1720 && tmp.count > 2)
2494 tmp.count = 2;
2495 err = snd_ctl_add(ice->card, snd_ctl_new1(&tmp, ice));
2496 if (err < 0)
2497 return err;
2498 }
2499
2500 err = snd_ctl_add(ice->card, snd_ctl_new1(&snd_vt1724_mixer_pro_peak, ice));
2501 if (err < 0)
2502 return err;
2503
2504 return 0;
2505}
2506
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002507static int snd_vt1724_free(struct snd_ice1712 *ice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508{
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002509 if (!ice->port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 goto __hw_end;
2511 /* mask all interrupts */
2512 outb(0xff, ICEMT1724(ice, DMA_INT_MASK));
2513 outb(0xff, ICEREG1724(ice, IRQMASK));
2514 /* --- */
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002515__hw_end:
Jeff Garzikf000fd82008-04-22 13:50:34 +02002516 if (ice->irq >= 0)
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002517 free_irq(ice->irq, ice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 pci_release_regions(ice->pci);
2519 snd_ice1712_akm4xxx_free(ice);
2520 pci_disable_device(ice->pci);
Takashi Iwai7cda8ba2008-01-18 13:36:07 +01002521 kfree(ice->spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 kfree(ice);
2523 return 0;
2524}
2525
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002526static int snd_vt1724_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002528 struct snd_ice1712 *ice = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 return snd_vt1724_free(ice);
2530}
2531
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002532static int __devinit snd_vt1724_create(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 struct pci_dev *pci,
2534 const char *modelname,
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002535 struct snd_ice1712 **r_ice1712)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536{
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002537 struct snd_ice1712 *ice;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 int err;
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002539 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 .dev_free = snd_vt1724_dev_free,
2541 };
2542
2543 *r_ice1712 = NULL;
2544
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002545 /* enable PCI device */
2546 err = pci_enable_device(pci);
2547 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 return err;
2549
Takashi Iwaie560d8d2005-09-09 14:21:46 +02002550 ice = kzalloc(sizeof(*ice), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 if (ice == NULL) {
2552 pci_disable_device(pci);
2553 return -ENOMEM;
2554 }
2555 ice->vt1724 = 1;
2556 spin_lock_init(&ice->reg_lock);
Ingo Molnar62932df2006-01-16 16:34:20 +01002557 mutex_init(&ice->gpio_mutex);
2558 mutex_init(&ice->open_mutex);
2559 mutex_init(&ice->i2c_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 ice->gpio.set_mask = snd_vt1724_set_gpio_mask;
Pavel Hofman49470302009-09-16 22:25:38 +02002561 ice->gpio.get_mask = snd_vt1724_get_gpio_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 ice->gpio.set_dir = snd_vt1724_set_gpio_dir;
Pavel Hofman49470302009-09-16 22:25:38 +02002563 ice->gpio.get_dir = snd_vt1724_get_gpio_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 ice->gpio.set_data = snd_vt1724_set_gpio_data;
2565 ice->gpio.get_data = snd_vt1724_get_gpio_data;
2566 ice->card = card;
2567 ice->pci = pci;
2568 ice->irq = -1;
2569 pci_set_master(pci);
2570 snd_vt1724_proc_init(ice);
2571 synchronize_irq(pci->irq);
2572
Igor Chernyshevb40e9532009-06-25 09:31:07 +02002573 card->private_data = ice;
2574
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002575 err = pci_request_regions(pci, "ICE1724");
2576 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 kfree(ice);
2578 pci_disable_device(pci);
2579 return err;
2580 }
2581 ice->port = pci_resource_start(pci, 0);
2582 ice->profi_port = pci_resource_start(pci, 1);
2583
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002584 if (request_irq(pci->irq, snd_vt1724_interrupt,
Takashi Iwai934c2b62011-06-10 16:36:37 +02002585 IRQF_SHARED, KBUILD_MODNAME, ice)) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02002586 snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 snd_vt1724_free(ice);
2588 return -EIO;
2589 }
2590
2591 ice->irq = pci->irq;
2592
Karsten Wiese988f0662008-04-22 12:52:15 +02002593 snd_vt1724_chip_reset(ice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 if (snd_vt1724_read_eeprom(ice, modelname) < 0) {
2595 snd_vt1724_free(ice);
2596 return -EIO;
2597 }
2598 if (snd_vt1724_chip_init(ice) < 0) {
2599 snd_vt1724_free(ice);
2600 return -EIO;
2601 }
2602
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002603 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, ice, &ops);
2604 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 snd_vt1724_free(ice);
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002606 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 }
2608
2609 snd_card_set_dev(card, &pci->dev);
2610
2611 *r_ice1712 = ice;
2612 return 0;
2613}
2614
2615
2616/*
2617 *
2618 * Registration
2619 *
2620 */
2621
2622static int __devinit snd_vt1724_probe(struct pci_dev *pci,
2623 const struct pci_device_id *pci_id)
2624{
2625 static int dev;
Takashi Iwaiab0c7d72005-11-17 15:00:18 +01002626 struct snd_card *card;
2627 struct snd_ice1712 *ice;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 int pcm_dev = 0, err;
Takashi Iwai1b60f6b2007-03-13 22:13:47 +01002629 struct snd_ice1712_card_info * const *tbl, *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630
2631 if (dev >= SNDRV_CARDS)
2632 return -ENODEV;
2633 if (!enable[dev]) {
2634 dev++;
2635 return -ENOENT;
2636 }
2637
Takashi Iwaie58de7b2008-12-28 16:44:30 +01002638 err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
2639 if (err < 0)
2640 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641
2642 strcpy(card->driver, "ICE1724");
2643 strcpy(card->shortname, "ICEnsemble ICE1724");
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002644
2645 err = snd_vt1724_create(card, pci, model[dev], &ice);
2646 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 snd_card_free(card);
2648 return err;
2649 }
2650
Pavel Hofman1ff97cb2009-09-16 22:25:40 +02002651 /* field init before calling chip_init */
2652 ice->ext_clock_count = 0;
2653
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 for (tbl = card_tables; *tbl; tbl++) {
Pavel Hofman2b151ef2012-01-10 20:28:47 +01002655 for (c = *tbl; c->name; c++) {
2656 if ((model[dev] && c->model &&
2657 !strcmp(model[dev], c->model)) ||
2658 (c->subvendor == ice->eeprom.subvendor)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 strcpy(card->shortname, c->name);
2660 if (c->driver) /* specific driver? */
2661 strcpy(card->driver, c->driver);
2662 if (c->chip_init) {
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002663 err = c->chip_init(ice);
2664 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 snd_card_free(card);
2666 return err;
2667 }
2668 }
2669 goto __found;
2670 }
2671 }
2672 }
2673 c = &no_matched;
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002674__found:
2675 /*
2676 * VT1724 has separate DMAs for the analog and the SPDIF streams while
2677 * ICE1712 has only one for both (mixed up).
2678 *
2679 * Confusingly the analog PCM is named "professional" here because it
2680 * was called so in ice1712 driver, and vt1724 driver is derived from
2681 * ice1712 driver.
2682 */
Pavel Hofmand16be8e2008-03-20 12:10:27 +01002683 ice->pro_rate_default = PRO_RATE_DEFAULT;
2684 if (!ice->is_spdif_master)
2685 ice->is_spdif_master = stdclock_is_spdif_master;
2686 if (!ice->get_rate)
2687 ice->get_rate = stdclock_get_rate;
2688 if (!ice->set_rate)
2689 ice->set_rate = stdclock_set_rate;
2690 if (!ice->set_mclk)
2691 ice->set_mclk = stdclock_set_mclk;
2692 if (!ice->set_spdif_clock)
2693 ice->set_spdif_clock = stdclock_set_spdif_clock;
Pavel Hofman1ff97cb2009-09-16 22:25:40 +02002694 if (!ice->get_spdif_master_type)
2695 ice->get_spdif_master_type = stdclock_get_spdif_master_type;
2696 if (!ice->ext_clock_names)
2697 ice->ext_clock_names = ext_clock_names;
2698 if (!ice->ext_clock_count)
2699 ice->ext_clock_count = ARRAY_SIZE(ext_clock_names);
2700
Pavel Hofmand16be8e2008-03-20 12:10:27 +01002701 if (!ice->hw_rates)
2702 set_std_hw_rates(ice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002704 err = snd_vt1724_pcm_profi(ice, pcm_dev++);
2705 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 snd_card_free(card);
2707 return err;
2708 }
2709
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002710 err = snd_vt1724_pcm_spdif(ice, pcm_dev++);
2711 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 snd_card_free(card);
2713 return err;
2714 }
2715
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002716 err = snd_vt1724_pcm_indep(ice, pcm_dev++);
2717 if (err < 0) {
2718 snd_card_free(card);
2719 return err;
2720 }
2721
2722 err = snd_vt1724_ac97_mixer(ice);
2723 if (err < 0) {
2724 snd_card_free(card);
2725 return err;
2726 }
2727
2728 err = snd_vt1724_build_controls(ice);
2729 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 snd_card_free(card);
2731 return err;
2732 }
2733
2734 if (ice->pcm && ice->has_spdif) { /* has SPDIF I/O */
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002735 err = snd_vt1724_spdif_build_controls(ice);
2736 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 snd_card_free(card);
2738 return err;
2739 }
2740 }
2741
2742 if (c->build_controls) {
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002743 err = c->build_controls(ice);
2744 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 snd_card_free(card);
2746 return err;
2747 }
2748 }
2749
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002750 if (!c->no_mpu401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 if (ice->eeprom.data[ICE_EEP2_SYSCONF] & VT1724_CFG_MPU401) {
Clemens Ladischaea3bfb2008-05-20 14:22:44 +02002752 struct snd_rawmidi *rmidi;
2753
2754 err = snd_rawmidi_new(card, "MIDI", 0, 1, 1, &rmidi);
2755 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 snd_card_free(card);
2757 return err;
2758 }
Clemens Ladischaea3bfb2008-05-20 14:22:44 +02002759 ice->rmidi[0] = rmidi;
2760 rmidi->private_data = ice;
2761 strcpy(rmidi->name, "ICE1724 MIDI");
2762 rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
2763 SNDRV_RAWMIDI_INFO_INPUT |
2764 SNDRV_RAWMIDI_INFO_DUPLEX;
2765 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
2766 &vt1724_midi_output_ops);
2767 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT,
2768 &vt1724_midi_input_ops);
2769
Takashi Iwai3a841d52008-04-23 17:47:28 +02002770 /* set watermarks */
2771 outb(VT1724_MPU_RX_FIFO | 0x1,
2772 ICEREG1724(ice, MPU_FIFO_WM));
2773 outb(0x1, ICEREG1724(ice, MPU_FIFO_WM));
Clemens Ladischaea3bfb2008-05-20 14:22:44 +02002774 /* set UART mode */
2775 outb(VT1724_MPU_UART, ICEREG1724(ice, MPU_CTRL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 }
2777 }
2778
2779 sprintf(card->longname, "%s at 0x%lx, irq %i",
2780 card->shortname, ice->port, ice->irq);
2781
Alexander Beregalov1de9fdc2008-09-07 14:11:04 +04002782 err = snd_card_register(card);
2783 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 snd_card_free(card);
2785 return err;
2786 }
2787 pci_set_drvdata(pci, card);
2788 dev++;
2789 return 0;
2790}
2791
2792static void __devexit snd_vt1724_remove(struct pci_dev *pci)
2793{
Ondrej Zary267bcca2012-10-14 21:09:19 +02002794 struct snd_card *card = pci_get_drvdata(pci);
2795 struct snd_ice1712 *ice = card->private_data;
2796
2797 if (ice->card_info && ice->card_info->chip_exit)
2798 ice->card_info->chip_exit(ice);
2799 snd_card_free(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 pci_set_drvdata(pci, NULL);
2801}
2802
Takashi Iwaic7561cd2012-08-14 18:12:04 +02002803#ifdef CONFIG_PM_SLEEP
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002804static int snd_vt1724_suspend(struct device *dev)
Igor Chernyshevb40e9532009-06-25 09:31:07 +02002805{
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002806 struct pci_dev *pci = to_pci_dev(dev);
2807 struct snd_card *card = dev_get_drvdata(dev);
Igor Chernyshevb40e9532009-06-25 09:31:07 +02002808 struct snd_ice1712 *ice = card->private_data;
2809
2810 if (!ice->pm_suspend_enabled)
2811 return 0;
2812
2813 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
2814
2815 snd_pcm_suspend_all(ice->pcm);
2816 snd_pcm_suspend_all(ice->pcm_pro);
2817 snd_pcm_suspend_all(ice->pcm_ds);
2818 snd_ac97_suspend(ice->ac97);
2819
2820 spin_lock_irq(&ice->reg_lock);
2821 ice->pm_saved_is_spdif_master = ice->is_spdif_master(ice);
2822 ice->pm_saved_spdif_ctrl = inw(ICEMT1724(ice, SPDIF_CTRL));
2823 ice->pm_saved_spdif_cfg = inb(ICEREG1724(ice, SPDIF_CFG));
2824 ice->pm_saved_route = inl(ICEMT1724(ice, ROUTE_PLAYBACK));
2825 spin_unlock_irq(&ice->reg_lock);
2826
2827 if (ice->pm_suspend)
2828 ice->pm_suspend(ice);
2829
2830 pci_disable_device(pci);
2831 pci_save_state(pci);
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002832 pci_set_power_state(pci, PCI_D3hot);
Igor Chernyshevb40e9532009-06-25 09:31:07 +02002833 return 0;
2834}
2835
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002836static int snd_vt1724_resume(struct device *dev)
Igor Chernyshevb40e9532009-06-25 09:31:07 +02002837{
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002838 struct pci_dev *pci = to_pci_dev(dev);
2839 struct snd_card *card = dev_get_drvdata(dev);
Igor Chernyshevb40e9532009-06-25 09:31:07 +02002840 struct snd_ice1712 *ice = card->private_data;
2841
2842 if (!ice->pm_suspend_enabled)
2843 return 0;
2844
2845 pci_set_power_state(pci, PCI_D0);
2846 pci_restore_state(pci);
2847
2848 if (pci_enable_device(pci) < 0) {
2849 snd_card_disconnect(card);
2850 return -EIO;
2851 }
2852
2853 pci_set_master(pci);
2854
2855 snd_vt1724_chip_reset(ice);
2856
2857 if (snd_vt1724_chip_init(ice) < 0) {
2858 snd_card_disconnect(card);
2859 return -EIO;
2860 }
2861
2862 if (ice->pm_resume)
2863 ice->pm_resume(ice);
2864
2865 if (ice->pm_saved_is_spdif_master) {
2866 /* switching to external clock via SPDIF */
Pavel Hofman1ff97cb2009-09-16 22:25:40 +02002867 ice->set_spdif_clock(ice, 0);
Igor Chernyshevb40e9532009-06-25 09:31:07 +02002868 } else {
2869 /* internal on-card clock */
2870 snd_vt1724_set_pro_rate(ice, ice->pro_rate_default, 1);
2871 }
2872
2873 update_spdif_bits(ice, ice->pm_saved_spdif_ctrl);
2874
2875 outb(ice->pm_saved_spdif_cfg, ICEREG1724(ice, SPDIF_CFG));
2876 outl(ice->pm_saved_route, ICEMT1724(ice, ROUTE_PLAYBACK));
2877
2878 if (ice->ac97)
2879 snd_ac97_resume(ice->ac97);
2880
2881 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
2882 return 0;
2883}
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002884
2885static SIMPLE_DEV_PM_OPS(snd_vt1724_pm, snd_vt1724_suspend, snd_vt1724_resume);
2886#define SND_VT1724_PM_OPS &snd_vt1724_pm
2887#else
2888#define SND_VT1724_PM_OPS NULL
Takashi Iwaic7561cd2012-08-14 18:12:04 +02002889#endif /* CONFIG_PM_SLEEP */
Igor Chernyshevb40e9532009-06-25 09:31:07 +02002890
Takashi Iwaie9f66d92012-04-24 12:25:00 +02002891static struct pci_driver vt1724_driver = {
Takashi Iwai3733e422011-06-10 16:20:20 +02002892 .name = KBUILD_MODNAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 .id_table = snd_vt1724_ids,
2894 .probe = snd_vt1724_probe,
2895 .remove = __devexit_p(snd_vt1724_remove),
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002896 .driver = {
2897 .pm = SND_VT1724_PM_OPS,
2898 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899};
2900
Takashi Iwaie9f66d92012-04-24 12:25:00 +02002901module_pci_driver(vt1724_driver);