1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /* azt3328.c - driver for Aztech AZF3328 based soundcards (e.g. PCI168).
3*4882a593Smuzhiyun * Copyright (C) 2002, 2005 - 2011 by Andreas Mohr <andi AT lisas.de>
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Framework borrowed from Bart Hartgers's als4000.c.
6*4882a593Smuzhiyun * Driver developed on PCI168 AP(W) version (PCI rev. 10, subsystem ID 1801),
7*4882a593Smuzhiyun * found in a Fujitsu-Siemens PC ("Cordant", aluminum case).
8*4882a593Smuzhiyun * Other versions are:
9*4882a593Smuzhiyun * PCI168 A(W), sub ID 1800
10*4882a593Smuzhiyun * PCI168 A/AP, sub ID 8000
11*4882a593Smuzhiyun * Please give me feedback in case you try my driver with one of these!!
12*4882a593Smuzhiyun *
13*4882a593Smuzhiyun * Keywords: Windows XP Vista 168nt4-125.zip 168win95-125.zip PCI 168 download
14*4882a593Smuzhiyun * (XP/Vista do not support this card at all but every Linux distribution
15*4882a593Smuzhiyun * has very good support out of the box;
16*4882a593Smuzhiyun * just to make sure that the right people hit this and get to know that,
17*4882a593Smuzhiyun * despite the high level of Internet ignorance - as usual :-P -
18*4882a593Smuzhiyun * about very good support for this card - on Linux!)
19*4882a593Smuzhiyun *
20*4882a593Smuzhiyun * NOTES
21*4882a593Smuzhiyun * Since Aztech does not provide any chipset documentation,
22*4882a593Smuzhiyun * even on repeated request to various addresses,
23*4882a593Smuzhiyun * and the answer that was finally given was negative
24*4882a593Smuzhiyun * (and I was stupid enough to manage to get hold of a PCI168 soundcard
25*4882a593Smuzhiyun * in the first place >:-P}),
26*4882a593Smuzhiyun * I was forced to base this driver on reverse engineering
27*4882a593Smuzhiyun * (3 weeks' worth of evenings filled with driver work).
28*4882a593Smuzhiyun * (and no, I did NOT go the easy way: to pick up a SB PCI128 for 9 Euros)
29*4882a593Smuzhiyun *
30*4882a593Smuzhiyun * It is quite likely that the AZF3328 chip is the PCI cousin of the
31*4882a593Smuzhiyun * AZF3318 ("azt1020 pnp", "MM Pro 16") ISA chip, given very similar specs.
32*4882a593Smuzhiyun *
33*4882a593Smuzhiyun * The AZF3328 chip (note: AZF3328, *not* AZT3328, that's just the driver name
34*4882a593Smuzhiyun * for compatibility reasons) from Azfin (joint-venture of Aztech and Fincitec,
35*4882a593Smuzhiyun * Fincitec acquired by National Semiconductor in 2002, together with the
36*4882a593Smuzhiyun * Fincitec-related company ARSmikro) has the following features:
37*4882a593Smuzhiyun *
38*4882a593Smuzhiyun * - compatibility & compliance:
39*4882a593Smuzhiyun * - Microsoft PC 97 ("PC 97 Hardware Design Guide",
40*4882a593Smuzhiyun * http://www.microsoft.com/whdc/archive/pcguides.mspx)
41*4882a593Smuzhiyun * - Microsoft PC 98 Baseline Audio
42*4882a593Smuzhiyun * - MPU401 UART
43*4882a593Smuzhiyun * - Sound Blaster Emulation (DOS Box)
44*4882a593Smuzhiyun * - builtin AC97 conformant codec (SNR over 80dB)
45*4882a593Smuzhiyun * Note that "conformant" != "compliant"!! this chip's mixer register layout
46*4882a593Smuzhiyun * *differs* from the standard AC97 layout:
47*4882a593Smuzhiyun * they chose to not implement the headphone register (which is not a
48*4882a593Smuzhiyun * problem since it's merely optional), yet when doing this, they committed
49*4882a593Smuzhiyun * the grave sin of letting other registers follow immediately instead of
50*4882a593Smuzhiyun * keeping a headphone dummy register, thereby shifting the mixer register
51*4882a593Smuzhiyun * addresses illegally. So far unfortunately it looks like the very flexible
52*4882a593Smuzhiyun * ALSA AC97 support is still not enough to easily compensate for such a
53*4882a593Smuzhiyun * grave layout violation despite all tweaks and quirks mechanisms it offers.
54*4882a593Smuzhiyun * Well, not quite: now ac97 layer is much improved (bus-specific ops!),
55*4882a593Smuzhiyun * thus I was able to implement support - it's actually working quite well.
56*4882a593Smuzhiyun * An interesting item might be Aztech AMR 2800-W, since it's an AC97
57*4882a593Smuzhiyun * modem card which might reveal the Aztech-specific codec ID which
58*4882a593Smuzhiyun * we might want to pretend, too. Dito PCI168's brother, PCI368,
59*4882a593Smuzhiyun * where the advertising datasheet says it's AC97-based and has a
60*4882a593Smuzhiyun * Digital Enhanced Game Port.
61*4882a593Smuzhiyun * - builtin genuine OPL3 - verified to work fine, 20080506
62*4882a593Smuzhiyun * - full duplex 16bit playback/record at independent sampling rate
63*4882a593Smuzhiyun * - MPU401 (+ legacy address support, claimed by one official spec sheet)
64*4882a593Smuzhiyun * FIXME: how to enable legacy addr??
65*4882a593Smuzhiyun * - game port (legacy address support)
66*4882a593Smuzhiyun * - builtin DirectInput support, helps reduce CPU overhead (interrupt-driven
67*4882a593Smuzhiyun * features supported). - See common term "Digital Enhanced Game Port"...
68*4882a593Smuzhiyun * (probably DirectInput 3.0 spec - confirm)
69*4882a593Smuzhiyun * - builtin 3D enhancement (said to be YAMAHA Ymersion)
70*4882a593Smuzhiyun * - built-in General DirectX timer having a 20 bits counter
71*4882a593Smuzhiyun * with 1us resolution (see below!)
72*4882a593Smuzhiyun * - I2S serial output port for external DAC
73*4882a593Smuzhiyun * [FIXME: 3.3V or 5V level? maximum rate is 66.2kHz right?]
74*4882a593Smuzhiyun * - supports 33MHz PCI spec 2.1, PCI power management 1.0, compliant with ACPI
75*4882a593Smuzhiyun * - supports hardware volume control
76*4882a593Smuzhiyun * - single chip low cost solution (128 pin QFP)
77*4882a593Smuzhiyun * - supports programmable Sub-vendor and Sub-system ID [24C02 SEEPROM chip]
78*4882a593Smuzhiyun * required for Microsoft's logo compliance (FIXME: where?)
79*4882a593Smuzhiyun * At least the Trident 4D Wave DX has one bit somewhere
80*4882a593Smuzhiyun * to enable writes to PCI subsystem VID registers, that should be it.
81*4882a593Smuzhiyun * This might easily be in extended PCI reg space, since PCI168 also has
82*4882a593Smuzhiyun * some custom data starting at 0x80. What kind of config settings
83*4882a593Smuzhiyun * are located in our extended PCI space anyway??
84*4882a593Smuzhiyun * - PCI168 AP(W) card: power amplifier with 4 Watts/channel at 4 Ohms
85*4882a593Smuzhiyun * [TDA1517P chip]
86*4882a593Smuzhiyun *
87*4882a593Smuzhiyun * Note that this driver now is actually *better* than the Windows driver,
88*4882a593Smuzhiyun * since it additionally supports the card's 1MHz DirectX timer - just try
89*4882a593Smuzhiyun * the following snd-seq module parameters etc.:
90*4882a593Smuzhiyun * - options snd-seq seq_default_timer_class=2 seq_default_timer_sclass=0
91*4882a593Smuzhiyun * seq_default_timer_card=0 seq_client_load=1 seq_default_timer_device=0
92*4882a593Smuzhiyun * seq_default_timer_subdevice=0 seq_default_timer_resolution=1000000
93*4882a593Smuzhiyun * - "timidity -iAv -B2,8 -Os -EFreverb=0"
94*4882a593Smuzhiyun * - "pmidi -p 128:0 jazz.mid"
95*4882a593Smuzhiyun *
96*4882a593Smuzhiyun * OPL3 hardware playback testing, try something like:
97*4882a593Smuzhiyun * cat /proc/asound/hwdep
98*4882a593Smuzhiyun * and
99*4882a593Smuzhiyun * aconnect -o
100*4882a593Smuzhiyun * Then use
101*4882a593Smuzhiyun * sbiload -Dhw:x,y --opl3 /usr/share/sounds/opl3/std.o3 ......./drums.o3
102*4882a593Smuzhiyun * where x,y is the xx-yy number as given in hwdep.
103*4882a593Smuzhiyun * Then try
104*4882a593Smuzhiyun * pmidi -p a:b jazz.mid
105*4882a593Smuzhiyun * where a:b is the client number plus 0 usually, as given by aconnect above.
106*4882a593Smuzhiyun * Oh, and make sure to unmute the FM mixer control (doh!)
107*4882a593Smuzhiyun * NOTE: power use during OPL3 playback is _VERY_ high (70W --> 90W!)
108*4882a593Smuzhiyun * despite no CPU activity, possibly due to hindering ACPI idling somehow.
109*4882a593Smuzhiyun * Shouldn't be a problem of the AZF3328 chip itself, I'd hope.
110*4882a593Smuzhiyun * Higher PCM / FM mixer levels seem to conflict (causes crackling),
111*4882a593Smuzhiyun * at least sometimes. Maybe even use with hardware sequencer timer above :)
112*4882a593Smuzhiyun * adplay/adplug-utils might soon offer hardware-based OPL3 playback, too.
113*4882a593Smuzhiyun *
114*4882a593Smuzhiyun * Certain PCI versions of this card are susceptible to DMA traffic underruns
115*4882a593Smuzhiyun * in some systems (resulting in sound crackling/clicking/popping),
116*4882a593Smuzhiyun * probably because they don't have a DMA FIFO buffer or so.
117*4882a593Smuzhiyun * Overview (PCI ID/PCI subID/PCI rev.):
118*4882a593Smuzhiyun * - no DMA crackling on SiS735: 0x50DC/0x1801/16
119*4882a593Smuzhiyun * - unknown performance: 0x50DC/0x1801/10
120*4882a593Smuzhiyun * (well, it's not bad on an Athlon 1800 with now very optimized IRQ handler)
121*4882a593Smuzhiyun *
122*4882a593Smuzhiyun * Crackling happens with VIA chipsets or, in my case, an SiS735, which is
123*4882a593Smuzhiyun * supposed to be very fast and supposed to get rid of crackling much
124*4882a593Smuzhiyun * better than a VIA, yet ironically I still get crackling, like many other
125*4882a593Smuzhiyun * people with the same chipset.
126*4882a593Smuzhiyun * Possible remedies:
127*4882a593Smuzhiyun * - use speaker (amplifier) output instead of headphone output
128*4882a593Smuzhiyun * (in case crackling is due to overloaded output clipping)
129*4882a593Smuzhiyun * - plug card into a different PCI slot, preferably one that isn't shared
130*4882a593Smuzhiyun * too much (this helps a lot, but not completely!)
131*4882a593Smuzhiyun * - get rid of PCI VGA card, use AGP instead
132*4882a593Smuzhiyun * - upgrade or downgrade BIOS
133*4882a593Smuzhiyun * - fiddle with PCI latency settings (setpci -v -s BUSID latency_timer=XX)
134*4882a593Smuzhiyun * Not too helpful.
135*4882a593Smuzhiyun * - Disable ACPI/power management/"Auto Detect RAM/PCI Clk" in BIOS
136*4882a593Smuzhiyun *
137*4882a593Smuzhiyun * BUGS
138*4882a593Smuzhiyun * - full-duplex might *still* be problematic, however a recent test was fine
139*4882a593Smuzhiyun * - (non-bug) "Bass/Treble or 3D settings don't work" - they do get evaluated
140*4882a593Smuzhiyun * if you set PCM output switch to "pre 3D" instead of "post 3D".
141*4882a593Smuzhiyun * If this can't be set, then get a mixer application that Isn't Stupid (tm)
142*4882a593Smuzhiyun * (e.g. kmix, gamix) - unfortunately several are!!
143*4882a593Smuzhiyun * - locking is not entirely clean, especially the audio stream activity
144*4882a593Smuzhiyun * ints --> may be racy
145*4882a593Smuzhiyun * - an _unconnected_ secondary joystick at the gameport will be reported
146*4882a593Smuzhiyun * to be "active" (floating values, not precisely -1) due to the way we need
147*4882a593Smuzhiyun * to read the Digital Enhanced Game Port. Not sure whether it is fixable.
148*4882a593Smuzhiyun *
149*4882a593Smuzhiyun * TODO
150*4882a593Smuzhiyun * - use PCI_VDEVICE
151*4882a593Smuzhiyun * - verify driver status on x86_64
152*4882a593Smuzhiyun * - test multi-card driver operation
153*4882a593Smuzhiyun * - (ab)use 1MHz DirectX timer as kernel clocksource
154*4882a593Smuzhiyun * - test MPU401 MIDI playback etc.
155*4882a593Smuzhiyun * - add more power micro-management (disable various units of the card
156*4882a593Smuzhiyun * as long as they're unused, to improve audio quality and save power).
157*4882a593Smuzhiyun * However this requires more I/O ports which I haven't figured out yet
158*4882a593Smuzhiyun * and which thus might not even exist...
159*4882a593Smuzhiyun * The standard suspend/resume functionality could probably make use of
160*4882a593Smuzhiyun * some improvement, too...
161*4882a593Smuzhiyun * - figure out what all unknown port bits are responsible for
162*4882a593Smuzhiyun * - figure out some cleverly evil scheme to possibly make ALSA AC97 code
163*4882a593Smuzhiyun * fully accept our quite incompatible ""AC97"" mixer and thus save some
164*4882a593Smuzhiyun * code (but I'm not too optimistic that doing this is possible at all)
165*4882a593Smuzhiyun * - use MMIO (memory-mapped I/O)? Slightly faster access, e.g. for gameport.
166*4882a593Smuzhiyun */
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun #include <linux/io.h>
169*4882a593Smuzhiyun #include <linux/init.h>
170*4882a593Smuzhiyun #include <linux/bug.h> /* WARN_ONCE */
171*4882a593Smuzhiyun #include <linux/pci.h>
172*4882a593Smuzhiyun #include <linux/delay.h>
173*4882a593Smuzhiyun #include <linux/slab.h>
174*4882a593Smuzhiyun #include <linux/gameport.h>
175*4882a593Smuzhiyun #include <linux/module.h>
176*4882a593Smuzhiyun #include <linux/dma-mapping.h>
177*4882a593Smuzhiyun #include <sound/core.h>
178*4882a593Smuzhiyun #include <sound/control.h>
179*4882a593Smuzhiyun #include <sound/pcm.h>
180*4882a593Smuzhiyun #include <sound/rawmidi.h>
181*4882a593Smuzhiyun #include <sound/mpu401.h>
182*4882a593Smuzhiyun #include <sound/opl3.h>
183*4882a593Smuzhiyun #include <sound/initval.h>
184*4882a593Smuzhiyun /*
185*4882a593Smuzhiyun * Config switch, to use ALSA's AC97 layer instead of old custom mixer crap.
186*4882a593Smuzhiyun * If the AC97 compatibility parts we needed to implement locally turn out
187*4882a593Smuzhiyun * to work nicely, then remove the old implementation eventually.
188*4882a593Smuzhiyun */
189*4882a593Smuzhiyun #define AZF_USE_AC97_LAYER 1
190*4882a593Smuzhiyun
191*4882a593Smuzhiyun #ifdef AZF_USE_AC97_LAYER
192*4882a593Smuzhiyun #include <sound/ac97_codec.h>
193*4882a593Smuzhiyun #endif
194*4882a593Smuzhiyun #include "azt3328.h"
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun MODULE_AUTHOR("Andreas Mohr <andi AT lisas.de>");
197*4882a593Smuzhiyun MODULE_DESCRIPTION("Aztech AZF3328 (PCI168)");
198*4882a593Smuzhiyun MODULE_LICENSE("GPL");
199*4882a593Smuzhiyun MODULE_SUPPORTED_DEVICE("{{Aztech,AZF3328}}");
200*4882a593Smuzhiyun
201*4882a593Smuzhiyun #if IS_REACHABLE(CONFIG_GAMEPORT)
202*4882a593Smuzhiyun #define SUPPORT_GAMEPORT 1
203*4882a593Smuzhiyun #endif
204*4882a593Smuzhiyun
205*4882a593Smuzhiyun /* === Debug settings ===
206*4882a593Smuzhiyun Further diagnostic functionality than the settings below
207*4882a593Smuzhiyun does not need to be provided, since one can easily write a POSIX shell script
208*4882a593Smuzhiyun to dump the card's I/O ports (those listed in lspci -v -v):
209*4882a593Smuzhiyun dump()
210*4882a593Smuzhiyun {
211*4882a593Smuzhiyun local descr=$1; local addr=$2; local count=$3
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun echo "${descr}: ${count} @ ${addr}:"
214*4882a593Smuzhiyun dd if=/dev/port skip=`printf %d ${addr}` count=${count} bs=1 \
215*4882a593Smuzhiyun 2>/dev/null| hexdump -C
216*4882a593Smuzhiyun }
217*4882a593Smuzhiyun and then use something like
218*4882a593Smuzhiyun "dump joy200 0x200 8", "dump mpu388 0x388 4", "dump joy 0xb400 8",
219*4882a593Smuzhiyun "dump codec00 0xa800 32", "dump mixer 0xb800 64", "dump synth 0xbc00 8",
220*4882a593Smuzhiyun possibly within a "while true; do ... sleep 1; done" loop.
221*4882a593Smuzhiyun Tweaking ports could be done using
222*4882a593Smuzhiyun VALSTRING="`printf "%02x" $value`"
223*4882a593Smuzhiyun printf "\x""$VALSTRING"|dd of=/dev/port seek=`printf %d ${addr}` bs=1 \
224*4882a593Smuzhiyun 2>/dev/null
225*4882a593Smuzhiyun */
226*4882a593Smuzhiyun
227*4882a593Smuzhiyun static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
228*4882a593Smuzhiyun module_param_array(index, int, NULL, 0444);
229*4882a593Smuzhiyun MODULE_PARM_DESC(index, "Index value for AZF3328 soundcard.");
230*4882a593Smuzhiyun
231*4882a593Smuzhiyun static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
232*4882a593Smuzhiyun module_param_array(id, charp, NULL, 0444);
233*4882a593Smuzhiyun MODULE_PARM_DESC(id, "ID string for AZF3328 soundcard.");
234*4882a593Smuzhiyun
235*4882a593Smuzhiyun static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
236*4882a593Smuzhiyun module_param_array(enable, bool, NULL, 0444);
237*4882a593Smuzhiyun MODULE_PARM_DESC(enable, "Enable AZF3328 soundcard.");
238*4882a593Smuzhiyun
239*4882a593Smuzhiyun static int seqtimer_scaling = 128;
240*4882a593Smuzhiyun module_param(seqtimer_scaling, int, 0444);
241*4882a593Smuzhiyun MODULE_PARM_DESC(seqtimer_scaling, "Set 1024000Hz sequencer timer scale factor (lockup danger!). Default 128.");
242*4882a593Smuzhiyun
243*4882a593Smuzhiyun enum snd_azf3328_codec_type {
244*4882a593Smuzhiyun /* warning: fixed indices (also used for bitmask checks!) */
245*4882a593Smuzhiyun AZF_CODEC_PLAYBACK = 0,
246*4882a593Smuzhiyun AZF_CODEC_CAPTURE = 1,
247*4882a593Smuzhiyun AZF_CODEC_I2S_OUT = 2,
248*4882a593Smuzhiyun };
249*4882a593Smuzhiyun
250*4882a593Smuzhiyun struct snd_azf3328_codec_data {
251*4882a593Smuzhiyun unsigned long io_base; /* keep first! (avoid offset calc) */
252*4882a593Smuzhiyun unsigned int dma_base; /* helper to avoid an indirection in hotpath */
253*4882a593Smuzhiyun spinlock_t *lock; /* TODO: convert to our own per-codec lock member */
254*4882a593Smuzhiyun struct snd_pcm_substream *substream;
255*4882a593Smuzhiyun bool running;
256*4882a593Smuzhiyun enum snd_azf3328_codec_type type;
257*4882a593Smuzhiyun const char *name;
258*4882a593Smuzhiyun };
259*4882a593Smuzhiyun
260*4882a593Smuzhiyun struct snd_azf3328 {
261*4882a593Smuzhiyun /* often-used fields towards beginning, then grouped */
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun unsigned long ctrl_io; /* usually 0xb000, size 128 */
264*4882a593Smuzhiyun unsigned long game_io; /* usually 0xb400, size 8 */
265*4882a593Smuzhiyun unsigned long mpu_io; /* usually 0xb800, size 4 */
266*4882a593Smuzhiyun unsigned long opl3_io; /* usually 0xbc00, size 8 */
267*4882a593Smuzhiyun unsigned long mixer_io; /* usually 0xc000, size 64 */
268*4882a593Smuzhiyun
269*4882a593Smuzhiyun spinlock_t reg_lock;
270*4882a593Smuzhiyun
271*4882a593Smuzhiyun struct snd_timer *timer;
272*4882a593Smuzhiyun
273*4882a593Smuzhiyun struct snd_pcm *pcm[3];
274*4882a593Smuzhiyun
275*4882a593Smuzhiyun /* playback, recording and I2S out codecs */
276*4882a593Smuzhiyun struct snd_azf3328_codec_data codecs[3];
277*4882a593Smuzhiyun
278*4882a593Smuzhiyun #ifdef AZF_USE_AC97_LAYER
279*4882a593Smuzhiyun struct snd_ac97 *ac97;
280*4882a593Smuzhiyun #endif
281*4882a593Smuzhiyun
282*4882a593Smuzhiyun struct snd_card *card;
283*4882a593Smuzhiyun struct snd_rawmidi *rmidi;
284*4882a593Smuzhiyun
285*4882a593Smuzhiyun #ifdef SUPPORT_GAMEPORT
286*4882a593Smuzhiyun struct gameport *gameport;
287*4882a593Smuzhiyun u16 axes[4];
288*4882a593Smuzhiyun #endif
289*4882a593Smuzhiyun
290*4882a593Smuzhiyun struct pci_dev *pci;
291*4882a593Smuzhiyun int irq;
292*4882a593Smuzhiyun
293*4882a593Smuzhiyun /* register 0x6a is write-only, thus need to remember setting.
294*4882a593Smuzhiyun * If we need to add more registers here, then we might try to fold this
295*4882a593Smuzhiyun * into some transparent combined shadow register handling with
296*4882a593Smuzhiyun * CONFIG_PM register storage below, but that's slightly difficult. */
297*4882a593Smuzhiyun u16 shadow_reg_ctrl_6AH;
298*4882a593Smuzhiyun
299*4882a593Smuzhiyun #ifdef CONFIG_PM_SLEEP
300*4882a593Smuzhiyun /* register value containers for power management
301*4882a593Smuzhiyun * Note: not always full I/O range preserved (similar to Win driver!) */
302*4882a593Smuzhiyun u32 saved_regs_ctrl[AZF_ALIGN(AZF_IO_SIZE_CTRL_PM) / 4];
303*4882a593Smuzhiyun u32 saved_regs_game[AZF_ALIGN(AZF_IO_SIZE_GAME_PM) / 4];
304*4882a593Smuzhiyun u32 saved_regs_mpu[AZF_ALIGN(AZF_IO_SIZE_MPU_PM) / 4];
305*4882a593Smuzhiyun u32 saved_regs_opl3[AZF_ALIGN(AZF_IO_SIZE_OPL3_PM) / 4];
306*4882a593Smuzhiyun u32 saved_regs_mixer[AZF_ALIGN(AZF_IO_SIZE_MIXER_PM) / 4];
307*4882a593Smuzhiyun #endif
308*4882a593Smuzhiyun };
309*4882a593Smuzhiyun
310*4882a593Smuzhiyun static const struct pci_device_id snd_azf3328_ids[] = {
311*4882a593Smuzhiyun { 0x122D, 0x50DC, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, /* PCI168/3328 */
312*4882a593Smuzhiyun { 0x122D, 0x80DA, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, /* 3328 */
313*4882a593Smuzhiyun { 0, }
314*4882a593Smuzhiyun };
315*4882a593Smuzhiyun
316*4882a593Smuzhiyun MODULE_DEVICE_TABLE(pci, snd_azf3328_ids);
317*4882a593Smuzhiyun
318*4882a593Smuzhiyun
319*4882a593Smuzhiyun static int
snd_azf3328_io_reg_setb(unsigned reg,u8 mask,bool do_set)320*4882a593Smuzhiyun snd_azf3328_io_reg_setb(unsigned reg, u8 mask, bool do_set)
321*4882a593Smuzhiyun {
322*4882a593Smuzhiyun /* Well, strictly spoken, the inb/outb sequence isn't atomic
323*4882a593Smuzhiyun and would need locking. However we currently don't care
324*4882a593Smuzhiyun since it potentially complicates matters. */
325*4882a593Smuzhiyun u8 prev = inb(reg), new;
326*4882a593Smuzhiyun
327*4882a593Smuzhiyun new = (do_set) ? (prev|mask) : (prev & ~mask);
328*4882a593Smuzhiyun /* we need to always write the new value no matter whether it differs
329*4882a593Smuzhiyun * or not, since some register bits don't indicate their setting */
330*4882a593Smuzhiyun outb(new, reg);
331*4882a593Smuzhiyun if (new != prev)
332*4882a593Smuzhiyun return 1;
333*4882a593Smuzhiyun
334*4882a593Smuzhiyun return 0;
335*4882a593Smuzhiyun }
336*4882a593Smuzhiyun
337*4882a593Smuzhiyun static inline void
snd_azf3328_codec_outb(const struct snd_azf3328_codec_data * codec,unsigned reg,u8 value)338*4882a593Smuzhiyun snd_azf3328_codec_outb(const struct snd_azf3328_codec_data *codec,
339*4882a593Smuzhiyun unsigned reg,
340*4882a593Smuzhiyun u8 value
341*4882a593Smuzhiyun )
342*4882a593Smuzhiyun {
343*4882a593Smuzhiyun outb(value, codec->io_base + reg);
344*4882a593Smuzhiyun }
345*4882a593Smuzhiyun
346*4882a593Smuzhiyun static inline u8
snd_azf3328_codec_inb(const struct snd_azf3328_codec_data * codec,unsigned reg)347*4882a593Smuzhiyun snd_azf3328_codec_inb(const struct snd_azf3328_codec_data *codec, unsigned reg)
348*4882a593Smuzhiyun {
349*4882a593Smuzhiyun return inb(codec->io_base + reg);
350*4882a593Smuzhiyun }
351*4882a593Smuzhiyun
352*4882a593Smuzhiyun static inline void
snd_azf3328_codec_outw(const struct snd_azf3328_codec_data * codec,unsigned reg,u16 value)353*4882a593Smuzhiyun snd_azf3328_codec_outw(const struct snd_azf3328_codec_data *codec,
354*4882a593Smuzhiyun unsigned reg,
355*4882a593Smuzhiyun u16 value
356*4882a593Smuzhiyun )
357*4882a593Smuzhiyun {
358*4882a593Smuzhiyun outw(value, codec->io_base + reg);
359*4882a593Smuzhiyun }
360*4882a593Smuzhiyun
361*4882a593Smuzhiyun static inline u16
snd_azf3328_codec_inw(const struct snd_azf3328_codec_data * codec,unsigned reg)362*4882a593Smuzhiyun snd_azf3328_codec_inw(const struct snd_azf3328_codec_data *codec, unsigned reg)
363*4882a593Smuzhiyun {
364*4882a593Smuzhiyun return inw(codec->io_base + reg);
365*4882a593Smuzhiyun }
366*4882a593Smuzhiyun
367*4882a593Smuzhiyun static inline void
snd_azf3328_codec_outl(const struct snd_azf3328_codec_data * codec,unsigned reg,u32 value)368*4882a593Smuzhiyun snd_azf3328_codec_outl(const struct snd_azf3328_codec_data *codec,
369*4882a593Smuzhiyun unsigned reg,
370*4882a593Smuzhiyun u32 value
371*4882a593Smuzhiyun )
372*4882a593Smuzhiyun {
373*4882a593Smuzhiyun outl(value, codec->io_base + reg);
374*4882a593Smuzhiyun }
375*4882a593Smuzhiyun
376*4882a593Smuzhiyun static inline void
snd_azf3328_codec_outl_multi(const struct snd_azf3328_codec_data * codec,unsigned reg,const void * buffer,int count)377*4882a593Smuzhiyun snd_azf3328_codec_outl_multi(const struct snd_azf3328_codec_data *codec,
378*4882a593Smuzhiyun unsigned reg, const void *buffer, int count
379*4882a593Smuzhiyun )
380*4882a593Smuzhiyun {
381*4882a593Smuzhiyun unsigned long addr = codec->io_base + reg;
382*4882a593Smuzhiyun if (count) {
383*4882a593Smuzhiyun const u32 *buf = buffer;
384*4882a593Smuzhiyun do {
385*4882a593Smuzhiyun outl(*buf++, addr);
386*4882a593Smuzhiyun addr += 4;
387*4882a593Smuzhiyun } while (--count);
388*4882a593Smuzhiyun }
389*4882a593Smuzhiyun }
390*4882a593Smuzhiyun
391*4882a593Smuzhiyun static inline u32
snd_azf3328_codec_inl(const struct snd_azf3328_codec_data * codec,unsigned reg)392*4882a593Smuzhiyun snd_azf3328_codec_inl(const struct snd_azf3328_codec_data *codec, unsigned reg)
393*4882a593Smuzhiyun {
394*4882a593Smuzhiyun return inl(codec->io_base + reg);
395*4882a593Smuzhiyun }
396*4882a593Smuzhiyun
397*4882a593Smuzhiyun static inline void
snd_azf3328_ctrl_outb(const struct snd_azf3328 * chip,unsigned reg,u8 value)398*4882a593Smuzhiyun snd_azf3328_ctrl_outb(const struct snd_azf3328 *chip, unsigned reg, u8 value)
399*4882a593Smuzhiyun {
400*4882a593Smuzhiyun outb(value, chip->ctrl_io + reg);
401*4882a593Smuzhiyun }
402*4882a593Smuzhiyun
403*4882a593Smuzhiyun static inline u8
snd_azf3328_ctrl_inb(const struct snd_azf3328 * chip,unsigned reg)404*4882a593Smuzhiyun snd_azf3328_ctrl_inb(const struct snd_azf3328 *chip, unsigned reg)
405*4882a593Smuzhiyun {
406*4882a593Smuzhiyun return inb(chip->ctrl_io + reg);
407*4882a593Smuzhiyun }
408*4882a593Smuzhiyun
409*4882a593Smuzhiyun static inline u16
snd_azf3328_ctrl_inw(const struct snd_azf3328 * chip,unsigned reg)410*4882a593Smuzhiyun snd_azf3328_ctrl_inw(const struct snd_azf3328 *chip, unsigned reg)
411*4882a593Smuzhiyun {
412*4882a593Smuzhiyun return inw(chip->ctrl_io + reg);
413*4882a593Smuzhiyun }
414*4882a593Smuzhiyun
415*4882a593Smuzhiyun static inline void
snd_azf3328_ctrl_outw(const struct snd_azf3328 * chip,unsigned reg,u16 value)416*4882a593Smuzhiyun snd_azf3328_ctrl_outw(const struct snd_azf3328 *chip, unsigned reg, u16 value)
417*4882a593Smuzhiyun {
418*4882a593Smuzhiyun outw(value, chip->ctrl_io + reg);
419*4882a593Smuzhiyun }
420*4882a593Smuzhiyun
421*4882a593Smuzhiyun static inline void
snd_azf3328_ctrl_outl(const struct snd_azf3328 * chip,unsigned reg,u32 value)422*4882a593Smuzhiyun snd_azf3328_ctrl_outl(const struct snd_azf3328 *chip, unsigned reg, u32 value)
423*4882a593Smuzhiyun {
424*4882a593Smuzhiyun outl(value, chip->ctrl_io + reg);
425*4882a593Smuzhiyun }
426*4882a593Smuzhiyun
427*4882a593Smuzhiyun static inline void
snd_azf3328_game_outb(const struct snd_azf3328 * chip,unsigned reg,u8 value)428*4882a593Smuzhiyun snd_azf3328_game_outb(const struct snd_azf3328 *chip, unsigned reg, u8 value)
429*4882a593Smuzhiyun {
430*4882a593Smuzhiyun outb(value, chip->game_io + reg);
431*4882a593Smuzhiyun }
432*4882a593Smuzhiyun
433*4882a593Smuzhiyun static inline void
snd_azf3328_game_outw(const struct snd_azf3328 * chip,unsigned reg,u16 value)434*4882a593Smuzhiyun snd_azf3328_game_outw(const struct snd_azf3328 *chip, unsigned reg, u16 value)
435*4882a593Smuzhiyun {
436*4882a593Smuzhiyun outw(value, chip->game_io + reg);
437*4882a593Smuzhiyun }
438*4882a593Smuzhiyun
439*4882a593Smuzhiyun static inline u8
snd_azf3328_game_inb(const struct snd_azf3328 * chip,unsigned reg)440*4882a593Smuzhiyun snd_azf3328_game_inb(const struct snd_azf3328 *chip, unsigned reg)
441*4882a593Smuzhiyun {
442*4882a593Smuzhiyun return inb(chip->game_io + reg);
443*4882a593Smuzhiyun }
444*4882a593Smuzhiyun
445*4882a593Smuzhiyun static inline u16
snd_azf3328_game_inw(const struct snd_azf3328 * chip,unsigned reg)446*4882a593Smuzhiyun snd_azf3328_game_inw(const struct snd_azf3328 *chip, unsigned reg)
447*4882a593Smuzhiyun {
448*4882a593Smuzhiyun return inw(chip->game_io + reg);
449*4882a593Smuzhiyun }
450*4882a593Smuzhiyun
451*4882a593Smuzhiyun static inline void
snd_azf3328_mixer_outw(const struct snd_azf3328 * chip,unsigned reg,u16 value)452*4882a593Smuzhiyun snd_azf3328_mixer_outw(const struct snd_azf3328 *chip, unsigned reg, u16 value)
453*4882a593Smuzhiyun {
454*4882a593Smuzhiyun outw(value, chip->mixer_io + reg);
455*4882a593Smuzhiyun }
456*4882a593Smuzhiyun
457*4882a593Smuzhiyun static inline u16
snd_azf3328_mixer_inw(const struct snd_azf3328 * chip,unsigned reg)458*4882a593Smuzhiyun snd_azf3328_mixer_inw(const struct snd_azf3328 *chip, unsigned reg)
459*4882a593Smuzhiyun {
460*4882a593Smuzhiyun return inw(chip->mixer_io + reg);
461*4882a593Smuzhiyun }
462*4882a593Smuzhiyun
463*4882a593Smuzhiyun #define AZF_MUTE_BIT 0x80
464*4882a593Smuzhiyun
465*4882a593Smuzhiyun static bool
snd_azf3328_mixer_mute_control(const struct snd_azf3328 * chip,unsigned reg,bool do_mute)466*4882a593Smuzhiyun snd_azf3328_mixer_mute_control(const struct snd_azf3328 *chip,
467*4882a593Smuzhiyun unsigned reg, bool do_mute
468*4882a593Smuzhiyun )
469*4882a593Smuzhiyun {
470*4882a593Smuzhiyun unsigned long portbase = chip->mixer_io + reg + 1;
471*4882a593Smuzhiyun bool updated;
472*4882a593Smuzhiyun
473*4882a593Smuzhiyun /* the mute bit is on the *second* (i.e. right) register of a
474*4882a593Smuzhiyun * left/right channel setting */
475*4882a593Smuzhiyun updated = snd_azf3328_io_reg_setb(portbase, AZF_MUTE_BIT, do_mute);
476*4882a593Smuzhiyun
477*4882a593Smuzhiyun /* indicate whether it was muted before */
478*4882a593Smuzhiyun return (do_mute) ? !updated : updated;
479*4882a593Smuzhiyun }
480*4882a593Smuzhiyun
481*4882a593Smuzhiyun static inline bool
snd_azf3328_mixer_mute_control_master(const struct snd_azf3328 * chip,bool do_mute)482*4882a593Smuzhiyun snd_azf3328_mixer_mute_control_master(const struct snd_azf3328 *chip,
483*4882a593Smuzhiyun bool do_mute
484*4882a593Smuzhiyun )
485*4882a593Smuzhiyun {
486*4882a593Smuzhiyun return snd_azf3328_mixer_mute_control(
487*4882a593Smuzhiyun chip,
488*4882a593Smuzhiyun IDX_MIXER_PLAY_MASTER,
489*4882a593Smuzhiyun do_mute
490*4882a593Smuzhiyun );
491*4882a593Smuzhiyun }
492*4882a593Smuzhiyun
493*4882a593Smuzhiyun static inline bool
snd_azf3328_mixer_mute_control_pcm(const struct snd_azf3328 * chip,bool do_mute)494*4882a593Smuzhiyun snd_azf3328_mixer_mute_control_pcm(const struct snd_azf3328 *chip,
495*4882a593Smuzhiyun bool do_mute
496*4882a593Smuzhiyun )
497*4882a593Smuzhiyun {
498*4882a593Smuzhiyun return snd_azf3328_mixer_mute_control(
499*4882a593Smuzhiyun chip,
500*4882a593Smuzhiyun IDX_MIXER_WAVEOUT,
501*4882a593Smuzhiyun do_mute
502*4882a593Smuzhiyun );
503*4882a593Smuzhiyun }
504*4882a593Smuzhiyun
505*4882a593Smuzhiyun static inline void
snd_azf3328_mixer_reset(const struct snd_azf3328 * chip)506*4882a593Smuzhiyun snd_azf3328_mixer_reset(const struct snd_azf3328 *chip)
507*4882a593Smuzhiyun {
508*4882a593Smuzhiyun /* reset (close) mixer:
509*4882a593Smuzhiyun * first mute master volume, then reset
510*4882a593Smuzhiyun */
511*4882a593Smuzhiyun snd_azf3328_mixer_mute_control_master(chip, 1);
512*4882a593Smuzhiyun snd_azf3328_mixer_outw(chip, IDX_MIXER_RESET, 0x0000);
513*4882a593Smuzhiyun }
514*4882a593Smuzhiyun
515*4882a593Smuzhiyun #ifdef AZF_USE_AC97_LAYER
516*4882a593Smuzhiyun
517*4882a593Smuzhiyun static inline void
snd_azf3328_mixer_ac97_map_unsupported(const struct snd_azf3328 * chip,unsigned short reg,const char * mode)518*4882a593Smuzhiyun snd_azf3328_mixer_ac97_map_unsupported(const struct snd_azf3328 *chip,
519*4882a593Smuzhiyun unsigned short reg, const char *mode)
520*4882a593Smuzhiyun {
521*4882a593Smuzhiyun /* need to add some more or less clever emulation? */
522*4882a593Smuzhiyun dev_warn(chip->card->dev,
523*4882a593Smuzhiyun "missing %s emulation for AC97 register 0x%02x!\n",
524*4882a593Smuzhiyun mode, reg);
525*4882a593Smuzhiyun }
526*4882a593Smuzhiyun
527*4882a593Smuzhiyun /*
528*4882a593Smuzhiyun * Need to have _special_ AC97 mixer hardware register index mapper,
529*4882a593Smuzhiyun * to compensate for the issue of a rather AC97-incompatible hardware layout.
530*4882a593Smuzhiyun */
531*4882a593Smuzhiyun #define AZF_REG_MASK 0x3f
532*4882a593Smuzhiyun #define AZF_AC97_REG_UNSUPPORTED 0x8000
533*4882a593Smuzhiyun #define AZF_AC97_REG_REAL_IO_READ 0x4000
534*4882a593Smuzhiyun #define AZF_AC97_REG_REAL_IO_WRITE 0x2000
535*4882a593Smuzhiyun #define AZF_AC97_REG_REAL_IO_RW \
536*4882a593Smuzhiyun (AZF_AC97_REG_REAL_IO_READ | AZF_AC97_REG_REAL_IO_WRITE)
537*4882a593Smuzhiyun #define AZF_AC97_REG_EMU_IO_READ 0x0400
538*4882a593Smuzhiyun #define AZF_AC97_REG_EMU_IO_WRITE 0x0200
539*4882a593Smuzhiyun #define AZF_AC97_REG_EMU_IO_RW \
540*4882a593Smuzhiyun (AZF_AC97_REG_EMU_IO_READ | AZF_AC97_REG_EMU_IO_WRITE)
541*4882a593Smuzhiyun static unsigned short
snd_azf3328_mixer_ac97_map_reg_idx(unsigned short reg)542*4882a593Smuzhiyun snd_azf3328_mixer_ac97_map_reg_idx(unsigned short reg)
543*4882a593Smuzhiyun {
544*4882a593Smuzhiyun static const struct {
545*4882a593Smuzhiyun unsigned short azf_reg;
546*4882a593Smuzhiyun } azf_reg_mapper[] = {
547*4882a593Smuzhiyun /* Especially when taking into consideration
548*4882a593Smuzhiyun * mono/stereo-based sequence of azf vs. AC97 control series,
549*4882a593Smuzhiyun * it's quite obvious that azf simply got rid
550*4882a593Smuzhiyun * of the AC97_HEADPHONE control at its intended offset,
551*4882a593Smuzhiyun * thus shifted _all_ controls by one,
552*4882a593Smuzhiyun * and _then_ simply added it as an FMSYNTH control at the end,
553*4882a593Smuzhiyun * to make up for the offset.
554*4882a593Smuzhiyun * This means we'll have to translate indices here as
555*4882a593Smuzhiyun * needed and then do some tiny AC97 patch action
556*4882a593Smuzhiyun * (snd_ac97_rename_vol_ctl() etc.) - that's it.
557*4882a593Smuzhiyun */
558*4882a593Smuzhiyun { /* AC97_RESET */ IDX_MIXER_RESET
559*4882a593Smuzhiyun | AZF_AC97_REG_REAL_IO_WRITE
560*4882a593Smuzhiyun | AZF_AC97_REG_EMU_IO_READ },
561*4882a593Smuzhiyun { /* AC97_MASTER */ IDX_MIXER_PLAY_MASTER },
562*4882a593Smuzhiyun /* note large shift: AC97_HEADPHONE to IDX_MIXER_FMSYNTH! */
563*4882a593Smuzhiyun { /* AC97_HEADPHONE */ IDX_MIXER_FMSYNTH },
564*4882a593Smuzhiyun { /* AC97_MASTER_MONO */ IDX_MIXER_MODEMOUT },
565*4882a593Smuzhiyun { /* AC97_MASTER_TONE */ IDX_MIXER_BASSTREBLE },
566*4882a593Smuzhiyun { /* AC97_PC_BEEP */ IDX_MIXER_PCBEEP },
567*4882a593Smuzhiyun { /* AC97_PHONE */ IDX_MIXER_MODEMIN },
568*4882a593Smuzhiyun { /* AC97_MIC */ IDX_MIXER_MIC },
569*4882a593Smuzhiyun { /* AC97_LINE */ IDX_MIXER_LINEIN },
570*4882a593Smuzhiyun { /* AC97_CD */ IDX_MIXER_CDAUDIO },
571*4882a593Smuzhiyun { /* AC97_VIDEO */ IDX_MIXER_VIDEO },
572*4882a593Smuzhiyun { /* AC97_AUX */ IDX_MIXER_AUX },
573*4882a593Smuzhiyun { /* AC97_PCM */ IDX_MIXER_WAVEOUT },
574*4882a593Smuzhiyun { /* AC97_REC_SEL */ IDX_MIXER_REC_SELECT },
575*4882a593Smuzhiyun { /* AC97_REC_GAIN */ IDX_MIXER_REC_VOLUME },
576*4882a593Smuzhiyun { /* AC97_REC_GAIN_MIC */ AZF_AC97_REG_EMU_IO_RW },
577*4882a593Smuzhiyun { /* AC97_GENERAL_PURPOSE */ IDX_MIXER_ADVCTL2 },
578*4882a593Smuzhiyun { /* AC97_3D_CONTROL */ IDX_MIXER_ADVCTL1 },
579*4882a593Smuzhiyun };
580*4882a593Smuzhiyun
581*4882a593Smuzhiyun unsigned short reg_azf = AZF_AC97_REG_UNSUPPORTED;
582*4882a593Smuzhiyun
583*4882a593Smuzhiyun /* azf3328 supports the low-numbered and low-spec:ed range
584*4882a593Smuzhiyun of AC97 regs only */
585*4882a593Smuzhiyun if (reg <= AC97_3D_CONTROL) {
586*4882a593Smuzhiyun unsigned short reg_idx = reg / 2;
587*4882a593Smuzhiyun reg_azf = azf_reg_mapper[reg_idx].azf_reg;
588*4882a593Smuzhiyun /* a translation-only entry means it's real read/write: */
589*4882a593Smuzhiyun if (!(reg_azf & ~AZF_REG_MASK))
590*4882a593Smuzhiyun reg_azf |= AZF_AC97_REG_REAL_IO_RW;
591*4882a593Smuzhiyun } else {
592*4882a593Smuzhiyun switch (reg) {
593*4882a593Smuzhiyun case AC97_POWERDOWN:
594*4882a593Smuzhiyun reg_azf = AZF_AC97_REG_EMU_IO_RW;
595*4882a593Smuzhiyun break;
596*4882a593Smuzhiyun case AC97_EXTENDED_ID:
597*4882a593Smuzhiyun reg_azf = AZF_AC97_REG_EMU_IO_READ;
598*4882a593Smuzhiyun break;
599*4882a593Smuzhiyun case AC97_EXTENDED_STATUS:
600*4882a593Smuzhiyun /* I don't know what the h*ll AC97 layer
601*4882a593Smuzhiyun * would consult this _extended_ register for
602*4882a593Smuzhiyun * given a base-AC97-advertised card,
603*4882a593Smuzhiyun * but let's just emulate it anyway :-P
604*4882a593Smuzhiyun */
605*4882a593Smuzhiyun reg_azf = AZF_AC97_REG_EMU_IO_RW;
606*4882a593Smuzhiyun break;
607*4882a593Smuzhiyun case AC97_VENDOR_ID1:
608*4882a593Smuzhiyun case AC97_VENDOR_ID2:
609*4882a593Smuzhiyun reg_azf = AZF_AC97_REG_EMU_IO_READ;
610*4882a593Smuzhiyun break;
611*4882a593Smuzhiyun }
612*4882a593Smuzhiyun }
613*4882a593Smuzhiyun return reg_azf;
614*4882a593Smuzhiyun }
615*4882a593Smuzhiyun
616*4882a593Smuzhiyun static const unsigned short
617*4882a593Smuzhiyun azf_emulated_ac97_caps =
618*4882a593Smuzhiyun AC97_BC_DEDICATED_MIC |
619*4882a593Smuzhiyun AC97_BC_BASS_TREBLE |
620*4882a593Smuzhiyun /* Headphone is an FM Synth control here */
621*4882a593Smuzhiyun AC97_BC_HEADPHONE |
622*4882a593Smuzhiyun /* no AC97_BC_LOUDNESS! */
623*4882a593Smuzhiyun /* mask 0x7c00 is
624*4882a593Smuzhiyun vendor-specific 3D enhancement
625*4882a593Smuzhiyun vendor indicator.
626*4882a593Smuzhiyun Since there actually _is_ an
627*4882a593Smuzhiyun entry for Aztech Labs
628*4882a593Smuzhiyun (13), make damn sure
629*4882a593Smuzhiyun to indicate it. */
630*4882a593Smuzhiyun (13 << 10);
631*4882a593Smuzhiyun
632*4882a593Smuzhiyun static const unsigned short
633*4882a593Smuzhiyun azf_emulated_ac97_powerdown =
634*4882a593Smuzhiyun /* pretend everything to be active */
635*4882a593Smuzhiyun AC97_PD_ADC_STATUS |
636*4882a593Smuzhiyun AC97_PD_DAC_STATUS |
637*4882a593Smuzhiyun AC97_PD_MIXER_STATUS |
638*4882a593Smuzhiyun AC97_PD_VREF_STATUS;
639*4882a593Smuzhiyun
640*4882a593Smuzhiyun /*
641*4882a593Smuzhiyun * Emulated, _inofficial_ vendor ID
642*4882a593Smuzhiyun * (there might be some devices such as the MR 2800-W
643*4882a593Smuzhiyun * which could reveal the real Aztech AC97 ID).
644*4882a593Smuzhiyun * We choose to use "AZT" prefix, and then use 1 to indicate PCI168
645*4882a593Smuzhiyun * (better don't use 0x68 since there's a PCI368 as well).
646*4882a593Smuzhiyun */
647*4882a593Smuzhiyun static const unsigned int
648*4882a593Smuzhiyun azf_emulated_ac97_vendor_id = 0x415a5401;
649*4882a593Smuzhiyun
650*4882a593Smuzhiyun static unsigned short
snd_azf3328_mixer_ac97_read(struct snd_ac97 * ac97,unsigned short reg_ac97)651*4882a593Smuzhiyun snd_azf3328_mixer_ac97_read(struct snd_ac97 *ac97, unsigned short reg_ac97)
652*4882a593Smuzhiyun {
653*4882a593Smuzhiyun const struct snd_azf3328 *chip = ac97->private_data;
654*4882a593Smuzhiyun unsigned short reg_azf = snd_azf3328_mixer_ac97_map_reg_idx(reg_ac97);
655*4882a593Smuzhiyun unsigned short reg_val = 0;
656*4882a593Smuzhiyun bool unsupported = false;
657*4882a593Smuzhiyun
658*4882a593Smuzhiyun dev_dbg(chip->card->dev, "snd_azf3328_mixer_ac97_read reg_ac97 %u\n",
659*4882a593Smuzhiyun reg_ac97);
660*4882a593Smuzhiyun if (reg_azf & AZF_AC97_REG_UNSUPPORTED)
661*4882a593Smuzhiyun unsupported = true;
662*4882a593Smuzhiyun else {
663*4882a593Smuzhiyun if (reg_azf & AZF_AC97_REG_REAL_IO_READ)
664*4882a593Smuzhiyun reg_val = snd_azf3328_mixer_inw(chip,
665*4882a593Smuzhiyun reg_azf & AZF_REG_MASK);
666*4882a593Smuzhiyun else {
667*4882a593Smuzhiyun /*
668*4882a593Smuzhiyun * Proceed with dummy I/O read,
669*4882a593Smuzhiyun * to ensure compatible timing where this may matter.
670*4882a593Smuzhiyun * (ALSA AC97 layer usually doesn't call I/O functions
671*4882a593Smuzhiyun * due to intelligent I/O caching anyway)
672*4882a593Smuzhiyun * Choose a mixer register that's thoroughly unrelated
673*4882a593Smuzhiyun * to common audio (try to minimize distortion).
674*4882a593Smuzhiyun */
675*4882a593Smuzhiyun snd_azf3328_mixer_inw(chip, IDX_MIXER_SOMETHING30H);
676*4882a593Smuzhiyun }
677*4882a593Smuzhiyun
678*4882a593Smuzhiyun if (reg_azf & AZF_AC97_REG_EMU_IO_READ) {
679*4882a593Smuzhiyun switch (reg_ac97) {
680*4882a593Smuzhiyun case AC97_RESET:
681*4882a593Smuzhiyun reg_val |= azf_emulated_ac97_caps;
682*4882a593Smuzhiyun break;
683*4882a593Smuzhiyun case AC97_POWERDOWN:
684*4882a593Smuzhiyun reg_val |= azf_emulated_ac97_powerdown;
685*4882a593Smuzhiyun break;
686*4882a593Smuzhiyun case AC97_EXTENDED_ID:
687*4882a593Smuzhiyun case AC97_EXTENDED_STATUS:
688*4882a593Smuzhiyun /* AFAICS we simply can't support anything: */
689*4882a593Smuzhiyun reg_val |= 0;
690*4882a593Smuzhiyun break;
691*4882a593Smuzhiyun case AC97_VENDOR_ID1:
692*4882a593Smuzhiyun reg_val = azf_emulated_ac97_vendor_id >> 16;
693*4882a593Smuzhiyun break;
694*4882a593Smuzhiyun case AC97_VENDOR_ID2:
695*4882a593Smuzhiyun reg_val = azf_emulated_ac97_vendor_id & 0xffff;
696*4882a593Smuzhiyun break;
697*4882a593Smuzhiyun default:
698*4882a593Smuzhiyun unsupported = true;
699*4882a593Smuzhiyun break;
700*4882a593Smuzhiyun }
701*4882a593Smuzhiyun }
702*4882a593Smuzhiyun }
703*4882a593Smuzhiyun if (unsupported)
704*4882a593Smuzhiyun snd_azf3328_mixer_ac97_map_unsupported(chip, reg_ac97, "read");
705*4882a593Smuzhiyun
706*4882a593Smuzhiyun return reg_val;
707*4882a593Smuzhiyun }
708*4882a593Smuzhiyun
709*4882a593Smuzhiyun static void
snd_azf3328_mixer_ac97_write(struct snd_ac97 * ac97,unsigned short reg_ac97,unsigned short val)710*4882a593Smuzhiyun snd_azf3328_mixer_ac97_write(struct snd_ac97 *ac97,
711*4882a593Smuzhiyun unsigned short reg_ac97, unsigned short val)
712*4882a593Smuzhiyun {
713*4882a593Smuzhiyun const struct snd_azf3328 *chip = ac97->private_data;
714*4882a593Smuzhiyun unsigned short reg_azf = snd_azf3328_mixer_ac97_map_reg_idx(reg_ac97);
715*4882a593Smuzhiyun bool unsupported = false;
716*4882a593Smuzhiyun
717*4882a593Smuzhiyun dev_dbg(chip->card->dev,
718*4882a593Smuzhiyun "snd_azf3328_mixer_ac97_write reg_ac97 %u val %u\n",
719*4882a593Smuzhiyun reg_ac97, val);
720*4882a593Smuzhiyun if (reg_azf & AZF_AC97_REG_UNSUPPORTED)
721*4882a593Smuzhiyun unsupported = true;
722*4882a593Smuzhiyun else {
723*4882a593Smuzhiyun if (reg_azf & AZF_AC97_REG_REAL_IO_WRITE)
724*4882a593Smuzhiyun snd_azf3328_mixer_outw(
725*4882a593Smuzhiyun chip,
726*4882a593Smuzhiyun reg_azf & AZF_REG_MASK,
727*4882a593Smuzhiyun val
728*4882a593Smuzhiyun );
729*4882a593Smuzhiyun else
730*4882a593Smuzhiyun if (reg_azf & AZF_AC97_REG_EMU_IO_WRITE) {
731*4882a593Smuzhiyun switch (reg_ac97) {
732*4882a593Smuzhiyun case AC97_REC_GAIN_MIC:
733*4882a593Smuzhiyun case AC97_POWERDOWN:
734*4882a593Smuzhiyun case AC97_EXTENDED_STATUS:
735*4882a593Smuzhiyun /*
736*4882a593Smuzhiyun * Silently swallow these writes.
737*4882a593Smuzhiyun * Since for most registers our card doesn't
738*4882a593Smuzhiyun * actually support a comparable feature,
739*4882a593Smuzhiyun * this is exactly what we should do here.
740*4882a593Smuzhiyun * The AC97 layer's I/O caching probably
741*4882a593Smuzhiyun * automatically takes care of all the rest...
742*4882a593Smuzhiyun * (remembers written values etc.)
743*4882a593Smuzhiyun */
744*4882a593Smuzhiyun break;
745*4882a593Smuzhiyun default:
746*4882a593Smuzhiyun unsupported = true;
747*4882a593Smuzhiyun break;
748*4882a593Smuzhiyun }
749*4882a593Smuzhiyun }
750*4882a593Smuzhiyun }
751*4882a593Smuzhiyun if (unsupported)
752*4882a593Smuzhiyun snd_azf3328_mixer_ac97_map_unsupported(chip, reg_ac97, "write");
753*4882a593Smuzhiyun }
754*4882a593Smuzhiyun
755*4882a593Smuzhiyun static int
snd_azf3328_mixer_new(struct snd_azf3328 * chip)756*4882a593Smuzhiyun snd_azf3328_mixer_new(struct snd_azf3328 *chip)
757*4882a593Smuzhiyun {
758*4882a593Smuzhiyun struct snd_ac97_bus *bus;
759*4882a593Smuzhiyun struct snd_ac97_template ac97;
760*4882a593Smuzhiyun static const struct snd_ac97_bus_ops ops = {
761*4882a593Smuzhiyun .write = snd_azf3328_mixer_ac97_write,
762*4882a593Smuzhiyun .read = snd_azf3328_mixer_ac97_read,
763*4882a593Smuzhiyun };
764*4882a593Smuzhiyun int rc;
765*4882a593Smuzhiyun
766*4882a593Smuzhiyun memset(&ac97, 0, sizeof(ac97));
767*4882a593Smuzhiyun ac97.scaps = AC97_SCAP_SKIP_MODEM
768*4882a593Smuzhiyun | AC97_SCAP_AUDIO /* we support audio! */
769*4882a593Smuzhiyun | AC97_SCAP_NO_SPDIF;
770*4882a593Smuzhiyun ac97.private_data = chip;
771*4882a593Smuzhiyun ac97.pci = chip->pci;
772*4882a593Smuzhiyun
773*4882a593Smuzhiyun /*
774*4882a593Smuzhiyun * ALSA's AC97 layer has terrible init crackling issues,
775*4882a593Smuzhiyun * unfortunately, and since it makes use of AC97_RESET,
776*4882a593Smuzhiyun * there's no use trying to mute Master Playback proactively.
777*4882a593Smuzhiyun */
778*4882a593Smuzhiyun
779*4882a593Smuzhiyun rc = snd_ac97_bus(chip->card, 0, &ops, NULL, &bus);
780*4882a593Smuzhiyun if (!rc)
781*4882a593Smuzhiyun rc = snd_ac97_mixer(bus, &ac97, &chip->ac97);
782*4882a593Smuzhiyun /*
783*4882a593Smuzhiyun * Make sure to complain loudly in case of AC97 init failure,
784*4882a593Smuzhiyun * since failure may happen quite often,
785*4882a593Smuzhiyun * due to this card being a very quirky AC97 "lookalike".
786*4882a593Smuzhiyun */
787*4882a593Smuzhiyun if (rc)
788*4882a593Smuzhiyun dev_err(chip->card->dev, "AC97 init failed, err %d!\n", rc);
789*4882a593Smuzhiyun
790*4882a593Smuzhiyun /* If we return an error here, then snd_card_free() should
791*4882a593Smuzhiyun * free up any ac97 codecs that got created, as well as the bus.
792*4882a593Smuzhiyun */
793*4882a593Smuzhiyun return rc;
794*4882a593Smuzhiyun }
795*4882a593Smuzhiyun #else /* AZF_USE_AC97_LAYER */
796*4882a593Smuzhiyun static void
snd_azf3328_mixer_write_volume_gradually(const struct snd_azf3328 * chip,unsigned reg,unsigned char dst_vol_left,unsigned char dst_vol_right,int chan_sel,int delay)797*4882a593Smuzhiyun snd_azf3328_mixer_write_volume_gradually(const struct snd_azf3328 *chip,
798*4882a593Smuzhiyun unsigned reg,
799*4882a593Smuzhiyun unsigned char dst_vol_left,
800*4882a593Smuzhiyun unsigned char dst_vol_right,
801*4882a593Smuzhiyun int chan_sel, int delay
802*4882a593Smuzhiyun )
803*4882a593Smuzhiyun {
804*4882a593Smuzhiyun unsigned long portbase = chip->mixer_io + reg;
805*4882a593Smuzhiyun unsigned char curr_vol_left = 0, curr_vol_right = 0;
806*4882a593Smuzhiyun int left_change = 0, right_change = 0;
807*4882a593Smuzhiyun
808*4882a593Smuzhiyun if (chan_sel & SET_CHAN_LEFT) {
809*4882a593Smuzhiyun curr_vol_left = inb(portbase + 1);
810*4882a593Smuzhiyun
811*4882a593Smuzhiyun /* take care of muting flag contained in left channel */
812*4882a593Smuzhiyun if (curr_vol_left & AZF_MUTE_BIT)
813*4882a593Smuzhiyun dst_vol_left |= AZF_MUTE_BIT;
814*4882a593Smuzhiyun else
815*4882a593Smuzhiyun dst_vol_left &= ~AZF_MUTE_BIT;
816*4882a593Smuzhiyun
817*4882a593Smuzhiyun left_change = (curr_vol_left > dst_vol_left) ? -1 : 1;
818*4882a593Smuzhiyun }
819*4882a593Smuzhiyun
820*4882a593Smuzhiyun if (chan_sel & SET_CHAN_RIGHT) {
821*4882a593Smuzhiyun curr_vol_right = inb(portbase + 0);
822*4882a593Smuzhiyun
823*4882a593Smuzhiyun right_change = (curr_vol_right > dst_vol_right) ? -1 : 1;
824*4882a593Smuzhiyun }
825*4882a593Smuzhiyun
826*4882a593Smuzhiyun do {
827*4882a593Smuzhiyun if (left_change) {
828*4882a593Smuzhiyun if (curr_vol_left != dst_vol_left) {
829*4882a593Smuzhiyun curr_vol_left += left_change;
830*4882a593Smuzhiyun outb(curr_vol_left, portbase + 1);
831*4882a593Smuzhiyun } else
832*4882a593Smuzhiyun left_change = 0;
833*4882a593Smuzhiyun }
834*4882a593Smuzhiyun if (right_change) {
835*4882a593Smuzhiyun if (curr_vol_right != dst_vol_right) {
836*4882a593Smuzhiyun curr_vol_right += right_change;
837*4882a593Smuzhiyun
838*4882a593Smuzhiyun /* during volume change, the right channel is crackling
839*4882a593Smuzhiyun * somewhat more than the left channel, unfortunately.
840*4882a593Smuzhiyun * This seems to be a hardware issue. */
841*4882a593Smuzhiyun outb(curr_vol_right, portbase + 0);
842*4882a593Smuzhiyun } else
843*4882a593Smuzhiyun right_change = 0;
844*4882a593Smuzhiyun }
845*4882a593Smuzhiyun if (delay)
846*4882a593Smuzhiyun mdelay(delay);
847*4882a593Smuzhiyun } while ((left_change) || (right_change));
848*4882a593Smuzhiyun }
849*4882a593Smuzhiyun
850*4882a593Smuzhiyun /*
851*4882a593Smuzhiyun * general mixer element
852*4882a593Smuzhiyun */
853*4882a593Smuzhiyun struct azf3328_mixer_reg {
854*4882a593Smuzhiyun unsigned reg;
855*4882a593Smuzhiyun unsigned int lchan_shift, rchan_shift;
856*4882a593Smuzhiyun unsigned int mask;
857*4882a593Smuzhiyun unsigned int invert: 1;
858*4882a593Smuzhiyun unsigned int stereo: 1;
859*4882a593Smuzhiyun unsigned int enum_c: 4;
860*4882a593Smuzhiyun };
861*4882a593Smuzhiyun
862*4882a593Smuzhiyun #define COMPOSE_MIXER_REG(reg,lchan_shift,rchan_shift,mask,invert,stereo,enum_c) \
863*4882a593Smuzhiyun ((reg) | (lchan_shift << 8) | (rchan_shift << 12) | \
864*4882a593Smuzhiyun (mask << 16) | \
865*4882a593Smuzhiyun (invert << 24) | \
866*4882a593Smuzhiyun (stereo << 25) | \
867*4882a593Smuzhiyun (enum_c << 26))
868*4882a593Smuzhiyun
snd_azf3328_mixer_reg_decode(struct azf3328_mixer_reg * r,unsigned long val)869*4882a593Smuzhiyun static void snd_azf3328_mixer_reg_decode(struct azf3328_mixer_reg *r, unsigned long val)
870*4882a593Smuzhiyun {
871*4882a593Smuzhiyun r->reg = val & 0xff;
872*4882a593Smuzhiyun r->lchan_shift = (val >> 8) & 0x0f;
873*4882a593Smuzhiyun r->rchan_shift = (val >> 12) & 0x0f;
874*4882a593Smuzhiyun r->mask = (val >> 16) & 0xff;
875*4882a593Smuzhiyun r->invert = (val >> 24) & 1;
876*4882a593Smuzhiyun r->stereo = (val >> 25) & 1;
877*4882a593Smuzhiyun r->enum_c = (val >> 26) & 0x0f;
878*4882a593Smuzhiyun }
879*4882a593Smuzhiyun
880*4882a593Smuzhiyun /*
881*4882a593Smuzhiyun * mixer switches/volumes
882*4882a593Smuzhiyun */
883*4882a593Smuzhiyun
884*4882a593Smuzhiyun #define AZF3328_MIXER_SWITCH(xname, reg, shift, invert) \
885*4882a593Smuzhiyun { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
886*4882a593Smuzhiyun .info = snd_azf3328_info_mixer, \
887*4882a593Smuzhiyun .get = snd_azf3328_get_mixer, .put = snd_azf3328_put_mixer, \
888*4882a593Smuzhiyun .private_value = COMPOSE_MIXER_REG(reg, shift, 0, 0x1, invert, 0, 0), \
889*4882a593Smuzhiyun }
890*4882a593Smuzhiyun
891*4882a593Smuzhiyun #define AZF3328_MIXER_VOL_STEREO(xname, reg, mask, invert) \
892*4882a593Smuzhiyun { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
893*4882a593Smuzhiyun .info = snd_azf3328_info_mixer, \
894*4882a593Smuzhiyun .get = snd_azf3328_get_mixer, .put = snd_azf3328_put_mixer, \
895*4882a593Smuzhiyun .private_value = COMPOSE_MIXER_REG(reg, 8, 0, mask, invert, 1, 0), \
896*4882a593Smuzhiyun }
897*4882a593Smuzhiyun
898*4882a593Smuzhiyun #define AZF3328_MIXER_VOL_MONO(xname, reg, mask, is_right_chan) \
899*4882a593Smuzhiyun { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
900*4882a593Smuzhiyun .info = snd_azf3328_info_mixer, \
901*4882a593Smuzhiyun .get = snd_azf3328_get_mixer, .put = snd_azf3328_put_mixer, \
902*4882a593Smuzhiyun .private_value = COMPOSE_MIXER_REG(reg, is_right_chan ? 0 : 8, 0, mask, 1, 0, 0), \
903*4882a593Smuzhiyun }
904*4882a593Smuzhiyun
905*4882a593Smuzhiyun #define AZF3328_MIXER_VOL_SPECIAL(xname, reg, mask, shift, invert) \
906*4882a593Smuzhiyun { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
907*4882a593Smuzhiyun .info = snd_azf3328_info_mixer, \
908*4882a593Smuzhiyun .get = snd_azf3328_get_mixer, .put = snd_azf3328_put_mixer, \
909*4882a593Smuzhiyun .private_value = COMPOSE_MIXER_REG(reg, shift, 0, mask, invert, 0, 0), \
910*4882a593Smuzhiyun }
911*4882a593Smuzhiyun
912*4882a593Smuzhiyun #define AZF3328_MIXER_ENUM(xname, reg, enum_c, shift) \
913*4882a593Smuzhiyun { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
914*4882a593Smuzhiyun .info = snd_azf3328_info_mixer_enum, \
915*4882a593Smuzhiyun .get = snd_azf3328_get_mixer_enum, .put = snd_azf3328_put_mixer_enum, \
916*4882a593Smuzhiyun .private_value = COMPOSE_MIXER_REG(reg, shift, 0, 0, 0, 0, enum_c), \
917*4882a593Smuzhiyun }
918*4882a593Smuzhiyun
919*4882a593Smuzhiyun static int
snd_azf3328_info_mixer(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)920*4882a593Smuzhiyun snd_azf3328_info_mixer(struct snd_kcontrol *kcontrol,
921*4882a593Smuzhiyun struct snd_ctl_elem_info *uinfo)
922*4882a593Smuzhiyun {
923*4882a593Smuzhiyun struct azf3328_mixer_reg reg;
924*4882a593Smuzhiyun
925*4882a593Smuzhiyun snd_azf3328_mixer_reg_decode(®, kcontrol->private_value);
926*4882a593Smuzhiyun uinfo->type = reg.mask == 1 ?
927*4882a593Smuzhiyun SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
928*4882a593Smuzhiyun uinfo->count = reg.stereo + 1;
929*4882a593Smuzhiyun uinfo->value.integer.min = 0;
930*4882a593Smuzhiyun uinfo->value.integer.max = reg.mask;
931*4882a593Smuzhiyun return 0;
932*4882a593Smuzhiyun }
933*4882a593Smuzhiyun
934*4882a593Smuzhiyun static int
snd_azf3328_get_mixer(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)935*4882a593Smuzhiyun snd_azf3328_get_mixer(struct snd_kcontrol *kcontrol,
936*4882a593Smuzhiyun struct snd_ctl_elem_value *ucontrol)
937*4882a593Smuzhiyun {
938*4882a593Smuzhiyun struct snd_azf3328 *chip = snd_kcontrol_chip(kcontrol);
939*4882a593Smuzhiyun struct azf3328_mixer_reg reg;
940*4882a593Smuzhiyun u16 oreg, val;
941*4882a593Smuzhiyun
942*4882a593Smuzhiyun snd_azf3328_mixer_reg_decode(®, kcontrol->private_value);
943*4882a593Smuzhiyun
944*4882a593Smuzhiyun oreg = snd_azf3328_mixer_inw(chip, reg.reg);
945*4882a593Smuzhiyun val = (oreg >> reg.lchan_shift) & reg.mask;
946*4882a593Smuzhiyun if (reg.invert)
947*4882a593Smuzhiyun val = reg.mask - val;
948*4882a593Smuzhiyun ucontrol->value.integer.value[0] = val;
949*4882a593Smuzhiyun if (reg.stereo) {
950*4882a593Smuzhiyun val = (oreg >> reg.rchan_shift) & reg.mask;
951*4882a593Smuzhiyun if (reg.invert)
952*4882a593Smuzhiyun val = reg.mask - val;
953*4882a593Smuzhiyun ucontrol->value.integer.value[1] = val;
954*4882a593Smuzhiyun }
955*4882a593Smuzhiyun dev_dbg(chip->card->dev,
956*4882a593Smuzhiyun "get: %02x is %04x -> vol %02lx|%02lx (shift %02d|%02d, mask %02x, inv. %d, stereo %d)\n",
957*4882a593Smuzhiyun reg.reg, oreg,
958*4882a593Smuzhiyun ucontrol->value.integer.value[0], ucontrol->value.integer.value[1],
959*4882a593Smuzhiyun reg.lchan_shift, reg.rchan_shift, reg.mask, reg.invert, reg.stereo);
960*4882a593Smuzhiyun return 0;
961*4882a593Smuzhiyun }
962*4882a593Smuzhiyun
963*4882a593Smuzhiyun static int
snd_azf3328_put_mixer(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)964*4882a593Smuzhiyun snd_azf3328_put_mixer(struct snd_kcontrol *kcontrol,
965*4882a593Smuzhiyun struct snd_ctl_elem_value *ucontrol)
966*4882a593Smuzhiyun {
967*4882a593Smuzhiyun struct snd_azf3328 *chip = snd_kcontrol_chip(kcontrol);
968*4882a593Smuzhiyun struct azf3328_mixer_reg reg;
969*4882a593Smuzhiyun u16 oreg, nreg, val;
970*4882a593Smuzhiyun
971*4882a593Smuzhiyun snd_azf3328_mixer_reg_decode(®, kcontrol->private_value);
972*4882a593Smuzhiyun oreg = snd_azf3328_mixer_inw(chip, reg.reg);
973*4882a593Smuzhiyun val = ucontrol->value.integer.value[0] & reg.mask;
974*4882a593Smuzhiyun if (reg.invert)
975*4882a593Smuzhiyun val = reg.mask - val;
976*4882a593Smuzhiyun nreg = oreg & ~(reg.mask << reg.lchan_shift);
977*4882a593Smuzhiyun nreg |= (val << reg.lchan_shift);
978*4882a593Smuzhiyun if (reg.stereo) {
979*4882a593Smuzhiyun val = ucontrol->value.integer.value[1] & reg.mask;
980*4882a593Smuzhiyun if (reg.invert)
981*4882a593Smuzhiyun val = reg.mask - val;
982*4882a593Smuzhiyun nreg &= ~(reg.mask << reg.rchan_shift);
983*4882a593Smuzhiyun nreg |= (val << reg.rchan_shift);
984*4882a593Smuzhiyun }
985*4882a593Smuzhiyun if (reg.mask >= 0x07) /* it's a volume control, so better take care */
986*4882a593Smuzhiyun snd_azf3328_mixer_write_volume_gradually(
987*4882a593Smuzhiyun chip, reg.reg, nreg >> 8, nreg & 0xff,
988*4882a593Smuzhiyun /* just set both channels, doesn't matter */
989*4882a593Smuzhiyun SET_CHAN_LEFT|SET_CHAN_RIGHT,
990*4882a593Smuzhiyun 0);
991*4882a593Smuzhiyun else
992*4882a593Smuzhiyun snd_azf3328_mixer_outw(chip, reg.reg, nreg);
993*4882a593Smuzhiyun
994*4882a593Smuzhiyun dev_dbg(chip->card->dev,
995*4882a593Smuzhiyun "put: %02x to %02lx|%02lx, oreg %04x; shift %02d|%02d -> nreg %04x; after: %04x\n",
996*4882a593Smuzhiyun reg.reg, ucontrol->value.integer.value[0], ucontrol->value.integer.value[1],
997*4882a593Smuzhiyun oreg, reg.lchan_shift, reg.rchan_shift,
998*4882a593Smuzhiyun nreg, snd_azf3328_mixer_inw(chip, reg.reg));
999*4882a593Smuzhiyun return (nreg != oreg);
1000*4882a593Smuzhiyun }
1001*4882a593Smuzhiyun
1002*4882a593Smuzhiyun static int
snd_azf3328_info_mixer_enum(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1003*4882a593Smuzhiyun snd_azf3328_info_mixer_enum(struct snd_kcontrol *kcontrol,
1004*4882a593Smuzhiyun struct snd_ctl_elem_info *uinfo)
1005*4882a593Smuzhiyun {
1006*4882a593Smuzhiyun static const char * const texts1[] = {
1007*4882a593Smuzhiyun "Mic1", "Mic2"
1008*4882a593Smuzhiyun };
1009*4882a593Smuzhiyun static const char * const texts2[] = {
1010*4882a593Smuzhiyun "Mix", "Mic"
1011*4882a593Smuzhiyun };
1012*4882a593Smuzhiyun static const char * const texts3[] = {
1013*4882a593Smuzhiyun "Mic", "CD", "Video", "Aux",
1014*4882a593Smuzhiyun "Line", "Mix", "Mix Mono", "Phone"
1015*4882a593Smuzhiyun };
1016*4882a593Smuzhiyun static const char * const texts4[] = {
1017*4882a593Smuzhiyun "pre 3D", "post 3D"
1018*4882a593Smuzhiyun };
1019*4882a593Smuzhiyun struct azf3328_mixer_reg reg;
1020*4882a593Smuzhiyun const char * const *p = NULL;
1021*4882a593Smuzhiyun
1022*4882a593Smuzhiyun snd_azf3328_mixer_reg_decode(®, kcontrol->private_value);
1023*4882a593Smuzhiyun if (reg.reg == IDX_MIXER_ADVCTL2) {
1024*4882a593Smuzhiyun switch(reg.lchan_shift) {
1025*4882a593Smuzhiyun case 8: /* modem out sel */
1026*4882a593Smuzhiyun p = texts1;
1027*4882a593Smuzhiyun break;
1028*4882a593Smuzhiyun case 9: /* mono sel source */
1029*4882a593Smuzhiyun p = texts2;
1030*4882a593Smuzhiyun break;
1031*4882a593Smuzhiyun case 15: /* PCM Out Path */
1032*4882a593Smuzhiyun p = texts4;
1033*4882a593Smuzhiyun break;
1034*4882a593Smuzhiyun }
1035*4882a593Smuzhiyun } else if (reg.reg == IDX_MIXER_REC_SELECT)
1036*4882a593Smuzhiyun p = texts3;
1037*4882a593Smuzhiyun
1038*4882a593Smuzhiyun return snd_ctl_enum_info(uinfo,
1039*4882a593Smuzhiyun (reg.reg == IDX_MIXER_REC_SELECT) ? 2 : 1,
1040*4882a593Smuzhiyun reg.enum_c, p);
1041*4882a593Smuzhiyun }
1042*4882a593Smuzhiyun
1043*4882a593Smuzhiyun static int
snd_azf3328_get_mixer_enum(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1044*4882a593Smuzhiyun snd_azf3328_get_mixer_enum(struct snd_kcontrol *kcontrol,
1045*4882a593Smuzhiyun struct snd_ctl_elem_value *ucontrol)
1046*4882a593Smuzhiyun {
1047*4882a593Smuzhiyun struct snd_azf3328 *chip = snd_kcontrol_chip(kcontrol);
1048*4882a593Smuzhiyun struct azf3328_mixer_reg reg;
1049*4882a593Smuzhiyun unsigned short val;
1050*4882a593Smuzhiyun
1051*4882a593Smuzhiyun snd_azf3328_mixer_reg_decode(®, kcontrol->private_value);
1052*4882a593Smuzhiyun val = snd_azf3328_mixer_inw(chip, reg.reg);
1053*4882a593Smuzhiyun if (reg.reg == IDX_MIXER_REC_SELECT) {
1054*4882a593Smuzhiyun ucontrol->value.enumerated.item[0] = (val >> 8) & (reg.enum_c - 1);
1055*4882a593Smuzhiyun ucontrol->value.enumerated.item[1] = (val >> 0) & (reg.enum_c - 1);
1056*4882a593Smuzhiyun } else
1057*4882a593Smuzhiyun ucontrol->value.enumerated.item[0] = (val >> reg.lchan_shift) & (reg.enum_c - 1);
1058*4882a593Smuzhiyun
1059*4882a593Smuzhiyun dev_dbg(chip->card->dev,
1060*4882a593Smuzhiyun "get_enum: %02x is %04x -> %d|%d (shift %02d, enum_c %d)\n",
1061*4882a593Smuzhiyun reg.reg, val, ucontrol->value.enumerated.item[0], ucontrol->value.enumerated.item[1],
1062*4882a593Smuzhiyun reg.lchan_shift, reg.enum_c);
1063*4882a593Smuzhiyun return 0;
1064*4882a593Smuzhiyun }
1065*4882a593Smuzhiyun
1066*4882a593Smuzhiyun static int
snd_azf3328_put_mixer_enum(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1067*4882a593Smuzhiyun snd_azf3328_put_mixer_enum(struct snd_kcontrol *kcontrol,
1068*4882a593Smuzhiyun struct snd_ctl_elem_value *ucontrol)
1069*4882a593Smuzhiyun {
1070*4882a593Smuzhiyun struct snd_azf3328 *chip = snd_kcontrol_chip(kcontrol);
1071*4882a593Smuzhiyun struct azf3328_mixer_reg reg;
1072*4882a593Smuzhiyun u16 oreg, nreg, val;
1073*4882a593Smuzhiyun
1074*4882a593Smuzhiyun snd_azf3328_mixer_reg_decode(®, kcontrol->private_value);
1075*4882a593Smuzhiyun oreg = snd_azf3328_mixer_inw(chip, reg.reg);
1076*4882a593Smuzhiyun val = oreg;
1077*4882a593Smuzhiyun if (reg.reg == IDX_MIXER_REC_SELECT) {
1078*4882a593Smuzhiyun if (ucontrol->value.enumerated.item[0] > reg.enum_c - 1U ||
1079*4882a593Smuzhiyun ucontrol->value.enumerated.item[1] > reg.enum_c - 1U)
1080*4882a593Smuzhiyun return -EINVAL;
1081*4882a593Smuzhiyun val = (ucontrol->value.enumerated.item[0] << 8) |
1082*4882a593Smuzhiyun (ucontrol->value.enumerated.item[1] << 0);
1083*4882a593Smuzhiyun } else {
1084*4882a593Smuzhiyun if (ucontrol->value.enumerated.item[0] > reg.enum_c - 1U)
1085*4882a593Smuzhiyun return -EINVAL;
1086*4882a593Smuzhiyun val &= ~((reg.enum_c - 1) << reg.lchan_shift);
1087*4882a593Smuzhiyun val |= (ucontrol->value.enumerated.item[0] << reg.lchan_shift);
1088*4882a593Smuzhiyun }
1089*4882a593Smuzhiyun snd_azf3328_mixer_outw(chip, reg.reg, val);
1090*4882a593Smuzhiyun nreg = val;
1091*4882a593Smuzhiyun
1092*4882a593Smuzhiyun dev_dbg(chip->card->dev,
1093*4882a593Smuzhiyun "put_enum: %02x to %04x, oreg %04x\n", reg.reg, val, oreg);
1094*4882a593Smuzhiyun return (nreg != oreg);
1095*4882a593Smuzhiyun }
1096*4882a593Smuzhiyun
1097*4882a593Smuzhiyun static const struct snd_kcontrol_new snd_azf3328_mixer_controls[] = {
1098*4882a593Smuzhiyun AZF3328_MIXER_SWITCH("Master Playback Switch", IDX_MIXER_PLAY_MASTER, 15, 1),
1099*4882a593Smuzhiyun AZF3328_MIXER_VOL_STEREO("Master Playback Volume", IDX_MIXER_PLAY_MASTER, 0x1f, 1),
1100*4882a593Smuzhiyun AZF3328_MIXER_SWITCH("PCM Playback Switch", IDX_MIXER_WAVEOUT, 15, 1),
1101*4882a593Smuzhiyun AZF3328_MIXER_VOL_STEREO("PCM Playback Volume",
1102*4882a593Smuzhiyun IDX_MIXER_WAVEOUT, 0x1f, 1),
1103*4882a593Smuzhiyun AZF3328_MIXER_SWITCH("PCM 3D Bypass Playback Switch",
1104*4882a593Smuzhiyun IDX_MIXER_ADVCTL2, 7, 1),
1105*4882a593Smuzhiyun AZF3328_MIXER_SWITCH("FM Playback Switch", IDX_MIXER_FMSYNTH, 15, 1),
1106*4882a593Smuzhiyun AZF3328_MIXER_VOL_STEREO("FM Playback Volume", IDX_MIXER_FMSYNTH, 0x1f, 1),
1107*4882a593Smuzhiyun AZF3328_MIXER_SWITCH("CD Playback Switch", IDX_MIXER_CDAUDIO, 15, 1),
1108*4882a593Smuzhiyun AZF3328_MIXER_VOL_STEREO("CD Playback Volume", IDX_MIXER_CDAUDIO, 0x1f, 1),
1109*4882a593Smuzhiyun AZF3328_MIXER_SWITCH("Capture Switch", IDX_MIXER_REC_VOLUME, 15, 1),
1110*4882a593Smuzhiyun AZF3328_MIXER_VOL_STEREO("Capture Volume", IDX_MIXER_REC_VOLUME, 0x0f, 0),
1111*4882a593Smuzhiyun AZF3328_MIXER_ENUM("Capture Source", IDX_MIXER_REC_SELECT, 8, 0),
1112*4882a593Smuzhiyun AZF3328_MIXER_SWITCH("Mic Playback Switch", IDX_MIXER_MIC, 15, 1),
1113*4882a593Smuzhiyun AZF3328_MIXER_VOL_MONO("Mic Playback Volume", IDX_MIXER_MIC, 0x1f, 1),
1114*4882a593Smuzhiyun AZF3328_MIXER_SWITCH("Mic Boost (+20dB)", IDX_MIXER_MIC, 6, 0),
1115*4882a593Smuzhiyun AZF3328_MIXER_SWITCH("Line Playback Switch", IDX_MIXER_LINEIN, 15, 1),
1116*4882a593Smuzhiyun AZF3328_MIXER_VOL_STEREO("Line Playback Volume", IDX_MIXER_LINEIN, 0x1f, 1),
1117*4882a593Smuzhiyun AZF3328_MIXER_SWITCH("Beep Playback Switch", IDX_MIXER_PCBEEP, 15, 1),
1118*4882a593Smuzhiyun AZF3328_MIXER_VOL_SPECIAL("Beep Playback Volume", IDX_MIXER_PCBEEP, 0x0f, 1, 1),
1119*4882a593Smuzhiyun AZF3328_MIXER_SWITCH("Video Playback Switch", IDX_MIXER_VIDEO, 15, 1),
1120*4882a593Smuzhiyun AZF3328_MIXER_VOL_STEREO("Video Playback Volume", IDX_MIXER_VIDEO, 0x1f, 1),
1121*4882a593Smuzhiyun AZF3328_MIXER_SWITCH("Aux Playback Switch", IDX_MIXER_AUX, 15, 1),
1122*4882a593Smuzhiyun AZF3328_MIXER_VOL_STEREO("Aux Playback Volume", IDX_MIXER_AUX, 0x1f, 1),
1123*4882a593Smuzhiyun AZF3328_MIXER_SWITCH("Modem Playback Switch", IDX_MIXER_MODEMOUT, 15, 1),
1124*4882a593Smuzhiyun AZF3328_MIXER_VOL_MONO("Modem Playback Volume", IDX_MIXER_MODEMOUT, 0x1f, 1),
1125*4882a593Smuzhiyun AZF3328_MIXER_SWITCH("Modem Capture Switch", IDX_MIXER_MODEMIN, 15, 1),
1126*4882a593Smuzhiyun AZF3328_MIXER_VOL_MONO("Modem Capture Volume", IDX_MIXER_MODEMIN, 0x1f, 1),
1127*4882a593Smuzhiyun AZF3328_MIXER_ENUM("Mic Select", IDX_MIXER_ADVCTL2, 2, 8),
1128*4882a593Smuzhiyun AZF3328_MIXER_ENUM("Mono Output Select", IDX_MIXER_ADVCTL2, 2, 9),
1129*4882a593Smuzhiyun AZF3328_MIXER_ENUM("PCM Output Route", IDX_MIXER_ADVCTL2, 2, 15), /* PCM Out Path, place in front since it controls *both* 3D and Bass/Treble! */
1130*4882a593Smuzhiyun AZF3328_MIXER_VOL_SPECIAL("Tone Control - Treble", IDX_MIXER_BASSTREBLE, 0x07, 1, 0),
1131*4882a593Smuzhiyun AZF3328_MIXER_VOL_SPECIAL("Tone Control - Bass", IDX_MIXER_BASSTREBLE, 0x07, 9, 0),
1132*4882a593Smuzhiyun AZF3328_MIXER_SWITCH("3D Control - Switch", IDX_MIXER_ADVCTL2, 13, 0),
1133*4882a593Smuzhiyun AZF3328_MIXER_VOL_SPECIAL("3D Control - Width", IDX_MIXER_ADVCTL1, 0x07, 1, 0), /* "3D Width" */
1134*4882a593Smuzhiyun AZF3328_MIXER_VOL_SPECIAL("3D Control - Depth", IDX_MIXER_ADVCTL1, 0x03, 8, 0), /* "Hifi 3D" */
1135*4882a593Smuzhiyun #if MIXER_TESTING
1136*4882a593Smuzhiyun AZF3328_MIXER_SWITCH("0", IDX_MIXER_ADVCTL2, 0, 0),
1137*4882a593Smuzhiyun AZF3328_MIXER_SWITCH("1", IDX_MIXER_ADVCTL2, 1, 0),
1138*4882a593Smuzhiyun AZF3328_MIXER_SWITCH("2", IDX_MIXER_ADVCTL2, 2, 0),
1139*4882a593Smuzhiyun AZF3328_MIXER_SWITCH("3", IDX_MIXER_ADVCTL2, 3, 0),
1140*4882a593Smuzhiyun AZF3328_MIXER_SWITCH("4", IDX_MIXER_ADVCTL2, 4, 0),
1141*4882a593Smuzhiyun AZF3328_MIXER_SWITCH("5", IDX_MIXER_ADVCTL2, 5, 0),
1142*4882a593Smuzhiyun AZF3328_MIXER_SWITCH("6", IDX_MIXER_ADVCTL2, 6, 0),
1143*4882a593Smuzhiyun AZF3328_MIXER_SWITCH("7", IDX_MIXER_ADVCTL2, 7, 0),
1144*4882a593Smuzhiyun AZF3328_MIXER_SWITCH("8", IDX_MIXER_ADVCTL2, 8, 0),
1145*4882a593Smuzhiyun AZF3328_MIXER_SWITCH("9", IDX_MIXER_ADVCTL2, 9, 0),
1146*4882a593Smuzhiyun AZF3328_MIXER_SWITCH("10", IDX_MIXER_ADVCTL2, 10, 0),
1147*4882a593Smuzhiyun AZF3328_MIXER_SWITCH("11", IDX_MIXER_ADVCTL2, 11, 0),
1148*4882a593Smuzhiyun AZF3328_MIXER_SWITCH("12", IDX_MIXER_ADVCTL2, 12, 0),
1149*4882a593Smuzhiyun AZF3328_MIXER_SWITCH("13", IDX_MIXER_ADVCTL2, 13, 0),
1150*4882a593Smuzhiyun AZF3328_MIXER_SWITCH("14", IDX_MIXER_ADVCTL2, 14, 0),
1151*4882a593Smuzhiyun AZF3328_MIXER_SWITCH("15", IDX_MIXER_ADVCTL2, 15, 0),
1152*4882a593Smuzhiyun #endif
1153*4882a593Smuzhiyun };
1154*4882a593Smuzhiyun
1155*4882a593Smuzhiyun static const u16 snd_azf3328_init_values[][2] = {
1156*4882a593Smuzhiyun { IDX_MIXER_PLAY_MASTER, MIXER_MUTE_MASK|0x1f1f },
1157*4882a593Smuzhiyun { IDX_MIXER_MODEMOUT, MIXER_MUTE_MASK|0x1f1f },
1158*4882a593Smuzhiyun { IDX_MIXER_BASSTREBLE, 0x0000 },
1159*4882a593Smuzhiyun { IDX_MIXER_PCBEEP, MIXER_MUTE_MASK|0x1f1f },
1160*4882a593Smuzhiyun { IDX_MIXER_MODEMIN, MIXER_MUTE_MASK|0x1f1f },
1161*4882a593Smuzhiyun { IDX_MIXER_MIC, MIXER_MUTE_MASK|0x001f },
1162*4882a593Smuzhiyun { IDX_MIXER_LINEIN, MIXER_MUTE_MASK|0x1f1f },
1163*4882a593Smuzhiyun { IDX_MIXER_CDAUDIO, MIXER_MUTE_MASK|0x1f1f },
1164*4882a593Smuzhiyun { IDX_MIXER_VIDEO, MIXER_MUTE_MASK|0x1f1f },
1165*4882a593Smuzhiyun { IDX_MIXER_AUX, MIXER_MUTE_MASK|0x1f1f },
1166*4882a593Smuzhiyun { IDX_MIXER_WAVEOUT, MIXER_MUTE_MASK|0x1f1f },
1167*4882a593Smuzhiyun { IDX_MIXER_FMSYNTH, MIXER_MUTE_MASK|0x1f1f },
1168*4882a593Smuzhiyun { IDX_MIXER_REC_VOLUME, MIXER_MUTE_MASK|0x0707 },
1169*4882a593Smuzhiyun };
1170*4882a593Smuzhiyun
1171*4882a593Smuzhiyun static int
snd_azf3328_mixer_new(struct snd_azf3328 * chip)1172*4882a593Smuzhiyun snd_azf3328_mixer_new(struct snd_azf3328 *chip)
1173*4882a593Smuzhiyun {
1174*4882a593Smuzhiyun struct snd_card *card;
1175*4882a593Smuzhiyun const struct snd_kcontrol_new *sw;
1176*4882a593Smuzhiyun unsigned int idx;
1177*4882a593Smuzhiyun int err;
1178*4882a593Smuzhiyun
1179*4882a593Smuzhiyun if (snd_BUG_ON(!chip || !chip->card))
1180*4882a593Smuzhiyun return -EINVAL;
1181*4882a593Smuzhiyun
1182*4882a593Smuzhiyun card = chip->card;
1183*4882a593Smuzhiyun
1184*4882a593Smuzhiyun /* mixer reset */
1185*4882a593Smuzhiyun snd_azf3328_mixer_outw(chip, IDX_MIXER_RESET, 0x0000);
1186*4882a593Smuzhiyun
1187*4882a593Smuzhiyun /* mute and zero volume channels */
1188*4882a593Smuzhiyun for (idx = 0; idx < ARRAY_SIZE(snd_azf3328_init_values); ++idx) {
1189*4882a593Smuzhiyun snd_azf3328_mixer_outw(chip,
1190*4882a593Smuzhiyun snd_azf3328_init_values[idx][0],
1191*4882a593Smuzhiyun snd_azf3328_init_values[idx][1]);
1192*4882a593Smuzhiyun }
1193*4882a593Smuzhiyun
1194*4882a593Smuzhiyun /* add mixer controls */
1195*4882a593Smuzhiyun sw = snd_azf3328_mixer_controls;
1196*4882a593Smuzhiyun for (idx = 0; idx < ARRAY_SIZE(snd_azf3328_mixer_controls);
1197*4882a593Smuzhiyun ++idx, ++sw) {
1198*4882a593Smuzhiyun if ((err = snd_ctl_add(chip->card, snd_ctl_new1(sw, chip))) < 0)
1199*4882a593Smuzhiyun return err;
1200*4882a593Smuzhiyun }
1201*4882a593Smuzhiyun snd_component_add(card, "AZF3328 mixer");
1202*4882a593Smuzhiyun strcpy(card->mixername, "AZF3328 mixer");
1203*4882a593Smuzhiyun
1204*4882a593Smuzhiyun return 0;
1205*4882a593Smuzhiyun }
1206*4882a593Smuzhiyun #endif /* AZF_USE_AC97_LAYER */
1207*4882a593Smuzhiyun
1208*4882a593Smuzhiyun static void
snd_azf3328_codec_setfmt(struct snd_azf3328_codec_data * codec,enum azf_freq_t bitrate,unsigned int format_width,unsigned int channels)1209*4882a593Smuzhiyun snd_azf3328_codec_setfmt(struct snd_azf3328_codec_data *codec,
1210*4882a593Smuzhiyun enum azf_freq_t bitrate,
1211*4882a593Smuzhiyun unsigned int format_width,
1212*4882a593Smuzhiyun unsigned int channels
1213*4882a593Smuzhiyun )
1214*4882a593Smuzhiyun {
1215*4882a593Smuzhiyun unsigned long flags;
1216*4882a593Smuzhiyun u16 val = 0xff00;
1217*4882a593Smuzhiyun u8 freq = 0;
1218*4882a593Smuzhiyun
1219*4882a593Smuzhiyun switch (bitrate) {
1220*4882a593Smuzhiyun case AZF_FREQ_4000: freq = SOUNDFORMAT_FREQ_SUSPECTED_4000; break;
1221*4882a593Smuzhiyun case AZF_FREQ_4800: freq = SOUNDFORMAT_FREQ_SUSPECTED_4800; break;
1222*4882a593Smuzhiyun case AZF_FREQ_5512:
1223*4882a593Smuzhiyun /* the AZF3328 names it "5510" for some strange reason */
1224*4882a593Smuzhiyun freq = SOUNDFORMAT_FREQ_5510; break;
1225*4882a593Smuzhiyun case AZF_FREQ_6620: freq = SOUNDFORMAT_FREQ_6620; break;
1226*4882a593Smuzhiyun case AZF_FREQ_8000: freq = SOUNDFORMAT_FREQ_8000; break;
1227*4882a593Smuzhiyun case AZF_FREQ_9600: freq = SOUNDFORMAT_FREQ_9600; break;
1228*4882a593Smuzhiyun case AZF_FREQ_11025: freq = SOUNDFORMAT_FREQ_11025; break;
1229*4882a593Smuzhiyun case AZF_FREQ_13240: freq = SOUNDFORMAT_FREQ_SUSPECTED_13240; break;
1230*4882a593Smuzhiyun case AZF_FREQ_16000: freq = SOUNDFORMAT_FREQ_16000; break;
1231*4882a593Smuzhiyun case AZF_FREQ_22050: freq = SOUNDFORMAT_FREQ_22050; break;
1232*4882a593Smuzhiyun case AZF_FREQ_32000: freq = SOUNDFORMAT_FREQ_32000; break;
1233*4882a593Smuzhiyun default:
1234*4882a593Smuzhiyun snd_printk(KERN_WARNING "unknown bitrate %d, assuming 44.1kHz!\n", bitrate);
1235*4882a593Smuzhiyun fallthrough;
1236*4882a593Smuzhiyun case AZF_FREQ_44100: freq = SOUNDFORMAT_FREQ_44100; break;
1237*4882a593Smuzhiyun case AZF_FREQ_48000: freq = SOUNDFORMAT_FREQ_48000; break;
1238*4882a593Smuzhiyun case AZF_FREQ_66200: freq = SOUNDFORMAT_FREQ_SUSPECTED_66200; break;
1239*4882a593Smuzhiyun }
1240*4882a593Smuzhiyun /* val = 0xff07; 3m27.993s (65301Hz; -> 64000Hz???) hmm, 66120, 65967, 66123 */
1241*4882a593Smuzhiyun /* val = 0xff09; 17m15.098s (13123,478Hz; -> 12000Hz???) hmm, 13237.2Hz? */
1242*4882a593Smuzhiyun /* val = 0xff0a; 47m30.599s (4764,891Hz; -> 4800Hz???) yup, 4803Hz */
1243*4882a593Smuzhiyun /* val = 0xff0c; 57m0.510s (4010,263Hz; -> 4000Hz???) yup, 4003Hz */
1244*4882a593Smuzhiyun /* val = 0xff05; 5m11.556s (... -> 44100Hz) */
1245*4882a593Smuzhiyun /* val = 0xff03; 10m21.529s (21872,463Hz; -> 22050Hz???) */
1246*4882a593Smuzhiyun /* val = 0xff0f; 20m41.883s (10937,993Hz; -> 11025Hz???) */
1247*4882a593Smuzhiyun /* val = 0xff0d; 41m23.135s (5523,600Hz; -> 5512Hz???) */
1248*4882a593Smuzhiyun /* val = 0xff0e; 28m30.777s (8017Hz; -> 8000Hz???) */
1249*4882a593Smuzhiyun
1250*4882a593Smuzhiyun val |= freq;
1251*4882a593Smuzhiyun
1252*4882a593Smuzhiyun if (channels == 2)
1253*4882a593Smuzhiyun val |= SOUNDFORMAT_FLAG_2CHANNELS;
1254*4882a593Smuzhiyun
1255*4882a593Smuzhiyun if (format_width == 16)
1256*4882a593Smuzhiyun val |= SOUNDFORMAT_FLAG_16BIT;
1257*4882a593Smuzhiyun
1258*4882a593Smuzhiyun spin_lock_irqsave(codec->lock, flags);
1259*4882a593Smuzhiyun
1260*4882a593Smuzhiyun /* set bitrate/format */
1261*4882a593Smuzhiyun snd_azf3328_codec_outw(codec, IDX_IO_CODEC_SOUNDFORMAT, val);
1262*4882a593Smuzhiyun
1263*4882a593Smuzhiyun /* changing the bitrate/format settings switches off the
1264*4882a593Smuzhiyun * audio output with an annoying click in case of 8/16bit format change
1265*4882a593Smuzhiyun * (maybe shutting down DAC/ADC?), thus immediately
1266*4882a593Smuzhiyun * do some tweaking to reenable it and get rid of the clicking
1267*4882a593Smuzhiyun * (FIXME: yes, it works, but what exactly am I doing here?? :)
1268*4882a593Smuzhiyun * FIXME: does this have some side effects for full-duplex
1269*4882a593Smuzhiyun * or other dramatic side effects? */
1270*4882a593Smuzhiyun /* do it for non-capture codecs only */
1271*4882a593Smuzhiyun if (codec->type != AZF_CODEC_CAPTURE)
1272*4882a593Smuzhiyun snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS,
1273*4882a593Smuzhiyun snd_azf3328_codec_inw(codec, IDX_IO_CODEC_DMA_FLAGS) |
1274*4882a593Smuzhiyun DMA_RUN_SOMETHING1 |
1275*4882a593Smuzhiyun DMA_RUN_SOMETHING2 |
1276*4882a593Smuzhiyun SOMETHING_ALMOST_ALWAYS_SET |
1277*4882a593Smuzhiyun DMA_EPILOGUE_SOMETHING |
1278*4882a593Smuzhiyun DMA_SOMETHING_ELSE
1279*4882a593Smuzhiyun );
1280*4882a593Smuzhiyun
1281*4882a593Smuzhiyun spin_unlock_irqrestore(codec->lock, flags);
1282*4882a593Smuzhiyun }
1283*4882a593Smuzhiyun
1284*4882a593Smuzhiyun static inline void
snd_azf3328_codec_setfmt_lowpower(struct snd_azf3328_codec_data * codec)1285*4882a593Smuzhiyun snd_azf3328_codec_setfmt_lowpower(struct snd_azf3328_codec_data *codec
1286*4882a593Smuzhiyun )
1287*4882a593Smuzhiyun {
1288*4882a593Smuzhiyun /* choose lowest frequency for low power consumption.
1289*4882a593Smuzhiyun * While this will cause louder noise due to rather coarse frequency,
1290*4882a593Smuzhiyun * it should never matter since output should always
1291*4882a593Smuzhiyun * get disabled properly when idle anyway. */
1292*4882a593Smuzhiyun snd_azf3328_codec_setfmt(codec, AZF_FREQ_4000, 8, 1);
1293*4882a593Smuzhiyun }
1294*4882a593Smuzhiyun
1295*4882a593Smuzhiyun static void
snd_azf3328_ctrl_reg_6AH_update(struct snd_azf3328 * chip,unsigned bitmask,bool enable)1296*4882a593Smuzhiyun snd_azf3328_ctrl_reg_6AH_update(struct snd_azf3328 *chip,
1297*4882a593Smuzhiyun unsigned bitmask,
1298*4882a593Smuzhiyun bool enable
1299*4882a593Smuzhiyun )
1300*4882a593Smuzhiyun {
1301*4882a593Smuzhiyun bool do_mask = !enable;
1302*4882a593Smuzhiyun if (do_mask)
1303*4882a593Smuzhiyun chip->shadow_reg_ctrl_6AH |= bitmask;
1304*4882a593Smuzhiyun else
1305*4882a593Smuzhiyun chip->shadow_reg_ctrl_6AH &= ~bitmask;
1306*4882a593Smuzhiyun dev_dbg(chip->card->dev,
1307*4882a593Smuzhiyun "6AH_update mask 0x%04x do_mask %d: val 0x%04x\n",
1308*4882a593Smuzhiyun bitmask, do_mask, chip->shadow_reg_ctrl_6AH);
1309*4882a593Smuzhiyun snd_azf3328_ctrl_outw(chip, IDX_IO_6AH, chip->shadow_reg_ctrl_6AH);
1310*4882a593Smuzhiyun }
1311*4882a593Smuzhiyun
1312*4882a593Smuzhiyun static inline void
snd_azf3328_ctrl_enable_codecs(struct snd_azf3328 * chip,bool enable)1313*4882a593Smuzhiyun snd_azf3328_ctrl_enable_codecs(struct snd_azf3328 *chip, bool enable)
1314*4882a593Smuzhiyun {
1315*4882a593Smuzhiyun dev_dbg(chip->card->dev, "codec_enable %d\n", enable);
1316*4882a593Smuzhiyun /* no idea what exactly is being done here, but I strongly assume it's
1317*4882a593Smuzhiyun * PM related */
1318*4882a593Smuzhiyun snd_azf3328_ctrl_reg_6AH_update(
1319*4882a593Smuzhiyun chip, IO_6A_PAUSE_PLAYBACK_BIT8, enable
1320*4882a593Smuzhiyun );
1321*4882a593Smuzhiyun }
1322*4882a593Smuzhiyun
1323*4882a593Smuzhiyun static void
snd_azf3328_ctrl_codec_activity(struct snd_azf3328 * chip,enum snd_azf3328_codec_type codec_type,bool enable)1324*4882a593Smuzhiyun snd_azf3328_ctrl_codec_activity(struct snd_azf3328 *chip,
1325*4882a593Smuzhiyun enum snd_azf3328_codec_type codec_type,
1326*4882a593Smuzhiyun bool enable
1327*4882a593Smuzhiyun )
1328*4882a593Smuzhiyun {
1329*4882a593Smuzhiyun struct snd_azf3328_codec_data *codec = &chip->codecs[codec_type];
1330*4882a593Smuzhiyun bool need_change = (codec->running != enable);
1331*4882a593Smuzhiyun
1332*4882a593Smuzhiyun dev_dbg(chip->card->dev,
1333*4882a593Smuzhiyun "codec_activity: %s codec, enable %d, need_change %d\n",
1334*4882a593Smuzhiyun codec->name, enable, need_change
1335*4882a593Smuzhiyun );
1336*4882a593Smuzhiyun if (need_change) {
1337*4882a593Smuzhiyun static const struct {
1338*4882a593Smuzhiyun enum snd_azf3328_codec_type other1;
1339*4882a593Smuzhiyun enum snd_azf3328_codec_type other2;
1340*4882a593Smuzhiyun } peer_codecs[3] =
1341*4882a593Smuzhiyun { { AZF_CODEC_CAPTURE, AZF_CODEC_I2S_OUT },
1342*4882a593Smuzhiyun { AZF_CODEC_PLAYBACK, AZF_CODEC_I2S_OUT },
1343*4882a593Smuzhiyun { AZF_CODEC_PLAYBACK, AZF_CODEC_CAPTURE } };
1344*4882a593Smuzhiyun bool call_function;
1345*4882a593Smuzhiyun
1346*4882a593Smuzhiyun if (enable)
1347*4882a593Smuzhiyun /* if enable codec, call enable_codecs func
1348*4882a593Smuzhiyun to enable codec supply... */
1349*4882a593Smuzhiyun call_function = 1;
1350*4882a593Smuzhiyun else {
1351*4882a593Smuzhiyun /* ...otherwise call enable_codecs func
1352*4882a593Smuzhiyun (which globally shuts down operation of codecs)
1353*4882a593Smuzhiyun only in case the other codecs are currently
1354*4882a593Smuzhiyun not active either! */
1355*4882a593Smuzhiyun call_function =
1356*4882a593Smuzhiyun ((!chip->codecs[peer_codecs[codec_type].other1]
1357*4882a593Smuzhiyun .running)
1358*4882a593Smuzhiyun && (!chip->codecs[peer_codecs[codec_type].other2]
1359*4882a593Smuzhiyun .running));
1360*4882a593Smuzhiyun }
1361*4882a593Smuzhiyun if (call_function)
1362*4882a593Smuzhiyun snd_azf3328_ctrl_enable_codecs(chip, enable);
1363*4882a593Smuzhiyun
1364*4882a593Smuzhiyun /* ...and adjust clock, too
1365*4882a593Smuzhiyun * (reduce noise and power consumption) */
1366*4882a593Smuzhiyun if (!enable)
1367*4882a593Smuzhiyun snd_azf3328_codec_setfmt_lowpower(codec);
1368*4882a593Smuzhiyun codec->running = enable;
1369*4882a593Smuzhiyun }
1370*4882a593Smuzhiyun }
1371*4882a593Smuzhiyun
1372*4882a593Smuzhiyun static void
snd_azf3328_codec_setdmaa(struct snd_azf3328 * chip,struct snd_azf3328_codec_data * codec,unsigned long addr,unsigned int period_bytes,unsigned int buffer_bytes)1373*4882a593Smuzhiyun snd_azf3328_codec_setdmaa(struct snd_azf3328 *chip,
1374*4882a593Smuzhiyun struct snd_azf3328_codec_data *codec,
1375*4882a593Smuzhiyun unsigned long addr,
1376*4882a593Smuzhiyun unsigned int period_bytes,
1377*4882a593Smuzhiyun unsigned int buffer_bytes
1378*4882a593Smuzhiyun )
1379*4882a593Smuzhiyun {
1380*4882a593Smuzhiyun WARN_ONCE(period_bytes & 1, "odd period length!?\n");
1381*4882a593Smuzhiyun WARN_ONCE(buffer_bytes != 2 * period_bytes,
1382*4882a593Smuzhiyun "missed our input expectations! %u vs. %u\n",
1383*4882a593Smuzhiyun buffer_bytes, period_bytes);
1384*4882a593Smuzhiyun if (!codec->running) {
1385*4882a593Smuzhiyun /* AZF3328 uses a two buffer pointer DMA transfer approach */
1386*4882a593Smuzhiyun
1387*4882a593Smuzhiyun unsigned long flags;
1388*4882a593Smuzhiyun
1389*4882a593Smuzhiyun /* width 32bit (prevent overflow): */
1390*4882a593Smuzhiyun u32 area_length;
1391*4882a593Smuzhiyun struct codec_setup_io {
1392*4882a593Smuzhiyun u32 dma_start_1;
1393*4882a593Smuzhiyun u32 dma_start_2;
1394*4882a593Smuzhiyun u32 dma_lengths;
1395*4882a593Smuzhiyun } __attribute__((packed)) setup_io;
1396*4882a593Smuzhiyun
1397*4882a593Smuzhiyun area_length = buffer_bytes/2;
1398*4882a593Smuzhiyun
1399*4882a593Smuzhiyun setup_io.dma_start_1 = addr;
1400*4882a593Smuzhiyun setup_io.dma_start_2 = addr+area_length;
1401*4882a593Smuzhiyun
1402*4882a593Smuzhiyun dev_dbg(chip->card->dev,
1403*4882a593Smuzhiyun "setdma: buffers %08x[%u] / %08x[%u], %u, %u\n",
1404*4882a593Smuzhiyun setup_io.dma_start_1, area_length,
1405*4882a593Smuzhiyun setup_io.dma_start_2, area_length,
1406*4882a593Smuzhiyun period_bytes, buffer_bytes);
1407*4882a593Smuzhiyun
1408*4882a593Smuzhiyun /* Hmm, are we really supposed to decrement this by 1??
1409*4882a593Smuzhiyun Most definitely certainly not: configuring full length does
1410*4882a593Smuzhiyun work properly (i.e. likely better), and BTW we
1411*4882a593Smuzhiyun violated possibly differing frame sizes with this...
1412*4882a593Smuzhiyun
1413*4882a593Smuzhiyun area_length--; |* max. index *|
1414*4882a593Smuzhiyun */
1415*4882a593Smuzhiyun
1416*4882a593Smuzhiyun /* build combined I/O buffer length word */
1417*4882a593Smuzhiyun setup_io.dma_lengths = (area_length << 16) | (area_length);
1418*4882a593Smuzhiyun
1419*4882a593Smuzhiyun spin_lock_irqsave(codec->lock, flags);
1420*4882a593Smuzhiyun snd_azf3328_codec_outl_multi(
1421*4882a593Smuzhiyun codec, IDX_IO_CODEC_DMA_START_1, &setup_io, 3
1422*4882a593Smuzhiyun );
1423*4882a593Smuzhiyun spin_unlock_irqrestore(codec->lock, flags);
1424*4882a593Smuzhiyun }
1425*4882a593Smuzhiyun }
1426*4882a593Smuzhiyun
1427*4882a593Smuzhiyun static int
snd_azf3328_pcm_prepare(struct snd_pcm_substream * substream)1428*4882a593Smuzhiyun snd_azf3328_pcm_prepare(struct snd_pcm_substream *substream)
1429*4882a593Smuzhiyun {
1430*4882a593Smuzhiyun struct snd_pcm_runtime *runtime = substream->runtime;
1431*4882a593Smuzhiyun struct snd_azf3328_codec_data *codec = runtime->private_data;
1432*4882a593Smuzhiyun #if 0
1433*4882a593Smuzhiyun unsigned int size = snd_pcm_lib_buffer_bytes(substream);
1434*4882a593Smuzhiyun unsigned int count = snd_pcm_lib_period_bytes(substream);
1435*4882a593Smuzhiyun #endif
1436*4882a593Smuzhiyun
1437*4882a593Smuzhiyun codec->dma_base = runtime->dma_addr;
1438*4882a593Smuzhiyun
1439*4882a593Smuzhiyun #if 0
1440*4882a593Smuzhiyun snd_azf3328_codec_setfmt(codec,
1441*4882a593Smuzhiyun runtime->rate,
1442*4882a593Smuzhiyun snd_pcm_format_width(runtime->format),
1443*4882a593Smuzhiyun runtime->channels);
1444*4882a593Smuzhiyun snd_azf3328_codec_setdmaa(chip, codec,
1445*4882a593Smuzhiyun runtime->dma_addr, count, size);
1446*4882a593Smuzhiyun #endif
1447*4882a593Smuzhiyun return 0;
1448*4882a593Smuzhiyun }
1449*4882a593Smuzhiyun
1450*4882a593Smuzhiyun static int
snd_azf3328_pcm_trigger(struct snd_pcm_substream * substream,int cmd)1451*4882a593Smuzhiyun snd_azf3328_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1452*4882a593Smuzhiyun {
1453*4882a593Smuzhiyun struct snd_azf3328 *chip = snd_pcm_substream_chip(substream);
1454*4882a593Smuzhiyun struct snd_pcm_runtime *runtime = substream->runtime;
1455*4882a593Smuzhiyun struct snd_azf3328_codec_data *codec = runtime->private_data;
1456*4882a593Smuzhiyun int result = 0;
1457*4882a593Smuzhiyun u16 flags1;
1458*4882a593Smuzhiyun bool previously_muted = false;
1459*4882a593Smuzhiyun bool is_main_mixer_playback_codec = (AZF_CODEC_PLAYBACK == codec->type);
1460*4882a593Smuzhiyun
1461*4882a593Smuzhiyun switch (cmd) {
1462*4882a593Smuzhiyun case SNDRV_PCM_TRIGGER_START:
1463*4882a593Smuzhiyun dev_dbg(chip->card->dev, "START PCM %s\n", codec->name);
1464*4882a593Smuzhiyun
1465*4882a593Smuzhiyun if (is_main_mixer_playback_codec) {
1466*4882a593Smuzhiyun /* mute WaveOut (avoid clicking during setup) */
1467*4882a593Smuzhiyun previously_muted =
1468*4882a593Smuzhiyun snd_azf3328_mixer_mute_control_pcm(
1469*4882a593Smuzhiyun chip, 1
1470*4882a593Smuzhiyun );
1471*4882a593Smuzhiyun }
1472*4882a593Smuzhiyun
1473*4882a593Smuzhiyun snd_azf3328_codec_setfmt(codec,
1474*4882a593Smuzhiyun runtime->rate,
1475*4882a593Smuzhiyun snd_pcm_format_width(runtime->format),
1476*4882a593Smuzhiyun runtime->channels);
1477*4882a593Smuzhiyun
1478*4882a593Smuzhiyun spin_lock(codec->lock);
1479*4882a593Smuzhiyun /* first, remember current value: */
1480*4882a593Smuzhiyun flags1 = snd_azf3328_codec_inw(codec, IDX_IO_CODEC_DMA_FLAGS);
1481*4882a593Smuzhiyun
1482*4882a593Smuzhiyun /* stop transfer */
1483*4882a593Smuzhiyun flags1 &= ~DMA_RESUME;
1484*4882a593Smuzhiyun snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS, flags1);
1485*4882a593Smuzhiyun
1486*4882a593Smuzhiyun /* FIXME: clear interrupts or what??? */
1487*4882a593Smuzhiyun snd_azf3328_codec_outw(codec, IDX_IO_CODEC_IRQTYPE, 0xffff);
1488*4882a593Smuzhiyun spin_unlock(codec->lock);
1489*4882a593Smuzhiyun
1490*4882a593Smuzhiyun snd_azf3328_codec_setdmaa(chip, codec, runtime->dma_addr,
1491*4882a593Smuzhiyun snd_pcm_lib_period_bytes(substream),
1492*4882a593Smuzhiyun snd_pcm_lib_buffer_bytes(substream)
1493*4882a593Smuzhiyun );
1494*4882a593Smuzhiyun
1495*4882a593Smuzhiyun spin_lock(codec->lock);
1496*4882a593Smuzhiyun #ifdef WIN9X
1497*4882a593Smuzhiyun /* FIXME: enable playback/recording??? */
1498*4882a593Smuzhiyun flags1 |= DMA_RUN_SOMETHING1 | DMA_RUN_SOMETHING2;
1499*4882a593Smuzhiyun snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS, flags1);
1500*4882a593Smuzhiyun
1501*4882a593Smuzhiyun /* start transfer again */
1502*4882a593Smuzhiyun /* FIXME: what is this value (0x0010)??? */
1503*4882a593Smuzhiyun flags1 |= DMA_RESUME | DMA_EPILOGUE_SOMETHING;
1504*4882a593Smuzhiyun snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS, flags1);
1505*4882a593Smuzhiyun #else /* NT4 */
1506*4882a593Smuzhiyun snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS,
1507*4882a593Smuzhiyun 0x0000);
1508*4882a593Smuzhiyun snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS,
1509*4882a593Smuzhiyun DMA_RUN_SOMETHING1);
1510*4882a593Smuzhiyun snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS,
1511*4882a593Smuzhiyun DMA_RUN_SOMETHING1 |
1512*4882a593Smuzhiyun DMA_RUN_SOMETHING2);
1513*4882a593Smuzhiyun snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS,
1514*4882a593Smuzhiyun DMA_RESUME |
1515*4882a593Smuzhiyun SOMETHING_ALMOST_ALWAYS_SET |
1516*4882a593Smuzhiyun DMA_EPILOGUE_SOMETHING |
1517*4882a593Smuzhiyun DMA_SOMETHING_ELSE);
1518*4882a593Smuzhiyun #endif
1519*4882a593Smuzhiyun spin_unlock(codec->lock);
1520*4882a593Smuzhiyun snd_azf3328_ctrl_codec_activity(chip, codec->type, 1);
1521*4882a593Smuzhiyun
1522*4882a593Smuzhiyun if (is_main_mixer_playback_codec) {
1523*4882a593Smuzhiyun /* now unmute WaveOut */
1524*4882a593Smuzhiyun if (!previously_muted)
1525*4882a593Smuzhiyun snd_azf3328_mixer_mute_control_pcm(
1526*4882a593Smuzhiyun chip, 0
1527*4882a593Smuzhiyun );
1528*4882a593Smuzhiyun }
1529*4882a593Smuzhiyun
1530*4882a593Smuzhiyun dev_dbg(chip->card->dev, "PCM STARTED %s\n", codec->name);
1531*4882a593Smuzhiyun break;
1532*4882a593Smuzhiyun case SNDRV_PCM_TRIGGER_RESUME:
1533*4882a593Smuzhiyun dev_dbg(chip->card->dev, "PCM RESUME %s\n", codec->name);
1534*4882a593Smuzhiyun /* resume codec if we were active */
1535*4882a593Smuzhiyun spin_lock(codec->lock);
1536*4882a593Smuzhiyun if (codec->running)
1537*4882a593Smuzhiyun snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS,
1538*4882a593Smuzhiyun snd_azf3328_codec_inw(
1539*4882a593Smuzhiyun codec, IDX_IO_CODEC_DMA_FLAGS
1540*4882a593Smuzhiyun ) | DMA_RESUME
1541*4882a593Smuzhiyun );
1542*4882a593Smuzhiyun spin_unlock(codec->lock);
1543*4882a593Smuzhiyun break;
1544*4882a593Smuzhiyun case SNDRV_PCM_TRIGGER_STOP:
1545*4882a593Smuzhiyun dev_dbg(chip->card->dev, "PCM STOP %s\n", codec->name);
1546*4882a593Smuzhiyun
1547*4882a593Smuzhiyun if (is_main_mixer_playback_codec) {
1548*4882a593Smuzhiyun /* mute WaveOut (avoid clicking during setup) */
1549*4882a593Smuzhiyun previously_muted =
1550*4882a593Smuzhiyun snd_azf3328_mixer_mute_control_pcm(
1551*4882a593Smuzhiyun chip, 1
1552*4882a593Smuzhiyun );
1553*4882a593Smuzhiyun }
1554*4882a593Smuzhiyun
1555*4882a593Smuzhiyun spin_lock(codec->lock);
1556*4882a593Smuzhiyun /* first, remember current value: */
1557*4882a593Smuzhiyun flags1 = snd_azf3328_codec_inw(codec, IDX_IO_CODEC_DMA_FLAGS);
1558*4882a593Smuzhiyun
1559*4882a593Smuzhiyun /* stop transfer */
1560*4882a593Smuzhiyun flags1 &= ~DMA_RESUME;
1561*4882a593Smuzhiyun snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS, flags1);
1562*4882a593Smuzhiyun
1563*4882a593Smuzhiyun /* hmm, is this really required? we're resetting the same bit
1564*4882a593Smuzhiyun * immediately thereafter... */
1565*4882a593Smuzhiyun flags1 |= DMA_RUN_SOMETHING1;
1566*4882a593Smuzhiyun snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS, flags1);
1567*4882a593Smuzhiyun
1568*4882a593Smuzhiyun flags1 &= ~DMA_RUN_SOMETHING1;
1569*4882a593Smuzhiyun snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS, flags1);
1570*4882a593Smuzhiyun spin_unlock(codec->lock);
1571*4882a593Smuzhiyun snd_azf3328_ctrl_codec_activity(chip, codec->type, 0);
1572*4882a593Smuzhiyun
1573*4882a593Smuzhiyun if (is_main_mixer_playback_codec) {
1574*4882a593Smuzhiyun /* now unmute WaveOut */
1575*4882a593Smuzhiyun if (!previously_muted)
1576*4882a593Smuzhiyun snd_azf3328_mixer_mute_control_pcm(
1577*4882a593Smuzhiyun chip, 0
1578*4882a593Smuzhiyun );
1579*4882a593Smuzhiyun }
1580*4882a593Smuzhiyun
1581*4882a593Smuzhiyun dev_dbg(chip->card->dev, "PCM STOPPED %s\n", codec->name);
1582*4882a593Smuzhiyun break;
1583*4882a593Smuzhiyun case SNDRV_PCM_TRIGGER_SUSPEND:
1584*4882a593Smuzhiyun dev_dbg(chip->card->dev, "PCM SUSPEND %s\n", codec->name);
1585*4882a593Smuzhiyun /* make sure codec is stopped */
1586*4882a593Smuzhiyun snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS,
1587*4882a593Smuzhiyun snd_azf3328_codec_inw(
1588*4882a593Smuzhiyun codec, IDX_IO_CODEC_DMA_FLAGS
1589*4882a593Smuzhiyun ) & ~DMA_RESUME
1590*4882a593Smuzhiyun );
1591*4882a593Smuzhiyun break;
1592*4882a593Smuzhiyun case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1593*4882a593Smuzhiyun WARN(1, "FIXME: SNDRV_PCM_TRIGGER_PAUSE_PUSH NIY!\n");
1594*4882a593Smuzhiyun break;
1595*4882a593Smuzhiyun case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1596*4882a593Smuzhiyun WARN(1, "FIXME: SNDRV_PCM_TRIGGER_PAUSE_RELEASE NIY!\n");
1597*4882a593Smuzhiyun break;
1598*4882a593Smuzhiyun default:
1599*4882a593Smuzhiyun WARN(1, "FIXME: unknown trigger mode!\n");
1600*4882a593Smuzhiyun return -EINVAL;
1601*4882a593Smuzhiyun }
1602*4882a593Smuzhiyun
1603*4882a593Smuzhiyun return result;
1604*4882a593Smuzhiyun }
1605*4882a593Smuzhiyun
1606*4882a593Smuzhiyun static snd_pcm_uframes_t
snd_azf3328_pcm_pointer(struct snd_pcm_substream * substream)1607*4882a593Smuzhiyun snd_azf3328_pcm_pointer(struct snd_pcm_substream *substream
1608*4882a593Smuzhiyun )
1609*4882a593Smuzhiyun {
1610*4882a593Smuzhiyun const struct snd_azf3328_codec_data *codec =
1611*4882a593Smuzhiyun substream->runtime->private_data;
1612*4882a593Smuzhiyun unsigned long result;
1613*4882a593Smuzhiyun snd_pcm_uframes_t frmres;
1614*4882a593Smuzhiyun
1615*4882a593Smuzhiyun result = snd_azf3328_codec_inl(codec, IDX_IO_CODEC_DMA_CURRPOS);
1616*4882a593Smuzhiyun
1617*4882a593Smuzhiyun /* calculate offset */
1618*4882a593Smuzhiyun #ifdef QUERY_HARDWARE
1619*4882a593Smuzhiyun result -= snd_azf3328_codec_inl(codec, IDX_IO_CODEC_DMA_START_1);
1620*4882a593Smuzhiyun #else
1621*4882a593Smuzhiyun result -= codec->dma_base;
1622*4882a593Smuzhiyun #endif
1623*4882a593Smuzhiyun frmres = bytes_to_frames( substream->runtime, result);
1624*4882a593Smuzhiyun dev_dbg(substream->pcm->card->dev, "%08li %s @ 0x%8lx, frames %8ld\n",
1625*4882a593Smuzhiyun jiffies, codec->name, result, frmres);
1626*4882a593Smuzhiyun return frmres;
1627*4882a593Smuzhiyun }
1628*4882a593Smuzhiyun
1629*4882a593Smuzhiyun /******************************************************************/
1630*4882a593Smuzhiyun
1631*4882a593Smuzhiyun #ifdef SUPPORT_GAMEPORT
1632*4882a593Smuzhiyun static inline void
snd_azf3328_gameport_irq_enable(struct snd_azf3328 * chip,bool enable)1633*4882a593Smuzhiyun snd_azf3328_gameport_irq_enable(struct snd_azf3328 *chip,
1634*4882a593Smuzhiyun bool enable
1635*4882a593Smuzhiyun )
1636*4882a593Smuzhiyun {
1637*4882a593Smuzhiyun snd_azf3328_io_reg_setb(
1638*4882a593Smuzhiyun chip->game_io+IDX_GAME_HWCONFIG,
1639*4882a593Smuzhiyun GAME_HWCFG_IRQ_ENABLE,
1640*4882a593Smuzhiyun enable
1641*4882a593Smuzhiyun );
1642*4882a593Smuzhiyun }
1643*4882a593Smuzhiyun
1644*4882a593Smuzhiyun static inline void
snd_azf3328_gameport_legacy_address_enable(struct snd_azf3328 * chip,bool enable)1645*4882a593Smuzhiyun snd_azf3328_gameport_legacy_address_enable(struct snd_azf3328 *chip,
1646*4882a593Smuzhiyun bool enable
1647*4882a593Smuzhiyun )
1648*4882a593Smuzhiyun {
1649*4882a593Smuzhiyun snd_azf3328_io_reg_setb(
1650*4882a593Smuzhiyun chip->game_io+IDX_GAME_HWCONFIG,
1651*4882a593Smuzhiyun GAME_HWCFG_LEGACY_ADDRESS_ENABLE,
1652*4882a593Smuzhiyun enable
1653*4882a593Smuzhiyun );
1654*4882a593Smuzhiyun }
1655*4882a593Smuzhiyun
1656*4882a593Smuzhiyun static void
snd_azf3328_gameport_set_counter_frequency(struct snd_azf3328 * chip,unsigned int freq_cfg)1657*4882a593Smuzhiyun snd_azf3328_gameport_set_counter_frequency(struct snd_azf3328 *chip,
1658*4882a593Smuzhiyun unsigned int freq_cfg
1659*4882a593Smuzhiyun )
1660*4882a593Smuzhiyun {
1661*4882a593Smuzhiyun snd_azf3328_io_reg_setb(
1662*4882a593Smuzhiyun chip->game_io+IDX_GAME_HWCONFIG,
1663*4882a593Smuzhiyun 0x02,
1664*4882a593Smuzhiyun (freq_cfg & 1) != 0
1665*4882a593Smuzhiyun );
1666*4882a593Smuzhiyun snd_azf3328_io_reg_setb(
1667*4882a593Smuzhiyun chip->game_io+IDX_GAME_HWCONFIG,
1668*4882a593Smuzhiyun 0x04,
1669*4882a593Smuzhiyun (freq_cfg & 2) != 0
1670*4882a593Smuzhiyun );
1671*4882a593Smuzhiyun }
1672*4882a593Smuzhiyun
1673*4882a593Smuzhiyun static inline void
snd_azf3328_gameport_axis_circuit_enable(struct snd_azf3328 * chip,bool enable)1674*4882a593Smuzhiyun snd_azf3328_gameport_axis_circuit_enable(struct snd_azf3328 *chip, bool enable)
1675*4882a593Smuzhiyun {
1676*4882a593Smuzhiyun snd_azf3328_ctrl_reg_6AH_update(
1677*4882a593Smuzhiyun chip, IO_6A_SOMETHING2_GAMEPORT, enable
1678*4882a593Smuzhiyun );
1679*4882a593Smuzhiyun }
1680*4882a593Smuzhiyun
1681*4882a593Smuzhiyun static inline void
snd_azf3328_gameport_interrupt(struct snd_azf3328 * chip)1682*4882a593Smuzhiyun snd_azf3328_gameport_interrupt(struct snd_azf3328 *chip)
1683*4882a593Smuzhiyun {
1684*4882a593Smuzhiyun /*
1685*4882a593Smuzhiyun * skeleton handler only
1686*4882a593Smuzhiyun * (we do not want axis reading in interrupt handler - too much load!)
1687*4882a593Smuzhiyun */
1688*4882a593Smuzhiyun dev_dbg(chip->card->dev, "gameport irq\n");
1689*4882a593Smuzhiyun
1690*4882a593Smuzhiyun /* this should ACK the gameport IRQ properly, hopefully. */
1691*4882a593Smuzhiyun snd_azf3328_game_inw(chip, IDX_GAME_AXIS_VALUE);
1692*4882a593Smuzhiyun }
1693*4882a593Smuzhiyun
1694*4882a593Smuzhiyun static int
snd_azf3328_gameport_open(struct gameport * gameport,int mode)1695*4882a593Smuzhiyun snd_azf3328_gameport_open(struct gameport *gameport, int mode)
1696*4882a593Smuzhiyun {
1697*4882a593Smuzhiyun struct snd_azf3328 *chip = gameport_get_port_data(gameport);
1698*4882a593Smuzhiyun int res;
1699*4882a593Smuzhiyun
1700*4882a593Smuzhiyun dev_dbg(chip->card->dev, "gameport_open, mode %d\n", mode);
1701*4882a593Smuzhiyun switch (mode) {
1702*4882a593Smuzhiyun case GAMEPORT_MODE_COOKED:
1703*4882a593Smuzhiyun case GAMEPORT_MODE_RAW:
1704*4882a593Smuzhiyun res = 0;
1705*4882a593Smuzhiyun break;
1706*4882a593Smuzhiyun default:
1707*4882a593Smuzhiyun res = -1;
1708*4882a593Smuzhiyun break;
1709*4882a593Smuzhiyun }
1710*4882a593Smuzhiyun
1711*4882a593Smuzhiyun snd_azf3328_gameport_set_counter_frequency(chip,
1712*4882a593Smuzhiyun GAME_HWCFG_ADC_COUNTER_FREQ_STD);
1713*4882a593Smuzhiyun snd_azf3328_gameport_axis_circuit_enable(chip, (res == 0));
1714*4882a593Smuzhiyun
1715*4882a593Smuzhiyun return res;
1716*4882a593Smuzhiyun }
1717*4882a593Smuzhiyun
1718*4882a593Smuzhiyun static void
snd_azf3328_gameport_close(struct gameport * gameport)1719*4882a593Smuzhiyun snd_azf3328_gameport_close(struct gameport *gameport)
1720*4882a593Smuzhiyun {
1721*4882a593Smuzhiyun struct snd_azf3328 *chip = gameport_get_port_data(gameport);
1722*4882a593Smuzhiyun
1723*4882a593Smuzhiyun dev_dbg(chip->card->dev, "gameport_close\n");
1724*4882a593Smuzhiyun snd_azf3328_gameport_set_counter_frequency(chip,
1725*4882a593Smuzhiyun GAME_HWCFG_ADC_COUNTER_FREQ_1_200);
1726*4882a593Smuzhiyun snd_azf3328_gameport_axis_circuit_enable(chip, 0);
1727*4882a593Smuzhiyun }
1728*4882a593Smuzhiyun
1729*4882a593Smuzhiyun static int
snd_azf3328_gameport_cooked_read(struct gameport * gameport,int * axes,int * buttons)1730*4882a593Smuzhiyun snd_azf3328_gameport_cooked_read(struct gameport *gameport,
1731*4882a593Smuzhiyun int *axes,
1732*4882a593Smuzhiyun int *buttons
1733*4882a593Smuzhiyun )
1734*4882a593Smuzhiyun {
1735*4882a593Smuzhiyun struct snd_azf3328 *chip = gameport_get_port_data(gameport);
1736*4882a593Smuzhiyun int i;
1737*4882a593Smuzhiyun u8 val;
1738*4882a593Smuzhiyun unsigned long flags;
1739*4882a593Smuzhiyun
1740*4882a593Smuzhiyun if (snd_BUG_ON(!chip))
1741*4882a593Smuzhiyun return 0;
1742*4882a593Smuzhiyun
1743*4882a593Smuzhiyun spin_lock_irqsave(&chip->reg_lock, flags);
1744*4882a593Smuzhiyun val = snd_azf3328_game_inb(chip, IDX_GAME_LEGACY_COMPATIBLE);
1745*4882a593Smuzhiyun *buttons = (~(val) >> 4) & 0xf;
1746*4882a593Smuzhiyun
1747*4882a593Smuzhiyun /* ok, this one is a bit dirty: cooked_read is being polled by a timer,
1748*4882a593Smuzhiyun * thus we're atomic and cannot actively wait in here
1749*4882a593Smuzhiyun * (which would be useful for us since it probably would be better
1750*4882a593Smuzhiyun * to trigger a measurement in here, then wait a short amount of
1751*4882a593Smuzhiyun * time until it's finished, then read values of _this_ measurement).
1752*4882a593Smuzhiyun *
1753*4882a593Smuzhiyun * Thus we simply resort to reading values if they're available already
1754*4882a593Smuzhiyun * and trigger the next measurement.
1755*4882a593Smuzhiyun */
1756*4882a593Smuzhiyun
1757*4882a593Smuzhiyun val = snd_azf3328_game_inb(chip, IDX_GAME_AXES_CONFIG);
1758*4882a593Smuzhiyun if (val & GAME_AXES_SAMPLING_READY) {
1759*4882a593Smuzhiyun for (i = 0; i < ARRAY_SIZE(chip->axes); ++i) {
1760*4882a593Smuzhiyun /* configure the axis to read */
1761*4882a593Smuzhiyun val = (i << 4) | 0x0f;
1762*4882a593Smuzhiyun snd_azf3328_game_outb(chip, IDX_GAME_AXES_CONFIG, val);
1763*4882a593Smuzhiyun
1764*4882a593Smuzhiyun chip->axes[i] = snd_azf3328_game_inw(
1765*4882a593Smuzhiyun chip, IDX_GAME_AXIS_VALUE
1766*4882a593Smuzhiyun );
1767*4882a593Smuzhiyun }
1768*4882a593Smuzhiyun }
1769*4882a593Smuzhiyun
1770*4882a593Smuzhiyun /* trigger next sampling of axes, to be evaluated the next time we
1771*4882a593Smuzhiyun * enter this function */
1772*4882a593Smuzhiyun
1773*4882a593Smuzhiyun /* for some very, very strange reason we cannot enable
1774*4882a593Smuzhiyun * Measurement Ready monitoring for all axes here,
1775*4882a593Smuzhiyun * at least not when only one joystick connected */
1776*4882a593Smuzhiyun val = 0x03; /* we're able to monitor axes 1 and 2 only */
1777*4882a593Smuzhiyun snd_azf3328_game_outb(chip, IDX_GAME_AXES_CONFIG, val);
1778*4882a593Smuzhiyun
1779*4882a593Smuzhiyun snd_azf3328_game_outw(chip, IDX_GAME_AXIS_VALUE, 0xffff);
1780*4882a593Smuzhiyun spin_unlock_irqrestore(&chip->reg_lock, flags);
1781*4882a593Smuzhiyun
1782*4882a593Smuzhiyun for (i = 0; i < ARRAY_SIZE(chip->axes); i++) {
1783*4882a593Smuzhiyun axes[i] = chip->axes[i];
1784*4882a593Smuzhiyun if (axes[i] == 0xffff)
1785*4882a593Smuzhiyun axes[i] = -1;
1786*4882a593Smuzhiyun }
1787*4882a593Smuzhiyun
1788*4882a593Smuzhiyun dev_dbg(chip->card->dev, "cooked_read: axes %d %d %d %d buttons %d\n",
1789*4882a593Smuzhiyun axes[0], axes[1], axes[2], axes[3], *buttons);
1790*4882a593Smuzhiyun
1791*4882a593Smuzhiyun return 0;
1792*4882a593Smuzhiyun }
1793*4882a593Smuzhiyun
1794*4882a593Smuzhiyun static int
snd_azf3328_gameport(struct snd_azf3328 * chip,int dev)1795*4882a593Smuzhiyun snd_azf3328_gameport(struct snd_azf3328 *chip, int dev)
1796*4882a593Smuzhiyun {
1797*4882a593Smuzhiyun struct gameport *gp;
1798*4882a593Smuzhiyun
1799*4882a593Smuzhiyun chip->gameport = gp = gameport_allocate_port();
1800*4882a593Smuzhiyun if (!gp) {
1801*4882a593Smuzhiyun dev_err(chip->card->dev, "cannot alloc memory for gameport\n");
1802*4882a593Smuzhiyun return -ENOMEM;
1803*4882a593Smuzhiyun }
1804*4882a593Smuzhiyun
1805*4882a593Smuzhiyun gameport_set_name(gp, "AZF3328 Gameport");
1806*4882a593Smuzhiyun gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
1807*4882a593Smuzhiyun gameport_set_dev_parent(gp, &chip->pci->dev);
1808*4882a593Smuzhiyun gp->io = chip->game_io;
1809*4882a593Smuzhiyun gameport_set_port_data(gp, chip);
1810*4882a593Smuzhiyun
1811*4882a593Smuzhiyun gp->open = snd_azf3328_gameport_open;
1812*4882a593Smuzhiyun gp->close = snd_azf3328_gameport_close;
1813*4882a593Smuzhiyun gp->fuzz = 16; /* seems ok */
1814*4882a593Smuzhiyun gp->cooked_read = snd_azf3328_gameport_cooked_read;
1815*4882a593Smuzhiyun
1816*4882a593Smuzhiyun /* DISABLE legacy address: we don't need it! */
1817*4882a593Smuzhiyun snd_azf3328_gameport_legacy_address_enable(chip, 0);
1818*4882a593Smuzhiyun
1819*4882a593Smuzhiyun snd_azf3328_gameport_set_counter_frequency(chip,
1820*4882a593Smuzhiyun GAME_HWCFG_ADC_COUNTER_FREQ_1_200);
1821*4882a593Smuzhiyun snd_azf3328_gameport_axis_circuit_enable(chip, 0);
1822*4882a593Smuzhiyun
1823*4882a593Smuzhiyun gameport_register_port(chip->gameport);
1824*4882a593Smuzhiyun
1825*4882a593Smuzhiyun return 0;
1826*4882a593Smuzhiyun }
1827*4882a593Smuzhiyun
1828*4882a593Smuzhiyun static void
snd_azf3328_gameport_free(struct snd_azf3328 * chip)1829*4882a593Smuzhiyun snd_azf3328_gameport_free(struct snd_azf3328 *chip)
1830*4882a593Smuzhiyun {
1831*4882a593Smuzhiyun if (chip->gameport) {
1832*4882a593Smuzhiyun gameport_unregister_port(chip->gameport);
1833*4882a593Smuzhiyun chip->gameport = NULL;
1834*4882a593Smuzhiyun }
1835*4882a593Smuzhiyun snd_azf3328_gameport_irq_enable(chip, 0);
1836*4882a593Smuzhiyun }
1837*4882a593Smuzhiyun #else
1838*4882a593Smuzhiyun static inline int
snd_azf3328_gameport(struct snd_azf3328 * chip,int dev)1839*4882a593Smuzhiyun snd_azf3328_gameport(struct snd_azf3328 *chip, int dev) { return -ENOSYS; }
1840*4882a593Smuzhiyun static inline void
snd_azf3328_gameport_free(struct snd_azf3328 * chip)1841*4882a593Smuzhiyun snd_azf3328_gameport_free(struct snd_azf3328 *chip) { }
1842*4882a593Smuzhiyun static inline void
snd_azf3328_gameport_interrupt(struct snd_azf3328 * chip)1843*4882a593Smuzhiyun snd_azf3328_gameport_interrupt(struct snd_azf3328 *chip)
1844*4882a593Smuzhiyun {
1845*4882a593Smuzhiyun dev_warn(chip->card->dev, "huh, game port IRQ occurred!?\n");
1846*4882a593Smuzhiyun }
1847*4882a593Smuzhiyun #endif /* SUPPORT_GAMEPORT */
1848*4882a593Smuzhiyun
1849*4882a593Smuzhiyun /******************************************************************/
1850*4882a593Smuzhiyun
1851*4882a593Smuzhiyun static inline void
snd_azf3328_irq_log_unknown_type(struct snd_azf3328 * chip,u8 which)1852*4882a593Smuzhiyun snd_azf3328_irq_log_unknown_type(struct snd_azf3328 *chip, u8 which)
1853*4882a593Smuzhiyun {
1854*4882a593Smuzhiyun dev_dbg(chip->card->dev,
1855*4882a593Smuzhiyun "unknown IRQ type (%x) occurred, please report!\n",
1856*4882a593Smuzhiyun which);
1857*4882a593Smuzhiyun }
1858*4882a593Smuzhiyun
1859*4882a593Smuzhiyun static inline void
snd_azf3328_pcm_interrupt(struct snd_azf3328 * chip,const struct snd_azf3328_codec_data * first_codec,u8 status)1860*4882a593Smuzhiyun snd_azf3328_pcm_interrupt(struct snd_azf3328 *chip,
1861*4882a593Smuzhiyun const struct snd_azf3328_codec_data *first_codec,
1862*4882a593Smuzhiyun u8 status
1863*4882a593Smuzhiyun )
1864*4882a593Smuzhiyun {
1865*4882a593Smuzhiyun u8 which;
1866*4882a593Smuzhiyun enum snd_azf3328_codec_type codec_type;
1867*4882a593Smuzhiyun const struct snd_azf3328_codec_data *codec = first_codec;
1868*4882a593Smuzhiyun
1869*4882a593Smuzhiyun for (codec_type = AZF_CODEC_PLAYBACK;
1870*4882a593Smuzhiyun codec_type <= AZF_CODEC_I2S_OUT;
1871*4882a593Smuzhiyun ++codec_type, ++codec) {
1872*4882a593Smuzhiyun
1873*4882a593Smuzhiyun /* skip codec if there's no interrupt for it */
1874*4882a593Smuzhiyun if (!(status & (1 << codec_type)))
1875*4882a593Smuzhiyun continue;
1876*4882a593Smuzhiyun
1877*4882a593Smuzhiyun spin_lock(codec->lock);
1878*4882a593Smuzhiyun which = snd_azf3328_codec_inb(codec, IDX_IO_CODEC_IRQTYPE);
1879*4882a593Smuzhiyun /* ack all IRQ types immediately */
1880*4882a593Smuzhiyun snd_azf3328_codec_outb(codec, IDX_IO_CODEC_IRQTYPE, which);
1881*4882a593Smuzhiyun spin_unlock(codec->lock);
1882*4882a593Smuzhiyun
1883*4882a593Smuzhiyun if (codec->substream) {
1884*4882a593Smuzhiyun snd_pcm_period_elapsed(codec->substream);
1885*4882a593Smuzhiyun dev_dbg(chip->card->dev, "%s period done (#%x), @ %x\n",
1886*4882a593Smuzhiyun codec->name,
1887*4882a593Smuzhiyun which,
1888*4882a593Smuzhiyun snd_azf3328_codec_inl(
1889*4882a593Smuzhiyun codec, IDX_IO_CODEC_DMA_CURRPOS));
1890*4882a593Smuzhiyun } else
1891*4882a593Smuzhiyun dev_warn(chip->card->dev, "irq handler problem!\n");
1892*4882a593Smuzhiyun if (which & IRQ_SOMETHING)
1893*4882a593Smuzhiyun snd_azf3328_irq_log_unknown_type(chip, which);
1894*4882a593Smuzhiyun }
1895*4882a593Smuzhiyun }
1896*4882a593Smuzhiyun
1897*4882a593Smuzhiyun static irqreturn_t
snd_azf3328_interrupt(int irq,void * dev_id)1898*4882a593Smuzhiyun snd_azf3328_interrupt(int irq, void *dev_id)
1899*4882a593Smuzhiyun {
1900*4882a593Smuzhiyun struct snd_azf3328 *chip = dev_id;
1901*4882a593Smuzhiyun u8 status;
1902*4882a593Smuzhiyun static unsigned long irq_count;
1903*4882a593Smuzhiyun
1904*4882a593Smuzhiyun status = snd_azf3328_ctrl_inb(chip, IDX_IO_IRQSTATUS);
1905*4882a593Smuzhiyun
1906*4882a593Smuzhiyun /* fast path out, to ease interrupt sharing */
1907*4882a593Smuzhiyun if (!(status &
1908*4882a593Smuzhiyun (IRQ_PLAYBACK|IRQ_RECORDING|IRQ_I2S_OUT
1909*4882a593Smuzhiyun |IRQ_GAMEPORT|IRQ_MPU401|IRQ_TIMER)
1910*4882a593Smuzhiyun ))
1911*4882a593Smuzhiyun return IRQ_NONE; /* must be interrupt for another device */
1912*4882a593Smuzhiyun
1913*4882a593Smuzhiyun dev_dbg(chip->card->dev,
1914*4882a593Smuzhiyun "irq_count %ld! IDX_IO_IRQSTATUS %04x\n",
1915*4882a593Smuzhiyun irq_count++ /* debug-only */,
1916*4882a593Smuzhiyun status);
1917*4882a593Smuzhiyun
1918*4882a593Smuzhiyun if (status & IRQ_TIMER) {
1919*4882a593Smuzhiyun /* dev_dbg(chip->card->dev, "timer %ld\n",
1920*4882a593Smuzhiyun snd_azf3328_codec_inl(chip, IDX_IO_TIMER_VALUE)
1921*4882a593Smuzhiyun & TIMER_VALUE_MASK
1922*4882a593Smuzhiyun ); */
1923*4882a593Smuzhiyun if (chip->timer)
1924*4882a593Smuzhiyun snd_timer_interrupt(chip->timer, chip->timer->sticks);
1925*4882a593Smuzhiyun /* ACK timer */
1926*4882a593Smuzhiyun spin_lock(&chip->reg_lock);
1927*4882a593Smuzhiyun snd_azf3328_ctrl_outb(chip, IDX_IO_TIMER_VALUE + 3, 0x07);
1928*4882a593Smuzhiyun spin_unlock(&chip->reg_lock);
1929*4882a593Smuzhiyun dev_dbg(chip->card->dev, "timer IRQ\n");
1930*4882a593Smuzhiyun }
1931*4882a593Smuzhiyun
1932*4882a593Smuzhiyun if (status & (IRQ_PLAYBACK|IRQ_RECORDING|IRQ_I2S_OUT))
1933*4882a593Smuzhiyun snd_azf3328_pcm_interrupt(chip, chip->codecs, status);
1934*4882a593Smuzhiyun
1935*4882a593Smuzhiyun if (status & IRQ_GAMEPORT)
1936*4882a593Smuzhiyun snd_azf3328_gameport_interrupt(chip);
1937*4882a593Smuzhiyun
1938*4882a593Smuzhiyun /* MPU401 has less critical IRQ requirements
1939*4882a593Smuzhiyun * than timer and playback/recording, right? */
1940*4882a593Smuzhiyun if (status & IRQ_MPU401) {
1941*4882a593Smuzhiyun snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
1942*4882a593Smuzhiyun
1943*4882a593Smuzhiyun /* hmm, do we have to ack the IRQ here somehow?
1944*4882a593Smuzhiyun * If so, then I don't know how yet... */
1945*4882a593Smuzhiyun dev_dbg(chip->card->dev, "MPU401 IRQ\n");
1946*4882a593Smuzhiyun }
1947*4882a593Smuzhiyun return IRQ_HANDLED;
1948*4882a593Smuzhiyun }
1949*4882a593Smuzhiyun
1950*4882a593Smuzhiyun /*****************************************************************/
1951*4882a593Smuzhiyun
1952*4882a593Smuzhiyun /* as long as we think we have identical snd_pcm_hardware parameters
1953*4882a593Smuzhiyun for playback, capture and i2s out, we can use the same physical struct
1954*4882a593Smuzhiyun since the struct is simply being copied into a member.
1955*4882a593Smuzhiyun */
1956*4882a593Smuzhiyun static const struct snd_pcm_hardware snd_azf3328_hardware =
1957*4882a593Smuzhiyun {
1958*4882a593Smuzhiyun /* FIXME!! Correct? */
1959*4882a593Smuzhiyun .info = SNDRV_PCM_INFO_MMAP |
1960*4882a593Smuzhiyun SNDRV_PCM_INFO_INTERLEAVED |
1961*4882a593Smuzhiyun SNDRV_PCM_INFO_MMAP_VALID,
1962*4882a593Smuzhiyun .formats = SNDRV_PCM_FMTBIT_S8 |
1963*4882a593Smuzhiyun SNDRV_PCM_FMTBIT_U8 |
1964*4882a593Smuzhiyun SNDRV_PCM_FMTBIT_S16_LE |
1965*4882a593Smuzhiyun SNDRV_PCM_FMTBIT_U16_LE,
1966*4882a593Smuzhiyun .rates = SNDRV_PCM_RATE_5512 |
1967*4882a593Smuzhiyun SNDRV_PCM_RATE_8000_48000 |
1968*4882a593Smuzhiyun SNDRV_PCM_RATE_KNOT,
1969*4882a593Smuzhiyun .rate_min = AZF_FREQ_4000,
1970*4882a593Smuzhiyun .rate_max = AZF_FREQ_66200,
1971*4882a593Smuzhiyun .channels_min = 1,
1972*4882a593Smuzhiyun .channels_max = 2,
1973*4882a593Smuzhiyun .buffer_bytes_max = (64*1024),
1974*4882a593Smuzhiyun .period_bytes_min = 1024,
1975*4882a593Smuzhiyun .period_bytes_max = (32*1024),
1976*4882a593Smuzhiyun /* We simply have two DMA areas (instead of a list of descriptors
1977*4882a593Smuzhiyun such as other cards); I believe that this is a fixed hardware
1978*4882a593Smuzhiyun attribute and there isn't much driver magic to be done to expand it.
1979*4882a593Smuzhiyun Thus indicate that we have at least and at most 2 periods. */
1980*4882a593Smuzhiyun .periods_min = 2,
1981*4882a593Smuzhiyun .periods_max = 2,
1982*4882a593Smuzhiyun /* FIXME: maybe that card actually has a FIFO?
1983*4882a593Smuzhiyun * Hmm, it seems newer revisions do have one, but we still don't know
1984*4882a593Smuzhiyun * its size... */
1985*4882a593Smuzhiyun .fifo_size = 0,
1986*4882a593Smuzhiyun };
1987*4882a593Smuzhiyun
1988*4882a593Smuzhiyun
1989*4882a593Smuzhiyun static const unsigned int snd_azf3328_fixed_rates[] = {
1990*4882a593Smuzhiyun AZF_FREQ_4000,
1991*4882a593Smuzhiyun AZF_FREQ_4800,
1992*4882a593Smuzhiyun AZF_FREQ_5512,
1993*4882a593Smuzhiyun AZF_FREQ_6620,
1994*4882a593Smuzhiyun AZF_FREQ_8000,
1995*4882a593Smuzhiyun AZF_FREQ_9600,
1996*4882a593Smuzhiyun AZF_FREQ_11025,
1997*4882a593Smuzhiyun AZF_FREQ_13240,
1998*4882a593Smuzhiyun AZF_FREQ_16000,
1999*4882a593Smuzhiyun AZF_FREQ_22050,
2000*4882a593Smuzhiyun AZF_FREQ_32000,
2001*4882a593Smuzhiyun AZF_FREQ_44100,
2002*4882a593Smuzhiyun AZF_FREQ_48000,
2003*4882a593Smuzhiyun AZF_FREQ_66200
2004*4882a593Smuzhiyun };
2005*4882a593Smuzhiyun
2006*4882a593Smuzhiyun static const struct snd_pcm_hw_constraint_list snd_azf3328_hw_constraints_rates = {
2007*4882a593Smuzhiyun .count = ARRAY_SIZE(snd_azf3328_fixed_rates),
2008*4882a593Smuzhiyun .list = snd_azf3328_fixed_rates,
2009*4882a593Smuzhiyun .mask = 0,
2010*4882a593Smuzhiyun };
2011*4882a593Smuzhiyun
2012*4882a593Smuzhiyun /*****************************************************************/
2013*4882a593Smuzhiyun
2014*4882a593Smuzhiyun static int
snd_azf3328_pcm_open(struct snd_pcm_substream * substream,enum snd_azf3328_codec_type codec_type)2015*4882a593Smuzhiyun snd_azf3328_pcm_open(struct snd_pcm_substream *substream,
2016*4882a593Smuzhiyun enum snd_azf3328_codec_type codec_type
2017*4882a593Smuzhiyun )
2018*4882a593Smuzhiyun {
2019*4882a593Smuzhiyun struct snd_azf3328 *chip = snd_pcm_substream_chip(substream);
2020*4882a593Smuzhiyun struct snd_pcm_runtime *runtime = substream->runtime;
2021*4882a593Smuzhiyun struct snd_azf3328_codec_data *codec = &chip->codecs[codec_type];
2022*4882a593Smuzhiyun
2023*4882a593Smuzhiyun codec->substream = substream;
2024*4882a593Smuzhiyun
2025*4882a593Smuzhiyun /* same parameters for all our codecs - at least we think so... */
2026*4882a593Smuzhiyun runtime->hw = snd_azf3328_hardware;
2027*4882a593Smuzhiyun
2028*4882a593Smuzhiyun snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2029*4882a593Smuzhiyun &snd_azf3328_hw_constraints_rates);
2030*4882a593Smuzhiyun runtime->private_data = codec;
2031*4882a593Smuzhiyun return 0;
2032*4882a593Smuzhiyun }
2033*4882a593Smuzhiyun
2034*4882a593Smuzhiyun static int
snd_azf3328_pcm_playback_open(struct snd_pcm_substream * substream)2035*4882a593Smuzhiyun snd_azf3328_pcm_playback_open(struct snd_pcm_substream *substream)
2036*4882a593Smuzhiyun {
2037*4882a593Smuzhiyun return snd_azf3328_pcm_open(substream, AZF_CODEC_PLAYBACK);
2038*4882a593Smuzhiyun }
2039*4882a593Smuzhiyun
2040*4882a593Smuzhiyun static int
snd_azf3328_pcm_capture_open(struct snd_pcm_substream * substream)2041*4882a593Smuzhiyun snd_azf3328_pcm_capture_open(struct snd_pcm_substream *substream)
2042*4882a593Smuzhiyun {
2043*4882a593Smuzhiyun return snd_azf3328_pcm_open(substream, AZF_CODEC_CAPTURE);
2044*4882a593Smuzhiyun }
2045*4882a593Smuzhiyun
2046*4882a593Smuzhiyun static int
snd_azf3328_pcm_i2s_out_open(struct snd_pcm_substream * substream)2047*4882a593Smuzhiyun snd_azf3328_pcm_i2s_out_open(struct snd_pcm_substream *substream)
2048*4882a593Smuzhiyun {
2049*4882a593Smuzhiyun return snd_azf3328_pcm_open(substream, AZF_CODEC_I2S_OUT);
2050*4882a593Smuzhiyun }
2051*4882a593Smuzhiyun
2052*4882a593Smuzhiyun static int
snd_azf3328_pcm_close(struct snd_pcm_substream * substream)2053*4882a593Smuzhiyun snd_azf3328_pcm_close(struct snd_pcm_substream *substream
2054*4882a593Smuzhiyun )
2055*4882a593Smuzhiyun {
2056*4882a593Smuzhiyun struct snd_azf3328_codec_data *codec =
2057*4882a593Smuzhiyun substream->runtime->private_data;
2058*4882a593Smuzhiyun
2059*4882a593Smuzhiyun codec->substream = NULL;
2060*4882a593Smuzhiyun return 0;
2061*4882a593Smuzhiyun }
2062*4882a593Smuzhiyun
2063*4882a593Smuzhiyun /******************************************************************/
2064*4882a593Smuzhiyun
2065*4882a593Smuzhiyun static const struct snd_pcm_ops snd_azf3328_playback_ops = {
2066*4882a593Smuzhiyun .open = snd_azf3328_pcm_playback_open,
2067*4882a593Smuzhiyun .close = snd_azf3328_pcm_close,
2068*4882a593Smuzhiyun .prepare = snd_azf3328_pcm_prepare,
2069*4882a593Smuzhiyun .trigger = snd_azf3328_pcm_trigger,
2070*4882a593Smuzhiyun .pointer = snd_azf3328_pcm_pointer
2071*4882a593Smuzhiyun };
2072*4882a593Smuzhiyun
2073*4882a593Smuzhiyun static const struct snd_pcm_ops snd_azf3328_capture_ops = {
2074*4882a593Smuzhiyun .open = snd_azf3328_pcm_capture_open,
2075*4882a593Smuzhiyun .close = snd_azf3328_pcm_close,
2076*4882a593Smuzhiyun .prepare = snd_azf3328_pcm_prepare,
2077*4882a593Smuzhiyun .trigger = snd_azf3328_pcm_trigger,
2078*4882a593Smuzhiyun .pointer = snd_azf3328_pcm_pointer
2079*4882a593Smuzhiyun };
2080*4882a593Smuzhiyun
2081*4882a593Smuzhiyun static const struct snd_pcm_ops snd_azf3328_i2s_out_ops = {
2082*4882a593Smuzhiyun .open = snd_azf3328_pcm_i2s_out_open,
2083*4882a593Smuzhiyun .close = snd_azf3328_pcm_close,
2084*4882a593Smuzhiyun .prepare = snd_azf3328_pcm_prepare,
2085*4882a593Smuzhiyun .trigger = snd_azf3328_pcm_trigger,
2086*4882a593Smuzhiyun .pointer = snd_azf3328_pcm_pointer
2087*4882a593Smuzhiyun };
2088*4882a593Smuzhiyun
2089*4882a593Smuzhiyun static int
snd_azf3328_pcm(struct snd_azf3328 * chip)2090*4882a593Smuzhiyun snd_azf3328_pcm(struct snd_azf3328 *chip)
2091*4882a593Smuzhiyun {
2092*4882a593Smuzhiyun /* pcm devices */
2093*4882a593Smuzhiyun enum { AZF_PCMDEV_STD, AZF_PCMDEV_I2S_OUT, NUM_AZF_PCMDEVS };
2094*4882a593Smuzhiyun
2095*4882a593Smuzhiyun struct snd_pcm *pcm;
2096*4882a593Smuzhiyun int err;
2097*4882a593Smuzhiyun
2098*4882a593Smuzhiyun err = snd_pcm_new(chip->card, "AZF3328 DSP", AZF_PCMDEV_STD,
2099*4882a593Smuzhiyun 1, 1, &pcm);
2100*4882a593Smuzhiyun if (err < 0)
2101*4882a593Smuzhiyun return err;
2102*4882a593Smuzhiyun snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
2103*4882a593Smuzhiyun &snd_azf3328_playback_ops);
2104*4882a593Smuzhiyun snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
2105*4882a593Smuzhiyun &snd_azf3328_capture_ops);
2106*4882a593Smuzhiyun
2107*4882a593Smuzhiyun pcm->private_data = chip;
2108*4882a593Smuzhiyun pcm->info_flags = 0;
2109*4882a593Smuzhiyun strcpy(pcm->name, chip->card->shortname);
2110*4882a593Smuzhiyun /* same pcm object for playback/capture (see snd_pcm_new() above) */
2111*4882a593Smuzhiyun chip->pcm[AZF_CODEC_PLAYBACK] = pcm;
2112*4882a593Smuzhiyun chip->pcm[AZF_CODEC_CAPTURE] = pcm;
2113*4882a593Smuzhiyun
2114*4882a593Smuzhiyun snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, &chip->pci->dev,
2115*4882a593Smuzhiyun 64*1024, 64*1024);
2116*4882a593Smuzhiyun
2117*4882a593Smuzhiyun err = snd_pcm_new(chip->card, "AZF3328 I2S OUT", AZF_PCMDEV_I2S_OUT,
2118*4882a593Smuzhiyun 1, 0, &pcm);
2119*4882a593Smuzhiyun if (err < 0)
2120*4882a593Smuzhiyun return err;
2121*4882a593Smuzhiyun snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
2122*4882a593Smuzhiyun &snd_azf3328_i2s_out_ops);
2123*4882a593Smuzhiyun
2124*4882a593Smuzhiyun pcm->private_data = chip;
2125*4882a593Smuzhiyun pcm->info_flags = 0;
2126*4882a593Smuzhiyun strcpy(pcm->name, chip->card->shortname);
2127*4882a593Smuzhiyun chip->pcm[AZF_CODEC_I2S_OUT] = pcm;
2128*4882a593Smuzhiyun
2129*4882a593Smuzhiyun snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, &chip->pci->dev,
2130*4882a593Smuzhiyun 64*1024, 64*1024);
2131*4882a593Smuzhiyun
2132*4882a593Smuzhiyun return 0;
2133*4882a593Smuzhiyun }
2134*4882a593Smuzhiyun
2135*4882a593Smuzhiyun /******************************************************************/
2136*4882a593Smuzhiyun
2137*4882a593Smuzhiyun /*** NOTE: the physical timer resolution actually is 1024000 ticks per second
2138*4882a593Smuzhiyun *** (probably derived from main crystal via a divider of 24),
2139*4882a593Smuzhiyun *** but announcing those attributes to user-space would make programs
2140*4882a593Smuzhiyun *** configure the timer to a 1 tick value, resulting in an absolutely fatal
2141*4882a593Smuzhiyun *** timer IRQ storm.
2142*4882a593Smuzhiyun *** Thus I chose to announce a down-scaled virtual timer to the outside and
2143*4882a593Smuzhiyun *** calculate real timer countdown values internally.
2144*4882a593Smuzhiyun *** (the scale factor can be set via module parameter "seqtimer_scaling").
2145*4882a593Smuzhiyun ***/
2146*4882a593Smuzhiyun
2147*4882a593Smuzhiyun static int
snd_azf3328_timer_start(struct snd_timer * timer)2148*4882a593Smuzhiyun snd_azf3328_timer_start(struct snd_timer *timer)
2149*4882a593Smuzhiyun {
2150*4882a593Smuzhiyun struct snd_azf3328 *chip;
2151*4882a593Smuzhiyun unsigned long flags;
2152*4882a593Smuzhiyun unsigned int delay;
2153*4882a593Smuzhiyun
2154*4882a593Smuzhiyun chip = snd_timer_chip(timer);
2155*4882a593Smuzhiyun delay = ((timer->sticks * seqtimer_scaling) - 1) & TIMER_VALUE_MASK;
2156*4882a593Smuzhiyun if (delay < 49) {
2157*4882a593Smuzhiyun /* uhoh, that's not good, since user-space won't know about
2158*4882a593Smuzhiyun * this timing tweak
2159*4882a593Smuzhiyun * (we need to do it to avoid a lockup, though) */
2160*4882a593Smuzhiyun
2161*4882a593Smuzhiyun dev_dbg(chip->card->dev, "delay was too low (%d)!\n", delay);
2162*4882a593Smuzhiyun delay = 49; /* minimum time is 49 ticks */
2163*4882a593Smuzhiyun }
2164*4882a593Smuzhiyun dev_dbg(chip->card->dev, "setting timer countdown value %d\n", delay);
2165*4882a593Smuzhiyun delay |= TIMER_COUNTDOWN_ENABLE | TIMER_IRQ_ENABLE;
2166*4882a593Smuzhiyun spin_lock_irqsave(&chip->reg_lock, flags);
2167*4882a593Smuzhiyun snd_azf3328_ctrl_outl(chip, IDX_IO_TIMER_VALUE, delay);
2168*4882a593Smuzhiyun spin_unlock_irqrestore(&chip->reg_lock, flags);
2169*4882a593Smuzhiyun return 0;
2170*4882a593Smuzhiyun }
2171*4882a593Smuzhiyun
2172*4882a593Smuzhiyun static int
snd_azf3328_timer_stop(struct snd_timer * timer)2173*4882a593Smuzhiyun snd_azf3328_timer_stop(struct snd_timer *timer)
2174*4882a593Smuzhiyun {
2175*4882a593Smuzhiyun struct snd_azf3328 *chip;
2176*4882a593Smuzhiyun unsigned long flags;
2177*4882a593Smuzhiyun
2178*4882a593Smuzhiyun chip = snd_timer_chip(timer);
2179*4882a593Smuzhiyun spin_lock_irqsave(&chip->reg_lock, flags);
2180*4882a593Smuzhiyun /* disable timer countdown and interrupt */
2181*4882a593Smuzhiyun /* Hmm, should we write TIMER_IRQ_ACK here?
2182*4882a593Smuzhiyun YES indeed, otherwise a rogue timer operation - which prompts
2183*4882a593Smuzhiyun ALSA(?) to call repeated stop() in vain, but NOT start() -
2184*4882a593Smuzhiyun will never end (value 0x03 is kept shown in control byte).
2185*4882a593Smuzhiyun Simply manually poking 0x04 _once_ immediately successfully stops
2186*4882a593Smuzhiyun the hardware/ALSA interrupt activity. */
2187*4882a593Smuzhiyun snd_azf3328_ctrl_outb(chip, IDX_IO_TIMER_VALUE + 3, 0x04);
2188*4882a593Smuzhiyun spin_unlock_irqrestore(&chip->reg_lock, flags);
2189*4882a593Smuzhiyun return 0;
2190*4882a593Smuzhiyun }
2191*4882a593Smuzhiyun
2192*4882a593Smuzhiyun
2193*4882a593Smuzhiyun static int
snd_azf3328_timer_precise_resolution(struct snd_timer * timer,unsigned long * num,unsigned long * den)2194*4882a593Smuzhiyun snd_azf3328_timer_precise_resolution(struct snd_timer *timer,
2195*4882a593Smuzhiyun unsigned long *num, unsigned long *den)
2196*4882a593Smuzhiyun {
2197*4882a593Smuzhiyun *num = 1;
2198*4882a593Smuzhiyun *den = 1024000 / seqtimer_scaling;
2199*4882a593Smuzhiyun return 0;
2200*4882a593Smuzhiyun }
2201*4882a593Smuzhiyun
2202*4882a593Smuzhiyun static struct snd_timer_hardware snd_azf3328_timer_hw = {
2203*4882a593Smuzhiyun .flags = SNDRV_TIMER_HW_AUTO,
2204*4882a593Smuzhiyun .resolution = 977, /* 1000000/1024000 = 0.9765625us */
2205*4882a593Smuzhiyun .ticks = 1024000, /* max tick count, defined by the value register; actually it's not 1024000, but 1048576, but we don't care */
2206*4882a593Smuzhiyun .start = snd_azf3328_timer_start,
2207*4882a593Smuzhiyun .stop = snd_azf3328_timer_stop,
2208*4882a593Smuzhiyun .precise_resolution = snd_azf3328_timer_precise_resolution,
2209*4882a593Smuzhiyun };
2210*4882a593Smuzhiyun
2211*4882a593Smuzhiyun static int
snd_azf3328_timer(struct snd_azf3328 * chip,int device)2212*4882a593Smuzhiyun snd_azf3328_timer(struct snd_azf3328 *chip, int device)
2213*4882a593Smuzhiyun {
2214*4882a593Smuzhiyun struct snd_timer *timer = NULL;
2215*4882a593Smuzhiyun struct snd_timer_id tid;
2216*4882a593Smuzhiyun int err;
2217*4882a593Smuzhiyun
2218*4882a593Smuzhiyun tid.dev_class = SNDRV_TIMER_CLASS_CARD;
2219*4882a593Smuzhiyun tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
2220*4882a593Smuzhiyun tid.card = chip->card->number;
2221*4882a593Smuzhiyun tid.device = device;
2222*4882a593Smuzhiyun tid.subdevice = 0;
2223*4882a593Smuzhiyun
2224*4882a593Smuzhiyun snd_azf3328_timer_hw.resolution *= seqtimer_scaling;
2225*4882a593Smuzhiyun snd_azf3328_timer_hw.ticks /= seqtimer_scaling;
2226*4882a593Smuzhiyun
2227*4882a593Smuzhiyun err = snd_timer_new(chip->card, "AZF3328", &tid, &timer);
2228*4882a593Smuzhiyun if (err < 0)
2229*4882a593Smuzhiyun goto out;
2230*4882a593Smuzhiyun
2231*4882a593Smuzhiyun strcpy(timer->name, "AZF3328 timer");
2232*4882a593Smuzhiyun timer->private_data = chip;
2233*4882a593Smuzhiyun timer->hw = snd_azf3328_timer_hw;
2234*4882a593Smuzhiyun
2235*4882a593Smuzhiyun chip->timer = timer;
2236*4882a593Smuzhiyun
2237*4882a593Smuzhiyun snd_azf3328_timer_stop(timer);
2238*4882a593Smuzhiyun
2239*4882a593Smuzhiyun err = 0;
2240*4882a593Smuzhiyun
2241*4882a593Smuzhiyun out:
2242*4882a593Smuzhiyun return err;
2243*4882a593Smuzhiyun }
2244*4882a593Smuzhiyun
2245*4882a593Smuzhiyun /******************************************************************/
2246*4882a593Smuzhiyun
2247*4882a593Smuzhiyun static int
snd_azf3328_free(struct snd_azf3328 * chip)2248*4882a593Smuzhiyun snd_azf3328_free(struct snd_azf3328 *chip)
2249*4882a593Smuzhiyun {
2250*4882a593Smuzhiyun if (chip->irq < 0)
2251*4882a593Smuzhiyun goto __end_hw;
2252*4882a593Smuzhiyun
2253*4882a593Smuzhiyun snd_azf3328_mixer_reset(chip);
2254*4882a593Smuzhiyun
2255*4882a593Smuzhiyun snd_azf3328_timer_stop(chip->timer);
2256*4882a593Smuzhiyun snd_azf3328_gameport_free(chip);
2257*4882a593Smuzhiyun
2258*4882a593Smuzhiyun __end_hw:
2259*4882a593Smuzhiyun if (chip->irq >= 0)
2260*4882a593Smuzhiyun free_irq(chip->irq, chip);
2261*4882a593Smuzhiyun pci_release_regions(chip->pci);
2262*4882a593Smuzhiyun pci_disable_device(chip->pci);
2263*4882a593Smuzhiyun
2264*4882a593Smuzhiyun kfree(chip);
2265*4882a593Smuzhiyun return 0;
2266*4882a593Smuzhiyun }
2267*4882a593Smuzhiyun
2268*4882a593Smuzhiyun static int
snd_azf3328_dev_free(struct snd_device * device)2269*4882a593Smuzhiyun snd_azf3328_dev_free(struct snd_device *device)
2270*4882a593Smuzhiyun {
2271*4882a593Smuzhiyun struct snd_azf3328 *chip = device->device_data;
2272*4882a593Smuzhiyun return snd_azf3328_free(chip);
2273*4882a593Smuzhiyun }
2274*4882a593Smuzhiyun
2275*4882a593Smuzhiyun #if 0
2276*4882a593Smuzhiyun /* check whether a bit can be modified */
2277*4882a593Smuzhiyun static void
2278*4882a593Smuzhiyun snd_azf3328_test_bit(unsigned unsigned reg, int bit)
2279*4882a593Smuzhiyun {
2280*4882a593Smuzhiyun unsigned char val, valoff, valon;
2281*4882a593Smuzhiyun
2282*4882a593Smuzhiyun val = inb(reg);
2283*4882a593Smuzhiyun
2284*4882a593Smuzhiyun outb(val & ~(1 << bit), reg);
2285*4882a593Smuzhiyun valoff = inb(reg);
2286*4882a593Smuzhiyun
2287*4882a593Smuzhiyun outb(val|(1 << bit), reg);
2288*4882a593Smuzhiyun valon = inb(reg);
2289*4882a593Smuzhiyun
2290*4882a593Smuzhiyun outb(val, reg);
2291*4882a593Smuzhiyun
2292*4882a593Smuzhiyun printk(KERN_DEBUG "reg %04x bit %d: %02x %02x %02x\n",
2293*4882a593Smuzhiyun reg, bit, val, valoff, valon
2294*4882a593Smuzhiyun );
2295*4882a593Smuzhiyun }
2296*4882a593Smuzhiyun #endif
2297*4882a593Smuzhiyun
2298*4882a593Smuzhiyun static inline void
snd_azf3328_debug_show_ports(const struct snd_azf3328 * chip)2299*4882a593Smuzhiyun snd_azf3328_debug_show_ports(const struct snd_azf3328 *chip)
2300*4882a593Smuzhiyun {
2301*4882a593Smuzhiyun u16 tmp;
2302*4882a593Smuzhiyun
2303*4882a593Smuzhiyun dev_dbg(chip->card->dev,
2304*4882a593Smuzhiyun "ctrl_io 0x%lx, game_io 0x%lx, mpu_io 0x%lx, "
2305*4882a593Smuzhiyun "opl3_io 0x%lx, mixer_io 0x%lx, irq %d\n",
2306*4882a593Smuzhiyun chip->ctrl_io, chip->game_io, chip->mpu_io,
2307*4882a593Smuzhiyun chip->opl3_io, chip->mixer_io, chip->irq);
2308*4882a593Smuzhiyun
2309*4882a593Smuzhiyun dev_dbg(chip->card->dev,
2310*4882a593Smuzhiyun "game %02x %02x %02x %02x %02x %02x\n",
2311*4882a593Smuzhiyun snd_azf3328_game_inb(chip, 0),
2312*4882a593Smuzhiyun snd_azf3328_game_inb(chip, 1),
2313*4882a593Smuzhiyun snd_azf3328_game_inb(chip, 2),
2314*4882a593Smuzhiyun snd_azf3328_game_inb(chip, 3),
2315*4882a593Smuzhiyun snd_azf3328_game_inb(chip, 4),
2316*4882a593Smuzhiyun snd_azf3328_game_inb(chip, 5));
2317*4882a593Smuzhiyun
2318*4882a593Smuzhiyun for (tmp = 0; tmp < 0x07; tmp += 1)
2319*4882a593Smuzhiyun dev_dbg(chip->card->dev,
2320*4882a593Smuzhiyun "mpu_io 0x%04x\n", inb(chip->mpu_io + tmp));
2321*4882a593Smuzhiyun
2322*4882a593Smuzhiyun for (tmp = 0; tmp <= 0x07; tmp += 1)
2323*4882a593Smuzhiyun dev_dbg(chip->card->dev,
2324*4882a593Smuzhiyun "0x%02x: game200 0x%04x, game208 0x%04x\n",
2325*4882a593Smuzhiyun tmp, inb(0x200 + tmp), inb(0x208 + tmp));
2326*4882a593Smuzhiyun
2327*4882a593Smuzhiyun for (tmp = 0; tmp <= 0x01; tmp += 1)
2328*4882a593Smuzhiyun dev_dbg(chip->card->dev,
2329*4882a593Smuzhiyun "0x%02x: mpu300 0x%04x, mpu310 0x%04x, mpu320 0x%04x, "
2330*4882a593Smuzhiyun "mpu330 0x%04x opl388 0x%04x opl38c 0x%04x\n",
2331*4882a593Smuzhiyun tmp,
2332*4882a593Smuzhiyun inb(0x300 + tmp),
2333*4882a593Smuzhiyun inb(0x310 + tmp),
2334*4882a593Smuzhiyun inb(0x320 + tmp),
2335*4882a593Smuzhiyun inb(0x330 + tmp),
2336*4882a593Smuzhiyun inb(0x388 + tmp),
2337*4882a593Smuzhiyun inb(0x38c + tmp));
2338*4882a593Smuzhiyun
2339*4882a593Smuzhiyun for (tmp = 0; tmp < AZF_IO_SIZE_CTRL; tmp += 2)
2340*4882a593Smuzhiyun dev_dbg(chip->card->dev,
2341*4882a593Smuzhiyun "ctrl 0x%02x: 0x%04x\n",
2342*4882a593Smuzhiyun tmp, snd_azf3328_ctrl_inw(chip, tmp));
2343*4882a593Smuzhiyun
2344*4882a593Smuzhiyun for (tmp = 0; tmp < AZF_IO_SIZE_MIXER; tmp += 2)
2345*4882a593Smuzhiyun dev_dbg(chip->card->dev,
2346*4882a593Smuzhiyun "mixer 0x%02x: 0x%04x\n",
2347*4882a593Smuzhiyun tmp, snd_azf3328_mixer_inw(chip, tmp));
2348*4882a593Smuzhiyun }
2349*4882a593Smuzhiyun
2350*4882a593Smuzhiyun static int
snd_azf3328_create(struct snd_card * card,struct pci_dev * pci,unsigned long device_type,struct snd_azf3328 ** rchip)2351*4882a593Smuzhiyun snd_azf3328_create(struct snd_card *card,
2352*4882a593Smuzhiyun struct pci_dev *pci,
2353*4882a593Smuzhiyun unsigned long device_type,
2354*4882a593Smuzhiyun struct snd_azf3328 **rchip)
2355*4882a593Smuzhiyun {
2356*4882a593Smuzhiyun struct snd_azf3328 *chip;
2357*4882a593Smuzhiyun int err;
2358*4882a593Smuzhiyun static const struct snd_device_ops ops = {
2359*4882a593Smuzhiyun .dev_free = snd_azf3328_dev_free,
2360*4882a593Smuzhiyun };
2361*4882a593Smuzhiyun u8 dma_init;
2362*4882a593Smuzhiyun enum snd_azf3328_codec_type codec_type;
2363*4882a593Smuzhiyun struct snd_azf3328_codec_data *codec_setup;
2364*4882a593Smuzhiyun
2365*4882a593Smuzhiyun *rchip = NULL;
2366*4882a593Smuzhiyun
2367*4882a593Smuzhiyun err = pci_enable_device(pci);
2368*4882a593Smuzhiyun if (err < 0)
2369*4882a593Smuzhiyun return err;
2370*4882a593Smuzhiyun
2371*4882a593Smuzhiyun chip = kzalloc(sizeof(*chip), GFP_KERNEL);
2372*4882a593Smuzhiyun if (chip == NULL) {
2373*4882a593Smuzhiyun err = -ENOMEM;
2374*4882a593Smuzhiyun goto out_err;
2375*4882a593Smuzhiyun }
2376*4882a593Smuzhiyun spin_lock_init(&chip->reg_lock);
2377*4882a593Smuzhiyun chip->card = card;
2378*4882a593Smuzhiyun chip->pci = pci;
2379*4882a593Smuzhiyun chip->irq = -1;
2380*4882a593Smuzhiyun
2381*4882a593Smuzhiyun /* check if we can restrict PCI DMA transfers to 24 bits */
2382*4882a593Smuzhiyun if (dma_set_mask(&pci->dev, DMA_BIT_MASK(24)) < 0 ||
2383*4882a593Smuzhiyun dma_set_coherent_mask(&pci->dev, DMA_BIT_MASK(24)) < 0) {
2384*4882a593Smuzhiyun dev_err(card->dev,
2385*4882a593Smuzhiyun "architecture does not support 24bit PCI busmaster DMA\n"
2386*4882a593Smuzhiyun );
2387*4882a593Smuzhiyun err = -ENXIO;
2388*4882a593Smuzhiyun goto out_err;
2389*4882a593Smuzhiyun }
2390*4882a593Smuzhiyun
2391*4882a593Smuzhiyun err = pci_request_regions(pci, "Aztech AZF3328");
2392*4882a593Smuzhiyun if (err < 0)
2393*4882a593Smuzhiyun goto out_err;
2394*4882a593Smuzhiyun
2395*4882a593Smuzhiyun chip->ctrl_io = pci_resource_start(pci, 0);
2396*4882a593Smuzhiyun chip->game_io = pci_resource_start(pci, 1);
2397*4882a593Smuzhiyun chip->mpu_io = pci_resource_start(pci, 2);
2398*4882a593Smuzhiyun chip->opl3_io = pci_resource_start(pci, 3);
2399*4882a593Smuzhiyun chip->mixer_io = pci_resource_start(pci, 4);
2400*4882a593Smuzhiyun
2401*4882a593Smuzhiyun codec_setup = &chip->codecs[AZF_CODEC_PLAYBACK];
2402*4882a593Smuzhiyun codec_setup->io_base = chip->ctrl_io + AZF_IO_OFFS_CODEC_PLAYBACK;
2403*4882a593Smuzhiyun codec_setup->lock = &chip->reg_lock;
2404*4882a593Smuzhiyun codec_setup->type = AZF_CODEC_PLAYBACK;
2405*4882a593Smuzhiyun codec_setup->name = "PLAYBACK";
2406*4882a593Smuzhiyun
2407*4882a593Smuzhiyun codec_setup = &chip->codecs[AZF_CODEC_CAPTURE];
2408*4882a593Smuzhiyun codec_setup->io_base = chip->ctrl_io + AZF_IO_OFFS_CODEC_CAPTURE;
2409*4882a593Smuzhiyun codec_setup->lock = &chip->reg_lock;
2410*4882a593Smuzhiyun codec_setup->type = AZF_CODEC_CAPTURE;
2411*4882a593Smuzhiyun codec_setup->name = "CAPTURE";
2412*4882a593Smuzhiyun
2413*4882a593Smuzhiyun codec_setup = &chip->codecs[AZF_CODEC_I2S_OUT];
2414*4882a593Smuzhiyun codec_setup->io_base = chip->ctrl_io + AZF_IO_OFFS_CODEC_I2S_OUT;
2415*4882a593Smuzhiyun codec_setup->lock = &chip->reg_lock;
2416*4882a593Smuzhiyun codec_setup->type = AZF_CODEC_I2S_OUT;
2417*4882a593Smuzhiyun codec_setup->name = "I2S_OUT";
2418*4882a593Smuzhiyun
2419*4882a593Smuzhiyun if (request_irq(pci->irq, snd_azf3328_interrupt,
2420*4882a593Smuzhiyun IRQF_SHARED, KBUILD_MODNAME, chip)) {
2421*4882a593Smuzhiyun dev_err(card->dev, "unable to grab IRQ %d\n", pci->irq);
2422*4882a593Smuzhiyun err = -EBUSY;
2423*4882a593Smuzhiyun goto out_err;
2424*4882a593Smuzhiyun }
2425*4882a593Smuzhiyun chip->irq = pci->irq;
2426*4882a593Smuzhiyun card->sync_irq = chip->irq;
2427*4882a593Smuzhiyun pci_set_master(pci);
2428*4882a593Smuzhiyun
2429*4882a593Smuzhiyun snd_azf3328_debug_show_ports(chip);
2430*4882a593Smuzhiyun
2431*4882a593Smuzhiyun err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
2432*4882a593Smuzhiyun if (err < 0)
2433*4882a593Smuzhiyun goto out_err;
2434*4882a593Smuzhiyun
2435*4882a593Smuzhiyun /* create mixer interface & switches */
2436*4882a593Smuzhiyun err = snd_azf3328_mixer_new(chip);
2437*4882a593Smuzhiyun if (err < 0)
2438*4882a593Smuzhiyun goto out_err;
2439*4882a593Smuzhiyun
2440*4882a593Smuzhiyun /* standard codec init stuff */
2441*4882a593Smuzhiyun /* default DMA init value */
2442*4882a593Smuzhiyun dma_init = DMA_RUN_SOMETHING2|DMA_EPILOGUE_SOMETHING|DMA_SOMETHING_ELSE;
2443*4882a593Smuzhiyun
2444*4882a593Smuzhiyun for (codec_type = AZF_CODEC_PLAYBACK;
2445*4882a593Smuzhiyun codec_type <= AZF_CODEC_I2S_OUT; ++codec_type) {
2446*4882a593Smuzhiyun struct snd_azf3328_codec_data *codec =
2447*4882a593Smuzhiyun &chip->codecs[codec_type];
2448*4882a593Smuzhiyun
2449*4882a593Smuzhiyun /* shutdown codecs to reduce power / noise */
2450*4882a593Smuzhiyun /* have ...ctrl_codec_activity() act properly */
2451*4882a593Smuzhiyun codec->running = 1;
2452*4882a593Smuzhiyun snd_azf3328_ctrl_codec_activity(chip, codec_type, 0);
2453*4882a593Smuzhiyun
2454*4882a593Smuzhiyun spin_lock_irq(codec->lock);
2455*4882a593Smuzhiyun snd_azf3328_codec_outb(codec, IDX_IO_CODEC_DMA_FLAGS,
2456*4882a593Smuzhiyun dma_init);
2457*4882a593Smuzhiyun spin_unlock_irq(codec->lock);
2458*4882a593Smuzhiyun }
2459*4882a593Smuzhiyun
2460*4882a593Smuzhiyun *rchip = chip;
2461*4882a593Smuzhiyun
2462*4882a593Smuzhiyun err = 0;
2463*4882a593Smuzhiyun goto out;
2464*4882a593Smuzhiyun
2465*4882a593Smuzhiyun out_err:
2466*4882a593Smuzhiyun if (chip)
2467*4882a593Smuzhiyun snd_azf3328_free(chip);
2468*4882a593Smuzhiyun pci_disable_device(pci);
2469*4882a593Smuzhiyun
2470*4882a593Smuzhiyun out:
2471*4882a593Smuzhiyun return err;
2472*4882a593Smuzhiyun }
2473*4882a593Smuzhiyun
2474*4882a593Smuzhiyun static int
snd_azf3328_probe(struct pci_dev * pci,const struct pci_device_id * pci_id)2475*4882a593Smuzhiyun snd_azf3328_probe(struct pci_dev *pci, const struct pci_device_id *pci_id)
2476*4882a593Smuzhiyun {
2477*4882a593Smuzhiyun static int dev;
2478*4882a593Smuzhiyun struct snd_card *card;
2479*4882a593Smuzhiyun struct snd_azf3328 *chip;
2480*4882a593Smuzhiyun struct snd_opl3 *opl3;
2481*4882a593Smuzhiyun int err;
2482*4882a593Smuzhiyun
2483*4882a593Smuzhiyun if (dev >= SNDRV_CARDS) {
2484*4882a593Smuzhiyun err = -ENODEV;
2485*4882a593Smuzhiyun goto out;
2486*4882a593Smuzhiyun }
2487*4882a593Smuzhiyun if (!enable[dev]) {
2488*4882a593Smuzhiyun dev++;
2489*4882a593Smuzhiyun err = -ENOENT;
2490*4882a593Smuzhiyun goto out;
2491*4882a593Smuzhiyun }
2492*4882a593Smuzhiyun
2493*4882a593Smuzhiyun err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
2494*4882a593Smuzhiyun 0, &card);
2495*4882a593Smuzhiyun if (err < 0)
2496*4882a593Smuzhiyun goto out;
2497*4882a593Smuzhiyun
2498*4882a593Smuzhiyun strcpy(card->driver, "AZF3328");
2499*4882a593Smuzhiyun strcpy(card->shortname, "Aztech AZF3328 (PCI168)");
2500*4882a593Smuzhiyun
2501*4882a593Smuzhiyun err = snd_azf3328_create(card, pci, pci_id->driver_data, &chip);
2502*4882a593Smuzhiyun if (err < 0)
2503*4882a593Smuzhiyun goto out_err;
2504*4882a593Smuzhiyun
2505*4882a593Smuzhiyun card->private_data = chip;
2506*4882a593Smuzhiyun
2507*4882a593Smuzhiyun /* chose to use MPU401_HW_AZT2320 ID instead of MPU401_HW_MPU401,
2508*4882a593Smuzhiyun since our hardware ought to be similar, thus use same ID. */
2509*4882a593Smuzhiyun err = snd_mpu401_uart_new(
2510*4882a593Smuzhiyun card, 0,
2511*4882a593Smuzhiyun MPU401_HW_AZT2320, chip->mpu_io,
2512*4882a593Smuzhiyun MPU401_INFO_INTEGRATED | MPU401_INFO_IRQ_HOOK,
2513*4882a593Smuzhiyun -1, &chip->rmidi
2514*4882a593Smuzhiyun );
2515*4882a593Smuzhiyun if (err < 0) {
2516*4882a593Smuzhiyun dev_err(card->dev, "no MPU-401 device at 0x%lx?\n",
2517*4882a593Smuzhiyun chip->mpu_io
2518*4882a593Smuzhiyun );
2519*4882a593Smuzhiyun goto out_err;
2520*4882a593Smuzhiyun }
2521*4882a593Smuzhiyun
2522*4882a593Smuzhiyun err = snd_azf3328_timer(chip, 0);
2523*4882a593Smuzhiyun if (err < 0)
2524*4882a593Smuzhiyun goto out_err;
2525*4882a593Smuzhiyun
2526*4882a593Smuzhiyun err = snd_azf3328_pcm(chip);
2527*4882a593Smuzhiyun if (err < 0)
2528*4882a593Smuzhiyun goto out_err;
2529*4882a593Smuzhiyun
2530*4882a593Smuzhiyun if (snd_opl3_create(card, chip->opl3_io, chip->opl3_io+2,
2531*4882a593Smuzhiyun OPL3_HW_AUTO, 1, &opl3) < 0) {
2532*4882a593Smuzhiyun dev_err(card->dev, "no OPL3 device at 0x%lx-0x%lx?\n",
2533*4882a593Smuzhiyun chip->opl3_io, chip->opl3_io+2
2534*4882a593Smuzhiyun );
2535*4882a593Smuzhiyun } else {
2536*4882a593Smuzhiyun /* need to use IDs 1, 2 since ID 0 is snd_azf3328_timer above */
2537*4882a593Smuzhiyun err = snd_opl3_timer_new(opl3, 1, 2);
2538*4882a593Smuzhiyun if (err < 0)
2539*4882a593Smuzhiyun goto out_err;
2540*4882a593Smuzhiyun err = snd_opl3_hwdep_new(opl3, 0, 1, NULL);
2541*4882a593Smuzhiyun if (err < 0)
2542*4882a593Smuzhiyun goto out_err;
2543*4882a593Smuzhiyun opl3->private_data = chip;
2544*4882a593Smuzhiyun }
2545*4882a593Smuzhiyun
2546*4882a593Smuzhiyun sprintf(card->longname, "%s at 0x%lx, irq %i",
2547*4882a593Smuzhiyun card->shortname, chip->ctrl_io, chip->irq);
2548*4882a593Smuzhiyun
2549*4882a593Smuzhiyun err = snd_card_register(card);
2550*4882a593Smuzhiyun if (err < 0)
2551*4882a593Smuzhiyun goto out_err;
2552*4882a593Smuzhiyun
2553*4882a593Smuzhiyun #ifdef MODULE
2554*4882a593Smuzhiyun dev_info(card->dev,
2555*4882a593Smuzhiyun "Sound driver for Aztech AZF3328-based soundcards such as PCI168.\n");
2556*4882a593Smuzhiyun dev_info(card->dev,
2557*4882a593Smuzhiyun "Hardware was completely undocumented, unfortunately.\n");
2558*4882a593Smuzhiyun dev_info(card->dev,
2559*4882a593Smuzhiyun "Feel free to contact andi AT lisas.de for bug reports etc.!\n");
2560*4882a593Smuzhiyun dev_info(card->dev,
2561*4882a593Smuzhiyun "User-scalable sequencer timer set to %dHz (1024000Hz / %d).\n",
2562*4882a593Smuzhiyun 1024000 / seqtimer_scaling, seqtimer_scaling);
2563*4882a593Smuzhiyun #endif
2564*4882a593Smuzhiyun
2565*4882a593Smuzhiyun snd_azf3328_gameport(chip, dev);
2566*4882a593Smuzhiyun
2567*4882a593Smuzhiyun pci_set_drvdata(pci, card);
2568*4882a593Smuzhiyun dev++;
2569*4882a593Smuzhiyun
2570*4882a593Smuzhiyun err = 0;
2571*4882a593Smuzhiyun goto out;
2572*4882a593Smuzhiyun
2573*4882a593Smuzhiyun out_err:
2574*4882a593Smuzhiyun dev_err(card->dev, "something failed, exiting\n");
2575*4882a593Smuzhiyun snd_card_free(card);
2576*4882a593Smuzhiyun
2577*4882a593Smuzhiyun out:
2578*4882a593Smuzhiyun return err;
2579*4882a593Smuzhiyun }
2580*4882a593Smuzhiyun
2581*4882a593Smuzhiyun static void
snd_azf3328_remove(struct pci_dev * pci)2582*4882a593Smuzhiyun snd_azf3328_remove(struct pci_dev *pci)
2583*4882a593Smuzhiyun {
2584*4882a593Smuzhiyun snd_card_free(pci_get_drvdata(pci));
2585*4882a593Smuzhiyun }
2586*4882a593Smuzhiyun
2587*4882a593Smuzhiyun #ifdef CONFIG_PM_SLEEP
2588*4882a593Smuzhiyun static inline void
snd_azf3328_suspend_regs(const struct snd_azf3328 * chip,unsigned long io_addr,unsigned count,u32 * saved_regs)2589*4882a593Smuzhiyun snd_azf3328_suspend_regs(const struct snd_azf3328 *chip,
2590*4882a593Smuzhiyun unsigned long io_addr, unsigned count, u32 *saved_regs)
2591*4882a593Smuzhiyun {
2592*4882a593Smuzhiyun unsigned reg;
2593*4882a593Smuzhiyun
2594*4882a593Smuzhiyun for (reg = 0; reg < count; ++reg) {
2595*4882a593Smuzhiyun *saved_regs = inl(io_addr);
2596*4882a593Smuzhiyun dev_dbg(chip->card->dev, "suspend: io 0x%04lx: 0x%08x\n",
2597*4882a593Smuzhiyun io_addr, *saved_regs);
2598*4882a593Smuzhiyun ++saved_regs;
2599*4882a593Smuzhiyun io_addr += sizeof(*saved_regs);
2600*4882a593Smuzhiyun }
2601*4882a593Smuzhiyun }
2602*4882a593Smuzhiyun
2603*4882a593Smuzhiyun static inline void
snd_azf3328_resume_regs(const struct snd_azf3328 * chip,const u32 * saved_regs,unsigned long io_addr,unsigned count)2604*4882a593Smuzhiyun snd_azf3328_resume_regs(const struct snd_azf3328 *chip,
2605*4882a593Smuzhiyun const u32 *saved_regs,
2606*4882a593Smuzhiyun unsigned long io_addr,
2607*4882a593Smuzhiyun unsigned count
2608*4882a593Smuzhiyun )
2609*4882a593Smuzhiyun {
2610*4882a593Smuzhiyun unsigned reg;
2611*4882a593Smuzhiyun
2612*4882a593Smuzhiyun for (reg = 0; reg < count; ++reg) {
2613*4882a593Smuzhiyun outl(*saved_regs, io_addr);
2614*4882a593Smuzhiyun dev_dbg(chip->card->dev,
2615*4882a593Smuzhiyun "resume: io 0x%04lx: 0x%08x --> 0x%08x\n",
2616*4882a593Smuzhiyun io_addr, *saved_regs, inl(io_addr));
2617*4882a593Smuzhiyun ++saved_regs;
2618*4882a593Smuzhiyun io_addr += sizeof(*saved_regs);
2619*4882a593Smuzhiyun }
2620*4882a593Smuzhiyun }
2621*4882a593Smuzhiyun
2622*4882a593Smuzhiyun static inline void
snd_azf3328_suspend_ac97(struct snd_azf3328 * chip)2623*4882a593Smuzhiyun snd_azf3328_suspend_ac97(struct snd_azf3328 *chip)
2624*4882a593Smuzhiyun {
2625*4882a593Smuzhiyun #ifdef AZF_USE_AC97_LAYER
2626*4882a593Smuzhiyun snd_ac97_suspend(chip->ac97);
2627*4882a593Smuzhiyun #else
2628*4882a593Smuzhiyun snd_azf3328_suspend_regs(chip, chip->mixer_io,
2629*4882a593Smuzhiyun ARRAY_SIZE(chip->saved_regs_mixer), chip->saved_regs_mixer);
2630*4882a593Smuzhiyun
2631*4882a593Smuzhiyun /* make sure to disable master volume etc. to prevent looping sound */
2632*4882a593Smuzhiyun snd_azf3328_mixer_mute_control_master(chip, 1);
2633*4882a593Smuzhiyun snd_azf3328_mixer_mute_control_pcm(chip, 1);
2634*4882a593Smuzhiyun #endif /* AZF_USE_AC97_LAYER */
2635*4882a593Smuzhiyun }
2636*4882a593Smuzhiyun
2637*4882a593Smuzhiyun static inline void
snd_azf3328_resume_ac97(const struct snd_azf3328 * chip)2638*4882a593Smuzhiyun snd_azf3328_resume_ac97(const struct snd_azf3328 *chip)
2639*4882a593Smuzhiyun {
2640*4882a593Smuzhiyun #ifdef AZF_USE_AC97_LAYER
2641*4882a593Smuzhiyun snd_ac97_resume(chip->ac97);
2642*4882a593Smuzhiyun #else
2643*4882a593Smuzhiyun snd_azf3328_resume_regs(chip, chip->saved_regs_mixer, chip->mixer_io,
2644*4882a593Smuzhiyun ARRAY_SIZE(chip->saved_regs_mixer));
2645*4882a593Smuzhiyun
2646*4882a593Smuzhiyun /* unfortunately with 32bit transfers, IDX_MIXER_PLAY_MASTER (0x02)
2647*4882a593Smuzhiyun and IDX_MIXER_RESET (offset 0x00) get touched at the same time,
2648*4882a593Smuzhiyun resulting in a mixer reset condition persisting until _after_
2649*4882a593Smuzhiyun master vol was restored. Thus master vol needs an extra restore. */
2650*4882a593Smuzhiyun outw(((u16 *)chip->saved_regs_mixer)[1], chip->mixer_io + 2);
2651*4882a593Smuzhiyun #endif /* AZF_USE_AC97_LAYER */
2652*4882a593Smuzhiyun }
2653*4882a593Smuzhiyun
2654*4882a593Smuzhiyun static int
snd_azf3328_suspend(struct device * dev)2655*4882a593Smuzhiyun snd_azf3328_suspend(struct device *dev)
2656*4882a593Smuzhiyun {
2657*4882a593Smuzhiyun struct snd_card *card = dev_get_drvdata(dev);
2658*4882a593Smuzhiyun struct snd_azf3328 *chip = card->private_data;
2659*4882a593Smuzhiyun u16 *saved_regs_ctrl_u16;
2660*4882a593Smuzhiyun
2661*4882a593Smuzhiyun snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
2662*4882a593Smuzhiyun
2663*4882a593Smuzhiyun snd_azf3328_suspend_ac97(chip);
2664*4882a593Smuzhiyun
2665*4882a593Smuzhiyun snd_azf3328_suspend_regs(chip, chip->ctrl_io,
2666*4882a593Smuzhiyun ARRAY_SIZE(chip->saved_regs_ctrl), chip->saved_regs_ctrl);
2667*4882a593Smuzhiyun
2668*4882a593Smuzhiyun /* manually store the one currently relevant write-only reg, too */
2669*4882a593Smuzhiyun saved_regs_ctrl_u16 = (u16 *)chip->saved_regs_ctrl;
2670*4882a593Smuzhiyun saved_regs_ctrl_u16[IDX_IO_6AH / 2] = chip->shadow_reg_ctrl_6AH;
2671*4882a593Smuzhiyun
2672*4882a593Smuzhiyun snd_azf3328_suspend_regs(chip, chip->game_io,
2673*4882a593Smuzhiyun ARRAY_SIZE(chip->saved_regs_game), chip->saved_regs_game);
2674*4882a593Smuzhiyun snd_azf3328_suspend_regs(chip, chip->mpu_io,
2675*4882a593Smuzhiyun ARRAY_SIZE(chip->saved_regs_mpu), chip->saved_regs_mpu);
2676*4882a593Smuzhiyun snd_azf3328_suspend_regs(chip, chip->opl3_io,
2677*4882a593Smuzhiyun ARRAY_SIZE(chip->saved_regs_opl3), chip->saved_regs_opl3);
2678*4882a593Smuzhiyun return 0;
2679*4882a593Smuzhiyun }
2680*4882a593Smuzhiyun
2681*4882a593Smuzhiyun static int
snd_azf3328_resume(struct device * dev)2682*4882a593Smuzhiyun snd_azf3328_resume(struct device *dev)
2683*4882a593Smuzhiyun {
2684*4882a593Smuzhiyun struct snd_card *card = dev_get_drvdata(dev);
2685*4882a593Smuzhiyun const struct snd_azf3328 *chip = card->private_data;
2686*4882a593Smuzhiyun
2687*4882a593Smuzhiyun snd_azf3328_resume_regs(chip, chip->saved_regs_game, chip->game_io,
2688*4882a593Smuzhiyun ARRAY_SIZE(chip->saved_regs_game));
2689*4882a593Smuzhiyun snd_azf3328_resume_regs(chip, chip->saved_regs_mpu, chip->mpu_io,
2690*4882a593Smuzhiyun ARRAY_SIZE(chip->saved_regs_mpu));
2691*4882a593Smuzhiyun snd_azf3328_resume_regs(chip, chip->saved_regs_opl3, chip->opl3_io,
2692*4882a593Smuzhiyun ARRAY_SIZE(chip->saved_regs_opl3));
2693*4882a593Smuzhiyun
2694*4882a593Smuzhiyun snd_azf3328_resume_ac97(chip);
2695*4882a593Smuzhiyun
2696*4882a593Smuzhiyun snd_azf3328_resume_regs(chip, chip->saved_regs_ctrl, chip->ctrl_io,
2697*4882a593Smuzhiyun ARRAY_SIZE(chip->saved_regs_ctrl));
2698*4882a593Smuzhiyun
2699*4882a593Smuzhiyun snd_power_change_state(card, SNDRV_CTL_POWER_D0);
2700*4882a593Smuzhiyun return 0;
2701*4882a593Smuzhiyun }
2702*4882a593Smuzhiyun
2703*4882a593Smuzhiyun static SIMPLE_DEV_PM_OPS(snd_azf3328_pm, snd_azf3328_suspend, snd_azf3328_resume);
2704*4882a593Smuzhiyun #define SND_AZF3328_PM_OPS &snd_azf3328_pm
2705*4882a593Smuzhiyun #else
2706*4882a593Smuzhiyun #define SND_AZF3328_PM_OPS NULL
2707*4882a593Smuzhiyun #endif /* CONFIG_PM_SLEEP */
2708*4882a593Smuzhiyun
2709*4882a593Smuzhiyun static struct pci_driver azf3328_driver = {
2710*4882a593Smuzhiyun .name = KBUILD_MODNAME,
2711*4882a593Smuzhiyun .id_table = snd_azf3328_ids,
2712*4882a593Smuzhiyun .probe = snd_azf3328_probe,
2713*4882a593Smuzhiyun .remove = snd_azf3328_remove,
2714*4882a593Smuzhiyun .driver = {
2715*4882a593Smuzhiyun .pm = SND_AZF3328_PM_OPS,
2716*4882a593Smuzhiyun },
2717*4882a593Smuzhiyun };
2718*4882a593Smuzhiyun
2719*4882a593Smuzhiyun module_pci_driver(azf3328_driver);
2720