azt3328.c 74.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2
/*
 *  azt3328.c - driver for Aztech AZF3328 based soundcards (e.g. PCI168).
3
 *  Copyright (C) 2002, 2005 - 2009 by Andreas Mohr <andi AT lisas.de>
L
Linus Torvalds 已提交
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!!
 *
13 14 15 16 17 18 19
 *  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 -
 *   about Linux support for this card)
 *
L
Linus Torvalds 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
 * GPL LICENSE
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.

 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 * 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).
43
 *  (and no, I did NOT go the easy way: to pick up a SB PCI128 for 9 Euros)
L
Linus Torvalds 已提交
44
 *
45 46 47
 *  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.
 *
L
Linus Torvalds 已提交
48
 *  The AZF3328 chip (note: AZF3328, *not* AZT3328, that's just the driver name
49 50 51
 *  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:
L
Linus Torvalds 已提交
52
 *
53 54 55 56 57 58
 *  - 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)
L
Linus Torvalds 已提交
59
 *  - builtin AC97 conformant codec (SNR over 80dB)
60 61 62 63 64 65 66 67 68
 *    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.
69
 *  - builtin genuine OPL3 - verified to work fine, 20080506
L
Linus Torvalds 已提交
70
 *  - full duplex 16bit playback/record at independent sampling rate
71 72
 *  - MPU401 (+ legacy address support, claimed by one official spec sheet)
 *    FIXME: how to enable legacy addr??
L
Linus Torvalds 已提交
73
 *  - game port (legacy address support)
74
 *  - builtin DirectInput support, helps reduce CPU overhead (interrupt-driven
75 76 77
 *    features supported). - See common term "Digital Enhanced Game Port"...
 *    (probably DirectInput 3.0 spec - confirm)
 *  - builtin 3D enhancement (said to be YAMAHA Ymersion)
L
Linus Torvalds 已提交
78
 *  - built-in General DirectX timer having a 20 bits counter
A
Andreas Mohr 已提交
79
 *    with 1us resolution (see below!)
80
 *  - I2S serial output port for external DAC
81
 *    [FIXME: 3.3V or 5V level? maximum rate is 66.2kHz right?]
L
Linus Torvalds 已提交
82 83 84
 *  - 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)
85
 *  - supports programmable Sub-vendor and Sub-system ID [24C02 SEEPROM chip]
L
Linus Torvalds 已提交
86
 *    required for Microsoft's logo compliance (FIXME: where?)
87 88 89 90 91
 *    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??
L
Linus Torvalds 已提交
92
 *  - PCI168 AP(W) card: power amplifier with 4 Watts/channel at 4 Ohms
93
 *    [TDA1517P chip]
L
Linus Torvalds 已提交
94
 *
A
Andreas Mohr 已提交
95 96 97 98 99 100 101 102 103
 *  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"
 *
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
 *  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.
 *
L
Linus Torvalds 已提交
122 123 124 125 126 127
 *  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
A
Andreas Mohr 已提交
128 129
 *    (well, it's not bad on an Athlon 1800 with now very optimized IRQ handler)
 *
L
Linus Torvalds 已提交
130 131 132 133 134
 *  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:
135 136
 *  - use speaker (amplifier) output instead of headphone output
 *    (in case crackling is due to overloaded output clipping)
L
Linus Torvalds 已提交
137 138 139 140 141 142 143
 *  - plug card into a different PCI slot, preferrably one that isn't shared
 *    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
144
 *
L
Linus Torvalds 已提交
145
 * BUGS
146
 *  - full-duplex might *still* be problematic, however a recent test was fine
147 148 149 150
 *  - (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!!
151 152 153 154 155 156
 *  - 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.
 *
L
Linus Torvalds 已提交
157
 * TODO
158 159 160 161
 *  - use PCI_VDEVICE
 *  - verify driver status on x86_64
 *  - test multi-card driver operation
 *  - (ab)use 1MHz DirectX timer as kernel clocksource
L
Linus Torvalds 已提交
162
 *  - test MPU401 MIDI playback etc.
163
 *  - add more power micro-management (disable various units of the card
164 165 166
 *    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...
167 168
 *    The standard suspend/resume functionality could probably make use of
 *    some improvement, too...
L
Linus Torvalds 已提交
169
 *  - figure out what all unknown port bits are responsible for
170 171 172
 *  - 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)
173
 *  - use MMIO (memory-mapped I/O)? Slightly faster access, e.g. for gameport.
L
Linus Torvalds 已提交
174 175 176 177 178 179 180 181 182
 */

#include <asm/io.h>
#include <linux/init.h>
#include <linux/pci.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/gameport.h>
#include <linux/moduleparam.h>
183
#include <linux/dma-mapping.h>
L
Linus Torvalds 已提交
184 185 186 187 188 189 190 191 192
#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>
#include "azt3328.h"

A
Andreas Mohr 已提交
193
MODULE_AUTHOR("Andreas Mohr <andi AT lisas.de>");
L
Linus Torvalds 已提交
194 195 196 197 198
MODULE_DESCRIPTION("Aztech AZF3328 (PCI168)");
MODULE_LICENSE("GPL");
MODULE_SUPPORTED_DEVICE("{{Aztech,AZF3328}}");

#if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
199
#define SUPPORT_GAMEPORT 1
L
Linus Torvalds 已提交
200 201
#endif

202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
/* === Debug settings ===
  Further diagnostic functionality than the settings below
  does not need to be provided, since one can easily write a bash script
  to dump the card's I/O ports (those listed in lspci -v -v):
  function dump()
  {
    local descr=$1; local addr=$2; local count=$3

    echo "${descr}: ${count} @ ${addr}:"
    dd if=/dev/port skip=$[${addr}] count=${count} bs=1 2>/dev/null| hexdump -C
  }
  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`"
  printf "\x""$VALSTRING"|dd of=/dev/port seek=$[${addr}] bs=1 2>/dev/null
*/

L
Linus Torvalds 已提交
222 223 224 225 226
#define DEBUG_MISC	0
#define DEBUG_CALLS	0
#define DEBUG_MIXER	0
#define DEBUG_PLAY_REC	0
#define DEBUG_IO	0
A
Andreas Mohr 已提交
227
#define DEBUG_TIMER	0
228
#define DEBUG_GAME	0
L
Linus Torvalds 已提交
229 230 231 232 233 234
#define MIXER_TESTING	0

#if DEBUG_MISC
#define snd_azf3328_dbgmisc(format, args...) printk(KERN_ERR format, ##args)
#else
#define snd_azf3328_dbgmisc(format, args...)
235
#endif
L
Linus Torvalds 已提交
236 237 238

#if DEBUG_CALLS
#define snd_azf3328_dbgcalls(format, args...) printk(format, ##args)
239 240
#define snd_azf3328_dbgcallenter() printk(KERN_ERR "--> %s\n", __func__)
#define snd_azf3328_dbgcallleave() printk(KERN_ERR "<-- %s\n", __func__)
L
Linus Torvalds 已提交
241 242 243 244
#else
#define snd_azf3328_dbgcalls(format, args...)
#define snd_azf3328_dbgcallenter()
#define snd_azf3328_dbgcallleave()
245
#endif
L
Linus Torvalds 已提交
246 247

#if DEBUG_MIXER
248
#define snd_azf3328_dbgmixer(format, args...) printk(KERN_DEBUG format, ##args)
L
Linus Torvalds 已提交
249 250
#else
#define snd_azf3328_dbgmixer(format, args...)
251
#endif
L
Linus Torvalds 已提交
252 253

#if DEBUG_PLAY_REC
254
#define snd_azf3328_dbgplay(format, args...) printk(KERN_DEBUG format, ##args)
L
Linus Torvalds 已提交
255 256
#else
#define snd_azf3328_dbgplay(format, args...)
257
#endif
L
Linus Torvalds 已提交
258

A
Andreas Mohr 已提交
259
#if DEBUG_MISC
260
#define snd_azf3328_dbgtimer(format, args...) printk(KERN_DEBUG format, ##args)
L
Linus Torvalds 已提交
261
#else
A
Andreas Mohr 已提交
262
#define snd_azf3328_dbgtimer(format, args...)
263 264 265
#endif

#if DEBUG_GAME
266
#define snd_azf3328_dbggame(format, args...) printk(KERN_DEBUG format, ##args)
267 268 269
#else
#define snd_azf3328_dbggame(format, args...)
#endif
A
Andreas Mohr 已提交
270

L
Linus Torvalds 已提交
271 272 273 274 275 276 277 278 279 280 281 282
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.");

static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;	/* Enable this card */
module_param_array(enable, bool, NULL, 0444);
MODULE_PARM_DESC(enable, "Enable AZF3328 soundcard.");

A
Andreas Mohr 已提交
283 284 285
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.");
L
Linus Torvalds 已提交
286

287 288
struct snd_azf3328_codec_data {
	unsigned long io_base;
289
	struct snd_pcm_substream *substream;
290 291
	bool running;
	const char *name;
292 293
};

294 295 296 297
enum snd_azf3328_codec_type {
  AZF_CODEC_PLAYBACK = 0,
  AZF_CODEC_CAPTURE = 1,
  AZF_CODEC_I2S_OUT = 2,
298 299
};

300
struct snd_azf3328 {
A
Andreas Mohr 已提交
301
	/* often-used fields towards beginning, then grouped */
302

303
	unsigned long ctrl_io; /* usually 0xb000, size 128 */
304 305 306 307
	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 */
L
Linus Torvalds 已提交
308

A
Andreas Mohr 已提交
309
	spinlock_t reg_lock;
L
Linus Torvalds 已提交
310

311
	struct snd_timer *timer;
312

313 314 315 316
	struct snd_pcm *pcm[3];

	/* playback, recording and I2S out codecs */
	struct snd_azf3328_codec_data codecs[3];
L
Linus Torvalds 已提交
317

318 319
	struct snd_card *card;
	struct snd_rawmidi *rmidi;
A
Andreas Mohr 已提交
320

321
#ifdef SUPPORT_GAMEPORT
A
Andreas Mohr 已提交
322
	struct gameport *gameport;
323
	u16 axes[4];
A
Andreas Mohr 已提交
324
#endif
L
Linus Torvalds 已提交
325

A
Andreas Mohr 已提交
326 327
	struct pci_dev *pci;
	int irq;
328

329 330 331 332
	/* 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. */
333
	u16 shadow_reg_ctrl_6AH;
334

335 336 337
#ifdef CONFIG_PM
	/* register value containers for power management
	 * Note: not always full I/O range preserved (just like Win driver!) */
338
	u16 saved_regs_ctrl[AZF_IO_SIZE_CTRL_PM / 2];
339 340 341
	u16 saved_regs_game [AZF_IO_SIZE_GAME_PM / 2];
	u16 saved_regs_mpu  [AZF_IO_SIZE_MPU_PM / 2];
	u16 saved_regs_opl3 [AZF_IO_SIZE_OPL3_PM / 2];
342 343
	u16 saved_regs_mixer[AZF_IO_SIZE_MIXER_PM / 2];
#endif
344
};
A
Andreas Mohr 已提交
345

346
static const struct pci_device_id snd_azf3328_ids[] = {
L
Linus Torvalds 已提交
347 348 349 350 351 352 353
	{ 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);

354 355

static int
356
snd_azf3328_io_reg_setb(unsigned reg, u8 mask, bool do_set)
357 358 359 360 361 362 363 364 365 366 367 368 369
{
	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;
}

A
Andreas Mohr 已提交
370
static inline void
371 372 373 374
snd_azf3328_codec_outb(const struct snd_azf3328_codec_data *codec,
		       unsigned reg,
		       u8 value
)
A
Andreas Mohr 已提交
375
{
376
	outb(value, codec->io_base + reg);
A
Andreas Mohr 已提交
377 378 379
}

static inline u8
380
snd_azf3328_codec_inb(const struct snd_azf3328_codec_data *codec, unsigned reg)
A
Andreas Mohr 已提交
381
{
382
	return inb(codec->io_base + reg);
A
Andreas Mohr 已提交
383 384 385
}

static inline void
386 387 388 389
snd_azf3328_codec_outw(const struct snd_azf3328_codec_data *codec,
		       unsigned reg,
		       u16 value
)
A
Andreas Mohr 已提交
390
{
391
	outw(value, codec->io_base + reg);
A
Andreas Mohr 已提交
392 393 394
}

static inline u16
395
snd_azf3328_codec_inw(const struct snd_azf3328_codec_data *codec, unsigned reg)
396
{
397
	return inw(codec->io_base + reg);
398 399 400
}

static inline void
401 402 403 404
snd_azf3328_codec_outl(const struct snd_azf3328_codec_data *codec,
		       unsigned reg,
		       u32 value
)
405
{
406
	outl(value, codec->io_base + reg);
407 408 409
}

static inline u32
410 411 412 413 414 415 416
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)
A
Andreas Mohr 已提交
417
{
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
	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);
}

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);
A
Andreas Mohr 已提交
437 438 439
}

static inline void
440
snd_azf3328_game_outb(const struct snd_azf3328 *chip, unsigned reg, u8 value)
A
Andreas Mohr 已提交
441
{
442
	outb(value, chip->game_io + reg);
A
Andreas Mohr 已提交
443 444 445
}

static inline void
446
snd_azf3328_game_outw(const struct snd_azf3328 *chip, unsigned reg, u16 value)
L
Linus Torvalds 已提交
447
{
448
	outw(value, chip->game_io + reg);
L
Linus Torvalds 已提交
449 450
}

A
Andreas Mohr 已提交
451
static inline u8
452 453 454 455 456 457 458
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)
L
Linus Torvalds 已提交
459
{
460
	return inw(chip->game_io + reg);
L
Linus Torvalds 已提交
461 462
}

A
Andreas Mohr 已提交
463
static inline void
464
snd_azf3328_mixer_outw(const struct snd_azf3328 *chip, unsigned reg, u16 value)
L
Linus Torvalds 已提交
465
{
466
	outw(value, chip->mixer_io + reg);
A
Andreas Mohr 已提交
467 468 469
}

static inline u16
470
snd_azf3328_mixer_inw(const struct snd_azf3328 *chip, unsigned reg)
A
Andreas Mohr 已提交
471
{
472
	return inw(chip->mixer_io + reg);
L
Linus Torvalds 已提交
473 474
}

475 476
#define AZF_MUTE_BIT 0x80

477
static bool
478
snd_azf3328_mixer_set_mute(const struct snd_azf3328 *chip,
479
			   unsigned reg, bool do_mute
480
)
L
Linus Torvalds 已提交
481
{
482
	unsigned long portbase = chip->mixer_io + reg + 1;
483
	bool updated;
L
Linus Torvalds 已提交
484 485 486

	/* the mute bit is on the *second* (i.e. right) register of a
	 * left/right channel setting */
487 488 489 490
	updated = snd_azf3328_io_reg_setb(portbase, AZF_MUTE_BIT, do_mute);

	/* indicate whether it was muted before */
	return (do_mute) ? !updated : updated;
L
Linus Torvalds 已提交
491 492
}

A
Andreas Mohr 已提交
493
static void
494 495 496 497 498 499
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
)
L
Linus Torvalds 已提交
500
{
501
	unsigned long portbase = chip->mixer_io + reg;
L
Linus Torvalds 已提交
502
	unsigned char curr_vol_left = 0, curr_vol_right = 0;
503 504
	int left_change = 0, right_change = 0;

L
Linus Torvalds 已提交
505
	snd_azf3328_dbgcallenter();
506 507

	if (chan_sel & SET_CHAN_LEFT) {
A
Andreas Mohr 已提交
508
		curr_vol_left  = inb(portbase + 1);
509 510 511 512 513 514 515 516 517 518 519

		/* 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) {
A
Andreas Mohr 已提交
520
		curr_vol_right = inb(portbase + 0);
521 522 523

		right_change = (curr_vol_right > dst_vol_right) ? -1 : 1;
	}
L
Linus Torvalds 已提交
524

525
	do {
526 527 528 529 530 531
		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;
L
Linus Torvalds 已提交
532
		}
533 534 535 536
		if (right_change) {
			if (curr_vol_right != dst_vol_right) {
				curr_vol_right += right_change;

L
Linus Torvalds 已提交
537 538 539
			/* during volume change, the right channel is crackling
			 * somewhat more than the left channel, unfortunately.
			 * This seems to be a hardware issue. */
540 541 542
				outb(curr_vol_right, portbase + 0);
			} else
			    right_change = 0;
L
Linus Torvalds 已提交
543 544 545
		}
		if (delay)
			mdelay(delay);
546
	} while ((left_change) || (right_change));
L
Linus Torvalds 已提交
547 548 549 550 551 552
	snd_azf3328_dbgcallleave();
}

/*
 * general mixer element
 */
553
struct azf3328_mixer_reg {
554
	unsigned reg;
L
Linus Torvalds 已提交
555 556 557 558 559
	unsigned int lchan_shift, rchan_shift;
	unsigned int mask;
	unsigned int invert: 1;
	unsigned int stereo: 1;
	unsigned int enum_c: 4;
560
};
L
Linus Torvalds 已提交
561 562

#define COMPOSE_MIXER_REG(reg,lchan_shift,rchan_shift,mask,invert,stereo,enum_c) \
A
Andreas Mohr 已提交
563 564 565 566 567
 ((reg) | (lchan_shift << 8) | (rchan_shift << 12) | \
  (mask << 16) | \
  (invert << 24) | \
  (stereo << 25) | \
  (enum_c << 26))
L
Linus Torvalds 已提交
568

569
static void snd_azf3328_mixer_reg_decode(struct azf3328_mixer_reg *r, unsigned long val)
L
Linus Torvalds 已提交
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
{
	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), \
}

A
Andreas Mohr 已提交
619
static int
620 621
snd_azf3328_info_mixer(struct snd_kcontrol *kcontrol,
		       struct snd_ctl_elem_info *uinfo)
L
Linus Torvalds 已提交
622
{
623
	struct azf3328_mixer_reg reg;
L
Linus Torvalds 已提交
624 625 626

	snd_azf3328_dbgcallenter();
	snd_azf3328_mixer_reg_decode(&reg, kcontrol->private_value);
A
Andreas Mohr 已提交
627 628
	uinfo->type = reg.mask == 1 ?
		SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
L
Linus Torvalds 已提交
629 630 631 632 633 634 635
	uinfo->count = reg.stereo + 1;
	uinfo->value.integer.min = 0;
	uinfo->value.integer.max = reg.mask;
	snd_azf3328_dbgcallleave();
	return 0;
}

A
Andreas Mohr 已提交
636
static int
637 638
snd_azf3328_get_mixer(struct snd_kcontrol *kcontrol,
		      struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
639
{
640 641
	struct snd_azf3328 *chip = snd_kcontrol_chip(kcontrol);
	struct azf3328_mixer_reg reg;
642
	u16 oreg, val;
L
Linus Torvalds 已提交
643 644 645 646

	snd_azf3328_dbgcallenter();
	snd_azf3328_mixer_reg_decode(&reg, kcontrol->private_value);

A
Andreas Mohr 已提交
647
	oreg = snd_azf3328_mixer_inw(chip, reg.reg);
L
Linus Torvalds 已提交
648 649 650 651 652 653 654 655 656 657
	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;
	}
A
Andreas Mohr 已提交
658 659 660 661 662
	snd_azf3328_dbgmixer("get: %02x is %04x -> vol %02lx|%02lx "
			     "(shift %02d|%02d, mask %02x, inv. %d, stereo %d)\n",
		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);
L
Linus Torvalds 已提交
663 664 665 666
	snd_azf3328_dbgcallleave();
	return 0;
}

A
Andreas Mohr 已提交
667
static int
668 669
snd_azf3328_put_mixer(struct snd_kcontrol *kcontrol,
		      struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
670
{
671 672
	struct snd_azf3328 *chip = snd_kcontrol_chip(kcontrol);
	struct azf3328_mixer_reg reg;
673
	u16 oreg, nreg, val;
L
Linus Torvalds 已提交
674 675 676

	snd_azf3328_dbgcallenter();
	snd_azf3328_mixer_reg_decode(&reg, kcontrol->private_value);
A
Andreas Mohr 已提交
677
	oreg = snd_azf3328_mixer_inw(chip, reg.reg);
L
Linus Torvalds 已提交
678 679 680 681 682 683 684 685 686 687 688 689 690
	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 */
A
Andreas Mohr 已提交
691 692 693 694 695
		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);
L
Linus Torvalds 已提交
696
	else
A
Andreas Mohr 已提交
697
        	snd_azf3328_mixer_outw(chip, reg.reg, nreg);
L
Linus Torvalds 已提交
698

A
Andreas Mohr 已提交
699 700 701 702 703
	snd_azf3328_dbgmixer("put: %02x to %02lx|%02lx, "
			     "oreg %04x; shift %02d|%02d -> nreg %04x; after: %04x\n",
		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));
L
Linus Torvalds 已提交
704 705 706 707
	snd_azf3328_dbgcallleave();
	return (nreg != oreg);
}

A
Andreas Mohr 已提交
708
static int
709 710
snd_azf3328_info_mixer_enum(struct snd_kcontrol *kcontrol,
			    struct snd_ctl_elem_info *uinfo)
L
Linus Torvalds 已提交
711
{
A
Andreas Mohr 已提交
712
	static const char * const texts1[] = {
713
		"Mic1", "Mic2"
A
Andreas Mohr 已提交
714 715
	};
	static const char * const texts2[] = {
716
		"Mix", "Mic"
A
Andreas Mohr 已提交
717 718
	};
	static const char * const texts3[] = {
719
		"Mic", "CD", "Video", "Aux",
A
Andreas Mohr 已提交
720
		"Line", "Mix", "Mix Mono", "Phone"
L
Linus Torvalds 已提交
721
        };
722 723 724
	static const char * const texts4[] = {
		"pre 3D", "post 3D"
        };
725
	struct azf3328_mixer_reg reg;
726
	const char * const *p = NULL;
L
Linus Torvalds 已提交
727 728 729 730 731 732 733

	snd_azf3328_mixer_reg_decode(&reg, kcontrol->private_value);
        uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
        uinfo->count = (reg.reg == IDX_MIXER_REC_SELECT) ? 2 : 1;
        uinfo->value.enumerated.items = reg.enum_c;
        if (uinfo->value.enumerated.item > reg.enum_c - 1U)
                uinfo->value.enumerated.item = reg.enum_c - 1U;
734
	if (reg.reg == IDX_MIXER_ADVCTL2) {
735 736
		switch(reg.lchan_shift) {
		case 8: /* modem out sel */
737
			p = texts1;
738 739
			break;
		case 9: /* mono sel source */
740
			p = texts2;
741 742
			break;
		case 15: /* PCM Out Path */
743
			p = texts4;
744 745
			break;
		}
746
	} else
747
	if (reg.reg == IDX_MIXER_REC_SELECT)
748
		p = texts3;
749

750
	strcpy(uinfo->value.enumerated.name, p[uinfo->value.enumerated.item]);
L
Linus Torvalds 已提交
751 752 753
        return 0;
}

A
Andreas Mohr 已提交
754
static int
755 756
snd_azf3328_get_mixer_enum(struct snd_kcontrol *kcontrol,
			   struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
757
{
758 759
        struct snd_azf3328 *chip = snd_kcontrol_chip(kcontrol);
	struct azf3328_mixer_reg reg;
L
Linus Torvalds 已提交
760
        unsigned short val;
761

L
Linus Torvalds 已提交
762
	snd_azf3328_mixer_reg_decode(&reg, kcontrol->private_value);
A
Andreas Mohr 已提交
763
	val = snd_azf3328_mixer_inw(chip, reg.reg);
764
	if (reg.reg == IDX_MIXER_REC_SELECT) {
L
Linus Torvalds 已提交
765 766
        	ucontrol->value.enumerated.item[0] = (val >> 8) & (reg.enum_c - 1);
        	ucontrol->value.enumerated.item[1] = (val >> 0) & (reg.enum_c - 1);
767
	} else
L
Linus Torvalds 已提交
768
        	ucontrol->value.enumerated.item[0] = (val >> reg.lchan_shift) & (reg.enum_c - 1);
A
Andreas Mohr 已提交
769 770 771 772

	snd_azf3328_dbgmixer("get_enum: %02x is %04x -> %d|%d (shift %02d, enum_c %d)\n",
		reg.reg, val, ucontrol->value.enumerated.item[0], ucontrol->value.enumerated.item[1],
		reg.lchan_shift, reg.enum_c);
L
Linus Torvalds 已提交
773 774 775
        return 0;
}

A
Andreas Mohr 已提交
776
static int
777 778
snd_azf3328_put_mixer_enum(struct snd_kcontrol *kcontrol,
			   struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
779
{
780 781
        struct snd_azf3328 *chip = snd_kcontrol_chip(kcontrol);
	struct azf3328_mixer_reg reg;
782
	u16 oreg, nreg, val;
783

L
Linus Torvalds 已提交
784
	snd_azf3328_mixer_reg_decode(&reg, kcontrol->private_value);
A
Andreas Mohr 已提交
785
	oreg = snd_azf3328_mixer_inw(chip, reg.reg);
L
Linus Torvalds 已提交
786
	val = oreg;
787
	if (reg.reg == IDX_MIXER_REC_SELECT) {
L
Linus Torvalds 已提交
788 789 790 791 792
        	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);
793
	} else {
L
Linus Torvalds 已提交
794 795 796 797 798
        	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);
	}
A
Andreas Mohr 已提交
799
	snd_azf3328_mixer_outw(chip, reg.reg, val);
L
Linus Torvalds 已提交
800 801 802 803 804 805
	nreg = val;

	snd_azf3328_dbgmixer("put_enum: %02x to %04x, oreg %04x\n", reg.reg, val, oreg);
	return (nreg != oreg);
}

806
static struct snd_kcontrol_new snd_azf3328_mixer_controls[] __devinitdata = {
L
Linus Torvalds 已提交
807 808
	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),
809 810 811 812 813
	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),
L
Linus Torvalds 已提交
814 815 816 817 818 819 820 821 822 823 824 825
	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),
A
Andreas Mohr 已提交
826 827
	AZF3328_MIXER_SWITCH("PC Speaker Playback Switch", IDX_MIXER_PCBEEP, 15, 1),
	AZF3328_MIXER_VOL_SPECIAL("PC Speaker Playback Volume", IDX_MIXER_PCBEEP, 0x0f, 1, 1),
L
Linus Torvalds 已提交
828 829 830 831 832 833 834 835
	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),
836 837
	AZF3328_MIXER_ENUM("Mic Select", IDX_MIXER_ADVCTL2, 2, 8),
	AZF3328_MIXER_ENUM("Mono Output Select", IDX_MIXER_ADVCTL2, 2, 9),
838
	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! */
L
Linus Torvalds 已提交
839 840
	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),
A
Andreas Mohr 已提交
841
	AZF3328_MIXER_SWITCH("3D Control - Switch", IDX_MIXER_ADVCTL2, 13, 0),
842 843
	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" */
L
Linus Torvalds 已提交
844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863
#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
};

864
static u16 __devinitdata snd_azf3328_init_values[][2] = {
L
Linus Torvalds 已提交
865 866 867 868 869 870 871 872 873 874 875 876 877 878 879
        { 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 },
};

A
Andreas Mohr 已提交
880
static int __devinit
881
snd_azf3328_mixer_new(struct snd_azf3328 *chip)
L
Linus Torvalds 已提交
882
{
883 884
	struct snd_card *card;
	const struct snd_kcontrol_new *sw;
L
Linus Torvalds 已提交
885 886 887 888
	unsigned int idx;
	int err;

	snd_azf3328_dbgcallenter();
889 890
	if (snd_BUG_ON(!chip || !chip->card))
		return -EINVAL;
L
Linus Torvalds 已提交
891 892 893 894

	card = chip->card;

	/* mixer reset */
A
Andreas Mohr 已提交
895
	snd_azf3328_mixer_outw(chip, IDX_MIXER_RESET, 0x0000);
L
Linus Torvalds 已提交
896 897

	/* mute and zero volume channels */
898
	for (idx = 0; idx < ARRAY_SIZE(snd_azf3328_init_values); ++idx) {
A
Andreas Mohr 已提交
899 900 901
		snd_azf3328_mixer_outw(chip,
			snd_azf3328_init_values[idx][0],
			snd_azf3328_init_values[idx][1]);
L
Linus Torvalds 已提交
902
	}
903

L
Linus Torvalds 已提交
904 905
	/* add mixer controls */
	sw = snd_azf3328_mixer_controls;
906 907
	for (idx = 0; idx < ARRAY_SIZE(snd_azf3328_mixer_controls);
			++idx, ++sw) {
L
Linus Torvalds 已提交
908 909 910 911 912 913 914 915 916 917
		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");

	snd_azf3328_dbgcallleave();
	return 0;
}

A
Andreas Mohr 已提交
918
static int
919 920
snd_azf3328_hw_params(struct snd_pcm_substream *substream,
				 struct snd_pcm_hw_params *hw_params)
L
Linus Torvalds 已提交
921 922 923 924 925 926 927 928
{
	int res;
	snd_azf3328_dbgcallenter();
	res = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
	snd_azf3328_dbgcallleave();
	return res;
}

A
Andreas Mohr 已提交
929
static int
930
snd_azf3328_hw_free(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
931 932 933 934 935 936 937
{
	snd_azf3328_dbgcallenter();
	snd_pcm_lib_free_pages(substream);
	snd_azf3328_dbgcallleave();
	return 0;
}

A
Andreas Mohr 已提交
938
static void
939
snd_azf3328_codec_setfmt(struct snd_azf3328 *chip,
940
			       enum snd_azf3328_codec_type codec_type,
941
			       enum azf_freq_t bitrate,
L
Linus Torvalds 已提交
942 943 944 945 946
			       unsigned int format_width,
			       unsigned int channels
)
{
	unsigned long flags;
947 948
	const struct snd_azf3328_codec_data *codec = &chip->codecs[codec_type];
	u16 val = 0xff00;
L
Linus Torvalds 已提交
949 950 951

	snd_azf3328_dbgcallenter();
	switch (bitrate) {
952 953 954 955 956 957 958 959 960 961 962 963 964
	case AZF_FREQ_4000:  val |= SOUNDFORMAT_FREQ_SUSPECTED_4000; break;
	case AZF_FREQ_4800:  val |= SOUNDFORMAT_FREQ_SUSPECTED_4800; break;
	case AZF_FREQ_5512:
		/* the AZF3328 names it "5510" for some strange reason */
			     val |= SOUNDFORMAT_FREQ_5510; break;
	case AZF_FREQ_6620:  val |= SOUNDFORMAT_FREQ_6620; break;
	case AZF_FREQ_8000:  val |= SOUNDFORMAT_FREQ_8000; break;
	case AZF_FREQ_9600:  val |= SOUNDFORMAT_FREQ_9600; break;
	case AZF_FREQ_11025: val |= SOUNDFORMAT_FREQ_11025; break;
	case AZF_FREQ_13240: val |= SOUNDFORMAT_FREQ_SUSPECTED_13240; break;
	case AZF_FREQ_16000: val |= SOUNDFORMAT_FREQ_16000; break;
	case AZF_FREQ_22050: val |= SOUNDFORMAT_FREQ_22050; break;
	case AZF_FREQ_32000: val |= SOUNDFORMAT_FREQ_32000; break;
L
Linus Torvalds 已提交
965
	default:
966
		snd_printk(KERN_WARNING "unknown bitrate %d, assuming 44.1kHz!\n", bitrate);
967 968 969 970
		/* fall-through */
	case AZF_FREQ_44100: val |= SOUNDFORMAT_FREQ_44100; break;
	case AZF_FREQ_48000: val |= SOUNDFORMAT_FREQ_48000; break;
	case AZF_FREQ_66200: val |= SOUNDFORMAT_FREQ_SUSPECTED_66200; break;
L
Linus Torvalds 已提交
971
	}
A
Andreas Mohr 已提交
972 973 974 975
	/* 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 */
L
Linus Torvalds 已提交
976 977 978 979 980
	/* 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???) */
A
Andreas Mohr 已提交
981

L
Linus Torvalds 已提交
982 983 984 985 986 987 988
	if (channels == 2)
		val |= SOUNDFORMAT_FLAG_2CHANNELS;

	if (format_width == 16)
		val |= SOUNDFORMAT_FLAG_16BIT;

	spin_lock_irqsave(&chip->reg_lock, flags);
989

L
Linus Torvalds 已提交
990
	/* set bitrate/format */
991
	snd_azf3328_codec_outw(codec, IDX_IO_CODEC_SOUNDFORMAT, val);
992

L
Linus Torvalds 已提交
993 994 995 996 997 998 999
	/* 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? */
1000 1001 1002 1003 1004
	if (codec_type == AZF_CODEC_PLAYBACK) /* only do it for playback */
		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 |
A
Andreas Mohr 已提交
1005 1006 1007 1008
			SOMETHING_ALMOST_ALWAYS_SET |
			DMA_EPILOGUE_SOMETHING |
			DMA_SOMETHING_ELSE
		);
L
Linus Torvalds 已提交
1009 1010 1011 1012 1013

	spin_unlock_irqrestore(&chip->reg_lock, flags);
	snd_azf3328_dbgcallleave();
}

1014 1015
static inline void
snd_azf3328_codec_setfmt_lowpower(struct snd_azf3328 *chip,
1016
			    enum snd_azf3328_codec_type codec_type
1017 1018 1019 1020 1021 1022
)
{
	/* 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. */
1023
	snd_azf3328_codec_setfmt(chip, codec_type, AZF_FREQ_4000, 8, 1);
1024 1025
}

1026
static void
1027
snd_azf3328_ctrl_reg_6AH_update(struct snd_azf3328 *chip,
1028
					unsigned bitmask,
1029
					bool enable
1030 1031 1032
)
{
	if (enable)
1033
		chip->shadow_reg_ctrl_6AH &= ~bitmask;
1034
	else
1035
		chip->shadow_reg_ctrl_6AH |= bitmask;
1036
	snd_azf3328_dbgplay("6AH_update mask 0x%04x enable %d: val 0x%04x\n",
1037 1038
			bitmask, enable, chip->shadow_reg_ctrl_6AH);
	snd_azf3328_ctrl_outw(chip, IDX_IO_6AH, chip->shadow_reg_ctrl_6AH);
1039 1040
}

1041
static inline void
1042
snd_azf3328_ctrl_enable_codecs(struct snd_azf3328 *chip, bool enable)
1043
{
1044
	snd_azf3328_dbgplay("codec_enable %d\n", enable);
1045 1046
	/* no idea what exactly is being done here, but I strongly assume it's
	 * PM related */
1047
	snd_azf3328_ctrl_reg_6AH_update(
1048
		chip, IO_6A_PAUSE_PLAYBACK_BIT8, enable
1049 1050 1051 1052
	);
}

static void
1053 1054 1055
snd_azf3328_ctrl_codec_activity(struct snd_azf3328 *chip,
				enum snd_azf3328_codec_type codec_type,
				bool enable
1056 1057
)
{
1058 1059
	struct snd_azf3328_codec_data *codec = &chip->codecs[codec_type];
	bool need_change = (codec->running != enable);
1060 1061

	snd_azf3328_dbgplay(
1062 1063
		"codec_activity: %s codec, enable %d, need_change %d\n",
				codec->name, enable, need_change
1064 1065
	);
	if (need_change) {
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
		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! */
			if ((!chip->codecs[peer_codecs[codec_type].other1]
				.running)
			&&  (!chip->codecs[peer_codecs[codec_type].other2]
				.running))
				call_function = 1;
		 }
		 if (call_function)
			snd_azf3328_ctrl_enable_codecs(chip, enable);
1092 1093 1094 1095 1096 1097

		/* ...and adjust clock, too
		 * (reduce noise and power consumption) */
		if (!enable)
			snd_azf3328_codec_setfmt_lowpower(
				chip,
1098
				codec_type
1099 1100
			);
	}
1101
	codec->running = enable;
1102 1103
}

A
Andreas Mohr 已提交
1104
static void
1105 1106 1107 1108 1109
snd_azf3328_codec_setdmaa(struct snd_azf3328 *chip,
				enum snd_azf3328_codec_type codec_type,
				unsigned long addr,
				unsigned int count,
				unsigned int size
1110
)
L
Linus Torvalds 已提交
1111
{
1112
	const struct snd_azf3328_codec_data *codec = &chip->codecs[codec_type];
L
Linus Torvalds 已提交
1113
	snd_azf3328_dbgcallenter();
1114 1115
	if (!codec->running) {
		/* AZF3328 uses a two buffer pointer DMA transfer approach */
1116

1117
		unsigned long flags;
1118 1119

		/* width 32bit (prevent overflow): */
1120
		u32 addr_area2, count_areas, lengths;
A
Andreas Mohr 已提交
1121 1122 1123 1124 1125 1126 1127

		count_areas = size/2;
		addr_area2 = addr+count_areas;
		count_areas--; /* max. index */
		snd_azf3328_dbgplay("set DMA: buf1 %08lx[%lu], buf2 %08lx[%lu]\n", addr, count_areas, addr_area2, count_areas);

		/* build combined I/O buffer length word */
1128
		lengths = (count_areas << 16) | (count_areas);
L
Linus Torvalds 已提交
1129
		spin_lock_irqsave(&chip->reg_lock, flags);
1130 1131 1132 1133 1134
		snd_azf3328_codec_outl(codec, IDX_IO_CODEC_DMA_START_1, addr);
		snd_azf3328_codec_outl(codec, IDX_IO_CODEC_DMA_START_2,
								addr_area2);
		snd_azf3328_codec_outl(codec, IDX_IO_CODEC_DMA_LENGTHS,
								lengths);
L
Linus Torvalds 已提交
1135 1136 1137 1138 1139
		spin_unlock_irqrestore(&chip->reg_lock, flags);
	}
	snd_azf3328_dbgcallleave();
}

A
Andreas Mohr 已提交
1140
static int
1141
snd_azf3328_codec_prepare(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
1142 1143
{
#if 0
1144 1145
	struct snd_azf3328 *chip = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
L
Linus Torvalds 已提交
1146 1147 1148 1149 1150 1151
        unsigned int size = snd_pcm_lib_buffer_bytes(substream);
	unsigned int count = snd_pcm_lib_period_bytes(substream);
#endif

	snd_azf3328_dbgcallenter();
#if 0
1152
	snd_azf3328_codec_setfmt(chip, AZF_CODEC_...,
A
Andreas Mohr 已提交
1153 1154 1155
		runtime->rate,
		snd_pcm_format_width(runtime->format),
		runtime->channels);
1156 1157
	snd_azf3328_codec_setdmaa(chip, AZF_CODEC_...,
					runtime->dma_addr, count, size);
L
Linus Torvalds 已提交
1158 1159 1160 1161 1162
#endif
	snd_azf3328_dbgcallleave();
	return 0;
}

A
Andreas Mohr 已提交
1163
static int
1164 1165
snd_azf3328_codec_trigger(enum snd_azf3328_codec_type codec_type,
			struct snd_pcm_substream *substream, int cmd)
L
Linus Torvalds 已提交
1166
{
1167
	struct snd_azf3328 *chip = snd_pcm_substream_chip(substream);
1168
	const struct snd_azf3328_codec_data *codec = &chip->codecs[codec_type];
1169
	struct snd_pcm_runtime *runtime = substream->runtime;
L
Linus Torvalds 已提交
1170
	int result = 0;
1171 1172 1173
	u16 flags1;
	bool previously_muted = 0;
	bool is_playback_codec = (AZF_CODEC_PLAYBACK == codec_type);
L
Linus Torvalds 已提交
1174

1175
	snd_azf3328_dbgcalls("snd_azf3328_codec_trigger cmd %d\n", cmd);
A
Andreas Mohr 已提交
1176

L
Linus Torvalds 已提交
1177 1178
	switch (cmd) {
	case SNDRV_PCM_TRIGGER_START:
1179 1180 1181 1182 1183 1184 1185 1186 1187
		snd_azf3328_dbgplay("START %s\n", codec->name);

		if (is_playback_codec) {
			/* mute WaveOut (avoid clicking during setup) */
			previously_muted =
				snd_azf3328_mixer_set_mute(
						chip, IDX_MIXER_WAVEOUT, 1
				);
		}
L
Linus Torvalds 已提交
1188

1189
		snd_azf3328_codec_setfmt(chip, codec_type,
A
Andreas Mohr 已提交
1190 1191 1192
			runtime->rate,
			snd_pcm_format_width(runtime->format),
			runtime->channels);
L
Linus Torvalds 已提交
1193 1194

		spin_lock(&chip->reg_lock);
1195
		/* first, remember current value: */
1196
		flags1 = snd_azf3328_codec_inw(codec, IDX_IO_CODEC_DMA_FLAGS);
1197

1198 1199 1200
		/* stop transfer */
		flags1 &= ~DMA_RESUME;
		snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS, flags1);
1201

L
Linus Torvalds 已提交
1202
		/* FIXME: clear interrupts or what??? */
1203
		snd_azf3328_codec_outw(codec, IDX_IO_CODEC_IRQTYPE, 0xffff);
L
Linus Torvalds 已提交
1204 1205
		spin_unlock(&chip->reg_lock);

1206
		snd_azf3328_codec_setdmaa(chip, codec_type, runtime->dma_addr,
A
Andreas Mohr 已提交
1207
			snd_pcm_lib_period_bytes(substream),
1208 1209
			snd_pcm_lib_buffer_bytes(substream)
		);
L
Linus Torvalds 已提交
1210 1211 1212 1213

		spin_lock(&chip->reg_lock);
#ifdef WIN9X
		/* FIXME: enable playback/recording??? */
1214 1215
		flags1 |= DMA_RUN_SOMETHING1 | DMA_RUN_SOMETHING2;
		snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS, flags1);
L
Linus Torvalds 已提交
1216

1217
		/* start transfer again */
L
Linus Torvalds 已提交
1218
		/* FIXME: what is this value (0x0010)??? */
1219 1220
		flags1 |= DMA_RESUME | DMA_EPILOGUE_SOMETHING;
		snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS, flags1);
L
Linus Torvalds 已提交
1221
#else /* NT4 */
1222
		snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS,
A
Andreas Mohr 已提交
1223
			0x0000);
1224 1225 1226 1227 1228 1229
		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,
A
Andreas Mohr 已提交
1230 1231 1232 1233
			DMA_RESUME |
			SOMETHING_ALMOST_ALWAYS_SET |
			DMA_EPILOGUE_SOMETHING |
			DMA_SOMETHING_ELSE);
L
Linus Torvalds 已提交
1234 1235
#endif
		spin_unlock(&chip->reg_lock);
1236 1237 1238 1239 1240 1241 1242 1243 1244
		snd_azf3328_ctrl_codec_activity(chip, codec_type, 1);

		if (is_playback_codec) {
			/* now unmute WaveOut */
			if (!previously_muted)
				snd_azf3328_mixer_set_mute(
						chip, IDX_MIXER_WAVEOUT, 0
				);
		}
L
Linus Torvalds 已提交
1245

1246
		snd_azf3328_dbgplay("STARTED %s\n", codec->name);
L
Linus Torvalds 已提交
1247
		break;
1248
	case SNDRV_PCM_TRIGGER_RESUME:
1249 1250
		snd_azf3328_dbgplay("RESUME %s\n", codec->name);
		/* resume codec if we were active */
1251
		spin_lock(&chip->reg_lock);
1252 1253 1254 1255 1256 1257
		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
			);
1258
		spin_unlock(&chip->reg_lock);
1259
		break;
A
Andreas Mohr 已提交
1260
	case SNDRV_PCM_TRIGGER_STOP:
1261 1262 1263 1264 1265 1266 1267 1268 1269
		snd_azf3328_dbgplay("STOP %s\n", codec->name);

		if (is_playback_codec) {
			/* mute WaveOut (avoid clicking during setup) */
			previously_muted =
				snd_azf3328_mixer_set_mute(
						chip, IDX_MIXER_WAVEOUT, 1
				);
		}
L
Linus Torvalds 已提交
1270 1271

		spin_lock(&chip->reg_lock);
1272
		/* first, remember current value: */
1273
		flags1 = snd_azf3328_codec_inw(codec, IDX_IO_CODEC_DMA_FLAGS);
L
Linus Torvalds 已提交
1274

1275 1276 1277
		/* stop transfer */
		flags1 &= ~DMA_RESUME;
		snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS, flags1);
L
Linus Torvalds 已提交
1278

A
Andreas Mohr 已提交
1279 1280
		/* hmm, is this really required? we're resetting the same bit
		 * immediately thereafter... */
1281 1282
		flags1 |= DMA_RUN_SOMETHING1;
		snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS, flags1);
L
Linus Torvalds 已提交
1283

1284 1285
		flags1 &= ~DMA_RUN_SOMETHING1;
		snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS, flags1);
L
Linus Torvalds 已提交
1286
		spin_unlock(&chip->reg_lock);
1287 1288 1289 1290 1291 1292 1293 1294 1295
		snd_azf3328_ctrl_codec_activity(chip, codec_type, 0);

		if (is_playback_codec) {
			/* now unmute WaveOut */
			if (!previously_muted)
				snd_azf3328_mixer_set_mute(
						chip, IDX_MIXER_WAVEOUT, 0
				);
		}
1296

1297
		snd_azf3328_dbgplay("STOPPED %s\n", codec->name);
L
Linus Torvalds 已提交
1298
		break;
1299
	case SNDRV_PCM_TRIGGER_SUSPEND:
1300 1301 1302 1303 1304 1305 1306
		snd_azf3328_dbgplay("SUSPEND %s\n", codec->name);
		/* 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
		);
1307
		break;
L
Linus Torvalds 已提交
1308
        case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1309
		snd_printk(KERN_ERR "FIXME: SNDRV_PCM_TRIGGER_PAUSE_PUSH NIY!\n");
L
Linus Torvalds 已提交
1310 1311
                break;
        case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1312
		snd_printk(KERN_ERR "FIXME: SNDRV_PCM_TRIGGER_PAUSE_RELEASE NIY!\n");
L
Linus Torvalds 已提交
1313 1314
                break;
        default:
1315
		printk(KERN_ERR "FIXME: unknown trigger mode!\n");
L
Linus Torvalds 已提交
1316 1317
                return -EINVAL;
	}
1318

L
Linus Torvalds 已提交
1319 1320 1321 1322
	snd_azf3328_dbgcallleave();
	return result;
}

A
Andreas Mohr 已提交
1323
static int
1324
snd_azf3328_codec_playback_trigger(struct snd_pcm_substream *substream, int cmd)
L
Linus Torvalds 已提交
1325
{
1326 1327
	return snd_azf3328_codec_trigger(AZF_CODEC_PLAYBACK, substream, cmd);
}
1328

1329 1330 1331 1332 1333
static int
snd_azf3328_codec_capture_trigger(struct snd_pcm_substream *substream, int cmd)
{
	return snd_azf3328_codec_trigger(AZF_CODEC_CAPTURE, substream, cmd);
}
1334

1335 1336 1337 1338
static int
snd_azf3328_codec_i2s_out_trigger(struct snd_pcm_substream *substream, int cmd)
{
	return snd_azf3328_codec_trigger(AZF_CODEC_I2S_OUT, substream, cmd);
L
Linus Torvalds 已提交
1339 1340
}

A
Andreas Mohr 已提交
1341
static snd_pcm_uframes_t
1342 1343 1344
snd_azf3328_codec_pointer(struct snd_pcm_substream *substream,
			  enum snd_azf3328_codec_type codec_type
)
L
Linus Torvalds 已提交
1345
{
1346 1347
	const struct snd_azf3328 *chip = snd_pcm_substream_chip(substream);
	const struct snd_azf3328_codec_data *codec = &chip->codecs[codec_type];
A
Andreas Mohr 已提交
1348
	unsigned long bufptr, result;
L
Linus Torvalds 已提交
1349 1350 1351
	snd_pcm_uframes_t frmres;

#ifdef QUERY_HARDWARE
1352
	bufptr = snd_azf3328_codec_inl(codec, IDX_IO_CODEC_DMA_START_1);
L
Linus Torvalds 已提交
1353 1354 1355
#else
	bufptr = substream->runtime->dma_addr;
#endif
1356
	result = snd_azf3328_codec_inl(codec, IDX_IO_CODEC_DMA_CURRPOS);
L
Linus Torvalds 已提交
1357

A
Andreas Mohr 已提交
1358 1359 1360
	/* calculate offset */
	result -= bufptr;
	frmres = bytes_to_frames( substream->runtime, result);
1361 1362
	snd_azf3328_dbgplay("%s @ 0x%8lx, frames %8ld\n",
				codec->name, result, frmres);
L
Linus Torvalds 已提交
1363 1364 1365
	return frmres;
}

A
Andreas Mohr 已提交
1366
static snd_pcm_uframes_t
1367
snd_azf3328_codec_playback_pointer(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
1368
{
1369 1370
	return snd_azf3328_codec_pointer(substream, AZF_CODEC_PLAYBACK);
}
L
Linus Torvalds 已提交
1371

1372 1373 1374 1375 1376
static snd_pcm_uframes_t
snd_azf3328_codec_capture_pointer(struct snd_pcm_substream *substream)
{
	return snd_azf3328_codec_pointer(substream, AZF_CODEC_CAPTURE);
}
L
Linus Torvalds 已提交
1377

1378 1379 1380 1381
static snd_pcm_uframes_t
snd_azf3328_codec_i2s_out_pointer(struct snd_pcm_substream *substream)
{
	return snd_azf3328_codec_pointer(substream, AZF_CODEC_I2S_OUT);
L
Linus Torvalds 已提交
1382 1383
}

1384 1385 1386 1387
/******************************************************************/

#ifdef SUPPORT_GAMEPORT
static inline void
1388 1389 1390
snd_azf3328_gameport_irq_enable(struct snd_azf3328 *chip,
				bool enable
)
1391 1392 1393 1394 1395 1396 1397 1398 1399
{
	snd_azf3328_io_reg_setb(
		chip->game_io+IDX_GAME_HWCONFIG,
		GAME_HWCFG_IRQ_ENABLE,
		enable
	);
}

static inline void
1400 1401 1402
snd_azf3328_gameport_legacy_address_enable(struct snd_azf3328 *chip,
					   bool enable
)
1403 1404 1405 1406 1407 1408 1409 1410
{
	snd_azf3328_io_reg_setb(
		chip->game_io+IDX_GAME_HWCONFIG,
		GAME_HWCFG_LEGACY_ADDRESS_ENABLE,
		enable
	);
}

1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427
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
	);
}

1428
static inline void
1429
snd_azf3328_gameport_axis_circuit_enable(struct snd_azf3328 *chip, bool enable)
1430
{
1431
	snd_azf3328_ctrl_reg_6AH_update(
1432
		chip, IO_6A_SOMETHING2_GAMEPORT, enable
1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465
	);
}

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!)
	 */
	snd_azf3328_dbggame("gameport irq\n");

	 /* 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;

	snd_azf3328_dbggame("gameport_open, mode %d\n", mode);
	switch (mode) {
	case GAMEPORT_MODE_COOKED:
	case GAMEPORT_MODE_RAW:
		res = 0;
		break;
	default:
		res = -1;
		break;
	}

1466 1467
	snd_azf3328_gameport_set_counter_frequency(chip,
				GAME_HWCFG_ADC_COUNTER_FREQ_STD);
1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478
	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);

	snd_azf3328_dbggame("gameport_close\n");
1479 1480
	snd_azf3328_gameport_set_counter_frequency(chip,
				GAME_HWCFG_ADC_COUNTER_FREQ_1_200);
1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494
	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;

1495 1496
	if (snd_BUG_ON(!chip))
		return 0;
1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513

	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) {
1514
		for (i = 0; i < ARRAY_SIZE(chip->axes); ++i) {
1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536
			/* 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
					);
		}
	}

	/* trigger next axes sampling, to be evaluated the next time we
	 * 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);

1537
	for (i = 0; i < ARRAY_SIZE(chip->axes); i++) {
1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563
		axes[i] = chip->axes[i];
		if (axes[i] == 0xffff)
			axes[i] = -1;
	}

	snd_azf3328_dbggame("cooked_read: axes %d %d %d %d buttons %d\n",
		axes[0], axes[1], axes[2], axes[3], *buttons
	);

	return 0;
}

static int __devinit
snd_azf3328_gameport(struct snd_azf3328 *chip, int dev)
{
	struct gameport *gp;

	chip->gameport = gp = gameport_allocate_port();
	if (!gp) {
		printk(KERN_ERR "azt3328: cannot alloc memory for gameport\n");
		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);
1564
	gp->io = chip->game_io;
1565 1566 1567 1568 1569 1570 1571 1572 1573 1574
	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);

1575 1576
	snd_azf3328_gameport_set_counter_frequency(chip,
				GAME_HWCFG_ADC_COUNTER_FREQ_1_200);
1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606
	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)
{
	printk(KERN_WARNING "huh, game port IRQ occurred!?\n");
}
#endif /* SUPPORT_GAMEPORT */

/******************************************************************/

1607 1608 1609 1610 1611 1612 1613 1614 1615
static inline void
snd_azf3328_irq_log_unknown_type(u8 which)
{
	snd_azf3328_dbgplay(
	"azt3328: unknown IRQ type (%x) occurred, please report!\n",
		which
	);
}

1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657
static inline void
snd_azf3328_codec_interrupt(struct snd_azf3328 *chip, u8 status)
{
	u8 which;
	enum snd_azf3328_codec_type codec_type;
	const struct snd_azf3328_codec_data *codec;

	for (codec_type = AZF_CODEC_PLAYBACK;
		 codec_type <= AZF_CODEC_I2S_OUT;
			 ++codec_type) {

		/* skip codec if there's no interrupt for it */
		if (!(status & (1 << codec_type)))
			continue;

		codec = &chip->codecs[codec_type];

		spin_lock(&chip->reg_lock);
		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);
		spin_unlock(&chip->reg_lock);

		if ((chip->pcm[codec_type])
		&&  (chip->codecs[codec_type].substream)) {
			snd_pcm_period_elapsed(
				chip->codecs[codec_type].substream
			);
			snd_azf3328_dbgplay("%s period done (#%x), @ %x\n",
				codec->name,
				which,
				snd_azf3328_codec_inl(
					codec, IDX_IO_CODEC_DMA_CURRPOS
				)
			);
		} else
			printk(KERN_WARNING "azt3328: irq handler problem!\n");
		if (which & IRQ_SOMETHING)
			snd_azf3328_irq_log_unknown_type(which);
	}
}

A
Andreas Mohr 已提交
1658
static irqreturn_t
1659
snd_azf3328_interrupt(int irq, void *dev_id)
L
Linus Torvalds 已提交
1660
{
1661
	struct snd_azf3328 *chip = dev_id;
1662
	u8 status;
1663
#if DEBUG_PLAY_REC
A
Andreas Mohr 已提交
1664
	static unsigned long irq_count;
1665
#endif
L
Linus Torvalds 已提交
1666

1667
	status = snd_azf3328_ctrl_inb(chip, IDX_IO_IRQSTATUS);
L
Linus Torvalds 已提交
1668 1669

        /* fast path out, to ease interrupt sharing */
1670
	if (!(status &
1671 1672
		(IRQ_PLAYBACK|IRQ_RECORDING|IRQ_I2S_OUT
		|IRQ_GAMEPORT|IRQ_MPU401|IRQ_TIMER)
1673
	))
L
Linus Torvalds 已提交
1674 1675
		return IRQ_NONE; /* must be interrupt for another device */

1676
	snd_azf3328_dbgplay(
1677
		"irq_count %ld! IDX_IO_IRQSTATUS %04x\n",
1678 1679 1680
			irq_count++ /* debug-only */,
			status
	);
1681

1682
	if (status & IRQ_TIMER) {
1683 1684 1685 1686
		/* snd_azf3328_dbgplay("timer %ld\n",
			snd_azf3328_codec_inl(chip, IDX_IO_TIMER_VALUE)
				& TIMER_VALUE_MASK
		); */
A
Andreas Mohr 已提交
1687 1688 1689 1690
		if (chip->timer)
			snd_timer_interrupt(chip->timer, chip->timer->sticks);
		/* ACK timer */
                spin_lock(&chip->reg_lock);
1691
		snd_azf3328_ctrl_outb(chip, IDX_IO_TIMER_VALUE + 3, 0x07);
A
Andreas Mohr 已提交
1692 1693 1694 1695
		spin_unlock(&chip->reg_lock);
		snd_azf3328_dbgplay("azt3328: timer IRQ\n");
	}

1696 1697
	if (status & (IRQ_PLAYBACK|IRQ_RECORDING|IRQ_I2S_OUT))
		snd_azf3328_codec_interrupt(chip, status);
A
Andreas Mohr 已提交
1698

1699 1700
	if (status & IRQ_GAMEPORT)
		snd_azf3328_gameport_interrupt(chip);
1701

A
Andreas Mohr 已提交
1702 1703
	/* MPU401 has less critical IRQ requirements
	 * than timer and playback/recording, right? */
1704
	if (status & IRQ_MPU401) {
1705
		snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
A
Andreas Mohr 已提交
1706 1707

		/* hmm, do we have to ack the IRQ here somehow?
1708
		 * If so, then I don't know how yet... */
A
Andreas Mohr 已提交
1709 1710
		snd_azf3328_dbgplay("azt3328: MPU401 IRQ\n");
	}
L
Linus Torvalds 已提交
1711 1712 1713 1714 1715
	return IRQ_HANDLED;
}

/*****************************************************************/

1716 1717 1718 1719 1720
/* 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 =
L
Linus Torvalds 已提交
1721 1722
{
	/* FIXME!! Correct? */
A
Andreas Mohr 已提交
1723 1724 1725 1726 1727 1728 1729 1730 1731 1732
	.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,
1733 1734
	.rate_min =		AZF_FREQ_4000,
	.rate_max =		AZF_FREQ_66200,
L
Linus Torvalds 已提交
1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749
	.channels_min =		1,
	.channels_max =		2,
	.buffer_bytes_max =	65536,
	.period_bytes_min =	64,
	.period_bytes_max =	65536,
	.periods_min =		1,
	.periods_max =		1024,
	/* 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,
};


static unsigned int snd_azf3328_fixed_rates[] = {
1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765
	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
};

1766
static struct snd_pcm_hw_constraint_list snd_azf3328_hw_constraints_rates = {
1767
	.count = ARRAY_SIZE(snd_azf3328_fixed_rates),
L
Linus Torvalds 已提交
1768 1769 1770 1771 1772 1773
	.list = snd_azf3328_fixed_rates,
	.mask = 0,
};

/*****************************************************************/

A
Andreas Mohr 已提交
1774
static int
1775 1776 1777
snd_azf3328_pcm_open(struct snd_pcm_substream *substream,
		     enum snd_azf3328_codec_type codec_type
)
L
Linus Torvalds 已提交
1778
{
1779 1780
	struct snd_azf3328 *chip = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
L
Linus Torvalds 已提交
1781 1782

	snd_azf3328_dbgcallenter();
1783 1784 1785 1786 1787
	chip->codecs[codec_type].substream = substream;

	/* same parameters for all our codecs - at least we think so... */
	runtime->hw = snd_azf3328_hardware;

L
Linus Torvalds 已提交
1788 1789 1790 1791 1792 1793
	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
				   &snd_azf3328_hw_constraints_rates);
	snd_azf3328_dbgcallleave();
	return 0;
}

1794 1795 1796 1797 1798 1799
static int
snd_azf3328_playback_open(struct snd_pcm_substream *substream)
{
	return snd_azf3328_pcm_open(substream, AZF_CODEC_PLAYBACK);
}

A
Andreas Mohr 已提交
1800
static int
1801
snd_azf3328_capture_open(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
1802
{
1803 1804
	return snd_azf3328_pcm_open(substream, AZF_CODEC_CAPTURE);
}
L
Linus Torvalds 已提交
1805

1806 1807 1808 1809
static int
snd_azf3328_i2s_out_open(struct snd_pcm_substream *substream)
{
	return snd_azf3328_pcm_open(substream, AZF_CODEC_I2S_OUT);
L
Linus Torvalds 已提交
1810 1811
}

A
Andreas Mohr 已提交
1812
static int
1813 1814 1815
snd_azf3328_pcm_close(struct snd_pcm_substream *substream,
		      enum snd_azf3328_codec_type codec_type
)
L
Linus Torvalds 已提交
1816
{
1817
	struct snd_azf3328 *chip = snd_pcm_substream_chip(substream);
L
Linus Torvalds 已提交
1818 1819

	snd_azf3328_dbgcallenter();
1820
	chip->codecs[codec_type].substream = NULL;
L
Linus Torvalds 已提交
1821 1822 1823 1824
	snd_azf3328_dbgcallleave();
	return 0;
}

1825 1826 1827 1828 1829 1830
static int
snd_azf3328_playback_close(struct snd_pcm_substream *substream)
{
	return snd_azf3328_pcm_close(substream, AZF_CODEC_PLAYBACK);
}

A
Andreas Mohr 已提交
1831
static int
1832
snd_azf3328_capture_close(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
1833
{
1834 1835
	return snd_azf3328_pcm_close(substream, AZF_CODEC_CAPTURE);
}
L
Linus Torvalds 已提交
1836

1837 1838 1839 1840
static int
snd_azf3328_i2s_out_close(struct snd_pcm_substream *substream)
{
	return snd_azf3328_pcm_close(substream, AZF_CODEC_I2S_OUT);
L
Linus Torvalds 已提交
1841 1842 1843 1844
}

/******************************************************************/

1845
static struct snd_pcm_ops snd_azf3328_playback_ops = {
L
Linus Torvalds 已提交
1846 1847 1848 1849 1850
	.open =		snd_azf3328_playback_open,
	.close =	snd_azf3328_playback_close,
	.ioctl =	snd_pcm_lib_ioctl,
	.hw_params =	snd_azf3328_hw_params,
	.hw_free =	snd_azf3328_hw_free,
1851 1852 1853
	.prepare =	snd_azf3328_codec_prepare,
	.trigger =	snd_azf3328_codec_playback_trigger,
	.pointer =	snd_azf3328_codec_playback_pointer
L
Linus Torvalds 已提交
1854 1855
};

1856
static struct snd_pcm_ops snd_azf3328_capture_ops = {
L
Linus Torvalds 已提交
1857 1858 1859 1860 1861
	.open =		snd_azf3328_capture_open,
	.close =	snd_azf3328_capture_close,
	.ioctl =	snd_pcm_lib_ioctl,
	.hw_params =	snd_azf3328_hw_params,
	.hw_free =	snd_azf3328_hw_free,
1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875
	.prepare =	snd_azf3328_codec_prepare,
	.trigger =	snd_azf3328_codec_capture_trigger,
	.pointer =	snd_azf3328_codec_capture_pointer
};

static struct snd_pcm_ops snd_azf3328_i2s_out_ops = {
	.open =		snd_azf3328_i2s_out_open,
	.close =	snd_azf3328_i2s_out_close,
	.ioctl =	snd_pcm_lib_ioctl,
	.hw_params =	snd_azf3328_hw_params,
	.hw_free =	snd_azf3328_hw_free,
	.prepare =	snd_azf3328_codec_prepare,
	.trigger =	snd_azf3328_codec_i2s_out_trigger,
	.pointer =	snd_azf3328_codec_i2s_out_pointer
L
Linus Torvalds 已提交
1876 1877
};

A
Andreas Mohr 已提交
1878
static int __devinit
1879
snd_azf3328_pcm(struct snd_azf3328 *chip)
L
Linus Torvalds 已提交
1880
{
1881 1882
enum { AZF_PCMDEV_STD, AZF_PCMDEV_I2S_OUT, NUM_AZF_PCMDEVS }; /* pcm devices */

1883
	struct snd_pcm *pcm;
L
Linus Torvalds 已提交
1884 1885 1886
	int err;

	snd_azf3328_dbgcallenter();
1887 1888 1889 1890

	err = snd_pcm_new(chip->card, "AZF3328 DSP", AZF_PCMDEV_STD,
								1, 1, &pcm);
	if (err < 0)
L
Linus Torvalds 已提交
1891
		return err;
1892 1893 1894 1895
	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);
L
Linus Torvalds 已提交
1896 1897 1898 1899

	pcm->private_data = chip;
	pcm->info_flags = 0;
	strcpy(pcm->name, chip->card->shortname);
1900 1901 1902
	/* same pcm object for playback/capture (see snd_pcm_new() above) */
	chip->pcm[AZF_CODEC_PLAYBACK] = pcm;
	chip->pcm[AZF_CODEC_CAPTURE] = pcm;
L
Linus Torvalds 已提交
1903 1904

	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922
						snd_dma_pci_data(chip->pci),
							64*1024, 64*1024);

	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;

	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
						snd_dma_pci_data(chip->pci),
							64*1024, 64*1024);
L
Linus Torvalds 已提交
1923 1924 1925 1926 1927 1928 1929

	snd_azf3328_dbgcallleave();
	return 0;
}

/******************************************************************/

1930 1931
/*** NOTE: the physical timer resolution actually is 1024000 ticks per second
 *** (probably derived from main crystal via a divider of 24),
A
Andreas Mohr 已提交
1932 1933 1934 1935 1936 1937 1938 1939 1940
 *** 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
1941
snd_azf3328_timer_start(struct snd_timer *timer)
A
Andreas Mohr 已提交
1942
{
1943
	struct snd_azf3328 *chip;
A
Andreas Mohr 已提交
1944 1945 1946 1947 1948 1949
	unsigned long flags;
	unsigned int delay;

	snd_azf3328_dbgcallenter();
	chip = snd_timer_chip(timer);
	delay = ((timer->sticks * seqtimer_scaling) - 1) & TIMER_VALUE_MASK;
1950
	if (delay < 49) {
A
Andreas Mohr 已提交
1951 1952 1953 1954 1955 1956 1957 1958
		/* 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) */

		snd_azf3328_dbgtimer("delay was too low (%d)!\n", delay);
		delay = 49; /* minimum time is 49 ticks */
	}
	snd_azf3328_dbgtimer("setting timer countdown value %d, add COUNTDOWN|IRQ\n", delay);
1959
	delay |= TIMER_COUNTDOWN_ENABLE | TIMER_IRQ_ENABLE;
A
Andreas Mohr 已提交
1960
	spin_lock_irqsave(&chip->reg_lock, flags);
1961
	snd_azf3328_ctrl_outl(chip, IDX_IO_TIMER_VALUE, delay);
A
Andreas Mohr 已提交
1962 1963 1964 1965 1966 1967
	spin_unlock_irqrestore(&chip->reg_lock, flags);
	snd_azf3328_dbgcallleave();
	return 0;
}

static int
1968
snd_azf3328_timer_stop(struct snd_timer *timer)
A
Andreas Mohr 已提交
1969
{
1970
	struct snd_azf3328 *chip;
A
Andreas Mohr 已提交
1971 1972 1973 1974 1975 1976
	unsigned long flags;

	snd_azf3328_dbgcallenter();
	chip = snd_timer_chip(timer);
	spin_lock_irqsave(&chip->reg_lock, flags);
	/* disable timer countdown and interrupt */
1977
	/* FIXME: should we write TIMER_IRQ_ACK here? */
1978
	snd_azf3328_ctrl_outb(chip, IDX_IO_TIMER_VALUE + 3, 0);
A
Andreas Mohr 已提交
1979 1980 1981 1982 1983 1984 1985
	spin_unlock_irqrestore(&chip->reg_lock, flags);
	snd_azf3328_dbgcallleave();
	return 0;
}


static int
1986
snd_azf3328_timer_precise_resolution(struct snd_timer *timer,
A
Andreas Mohr 已提交
1987 1988 1989 1990 1991 1992 1993 1994 1995
					       unsigned long *num, unsigned long *den)
{
	snd_azf3328_dbgcallenter();
	*num = 1;
	*den = 1024000 / seqtimer_scaling;
	snd_azf3328_dbgcallleave();
	return 0;
}

1996
static struct snd_timer_hardware snd_azf3328_timer_hw = {
A
Andreas Mohr 已提交
1997 1998 1999 2000 2001 2002 2003 2004 2005
	.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,
};

static int __devinit
2006
snd_azf3328_timer(struct snd_azf3328 *chip, int device)
A
Andreas Mohr 已提交
2007
{
2008 2009
	struct snd_timer *timer = NULL;
	struct snd_timer_id tid;
A
Andreas Mohr 已提交
2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020
	int err;

	snd_azf3328_dbgcallenter();
	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;
2021 2022 2023

	err = snd_timer_new(chip->card, "AZF3328", &tid, &timer);
	if (err < 0)
A
Andreas Mohr 已提交
2024 2025 2026 2027 2028 2029 2030 2031
		goto out;

	strcpy(timer->name, "AZF3328 timer");
	timer->private_data = chip;
	timer->hw = snd_azf3328_timer_hw;

	chip->timer = timer;

2032 2033
	snd_azf3328_timer_stop(timer);

A
Andreas Mohr 已提交
2034 2035 2036 2037 2038 2039 2040 2041 2042
	err = 0;

out:
	snd_azf3328_dbgcallleave();
	return err;
}

/******************************************************************/

2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076
static int
snd_azf3328_free(struct snd_azf3328 *chip)
{
	if (chip->irq < 0)
		goto __end_hw;

	/* reset (close) mixer:
	 * first mute master volume, then reset
	 */
	snd_azf3328_mixer_set_mute(chip, IDX_MIXER_PLAY_MASTER, 1);
	snd_azf3328_mixer_outw(chip, IDX_MIXER_RESET, 0x0000);

	snd_azf3328_timer_stop(chip->timer);
	snd_azf3328_gameport_free(chip);

	if (chip->irq >= 0)
		synchronize_irq(chip->irq);
__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);
}

L
Linus Torvalds 已提交
2077 2078
#if 0
/* check whether a bit can be modified */
A
Andreas Mohr 已提交
2079
static void
2080
snd_azf3328_test_bit(unsigned unsigned reg, int bit)
L
Linus Torvalds 已提交
2081 2082 2083 2084 2085 2086 2087 2088 2089 2090
{
	unsigned char val, valoff, valon;

	val = inb(reg);

	outb(val & ~(1 << bit), reg);
	valoff = inb(reg);

	outb(val|(1 << bit), reg);
	valon = inb(reg);
2091

L
Linus Torvalds 已提交
2092 2093
	outb(val, reg);

2094 2095 2096
	printk(KERN_ERR "reg %04x bit %d: %02x %02x %02x\n",
				reg, bit, val, valoff, valon
	);
L
Linus Torvalds 已提交
2097 2098 2099
}
#endif

2100
static inline void
2101
snd_azf3328_debug_show_ports(const struct snd_azf3328 *chip)
A
Andreas Mohr 已提交
2102
{
2103
#if DEBUG_MISC
A
Andreas Mohr 已提交
2104 2105
	u16 tmp;

2106
	snd_azf3328_dbgmisc(
2107
		"ctrl_io 0x%lx, game_io 0x%lx, mpu_io 0x%lx, "
2108
		"opl3_io 0x%lx, mixer_io 0x%lx, irq %d\n",
2109
		chip->ctrl_io, chip->game_io, chip->mpu_io,
2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140
		chip->opl3_io, chip->mixer_io, chip->irq
	);

	snd_azf3328_dbgmisc("game %02x %02x %02x %02x %02x %02x\n",
		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),
		snd_azf3328_game_inb(chip, 5)
	);

	for (tmp = 0; tmp < 0x07; tmp += 1)
		snd_azf3328_dbgmisc("mpu_io 0x%04x\n", inb(chip->mpu_io + tmp));

	for (tmp = 0; tmp <= 0x07; tmp += 1)
		snd_azf3328_dbgmisc("0x%02x: game200 0x%04x, game208 0x%04x\n",
			tmp, inb(0x200 + tmp), inb(0x208 + tmp));

	for (tmp = 0; tmp <= 0x01; tmp += 1)
		snd_azf3328_dbgmisc(
			"0x%02x: mpu300 0x%04x, mpu310 0x%04x, mpu320 0x%04x, "
			"mpu330 0x%04x opl388 0x%04x opl38c 0x%04x\n",
				tmp,
				inb(0x300 + tmp),
				inb(0x310 + tmp),
				inb(0x320 + tmp),
				inb(0x330 + tmp),
				inb(0x388 + tmp),
				inb(0x38c + tmp)
		);
A
Andreas Mohr 已提交
2141

2142 2143 2144
	for (tmp = 0; tmp < AZF_IO_SIZE_CTRL; tmp += 2)
		snd_azf3328_dbgmisc("ctrl 0x%02x: 0x%04x\n",
			tmp, snd_azf3328_ctrl_inw(chip, tmp)
2145
		);
2146 2147

	for (tmp = 0; tmp < AZF_IO_SIZE_MIXER; tmp += 2)
2148 2149 2150 2151
		snd_azf3328_dbgmisc("mixer 0x%02x: 0x%04x\n",
			tmp, snd_azf3328_mixer_inw(chip, tmp)
		);
#endif /* DEBUG_MISC */
A
Andreas Mohr 已提交
2152 2153 2154
}

static int __devinit
2155
snd_azf3328_create(struct snd_card *card,
2156 2157 2158
		   struct pci_dev *pci,
		   unsigned long device_type,
		   struct snd_azf3328 **rchip)
L
Linus Torvalds 已提交
2159
{
2160
	struct snd_azf3328 *chip;
L
Linus Torvalds 已提交
2161
	int err;
2162
	static struct snd_device_ops ops = {
L
Linus Torvalds 已提交
2163 2164
		.dev_free =     snd_azf3328_dev_free,
	};
2165 2166
	u8 dma_init;
	enum snd_azf3328_codec_type codec_type;
L
Linus Torvalds 已提交
2167 2168 2169

	*rchip = NULL;

2170 2171
	err = pci_enable_device(pci);
	if (err < 0)
L
Linus Torvalds 已提交
2172 2173
		return err;

2174
	chip = kzalloc(sizeof(*chip), GFP_KERNEL);
L
Linus Torvalds 已提交
2175
	if (chip == NULL) {
A
Andreas Mohr 已提交
2176 2177
		err = -ENOMEM;
		goto out_err;
L
Linus Torvalds 已提交
2178 2179 2180 2181 2182 2183 2184
	}
	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 */
2185 2186
	if (pci_set_dma_mask(pci, DMA_BIT_MASK(24)) < 0 ||
	    pci_set_consistent_dma_mask(pci, DMA_BIT_MASK(24)) < 0) {
2187 2188 2189
		snd_printk(KERN_ERR "architecture does not support "
					"24bit PCI busmaster DMA\n"
		);
A
Andreas Mohr 已提交
2190 2191
		err = -ENXIO;
		goto out_err;
L
Linus Torvalds 已提交
2192 2193
	}

2194 2195
	err = pci_request_regions(pci, "Aztech AZF3328");
	if (err < 0)
A
Andreas Mohr 已提交
2196
		goto out_err;
L
Linus Torvalds 已提交
2197

2198
	chip->ctrl_io  = pci_resource_start(pci, 0);
2199 2200
	chip->game_io  = pci_resource_start(pci, 1);
	chip->mpu_io   = pci_resource_start(pci, 2);
2201
	chip->opl3_io  = pci_resource_start(pci, 3);
2202 2203
	chip->mixer_io = pci_resource_start(pci, 4);

2204 2205 2206 2207 2208 2209 2210 2211 2212
	chip->codecs[AZF_CODEC_PLAYBACK].io_base =
				chip->ctrl_io + AZF_IO_OFFS_CODEC_PLAYBACK;
	chip->codecs[AZF_CODEC_PLAYBACK].name = "PLAYBACK";
	chip->codecs[AZF_CODEC_CAPTURE].io_base =
				chip->ctrl_io + AZF_IO_OFFS_CODEC_CAPTURE;
	chip->codecs[AZF_CODEC_CAPTURE].name = "CAPTURE";
	chip->codecs[AZF_CODEC_I2S_OUT].io_base =
				chip->ctrl_io + AZF_IO_OFFS_CODEC_I2S_OUT;
	chip->codecs[AZF_CODEC_I2S_OUT].name = "I2S_OUT";
L
Linus Torvalds 已提交
2213

2214 2215
	if (request_irq(pci->irq, snd_azf3328_interrupt,
			IRQF_SHARED, card->shortname, chip)) {
2216
		snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
A
Andreas Mohr 已提交
2217 2218
		err = -EBUSY;
		goto out_err;
L
Linus Torvalds 已提交
2219 2220 2221 2222 2223
	}
	chip->irq = pci->irq;
	pci_set_master(pci);
	synchronize_irq(chip->irq);

A
Andreas Mohr 已提交
2224
	snd_azf3328_debug_show_ports(chip);
2225 2226 2227

	err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
	if (err < 0)
A
Andreas Mohr 已提交
2228
		goto out_err;
L
Linus Torvalds 已提交
2229 2230

	/* create mixer interface & switches */
2231 2232
	err = snd_azf3328_mixer_new(chip);
	if (err < 0)
A
Andreas Mohr 已提交
2233
		goto out_err;
L
Linus Torvalds 已提交
2234

2235 2236 2237 2238 2239 2240 2241 2242
	/* 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];
L
Linus Torvalds 已提交
2243

2244 2245 2246 2247
		/* shutdown codecs to save power */
			/* have ...ctrl_codec_activity() act properly */
		codec->running = 1;
		snd_azf3328_ctrl_codec_activity(chip, codec_type, 0);
L
Linus Torvalds 已提交
2248

2249 2250 2251 2252 2253
		spin_lock_irq(&chip->reg_lock);
		snd_azf3328_codec_outb(codec, IDX_IO_CODEC_DMA_FLAGS,
						 dma_init);
		spin_unlock_irq(&chip->reg_lock);
	}
L
Linus Torvalds 已提交
2254 2255 2256 2257

	snd_card_set_dev(card, &pci->dev);

	*rchip = chip;
A
Andreas Mohr 已提交
2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268

	err = 0;
	goto out;

out_err:
	if (chip)
		snd_azf3328_free(chip);
	pci_disable_device(pci);

out:
	return err;
L
Linus Torvalds 已提交
2269 2270
}

A
Andreas Mohr 已提交
2271 2272
static int __devinit
snd_azf3328_probe(struct pci_dev *pci, const struct pci_device_id *pci_id)
L
Linus Torvalds 已提交
2273 2274
{
	static int dev;
2275 2276 2277
	struct snd_card *card;
	struct snd_azf3328 *chip;
	struct snd_opl3 *opl3;
L
Linus Torvalds 已提交
2278 2279 2280 2281 2282 2283 2284 2285 2286 2287
	int err;

	snd_azf3328_dbgcallenter();
	if (dev >= SNDRV_CARDS)
		return -ENODEV;
	if (!enable[dev]) {
		dev++;
		return -ENOENT;
	}

2288 2289 2290
	err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
	if (err < 0)
		return err;
L
Linus Torvalds 已提交
2291 2292 2293 2294

	strcpy(card->driver, "AZF3328");
	strcpy(card->shortname, "Aztech AZF3328 (PCI168)");

2295 2296
	err = snd_azf3328_create(card, pci, pci_id->driver_data, &chip);
	if (err < 0)
A
Andreas Mohr 已提交
2297
		goto out_err;
L
Linus Torvalds 已提交
2298

2299 2300
	card->private_data = chip;

2301 2302 2303 2304 2305 2306 2307 2308
	err = snd_mpu401_uart_new(
		card, 0, MPU401_HW_MPU401, chip->mpu_io, MPU401_INFO_INTEGRATED,
		pci->irq, 0, &chip->rmidi
	);
	if (err < 0) {
		snd_printk(KERN_ERR "azf3328: no MPU-401 device at 0x%lx?\n",
				chip->mpu_io
		);
A
Andreas Mohr 已提交
2309 2310 2311
		goto out_err;
	}

2312 2313
	err = snd_azf3328_timer(chip, 0);
	if (err < 0)
A
Andreas Mohr 已提交
2314
		goto out_err;
L
Linus Torvalds 已提交
2315

2316
	err = snd_azf3328_pcm(chip);
2317
	if (err < 0)
A
Andreas Mohr 已提交
2318
		goto out_err;
L
Linus Torvalds 已提交
2319

2320
	if (snd_opl3_create(card, chip->opl3_io, chip->opl3_io+2,
L
Linus Torvalds 已提交
2321
			    OPL3_HW_AUTO, 1, &opl3) < 0) {
2322
		snd_printk(KERN_ERR "azf3328: no OPL3 device at 0x%lx-0x%lx?\n",
2323 2324
			   chip->opl3_io, chip->opl3_io+2
		);
L
Linus Torvalds 已提交
2325
	} else {
2326 2327 2328 2329 2330 2331
		/* 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)
A
Andreas Mohr 已提交
2332
			goto out_err;
L
Linus Torvalds 已提交
2333 2334
	}

2335 2336
	opl3->private_data = chip;

L
Linus Torvalds 已提交
2337
	sprintf(card->longname, "%s at 0x%lx, irq %i",
2338
		card->shortname, chip->ctrl_io, chip->irq);
L
Linus Torvalds 已提交
2339

2340 2341
	err = snd_card_register(card);
	if (err < 0)
A
Andreas Mohr 已提交
2342
		goto out_err;
L
Linus Torvalds 已提交
2343 2344 2345

#ifdef MODULE
	printk(
2346 2347
"azt3328: Sound driver for Aztech AZF3328-based soundcards such as PCI168.\n"
"azt3328: Hardware was completely undocumented, unfortunately.\n"
A
Andreas Mohr 已提交
2348 2349 2350
"azt3328: Feel free to contact andi AT lisas.de for bug reports etc.!\n"
"azt3328: User-scalable sequencer timer set to %dHz (1024000Hz / %d).\n",
	1024000 / seqtimer_scaling, seqtimer_scaling);
L
Linus Torvalds 已提交
2351 2352
#endif

2353
	snd_azf3328_gameport(chip, dev);
L
Linus Torvalds 已提交
2354 2355 2356 2357

	pci_set_drvdata(pci, card);
	dev++;

A
Andreas Mohr 已提交
2358 2359
	err = 0;
	goto out;
2360

A
Andreas Mohr 已提交
2361
out_err:
2362
	snd_printk(KERN_ERR "azf3328: something failed, exiting\n");
A
Andreas Mohr 已提交
2363
	snd_card_free(card);
2364

A
Andreas Mohr 已提交
2365
out:
L
Linus Torvalds 已提交
2366
	snd_azf3328_dbgcallleave();
A
Andreas Mohr 已提交
2367
	return err;
L
Linus Torvalds 已提交
2368 2369
}

A
Andreas Mohr 已提交
2370 2371
static void __devexit
snd_azf3328_remove(struct pci_dev *pci)
L
Linus Torvalds 已提交
2372 2373 2374 2375 2376 2377 2378
{
	snd_azf3328_dbgcallenter();
	snd_card_free(pci_get_drvdata(pci));
	pci_set_drvdata(pci, NULL);
	snd_azf3328_dbgcallleave();
}

2379 2380 2381 2382 2383 2384
#ifdef CONFIG_PM
static int
snd_azf3328_suspend(struct pci_dev *pci, pm_message_t state)
{
	struct snd_card *card = pci_get_drvdata(pci);
	struct snd_azf3328 *chip = card->private_data;
2385
	unsigned reg;
2386 2387

	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
2388

2389 2390
	snd_pcm_suspend_all(chip->pcm[AZF_CODEC_PLAYBACK]);
	snd_pcm_suspend_all(chip->pcm[AZF_CODEC_I2S_OUT]);
2391

2392 2393
	for (reg = 0; reg < AZF_IO_SIZE_MIXER_PM / 2; ++reg)
		chip->saved_regs_mixer[reg] = inw(chip->mixer_io + reg * 2);
2394 2395 2396 2397

	/* make sure to disable master volume etc. to prevent looping sound */
	snd_azf3328_mixer_set_mute(chip, IDX_MIXER_PLAY_MASTER, 1);
	snd_azf3328_mixer_set_mute(chip, IDX_MIXER_WAVEOUT, 1);
2398

2399 2400
	for (reg = 0; reg < AZF_IO_SIZE_CTRL_PM / 2; ++reg)
		chip->saved_regs_ctrl[reg] = inw(chip->ctrl_io + reg * 2);
2401 2402

	/* manually store the one currently relevant write-only reg, too */
2403
	chip->saved_regs_ctrl[IDX_IO_6AH / 2] = chip->shadow_reg_ctrl_6AH;
2404

2405 2406 2407 2408 2409 2410
	for (reg = 0; reg < AZF_IO_SIZE_GAME_PM / 2; ++reg)
		chip->saved_regs_game[reg] = inw(chip->game_io + reg * 2);
	for (reg = 0; reg < AZF_IO_SIZE_MPU_PM / 2; ++reg)
		chip->saved_regs_mpu[reg] = inw(chip->mpu_io + reg * 2);
	for (reg = 0; reg < AZF_IO_SIZE_OPL3_PM / 2; ++reg)
		chip->saved_regs_opl3[reg] = inw(chip->opl3_io + reg * 2);
2411 2412 2413

	pci_disable_device(pci);
	pci_save_state(pci);
2414
	pci_set_power_state(pci, pci_choose_state(pci, state));
2415 2416 2417 2418 2419 2420 2421
	return 0;
}

static int
snd_azf3328_resume(struct pci_dev *pci)
{
	struct snd_card *card = pci_get_drvdata(pci);
2422
	const struct snd_azf3328 *chip = card->private_data;
2423
	unsigned reg;
2424 2425

	pci_set_power_state(pci, PCI_D0);
2426 2427 2428 2429 2430 2431 2432
	pci_restore_state(pci);
	if (pci_enable_device(pci) < 0) {
		printk(KERN_ERR "azt3328: pci_enable_device failed, "
		       "disabling device\n");
		snd_card_disconnect(card);
		return -EIO;
	}
2433 2434
	pci_set_master(pci);

2435 2436 2437 2438 2439 2440 2441 2442
	for (reg = 0; reg < AZF_IO_SIZE_GAME_PM / 2; ++reg)
		outw(chip->saved_regs_game[reg], chip->game_io + reg * 2);
	for (reg = 0; reg < AZF_IO_SIZE_MPU_PM / 2; ++reg)
		outw(chip->saved_regs_mpu[reg], chip->mpu_io + reg * 2);
	for (reg = 0; reg < AZF_IO_SIZE_OPL3_PM / 2; ++reg)
		outw(chip->saved_regs_opl3[reg], chip->opl3_io + reg * 2);
	for (reg = 0; reg < AZF_IO_SIZE_MIXER_PM / 2; ++reg)
		outw(chip->saved_regs_mixer[reg], chip->mixer_io + reg * 2);
2443 2444
	for (reg = 0; reg < AZF_IO_SIZE_CTRL_PM / 2; ++reg)
		outw(chip->saved_regs_ctrl[reg], chip->ctrl_io + reg * 2);
2445 2446 2447 2448

	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
	return 0;
}
2449
#endif /* CONFIG_PM */
2450 2451


L
Linus Torvalds 已提交
2452 2453 2454 2455 2456
static struct pci_driver driver = {
	.name = "AZF3328",
	.id_table = snd_azf3328_ids,
	.probe = snd_azf3328_probe,
	.remove = __devexit_p(snd_azf3328_remove),
2457 2458 2459 2460
#ifdef CONFIG_PM
	.suspend = snd_azf3328_suspend,
	.resume = snd_azf3328_resume,
#endif
L
Linus Torvalds 已提交
2461 2462
};

A
Andreas Mohr 已提交
2463 2464
static int __init
alsa_card_azf3328_init(void)
L
Linus Torvalds 已提交
2465 2466 2467
{
	int err;
	snd_azf3328_dbgcallenter();
2468
	err = pci_register_driver(&driver);
L
Linus Torvalds 已提交
2469 2470 2471 2472
	snd_azf3328_dbgcallleave();
	return err;
}

A
Andreas Mohr 已提交
2473 2474
static void __exit
alsa_card_azf3328_exit(void)
L
Linus Torvalds 已提交
2475 2476 2477 2478 2479 2480 2481 2482
{
	snd_azf3328_dbgcallenter();
	pci_unregister_driver(&driver);
	snd_azf3328_dbgcallleave();
}

module_init(alsa_card_azf3328_init)
module_exit(alsa_card_azf3328_exit)