blob: f92c9cbb955a211cb85ca7907a9b8537973d3995 [file] [log] [blame]
Thomas Gleixner778ddf52019-05-23 11:14:53 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Andreas Mohrb5dc20c2011-02-19 00:49:32 +01002/* azt3328.c - driver for Aztech AZF3328 based soundcards (e.g. PCI168).
3 * Copyright (C) 2002, 2005 - 2011 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 * NOTES
21 * Since Aztech does not provide any chipset documentation,
22 * even on repeated request to various addresses,
23 * and the answer that was finally given was negative
24 * (and I was stupid enough to manage to get hold of a PCI168 soundcard
25 * in the first place >:-P}),
26 * I was forced to base this driver on reverse engineering
27 * (3 weeks' worth of evenings filled with driver work).
Andreas Mohre2f87262006-05-17 11:04:19 +020028 * (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 -070029 *
Andreas Mohr02330fba2008-05-16 12:18:29 +020030 * It is quite likely that the AZF3328 chip is the PCI cousin of the
31 * AZF3318 ("azt1020 pnp", "MM Pro 16") ISA chip, given very similar specs.
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 *
Andreas Mohr02330fba2008-05-16 12:18:29 +020033 * The AZF3328 chip (note: AZF3328, *not* AZT3328, that's just the driver name
34 * for compatibility reasons) from Azfin (joint-venture of Aztech and Fincitec,
35 * Fincitec acquired by National Semiconductor in 2002, together with the
36 * Fincitec-related company ARSmikro) has the following features:
37 *
38 * - compatibility & compliance:
39 * - Microsoft PC 97 ("PC 97 Hardware Design Guide",
40 * http://www.microsoft.com/whdc/archive/pcguides.mspx)
41 * - Microsoft PC 98 Baseline Audio
42 * - MPU401 UART
43 * - Sound Blaster Emulation (DOS Box)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 * - builtin AC97 conformant codec (SNR over 80dB)
Andreas Mohr13769e32006-05-17 11:03:16 +020045 * Note that "conformant" != "compliant"!! this chip's mixer register layout
46 * *differs* from the standard AC97 layout:
47 * they chose to not implement the headphone register (which is not a
48 * problem since it's merely optional), yet when doing this, they committed
49 * the grave sin of letting other registers follow immediately instead of
50 * keeping a headphone dummy register, thereby shifting the mixer register
51 * addresses illegally. So far unfortunately it looks like the very flexible
52 * ALSA AC97 support is still not enough to easily compensate for such a
53 * grave layout violation despite all tweaks and quirks mechanisms it offers.
Andreas Mohrb5dc20c2011-02-19 00:49:32 +010054 * Well, not quite: now ac97 layer is much improved (bus-specific ops!),
55 * thus I was able to implement support - it's actually working quite well.
56 * An interesting item might be Aztech AMR 2800-W, since it's an AC97
57 * modem card which might reveal the Aztech-specific codec ID which
58 * we might want to pretend, too. Dito PCI168's brother, PCI368,
59 * where the advertising datasheet says it's AC97-based and has a
60 * Digital Enhanced Game Port.
Andreas Mohr02330fba2008-05-16 12:18:29 +020061 * - builtin genuine OPL3 - verified to work fine, 20080506
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 * - full duplex 16bit playback/record at independent sampling rate
Andreas Mohr02330fba2008-05-16 12:18:29 +020063 * - MPU401 (+ legacy address support, claimed by one official spec sheet)
64 * FIXME: how to enable legacy addr??
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 * - game port (legacy address support)
Andreas Mohre24a1212007-03-26 12:49:45 +020066 * - builtin DirectInput support, helps reduce CPU overhead (interrupt-driven
Andreas Mohr02330fba2008-05-16 12:18:29 +020067 * features supported). - See common term "Digital Enhanced Game Port"...
68 * (probably DirectInput 3.0 spec - confirm)
69 * - builtin 3D enhancement (said to be YAMAHA Ymersion)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 * - built-in General DirectX timer having a 20 bits counter
Andreas Mohrd91c64c2005-10-25 11:17:45 +020071 * with 1us resolution (see below!)
Andreas Mohr02330fba2008-05-16 12:18:29 +020072 * - I2S serial output port for external DAC
Andreas Mohrdfbf9512009-07-05 13:55:46 +020073 * [FIXME: 3.3V or 5V level? maximum rate is 66.2kHz right?]
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 * - supports 33MHz PCI spec 2.1, PCI power management 1.0, compliant with ACPI
75 * - supports hardware volume control
76 * - single chip low cost solution (128 pin QFP)
Andreas Mohrdfbf9512009-07-05 13:55:46 +020077 * - supports programmable Sub-vendor and Sub-system ID [24C02 SEEPROM chip]
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 * required for Microsoft's logo compliance (FIXME: where?)
Andreas Mohr02330fba2008-05-16 12:18:29 +020079 * At least the Trident 4D Wave DX has one bit somewhere
80 * to enable writes to PCI subsystem VID registers, that should be it.
81 * This might easily be in extended PCI reg space, since PCI168 also has
82 * some custom data starting at 0x80. What kind of config settings
83 * are located in our extended PCI space anyway??
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 * - PCI168 AP(W) card: power amplifier with 4 Watts/channel at 4 Ohms
Andreas Mohrdfbf9512009-07-05 13:55:46 +020085 * [TDA1517P chip]
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 *
Andreas Mohrd91c64c2005-10-25 11:17:45 +020087 * Note that this driver now is actually *better* than the Windows driver,
88 * since it additionally supports the card's 1MHz DirectX timer - just try
89 * the following snd-seq module parameters etc.:
90 * - options snd-seq seq_default_timer_class=2 seq_default_timer_sclass=0
91 * seq_default_timer_card=0 seq_client_load=1 seq_default_timer_device=0
92 * seq_default_timer_subdevice=0 seq_default_timer_resolution=1000000
93 * - "timidity -iAv -B2,8 -Os -EFreverb=0"
94 * - "pmidi -p 128:0 jazz.mid"
95 *
Andreas Mohr02330fba2008-05-16 12:18:29 +020096 * OPL3 hardware playback testing, try something like:
97 * cat /proc/asound/hwdep
98 * and
99 * aconnect -o
100 * Then use
101 * sbiload -Dhw:x,y --opl3 /usr/share/sounds/opl3/std.o3 ......./drums.o3
102 * where x,y is the xx-yy number as given in hwdep.
103 * Then try
104 * pmidi -p a:b jazz.mid
105 * where a:b is the client number plus 0 usually, as given by aconnect above.
106 * Oh, and make sure to unmute the FM mixer control (doh!)
107 * NOTE: power use during OPL3 playback is _VERY_ high (70W --> 90W!)
108 * despite no CPU activity, possibly due to hindering ACPI idling somehow.
109 * Shouldn't be a problem of the AZF3328 chip itself, I'd hope.
110 * Higher PCM / FM mixer levels seem to conflict (causes crackling),
111 * at least sometimes. Maybe even use with hardware sequencer timer above :)
112 * adplay/adplug-utils might soon offer hardware-based OPL3 playback, too.
113 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 * Certain PCI versions of this card are susceptible to DMA traffic underruns
115 * in some systems (resulting in sound crackling/clicking/popping),
116 * probably because they don't have a DMA FIFO buffer or so.
117 * Overview (PCI ID/PCI subID/PCI rev.):
118 * - no DMA crackling on SiS735: 0x50DC/0x1801/16
119 * - unknown performance: 0x50DC/0x1801/10
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200120 * (well, it's not bad on an Athlon 1800 with now very optimized IRQ handler)
121 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 * Crackling happens with VIA chipsets or, in my case, an SiS735, which is
123 * supposed to be very fast and supposed to get rid of crackling much
124 * better than a VIA, yet ironically I still get crackling, like many other
125 * people with the same chipset.
126 * Possible remedies:
Andreas Mohr02330fba2008-05-16 12:18:29 +0200127 * - use speaker (amplifier) output instead of headphone output
128 * (in case crackling is due to overloaded output clipping)
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300129 * - plug card into a different PCI slot, preferably one that isn't shared
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 * too much (this helps a lot, but not completely!)
131 * - get rid of PCI VGA card, use AGP instead
132 * - upgrade or downgrade BIOS
133 * - fiddle with PCI latency settings (setpci -v -s BUSID latency_timer=XX)
134 * Not too helpful.
135 * - Disable ACPI/power management/"Auto Detect RAM/PCI Clk" in BIOS
Andreas Mohr02330fba2008-05-16 12:18:29 +0200136 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 * BUGS
Andreas Mohr02330fba2008-05-16 12:18:29 +0200138 * - full-duplex might *still* be problematic, however a recent test was fine
Andreas Mohre24a1212007-03-26 12:49:45 +0200139 * - (non-bug) "Bass/Treble or 3D settings don't work" - they do get evaluated
140 * if you set PCM output switch to "pre 3D" instead of "post 3D".
141 * If this can't be set, then get a mixer application that Isn't Stupid (tm)
142 * (e.g. kmix, gamix) - unfortunately several are!!
Andreas Mohr02330fba2008-05-16 12:18:29 +0200143 * - locking is not entirely clean, especially the audio stream activity
144 * ints --> may be racy
145 * - an _unconnected_ secondary joystick at the gameport will be reported
146 * to be "active" (floating values, not precisely -1) due to the way we need
147 * to read the Digital Enhanced Game Port. Not sure whether it is fixable.
148 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 * TODO
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200150 * - use PCI_VDEVICE
151 * - verify driver status on x86_64
152 * - test multi-card driver operation
153 * - (ab)use 1MHz DirectX timer as kernel clocksource
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 * - test MPU401 MIDI playback etc.
Andreas Mohr02330fba2008-05-16 12:18:29 +0200155 * - add more power micro-management (disable various units of the card
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200156 * as long as they're unused, to improve audio quality and save power).
157 * However this requires more I/O ports which I haven't figured out yet
158 * and which thus might not even exist...
Andreas Mohrca54bde2006-05-17 11:02:24 +0200159 * The standard suspend/resume functionality could probably make use of
160 * some improvement, too...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 * - figure out what all unknown port bits are responsible for
Andreas Mohr13769e32006-05-17 11:03:16 +0200162 * - figure out some cleverly evil scheme to possibly make ALSA AC97 code
163 * fully accept our quite incompatible ""AC97"" mixer and thus save some
164 * code (but I'm not too optimistic that doing this is possible at all)
Andreas Mohr02330fba2008-05-16 12:18:29 +0200165 * - use MMIO (memory-mapped I/O)? Slightly faster access, e.g. for gameport.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 */
167
Takashi Iwai6cbbfe12015-01-28 16:49:33 +0100168#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169#include <linux/init.h>
Andreas Mohr689c6912010-12-27 21:17:35 +0100170#include <linux/bug.h> /* WARN_ONCE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171#include <linux/pci.h>
172#include <linux/delay.h>
173#include <linux/slab.h>
174#include <linux/gameport.h>
Paul Gortmaker65a77212011-07-15 13:13:37 -0400175#include <linux/module.h>
Matthias Gehre910638a2006-03-28 01:56:48 -0800176#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177#include <sound/core.h>
178#include <sound/control.h>
179#include <sound/pcm.h>
180#include <sound/rawmidi.h>
181#include <sound/mpu401.h>
182#include <sound/opl3.h>
183#include <sound/initval.h>
Andreas Mohrb5dc20c2011-02-19 00:49:32 +0100184/*
185 * Config switch, to use ALSA's AC97 layer instead of old custom mixer crap.
186 * If the AC97 compatibility parts we needed to implement locally turn out
187 * to work nicely, then remove the old implementation eventually.
188 */
189#define AZF_USE_AC97_LAYER 1
190
191#ifdef AZF_USE_AC97_LAYER
192#include <sound/ac97_codec.h>
193#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194#include "azt3328.h"
195
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200196MODULE_AUTHOR("Andreas Mohr <andi AT lisas.de>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197MODULE_DESCRIPTION("Aztech AZF3328 (PCI168)");
198MODULE_LICENSE("GPL");
199MODULE_SUPPORTED_DEVICE("{{Aztech,AZF3328}}");
200
Fabian Frederickb2fac072016-11-12 23:26:41 +0100201#if IS_REACHABLE(CONFIG_GAMEPORT)
Andreas Mohr02330fba2008-05-16 12:18:29 +0200202#define SUPPORT_GAMEPORT 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203#endif
204
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200205/* === Debug settings ===
206 Further diagnostic functionality than the settings below
Andreas Mohradf59312010-12-27 21:16:43 +0100207 does not need to be provided, since one can easily write a POSIX shell script
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200208 to dump the card's I/O ports (those listed in lspci -v -v):
Andreas Mohradf59312010-12-27 21:16:43 +0100209 dump()
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200210 {
211 local descr=$1; local addr=$2; local count=$3
212
213 echo "${descr}: ${count} @ ${addr}:"
Andreas Mohradf59312010-12-27 21:16:43 +0100214 dd if=/dev/port skip=`printf %d ${addr}` count=${count} bs=1 \
215 2>/dev/null| hexdump -C
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200216 }
217 and then use something like
218 "dump joy200 0x200 8", "dump mpu388 0x388 4", "dump joy 0xb400 8",
219 "dump codec00 0xa800 32", "dump mixer 0xb800 64", "dump synth 0xbc00 8",
220 possibly within a "while true; do ... sleep 1; done" loop.
221 Tweaking ports could be done using
222 VALSTRING="`printf "%02x" $value`"
Andreas Mohradf59312010-12-27 21:16:43 +0100223 printf "\x""$VALSTRING"|dd of=/dev/port seek=`printf %d ${addr}` bs=1 \
224 2>/dev/null
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200225*/
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
228module_param_array(index, int, NULL, 0444);
229MODULE_PARM_DESC(index, "Index value for AZF3328 soundcard.");
230
231static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
232module_param_array(id, charp, NULL, 0444);
233MODULE_PARM_DESC(id, "ID string for AZF3328 soundcard.");
234
Rusty Russella67ff6a2011-12-15 13:49:36 +1030235static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236module_param_array(enable, bool, NULL, 0444);
237MODULE_PARM_DESC(enable, "Enable AZF3328 soundcard.");
238
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200239static int seqtimer_scaling = 128;
240module_param(seqtimer_scaling, int, 0444);
241MODULE_PARM_DESC(seqtimer_scaling, "Set 1024000Hz sequencer timer scale factor (lockup danger!). Default 128.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200243enum snd_azf3328_codec_type {
Andreas Mohradf59312010-12-27 21:16:43 +0100244 /* warning: fixed indices (also used for bitmask checks!) */
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200245 AZF_CODEC_PLAYBACK = 0,
246 AZF_CODEC_CAPTURE = 1,
247 AZF_CODEC_I2S_OUT = 2,
Andreas Mohr02330fba2008-05-16 12:18:29 +0200248};
249
Andreas Mohrda237f32010-12-27 21:17:26 +0100250struct snd_azf3328_codec_data {
251 unsigned long io_base; /* keep first! (avoid offset calc) */
252 unsigned int dma_base; /* helper to avoid an indirection in hotpath */
253 spinlock_t *lock; /* TODO: convert to our own per-codec lock member */
254 struct snd_pcm_substream *substream;
255 bool running;
256 enum snd_azf3328_codec_type type;
257 const char *name;
258};
259
Takashi Iwai95de7762005-11-17 15:02:42 +0100260struct snd_azf3328 {
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200261 /* often-used fields towards beginning, then grouped */
Andreas Mohr02330fba2008-05-16 12:18:29 +0200262
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200263 unsigned long ctrl_io; /* usually 0xb000, size 128 */
Andreas Mohr02330fba2008-05-16 12:18:29 +0200264 unsigned long game_io; /* usually 0xb400, size 8 */
265 unsigned long mpu_io; /* usually 0xb800, size 4 */
266 unsigned long opl3_io; /* usually 0xbc00, size 8 */
267 unsigned long mixer_io; /* usually 0xc000, size 64 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200269 spinlock_t reg_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Takashi Iwai95de7762005-11-17 15:02:42 +0100271 struct snd_timer *timer;
Andreas Mohr02330fba2008-05-16 12:18:29 +0200272
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200273 struct snd_pcm *pcm[3];
274
275 /* playback, recording and I2S out codecs */
276 struct snd_azf3328_codec_data codecs[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Andreas Mohrb5dc20c2011-02-19 00:49:32 +0100278#ifdef AZF_USE_AC97_LAYER
279 struct snd_ac97 *ac97;
280#endif
281
Takashi Iwai95de7762005-11-17 15:02:42 +0100282 struct snd_card *card;
283 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Andreas Mohr02330fba2008-05-16 12:18:29 +0200285#ifdef SUPPORT_GAMEPORT
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200286 struct gameport *gameport;
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200287 u16 axes[4];
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200288#endif
289
290 struct pci_dev *pci;
291 int irq;
Andreas Mohrca54bde2006-05-17 11:02:24 +0200292
Andreas Mohr627d3e72008-06-23 11:50:47 +0200293 /* register 0x6a is write-only, thus need to remember setting.
294 * If we need to add more registers here, then we might try to fold this
295 * into some transparent combined shadow register handling with
296 * CONFIG_PM register storage below, but that's slightly difficult. */
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200297 u16 shadow_reg_ctrl_6AH;
Andreas Mohr627d3e72008-06-23 11:50:47 +0200298
Takashi Iwaic7561cd2012-08-14 18:12:04 +0200299#ifdef CONFIG_PM_SLEEP
Andreas Mohrca54bde2006-05-17 11:02:24 +0200300 /* register value containers for power management
Andreas Mohr78df6172009-07-12 22:17:54 +0200301 * Note: not always full I/O range preserved (similar to Win driver!) */
302 u32 saved_regs_ctrl[AZF_ALIGN(AZF_IO_SIZE_CTRL_PM) / 4];
303 u32 saved_regs_game[AZF_ALIGN(AZF_IO_SIZE_GAME_PM) / 4];
304 u32 saved_regs_mpu[AZF_ALIGN(AZF_IO_SIZE_MPU_PM) / 4];
305 u32 saved_regs_opl3[AZF_ALIGN(AZF_IO_SIZE_OPL3_PM) / 4];
306 u32 saved_regs_mixer[AZF_ALIGN(AZF_IO_SIZE_MIXER_PM) / 4];
Andreas Mohrca54bde2006-05-17 11:02:24 +0200307#endif
Takashi Iwai95de7762005-11-17 15:02:42 +0100308};
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200309
Benoit Taine9baa3c32014-08-08 15:56:03 +0200310static const struct pci_device_id snd_azf3328_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 { 0x122D, 0x50DC, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, /* PCI168/3328 */
312 { 0x122D, 0x80DA, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, /* 3328 */
313 { 0, }
314};
315
316MODULE_DEVICE_TABLE(pci, snd_azf3328_ids);
317
Andreas Mohr02330fba2008-05-16 12:18:29 +0200318
319static int
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200320snd_azf3328_io_reg_setb(unsigned reg, u8 mask, bool do_set)
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200321{
Andreas Mohradf59312010-12-27 21:16:43 +0100322 /* Well, strictly spoken, the inb/outb sequence isn't atomic
323 and would need locking. However we currently don't care
324 since it potentially complicates matters. */
Andreas Mohr02330fba2008-05-16 12:18:29 +0200325 u8 prev = inb(reg), new;
326
327 new = (do_set) ? (prev|mask) : (prev & ~mask);
328 /* we need to always write the new value no matter whether it differs
329 * or not, since some register bits don't indicate their setting */
330 outb(new, reg);
331 if (new != prev)
332 return 1;
333
334 return 0;
335}
336
Andreas Mohr02330fba2008-05-16 12:18:29 +0200337static inline void
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200338snd_azf3328_codec_outb(const struct snd_azf3328_codec_data *codec,
339 unsigned reg,
340 u8 value
341)
Andreas Mohr02330fba2008-05-16 12:18:29 +0200342{
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200343 outb(value, codec->io_base + reg);
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200344}
345
346static inline u8
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200347snd_azf3328_codec_inb(const struct snd_azf3328_codec_data *codec, unsigned reg)
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200348{
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200349 return inb(codec->io_base + reg);
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200350}
351
352static inline void
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200353snd_azf3328_codec_outw(const struct snd_azf3328_codec_data *codec,
354 unsigned reg,
355 u16 value
356)
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200357{
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200358 outw(value, codec->io_base + reg);
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200359}
360
361static inline u16
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200362snd_azf3328_codec_inw(const struct snd_azf3328_codec_data *codec, unsigned reg)
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200363{
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200364 return inw(codec->io_base + reg);
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200365}
366
367static inline void
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200368snd_azf3328_codec_outl(const struct snd_azf3328_codec_data *codec,
369 unsigned reg,
370 u32 value
371)
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200372{
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200373 outl(value, codec->io_base + reg);
Andreas Mohr02330fba2008-05-16 12:18:29 +0200374}
375
Andreas Mohr689c6912010-12-27 21:17:35 +0100376static inline void
377snd_azf3328_codec_outl_multi(const struct snd_azf3328_codec_data *codec,
378 unsigned reg, const void *buffer, int count
379)
380{
381 unsigned long addr = codec->io_base + reg;
382 if (count) {
383 const u32 *buf = buffer;
384 do {
385 outl(*buf++, addr);
386 addr += 4;
387 } while (--count);
388 }
389}
390
Andreas Mohr02330fba2008-05-16 12:18:29 +0200391static inline u32
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200392snd_azf3328_codec_inl(const struct snd_azf3328_codec_data *codec, unsigned reg)
Andreas Mohr02330fba2008-05-16 12:18:29 +0200393{
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200394 return inl(codec->io_base + reg);
395}
396
397static inline void
398snd_azf3328_ctrl_outb(const struct snd_azf3328 *chip, unsigned reg, u8 value)
399{
400 outb(value, chip->ctrl_io + reg);
401}
402
403static inline u8
404snd_azf3328_ctrl_inb(const struct snd_azf3328 *chip, unsigned reg)
405{
406 return inb(chip->ctrl_io + reg);
407}
408
Takashi Iwai4a8d9d72014-02-25 14:04:46 +0100409static inline u16
410snd_azf3328_ctrl_inw(const struct snd_azf3328 *chip, unsigned reg)
411{
412 return inw(chip->ctrl_io + reg);
413}
414
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200415static inline void
416snd_azf3328_ctrl_outw(const struct snd_azf3328 *chip, unsigned reg, u16 value)
417{
418 outw(value, chip->ctrl_io + reg);
419}
420
421static inline void
422snd_azf3328_ctrl_outl(const struct snd_azf3328 *chip, unsigned reg, u32 value)
423{
424 outl(value, chip->ctrl_io + reg);
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200425}
426
427static inline void
Andreas Mohr02330fba2008-05-16 12:18:29 +0200428snd_azf3328_game_outb(const struct snd_azf3328 *chip, unsigned reg, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
Andreas Mohr02330fba2008-05-16 12:18:29 +0200430 outb(value, chip->game_io + reg);
431}
432
433static inline void
434snd_azf3328_game_outw(const struct snd_azf3328 *chip, unsigned reg, u16 value)
435{
436 outw(value, chip->game_io + reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
438
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200439static inline u8
Andreas Mohr02330fba2008-05-16 12:18:29 +0200440snd_azf3328_game_inb(const struct snd_azf3328 *chip, unsigned reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
Andreas Mohr02330fba2008-05-16 12:18:29 +0200442 return inb(chip->game_io + reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443}
444
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200445static inline u16
Andreas Mohr02330fba2008-05-16 12:18:29 +0200446snd_azf3328_game_inw(const struct snd_azf3328 *chip, unsigned reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
Andreas Mohr02330fba2008-05-16 12:18:29 +0200448 return inw(chip->game_io + reg);
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200449}
450
Andreas Mohr02330fba2008-05-16 12:18:29 +0200451static inline void
452snd_azf3328_mixer_outw(const struct snd_azf3328 *chip, unsigned reg, u16 value)
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200453{
Andreas Mohr02330fba2008-05-16 12:18:29 +0200454 outw(value, chip->mixer_io + reg);
455}
456
457static inline u16
458snd_azf3328_mixer_inw(const struct snd_azf3328 *chip, unsigned reg)
459{
460 return inw(chip->mixer_io + reg);
461}
462
463#define AZF_MUTE_BIT 0x80
464
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200465static bool
Andreas Mohrb5dc20c2011-02-19 00:49:32 +0100466snd_azf3328_mixer_mute_control(const struct snd_azf3328 *chip,
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200467 unsigned reg, bool do_mute
Andreas Mohr02330fba2008-05-16 12:18:29 +0200468)
469{
470 unsigned long portbase = chip->mixer_io + reg + 1;
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200471 bool updated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 /* the mute bit is on the *second* (i.e. right) register of a
474 * left/right channel setting */
Andreas Mohr02330fba2008-05-16 12:18:29 +0200475 updated = snd_azf3328_io_reg_setb(portbase, AZF_MUTE_BIT, do_mute);
476
477 /* indicate whether it was muted before */
478 return (do_mute) ? !updated : updated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479}
480
Andreas Mohrb5dc20c2011-02-19 00:49:32 +0100481static inline bool
482snd_azf3328_mixer_mute_control_master(const struct snd_azf3328 *chip,
483 bool do_mute
484)
485{
486 return snd_azf3328_mixer_mute_control(
487 chip,
488 IDX_MIXER_PLAY_MASTER,
489 do_mute
490 );
491}
492
493static inline bool
494snd_azf3328_mixer_mute_control_pcm(const struct snd_azf3328 *chip,
495 bool do_mute
496)
497{
498 return snd_azf3328_mixer_mute_control(
499 chip,
500 IDX_MIXER_WAVEOUT,
501 do_mute
502 );
503}
504
505static inline void
506snd_azf3328_mixer_reset(const struct snd_azf3328 *chip)
507{
508 /* reset (close) mixer:
509 * first mute master volume, then reset
510 */
511 snd_azf3328_mixer_mute_control_master(chip, 1);
512 snd_azf3328_mixer_outw(chip, IDX_MIXER_RESET, 0x0000);
513}
514
515#ifdef AZF_USE_AC97_LAYER
516
517static inline void
Takashi Iwai4a8d9d72014-02-25 14:04:46 +0100518snd_azf3328_mixer_ac97_map_unsupported(const struct snd_azf3328 *chip,
519 unsigned short reg, const char *mode)
Andreas Mohrb5dc20c2011-02-19 00:49:32 +0100520{
521 /* need to add some more or less clever emulation? */
Takashi Iwai4a8d9d72014-02-25 14:04:46 +0100522 dev_warn(chip->card->dev,
523 "missing %s emulation for AC97 register 0x%02x!\n",
Andreas Mohrb5dc20c2011-02-19 00:49:32 +0100524 mode, reg);
525}
526
527/*
528 * Need to have _special_ AC97 mixer hardware register index mapper,
529 * to compensate for the issue of a rather AC97-incompatible hardware layout.
530 */
531#define AZF_REG_MASK 0x3f
532#define AZF_AC97_REG_UNSUPPORTED 0x8000
533#define AZF_AC97_REG_REAL_IO_READ 0x4000
534#define AZF_AC97_REG_REAL_IO_WRITE 0x2000
535#define AZF_AC97_REG_REAL_IO_RW \
536 (AZF_AC97_REG_REAL_IO_READ | AZF_AC97_REG_REAL_IO_WRITE)
537#define AZF_AC97_REG_EMU_IO_READ 0x0400
538#define AZF_AC97_REG_EMU_IO_WRITE 0x0200
539#define AZF_AC97_REG_EMU_IO_RW \
540 (AZF_AC97_REG_EMU_IO_READ | AZF_AC97_REG_EMU_IO_WRITE)
541static unsigned short
542snd_azf3328_mixer_ac97_map_reg_idx(unsigned short reg)
543{
544 static const struct {
545 unsigned short azf_reg;
546 } azf_reg_mapper[] = {
547 /* Especially when taking into consideration
548 * mono/stereo-based sequence of azf vs. AC97 control series,
549 * it's quite obvious that azf simply got rid
550 * of the AC97_HEADPHONE control at its intended offset,
551 * thus shifted _all_ controls by one,
552 * and _then_ simply added it as an FMSYNTH control at the end,
553 * to make up for the offset.
554 * This means we'll have to translate indices here as
555 * needed and then do some tiny AC97 patch action
556 * (snd_ac97_rename_vol_ctl() etc.) - that's it.
557 */
558 { /* AC97_RESET */ IDX_MIXER_RESET
559 | AZF_AC97_REG_REAL_IO_WRITE
560 | AZF_AC97_REG_EMU_IO_READ },
561 { /* AC97_MASTER */ IDX_MIXER_PLAY_MASTER },
562 /* note large shift: AC97_HEADPHONE to IDX_MIXER_FMSYNTH! */
563 { /* AC97_HEADPHONE */ IDX_MIXER_FMSYNTH },
564 { /* AC97_MASTER_MONO */ IDX_MIXER_MODEMOUT },
565 { /* AC97_MASTER_TONE */ IDX_MIXER_BASSTREBLE },
566 { /* AC97_PC_BEEP */ IDX_MIXER_PCBEEP },
567 { /* AC97_PHONE */ IDX_MIXER_MODEMIN },
568 { /* AC97_MIC */ IDX_MIXER_MIC },
569 { /* AC97_LINE */ IDX_MIXER_LINEIN },
570 { /* AC97_CD */ IDX_MIXER_CDAUDIO },
571 { /* AC97_VIDEO */ IDX_MIXER_VIDEO },
572 { /* AC97_AUX */ IDX_MIXER_AUX },
573 { /* AC97_PCM */ IDX_MIXER_WAVEOUT },
574 { /* AC97_REC_SEL */ IDX_MIXER_REC_SELECT },
575 { /* AC97_REC_GAIN */ IDX_MIXER_REC_VOLUME },
576 { /* AC97_REC_GAIN_MIC */ AZF_AC97_REG_EMU_IO_RW },
577 { /* AC97_GENERAL_PURPOSE */ IDX_MIXER_ADVCTL2 },
578 { /* AC97_3D_CONTROL */ IDX_MIXER_ADVCTL1 },
579 };
580
581 unsigned short reg_azf = AZF_AC97_REG_UNSUPPORTED;
582
583 /* azf3328 supports the low-numbered and low-spec:ed range
584 of AC97 regs only */
585 if (reg <= AC97_3D_CONTROL) {
586 unsigned short reg_idx = reg / 2;
587 reg_azf = azf_reg_mapper[reg_idx].azf_reg;
588 /* a translation-only entry means it's real read/write: */
589 if (!(reg_azf & ~AZF_REG_MASK))
590 reg_azf |= AZF_AC97_REG_REAL_IO_RW;
591 } else {
592 switch (reg) {
593 case AC97_POWERDOWN:
594 reg_azf = AZF_AC97_REG_EMU_IO_RW;
595 break;
596 case AC97_EXTENDED_ID:
597 reg_azf = AZF_AC97_REG_EMU_IO_READ;
598 break;
599 case AC97_EXTENDED_STATUS:
600 /* I don't know what the h*ll AC97 layer
601 * would consult this _extended_ register for
602 * given a base-AC97-advertised card,
603 * but let's just emulate it anyway :-P
604 */
605 reg_azf = AZF_AC97_REG_EMU_IO_RW;
606 break;
607 case AC97_VENDOR_ID1:
608 case AC97_VENDOR_ID2:
609 reg_azf = AZF_AC97_REG_EMU_IO_READ;
610 break;
611 }
612 }
613 return reg_azf;
614}
615
616static const unsigned short
617azf_emulated_ac97_caps =
618 AC97_BC_DEDICATED_MIC |
619 AC97_BC_BASS_TREBLE |
620 /* Headphone is an FM Synth control here */
621 AC97_BC_HEADPHONE |
622 /* no AC97_BC_LOUDNESS! */
623 /* mask 0x7c00 is
624 vendor-specific 3D enhancement
625 vendor indicator.
626 Since there actually _is_ an
627 entry for Aztech Labs
628 (13), make damn sure
629 to indicate it. */
630 (13 << 10);
631
632static const unsigned short
633azf_emulated_ac97_powerdown =
634 /* pretend everything to be active */
635 AC97_PD_ADC_STATUS |
636 AC97_PD_DAC_STATUS |
637 AC97_PD_MIXER_STATUS |
638 AC97_PD_VREF_STATUS;
639
640/*
641 * Emulated, _inofficial_ vendor ID
642 * (there might be some devices such as the MR 2800-W
643 * which could reveal the real Aztech AC97 ID).
644 * We choose to use "AZT" prefix, and then use 1 to indicate PCI168
645 * (better don't use 0x68 since there's a PCI368 as well).
646 */
647static const unsigned int
648azf_emulated_ac97_vendor_id = 0x415a5401;
649
650static unsigned short
651snd_azf3328_mixer_ac97_read(struct snd_ac97 *ac97, unsigned short reg_ac97)
652{
653 const struct snd_azf3328 *chip = ac97->private_data;
654 unsigned short reg_azf = snd_azf3328_mixer_ac97_map_reg_idx(reg_ac97);
655 unsigned short reg_val = 0;
Peter Senna Tschudine0f17c72013-09-22 20:44:12 +0200656 bool unsupported = false;
Andreas Mohrb5dc20c2011-02-19 00:49:32 +0100657
Takashi Iwai4a8d9d72014-02-25 14:04:46 +0100658 dev_dbg(chip->card->dev, "snd_azf3328_mixer_ac97_read reg_ac97 %u\n",
659 reg_ac97);
Andreas Mohrb5dc20c2011-02-19 00:49:32 +0100660 if (reg_azf & AZF_AC97_REG_UNSUPPORTED)
Peter Senna Tschudine0f17c72013-09-22 20:44:12 +0200661 unsupported = true;
Andreas Mohrb5dc20c2011-02-19 00:49:32 +0100662 else {
663 if (reg_azf & AZF_AC97_REG_REAL_IO_READ)
664 reg_val = snd_azf3328_mixer_inw(chip,
665 reg_azf & AZF_REG_MASK);
666 else {
667 /*
668 * Proceed with dummy I/O read,
669 * to ensure compatible timing where this may matter.
670 * (ALSA AC97 layer usually doesn't call I/O functions
671 * due to intelligent I/O caching anyway)
672 * Choose a mixer register that's thoroughly unrelated
673 * to common audio (try to minimize distortion).
674 */
675 snd_azf3328_mixer_inw(chip, IDX_MIXER_SOMETHING30H);
676 }
677
678 if (reg_azf & AZF_AC97_REG_EMU_IO_READ) {
679 switch (reg_ac97) {
680 case AC97_RESET:
681 reg_val |= azf_emulated_ac97_caps;
682 break;
683 case AC97_POWERDOWN:
684 reg_val |= azf_emulated_ac97_powerdown;
685 break;
686 case AC97_EXTENDED_ID:
687 case AC97_EXTENDED_STATUS:
688 /* AFAICS we simply can't support anything: */
689 reg_val |= 0;
690 break;
691 case AC97_VENDOR_ID1:
692 reg_val = azf_emulated_ac97_vendor_id >> 16;
693 break;
694 case AC97_VENDOR_ID2:
695 reg_val = azf_emulated_ac97_vendor_id & 0xffff;
696 break;
697 default:
Peter Senna Tschudine0f17c72013-09-22 20:44:12 +0200698 unsupported = true;
Andreas Mohrb5dc20c2011-02-19 00:49:32 +0100699 break;
700 }
701 }
702 }
703 if (unsupported)
Takashi Iwai4a8d9d72014-02-25 14:04:46 +0100704 snd_azf3328_mixer_ac97_map_unsupported(chip, reg_ac97, "read");
Andreas Mohrb5dc20c2011-02-19 00:49:32 +0100705
706 return reg_val;
707}
708
709static void
710snd_azf3328_mixer_ac97_write(struct snd_ac97 *ac97,
711 unsigned short reg_ac97, unsigned short val)
712{
713 const struct snd_azf3328 *chip = ac97->private_data;
714 unsigned short reg_azf = snd_azf3328_mixer_ac97_map_reg_idx(reg_ac97);
Peter Senna Tschudine0f17c72013-09-22 20:44:12 +0200715 bool unsupported = false;
Andreas Mohrb5dc20c2011-02-19 00:49:32 +0100716
Takashi Iwai4a8d9d72014-02-25 14:04:46 +0100717 dev_dbg(chip->card->dev,
Andreas Mohrb5dc20c2011-02-19 00:49:32 +0100718 "snd_azf3328_mixer_ac97_write reg_ac97 %u val %u\n",
Takashi Iwai4a8d9d72014-02-25 14:04:46 +0100719 reg_ac97, val);
Andreas Mohrb5dc20c2011-02-19 00:49:32 +0100720 if (reg_azf & AZF_AC97_REG_UNSUPPORTED)
Peter Senna Tschudine0f17c72013-09-22 20:44:12 +0200721 unsupported = true;
Andreas Mohrb5dc20c2011-02-19 00:49:32 +0100722 else {
723 if (reg_azf & AZF_AC97_REG_REAL_IO_WRITE)
724 snd_azf3328_mixer_outw(
725 chip,
726 reg_azf & AZF_REG_MASK,
727 val
728 );
729 else
730 if (reg_azf & AZF_AC97_REG_EMU_IO_WRITE) {
731 switch (reg_ac97) {
732 case AC97_REC_GAIN_MIC:
733 case AC97_POWERDOWN:
734 case AC97_EXTENDED_STATUS:
735 /*
736 * Silently swallow these writes.
737 * Since for most registers our card doesn't
738 * actually support a comparable feature,
739 * this is exactly what we should do here.
740 * The AC97 layer's I/O caching probably
741 * automatically takes care of all the rest...
742 * (remembers written values etc.)
743 */
744 break;
745 default:
Peter Senna Tschudine0f17c72013-09-22 20:44:12 +0200746 unsupported = true;
Andreas Mohrb5dc20c2011-02-19 00:49:32 +0100747 break;
748 }
749 }
750 }
751 if (unsupported)
Takashi Iwai4a8d9d72014-02-25 14:04:46 +0100752 snd_azf3328_mixer_ac97_map_unsupported(chip, reg_ac97, "write");
Andreas Mohrb5dc20c2011-02-19 00:49:32 +0100753}
754
Bill Pembertone23e7a12012-12-06 12:35:10 -0500755static int
Andreas Mohrb5dc20c2011-02-19 00:49:32 +0100756snd_azf3328_mixer_new(struct snd_azf3328 *chip)
757{
758 struct snd_ac97_bus *bus;
759 struct snd_ac97_template ac97;
760 static struct snd_ac97_bus_ops ops = {
761 .write = snd_azf3328_mixer_ac97_write,
762 .read = snd_azf3328_mixer_ac97_read,
763 };
764 int rc;
765
766 memset(&ac97, 0, sizeof(ac97));
767 ac97.scaps = AC97_SCAP_SKIP_MODEM
768 | AC97_SCAP_AUDIO /* we support audio! */
769 | AC97_SCAP_NO_SPDIF;
770 ac97.private_data = chip;
771 ac97.pci = chip->pci;
772
773 /*
774 * ALSA's AC97 layer has terrible init crackling issues,
775 * unfortunately, and since it makes use of AC97_RESET,
776 * there's no use trying to mute Master Playback proactively.
777 */
778
779 rc = snd_ac97_bus(chip->card, 0, &ops, NULL, &bus);
780 if (!rc)
781 rc = snd_ac97_mixer(bus, &ac97, &chip->ac97);
782 /*
783 * Make sure to complain loudly in case of AC97 init failure,
784 * since failure may happen quite often,
785 * due to this card being a very quirky AC97 "lookalike".
786 */
787 if (rc)
Takashi Iwai4a8d9d72014-02-25 14:04:46 +0100788 dev_err(chip->card->dev, "AC97 init failed, err %d!\n", rc);
Andreas Mohrb5dc20c2011-02-19 00:49:32 +0100789
790 /* If we return an error here, then snd_card_free() should
791 * free up any ac97 codecs that got created, as well as the bus.
792 */
793 return rc;
794}
795#else /* AZF_USE_AC97_LAYER */
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200796static void
Andreas Mohr02330fba2008-05-16 12:18:29 +0200797snd_azf3328_mixer_write_volume_gradually(const struct snd_azf3328 *chip,
798 unsigned reg,
799 unsigned char dst_vol_left,
800 unsigned char dst_vol_right,
801 int chan_sel, int delay
802)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803{
Andreas Mohr02330fba2008-05-16 12:18:29 +0200804 unsigned long portbase = chip->mixer_io + reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 unsigned char curr_vol_left = 0, curr_vol_right = 0;
Andreas Mohr02330fba2008-05-16 12:18:29 +0200806 int left_change = 0, right_change = 0;
807
Andreas Mohr02330fba2008-05-16 12:18:29 +0200808 if (chan_sel & SET_CHAN_LEFT) {
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200809 curr_vol_left = inb(portbase + 1);
Andreas Mohr02330fba2008-05-16 12:18:29 +0200810
811 /* take care of muting flag contained in left channel */
812 if (curr_vol_left & AZF_MUTE_BIT)
813 dst_vol_left |= AZF_MUTE_BIT;
814 else
815 dst_vol_left &= ~AZF_MUTE_BIT;
816
817 left_change = (curr_vol_left > dst_vol_left) ? -1 : 1;
818 }
819
820 if (chan_sel & SET_CHAN_RIGHT) {
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200821 curr_vol_right = inb(portbase + 0);
Andreas Mohr02330fba2008-05-16 12:18:29 +0200822
823 right_change = (curr_vol_right > dst_vol_right) ? -1 : 1;
824 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Andreas Mohre2f87262006-05-17 11:04:19 +0200826 do {
Andreas Mohr02330fba2008-05-16 12:18:29 +0200827 if (left_change) {
828 if (curr_vol_left != dst_vol_left) {
829 curr_vol_left += left_change;
830 outb(curr_vol_left, portbase + 1);
831 } else
832 left_change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 }
Andreas Mohr02330fba2008-05-16 12:18:29 +0200834 if (right_change) {
835 if (curr_vol_right != dst_vol_right) {
836 curr_vol_right += right_change;
837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 /* during volume change, the right channel is crackling
839 * somewhat more than the left channel, unfortunately.
840 * This seems to be a hardware issue. */
Andreas Mohr02330fba2008-05-16 12:18:29 +0200841 outb(curr_vol_right, portbase + 0);
842 } else
843 right_change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 }
845 if (delay)
846 mdelay(delay);
Andreas Mohr02330fba2008-05-16 12:18:29 +0200847 } while ((left_change) || (right_change));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848}
849
850/*
851 * general mixer element
852 */
Takashi Iwai95de7762005-11-17 15:02:42 +0100853struct azf3328_mixer_reg {
Andreas Mohr02330fba2008-05-16 12:18:29 +0200854 unsigned reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 unsigned int lchan_shift, rchan_shift;
856 unsigned int mask;
857 unsigned int invert: 1;
858 unsigned int stereo: 1;
859 unsigned int enum_c: 4;
Takashi Iwai95de7762005-11-17 15:02:42 +0100860};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
862#define COMPOSE_MIXER_REG(reg,lchan_shift,rchan_shift,mask,invert,stereo,enum_c) \
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200863 ((reg) | (lchan_shift << 8) | (rchan_shift << 12) | \
864 (mask << 16) | \
865 (invert << 24) | \
866 (stereo << 25) | \
867 (enum_c << 26))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Takashi Iwai95de7762005-11-17 15:02:42 +0100869static void snd_azf3328_mixer_reg_decode(struct azf3328_mixer_reg *r, unsigned long val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
871 r->reg = val & 0xff;
872 r->lchan_shift = (val >> 8) & 0x0f;
873 r->rchan_shift = (val >> 12) & 0x0f;
874 r->mask = (val >> 16) & 0xff;
875 r->invert = (val >> 24) & 1;
876 r->stereo = (val >> 25) & 1;
877 r->enum_c = (val >> 26) & 0x0f;
878}
879
880/*
881 * mixer switches/volumes
882 */
883
884#define AZF3328_MIXER_SWITCH(xname, reg, shift, invert) \
885{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
886 .info = snd_azf3328_info_mixer, \
887 .get = snd_azf3328_get_mixer, .put = snd_azf3328_put_mixer, \
888 .private_value = COMPOSE_MIXER_REG(reg, shift, 0, 0x1, invert, 0, 0), \
889}
890
891#define AZF3328_MIXER_VOL_STEREO(xname, reg, mask, invert) \
892{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
893 .info = snd_azf3328_info_mixer, \
894 .get = snd_azf3328_get_mixer, .put = snd_azf3328_put_mixer, \
895 .private_value = COMPOSE_MIXER_REG(reg, 8, 0, mask, invert, 1, 0), \
896}
897
898#define AZF3328_MIXER_VOL_MONO(xname, reg, mask, is_right_chan) \
899{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
900 .info = snd_azf3328_info_mixer, \
901 .get = snd_azf3328_get_mixer, .put = snd_azf3328_put_mixer, \
902 .private_value = COMPOSE_MIXER_REG(reg, is_right_chan ? 0 : 8, 0, mask, 1, 0, 0), \
903}
904
905#define AZF3328_MIXER_VOL_SPECIAL(xname, reg, mask, shift, invert) \
906{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
907 .info = snd_azf3328_info_mixer, \
908 .get = snd_azf3328_get_mixer, .put = snd_azf3328_put_mixer, \
909 .private_value = COMPOSE_MIXER_REG(reg, shift, 0, mask, invert, 0, 0), \
910}
911
912#define AZF3328_MIXER_ENUM(xname, reg, enum_c, shift) \
913{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
914 .info = snd_azf3328_info_mixer_enum, \
915 .get = snd_azf3328_get_mixer_enum, .put = snd_azf3328_put_mixer_enum, \
916 .private_value = COMPOSE_MIXER_REG(reg, shift, 0, 0, 0, 0, enum_c), \
917}
918
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200919static int
Takashi Iwai95de7762005-11-17 15:02:42 +0100920snd_azf3328_info_mixer(struct snd_kcontrol *kcontrol,
921 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
Takashi Iwai95de7762005-11-17 15:02:42 +0100923 struct azf3328_mixer_reg reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 snd_azf3328_mixer_reg_decode(&reg, kcontrol->private_value);
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200926 uinfo->type = reg.mask == 1 ?
927 SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 uinfo->count = reg.stereo + 1;
929 uinfo->value.integer.min = 0;
930 uinfo->value.integer.max = reg.mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 return 0;
932}
933
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200934static int
Takashi Iwai95de7762005-11-17 15:02:42 +0100935snd_azf3328_get_mixer(struct snd_kcontrol *kcontrol,
936 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
Takashi Iwai95de7762005-11-17 15:02:42 +0100938 struct snd_azf3328 *chip = snd_kcontrol_chip(kcontrol);
939 struct azf3328_mixer_reg reg;
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200940 u16 oreg, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 snd_azf3328_mixer_reg_decode(&reg, kcontrol->private_value);
943
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200944 oreg = snd_azf3328_mixer_inw(chip, reg.reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 val = (oreg >> reg.lchan_shift) & reg.mask;
946 if (reg.invert)
947 val = reg.mask - val;
948 ucontrol->value.integer.value[0] = val;
949 if (reg.stereo) {
950 val = (oreg >> reg.rchan_shift) & reg.mask;
951 if (reg.invert)
952 val = reg.mask - val;
953 ucontrol->value.integer.value[1] = val;
954 }
Takashi Iwai4a8d9d72014-02-25 14:04:46 +0100955 dev_dbg(chip->card->dev,
956 "get: %02x is %04x -> vol %02lx|%02lx (shift %02d|%02d, mask %02x, inv. %d, stereo %d)\n",
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200957 reg.reg, oreg,
958 ucontrol->value.integer.value[0], ucontrol->value.integer.value[1],
959 reg.lchan_shift, reg.rchan_shift, reg.mask, reg.invert, reg.stereo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 return 0;
961}
962
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200963static int
Takashi Iwai95de7762005-11-17 15:02:42 +0100964snd_azf3328_put_mixer(struct snd_kcontrol *kcontrol,
965 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
Takashi Iwai95de7762005-11-17 15:02:42 +0100967 struct snd_azf3328 *chip = snd_kcontrol_chip(kcontrol);
968 struct azf3328_mixer_reg reg;
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200969 u16 oreg, nreg, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 snd_azf3328_mixer_reg_decode(&reg, kcontrol->private_value);
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200972 oreg = snd_azf3328_mixer_inw(chip, reg.reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 val = ucontrol->value.integer.value[0] & reg.mask;
974 if (reg.invert)
975 val = reg.mask - val;
976 nreg = oreg & ~(reg.mask << reg.lchan_shift);
977 nreg |= (val << reg.lchan_shift);
978 if (reg.stereo) {
979 val = ucontrol->value.integer.value[1] & reg.mask;
980 if (reg.invert)
981 val = reg.mask - val;
982 nreg &= ~(reg.mask << reg.rchan_shift);
983 nreg |= (val << reg.rchan_shift);
984 }
985 if (reg.mask >= 0x07) /* it's a volume control, so better take care */
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200986 snd_azf3328_mixer_write_volume_gradually(
987 chip, reg.reg, nreg >> 8, nreg & 0xff,
988 /* just set both channels, doesn't matter */
989 SET_CHAN_LEFT|SET_CHAN_RIGHT,
990 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 else
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200992 snd_azf3328_mixer_outw(chip, reg.reg, nreg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Takashi Iwai4a8d9d72014-02-25 14:04:46 +0100994 dev_dbg(chip->card->dev,
995 "put: %02x to %02lx|%02lx, oreg %04x; shift %02d|%02d -> nreg %04x; after: %04x\n",
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200996 reg.reg, ucontrol->value.integer.value[0], ucontrol->value.integer.value[1],
997 oreg, reg.lchan_shift, reg.rchan_shift,
998 nreg, snd_azf3328_mixer_inw(chip, reg.reg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 return (nreg != oreg);
1000}
1001
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001002static int
Takashi Iwai95de7762005-11-17 15:02:42 +01001003snd_azf3328_info_mixer_enum(struct snd_kcontrol *kcontrol,
1004 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005{
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001006 static const char * const texts1[] = {
Andreas Mohr13769e32006-05-17 11:03:16 +02001007 "Mic1", "Mic2"
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001008 };
1009 static const char * const texts2[] = {
Andreas Mohr13769e32006-05-17 11:03:16 +02001010 "Mix", "Mic"
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001011 };
1012 static const char * const texts3[] = {
Andreas Mohr02330fba2008-05-16 12:18:29 +02001013 "Mic", "CD", "Video", "Aux",
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001014 "Line", "Mix", "Mix Mono", "Phone"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 };
Andreas Mohr13769e32006-05-17 11:03:16 +02001016 static const char * const texts4[] = {
1017 "pre 3D", "post 3D"
1018 };
Takashi Iwai95de7762005-11-17 15:02:42 +01001019 struct azf3328_mixer_reg reg;
Andreas Mohr627d3e72008-06-23 11:50:47 +02001020 const char * const *p = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
1022 snd_azf3328_mixer_reg_decode(&reg, kcontrol->private_value);
Andreas Mohre2f87262006-05-17 11:04:19 +02001023 if (reg.reg == IDX_MIXER_ADVCTL2) {
Andreas Mohr13769e32006-05-17 11:03:16 +02001024 switch(reg.lchan_shift) {
1025 case 8: /* modem out sel */
Andreas Mohr627d3e72008-06-23 11:50:47 +02001026 p = texts1;
Andreas Mohr13769e32006-05-17 11:03:16 +02001027 break;
1028 case 9: /* mono sel source */
Andreas Mohr627d3e72008-06-23 11:50:47 +02001029 p = texts2;
Andreas Mohr13769e32006-05-17 11:03:16 +02001030 break;
1031 case 15: /* PCM Out Path */
Andreas Mohr627d3e72008-06-23 11:50:47 +02001032 p = texts4;
Andreas Mohr13769e32006-05-17 11:03:16 +02001033 break;
1034 }
Takashi Iwai9b311a02014-10-20 18:16:13 +02001035 } else if (reg.reg == IDX_MIXER_REC_SELECT)
Andreas Mohr627d3e72008-06-23 11:50:47 +02001036 p = texts3;
Andreas Mohr02330fba2008-05-16 12:18:29 +02001037
Takashi Iwai9b311a02014-10-20 18:16:13 +02001038 return snd_ctl_enum_info(uinfo,
1039 (reg.reg == IDX_MIXER_REC_SELECT) ? 2 : 1,
1040 reg.enum_c, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041}
1042
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001043static int
Takashi Iwai95de7762005-11-17 15:02:42 +01001044snd_azf3328_get_mixer_enum(struct snd_kcontrol *kcontrol,
1045 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
Takashi Iwai95de7762005-11-17 15:02:42 +01001047 struct snd_azf3328 *chip = snd_kcontrol_chip(kcontrol);
1048 struct azf3328_mixer_reg reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 unsigned short val;
Andreas Mohr02330fba2008-05-16 12:18:29 +02001050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 snd_azf3328_mixer_reg_decode(&reg, kcontrol->private_value);
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001052 val = snd_azf3328_mixer_inw(chip, reg.reg);
Andreas Mohre2f87262006-05-17 11:04:19 +02001053 if (reg.reg == IDX_MIXER_REC_SELECT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 ucontrol->value.enumerated.item[0] = (val >> 8) & (reg.enum_c - 1);
1055 ucontrol->value.enumerated.item[1] = (val >> 0) & (reg.enum_c - 1);
Andreas Mohre2f87262006-05-17 11:04:19 +02001056 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 ucontrol->value.enumerated.item[0] = (val >> reg.lchan_shift) & (reg.enum_c - 1);
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001058
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001059 dev_dbg(chip->card->dev,
1060 "get_enum: %02x is %04x -> %d|%d (shift %02d, enum_c %d)\n",
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001061 reg.reg, val, ucontrol->value.enumerated.item[0], ucontrol->value.enumerated.item[1],
1062 reg.lchan_shift, reg.enum_c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 return 0;
1064}
1065
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001066static int
Takashi Iwai95de7762005-11-17 15:02:42 +01001067snd_azf3328_put_mixer_enum(struct snd_kcontrol *kcontrol,
1068 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069{
Takashi Iwai95de7762005-11-17 15:02:42 +01001070 struct snd_azf3328 *chip = snd_kcontrol_chip(kcontrol);
1071 struct azf3328_mixer_reg reg;
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001072 u16 oreg, nreg, val;
Andreas Mohr02330fba2008-05-16 12:18:29 +02001073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 snd_azf3328_mixer_reg_decode(&reg, kcontrol->private_value);
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001075 oreg = snd_azf3328_mixer_inw(chip, reg.reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 val = oreg;
Andreas Mohre2f87262006-05-17 11:04:19 +02001077 if (reg.reg == IDX_MIXER_REC_SELECT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 if (ucontrol->value.enumerated.item[0] > reg.enum_c - 1U ||
1079 ucontrol->value.enumerated.item[1] > reg.enum_c - 1U)
1080 return -EINVAL;
1081 val = (ucontrol->value.enumerated.item[0] << 8) |
1082 (ucontrol->value.enumerated.item[1] << 0);
Andreas Mohre2f87262006-05-17 11:04:19 +02001083 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 if (ucontrol->value.enumerated.item[0] > reg.enum_c - 1U)
1085 return -EINVAL;
1086 val &= ~((reg.enum_c - 1) << reg.lchan_shift);
1087 val |= (ucontrol->value.enumerated.item[0] << reg.lchan_shift);
1088 }
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001089 snd_azf3328_mixer_outw(chip, reg.reg, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 nreg = val;
1091
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001092 dev_dbg(chip->card->dev,
1093 "put_enum: %02x to %04x, oreg %04x\n", reg.reg, val, oreg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 return (nreg != oreg);
1095}
1096
Bill Pembertone23e7a12012-12-06 12:35:10 -05001097static struct snd_kcontrol_new snd_azf3328_mixer_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 AZF3328_MIXER_SWITCH("Master Playback Switch", IDX_MIXER_PLAY_MASTER, 15, 1),
1099 AZF3328_MIXER_VOL_STEREO("Master Playback Volume", IDX_MIXER_PLAY_MASTER, 0x1f, 1),
Andreas Mohr627d3e72008-06-23 11:50:47 +02001100 AZF3328_MIXER_SWITCH("PCM Playback Switch", IDX_MIXER_WAVEOUT, 15, 1),
1101 AZF3328_MIXER_VOL_STEREO("PCM Playback Volume",
1102 IDX_MIXER_WAVEOUT, 0x1f, 1),
1103 AZF3328_MIXER_SWITCH("PCM 3D Bypass Playback Switch",
1104 IDX_MIXER_ADVCTL2, 7, 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 AZF3328_MIXER_SWITCH("FM Playback Switch", IDX_MIXER_FMSYNTH, 15, 1),
1106 AZF3328_MIXER_VOL_STEREO("FM Playback Volume", IDX_MIXER_FMSYNTH, 0x1f, 1),
1107 AZF3328_MIXER_SWITCH("CD Playback Switch", IDX_MIXER_CDAUDIO, 15, 1),
1108 AZF3328_MIXER_VOL_STEREO("CD Playback Volume", IDX_MIXER_CDAUDIO, 0x1f, 1),
1109 AZF3328_MIXER_SWITCH("Capture Switch", IDX_MIXER_REC_VOLUME, 15, 1),
1110 AZF3328_MIXER_VOL_STEREO("Capture Volume", IDX_MIXER_REC_VOLUME, 0x0f, 0),
1111 AZF3328_MIXER_ENUM("Capture Source", IDX_MIXER_REC_SELECT, 8, 0),
1112 AZF3328_MIXER_SWITCH("Mic Playback Switch", IDX_MIXER_MIC, 15, 1),
1113 AZF3328_MIXER_VOL_MONO("Mic Playback Volume", IDX_MIXER_MIC, 0x1f, 1),
1114 AZF3328_MIXER_SWITCH("Mic Boost (+20dB)", IDX_MIXER_MIC, 6, 0),
1115 AZF3328_MIXER_SWITCH("Line Playback Switch", IDX_MIXER_LINEIN, 15, 1),
1116 AZF3328_MIXER_VOL_STEREO("Line Playback Volume", IDX_MIXER_LINEIN, 0x1f, 1),
Jaroslav Kyselad355c82a2009-11-03 15:47:25 +01001117 AZF3328_MIXER_SWITCH("Beep Playback Switch", IDX_MIXER_PCBEEP, 15, 1),
1118 AZF3328_MIXER_VOL_SPECIAL("Beep Playback Volume", IDX_MIXER_PCBEEP, 0x0f, 1, 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 AZF3328_MIXER_SWITCH("Video Playback Switch", IDX_MIXER_VIDEO, 15, 1),
1120 AZF3328_MIXER_VOL_STEREO("Video Playback Volume", IDX_MIXER_VIDEO, 0x1f, 1),
1121 AZF3328_MIXER_SWITCH("Aux Playback Switch", IDX_MIXER_AUX, 15, 1),
1122 AZF3328_MIXER_VOL_STEREO("Aux Playback Volume", IDX_MIXER_AUX, 0x1f, 1),
1123 AZF3328_MIXER_SWITCH("Modem Playback Switch", IDX_MIXER_MODEMOUT, 15, 1),
1124 AZF3328_MIXER_VOL_MONO("Modem Playback Volume", IDX_MIXER_MODEMOUT, 0x1f, 1),
1125 AZF3328_MIXER_SWITCH("Modem Capture Switch", IDX_MIXER_MODEMIN, 15, 1),
1126 AZF3328_MIXER_VOL_MONO("Modem Capture Volume", IDX_MIXER_MODEMIN, 0x1f, 1),
Andreas Mohr13769e32006-05-17 11:03:16 +02001127 AZF3328_MIXER_ENUM("Mic Select", IDX_MIXER_ADVCTL2, 2, 8),
1128 AZF3328_MIXER_ENUM("Mono Output Select", IDX_MIXER_ADVCTL2, 2, 9),
Andreas Mohre24a1212007-03-26 12:49:45 +02001129 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 -07001130 AZF3328_MIXER_VOL_SPECIAL("Tone Control - Treble", IDX_MIXER_BASSTREBLE, 0x07, 1, 0),
1131 AZF3328_MIXER_VOL_SPECIAL("Tone Control - Bass", IDX_MIXER_BASSTREBLE, 0x07, 9, 0),
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001132 AZF3328_MIXER_SWITCH("3D Control - Switch", IDX_MIXER_ADVCTL2, 13, 0),
Andreas Mohr13769e32006-05-17 11:03:16 +02001133 AZF3328_MIXER_VOL_SPECIAL("3D Control - Width", IDX_MIXER_ADVCTL1, 0x07, 1, 0), /* "3D Width" */
1134 AZF3328_MIXER_VOL_SPECIAL("3D Control - Depth", IDX_MIXER_ADVCTL1, 0x03, 8, 0), /* "Hifi 3D" */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135#if MIXER_TESTING
1136 AZF3328_MIXER_SWITCH("0", IDX_MIXER_ADVCTL2, 0, 0),
1137 AZF3328_MIXER_SWITCH("1", IDX_MIXER_ADVCTL2, 1, 0),
1138 AZF3328_MIXER_SWITCH("2", IDX_MIXER_ADVCTL2, 2, 0),
1139 AZF3328_MIXER_SWITCH("3", IDX_MIXER_ADVCTL2, 3, 0),
1140 AZF3328_MIXER_SWITCH("4", IDX_MIXER_ADVCTL2, 4, 0),
1141 AZF3328_MIXER_SWITCH("5", IDX_MIXER_ADVCTL2, 5, 0),
1142 AZF3328_MIXER_SWITCH("6", IDX_MIXER_ADVCTL2, 6, 0),
1143 AZF3328_MIXER_SWITCH("7", IDX_MIXER_ADVCTL2, 7, 0),
1144 AZF3328_MIXER_SWITCH("8", IDX_MIXER_ADVCTL2, 8, 0),
1145 AZF3328_MIXER_SWITCH("9", IDX_MIXER_ADVCTL2, 9, 0),
1146 AZF3328_MIXER_SWITCH("10", IDX_MIXER_ADVCTL2, 10, 0),
1147 AZF3328_MIXER_SWITCH("11", IDX_MIXER_ADVCTL2, 11, 0),
1148 AZF3328_MIXER_SWITCH("12", IDX_MIXER_ADVCTL2, 12, 0),
1149 AZF3328_MIXER_SWITCH("13", IDX_MIXER_ADVCTL2, 13, 0),
1150 AZF3328_MIXER_SWITCH("14", IDX_MIXER_ADVCTL2, 14, 0),
1151 AZF3328_MIXER_SWITCH("15", IDX_MIXER_ADVCTL2, 15, 0),
1152#endif
1153};
1154
Bill Pembertone23e7a12012-12-06 12:35:10 -05001155static u16 snd_azf3328_init_values[][2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 { IDX_MIXER_PLAY_MASTER, MIXER_MUTE_MASK|0x1f1f },
1157 { IDX_MIXER_MODEMOUT, MIXER_MUTE_MASK|0x1f1f },
1158 { IDX_MIXER_BASSTREBLE, 0x0000 },
1159 { IDX_MIXER_PCBEEP, MIXER_MUTE_MASK|0x1f1f },
1160 { IDX_MIXER_MODEMIN, MIXER_MUTE_MASK|0x1f1f },
1161 { IDX_MIXER_MIC, MIXER_MUTE_MASK|0x001f },
1162 { IDX_MIXER_LINEIN, MIXER_MUTE_MASK|0x1f1f },
1163 { IDX_MIXER_CDAUDIO, MIXER_MUTE_MASK|0x1f1f },
1164 { IDX_MIXER_VIDEO, MIXER_MUTE_MASK|0x1f1f },
1165 { IDX_MIXER_AUX, MIXER_MUTE_MASK|0x1f1f },
1166 { IDX_MIXER_WAVEOUT, MIXER_MUTE_MASK|0x1f1f },
1167 { IDX_MIXER_FMSYNTH, MIXER_MUTE_MASK|0x1f1f },
1168 { IDX_MIXER_REC_VOLUME, MIXER_MUTE_MASK|0x0707 },
1169};
1170
Bill Pembertone23e7a12012-12-06 12:35:10 -05001171static int
Takashi Iwai95de7762005-11-17 15:02:42 +01001172snd_azf3328_mixer_new(struct snd_azf3328 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173{
Takashi Iwai95de7762005-11-17 15:02:42 +01001174 struct snd_card *card;
1175 const struct snd_kcontrol_new *sw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 unsigned int idx;
1177 int err;
1178
Takashi Iwaida3cec32008-08-08 17:12:14 +02001179 if (snd_BUG_ON(!chip || !chip->card))
1180 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
1182 card = chip->card;
1183
1184 /* mixer reset */
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001185 snd_azf3328_mixer_outw(chip, IDX_MIXER_RESET, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
1187 /* mute and zero volume channels */
Andreas Mohr02330fba2008-05-16 12:18:29 +02001188 for (idx = 0; idx < ARRAY_SIZE(snd_azf3328_init_values); ++idx) {
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001189 snd_azf3328_mixer_outw(chip,
1190 snd_azf3328_init_values[idx][0],
1191 snd_azf3328_init_values[idx][1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 }
Andreas Mohr02330fba2008-05-16 12:18:29 +02001193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 /* add mixer controls */
1195 sw = snd_azf3328_mixer_controls;
Andreas Mohr02330fba2008-05-16 12:18:29 +02001196 for (idx = 0; idx < ARRAY_SIZE(snd_azf3328_mixer_controls);
1197 ++idx, ++sw) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(sw, chip))) < 0)
1199 return err;
1200 }
1201 snd_component_add(card, "AZF3328 mixer");
1202 strcpy(card->mixername, "AZF3328 mixer");
1203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 return 0;
1205}
Andreas Mohrb5dc20c2011-02-19 00:49:32 +01001206#endif /* AZF_USE_AC97_LAYER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001208static int
Takashi Iwai95de7762005-11-17 15:02:42 +01001209snd_azf3328_hw_params(struct snd_pcm_substream *substream,
1210 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211{
Takashi Iwai4162cd32014-02-25 12:59:05 +01001212 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213}
1214
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001215static int
Takashi Iwai95de7762005-11-17 15:02:42 +01001216snd_azf3328_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 snd_pcm_lib_free_pages(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 return 0;
1220}
1221
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001222static void
Andreas Mohrda237f32010-12-27 21:17:26 +01001223snd_azf3328_codec_setfmt(struct snd_azf3328_codec_data *codec,
Andreas Mohr627d3e72008-06-23 11:50:47 +02001224 enum azf_freq_t bitrate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 unsigned int format_width,
1226 unsigned int channels
1227)
1228{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 unsigned long flags;
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001230 u16 val = 0xff00;
Andreas Mohr8d9a1142010-12-27 21:16:49 +01001231 u8 freq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 switch (bitrate) {
Andreas Mohrc9ba3742011-01-25 06:46:31 +01001234 case AZF_FREQ_4000: freq = SOUNDFORMAT_FREQ_SUSPECTED_4000; break;
1235 case AZF_FREQ_4800: freq = SOUNDFORMAT_FREQ_SUSPECTED_4800; break;
1236 case AZF_FREQ_5512:
1237 /* the AZF3328 names it "5510" for some strange reason */
1238 freq = SOUNDFORMAT_FREQ_5510; break;
1239 case AZF_FREQ_6620: freq = SOUNDFORMAT_FREQ_6620; break;
1240 case AZF_FREQ_8000: freq = SOUNDFORMAT_FREQ_8000; break;
1241 case AZF_FREQ_9600: freq = SOUNDFORMAT_FREQ_9600; break;
1242 case AZF_FREQ_11025: freq = SOUNDFORMAT_FREQ_11025; break;
1243 case AZF_FREQ_13240: freq = SOUNDFORMAT_FREQ_SUSPECTED_13240; break;
1244 case AZF_FREQ_16000: freq = SOUNDFORMAT_FREQ_16000; break;
1245 case AZF_FREQ_22050: freq = SOUNDFORMAT_FREQ_22050; break;
1246 case AZF_FREQ_32000: freq = SOUNDFORMAT_FREQ_32000; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 default:
Takashi Iwai99b359b2005-10-20 18:26:44 +02001248 snd_printk(KERN_WARNING "unknown bitrate %d, assuming 44.1kHz!\n", bitrate);
Andreas Mohr02330fba2008-05-16 12:18:29 +02001249 /* fall-through */
Andreas Mohrc9ba3742011-01-25 06:46:31 +01001250 case AZF_FREQ_44100: freq = SOUNDFORMAT_FREQ_44100; break;
1251 case AZF_FREQ_48000: freq = SOUNDFORMAT_FREQ_48000; break;
1252 case AZF_FREQ_66200: freq = SOUNDFORMAT_FREQ_SUSPECTED_66200; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 }
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001254 /* val = 0xff07; 3m27.993s (65301Hz; -> 64000Hz???) hmm, 66120, 65967, 66123 */
1255 /* val = 0xff09; 17m15.098s (13123,478Hz; -> 12000Hz???) hmm, 13237.2Hz? */
1256 /* val = 0xff0a; 47m30.599s (4764,891Hz; -> 4800Hz???) yup, 4803Hz */
1257 /* val = 0xff0c; 57m0.510s (4010,263Hz; -> 4000Hz???) yup, 4003Hz */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 /* val = 0xff05; 5m11.556s (... -> 44100Hz) */
1259 /* val = 0xff03; 10m21.529s (21872,463Hz; -> 22050Hz???) */
1260 /* val = 0xff0f; 20m41.883s (10937,993Hz; -> 11025Hz???) */
1261 /* val = 0xff0d; 41m23.135s (5523,600Hz; -> 5512Hz???) */
1262 /* val = 0xff0e; 28m30.777s (8017Hz; -> 8000Hz???) */
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001263
Andreas Mohr8d9a1142010-12-27 21:16:49 +01001264 val |= freq;
1265
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 if (channels == 2)
1267 val |= SOUNDFORMAT_FLAG_2CHANNELS;
1268
1269 if (format_width == 16)
1270 val |= SOUNDFORMAT_FLAG_16BIT;
1271
Andreas Mohrda237f32010-12-27 21:17:26 +01001272 spin_lock_irqsave(codec->lock, flags);
Andreas Mohr02330fba2008-05-16 12:18:29 +02001273
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 /* set bitrate/format */
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001275 snd_azf3328_codec_outw(codec, IDX_IO_CODEC_SOUNDFORMAT, val);
Andreas Mohr02330fba2008-05-16 12:18:29 +02001276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 /* changing the bitrate/format settings switches off the
1278 * audio output with an annoying click in case of 8/16bit format change
1279 * (maybe shutting down DAC/ADC?), thus immediately
1280 * do some tweaking to reenable it and get rid of the clicking
1281 * (FIXME: yes, it works, but what exactly am I doing here?? :)
1282 * FIXME: does this have some side effects for full-duplex
1283 * or other dramatic side effects? */
Andreas Mohradf59312010-12-27 21:16:43 +01001284 /* do it for non-capture codecs only */
Andreas Mohrda237f32010-12-27 21:17:26 +01001285 if (codec->type != AZF_CODEC_CAPTURE)
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001286 snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS,
1287 snd_azf3328_codec_inw(codec, IDX_IO_CODEC_DMA_FLAGS) |
1288 DMA_RUN_SOMETHING1 |
1289 DMA_RUN_SOMETHING2 |
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001290 SOMETHING_ALMOST_ALWAYS_SET |
1291 DMA_EPILOGUE_SOMETHING |
1292 DMA_SOMETHING_ELSE
1293 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
Andreas Mohrda237f32010-12-27 21:17:26 +01001295 spin_unlock_irqrestore(codec->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296}
1297
Andreas Mohr02330fba2008-05-16 12:18:29 +02001298static inline void
Andreas Mohrda237f32010-12-27 21:17:26 +01001299snd_azf3328_codec_setfmt_lowpower(struct snd_azf3328_codec_data *codec
Andreas Mohr02330fba2008-05-16 12:18:29 +02001300)
1301{
1302 /* choose lowest frequency for low power consumption.
1303 * While this will cause louder noise due to rather coarse frequency,
1304 * it should never matter since output should always
1305 * get disabled properly when idle anyway. */
Andreas Mohrda237f32010-12-27 21:17:26 +01001306 snd_azf3328_codec_setfmt(codec, AZF_FREQ_4000, 8, 1);
Andreas Mohr02330fba2008-05-16 12:18:29 +02001307}
1308
Andreas Mohr627d3e72008-06-23 11:50:47 +02001309static void
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001310snd_azf3328_ctrl_reg_6AH_update(struct snd_azf3328 *chip,
Andreas Mohr627d3e72008-06-23 11:50:47 +02001311 unsigned bitmask,
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001312 bool enable
Andreas Mohr627d3e72008-06-23 11:50:47 +02001313)
1314{
Andreas Mohr78df6172009-07-12 22:17:54 +02001315 bool do_mask = !enable;
1316 if (do_mask)
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001317 chip->shadow_reg_ctrl_6AH |= bitmask;
Andreas Mohr78df6172009-07-12 22:17:54 +02001318 else
1319 chip->shadow_reg_ctrl_6AH &= ~bitmask;
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001320 dev_dbg(chip->card->dev,
1321 "6AH_update mask 0x%04x do_mask %d: val 0x%04x\n",
1322 bitmask, do_mask, chip->shadow_reg_ctrl_6AH);
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001323 snd_azf3328_ctrl_outw(chip, IDX_IO_6AH, chip->shadow_reg_ctrl_6AH);
Andreas Mohr627d3e72008-06-23 11:50:47 +02001324}
1325
Andreas Mohr02330fba2008-05-16 12:18:29 +02001326static inline void
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001327snd_azf3328_ctrl_enable_codecs(struct snd_azf3328 *chip, bool enable)
Andreas Mohr02330fba2008-05-16 12:18:29 +02001328{
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001329 dev_dbg(chip->card->dev, "codec_enable %d\n", enable);
Andreas Mohr02330fba2008-05-16 12:18:29 +02001330 /* no idea what exactly is being done here, but I strongly assume it's
1331 * PM related */
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001332 snd_azf3328_ctrl_reg_6AH_update(
Andreas Mohr627d3e72008-06-23 11:50:47 +02001333 chip, IO_6A_PAUSE_PLAYBACK_BIT8, enable
Andreas Mohr02330fba2008-05-16 12:18:29 +02001334 );
1335}
1336
1337static void
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001338snd_azf3328_ctrl_codec_activity(struct snd_azf3328 *chip,
1339 enum snd_azf3328_codec_type codec_type,
1340 bool enable
Andreas Mohr02330fba2008-05-16 12:18:29 +02001341)
1342{
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001343 struct snd_azf3328_codec_data *codec = &chip->codecs[codec_type];
1344 bool need_change = (codec->running != enable);
Andreas Mohr02330fba2008-05-16 12:18:29 +02001345
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001346 dev_dbg(chip->card->dev,
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001347 "codec_activity: %s codec, enable %d, need_change %d\n",
1348 codec->name, enable, need_change
Andreas Mohr02330fba2008-05-16 12:18:29 +02001349 );
1350 if (need_change) {
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001351 static const struct {
1352 enum snd_azf3328_codec_type other1;
1353 enum snd_azf3328_codec_type other2;
1354 } peer_codecs[3] =
1355 { { AZF_CODEC_CAPTURE, AZF_CODEC_I2S_OUT },
1356 { AZF_CODEC_PLAYBACK, AZF_CODEC_I2S_OUT },
1357 { AZF_CODEC_PLAYBACK, AZF_CODEC_CAPTURE } };
1358 bool call_function;
1359
1360 if (enable)
1361 /* if enable codec, call enable_codecs func
1362 to enable codec supply... */
1363 call_function = 1;
1364 else {
1365 /* ...otherwise call enable_codecs func
1366 (which globally shuts down operation of codecs)
1367 only in case the other codecs are currently
1368 not active either! */
Andreas Mohr78df6172009-07-12 22:17:54 +02001369 call_function =
1370 ((!chip->codecs[peer_codecs[codec_type].other1]
1371 .running)
1372 && (!chip->codecs[peer_codecs[codec_type].other2]
1373 .running));
Dan Carpenter7f788e02015-02-25 16:32:45 +03001374 }
1375 if (call_function)
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001376 snd_azf3328_ctrl_enable_codecs(chip, enable);
Andreas Mohr02330fba2008-05-16 12:18:29 +02001377
1378 /* ...and adjust clock, too
1379 * (reduce noise and power consumption) */
1380 if (!enable)
Andreas Mohrda237f32010-12-27 21:17:26 +01001381 snd_azf3328_codec_setfmt_lowpower(codec);
Andreas Mohr78df6172009-07-12 22:17:54 +02001382 codec->running = enable;
Andreas Mohr02330fba2008-05-16 12:18:29 +02001383 }
Andreas Mohr02330fba2008-05-16 12:18:29 +02001384}
1385
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001386static void
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001387snd_azf3328_codec_setdmaa(struct snd_azf3328 *chip,
1388 struct snd_azf3328_codec_data *codec,
1389 unsigned long addr,
1390 unsigned int period_bytes,
1391 unsigned int buffer_bytes
Andreas Mohr02330fba2008-05-16 12:18:29 +02001392)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393{
Andreas Mohr689c6912010-12-27 21:17:35 +01001394 WARN_ONCE(period_bytes & 1, "odd period length!?\n");
1395 WARN_ONCE(buffer_bytes != 2 * period_bytes,
1396 "missed our input expectations! %u vs. %u\n",
1397 buffer_bytes, period_bytes);
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001398 if (!codec->running) {
1399 /* AZF3328 uses a two buffer pointer DMA transfer approach */
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001400
Andreas Mohr689c6912010-12-27 21:17:35 +01001401 unsigned long flags;
Andreas Mohr02330fba2008-05-16 12:18:29 +02001402
1403 /* width 32bit (prevent overflow): */
Andreas Mohr689c6912010-12-27 21:17:35 +01001404 u32 area_length;
1405 struct codec_setup_io {
1406 u32 dma_start_1;
1407 u32 dma_start_2;
1408 u32 dma_lengths;
1409 } __attribute__((packed)) setup_io;
Andreas Mohr02330fba2008-05-16 12:18:29 +02001410
Andreas Mohr689c6912010-12-27 21:17:35 +01001411 area_length = buffer_bytes/2;
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001412
Andreas Mohr689c6912010-12-27 21:17:35 +01001413 setup_io.dma_start_1 = addr;
1414 setup_io.dma_start_2 = addr+area_length;
1415
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001416 dev_dbg(chip->card->dev,
Andreas Mohr689c6912010-12-27 21:17:35 +01001417 "setdma: buffers %08x[%u] / %08x[%u], %u, %u\n",
1418 setup_io.dma_start_1, area_length,
1419 setup_io.dma_start_2, area_length,
1420 period_bytes, buffer_bytes);
1421
1422 /* Hmm, are we really supposed to decrement this by 1??
1423 Most definitely certainly not: configuring full length does
1424 work properly (i.e. likely better), and BTW we
1425 violated possibly differing frame sizes with this...
1426
1427 area_length--; |* max. index *|
1428 */
Andreas Mohr79741502010-11-21 12:09:32 +01001429
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001430 /* build combined I/O buffer length word */
Andreas Mohr689c6912010-12-27 21:17:35 +01001431 setup_io.dma_lengths = (area_length << 16) | (area_length);
1432
Andreas Mohrda237f32010-12-27 21:17:26 +01001433 spin_lock_irqsave(codec->lock, flags);
Andreas Mohr689c6912010-12-27 21:17:35 +01001434 snd_azf3328_codec_outl_multi(
1435 codec, IDX_IO_CODEC_DMA_START_1, &setup_io, 3
1436 );
Andreas Mohrda237f32010-12-27 21:17:26 +01001437 spin_unlock_irqrestore(codec->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439}
1440
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001441static int
Andreas Mohrda237f32010-12-27 21:17:26 +01001442snd_azf3328_pcm_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443{
Takashi Iwai95de7762005-11-17 15:02:42 +01001444 struct snd_pcm_runtime *runtime = substream->runtime;
Andreas Mohrda237f32010-12-27 21:17:26 +01001445 struct snd_azf3328_codec_data *codec = runtime->private_data;
Andreas Mohr34585592010-12-27 21:17:11 +01001446#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
1448 unsigned int count = snd_pcm_lib_period_bytes(substream);
1449#endif
1450
Andreas Mohr34585592010-12-27 21:17:11 +01001451 codec->dma_base = runtime->dma_addr;
1452
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453#if 0
Andreas Mohrda237f32010-12-27 21:17:26 +01001454 snd_azf3328_codec_setfmt(codec,
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001455 runtime->rate,
1456 snd_pcm_format_width(runtime->format),
1457 runtime->channels);
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001458 snd_azf3328_codec_setdmaa(chip, codec,
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001459 runtime->dma_addr, count, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 return 0;
1462}
1463
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001464static int
Andreas Mohrda237f32010-12-27 21:17:26 +01001465snd_azf3328_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466{
Takashi Iwai95de7762005-11-17 15:02:42 +01001467 struct snd_azf3328 *chip = snd_pcm_substream_chip(substream);
1468 struct snd_pcm_runtime *runtime = substream->runtime;
Andreas Mohrda237f32010-12-27 21:17:26 +01001469 struct snd_azf3328_codec_data *codec = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 int result = 0;
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001471 u16 flags1;
Peter Senna Tschudine0f17c72013-09-22 20:44:12 +02001472 bool previously_muted = false;
Andreas Mohrda237f32010-12-27 21:17:26 +01001473 bool is_main_mixer_playback_codec = (AZF_CODEC_PLAYBACK == codec->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 switch (cmd) {
1476 case SNDRV_PCM_TRIGGER_START:
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001477 dev_dbg(chip->card->dev, "START PCM %s\n", codec->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Andreas Mohrda237f32010-12-27 21:17:26 +01001479 if (is_main_mixer_playback_codec) {
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001480 /* mute WaveOut (avoid clicking during setup) */
1481 previously_muted =
Andreas Mohrb5dc20c2011-02-19 00:49:32 +01001482 snd_azf3328_mixer_mute_control_pcm(
1483 chip, 1
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001484 );
1485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Andreas Mohrda237f32010-12-27 21:17:26 +01001487 snd_azf3328_codec_setfmt(codec,
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001488 runtime->rate,
1489 snd_pcm_format_width(runtime->format),
1490 runtime->channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
Andreas Mohrda237f32010-12-27 21:17:26 +01001492 spin_lock(codec->lock);
Andreas Mohr02330fba2008-05-16 12:18:29 +02001493 /* first, remember current value: */
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001494 flags1 = snd_azf3328_codec_inw(codec, IDX_IO_CODEC_DMA_FLAGS);
Andreas Mohr02330fba2008-05-16 12:18:29 +02001495
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001496 /* stop transfer */
1497 flags1 &= ~DMA_RESUME;
1498 snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS, flags1);
Andreas Mohr02330fba2008-05-16 12:18:29 +02001499
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 /* FIXME: clear interrupts or what??? */
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001501 snd_azf3328_codec_outw(codec, IDX_IO_CODEC_IRQTYPE, 0xffff);
Andreas Mohrda237f32010-12-27 21:17:26 +01001502 spin_unlock(codec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001504 snd_azf3328_codec_setdmaa(chip, codec, runtime->dma_addr,
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001505 snd_pcm_lib_period_bytes(substream),
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001506 snd_pcm_lib_buffer_bytes(substream)
1507 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
Andreas Mohrda237f32010-12-27 21:17:26 +01001509 spin_lock(codec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510#ifdef WIN9X
1511 /* FIXME: enable playback/recording??? */
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001512 flags1 |= DMA_RUN_SOMETHING1 | DMA_RUN_SOMETHING2;
1513 snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS, flags1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001515 /* start transfer again */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 /* FIXME: what is this value (0x0010)??? */
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001517 flags1 |= DMA_RESUME | DMA_EPILOGUE_SOMETHING;
1518 snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS, flags1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519#else /* NT4 */
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001520 snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS,
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001521 0x0000);
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001522 snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS,
1523 DMA_RUN_SOMETHING1);
1524 snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS,
1525 DMA_RUN_SOMETHING1 |
1526 DMA_RUN_SOMETHING2);
1527 snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS,
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001528 DMA_RESUME |
1529 SOMETHING_ALMOST_ALWAYS_SET |
1530 DMA_EPILOGUE_SOMETHING |
1531 DMA_SOMETHING_ELSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532#endif
Andreas Mohrda237f32010-12-27 21:17:26 +01001533 spin_unlock(codec->lock);
1534 snd_azf3328_ctrl_codec_activity(chip, codec->type, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
Andreas Mohrda237f32010-12-27 21:17:26 +01001536 if (is_main_mixer_playback_codec) {
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001537 /* now unmute WaveOut */
1538 if (!previously_muted)
Andreas Mohrb5dc20c2011-02-19 00:49:32 +01001539 snd_azf3328_mixer_mute_control_pcm(
1540 chip, 0
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001541 );
1542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001544 dev_dbg(chip->card->dev, "PCM STARTED %s\n", codec->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 break;
Andreas Mohrca54bde2006-05-17 11:02:24 +02001546 case SNDRV_PCM_TRIGGER_RESUME:
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001547 dev_dbg(chip->card->dev, "PCM RESUME %s\n", codec->name);
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001548 /* resume codec if we were active */
Andreas Mohrda237f32010-12-27 21:17:26 +01001549 spin_lock(codec->lock);
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001550 if (codec->running)
1551 snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS,
1552 snd_azf3328_codec_inw(
1553 codec, IDX_IO_CODEC_DMA_FLAGS
1554 ) | DMA_RESUME
1555 );
Andreas Mohrda237f32010-12-27 21:17:26 +01001556 spin_unlock(codec->lock);
Andreas Mohrca54bde2006-05-17 11:02:24 +02001557 break;
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001558 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001559 dev_dbg(chip->card->dev, "PCM STOP %s\n", codec->name);
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001560
Andreas Mohrda237f32010-12-27 21:17:26 +01001561 if (is_main_mixer_playback_codec) {
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001562 /* mute WaveOut (avoid clicking during setup) */
1563 previously_muted =
Andreas Mohrb5dc20c2011-02-19 00:49:32 +01001564 snd_azf3328_mixer_mute_control_pcm(
1565 chip, 1
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001566 );
1567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
Andreas Mohrda237f32010-12-27 21:17:26 +01001569 spin_lock(codec->lock);
Andreas Mohr02330fba2008-05-16 12:18:29 +02001570 /* first, remember current value: */
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001571 flags1 = snd_azf3328_codec_inw(codec, IDX_IO_CODEC_DMA_FLAGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001573 /* stop transfer */
1574 flags1 &= ~DMA_RESUME;
1575 snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS, flags1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001577 /* hmm, is this really required? we're resetting the same bit
1578 * immediately thereafter... */
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001579 flags1 |= DMA_RUN_SOMETHING1;
1580 snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS, flags1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001582 flags1 &= ~DMA_RUN_SOMETHING1;
1583 snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS, flags1);
Andreas Mohrda237f32010-12-27 21:17:26 +01001584 spin_unlock(codec->lock);
1585 snd_azf3328_ctrl_codec_activity(chip, codec->type, 0);
Andreas Mohr02330fba2008-05-16 12:18:29 +02001586
Andreas Mohrda237f32010-12-27 21:17:26 +01001587 if (is_main_mixer_playback_codec) {
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001588 /* now unmute WaveOut */
1589 if (!previously_muted)
Andreas Mohrb5dc20c2011-02-19 00:49:32 +01001590 snd_azf3328_mixer_mute_control_pcm(
1591 chip, 0
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001592 );
1593 }
Andreas Mohr02330fba2008-05-16 12:18:29 +02001594
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001595 dev_dbg(chip->card->dev, "PCM STOPPED %s\n", codec->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 break;
Andreas Mohrca54bde2006-05-17 11:02:24 +02001597 case SNDRV_PCM_TRIGGER_SUSPEND:
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001598 dev_dbg(chip->card->dev, "PCM SUSPEND %s\n", codec->name);
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001599 /* make sure codec is stopped */
1600 snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS,
1601 snd_azf3328_codec_inw(
1602 codec, IDX_IO_CODEC_DMA_FLAGS
1603 ) & ~DMA_RESUME
1604 );
Andreas Mohrca54bde2006-05-17 11:02:24 +02001605 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001607 WARN(1, "FIXME: SNDRV_PCM_TRIGGER_PAUSE_PUSH NIY!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 break;
1609 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001610 WARN(1, "FIXME: SNDRV_PCM_TRIGGER_PAUSE_RELEASE NIY!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 break;
1612 default:
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001613 WARN(1, "FIXME: unknown trigger mode!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 return -EINVAL;
1615 }
Andreas Mohr02330fba2008-05-16 12:18:29 +02001616
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 return result;
1618}
1619
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001620static snd_pcm_uframes_t
Andreas Mohrda237f32010-12-27 21:17:26 +01001621snd_azf3328_pcm_pointer(struct snd_pcm_substream *substream
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001622)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623{
Andreas Mohrda237f32010-12-27 21:17:26 +01001624 const struct snd_azf3328_codec_data *codec =
1625 substream->runtime->private_data;
Andreas Mohr34585592010-12-27 21:17:11 +01001626 unsigned long result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 snd_pcm_uframes_t frmres;
1628
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001629 result = snd_azf3328_codec_inl(codec, IDX_IO_CODEC_DMA_CURRPOS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001631 /* calculate offset */
Andreas Mohr34585592010-12-27 21:17:11 +01001632#ifdef QUERY_HARDWARE
1633 result -= snd_azf3328_codec_inl(codec, IDX_IO_CODEC_DMA_START_1);
1634#else
1635 result -= codec->dma_base;
1636#endif
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001637 frmres = bytes_to_frames( substream->runtime, result);
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001638 dev_dbg(substream->pcm->card->dev, "%08li %s @ 0x%8lx, frames %8ld\n",
1639 jiffies, codec->name, result, frmres);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 return frmres;
1641}
1642
Andreas Mohr02330fba2008-05-16 12:18:29 +02001643/******************************************************************/
1644
1645#ifdef SUPPORT_GAMEPORT
1646static inline void
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001647snd_azf3328_gameport_irq_enable(struct snd_azf3328 *chip,
1648 bool enable
1649)
Andreas Mohr02330fba2008-05-16 12:18:29 +02001650{
1651 snd_azf3328_io_reg_setb(
1652 chip->game_io+IDX_GAME_HWCONFIG,
1653 GAME_HWCFG_IRQ_ENABLE,
1654 enable
1655 );
1656}
1657
1658static inline void
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001659snd_azf3328_gameport_legacy_address_enable(struct snd_azf3328 *chip,
1660 bool enable
1661)
Andreas Mohr02330fba2008-05-16 12:18:29 +02001662{
1663 snd_azf3328_io_reg_setb(
1664 chip->game_io+IDX_GAME_HWCONFIG,
1665 GAME_HWCFG_LEGACY_ADDRESS_ENABLE,
1666 enable
1667 );
1668}
1669
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001670static void
1671snd_azf3328_gameport_set_counter_frequency(struct snd_azf3328 *chip,
1672 unsigned int freq_cfg
1673)
Andreas Mohr02330fba2008-05-16 12:18:29 +02001674{
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001675 snd_azf3328_io_reg_setb(
1676 chip->game_io+IDX_GAME_HWCONFIG,
1677 0x02,
1678 (freq_cfg & 1) != 0
1679 );
1680 snd_azf3328_io_reg_setb(
1681 chip->game_io+IDX_GAME_HWCONFIG,
1682 0x04,
1683 (freq_cfg & 2) != 0
1684 );
1685}
1686
1687static inline void
1688snd_azf3328_gameport_axis_circuit_enable(struct snd_azf3328 *chip, bool enable)
1689{
1690 snd_azf3328_ctrl_reg_6AH_update(
Andreas Mohr627d3e72008-06-23 11:50:47 +02001691 chip, IO_6A_SOMETHING2_GAMEPORT, enable
Andreas Mohr02330fba2008-05-16 12:18:29 +02001692 );
1693}
1694
1695static inline void
1696snd_azf3328_gameport_interrupt(struct snd_azf3328 *chip)
1697{
1698 /*
1699 * skeleton handler only
1700 * (we do not want axis reading in interrupt handler - too much load!)
1701 */
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001702 dev_dbg(chip->card->dev, "gameport irq\n");
Andreas Mohr02330fba2008-05-16 12:18:29 +02001703
1704 /* this should ACK the gameport IRQ properly, hopefully. */
1705 snd_azf3328_game_inw(chip, IDX_GAME_AXIS_VALUE);
1706}
1707
1708static int
1709snd_azf3328_gameport_open(struct gameport *gameport, int mode)
1710{
1711 struct snd_azf3328 *chip = gameport_get_port_data(gameport);
1712 int res;
1713
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001714 dev_dbg(chip->card->dev, "gameport_open, mode %d\n", mode);
Andreas Mohr02330fba2008-05-16 12:18:29 +02001715 switch (mode) {
1716 case GAMEPORT_MODE_COOKED:
1717 case GAMEPORT_MODE_RAW:
1718 res = 0;
1719 break;
1720 default:
1721 res = -1;
1722 break;
1723 }
1724
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001725 snd_azf3328_gameport_set_counter_frequency(chip,
1726 GAME_HWCFG_ADC_COUNTER_FREQ_STD);
Andreas Mohr02330fba2008-05-16 12:18:29 +02001727 snd_azf3328_gameport_axis_circuit_enable(chip, (res == 0));
1728
1729 return res;
1730}
1731
1732static void
1733snd_azf3328_gameport_close(struct gameport *gameport)
1734{
1735 struct snd_azf3328 *chip = gameport_get_port_data(gameport);
1736
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001737 dev_dbg(chip->card->dev, "gameport_close\n");
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001738 snd_azf3328_gameport_set_counter_frequency(chip,
1739 GAME_HWCFG_ADC_COUNTER_FREQ_1_200);
Andreas Mohr02330fba2008-05-16 12:18:29 +02001740 snd_azf3328_gameport_axis_circuit_enable(chip, 0);
1741}
1742
1743static int
1744snd_azf3328_gameport_cooked_read(struct gameport *gameport,
1745 int *axes,
1746 int *buttons
1747)
1748{
1749 struct snd_azf3328 *chip = gameport_get_port_data(gameport);
1750 int i;
1751 u8 val;
1752 unsigned long flags;
1753
Takashi Iwaida3cec32008-08-08 17:12:14 +02001754 if (snd_BUG_ON(!chip))
1755 return 0;
Andreas Mohr02330fba2008-05-16 12:18:29 +02001756
1757 spin_lock_irqsave(&chip->reg_lock, flags);
1758 val = snd_azf3328_game_inb(chip, IDX_GAME_LEGACY_COMPATIBLE);
1759 *buttons = (~(val) >> 4) & 0xf;
1760
1761 /* ok, this one is a bit dirty: cooked_read is being polled by a timer,
1762 * thus we're atomic and cannot actively wait in here
1763 * (which would be useful for us since it probably would be better
1764 * to trigger a measurement in here, then wait a short amount of
1765 * time until it's finished, then read values of _this_ measurement).
1766 *
1767 * Thus we simply resort to reading values if they're available already
1768 * and trigger the next measurement.
1769 */
1770
1771 val = snd_azf3328_game_inb(chip, IDX_GAME_AXES_CONFIG);
1772 if (val & GAME_AXES_SAMPLING_READY) {
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001773 for (i = 0; i < ARRAY_SIZE(chip->axes); ++i) {
Andreas Mohr02330fba2008-05-16 12:18:29 +02001774 /* configure the axis to read */
1775 val = (i << 4) | 0x0f;
1776 snd_azf3328_game_outb(chip, IDX_GAME_AXES_CONFIG, val);
1777
1778 chip->axes[i] = snd_azf3328_game_inw(
1779 chip, IDX_GAME_AXIS_VALUE
1780 );
1781 }
1782 }
1783
Andreas Mohradf59312010-12-27 21:16:43 +01001784 /* trigger next sampling of axes, to be evaluated the next time we
Andreas Mohr02330fba2008-05-16 12:18:29 +02001785 * enter this function */
1786
1787 /* for some very, very strange reason we cannot enable
1788 * Measurement Ready monitoring for all axes here,
1789 * at least not when only one joystick connected */
1790 val = 0x03; /* we're able to monitor axes 1 and 2 only */
1791 snd_azf3328_game_outb(chip, IDX_GAME_AXES_CONFIG, val);
1792
1793 snd_azf3328_game_outw(chip, IDX_GAME_AXIS_VALUE, 0xffff);
1794 spin_unlock_irqrestore(&chip->reg_lock, flags);
1795
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001796 for (i = 0; i < ARRAY_SIZE(chip->axes); i++) {
Andreas Mohr02330fba2008-05-16 12:18:29 +02001797 axes[i] = chip->axes[i];
1798 if (axes[i] == 0xffff)
1799 axes[i] = -1;
1800 }
1801
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001802 dev_dbg(chip->card->dev, "cooked_read: axes %d %d %d %d buttons %d\n",
1803 axes[0], axes[1], axes[2], axes[3], *buttons);
Andreas Mohr02330fba2008-05-16 12:18:29 +02001804
1805 return 0;
1806}
1807
Bill Pembertone23e7a12012-12-06 12:35:10 -05001808static int
Andreas Mohr02330fba2008-05-16 12:18:29 +02001809snd_azf3328_gameport(struct snd_azf3328 *chip, int dev)
1810{
1811 struct gameport *gp;
1812
Andreas Mohr02330fba2008-05-16 12:18:29 +02001813 chip->gameport = gp = gameport_allocate_port();
1814 if (!gp) {
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001815 dev_err(chip->card->dev, "cannot alloc memory for gameport\n");
Andreas Mohr02330fba2008-05-16 12:18:29 +02001816 return -ENOMEM;
1817 }
1818
1819 gameport_set_name(gp, "AZF3328 Gameport");
1820 gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
1821 gameport_set_dev_parent(gp, &chip->pci->dev);
Andreas Mohr627d3e72008-06-23 11:50:47 +02001822 gp->io = chip->game_io;
Andreas Mohr02330fba2008-05-16 12:18:29 +02001823 gameport_set_port_data(gp, chip);
1824
1825 gp->open = snd_azf3328_gameport_open;
1826 gp->close = snd_azf3328_gameport_close;
1827 gp->fuzz = 16; /* seems ok */
1828 gp->cooked_read = snd_azf3328_gameport_cooked_read;
1829
1830 /* DISABLE legacy address: we don't need it! */
1831 snd_azf3328_gameport_legacy_address_enable(chip, 0);
1832
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001833 snd_azf3328_gameport_set_counter_frequency(chip,
1834 GAME_HWCFG_ADC_COUNTER_FREQ_1_200);
Andreas Mohr02330fba2008-05-16 12:18:29 +02001835 snd_azf3328_gameport_axis_circuit_enable(chip, 0);
1836
1837 gameport_register_port(chip->gameport);
1838
1839 return 0;
1840}
1841
1842static void
1843snd_azf3328_gameport_free(struct snd_azf3328 *chip)
1844{
1845 if (chip->gameport) {
1846 gameport_unregister_port(chip->gameport);
1847 chip->gameport = NULL;
1848 }
1849 snd_azf3328_gameport_irq_enable(chip, 0);
1850}
1851#else
1852static inline int
1853snd_azf3328_gameport(struct snd_azf3328 *chip, int dev) { return -ENOSYS; }
1854static inline void
1855snd_azf3328_gameport_free(struct snd_azf3328 *chip) { }
1856static inline void
1857snd_azf3328_gameport_interrupt(struct snd_azf3328 *chip)
1858{
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001859 dev_warn(chip->card->dev, "huh, game port IRQ occurred!?\n");
Andreas Mohr02330fba2008-05-16 12:18:29 +02001860}
1861#endif /* SUPPORT_GAMEPORT */
1862
1863/******************************************************************/
1864
Andreas Mohr627d3e72008-06-23 11:50:47 +02001865static inline void
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001866snd_azf3328_irq_log_unknown_type(struct snd_azf3328 *chip, u8 which)
Andreas Mohr627d3e72008-06-23 11:50:47 +02001867{
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001868 dev_dbg(chip->card->dev,
1869 "unknown IRQ type (%x) occurred, please report!\n",
1870 which);
Andreas Mohr627d3e72008-06-23 11:50:47 +02001871}
1872
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001873static inline void
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001874snd_azf3328_pcm_interrupt(struct snd_azf3328 *chip,
1875 const struct snd_azf3328_codec_data *first_codec,
Andreas Mohrda237f32010-12-27 21:17:26 +01001876 u8 status
1877)
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001878{
1879 u8 which;
1880 enum snd_azf3328_codec_type codec_type;
Andreas Mohrda237f32010-12-27 21:17:26 +01001881 const struct snd_azf3328_codec_data *codec = first_codec;
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001882
1883 for (codec_type = AZF_CODEC_PLAYBACK;
1884 codec_type <= AZF_CODEC_I2S_OUT;
Andreas Mohrda237f32010-12-27 21:17:26 +01001885 ++codec_type, ++codec) {
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001886
1887 /* skip codec if there's no interrupt for it */
1888 if (!(status & (1 << codec_type)))
1889 continue;
1890
Andreas Mohrda237f32010-12-27 21:17:26 +01001891 spin_lock(codec->lock);
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001892 which = snd_azf3328_codec_inb(codec, IDX_IO_CODEC_IRQTYPE);
1893 /* ack all IRQ types immediately */
1894 snd_azf3328_codec_outb(codec, IDX_IO_CODEC_IRQTYPE, which);
Andreas Mohrda237f32010-12-27 21:17:26 +01001895 spin_unlock(codec->lock);
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001896
Andreas Mohrda237f32010-12-27 21:17:26 +01001897 if (codec->substream) {
Andreas Mohr78df6172009-07-12 22:17:54 +02001898 snd_pcm_period_elapsed(codec->substream);
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001899 dev_dbg(chip->card->dev, "%s period done (#%x), @ %x\n",
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001900 codec->name,
1901 which,
1902 snd_azf3328_codec_inl(
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001903 codec, IDX_IO_CODEC_DMA_CURRPOS));
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001904 } else
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001905 dev_warn(chip->card->dev, "irq handler problem!\n");
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001906 if (which & IRQ_SOMETHING)
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001907 snd_azf3328_irq_log_unknown_type(chip, which);
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001908 }
1909}
1910
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001911static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +01001912snd_azf3328_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913{
Takashi Iwai95de7762005-11-17 15:02:42 +01001914 struct snd_azf3328 *chip = dev_id;
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001915 u8 status;
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001916 static unsigned long irq_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001918 status = snd_azf3328_ctrl_inb(chip, IDX_IO_IRQSTATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
1920 /* fast path out, to ease interrupt sharing */
Andreas Mohr02330fba2008-05-16 12:18:29 +02001921 if (!(status &
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001922 (IRQ_PLAYBACK|IRQ_RECORDING|IRQ_I2S_OUT
1923 |IRQ_GAMEPORT|IRQ_MPU401|IRQ_TIMER)
Andreas Mohr02330fba2008-05-16 12:18:29 +02001924 ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 return IRQ_NONE; /* must be interrupt for another device */
1926
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001927 dev_dbg(chip->card->dev,
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001928 "irq_count %ld! IDX_IO_IRQSTATUS %04x\n",
Andreas Mohr627d3e72008-06-23 11:50:47 +02001929 irq_count++ /* debug-only */,
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001930 status);
Andreas Mohr02330fba2008-05-16 12:18:29 +02001931
Andreas Mohre2f87262006-05-17 11:04:19 +02001932 if (status & IRQ_TIMER) {
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001933 /* dev_dbg(chip->card->dev, "timer %ld\n",
Andreas Mohr02330fba2008-05-16 12:18:29 +02001934 snd_azf3328_codec_inl(chip, IDX_IO_TIMER_VALUE)
1935 & TIMER_VALUE_MASK
1936 ); */
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001937 if (chip->timer)
1938 snd_timer_interrupt(chip->timer, chip->timer->sticks);
1939 /* ACK timer */
1940 spin_lock(&chip->reg_lock);
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001941 snd_azf3328_ctrl_outb(chip, IDX_IO_TIMER_VALUE + 3, 0x07);
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001942 spin_unlock(&chip->reg_lock);
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001943 dev_dbg(chip->card->dev, "timer IRQ\n");
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001944 }
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001945
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001946 if (status & (IRQ_PLAYBACK|IRQ_RECORDING|IRQ_I2S_OUT))
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001947 snd_azf3328_pcm_interrupt(chip, chip->codecs, status);
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001948
Andreas Mohr02330fba2008-05-16 12:18:29 +02001949 if (status & IRQ_GAMEPORT)
1950 snd_azf3328_gameport_interrupt(chip);
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001951
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001952 /* MPU401 has less critical IRQ requirements
1953 * than timer and playback/recording, right? */
Andreas Mohre2f87262006-05-17 11:04:19 +02001954 if (status & IRQ_MPU401) {
David Howells7d12e782006-10-05 14:55:46 +01001955 snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001956
1957 /* hmm, do we have to ack the IRQ here somehow?
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001958 * If so, then I don't know how yet... */
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001959 dev_dbg(chip->card->dev, "MPU401 IRQ\n");
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 return IRQ_HANDLED;
1962}
1963
1964/*****************************************************************/
1965
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001966/* as long as we think we have identical snd_pcm_hardware parameters
1967 for playback, capture and i2s out, we can use the same physical struct
1968 since the struct is simply being copied into a member.
1969*/
1970static const struct snd_pcm_hardware snd_azf3328_hardware =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971{
1972 /* FIXME!! Correct? */
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001973 .info = SNDRV_PCM_INFO_MMAP |
1974 SNDRV_PCM_INFO_INTERLEAVED |
1975 SNDRV_PCM_INFO_MMAP_VALID,
1976 .formats = SNDRV_PCM_FMTBIT_S8 |
1977 SNDRV_PCM_FMTBIT_U8 |
1978 SNDRV_PCM_FMTBIT_S16_LE |
1979 SNDRV_PCM_FMTBIT_U16_LE,
1980 .rates = SNDRV_PCM_RATE_5512 |
1981 SNDRV_PCM_RATE_8000_48000 |
1982 SNDRV_PCM_RATE_KNOT,
Andreas Mohr02330fba2008-05-16 12:18:29 +02001983 .rate_min = AZF_FREQ_4000,
1984 .rate_max = AZF_FREQ_66200,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 .channels_min = 1,
1986 .channels_max = 2,
Andreas Mohr79741502010-11-21 12:09:32 +01001987 .buffer_bytes_max = (64*1024),
1988 .period_bytes_min = 1024,
1989 .period_bytes_max = (32*1024),
1990 /* We simply have two DMA areas (instead of a list of descriptors
1991 such as other cards); I believe that this is a fixed hardware
1992 attribute and there isn't much driver magic to be done to expand it.
1993 Thus indicate that we have at least and at most 2 periods. */
1994 .periods_min = 2,
1995 .periods_max = 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 /* FIXME: maybe that card actually has a FIFO?
1997 * Hmm, it seems newer revisions do have one, but we still don't know
1998 * its size... */
1999 .fifo_size = 0,
2000};
2001
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
Takashi Iwai3e2fd042017-06-07 14:18:16 +02002003static const unsigned int snd_azf3328_fixed_rates[] = {
Andreas Mohr02330fba2008-05-16 12:18:29 +02002004 AZF_FREQ_4000,
2005 AZF_FREQ_4800,
2006 AZF_FREQ_5512,
2007 AZF_FREQ_6620,
2008 AZF_FREQ_8000,
2009 AZF_FREQ_9600,
2010 AZF_FREQ_11025,
2011 AZF_FREQ_13240,
2012 AZF_FREQ_16000,
2013 AZF_FREQ_22050,
2014 AZF_FREQ_32000,
2015 AZF_FREQ_44100,
2016 AZF_FREQ_48000,
2017 AZF_FREQ_66200
2018};
2019
Takashi Iwai3e2fd042017-06-07 14:18:16 +02002020static const struct snd_pcm_hw_constraint_list snd_azf3328_hw_constraints_rates = {
Andreas Mohr02330fba2008-05-16 12:18:29 +02002021 .count = ARRAY_SIZE(snd_azf3328_fixed_rates),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 .list = snd_azf3328_fixed_rates,
2023 .mask = 0,
2024};
2025
2026/*****************************************************************/
2027
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002028static int
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002029snd_azf3328_pcm_open(struct snd_pcm_substream *substream,
2030 enum snd_azf3328_codec_type codec_type
2031)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032{
Takashi Iwai95de7762005-11-17 15:02:42 +01002033 struct snd_azf3328 *chip = snd_pcm_substream_chip(substream);
2034 struct snd_pcm_runtime *runtime = substream->runtime;
Andreas Mohrda237f32010-12-27 21:17:26 +01002035 struct snd_azf3328_codec_data *codec = &chip->codecs[codec_type];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
Andreas Mohrda237f32010-12-27 21:17:26 +01002037 codec->substream = substream;
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002038
2039 /* same parameters for all our codecs - at least we think so... */
2040 runtime->hw = snd_azf3328_hardware;
2041
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2043 &snd_azf3328_hw_constraints_rates);
Andreas Mohrda237f32010-12-27 21:17:26 +01002044 runtime->private_data = codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 return 0;
2046}
2047
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002048static int
Andreas Mohrda237f32010-12-27 21:17:26 +01002049snd_azf3328_pcm_playback_open(struct snd_pcm_substream *substream)
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002050{
2051 return snd_azf3328_pcm_open(substream, AZF_CODEC_PLAYBACK);
2052}
2053
2054static int
Andreas Mohrda237f32010-12-27 21:17:26 +01002055snd_azf3328_pcm_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056{
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002057 return snd_azf3328_pcm_open(substream, AZF_CODEC_CAPTURE);
2058}
2059
2060static int
Andreas Mohrda237f32010-12-27 21:17:26 +01002061snd_azf3328_pcm_i2s_out_open(struct snd_pcm_substream *substream)
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002062{
2063 return snd_azf3328_pcm_open(substream, AZF_CODEC_I2S_OUT);
2064}
2065
2066static int
Andreas Mohrda237f32010-12-27 21:17:26 +01002067snd_azf3328_pcm_close(struct snd_pcm_substream *substream
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002068)
2069{
Andreas Mohrda237f32010-12-27 21:17:26 +01002070 struct snd_azf3328_codec_data *codec =
2071 substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
Andreas Mohrda237f32010-12-27 21:17:26 +01002073 codec->substream = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 return 0;
2075}
2076
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077/******************************************************************/
2078
Julia Lawall6769e9882016-09-02 00:13:10 +02002079static const struct snd_pcm_ops snd_azf3328_playback_ops = {
Andreas Mohrda237f32010-12-27 21:17:26 +01002080 .open = snd_azf3328_pcm_playback_open,
2081 .close = snd_azf3328_pcm_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 .ioctl = snd_pcm_lib_ioctl,
2083 .hw_params = snd_azf3328_hw_params,
2084 .hw_free = snd_azf3328_hw_free,
Andreas Mohrda237f32010-12-27 21:17:26 +01002085 .prepare = snd_azf3328_pcm_prepare,
2086 .trigger = snd_azf3328_pcm_trigger,
2087 .pointer = snd_azf3328_pcm_pointer
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088};
2089
Julia Lawall6769e9882016-09-02 00:13:10 +02002090static const struct snd_pcm_ops snd_azf3328_capture_ops = {
Andreas Mohrda237f32010-12-27 21:17:26 +01002091 .open = snd_azf3328_pcm_capture_open,
2092 .close = snd_azf3328_pcm_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 .ioctl = snd_pcm_lib_ioctl,
2094 .hw_params = snd_azf3328_hw_params,
2095 .hw_free = snd_azf3328_hw_free,
Andreas Mohrda237f32010-12-27 21:17:26 +01002096 .prepare = snd_azf3328_pcm_prepare,
2097 .trigger = snd_azf3328_pcm_trigger,
2098 .pointer = snd_azf3328_pcm_pointer
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002099};
2100
Julia Lawall6769e9882016-09-02 00:13:10 +02002101static const struct snd_pcm_ops snd_azf3328_i2s_out_ops = {
Andreas Mohrda237f32010-12-27 21:17:26 +01002102 .open = snd_azf3328_pcm_i2s_out_open,
2103 .close = snd_azf3328_pcm_close,
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002104 .ioctl = snd_pcm_lib_ioctl,
2105 .hw_params = snd_azf3328_hw_params,
2106 .hw_free = snd_azf3328_hw_free,
Andreas Mohrda237f32010-12-27 21:17:26 +01002107 .prepare = snd_azf3328_pcm_prepare,
2108 .trigger = snd_azf3328_pcm_trigger,
2109 .pointer = snd_azf3328_pcm_pointer
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110};
2111
Bill Pembertone23e7a12012-12-06 12:35:10 -05002112static int
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002113snd_azf3328_pcm(struct snd_azf3328 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114{
Dan Carpenter7f788e02015-02-25 16:32:45 +03002115 /* pcm devices */
2116 enum { AZF_PCMDEV_STD, AZF_PCMDEV_I2S_OUT, NUM_AZF_PCMDEVS };
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002117
Takashi Iwai95de7762005-11-17 15:02:42 +01002118 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 int err;
2120
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002121 err = snd_pcm_new(chip->card, "AZF3328 DSP", AZF_PCMDEV_STD,
2122 1, 1, &pcm);
2123 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 return err;
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002125 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
2126 &snd_azf3328_playback_ops);
2127 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
2128 &snd_azf3328_capture_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
2130 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 pcm->info_flags = 0;
2132 strcpy(pcm->name, chip->card->shortname);
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002133 /* same pcm object for playback/capture (see snd_pcm_new() above) */
2134 chip->pcm[AZF_CODEC_PLAYBACK] = pcm;
2135 chip->pcm[AZF_CODEC_CAPTURE] = pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
2137 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002138 snd_dma_pci_data(chip->pci),
2139 64*1024, 64*1024);
2140
2141 err = snd_pcm_new(chip->card, "AZF3328 I2S OUT", AZF_PCMDEV_I2S_OUT,
2142 1, 0, &pcm);
2143 if (err < 0)
2144 return err;
2145 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
2146 &snd_azf3328_i2s_out_ops);
2147
2148 pcm->private_data = chip;
2149 pcm->info_flags = 0;
2150 strcpy(pcm->name, chip->card->shortname);
2151 chip->pcm[AZF_CODEC_I2S_OUT] = pcm;
2152
2153 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
2154 snd_dma_pci_data(chip->pci),
2155 64*1024, 64*1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 return 0;
2158}
2159
2160/******************************************************************/
2161
Andreas Mohr02330fba2008-05-16 12:18:29 +02002162/*** NOTE: the physical timer resolution actually is 1024000 ticks per second
2163 *** (probably derived from main crystal via a divider of 24),
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002164 *** but announcing those attributes to user-space would make programs
2165 *** configure the timer to a 1 tick value, resulting in an absolutely fatal
2166 *** timer IRQ storm.
2167 *** Thus I chose to announce a down-scaled virtual timer to the outside and
2168 *** calculate real timer countdown values internally.
2169 *** (the scale factor can be set via module parameter "seqtimer_scaling").
2170 ***/
2171
2172static int
Takashi Iwai95de7762005-11-17 15:02:42 +01002173snd_azf3328_timer_start(struct snd_timer *timer)
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002174{
Takashi Iwai95de7762005-11-17 15:02:42 +01002175 struct snd_azf3328 *chip;
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002176 unsigned long flags;
2177 unsigned int delay;
2178
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002179 chip = snd_timer_chip(timer);
2180 delay = ((timer->sticks * seqtimer_scaling) - 1) & TIMER_VALUE_MASK;
Andreas Mohre2f87262006-05-17 11:04:19 +02002181 if (delay < 49) {
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002182 /* uhoh, that's not good, since user-space won't know about
2183 * this timing tweak
2184 * (we need to do it to avoid a lockup, though) */
2185
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002186 dev_dbg(chip->card->dev, "delay was too low (%d)!\n", delay);
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002187 delay = 49; /* minimum time is 49 ticks */
2188 }
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002189 dev_dbg(chip->card->dev, "setting timer countdown value %d\n", delay);
Andreas Mohr02330fba2008-05-16 12:18:29 +02002190 delay |= TIMER_COUNTDOWN_ENABLE | TIMER_IRQ_ENABLE;
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002191 spin_lock_irqsave(&chip->reg_lock, flags);
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002192 snd_azf3328_ctrl_outl(chip, IDX_IO_TIMER_VALUE, delay);
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002193 spin_unlock_irqrestore(&chip->reg_lock, flags);
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002194 return 0;
2195}
2196
2197static int
Takashi Iwai95de7762005-11-17 15:02:42 +01002198snd_azf3328_timer_stop(struct snd_timer *timer)
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002199{
Takashi Iwai95de7762005-11-17 15:02:42 +01002200 struct snd_azf3328 *chip;
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002201 unsigned long flags;
2202
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002203 chip = snd_timer_chip(timer);
2204 spin_lock_irqsave(&chip->reg_lock, flags);
2205 /* disable timer countdown and interrupt */
Andreas Mohr79741502010-11-21 12:09:32 +01002206 /* Hmm, should we write TIMER_IRQ_ACK here?
2207 YES indeed, otherwise a rogue timer operation - which prompts
2208 ALSA(?) to call repeated stop() in vain, but NOT start() -
2209 will never end (value 0x03 is kept shown in control byte).
2210 Simply manually poking 0x04 _once_ immediately successfully stops
2211 the hardware/ALSA interrupt activity. */
2212 snd_azf3328_ctrl_outb(chip, IDX_IO_TIMER_VALUE + 3, 0x04);
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002213 spin_unlock_irqrestore(&chip->reg_lock, flags);
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002214 return 0;
2215}
2216
2217
2218static int
Takashi Iwai95de7762005-11-17 15:02:42 +01002219snd_azf3328_timer_precise_resolution(struct snd_timer *timer,
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002220 unsigned long *num, unsigned long *den)
2221{
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002222 *num = 1;
2223 *den = 1024000 / seqtimer_scaling;
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002224 return 0;
2225}
2226
Takashi Iwai95de7762005-11-17 15:02:42 +01002227static struct snd_timer_hardware snd_azf3328_timer_hw = {
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002228 .flags = SNDRV_TIMER_HW_AUTO,
2229 .resolution = 977, /* 1000000/1024000 = 0.9765625us */
2230 .ticks = 1024000, /* max tick count, defined by the value register; actually it's not 1024000, but 1048576, but we don't care */
2231 .start = snd_azf3328_timer_start,
2232 .stop = snd_azf3328_timer_stop,
2233 .precise_resolution = snd_azf3328_timer_precise_resolution,
2234};
2235
Bill Pembertone23e7a12012-12-06 12:35:10 -05002236static int
Takashi Iwai95de7762005-11-17 15:02:42 +01002237snd_azf3328_timer(struct snd_azf3328 *chip, int device)
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002238{
Takashi Iwai95de7762005-11-17 15:02:42 +01002239 struct snd_timer *timer = NULL;
2240 struct snd_timer_id tid;
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002241 int err;
2242
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002243 tid.dev_class = SNDRV_TIMER_CLASS_CARD;
2244 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
2245 tid.card = chip->card->number;
2246 tid.device = device;
2247 tid.subdevice = 0;
2248
2249 snd_azf3328_timer_hw.resolution *= seqtimer_scaling;
2250 snd_azf3328_timer_hw.ticks /= seqtimer_scaling;
Andreas Mohr02330fba2008-05-16 12:18:29 +02002251
2252 err = snd_timer_new(chip->card, "AZF3328", &tid, &timer);
2253 if (err < 0)
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002254 goto out;
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002255
2256 strcpy(timer->name, "AZF3328 timer");
2257 timer->private_data = chip;
2258 timer->hw = snd_azf3328_timer_hw;
2259
2260 chip->timer = timer;
2261
Andreas Mohr02330fba2008-05-16 12:18:29 +02002262 snd_azf3328_timer_stop(timer);
2263
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002264 err = 0;
2265
2266out:
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002267 return err;
2268}
2269
2270/******************************************************************/
2271
Andreas Mohr02330fba2008-05-16 12:18:29 +02002272static int
2273snd_azf3328_free(struct snd_azf3328 *chip)
2274{
2275 if (chip->irq < 0)
2276 goto __end_hw;
2277
Andreas Mohrb5dc20c2011-02-19 00:49:32 +01002278 snd_azf3328_mixer_reset(chip);
Andreas Mohr02330fba2008-05-16 12:18:29 +02002279
2280 snd_azf3328_timer_stop(chip->timer);
2281 snd_azf3328_gameport_free(chip);
2282
Andreas Mohr02330fba2008-05-16 12:18:29 +02002283__end_hw:
2284 if (chip->irq >= 0)
2285 free_irq(chip->irq, chip);
2286 pci_release_regions(chip->pci);
2287 pci_disable_device(chip->pci);
2288
2289 kfree(chip);
2290 return 0;
2291}
2292
2293static int
2294snd_azf3328_dev_free(struct snd_device *device)
2295{
2296 struct snd_azf3328 *chip = device->device_data;
2297 return snd_azf3328_free(chip);
2298}
2299
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300#if 0
2301/* check whether a bit can be modified */
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002302static void
Andreas Mohr02330fba2008-05-16 12:18:29 +02002303snd_azf3328_test_bit(unsigned unsigned reg, int bit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304{
2305 unsigned char val, valoff, valon;
2306
2307 val = inb(reg);
2308
2309 outb(val & ~(1 << bit), reg);
2310 valoff = inb(reg);
2311
2312 outb(val|(1 << bit), reg);
2313 valon = inb(reg);
Andreas Mohr02330fba2008-05-16 12:18:29 +02002314
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 outb(val, reg);
2316
Andreas Mohr78df6172009-07-12 22:17:54 +02002317 printk(KERN_DEBUG "reg %04x bit %d: %02x %02x %02x\n",
Andreas Mohr02330fba2008-05-16 12:18:29 +02002318 reg, bit, val, valoff, valon
2319 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320}
2321#endif
2322
Andreas Mohr02330fba2008-05-16 12:18:29 +02002323static inline void
Takashi Iwai95de7762005-11-17 15:02:42 +01002324snd_azf3328_debug_show_ports(const struct snd_azf3328 *chip)
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002325{
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002326 u16 tmp;
2327
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002328 dev_dbg(chip->card->dev,
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002329 "ctrl_io 0x%lx, game_io 0x%lx, mpu_io 0x%lx, "
Andreas Mohr02330fba2008-05-16 12:18:29 +02002330 "opl3_io 0x%lx, mixer_io 0x%lx, irq %d\n",
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002331 chip->ctrl_io, chip->game_io, chip->mpu_io,
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002332 chip->opl3_io, chip->mixer_io, chip->irq);
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002333
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002334 dev_dbg(chip->card->dev,
2335 "game %02x %02x %02x %02x %02x %02x\n",
Andreas Mohr02330fba2008-05-16 12:18:29 +02002336 snd_azf3328_game_inb(chip, 0),
2337 snd_azf3328_game_inb(chip, 1),
2338 snd_azf3328_game_inb(chip, 2),
2339 snd_azf3328_game_inb(chip, 3),
2340 snd_azf3328_game_inb(chip, 4),
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002341 snd_azf3328_game_inb(chip, 5));
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002342
Andreas Mohr02330fba2008-05-16 12:18:29 +02002343 for (tmp = 0; tmp < 0x07; tmp += 1)
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002344 dev_dbg(chip->card->dev,
2345 "mpu_io 0x%04x\n", inb(chip->mpu_io + tmp));
Andreas Mohr02330fba2008-05-16 12:18:29 +02002346
2347 for (tmp = 0; tmp <= 0x07; tmp += 1)
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002348 dev_dbg(chip->card->dev,
2349 "0x%02x: game200 0x%04x, game208 0x%04x\n",
Andreas Mohr02330fba2008-05-16 12:18:29 +02002350 tmp, inb(0x200 + tmp), inb(0x208 + tmp));
2351
2352 for (tmp = 0; tmp <= 0x01; tmp += 1)
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002353 dev_dbg(chip->card->dev,
Andreas Mohr02330fba2008-05-16 12:18:29 +02002354 "0x%02x: mpu300 0x%04x, mpu310 0x%04x, mpu320 0x%04x, "
2355 "mpu330 0x%04x opl388 0x%04x opl38c 0x%04x\n",
2356 tmp,
2357 inb(0x300 + tmp),
2358 inb(0x310 + tmp),
2359 inb(0x320 + tmp),
2360 inb(0x330 + tmp),
2361 inb(0x388 + tmp),
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002362 inb(0x38c + tmp));
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002363
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002364 for (tmp = 0; tmp < AZF_IO_SIZE_CTRL; tmp += 2)
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002365 dev_dbg(chip->card->dev,
2366 "ctrl 0x%02x: 0x%04x\n",
2367 tmp, snd_azf3328_ctrl_inw(chip, tmp));
Andreas Mohre24a1212007-03-26 12:49:45 +02002368
2369 for (tmp = 0; tmp < AZF_IO_SIZE_MIXER; tmp += 2)
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002370 dev_dbg(chip->card->dev,
2371 "mixer 0x%02x: 0x%04x\n",
2372 tmp, snd_azf3328_mixer_inw(chip, tmp));
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002373}
2374
Bill Pembertone23e7a12012-12-06 12:35:10 -05002375static int
Takashi Iwai95de7762005-11-17 15:02:42 +01002376snd_azf3328_create(struct snd_card *card,
Andreas Mohr02330fba2008-05-16 12:18:29 +02002377 struct pci_dev *pci,
2378 unsigned long device_type,
2379 struct snd_azf3328 **rchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380{
Takashi Iwai95de7762005-11-17 15:02:42 +01002381 struct snd_azf3328 *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 int err;
Takashi Iwai95de7762005-11-17 15:02:42 +01002383 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 .dev_free = snd_azf3328_dev_free,
2385 };
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002386 u8 dma_init;
2387 enum snd_azf3328_codec_type codec_type;
Andreas Mohrda237f32010-12-27 21:17:26 +01002388 struct snd_azf3328_codec_data *codec_setup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389
2390 *rchip = NULL;
2391
Andreas Mohr02330fba2008-05-16 12:18:29 +02002392 err = pci_enable_device(pci);
2393 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 return err;
2395
Takashi Iwaie560d8d2005-09-09 14:21:46 +02002396 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 if (chip == NULL) {
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002398 err = -ENOMEM;
2399 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 }
2401 spin_lock_init(&chip->reg_lock);
2402 chip->card = card;
2403 chip->pci = pci;
2404 chip->irq = -1;
2405
2406 /* check if we can restrict PCI DMA transfers to 24 bits */
Quentin Lambert412b9792015-04-15 16:10:17 +02002407 if (dma_set_mask(&pci->dev, DMA_BIT_MASK(24)) < 0 ||
2408 dma_set_coherent_mask(&pci->dev, DMA_BIT_MASK(24)) < 0) {
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002409 dev_err(card->dev,
2410 "architecture does not support 24bit PCI busmaster DMA\n"
Andreas Mohr02330fba2008-05-16 12:18:29 +02002411 );
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002412 err = -ENXIO;
2413 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 }
2415
Andreas Mohr02330fba2008-05-16 12:18:29 +02002416 err = pci_request_regions(pci, "Aztech AZF3328");
2417 if (err < 0)
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002418 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002420 chip->ctrl_io = pci_resource_start(pci, 0);
Andreas Mohr02330fba2008-05-16 12:18:29 +02002421 chip->game_io = pci_resource_start(pci, 1);
2422 chip->mpu_io = pci_resource_start(pci, 2);
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002423 chip->opl3_io = pci_resource_start(pci, 3);
Andreas Mohr02330fba2008-05-16 12:18:29 +02002424 chip->mixer_io = pci_resource_start(pci, 4);
2425
Andreas Mohr9fd8d362010-12-27 21:17:00 +01002426 codec_setup = &chip->codecs[AZF_CODEC_PLAYBACK];
2427 codec_setup->io_base = chip->ctrl_io + AZF_IO_OFFS_CODEC_PLAYBACK;
Andreas Mohrda237f32010-12-27 21:17:26 +01002428 codec_setup->lock = &chip->reg_lock;
2429 codec_setup->type = AZF_CODEC_PLAYBACK;
Andreas Mohr9fd8d362010-12-27 21:17:00 +01002430 codec_setup->name = "PLAYBACK";
2431
2432 codec_setup = &chip->codecs[AZF_CODEC_CAPTURE];
2433 codec_setup->io_base = chip->ctrl_io + AZF_IO_OFFS_CODEC_CAPTURE;
Andreas Mohrda237f32010-12-27 21:17:26 +01002434 codec_setup->lock = &chip->reg_lock;
2435 codec_setup->type = AZF_CODEC_CAPTURE;
Andreas Mohr9fd8d362010-12-27 21:17:00 +01002436 codec_setup->name = "CAPTURE";
2437
2438 codec_setup = &chip->codecs[AZF_CODEC_I2S_OUT];
2439 codec_setup->io_base = chip->ctrl_io + AZF_IO_OFFS_CODEC_I2S_OUT;
Andreas Mohrda237f32010-12-27 21:17:26 +01002440 codec_setup->lock = &chip->reg_lock;
2441 codec_setup->type = AZF_CODEC_I2S_OUT;
Andreas Mohr9fd8d362010-12-27 21:17:00 +01002442 codec_setup->name = "I2S_OUT";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443
Takashi Iwai437a5a42006-11-21 12:14:23 +01002444 if (request_irq(pci->irq, snd_azf3328_interrupt,
Takashi Iwai934c2b62011-06-10 16:36:37 +02002445 IRQF_SHARED, KBUILD_MODNAME, chip)) {
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002446 dev_err(card->dev, "unable to grab IRQ %d\n", pci->irq);
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002447 err = -EBUSY;
2448 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 }
2450 chip->irq = pci->irq;
2451 pci_set_master(pci);
2452 synchronize_irq(chip->irq);
2453
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002454 snd_azf3328_debug_show_ports(chip);
Andreas Mohr02330fba2008-05-16 12:18:29 +02002455
2456 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
2457 if (err < 0)
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002458 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459
2460 /* create mixer interface & switches */
Andreas Mohr02330fba2008-05-16 12:18:29 +02002461 err = snd_azf3328_mixer_new(chip);
2462 if (err < 0)
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002463 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002465 /* standard codec init stuff */
2466 /* default DMA init value */
2467 dma_init = DMA_RUN_SOMETHING2|DMA_EPILOGUE_SOMETHING|DMA_SOMETHING_ELSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002469 for (codec_type = AZF_CODEC_PLAYBACK;
2470 codec_type <= AZF_CODEC_I2S_OUT; ++codec_type) {
2471 struct snd_azf3328_codec_data *codec =
2472 &chip->codecs[codec_type];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473
Andreas Mohradf59312010-12-27 21:16:43 +01002474 /* shutdown codecs to reduce power / noise */
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002475 /* have ...ctrl_codec_activity() act properly */
2476 codec->running = 1;
2477 snd_azf3328_ctrl_codec_activity(chip, codec_type, 0);
2478
Andreas Mohrda237f32010-12-27 21:17:26 +01002479 spin_lock_irq(codec->lock);
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002480 snd_azf3328_codec_outb(codec, IDX_IO_CODEC_DMA_FLAGS,
2481 dma_init);
Andreas Mohrda237f32010-12-27 21:17:26 +01002482 spin_unlock_irq(codec->lock);
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 *rchip = chip;
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002486
2487 err = 0;
2488 goto out;
2489
2490out_err:
2491 if (chip)
2492 snd_azf3328_free(chip);
2493 pci_disable_device(pci);
2494
2495out:
2496 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497}
2498
Bill Pembertone23e7a12012-12-06 12:35:10 -05002499static int
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002500snd_azf3328_probe(struct pci_dev *pci, const struct pci_device_id *pci_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501{
2502 static int dev;
Takashi Iwai95de7762005-11-17 15:02:42 +01002503 struct snd_card *card;
2504 struct snd_azf3328 *chip;
2505 struct snd_opl3 *opl3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 int err;
2507
Julia Lawalla5a39732011-08-10 11:49:04 +02002508 if (dev >= SNDRV_CARDS) {
2509 err = -ENODEV;
2510 goto out;
2511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 if (!enable[dev]) {
2513 dev++;
Julia Lawalla5a39732011-08-10 11:49:04 +02002514 err = -ENOENT;
2515 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 }
2517
Takashi Iwai60c57722014-01-29 14:20:19 +01002518 err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
2519 0, &card);
Takashi Iwaie58de7b2008-12-28 16:44:30 +01002520 if (err < 0)
Julia Lawalla5a39732011-08-10 11:49:04 +02002521 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522
2523 strcpy(card->driver, "AZF3328");
2524 strcpy(card->shortname, "Aztech AZF3328 (PCI168)");
2525
Andreas Mohr02330fba2008-05-16 12:18:29 +02002526 err = snd_azf3328_create(card, pci, pci_id->driver_data, &chip);
2527 if (err < 0)
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002528 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529
Andreas Mohrca54bde2006-05-17 11:02:24 +02002530 card->private_data = chip;
2531
Andreas Mohr78df6172009-07-12 22:17:54 +02002532 /* chose to use MPU401_HW_AZT2320 ID instead of MPU401_HW_MPU401,
2533 since our hardware ought to be similar, thus use same ID. */
Andreas Mohr02330fba2008-05-16 12:18:29 +02002534 err = snd_mpu401_uart_new(
Andreas Mohr78df6172009-07-12 22:17:54 +02002535 card, 0,
Clemens Ladischdba8b462011-09-13 11:24:41 +02002536 MPU401_HW_AZT2320, chip->mpu_io,
2537 MPU401_INFO_INTEGRATED | MPU401_INFO_IRQ_HOOK,
2538 -1, &chip->rmidi
Andreas Mohr02330fba2008-05-16 12:18:29 +02002539 );
2540 if (err < 0) {
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002541 dev_err(card->dev, "no MPU-401 device at 0x%lx?\n",
Andreas Mohr02330fba2008-05-16 12:18:29 +02002542 chip->mpu_io
2543 );
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002544 goto out_err;
2545 }
2546
Andreas Mohr02330fba2008-05-16 12:18:29 +02002547 err = snd_azf3328_timer(chip, 0);
2548 if (err < 0)
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002549 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002551 err = snd_azf3328_pcm(chip);
Andreas Mohr02330fba2008-05-16 12:18:29 +02002552 if (err < 0)
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002553 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554
Andreas Mohr02330fba2008-05-16 12:18:29 +02002555 if (snd_opl3_create(card, chip->opl3_io, chip->opl3_io+2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 OPL3_HW_AUTO, 1, &opl3) < 0) {
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002557 dev_err(card->dev, "no OPL3 device at 0x%lx-0x%lx?\n",
Andreas Mohr02330fba2008-05-16 12:18:29 +02002558 chip->opl3_io, chip->opl3_io+2
2559 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 } else {
Andreas Mohr02330fba2008-05-16 12:18:29 +02002561 /* need to use IDs 1, 2 since ID 0 is snd_azf3328_timer above */
2562 err = snd_opl3_timer_new(opl3, 1, 2);
2563 if (err < 0)
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002564 goto out_err;
Andreas Mohr02330fba2008-05-16 12:18:29 +02002565 err = snd_opl3_hwdep_new(opl3, 0, 1, NULL);
2566 if (err < 0)
2567 goto out_err;
Alban Bedel87c9e7d2012-02-25 16:15:57 +01002568 opl3->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 }
2570
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 sprintf(card->longname, "%s at 0x%lx, irq %i",
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002572 card->shortname, chip->ctrl_io, chip->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573
Andreas Mohr02330fba2008-05-16 12:18:29 +02002574 err = snd_card_register(card);
2575 if (err < 0)
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002576 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577
2578#ifdef MODULE
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002579 dev_info(card->dev,
2580 "Sound driver for Aztech AZF3328-based soundcards such as PCI168.\n");
2581 dev_info(card->dev,
2582 "Hardware was completely undocumented, unfortunately.\n");
2583 dev_info(card->dev,
2584 "Feel free to contact andi AT lisas.de for bug reports etc.!\n");
2585 dev_info(card->dev,
2586 "User-scalable sequencer timer set to %dHz (1024000Hz / %d).\n",
2587 1024000 / seqtimer_scaling, seqtimer_scaling);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588#endif
2589
Andreas Mohr02330fba2008-05-16 12:18:29 +02002590 snd_azf3328_gameport(chip, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
2592 pci_set_drvdata(pci, card);
2593 dev++;
2594
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002595 err = 0;
2596 goto out;
Andreas Mohr02330fba2008-05-16 12:18:29 +02002597
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002598out_err:
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002599 dev_err(card->dev, "something failed, exiting\n");
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002600 snd_card_free(card);
Andreas Mohr02330fba2008-05-16 12:18:29 +02002601
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002602out:
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002603 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604}
2605
Bill Pembertone23e7a12012-12-06 12:35:10 -05002606static void
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002607snd_azf3328_remove(struct pci_dev *pci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 snd_card_free(pci_get_drvdata(pci));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610}
2611
Takashi Iwaic7561cd2012-08-14 18:12:04 +02002612#ifdef CONFIG_PM_SLEEP
Andreas Mohr78df6172009-07-12 22:17:54 +02002613static inline void
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002614snd_azf3328_suspend_regs(const struct snd_azf3328 *chip,
2615 unsigned long io_addr, unsigned count, u32 *saved_regs)
Andreas Mohr78df6172009-07-12 22:17:54 +02002616{
2617 unsigned reg;
2618
2619 for (reg = 0; reg < count; ++reg) {
2620 *saved_regs = inl(io_addr);
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002621 dev_dbg(chip->card->dev, "suspend: io 0x%04lx: 0x%08x\n",
Andreas Mohr78df6172009-07-12 22:17:54 +02002622 io_addr, *saved_regs);
2623 ++saved_regs;
2624 io_addr += sizeof(*saved_regs);
2625 }
2626}
2627
Andreas Mohrb5dc20c2011-02-19 00:49:32 +01002628static inline void
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002629snd_azf3328_resume_regs(const struct snd_azf3328 *chip,
2630 const u32 *saved_regs,
Andreas Mohrb5dc20c2011-02-19 00:49:32 +01002631 unsigned long io_addr,
2632 unsigned count
2633)
2634{
2635 unsigned reg;
2636
2637 for (reg = 0; reg < count; ++reg) {
2638 outl(*saved_regs, io_addr);
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002639 dev_dbg(chip->card->dev,
2640 "resume: io 0x%04lx: 0x%08x --> 0x%08x\n",
Andreas Mohrb5dc20c2011-02-19 00:49:32 +01002641 io_addr, *saved_regs, inl(io_addr));
2642 ++saved_regs;
2643 io_addr += sizeof(*saved_regs);
2644 }
2645}
2646
2647static inline void
2648snd_azf3328_suspend_ac97(struct snd_azf3328 *chip)
2649{
2650#ifdef AZF_USE_AC97_LAYER
2651 snd_ac97_suspend(chip->ac97);
2652#else
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002653 snd_azf3328_suspend_regs(chip, chip->mixer_io,
Andreas Mohrb5dc20c2011-02-19 00:49:32 +01002654 ARRAY_SIZE(chip->saved_regs_mixer), chip->saved_regs_mixer);
2655
2656 /* make sure to disable master volume etc. to prevent looping sound */
2657 snd_azf3328_mixer_mute_control_master(chip, 1);
2658 snd_azf3328_mixer_mute_control_pcm(chip, 1);
2659#endif /* AZF_USE_AC97_LAYER */
2660}
2661
2662static inline void
2663snd_azf3328_resume_ac97(const struct snd_azf3328 *chip)
2664{
2665#ifdef AZF_USE_AC97_LAYER
2666 snd_ac97_resume(chip->ac97);
2667#else
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002668 snd_azf3328_resume_regs(chip, chip->saved_regs_mixer, chip->mixer_io,
Andreas Mohrb5dc20c2011-02-19 00:49:32 +01002669 ARRAY_SIZE(chip->saved_regs_mixer));
2670
2671 /* unfortunately with 32bit transfers, IDX_MIXER_PLAY_MASTER (0x02)
2672 and IDX_MIXER_RESET (offset 0x00) get touched at the same time,
2673 resulting in a mixer reset condition persisting until _after_
2674 master vol was restored. Thus master vol needs an extra restore. */
2675 outw(((u16 *)chip->saved_regs_mixer)[1], chip->mixer_io + 2);
2676#endif /* AZF_USE_AC97_LAYER */
2677}
2678
Andreas Mohrca54bde2006-05-17 11:02:24 +02002679static int
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002680snd_azf3328_suspend(struct device *dev)
Andreas Mohrca54bde2006-05-17 11:02:24 +02002681{
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002682 struct snd_card *card = dev_get_drvdata(dev);
Andreas Mohrca54bde2006-05-17 11:02:24 +02002683 struct snd_azf3328 *chip = card->private_data;
Andreas Mohr78df6172009-07-12 22:17:54 +02002684 u16 *saved_regs_ctrl_u16;
Andreas Mohrca54bde2006-05-17 11:02:24 +02002685
2686 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
Andreas Mohr02330fba2008-05-16 12:18:29 +02002687
Andreas Mohrb5dc20c2011-02-19 00:49:32 +01002688 snd_azf3328_suspend_ac97(chip);
Andreas Mohr02330fba2008-05-16 12:18:29 +02002689
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002690 snd_azf3328_suspend_regs(chip, chip->ctrl_io,
Andreas Mohr78df6172009-07-12 22:17:54 +02002691 ARRAY_SIZE(chip->saved_regs_ctrl), chip->saved_regs_ctrl);
Andreas Mohr627d3e72008-06-23 11:50:47 +02002692
2693 /* manually store the one currently relevant write-only reg, too */
Andreas Mohr78df6172009-07-12 22:17:54 +02002694 saved_regs_ctrl_u16 = (u16 *)chip->saved_regs_ctrl;
2695 saved_regs_ctrl_u16[IDX_IO_6AH / 2] = chip->shadow_reg_ctrl_6AH;
Andreas Mohr627d3e72008-06-23 11:50:47 +02002696
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002697 snd_azf3328_suspend_regs(chip, chip->game_io,
Andreas Mohr78df6172009-07-12 22:17:54 +02002698 ARRAY_SIZE(chip->saved_regs_game), chip->saved_regs_game);
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002699 snd_azf3328_suspend_regs(chip, chip->mpu_io,
Andreas Mohr78df6172009-07-12 22:17:54 +02002700 ARRAY_SIZE(chip->saved_regs_mpu), chip->saved_regs_mpu);
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002701 snd_azf3328_suspend_regs(chip, chip->opl3_io,
Andreas Mohr78df6172009-07-12 22:17:54 +02002702 ARRAY_SIZE(chip->saved_regs_opl3), chip->saved_regs_opl3);
Andreas Mohrca54bde2006-05-17 11:02:24 +02002703 return 0;
2704}
2705
2706static int
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002707snd_azf3328_resume(struct device *dev)
Andreas Mohrca54bde2006-05-17 11:02:24 +02002708{
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002709 struct snd_card *card = dev_get_drvdata(dev);
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002710 const struct snd_azf3328 *chip = card->private_data;
Andreas Mohrca54bde2006-05-17 11:02:24 +02002711
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002712 snd_azf3328_resume_regs(chip, chip->saved_regs_game, chip->game_io,
Andreas Mohr78df6172009-07-12 22:17:54 +02002713 ARRAY_SIZE(chip->saved_regs_game));
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002714 snd_azf3328_resume_regs(chip, chip->saved_regs_mpu, chip->mpu_io,
Andreas Mohr78df6172009-07-12 22:17:54 +02002715 ARRAY_SIZE(chip->saved_regs_mpu));
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002716 snd_azf3328_resume_regs(chip, chip->saved_regs_opl3, chip->opl3_io,
Andreas Mohr78df6172009-07-12 22:17:54 +02002717 ARRAY_SIZE(chip->saved_regs_opl3));
2718
Andreas Mohrb5dc20c2011-02-19 00:49:32 +01002719 snd_azf3328_resume_ac97(chip);
Andreas Mohr78df6172009-07-12 22:17:54 +02002720
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002721 snd_azf3328_resume_regs(chip, chip->saved_regs_ctrl, chip->ctrl_io,
Andreas Mohr78df6172009-07-12 22:17:54 +02002722 ARRAY_SIZE(chip->saved_regs_ctrl));
Andreas Mohrca54bde2006-05-17 11:02:24 +02002723
2724 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
2725 return 0;
2726}
Andreas Mohrca54bde2006-05-17 11:02:24 +02002727
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002728static SIMPLE_DEV_PM_OPS(snd_azf3328_pm, snd_azf3328_suspend, snd_azf3328_resume);
2729#define SND_AZF3328_PM_OPS &snd_azf3328_pm
2730#else
2731#define SND_AZF3328_PM_OPS NULL
Takashi Iwaic7561cd2012-08-14 18:12:04 +02002732#endif /* CONFIG_PM_SLEEP */
Andreas Mohrca54bde2006-05-17 11:02:24 +02002733
Takashi Iwaie9f66d92012-04-24 12:25:00 +02002734static struct pci_driver azf3328_driver = {
Takashi Iwai3733e422011-06-10 16:20:20 +02002735 .name = KBUILD_MODNAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 .id_table = snd_azf3328_ids,
2737 .probe = snd_azf3328_probe,
Bill Pembertone23e7a12012-12-06 12:35:10 -05002738 .remove = snd_azf3328_remove,
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002739 .driver = {
2740 .pm = SND_AZF3328_PM_OPS,
2741 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742};
2743
Takashi Iwaie9f66d92012-04-24 12:25:00 +02002744module_pci_driver(azf3328_driver);