Blame view

sound/pci/azt3328.c 80.9 KB
778ddf544   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-or-later
b5dc20cd2   Andreas Mohr   ALSA: azt3328: ad...
2
3
  /*  azt3328.c - driver for Aztech AZF3328 based soundcards (e.g. PCI168).
   *  Copyright (C) 2002, 2005 - 2011 by Andreas Mohr <andi AT lisas.de>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4
5
6
7
8
9
10
11
12
   *
   *  Framework borrowed from Bart Hartgers's als4000.c.
   *  Driver developed on PCI168 AP(W) version (PCI rev. 10, subsystem ID 1801),
   *  found in a Fujitsu-Siemens PC ("Cordant", aluminum case).
   *  Other versions are:
   *  PCI168 A(W), sub ID 1800
   *  PCI168 A/AP, sub ID 8000
   *  Please give me feedback in case you try my driver with one of these!!
   *
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
13
14
15
16
17
   *  Keywords: Windows XP Vista 168nt4-125.zip 168win95-125.zip PCI 168 download
   *  (XP/Vista do not support this card at all but every Linux distribution
   *   has very good support out of the box;
   *   just to make sure that the right people hit this and get to know that,
   *   despite the high level of Internet ignorance - as usual :-P -
78df617ac   Andreas Mohr   ALSA: azt3328: fi...
18
   *   about very good support for this card - on Linux!)
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
19
   *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
20
21
22
23
24
25
26
27
   * NOTES
   *  Since Aztech does not provide any chipset documentation,
   *  even on repeated request to various addresses,
   *  and the answer that was finally given was negative
   *  (and I was stupid enough to manage to get hold of a PCI168 soundcard
   *  in the first place >:-P}),
   *  I was forced to base this driver on reverse engineering
   *  (3 weeks' worth of evenings filled with driver work).
e2f872608   Andreas Mohr   [ALSA] azt3328.c:...
28
   *  (and no, I did NOT go the easy way: to pick up a SB PCI128 for 9 Euros)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
29
   *
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
30
31
32
   *  It is quite likely that the AZF3328 chip is the PCI cousin of the
   *  AZF3318 ("azt1020 pnp", "MM Pro 16") ISA chip, given very similar specs.
   *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
33
   *  The AZF3328 chip (note: AZF3328, *not* AZT3328, that's just the driver name
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
34
35
36
   *  for compatibility reasons) from Azfin (joint-venture of Aztech and Fincitec,
   *  Fincitec acquired by National Semiconductor in 2002, together with the
   *  Fincitec-related company ARSmikro) has the following features:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
37
   *
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
38
39
40
41
42
43
   *  - compatibility & compliance:
   *    - Microsoft PC 97 ("PC 97 Hardware Design Guide",
   *                       http://www.microsoft.com/whdc/archive/pcguides.mspx)
   *    - Microsoft PC 98 Baseline Audio
   *    - MPU401 UART
   *    - Sound Blaster Emulation (DOS Box)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
44
   *  - builtin AC97 conformant codec (SNR over 80dB)
13769e3f2   Andreas Mohr   [ALSA] azt3328.c:...
45
46
47
48
49
50
51
52
53
   *    Note that "conformant" != "compliant"!! this chip's mixer register layout
   *    *differs* from the standard AC97 layout:
   *    they chose to not implement the headphone register (which is not a
   *    problem since it's merely optional), yet when doing this, they committed
   *    the grave sin of letting other registers follow immediately instead of
   *    keeping a headphone dummy register, thereby shifting the mixer register
   *    addresses illegally. So far unfortunately it looks like the very flexible
   *    ALSA AC97 support is still not enough to easily compensate for such a
   *    grave layout violation despite all tweaks and quirks mechanisms it offers.
b5dc20cd2   Andreas Mohr   ALSA: azt3328: ad...
54
55
56
57
58
59
60
   *    Well, not quite: now ac97 layer is much improved (bus-specific ops!),
   *    thus I was able to implement support - it's actually working quite well.
   *    An interesting item might be Aztech AMR 2800-W, since it's an AC97
   *    modem card which might reveal the Aztech-specific codec ID which
   *    we might want to pretend, too. Dito PCI168's brother, PCI368,
   *    where the advertising datasheet says it's AC97-based and has a
   *    Digital Enhanced Game Port.
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
61
   *  - builtin genuine OPL3 - verified to work fine, 20080506
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
62
   *  - full duplex 16bit playback/record at independent sampling rate
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
63
64
   *  - MPU401 (+ legacy address support, claimed by one official spec sheet)
   *    FIXME: how to enable legacy addr??
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
65
   *  - game port (legacy address support)
e24a121aa   Andreas Mohr   [ALSA] azt3328.c:...
66
   *  - builtin DirectInput support, helps reduce CPU overhead (interrupt-driven
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
67
68
69
   *    features supported). - See common term "Digital Enhanced Game Port"...
   *    (probably DirectInput 3.0 spec - confirm)
   *  - builtin 3D enhancement (said to be YAMAHA Ymersion)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
70
   *  - built-in General DirectX timer having a 20 bits counter
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
71
   *    with 1us resolution (see below!)
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
72
   *  - I2S serial output port for external DAC
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
73
   *    [FIXME: 3.3V or 5V level? maximum rate is 66.2kHz right?]
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
74
75
76
   *  - supports 33MHz PCI spec 2.1, PCI power management 1.0, compliant with ACPI
   *  - supports hardware volume control
   *  - single chip low cost solution (128 pin QFP)
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
77
   *  - supports programmable Sub-vendor and Sub-system ID [24C02 SEEPROM chip]
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
78
   *    required for Microsoft's logo compliance (FIXME: where?)
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
79
80
81
82
83
   *    At least the Trident 4D Wave DX has one bit somewhere
   *    to enable writes to PCI subsystem VID registers, that should be it.
   *    This might easily be in extended PCI reg space, since PCI168 also has
   *    some custom data starting at 0x80. What kind of config settings
   *    are located in our extended PCI space anyway??
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
84
   *  - PCI168 AP(W) card: power amplifier with 4 Watts/channel at 4 Ohms
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
85
   *    [TDA1517P chip]
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
86
   *
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
87
88
89
90
91
92
93
94
95
   *  Note that this driver now is actually *better* than the Windows driver,
   *  since it additionally supports the card's 1MHz DirectX timer - just try
   *  the following snd-seq module parameters etc.:
   *  - options snd-seq seq_default_timer_class=2 seq_default_timer_sclass=0
   *    seq_default_timer_card=0 seq_client_load=1 seq_default_timer_device=0
   *    seq_default_timer_subdevice=0 seq_default_timer_resolution=1000000
   *  - "timidity -iAv -B2,8 -Os -EFreverb=0"
   *  - "pmidi -p 128:0 jazz.mid"
   *
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
   *  OPL3 hardware playback testing, try something like:
   *  cat /proc/asound/hwdep
   *  and
   *  aconnect -o
   *  Then use
   *  sbiload -Dhw:x,y --opl3 /usr/share/sounds/opl3/std.o3 ......./drums.o3
   *  where x,y is the xx-yy number as given in hwdep.
   *  Then try
   *  pmidi -p a:b jazz.mid
   *  where a:b is the client number plus 0 usually, as given by aconnect above.
   *  Oh, and make sure to unmute the FM mixer control (doh!)
   *  NOTE: power use during OPL3 playback is _VERY_ high (70W --> 90W!)
   *  despite no CPU activity, possibly due to hindering ACPI idling somehow.
   *  Shouldn't be a problem of the AZF3328 chip itself, I'd hope.
   *  Higher PCM / FM mixer levels seem to conflict (causes crackling),
   *  at least sometimes.   Maybe even use with hardware sequencer timer above :)
   *  adplay/adplug-utils might soon offer hardware-based OPL3 playback, too.
   *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
114
115
116
117
118
119
   *  Certain PCI versions of this card are susceptible to DMA traffic underruns
   *  in some systems (resulting in sound crackling/clicking/popping),
   *  probably because they don't have a DMA FIFO buffer or so.
   *  Overview (PCI ID/PCI subID/PCI rev.):
   *  - no DMA crackling on SiS735: 0x50DC/0x1801/16
   *  - unknown performance: 0x50DC/0x1801/10
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
120
121
   *    (well, it's not bad on an Athlon 1800 with now very optimized IRQ handler)
   *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
122
123
124
125
126
   *  Crackling happens with VIA chipsets or, in my case, an SiS735, which is
   *  supposed to be very fast and supposed to get rid of crackling much
   *  better than a VIA, yet ironically I still get crackling, like many other
   *  people with the same chipset.
   *  Possible remedies:
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
127
128
   *  - use speaker (amplifier) output instead of headphone output
   *    (in case crackling is due to overloaded output clipping)
25985edce   Lucas De Marchi   Fix common misspe...
129
   *  - plug card into a different PCI slot, preferably one that isn't shared
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
130
131
132
133
134
135
   *    too much (this helps a lot, but not completely!)
   *  - get rid of PCI VGA card, use AGP instead
   *  - upgrade or downgrade BIOS
   *  - fiddle with PCI latency settings (setpci -v -s BUSID latency_timer=XX)
   *    Not too helpful.
   *  - Disable ACPI/power management/"Auto Detect RAM/PCI Clk" in BIOS
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
136
   *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
137
   * BUGS
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
138
   *  - full-duplex might *still* be problematic, however a recent test was fine
e24a121aa   Andreas Mohr   [ALSA] azt3328.c:...
139
140
141
142
   *  - (non-bug) "Bass/Treble or 3D settings don't work" - they do get evaluated
   *    if you set PCM output switch to "pre 3D" instead of "post 3D".
   *    If this can't be set, then get a mixer application that Isn't Stupid (tm)
   *    (e.g. kmix, gamix) - unfortunately several are!!
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
143
144
145
146
147
148
   *  - locking is not entirely clean, especially the audio stream activity
   *    ints --> may be racy
   *  - an _unconnected_ secondary joystick at the gameport will be reported
   *    to be "active" (floating values, not precisely -1) due to the way we need
   *    to read the Digital Enhanced Game Port. Not sure whether it is fixable.
   *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
149
   * TODO
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
150
151
152
153
   *  - use PCI_VDEVICE
   *  - verify driver status on x86_64
   *  - test multi-card driver operation
   *  - (ab)use 1MHz DirectX timer as kernel clocksource
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
154
   *  - test MPU401 MIDI playback etc.
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
155
   *  - add more power micro-management (disable various units of the card
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
156
157
158
   *    as long as they're unused, to improve audio quality and save power).
   *    However this requires more I/O ports which I haven't figured out yet
   *    and which thus might not even exist...
ca54bde36   Andreas Mohr   [ALSA] azt3328.c:...
159
160
   *    The standard suspend/resume functionality could probably make use of
   *    some improvement, too...
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
161
   *  - figure out what all unknown port bits are responsible for
13769e3f2   Andreas Mohr   [ALSA] azt3328.c:...
162
163
164
   *  - figure out some cleverly evil scheme to possibly make ALSA AC97 code
   *    fully accept our quite incompatible ""AC97"" mixer and thus save some
   *    code (but I'm not too optimistic that doing this is possible at all)
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
165
   *  - use MMIO (memory-mapped I/O)? Slightly faster access, e.g. for gameport.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
166
   */
6cbbfe1c8   Takashi Iwai   ALSA: Include lin...
167
  #include <linux/io.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
168
  #include <linux/init.h>
689c69120   Andreas Mohr   ALSA: azt3328: im...
169
  #include <linux/bug.h> /* WARN_ONCE */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
170
171
172
173
  #include <linux/pci.h>
  #include <linux/delay.h>
  #include <linux/slab.h>
  #include <linux/gameport.h>
65a772172   Paul Gortmaker   sound: fix driver...
174
  #include <linux/module.h>
910638ae7   Matthias Gehre   [PATCH] Replace 0...
175
  #include <linux/dma-mapping.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
176
177
178
179
180
181
182
  #include <sound/core.h>
  #include <sound/control.h>
  #include <sound/pcm.h>
  #include <sound/rawmidi.h>
  #include <sound/mpu401.h>
  #include <sound/opl3.h>
  #include <sound/initval.h>
b5dc20cd2   Andreas Mohr   ALSA: azt3328: ad...
183
184
185
186
187
188
189
190
191
192
  /*
   * Config switch, to use ALSA's AC97 layer instead of old custom mixer crap.
   * If the AC97 compatibility parts we needed to implement locally turn out
   * to work nicely, then remove the old implementation eventually.
   */
  #define AZF_USE_AC97_LAYER 1
  
  #ifdef AZF_USE_AC97_LAYER
  #include <sound/ac97_codec.h>
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
193
  #include "azt3328.h"
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
194
  MODULE_AUTHOR("Andreas Mohr <andi AT lisas.de>");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
195
196
197
  MODULE_DESCRIPTION("Aztech AZF3328 (PCI168)");
  MODULE_LICENSE("GPL");
  MODULE_SUPPORTED_DEVICE("{{Aztech,AZF3328}}");
b2fac0730   Fabian Frederick   ALSA: pci: don't ...
198
  #if IS_REACHABLE(CONFIG_GAMEPORT)
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
199
  #define SUPPORT_GAMEPORT 1
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
200
  #endif
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
201
202
  /* === Debug settings ===
    Further diagnostic functionality than the settings below
adf5931f8   Andreas Mohr   ALSA: azt3328: co...
203
    does not need to be provided, since one can easily write a POSIX shell script
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
204
    to dump the card's I/O ports (those listed in lspci -v -v):
adf5931f8   Andreas Mohr   ALSA: azt3328: co...
205
    dump()
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
206
207
208
209
    {
      local descr=$1; local addr=$2; local count=$3
  
      echo "${descr}: ${count} @ ${addr}:"
adf5931f8   Andreas Mohr   ALSA: azt3328: co...
210
211
      dd if=/dev/port skip=`printf %d ${addr}` count=${count} bs=1 \
        2>/dev/null| hexdump -C
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
212
213
214
215
216
217
218
    }
    and then use something like
    "dump joy200 0x200 8", "dump mpu388 0x388 4", "dump joy 0xb400 8",
    "dump codec00 0xa800 32", "dump mixer 0xb800 64", "dump synth 0xbc00 8",
    possibly within a "while true; do ... sleep 1; done" loop.
    Tweaking ports could be done using
    VALSTRING="`printf "%02x" $value`"
adf5931f8   Andreas Mohr   ALSA: azt3328: co...
219
220
    printf "\x""$VALSTRING"|dd of=/dev/port seek=`printf %d ${addr}` bs=1 \
      2>/dev/null
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
221
  */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
222
223
224
225
226
227
228
  static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	/* Index 0-MAX */
  module_param_array(index, int, NULL, 0444);
  MODULE_PARM_DESC(index, "Index value for AZF3328 soundcard.");
  
  static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* ID for this card */
  module_param_array(id, charp, NULL, 0444);
  MODULE_PARM_DESC(id, "ID string for AZF3328 soundcard.");
a67ff6a54   Rusty Russell   ALSA: module_para...
229
  static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;	/* Enable this card */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
230
231
  module_param_array(enable, bool, NULL, 0444);
  MODULE_PARM_DESC(enable, "Enable AZF3328 soundcard.");
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
232
233
234
  static int seqtimer_scaling = 128;
  module_param(seqtimer_scaling, int, 0444);
  MODULE_PARM_DESC(seqtimer_scaling, "Set 1024000Hz sequencer timer scale factor (lockup danger!). Default 128.");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
235

dfbf95111   Andreas Mohr   ALSA: azt3328: la...
236
  enum snd_azf3328_codec_type {
adf5931f8   Andreas Mohr   ALSA: azt3328: co...
237
    /* warning: fixed indices (also used for bitmask checks!) */
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
238
239
240
    AZF_CODEC_PLAYBACK = 0,
    AZF_CODEC_CAPTURE = 1,
    AZF_CODEC_I2S_OUT = 2,
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
241
  };
da237f35a   Andreas Mohr   ALSA: azt3328: us...
242
243
244
245
246
247
248
249
250
  struct snd_azf3328_codec_data {
  	unsigned long io_base; /* keep first! (avoid offset calc) */
  	unsigned int dma_base; /* helper to avoid an indirection in hotpath */
  	spinlock_t *lock; /* TODO: convert to our own per-codec lock member */
  	struct snd_pcm_substream *substream;
  	bool running;
  	enum snd_azf3328_codec_type type;
  	const char *name;
  };
95de77660   Takashi Iwai   [ALSA] Remove xxx...
251
  struct snd_azf3328 {
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
252
  	/* often-used fields towards beginning, then grouped */
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
253

dfbf95111   Andreas Mohr   ALSA: azt3328: la...
254
  	unsigned long ctrl_io; /* usually 0xb000, size 128 */
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
255
256
257
258
  	unsigned long game_io;  /* usually 0xb400, size 8 */
  	unsigned long mpu_io;   /* usually 0xb800, size 4 */
  	unsigned long opl3_io; /* usually 0xbc00, size 8 */
  	unsigned long mixer_io; /* usually 0xc000, size 64 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
259

d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
260
  	spinlock_t reg_lock;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
261

95de77660   Takashi Iwai   [ALSA] Remove xxx...
262
  	struct snd_timer *timer;
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
263

dfbf95111   Andreas Mohr   ALSA: azt3328: la...
264
265
266
267
  	struct snd_pcm *pcm[3];
  
  	/* playback, recording and I2S out codecs */
  	struct snd_azf3328_codec_data codecs[3];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
268

b5dc20cd2   Andreas Mohr   ALSA: azt3328: ad...
269
270
271
  #ifdef AZF_USE_AC97_LAYER
  	struct snd_ac97 *ac97;
  #endif
95de77660   Takashi Iwai   [ALSA] Remove xxx...
272
273
  	struct snd_card *card;
  	struct snd_rawmidi *rmidi;
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
274

02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
275
  #ifdef SUPPORT_GAMEPORT
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
276
  	struct gameport *gameport;
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
277
  	u16 axes[4];
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
278
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
279

d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
280
281
  	struct pci_dev *pci;
  	int irq;
ca54bde36   Andreas Mohr   [ALSA] azt3328.c:...
282

627d3e7ab   Andreas Mohr   ALSA: PCI168 snd-...
283
284
285
286
  	/* register 0x6a is write-only, thus need to remember setting.
  	 * If we need to add more registers here, then we might try to fold this
  	 * into some transparent combined shadow register handling with
  	 * CONFIG_PM register storage below, but that's slightly difficult. */
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
287
  	u16 shadow_reg_ctrl_6AH;
627d3e7ab   Andreas Mohr   ALSA: PCI168 snd-...
288

c7561cd80   Takashi Iwai   ALSA: PCI: Replac...
289
  #ifdef CONFIG_PM_SLEEP
ca54bde36   Andreas Mohr   [ALSA] azt3328.c:...
290
  	/* register value containers for power management
78df617ac   Andreas Mohr   ALSA: azt3328: fi...
291
292
293
294
295
296
  	 * Note: not always full I/O range preserved (similar to Win driver!) */
  	u32 saved_regs_ctrl[AZF_ALIGN(AZF_IO_SIZE_CTRL_PM) / 4];
  	u32 saved_regs_game[AZF_ALIGN(AZF_IO_SIZE_GAME_PM) / 4];
  	u32 saved_regs_mpu[AZF_ALIGN(AZF_IO_SIZE_MPU_PM) / 4];
  	u32 saved_regs_opl3[AZF_ALIGN(AZF_IO_SIZE_OPL3_PM) / 4];
  	u32 saved_regs_mixer[AZF_ALIGN(AZF_IO_SIZE_MIXER_PM) / 4];
ca54bde36   Andreas Mohr   [ALSA] azt3328.c:...
297
  #endif
95de77660   Takashi Iwai   [ALSA] Remove xxx...
298
  };
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
299

9baa3c34a   Benoit Taine   PCI: Remove DEFIN...
300
  static const struct pci_device_id snd_azf3328_ids[] = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
301
302
303
304
305
306
  	{ 0x122D, 0x50DC, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },   /* PCI168/3328 */
  	{ 0x122D, 0x80DA, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },   /* 3328 */
  	{ 0, }
  };
  
  MODULE_DEVICE_TABLE(pci, snd_azf3328_ids);
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
307
308
  
  static int
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
309
  snd_azf3328_io_reg_setb(unsigned reg, u8 mask, bool do_set)
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
310
  {
adf5931f8   Andreas Mohr   ALSA: azt3328: co...
311
312
313
  	/* Well, strictly spoken, the inb/outb sequence isn't atomic
  	   and would need locking. However we currently don't care
  	   since it potentially complicates matters. */
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
314
315
316
317
318
319
320
321
322
323
324
  	u8 prev = inb(reg), new;
  
  	new = (do_set) ? (prev|mask) : (prev & ~mask);
  	/* we need to always write the new value no matter whether it differs
  	 * or not, since some register bits don't indicate their setting */
  	outb(new, reg);
  	if (new != prev)
  		return 1;
  
  	return 0;
  }
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
325
  static inline void
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
326
327
328
329
  snd_azf3328_codec_outb(const struct snd_azf3328_codec_data *codec,
  		       unsigned reg,
  		       u8 value
  )
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
330
  {
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
331
  	outb(value, codec->io_base + reg);
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
332
333
334
  }
  
  static inline u8
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
335
  snd_azf3328_codec_inb(const struct snd_azf3328_codec_data *codec, unsigned reg)
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
336
  {
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
337
  	return inb(codec->io_base + reg);
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
338
339
340
  }
  
  static inline void
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
341
342
343
344
  snd_azf3328_codec_outw(const struct snd_azf3328_codec_data *codec,
  		       unsigned reg,
  		       u16 value
  )
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
345
  {
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
346
  	outw(value, codec->io_base + reg);
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
347
348
349
  }
  
  static inline u16
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
350
  snd_azf3328_codec_inw(const struct snd_azf3328_codec_data *codec, unsigned reg)
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
351
  {
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
352
  	return inw(codec->io_base + reg);
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
353
354
355
  }
  
  static inline void
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
356
357
358
359
  snd_azf3328_codec_outl(const struct snd_azf3328_codec_data *codec,
  		       unsigned reg,
  		       u32 value
  )
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
360
  {
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
361
  	outl(value, codec->io_base + reg);
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
362
  }
689c69120   Andreas Mohr   ALSA: azt3328: im...
363
364
365
366
367
368
369
370
371
372
373
374
375
376
  static inline void
  snd_azf3328_codec_outl_multi(const struct snd_azf3328_codec_data *codec,
  			     unsigned reg, const void *buffer, int count
  )
  {
  	unsigned long addr = codec->io_base + reg;
  	if (count) {
  		const u32 *buf = buffer;
  		do {
  			outl(*buf++, addr);
  			addr += 4;
  		} while (--count);
  	}
  }
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
377
  static inline u32
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
378
379
380
381
382
383
384
  snd_azf3328_codec_inl(const struct snd_azf3328_codec_data *codec, unsigned reg)
  {
  	return inl(codec->io_base + reg);
  }
  
  static inline void
  snd_azf3328_ctrl_outb(const struct snd_azf3328 *chip, unsigned reg, u8 value)
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
385
  {
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
386
387
388
389
390
391
392
393
  	outb(value, chip->ctrl_io + reg);
  }
  
  static inline u8
  snd_azf3328_ctrl_inb(const struct snd_azf3328 *chip, unsigned reg)
  {
  	return inb(chip->ctrl_io + reg);
  }
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
394
395
396
397
398
  static inline u16
  snd_azf3328_ctrl_inw(const struct snd_azf3328 *chip, unsigned reg)
  {
  	return inw(chip->ctrl_io + reg);
  }
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
399
400
401
402
403
404
405
406
407
408
  static inline void
  snd_azf3328_ctrl_outw(const struct snd_azf3328 *chip, unsigned reg, u16 value)
  {
  	outw(value, chip->ctrl_io + reg);
  }
  
  static inline void
  snd_azf3328_ctrl_outl(const struct snd_azf3328 *chip, unsigned reg, u32 value)
  {
  	outl(value, chip->ctrl_io + reg);
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
409
410
411
  }
  
  static inline void
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
412
  snd_azf3328_game_outb(const struct snd_azf3328 *chip, unsigned reg, u8 value)
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
413
  {
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
414
  	outb(value, chip->game_io + reg);
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
415
416
417
  }
  
  static inline void
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
418
  snd_azf3328_game_outw(const struct snd_azf3328 *chip, unsigned reg, u16 value)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
419
  {
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
420
  	outw(value, chip->game_io + reg);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
421
  }
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
422
  static inline u8
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
423
424
425
426
427
428
429
  snd_azf3328_game_inb(const struct snd_azf3328 *chip, unsigned reg)
  {
  	return inb(chip->game_io + reg);
  }
  
  static inline u16
  snd_azf3328_game_inw(const struct snd_azf3328 *chip, unsigned reg)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
430
  {
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
431
  	return inw(chip->game_io + reg);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
432
  }
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
433
  static inline void
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
434
  snd_azf3328_mixer_outw(const struct snd_azf3328 *chip, unsigned reg, u16 value)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
435
  {
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
436
  	outw(value, chip->mixer_io + reg);
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
437
438
439
  }
  
  static inline u16
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
440
  snd_azf3328_mixer_inw(const struct snd_azf3328 *chip, unsigned reg)
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
441
  {
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
442
  	return inw(chip->mixer_io + reg);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
443
  }
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
444
  #define AZF_MUTE_BIT 0x80
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
445
  static bool
b5dc20cd2   Andreas Mohr   ALSA: azt3328: ad...
446
  snd_azf3328_mixer_mute_control(const struct snd_azf3328 *chip,
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
447
  			   unsigned reg, bool do_mute
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
448
  )
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
449
  {
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
450
  	unsigned long portbase = chip->mixer_io + reg + 1;
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
451
  	bool updated;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
452
453
454
  
  	/* the mute bit is on the *second* (i.e. right) register of a
  	 * left/right channel setting */
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
455
456
457
458
  	updated = snd_azf3328_io_reg_setb(portbase, AZF_MUTE_BIT, do_mute);
  
  	/* indicate whether it was muted before */
  	return (do_mute) ? !updated : updated;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
459
  }
b5dc20cd2   Andreas Mohr   ALSA: azt3328: ad...
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
  static inline bool
  snd_azf3328_mixer_mute_control_master(const struct snd_azf3328 *chip,
  			   bool do_mute
  )
  {
  	return snd_azf3328_mixer_mute_control(
  		chip,
  		IDX_MIXER_PLAY_MASTER,
  		do_mute
  	);
  }
  
  static inline bool
  snd_azf3328_mixer_mute_control_pcm(const struct snd_azf3328 *chip,
  			   bool do_mute
  )
  {
  	return snd_azf3328_mixer_mute_control(
  		chip,
  		IDX_MIXER_WAVEOUT,
  		do_mute
  	);
  }
  
  static inline void
  snd_azf3328_mixer_reset(const struct snd_azf3328 *chip)
  {
  	/* reset (close) mixer:
  	 * first mute master volume, then reset
  	 */
  	snd_azf3328_mixer_mute_control_master(chip, 1);
  	snd_azf3328_mixer_outw(chip, IDX_MIXER_RESET, 0x0000);
  }
  
  #ifdef AZF_USE_AC97_LAYER
  
  static inline void
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
497
498
  snd_azf3328_mixer_ac97_map_unsupported(const struct snd_azf3328 *chip,
  				       unsigned short reg, const char *mode)
b5dc20cd2   Andreas Mohr   ALSA: azt3328: ad...
499
500
  {
  	/* need to add some more or less clever emulation? */
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
501
502
503
  	dev_warn(chip->card->dev,
  		"missing %s emulation for AC97 register 0x%02x!
  ",
b5dc20cd2   Andreas Mohr   ALSA: azt3328: ad...
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
  		mode, reg);
  }
  
  /*
   * Need to have _special_ AC97 mixer hardware register index mapper,
   * to compensate for the issue of a rather AC97-incompatible hardware layout.
   */
  #define AZF_REG_MASK 0x3f
  #define AZF_AC97_REG_UNSUPPORTED 0x8000
  #define AZF_AC97_REG_REAL_IO_READ 0x4000
  #define AZF_AC97_REG_REAL_IO_WRITE 0x2000
  #define AZF_AC97_REG_REAL_IO_RW \
  	(AZF_AC97_REG_REAL_IO_READ | AZF_AC97_REG_REAL_IO_WRITE)
  #define AZF_AC97_REG_EMU_IO_READ 0x0400
  #define AZF_AC97_REG_EMU_IO_WRITE 0x0200
  #define AZF_AC97_REG_EMU_IO_RW \
  	(AZF_AC97_REG_EMU_IO_READ | AZF_AC97_REG_EMU_IO_WRITE)
  static unsigned short
  snd_azf3328_mixer_ac97_map_reg_idx(unsigned short reg)
  {
  	static const struct {
  		unsigned short azf_reg;
  	} azf_reg_mapper[] = {
  		/* Especially when taking into consideration
  		 * mono/stereo-based sequence of azf vs. AC97 control series,
  		 * it's quite obvious that azf simply got rid
  		 * of the AC97_HEADPHONE control at its intended offset,
  		 * thus shifted _all_ controls by one,
  		 * and _then_ simply added it as an FMSYNTH control at the end,
  		 * to make up for the offset.
  		 * This means we'll have to translate indices here as
  		 * needed and then do some tiny AC97 patch action
  		 * (snd_ac97_rename_vol_ctl() etc.) - that's it.
  		 */
  		{ /* AC97_RESET */ IDX_MIXER_RESET
  			| AZF_AC97_REG_REAL_IO_WRITE
  			| AZF_AC97_REG_EMU_IO_READ },
  		{ /* AC97_MASTER */ IDX_MIXER_PLAY_MASTER },
  		 /* note large shift: AC97_HEADPHONE to IDX_MIXER_FMSYNTH! */
  		{ /* AC97_HEADPHONE */ IDX_MIXER_FMSYNTH },
  		{ /* AC97_MASTER_MONO */ IDX_MIXER_MODEMOUT },
  		{ /* AC97_MASTER_TONE */ IDX_MIXER_BASSTREBLE },
  		{ /* AC97_PC_BEEP */ IDX_MIXER_PCBEEP },
  		{ /* AC97_PHONE */ IDX_MIXER_MODEMIN },
  		{ /* AC97_MIC */ IDX_MIXER_MIC },
  		{ /* AC97_LINE */ IDX_MIXER_LINEIN },
  		{ /* AC97_CD */ IDX_MIXER_CDAUDIO },
  		{ /* AC97_VIDEO */ IDX_MIXER_VIDEO },
  		{ /* AC97_AUX */ IDX_MIXER_AUX },
  		{ /* AC97_PCM */ IDX_MIXER_WAVEOUT },
  		{ /* AC97_REC_SEL */ IDX_MIXER_REC_SELECT },
  		{ /* AC97_REC_GAIN */ IDX_MIXER_REC_VOLUME },
  		{ /* AC97_REC_GAIN_MIC */ AZF_AC97_REG_EMU_IO_RW },
  		{ /* AC97_GENERAL_PURPOSE */ IDX_MIXER_ADVCTL2 },
  		{ /* AC97_3D_CONTROL */ IDX_MIXER_ADVCTL1 },
  	};
  
  	unsigned short reg_azf = AZF_AC97_REG_UNSUPPORTED;
  
  	/* azf3328 supports the low-numbered and low-spec:ed range
  	   of AC97 regs only */
  	if (reg <= AC97_3D_CONTROL) {
  		unsigned short reg_idx = reg / 2;
  		reg_azf = azf_reg_mapper[reg_idx].azf_reg;
  		/* a translation-only entry means it's real read/write: */
  		if (!(reg_azf & ~AZF_REG_MASK))
  			reg_azf |= AZF_AC97_REG_REAL_IO_RW;
  	} else {
  		switch (reg) {
  		case AC97_POWERDOWN:
  			reg_azf = AZF_AC97_REG_EMU_IO_RW;
  			break;
  		case AC97_EXTENDED_ID:
  			reg_azf = AZF_AC97_REG_EMU_IO_READ;
  			break;
  		case AC97_EXTENDED_STATUS:
  			/* I don't know what the h*ll AC97 layer
  			 * would consult this _extended_ register for
  			 * given a base-AC97-advertised card,
  			 * but let's just emulate it anyway :-P
  			 */
  			reg_azf = AZF_AC97_REG_EMU_IO_RW;
  			break;
  		case AC97_VENDOR_ID1:
  		case AC97_VENDOR_ID2:
  			reg_azf = AZF_AC97_REG_EMU_IO_READ;
  			break;
  		}
  	}
  	return reg_azf;
  }
  
  static const unsigned short
  azf_emulated_ac97_caps =
  	AC97_BC_DEDICATED_MIC |
  	AC97_BC_BASS_TREBLE |
  	/* Headphone is an FM Synth control here */
  	AC97_BC_HEADPHONE |
  	/* no AC97_BC_LOUDNESS! */
  	/* mask 0x7c00 is
  	   vendor-specific 3D enhancement
  	   vendor indicator.
  	   Since there actually _is_ an
  	   entry for Aztech Labs
  	   (13), make damn sure
  	   to indicate it. */
  	(13 << 10);
  
  static const unsigned short
  azf_emulated_ac97_powerdown =
  	/* pretend everything to be active */
  		AC97_PD_ADC_STATUS |
  		AC97_PD_DAC_STATUS |
  		AC97_PD_MIXER_STATUS |
  		AC97_PD_VREF_STATUS;
  
  /*
   * Emulated, _inofficial_ vendor ID
   * (there might be some devices such as the MR 2800-W
   * which could reveal the real Aztech AC97 ID).
   * We choose to use "AZT" prefix, and then use 1 to indicate PCI168
   * (better don't use 0x68 since there's a PCI368 as well).
   */
  static const unsigned int
  azf_emulated_ac97_vendor_id = 0x415a5401;
  
  static unsigned short
  snd_azf3328_mixer_ac97_read(struct snd_ac97 *ac97, unsigned short reg_ac97)
  {
  	const struct snd_azf3328 *chip = ac97->private_data;
  	unsigned short reg_azf = snd_azf3328_mixer_ac97_map_reg_idx(reg_ac97);
  	unsigned short reg_val = 0;
e0f17c75d   Peter Senna Tschudin   ALSA: Fix assignm...
636
  	bool unsupported = false;
b5dc20cd2   Andreas Mohr   ALSA: azt3328: ad...
637

4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
638
639
640
  	dev_dbg(chip->card->dev, "snd_azf3328_mixer_ac97_read reg_ac97 %u
  ",
  		reg_ac97);
b5dc20cd2   Andreas Mohr   ALSA: azt3328: ad...
641
  	if (reg_azf & AZF_AC97_REG_UNSUPPORTED)
e0f17c75d   Peter Senna Tschudin   ALSA: Fix assignm...
642
  		unsupported = true;
b5dc20cd2   Andreas Mohr   ALSA: azt3328: ad...
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
  	else {
  		if (reg_azf & AZF_AC97_REG_REAL_IO_READ)
  			reg_val = snd_azf3328_mixer_inw(chip,
  						reg_azf & AZF_REG_MASK);
  		else {
  			/*
  			 * Proceed with dummy I/O read,
  			 * to ensure compatible timing where this may matter.
  			 * (ALSA AC97 layer usually doesn't call I/O functions
  			 * due to intelligent I/O caching anyway)
  			 * Choose a mixer register that's thoroughly unrelated
  			 * to common audio (try to minimize distortion).
  			 */
  			snd_azf3328_mixer_inw(chip, IDX_MIXER_SOMETHING30H);
  		}
  
  		if (reg_azf & AZF_AC97_REG_EMU_IO_READ) {
  			switch (reg_ac97) {
  			case AC97_RESET:
  				reg_val |= azf_emulated_ac97_caps;
  				break;
  			case AC97_POWERDOWN:
  				reg_val |= azf_emulated_ac97_powerdown;
  				break;
  			case AC97_EXTENDED_ID:
  			case AC97_EXTENDED_STATUS:
  				/* AFAICS we simply can't support anything: */
  				reg_val |= 0;
  				break;
  			case AC97_VENDOR_ID1:
  				reg_val = azf_emulated_ac97_vendor_id >> 16;
  				break;
  			case AC97_VENDOR_ID2:
  				reg_val = azf_emulated_ac97_vendor_id & 0xffff;
  				break;
  			default:
e0f17c75d   Peter Senna Tschudin   ALSA: Fix assignm...
679
  				unsupported = true;
b5dc20cd2   Andreas Mohr   ALSA: azt3328: ad...
680
681
682
683
684
  				break;
  			}
  		}
  	}
  	if (unsupported)
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
685
  		snd_azf3328_mixer_ac97_map_unsupported(chip, reg_ac97, "read");
b5dc20cd2   Andreas Mohr   ALSA: azt3328: ad...
686
687
688
689
690
691
692
693
694
695
  
  	return reg_val;
  }
  
  static void
  snd_azf3328_mixer_ac97_write(struct snd_ac97 *ac97,
  		     unsigned short reg_ac97, unsigned short val)
  {
  	const struct snd_azf3328 *chip = ac97->private_data;
  	unsigned short reg_azf = snd_azf3328_mixer_ac97_map_reg_idx(reg_ac97);
e0f17c75d   Peter Senna Tschudin   ALSA: Fix assignm...
696
  	bool unsupported = false;
b5dc20cd2   Andreas Mohr   ALSA: azt3328: ad...
697

4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
698
  	dev_dbg(chip->card->dev,
b5dc20cd2   Andreas Mohr   ALSA: azt3328: ad...
699
700
  		"snd_azf3328_mixer_ac97_write reg_ac97 %u val %u
  ",
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
701
  		reg_ac97, val);
b5dc20cd2   Andreas Mohr   ALSA: azt3328: ad...
702
  	if (reg_azf & AZF_AC97_REG_UNSUPPORTED)
e0f17c75d   Peter Senna Tschudin   ALSA: Fix assignm...
703
  		unsupported = true;
b5dc20cd2   Andreas Mohr   ALSA: azt3328: ad...
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
  	else {
  		if (reg_azf & AZF_AC97_REG_REAL_IO_WRITE)
  			snd_azf3328_mixer_outw(
  				chip,
  				reg_azf & AZF_REG_MASK,
  				val
  			);
  		else
  		if (reg_azf & AZF_AC97_REG_EMU_IO_WRITE) {
  			switch (reg_ac97) {
  			case AC97_REC_GAIN_MIC:
  			case AC97_POWERDOWN:
  			case AC97_EXTENDED_STATUS:
  				/*
  				 * Silently swallow these writes.
  				 * Since for most registers our card doesn't
  				 * actually support a comparable feature,
  				 * this is exactly what we should do here.
  				 * The AC97 layer's I/O caching probably
  				 * automatically takes care of all the rest...
  				 * (remembers written values etc.)
  				 */
  				break;
  			default:
e0f17c75d   Peter Senna Tschudin   ALSA: Fix assignm...
728
  				unsupported = true;
b5dc20cd2   Andreas Mohr   ALSA: azt3328: ad...
729
730
731
732
733
  				break;
  			}
  		}
  	}
  	if (unsupported)
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
734
  		snd_azf3328_mixer_ac97_map_unsupported(chip, reg_ac97, "write");
b5dc20cd2   Andreas Mohr   ALSA: azt3328: ad...
735
  }
e23e7a143   Bill Pemberton   ALSA: pci: remove...
736
  static int
b5dc20cd2   Andreas Mohr   ALSA: azt3328: ad...
737
738
739
740
  snd_azf3328_mixer_new(struct snd_azf3328 *chip)
  {
  	struct snd_ac97_bus *bus;
  	struct snd_ac97_template ac97;
51055da51   Takashi Iwai   ALSA: pci: Consti...
741
  	static const struct snd_ac97_bus_ops ops = {
b5dc20cd2   Andreas Mohr   ALSA: azt3328: ad...
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
  		.write = snd_azf3328_mixer_ac97_write,
  		.read = snd_azf3328_mixer_ac97_read,
  	};
  	int rc;
  
  	memset(&ac97, 0, sizeof(ac97));
  	ac97.scaps = AC97_SCAP_SKIP_MODEM
  			| AC97_SCAP_AUDIO /* we support audio! */
  			| AC97_SCAP_NO_SPDIF;
  	ac97.private_data = chip;
  	ac97.pci = chip->pci;
  
  	/*
  	 * ALSA's AC97 layer has terrible init crackling issues,
  	 * unfortunately, and since it makes use of AC97_RESET,
  	 * there's no use trying to mute Master Playback proactively.
  	 */
  
  	rc = snd_ac97_bus(chip->card, 0, &ops, NULL, &bus);
  	if (!rc)
  		rc = snd_ac97_mixer(bus, &ac97, &chip->ac97);
  		/*
  		 * Make sure to complain loudly in case of AC97 init failure,
  		 * since failure may happen quite often,
  		 * due to this card being a very quirky AC97 "lookalike".
  		 */
  	if (rc)
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
769
770
  		dev_err(chip->card->dev, "AC97 init failed, err %d!
  ", rc);
b5dc20cd2   Andreas Mohr   ALSA: azt3328: ad...
771
772
773
774
775
776
777
  
  	/* If we return an error here, then snd_card_free() should
  	 * free up any ac97 codecs that got created, as well as the bus.
  	 */
  	return rc;
  }
  #else /* AZF_USE_AC97_LAYER */
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
778
  static void
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
779
780
781
782
783
784
  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
  )
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
785
  {
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
786
  	unsigned long portbase = chip->mixer_io + reg;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
787
  	unsigned char curr_vol_left = 0, curr_vol_right = 0;
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
788
  	int left_change = 0, right_change = 0;
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
789
  	if (chan_sel & SET_CHAN_LEFT) {
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
790
  		curr_vol_left  = inb(portbase + 1);
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
791
792
793
794
795
796
797
798
799
800
801
  
  		/* take care of muting flag contained in left channel */
  		if (curr_vol_left & AZF_MUTE_BIT)
  			dst_vol_left |= AZF_MUTE_BIT;
  		else
  			dst_vol_left &= ~AZF_MUTE_BIT;
  
  		left_change = (curr_vol_left > dst_vol_left) ? -1 : 1;
  	}
  
  	if (chan_sel & SET_CHAN_RIGHT) {
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
802
  		curr_vol_right = inb(portbase + 0);
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
803
804
805
  
  		right_change = (curr_vol_right > dst_vol_right) ? -1 : 1;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
806

e2f872608   Andreas Mohr   [ALSA] azt3328.c:...
807
  	do {
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
808
809
810
811
812
813
  		if (left_change) {
  			if (curr_vol_left != dst_vol_left) {
  				curr_vol_left += left_change;
  				outb(curr_vol_left, portbase + 1);
  			} else
  			    left_change = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
814
  		}
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
815
816
817
  		if (right_change) {
  			if (curr_vol_right != dst_vol_right) {
  				curr_vol_right += right_change;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
818
819
820
  			/* during volume change, the right channel is crackling
  			 * somewhat more than the left channel, unfortunately.
  			 * This seems to be a hardware issue. */
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
821
822
823
  				outb(curr_vol_right, portbase + 0);
  			} else
  			    right_change = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
824
825
826
  		}
  		if (delay)
  			mdelay(delay);
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
827
  	} while ((left_change) || (right_change));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
828
829
830
831
832
  }
  
  /*
   * general mixer element
   */
95de77660   Takashi Iwai   [ALSA] Remove xxx...
833
  struct azf3328_mixer_reg {
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
834
  	unsigned reg;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
835
836
837
838
839
  	unsigned int lchan_shift, rchan_shift;
  	unsigned int mask;
  	unsigned int invert: 1;
  	unsigned int stereo: 1;
  	unsigned int enum_c: 4;
95de77660   Takashi Iwai   [ALSA] Remove xxx...
840
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
841
842
  
  #define COMPOSE_MIXER_REG(reg,lchan_shift,rchan_shift,mask,invert,stereo,enum_c) \
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
843
844
845
846
847
   ((reg) | (lchan_shift << 8) | (rchan_shift << 12) | \
    (mask << 16) | \
    (invert << 24) | \
    (stereo << 25) | \
    (enum_c << 26))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
848

95de77660   Takashi Iwai   [ALSA] Remove xxx...
849
  static void snd_azf3328_mixer_reg_decode(struct azf3328_mixer_reg *r, unsigned long val)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
  {
  	r->reg = val & 0xff;
  	r->lchan_shift = (val >> 8) & 0x0f;
  	r->rchan_shift = (val >> 12) & 0x0f;
  	r->mask = (val >> 16) & 0xff;
  	r->invert = (val >> 24) & 1;
  	r->stereo = (val >> 25) & 1;
  	r->enum_c = (val >> 26) & 0x0f;
  }
  
  /*
   * mixer switches/volumes
   */
  
  #define AZF3328_MIXER_SWITCH(xname, reg, shift, invert) \
  { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
    .info = snd_azf3328_info_mixer, \
    .get = snd_azf3328_get_mixer, .put = snd_azf3328_put_mixer, \
    .private_value = COMPOSE_MIXER_REG(reg, shift, 0, 0x1, invert, 0, 0), \
  }
  
  #define AZF3328_MIXER_VOL_STEREO(xname, reg, mask, invert) \
  { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
    .info = snd_azf3328_info_mixer, \
    .get = snd_azf3328_get_mixer, .put = snd_azf3328_put_mixer, \
    .private_value = COMPOSE_MIXER_REG(reg, 8, 0, mask, invert, 1, 0), \
  }
  
  #define AZF3328_MIXER_VOL_MONO(xname, reg, mask, is_right_chan) \
  { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
    .info = snd_azf3328_info_mixer, \
    .get = snd_azf3328_get_mixer, .put = snd_azf3328_put_mixer, \
    .private_value = COMPOSE_MIXER_REG(reg, is_right_chan ? 0 : 8, 0, mask, 1, 0, 0), \
  }
  
  #define AZF3328_MIXER_VOL_SPECIAL(xname, reg, mask, shift, invert) \
  { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
    .info = snd_azf3328_info_mixer, \
    .get = snd_azf3328_get_mixer, .put = snd_azf3328_put_mixer, \
    .private_value = COMPOSE_MIXER_REG(reg, shift, 0, mask, invert, 0, 0), \
  }
  
  #define AZF3328_MIXER_ENUM(xname, reg, enum_c, shift) \
  { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
    .info = snd_azf3328_info_mixer_enum, \
    .get = snd_azf3328_get_mixer_enum, .put = snd_azf3328_put_mixer_enum, \
    .private_value = COMPOSE_MIXER_REG(reg, shift, 0, 0, 0, 0, enum_c), \
  }
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
898
  static int
95de77660   Takashi Iwai   [ALSA] Remove xxx...
899
900
  snd_azf3328_info_mixer(struct snd_kcontrol *kcontrol,
  		       struct snd_ctl_elem_info *uinfo)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
901
  {
95de77660   Takashi Iwai   [ALSA] Remove xxx...
902
  	struct azf3328_mixer_reg reg;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
903

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
904
  	snd_azf3328_mixer_reg_decode(&reg, kcontrol->private_value);
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
905
906
  	uinfo->type = reg.mask == 1 ?
  		SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
907
908
909
  	uinfo->count = reg.stereo + 1;
  	uinfo->value.integer.min = 0;
  	uinfo->value.integer.max = reg.mask;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
910
911
  	return 0;
  }
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
912
  static int
95de77660   Takashi Iwai   [ALSA] Remove xxx...
913
914
  snd_azf3328_get_mixer(struct snd_kcontrol *kcontrol,
  		      struct snd_ctl_elem_value *ucontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
915
  {
95de77660   Takashi Iwai   [ALSA] Remove xxx...
916
917
  	struct snd_azf3328 *chip = snd_kcontrol_chip(kcontrol);
  	struct azf3328_mixer_reg reg;
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
918
  	u16 oreg, val;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
919

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
920
  	snd_azf3328_mixer_reg_decode(&reg, kcontrol->private_value);
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
921
  	oreg = snd_azf3328_mixer_inw(chip, reg.reg);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
922
923
924
925
926
927
928
929
930
931
  	val = (oreg >> reg.lchan_shift) & reg.mask;
  	if (reg.invert)
  		val = reg.mask - val;
  	ucontrol->value.integer.value[0] = val;
  	if (reg.stereo) {
  		val = (oreg >> reg.rchan_shift) & reg.mask;
  		if (reg.invert)
  			val = reg.mask - val;
  		ucontrol->value.integer.value[1] = val;
  	}
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
932
933
934
  	dev_dbg(chip->card->dev,
  		"get: %02x is %04x -> vol %02lx|%02lx (shift %02d|%02d, mask %02x, inv. %d, stereo %d)
  ",
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
935
936
937
  		reg.reg, oreg,
  		ucontrol->value.integer.value[0], ucontrol->value.integer.value[1],
  		reg.lchan_shift, reg.rchan_shift, reg.mask, reg.invert, reg.stereo);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
938
939
  	return 0;
  }
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
940
  static int
95de77660   Takashi Iwai   [ALSA] Remove xxx...
941
942
  snd_azf3328_put_mixer(struct snd_kcontrol *kcontrol,
  		      struct snd_ctl_elem_value *ucontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
943
  {
95de77660   Takashi Iwai   [ALSA] Remove xxx...
944
945
  	struct snd_azf3328 *chip = snd_kcontrol_chip(kcontrol);
  	struct azf3328_mixer_reg reg;
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
946
  	u16 oreg, nreg, val;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
947

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
948
  	snd_azf3328_mixer_reg_decode(&reg, kcontrol->private_value);
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
949
  	oreg = snd_azf3328_mixer_inw(chip, reg.reg);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
950
951
952
953
954
955
956
957
958
959
960
961
962
  	val = ucontrol->value.integer.value[0] & reg.mask;
  	if (reg.invert)
  		val = reg.mask - val;
  	nreg = oreg & ~(reg.mask << reg.lchan_shift);
  	nreg |= (val << reg.lchan_shift);
  	if (reg.stereo) {
  		val = ucontrol->value.integer.value[1] & reg.mask;
  		if (reg.invert)
  			val = reg.mask - val;
  		nreg &= ~(reg.mask << reg.rchan_shift);
  		nreg |= (val << reg.rchan_shift);
  	}
  	if (reg.mask >= 0x07) /* it's a volume control, so better take care */
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
963
964
965
966
967
  		snd_azf3328_mixer_write_volume_gradually(
  			chip, reg.reg, nreg >> 8, nreg & 0xff,
  			/* just set both channels, doesn't matter */
  			SET_CHAN_LEFT|SET_CHAN_RIGHT,
  			0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
968
  	else
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
969
          	snd_azf3328_mixer_outw(chip, reg.reg, nreg);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
970

4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
971
972
973
  	dev_dbg(chip->card->dev,
  		"put: %02x to %02lx|%02lx, oreg %04x; shift %02d|%02d -> nreg %04x; after: %04x
  ",
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
974
975
976
  		reg.reg, ucontrol->value.integer.value[0], ucontrol->value.integer.value[1],
  		oreg, reg.lchan_shift, reg.rchan_shift,
  		nreg, snd_azf3328_mixer_inw(chip, reg.reg));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
977
978
  	return (nreg != oreg);
  }
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
979
  static int
95de77660   Takashi Iwai   [ALSA] Remove xxx...
980
981
  snd_azf3328_info_mixer_enum(struct snd_kcontrol *kcontrol,
  			    struct snd_ctl_elem_info *uinfo)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
982
  {
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
983
  	static const char * const texts1[] = {
13769e3f2   Andreas Mohr   [ALSA] azt3328.c:...
984
  		"Mic1", "Mic2"
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
985
986
  	};
  	static const char * const texts2[] = {
13769e3f2   Andreas Mohr   [ALSA] azt3328.c:...
987
  		"Mix", "Mic"
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
988
989
  	};
  	static const char * const texts3[] = {
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
990
  		"Mic", "CD", "Video", "Aux",
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
991
  		"Line", "Mix", "Mix Mono", "Phone"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
992
          };
13769e3f2   Andreas Mohr   [ALSA] azt3328.c:...
993
994
995
  	static const char * const texts4[] = {
  		"pre 3D", "post 3D"
          };
95de77660   Takashi Iwai   [ALSA] Remove xxx...
996
  	struct azf3328_mixer_reg reg;
627d3e7ab   Andreas Mohr   ALSA: PCI168 snd-...
997
  	const char * const *p = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
998
999
  
  	snd_azf3328_mixer_reg_decode(&reg, kcontrol->private_value);
e2f872608   Andreas Mohr   [ALSA] azt3328.c:...
1000
  	if (reg.reg == IDX_MIXER_ADVCTL2) {
13769e3f2   Andreas Mohr   [ALSA] azt3328.c:...
1001
1002
  		switch(reg.lchan_shift) {
  		case 8: /* modem out sel */
627d3e7ab   Andreas Mohr   ALSA: PCI168 snd-...
1003
  			p = texts1;
13769e3f2   Andreas Mohr   [ALSA] azt3328.c:...
1004
1005
  			break;
  		case 9: /* mono sel source */
627d3e7ab   Andreas Mohr   ALSA: PCI168 snd-...
1006
  			p = texts2;
13769e3f2   Andreas Mohr   [ALSA] azt3328.c:...
1007
1008
  			break;
  		case 15: /* PCM Out Path */
627d3e7ab   Andreas Mohr   ALSA: PCI168 snd-...
1009
  			p = texts4;
13769e3f2   Andreas Mohr   [ALSA] azt3328.c:...
1010
1011
  			break;
  		}
9b311a0ad   Takashi Iwai   ALSA: azt3328: Us...
1012
  	} else if (reg.reg == IDX_MIXER_REC_SELECT)
627d3e7ab   Andreas Mohr   ALSA: PCI168 snd-...
1013
  		p = texts3;
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
1014

9b311a0ad   Takashi Iwai   ALSA: azt3328: Us...
1015
1016
1017
  	return snd_ctl_enum_info(uinfo,
  				 (reg.reg == IDX_MIXER_REC_SELECT) ? 2 : 1,
  				 reg.enum_c, p);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1018
  }
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
1019
  static int
95de77660   Takashi Iwai   [ALSA] Remove xxx...
1020
1021
  snd_azf3328_get_mixer_enum(struct snd_kcontrol *kcontrol,
  			   struct snd_ctl_elem_value *ucontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1022
  {
95de77660   Takashi Iwai   [ALSA] Remove xxx...
1023
1024
          struct snd_azf3328 *chip = snd_kcontrol_chip(kcontrol);
  	struct azf3328_mixer_reg reg;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1025
          unsigned short val;
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
1026

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1027
  	snd_azf3328_mixer_reg_decode(&reg, kcontrol->private_value);
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
1028
  	val = snd_azf3328_mixer_inw(chip, reg.reg);
e2f872608   Andreas Mohr   [ALSA] azt3328.c:...
1029
  	if (reg.reg == IDX_MIXER_REC_SELECT) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1030
1031
          	ucontrol->value.enumerated.item[0] = (val >> 8) & (reg.enum_c - 1);
          	ucontrol->value.enumerated.item[1] = (val >> 0) & (reg.enum_c - 1);
e2f872608   Andreas Mohr   [ALSA] azt3328.c:...
1032
  	} else
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1033
          	ucontrol->value.enumerated.item[0] = (val >> reg.lchan_shift) & (reg.enum_c - 1);
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
1034

4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
1035
1036
1037
  	dev_dbg(chip->card->dev,
  		"get_enum: %02x is %04x -> %d|%d (shift %02d, enum_c %d)
  ",
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
1038
1039
  		reg.reg, val, ucontrol->value.enumerated.item[0], ucontrol->value.enumerated.item[1],
  		reg.lchan_shift, reg.enum_c);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1040
1041
          return 0;
  }
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
1042
  static int
95de77660   Takashi Iwai   [ALSA] Remove xxx...
1043
1044
  snd_azf3328_put_mixer_enum(struct snd_kcontrol *kcontrol,
  			   struct snd_ctl_elem_value *ucontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1045
  {
95de77660   Takashi Iwai   [ALSA] Remove xxx...
1046
1047
          struct snd_azf3328 *chip = snd_kcontrol_chip(kcontrol);
  	struct azf3328_mixer_reg reg;
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1048
  	u16 oreg, nreg, val;
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
1049

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1050
  	snd_azf3328_mixer_reg_decode(&reg, kcontrol->private_value);
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
1051
  	oreg = snd_azf3328_mixer_inw(chip, reg.reg);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1052
  	val = oreg;
e2f872608   Andreas Mohr   [ALSA] azt3328.c:...
1053
  	if (reg.reg == IDX_MIXER_REC_SELECT) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1054
1055
1056
1057
1058
          	if (ucontrol->value.enumerated.item[0] > reg.enum_c - 1U ||
              	ucontrol->value.enumerated.item[1] > reg.enum_c - 1U)
                  	return -EINVAL;
          	val = (ucontrol->value.enumerated.item[0] << 8) |
          	      (ucontrol->value.enumerated.item[1] << 0);
e2f872608   Andreas Mohr   [ALSA] azt3328.c:...
1059
  	} else {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1060
1061
1062
1063
1064
          	if (ucontrol->value.enumerated.item[0] > reg.enum_c - 1U)
                  	return -EINVAL;
  		val &= ~((reg.enum_c - 1) << reg.lchan_shift);
          	val |= (ucontrol->value.enumerated.item[0] << reg.lchan_shift);
  	}
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
1065
  	snd_azf3328_mixer_outw(chip, reg.reg, val);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1066
  	nreg = val;
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
1067
1068
1069
  	dev_dbg(chip->card->dev,
  		"put_enum: %02x to %04x, oreg %04x
  ", reg.reg, val, oreg);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1070
1071
  	return (nreg != oreg);
  }
b4e5e7077   Takashi Iwai   ALSA: pci: Consti...
1072
  static const struct snd_kcontrol_new snd_azf3328_mixer_controls[] = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1073
1074
  	AZF3328_MIXER_SWITCH("Master Playback Switch", IDX_MIXER_PLAY_MASTER, 15, 1),
  	AZF3328_MIXER_VOL_STEREO("Master Playback Volume", IDX_MIXER_PLAY_MASTER, 0x1f, 1),
627d3e7ab   Andreas Mohr   ALSA: PCI168 snd-...
1075
1076
1077
1078
1079
  	AZF3328_MIXER_SWITCH("PCM Playback Switch", IDX_MIXER_WAVEOUT, 15, 1),
  	AZF3328_MIXER_VOL_STEREO("PCM Playback Volume",
  					IDX_MIXER_WAVEOUT, 0x1f, 1),
  	AZF3328_MIXER_SWITCH("PCM 3D Bypass Playback Switch",
  					IDX_MIXER_ADVCTL2, 7, 1),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
  	AZF3328_MIXER_SWITCH("FM Playback Switch", IDX_MIXER_FMSYNTH, 15, 1),
  	AZF3328_MIXER_VOL_STEREO("FM Playback Volume", IDX_MIXER_FMSYNTH, 0x1f, 1),
  	AZF3328_MIXER_SWITCH("CD Playback Switch", IDX_MIXER_CDAUDIO, 15, 1),
  	AZF3328_MIXER_VOL_STEREO("CD Playback Volume", IDX_MIXER_CDAUDIO, 0x1f, 1),
  	AZF3328_MIXER_SWITCH("Capture Switch", IDX_MIXER_REC_VOLUME, 15, 1),
  	AZF3328_MIXER_VOL_STEREO("Capture Volume", IDX_MIXER_REC_VOLUME, 0x0f, 0),
  	AZF3328_MIXER_ENUM("Capture Source", IDX_MIXER_REC_SELECT, 8, 0),
  	AZF3328_MIXER_SWITCH("Mic Playback Switch", IDX_MIXER_MIC, 15, 1),
  	AZF3328_MIXER_VOL_MONO("Mic Playback Volume", IDX_MIXER_MIC, 0x1f, 1),
  	AZF3328_MIXER_SWITCH("Mic Boost (+20dB)", IDX_MIXER_MIC, 6, 0),
  	AZF3328_MIXER_SWITCH("Line Playback Switch", IDX_MIXER_LINEIN, 15, 1),
  	AZF3328_MIXER_VOL_STEREO("Line Playback Volume", IDX_MIXER_LINEIN, 0x1f, 1),
d355c82a0   Jaroslav Kysela   ALSA: rename "PC ...
1092
1093
  	AZF3328_MIXER_SWITCH("Beep Playback Switch", IDX_MIXER_PCBEEP, 15, 1),
  	AZF3328_MIXER_VOL_SPECIAL("Beep Playback Volume", IDX_MIXER_PCBEEP, 0x0f, 1, 1),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1094
1095
1096
1097
1098
1099
1100
1101
  	AZF3328_MIXER_SWITCH("Video Playback Switch", IDX_MIXER_VIDEO, 15, 1),
  	AZF3328_MIXER_VOL_STEREO("Video Playback Volume", IDX_MIXER_VIDEO, 0x1f, 1),
  	AZF3328_MIXER_SWITCH("Aux Playback Switch", IDX_MIXER_AUX, 15, 1),
  	AZF3328_MIXER_VOL_STEREO("Aux Playback Volume", IDX_MIXER_AUX, 0x1f, 1),
  	AZF3328_MIXER_SWITCH("Modem Playback Switch", IDX_MIXER_MODEMOUT, 15, 1),
  	AZF3328_MIXER_VOL_MONO("Modem Playback Volume", IDX_MIXER_MODEMOUT, 0x1f, 1),
  	AZF3328_MIXER_SWITCH("Modem Capture Switch", IDX_MIXER_MODEMIN, 15, 1),
  	AZF3328_MIXER_VOL_MONO("Modem Capture Volume", IDX_MIXER_MODEMIN, 0x1f, 1),
13769e3f2   Andreas Mohr   [ALSA] azt3328.c:...
1102
1103
  	AZF3328_MIXER_ENUM("Mic Select", IDX_MIXER_ADVCTL2, 2, 8),
  	AZF3328_MIXER_ENUM("Mono Output Select", IDX_MIXER_ADVCTL2, 2, 9),
e24a121aa   Andreas Mohr   [ALSA] azt3328.c:...
1104
  	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! */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1105
1106
  	AZF3328_MIXER_VOL_SPECIAL("Tone Control - Treble", IDX_MIXER_BASSTREBLE, 0x07, 1, 0),
  	AZF3328_MIXER_VOL_SPECIAL("Tone Control - Bass", IDX_MIXER_BASSTREBLE, 0x07, 9, 0),
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
1107
  	AZF3328_MIXER_SWITCH("3D Control - Switch", IDX_MIXER_ADVCTL2, 13, 0),
13769e3f2   Andreas Mohr   [ALSA] azt3328.c:...
1108
1109
  	AZF3328_MIXER_VOL_SPECIAL("3D Control - Width", IDX_MIXER_ADVCTL1, 0x07, 1, 0), /* "3D Width" */
  	AZF3328_MIXER_VOL_SPECIAL("3D Control - Depth", IDX_MIXER_ADVCTL1, 0x03, 8, 0), /* "Hifi 3D" */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
  #if MIXER_TESTING
  	AZF3328_MIXER_SWITCH("0", IDX_MIXER_ADVCTL2, 0, 0),
  	AZF3328_MIXER_SWITCH("1", IDX_MIXER_ADVCTL2, 1, 0),
  	AZF3328_MIXER_SWITCH("2", IDX_MIXER_ADVCTL2, 2, 0),
  	AZF3328_MIXER_SWITCH("3", IDX_MIXER_ADVCTL2, 3, 0),
  	AZF3328_MIXER_SWITCH("4", IDX_MIXER_ADVCTL2, 4, 0),
  	AZF3328_MIXER_SWITCH("5", IDX_MIXER_ADVCTL2, 5, 0),
  	AZF3328_MIXER_SWITCH("6", IDX_MIXER_ADVCTL2, 6, 0),
  	AZF3328_MIXER_SWITCH("7", IDX_MIXER_ADVCTL2, 7, 0),
  	AZF3328_MIXER_SWITCH("8", IDX_MIXER_ADVCTL2, 8, 0),
  	AZF3328_MIXER_SWITCH("9", IDX_MIXER_ADVCTL2, 9, 0),
  	AZF3328_MIXER_SWITCH("10", IDX_MIXER_ADVCTL2, 10, 0),
  	AZF3328_MIXER_SWITCH("11", IDX_MIXER_ADVCTL2, 11, 0),
  	AZF3328_MIXER_SWITCH("12", IDX_MIXER_ADVCTL2, 12, 0),
  	AZF3328_MIXER_SWITCH("13", IDX_MIXER_ADVCTL2, 13, 0),
  	AZF3328_MIXER_SWITCH("14", IDX_MIXER_ADVCTL2, 14, 0),
  	AZF3328_MIXER_SWITCH("15", IDX_MIXER_ADVCTL2, 15, 0),
  #endif
  };
83fdb6fbc   Takashi Iwai   ALSA: azt3328: Mo...
1129
  static const u16 snd_azf3328_init_values[][2] = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
          { IDX_MIXER_PLAY_MASTER,	MIXER_MUTE_MASK|0x1f1f },
          { IDX_MIXER_MODEMOUT,		MIXER_MUTE_MASK|0x1f1f },
  	{ IDX_MIXER_BASSTREBLE,		0x0000 },
  	{ IDX_MIXER_PCBEEP,		MIXER_MUTE_MASK|0x1f1f },
  	{ IDX_MIXER_MODEMIN,		MIXER_MUTE_MASK|0x1f1f },
  	{ IDX_MIXER_MIC,		MIXER_MUTE_MASK|0x001f },
  	{ IDX_MIXER_LINEIN,		MIXER_MUTE_MASK|0x1f1f },
  	{ IDX_MIXER_CDAUDIO,		MIXER_MUTE_MASK|0x1f1f },
  	{ IDX_MIXER_VIDEO,		MIXER_MUTE_MASK|0x1f1f },
  	{ IDX_MIXER_AUX,		MIXER_MUTE_MASK|0x1f1f },
          { IDX_MIXER_WAVEOUT,		MIXER_MUTE_MASK|0x1f1f },
          { IDX_MIXER_FMSYNTH,		MIXER_MUTE_MASK|0x1f1f },
          { IDX_MIXER_REC_VOLUME,		MIXER_MUTE_MASK|0x0707 },
  };
e23e7a143   Bill Pemberton   ALSA: pci: remove...
1144
  static int
95de77660   Takashi Iwai   [ALSA] Remove xxx...
1145
  snd_azf3328_mixer_new(struct snd_azf3328 *chip)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1146
  {
95de77660   Takashi Iwai   [ALSA] Remove xxx...
1147
1148
  	struct snd_card *card;
  	const struct snd_kcontrol_new *sw;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1149
1150
  	unsigned int idx;
  	int err;
da3cec35d   Takashi Iwai   ALSA: Kill snd_as...
1151
1152
  	if (snd_BUG_ON(!chip || !chip->card))
  		return -EINVAL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1153
1154
1155
1156
  
  	card = chip->card;
  
  	/* mixer reset */
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
1157
  	snd_azf3328_mixer_outw(chip, IDX_MIXER_RESET, 0x0000);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1158
1159
  
  	/* mute and zero volume channels */
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
1160
  	for (idx = 0; idx < ARRAY_SIZE(snd_azf3328_init_values); ++idx) {
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
1161
1162
1163
  		snd_azf3328_mixer_outw(chip,
  			snd_azf3328_init_values[idx][0],
  			snd_azf3328_init_values[idx][1]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1164
  	}
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
1165

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1166
1167
  	/* add mixer controls */
  	sw = snd_azf3328_mixer_controls;
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
1168
1169
  	for (idx = 0; idx < ARRAY_SIZE(snd_azf3328_mixer_controls);
  			++idx, ++sw) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1170
1171
1172
1173
1174
  		if ((err = snd_ctl_add(chip->card, snd_ctl_new1(sw, chip))) < 0)
  			return err;
  	}
  	snd_component_add(card, "AZF3328 mixer");
  	strcpy(card->mixername, "AZF3328 mixer");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1175
1176
  	return 0;
  }
b5dc20cd2   Andreas Mohr   ALSA: azt3328: ad...
1177
  #endif /* AZF_USE_AC97_LAYER */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1178

d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
1179
  static void
da237f35a   Andreas Mohr   ALSA: azt3328: us...
1180
  snd_azf3328_codec_setfmt(struct snd_azf3328_codec_data *codec,
627d3e7ab   Andreas Mohr   ALSA: PCI168 snd-...
1181
  			       enum azf_freq_t bitrate,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1182
1183
1184
1185
  			       unsigned int format_width,
  			       unsigned int channels
  )
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1186
  	unsigned long flags;
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1187
  	u16 val = 0xff00;
8d9a114e6   Andreas Mohr   ALSA: azt3328: _s...
1188
  	u8 freq = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1189

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1190
  	switch (bitrate) {
c9ba374d2   Andreas Mohr   ALSA: azt3328 - ...
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
  	case AZF_FREQ_4000:  freq = SOUNDFORMAT_FREQ_SUSPECTED_4000; break;
  	case AZF_FREQ_4800:  freq = SOUNDFORMAT_FREQ_SUSPECTED_4800; break;
  	case AZF_FREQ_5512:
  		/* the AZF3328 names it "5510" for some strange reason */
  			     freq = SOUNDFORMAT_FREQ_5510; break;
  	case AZF_FREQ_6620:  freq = SOUNDFORMAT_FREQ_6620; break;
  	case AZF_FREQ_8000:  freq = SOUNDFORMAT_FREQ_8000; break;
  	case AZF_FREQ_9600:  freq = SOUNDFORMAT_FREQ_9600; break;
  	case AZF_FREQ_11025: freq = SOUNDFORMAT_FREQ_11025; break;
  	case AZF_FREQ_13240: freq = SOUNDFORMAT_FREQ_SUSPECTED_13240; break;
  	case AZF_FREQ_16000: freq = SOUNDFORMAT_FREQ_16000; break;
  	case AZF_FREQ_22050: freq = SOUNDFORMAT_FREQ_22050; break;
  	case AZF_FREQ_32000: freq = SOUNDFORMAT_FREQ_32000; break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1204
  	default:
99b359ba1   Takashi Iwai   [ALSA] Add missin...
1205
1206
  		snd_printk(KERN_WARNING "unknown bitrate %d, assuming 44.1kHz!
  ", bitrate);
c0dbbdad4   Gustavo A. R. Silva   ALSA: Use fallthr...
1207
  		fallthrough;
c9ba374d2   Andreas Mohr   ALSA: azt3328 - ...
1208
1209
1210
  	case AZF_FREQ_44100: freq = SOUNDFORMAT_FREQ_44100; break;
  	case AZF_FREQ_48000: freq = SOUNDFORMAT_FREQ_48000; break;
  	case AZF_FREQ_66200: freq = SOUNDFORMAT_FREQ_SUSPECTED_66200; break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1211
  	}
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
1212
1213
1214
1215
  	/* val = 0xff07; 3m27.993s (65301Hz; -> 64000Hz???) hmm, 66120, 65967, 66123 */
  	/* val = 0xff09; 17m15.098s (13123,478Hz; -> 12000Hz???) hmm, 13237.2Hz? */
  	/* val = 0xff0a; 47m30.599s (4764,891Hz; -> 4800Hz???) yup, 4803Hz */
  	/* val = 0xff0c; 57m0.510s (4010,263Hz; -> 4000Hz???) yup, 4003Hz */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1216
1217
1218
1219
1220
  	/* val = 0xff05; 5m11.556s (... -> 44100Hz) */
  	/* val = 0xff03; 10m21.529s (21872,463Hz; -> 22050Hz???) */
  	/* val = 0xff0f; 20m41.883s (10937,993Hz; -> 11025Hz???) */
  	/* val = 0xff0d; 41m23.135s (5523,600Hz; -> 5512Hz???) */
  	/* val = 0xff0e; 28m30.777s (8017Hz; -> 8000Hz???) */
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
1221

8d9a114e6   Andreas Mohr   ALSA: azt3328: _s...
1222
  	val |= freq;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1223
1224
1225
1226
1227
  	if (channels == 2)
  		val |= SOUNDFORMAT_FLAG_2CHANNELS;
  
  	if (format_width == 16)
  		val |= SOUNDFORMAT_FLAG_16BIT;
da237f35a   Andreas Mohr   ALSA: azt3328: us...
1228
  	spin_lock_irqsave(codec->lock, flags);
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
1229

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1230
  	/* set bitrate/format */
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1231
  	snd_azf3328_codec_outw(codec, IDX_IO_CODEC_SOUNDFORMAT, val);
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
1232

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1233
1234
1235
1236
1237
1238
1239
  	/* changing the bitrate/format settings switches off the
  	 * audio output with an annoying click in case of 8/16bit format change
  	 * (maybe shutting down DAC/ADC?), thus immediately
  	 * do some tweaking to reenable it and get rid of the clicking
  	 * (FIXME: yes, it works, but what exactly am I doing here?? :)
  	 * FIXME: does this have some side effects for full-duplex
  	 * or other dramatic side effects? */
adf5931f8   Andreas Mohr   ALSA: azt3328: co...
1240
  	/* do it for non-capture codecs only */
da237f35a   Andreas Mohr   ALSA: azt3328: us...
1241
  	if (codec->type != AZF_CODEC_CAPTURE)
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1242
1243
1244
1245
  		snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS,
  			snd_azf3328_codec_inw(codec, IDX_IO_CODEC_DMA_FLAGS) |
  			DMA_RUN_SOMETHING1 |
  			DMA_RUN_SOMETHING2 |
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
1246
1247
1248
1249
  			SOMETHING_ALMOST_ALWAYS_SET |
  			DMA_EPILOGUE_SOMETHING |
  			DMA_SOMETHING_ELSE
  		);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1250

da237f35a   Andreas Mohr   ALSA: azt3328: us...
1251
  	spin_unlock_irqrestore(codec->lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1252
  }
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
1253
  static inline void
da237f35a   Andreas Mohr   ALSA: azt3328: us...
1254
  snd_azf3328_codec_setfmt_lowpower(struct snd_azf3328_codec_data *codec
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
1255
1256
1257
1258
1259
1260
  )
  {
  	/* choose lowest frequency for low power consumption.
  	 * While this will cause louder noise due to rather coarse frequency,
  	 * it should never matter since output should always
  	 * get disabled properly when idle anyway. */
da237f35a   Andreas Mohr   ALSA: azt3328: us...
1261
  	snd_azf3328_codec_setfmt(codec, AZF_FREQ_4000, 8, 1);
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
1262
  }
627d3e7ab   Andreas Mohr   ALSA: PCI168 snd-...
1263
  static void
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1264
  snd_azf3328_ctrl_reg_6AH_update(struct snd_azf3328 *chip,
627d3e7ab   Andreas Mohr   ALSA: PCI168 snd-...
1265
  					unsigned bitmask,
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1266
  					bool enable
627d3e7ab   Andreas Mohr   ALSA: PCI168 snd-...
1267
1268
  )
  {
78df617ac   Andreas Mohr   ALSA: azt3328: fi...
1269
1270
  	bool do_mask = !enable;
  	if (do_mask)
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1271
  		chip->shadow_reg_ctrl_6AH |= bitmask;
78df617ac   Andreas Mohr   ALSA: azt3328: fi...
1272
1273
  	else
  		chip->shadow_reg_ctrl_6AH &= ~bitmask;
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
1274
1275
1276
1277
  	dev_dbg(chip->card->dev,
  		"6AH_update mask 0x%04x do_mask %d: val 0x%04x
  ",
  		bitmask, do_mask, chip->shadow_reg_ctrl_6AH);
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1278
  	snd_azf3328_ctrl_outw(chip, IDX_IO_6AH, chip->shadow_reg_ctrl_6AH);
627d3e7ab   Andreas Mohr   ALSA: PCI168 snd-...
1279
  }
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
1280
  static inline void
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1281
  snd_azf3328_ctrl_enable_codecs(struct snd_azf3328 *chip, bool enable)
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
1282
  {
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
1283
1284
  	dev_dbg(chip->card->dev, "codec_enable %d
  ", enable);
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
1285
1286
  	/* no idea what exactly is being done here, but I strongly assume it's
  	 * PM related */
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1287
  	snd_azf3328_ctrl_reg_6AH_update(
627d3e7ab   Andreas Mohr   ALSA: PCI168 snd-...
1288
  		chip, IO_6A_PAUSE_PLAYBACK_BIT8, enable
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
1289
1290
1291
1292
  	);
  }
  
  static void
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1293
1294
1295
  snd_azf3328_ctrl_codec_activity(struct snd_azf3328 *chip,
  				enum snd_azf3328_codec_type codec_type,
  				bool enable
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
1296
1297
  )
  {
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1298
1299
  	struct snd_azf3328_codec_data *codec = &chip->codecs[codec_type];
  	bool need_change = (codec->running != enable);
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
1300

4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
1301
  	dev_dbg(chip->card->dev,
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1302
1303
1304
  		"codec_activity: %s codec, enable %d, need_change %d
  ",
  				codec->name, enable, need_change
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
1305
1306
  	);
  	if (need_change) {
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
  		static const struct {
  			enum snd_azf3328_codec_type other1;
  			enum snd_azf3328_codec_type other2;
  		} peer_codecs[3] =
  			{ { AZF_CODEC_CAPTURE, AZF_CODEC_I2S_OUT },
  			  { AZF_CODEC_PLAYBACK, AZF_CODEC_I2S_OUT },
  			  { AZF_CODEC_PLAYBACK, AZF_CODEC_CAPTURE } };
  		bool call_function;
  
  		if (enable)
  			/* if enable codec, call enable_codecs func
  			   to enable codec supply... */
  			call_function = 1;
  		else {
  			/* ...otherwise call enable_codecs func
  			   (which globally shuts down operation of codecs)
  			   only in case the other codecs are currently
  			   not active either! */
78df617ac   Andreas Mohr   ALSA: azt3328: fi...
1325
1326
1327
1328
1329
  			call_function =
  				((!chip->codecs[peer_codecs[codec_type].other1]
  					.running)
  			     &&  (!chip->codecs[peer_codecs[codec_type].other2]
  					.running));
7f788e0cc   Dan Carpenter   ALSA: azt3328: so...
1330
1331
  		}
  		if (call_function)
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1332
  			snd_azf3328_ctrl_enable_codecs(chip, enable);
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
1333
1334
1335
1336
  
  		/* ...and adjust clock, too
  		 * (reduce noise and power consumption) */
  		if (!enable)
da237f35a   Andreas Mohr   ALSA: azt3328: us...
1337
  			snd_azf3328_codec_setfmt_lowpower(codec);
78df617ac   Andreas Mohr   ALSA: azt3328: fi...
1338
  		codec->running = enable;
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
1339
  	}
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
1340
  }
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
1341
  static void
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
1342
1343
1344
1345
1346
  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
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
1347
  )
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1348
  {
689c69120   Andreas Mohr   ALSA: azt3328: im...
1349
1350
1351
1352
1353
1354
  	WARN_ONCE(period_bytes & 1, "odd period length!?
  ");
  	WARN_ONCE(buffer_bytes != 2 * period_bytes,
  		 "missed our input expectations! %u vs. %u
  ",
  		 buffer_bytes, period_bytes);
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1355
1356
  	if (!codec->running) {
  		/* AZF3328 uses a two buffer pointer DMA transfer approach */
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
1357

689c69120   Andreas Mohr   ALSA: azt3328: im...
1358
  		unsigned long flags;
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
1359
1360
  
  		/* width 32bit (prevent overflow): */
689c69120   Andreas Mohr   ALSA: azt3328: im...
1361
1362
1363
1364
1365
1366
  		u32 area_length;
  		struct codec_setup_io {
  			u32 dma_start_1;
  			u32 dma_start_2;
  			u32 dma_lengths;
  		} __attribute__((packed)) setup_io;
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
1367

689c69120   Andreas Mohr   ALSA: azt3328: im...
1368
  		area_length = buffer_bytes/2;
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
1369

689c69120   Andreas Mohr   ALSA: azt3328: im...
1370
1371
  		setup_io.dma_start_1 = addr;
  		setup_io.dma_start_2 = addr+area_length;
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
1372
  		dev_dbg(chip->card->dev,
689c69120   Andreas Mohr   ALSA: azt3328: im...
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
  			"setdma: buffers %08x[%u] / %08x[%u], %u, %u
  ",
  				setup_io.dma_start_1, area_length,
  				setup_io.dma_start_2, area_length,
  				period_bytes, buffer_bytes);
  
  		/* Hmm, are we really supposed to decrement this by 1??
  		   Most definitely certainly not: configuring full length does
  		   work properly (i.e. likely better), and BTW we
  		   violated possibly differing frame sizes with this...
  
  		area_length--; |* max. index *|
  		*/
7974150c8   Andreas Mohr   ALSA: azt3328: pe...
1386

d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
1387
  		/* build combined I/O buffer length word */
689c69120   Andreas Mohr   ALSA: azt3328: im...
1388
  		setup_io.dma_lengths = (area_length << 16) | (area_length);
da237f35a   Andreas Mohr   ALSA: azt3328: us...
1389
  		spin_lock_irqsave(codec->lock, flags);
689c69120   Andreas Mohr   ALSA: azt3328: im...
1390
1391
1392
  		snd_azf3328_codec_outl_multi(
  			codec, IDX_IO_CODEC_DMA_START_1, &setup_io, 3
  		);
da237f35a   Andreas Mohr   ALSA: azt3328: us...
1393
  		spin_unlock_irqrestore(codec->lock, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1394
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1395
  }
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
1396
  static int
da237f35a   Andreas Mohr   ALSA: azt3328: us...
1397
  snd_azf3328_pcm_prepare(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1398
  {
95de77660   Takashi Iwai   [ALSA] Remove xxx...
1399
  	struct snd_pcm_runtime *runtime = substream->runtime;
da237f35a   Andreas Mohr   ALSA: azt3328: us...
1400
  	struct snd_azf3328_codec_data *codec = runtime->private_data;
345855951   Andreas Mohr   ALSA: azt3328: us...
1401
  #if 0
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1402
1403
1404
          unsigned int size = snd_pcm_lib_buffer_bytes(substream);
  	unsigned int count = snd_pcm_lib_period_bytes(substream);
  #endif
345855951   Andreas Mohr   ALSA: azt3328: us...
1405
  	codec->dma_base = runtime->dma_addr;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1406
  #if 0
da237f35a   Andreas Mohr   ALSA: azt3328: us...
1407
  	snd_azf3328_codec_setfmt(codec,
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
1408
1409
1410
  		runtime->rate,
  		snd_pcm_format_width(runtime->format),
  		runtime->channels);
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
1411
  	snd_azf3328_codec_setdmaa(chip, codec,
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1412
  					runtime->dma_addr, count, size);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1413
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1414
1415
  	return 0;
  }
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
1416
  static int
da237f35a   Andreas Mohr   ALSA: azt3328: us...
1417
  snd_azf3328_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1418
  {
95de77660   Takashi Iwai   [ALSA] Remove xxx...
1419
1420
  	struct snd_azf3328 *chip = snd_pcm_substream_chip(substream);
  	struct snd_pcm_runtime *runtime = substream->runtime;
da237f35a   Andreas Mohr   ALSA: azt3328: us...
1421
  	struct snd_azf3328_codec_data *codec = runtime->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1422
  	int result = 0;
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1423
  	u16 flags1;
e0f17c75d   Peter Senna Tschudin   ALSA: Fix assignm...
1424
  	bool previously_muted = false;
da237f35a   Andreas Mohr   ALSA: azt3328: us...
1425
  	bool is_main_mixer_playback_codec = (AZF_CODEC_PLAYBACK == codec->type);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1426

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1427
1428
  	switch (cmd) {
  	case SNDRV_PCM_TRIGGER_START:
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
1429
1430
  		dev_dbg(chip->card->dev, "START PCM %s
  ", codec->name);
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1431

da237f35a   Andreas Mohr   ALSA: azt3328: us...
1432
  		if (is_main_mixer_playback_codec) {
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1433
1434
  			/* mute WaveOut (avoid clicking during setup) */
  			previously_muted =
b5dc20cd2   Andreas Mohr   ALSA: azt3328: ad...
1435
1436
  				snd_azf3328_mixer_mute_control_pcm(
  						chip, 1
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1437
1438
  				);
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1439

da237f35a   Andreas Mohr   ALSA: azt3328: us...
1440
  		snd_azf3328_codec_setfmt(codec,
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
1441
1442
1443
  			runtime->rate,
  			snd_pcm_format_width(runtime->format),
  			runtime->channels);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1444

da237f35a   Andreas Mohr   ALSA: azt3328: us...
1445
  		spin_lock(codec->lock);
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
1446
  		/* first, remember current value: */
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1447
  		flags1 = snd_azf3328_codec_inw(codec, IDX_IO_CODEC_DMA_FLAGS);
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
1448

dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1449
1450
1451
  		/* stop transfer */
  		flags1 &= ~DMA_RESUME;
  		snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS, flags1);
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
1452

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1453
  		/* FIXME: clear interrupts or what??? */
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1454
  		snd_azf3328_codec_outw(codec, IDX_IO_CODEC_IRQTYPE, 0xffff);
da237f35a   Andreas Mohr   ALSA: azt3328: us...
1455
  		spin_unlock(codec->lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1456

4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
1457
  		snd_azf3328_codec_setdmaa(chip, codec, runtime->dma_addr,
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
1458
  			snd_pcm_lib_period_bytes(substream),
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1459
1460
  			snd_pcm_lib_buffer_bytes(substream)
  		);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1461

da237f35a   Andreas Mohr   ALSA: azt3328: us...
1462
  		spin_lock(codec->lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1463
1464
  #ifdef WIN9X
  		/* FIXME: enable playback/recording??? */
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1465
1466
  		flags1 |= DMA_RUN_SOMETHING1 | DMA_RUN_SOMETHING2;
  		snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS, flags1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1467

dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1468
  		/* start transfer again */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1469
  		/* FIXME: what is this value (0x0010)??? */
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1470
1471
  		flags1 |= DMA_RESUME | DMA_EPILOGUE_SOMETHING;
  		snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS, flags1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1472
  #else /* NT4 */
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1473
  		snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS,
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
1474
  			0x0000);
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1475
1476
1477
1478
1479
1480
  		snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS,
  			DMA_RUN_SOMETHING1);
  		snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS,
  			DMA_RUN_SOMETHING1 |
  			DMA_RUN_SOMETHING2);
  		snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS,
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
1481
1482
1483
1484
  			DMA_RESUME |
  			SOMETHING_ALMOST_ALWAYS_SET |
  			DMA_EPILOGUE_SOMETHING |
  			DMA_SOMETHING_ELSE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1485
  #endif
da237f35a   Andreas Mohr   ALSA: azt3328: us...
1486
1487
  		spin_unlock(codec->lock);
  		snd_azf3328_ctrl_codec_activity(chip, codec->type, 1);
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1488

da237f35a   Andreas Mohr   ALSA: azt3328: us...
1489
  		if (is_main_mixer_playback_codec) {
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1490
1491
  			/* now unmute WaveOut */
  			if (!previously_muted)
b5dc20cd2   Andreas Mohr   ALSA: azt3328: ad...
1492
1493
  				snd_azf3328_mixer_mute_control_pcm(
  						chip, 0
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1494
1495
  				);
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1496

4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
1497
1498
  		dev_dbg(chip->card->dev, "PCM STARTED %s
  ", codec->name);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1499
  		break;
ca54bde36   Andreas Mohr   [ALSA] azt3328.c:...
1500
  	case SNDRV_PCM_TRIGGER_RESUME:
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
1501
1502
  		dev_dbg(chip->card->dev, "PCM RESUME %s
  ", codec->name);
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1503
  		/* resume codec if we were active */
da237f35a   Andreas Mohr   ALSA: azt3328: us...
1504
  		spin_lock(codec->lock);
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1505
1506
1507
1508
1509
1510
  		if (codec->running)
  			snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS,
  				snd_azf3328_codec_inw(
  					codec, IDX_IO_CODEC_DMA_FLAGS
  				) | DMA_RESUME
  			);
da237f35a   Andreas Mohr   ALSA: azt3328: us...
1511
  		spin_unlock(codec->lock);
ca54bde36   Andreas Mohr   [ALSA] azt3328.c:...
1512
  		break;
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
1513
  	case SNDRV_PCM_TRIGGER_STOP:
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
1514
1515
  		dev_dbg(chip->card->dev, "PCM STOP %s
  ", codec->name);
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1516

da237f35a   Andreas Mohr   ALSA: azt3328: us...
1517
  		if (is_main_mixer_playback_codec) {
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1518
1519
  			/* mute WaveOut (avoid clicking during setup) */
  			previously_muted =
b5dc20cd2   Andreas Mohr   ALSA: azt3328: ad...
1520
1521
  				snd_azf3328_mixer_mute_control_pcm(
  						chip, 1
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1522
1523
  				);
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1524

da237f35a   Andreas Mohr   ALSA: azt3328: us...
1525
  		spin_lock(codec->lock);
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
1526
  		/* first, remember current value: */
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1527
  		flags1 = snd_azf3328_codec_inw(codec, IDX_IO_CODEC_DMA_FLAGS);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1528

dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1529
1530
1531
  		/* stop transfer */
  		flags1 &= ~DMA_RESUME;
  		snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS, flags1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1532

d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
1533
1534
  		/* hmm, is this really required? we're resetting the same bit
  		 * immediately thereafter... */
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1535
1536
  		flags1 |= DMA_RUN_SOMETHING1;
  		snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS, flags1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1537

dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1538
1539
  		flags1 &= ~DMA_RUN_SOMETHING1;
  		snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS, flags1);
da237f35a   Andreas Mohr   ALSA: azt3328: us...
1540
1541
  		spin_unlock(codec->lock);
  		snd_azf3328_ctrl_codec_activity(chip, codec->type, 0);
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1542

da237f35a   Andreas Mohr   ALSA: azt3328: us...
1543
  		if (is_main_mixer_playback_codec) {
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1544
1545
  			/* now unmute WaveOut */
  			if (!previously_muted)
b5dc20cd2   Andreas Mohr   ALSA: azt3328: ad...
1546
1547
  				snd_azf3328_mixer_mute_control_pcm(
  						chip, 0
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1548
1549
  				);
  		}
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
1550

4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
1551
1552
  		dev_dbg(chip->card->dev, "PCM STOPPED %s
  ", codec->name);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1553
  		break;
ca54bde36   Andreas Mohr   [ALSA] azt3328.c:...
1554
  	case SNDRV_PCM_TRIGGER_SUSPEND:
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
1555
1556
  		dev_dbg(chip->card->dev, "PCM SUSPEND %s
  ", codec->name);
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1557
1558
1559
1560
1561
1562
  		/* make sure codec is stopped */
  		snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS,
  			snd_azf3328_codec_inw(
  				codec, IDX_IO_CODEC_DMA_FLAGS
  			) & ~DMA_RESUME
  		);
ca54bde36   Andreas Mohr   [ALSA] azt3328.c:...
1563
  		break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1564
          case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
1565
1566
  		WARN(1, "FIXME: SNDRV_PCM_TRIGGER_PAUSE_PUSH NIY!
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1567
1568
                  break;
          case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
1569
1570
  		WARN(1, "FIXME: SNDRV_PCM_TRIGGER_PAUSE_RELEASE NIY!
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1571
1572
                  break;
          default:
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
1573
1574
  		WARN(1, "FIXME: unknown trigger mode!
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1575
1576
                  return -EINVAL;
  	}
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
1577

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1578
1579
  	return result;
  }
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
1580
  static snd_pcm_uframes_t
da237f35a   Andreas Mohr   ALSA: azt3328: us...
1581
  snd_azf3328_pcm_pointer(struct snd_pcm_substream *substream
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1582
  )
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1583
  {
da237f35a   Andreas Mohr   ALSA: azt3328: us...
1584
1585
  	const struct snd_azf3328_codec_data *codec =
  		substream->runtime->private_data;
345855951   Andreas Mohr   ALSA: azt3328: us...
1586
  	unsigned long result;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1587
  	snd_pcm_uframes_t frmres;
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1588
  	result = snd_azf3328_codec_inl(codec, IDX_IO_CODEC_DMA_CURRPOS);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1589

d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
1590
  	/* calculate offset */
345855951   Andreas Mohr   ALSA: azt3328: us...
1591
1592
1593
1594
1595
  #ifdef QUERY_HARDWARE
  	result -= snd_azf3328_codec_inl(codec, IDX_IO_CODEC_DMA_START_1);
  #else
  	result -= codec->dma_base;
  #endif
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
1596
  	frmres = bytes_to_frames( substream->runtime, result);
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
1597
1598
1599
  	dev_dbg(substream->pcm->card->dev, "%08li %s @ 0x%8lx, frames %8ld
  ",
  		jiffies, codec->name, result, frmres);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1600
1601
  	return frmres;
  }
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
1602
1603
1604
1605
  /******************************************************************/
  
  #ifdef SUPPORT_GAMEPORT
  static inline void
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1606
1607
1608
  snd_azf3328_gameport_irq_enable(struct snd_azf3328 *chip,
  				bool enable
  )
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
1609
1610
1611
1612
1613
1614
1615
1616
1617
  {
  	snd_azf3328_io_reg_setb(
  		chip->game_io+IDX_GAME_HWCONFIG,
  		GAME_HWCFG_IRQ_ENABLE,
  		enable
  	);
  }
  
  static inline void
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1618
1619
1620
  snd_azf3328_gameport_legacy_address_enable(struct snd_azf3328 *chip,
  					   bool enable
  )
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
1621
1622
1623
1624
1625
1626
1627
  {
  	snd_azf3328_io_reg_setb(
  		chip->game_io+IDX_GAME_HWCONFIG,
  		GAME_HWCFG_LEGACY_ADDRESS_ENABLE,
  		enable
  	);
  }
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
  static void
  snd_azf3328_gameport_set_counter_frequency(struct snd_azf3328 *chip,
  					   unsigned int freq_cfg
  )
  {
  	snd_azf3328_io_reg_setb(
  		chip->game_io+IDX_GAME_HWCONFIG,
  		0x02,
  		(freq_cfg & 1) != 0
  	);
  	snd_azf3328_io_reg_setb(
  		chip->game_io+IDX_GAME_HWCONFIG,
  		0x04,
  		(freq_cfg & 2) != 0
  	);
  }
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
1644
  static inline void
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1645
  snd_azf3328_gameport_axis_circuit_enable(struct snd_azf3328 *chip, bool enable)
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
1646
  {
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1647
  	snd_azf3328_ctrl_reg_6AH_update(
627d3e7ab   Andreas Mohr   ALSA: PCI168 snd-...
1648
  		chip, IO_6A_SOMETHING2_GAMEPORT, enable
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
  	);
  }
  
  static inline void
  snd_azf3328_gameport_interrupt(struct snd_azf3328 *chip)
  {
  	/*
  	 * skeleton handler only
  	 * (we do not want axis reading in interrupt handler - too much load!)
  	 */
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
1659
1660
  	dev_dbg(chip->card->dev, "gameport irq
  ");
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
  
  	 /* this should ACK the gameport IRQ properly, hopefully. */
  	snd_azf3328_game_inw(chip, IDX_GAME_AXIS_VALUE);
  }
  
  static int
  snd_azf3328_gameport_open(struct gameport *gameport, int mode)
  {
  	struct snd_azf3328 *chip = gameport_get_port_data(gameport);
  	int res;
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
1671
1672
  	dev_dbg(chip->card->dev, "gameport_open, mode %d
  ", mode);
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
1673
1674
1675
1676
1677
1678
1679
1680
1681
  	switch (mode) {
  	case GAMEPORT_MODE_COOKED:
  	case GAMEPORT_MODE_RAW:
  		res = 0;
  		break;
  	default:
  		res = -1;
  		break;
  	}
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1682
1683
  	snd_azf3328_gameport_set_counter_frequency(chip,
  				GAME_HWCFG_ADC_COUNTER_FREQ_STD);
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
1684
1685
1686
1687
1688
1689
1690
1691
1692
  	snd_azf3328_gameport_axis_circuit_enable(chip, (res == 0));
  
  	return res;
  }
  
  static void
  snd_azf3328_gameport_close(struct gameport *gameport)
  {
  	struct snd_azf3328 *chip = gameport_get_port_data(gameport);
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
1693
1694
  	dev_dbg(chip->card->dev, "gameport_close
  ");
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1695
1696
  	snd_azf3328_gameport_set_counter_frequency(chip,
  				GAME_HWCFG_ADC_COUNTER_FREQ_1_200);
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
  	snd_azf3328_gameport_axis_circuit_enable(chip, 0);
  }
  
  static int
  snd_azf3328_gameport_cooked_read(struct gameport *gameport,
  				 int *axes,
  				 int *buttons
  )
  {
  	struct snd_azf3328 *chip = gameport_get_port_data(gameport);
  	int i;
  	u8 val;
  	unsigned long flags;
da3cec35d   Takashi Iwai   ALSA: Kill snd_as...
1710
1711
  	if (snd_BUG_ON(!chip))
  		return 0;
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
  
  	spin_lock_irqsave(&chip->reg_lock, flags);
  	val = snd_azf3328_game_inb(chip, IDX_GAME_LEGACY_COMPATIBLE);
  	*buttons = (~(val) >> 4) & 0xf;
  
  	/* ok, this one is a bit dirty: cooked_read is being polled by a timer,
  	 * thus we're atomic and cannot actively wait in here
  	 * (which would be useful for us since it probably would be better
  	 * to trigger a measurement in here, then wait a short amount of
  	 * time until it's finished, then read values of _this_ measurement).
  	 *
  	 * Thus we simply resort to reading values if they're available already
  	 * and trigger the next measurement.
  	 */
  
  	val = snd_azf3328_game_inb(chip, IDX_GAME_AXES_CONFIG);
  	if (val & GAME_AXES_SAMPLING_READY) {
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1729
  		for (i = 0; i < ARRAY_SIZE(chip->axes); ++i) {
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
1730
1731
1732
1733
1734
1735
1736
1737
1738
  			/* configure the axis to read */
  			val = (i << 4) | 0x0f;
  			snd_azf3328_game_outb(chip, IDX_GAME_AXES_CONFIG, val);
  
  			chip->axes[i] = snd_azf3328_game_inw(
  						chip, IDX_GAME_AXIS_VALUE
  					);
  		}
  	}
adf5931f8   Andreas Mohr   ALSA: azt3328: co...
1739
  	/* trigger next sampling of axes, to be evaluated the next time we
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
  	 * enter this function */
  
  	/* for some very, very strange reason we cannot enable
  	 * Measurement Ready monitoring for all axes here,
  	 * at least not when only one joystick connected */
  	val = 0x03; /* we're able to monitor axes 1 and 2 only */
  	snd_azf3328_game_outb(chip, IDX_GAME_AXES_CONFIG, val);
  
  	snd_azf3328_game_outw(chip, IDX_GAME_AXIS_VALUE, 0xffff);
  	spin_unlock_irqrestore(&chip->reg_lock, flags);
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1750
  	for (i = 0; i < ARRAY_SIZE(chip->axes); i++) {
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
1751
1752
1753
1754
  		axes[i] = chip->axes[i];
  		if (axes[i] == 0xffff)
  			axes[i] = -1;
  	}
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
1755
1756
1757
  	dev_dbg(chip->card->dev, "cooked_read: axes %d %d %d %d buttons %d
  ",
  		axes[0], axes[1], axes[2], axes[3], *buttons);
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
1758
1759
1760
  
  	return 0;
  }
e23e7a143   Bill Pemberton   ALSA: pci: remove...
1761
  static int
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
1762
1763
1764
  snd_azf3328_gameport(struct snd_azf3328 *chip, int dev)
  {
  	struct gameport *gp;
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
1765
1766
  	chip->gameport = gp = gameport_allocate_port();
  	if (!gp) {
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
1767
1768
  		dev_err(chip->card->dev, "cannot alloc memory for gameport
  ");
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
1769
1770
1771
1772
1773
1774
  		return -ENOMEM;
  	}
  
  	gameport_set_name(gp, "AZF3328 Gameport");
  	gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
  	gameport_set_dev_parent(gp, &chip->pci->dev);
627d3e7ab   Andreas Mohr   ALSA: PCI168 snd-...
1775
  	gp->io = chip->game_io;
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
1776
1777
1778
1779
1780
1781
1782
1783
1784
  	gameport_set_port_data(gp, chip);
  
  	gp->open = snd_azf3328_gameport_open;
  	gp->close = snd_azf3328_gameport_close;
  	gp->fuzz = 16; /* seems ok */
  	gp->cooked_read = snd_azf3328_gameport_cooked_read;
  
  	/* DISABLE legacy address: we don't need it! */
  	snd_azf3328_gameport_legacy_address_enable(chip, 0);
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1785
1786
  	snd_azf3328_gameport_set_counter_frequency(chip,
  				GAME_HWCFG_ADC_COUNTER_FREQ_1_200);
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
  	snd_azf3328_gameport_axis_circuit_enable(chip, 0);
  
  	gameport_register_port(chip->gameport);
  
  	return 0;
  }
  
  static void
  snd_azf3328_gameport_free(struct snd_azf3328 *chip)
  {
  	if (chip->gameport) {
  		gameport_unregister_port(chip->gameport);
  		chip->gameport = NULL;
  	}
  	snd_azf3328_gameport_irq_enable(chip, 0);
  }
  #else
  static inline int
  snd_azf3328_gameport(struct snd_azf3328 *chip, int dev) { return -ENOSYS; }
  static inline void
  snd_azf3328_gameport_free(struct snd_azf3328 *chip) { }
  static inline void
  snd_azf3328_gameport_interrupt(struct snd_azf3328 *chip)
  {
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
1811
1812
  	dev_warn(chip->card->dev, "huh, game port IRQ occurred!?
  ");
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
1813
1814
1815
1816
  }
  #endif /* SUPPORT_GAMEPORT */
  
  /******************************************************************/
627d3e7ab   Andreas Mohr   ALSA: PCI168 snd-...
1817
  static inline void
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
1818
  snd_azf3328_irq_log_unknown_type(struct snd_azf3328 *chip, u8 which)
627d3e7ab   Andreas Mohr   ALSA: PCI168 snd-...
1819
  {
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
1820
1821
1822
1823
  	dev_dbg(chip->card->dev,
  		"unknown IRQ type (%x) occurred, please report!
  ",
  		which);
627d3e7ab   Andreas Mohr   ALSA: PCI168 snd-...
1824
  }
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1825
  static inline void
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
1826
1827
  snd_azf3328_pcm_interrupt(struct snd_azf3328 *chip,
  			  const struct snd_azf3328_codec_data *first_codec,
da237f35a   Andreas Mohr   ALSA: azt3328: us...
1828
1829
  			  u8 status
  )
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1830
1831
1832
  {
  	u8 which;
  	enum snd_azf3328_codec_type codec_type;
da237f35a   Andreas Mohr   ALSA: azt3328: us...
1833
  	const struct snd_azf3328_codec_data *codec = first_codec;
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1834
1835
1836
  
  	for (codec_type = AZF_CODEC_PLAYBACK;
  		 codec_type <= AZF_CODEC_I2S_OUT;
da237f35a   Andreas Mohr   ALSA: azt3328: us...
1837
  			 ++codec_type, ++codec) {
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1838
1839
1840
1841
  
  		/* skip codec if there's no interrupt for it */
  		if (!(status & (1 << codec_type)))
  			continue;
da237f35a   Andreas Mohr   ALSA: azt3328: us...
1842
  		spin_lock(codec->lock);
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1843
1844
1845
  		which = snd_azf3328_codec_inb(codec, IDX_IO_CODEC_IRQTYPE);
  		/* ack all IRQ types immediately */
  		snd_azf3328_codec_outb(codec, IDX_IO_CODEC_IRQTYPE, which);
da237f35a   Andreas Mohr   ALSA: azt3328: us...
1846
  		spin_unlock(codec->lock);
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1847

da237f35a   Andreas Mohr   ALSA: azt3328: us...
1848
  		if (codec->substream) {
78df617ac   Andreas Mohr   ALSA: azt3328: fi...
1849
  			snd_pcm_period_elapsed(codec->substream);
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
1850
1851
  			dev_dbg(chip->card->dev, "%s period done (#%x), @ %x
  ",
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1852
1853
1854
  				codec->name,
  				which,
  				snd_azf3328_codec_inl(
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
1855
  					codec, IDX_IO_CODEC_DMA_CURRPOS));
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1856
  		} else
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
1857
1858
  			dev_warn(chip->card->dev, "irq handler problem!
  ");
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1859
  		if (which & IRQ_SOMETHING)
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
1860
  			snd_azf3328_irq_log_unknown_type(chip, which);
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1861
1862
  	}
  }
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
1863
  static irqreturn_t
7d12e780e   David Howells   IRQ: Maintain reg...
1864
  snd_azf3328_interrupt(int irq, void *dev_id)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1865
  {
95de77660   Takashi Iwai   [ALSA] Remove xxx...
1866
  	struct snd_azf3328 *chip = dev_id;
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1867
  	u8 status;
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
1868
  	static unsigned long irq_count;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1869

dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1870
  	status = snd_azf3328_ctrl_inb(chip, IDX_IO_IRQSTATUS);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1871
1872
  
          /* fast path out, to ease interrupt sharing */
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
1873
  	if (!(status &
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1874
1875
  		(IRQ_PLAYBACK|IRQ_RECORDING|IRQ_I2S_OUT
  		|IRQ_GAMEPORT|IRQ_MPU401|IRQ_TIMER)
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
1876
  	))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1877
  		return IRQ_NONE; /* must be interrupt for another device */
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
1878
  	dev_dbg(chip->card->dev,
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1879
1880
  		"irq_count %ld! IDX_IO_IRQSTATUS %04x
  ",
627d3e7ab   Andreas Mohr   ALSA: PCI168 snd-...
1881
  			irq_count++ /* debug-only */,
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
1882
  			status);
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
1883

e2f872608   Andreas Mohr   [ALSA] azt3328.c:...
1884
  	if (status & IRQ_TIMER) {
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
1885
1886
  		/* dev_dbg(chip->card->dev, "timer %ld
  ",
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
1887
1888
1889
  			snd_azf3328_codec_inl(chip, IDX_IO_TIMER_VALUE)
  				& TIMER_VALUE_MASK
  		); */
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
1890
1891
1892
1893
  		if (chip->timer)
  			snd_timer_interrupt(chip->timer, chip->timer->sticks);
  		/* ACK timer */
                  spin_lock(&chip->reg_lock);
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1894
  		snd_azf3328_ctrl_outb(chip, IDX_IO_TIMER_VALUE + 3, 0x07);
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
1895
  		spin_unlock(&chip->reg_lock);
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
1896
1897
  		dev_dbg(chip->card->dev, "timer IRQ
  ");
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
1898
  	}
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
1899

dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1900
  	if (status & (IRQ_PLAYBACK|IRQ_RECORDING|IRQ_I2S_OUT))
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
1901
  		snd_azf3328_pcm_interrupt(chip, chip->codecs, status);
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
1902

02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
1903
1904
  	if (status & IRQ_GAMEPORT)
  		snd_azf3328_gameport_interrupt(chip);
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1905

d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
1906
1907
  	/* MPU401 has less critical IRQ requirements
  	 * than timer and playback/recording, right? */
e2f872608   Andreas Mohr   [ALSA] azt3328.c:...
1908
  	if (status & IRQ_MPU401) {
7d12e780e   David Howells   IRQ: Maintain reg...
1909
  		snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
1910
1911
  
  		/* hmm, do we have to ack the IRQ here somehow?
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1912
  		 * If so, then I don't know how yet... */
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
1913
1914
  		dev_dbg(chip->card->dev, "MPU401 IRQ
  ");
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
1915
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1916
1917
1918
1919
  	return IRQ_HANDLED;
  }
  
  /*****************************************************************/
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1920
1921
1922
1923
1924
  /* as long as we think we have identical snd_pcm_hardware parameters
     for playback, capture and i2s out, we can use the same physical struct
     since the struct is simply being copied into a member.
  */
  static const struct snd_pcm_hardware snd_azf3328_hardware =
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1925
1926
  {
  	/* FIXME!! Correct? */
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
  	.info =			SNDRV_PCM_INFO_MMAP |
  				SNDRV_PCM_INFO_INTERLEAVED |
  				SNDRV_PCM_INFO_MMAP_VALID,
  	.formats =		SNDRV_PCM_FMTBIT_S8 |
  				SNDRV_PCM_FMTBIT_U8 |
  				SNDRV_PCM_FMTBIT_S16_LE |
  				SNDRV_PCM_FMTBIT_U16_LE,
  	.rates =		SNDRV_PCM_RATE_5512 |
  				SNDRV_PCM_RATE_8000_48000 |
  				SNDRV_PCM_RATE_KNOT,
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
1937
1938
  	.rate_min =		AZF_FREQ_4000,
  	.rate_max =		AZF_FREQ_66200,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1939
1940
  	.channels_min =		1,
  	.channels_max =		2,
7974150c8   Andreas Mohr   ALSA: azt3328: pe...
1941
1942
1943
1944
1945
1946
1947
1948
1949
  	.buffer_bytes_max =	(64*1024),
  	.period_bytes_min =	1024,
  	.period_bytes_max =	(32*1024),
  	/* We simply have two DMA areas (instead of a list of descriptors
  	   such as other cards); I believe that this is a fixed hardware
  	   attribute and there isn't much driver magic to be done to expand it.
  	   Thus indicate that we have at least and at most 2 periods. */
  	.periods_min =		2,
  	.periods_max =		2,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1950
1951
1952
1953
1954
  	/* FIXME: maybe that card actually has a FIFO?
  	 * Hmm, it seems newer revisions do have one, but we still don't know
  	 * its size... */
  	.fifo_size =		0,
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1955

3e2fd04f8   Takashi Iwai   ALSA: azt3328: Co...
1956
  static const unsigned int snd_azf3328_fixed_rates[] = {
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
  	AZF_FREQ_4000,
  	AZF_FREQ_4800,
  	AZF_FREQ_5512,
  	AZF_FREQ_6620,
  	AZF_FREQ_8000,
  	AZF_FREQ_9600,
  	AZF_FREQ_11025,
  	AZF_FREQ_13240,
  	AZF_FREQ_16000,
  	AZF_FREQ_22050,
  	AZF_FREQ_32000,
  	AZF_FREQ_44100,
  	AZF_FREQ_48000,
  	AZF_FREQ_66200
  };
3e2fd04f8   Takashi Iwai   ALSA: azt3328: Co...
1972
  static const struct snd_pcm_hw_constraint_list snd_azf3328_hw_constraints_rates = {
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
1973
  	.count = ARRAY_SIZE(snd_azf3328_fixed_rates),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1974
1975
1976
1977
1978
  	.list = snd_azf3328_fixed_rates,
  	.mask = 0,
  };
  
  /*****************************************************************/
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
1979
  static int
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1980
1981
1982
  snd_azf3328_pcm_open(struct snd_pcm_substream *substream,
  		     enum snd_azf3328_codec_type codec_type
  )
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1983
  {
95de77660   Takashi Iwai   [ALSA] Remove xxx...
1984
1985
  	struct snd_azf3328 *chip = snd_pcm_substream_chip(substream);
  	struct snd_pcm_runtime *runtime = substream->runtime;
da237f35a   Andreas Mohr   ALSA: azt3328: us...
1986
  	struct snd_azf3328_codec_data *codec = &chip->codecs[codec_type];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1987

da237f35a   Andreas Mohr   ALSA: azt3328: us...
1988
  	codec->substream = substream;
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1989
1990
1991
  
  	/* same parameters for all our codecs - at least we think so... */
  	runtime->hw = snd_azf3328_hardware;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1992
1993
  	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  				   &snd_azf3328_hw_constraints_rates);
da237f35a   Andreas Mohr   ALSA: azt3328: us...
1994
  	runtime->private_data = codec;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1995
1996
  	return 0;
  }
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
1997
  static int
da237f35a   Andreas Mohr   ALSA: azt3328: us...
1998
  snd_azf3328_pcm_playback_open(struct snd_pcm_substream *substream)
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
1999
2000
2001
2002
2003
  {
  	return snd_azf3328_pcm_open(substream, AZF_CODEC_PLAYBACK);
  }
  
  static int
da237f35a   Andreas Mohr   ALSA: azt3328: us...
2004
  snd_azf3328_pcm_capture_open(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2005
  {
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
2006
2007
  	return snd_azf3328_pcm_open(substream, AZF_CODEC_CAPTURE);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2008

dfbf95111   Andreas Mohr   ALSA: azt3328: la...
2009
  static int
da237f35a   Andreas Mohr   ALSA: azt3328: us...
2010
  snd_azf3328_pcm_i2s_out_open(struct snd_pcm_substream *substream)
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
2011
2012
  {
  	return snd_azf3328_pcm_open(substream, AZF_CODEC_I2S_OUT);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2013
  }
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
2014
  static int
da237f35a   Andreas Mohr   ALSA: azt3328: us...
2015
  snd_azf3328_pcm_close(struct snd_pcm_substream *substream
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
2016
  )
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2017
  {
da237f35a   Andreas Mohr   ALSA: azt3328: us...
2018
2019
  	struct snd_azf3328_codec_data *codec =
  		substream->runtime->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2020

da237f35a   Andreas Mohr   ALSA: azt3328: us...
2021
  	codec->substream = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2022
2023
  	return 0;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2024
  /******************************************************************/
6769e988b   Julia Lawall   ALSA: constify sn...
2025
  static const struct snd_pcm_ops snd_azf3328_playback_ops = {
da237f35a   Andreas Mohr   ALSA: azt3328: us...
2026
2027
  	.open =		snd_azf3328_pcm_playback_open,
  	.close =	snd_azf3328_pcm_close,
da237f35a   Andreas Mohr   ALSA: azt3328: us...
2028
2029
2030
  	.prepare =	snd_azf3328_pcm_prepare,
  	.trigger =	snd_azf3328_pcm_trigger,
  	.pointer =	snd_azf3328_pcm_pointer
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2031
  };
6769e988b   Julia Lawall   ALSA: constify sn...
2032
  static const struct snd_pcm_ops snd_azf3328_capture_ops = {
da237f35a   Andreas Mohr   ALSA: azt3328: us...
2033
2034
  	.open =		snd_azf3328_pcm_capture_open,
  	.close =	snd_azf3328_pcm_close,
da237f35a   Andreas Mohr   ALSA: azt3328: us...
2035
2036
2037
  	.prepare =	snd_azf3328_pcm_prepare,
  	.trigger =	snd_azf3328_pcm_trigger,
  	.pointer =	snd_azf3328_pcm_pointer
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
2038
  };
6769e988b   Julia Lawall   ALSA: constify sn...
2039
  static const struct snd_pcm_ops snd_azf3328_i2s_out_ops = {
da237f35a   Andreas Mohr   ALSA: azt3328: us...
2040
2041
  	.open =		snd_azf3328_pcm_i2s_out_open,
  	.close =	snd_azf3328_pcm_close,
da237f35a   Andreas Mohr   ALSA: azt3328: us...
2042
2043
2044
  	.prepare =	snd_azf3328_pcm_prepare,
  	.trigger =	snd_azf3328_pcm_trigger,
  	.pointer =	snd_azf3328_pcm_pointer
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2045
  };
e23e7a143   Bill Pemberton   ALSA: pci: remove...
2046
  static int
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
2047
  snd_azf3328_pcm(struct snd_azf3328 *chip)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2048
  {
7f788e0cc   Dan Carpenter   ALSA: azt3328: so...
2049
2050
  	/* pcm devices */
  	enum { AZF_PCMDEV_STD, AZF_PCMDEV_I2S_OUT, NUM_AZF_PCMDEVS };
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
2051

95de77660   Takashi Iwai   [ALSA] Remove xxx...
2052
  	struct snd_pcm *pcm;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2053
  	int err;
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
2054
2055
2056
  	err = snd_pcm_new(chip->card, "AZF3328 DSP", AZF_PCMDEV_STD,
  								1, 1, &pcm);
  	if (err < 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2057
  		return err;
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
2058
2059
2060
2061
  	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  						&snd_azf3328_playback_ops);
  	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
  						&snd_azf3328_capture_ops);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2062
2063
  
  	pcm->private_data = chip;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2064
2065
  	pcm->info_flags = 0;
  	strcpy(pcm->name, chip->card->shortname);
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
2066
2067
2068
  	/* same pcm object for playback/capture (see snd_pcm_new() above) */
  	chip->pcm[AZF_CODEC_PLAYBACK] = pcm;
  	chip->pcm[AZF_CODEC_CAPTURE] = pcm;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2069

830e7b007   Takashi Iwai   ALSA: azt3328: Us...
2070
2071
  	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, &chip->pci->dev,
  				       64*1024, 64*1024);
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
  
  	err = snd_pcm_new(chip->card, "AZF3328 I2S OUT", AZF_PCMDEV_I2S_OUT,
  								1, 0, &pcm);
  	if (err < 0)
  		return err;
  	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  						&snd_azf3328_i2s_out_ops);
  
  	pcm->private_data = chip;
  	pcm->info_flags = 0;
  	strcpy(pcm->name, chip->card->shortname);
  	chip->pcm[AZF_CODEC_I2S_OUT] = pcm;
830e7b007   Takashi Iwai   ALSA: azt3328: Us...
2084
2085
  	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, &chip->pci->dev,
  				       64*1024, 64*1024);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2086

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2087
2088
2089
2090
  	return 0;
  }
  
  /******************************************************************/
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
2091
2092
  /*** NOTE: the physical timer resolution actually is 1024000 ticks per second
   *** (probably derived from main crystal via a divider of 24),
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
2093
2094
2095
2096
2097
2098
2099
2100
2101
   *** but announcing those attributes to user-space would make programs
   *** configure the timer to a 1 tick value, resulting in an absolutely fatal
   *** timer IRQ storm.
   *** Thus I chose to announce a down-scaled virtual timer to the outside and
   *** calculate real timer countdown values internally.
   *** (the scale factor can be set via module parameter "seqtimer_scaling").
   ***/
  
  static int
95de77660   Takashi Iwai   [ALSA] Remove xxx...
2102
  snd_azf3328_timer_start(struct snd_timer *timer)
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
2103
  {
95de77660   Takashi Iwai   [ALSA] Remove xxx...
2104
  	struct snd_azf3328 *chip;
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
2105
2106
  	unsigned long flags;
  	unsigned int delay;
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
2107
2108
  	chip = snd_timer_chip(timer);
  	delay = ((timer->sticks * seqtimer_scaling) - 1) & TIMER_VALUE_MASK;
e2f872608   Andreas Mohr   [ALSA] azt3328.c:...
2109
  	if (delay < 49) {
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
2110
2111
2112
  		/* uhoh, that's not good, since user-space won't know about
  		 * this timing tweak
  		 * (we need to do it to avoid a lockup, though) */
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
2113
2114
  		dev_dbg(chip->card->dev, "delay was too low (%d)!
  ", delay);
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
2115
2116
  		delay = 49; /* minimum time is 49 ticks */
  	}
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
2117
2118
  	dev_dbg(chip->card->dev, "setting timer countdown value %d
  ", delay);
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
2119
  	delay |= TIMER_COUNTDOWN_ENABLE | TIMER_IRQ_ENABLE;
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
2120
  	spin_lock_irqsave(&chip->reg_lock, flags);
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
2121
  	snd_azf3328_ctrl_outl(chip, IDX_IO_TIMER_VALUE, delay);
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
2122
  	spin_unlock_irqrestore(&chip->reg_lock, flags);
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
2123
2124
2125
2126
  	return 0;
  }
  
  static int
95de77660   Takashi Iwai   [ALSA] Remove xxx...
2127
  snd_azf3328_timer_stop(struct snd_timer *timer)
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
2128
  {
95de77660   Takashi Iwai   [ALSA] Remove xxx...
2129
  	struct snd_azf3328 *chip;
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
2130
  	unsigned long flags;
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
2131
2132
2133
  	chip = snd_timer_chip(timer);
  	spin_lock_irqsave(&chip->reg_lock, flags);
  	/* disable timer countdown and interrupt */
7974150c8   Andreas Mohr   ALSA: azt3328: pe...
2134
2135
2136
2137
2138
2139
2140
  	/* Hmm, should we write TIMER_IRQ_ACK here?
  	   YES indeed, otherwise a rogue timer operation - which prompts
  	   ALSA(?) to call repeated stop() in vain, but NOT start() -
  	   will never end (value 0x03 is kept shown in control byte).
  	   Simply manually poking 0x04 _once_ immediately successfully stops
  	   the hardware/ALSA interrupt activity. */
  	snd_azf3328_ctrl_outb(chip, IDX_IO_TIMER_VALUE + 3, 0x04);
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
2141
  	spin_unlock_irqrestore(&chip->reg_lock, flags);
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
2142
2143
2144
2145
2146
  	return 0;
  }
  
  
  static int
95de77660   Takashi Iwai   [ALSA] Remove xxx...
2147
  snd_azf3328_timer_precise_resolution(struct snd_timer *timer,
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
2148
2149
  					       unsigned long *num, unsigned long *den)
  {
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
2150
2151
  	*num = 1;
  	*den = 1024000 / seqtimer_scaling;
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
2152
2153
  	return 0;
  }
95de77660   Takashi Iwai   [ALSA] Remove xxx...
2154
  static struct snd_timer_hardware snd_azf3328_timer_hw = {
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
2155
2156
2157
2158
2159
2160
2161
  	.flags = SNDRV_TIMER_HW_AUTO,
  	.resolution = 977, /* 1000000/1024000 = 0.9765625us */
  	.ticks = 1024000, /* max tick count, defined by the value register; actually it's not 1024000, but 1048576, but we don't care */
  	.start = snd_azf3328_timer_start,
  	.stop = snd_azf3328_timer_stop,
  	.precise_resolution = snd_azf3328_timer_precise_resolution,
  };
e23e7a143   Bill Pemberton   ALSA: pci: remove...
2162
  static int
95de77660   Takashi Iwai   [ALSA] Remove xxx...
2163
  snd_azf3328_timer(struct snd_azf3328 *chip, int device)
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
2164
  {
95de77660   Takashi Iwai   [ALSA] Remove xxx...
2165
2166
  	struct snd_timer *timer = NULL;
  	struct snd_timer_id tid;
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
2167
  	int err;
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
2168
2169
2170
2171
2172
2173
2174
2175
  	tid.dev_class = SNDRV_TIMER_CLASS_CARD;
  	tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
  	tid.card = chip->card->number;
  	tid.device = device;
  	tid.subdevice = 0;
  
  	snd_azf3328_timer_hw.resolution *= seqtimer_scaling;
  	snd_azf3328_timer_hw.ticks /= seqtimer_scaling;
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
2176
2177
2178
  
  	err = snd_timer_new(chip->card, "AZF3328", &tid, &timer);
  	if (err < 0)
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
2179
  		goto out;
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
2180
2181
2182
2183
2184
2185
  
  	strcpy(timer->name, "AZF3328 timer");
  	timer->private_data = chip;
  	timer->hw = snd_azf3328_timer_hw;
  
  	chip->timer = timer;
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
2186
  	snd_azf3328_timer_stop(timer);
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
2187
2188
2189
  	err = 0;
  
  out:
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
2190
2191
2192
2193
  	return err;
  }
  
  /******************************************************************/
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
2194
2195
2196
2197
2198
  static int
  snd_azf3328_free(struct snd_azf3328 *chip)
  {
  	if (chip->irq < 0)
  		goto __end_hw;
b5dc20cd2   Andreas Mohr   ALSA: azt3328: ad...
2199
  	snd_azf3328_mixer_reset(chip);
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
2200
2201
2202
  
  	snd_azf3328_timer_stop(chip->timer);
  	snd_azf3328_gameport_free(chip);
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
  __end_hw:
  	if (chip->irq >= 0)
  		free_irq(chip->irq, chip);
  	pci_release_regions(chip->pci);
  	pci_disable_device(chip->pci);
  
  	kfree(chip);
  	return 0;
  }
  
  static int
  snd_azf3328_dev_free(struct snd_device *device)
  {
  	struct snd_azf3328 *chip = device->device_data;
  	return snd_azf3328_free(chip);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2219
2220
  #if 0
  /* check whether a bit can be modified */
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
2221
  static void
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
2222
  snd_azf3328_test_bit(unsigned unsigned reg, int bit)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
  {
  	unsigned char val, valoff, valon;
  
  	val = inb(reg);
  
  	outb(val & ~(1 << bit), reg);
  	valoff = inb(reg);
  
  	outb(val|(1 << bit), reg);
  	valon = inb(reg);
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
2233

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2234
  	outb(val, reg);
78df617ac   Andreas Mohr   ALSA: azt3328: fi...
2235
2236
  	printk(KERN_DEBUG "reg %04x bit %d: %02x %02x %02x
  ",
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
2237
2238
  				reg, bit, val, valoff, valon
  	);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2239
2240
  }
  #endif
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
2241
  static inline void
95de77660   Takashi Iwai   [ALSA] Remove xxx...
2242
  snd_azf3328_debug_show_ports(const struct snd_azf3328 *chip)
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
2243
  {
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
2244
  	u16 tmp;
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
2245
  	dev_dbg(chip->card->dev,
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
2246
  		"ctrl_io 0x%lx, game_io 0x%lx, mpu_io 0x%lx, "
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
2247
2248
  		"opl3_io 0x%lx, mixer_io 0x%lx, irq %d
  ",
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
2249
  		chip->ctrl_io, chip->game_io, chip->mpu_io,
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
2250
  		chip->opl3_io, chip->mixer_io, chip->irq);
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
2251

4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
2252
2253
2254
  	dev_dbg(chip->card->dev,
  		"game %02x %02x %02x %02x %02x %02x
  ",
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
2255
2256
2257
2258
2259
  		snd_azf3328_game_inb(chip, 0),
  		snd_azf3328_game_inb(chip, 1),
  		snd_azf3328_game_inb(chip, 2),
  		snd_azf3328_game_inb(chip, 3),
  		snd_azf3328_game_inb(chip, 4),
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
2260
  		snd_azf3328_game_inb(chip, 5));
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
2261
2262
  
  	for (tmp = 0; tmp < 0x07; tmp += 1)
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
2263
2264
2265
  		dev_dbg(chip->card->dev,
  			"mpu_io 0x%04x
  ", inb(chip->mpu_io + tmp));
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
2266
2267
  
  	for (tmp = 0; tmp <= 0x07; tmp += 1)
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
2268
2269
2270
  		dev_dbg(chip->card->dev,
  			"0x%02x: game200 0x%04x, game208 0x%04x
  ",
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
2271
2272
2273
  			tmp, inb(0x200 + tmp), inb(0x208 + tmp));
  
  	for (tmp = 0; tmp <= 0x01; tmp += 1)
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
2274
  		dev_dbg(chip->card->dev,
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
2275
2276
2277
2278
2279
2280
2281
2282
2283
  			"0x%02x: mpu300 0x%04x, mpu310 0x%04x, mpu320 0x%04x, "
  			"mpu330 0x%04x opl388 0x%04x opl38c 0x%04x
  ",
  				tmp,
  				inb(0x300 + tmp),
  				inb(0x310 + tmp),
  				inb(0x320 + tmp),
  				inb(0x330 + tmp),
  				inb(0x388 + tmp),
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
2284
  				inb(0x38c + tmp));
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
2285

dfbf95111   Andreas Mohr   ALSA: azt3328: la...
2286
  	for (tmp = 0; tmp < AZF_IO_SIZE_CTRL; tmp += 2)
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
2287
2288
2289
2290
  		dev_dbg(chip->card->dev,
  			"ctrl 0x%02x: 0x%04x
  ",
  			tmp, snd_azf3328_ctrl_inw(chip, tmp));
e24a121aa   Andreas Mohr   [ALSA] azt3328.c:...
2291
2292
  
  	for (tmp = 0; tmp < AZF_IO_SIZE_MIXER; tmp += 2)
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
2293
2294
2295
2296
  		dev_dbg(chip->card->dev,
  			"mixer 0x%02x: 0x%04x
  ",
  			tmp, snd_azf3328_mixer_inw(chip, tmp));
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
2297
  }
e23e7a143   Bill Pemberton   ALSA: pci: remove...
2298
  static int
95de77660   Takashi Iwai   [ALSA] Remove xxx...
2299
  snd_azf3328_create(struct snd_card *card,
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
2300
2301
2302
  		   struct pci_dev *pci,
  		   unsigned long device_type,
  		   struct snd_azf3328 **rchip)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2303
  {
95de77660   Takashi Iwai   [ALSA] Remove xxx...
2304
  	struct snd_azf3328 *chip;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2305
  	int err;
efb0ad25d   Takashi Iwai   ALSA: pci: Consti...
2306
  	static const struct snd_device_ops ops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2307
2308
  		.dev_free =     snd_azf3328_dev_free,
  	};
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
2309
2310
  	u8 dma_init;
  	enum snd_azf3328_codec_type codec_type;
da237f35a   Andreas Mohr   ALSA: azt3328: us...
2311
  	struct snd_azf3328_codec_data *codec_setup;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2312
2313
  
  	*rchip = NULL;
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
2314
2315
  	err = pci_enable_device(pci);
  	if (err < 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2316
  		return err;
e560d8d83   Takashi Iwai   [ALSA] Replace wi...
2317
  	chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2318
  	if (chip == NULL) {
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
2319
2320
  		err = -ENOMEM;
  		goto out_err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2321
2322
2323
2324
2325
2326
2327
  	}
  	spin_lock_init(&chip->reg_lock);
  	chip->card = card;
  	chip->pci = pci;
  	chip->irq = -1;
  
  	/* check if we can restrict PCI DMA transfers to 24 bits */
412b979cc   Quentin Lambert   ALSA: remove depr...
2328
2329
  	if (dma_set_mask(&pci->dev, DMA_BIT_MASK(24)) < 0 ||
  	    dma_set_coherent_mask(&pci->dev, DMA_BIT_MASK(24)) < 0) {
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
2330
2331
2332
  		dev_err(card->dev,
  			"architecture does not support 24bit PCI busmaster DMA
  "
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
2333
  		);
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
2334
2335
  		err = -ENXIO;
  		goto out_err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2336
  	}
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
2337
2338
  	err = pci_request_regions(pci, "Aztech AZF3328");
  	if (err < 0)
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
2339
  		goto out_err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2340

dfbf95111   Andreas Mohr   ALSA: azt3328: la...
2341
  	chip->ctrl_io  = pci_resource_start(pci, 0);
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
2342
2343
  	chip->game_io  = pci_resource_start(pci, 1);
  	chip->mpu_io   = pci_resource_start(pci, 2);
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
2344
  	chip->opl3_io  = pci_resource_start(pci, 3);
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
2345
  	chip->mixer_io = pci_resource_start(pci, 4);
9fd8d36ca   Andreas Mohr   ALSA: azt3328: co...
2346
2347
  	codec_setup = &chip->codecs[AZF_CODEC_PLAYBACK];
  	codec_setup->io_base = chip->ctrl_io + AZF_IO_OFFS_CODEC_PLAYBACK;
da237f35a   Andreas Mohr   ALSA: azt3328: us...
2348
2349
  	codec_setup->lock = &chip->reg_lock;
  	codec_setup->type = AZF_CODEC_PLAYBACK;
9fd8d36ca   Andreas Mohr   ALSA: azt3328: co...
2350
2351
2352
2353
  	codec_setup->name = "PLAYBACK";
  
  	codec_setup = &chip->codecs[AZF_CODEC_CAPTURE];
  	codec_setup->io_base = chip->ctrl_io + AZF_IO_OFFS_CODEC_CAPTURE;
da237f35a   Andreas Mohr   ALSA: azt3328: us...
2354
2355
  	codec_setup->lock = &chip->reg_lock;
  	codec_setup->type = AZF_CODEC_CAPTURE;
9fd8d36ca   Andreas Mohr   ALSA: azt3328: co...
2356
2357
2358
2359
  	codec_setup->name = "CAPTURE";
  
  	codec_setup = &chip->codecs[AZF_CODEC_I2S_OUT];
  	codec_setup->io_base = chip->ctrl_io + AZF_IO_OFFS_CODEC_I2S_OUT;
da237f35a   Andreas Mohr   ALSA: azt3328: us...
2360
2361
  	codec_setup->lock = &chip->reg_lock;
  	codec_setup->type = AZF_CODEC_I2S_OUT;
9fd8d36ca   Andreas Mohr   ALSA: azt3328: co...
2362
  	codec_setup->name = "I2S_OUT";
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2363

437a5a460   Takashi Iwai   [ALSA] Remove IRQ...
2364
  	if (request_irq(pci->irq, snd_azf3328_interrupt,
934c2b6d0   Takashi Iwai   ALSA: use KBUILD_...
2365
  			IRQF_SHARED, KBUILD_MODNAME, chip)) {
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
2366
2367
  		dev_err(card->dev, "unable to grab IRQ %d
  ", pci->irq);
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
2368
2369
  		err = -EBUSY;
  		goto out_err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2370
2371
  	}
  	chip->irq = pci->irq;
aefd1860a   Takashi Iwai   ALSA: azt3328: Su...
2372
  	card->sync_irq = chip->irq;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2373
  	pci_set_master(pci);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2374

d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
2375
  	snd_azf3328_debug_show_ports(chip);
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
2376
2377
2378
  
  	err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
  	if (err < 0)
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
2379
  		goto out_err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2380
2381
  
  	/* create mixer interface & switches */
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
2382
2383
  	err = snd_azf3328_mixer_new(chip);
  	if (err < 0)
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
2384
  		goto out_err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2385

dfbf95111   Andreas Mohr   ALSA: azt3328: la...
2386
2387
2388
2389
2390
2391
2392
2393
  	/* standard codec init stuff */
  		/* default DMA init value */
  	dma_init = DMA_RUN_SOMETHING2|DMA_EPILOGUE_SOMETHING|DMA_SOMETHING_ELSE;
  
  	for (codec_type = AZF_CODEC_PLAYBACK;
  		codec_type <= AZF_CODEC_I2S_OUT; ++codec_type) {
  		struct snd_azf3328_codec_data *codec =
  			 &chip->codecs[codec_type];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2394

adf5931f8   Andreas Mohr   ALSA: azt3328: co...
2395
  		/* shutdown codecs to reduce power / noise */
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
2396
2397
2398
  			/* have ...ctrl_codec_activity() act properly */
  		codec->running = 1;
  		snd_azf3328_ctrl_codec_activity(chip, codec_type, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2399

da237f35a   Andreas Mohr   ALSA: azt3328: us...
2400
  		spin_lock_irq(codec->lock);
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
2401
2402
  		snd_azf3328_codec_outb(codec, IDX_IO_CODEC_DMA_FLAGS,
  						 dma_init);
da237f35a   Andreas Mohr   ALSA: azt3328: us...
2403
  		spin_unlock_irq(codec->lock);
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
2404
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2405

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2406
  	*rchip = chip;
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
  
  	err = 0;
  	goto out;
  
  out_err:
  	if (chip)
  		snd_azf3328_free(chip);
  	pci_disable_device(pci);
  
  out:
  	return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2418
  }
e23e7a143   Bill Pemberton   ALSA: pci: remove...
2419
  static int
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
2420
  snd_azf3328_probe(struct pci_dev *pci, const struct pci_device_id *pci_id)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2421
2422
  {
  	static int dev;
95de77660   Takashi Iwai   [ALSA] Remove xxx...
2423
2424
2425
  	struct snd_card *card;
  	struct snd_azf3328 *chip;
  	struct snd_opl3 *opl3;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2426
  	int err;
a5a3973da   Julia Lawall   ALSA: azt3328 - a...
2427
2428
2429
2430
  	if (dev >= SNDRV_CARDS) {
  		err = -ENODEV;
  		goto out;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2431
2432
  	if (!enable[dev]) {
  		dev++;
a5a3973da   Julia Lawall   ALSA: azt3328 - a...
2433
2434
  		err = -ENOENT;
  		goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2435
  	}
60c5772b5   Takashi Iwai   ALSA: pci: Conver...
2436
2437
  	err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
  			   0, &card);
e58de7baf   Takashi Iwai   ALSA: Convert to ...
2438
  	if (err < 0)
a5a3973da   Julia Lawall   ALSA: azt3328 - a...
2439
  		goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2440
2441
2442
  
  	strcpy(card->driver, "AZF3328");
  	strcpy(card->shortname, "Aztech AZF3328 (PCI168)");
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
2443
2444
  	err = snd_azf3328_create(card, pci, pci_id->driver_data, &chip);
  	if (err < 0)
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
2445
  		goto out_err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2446

ca54bde36   Andreas Mohr   [ALSA] azt3328.c:...
2447
  	card->private_data = chip;
78df617ac   Andreas Mohr   ALSA: azt3328: fi...
2448
2449
  	/* chose to use MPU401_HW_AZT2320 ID instead of MPU401_HW_MPU401,
  	   since our hardware ought to be similar, thus use same ID. */
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
2450
  	err = snd_mpu401_uart_new(
78df617ac   Andreas Mohr   ALSA: azt3328: fi...
2451
  		card, 0,
dba8b4699   Clemens Ladisch   ALSA: mpu401: cle...
2452
2453
2454
  		MPU401_HW_AZT2320, chip->mpu_io,
  		MPU401_INFO_INTEGRATED | MPU401_INFO_IRQ_HOOK,
  		-1, &chip->rmidi
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
2455
2456
  	);
  	if (err < 0) {
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
2457
2458
  		dev_err(card->dev, "no MPU-401 device at 0x%lx?
  ",
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
2459
2460
  				chip->mpu_io
  		);
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
2461
2462
  		goto out_err;
  	}
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
2463
2464
  	err = snd_azf3328_timer(chip, 0);
  	if (err < 0)
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
2465
  		goto out_err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2466

dfbf95111   Andreas Mohr   ALSA: azt3328: la...
2467
  	err = snd_azf3328_pcm(chip);
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
2468
  	if (err < 0)
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
2469
  		goto out_err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2470

02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
2471
  	if (snd_opl3_create(card, chip->opl3_io, chip->opl3_io+2,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2472
  			    OPL3_HW_AUTO, 1, &opl3) < 0) {
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
2473
2474
  		dev_err(card->dev, "no OPL3 device at 0x%lx-0x%lx?
  ",
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
2475
2476
  			   chip->opl3_io, chip->opl3_io+2
  		);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2477
  	} else {
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
2478
2479
2480
2481
2482
2483
  		/* need to use IDs 1, 2 since ID 0 is snd_azf3328_timer above */
  		err = snd_opl3_timer_new(opl3, 1, 2);
  		if (err < 0)
  			goto out_err;
  		err = snd_opl3_hwdep_new(opl3, 0, 1, NULL);
  		if (err < 0)
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
2484
  			goto out_err;
87c9e7d70   Alban Bedel   ALSA: azt3328 - F...
2485
  		opl3->private_data = chip;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2486
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2487
  	sprintf(card->longname, "%s at 0x%lx, irq %i",
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
2488
  		card->shortname, chip->ctrl_io, chip->irq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2489

02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
2490
2491
  	err = snd_card_register(card);
  	if (err < 0)
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
2492
  		goto out_err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2493
2494
  
  #ifdef MODULE
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
  	dev_info(card->dev,
  		 "Sound driver for Aztech AZF3328-based soundcards such as PCI168.
  ");
  	dev_info(card->dev,
  		 "Hardware was completely undocumented, unfortunately.
  ");
  	dev_info(card->dev,
  		 "Feel free to contact andi AT lisas.de for bug reports etc.!
  ");
  	dev_info(card->dev,
  		 "User-scalable sequencer timer set to %dHz (1024000Hz / %d).
  ",
  		 1024000 / seqtimer_scaling, seqtimer_scaling);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2508
  #endif
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
2509
  	snd_azf3328_gameport(chip, dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2510
2511
2512
  
  	pci_set_drvdata(pci, card);
  	dev++;
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
2513
2514
  	err = 0;
  	goto out;
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
2515

d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
2516
  out_err:
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
2517
2518
  	dev_err(card->dev, "something failed, exiting
  ");
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
2519
  	snd_card_free(card);
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
2520

d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
2521
  out:
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
2522
  	return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2523
  }
e23e7a143   Bill Pemberton   ALSA: pci: remove...
2524
  static void
d91c64c82   Andreas Mohr   [ALSA] AZT3328 dr...
2525
  snd_azf3328_remove(struct pci_dev *pci)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2526
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2527
  	snd_card_free(pci_get_drvdata(pci));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2528
  }
c7561cd80   Takashi Iwai   ALSA: PCI: Replac...
2529
  #ifdef CONFIG_PM_SLEEP
78df617ac   Andreas Mohr   ALSA: azt3328: fi...
2530
  static inline void
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
2531
2532
  snd_azf3328_suspend_regs(const struct snd_azf3328 *chip,
  			 unsigned long io_addr, unsigned count, u32 *saved_regs)
78df617ac   Andreas Mohr   ALSA: azt3328: fi...
2533
2534
2535
2536
2537
  {
  	unsigned reg;
  
  	for (reg = 0; reg < count; ++reg) {
  		*saved_regs = inl(io_addr);
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
2538
2539
  		dev_dbg(chip->card->dev, "suspend: io 0x%04lx: 0x%08x
  ",
78df617ac   Andreas Mohr   ALSA: azt3328: fi...
2540
2541
2542
2543
2544
  			io_addr, *saved_regs);
  		++saved_regs;
  		io_addr += sizeof(*saved_regs);
  	}
  }
b5dc20cd2   Andreas Mohr   ALSA: azt3328: ad...
2545
  static inline void
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
2546
2547
  snd_azf3328_resume_regs(const struct snd_azf3328 *chip,
  			const u32 *saved_regs,
b5dc20cd2   Andreas Mohr   ALSA: azt3328: ad...
2548
2549
2550
2551
2552
2553
2554
2555
  			unsigned long io_addr,
  			unsigned count
  )
  {
  	unsigned reg;
  
  	for (reg = 0; reg < count; ++reg) {
  		outl(*saved_regs, io_addr);
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
2556
2557
2558
  		dev_dbg(chip->card->dev,
  			"resume: io 0x%04lx: 0x%08x --> 0x%08x
  ",
b5dc20cd2   Andreas Mohr   ALSA: azt3328: ad...
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
  			io_addr, *saved_regs, inl(io_addr));
  		++saved_regs;
  		io_addr += sizeof(*saved_regs);
  	}
  }
  
  static inline void
  snd_azf3328_suspend_ac97(struct snd_azf3328 *chip)
  {
  #ifdef AZF_USE_AC97_LAYER
  	snd_ac97_suspend(chip->ac97);
  #else
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
2571
  	snd_azf3328_suspend_regs(chip, chip->mixer_io,
b5dc20cd2   Andreas Mohr   ALSA: azt3328: ad...
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
  		ARRAY_SIZE(chip->saved_regs_mixer), chip->saved_regs_mixer);
  
  	/* make sure to disable master volume etc. to prevent looping sound */
  	snd_azf3328_mixer_mute_control_master(chip, 1);
  	snd_azf3328_mixer_mute_control_pcm(chip, 1);
  #endif /* AZF_USE_AC97_LAYER */
  }
  
  static inline void
  snd_azf3328_resume_ac97(const struct snd_azf3328 *chip)
  {
  #ifdef AZF_USE_AC97_LAYER
  	snd_ac97_resume(chip->ac97);
  #else
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
2586
  	snd_azf3328_resume_regs(chip, chip->saved_regs_mixer, chip->mixer_io,
b5dc20cd2   Andreas Mohr   ALSA: azt3328: ad...
2587
2588
2589
2590
2591
2592
2593
2594
2595
  					ARRAY_SIZE(chip->saved_regs_mixer));
  
  	/* unfortunately with 32bit transfers, IDX_MIXER_PLAY_MASTER (0x02)
  	   and IDX_MIXER_RESET (offset 0x00) get touched at the same time,
  	   resulting in a mixer reset condition persisting until _after_
  	   master vol was restored. Thus master vol needs an extra restore. */
  	outw(((u16 *)chip->saved_regs_mixer)[1], chip->mixer_io + 2);
  #endif /* AZF_USE_AC97_LAYER */
  }
ca54bde36   Andreas Mohr   [ALSA] azt3328.c:...
2596
  static int
68cb2b559   Takashi Iwai   ALSA: Convert to ...
2597
  snd_azf3328_suspend(struct device *dev)
ca54bde36   Andreas Mohr   [ALSA] azt3328.c:...
2598
  {
68cb2b559   Takashi Iwai   ALSA: Convert to ...
2599
  	struct snd_card *card = dev_get_drvdata(dev);
ca54bde36   Andreas Mohr   [ALSA] azt3328.c:...
2600
  	struct snd_azf3328 *chip = card->private_data;
78df617ac   Andreas Mohr   ALSA: azt3328: fi...
2601
  	u16 *saved_regs_ctrl_u16;
ca54bde36   Andreas Mohr   [ALSA] azt3328.c:...
2602
2603
  
  	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
2604

b5dc20cd2   Andreas Mohr   ALSA: azt3328: ad...
2605
  	snd_azf3328_suspend_ac97(chip);
02330fbaa   Andreas Mohr   [ALSA] PCI168 snd...
2606

4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
2607
  	snd_azf3328_suspend_regs(chip, chip->ctrl_io,
78df617ac   Andreas Mohr   ALSA: azt3328: fi...
2608
  		ARRAY_SIZE(chip->saved_regs_ctrl), chip->saved_regs_ctrl);
627d3e7ab   Andreas Mohr   ALSA: PCI168 snd-...
2609
2610
  
  	/* manually store the one currently relevant write-only reg, too */
78df617ac   Andreas Mohr   ALSA: azt3328: fi...
2611
2612
  	saved_regs_ctrl_u16 = (u16 *)chip->saved_regs_ctrl;
  	saved_regs_ctrl_u16[IDX_IO_6AH / 2] = chip->shadow_reg_ctrl_6AH;
627d3e7ab   Andreas Mohr   ALSA: PCI168 snd-...
2613

4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
2614
  	snd_azf3328_suspend_regs(chip, chip->game_io,
78df617ac   Andreas Mohr   ALSA: azt3328: fi...
2615
  		ARRAY_SIZE(chip->saved_regs_game), chip->saved_regs_game);
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
2616
  	snd_azf3328_suspend_regs(chip, chip->mpu_io,
78df617ac   Andreas Mohr   ALSA: azt3328: fi...
2617
  		ARRAY_SIZE(chip->saved_regs_mpu), chip->saved_regs_mpu);
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
2618
  	snd_azf3328_suspend_regs(chip, chip->opl3_io,
78df617ac   Andreas Mohr   ALSA: azt3328: fi...
2619
  		ARRAY_SIZE(chip->saved_regs_opl3), chip->saved_regs_opl3);
ca54bde36   Andreas Mohr   [ALSA] azt3328.c:...
2620
2621
2622
2623
  	return 0;
  }
  
  static int
68cb2b559   Takashi Iwai   ALSA: Convert to ...
2624
  snd_azf3328_resume(struct device *dev)
ca54bde36   Andreas Mohr   [ALSA] azt3328.c:...
2625
  {
68cb2b559   Takashi Iwai   ALSA: Convert to ...
2626
  	struct snd_card *card = dev_get_drvdata(dev);
dfbf95111   Andreas Mohr   ALSA: azt3328: la...
2627
  	const struct snd_azf3328 *chip = card->private_data;
ca54bde36   Andreas Mohr   [ALSA] azt3328.c:...
2628

4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
2629
  	snd_azf3328_resume_regs(chip, chip->saved_regs_game, chip->game_io,
78df617ac   Andreas Mohr   ALSA: azt3328: fi...
2630
  					ARRAY_SIZE(chip->saved_regs_game));
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
2631
  	snd_azf3328_resume_regs(chip, chip->saved_regs_mpu, chip->mpu_io,
78df617ac   Andreas Mohr   ALSA: azt3328: fi...
2632
  					ARRAY_SIZE(chip->saved_regs_mpu));
4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
2633
  	snd_azf3328_resume_regs(chip, chip->saved_regs_opl3, chip->opl3_io,
78df617ac   Andreas Mohr   ALSA: azt3328: fi...
2634
  					ARRAY_SIZE(chip->saved_regs_opl3));
b5dc20cd2   Andreas Mohr   ALSA: azt3328: ad...
2635
  	snd_azf3328_resume_ac97(chip);
78df617ac   Andreas Mohr   ALSA: azt3328: fi...
2636

4a8d9d717   Takashi Iwai   ALSA: azt3328: Us...
2637
  	snd_azf3328_resume_regs(chip, chip->saved_regs_ctrl, chip->ctrl_io,
78df617ac   Andreas Mohr   ALSA: azt3328: fi...
2638
  					ARRAY_SIZE(chip->saved_regs_ctrl));
ca54bde36   Andreas Mohr   [ALSA] azt3328.c:...
2639
2640
2641
2642
  
  	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  	return 0;
  }
ca54bde36   Andreas Mohr   [ALSA] azt3328.c:...
2643

68cb2b559   Takashi Iwai   ALSA: Convert to ...
2644
2645
2646
2647
  static SIMPLE_DEV_PM_OPS(snd_azf3328_pm, snd_azf3328_suspend, snd_azf3328_resume);
  #define SND_AZF3328_PM_OPS	&snd_azf3328_pm
  #else
  #define SND_AZF3328_PM_OPS	NULL
c7561cd80   Takashi Iwai   ALSA: PCI: Replac...
2648
  #endif /* CONFIG_PM_SLEEP */
ca54bde36   Andreas Mohr   [ALSA] azt3328.c:...
2649

e9f66d9b9   Takashi Iwai   ALSA: pci: clean ...
2650
  static struct pci_driver azf3328_driver = {
3733e424c   Takashi Iwai   ALSA: Use KBUILD_...
2651
  	.name = KBUILD_MODNAME,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2652
2653
  	.id_table = snd_azf3328_ids,
  	.probe = snd_azf3328_probe,
e23e7a143   Bill Pemberton   ALSA: pci: remove...
2654
  	.remove = snd_azf3328_remove,
68cb2b559   Takashi Iwai   ALSA: Convert to ...
2655
2656
2657
  	.driver = {
  		.pm = SND_AZF3328_PM_OPS,
  	},
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2658
  };
e9f66d9b9   Takashi Iwai   ALSA: pci: clean ...
2659
  module_pci_driver(azf3328_driver);