blob: 76ff5fdb4838bd463f8c0190e2c36b4c3693851f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * azt3328.c - driver for Aztech AZF3328 based soundcards (e.g. PCI168).
Andreas Mohradf59312010-12-27 21:16:43 +01003 * Copyright (C) 2002, 2005 - 2010 by Andreas Mohr <andi AT lisas.de>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Framework borrowed from Bart Hartgers's als4000.c.
6 * Driver developed on PCI168 AP(W) version (PCI rev. 10, subsystem ID 1801),
7 * found in a Fujitsu-Siemens PC ("Cordant", aluminum case).
8 * Other versions are:
9 * PCI168 A(W), sub ID 1800
10 * PCI168 A/AP, sub ID 8000
11 * Please give me feedback in case you try my driver with one of these!!
12 *
Andreas Mohrdfbf9512009-07-05 13:55:46 +020013 * Keywords: Windows XP Vista 168nt4-125.zip 168win95-125.zip PCI 168 download
14 * (XP/Vista do not support this card at all but every Linux distribution
15 * has very good support out of the box;
16 * just to make sure that the right people hit this and get to know that,
17 * despite the high level of Internet ignorance - as usual :-P -
Andreas Mohr78df6172009-07-12 22:17:54 +020018 * about very good support for this card - on Linux!)
Andreas Mohrdfbf9512009-07-05 13:55:46 +020019 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 * GPL LICENSE
21 * This program is free software; you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License as published by
23 * the Free Software Foundation; either version 2 of the License, or
24 * (at your option) any later version.
25 *
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
30
31 * You should have received a copy of the GNU General Public License
32 * along with this program; if not, write to the Free Software
33 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 *
35 * NOTES
36 * Since Aztech does not provide any chipset documentation,
37 * even on repeated request to various addresses,
38 * and the answer that was finally given was negative
39 * (and I was stupid enough to manage to get hold of a PCI168 soundcard
40 * in the first place >:-P}),
41 * I was forced to base this driver on reverse engineering
42 * (3 weeks' worth of evenings filled with driver work).
Andreas Mohre2f87262006-05-17 11:04:19 +020043 * (and no, I did NOT go the easy way: to pick up a SB PCI128 for 9 Euros)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 *
Andreas Mohr02330fb2008-05-16 12:18:29 +020045 * It is quite likely that the AZF3328 chip is the PCI cousin of the
46 * AZF3318 ("azt1020 pnp", "MM Pro 16") ISA chip, given very similar specs.
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 *
Andreas Mohr02330fb2008-05-16 12:18:29 +020048 * The AZF3328 chip (note: AZF3328, *not* AZT3328, that's just the driver name
49 * for compatibility reasons) from Azfin (joint-venture of Aztech and Fincitec,
50 * Fincitec acquired by National Semiconductor in 2002, together with the
51 * Fincitec-related company ARSmikro) has the following features:
52 *
53 * - compatibility & compliance:
54 * - Microsoft PC 97 ("PC 97 Hardware Design Guide",
55 * http://www.microsoft.com/whdc/archive/pcguides.mspx)
56 * - Microsoft PC 98 Baseline Audio
57 * - MPU401 UART
58 * - Sound Blaster Emulation (DOS Box)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 * - builtin AC97 conformant codec (SNR over 80dB)
Andreas Mohr13769e32006-05-17 11:03:16 +020060 * Note that "conformant" != "compliant"!! this chip's mixer register layout
61 * *differs* from the standard AC97 layout:
62 * they chose to not implement the headphone register (which is not a
63 * problem since it's merely optional), yet when doing this, they committed
64 * the grave sin of letting other registers follow immediately instead of
65 * keeping a headphone dummy register, thereby shifting the mixer register
66 * addresses illegally. So far unfortunately it looks like the very flexible
67 * ALSA AC97 support is still not enough to easily compensate for such a
68 * grave layout violation despite all tweaks and quirks mechanisms it offers.
Andreas Mohr02330fb2008-05-16 12:18:29 +020069 * - builtin genuine OPL3 - verified to work fine, 20080506
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 * - full duplex 16bit playback/record at independent sampling rate
Andreas Mohr02330fb2008-05-16 12:18:29 +020071 * - MPU401 (+ legacy address support, claimed by one official spec sheet)
72 * FIXME: how to enable legacy addr??
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 * - game port (legacy address support)
Andreas Mohre24a1212007-03-26 12:49:45 +020074 * - builtin DirectInput support, helps reduce CPU overhead (interrupt-driven
Andreas Mohr02330fb2008-05-16 12:18:29 +020075 * features supported). - See common term "Digital Enhanced Game Port"...
76 * (probably DirectInput 3.0 spec - confirm)
77 * - builtin 3D enhancement (said to be YAMAHA Ymersion)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 * - built-in General DirectX timer having a 20 bits counter
Andreas Mohrd91c64c2005-10-25 11:17:45 +020079 * with 1us resolution (see below!)
Andreas Mohr02330fb2008-05-16 12:18:29 +020080 * - I2S serial output port for external DAC
Andreas Mohrdfbf9512009-07-05 13:55:46 +020081 * [FIXME: 3.3V or 5V level? maximum rate is 66.2kHz right?]
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 * - supports 33MHz PCI spec 2.1, PCI power management 1.0, compliant with ACPI
83 * - supports hardware volume control
84 * - single chip low cost solution (128 pin QFP)
Andreas Mohrdfbf9512009-07-05 13:55:46 +020085 * - supports programmable Sub-vendor and Sub-system ID [24C02 SEEPROM chip]
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 * required for Microsoft's logo compliance (FIXME: where?)
Andreas Mohr02330fb2008-05-16 12:18:29 +020087 * At least the Trident 4D Wave DX has one bit somewhere
88 * to enable writes to PCI subsystem VID registers, that should be it.
89 * This might easily be in extended PCI reg space, since PCI168 also has
90 * some custom data starting at 0x80. What kind of config settings
91 * are located in our extended PCI space anyway??
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 * - PCI168 AP(W) card: power amplifier with 4 Watts/channel at 4 Ohms
Andreas Mohrdfbf9512009-07-05 13:55:46 +020093 * [TDA1517P chip]
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 *
Andreas Mohrd91c64c2005-10-25 11:17:45 +020095 * Note that this driver now is actually *better* than the Windows driver,
96 * since it additionally supports the card's 1MHz DirectX timer - just try
97 * the following snd-seq module parameters etc.:
98 * - options snd-seq seq_default_timer_class=2 seq_default_timer_sclass=0
99 * seq_default_timer_card=0 seq_client_load=1 seq_default_timer_device=0
100 * seq_default_timer_subdevice=0 seq_default_timer_resolution=1000000
101 * - "timidity -iAv -B2,8 -Os -EFreverb=0"
102 * - "pmidi -p 128:0 jazz.mid"
103 *
Andreas Mohr02330fb2008-05-16 12:18:29 +0200104 * OPL3 hardware playback testing, try something like:
105 * cat /proc/asound/hwdep
106 * and
107 * aconnect -o
108 * Then use
109 * sbiload -Dhw:x,y --opl3 /usr/share/sounds/opl3/std.o3 ......./drums.o3
110 * where x,y is the xx-yy number as given in hwdep.
111 * Then try
112 * pmidi -p a:b jazz.mid
113 * where a:b is the client number plus 0 usually, as given by aconnect above.
114 * Oh, and make sure to unmute the FM mixer control (doh!)
115 * NOTE: power use during OPL3 playback is _VERY_ high (70W --> 90W!)
116 * despite no CPU activity, possibly due to hindering ACPI idling somehow.
117 * Shouldn't be a problem of the AZF3328 chip itself, I'd hope.
118 * Higher PCM / FM mixer levels seem to conflict (causes crackling),
119 * at least sometimes. Maybe even use with hardware sequencer timer above :)
120 * adplay/adplug-utils might soon offer hardware-based OPL3 playback, too.
121 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 * Certain PCI versions of this card are susceptible to DMA traffic underruns
123 * in some systems (resulting in sound crackling/clicking/popping),
124 * probably because they don't have a DMA FIFO buffer or so.
125 * Overview (PCI ID/PCI subID/PCI rev.):
126 * - no DMA crackling on SiS735: 0x50DC/0x1801/16
127 * - unknown performance: 0x50DC/0x1801/10
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200128 * (well, it's not bad on an Athlon 1800 with now very optimized IRQ handler)
129 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 * Crackling happens with VIA chipsets or, in my case, an SiS735, which is
131 * supposed to be very fast and supposed to get rid of crackling much
132 * better than a VIA, yet ironically I still get crackling, like many other
133 * people with the same chipset.
134 * Possible remedies:
Andreas Mohr02330fb2008-05-16 12:18:29 +0200135 * - use speaker (amplifier) output instead of headphone output
136 * (in case crackling is due to overloaded output clipping)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 * - plug card into a different PCI slot, preferrably one that isn't shared
138 * too much (this helps a lot, but not completely!)
139 * - get rid of PCI VGA card, use AGP instead
140 * - upgrade or downgrade BIOS
141 * - fiddle with PCI latency settings (setpci -v -s BUSID latency_timer=XX)
142 * Not too helpful.
143 * - Disable ACPI/power management/"Auto Detect RAM/PCI Clk" in BIOS
Andreas Mohr02330fb2008-05-16 12:18:29 +0200144 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 * BUGS
Andreas Mohr02330fb2008-05-16 12:18:29 +0200146 * - full-duplex might *still* be problematic, however a recent test was fine
Andreas Mohre24a1212007-03-26 12:49:45 +0200147 * - (non-bug) "Bass/Treble or 3D settings don't work" - they do get evaluated
148 * if you set PCM output switch to "pre 3D" instead of "post 3D".
149 * If this can't be set, then get a mixer application that Isn't Stupid (tm)
150 * (e.g. kmix, gamix) - unfortunately several are!!
Andreas Mohr02330fb2008-05-16 12:18:29 +0200151 * - locking is not entirely clean, especially the audio stream activity
152 * ints --> may be racy
153 * - an _unconnected_ secondary joystick at the gameport will be reported
154 * to be "active" (floating values, not precisely -1) due to the way we need
155 * to read the Digital Enhanced Game Port. Not sure whether it is fixable.
156 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 * TODO
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200158 * - use PCI_VDEVICE
159 * - verify driver status on x86_64
160 * - test multi-card driver operation
161 * - (ab)use 1MHz DirectX timer as kernel clocksource
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 * - test MPU401 MIDI playback etc.
Andreas Mohr02330fb2008-05-16 12:18:29 +0200163 * - add more power micro-management (disable various units of the card
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200164 * as long as they're unused, to improve audio quality and save power).
165 * However this requires more I/O ports which I haven't figured out yet
166 * and which thus might not even exist...
Andreas Mohrca54bde2006-05-17 11:02:24 +0200167 * The standard suspend/resume functionality could probably make use of
168 * some improvement, too...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 * - figure out what all unknown port bits are responsible for
Andreas Mohr13769e32006-05-17 11:03:16 +0200170 * - figure out some cleverly evil scheme to possibly make ALSA AC97 code
171 * fully accept our quite incompatible ""AC97"" mixer and thus save some
172 * code (but I'm not too optimistic that doing this is possible at all)
Andreas Mohr02330fb2008-05-16 12:18:29 +0200173 * - use MMIO (memory-mapped I/O)? Slightly faster access, e.g. for gameport.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 */
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176#include <asm/io.h>
177#include <linux/init.h>
178#include <linux/pci.h>
179#include <linux/delay.h>
180#include <linux/slab.h>
181#include <linux/gameport.h>
182#include <linux/moduleparam.h>
Matthias Gehre910638a2006-03-28 01:56:48 -0800183#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184#include <sound/core.h>
185#include <sound/control.h>
186#include <sound/pcm.h>
187#include <sound/rawmidi.h>
188#include <sound/mpu401.h>
189#include <sound/opl3.h>
190#include <sound/initval.h>
191#include "azt3328.h"
192
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200193MODULE_AUTHOR("Andreas Mohr <andi AT lisas.de>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194MODULE_DESCRIPTION("Aztech AZF3328 (PCI168)");
195MODULE_LICENSE("GPL");
196MODULE_SUPPORTED_DEVICE("{{Aztech,AZF3328}}");
197
198#if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
Andreas Mohr02330fb2008-05-16 12:18:29 +0200199#define SUPPORT_GAMEPORT 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200#endif
201
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200202/* === Debug settings ===
203 Further diagnostic functionality than the settings below
Andreas Mohradf59312010-12-27 21:16:43 +0100204 does not need to be provided, since one can easily write a POSIX shell script
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200205 to dump the card's I/O ports (those listed in lspci -v -v):
Andreas Mohradf59312010-12-27 21:16:43 +0100206 dump()
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200207 {
208 local descr=$1; local addr=$2; local count=$3
209
210 echo "${descr}: ${count} @ ${addr}:"
Andreas Mohradf59312010-12-27 21:16:43 +0100211 dd if=/dev/port skip=`printf %d ${addr}` count=${count} bs=1 \
212 2>/dev/null| hexdump -C
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200213 }
214 and then use something like
215 "dump joy200 0x200 8", "dump mpu388 0x388 4", "dump joy 0xb400 8",
216 "dump codec00 0xa800 32", "dump mixer 0xb800 64", "dump synth 0xbc00 8",
217 possibly within a "while true; do ... sleep 1; done" loop.
218 Tweaking ports could be done using
219 VALSTRING="`printf "%02x" $value`"
Andreas Mohradf59312010-12-27 21:16:43 +0100220 printf "\x""$VALSTRING"|dd of=/dev/port seek=`printf %d ${addr}` bs=1 \
221 2>/dev/null
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200222*/
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224#define DEBUG_MISC 0
225#define DEBUG_CALLS 0
226#define DEBUG_MIXER 0
Andreas Mohr78df6172009-07-12 22:17:54 +0200227#define DEBUG_CODEC 0
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200228#define DEBUG_TIMER 0
Andreas Mohr02330fb2008-05-16 12:18:29 +0200229#define DEBUG_GAME 0
Andreas Mohr78df6172009-07-12 22:17:54 +0200230#define DEBUG_PM 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231#define MIXER_TESTING 0
232
233#if DEBUG_MISC
Andreas Mohr78df6172009-07-12 22:17:54 +0200234#define snd_azf3328_dbgmisc(format, args...) printk(KERN_DEBUG format, ##args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235#else
236#define snd_azf3328_dbgmisc(format, args...)
Andreas Mohr02330fb2008-05-16 12:18:29 +0200237#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239#if DEBUG_CALLS
240#define snd_azf3328_dbgcalls(format, args...) printk(format, ##args)
Andreas Mohr78df6172009-07-12 22:17:54 +0200241#define snd_azf3328_dbgcallenter() printk(KERN_DEBUG "--> %s\n", __func__)
242#define snd_azf3328_dbgcallleave() printk(KERN_DEBUG "<-- %s\n", __func__)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243#else
244#define snd_azf3328_dbgcalls(format, args...)
245#define snd_azf3328_dbgcallenter()
246#define snd_azf3328_dbgcallleave()
Andreas Mohr02330fb2008-05-16 12:18:29 +0200247#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249#if DEBUG_MIXER
Takashi Iwaiee419652009-02-05 16:11:31 +0100250#define snd_azf3328_dbgmixer(format, args...) printk(KERN_DEBUG format, ##args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251#else
252#define snd_azf3328_dbgmixer(format, args...)
Andreas Mohr02330fb2008-05-16 12:18:29 +0200253#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Andreas Mohr78df6172009-07-12 22:17:54 +0200255#if DEBUG_CODEC
256#define snd_azf3328_dbgcodec(format, args...) printk(KERN_DEBUG format, ##args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257#else
Andreas Mohr78df6172009-07-12 22:17:54 +0200258#define snd_azf3328_dbgcodec(format, args...)
Andreas Mohr02330fb2008-05-16 12:18:29 +0200259#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200261#if DEBUG_MISC
Takashi Iwaiee419652009-02-05 16:11:31 +0100262#define snd_azf3328_dbgtimer(format, args...) printk(KERN_DEBUG format, ##args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263#else
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200264#define snd_azf3328_dbgtimer(format, args...)
Andreas Mohr02330fb2008-05-16 12:18:29 +0200265#endif
266
267#if DEBUG_GAME
Takashi Iwaiee419652009-02-05 16:11:31 +0100268#define snd_azf3328_dbggame(format, args...) printk(KERN_DEBUG format, ##args)
Andreas Mohr02330fb2008-05-16 12:18:29 +0200269#else
270#define snd_azf3328_dbggame(format, args...)
271#endif
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200272
Andreas Mohr78df6172009-07-12 22:17:54 +0200273#if DEBUG_PM
274#define snd_azf3328_dbgpm(format, args...) printk(KERN_DEBUG format, ##args)
275#else
276#define snd_azf3328_dbgpm(format, args...)
277#endif
278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
280module_param_array(index, int, NULL, 0444);
281MODULE_PARM_DESC(index, "Index value for AZF3328 soundcard.");
282
283static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
284module_param_array(id, charp, NULL, 0444);
285MODULE_PARM_DESC(id, "ID string for AZF3328 soundcard.");
286
287static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
288module_param_array(enable, bool, NULL, 0444);
289MODULE_PARM_DESC(enable, "Enable AZF3328 soundcard.");
290
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200291static int seqtimer_scaling = 128;
292module_param(seqtimer_scaling, int, 0444);
293MODULE_PARM_DESC(seqtimer_scaling, "Set 1024000Hz sequencer timer scale factor (lockup danger!). Default 128.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200295enum snd_azf3328_codec_type {
Andreas Mohradf59312010-12-27 21:16:43 +0100296 /* warning: fixed indices (also used for bitmask checks!) */
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200297 AZF_CODEC_PLAYBACK = 0,
298 AZF_CODEC_CAPTURE = 1,
299 AZF_CODEC_I2S_OUT = 2,
Andreas Mohr02330fb2008-05-16 12:18:29 +0200300};
301
Andreas Mohrda237f32010-12-27 21:17:26 +0100302struct snd_azf3328_codec_data {
303 unsigned long io_base; /* keep first! (avoid offset calc) */
304 unsigned int dma_base; /* helper to avoid an indirection in hotpath */
305 spinlock_t *lock; /* TODO: convert to our own per-codec lock member */
306 struct snd_pcm_substream *substream;
307 bool running;
308 enum snd_azf3328_codec_type type;
309 const char *name;
310};
311
Takashi Iwai95de7762005-11-17 15:02:42 +0100312struct snd_azf3328 {
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200313 /* often-used fields towards beginning, then grouped */
Andreas Mohr02330fb2008-05-16 12:18:29 +0200314
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200315 unsigned long ctrl_io; /* usually 0xb000, size 128 */
Andreas Mohr02330fb2008-05-16 12:18:29 +0200316 unsigned long game_io; /* usually 0xb400, size 8 */
317 unsigned long mpu_io; /* usually 0xb800, size 4 */
318 unsigned long opl3_io; /* usually 0xbc00, size 8 */
319 unsigned long mixer_io; /* usually 0xc000, size 64 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200321 spinlock_t reg_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Takashi Iwai95de7762005-11-17 15:02:42 +0100323 struct snd_timer *timer;
Andreas Mohr02330fb2008-05-16 12:18:29 +0200324
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200325 struct snd_pcm *pcm[3];
326
327 /* playback, recording and I2S out codecs */
328 struct snd_azf3328_codec_data codecs[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Takashi Iwai95de7762005-11-17 15:02:42 +0100330 struct snd_card *card;
331 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Andreas Mohr02330fb2008-05-16 12:18:29 +0200333#ifdef SUPPORT_GAMEPORT
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200334 struct gameport *gameport;
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200335 u16 axes[4];
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200336#endif
337
338 struct pci_dev *pci;
339 int irq;
Andreas Mohrca54bde2006-05-17 11:02:24 +0200340
Andreas Mohr627d3e72008-06-23 11:50:47 +0200341 /* register 0x6a is write-only, thus need to remember setting.
342 * If we need to add more registers here, then we might try to fold this
343 * into some transparent combined shadow register handling with
344 * CONFIG_PM register storage below, but that's slightly difficult. */
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200345 u16 shadow_reg_ctrl_6AH;
Andreas Mohr627d3e72008-06-23 11:50:47 +0200346
Andreas Mohrca54bde2006-05-17 11:02:24 +0200347#ifdef CONFIG_PM
348 /* register value containers for power management
Andreas Mohr78df6172009-07-12 22:17:54 +0200349 * Note: not always full I/O range preserved (similar to Win driver!) */
350 u32 saved_regs_ctrl[AZF_ALIGN(AZF_IO_SIZE_CTRL_PM) / 4];
351 u32 saved_regs_game[AZF_ALIGN(AZF_IO_SIZE_GAME_PM) / 4];
352 u32 saved_regs_mpu[AZF_ALIGN(AZF_IO_SIZE_MPU_PM) / 4];
353 u32 saved_regs_opl3[AZF_ALIGN(AZF_IO_SIZE_OPL3_PM) / 4];
354 u32 saved_regs_mixer[AZF_ALIGN(AZF_IO_SIZE_MIXER_PM) / 4];
Andreas Mohrca54bde2006-05-17 11:02:24 +0200355#endif
Takashi Iwai95de7762005-11-17 15:02:42 +0100356};
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200357
Alexey Dobriyancebe41d2010-02-06 00:21:03 +0200358static DEFINE_PCI_DEVICE_TABLE(snd_azf3328_ids) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 { 0x122D, 0x50DC, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, /* PCI168/3328 */
360 { 0x122D, 0x80DA, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, /* 3328 */
361 { 0, }
362};
363
364MODULE_DEVICE_TABLE(pci, snd_azf3328_ids);
365
Andreas Mohr02330fb2008-05-16 12:18:29 +0200366
367static int
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200368snd_azf3328_io_reg_setb(unsigned reg, u8 mask, bool do_set)
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200369{
Andreas Mohradf59312010-12-27 21:16:43 +0100370 /* Well, strictly spoken, the inb/outb sequence isn't atomic
371 and would need locking. However we currently don't care
372 since it potentially complicates matters. */
Andreas Mohr02330fb2008-05-16 12:18:29 +0200373 u8 prev = inb(reg), new;
374
375 new = (do_set) ? (prev|mask) : (prev & ~mask);
376 /* we need to always write the new value no matter whether it differs
377 * or not, since some register bits don't indicate their setting */
378 outb(new, reg);
379 if (new != prev)
380 return 1;
381
382 return 0;
383}
384
Andreas Mohr02330fb2008-05-16 12:18:29 +0200385static inline void
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200386snd_azf3328_codec_outb(const struct snd_azf3328_codec_data *codec,
387 unsigned reg,
388 u8 value
389)
Andreas Mohr02330fb2008-05-16 12:18:29 +0200390{
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200391 outb(value, codec->io_base + reg);
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200392}
393
394static inline u8
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200395snd_azf3328_codec_inb(const struct snd_azf3328_codec_data *codec, unsigned reg)
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200396{
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200397 return inb(codec->io_base + reg);
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200398}
399
400static inline void
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200401snd_azf3328_codec_outw(const struct snd_azf3328_codec_data *codec,
402 unsigned reg,
403 u16 value
404)
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200405{
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200406 outw(value, codec->io_base + reg);
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200407}
408
409static inline u16
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200410snd_azf3328_codec_inw(const struct snd_azf3328_codec_data *codec, unsigned reg)
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200411{
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200412 return inw(codec->io_base + reg);
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200413}
414
415static inline void
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200416snd_azf3328_codec_outl(const struct snd_azf3328_codec_data *codec,
417 unsigned reg,
418 u32 value
419)
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200420{
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200421 outl(value, codec->io_base + reg);
Andreas Mohr02330fb2008-05-16 12:18:29 +0200422}
423
424static inline u32
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200425snd_azf3328_codec_inl(const struct snd_azf3328_codec_data *codec, unsigned reg)
Andreas Mohr02330fb2008-05-16 12:18:29 +0200426{
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200427 return inl(codec->io_base + reg);
428}
429
430static inline void
431snd_azf3328_ctrl_outb(const struct snd_azf3328 *chip, unsigned reg, u8 value)
432{
433 outb(value, chip->ctrl_io + reg);
434}
435
436static inline u8
437snd_azf3328_ctrl_inb(const struct snd_azf3328 *chip, unsigned reg)
438{
439 return inb(chip->ctrl_io + reg);
440}
441
442static inline void
443snd_azf3328_ctrl_outw(const struct snd_azf3328 *chip, unsigned reg, u16 value)
444{
445 outw(value, chip->ctrl_io + reg);
446}
447
448static inline void
449snd_azf3328_ctrl_outl(const struct snd_azf3328 *chip, unsigned reg, u32 value)
450{
451 outl(value, chip->ctrl_io + reg);
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200452}
453
454static inline void
Andreas Mohr02330fb2008-05-16 12:18:29 +0200455snd_azf3328_game_outb(const struct snd_azf3328 *chip, unsigned reg, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
Andreas Mohr02330fb2008-05-16 12:18:29 +0200457 outb(value, chip->game_io + reg);
458}
459
460static inline void
461snd_azf3328_game_outw(const struct snd_azf3328 *chip, unsigned reg, u16 value)
462{
463 outw(value, chip->game_io + reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200466static inline u8
Andreas Mohr02330fb2008-05-16 12:18:29 +0200467snd_azf3328_game_inb(const struct snd_azf3328 *chip, unsigned reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
Andreas Mohr02330fb2008-05-16 12:18:29 +0200469 return inb(chip->game_io + reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470}
471
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200472static inline u16
Andreas Mohr02330fb2008-05-16 12:18:29 +0200473snd_azf3328_game_inw(const struct snd_azf3328 *chip, unsigned reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
Andreas Mohr02330fb2008-05-16 12:18:29 +0200475 return inw(chip->game_io + reg);
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200476}
477
Andreas Mohr02330fb2008-05-16 12:18:29 +0200478static inline void
479snd_azf3328_mixer_outw(const struct snd_azf3328 *chip, unsigned reg, u16 value)
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200480{
Andreas Mohr02330fb2008-05-16 12:18:29 +0200481 outw(value, chip->mixer_io + reg);
482}
483
484static inline u16
485snd_azf3328_mixer_inw(const struct snd_azf3328 *chip, unsigned reg)
486{
487 return inw(chip->mixer_io + reg);
488}
489
490#define AZF_MUTE_BIT 0x80
491
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200492static bool
Andreas Mohr02330fb2008-05-16 12:18:29 +0200493snd_azf3328_mixer_set_mute(const struct snd_azf3328 *chip,
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200494 unsigned reg, bool do_mute
Andreas Mohr02330fb2008-05-16 12:18:29 +0200495)
496{
497 unsigned long portbase = chip->mixer_io + reg + 1;
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200498 bool updated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 /* the mute bit is on the *second* (i.e. right) register of a
501 * left/right channel setting */
Andreas Mohr02330fb2008-05-16 12:18:29 +0200502 updated = snd_azf3328_io_reg_setb(portbase, AZF_MUTE_BIT, do_mute);
503
504 /* indicate whether it was muted before */
505 return (do_mute) ? !updated : updated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506}
507
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200508static void
Andreas Mohr02330fb2008-05-16 12:18:29 +0200509snd_azf3328_mixer_write_volume_gradually(const struct snd_azf3328 *chip,
510 unsigned reg,
511 unsigned char dst_vol_left,
512 unsigned char dst_vol_right,
513 int chan_sel, int delay
514)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
Andreas Mohr02330fb2008-05-16 12:18:29 +0200516 unsigned long portbase = chip->mixer_io + reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 unsigned char curr_vol_left = 0, curr_vol_right = 0;
Andreas Mohr02330fb2008-05-16 12:18:29 +0200518 int left_change = 0, right_change = 0;
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 snd_azf3328_dbgcallenter();
Andreas Mohr02330fb2008-05-16 12:18:29 +0200521
522 if (chan_sel & SET_CHAN_LEFT) {
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200523 curr_vol_left = inb(portbase + 1);
Andreas Mohr02330fb2008-05-16 12:18:29 +0200524
525 /* take care of muting flag contained in left channel */
526 if (curr_vol_left & AZF_MUTE_BIT)
527 dst_vol_left |= AZF_MUTE_BIT;
528 else
529 dst_vol_left &= ~AZF_MUTE_BIT;
530
531 left_change = (curr_vol_left > dst_vol_left) ? -1 : 1;
532 }
533
534 if (chan_sel & SET_CHAN_RIGHT) {
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200535 curr_vol_right = inb(portbase + 0);
Andreas Mohr02330fb2008-05-16 12:18:29 +0200536
537 right_change = (curr_vol_right > dst_vol_right) ? -1 : 1;
538 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Andreas Mohre2f87262006-05-17 11:04:19 +0200540 do {
Andreas Mohr02330fb2008-05-16 12:18:29 +0200541 if (left_change) {
542 if (curr_vol_left != dst_vol_left) {
543 curr_vol_left += left_change;
544 outb(curr_vol_left, portbase + 1);
545 } else
546 left_change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 }
Andreas Mohr02330fb2008-05-16 12:18:29 +0200548 if (right_change) {
549 if (curr_vol_right != dst_vol_right) {
550 curr_vol_right += right_change;
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 /* during volume change, the right channel is crackling
553 * somewhat more than the left channel, unfortunately.
554 * This seems to be a hardware issue. */
Andreas Mohr02330fb2008-05-16 12:18:29 +0200555 outb(curr_vol_right, portbase + 0);
556 } else
557 right_change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
559 if (delay)
560 mdelay(delay);
Andreas Mohr02330fb2008-05-16 12:18:29 +0200561 } while ((left_change) || (right_change));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 snd_azf3328_dbgcallleave();
563}
564
565/*
566 * general mixer element
567 */
Takashi Iwai95de7762005-11-17 15:02:42 +0100568struct azf3328_mixer_reg {
Andreas Mohr02330fb2008-05-16 12:18:29 +0200569 unsigned reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 unsigned int lchan_shift, rchan_shift;
571 unsigned int mask;
572 unsigned int invert: 1;
573 unsigned int stereo: 1;
574 unsigned int enum_c: 4;
Takashi Iwai95de7762005-11-17 15:02:42 +0100575};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577#define COMPOSE_MIXER_REG(reg,lchan_shift,rchan_shift,mask,invert,stereo,enum_c) \
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200578 ((reg) | (lchan_shift << 8) | (rchan_shift << 12) | \
579 (mask << 16) | \
580 (invert << 24) | \
581 (stereo << 25) | \
582 (enum_c << 26))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Takashi Iwai95de7762005-11-17 15:02:42 +0100584static void snd_azf3328_mixer_reg_decode(struct azf3328_mixer_reg *r, unsigned long val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
586 r->reg = val & 0xff;
587 r->lchan_shift = (val >> 8) & 0x0f;
588 r->rchan_shift = (val >> 12) & 0x0f;
589 r->mask = (val >> 16) & 0xff;
590 r->invert = (val >> 24) & 1;
591 r->stereo = (val >> 25) & 1;
592 r->enum_c = (val >> 26) & 0x0f;
593}
594
595/*
596 * mixer switches/volumes
597 */
598
599#define AZF3328_MIXER_SWITCH(xname, reg, shift, invert) \
600{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
601 .info = snd_azf3328_info_mixer, \
602 .get = snd_azf3328_get_mixer, .put = snd_azf3328_put_mixer, \
603 .private_value = COMPOSE_MIXER_REG(reg, shift, 0, 0x1, invert, 0, 0), \
604}
605
606#define AZF3328_MIXER_VOL_STEREO(xname, reg, mask, invert) \
607{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
608 .info = snd_azf3328_info_mixer, \
609 .get = snd_azf3328_get_mixer, .put = snd_azf3328_put_mixer, \
610 .private_value = COMPOSE_MIXER_REG(reg, 8, 0, mask, invert, 1, 0), \
611}
612
613#define AZF3328_MIXER_VOL_MONO(xname, reg, mask, is_right_chan) \
614{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
615 .info = snd_azf3328_info_mixer, \
616 .get = snd_azf3328_get_mixer, .put = snd_azf3328_put_mixer, \
617 .private_value = COMPOSE_MIXER_REG(reg, is_right_chan ? 0 : 8, 0, mask, 1, 0, 0), \
618}
619
620#define AZF3328_MIXER_VOL_SPECIAL(xname, reg, mask, shift, invert) \
621{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
622 .info = snd_azf3328_info_mixer, \
623 .get = snd_azf3328_get_mixer, .put = snd_azf3328_put_mixer, \
624 .private_value = COMPOSE_MIXER_REG(reg, shift, 0, mask, invert, 0, 0), \
625}
626
627#define AZF3328_MIXER_ENUM(xname, reg, enum_c, shift) \
628{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
629 .info = snd_azf3328_info_mixer_enum, \
630 .get = snd_azf3328_get_mixer_enum, .put = snd_azf3328_put_mixer_enum, \
631 .private_value = COMPOSE_MIXER_REG(reg, shift, 0, 0, 0, 0, enum_c), \
632}
633
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200634static int
Takashi Iwai95de7762005-11-17 15:02:42 +0100635snd_azf3328_info_mixer(struct snd_kcontrol *kcontrol,
636 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
Takashi Iwai95de7762005-11-17 15:02:42 +0100638 struct azf3328_mixer_reg reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640 snd_azf3328_dbgcallenter();
641 snd_azf3328_mixer_reg_decode(&reg, kcontrol->private_value);
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200642 uinfo->type = reg.mask == 1 ?
643 SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 uinfo->count = reg.stereo + 1;
645 uinfo->value.integer.min = 0;
646 uinfo->value.integer.max = reg.mask;
647 snd_azf3328_dbgcallleave();
648 return 0;
649}
650
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200651static int
Takashi Iwai95de7762005-11-17 15:02:42 +0100652snd_azf3328_get_mixer(struct snd_kcontrol *kcontrol,
653 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
Takashi Iwai95de7762005-11-17 15:02:42 +0100655 struct snd_azf3328 *chip = snd_kcontrol_chip(kcontrol);
656 struct azf3328_mixer_reg reg;
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200657 u16 oreg, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659 snd_azf3328_dbgcallenter();
660 snd_azf3328_mixer_reg_decode(&reg, kcontrol->private_value);
661
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200662 oreg = snd_azf3328_mixer_inw(chip, reg.reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 val = (oreg >> reg.lchan_shift) & reg.mask;
664 if (reg.invert)
665 val = reg.mask - val;
666 ucontrol->value.integer.value[0] = val;
667 if (reg.stereo) {
668 val = (oreg >> reg.rchan_shift) & reg.mask;
669 if (reg.invert)
670 val = reg.mask - val;
671 ucontrol->value.integer.value[1] = val;
672 }
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200673 snd_azf3328_dbgmixer("get: %02x is %04x -> vol %02lx|%02lx "
674 "(shift %02d|%02d, mask %02x, inv. %d, stereo %d)\n",
675 reg.reg, oreg,
676 ucontrol->value.integer.value[0], ucontrol->value.integer.value[1],
677 reg.lchan_shift, reg.rchan_shift, reg.mask, reg.invert, reg.stereo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 snd_azf3328_dbgcallleave();
679 return 0;
680}
681
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200682static int
Takashi Iwai95de7762005-11-17 15:02:42 +0100683snd_azf3328_put_mixer(struct snd_kcontrol *kcontrol,
684 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
Takashi Iwai95de7762005-11-17 15:02:42 +0100686 struct snd_azf3328 *chip = snd_kcontrol_chip(kcontrol);
687 struct azf3328_mixer_reg reg;
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200688 u16 oreg, nreg, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 snd_azf3328_dbgcallenter();
691 snd_azf3328_mixer_reg_decode(&reg, kcontrol->private_value);
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200692 oreg = snd_azf3328_mixer_inw(chip, reg.reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 val = ucontrol->value.integer.value[0] & reg.mask;
694 if (reg.invert)
695 val = reg.mask - val;
696 nreg = oreg & ~(reg.mask << reg.lchan_shift);
697 nreg |= (val << reg.lchan_shift);
698 if (reg.stereo) {
699 val = ucontrol->value.integer.value[1] & reg.mask;
700 if (reg.invert)
701 val = reg.mask - val;
702 nreg &= ~(reg.mask << reg.rchan_shift);
703 nreg |= (val << reg.rchan_shift);
704 }
705 if (reg.mask >= 0x07) /* it's a volume control, so better take care */
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200706 snd_azf3328_mixer_write_volume_gradually(
707 chip, reg.reg, nreg >> 8, nreg & 0xff,
708 /* just set both channels, doesn't matter */
709 SET_CHAN_LEFT|SET_CHAN_RIGHT,
710 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 else
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200712 snd_azf3328_mixer_outw(chip, reg.reg, nreg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200714 snd_azf3328_dbgmixer("put: %02x to %02lx|%02lx, "
715 "oreg %04x; shift %02d|%02d -> nreg %04x; after: %04x\n",
716 reg.reg, ucontrol->value.integer.value[0], ucontrol->value.integer.value[1],
717 oreg, reg.lchan_shift, reg.rchan_shift,
718 nreg, snd_azf3328_mixer_inw(chip, reg.reg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 snd_azf3328_dbgcallleave();
720 return (nreg != oreg);
721}
722
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200723static int
Takashi Iwai95de7762005-11-17 15:02:42 +0100724snd_azf3328_info_mixer_enum(struct snd_kcontrol *kcontrol,
725 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200727 static const char * const texts1[] = {
Andreas Mohr13769e32006-05-17 11:03:16 +0200728 "Mic1", "Mic2"
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200729 };
730 static const char * const texts2[] = {
Andreas Mohr13769e32006-05-17 11:03:16 +0200731 "Mix", "Mic"
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200732 };
733 static const char * const texts3[] = {
Andreas Mohr02330fb2008-05-16 12:18:29 +0200734 "Mic", "CD", "Video", "Aux",
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200735 "Line", "Mix", "Mix Mono", "Phone"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 };
Andreas Mohr13769e32006-05-17 11:03:16 +0200737 static const char * const texts4[] = {
738 "pre 3D", "post 3D"
739 };
Takashi Iwai95de7762005-11-17 15:02:42 +0100740 struct azf3328_mixer_reg reg;
Andreas Mohr627d3e72008-06-23 11:50:47 +0200741 const char * const *p = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
743 snd_azf3328_mixer_reg_decode(&reg, kcontrol->private_value);
744 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
745 uinfo->count = (reg.reg == IDX_MIXER_REC_SELECT) ? 2 : 1;
746 uinfo->value.enumerated.items = reg.enum_c;
747 if (uinfo->value.enumerated.item > reg.enum_c - 1U)
748 uinfo->value.enumerated.item = reg.enum_c - 1U;
Andreas Mohre2f87262006-05-17 11:04:19 +0200749 if (reg.reg == IDX_MIXER_ADVCTL2) {
Andreas Mohr13769e32006-05-17 11:03:16 +0200750 switch(reg.lchan_shift) {
751 case 8: /* modem out sel */
Andreas Mohr627d3e72008-06-23 11:50:47 +0200752 p = texts1;
Andreas Mohr13769e32006-05-17 11:03:16 +0200753 break;
754 case 9: /* mono sel source */
Andreas Mohr627d3e72008-06-23 11:50:47 +0200755 p = texts2;
Andreas Mohr13769e32006-05-17 11:03:16 +0200756 break;
757 case 15: /* PCM Out Path */
Andreas Mohr627d3e72008-06-23 11:50:47 +0200758 p = texts4;
Andreas Mohr13769e32006-05-17 11:03:16 +0200759 break;
760 }
Andreas Mohre2f87262006-05-17 11:04:19 +0200761 } else
Andreas Mohr02330fb2008-05-16 12:18:29 +0200762 if (reg.reg == IDX_MIXER_REC_SELECT)
Andreas Mohr627d3e72008-06-23 11:50:47 +0200763 p = texts3;
Andreas Mohr02330fb2008-05-16 12:18:29 +0200764
Andreas Mohr627d3e72008-06-23 11:50:47 +0200765 strcpy(uinfo->value.enumerated.name, p[uinfo->value.enumerated.item]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 return 0;
767}
768
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200769static int
Takashi Iwai95de7762005-11-17 15:02:42 +0100770snd_azf3328_get_mixer_enum(struct snd_kcontrol *kcontrol,
771 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
Takashi Iwai95de7762005-11-17 15:02:42 +0100773 struct snd_azf3328 *chip = snd_kcontrol_chip(kcontrol);
774 struct azf3328_mixer_reg reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 unsigned short val;
Andreas Mohr02330fb2008-05-16 12:18:29 +0200776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 snd_azf3328_mixer_reg_decode(&reg, kcontrol->private_value);
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200778 val = snd_azf3328_mixer_inw(chip, reg.reg);
Andreas Mohre2f87262006-05-17 11:04:19 +0200779 if (reg.reg == IDX_MIXER_REC_SELECT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 ucontrol->value.enumerated.item[0] = (val >> 8) & (reg.enum_c - 1);
781 ucontrol->value.enumerated.item[1] = (val >> 0) & (reg.enum_c - 1);
Andreas Mohre2f87262006-05-17 11:04:19 +0200782 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 ucontrol->value.enumerated.item[0] = (val >> reg.lchan_shift) & (reg.enum_c - 1);
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200784
785 snd_azf3328_dbgmixer("get_enum: %02x is %04x -> %d|%d (shift %02d, enum_c %d)\n",
786 reg.reg, val, ucontrol->value.enumerated.item[0], ucontrol->value.enumerated.item[1],
787 reg.lchan_shift, reg.enum_c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 return 0;
789}
790
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200791static int
Takashi Iwai95de7762005-11-17 15:02:42 +0100792snd_azf3328_put_mixer_enum(struct snd_kcontrol *kcontrol,
793 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
Takashi Iwai95de7762005-11-17 15:02:42 +0100795 struct snd_azf3328 *chip = snd_kcontrol_chip(kcontrol);
796 struct azf3328_mixer_reg reg;
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200797 u16 oreg, nreg, val;
Andreas Mohr02330fb2008-05-16 12:18:29 +0200798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 snd_azf3328_mixer_reg_decode(&reg, kcontrol->private_value);
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200800 oreg = snd_azf3328_mixer_inw(chip, reg.reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 val = oreg;
Andreas Mohre2f87262006-05-17 11:04:19 +0200802 if (reg.reg == IDX_MIXER_REC_SELECT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 if (ucontrol->value.enumerated.item[0] > reg.enum_c - 1U ||
804 ucontrol->value.enumerated.item[1] > reg.enum_c - 1U)
805 return -EINVAL;
806 val = (ucontrol->value.enumerated.item[0] << 8) |
807 (ucontrol->value.enumerated.item[1] << 0);
Andreas Mohre2f87262006-05-17 11:04:19 +0200808 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 if (ucontrol->value.enumerated.item[0] > reg.enum_c - 1U)
810 return -EINVAL;
811 val &= ~((reg.enum_c - 1) << reg.lchan_shift);
812 val |= (ucontrol->value.enumerated.item[0] << reg.lchan_shift);
813 }
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200814 snd_azf3328_mixer_outw(chip, reg.reg, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 nreg = val;
816
817 snd_azf3328_dbgmixer("put_enum: %02x to %04x, oreg %04x\n", reg.reg, val, oreg);
818 return (nreg != oreg);
819}
820
Takashi Iwai1b60f6b2007-03-13 22:13:47 +0100821static struct snd_kcontrol_new snd_azf3328_mixer_controls[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 AZF3328_MIXER_SWITCH("Master Playback Switch", IDX_MIXER_PLAY_MASTER, 15, 1),
823 AZF3328_MIXER_VOL_STEREO("Master Playback Volume", IDX_MIXER_PLAY_MASTER, 0x1f, 1),
Andreas Mohr627d3e72008-06-23 11:50:47 +0200824 AZF3328_MIXER_SWITCH("PCM Playback Switch", IDX_MIXER_WAVEOUT, 15, 1),
825 AZF3328_MIXER_VOL_STEREO("PCM Playback Volume",
826 IDX_MIXER_WAVEOUT, 0x1f, 1),
827 AZF3328_MIXER_SWITCH("PCM 3D Bypass Playback Switch",
828 IDX_MIXER_ADVCTL2, 7, 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 AZF3328_MIXER_SWITCH("FM Playback Switch", IDX_MIXER_FMSYNTH, 15, 1),
830 AZF3328_MIXER_VOL_STEREO("FM Playback Volume", IDX_MIXER_FMSYNTH, 0x1f, 1),
831 AZF3328_MIXER_SWITCH("CD Playback Switch", IDX_MIXER_CDAUDIO, 15, 1),
832 AZF3328_MIXER_VOL_STEREO("CD Playback Volume", IDX_MIXER_CDAUDIO, 0x1f, 1),
833 AZF3328_MIXER_SWITCH("Capture Switch", IDX_MIXER_REC_VOLUME, 15, 1),
834 AZF3328_MIXER_VOL_STEREO("Capture Volume", IDX_MIXER_REC_VOLUME, 0x0f, 0),
835 AZF3328_MIXER_ENUM("Capture Source", IDX_MIXER_REC_SELECT, 8, 0),
836 AZF3328_MIXER_SWITCH("Mic Playback Switch", IDX_MIXER_MIC, 15, 1),
837 AZF3328_MIXER_VOL_MONO("Mic Playback Volume", IDX_MIXER_MIC, 0x1f, 1),
838 AZF3328_MIXER_SWITCH("Mic Boost (+20dB)", IDX_MIXER_MIC, 6, 0),
839 AZF3328_MIXER_SWITCH("Line Playback Switch", IDX_MIXER_LINEIN, 15, 1),
840 AZF3328_MIXER_VOL_STEREO("Line Playback Volume", IDX_MIXER_LINEIN, 0x1f, 1),
Jaroslav Kyselad355c82a2009-11-03 15:47:25 +0100841 AZF3328_MIXER_SWITCH("Beep Playback Switch", IDX_MIXER_PCBEEP, 15, 1),
842 AZF3328_MIXER_VOL_SPECIAL("Beep Playback Volume", IDX_MIXER_PCBEEP, 0x0f, 1, 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 AZF3328_MIXER_SWITCH("Video Playback Switch", IDX_MIXER_VIDEO, 15, 1),
844 AZF3328_MIXER_VOL_STEREO("Video Playback Volume", IDX_MIXER_VIDEO, 0x1f, 1),
845 AZF3328_MIXER_SWITCH("Aux Playback Switch", IDX_MIXER_AUX, 15, 1),
846 AZF3328_MIXER_VOL_STEREO("Aux Playback Volume", IDX_MIXER_AUX, 0x1f, 1),
847 AZF3328_MIXER_SWITCH("Modem Playback Switch", IDX_MIXER_MODEMOUT, 15, 1),
848 AZF3328_MIXER_VOL_MONO("Modem Playback Volume", IDX_MIXER_MODEMOUT, 0x1f, 1),
849 AZF3328_MIXER_SWITCH("Modem Capture Switch", IDX_MIXER_MODEMIN, 15, 1),
850 AZF3328_MIXER_VOL_MONO("Modem Capture Volume", IDX_MIXER_MODEMIN, 0x1f, 1),
Andreas Mohr13769e32006-05-17 11:03:16 +0200851 AZF3328_MIXER_ENUM("Mic Select", IDX_MIXER_ADVCTL2, 2, 8),
852 AZF3328_MIXER_ENUM("Mono Output Select", IDX_MIXER_ADVCTL2, 2, 9),
Andreas Mohre24a1212007-03-26 12:49:45 +0200853 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! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 AZF3328_MIXER_VOL_SPECIAL("Tone Control - Treble", IDX_MIXER_BASSTREBLE, 0x07, 1, 0),
855 AZF3328_MIXER_VOL_SPECIAL("Tone Control - Bass", IDX_MIXER_BASSTREBLE, 0x07, 9, 0),
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200856 AZF3328_MIXER_SWITCH("3D Control - Switch", IDX_MIXER_ADVCTL2, 13, 0),
Andreas Mohr13769e32006-05-17 11:03:16 +0200857 AZF3328_MIXER_VOL_SPECIAL("3D Control - Width", IDX_MIXER_ADVCTL1, 0x07, 1, 0), /* "3D Width" */
858 AZF3328_MIXER_VOL_SPECIAL("3D Control - Depth", IDX_MIXER_ADVCTL1, 0x03, 8, 0), /* "Hifi 3D" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859#if MIXER_TESTING
860 AZF3328_MIXER_SWITCH("0", IDX_MIXER_ADVCTL2, 0, 0),
861 AZF3328_MIXER_SWITCH("1", IDX_MIXER_ADVCTL2, 1, 0),
862 AZF3328_MIXER_SWITCH("2", IDX_MIXER_ADVCTL2, 2, 0),
863 AZF3328_MIXER_SWITCH("3", IDX_MIXER_ADVCTL2, 3, 0),
864 AZF3328_MIXER_SWITCH("4", IDX_MIXER_ADVCTL2, 4, 0),
865 AZF3328_MIXER_SWITCH("5", IDX_MIXER_ADVCTL2, 5, 0),
866 AZF3328_MIXER_SWITCH("6", IDX_MIXER_ADVCTL2, 6, 0),
867 AZF3328_MIXER_SWITCH("7", IDX_MIXER_ADVCTL2, 7, 0),
868 AZF3328_MIXER_SWITCH("8", IDX_MIXER_ADVCTL2, 8, 0),
869 AZF3328_MIXER_SWITCH("9", IDX_MIXER_ADVCTL2, 9, 0),
870 AZF3328_MIXER_SWITCH("10", IDX_MIXER_ADVCTL2, 10, 0),
871 AZF3328_MIXER_SWITCH("11", IDX_MIXER_ADVCTL2, 11, 0),
872 AZF3328_MIXER_SWITCH("12", IDX_MIXER_ADVCTL2, 12, 0),
873 AZF3328_MIXER_SWITCH("13", IDX_MIXER_ADVCTL2, 13, 0),
874 AZF3328_MIXER_SWITCH("14", IDX_MIXER_ADVCTL2, 14, 0),
875 AZF3328_MIXER_SWITCH("15", IDX_MIXER_ADVCTL2, 15, 0),
876#endif
877};
878
Takashi Iwai1b60f6b2007-03-13 22:13:47 +0100879static u16 __devinitdata snd_azf3328_init_values[][2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 { IDX_MIXER_PLAY_MASTER, MIXER_MUTE_MASK|0x1f1f },
881 { IDX_MIXER_MODEMOUT, MIXER_MUTE_MASK|0x1f1f },
882 { IDX_MIXER_BASSTREBLE, 0x0000 },
883 { IDX_MIXER_PCBEEP, MIXER_MUTE_MASK|0x1f1f },
884 { IDX_MIXER_MODEMIN, MIXER_MUTE_MASK|0x1f1f },
885 { IDX_MIXER_MIC, MIXER_MUTE_MASK|0x001f },
886 { IDX_MIXER_LINEIN, MIXER_MUTE_MASK|0x1f1f },
887 { IDX_MIXER_CDAUDIO, MIXER_MUTE_MASK|0x1f1f },
888 { IDX_MIXER_VIDEO, MIXER_MUTE_MASK|0x1f1f },
889 { IDX_MIXER_AUX, MIXER_MUTE_MASK|0x1f1f },
890 { IDX_MIXER_WAVEOUT, MIXER_MUTE_MASK|0x1f1f },
891 { IDX_MIXER_FMSYNTH, MIXER_MUTE_MASK|0x1f1f },
892 { IDX_MIXER_REC_VOLUME, MIXER_MUTE_MASK|0x0707 },
893};
894
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200895static int __devinit
Takashi Iwai95de7762005-11-17 15:02:42 +0100896snd_azf3328_mixer_new(struct snd_azf3328 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
Takashi Iwai95de7762005-11-17 15:02:42 +0100898 struct snd_card *card;
899 const struct snd_kcontrol_new *sw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 unsigned int idx;
901 int err;
902
903 snd_azf3328_dbgcallenter();
Takashi Iwaida3cec32008-08-08 17:12:14 +0200904 if (snd_BUG_ON(!chip || !chip->card))
905 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
907 card = chip->card;
908
909 /* mixer reset */
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200910 snd_azf3328_mixer_outw(chip, IDX_MIXER_RESET, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
912 /* mute and zero volume channels */
Andreas Mohr02330fb2008-05-16 12:18:29 +0200913 for (idx = 0; idx < ARRAY_SIZE(snd_azf3328_init_values); ++idx) {
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200914 snd_azf3328_mixer_outw(chip,
915 snd_azf3328_init_values[idx][0],
916 snd_azf3328_init_values[idx][1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 }
Andreas Mohr02330fb2008-05-16 12:18:29 +0200918
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 /* add mixer controls */
920 sw = snd_azf3328_mixer_controls;
Andreas Mohr02330fb2008-05-16 12:18:29 +0200921 for (idx = 0; idx < ARRAY_SIZE(snd_azf3328_mixer_controls);
922 ++idx, ++sw) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(sw, chip))) < 0)
924 return err;
925 }
926 snd_component_add(card, "AZF3328 mixer");
927 strcpy(card->mixername, "AZF3328 mixer");
928
929 snd_azf3328_dbgcallleave();
930 return 0;
931}
932
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200933static int
Takashi Iwai95de7762005-11-17 15:02:42 +0100934snd_azf3328_hw_params(struct snd_pcm_substream *substream,
935 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
937 int res;
938 snd_azf3328_dbgcallenter();
939 res = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
940 snd_azf3328_dbgcallleave();
941 return res;
942}
943
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200944static int
Takashi Iwai95de7762005-11-17 15:02:42 +0100945snd_azf3328_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946{
947 snd_azf3328_dbgcallenter();
948 snd_pcm_lib_free_pages(substream);
949 snd_azf3328_dbgcallleave();
950 return 0;
951}
952
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200953static void
Andreas Mohrda237f32010-12-27 21:17:26 +0100954snd_azf3328_codec_setfmt(struct snd_azf3328_codec_data *codec,
Andreas Mohr627d3e72008-06-23 11:50:47 +0200955 enum azf_freq_t bitrate,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 unsigned int format_width,
957 unsigned int channels
958)
959{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 unsigned long flags;
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200961 u16 val = 0xff00;
Andreas Mohr8d9a1142010-12-27 21:16:49 +0100962 u8 freq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
964 snd_azf3328_dbgcallenter();
965 switch (bitrate) {
Andreas Mohr8d9a1142010-12-27 21:16:49 +0100966#define AZF_FMT_XLATE(in_freq, out_bits) \
967 do { \
968 case AZF_FREQ_ ## in_freq: \
969 freq = SOUNDFORMAT_FREQ_ ## out_bits; \
970 break; \
971 } while (0);
972 AZF_FMT_XLATE(4000, SUSPECTED_4000)
973 AZF_FMT_XLATE(4800, SUSPECTED_4800)
974 /* the AZF3328 names it "5510" for some strange reason: */
975 AZF_FMT_XLATE(5512, 5510)
976 AZF_FMT_XLATE(6620, 6620)
977 AZF_FMT_XLATE(8000, 8000)
978 AZF_FMT_XLATE(9600, 9600)
979 AZF_FMT_XLATE(11025, 11025)
980 AZF_FMT_XLATE(13240, SUSPECTED_13240)
981 AZF_FMT_XLATE(16000, 16000)
982 AZF_FMT_XLATE(22050, 22050)
983 AZF_FMT_XLATE(32000, 32000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 default:
Takashi Iwai99b359b2005-10-20 18:26:44 +0200985 snd_printk(KERN_WARNING "unknown bitrate %d, assuming 44.1kHz!\n", bitrate);
Andreas Mohr02330fb2008-05-16 12:18:29 +0200986 /* fall-through */
Andreas Mohr8d9a1142010-12-27 21:16:49 +0100987 AZF_FMT_XLATE(44100, 44100)
988 AZF_FMT_XLATE(48000, 48000)
989 AZF_FMT_XLATE(66200, SUSPECTED_66200)
990#undef AZF_FMT_XLATE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 }
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200992 /* val = 0xff07; 3m27.993s (65301Hz; -> 64000Hz???) hmm, 66120, 65967, 66123 */
993 /* val = 0xff09; 17m15.098s (13123,478Hz; -> 12000Hz???) hmm, 13237.2Hz? */
994 /* val = 0xff0a; 47m30.599s (4764,891Hz; -> 4800Hz???) yup, 4803Hz */
995 /* val = 0xff0c; 57m0.510s (4010,263Hz; -> 4000Hz???) yup, 4003Hz */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 /* val = 0xff05; 5m11.556s (... -> 44100Hz) */
997 /* val = 0xff03; 10m21.529s (21872,463Hz; -> 22050Hz???) */
998 /* val = 0xff0f; 20m41.883s (10937,993Hz; -> 11025Hz???) */
999 /* val = 0xff0d; 41m23.135s (5523,600Hz; -> 5512Hz???) */
1000 /* val = 0xff0e; 28m30.777s (8017Hz; -> 8000Hz???) */
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001001
Andreas Mohr8d9a1142010-12-27 21:16:49 +01001002 val |= freq;
1003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 if (channels == 2)
1005 val |= SOUNDFORMAT_FLAG_2CHANNELS;
1006
1007 if (format_width == 16)
1008 val |= SOUNDFORMAT_FLAG_16BIT;
1009
Andreas Mohrda237f32010-12-27 21:17:26 +01001010 spin_lock_irqsave(codec->lock, flags);
Andreas Mohr02330fb2008-05-16 12:18:29 +02001011
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 /* set bitrate/format */
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001013 snd_azf3328_codec_outw(codec, IDX_IO_CODEC_SOUNDFORMAT, val);
Andreas Mohr02330fb2008-05-16 12:18:29 +02001014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 /* changing the bitrate/format settings switches off the
1016 * audio output with an annoying click in case of 8/16bit format change
1017 * (maybe shutting down DAC/ADC?), thus immediately
1018 * do some tweaking to reenable it and get rid of the clicking
1019 * (FIXME: yes, it works, but what exactly am I doing here?? :)
1020 * FIXME: does this have some side effects for full-duplex
1021 * or other dramatic side effects? */
Andreas Mohradf59312010-12-27 21:16:43 +01001022 /* do it for non-capture codecs only */
Andreas Mohrda237f32010-12-27 21:17:26 +01001023 if (codec->type != AZF_CODEC_CAPTURE)
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001024 snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS,
1025 snd_azf3328_codec_inw(codec, IDX_IO_CODEC_DMA_FLAGS) |
1026 DMA_RUN_SOMETHING1 |
1027 DMA_RUN_SOMETHING2 |
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001028 SOMETHING_ALMOST_ALWAYS_SET |
1029 DMA_EPILOGUE_SOMETHING |
1030 DMA_SOMETHING_ELSE
1031 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
Andreas Mohrda237f32010-12-27 21:17:26 +01001033 spin_unlock_irqrestore(codec->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 snd_azf3328_dbgcallleave();
1035}
1036
Andreas Mohr02330fb2008-05-16 12:18:29 +02001037static inline void
Andreas Mohrda237f32010-12-27 21:17:26 +01001038snd_azf3328_codec_setfmt_lowpower(struct snd_azf3328_codec_data *codec
Andreas Mohr02330fb2008-05-16 12:18:29 +02001039)
1040{
1041 /* choose lowest frequency for low power consumption.
1042 * While this will cause louder noise due to rather coarse frequency,
1043 * it should never matter since output should always
1044 * get disabled properly when idle anyway. */
Andreas Mohrda237f32010-12-27 21:17:26 +01001045 snd_azf3328_codec_setfmt(codec, AZF_FREQ_4000, 8, 1);
Andreas Mohr02330fb2008-05-16 12:18:29 +02001046}
1047
Andreas Mohr627d3e72008-06-23 11:50:47 +02001048static void
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001049snd_azf3328_ctrl_reg_6AH_update(struct snd_azf3328 *chip,
Andreas Mohr627d3e72008-06-23 11:50:47 +02001050 unsigned bitmask,
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001051 bool enable
Andreas Mohr627d3e72008-06-23 11:50:47 +02001052)
1053{
Andreas Mohr78df6172009-07-12 22:17:54 +02001054 bool do_mask = !enable;
1055 if (do_mask)
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001056 chip->shadow_reg_ctrl_6AH |= bitmask;
Andreas Mohr78df6172009-07-12 22:17:54 +02001057 else
1058 chip->shadow_reg_ctrl_6AH &= ~bitmask;
1059 snd_azf3328_dbgcodec("6AH_update mask 0x%04x do_mask %d: val 0x%04x\n",
1060 bitmask, do_mask, chip->shadow_reg_ctrl_6AH);
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001061 snd_azf3328_ctrl_outw(chip, IDX_IO_6AH, chip->shadow_reg_ctrl_6AH);
Andreas Mohr627d3e72008-06-23 11:50:47 +02001062}
1063
Andreas Mohr02330fb2008-05-16 12:18:29 +02001064static inline void
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001065snd_azf3328_ctrl_enable_codecs(struct snd_azf3328 *chip, bool enable)
Andreas Mohr02330fb2008-05-16 12:18:29 +02001066{
Andreas Mohr78df6172009-07-12 22:17:54 +02001067 snd_azf3328_dbgcodec("codec_enable %d\n", enable);
Andreas Mohr02330fb2008-05-16 12:18:29 +02001068 /* no idea what exactly is being done here, but I strongly assume it's
1069 * PM related */
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001070 snd_azf3328_ctrl_reg_6AH_update(
Andreas Mohr627d3e72008-06-23 11:50:47 +02001071 chip, IO_6A_PAUSE_PLAYBACK_BIT8, enable
Andreas Mohr02330fb2008-05-16 12:18:29 +02001072 );
1073}
1074
1075static void
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001076snd_azf3328_ctrl_codec_activity(struct snd_azf3328 *chip,
1077 enum snd_azf3328_codec_type codec_type,
1078 bool enable
Andreas Mohr02330fb2008-05-16 12:18:29 +02001079)
1080{
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001081 struct snd_azf3328_codec_data *codec = &chip->codecs[codec_type];
1082 bool need_change = (codec->running != enable);
Andreas Mohr02330fb2008-05-16 12:18:29 +02001083
Andreas Mohr78df6172009-07-12 22:17:54 +02001084 snd_azf3328_dbgcodec(
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001085 "codec_activity: %s codec, enable %d, need_change %d\n",
1086 codec->name, enable, need_change
Andreas Mohr02330fb2008-05-16 12:18:29 +02001087 );
1088 if (need_change) {
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001089 static const struct {
1090 enum snd_azf3328_codec_type other1;
1091 enum snd_azf3328_codec_type other2;
1092 } peer_codecs[3] =
1093 { { AZF_CODEC_CAPTURE, AZF_CODEC_I2S_OUT },
1094 { AZF_CODEC_PLAYBACK, AZF_CODEC_I2S_OUT },
1095 { AZF_CODEC_PLAYBACK, AZF_CODEC_CAPTURE } };
1096 bool call_function;
1097
1098 if (enable)
1099 /* if enable codec, call enable_codecs func
1100 to enable codec supply... */
1101 call_function = 1;
1102 else {
1103 /* ...otherwise call enable_codecs func
1104 (which globally shuts down operation of codecs)
1105 only in case the other codecs are currently
1106 not active either! */
Andreas Mohr78df6172009-07-12 22:17:54 +02001107 call_function =
1108 ((!chip->codecs[peer_codecs[codec_type].other1]
1109 .running)
1110 && (!chip->codecs[peer_codecs[codec_type].other2]
1111 .running));
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001112 }
1113 if (call_function)
1114 snd_azf3328_ctrl_enable_codecs(chip, enable);
Andreas Mohr02330fb2008-05-16 12:18:29 +02001115
1116 /* ...and adjust clock, too
1117 * (reduce noise and power consumption) */
1118 if (!enable)
Andreas Mohrda237f32010-12-27 21:17:26 +01001119 snd_azf3328_codec_setfmt_lowpower(codec);
Andreas Mohr78df6172009-07-12 22:17:54 +02001120 codec->running = enable;
Andreas Mohr02330fb2008-05-16 12:18:29 +02001121 }
Andreas Mohr02330fb2008-05-16 12:18:29 +02001122}
1123
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001124static void
Andreas Mohrda237f32010-12-27 21:17:26 +01001125snd_azf3328_codec_setdmaa(struct snd_azf3328_codec_data *codec,
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001126 unsigned long addr,
1127 unsigned int count,
1128 unsigned int size
Andreas Mohr02330fb2008-05-16 12:18:29 +02001129)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 snd_azf3328_dbgcallenter();
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001132 if (!codec->running) {
1133 /* AZF3328 uses a two buffer pointer DMA transfer approach */
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001134
Andreas Mohr78df6172009-07-12 22:17:54 +02001135 unsigned long flags, addr_area2;
Andreas Mohr02330fb2008-05-16 12:18:29 +02001136
1137 /* width 32bit (prevent overflow): */
Andreas Mohr78df6172009-07-12 22:17:54 +02001138 u32 count_areas, lengths;
Andreas Mohr02330fb2008-05-16 12:18:29 +02001139
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001140 count_areas = size/2;
1141 addr_area2 = addr+count_areas;
Andreas Mohr78df6172009-07-12 22:17:54 +02001142 snd_azf3328_dbgcodec("setdma: buffers %08lx[%u] / %08lx[%u]\n",
1143 addr, count_areas, addr_area2, count_areas);
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001144
Andreas Mohr79741502010-11-21 12:09:32 +01001145 count_areas--; /* max. index */
1146
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001147 /* build combined I/O buffer length word */
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001148 lengths = (count_areas << 16) | (count_areas);
Andreas Mohrda237f32010-12-27 21:17:26 +01001149 spin_lock_irqsave(codec->lock, flags);
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001150 snd_azf3328_codec_outl(codec, IDX_IO_CODEC_DMA_START_1, addr);
1151 snd_azf3328_codec_outl(codec, IDX_IO_CODEC_DMA_START_2,
1152 addr_area2);
1153 snd_azf3328_codec_outl(codec, IDX_IO_CODEC_DMA_LENGTHS,
1154 lengths);
Andreas Mohrda237f32010-12-27 21:17:26 +01001155 spin_unlock_irqrestore(codec->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 }
1157 snd_azf3328_dbgcallleave();
1158}
1159
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001160static int
Andreas Mohrda237f32010-12-27 21:17:26 +01001161snd_azf3328_pcm_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162{
Takashi Iwai95de7762005-11-17 15:02:42 +01001163 struct snd_pcm_runtime *runtime = substream->runtime;
Andreas Mohrda237f32010-12-27 21:17:26 +01001164 struct snd_azf3328_codec_data *codec = runtime->private_data;
Andreas Mohr34585592010-12-27 21:17:11 +01001165#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
1167 unsigned int count = snd_pcm_lib_period_bytes(substream);
1168#endif
1169
1170 snd_azf3328_dbgcallenter();
Andreas Mohr34585592010-12-27 21:17:11 +01001171
1172 codec->dma_base = runtime->dma_addr;
1173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174#if 0
Andreas Mohrda237f32010-12-27 21:17:26 +01001175 snd_azf3328_codec_setfmt(codec,
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001176 runtime->rate,
1177 snd_pcm_format_width(runtime->format),
1178 runtime->channels);
Andreas Mohrda237f32010-12-27 21:17:26 +01001179 snd_azf3328_codec_setdmaa(codec,
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001180 runtime->dma_addr, count, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181#endif
1182 snd_azf3328_dbgcallleave();
1183 return 0;
1184}
1185
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001186static int
Andreas Mohrda237f32010-12-27 21:17:26 +01001187snd_azf3328_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188{
Takashi Iwai95de7762005-11-17 15:02:42 +01001189 struct snd_azf3328 *chip = snd_pcm_substream_chip(substream);
1190 struct snd_pcm_runtime *runtime = substream->runtime;
Andreas Mohrda237f32010-12-27 21:17:26 +01001191 struct snd_azf3328_codec_data *codec = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 int result = 0;
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001193 u16 flags1;
1194 bool previously_muted = 0;
Andreas Mohrda237f32010-12-27 21:17:26 +01001195 bool is_main_mixer_playback_codec = (AZF_CODEC_PLAYBACK == codec->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Andreas Mohrda237f32010-12-27 21:17:26 +01001197 snd_azf3328_dbgcalls("snd_azf3328_pcm_trigger cmd %d\n", cmd);
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 switch (cmd) {
1200 case SNDRV_PCM_TRIGGER_START:
Andreas Mohr78df6172009-07-12 22:17:54 +02001201 snd_azf3328_dbgcodec("START %s\n", codec->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Andreas Mohrda237f32010-12-27 21:17:26 +01001203 if (is_main_mixer_playback_codec) {
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001204 /* mute WaveOut (avoid clicking during setup) */
1205 previously_muted =
1206 snd_azf3328_mixer_set_mute(
1207 chip, IDX_MIXER_WAVEOUT, 1
1208 );
1209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
Andreas Mohrda237f32010-12-27 21:17:26 +01001211 snd_azf3328_codec_setfmt(codec,
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001212 runtime->rate,
1213 snd_pcm_format_width(runtime->format),
1214 runtime->channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Andreas Mohrda237f32010-12-27 21:17:26 +01001216 spin_lock(codec->lock);
Andreas Mohr02330fb2008-05-16 12:18:29 +02001217 /* first, remember current value: */
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001218 flags1 = snd_azf3328_codec_inw(codec, IDX_IO_CODEC_DMA_FLAGS);
Andreas Mohr02330fb2008-05-16 12:18:29 +02001219
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001220 /* stop transfer */
1221 flags1 &= ~DMA_RESUME;
1222 snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS, flags1);
Andreas Mohr02330fb2008-05-16 12:18:29 +02001223
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 /* FIXME: clear interrupts or what??? */
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001225 snd_azf3328_codec_outw(codec, IDX_IO_CODEC_IRQTYPE, 0xffff);
Andreas Mohrda237f32010-12-27 21:17:26 +01001226 spin_unlock(codec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Andreas Mohrda237f32010-12-27 21:17:26 +01001228 snd_azf3328_codec_setdmaa(codec, runtime->dma_addr,
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001229 snd_pcm_lib_period_bytes(substream),
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001230 snd_pcm_lib_buffer_bytes(substream)
1231 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
Andreas Mohrda237f32010-12-27 21:17:26 +01001233 spin_lock(codec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234#ifdef WIN9X
1235 /* FIXME: enable playback/recording??? */
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001236 flags1 |= DMA_RUN_SOMETHING1 | DMA_RUN_SOMETHING2;
1237 snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS, flags1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001239 /* start transfer again */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 /* FIXME: what is this value (0x0010)??? */
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001241 flags1 |= DMA_RESUME | DMA_EPILOGUE_SOMETHING;
1242 snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS, flags1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243#else /* NT4 */
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001244 snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS,
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001245 0x0000);
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001246 snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS,
1247 DMA_RUN_SOMETHING1);
1248 snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS,
1249 DMA_RUN_SOMETHING1 |
1250 DMA_RUN_SOMETHING2);
1251 snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS,
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001252 DMA_RESUME |
1253 SOMETHING_ALMOST_ALWAYS_SET |
1254 DMA_EPILOGUE_SOMETHING |
1255 DMA_SOMETHING_ELSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256#endif
Andreas Mohrda237f32010-12-27 21:17:26 +01001257 spin_unlock(codec->lock);
1258 snd_azf3328_ctrl_codec_activity(chip, codec->type, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Andreas Mohrda237f32010-12-27 21:17:26 +01001260 if (is_main_mixer_playback_codec) {
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001261 /* now unmute WaveOut */
1262 if (!previously_muted)
1263 snd_azf3328_mixer_set_mute(
1264 chip, IDX_MIXER_WAVEOUT, 0
1265 );
1266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Andreas Mohr78df6172009-07-12 22:17:54 +02001268 snd_azf3328_dbgcodec("STARTED %s\n", codec->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 break;
Andreas Mohrca54bde2006-05-17 11:02:24 +02001270 case SNDRV_PCM_TRIGGER_RESUME:
Andreas Mohr78df6172009-07-12 22:17:54 +02001271 snd_azf3328_dbgcodec("RESUME %s\n", codec->name);
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001272 /* resume codec if we were active */
Andreas Mohrda237f32010-12-27 21:17:26 +01001273 spin_lock(codec->lock);
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001274 if (codec->running)
1275 snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS,
1276 snd_azf3328_codec_inw(
1277 codec, IDX_IO_CODEC_DMA_FLAGS
1278 ) | DMA_RESUME
1279 );
Andreas Mohrda237f32010-12-27 21:17:26 +01001280 spin_unlock(codec->lock);
Andreas Mohrca54bde2006-05-17 11:02:24 +02001281 break;
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001282 case SNDRV_PCM_TRIGGER_STOP:
Andreas Mohr78df6172009-07-12 22:17:54 +02001283 snd_azf3328_dbgcodec("STOP %s\n", codec->name);
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001284
Andreas Mohrda237f32010-12-27 21:17:26 +01001285 if (is_main_mixer_playback_codec) {
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001286 /* mute WaveOut (avoid clicking during setup) */
1287 previously_muted =
1288 snd_azf3328_mixer_set_mute(
1289 chip, IDX_MIXER_WAVEOUT, 1
1290 );
1291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
Andreas Mohrda237f32010-12-27 21:17:26 +01001293 spin_lock(codec->lock);
Andreas Mohr02330fb2008-05-16 12:18:29 +02001294 /* first, remember current value: */
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001295 flags1 = snd_azf3328_codec_inw(codec, IDX_IO_CODEC_DMA_FLAGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001297 /* stop transfer */
1298 flags1 &= ~DMA_RESUME;
1299 snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS, flags1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001301 /* hmm, is this really required? we're resetting the same bit
1302 * immediately thereafter... */
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001303 flags1 |= DMA_RUN_SOMETHING1;
1304 snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS, flags1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001306 flags1 &= ~DMA_RUN_SOMETHING1;
1307 snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS, flags1);
Andreas Mohrda237f32010-12-27 21:17:26 +01001308 spin_unlock(codec->lock);
1309 snd_azf3328_ctrl_codec_activity(chip, codec->type, 0);
Andreas Mohr02330fb2008-05-16 12:18:29 +02001310
Andreas Mohrda237f32010-12-27 21:17:26 +01001311 if (is_main_mixer_playback_codec) {
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001312 /* now unmute WaveOut */
1313 if (!previously_muted)
1314 snd_azf3328_mixer_set_mute(
1315 chip, IDX_MIXER_WAVEOUT, 0
1316 );
1317 }
Andreas Mohr02330fb2008-05-16 12:18:29 +02001318
Andreas Mohr78df6172009-07-12 22:17:54 +02001319 snd_azf3328_dbgcodec("STOPPED %s\n", codec->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 break;
Andreas Mohrca54bde2006-05-17 11:02:24 +02001321 case SNDRV_PCM_TRIGGER_SUSPEND:
Andreas Mohr78df6172009-07-12 22:17:54 +02001322 snd_azf3328_dbgcodec("SUSPEND %s\n", codec->name);
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001323 /* make sure codec is stopped */
1324 snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS,
1325 snd_azf3328_codec_inw(
1326 codec, IDX_IO_CODEC_DMA_FLAGS
1327 ) & ~DMA_RESUME
1328 );
Andreas Mohrca54bde2006-05-17 11:02:24 +02001329 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Takashi Iwai99b359b2005-10-20 18:26:44 +02001331 snd_printk(KERN_ERR "FIXME: SNDRV_PCM_TRIGGER_PAUSE_PUSH NIY!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 break;
1333 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Takashi Iwai99b359b2005-10-20 18:26:44 +02001334 snd_printk(KERN_ERR "FIXME: SNDRV_PCM_TRIGGER_PAUSE_RELEASE NIY!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 break;
1336 default:
Andreas Mohr78df6172009-07-12 22:17:54 +02001337 snd_printk(KERN_ERR "FIXME: unknown trigger mode!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 return -EINVAL;
1339 }
Andreas Mohr02330fb2008-05-16 12:18:29 +02001340
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 snd_azf3328_dbgcallleave();
1342 return result;
1343}
1344
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001345static snd_pcm_uframes_t
Andreas Mohrda237f32010-12-27 21:17:26 +01001346snd_azf3328_pcm_pointer(struct snd_pcm_substream *substream
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001347)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348{
Andreas Mohrda237f32010-12-27 21:17:26 +01001349 const struct snd_azf3328_codec_data *codec =
1350 substream->runtime->private_data;
Andreas Mohr34585592010-12-27 21:17:11 +01001351 unsigned long result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 snd_pcm_uframes_t frmres;
1353
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001354 result = snd_azf3328_codec_inl(codec, IDX_IO_CODEC_DMA_CURRPOS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001356 /* calculate offset */
Andreas Mohr34585592010-12-27 21:17:11 +01001357#ifdef QUERY_HARDWARE
1358 result -= snd_azf3328_codec_inl(codec, IDX_IO_CODEC_DMA_START_1);
1359#else
1360 result -= codec->dma_base;
1361#endif
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001362 frmres = bytes_to_frames( substream->runtime, result);
Andreas Mohradf59312010-12-27 21:16:43 +01001363 snd_azf3328_dbgcodec("%08li %s @ 0x%8lx, frames %8ld\n",
1364 jiffies, codec->name, result, frmres);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 return frmres;
1366}
1367
Andreas Mohr02330fb2008-05-16 12:18:29 +02001368/******************************************************************/
1369
1370#ifdef SUPPORT_GAMEPORT
1371static inline void
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001372snd_azf3328_gameport_irq_enable(struct snd_azf3328 *chip,
1373 bool enable
1374)
Andreas Mohr02330fb2008-05-16 12:18:29 +02001375{
1376 snd_azf3328_io_reg_setb(
1377 chip->game_io+IDX_GAME_HWCONFIG,
1378 GAME_HWCFG_IRQ_ENABLE,
1379 enable
1380 );
1381}
1382
1383static inline void
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001384snd_azf3328_gameport_legacy_address_enable(struct snd_azf3328 *chip,
1385 bool enable
1386)
Andreas Mohr02330fb2008-05-16 12:18:29 +02001387{
1388 snd_azf3328_io_reg_setb(
1389 chip->game_io+IDX_GAME_HWCONFIG,
1390 GAME_HWCFG_LEGACY_ADDRESS_ENABLE,
1391 enable
1392 );
1393}
1394
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001395static void
1396snd_azf3328_gameport_set_counter_frequency(struct snd_azf3328 *chip,
1397 unsigned int freq_cfg
1398)
Andreas Mohr02330fb2008-05-16 12:18:29 +02001399{
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001400 snd_azf3328_io_reg_setb(
1401 chip->game_io+IDX_GAME_HWCONFIG,
1402 0x02,
1403 (freq_cfg & 1) != 0
1404 );
1405 snd_azf3328_io_reg_setb(
1406 chip->game_io+IDX_GAME_HWCONFIG,
1407 0x04,
1408 (freq_cfg & 2) != 0
1409 );
1410}
1411
1412static inline void
1413snd_azf3328_gameport_axis_circuit_enable(struct snd_azf3328 *chip, bool enable)
1414{
1415 snd_azf3328_ctrl_reg_6AH_update(
Andreas Mohr627d3e72008-06-23 11:50:47 +02001416 chip, IO_6A_SOMETHING2_GAMEPORT, enable
Andreas Mohr02330fb2008-05-16 12:18:29 +02001417 );
1418}
1419
1420static inline void
1421snd_azf3328_gameport_interrupt(struct snd_azf3328 *chip)
1422{
1423 /*
1424 * skeleton handler only
1425 * (we do not want axis reading in interrupt handler - too much load!)
1426 */
1427 snd_azf3328_dbggame("gameport irq\n");
1428
1429 /* this should ACK the gameport IRQ properly, hopefully. */
1430 snd_azf3328_game_inw(chip, IDX_GAME_AXIS_VALUE);
1431}
1432
1433static int
1434snd_azf3328_gameport_open(struct gameport *gameport, int mode)
1435{
1436 struct snd_azf3328 *chip = gameport_get_port_data(gameport);
1437 int res;
1438
1439 snd_azf3328_dbggame("gameport_open, mode %d\n", mode);
1440 switch (mode) {
1441 case GAMEPORT_MODE_COOKED:
1442 case GAMEPORT_MODE_RAW:
1443 res = 0;
1444 break;
1445 default:
1446 res = -1;
1447 break;
1448 }
1449
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001450 snd_azf3328_gameport_set_counter_frequency(chip,
1451 GAME_HWCFG_ADC_COUNTER_FREQ_STD);
Andreas Mohr02330fb2008-05-16 12:18:29 +02001452 snd_azf3328_gameport_axis_circuit_enable(chip, (res == 0));
1453
1454 return res;
1455}
1456
1457static void
1458snd_azf3328_gameport_close(struct gameport *gameport)
1459{
1460 struct snd_azf3328 *chip = gameport_get_port_data(gameport);
1461
1462 snd_azf3328_dbggame("gameport_close\n");
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001463 snd_azf3328_gameport_set_counter_frequency(chip,
1464 GAME_HWCFG_ADC_COUNTER_FREQ_1_200);
Andreas Mohr02330fb2008-05-16 12:18:29 +02001465 snd_azf3328_gameport_axis_circuit_enable(chip, 0);
1466}
1467
1468static int
1469snd_azf3328_gameport_cooked_read(struct gameport *gameport,
1470 int *axes,
1471 int *buttons
1472)
1473{
1474 struct snd_azf3328 *chip = gameport_get_port_data(gameport);
1475 int i;
1476 u8 val;
1477 unsigned long flags;
1478
Takashi Iwaida3cec32008-08-08 17:12:14 +02001479 if (snd_BUG_ON(!chip))
1480 return 0;
Andreas Mohr02330fb2008-05-16 12:18:29 +02001481
1482 spin_lock_irqsave(&chip->reg_lock, flags);
1483 val = snd_azf3328_game_inb(chip, IDX_GAME_LEGACY_COMPATIBLE);
1484 *buttons = (~(val) >> 4) & 0xf;
1485
1486 /* ok, this one is a bit dirty: cooked_read is being polled by a timer,
1487 * thus we're atomic and cannot actively wait in here
1488 * (which would be useful for us since it probably would be better
1489 * to trigger a measurement in here, then wait a short amount of
1490 * time until it's finished, then read values of _this_ measurement).
1491 *
1492 * Thus we simply resort to reading values if they're available already
1493 * and trigger the next measurement.
1494 */
1495
1496 val = snd_azf3328_game_inb(chip, IDX_GAME_AXES_CONFIG);
1497 if (val & GAME_AXES_SAMPLING_READY) {
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001498 for (i = 0; i < ARRAY_SIZE(chip->axes); ++i) {
Andreas Mohr02330fb2008-05-16 12:18:29 +02001499 /* configure the axis to read */
1500 val = (i << 4) | 0x0f;
1501 snd_azf3328_game_outb(chip, IDX_GAME_AXES_CONFIG, val);
1502
1503 chip->axes[i] = snd_azf3328_game_inw(
1504 chip, IDX_GAME_AXIS_VALUE
1505 );
1506 }
1507 }
1508
Andreas Mohradf59312010-12-27 21:16:43 +01001509 /* trigger next sampling of axes, to be evaluated the next time we
Andreas Mohr02330fb2008-05-16 12:18:29 +02001510 * enter this function */
1511
1512 /* for some very, very strange reason we cannot enable
1513 * Measurement Ready monitoring for all axes here,
1514 * at least not when only one joystick connected */
1515 val = 0x03; /* we're able to monitor axes 1 and 2 only */
1516 snd_azf3328_game_outb(chip, IDX_GAME_AXES_CONFIG, val);
1517
1518 snd_azf3328_game_outw(chip, IDX_GAME_AXIS_VALUE, 0xffff);
1519 spin_unlock_irqrestore(&chip->reg_lock, flags);
1520
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001521 for (i = 0; i < ARRAY_SIZE(chip->axes); i++) {
Andreas Mohr02330fb2008-05-16 12:18:29 +02001522 axes[i] = chip->axes[i];
1523 if (axes[i] == 0xffff)
1524 axes[i] = -1;
1525 }
1526
1527 snd_azf3328_dbggame("cooked_read: axes %d %d %d %d buttons %d\n",
1528 axes[0], axes[1], axes[2], axes[3], *buttons
1529 );
1530
1531 return 0;
1532}
1533
1534static int __devinit
1535snd_azf3328_gameport(struct snd_azf3328 *chip, int dev)
1536{
1537 struct gameport *gp;
1538
Andreas Mohr02330fb2008-05-16 12:18:29 +02001539 chip->gameport = gp = gameport_allocate_port();
1540 if (!gp) {
1541 printk(KERN_ERR "azt3328: cannot alloc memory for gameport\n");
1542 return -ENOMEM;
1543 }
1544
1545 gameport_set_name(gp, "AZF3328 Gameport");
1546 gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
1547 gameport_set_dev_parent(gp, &chip->pci->dev);
Andreas Mohr627d3e72008-06-23 11:50:47 +02001548 gp->io = chip->game_io;
Andreas Mohr02330fb2008-05-16 12:18:29 +02001549 gameport_set_port_data(gp, chip);
1550
1551 gp->open = snd_azf3328_gameport_open;
1552 gp->close = snd_azf3328_gameport_close;
1553 gp->fuzz = 16; /* seems ok */
1554 gp->cooked_read = snd_azf3328_gameport_cooked_read;
1555
1556 /* DISABLE legacy address: we don't need it! */
1557 snd_azf3328_gameport_legacy_address_enable(chip, 0);
1558
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001559 snd_azf3328_gameport_set_counter_frequency(chip,
1560 GAME_HWCFG_ADC_COUNTER_FREQ_1_200);
Andreas Mohr02330fb2008-05-16 12:18:29 +02001561 snd_azf3328_gameport_axis_circuit_enable(chip, 0);
1562
1563 gameport_register_port(chip->gameport);
1564
1565 return 0;
1566}
1567
1568static void
1569snd_azf3328_gameport_free(struct snd_azf3328 *chip)
1570{
1571 if (chip->gameport) {
1572 gameport_unregister_port(chip->gameport);
1573 chip->gameport = NULL;
1574 }
1575 snd_azf3328_gameport_irq_enable(chip, 0);
1576}
1577#else
1578static inline int
1579snd_azf3328_gameport(struct snd_azf3328 *chip, int dev) { return -ENOSYS; }
1580static inline void
1581snd_azf3328_gameport_free(struct snd_azf3328 *chip) { }
1582static inline void
1583snd_azf3328_gameport_interrupt(struct snd_azf3328 *chip)
1584{
1585 printk(KERN_WARNING "huh, game port IRQ occurred!?\n");
1586}
1587#endif /* SUPPORT_GAMEPORT */
1588
1589/******************************************************************/
1590
Andreas Mohr627d3e72008-06-23 11:50:47 +02001591static inline void
1592snd_azf3328_irq_log_unknown_type(u8 which)
1593{
Andreas Mohr78df6172009-07-12 22:17:54 +02001594 snd_azf3328_dbgcodec(
Andreas Mohr627d3e72008-06-23 11:50:47 +02001595 "azt3328: unknown IRQ type (%x) occurred, please report!\n",
1596 which
1597 );
1598}
1599
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001600static inline void
Andreas Mohrda237f32010-12-27 21:17:26 +01001601snd_azf3328_pcm_interrupt(const struct snd_azf3328_codec_data *first_codec,
1602 u8 status
1603)
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001604{
1605 u8 which;
1606 enum snd_azf3328_codec_type codec_type;
Andreas Mohrda237f32010-12-27 21:17:26 +01001607 const struct snd_azf3328_codec_data *codec = first_codec;
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001608
1609 for (codec_type = AZF_CODEC_PLAYBACK;
1610 codec_type <= AZF_CODEC_I2S_OUT;
Andreas Mohrda237f32010-12-27 21:17:26 +01001611 ++codec_type, ++codec) {
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001612
1613 /* skip codec if there's no interrupt for it */
1614 if (!(status & (1 << codec_type)))
1615 continue;
1616
Andreas Mohrda237f32010-12-27 21:17:26 +01001617 spin_lock(codec->lock);
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001618 which = snd_azf3328_codec_inb(codec, IDX_IO_CODEC_IRQTYPE);
1619 /* ack all IRQ types immediately */
1620 snd_azf3328_codec_outb(codec, IDX_IO_CODEC_IRQTYPE, which);
Andreas Mohrda237f32010-12-27 21:17:26 +01001621 spin_unlock(codec->lock);
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001622
Andreas Mohrda237f32010-12-27 21:17:26 +01001623 if (codec->substream) {
Andreas Mohr78df6172009-07-12 22:17:54 +02001624 snd_pcm_period_elapsed(codec->substream);
1625 snd_azf3328_dbgcodec("%s period done (#%x), @ %x\n",
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001626 codec->name,
1627 which,
1628 snd_azf3328_codec_inl(
1629 codec, IDX_IO_CODEC_DMA_CURRPOS
1630 )
1631 );
1632 } else
1633 printk(KERN_WARNING "azt3328: irq handler problem!\n");
1634 if (which & IRQ_SOMETHING)
1635 snd_azf3328_irq_log_unknown_type(which);
1636 }
1637}
1638
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001639static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +01001640snd_azf3328_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641{
Takashi Iwai95de7762005-11-17 15:02:42 +01001642 struct snd_azf3328 *chip = dev_id;
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001643 u8 status;
Andreas Mohr78df6172009-07-12 22:17:54 +02001644#if DEBUG_CODEC
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001645 static unsigned long irq_count;
Andreas Mohr02330fb2008-05-16 12:18:29 +02001646#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001648 status = snd_azf3328_ctrl_inb(chip, IDX_IO_IRQSTATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
1650 /* fast path out, to ease interrupt sharing */
Andreas Mohr02330fb2008-05-16 12:18:29 +02001651 if (!(status &
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001652 (IRQ_PLAYBACK|IRQ_RECORDING|IRQ_I2S_OUT
1653 |IRQ_GAMEPORT|IRQ_MPU401|IRQ_TIMER)
Andreas Mohr02330fb2008-05-16 12:18:29 +02001654 ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 return IRQ_NONE; /* must be interrupt for another device */
1656
Andreas Mohr78df6172009-07-12 22:17:54 +02001657 snd_azf3328_dbgcodec(
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001658 "irq_count %ld! IDX_IO_IRQSTATUS %04x\n",
Andreas Mohr627d3e72008-06-23 11:50:47 +02001659 irq_count++ /* debug-only */,
Andreas Mohr627d3e72008-06-23 11:50:47 +02001660 status
1661 );
Andreas Mohr02330fb2008-05-16 12:18:29 +02001662
Andreas Mohre2f87262006-05-17 11:04:19 +02001663 if (status & IRQ_TIMER) {
Andreas Mohr78df6172009-07-12 22:17:54 +02001664 /* snd_azf3328_dbgcodec("timer %ld\n",
Andreas Mohr02330fb2008-05-16 12:18:29 +02001665 snd_azf3328_codec_inl(chip, IDX_IO_TIMER_VALUE)
1666 & TIMER_VALUE_MASK
1667 ); */
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001668 if (chip->timer)
1669 snd_timer_interrupt(chip->timer, chip->timer->sticks);
1670 /* ACK timer */
1671 spin_lock(&chip->reg_lock);
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001672 snd_azf3328_ctrl_outb(chip, IDX_IO_TIMER_VALUE + 3, 0x07);
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001673 spin_unlock(&chip->reg_lock);
Andreas Mohr78df6172009-07-12 22:17:54 +02001674 snd_azf3328_dbgcodec("azt3328: timer IRQ\n");
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001675 }
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001676
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001677 if (status & (IRQ_PLAYBACK|IRQ_RECORDING|IRQ_I2S_OUT))
Andreas Mohrda237f32010-12-27 21:17:26 +01001678 snd_azf3328_pcm_interrupt(chip->codecs, status);
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001679
Andreas Mohr02330fb2008-05-16 12:18:29 +02001680 if (status & IRQ_GAMEPORT)
1681 snd_azf3328_gameport_interrupt(chip);
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001682
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001683 /* MPU401 has less critical IRQ requirements
1684 * than timer and playback/recording, right? */
Andreas Mohre2f87262006-05-17 11:04:19 +02001685 if (status & IRQ_MPU401) {
David Howells7d12e782006-10-05 14:55:46 +01001686 snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001687
1688 /* hmm, do we have to ack the IRQ here somehow?
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001689 * If so, then I don't know how yet... */
Andreas Mohr78df6172009-07-12 22:17:54 +02001690 snd_azf3328_dbgcodec("azt3328: MPU401 IRQ\n");
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 return IRQ_HANDLED;
1693}
1694
1695/*****************************************************************/
1696
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001697/* as long as we think we have identical snd_pcm_hardware parameters
1698 for playback, capture and i2s out, we can use the same physical struct
1699 since the struct is simply being copied into a member.
1700*/
1701static const struct snd_pcm_hardware snd_azf3328_hardware =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702{
1703 /* FIXME!! Correct? */
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001704 .info = SNDRV_PCM_INFO_MMAP |
1705 SNDRV_PCM_INFO_INTERLEAVED |
1706 SNDRV_PCM_INFO_MMAP_VALID,
1707 .formats = SNDRV_PCM_FMTBIT_S8 |
1708 SNDRV_PCM_FMTBIT_U8 |
1709 SNDRV_PCM_FMTBIT_S16_LE |
1710 SNDRV_PCM_FMTBIT_U16_LE,
1711 .rates = SNDRV_PCM_RATE_5512 |
1712 SNDRV_PCM_RATE_8000_48000 |
1713 SNDRV_PCM_RATE_KNOT,
Andreas Mohr02330fb2008-05-16 12:18:29 +02001714 .rate_min = AZF_FREQ_4000,
1715 .rate_max = AZF_FREQ_66200,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 .channels_min = 1,
1717 .channels_max = 2,
Andreas Mohr79741502010-11-21 12:09:32 +01001718 .buffer_bytes_max = (64*1024),
1719 .period_bytes_min = 1024,
1720 .period_bytes_max = (32*1024),
1721 /* We simply have two DMA areas (instead of a list of descriptors
1722 such as other cards); I believe that this is a fixed hardware
1723 attribute and there isn't much driver magic to be done to expand it.
1724 Thus indicate that we have at least and at most 2 periods. */
1725 .periods_min = 2,
1726 .periods_max = 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 /* FIXME: maybe that card actually has a FIFO?
1728 * Hmm, it seems newer revisions do have one, but we still don't know
1729 * its size... */
1730 .fifo_size = 0,
1731};
1732
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
1734static unsigned int snd_azf3328_fixed_rates[] = {
Andreas Mohr02330fb2008-05-16 12:18:29 +02001735 AZF_FREQ_4000,
1736 AZF_FREQ_4800,
1737 AZF_FREQ_5512,
1738 AZF_FREQ_6620,
1739 AZF_FREQ_8000,
1740 AZF_FREQ_9600,
1741 AZF_FREQ_11025,
1742 AZF_FREQ_13240,
1743 AZF_FREQ_16000,
1744 AZF_FREQ_22050,
1745 AZF_FREQ_32000,
1746 AZF_FREQ_44100,
1747 AZF_FREQ_48000,
1748 AZF_FREQ_66200
1749};
1750
Takashi Iwai95de7762005-11-17 15:02:42 +01001751static struct snd_pcm_hw_constraint_list snd_azf3328_hw_constraints_rates = {
Andreas Mohr02330fb2008-05-16 12:18:29 +02001752 .count = ARRAY_SIZE(snd_azf3328_fixed_rates),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 .list = snd_azf3328_fixed_rates,
1754 .mask = 0,
1755};
1756
1757/*****************************************************************/
1758
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001759static int
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001760snd_azf3328_pcm_open(struct snd_pcm_substream *substream,
1761 enum snd_azf3328_codec_type codec_type
1762)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763{
Takashi Iwai95de7762005-11-17 15:02:42 +01001764 struct snd_azf3328 *chip = snd_pcm_substream_chip(substream);
1765 struct snd_pcm_runtime *runtime = substream->runtime;
Andreas Mohrda237f32010-12-27 21:17:26 +01001766 struct snd_azf3328_codec_data *codec = &chip->codecs[codec_type];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
1768 snd_azf3328_dbgcallenter();
Andreas Mohrda237f32010-12-27 21:17:26 +01001769 codec->substream = substream;
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001770
1771 /* same parameters for all our codecs - at least we think so... */
1772 runtime->hw = snd_azf3328_hardware;
1773
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1775 &snd_azf3328_hw_constraints_rates);
Andreas Mohrda237f32010-12-27 21:17:26 +01001776 runtime->private_data = codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 snd_azf3328_dbgcallleave();
1778 return 0;
1779}
1780
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001781static int
Andreas Mohrda237f32010-12-27 21:17:26 +01001782snd_azf3328_pcm_playback_open(struct snd_pcm_substream *substream)
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001783{
1784 return snd_azf3328_pcm_open(substream, AZF_CODEC_PLAYBACK);
1785}
1786
1787static int
Andreas Mohrda237f32010-12-27 21:17:26 +01001788snd_azf3328_pcm_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789{
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001790 return snd_azf3328_pcm_open(substream, AZF_CODEC_CAPTURE);
1791}
1792
1793static int
Andreas Mohrda237f32010-12-27 21:17:26 +01001794snd_azf3328_pcm_i2s_out_open(struct snd_pcm_substream *substream)
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001795{
1796 return snd_azf3328_pcm_open(substream, AZF_CODEC_I2S_OUT);
1797}
1798
1799static int
Andreas Mohrda237f32010-12-27 21:17:26 +01001800snd_azf3328_pcm_close(struct snd_pcm_substream *substream
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001801)
1802{
Andreas Mohrda237f32010-12-27 21:17:26 +01001803 struct snd_azf3328_codec_data *codec =
1804 substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
1806 snd_azf3328_dbgcallenter();
Andreas Mohrda237f32010-12-27 21:17:26 +01001807 codec->substream = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 snd_azf3328_dbgcallleave();
1809 return 0;
1810}
1811
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812/******************************************************************/
1813
Takashi Iwai95de7762005-11-17 15:02:42 +01001814static struct snd_pcm_ops snd_azf3328_playback_ops = {
Andreas Mohrda237f32010-12-27 21:17:26 +01001815 .open = snd_azf3328_pcm_playback_open,
1816 .close = snd_azf3328_pcm_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 .ioctl = snd_pcm_lib_ioctl,
1818 .hw_params = snd_azf3328_hw_params,
1819 .hw_free = snd_azf3328_hw_free,
Andreas Mohrda237f32010-12-27 21:17:26 +01001820 .prepare = snd_azf3328_pcm_prepare,
1821 .trigger = snd_azf3328_pcm_trigger,
1822 .pointer = snd_azf3328_pcm_pointer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823};
1824
Takashi Iwai95de7762005-11-17 15:02:42 +01001825static struct snd_pcm_ops snd_azf3328_capture_ops = {
Andreas Mohrda237f32010-12-27 21:17:26 +01001826 .open = snd_azf3328_pcm_capture_open,
1827 .close = snd_azf3328_pcm_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 .ioctl = snd_pcm_lib_ioctl,
1829 .hw_params = snd_azf3328_hw_params,
1830 .hw_free = snd_azf3328_hw_free,
Andreas Mohrda237f32010-12-27 21:17:26 +01001831 .prepare = snd_azf3328_pcm_prepare,
1832 .trigger = snd_azf3328_pcm_trigger,
1833 .pointer = snd_azf3328_pcm_pointer
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001834};
1835
1836static struct snd_pcm_ops snd_azf3328_i2s_out_ops = {
Andreas Mohrda237f32010-12-27 21:17:26 +01001837 .open = snd_azf3328_pcm_i2s_out_open,
1838 .close = snd_azf3328_pcm_close,
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001839 .ioctl = snd_pcm_lib_ioctl,
1840 .hw_params = snd_azf3328_hw_params,
1841 .hw_free = snd_azf3328_hw_free,
Andreas Mohrda237f32010-12-27 21:17:26 +01001842 .prepare = snd_azf3328_pcm_prepare,
1843 .trigger = snd_azf3328_pcm_trigger,
1844 .pointer = snd_azf3328_pcm_pointer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845};
1846
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001847static int __devinit
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001848snd_azf3328_pcm(struct snd_azf3328 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849{
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001850enum { AZF_PCMDEV_STD, AZF_PCMDEV_I2S_OUT, NUM_AZF_PCMDEVS }; /* pcm devices */
1851
Takashi Iwai95de7762005-11-17 15:02:42 +01001852 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 int err;
1854
1855 snd_azf3328_dbgcallenter();
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001856
1857 err = snd_pcm_new(chip->card, "AZF3328 DSP", AZF_PCMDEV_STD,
1858 1, 1, &pcm);
1859 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 return err;
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001861 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1862 &snd_azf3328_playback_ops);
1863 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1864 &snd_azf3328_capture_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
1866 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 pcm->info_flags = 0;
1868 strcpy(pcm->name, chip->card->shortname);
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001869 /* same pcm object for playback/capture (see snd_pcm_new() above) */
1870 chip->pcm[AZF_CODEC_PLAYBACK] = pcm;
1871 chip->pcm[AZF_CODEC_CAPTURE] = pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
1873 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001874 snd_dma_pci_data(chip->pci),
1875 64*1024, 64*1024);
1876
1877 err = snd_pcm_new(chip->card, "AZF3328 I2S OUT", AZF_PCMDEV_I2S_OUT,
1878 1, 0, &pcm);
1879 if (err < 0)
1880 return err;
1881 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1882 &snd_azf3328_i2s_out_ops);
1883
1884 pcm->private_data = chip;
1885 pcm->info_flags = 0;
1886 strcpy(pcm->name, chip->card->shortname);
1887 chip->pcm[AZF_CODEC_I2S_OUT] = pcm;
1888
1889 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1890 snd_dma_pci_data(chip->pci),
1891 64*1024, 64*1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
1893 snd_azf3328_dbgcallleave();
1894 return 0;
1895}
1896
1897/******************************************************************/
1898
Andreas Mohr02330fb2008-05-16 12:18:29 +02001899/*** NOTE: the physical timer resolution actually is 1024000 ticks per second
1900 *** (probably derived from main crystal via a divider of 24),
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001901 *** but announcing those attributes to user-space would make programs
1902 *** configure the timer to a 1 tick value, resulting in an absolutely fatal
1903 *** timer IRQ storm.
1904 *** Thus I chose to announce a down-scaled virtual timer to the outside and
1905 *** calculate real timer countdown values internally.
1906 *** (the scale factor can be set via module parameter "seqtimer_scaling").
1907 ***/
1908
1909static int
Takashi Iwai95de7762005-11-17 15:02:42 +01001910snd_azf3328_timer_start(struct snd_timer *timer)
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001911{
Takashi Iwai95de7762005-11-17 15:02:42 +01001912 struct snd_azf3328 *chip;
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001913 unsigned long flags;
1914 unsigned int delay;
1915
1916 snd_azf3328_dbgcallenter();
1917 chip = snd_timer_chip(timer);
1918 delay = ((timer->sticks * seqtimer_scaling) - 1) & TIMER_VALUE_MASK;
Andreas Mohre2f87262006-05-17 11:04:19 +02001919 if (delay < 49) {
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001920 /* uhoh, that's not good, since user-space won't know about
1921 * this timing tweak
1922 * (we need to do it to avoid a lockup, though) */
1923
1924 snd_azf3328_dbgtimer("delay was too low (%d)!\n", delay);
1925 delay = 49; /* minimum time is 49 ticks */
1926 }
Andreas Mohradf59312010-12-27 21:16:43 +01001927 snd_azf3328_dbgtimer("setting timer countdown value %d\n", delay);
Andreas Mohr02330fb2008-05-16 12:18:29 +02001928 delay |= TIMER_COUNTDOWN_ENABLE | TIMER_IRQ_ENABLE;
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001929 spin_lock_irqsave(&chip->reg_lock, flags);
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001930 snd_azf3328_ctrl_outl(chip, IDX_IO_TIMER_VALUE, delay);
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001931 spin_unlock_irqrestore(&chip->reg_lock, flags);
1932 snd_azf3328_dbgcallleave();
1933 return 0;
1934}
1935
1936static int
Takashi Iwai95de7762005-11-17 15:02:42 +01001937snd_azf3328_timer_stop(struct snd_timer *timer)
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001938{
Takashi Iwai95de7762005-11-17 15:02:42 +01001939 struct snd_azf3328 *chip;
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001940 unsigned long flags;
1941
1942 snd_azf3328_dbgcallenter();
1943 chip = snd_timer_chip(timer);
1944 spin_lock_irqsave(&chip->reg_lock, flags);
1945 /* disable timer countdown and interrupt */
Andreas Mohr79741502010-11-21 12:09:32 +01001946 /* Hmm, should we write TIMER_IRQ_ACK here?
1947 YES indeed, otherwise a rogue timer operation - which prompts
1948 ALSA(?) to call repeated stop() in vain, but NOT start() -
1949 will never end (value 0x03 is kept shown in control byte).
1950 Simply manually poking 0x04 _once_ immediately successfully stops
1951 the hardware/ALSA interrupt activity. */
1952 snd_azf3328_ctrl_outb(chip, IDX_IO_TIMER_VALUE + 3, 0x04);
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001953 spin_unlock_irqrestore(&chip->reg_lock, flags);
1954 snd_azf3328_dbgcallleave();
1955 return 0;
1956}
1957
1958
1959static int
Takashi Iwai95de7762005-11-17 15:02:42 +01001960snd_azf3328_timer_precise_resolution(struct snd_timer *timer,
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001961 unsigned long *num, unsigned long *den)
1962{
1963 snd_azf3328_dbgcallenter();
1964 *num = 1;
1965 *den = 1024000 / seqtimer_scaling;
1966 snd_azf3328_dbgcallleave();
1967 return 0;
1968}
1969
Takashi Iwai95de7762005-11-17 15:02:42 +01001970static struct snd_timer_hardware snd_azf3328_timer_hw = {
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001971 .flags = SNDRV_TIMER_HW_AUTO,
1972 .resolution = 977, /* 1000000/1024000 = 0.9765625us */
1973 .ticks = 1024000, /* max tick count, defined by the value register; actually it's not 1024000, but 1048576, but we don't care */
1974 .start = snd_azf3328_timer_start,
1975 .stop = snd_azf3328_timer_stop,
1976 .precise_resolution = snd_azf3328_timer_precise_resolution,
1977};
1978
1979static int __devinit
Takashi Iwai95de7762005-11-17 15:02:42 +01001980snd_azf3328_timer(struct snd_azf3328 *chip, int device)
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001981{
Takashi Iwai95de7762005-11-17 15:02:42 +01001982 struct snd_timer *timer = NULL;
1983 struct snd_timer_id tid;
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001984 int err;
1985
1986 snd_azf3328_dbgcallenter();
1987 tid.dev_class = SNDRV_TIMER_CLASS_CARD;
1988 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1989 tid.card = chip->card->number;
1990 tid.device = device;
1991 tid.subdevice = 0;
1992
1993 snd_azf3328_timer_hw.resolution *= seqtimer_scaling;
1994 snd_azf3328_timer_hw.ticks /= seqtimer_scaling;
Andreas Mohr02330fb2008-05-16 12:18:29 +02001995
1996 err = snd_timer_new(chip->card, "AZF3328", &tid, &timer);
1997 if (err < 0)
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001998 goto out;
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001999
2000 strcpy(timer->name, "AZF3328 timer");
2001 timer->private_data = chip;
2002 timer->hw = snd_azf3328_timer_hw;
2003
2004 chip->timer = timer;
2005
Andreas Mohr02330fb2008-05-16 12:18:29 +02002006 snd_azf3328_timer_stop(timer);
2007
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002008 err = 0;
2009
2010out:
2011 snd_azf3328_dbgcallleave();
2012 return err;
2013}
2014
2015/******************************************************************/
2016
Andreas Mohr02330fb2008-05-16 12:18:29 +02002017static int
2018snd_azf3328_free(struct snd_azf3328 *chip)
2019{
2020 if (chip->irq < 0)
2021 goto __end_hw;
2022
2023 /* reset (close) mixer:
2024 * first mute master volume, then reset
2025 */
2026 snd_azf3328_mixer_set_mute(chip, IDX_MIXER_PLAY_MASTER, 1);
2027 snd_azf3328_mixer_outw(chip, IDX_MIXER_RESET, 0x0000);
2028
2029 snd_azf3328_timer_stop(chip->timer);
2030 snd_azf3328_gameport_free(chip);
2031
2032 if (chip->irq >= 0)
2033 synchronize_irq(chip->irq);
2034__end_hw:
2035 if (chip->irq >= 0)
2036 free_irq(chip->irq, chip);
2037 pci_release_regions(chip->pci);
2038 pci_disable_device(chip->pci);
2039
2040 kfree(chip);
2041 return 0;
2042}
2043
2044static int
2045snd_azf3328_dev_free(struct snd_device *device)
2046{
2047 struct snd_azf3328 *chip = device->device_data;
2048 return snd_azf3328_free(chip);
2049}
2050
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051#if 0
2052/* check whether a bit can be modified */
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002053static void
Andreas Mohr02330fb2008-05-16 12:18:29 +02002054snd_azf3328_test_bit(unsigned unsigned reg, int bit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055{
2056 unsigned char val, valoff, valon;
2057
2058 val = inb(reg);
2059
2060 outb(val & ~(1 << bit), reg);
2061 valoff = inb(reg);
2062
2063 outb(val|(1 << bit), reg);
2064 valon = inb(reg);
Andreas Mohr02330fb2008-05-16 12:18:29 +02002065
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 outb(val, reg);
2067
Andreas Mohr78df6172009-07-12 22:17:54 +02002068 printk(KERN_DEBUG "reg %04x bit %d: %02x %02x %02x\n",
Andreas Mohr02330fb2008-05-16 12:18:29 +02002069 reg, bit, val, valoff, valon
2070 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071}
2072#endif
2073
Andreas Mohr02330fb2008-05-16 12:18:29 +02002074static inline void
Takashi Iwai95de7762005-11-17 15:02:42 +01002075snd_azf3328_debug_show_ports(const struct snd_azf3328 *chip)
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002076{
Andreas Mohr02330fb2008-05-16 12:18:29 +02002077#if DEBUG_MISC
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002078 u16 tmp;
2079
Andreas Mohr02330fb2008-05-16 12:18:29 +02002080 snd_azf3328_dbgmisc(
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002081 "ctrl_io 0x%lx, game_io 0x%lx, mpu_io 0x%lx, "
Andreas Mohr02330fb2008-05-16 12:18:29 +02002082 "opl3_io 0x%lx, mixer_io 0x%lx, irq %d\n",
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002083 chip->ctrl_io, chip->game_io, chip->mpu_io,
Andreas Mohr02330fb2008-05-16 12:18:29 +02002084 chip->opl3_io, chip->mixer_io, chip->irq
2085 );
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002086
Andreas Mohr02330fb2008-05-16 12:18:29 +02002087 snd_azf3328_dbgmisc("game %02x %02x %02x %02x %02x %02x\n",
2088 snd_azf3328_game_inb(chip, 0),
2089 snd_azf3328_game_inb(chip, 1),
2090 snd_azf3328_game_inb(chip, 2),
2091 snd_azf3328_game_inb(chip, 3),
2092 snd_azf3328_game_inb(chip, 4),
2093 snd_azf3328_game_inb(chip, 5)
2094 );
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002095
Andreas Mohr02330fb2008-05-16 12:18:29 +02002096 for (tmp = 0; tmp < 0x07; tmp += 1)
2097 snd_azf3328_dbgmisc("mpu_io 0x%04x\n", inb(chip->mpu_io + tmp));
2098
2099 for (tmp = 0; tmp <= 0x07; tmp += 1)
2100 snd_azf3328_dbgmisc("0x%02x: game200 0x%04x, game208 0x%04x\n",
2101 tmp, inb(0x200 + tmp), inb(0x208 + tmp));
2102
2103 for (tmp = 0; tmp <= 0x01; tmp += 1)
2104 snd_azf3328_dbgmisc(
2105 "0x%02x: mpu300 0x%04x, mpu310 0x%04x, mpu320 0x%04x, "
2106 "mpu330 0x%04x opl388 0x%04x opl38c 0x%04x\n",
2107 tmp,
2108 inb(0x300 + tmp),
2109 inb(0x310 + tmp),
2110 inb(0x320 + tmp),
2111 inb(0x330 + tmp),
2112 inb(0x388 + tmp),
2113 inb(0x38c + tmp)
2114 );
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002115
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002116 for (tmp = 0; tmp < AZF_IO_SIZE_CTRL; tmp += 2)
2117 snd_azf3328_dbgmisc("ctrl 0x%02x: 0x%04x\n",
2118 tmp, snd_azf3328_ctrl_inw(chip, tmp)
Andreas Mohr02330fb2008-05-16 12:18:29 +02002119 );
Andreas Mohre24a1212007-03-26 12:49:45 +02002120
2121 for (tmp = 0; tmp < AZF_IO_SIZE_MIXER; tmp += 2)
Andreas Mohr02330fb2008-05-16 12:18:29 +02002122 snd_azf3328_dbgmisc("mixer 0x%02x: 0x%04x\n",
2123 tmp, snd_azf3328_mixer_inw(chip, tmp)
2124 );
2125#endif /* DEBUG_MISC */
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002126}
2127
2128static int __devinit
Takashi Iwai95de7762005-11-17 15:02:42 +01002129snd_azf3328_create(struct snd_card *card,
Andreas Mohr02330fb2008-05-16 12:18:29 +02002130 struct pci_dev *pci,
2131 unsigned long device_type,
2132 struct snd_azf3328 **rchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133{
Takashi Iwai95de7762005-11-17 15:02:42 +01002134 struct snd_azf3328 *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 int err;
Takashi Iwai95de7762005-11-17 15:02:42 +01002136 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 .dev_free = snd_azf3328_dev_free,
2138 };
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002139 u8 dma_init;
2140 enum snd_azf3328_codec_type codec_type;
Andreas Mohrda237f32010-12-27 21:17:26 +01002141 struct snd_azf3328_codec_data *codec_setup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
2143 *rchip = NULL;
2144
Andreas Mohr02330fb2008-05-16 12:18:29 +02002145 err = pci_enable_device(pci);
2146 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 return err;
2148
Takashi Iwaie560d8d2005-09-09 14:21:46 +02002149 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 if (chip == NULL) {
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002151 err = -ENOMEM;
2152 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 }
2154 spin_lock_init(&chip->reg_lock);
2155 chip->card = card;
2156 chip->pci = pci;
2157 chip->irq = -1;
2158
2159 /* check if we can restrict PCI DMA transfers to 24 bits */
Yang Hongyang2f4f27d2009-04-06 19:01:18 -07002160 if (pci_set_dma_mask(pci, DMA_BIT_MASK(24)) < 0 ||
2161 pci_set_consistent_dma_mask(pci, DMA_BIT_MASK(24)) < 0) {
Andreas Mohr02330fb2008-05-16 12:18:29 +02002162 snd_printk(KERN_ERR "architecture does not support "
2163 "24bit PCI busmaster DMA\n"
2164 );
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002165 err = -ENXIO;
2166 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 }
2168
Andreas Mohr02330fb2008-05-16 12:18:29 +02002169 err = pci_request_regions(pci, "Aztech AZF3328");
2170 if (err < 0)
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002171 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002173 chip->ctrl_io = pci_resource_start(pci, 0);
Andreas Mohr02330fb2008-05-16 12:18:29 +02002174 chip->game_io = pci_resource_start(pci, 1);
2175 chip->mpu_io = pci_resource_start(pci, 2);
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002176 chip->opl3_io = pci_resource_start(pci, 3);
Andreas Mohr02330fb2008-05-16 12:18:29 +02002177 chip->mixer_io = pci_resource_start(pci, 4);
2178
Andreas Mohr9fd8d362010-12-27 21:17:00 +01002179 codec_setup = &chip->codecs[AZF_CODEC_PLAYBACK];
2180 codec_setup->io_base = chip->ctrl_io + AZF_IO_OFFS_CODEC_PLAYBACK;
Andreas Mohrda237f32010-12-27 21:17:26 +01002181 codec_setup->lock = &chip->reg_lock;
2182 codec_setup->type = AZF_CODEC_PLAYBACK;
Andreas Mohr9fd8d362010-12-27 21:17:00 +01002183 codec_setup->name = "PLAYBACK";
2184
2185 codec_setup = &chip->codecs[AZF_CODEC_CAPTURE];
2186 codec_setup->io_base = chip->ctrl_io + AZF_IO_OFFS_CODEC_CAPTURE;
Andreas Mohrda237f32010-12-27 21:17:26 +01002187 codec_setup->lock = &chip->reg_lock;
2188 codec_setup->type = AZF_CODEC_CAPTURE;
Andreas Mohr9fd8d362010-12-27 21:17:00 +01002189 codec_setup->name = "CAPTURE";
2190
2191 codec_setup = &chip->codecs[AZF_CODEC_I2S_OUT];
2192 codec_setup->io_base = chip->ctrl_io + AZF_IO_OFFS_CODEC_I2S_OUT;
Andreas Mohrda237f32010-12-27 21:17:26 +01002193 codec_setup->lock = &chip->reg_lock;
2194 codec_setup->type = AZF_CODEC_I2S_OUT;
Andreas Mohr9fd8d362010-12-27 21:17:00 +01002195 codec_setup->name = "I2S_OUT";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196
Takashi Iwai437a5a42006-11-21 12:14:23 +01002197 if (request_irq(pci->irq, snd_azf3328_interrupt,
2198 IRQF_SHARED, card->shortname, chip)) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02002199 snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002200 err = -EBUSY;
2201 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 }
2203 chip->irq = pci->irq;
2204 pci_set_master(pci);
2205 synchronize_irq(chip->irq);
2206
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002207 snd_azf3328_debug_show_ports(chip);
Andreas Mohr02330fb2008-05-16 12:18:29 +02002208
2209 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
2210 if (err < 0)
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002211 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
2213 /* create mixer interface & switches */
Andreas Mohr02330fb2008-05-16 12:18:29 +02002214 err = snd_azf3328_mixer_new(chip);
2215 if (err < 0)
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002216 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002218 /* standard codec init stuff */
2219 /* default DMA init value */
2220 dma_init = DMA_RUN_SOMETHING2|DMA_EPILOGUE_SOMETHING|DMA_SOMETHING_ELSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002222 for (codec_type = AZF_CODEC_PLAYBACK;
2223 codec_type <= AZF_CODEC_I2S_OUT; ++codec_type) {
2224 struct snd_azf3328_codec_data *codec =
2225 &chip->codecs[codec_type];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
Andreas Mohradf59312010-12-27 21:16:43 +01002227 /* shutdown codecs to reduce power / noise */
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002228 /* have ...ctrl_codec_activity() act properly */
2229 codec->running = 1;
2230 snd_azf3328_ctrl_codec_activity(chip, codec_type, 0);
2231
Andreas Mohrda237f32010-12-27 21:17:26 +01002232 spin_lock_irq(codec->lock);
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002233 snd_azf3328_codec_outb(codec, IDX_IO_CODEC_DMA_FLAGS,
2234 dma_init);
Andreas Mohrda237f32010-12-27 21:17:26 +01002235 spin_unlock_irq(codec->lock);
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
2238 snd_card_set_dev(card, &pci->dev);
2239
2240 *rchip = chip;
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002241
2242 err = 0;
2243 goto out;
2244
2245out_err:
2246 if (chip)
2247 snd_azf3328_free(chip);
2248 pci_disable_device(pci);
2249
2250out:
2251 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252}
2253
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002254static int __devinit
2255snd_azf3328_probe(struct pci_dev *pci, const struct pci_device_id *pci_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256{
2257 static int dev;
Takashi Iwai95de7762005-11-17 15:02:42 +01002258 struct snd_card *card;
2259 struct snd_azf3328 *chip;
2260 struct snd_opl3 *opl3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 int err;
2262
2263 snd_azf3328_dbgcallenter();
2264 if (dev >= SNDRV_CARDS)
2265 return -ENODEV;
2266 if (!enable[dev]) {
2267 dev++;
2268 return -ENOENT;
2269 }
2270
Takashi Iwaie58de7b2008-12-28 16:44:30 +01002271 err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
2272 if (err < 0)
2273 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
2275 strcpy(card->driver, "AZF3328");
2276 strcpy(card->shortname, "Aztech AZF3328 (PCI168)");
2277
Andreas Mohr02330fb2008-05-16 12:18:29 +02002278 err = snd_azf3328_create(card, pci, pci_id->driver_data, &chip);
2279 if (err < 0)
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002280 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281
Andreas Mohrca54bde2006-05-17 11:02:24 +02002282 card->private_data = chip;
2283
Andreas Mohr78df6172009-07-12 22:17:54 +02002284 /* chose to use MPU401_HW_AZT2320 ID instead of MPU401_HW_MPU401,
2285 since our hardware ought to be similar, thus use same ID. */
Andreas Mohr02330fb2008-05-16 12:18:29 +02002286 err = snd_mpu401_uart_new(
Andreas Mohr78df6172009-07-12 22:17:54 +02002287 card, 0,
2288 MPU401_HW_AZT2320, chip->mpu_io, MPU401_INFO_INTEGRATED,
Andreas Mohr02330fb2008-05-16 12:18:29 +02002289 pci->irq, 0, &chip->rmidi
2290 );
2291 if (err < 0) {
2292 snd_printk(KERN_ERR "azf3328: no MPU-401 device at 0x%lx?\n",
2293 chip->mpu_io
2294 );
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002295 goto out_err;
2296 }
2297
Andreas Mohr02330fb2008-05-16 12:18:29 +02002298 err = snd_azf3328_timer(chip, 0);
2299 if (err < 0)
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002300 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002302 err = snd_azf3328_pcm(chip);
Andreas Mohr02330fb2008-05-16 12:18:29 +02002303 if (err < 0)
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002304 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305
Andreas Mohr02330fb2008-05-16 12:18:29 +02002306 if (snd_opl3_create(card, chip->opl3_io, chip->opl3_io+2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 OPL3_HW_AUTO, 1, &opl3) < 0) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02002308 snd_printk(KERN_ERR "azf3328: no OPL3 device at 0x%lx-0x%lx?\n",
Andreas Mohr02330fb2008-05-16 12:18:29 +02002309 chip->opl3_io, chip->opl3_io+2
2310 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 } else {
Andreas Mohr02330fb2008-05-16 12:18:29 +02002312 /* need to use IDs 1, 2 since ID 0 is snd_azf3328_timer above */
2313 err = snd_opl3_timer_new(opl3, 1, 2);
2314 if (err < 0)
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002315 goto out_err;
Andreas Mohr02330fb2008-05-16 12:18:29 +02002316 err = snd_opl3_hwdep_new(opl3, 0, 1, NULL);
2317 if (err < 0)
2318 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 }
2320
Andreas Mohrca54bde2006-05-17 11:02:24 +02002321 opl3->private_data = chip;
2322
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 sprintf(card->longname, "%s at 0x%lx, irq %i",
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002324 card->shortname, chip->ctrl_io, chip->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325
Andreas Mohr02330fb2008-05-16 12:18:29 +02002326 err = snd_card_register(card);
2327 if (err < 0)
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002328 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
2330#ifdef MODULE
Andreas Mohr78df6172009-07-12 22:17:54 +02002331 printk(KERN_INFO
Andreas Mohre24a1212007-03-26 12:49:45 +02002332"azt3328: Sound driver for Aztech AZF3328-based soundcards such as PCI168.\n"
2333"azt3328: Hardware was completely undocumented, unfortunately.\n"
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002334"azt3328: Feel free to contact andi AT lisas.de for bug reports etc.!\n"
2335"azt3328: User-scalable sequencer timer set to %dHz (1024000Hz / %d).\n",
2336 1024000 / seqtimer_scaling, seqtimer_scaling);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337#endif
2338
Andreas Mohr02330fb2008-05-16 12:18:29 +02002339 snd_azf3328_gameport(chip, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
2341 pci_set_drvdata(pci, card);
2342 dev++;
2343
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002344 err = 0;
2345 goto out;
Andreas Mohr02330fb2008-05-16 12:18:29 +02002346
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002347out_err:
Andreas Mohr02330fb2008-05-16 12:18:29 +02002348 snd_printk(KERN_ERR "azf3328: something failed, exiting\n");
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002349 snd_card_free(card);
Andreas Mohr02330fb2008-05-16 12:18:29 +02002350
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002351out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 snd_azf3328_dbgcallleave();
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002353 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354}
2355
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002356static void __devexit
2357snd_azf3328_remove(struct pci_dev *pci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358{
2359 snd_azf3328_dbgcallenter();
2360 snd_card_free(pci_get_drvdata(pci));
2361 pci_set_drvdata(pci, NULL);
2362 snd_azf3328_dbgcallleave();
2363}
2364
Andreas Mohrca54bde2006-05-17 11:02:24 +02002365#ifdef CONFIG_PM
Andreas Mohr78df6172009-07-12 22:17:54 +02002366static inline void
2367snd_azf3328_suspend_regs(unsigned long io_addr, unsigned count, u32 *saved_regs)
2368{
2369 unsigned reg;
2370
2371 for (reg = 0; reg < count; ++reg) {
2372 *saved_regs = inl(io_addr);
2373 snd_azf3328_dbgpm("suspend: io 0x%04lx: 0x%08x\n",
2374 io_addr, *saved_regs);
2375 ++saved_regs;
2376 io_addr += sizeof(*saved_regs);
2377 }
2378}
2379
Andreas Mohrca54bde2006-05-17 11:02:24 +02002380static int
2381snd_azf3328_suspend(struct pci_dev *pci, pm_message_t state)
2382{
2383 struct snd_card *card = pci_get_drvdata(pci);
2384 struct snd_azf3328 *chip = card->private_data;
Andreas Mohr78df6172009-07-12 22:17:54 +02002385 u16 *saved_regs_ctrl_u16;
Andreas Mohrca54bde2006-05-17 11:02:24 +02002386
2387 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
Andreas Mohr02330fb2008-05-16 12:18:29 +02002388
Andreas Mohradf59312010-12-27 21:16:43 +01002389 /* same pcm object for playback/capture */
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002390 snd_pcm_suspend_all(chip->pcm[AZF_CODEC_PLAYBACK]);
2391 snd_pcm_suspend_all(chip->pcm[AZF_CODEC_I2S_OUT]);
Andreas Mohrca54bde2006-05-17 11:02:24 +02002392
Andreas Mohr78df6172009-07-12 22:17:54 +02002393 snd_azf3328_suspend_regs(chip->mixer_io,
2394 ARRAY_SIZE(chip->saved_regs_mixer), chip->saved_regs_mixer);
Andreas Mohrca54bde2006-05-17 11:02:24 +02002395
2396 /* make sure to disable master volume etc. to prevent looping sound */
2397 snd_azf3328_mixer_set_mute(chip, IDX_MIXER_PLAY_MASTER, 1);
2398 snd_azf3328_mixer_set_mute(chip, IDX_MIXER_WAVEOUT, 1);
Andreas Mohr02330fb2008-05-16 12:18:29 +02002399
Andreas Mohr78df6172009-07-12 22:17:54 +02002400 snd_azf3328_suspend_regs(chip->ctrl_io,
2401 ARRAY_SIZE(chip->saved_regs_ctrl), chip->saved_regs_ctrl);
Andreas Mohr627d3e72008-06-23 11:50:47 +02002402
2403 /* manually store the one currently relevant write-only reg, too */
Andreas Mohr78df6172009-07-12 22:17:54 +02002404 saved_regs_ctrl_u16 = (u16 *)chip->saved_regs_ctrl;
2405 saved_regs_ctrl_u16[IDX_IO_6AH / 2] = chip->shadow_reg_ctrl_6AH;
Andreas Mohr627d3e72008-06-23 11:50:47 +02002406
Andreas Mohr78df6172009-07-12 22:17:54 +02002407 snd_azf3328_suspend_regs(chip->game_io,
2408 ARRAY_SIZE(chip->saved_regs_game), chip->saved_regs_game);
2409 snd_azf3328_suspend_regs(chip->mpu_io,
2410 ARRAY_SIZE(chip->saved_regs_mpu), chip->saved_regs_mpu);
2411 snd_azf3328_suspend_regs(chip->opl3_io,
2412 ARRAY_SIZE(chip->saved_regs_opl3), chip->saved_regs_opl3);
Andreas Mohrca54bde2006-05-17 11:02:24 +02002413
Andreas Mohrca54bde2006-05-17 11:02:24 +02002414 pci_disable_device(pci);
2415 pci_save_state(pci);
Takashi Iwai30b35392006-10-11 18:52:53 +02002416 pci_set_power_state(pci, pci_choose_state(pci, state));
Andreas Mohrca54bde2006-05-17 11:02:24 +02002417 return 0;
2418}
2419
Andreas Mohr78df6172009-07-12 22:17:54 +02002420static inline void
2421snd_azf3328_resume_regs(const u32 *saved_regs,
2422 unsigned long io_addr,
2423 unsigned count
2424)
2425{
2426 unsigned reg;
2427
2428 for (reg = 0; reg < count; ++reg) {
2429 outl(*saved_regs, io_addr);
2430 snd_azf3328_dbgpm("resume: io 0x%04lx: 0x%08x --> 0x%08x\n",
2431 io_addr, *saved_regs, inl(io_addr));
2432 ++saved_regs;
2433 io_addr += sizeof(*saved_regs);
2434 }
2435}
2436
Andreas Mohrca54bde2006-05-17 11:02:24 +02002437static int
2438snd_azf3328_resume(struct pci_dev *pci)
2439{
2440 struct snd_card *card = pci_get_drvdata(pci);
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002441 const struct snd_azf3328 *chip = card->private_data;
Andreas Mohrca54bde2006-05-17 11:02:24 +02002442
Andreas Mohrca54bde2006-05-17 11:02:24 +02002443 pci_set_power_state(pci, PCI_D0);
Takashi Iwai30b35392006-10-11 18:52:53 +02002444 pci_restore_state(pci);
2445 if (pci_enable_device(pci) < 0) {
2446 printk(KERN_ERR "azt3328: pci_enable_device failed, "
2447 "disabling device\n");
2448 snd_card_disconnect(card);
2449 return -EIO;
2450 }
Andreas Mohrca54bde2006-05-17 11:02:24 +02002451 pci_set_master(pci);
2452
Andreas Mohr78df6172009-07-12 22:17:54 +02002453 snd_azf3328_resume_regs(chip->saved_regs_game, chip->game_io,
2454 ARRAY_SIZE(chip->saved_regs_game));
2455 snd_azf3328_resume_regs(chip->saved_regs_mpu, chip->mpu_io,
2456 ARRAY_SIZE(chip->saved_regs_mpu));
2457 snd_azf3328_resume_regs(chip->saved_regs_opl3, chip->opl3_io,
2458 ARRAY_SIZE(chip->saved_regs_opl3));
2459
2460 snd_azf3328_resume_regs(chip->saved_regs_mixer, chip->mixer_io,
2461 ARRAY_SIZE(chip->saved_regs_mixer));
2462
2463 /* unfortunately with 32bit transfers, IDX_MIXER_PLAY_MASTER (0x02)
2464 and IDX_MIXER_RESET (offset 0x00) get touched at the same time,
2465 resulting in a mixer reset condition persisting until _after_
2466 master vol was restored. Thus master vol needs an extra restore. */
2467 outw(((u16 *)chip->saved_regs_mixer)[1], chip->mixer_io + 2);
2468
2469 snd_azf3328_resume_regs(chip->saved_regs_ctrl, chip->ctrl_io,
2470 ARRAY_SIZE(chip->saved_regs_ctrl));
Andreas Mohrca54bde2006-05-17 11:02:24 +02002471
2472 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
2473 return 0;
2474}
Andreas Mohr02330fb2008-05-16 12:18:29 +02002475#endif /* CONFIG_PM */
Andreas Mohrca54bde2006-05-17 11:02:24 +02002476
2477
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478static struct pci_driver driver = {
2479 .name = "AZF3328",
2480 .id_table = snd_azf3328_ids,
2481 .probe = snd_azf3328_probe,
2482 .remove = __devexit_p(snd_azf3328_remove),
Andreas Mohrca54bde2006-05-17 11:02:24 +02002483#ifdef CONFIG_PM
2484 .suspend = snd_azf3328_suspend,
2485 .resume = snd_azf3328_resume,
2486#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487};
2488
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002489static int __init
2490alsa_card_azf3328_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491{
2492 int err;
2493 snd_azf3328_dbgcallenter();
Takashi Iwai01d25d42005-04-11 16:58:24 +02002494 err = pci_register_driver(&driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 snd_azf3328_dbgcallleave();
2496 return err;
2497}
2498
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002499static void __exit
2500alsa_card_azf3328_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501{
2502 snd_azf3328_dbgcallenter();
2503 pci_unregister_driver(&driver);
2504 snd_azf3328_dbgcallleave();
2505}
2506
2507module_init(alsa_card_azf3328_init)
2508module_exit(alsa_card_azf3328_exit)