blob: 2414ee6307565861c872b0b498f518d1c959ca3c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * azt3328.c - driver for Aztech AZF3328 based soundcards (e.g. PCI168).
Andreas Mohrd91c64c2005-10-25 11:17:45 +02003 * Copyright (C) 2002, 2005 by Andreas Mohr <andi AT lisas.de>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Framework borrowed from Bart Hartgers's als4000.c.
6 * Driver developed on PCI168 AP(W) version (PCI rev. 10, subsystem ID 1801),
7 * found in a Fujitsu-Siemens PC ("Cordant", aluminum case).
8 * Other versions are:
9 * PCI168 A(W), sub ID 1800
10 * PCI168 A/AP, sub ID 8000
11 * Please give me feedback in case you try my driver with one of these!!
12 *
13 * GPL LICENSE
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 *
28 * NOTES
29 * Since Aztech does not provide any chipset documentation,
30 * even on repeated request to various addresses,
31 * and the answer that was finally given was negative
32 * (and I was stupid enough to manage to get hold of a PCI168 soundcard
33 * in the first place >:-P}),
34 * I was forced to base this driver on reverse engineering
35 * (3 weeks' worth of evenings filled with driver work).
Andreas Mohre2f87262006-05-17 11:04:19 +020036 * (and no, I did NOT go the easy way: to pick up a SB PCI128 for 9 Euros)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 *
38 * The AZF3328 chip (note: AZF3328, *not* AZT3328, that's just the driver name
39 * for compatibility reasons) has the following features:
40 *
41 * - builtin AC97 conformant codec (SNR over 80dB)
Andreas Mohr13769e32006-05-17 11:03:16 +020042 * Note that "conformant" != "compliant"!! this chip's mixer register layout
43 * *differs* from the standard AC97 layout:
44 * they chose to not implement the headphone register (which is not a
45 * problem since it's merely optional), yet when doing this, they committed
46 * the grave sin of letting other registers follow immediately instead of
47 * keeping a headphone dummy register, thereby shifting the mixer register
48 * addresses illegally. So far unfortunately it looks like the very flexible
49 * ALSA AC97 support is still not enough to easily compensate for such a
50 * grave layout violation despite all tweaks and quirks mechanisms it offers.
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 * - builtin genuine OPL3
52 * - full duplex 16bit playback/record at independent sampling rate
53 * - MPU401 (+ legacy address support) FIXME: how to enable legacy addr??
54 * - game port (legacy address support)
55 * - built-in General DirectX timer having a 20 bits counter
Andreas Mohrd91c64c2005-10-25 11:17:45 +020056 * with 1us resolution (see below!)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 * - I2S serial port for external DAC
58 * - supports 33MHz PCI spec 2.1, PCI power management 1.0, compliant with ACPI
59 * - supports hardware volume control
60 * - single chip low cost solution (128 pin QFP)
61 * - supports programmable Sub-vendor and Sub-system ID
62 * required for Microsoft's logo compliance (FIXME: where?)
63 * - PCI168 AP(W) card: power amplifier with 4 Watts/channel at 4 Ohms
64 *
Andreas Mohrd91c64c2005-10-25 11:17:45 +020065 * Note that this driver now is actually *better* than the Windows driver,
66 * since it additionally supports the card's 1MHz DirectX timer - just try
67 * the following snd-seq module parameters etc.:
68 * - options snd-seq seq_default_timer_class=2 seq_default_timer_sclass=0
69 * seq_default_timer_card=0 seq_client_load=1 seq_default_timer_device=0
70 * seq_default_timer_subdevice=0 seq_default_timer_resolution=1000000
71 * - "timidity -iAv -B2,8 -Os -EFreverb=0"
72 * - "pmidi -p 128:0 jazz.mid"
73 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 * Certain PCI versions of this card are susceptible to DMA traffic underruns
75 * in some systems (resulting in sound crackling/clicking/popping),
76 * probably because they don't have a DMA FIFO buffer or so.
77 * Overview (PCI ID/PCI subID/PCI rev.):
78 * - no DMA crackling on SiS735: 0x50DC/0x1801/16
79 * - unknown performance: 0x50DC/0x1801/10
Andreas Mohrd91c64c2005-10-25 11:17:45 +020080 * (well, it's not bad on an Athlon 1800 with now very optimized IRQ handler)
81 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 * Crackling happens with VIA chipsets or, in my case, an SiS735, which is
83 * supposed to be very fast and supposed to get rid of crackling much
84 * better than a VIA, yet ironically I still get crackling, like many other
85 * people with the same chipset.
86 * Possible remedies:
87 * - plug card into a different PCI slot, preferrably one that isn't shared
88 * too much (this helps a lot, but not completely!)
89 * - get rid of PCI VGA card, use AGP instead
90 * - upgrade or downgrade BIOS
91 * - fiddle with PCI latency settings (setpci -v -s BUSID latency_timer=XX)
92 * Not too helpful.
93 * - Disable ACPI/power management/"Auto Detect RAM/PCI Clk" in BIOS
94 *
95 * BUGS
Andreas Mohrd91c64c2005-10-25 11:17:45 +020096 * - full-duplex might *still* be problematic, not fully tested recently
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 *
98 * TODO
99 * - test MPU401 MIDI playback etc.
Andreas Mohrca54bde2006-05-17 11:02:24 +0200100 * - add some power micro-management (disable various units of the card
101 * as long as they're unused). However this requires I/O ports which I
102 * haven't figured out yet and which thus might not even exist...
103 * The standard suspend/resume functionality could probably make use of
104 * some improvement, too...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 * - figure out what all unknown port bits are responsible for
Andreas Mohr13769e32006-05-17 11:03:16 +0200106 * - figure out some cleverly evil scheme to possibly make ALSA AC97 code
107 * fully accept our quite incompatible ""AC97"" mixer and thus save some
108 * code (but I'm not too optimistic that doing this is possible at all)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 */
110
111#include <sound/driver.h>
112#include <asm/io.h>
113#include <linux/init.h>
114#include <linux/pci.h>
115#include <linux/delay.h>
116#include <linux/slab.h>
117#include <linux/gameport.h>
118#include <linux/moduleparam.h>
Matthias Gehre910638a2006-03-28 01:56:48 -0800119#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120#include <sound/core.h>
121#include <sound/control.h>
122#include <sound/pcm.h>
123#include <sound/rawmidi.h>
124#include <sound/mpu401.h>
125#include <sound/opl3.h>
126#include <sound/initval.h>
127#include "azt3328.h"
128
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200129MODULE_AUTHOR("Andreas Mohr <andi AT lisas.de>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130MODULE_DESCRIPTION("Aztech AZF3328 (PCI168)");
131MODULE_LICENSE("GPL");
132MODULE_SUPPORTED_DEVICE("{{Aztech,AZF3328}}");
133
134#if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
135#define SUPPORT_JOYSTICK 1
136#endif
137
138#define DEBUG_MISC 0
139#define DEBUG_CALLS 0
140#define DEBUG_MIXER 0
141#define DEBUG_PLAY_REC 0
142#define DEBUG_IO 0
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200143#define DEBUG_TIMER 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144#define MIXER_TESTING 0
145
146#if DEBUG_MISC
147#define snd_azf3328_dbgmisc(format, args...) printk(KERN_ERR format, ##args)
148#else
149#define snd_azf3328_dbgmisc(format, args...)
150#endif
151
152#if DEBUG_CALLS
153#define snd_azf3328_dbgcalls(format, args...) printk(format, ##args)
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200154#define snd_azf3328_dbgcallenter() printk(KERN_ERR "--> %s\n", __FUNCTION__)
155#define snd_azf3328_dbgcallleave() printk(KERN_ERR "<-- %s\n", __FUNCTION__)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156#else
157#define snd_azf3328_dbgcalls(format, args...)
158#define snd_azf3328_dbgcallenter()
159#define snd_azf3328_dbgcallleave()
160#endif
161
162#if DEBUG_MIXER
163#define snd_azf3328_dbgmixer(format, args...) printk(format, ##args)
164#else
165#define snd_azf3328_dbgmixer(format, args...)
166#endif
167
168#if DEBUG_PLAY_REC
169#define snd_azf3328_dbgplay(format, args...) printk(KERN_ERR format, ##args)
170#else
171#define snd_azf3328_dbgplay(format, args...)
172#endif
173
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200174#if DEBUG_MISC
175#define snd_azf3328_dbgtimer(format, args...) printk(KERN_ERR format, ##args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176#else
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200177#define snd_azf3328_dbgtimer(format, args...)
178#endif
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
181module_param_array(index, int, NULL, 0444);
182MODULE_PARM_DESC(index, "Index value for AZF3328 soundcard.");
183
184static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
185module_param_array(id, charp, NULL, 0444);
186MODULE_PARM_DESC(id, "ID string for AZF3328 soundcard.");
187
188static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
189module_param_array(enable, bool, NULL, 0444);
190MODULE_PARM_DESC(enable, "Enable AZF3328 soundcard.");
191
192#ifdef SUPPORT_JOYSTICK
193static int joystick[SNDRV_CARDS];
194module_param_array(joystick, bool, NULL, 0444);
195MODULE_PARM_DESC(joystick, "Enable joystick for AZF3328 soundcard.");
196#endif
197
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200198static int seqtimer_scaling = 128;
199module_param(seqtimer_scaling, int, 0444);
200MODULE_PARM_DESC(seqtimer_scaling, "Set 1024000Hz sequencer timer scale factor (lockup danger!). Default 128.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Takashi Iwai95de7762005-11-17 15:02:42 +0100202struct snd_azf3328 {
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200203 /* often-used fields towards beginning, then grouped */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 unsigned long codec_port;
205 unsigned long io2_port;
206 unsigned long mpu_port;
207 unsigned long synth_port;
208 unsigned long mixer_port;
209
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200210 spinlock_t reg_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Takashi Iwai95de7762005-11-17 15:02:42 +0100212 struct snd_timer *timer;
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200213
Takashi Iwai95de7762005-11-17 15:02:42 +0100214 struct snd_pcm *pcm;
215 struct snd_pcm_substream *playback_substream;
216 struct snd_pcm_substream *capture_substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 unsigned int is_playing;
218 unsigned int is_recording;
219
Takashi Iwai95de7762005-11-17 15:02:42 +0100220 struct snd_card *card;
221 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200223#ifdef SUPPORT_JOYSTICK
224 struct gameport *gameport;
225#endif
226
227 struct pci_dev *pci;
228 int irq;
Andreas Mohrca54bde2006-05-17 11:02:24 +0200229
230#ifdef CONFIG_PM
231 /* register value containers for power management
232 * Note: not always full I/O range preserved (just like Win driver!) */
233 u16 saved_regs_codec [AZF_IO_SIZE_CODEC_PM / 2];
234 u16 saved_regs_io2 [AZF_IO_SIZE_IO2_PM / 2];
235 u16 saved_regs_mpu [AZF_IO_SIZE_MPU_PM / 2];
236 u16 saved_regs_synth[AZF_IO_SIZE_SYNTH_PM / 2];
237 u16 saved_regs_mixer[AZF_IO_SIZE_MIXER_PM / 2];
238#endif
Takashi Iwai95de7762005-11-17 15:02:42 +0100239};
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200240
Takashi Iwaif40b6892006-07-05 16:51:05 +0200241static const struct pci_device_id snd_azf3328_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 { 0x122D, 0x50DC, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, /* PCI168/3328 */
243 { 0x122D, 0x80DA, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, /* 3328 */
244 { 0, }
245};
246
247MODULE_DEVICE_TABLE(pci, snd_azf3328_ids);
248
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200249static inline void
Takashi Iwai95de7762005-11-17 15:02:42 +0100250snd_azf3328_codec_outb(const struct snd_azf3328 *chip, int reg, u8 value)
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200251{
252 outb(value, chip->codec_port + reg);
253}
254
255static inline u8
Takashi Iwai95de7762005-11-17 15:02:42 +0100256snd_azf3328_codec_inb(const struct snd_azf3328 *chip, int reg)
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200257{
258 return inb(chip->codec_port + reg);
259}
260
261static inline void
Takashi Iwai95de7762005-11-17 15:02:42 +0100262snd_azf3328_codec_outw(const struct snd_azf3328 *chip, int reg, u16 value)
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200263{
264 outw(value, chip->codec_port + reg);
265}
266
267static inline u16
Takashi Iwai95de7762005-11-17 15:02:42 +0100268snd_azf3328_codec_inw(const struct snd_azf3328 *chip, int reg)
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200269{
270 return inw(chip->codec_port + reg);
271}
272
273static inline void
Takashi Iwai95de7762005-11-17 15:02:42 +0100274snd_azf3328_codec_outl(const struct snd_azf3328 *chip, int reg, u32 value)
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200275{
276 outl(value, chip->codec_port + reg);
277}
278
279static inline void
Takashi Iwai95de7762005-11-17 15:02:42 +0100280snd_azf3328_io2_outb(const struct snd_azf3328 *chip, int reg, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
282 outb(value, chip->io2_port + reg);
283}
284
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200285static inline u8
Takashi Iwai95de7762005-11-17 15:02:42 +0100286snd_azf3328_io2_inb(const struct snd_azf3328 *chip, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
288 return inb(chip->io2_port + reg);
289}
290
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200291static inline void
Takashi Iwai95de7762005-11-17 15:02:42 +0100292snd_azf3328_mixer_outw(const struct snd_azf3328 *chip, int reg, u16 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200294 outw(value, chip->mixer_port + reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200297static inline u16
Takashi Iwai95de7762005-11-17 15:02:42 +0100298snd_azf3328_mixer_inw(const struct snd_azf3328 *chip, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200300 return inw(chip->mixer_port + reg);
301}
302
303static void
Takashi Iwai95de7762005-11-17 15:02:42 +0100304snd_azf3328_mixer_set_mute(const struct snd_azf3328 *chip, int reg, int do_mute)
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200305{
306 unsigned long portbase = chip->mixer_port + reg + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 unsigned char oldval;
308
309 /* the mute bit is on the *second* (i.e. right) register of a
310 * left/right channel setting */
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200311 oldval = inb(portbase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 if (do_mute)
313 oldval |= 0x80;
314 else
315 oldval &= ~0x80;
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200316 outb(oldval, portbase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200319static void
Takashi Iwai95de7762005-11-17 15:02:42 +0100320snd_azf3328_mixer_write_volume_gradually(const struct snd_azf3328 *chip, int reg, unsigned char dst_vol_left, unsigned char dst_vol_right, int chan_sel, int delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200322 unsigned long portbase = chip->mixer_port + reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 unsigned char curr_vol_left = 0, curr_vol_right = 0;
324 int left_done = 0, right_done = 0;
325
326 snd_azf3328_dbgcallenter();
327 if (chan_sel & SET_CHAN_LEFT)
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200328 curr_vol_left = inb(portbase + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 else
330 left_done = 1;
331 if (chan_sel & SET_CHAN_RIGHT)
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200332 curr_vol_right = inb(portbase + 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 else
334 right_done = 1;
335
336 /* take care of muting flag (0x80) contained in left channel */
337 if (curr_vol_left & 0x80)
338 dst_vol_left |= 0x80;
339 else
340 dst_vol_left &= ~0x80;
341
Andreas Mohre2f87262006-05-17 11:04:19 +0200342 do {
343 if (!left_done) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 if (curr_vol_left > dst_vol_left)
345 curr_vol_left--;
346 else
347 if (curr_vol_left < dst_vol_left)
348 curr_vol_left++;
349 else
350 left_done = 1;
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200351 outb(curr_vol_left, portbase + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 }
Andreas Mohre2f87262006-05-17 11:04:19 +0200353 if (!right_done) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 if (curr_vol_right > dst_vol_right)
355 curr_vol_right--;
356 else
357 if (curr_vol_right < dst_vol_right)
358 curr_vol_right++;
359 else
360 right_done = 1;
361 /* during volume change, the right channel is crackling
362 * somewhat more than the left channel, unfortunately.
363 * This seems to be a hardware issue. */
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200364 outb(curr_vol_right, portbase + 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 }
366 if (delay)
367 mdelay(delay);
Andreas Mohre2f87262006-05-17 11:04:19 +0200368 } while ((!left_done) || (!right_done));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 snd_azf3328_dbgcallleave();
370}
371
372/*
373 * general mixer element
374 */
Takashi Iwai95de7762005-11-17 15:02:42 +0100375struct azf3328_mixer_reg {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 unsigned int reg;
377 unsigned int lchan_shift, rchan_shift;
378 unsigned int mask;
379 unsigned int invert: 1;
380 unsigned int stereo: 1;
381 unsigned int enum_c: 4;
Takashi Iwai95de7762005-11-17 15:02:42 +0100382};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384#define COMPOSE_MIXER_REG(reg,lchan_shift,rchan_shift,mask,invert,stereo,enum_c) \
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200385 ((reg) | (lchan_shift << 8) | (rchan_shift << 12) | \
386 (mask << 16) | \
387 (invert << 24) | \
388 (stereo << 25) | \
389 (enum_c << 26))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Takashi Iwai95de7762005-11-17 15:02:42 +0100391static void snd_azf3328_mixer_reg_decode(struct azf3328_mixer_reg *r, unsigned long val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
393 r->reg = val & 0xff;
394 r->lchan_shift = (val >> 8) & 0x0f;
395 r->rchan_shift = (val >> 12) & 0x0f;
396 r->mask = (val >> 16) & 0xff;
397 r->invert = (val >> 24) & 1;
398 r->stereo = (val >> 25) & 1;
399 r->enum_c = (val >> 26) & 0x0f;
400}
401
402/*
403 * mixer switches/volumes
404 */
405
406#define AZF3328_MIXER_SWITCH(xname, reg, shift, invert) \
407{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
408 .info = snd_azf3328_info_mixer, \
409 .get = snd_azf3328_get_mixer, .put = snd_azf3328_put_mixer, \
410 .private_value = COMPOSE_MIXER_REG(reg, shift, 0, 0x1, invert, 0, 0), \
411}
412
413#define AZF3328_MIXER_VOL_STEREO(xname, reg, mask, invert) \
414{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
415 .info = snd_azf3328_info_mixer, \
416 .get = snd_azf3328_get_mixer, .put = snd_azf3328_put_mixer, \
417 .private_value = COMPOSE_MIXER_REG(reg, 8, 0, mask, invert, 1, 0), \
418}
419
420#define AZF3328_MIXER_VOL_MONO(xname, reg, mask, is_right_chan) \
421{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
422 .info = snd_azf3328_info_mixer, \
423 .get = snd_azf3328_get_mixer, .put = snd_azf3328_put_mixer, \
424 .private_value = COMPOSE_MIXER_REG(reg, is_right_chan ? 0 : 8, 0, mask, 1, 0, 0), \
425}
426
427#define AZF3328_MIXER_VOL_SPECIAL(xname, reg, mask, shift, invert) \
428{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
429 .info = snd_azf3328_info_mixer, \
430 .get = snd_azf3328_get_mixer, .put = snd_azf3328_put_mixer, \
431 .private_value = COMPOSE_MIXER_REG(reg, shift, 0, mask, invert, 0, 0), \
432}
433
434#define AZF3328_MIXER_ENUM(xname, reg, enum_c, shift) \
435{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
436 .info = snd_azf3328_info_mixer_enum, \
437 .get = snd_azf3328_get_mixer_enum, .put = snd_azf3328_put_mixer_enum, \
438 .private_value = COMPOSE_MIXER_REG(reg, shift, 0, 0, 0, 0, enum_c), \
439}
440
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200441static int
Takashi Iwai95de7762005-11-17 15:02:42 +0100442snd_azf3328_info_mixer(struct snd_kcontrol *kcontrol,
443 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
Takashi Iwai95de7762005-11-17 15:02:42 +0100445 struct azf3328_mixer_reg reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 snd_azf3328_dbgcallenter();
448 snd_azf3328_mixer_reg_decode(&reg, kcontrol->private_value);
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200449 uinfo->type = reg.mask == 1 ?
450 SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 uinfo->count = reg.stereo + 1;
452 uinfo->value.integer.min = 0;
453 uinfo->value.integer.max = reg.mask;
454 snd_azf3328_dbgcallleave();
455 return 0;
456}
457
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200458static int
Takashi Iwai95de7762005-11-17 15:02:42 +0100459snd_azf3328_get_mixer(struct snd_kcontrol *kcontrol,
460 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
Takashi Iwai95de7762005-11-17 15:02:42 +0100462 struct snd_azf3328 *chip = snd_kcontrol_chip(kcontrol);
463 struct azf3328_mixer_reg reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 unsigned int oreg, val;
465
466 snd_azf3328_dbgcallenter();
467 snd_azf3328_mixer_reg_decode(&reg, kcontrol->private_value);
468
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200469 oreg = snd_azf3328_mixer_inw(chip, reg.reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 val = (oreg >> reg.lchan_shift) & reg.mask;
471 if (reg.invert)
472 val = reg.mask - val;
473 ucontrol->value.integer.value[0] = val;
474 if (reg.stereo) {
475 val = (oreg >> reg.rchan_shift) & reg.mask;
476 if (reg.invert)
477 val = reg.mask - val;
478 ucontrol->value.integer.value[1] = val;
479 }
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200480 snd_azf3328_dbgmixer("get: %02x is %04x -> vol %02lx|%02lx "
481 "(shift %02d|%02d, mask %02x, inv. %d, stereo %d)\n",
482 reg.reg, oreg,
483 ucontrol->value.integer.value[0], ucontrol->value.integer.value[1],
484 reg.lchan_shift, reg.rchan_shift, reg.mask, reg.invert, reg.stereo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 snd_azf3328_dbgcallleave();
486 return 0;
487}
488
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200489static int
Takashi Iwai95de7762005-11-17 15:02:42 +0100490snd_azf3328_put_mixer(struct snd_kcontrol *kcontrol,
491 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
Takashi Iwai95de7762005-11-17 15:02:42 +0100493 struct snd_azf3328 *chip = snd_kcontrol_chip(kcontrol);
494 struct azf3328_mixer_reg reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 unsigned int oreg, nreg, val;
496
497 snd_azf3328_dbgcallenter();
498 snd_azf3328_mixer_reg_decode(&reg, kcontrol->private_value);
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200499 oreg = snd_azf3328_mixer_inw(chip, reg.reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 val = ucontrol->value.integer.value[0] & reg.mask;
501 if (reg.invert)
502 val = reg.mask - val;
503 nreg = oreg & ~(reg.mask << reg.lchan_shift);
504 nreg |= (val << reg.lchan_shift);
505 if (reg.stereo) {
506 val = ucontrol->value.integer.value[1] & reg.mask;
507 if (reg.invert)
508 val = reg.mask - val;
509 nreg &= ~(reg.mask << reg.rchan_shift);
510 nreg |= (val << reg.rchan_shift);
511 }
512 if (reg.mask >= 0x07) /* it's a volume control, so better take care */
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200513 snd_azf3328_mixer_write_volume_gradually(
514 chip, reg.reg, nreg >> 8, nreg & 0xff,
515 /* just set both channels, doesn't matter */
516 SET_CHAN_LEFT|SET_CHAN_RIGHT,
517 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 else
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200519 snd_azf3328_mixer_outw(chip, reg.reg, nreg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200521 snd_azf3328_dbgmixer("put: %02x to %02lx|%02lx, "
522 "oreg %04x; shift %02d|%02d -> nreg %04x; after: %04x\n",
523 reg.reg, ucontrol->value.integer.value[0], ucontrol->value.integer.value[1],
524 oreg, reg.lchan_shift, reg.rchan_shift,
525 nreg, snd_azf3328_mixer_inw(chip, reg.reg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 snd_azf3328_dbgcallleave();
527 return (nreg != oreg);
528}
529
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200530static int
Takashi Iwai95de7762005-11-17 15:02:42 +0100531snd_azf3328_info_mixer_enum(struct snd_kcontrol *kcontrol,
532 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200534 static const char * const texts1[] = {
Andreas Mohr13769e32006-05-17 11:03:16 +0200535 "Mic1", "Mic2"
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200536 };
537 static const char * const texts2[] = {
Andreas Mohr13769e32006-05-17 11:03:16 +0200538 "Mix", "Mic"
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200539 };
540 static const char * const texts3[] = {
541 "Mic", "CD", "Video", "Aux",
542 "Line", "Mix", "Mix Mono", "Phone"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 };
Andreas Mohr13769e32006-05-17 11:03:16 +0200544 static const char * const texts4[] = {
545 "pre 3D", "post 3D"
546 };
Takashi Iwai95de7762005-11-17 15:02:42 +0100547 struct azf3328_mixer_reg reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549 snd_azf3328_mixer_reg_decode(&reg, kcontrol->private_value);
550 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
551 uinfo->count = (reg.reg == IDX_MIXER_REC_SELECT) ? 2 : 1;
552 uinfo->value.enumerated.items = reg.enum_c;
553 if (uinfo->value.enumerated.item > reg.enum_c - 1U)
554 uinfo->value.enumerated.item = reg.enum_c - 1U;
Andreas Mohre2f87262006-05-17 11:04:19 +0200555 if (reg.reg == IDX_MIXER_ADVCTL2) {
Andreas Mohr13769e32006-05-17 11:03:16 +0200556 switch(reg.lchan_shift) {
557 case 8: /* modem out sel */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 strcpy(uinfo->value.enumerated.name, texts1[uinfo->value.enumerated.item]);
Andreas Mohr13769e32006-05-17 11:03:16 +0200559 break;
560 case 9: /* mono sel source */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 strcpy(uinfo->value.enumerated.name, texts2[uinfo->value.enumerated.item]);
Andreas Mohr13769e32006-05-17 11:03:16 +0200562 break;
563 case 15: /* PCM Out Path */
564 strcpy(uinfo->value.enumerated.name, texts4[uinfo->value.enumerated.item]);
565 break;
566 }
Andreas Mohre2f87262006-05-17 11:04:19 +0200567 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 strcpy(uinfo->value.enumerated.name, texts3[uinfo->value.enumerated.item]
569);
570 return 0;
571}
572
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200573static int
Takashi Iwai95de7762005-11-17 15:02:42 +0100574snd_azf3328_get_mixer_enum(struct snd_kcontrol *kcontrol,
575 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
Takashi Iwai95de7762005-11-17 15:02:42 +0100577 struct snd_azf3328 *chip = snd_kcontrol_chip(kcontrol);
578 struct azf3328_mixer_reg reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 unsigned short val;
580
581 snd_azf3328_mixer_reg_decode(&reg, kcontrol->private_value);
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200582 val = snd_azf3328_mixer_inw(chip, reg.reg);
Andreas Mohre2f87262006-05-17 11:04:19 +0200583 if (reg.reg == IDX_MIXER_REC_SELECT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 ucontrol->value.enumerated.item[0] = (val >> 8) & (reg.enum_c - 1);
585 ucontrol->value.enumerated.item[1] = (val >> 0) & (reg.enum_c - 1);
Andreas Mohre2f87262006-05-17 11:04:19 +0200586 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 ucontrol->value.enumerated.item[0] = (val >> reg.lchan_shift) & (reg.enum_c - 1);
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200588
589 snd_azf3328_dbgmixer("get_enum: %02x is %04x -> %d|%d (shift %02d, enum_c %d)\n",
590 reg.reg, val, ucontrol->value.enumerated.item[0], ucontrol->value.enumerated.item[1],
591 reg.lchan_shift, reg.enum_c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 return 0;
593}
594
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200595static int
Takashi Iwai95de7762005-11-17 15:02:42 +0100596snd_azf3328_put_mixer_enum(struct snd_kcontrol *kcontrol,
597 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
Takashi Iwai95de7762005-11-17 15:02:42 +0100599 struct snd_azf3328 *chip = snd_kcontrol_chip(kcontrol);
600 struct azf3328_mixer_reg reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 unsigned int oreg, nreg, val;
602
603 snd_azf3328_mixer_reg_decode(&reg, kcontrol->private_value);
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200604 oreg = snd_azf3328_mixer_inw(chip, reg.reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 val = oreg;
Andreas Mohre2f87262006-05-17 11:04:19 +0200606 if (reg.reg == IDX_MIXER_REC_SELECT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 if (ucontrol->value.enumerated.item[0] > reg.enum_c - 1U ||
608 ucontrol->value.enumerated.item[1] > reg.enum_c - 1U)
609 return -EINVAL;
610 val = (ucontrol->value.enumerated.item[0] << 8) |
611 (ucontrol->value.enumerated.item[1] << 0);
Andreas Mohre2f87262006-05-17 11:04:19 +0200612 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 if (ucontrol->value.enumerated.item[0] > reg.enum_c - 1U)
614 return -EINVAL;
615 val &= ~((reg.enum_c - 1) << reg.lchan_shift);
616 val |= (ucontrol->value.enumerated.item[0] << reg.lchan_shift);
617 }
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200618 snd_azf3328_mixer_outw(chip, reg.reg, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 nreg = val;
620
621 snd_azf3328_dbgmixer("put_enum: %02x to %04x, oreg %04x\n", reg.reg, val, oreg);
622 return (nreg != oreg);
623}
624
Takashi Iwai95de7762005-11-17 15:02:42 +0100625static const struct snd_kcontrol_new snd_azf3328_mixer_controls[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 AZF3328_MIXER_SWITCH("Master Playback Switch", IDX_MIXER_PLAY_MASTER, 15, 1),
627 AZF3328_MIXER_VOL_STEREO("Master Playback Volume", IDX_MIXER_PLAY_MASTER, 0x1f, 1),
628 AZF3328_MIXER_SWITCH("Wave Playback Switch", IDX_MIXER_WAVEOUT, 15, 1),
629 AZF3328_MIXER_VOL_STEREO("Wave Playback Volume", IDX_MIXER_WAVEOUT, 0x1f, 1),
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200630 AZF3328_MIXER_SWITCH("Wave 3D Bypass Playback Switch", IDX_MIXER_ADVCTL2, 7, 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 AZF3328_MIXER_SWITCH("FM Playback Switch", IDX_MIXER_FMSYNTH, 15, 1),
632 AZF3328_MIXER_VOL_STEREO("FM Playback Volume", IDX_MIXER_FMSYNTH, 0x1f, 1),
633 AZF3328_MIXER_SWITCH("CD Playback Switch", IDX_MIXER_CDAUDIO, 15, 1),
634 AZF3328_MIXER_VOL_STEREO("CD Playback Volume", IDX_MIXER_CDAUDIO, 0x1f, 1),
635 AZF3328_MIXER_SWITCH("Capture Switch", IDX_MIXER_REC_VOLUME, 15, 1),
636 AZF3328_MIXER_VOL_STEREO("Capture Volume", IDX_MIXER_REC_VOLUME, 0x0f, 0),
637 AZF3328_MIXER_ENUM("Capture Source", IDX_MIXER_REC_SELECT, 8, 0),
638 AZF3328_MIXER_SWITCH("Mic Playback Switch", IDX_MIXER_MIC, 15, 1),
639 AZF3328_MIXER_VOL_MONO("Mic Playback Volume", IDX_MIXER_MIC, 0x1f, 1),
640 AZF3328_MIXER_SWITCH("Mic Boost (+20dB)", IDX_MIXER_MIC, 6, 0),
641 AZF3328_MIXER_SWITCH("Line Playback Switch", IDX_MIXER_LINEIN, 15, 1),
642 AZF3328_MIXER_VOL_STEREO("Line Playback Volume", IDX_MIXER_LINEIN, 0x1f, 1),
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200643 AZF3328_MIXER_SWITCH("PC Speaker Playback Switch", IDX_MIXER_PCBEEP, 15, 1),
644 AZF3328_MIXER_VOL_SPECIAL("PC Speaker Playback Volume", IDX_MIXER_PCBEEP, 0x0f, 1, 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 AZF3328_MIXER_SWITCH("Video Playback Switch", IDX_MIXER_VIDEO, 15, 1),
646 AZF3328_MIXER_VOL_STEREO("Video Playback Volume", IDX_MIXER_VIDEO, 0x1f, 1),
647 AZF3328_MIXER_SWITCH("Aux Playback Switch", IDX_MIXER_AUX, 15, 1),
648 AZF3328_MIXER_VOL_STEREO("Aux Playback Volume", IDX_MIXER_AUX, 0x1f, 1),
649 AZF3328_MIXER_SWITCH("Modem Playback Switch", IDX_MIXER_MODEMOUT, 15, 1),
650 AZF3328_MIXER_VOL_MONO("Modem Playback Volume", IDX_MIXER_MODEMOUT, 0x1f, 1),
651 AZF3328_MIXER_SWITCH("Modem Capture Switch", IDX_MIXER_MODEMIN, 15, 1),
652 AZF3328_MIXER_VOL_MONO("Modem Capture Volume", IDX_MIXER_MODEMIN, 0x1f, 1),
Andreas Mohr13769e32006-05-17 11:03:16 +0200653 AZF3328_MIXER_ENUM("Mic Select", IDX_MIXER_ADVCTL2, 2, 8),
654 AZF3328_MIXER_ENUM("Mono Output Select", IDX_MIXER_ADVCTL2, 2, 9),
655 AZF3328_MIXER_ENUM("PCM", IDX_MIXER_ADVCTL2, 2, 15), /* PCM Out Path, place in front since it controls *both* 3D and Bass/Treble! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 AZF3328_MIXER_VOL_SPECIAL("Tone Control - Treble", IDX_MIXER_BASSTREBLE, 0x07, 1, 0),
657 AZF3328_MIXER_VOL_SPECIAL("Tone Control - Bass", IDX_MIXER_BASSTREBLE, 0x07, 9, 0),
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200658 AZF3328_MIXER_SWITCH("3D Control - Switch", IDX_MIXER_ADVCTL2, 13, 0),
Andreas Mohr13769e32006-05-17 11:03:16 +0200659 AZF3328_MIXER_VOL_SPECIAL("3D Control - Width", IDX_MIXER_ADVCTL1, 0x07, 1, 0), /* "3D Width" */
660 AZF3328_MIXER_VOL_SPECIAL("3D Control - Depth", IDX_MIXER_ADVCTL1, 0x03, 8, 0), /* "Hifi 3D" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661#if MIXER_TESTING
662 AZF3328_MIXER_SWITCH("0", IDX_MIXER_ADVCTL2, 0, 0),
663 AZF3328_MIXER_SWITCH("1", IDX_MIXER_ADVCTL2, 1, 0),
664 AZF3328_MIXER_SWITCH("2", IDX_MIXER_ADVCTL2, 2, 0),
665 AZF3328_MIXER_SWITCH("3", IDX_MIXER_ADVCTL2, 3, 0),
666 AZF3328_MIXER_SWITCH("4", IDX_MIXER_ADVCTL2, 4, 0),
667 AZF3328_MIXER_SWITCH("5", IDX_MIXER_ADVCTL2, 5, 0),
668 AZF3328_MIXER_SWITCH("6", IDX_MIXER_ADVCTL2, 6, 0),
669 AZF3328_MIXER_SWITCH("7", IDX_MIXER_ADVCTL2, 7, 0),
670 AZF3328_MIXER_SWITCH("8", IDX_MIXER_ADVCTL2, 8, 0),
671 AZF3328_MIXER_SWITCH("9", IDX_MIXER_ADVCTL2, 9, 0),
672 AZF3328_MIXER_SWITCH("10", IDX_MIXER_ADVCTL2, 10, 0),
673 AZF3328_MIXER_SWITCH("11", IDX_MIXER_ADVCTL2, 11, 0),
674 AZF3328_MIXER_SWITCH("12", IDX_MIXER_ADVCTL2, 12, 0),
675 AZF3328_MIXER_SWITCH("13", IDX_MIXER_ADVCTL2, 13, 0),
676 AZF3328_MIXER_SWITCH("14", IDX_MIXER_ADVCTL2, 14, 0),
677 AZF3328_MIXER_SWITCH("15", IDX_MIXER_ADVCTL2, 15, 0),
678#endif
679};
680
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200681static const u16 __devinitdata snd_azf3328_init_values[][2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 { IDX_MIXER_PLAY_MASTER, MIXER_MUTE_MASK|0x1f1f },
683 { IDX_MIXER_MODEMOUT, MIXER_MUTE_MASK|0x1f1f },
684 { IDX_MIXER_BASSTREBLE, 0x0000 },
685 { IDX_MIXER_PCBEEP, MIXER_MUTE_MASK|0x1f1f },
686 { IDX_MIXER_MODEMIN, MIXER_MUTE_MASK|0x1f1f },
687 { IDX_MIXER_MIC, MIXER_MUTE_MASK|0x001f },
688 { IDX_MIXER_LINEIN, MIXER_MUTE_MASK|0x1f1f },
689 { IDX_MIXER_CDAUDIO, MIXER_MUTE_MASK|0x1f1f },
690 { IDX_MIXER_VIDEO, MIXER_MUTE_MASK|0x1f1f },
691 { IDX_MIXER_AUX, MIXER_MUTE_MASK|0x1f1f },
692 { IDX_MIXER_WAVEOUT, MIXER_MUTE_MASK|0x1f1f },
693 { IDX_MIXER_FMSYNTH, MIXER_MUTE_MASK|0x1f1f },
694 { IDX_MIXER_REC_VOLUME, MIXER_MUTE_MASK|0x0707 },
695};
696
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200697static int __devinit
Takashi Iwai95de7762005-11-17 15:02:42 +0100698snd_azf3328_mixer_new(struct snd_azf3328 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
Takashi Iwai95de7762005-11-17 15:02:42 +0100700 struct snd_card *card;
701 const struct snd_kcontrol_new *sw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 unsigned int idx;
703 int err;
704
705 snd_azf3328_dbgcallenter();
706 snd_assert(chip != NULL && chip->card != NULL, return -EINVAL);
707
708 card = chip->card;
709
710 /* mixer reset */
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200711 snd_azf3328_mixer_outw(chip, IDX_MIXER_RESET, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
713 /* mute and zero volume channels */
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200714 for (idx = 0; idx < ARRAY_SIZE(snd_azf3328_init_values); idx++) {
715 snd_azf3328_mixer_outw(chip,
716 snd_azf3328_init_values[idx][0],
717 snd_azf3328_init_values[idx][1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 }
719
720 /* add mixer controls */
721 sw = snd_azf3328_mixer_controls;
722 for (idx = 0; idx < ARRAY_SIZE(snd_azf3328_mixer_controls); idx++, sw++) {
723 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(sw, chip))) < 0)
724 return err;
725 }
726 snd_component_add(card, "AZF3328 mixer");
727 strcpy(card->mixername, "AZF3328 mixer");
728
729 snd_azf3328_dbgcallleave();
730 return 0;
731}
732
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200733static int
Takashi Iwai95de7762005-11-17 15:02:42 +0100734snd_azf3328_hw_params(struct snd_pcm_substream *substream,
735 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
737 int res;
738 snd_azf3328_dbgcallenter();
739 res = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
740 snd_azf3328_dbgcallleave();
741 return res;
742}
743
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200744static int
Takashi Iwai95de7762005-11-17 15:02:42 +0100745snd_azf3328_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
747 snd_azf3328_dbgcallenter();
748 snd_pcm_lib_free_pages(substream);
749 snd_azf3328_dbgcallleave();
750 return 0;
751}
752
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200753static void
Takashi Iwai95de7762005-11-17 15:02:42 +0100754snd_azf3328_setfmt(struct snd_azf3328 *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 unsigned int reg,
756 unsigned int bitrate,
757 unsigned int format_width,
758 unsigned int channels
759)
760{
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200761 u16 val = 0xff00;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 unsigned long flags;
763
764 snd_azf3328_dbgcallenter();
765 switch (bitrate) {
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200766 case 4000: val |= SOUNDFORMAT_FREQ_SUSPECTED_4000; break;
767 case 4800: val |= SOUNDFORMAT_FREQ_SUSPECTED_4800; break;
768 case 5512: val |= SOUNDFORMAT_FREQ_5510; break; /* the AZF3328 names it "5510" for some strange reason */
769 case 6620: val |= SOUNDFORMAT_FREQ_6620; break;
770 case 8000: val |= SOUNDFORMAT_FREQ_8000; break;
771 case 9600: val |= SOUNDFORMAT_FREQ_9600; break;
772 case 11025: val |= SOUNDFORMAT_FREQ_11025; break;
773 case 13240: val |= SOUNDFORMAT_FREQ_SUSPECTED_13240; break;
774 case 16000: val |= SOUNDFORMAT_FREQ_16000; break;
775 case 22050: val |= SOUNDFORMAT_FREQ_22050; break;
776 case 32000: val |= SOUNDFORMAT_FREQ_32000; break;
777 case 44100: val |= SOUNDFORMAT_FREQ_44100; break;
778 case 48000: val |= SOUNDFORMAT_FREQ_48000; break;
779 case 66200: val |= SOUNDFORMAT_FREQ_SUSPECTED_66200; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 default:
Takashi Iwai99b359b2005-10-20 18:26:44 +0200781 snd_printk(KERN_WARNING "unknown bitrate %d, assuming 44.1kHz!\n", bitrate);
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200782 val |= SOUNDFORMAT_FREQ_44100;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 break;
784 }
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200785 /* val = 0xff07; 3m27.993s (65301Hz; -> 64000Hz???) hmm, 66120, 65967, 66123 */
786 /* val = 0xff09; 17m15.098s (13123,478Hz; -> 12000Hz???) hmm, 13237.2Hz? */
787 /* val = 0xff0a; 47m30.599s (4764,891Hz; -> 4800Hz???) yup, 4803Hz */
788 /* val = 0xff0c; 57m0.510s (4010,263Hz; -> 4000Hz???) yup, 4003Hz */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 /* val = 0xff05; 5m11.556s (... -> 44100Hz) */
790 /* val = 0xff03; 10m21.529s (21872,463Hz; -> 22050Hz???) */
791 /* val = 0xff0f; 20m41.883s (10937,993Hz; -> 11025Hz???) */
792 /* val = 0xff0d; 41m23.135s (5523,600Hz; -> 5512Hz???) */
793 /* val = 0xff0e; 28m30.777s (8017Hz; -> 8000Hz???) */
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 if (channels == 2)
796 val |= SOUNDFORMAT_FLAG_2CHANNELS;
797
798 if (format_width == 16)
799 val |= SOUNDFORMAT_FLAG_16BIT;
800
801 spin_lock_irqsave(&chip->reg_lock, flags);
802
803 /* set bitrate/format */
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200804 snd_azf3328_codec_outw(chip, reg, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
806 /* changing the bitrate/format settings switches off the
807 * audio output with an annoying click in case of 8/16bit format change
808 * (maybe shutting down DAC/ADC?), thus immediately
809 * do some tweaking to reenable it and get rid of the clicking
810 * (FIXME: yes, it works, but what exactly am I doing here?? :)
811 * FIXME: does this have some side effects for full-duplex
812 * or other dramatic side effects? */
813 if (reg == IDX_IO_PLAY_SOUNDFORMAT) /* only do it for playback */
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200814 snd_azf3328_codec_outw(chip, IDX_IO_PLAY_FLAGS,
815 snd_azf3328_codec_inw(chip, IDX_IO_PLAY_FLAGS) |
816 DMA_PLAY_SOMETHING1 |
817 DMA_PLAY_SOMETHING2 |
818 SOMETHING_ALMOST_ALWAYS_SET |
819 DMA_EPILOGUE_SOMETHING |
820 DMA_SOMETHING_ELSE
821 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 spin_unlock_irqrestore(&chip->reg_lock, flags);
824 snd_azf3328_dbgcallleave();
825}
826
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200827static void
Takashi Iwai95de7762005-11-17 15:02:42 +0100828snd_azf3328_setdmaa(struct snd_azf3328 *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 long unsigned int addr,
830 unsigned int count,
831 unsigned int size,
832 int do_recording)
833{
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200834 unsigned long flags, portbase;
835 unsigned int is_running;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
837 snd_azf3328_dbgcallenter();
Andreas Mohre2f87262006-05-17 11:04:19 +0200838 if (do_recording) {
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200839 /* access capture registers, i.e. skip playback reg section */
840 portbase = chip->codec_port + 0x20;
841 is_running = chip->is_recording;
Andreas Mohre2f87262006-05-17 11:04:19 +0200842 } else {
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200843 /* access the playback register section */
844 portbase = chip->codec_port + 0x00;
845 is_running = chip->is_playing;
846 }
847
848 /* AZF3328 uses a two buffer pointer DMA playback approach */
Andreas Mohre2f87262006-05-17 11:04:19 +0200849 if (!is_running) {
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200850 unsigned long addr_area2;
851 unsigned long count_areas, count_tmp; /* width 32bit -- overflow!! */
852 count_areas = size/2;
853 addr_area2 = addr+count_areas;
854 count_areas--; /* max. index */
855 snd_azf3328_dbgplay("set DMA: buf1 %08lx[%lu], buf2 %08lx[%lu]\n", addr, count_areas, addr_area2, count_areas);
856
857 /* build combined I/O buffer length word */
858 count_tmp = count_areas;
859 count_areas |= (count_tmp << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 spin_lock_irqsave(&chip->reg_lock, flags);
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200861 outl(addr, portbase + IDX_IO_PLAY_DMA_START_1);
862 outl(addr_area2, portbase + IDX_IO_PLAY_DMA_START_2);
863 outl(count_areas, portbase + IDX_IO_PLAY_DMA_LEN_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 spin_unlock_irqrestore(&chip->reg_lock, flags);
865 }
866 snd_azf3328_dbgcallleave();
867}
868
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200869static int
Takashi Iwai95de7762005-11-17 15:02:42 +0100870snd_azf3328_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
872#if 0
Takashi Iwai95de7762005-11-17 15:02:42 +0100873 struct snd_azf3328 *chip = snd_pcm_substream_chip(substream);
874 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
876 unsigned int count = snd_pcm_lib_period_bytes(substream);
877#endif
878
879 snd_azf3328_dbgcallenter();
880#if 0
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200881 snd_azf3328_setfmt(chip, IDX_IO_PLAY_SOUNDFORMAT,
882 runtime->rate,
883 snd_pcm_format_width(runtime->format),
884 runtime->channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 snd_azf3328_setdmaa(chip, runtime->dma_addr, count, size, 0);
886#endif
887 snd_azf3328_dbgcallleave();
888 return 0;
889}
890
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200891static int
Takashi Iwai95de7762005-11-17 15:02:42 +0100892snd_azf3328_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
894#if 0
Takashi Iwai95de7762005-11-17 15:02:42 +0100895 struct snd_azf3328 *chip = snd_pcm_substream_chip(substream);
896 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
898 unsigned int count = snd_pcm_lib_period_bytes(substream);
899#endif
900
901 snd_azf3328_dbgcallenter();
902#if 0
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200903 snd_azf3328_setfmt(chip, IDX_IO_REC_SOUNDFORMAT,
904 runtime->rate,
905 snd_pcm_format_width(runtime->format),
906 runtime->channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 snd_azf3328_setdmaa(chip, runtime->dma_addr, count, size, 1);
908#endif
909 snd_azf3328_dbgcallleave();
910 return 0;
911}
912
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200913static int
Takashi Iwai95de7762005-11-17 15:02:42 +0100914snd_azf3328_playback_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
Takashi Iwai95de7762005-11-17 15:02:42 +0100916 struct snd_azf3328 *chip = snd_pcm_substream_chip(substream);
917 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 int result = 0;
919 unsigned int status1;
920
921 snd_azf3328_dbgcalls("snd_azf3328_playback_trigger cmd %d\n", cmd);
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200922
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 switch (cmd) {
924 case SNDRV_PCM_TRIGGER_START:
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200925 snd_azf3328_dbgplay("START PLAYBACK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
927 /* mute WaveOut */
928 snd_azf3328_mixer_set_mute(chip, IDX_MIXER_WAVEOUT, 1);
929
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200930 snd_azf3328_setfmt(chip, IDX_IO_PLAY_SOUNDFORMAT,
931 runtime->rate,
932 snd_pcm_format_width(runtime->format),
933 runtime->channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935 spin_lock(&chip->reg_lock);
936 /* stop playback */
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200937 status1 = snd_azf3328_codec_inw(chip, IDX_IO_PLAY_FLAGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 status1 &= ~DMA_RESUME;
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200939 snd_azf3328_codec_outw(chip, IDX_IO_PLAY_FLAGS, status1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
941 /* FIXME: clear interrupts or what??? */
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200942 snd_azf3328_codec_outw(chip, IDX_IO_PLAY_IRQTYPE, 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 spin_unlock(&chip->reg_lock);
944
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200945 snd_azf3328_setdmaa(chip, runtime->dma_addr,
946 snd_pcm_lib_period_bytes(substream),
947 snd_pcm_lib_buffer_bytes(substream),
948 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
950 spin_lock(&chip->reg_lock);
951#ifdef WIN9X
952 /* FIXME: enable playback/recording??? */
953 status1 |= DMA_PLAY_SOMETHING1 | DMA_PLAY_SOMETHING2;
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200954 snd_azf3328_codec_outw(chip, IDX_IO_PLAY_FLAGS, status1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956 /* start playback again */
957 /* FIXME: what is this value (0x0010)??? */
958 status1 |= DMA_RESUME | DMA_EPILOGUE_SOMETHING;
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200959 snd_azf3328_codec_outw(chip, IDX_IO_PLAY_FLAGS, status1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960#else /* NT4 */
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200961 snd_azf3328_codec_outw(chip, IDX_IO_PLAY_FLAGS,
962 0x0000);
963 snd_azf3328_codec_outw(chip, IDX_IO_PLAY_FLAGS,
964 DMA_PLAY_SOMETHING1);
965 snd_azf3328_codec_outw(chip, IDX_IO_PLAY_FLAGS,
966 DMA_PLAY_SOMETHING1 |
967 DMA_PLAY_SOMETHING2);
968 snd_azf3328_codec_outw(chip, IDX_IO_PLAY_FLAGS,
969 DMA_RESUME |
970 SOMETHING_ALMOST_ALWAYS_SET |
971 DMA_EPILOGUE_SOMETHING |
972 DMA_SOMETHING_ELSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973#endif
974 spin_unlock(&chip->reg_lock);
975
976 /* now unmute WaveOut */
977 snd_azf3328_mixer_set_mute(chip, IDX_MIXER_WAVEOUT, 0);
978
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 chip->is_playing = 1;
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200980 snd_azf3328_dbgplay("STARTED PLAYBACK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 break;
Andreas Mohrca54bde2006-05-17 11:02:24 +0200982 case SNDRV_PCM_TRIGGER_RESUME:
983 snd_azf3328_dbgplay("RESUME PLAYBACK\n");
984 /* resume playback if we were active */
985 if (chip->is_playing)
986 snd_azf3328_codec_outw(chip, IDX_IO_PLAY_FLAGS,
987 snd_azf3328_codec_inw(chip, IDX_IO_PLAY_FLAGS) | DMA_RESUME);
988 break;
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200989 case SNDRV_PCM_TRIGGER_STOP:
990 snd_azf3328_dbgplay("STOP PLAYBACK\n");
991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 /* mute WaveOut */
993 snd_azf3328_mixer_set_mute(chip, IDX_MIXER_WAVEOUT, 1);
994
995 spin_lock(&chip->reg_lock);
996 /* stop playback */
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200997 status1 = snd_azf3328_codec_inw(chip, IDX_IO_PLAY_FLAGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
999 status1 &= ~DMA_RESUME;
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001000 snd_azf3328_codec_outw(chip, IDX_IO_PLAY_FLAGS, status1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001002 /* hmm, is this really required? we're resetting the same bit
1003 * immediately thereafter... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 status1 |= DMA_PLAY_SOMETHING1;
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001005 snd_azf3328_codec_outw(chip, IDX_IO_PLAY_FLAGS, status1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
1007 status1 &= ~DMA_PLAY_SOMETHING1;
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001008 snd_azf3328_codec_outw(chip, IDX_IO_PLAY_FLAGS, status1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 spin_unlock(&chip->reg_lock);
1010
1011 /* now unmute WaveOut */
1012 snd_azf3328_mixer_set_mute(chip, IDX_MIXER_WAVEOUT, 0);
1013 chip->is_playing = 0;
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001014 snd_azf3328_dbgplay("STOPPED PLAYBACK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 break;
Andreas Mohrca54bde2006-05-17 11:02:24 +02001016 case SNDRV_PCM_TRIGGER_SUSPEND:
1017 snd_azf3328_dbgplay("SUSPEND PLAYBACK\n");
1018 /* make sure playback is stopped */
1019 snd_azf3328_codec_outw(chip, IDX_IO_PLAY_FLAGS,
1020 snd_azf3328_codec_inw(chip, IDX_IO_PLAY_FLAGS) & ~DMA_RESUME);
1021 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Takashi Iwai99b359b2005-10-20 18:26:44 +02001023 snd_printk(KERN_ERR "FIXME: SNDRV_PCM_TRIGGER_PAUSE_PUSH NIY!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 break;
1025 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Takashi Iwai99b359b2005-10-20 18:26:44 +02001026 snd_printk(KERN_ERR "FIXME: SNDRV_PCM_TRIGGER_PAUSE_RELEASE NIY!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 break;
1028 default:
Andreas Mohrca54bde2006-05-17 11:02:24 +02001029 printk(KERN_ERR "FIXME: unknown trigger mode!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 return -EINVAL;
1031 }
1032
1033 snd_azf3328_dbgcallleave();
1034 return result;
1035}
1036
1037/* this is just analogous to playback; I'm not quite sure whether recording
1038 * should actually be triggered like that */
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001039static int
Takashi Iwai95de7762005-11-17 15:02:42 +01001040snd_azf3328_capture_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041{
Takashi Iwai95de7762005-11-17 15:02:42 +01001042 struct snd_azf3328 *chip = snd_pcm_substream_chip(substream);
1043 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 int result = 0;
1045 unsigned int status1;
1046
1047 snd_azf3328_dbgcalls("snd_azf3328_capture_trigger cmd %d\n", cmd);
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 switch (cmd) {
1050 case SNDRV_PCM_TRIGGER_START:
1051
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001052 snd_azf3328_dbgplay("START CAPTURE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001054 snd_azf3328_setfmt(chip, IDX_IO_REC_SOUNDFORMAT,
1055 runtime->rate,
1056 snd_pcm_format_width(runtime->format),
1057 runtime->channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
1059 spin_lock(&chip->reg_lock);
1060 /* stop recording */
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001061 status1 = snd_azf3328_codec_inw(chip, IDX_IO_REC_FLAGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 status1 &= ~DMA_RESUME;
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001063 snd_azf3328_codec_outw(chip, IDX_IO_REC_FLAGS, status1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
1065 /* FIXME: clear interrupts or what??? */
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001066 snd_azf3328_codec_outw(chip, IDX_IO_REC_IRQTYPE, 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 spin_unlock(&chip->reg_lock);
1068
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001069 snd_azf3328_setdmaa(chip, runtime->dma_addr,
1070 snd_pcm_lib_period_bytes(substream),
1071 snd_pcm_lib_buffer_bytes(substream),
1072 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
1074 spin_lock(&chip->reg_lock);
1075#ifdef WIN9X
1076 /* FIXME: enable playback/recording??? */
1077 status1 |= DMA_PLAY_SOMETHING1 | DMA_PLAY_SOMETHING2;
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001078 snd_azf3328_codec_outw(chip, IDX_IO_REC_FLAGS, status1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001080 /* start capture again */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 /* FIXME: what is this value (0x0010)??? */
1082 status1 |= DMA_RESUME | DMA_EPILOGUE_SOMETHING;
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001083 snd_azf3328_codec_outw(chip, IDX_IO_REC_FLAGS, status1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084#else
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001085 snd_azf3328_codec_outw(chip, IDX_IO_REC_FLAGS,
1086 0x0000);
1087 snd_azf3328_codec_outw(chip, IDX_IO_REC_FLAGS,
1088 DMA_PLAY_SOMETHING1);
1089 snd_azf3328_codec_outw(chip, IDX_IO_REC_FLAGS,
1090 DMA_PLAY_SOMETHING1 |
1091 DMA_PLAY_SOMETHING2);
1092 snd_azf3328_codec_outw(chip, IDX_IO_REC_FLAGS,
1093 DMA_RESUME |
1094 SOMETHING_ALMOST_ALWAYS_SET |
1095 DMA_EPILOGUE_SOMETHING |
1096 DMA_SOMETHING_ELSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097#endif
1098 spin_unlock(&chip->reg_lock);
1099
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001100 chip->is_recording = 1;
1101 snd_azf3328_dbgplay("STARTED CAPTURE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 break;
Andreas Mohrca54bde2006-05-17 11:02:24 +02001103 case SNDRV_PCM_TRIGGER_RESUME:
1104 snd_azf3328_dbgplay("RESUME CAPTURE\n");
1105 /* resume recording if we were active */
1106 if (chip->is_recording)
1107 snd_azf3328_codec_outw(chip, IDX_IO_REC_FLAGS,
1108 snd_azf3328_codec_inw(chip, IDX_IO_REC_FLAGS) | DMA_RESUME);
1109 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 case SNDRV_PCM_TRIGGER_STOP:
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001111 snd_azf3328_dbgplay("STOP CAPTURE\n");
1112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 spin_lock(&chip->reg_lock);
1114 /* stop recording */
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001115 status1 = snd_azf3328_codec_inw(chip, IDX_IO_REC_FLAGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117 status1 &= ~DMA_RESUME;
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001118 snd_azf3328_codec_outw(chip, IDX_IO_REC_FLAGS, status1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
1120 status1 |= DMA_PLAY_SOMETHING1;
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001121 snd_azf3328_codec_outw(chip, IDX_IO_REC_FLAGS, status1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
1123 status1 &= ~DMA_PLAY_SOMETHING1;
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001124 snd_azf3328_codec_outw(chip, IDX_IO_REC_FLAGS, status1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 spin_unlock(&chip->reg_lock);
1126
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001127 chip->is_recording = 0;
1128 snd_azf3328_dbgplay("STOPPED CAPTURE\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 break;
Andreas Mohrca54bde2006-05-17 11:02:24 +02001130 case SNDRV_PCM_TRIGGER_SUSPEND:
1131 snd_azf3328_dbgplay("SUSPEND CAPTURE\n");
1132 /* make sure recording is stopped */
1133 snd_azf3328_codec_outw(chip, IDX_IO_REC_FLAGS,
1134 snd_azf3328_codec_inw(chip, IDX_IO_REC_FLAGS) & ~DMA_RESUME);
1135 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Takashi Iwai99b359b2005-10-20 18:26:44 +02001137 snd_printk(KERN_ERR "FIXME: SNDRV_PCM_TRIGGER_PAUSE_PUSH NIY!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 break;
1139 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Takashi Iwai99b359b2005-10-20 18:26:44 +02001140 snd_printk(KERN_ERR "FIXME: SNDRV_PCM_TRIGGER_PAUSE_RELEASE NIY!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 break;
1142 default:
Andreas Mohrca54bde2006-05-17 11:02:24 +02001143 printk(KERN_ERR "FIXME: unknown trigger mode!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 return -EINVAL;
1145 }
1146
1147 snd_azf3328_dbgcallleave();
1148 return result;
1149}
1150
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001151static snd_pcm_uframes_t
Takashi Iwai95de7762005-11-17 15:02:42 +01001152snd_azf3328_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153{
Takashi Iwai95de7762005-11-17 15:02:42 +01001154 struct snd_azf3328 *chip = snd_pcm_substream_chip(substream);
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001155 unsigned long bufptr, result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 snd_pcm_uframes_t frmres;
1157
1158#ifdef QUERY_HARDWARE
1159 bufptr = inl(chip->codec_port+IDX_IO_PLAY_DMA_START_1);
1160#else
1161 bufptr = substream->runtime->dma_addr;
1162#endif
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001163 result = inl(chip->codec_port+IDX_IO_PLAY_DMA_CURRPOS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001165 /* calculate offset */
1166 result -= bufptr;
1167 frmres = bytes_to_frames( substream->runtime, result);
1168 snd_azf3328_dbgplay("PLAY @ 0x%8lx, frames %8ld\n", result, frmres);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 return frmres;
1170}
1171
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001172static snd_pcm_uframes_t
Takashi Iwai95de7762005-11-17 15:02:42 +01001173snd_azf3328_capture_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174{
Takashi Iwai95de7762005-11-17 15:02:42 +01001175 struct snd_azf3328 *chip = snd_pcm_substream_chip(substream);
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001176 unsigned long bufptr, result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 snd_pcm_uframes_t frmres;
1178
1179#ifdef QUERY_HARDWARE
1180 bufptr = inl(chip->codec_port+IDX_IO_REC_DMA_START_1);
1181#else
1182 bufptr = substream->runtime->dma_addr;
1183#endif
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001184 result = inl(chip->codec_port+IDX_IO_REC_DMA_CURRPOS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001186 /* calculate offset */
1187 result -= bufptr;
1188 frmres = bytes_to_frames( substream->runtime, result);
1189 snd_azf3328_dbgplay("REC @ 0x%8lx, frames %8ld\n", result, frmres);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 return frmres;
1191}
1192
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001193static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +01001194snd_azf3328_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195{
Takashi Iwai95de7762005-11-17 15:02:42 +01001196 struct snd_azf3328 *chip = dev_id;
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001197 u8 status, which;
1198 static unsigned long irq_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001200 status = snd_azf3328_codec_inb(chip, IDX_IO_IRQSTATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
1202 /* fast path out, to ease interrupt sharing */
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001203 if (!(status & (IRQ_PLAYBACK|IRQ_RECORDING|IRQ_MPU401|IRQ_TIMER)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 return IRQ_NONE; /* must be interrupt for another device */
1205
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001206 snd_azf3328_dbgplay("Interrupt %ld!\nIDX_IO_PLAY_FLAGS %04x, IDX_IO_PLAY_IRQTYPE %04x, IDX_IO_IRQSTATUS %04x\n",
1207 irq_count,
1208 snd_azf3328_codec_inw(chip, IDX_IO_PLAY_FLAGS),
1209 snd_azf3328_codec_inw(chip, IDX_IO_PLAY_IRQTYPE),
1210 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Andreas Mohre2f87262006-05-17 11:04:19 +02001212 if (status & IRQ_TIMER) {
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001213 /* snd_azf3328_dbgplay("timer %ld\n", inl(chip->codec_port+IDX_IO_TIMER_VALUE) & TIMER_VALUE_MASK); */
1214 if (chip->timer)
1215 snd_timer_interrupt(chip->timer, chip->timer->sticks);
1216 /* ACK timer */
1217 spin_lock(&chip->reg_lock);
1218 snd_azf3328_codec_outb(chip, IDX_IO_TIMER_VALUE + 3, 0x07);
1219 spin_unlock(&chip->reg_lock);
1220 snd_azf3328_dbgplay("azt3328: timer IRQ\n");
1221 }
Andreas Mohre2f87262006-05-17 11:04:19 +02001222 if (status & IRQ_PLAYBACK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 spin_lock(&chip->reg_lock);
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001224 which = snd_azf3328_codec_inb(chip, IDX_IO_PLAY_IRQTYPE);
1225 /* ack all IRQ types immediately */
1226 snd_azf3328_codec_outb(chip, IDX_IO_PLAY_IRQTYPE, which);
1227 spin_unlock(&chip->reg_lock);
1228
Andreas Mohre2f87262006-05-17 11:04:19 +02001229 if (chip->pcm && chip->playback_substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 snd_pcm_period_elapsed(chip->playback_substream);
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001231 snd_azf3328_dbgplay("PLAY period done (#%x), @ %x\n",
1232 which,
1233 inl(chip->codec_port+IDX_IO_PLAY_DMA_CURRPOS));
Andreas Mohre2f87262006-05-17 11:04:19 +02001234 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 snd_azf3328_dbgplay("azt3328: ouch, irq handler problem!\n");
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001236 if (which & IRQ_PLAY_SOMETHING)
1237 snd_azf3328_dbgplay("azt3328: unknown play IRQ type occurred, please report!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 }
Andreas Mohre2f87262006-05-17 11:04:19 +02001239 if (status & IRQ_RECORDING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 spin_lock(&chip->reg_lock);
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001241 which = snd_azf3328_codec_inb(chip, IDX_IO_REC_IRQTYPE);
1242 /* ack all IRQ types immediately */
1243 snd_azf3328_codec_outb(chip, IDX_IO_REC_IRQTYPE, which);
1244 spin_unlock(&chip->reg_lock);
1245
Andreas Mohre2f87262006-05-17 11:04:19 +02001246 if (chip->pcm && chip->capture_substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 snd_pcm_period_elapsed(chip->capture_substream);
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001248 snd_azf3328_dbgplay("REC period done (#%x), @ %x\n",
1249 which,
1250 inl(chip->codec_port+IDX_IO_REC_DMA_CURRPOS));
Andreas Mohre2f87262006-05-17 11:04:19 +02001251 } else
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001252 snd_azf3328_dbgplay("azt3328: ouch, irq handler problem!\n");
1253 if (which & IRQ_REC_SOMETHING)
1254 snd_azf3328_dbgplay("azt3328: unknown rec IRQ type occurred, please report!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 }
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001256 /* MPU401 has less critical IRQ requirements
1257 * than timer and playback/recording, right? */
Andreas Mohre2f87262006-05-17 11:04:19 +02001258 if (status & IRQ_MPU401) {
David Howells7d12e782006-10-05 14:55:46 +01001259 snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001260
1261 /* hmm, do we have to ack the IRQ here somehow?
1262 * If so, then I don't know how... */
1263 snd_azf3328_dbgplay("azt3328: MPU401 IRQ\n");
1264 }
1265 irq_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 return IRQ_HANDLED;
1267}
1268
1269/*****************************************************************/
1270
Takashi Iwai95de7762005-11-17 15:02:42 +01001271static const struct snd_pcm_hardware snd_azf3328_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272{
1273 /* FIXME!! Correct? */
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001274 .info = SNDRV_PCM_INFO_MMAP |
1275 SNDRV_PCM_INFO_INTERLEAVED |
1276 SNDRV_PCM_INFO_MMAP_VALID,
1277 .formats = SNDRV_PCM_FMTBIT_S8 |
1278 SNDRV_PCM_FMTBIT_U8 |
1279 SNDRV_PCM_FMTBIT_S16_LE |
1280 SNDRV_PCM_FMTBIT_U16_LE,
1281 .rates = SNDRV_PCM_RATE_5512 |
1282 SNDRV_PCM_RATE_8000_48000 |
1283 SNDRV_PCM_RATE_KNOT,
1284 .rate_min = 4000,
1285 .rate_max = 66200,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 .channels_min = 1,
1287 .channels_max = 2,
1288 .buffer_bytes_max = 65536,
1289 .period_bytes_min = 64,
1290 .period_bytes_max = 65536,
1291 .periods_min = 1,
1292 .periods_max = 1024,
1293 /* FIXME: maybe that card actually has a FIFO?
1294 * Hmm, it seems newer revisions do have one, but we still don't know
1295 * its size... */
1296 .fifo_size = 0,
1297};
1298
Takashi Iwai95de7762005-11-17 15:02:42 +01001299static const struct snd_pcm_hardware snd_azf3328_capture =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300{
1301 /* FIXME */
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001302 .info = SNDRV_PCM_INFO_MMAP |
1303 SNDRV_PCM_INFO_INTERLEAVED |
1304 SNDRV_PCM_INFO_MMAP_VALID,
1305 .formats = SNDRV_PCM_FMTBIT_S8 |
1306 SNDRV_PCM_FMTBIT_U8 |
1307 SNDRV_PCM_FMTBIT_S16_LE |
1308 SNDRV_PCM_FMTBIT_U16_LE,
1309 .rates = SNDRV_PCM_RATE_5512 |
1310 SNDRV_PCM_RATE_8000_48000 |
1311 SNDRV_PCM_RATE_KNOT,
1312 .rate_min = 4000,
1313 .rate_max = 66200,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 .channels_min = 1,
1315 .channels_max = 2,
1316 .buffer_bytes_max = 65536,
1317 .period_bytes_min = 64,
1318 .period_bytes_max = 65536,
1319 .periods_min = 1,
1320 .periods_max = 1024,
1321 .fifo_size = 0,
1322};
1323
1324
1325static unsigned int snd_azf3328_fixed_rates[] = {
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001326 4000, 4800, 5512, 6620, 8000, 9600, 11025, 13240, 16000, 22050, 32000,
1327 44100, 48000, 66200 };
Takashi Iwai95de7762005-11-17 15:02:42 +01001328static struct snd_pcm_hw_constraint_list snd_azf3328_hw_constraints_rates = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 .count = ARRAY_SIZE(snd_azf3328_fixed_rates),
1330 .list = snd_azf3328_fixed_rates,
1331 .mask = 0,
1332};
1333
1334/*****************************************************************/
1335
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001336static int
Takashi Iwai95de7762005-11-17 15:02:42 +01001337snd_azf3328_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
Takashi Iwai95de7762005-11-17 15:02:42 +01001339 struct snd_azf3328 *chip = snd_pcm_substream_chip(substream);
1340 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
1342 snd_azf3328_dbgcallenter();
1343 chip->playback_substream = substream;
1344 runtime->hw = snd_azf3328_playback;
1345 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1346 &snd_azf3328_hw_constraints_rates);
1347 snd_azf3328_dbgcallleave();
1348 return 0;
1349}
1350
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001351static int
Takashi Iwai95de7762005-11-17 15:02:42 +01001352snd_azf3328_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353{
Takashi Iwai95de7762005-11-17 15:02:42 +01001354 struct snd_azf3328 *chip = snd_pcm_substream_chip(substream);
1355 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
1357 snd_azf3328_dbgcallenter();
1358 chip->capture_substream = substream;
1359 runtime->hw = snd_azf3328_capture;
1360 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1361 &snd_azf3328_hw_constraints_rates);
1362 snd_azf3328_dbgcallleave();
1363 return 0;
1364}
1365
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001366static int
Takashi Iwai95de7762005-11-17 15:02:42 +01001367snd_azf3328_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368{
Takashi Iwai95de7762005-11-17 15:02:42 +01001369 struct snd_azf3328 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
1371 snd_azf3328_dbgcallenter();
1372
1373 chip->playback_substream = NULL;
1374 snd_azf3328_dbgcallleave();
1375 return 0;
1376}
1377
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001378static int
Takashi Iwai95de7762005-11-17 15:02:42 +01001379snd_azf3328_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380{
Takashi Iwai95de7762005-11-17 15:02:42 +01001381 struct snd_azf3328 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
1383 snd_azf3328_dbgcallenter();
1384 chip->capture_substream = NULL;
1385 snd_azf3328_dbgcallleave();
1386 return 0;
1387}
1388
1389/******************************************************************/
1390
Takashi Iwai95de7762005-11-17 15:02:42 +01001391static struct snd_pcm_ops snd_azf3328_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 .open = snd_azf3328_playback_open,
1393 .close = snd_azf3328_playback_close,
1394 .ioctl = snd_pcm_lib_ioctl,
1395 .hw_params = snd_azf3328_hw_params,
1396 .hw_free = snd_azf3328_hw_free,
1397 .prepare = snd_azf3328_playback_prepare,
1398 .trigger = snd_azf3328_playback_trigger,
1399 .pointer = snd_azf3328_playback_pointer
1400};
1401
Takashi Iwai95de7762005-11-17 15:02:42 +01001402static struct snd_pcm_ops snd_azf3328_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 .open = snd_azf3328_capture_open,
1404 .close = snd_azf3328_capture_close,
1405 .ioctl = snd_pcm_lib_ioctl,
1406 .hw_params = snd_azf3328_hw_params,
1407 .hw_free = snd_azf3328_hw_free,
1408 .prepare = snd_azf3328_capture_prepare,
1409 .trigger = snd_azf3328_capture_trigger,
1410 .pointer = snd_azf3328_capture_pointer
1411};
1412
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001413static int __devinit
Takashi Iwai95de7762005-11-17 15:02:42 +01001414snd_azf3328_pcm(struct snd_azf3328 *chip, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415{
Takashi Iwai95de7762005-11-17 15:02:42 +01001416 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 int err;
1418
1419 snd_azf3328_dbgcallenter();
1420 if ((err = snd_pcm_new(chip->card, "AZF3328 DSP", device, 1, 1, &pcm)) < 0)
1421 return err;
1422 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_azf3328_playback_ops);
1423 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_azf3328_capture_ops);
1424
1425 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 pcm->info_flags = 0;
1427 strcpy(pcm->name, chip->card->shortname);
1428 chip->pcm = pcm;
1429
1430 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1431 snd_dma_pci_data(chip->pci), 64*1024, 64*1024);
1432
1433 snd_azf3328_dbgcallleave();
1434 return 0;
1435}
1436
1437/******************************************************************/
1438
1439#ifdef SUPPORT_JOYSTICK
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001440static int __devinit
Takashi Iwai95de7762005-11-17 15:02:42 +01001441snd_azf3328_config_joystick(struct snd_azf3328 *chip, int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442{
1443 struct gameport *gp;
1444 struct resource *r;
1445
1446 if (!joystick[dev])
1447 return -ENODEV;
1448
1449 if (!(r = request_region(0x200, 8, "AZF3328 gameport"))) {
1450 printk(KERN_WARNING "azt3328: cannot reserve joystick ports\n");
1451 return -EBUSY;
1452 }
1453
1454 chip->gameport = gp = gameport_allocate_port();
1455 if (!gp) {
1456 printk(KERN_ERR "azt3328: cannot allocate memory for gameport\n");
Takashi Iwaib1d57762005-10-10 11:56:31 +02001457 release_and_free_resource(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 return -ENOMEM;
1459 }
1460
1461 gameport_set_name(gp, "AZF3328 Gameport");
1462 gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
1463 gameport_set_dev_parent(gp, &chip->pci->dev);
1464 gp->io = 0x200;
1465 gameport_set_port_data(gp, r);
1466
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001467 snd_azf3328_io2_outb(chip, IDX_IO2_LEGACY_ADDR,
1468 snd_azf3328_io2_inb(chip, IDX_IO2_LEGACY_ADDR) | LEGACY_JOY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
1470 gameport_register_port(chip->gameport);
1471
1472 return 0;
1473}
1474
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001475static void
Takashi Iwai95de7762005-11-17 15:02:42 +01001476snd_azf3328_free_joystick(struct snd_azf3328 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477{
1478 if (chip->gameport) {
1479 struct resource *r = gameport_get_port_data(chip->gameport);
1480
1481 gameport_unregister_port(chip->gameport);
1482 chip->gameport = NULL;
1483 /* disable gameport */
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001484 snd_azf3328_io2_outb(chip, IDX_IO2_LEGACY_ADDR,
1485 snd_azf3328_io2_inb(chip, IDX_IO2_LEGACY_ADDR) & ~LEGACY_JOY);
Takashi Iwaib1d57762005-10-10 11:56:31 +02001486 release_and_free_resource(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 }
1488}
1489#else
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001490static inline int
Takashi Iwai95de7762005-11-17 15:02:42 +01001491snd_azf3328_config_joystick(struct snd_azf3328 *chip, int dev) { return -ENOSYS; }
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001492static inline void
Takashi Iwai95de7762005-11-17 15:02:42 +01001493snd_azf3328_free_joystick(struct snd_azf3328 *chip) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494#endif
1495
1496/******************************************************************/
1497
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001498static int
Takashi Iwai95de7762005-11-17 15:02:42 +01001499snd_azf3328_free(struct snd_azf3328 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500{
1501 if (chip->irq < 0)
1502 goto __end_hw;
1503
1504 /* reset (close) mixer */
1505 snd_azf3328_mixer_set_mute(chip, IDX_MIXER_PLAY_MASTER, 1); /* first mute master volume */
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001506 snd_azf3328_mixer_outw(chip, IDX_MIXER_RESET, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001508 /* interrupt setup - mask everything (FIXME!) */
1509 /* well, at least we know how to disable the timer IRQ */
1510 snd_azf3328_codec_outb(chip, IDX_IO_TIMER_VALUE + 3, 0x00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
1512 synchronize_irq(chip->irq);
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001513__end_hw:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 snd_azf3328_free_joystick(chip);
1515 if (chip->irq >= 0)
1516 free_irq(chip->irq, (void *)chip);
1517 pci_release_regions(chip->pci);
1518 pci_disable_device(chip->pci);
1519
1520 kfree(chip);
1521 return 0;
1522}
1523
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001524static int
Takashi Iwai95de7762005-11-17 15:02:42 +01001525snd_azf3328_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526{
Takashi Iwai95de7762005-11-17 15:02:42 +01001527 struct snd_azf3328 *chip = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 return snd_azf3328_free(chip);
1529}
1530
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001531/******************************************************************/
1532
1533/*** NOTE: the physical timer resolution actually is 1024000 ticks per second,
1534 *** but announcing those attributes to user-space would make programs
1535 *** configure the timer to a 1 tick value, resulting in an absolutely fatal
1536 *** timer IRQ storm.
1537 *** Thus I chose to announce a down-scaled virtual timer to the outside and
1538 *** calculate real timer countdown values internally.
1539 *** (the scale factor can be set via module parameter "seqtimer_scaling").
1540 ***/
1541
1542static int
Takashi Iwai95de7762005-11-17 15:02:42 +01001543snd_azf3328_timer_start(struct snd_timer *timer)
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001544{
Takashi Iwai95de7762005-11-17 15:02:42 +01001545 struct snd_azf3328 *chip;
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001546 unsigned long flags;
1547 unsigned int delay;
1548
1549 snd_azf3328_dbgcallenter();
1550 chip = snd_timer_chip(timer);
1551 delay = ((timer->sticks * seqtimer_scaling) - 1) & TIMER_VALUE_MASK;
Andreas Mohre2f87262006-05-17 11:04:19 +02001552 if (delay < 49) {
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001553 /* uhoh, that's not good, since user-space won't know about
1554 * this timing tweak
1555 * (we need to do it to avoid a lockup, though) */
1556
1557 snd_azf3328_dbgtimer("delay was too low (%d)!\n", delay);
1558 delay = 49; /* minimum time is 49 ticks */
1559 }
1560 snd_azf3328_dbgtimer("setting timer countdown value %d, add COUNTDOWN|IRQ\n", delay);
1561 delay |= TIMER_ENABLE_COUNTDOWN | TIMER_ENABLE_IRQ;
1562 spin_lock_irqsave(&chip->reg_lock, flags);
1563 snd_azf3328_codec_outl(chip, IDX_IO_TIMER_VALUE, delay);
1564 spin_unlock_irqrestore(&chip->reg_lock, flags);
1565 snd_azf3328_dbgcallleave();
1566 return 0;
1567}
1568
1569static int
Takashi Iwai95de7762005-11-17 15:02:42 +01001570snd_azf3328_timer_stop(struct snd_timer *timer)
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001571{
Takashi Iwai95de7762005-11-17 15:02:42 +01001572 struct snd_azf3328 *chip;
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001573 unsigned long flags;
1574
1575 snd_azf3328_dbgcallenter();
1576 chip = snd_timer_chip(timer);
1577 spin_lock_irqsave(&chip->reg_lock, flags);
1578 /* disable timer countdown and interrupt */
1579 /* FIXME: should we write TIMER_ACK_IRQ here? */
1580 snd_azf3328_codec_outb(chip, IDX_IO_TIMER_VALUE + 3, 0);
1581 spin_unlock_irqrestore(&chip->reg_lock, flags);
1582 snd_azf3328_dbgcallleave();
1583 return 0;
1584}
1585
1586
1587static int
Takashi Iwai95de7762005-11-17 15:02:42 +01001588snd_azf3328_timer_precise_resolution(struct snd_timer *timer,
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001589 unsigned long *num, unsigned long *den)
1590{
1591 snd_azf3328_dbgcallenter();
1592 *num = 1;
1593 *den = 1024000 / seqtimer_scaling;
1594 snd_azf3328_dbgcallleave();
1595 return 0;
1596}
1597
Takashi Iwai95de7762005-11-17 15:02:42 +01001598static struct snd_timer_hardware snd_azf3328_timer_hw = {
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001599 .flags = SNDRV_TIMER_HW_AUTO,
1600 .resolution = 977, /* 1000000/1024000 = 0.9765625us */
1601 .ticks = 1024000, /* max tick count, defined by the value register; actually it's not 1024000, but 1048576, but we don't care */
1602 .start = snd_azf3328_timer_start,
1603 .stop = snd_azf3328_timer_stop,
1604 .precise_resolution = snd_azf3328_timer_precise_resolution,
1605};
1606
1607static int __devinit
Takashi Iwai95de7762005-11-17 15:02:42 +01001608snd_azf3328_timer(struct snd_azf3328 *chip, int device)
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001609{
Takashi Iwai95de7762005-11-17 15:02:42 +01001610 struct snd_timer *timer = NULL;
1611 struct snd_timer_id tid;
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001612 int err;
1613
1614 snd_azf3328_dbgcallenter();
1615 tid.dev_class = SNDRV_TIMER_CLASS_CARD;
1616 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1617 tid.card = chip->card->number;
1618 tid.device = device;
1619 tid.subdevice = 0;
1620
1621 snd_azf3328_timer_hw.resolution *= seqtimer_scaling;
1622 snd_azf3328_timer_hw.ticks /= seqtimer_scaling;
1623 if ((err = snd_timer_new(chip->card, "AZF3328", &tid, &timer)) < 0) {
1624 goto out;
1625 }
1626
1627 strcpy(timer->name, "AZF3328 timer");
1628 timer->private_data = chip;
1629 timer->hw = snd_azf3328_timer_hw;
1630
1631 chip->timer = timer;
1632
1633 err = 0;
1634
1635out:
1636 snd_azf3328_dbgcallleave();
1637 return err;
1638}
1639
1640/******************************************************************/
1641
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642#if 0
1643/* check whether a bit can be modified */
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001644static void
1645snd_azf3328_test_bit(unsigned int reg, int bit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646{
1647 unsigned char val, valoff, valon;
1648
1649 val = inb(reg);
1650
1651 outb(val & ~(1 << bit), reg);
1652 valoff = inb(reg);
1653
1654 outb(val|(1 << bit), reg);
1655 valon = inb(reg);
1656
1657 outb(val, reg);
1658
1659 printk(KERN_ERR "reg %04x bit %d: %02x %02x %02x\n", reg, bit, val, valoff, valon);
1660}
1661#endif
1662
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001663static void
Takashi Iwai95de7762005-11-17 15:02:42 +01001664snd_azf3328_debug_show_ports(const struct snd_azf3328 *chip)
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001665{
1666#if DEBUG_MISC
1667 u16 tmp;
1668
1669 snd_azf3328_dbgmisc("codec_port 0x%lx, io2_port 0x%lx, mpu_port 0x%lx, synth_port 0x%lx, mixer_port 0x%lx, irq %d\n", chip->codec_port, chip->io2_port, chip->mpu_port, chip->synth_port, chip->mixer_port, chip->irq);
1670
1671 snd_azf3328_dbgmisc("io2 %02x %02x %02x %02x %02x %02x\n", snd_azf3328_io2_inb(chip, 0), snd_azf3328_io2_inb(chip, 1), snd_azf3328_io2_inb(chip, 2), snd_azf3328_io2_inb(chip, 3), snd_azf3328_io2_inb(chip, 4), snd_azf3328_io2_inb(chip, 5));
1672
1673 for (tmp=0; tmp <= 0x01; tmp += 1)
1674 snd_azf3328_dbgmisc("0x%02x: opl 0x%04x, mpu300 0x%04x, mpu310 0x%04x, mpu320 0x%04x, mpu330 0x%04x\n", tmp, inb(0x388 + tmp), inb(0x300 + tmp), inb(0x310 + tmp), inb(0x320 + tmp), inb(0x330 + tmp));
1675
1676 for (tmp = 0; tmp <= 0x6E; tmp += 2)
1677 snd_azf3328_dbgmisc("0x%02x: 0x%04x\n", tmp, snd_azf3328_codec_inb(chip, tmp));
1678#endif
1679}
1680
1681static int __devinit
Takashi Iwai95de7762005-11-17 15:02:42 +01001682snd_azf3328_create(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 struct pci_dev *pci,
1684 unsigned long device_type,
Takashi Iwai95de7762005-11-17 15:02:42 +01001685 struct snd_azf3328 ** rchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686{
Takashi Iwai95de7762005-11-17 15:02:42 +01001687 struct snd_azf3328 *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 int err;
Takashi Iwai95de7762005-11-17 15:02:42 +01001689 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 .dev_free = snd_azf3328_dev_free,
1691 };
1692 u16 tmp;
1693
1694 *rchip = NULL;
1695
1696 if ((err = pci_enable_device(pci)) < 0)
1697 return err;
1698
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001699 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 if (chip == NULL) {
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001701 err = -ENOMEM;
1702 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 }
1704 spin_lock_init(&chip->reg_lock);
1705 chip->card = card;
1706 chip->pci = pci;
1707 chip->irq = -1;
1708
1709 /* check if we can restrict PCI DMA transfers to 24 bits */
Matthias Gehre910638a2006-03-28 01:56:48 -08001710 if (pci_set_dma_mask(pci, DMA_24BIT_MASK) < 0 ||
1711 pci_set_consistent_dma_mask(pci, DMA_24BIT_MASK) < 0) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02001712 snd_printk(KERN_ERR "architecture does not support 24bit PCI busmaster DMA\n");
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001713 err = -ENXIO;
1714 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 }
1716
1717 if ((err = pci_request_regions(pci, "Aztech AZF3328")) < 0) {
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001718 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 }
1720
1721 chip->codec_port = pci_resource_start(pci, 0);
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001722 chip->io2_port = pci_resource_start(pci, 1);
1723 chip->mpu_port = pci_resource_start(pci, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 chip->synth_port = pci_resource_start(pci, 3);
1725 chip->mixer_port = pci_resource_start(pci, 4);
1726
Thomas Gleixner65ca68b2006-07-01 19:29:46 -07001727 if (request_irq(pci->irq, snd_azf3328_interrupt, IRQF_DISABLED|IRQF_SHARED, card->shortname, (void *)chip)) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02001728 snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001729 err = -EBUSY;
1730 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 }
1732 chip->irq = pci->irq;
1733 pci_set_master(pci);
1734 synchronize_irq(chip->irq);
1735
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001736 snd_azf3328_debug_show_ports(chip);
1737
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001739 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 }
1741
1742 /* create mixer interface & switches */
1743 if ((err = snd_azf3328_mixer_new(chip)) < 0)
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001744 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
1746#if 0
1747 /* set very low bitrate to reduce noise and power consumption? */
1748 snd_azf3328_setfmt(chip, IDX_IO_PLAY_SOUNDFORMAT, 5512, 8, 1);
1749#endif
1750
1751 /* standard chip init stuff */
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001752 /* default IRQ init value */
1753 tmp = DMA_PLAY_SOMETHING2|DMA_EPILOGUE_SOMETHING|DMA_SOMETHING_ELSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001755 spin_lock_irq(&chip->reg_lock);
1756 snd_azf3328_codec_outb(chip, IDX_IO_PLAY_FLAGS, tmp);
1757 snd_azf3328_codec_outb(chip, IDX_IO_REC_FLAGS, tmp);
1758 snd_azf3328_codec_outb(chip, IDX_IO_SOMETHING_FLAGS, tmp);
1759 snd_azf3328_codec_outb(chip, IDX_IO_TIMER_VALUE + 3, 0x00); /* disable timer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 spin_unlock_irq(&chip->reg_lock);
1761
1762 snd_card_set_dev(card, &pci->dev);
1763
1764 *rchip = chip;
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001765
1766 err = 0;
1767 goto out;
1768
1769out_err:
1770 if (chip)
1771 snd_azf3328_free(chip);
1772 pci_disable_device(pci);
1773
1774out:
1775 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776}
1777
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001778static int __devinit
1779snd_azf3328_probe(struct pci_dev *pci, const struct pci_device_id *pci_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780{
1781 static int dev;
Takashi Iwai95de7762005-11-17 15:02:42 +01001782 struct snd_card *card;
1783 struct snd_azf3328 *chip;
1784 struct snd_opl3 *opl3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 int err;
1786
1787 snd_azf3328_dbgcallenter();
1788 if (dev >= SNDRV_CARDS)
1789 return -ENODEV;
1790 if (!enable[dev]) {
1791 dev++;
1792 return -ENOENT;
1793 }
1794
1795 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0 );
1796 if (card == NULL)
1797 return -ENOMEM;
1798
1799 strcpy(card->driver, "AZF3328");
1800 strcpy(card->shortname, "Aztech AZF3328 (PCI168)");
1801
1802 if ((err = snd_azf3328_create(card, pci, pci_id->driver_data, &chip)) < 0) {
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001803 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 }
1805
Andreas Mohrca54bde2006-05-17 11:02:24 +02001806 card->private_data = chip;
1807
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 if ((err = snd_mpu401_uart_new( card, 0, MPU401_HW_MPU401,
Takashi Iwai302e4c22006-05-23 13:24:30 +02001809 chip->mpu_port, MPU401_INFO_INTEGRATED,
1810 pci->irq, 0, &chip->rmidi)) < 0) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02001811 snd_printk(KERN_ERR "azf3328: no MPU-401 device at 0x%lx?\n", chip->mpu_port);
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001812 goto out_err;
1813 }
1814
1815 if ((err = snd_azf3328_timer(chip, 0)) < 0) {
1816 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 }
1818
1819 if ((err = snd_azf3328_pcm(chip, 0)) < 0) {
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001820 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 }
1822
1823 if (snd_opl3_create(card, chip->synth_port, chip->synth_port+2,
1824 OPL3_HW_AUTO, 1, &opl3) < 0) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02001825 snd_printk(KERN_ERR "azf3328: no OPL3 device at 0x%lx-0x%lx?\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 chip->synth_port, chip->synth_port+2 );
1827 } else {
1828 if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001829 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 }
1831 }
1832
Andreas Mohrca54bde2006-05-17 11:02:24 +02001833 opl3->private_data = chip;
1834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 sprintf(card->longname, "%s at 0x%lx, irq %i",
1836 card->shortname, chip->codec_port, chip->irq);
1837
1838 if ((err = snd_card_register(card)) < 0) {
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001839 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 }
1841
1842#ifdef MODULE
1843 printk(
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001844"azt3328: Sound driver for Aztech AZF3328-based soundcards such as PCI168\n"
1845"azt3328: (hardware was completely undocumented - ZERO support from Aztech).\n"
1846"azt3328: Feel free to contact andi AT lisas.de for bug reports etc.!\n"
1847"azt3328: User-scalable sequencer timer set to %dHz (1024000Hz / %d).\n",
1848 1024000 / seqtimer_scaling, seqtimer_scaling);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849#endif
1850
1851 if (snd_azf3328_config_joystick(chip, dev) < 0)
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001852 snd_azf3328_io2_outb(chip, IDX_IO2_LEGACY_ADDR,
1853 snd_azf3328_io2_inb(chip, IDX_IO2_LEGACY_ADDR) & ~LEGACY_JOY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
1855 pci_set_drvdata(pci, card);
1856 dev++;
1857
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001858 err = 0;
1859 goto out;
1860
1861out_err:
1862 snd_card_free(card);
1863
1864out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 snd_azf3328_dbgcallleave();
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001866 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867}
1868
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001869static void __devexit
1870snd_azf3328_remove(struct pci_dev *pci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871{
1872 snd_azf3328_dbgcallenter();
1873 snd_card_free(pci_get_drvdata(pci));
1874 pci_set_drvdata(pci, NULL);
1875 snd_azf3328_dbgcallleave();
1876}
1877
Andreas Mohrca54bde2006-05-17 11:02:24 +02001878#ifdef CONFIG_PM
1879static int
1880snd_azf3328_suspend(struct pci_dev *pci, pm_message_t state)
1881{
1882 struct snd_card *card = pci_get_drvdata(pci);
1883 struct snd_azf3328 *chip = card->private_data;
1884 int reg;
1885
1886 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1887
1888 snd_pcm_suspend_all(chip->pcm);
1889
1890 for (reg = 0; reg < AZF_IO_SIZE_MIXER_PM / 2; reg++)
1891 chip->saved_regs_mixer[reg] = inw(chip->mixer_port + reg * 2);
1892
1893 /* make sure to disable master volume etc. to prevent looping sound */
1894 snd_azf3328_mixer_set_mute(chip, IDX_MIXER_PLAY_MASTER, 1);
1895 snd_azf3328_mixer_set_mute(chip, IDX_MIXER_WAVEOUT, 1);
1896
1897 for (reg = 0; reg < AZF_IO_SIZE_CODEC_PM / 2; reg++)
1898 chip->saved_regs_codec[reg] = inw(chip->codec_port + reg * 2);
1899 for (reg = 0; reg < AZF_IO_SIZE_IO2_PM / 2; reg++)
1900 chip->saved_regs_io2[reg] = inw(chip->io2_port + reg * 2);
1901 for (reg = 0; reg < AZF_IO_SIZE_MPU_PM / 2; reg++)
1902 chip->saved_regs_mpu[reg] = inw(chip->mpu_port + reg * 2);
1903 for (reg = 0; reg < AZF_IO_SIZE_SYNTH_PM / 2; reg++)
1904 chip->saved_regs_synth[reg] = inw(chip->synth_port + reg * 2);
1905
Andreas Mohrca54bde2006-05-17 11:02:24 +02001906 pci_disable_device(pci);
1907 pci_save_state(pci);
Takashi Iwai30b35392006-10-11 18:52:53 +02001908 pci_set_power_state(pci, pci_choose_state(pci, state));
Andreas Mohrca54bde2006-05-17 11:02:24 +02001909 return 0;
1910}
1911
1912static int
1913snd_azf3328_resume(struct pci_dev *pci)
1914{
1915 struct snd_card *card = pci_get_drvdata(pci);
1916 struct snd_azf3328 *chip = card->private_data;
1917 int reg;
1918
Andreas Mohrca54bde2006-05-17 11:02:24 +02001919 pci_set_power_state(pci, PCI_D0);
Takashi Iwai30b35392006-10-11 18:52:53 +02001920 pci_restore_state(pci);
1921 if (pci_enable_device(pci) < 0) {
1922 printk(KERN_ERR "azt3328: pci_enable_device failed, "
1923 "disabling device\n");
1924 snd_card_disconnect(card);
1925 return -EIO;
1926 }
Andreas Mohrca54bde2006-05-17 11:02:24 +02001927 pci_set_master(pci);
1928
1929 for (reg = 0; reg < AZF_IO_SIZE_IO2_PM / 2; reg++)
1930 outw(chip->saved_regs_io2[reg], chip->io2_port + reg * 2);
1931 for (reg = 0; reg < AZF_IO_SIZE_MPU_PM / 2; reg++)
1932 outw(chip->saved_regs_mpu[reg], chip->mpu_port + reg * 2);
1933 for (reg = 0; reg < AZF_IO_SIZE_SYNTH_PM / 2; reg++)
1934 outw(chip->saved_regs_synth[reg], chip->synth_port + reg * 2);
1935 for (reg = 0; reg < AZF_IO_SIZE_MIXER_PM / 2; reg++)
1936 outw(chip->saved_regs_mixer[reg], chip->mixer_port + reg * 2);
1937 for (reg = 0; reg < AZF_IO_SIZE_CODEC_PM / 2; reg++)
1938 outw(chip->saved_regs_codec[reg], chip->codec_port + reg * 2);
1939
1940 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1941 return 0;
1942}
1943#endif
1944
1945
1946
1947
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948static struct pci_driver driver = {
1949 .name = "AZF3328",
1950 .id_table = snd_azf3328_ids,
1951 .probe = snd_azf3328_probe,
1952 .remove = __devexit_p(snd_azf3328_remove),
Andreas Mohrca54bde2006-05-17 11:02:24 +02001953#ifdef CONFIG_PM
1954 .suspend = snd_azf3328_suspend,
1955 .resume = snd_azf3328_resume,
1956#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957};
1958
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001959static int __init
1960alsa_card_azf3328_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961{
1962 int err;
1963 snd_azf3328_dbgcallenter();
Takashi Iwai01d25d42005-04-11 16:58:24 +02001964 err = pci_register_driver(&driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 snd_azf3328_dbgcallleave();
1966 return err;
1967}
1968
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001969static void __exit
1970alsa_card_azf3328_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971{
1972 snd_azf3328_dbgcallenter();
1973 pci_unregister_driver(&driver);
1974 snd_azf3328_dbgcallleave();
1975}
1976
1977module_init(alsa_card_azf3328_init)
1978module_exit(alsa_card_azf3328_exit)