blob: 089050470ff275dc129f0bfe6d28130f639ea6a0 [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");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Fabian Frederickb2fac072016-11-12 23:26:41 +0100200#if IS_REACHABLE(CONFIG_GAMEPORT)
Andreas Mohr02330fba2008-05-16 12:18:29 +0200201#define SUPPORT_GAMEPORT 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202#endif
203
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200204/* === Debug settings ===
205 Further diagnostic functionality than the settings below
Andreas Mohradf59312010-12-27 21:16:43 +0100206 does not need to be provided, since one can easily write a POSIX shell script
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200207 to dump the card's I/O ports (those listed in lspci -v -v):
Andreas Mohradf59312010-12-27 21:16:43 +0100208 dump()
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200209 {
210 local descr=$1; local addr=$2; local count=$3
211
212 echo "${descr}: ${count} @ ${addr}:"
Andreas Mohradf59312010-12-27 21:16:43 +0100213 dd if=/dev/port skip=`printf %d ${addr}` count=${count} bs=1 \
214 2>/dev/null| hexdump -C
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200215 }
216 and then use something like
217 "dump joy200 0x200 8", "dump mpu388 0x388 4", "dump joy 0xb400 8",
218 "dump codec00 0xa800 32", "dump mixer 0xb800 64", "dump synth 0xbc00 8",
219 possibly within a "while true; do ... sleep 1; done" loop.
220 Tweaking ports could be done using
221 VALSTRING="`printf "%02x" $value`"
Andreas Mohradf59312010-12-27 21:16:43 +0100222 printf "\x""$VALSTRING"|dd of=/dev/port seek=`printf %d ${addr}` bs=1 \
223 2>/dev/null
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200224*/
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
227module_param_array(index, int, NULL, 0444);
228MODULE_PARM_DESC(index, "Index value for AZF3328 soundcard.");
229
230static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
231module_param_array(id, charp, NULL, 0444);
232MODULE_PARM_DESC(id, "ID string for AZF3328 soundcard.");
233
Rusty Russella67ff6a2011-12-15 13:49:36 +1030234static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235module_param_array(enable, bool, NULL, 0444);
236MODULE_PARM_DESC(enable, "Enable AZF3328 soundcard.");
237
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200238static int seqtimer_scaling = 128;
239module_param(seqtimer_scaling, int, 0444);
240MODULE_PARM_DESC(seqtimer_scaling, "Set 1024000Hz sequencer timer scale factor (lockup danger!). Default 128.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200242enum snd_azf3328_codec_type {
Andreas Mohradf59312010-12-27 21:16:43 +0100243 /* warning: fixed indices (also used for bitmask checks!) */
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200244 AZF_CODEC_PLAYBACK = 0,
245 AZF_CODEC_CAPTURE = 1,
246 AZF_CODEC_I2S_OUT = 2,
Andreas Mohr02330fba2008-05-16 12:18:29 +0200247};
248
Andreas Mohrda237f32010-12-27 21:17:26 +0100249struct snd_azf3328_codec_data {
250 unsigned long io_base; /* keep first! (avoid offset calc) */
251 unsigned int dma_base; /* helper to avoid an indirection in hotpath */
252 spinlock_t *lock; /* TODO: convert to our own per-codec lock member */
253 struct snd_pcm_substream *substream;
254 bool running;
255 enum snd_azf3328_codec_type type;
256 const char *name;
257};
258
Takashi Iwai95de7762005-11-17 15:02:42 +0100259struct snd_azf3328 {
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200260 /* often-used fields towards beginning, then grouped */
Andreas Mohr02330fba2008-05-16 12:18:29 +0200261
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200262 unsigned long ctrl_io; /* usually 0xb000, size 128 */
Andreas Mohr02330fba2008-05-16 12:18:29 +0200263 unsigned long game_io; /* usually 0xb400, size 8 */
264 unsigned long mpu_io; /* usually 0xb800, size 4 */
265 unsigned long opl3_io; /* usually 0xbc00, size 8 */
266 unsigned long mixer_io; /* usually 0xc000, size 64 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200268 spinlock_t reg_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Takashi Iwai95de7762005-11-17 15:02:42 +0100270 struct snd_timer *timer;
Andreas Mohr02330fba2008-05-16 12:18:29 +0200271
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200272 struct snd_pcm *pcm[3];
273
274 /* playback, recording and I2S out codecs */
275 struct snd_azf3328_codec_data codecs[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Andreas Mohrb5dc20c2011-02-19 00:49:32 +0100277#ifdef AZF_USE_AC97_LAYER
278 struct snd_ac97 *ac97;
279#endif
280
Takashi Iwai95de7762005-11-17 15:02:42 +0100281 struct snd_card *card;
282 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Andreas Mohr02330fba2008-05-16 12:18:29 +0200284#ifdef SUPPORT_GAMEPORT
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200285 struct gameport *gameport;
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200286 u16 axes[4];
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200287#endif
288
289 struct pci_dev *pci;
290 int irq;
Andreas Mohrca54bde2006-05-17 11:02:24 +0200291
Andreas Mohr627d3e72008-06-23 11:50:47 +0200292 /* register 0x6a is write-only, thus need to remember setting.
293 * If we need to add more registers here, then we might try to fold this
294 * into some transparent combined shadow register handling with
295 * CONFIG_PM register storage below, but that's slightly difficult. */
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200296 u16 shadow_reg_ctrl_6AH;
Andreas Mohr627d3e72008-06-23 11:50:47 +0200297
Takashi Iwaic7561cd2012-08-14 18:12:04 +0200298#ifdef CONFIG_PM_SLEEP
Andreas Mohrca54bde2006-05-17 11:02:24 +0200299 /* register value containers for power management
Andreas Mohr78df6172009-07-12 22:17:54 +0200300 * Note: not always full I/O range preserved (similar to Win driver!) */
301 u32 saved_regs_ctrl[AZF_ALIGN(AZF_IO_SIZE_CTRL_PM) / 4];
302 u32 saved_regs_game[AZF_ALIGN(AZF_IO_SIZE_GAME_PM) / 4];
303 u32 saved_regs_mpu[AZF_ALIGN(AZF_IO_SIZE_MPU_PM) / 4];
304 u32 saved_regs_opl3[AZF_ALIGN(AZF_IO_SIZE_OPL3_PM) / 4];
305 u32 saved_regs_mixer[AZF_ALIGN(AZF_IO_SIZE_MIXER_PM) / 4];
Andreas Mohrca54bde2006-05-17 11:02:24 +0200306#endif
Takashi Iwai95de7762005-11-17 15:02:42 +0100307};
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200308
Benoit Taine9baa3c32014-08-08 15:56:03 +0200309static const struct pci_device_id snd_azf3328_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 { 0x122D, 0x50DC, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, /* PCI168/3328 */
311 { 0x122D, 0x80DA, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, /* 3328 */
312 { 0, }
313};
314
315MODULE_DEVICE_TABLE(pci, snd_azf3328_ids);
316
Andreas Mohr02330fba2008-05-16 12:18:29 +0200317
318static int
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200319snd_azf3328_io_reg_setb(unsigned reg, u8 mask, bool do_set)
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200320{
Andreas Mohradf59312010-12-27 21:16:43 +0100321 /* Well, strictly spoken, the inb/outb sequence isn't atomic
322 and would need locking. However we currently don't care
323 since it potentially complicates matters. */
Andreas Mohr02330fba2008-05-16 12:18:29 +0200324 u8 prev = inb(reg), new;
325
326 new = (do_set) ? (prev|mask) : (prev & ~mask);
327 /* we need to always write the new value no matter whether it differs
328 * or not, since some register bits don't indicate their setting */
329 outb(new, reg);
330 if (new != prev)
331 return 1;
332
333 return 0;
334}
335
Andreas Mohr02330fba2008-05-16 12:18:29 +0200336static inline void
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200337snd_azf3328_codec_outb(const struct snd_azf3328_codec_data *codec,
338 unsigned reg,
339 u8 value
340)
Andreas Mohr02330fba2008-05-16 12:18:29 +0200341{
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200342 outb(value, codec->io_base + reg);
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200343}
344
345static inline u8
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200346snd_azf3328_codec_inb(const struct snd_azf3328_codec_data *codec, unsigned reg)
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200347{
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200348 return inb(codec->io_base + reg);
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200349}
350
351static inline void
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200352snd_azf3328_codec_outw(const struct snd_azf3328_codec_data *codec,
353 unsigned reg,
354 u16 value
355)
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200356{
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200357 outw(value, codec->io_base + reg);
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200358}
359
360static inline u16
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200361snd_azf3328_codec_inw(const struct snd_azf3328_codec_data *codec, unsigned reg)
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200362{
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200363 return inw(codec->io_base + reg);
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200364}
365
366static inline void
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200367snd_azf3328_codec_outl(const struct snd_azf3328_codec_data *codec,
368 unsigned reg,
369 u32 value
370)
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200371{
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200372 outl(value, codec->io_base + reg);
Andreas Mohr02330fba2008-05-16 12:18:29 +0200373}
374
Andreas Mohr689c6912010-12-27 21:17:35 +0100375static inline void
376snd_azf3328_codec_outl_multi(const struct snd_azf3328_codec_data *codec,
377 unsigned reg, const void *buffer, int count
378)
379{
380 unsigned long addr = codec->io_base + reg;
381 if (count) {
382 const u32 *buf = buffer;
383 do {
384 outl(*buf++, addr);
385 addr += 4;
386 } while (--count);
387 }
388}
389
Andreas Mohr02330fba2008-05-16 12:18:29 +0200390static inline u32
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200391snd_azf3328_codec_inl(const struct snd_azf3328_codec_data *codec, unsigned reg)
Andreas Mohr02330fba2008-05-16 12:18:29 +0200392{
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200393 return inl(codec->io_base + reg);
394}
395
396static inline void
397snd_azf3328_ctrl_outb(const struct snd_azf3328 *chip, unsigned reg, u8 value)
398{
399 outb(value, chip->ctrl_io + reg);
400}
401
402static inline u8
403snd_azf3328_ctrl_inb(const struct snd_azf3328 *chip, unsigned reg)
404{
405 return inb(chip->ctrl_io + reg);
406}
407
Takashi Iwai4a8d9d72014-02-25 14:04:46 +0100408static inline u16
409snd_azf3328_ctrl_inw(const struct snd_azf3328 *chip, unsigned reg)
410{
411 return inw(chip->ctrl_io + reg);
412}
413
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200414static inline void
415snd_azf3328_ctrl_outw(const struct snd_azf3328 *chip, unsigned reg, u16 value)
416{
417 outw(value, chip->ctrl_io + reg);
418}
419
420static inline void
421snd_azf3328_ctrl_outl(const struct snd_azf3328 *chip, unsigned reg, u32 value)
422{
423 outl(value, chip->ctrl_io + reg);
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200424}
425
426static inline void
Andreas Mohr02330fba2008-05-16 12:18:29 +0200427snd_azf3328_game_outb(const struct snd_azf3328 *chip, unsigned reg, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
Andreas Mohr02330fba2008-05-16 12:18:29 +0200429 outb(value, chip->game_io + reg);
430}
431
432static inline void
433snd_azf3328_game_outw(const struct snd_azf3328 *chip, unsigned reg, u16 value)
434{
435 outw(value, chip->game_io + reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436}
437
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200438static inline u8
Andreas Mohr02330fba2008-05-16 12:18:29 +0200439snd_azf3328_game_inb(const struct snd_azf3328 *chip, unsigned reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
Andreas Mohr02330fba2008-05-16 12:18:29 +0200441 return inb(chip->game_io + reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
443
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200444static inline u16
Andreas Mohr02330fba2008-05-16 12:18:29 +0200445snd_azf3328_game_inw(const struct snd_azf3328 *chip, unsigned reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
Andreas Mohr02330fba2008-05-16 12:18:29 +0200447 return inw(chip->game_io + reg);
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200448}
449
Andreas Mohr02330fba2008-05-16 12:18:29 +0200450static inline void
451snd_azf3328_mixer_outw(const struct snd_azf3328 *chip, unsigned reg, u16 value)
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200452{
Andreas Mohr02330fba2008-05-16 12:18:29 +0200453 outw(value, chip->mixer_io + reg);
454}
455
456static inline u16
457snd_azf3328_mixer_inw(const struct snd_azf3328 *chip, unsigned reg)
458{
459 return inw(chip->mixer_io + reg);
460}
461
462#define AZF_MUTE_BIT 0x80
463
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200464static bool
Andreas Mohrb5dc20c2011-02-19 00:49:32 +0100465snd_azf3328_mixer_mute_control(const struct snd_azf3328 *chip,
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200466 unsigned reg, bool do_mute
Andreas Mohr02330fba2008-05-16 12:18:29 +0200467)
468{
469 unsigned long portbase = chip->mixer_io + reg + 1;
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200470 bool updated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 /* the mute bit is on the *second* (i.e. right) register of a
473 * left/right channel setting */
Andreas Mohr02330fba2008-05-16 12:18:29 +0200474 updated = snd_azf3328_io_reg_setb(portbase, AZF_MUTE_BIT, do_mute);
475
476 /* indicate whether it was muted before */
477 return (do_mute) ? !updated : updated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478}
479
Andreas Mohrb5dc20c2011-02-19 00:49:32 +0100480static inline bool
481snd_azf3328_mixer_mute_control_master(const struct snd_azf3328 *chip,
482 bool do_mute
483)
484{
485 return snd_azf3328_mixer_mute_control(
486 chip,
487 IDX_MIXER_PLAY_MASTER,
488 do_mute
489 );
490}
491
492static inline bool
493snd_azf3328_mixer_mute_control_pcm(const struct snd_azf3328 *chip,
494 bool do_mute
495)
496{
497 return snd_azf3328_mixer_mute_control(
498 chip,
499 IDX_MIXER_WAVEOUT,
500 do_mute
501 );
502}
503
504static inline void
505snd_azf3328_mixer_reset(const struct snd_azf3328 *chip)
506{
507 /* reset (close) mixer:
508 * first mute master volume, then reset
509 */
510 snd_azf3328_mixer_mute_control_master(chip, 1);
511 snd_azf3328_mixer_outw(chip, IDX_MIXER_RESET, 0x0000);
512}
513
514#ifdef AZF_USE_AC97_LAYER
515
516static inline void
Takashi Iwai4a8d9d72014-02-25 14:04:46 +0100517snd_azf3328_mixer_ac97_map_unsupported(const struct snd_azf3328 *chip,
518 unsigned short reg, const char *mode)
Andreas Mohrb5dc20c2011-02-19 00:49:32 +0100519{
520 /* need to add some more or less clever emulation? */
Takashi Iwai4a8d9d72014-02-25 14:04:46 +0100521 dev_warn(chip->card->dev,
522 "missing %s emulation for AC97 register 0x%02x!\n",
Andreas Mohrb5dc20c2011-02-19 00:49:32 +0100523 mode, reg);
524}
525
526/*
527 * Need to have _special_ AC97 mixer hardware register index mapper,
528 * to compensate for the issue of a rather AC97-incompatible hardware layout.
529 */
530#define AZF_REG_MASK 0x3f
531#define AZF_AC97_REG_UNSUPPORTED 0x8000
532#define AZF_AC97_REG_REAL_IO_READ 0x4000
533#define AZF_AC97_REG_REAL_IO_WRITE 0x2000
534#define AZF_AC97_REG_REAL_IO_RW \
535 (AZF_AC97_REG_REAL_IO_READ | AZF_AC97_REG_REAL_IO_WRITE)
536#define AZF_AC97_REG_EMU_IO_READ 0x0400
537#define AZF_AC97_REG_EMU_IO_WRITE 0x0200
538#define AZF_AC97_REG_EMU_IO_RW \
539 (AZF_AC97_REG_EMU_IO_READ | AZF_AC97_REG_EMU_IO_WRITE)
540static unsigned short
541snd_azf3328_mixer_ac97_map_reg_idx(unsigned short reg)
542{
543 static const struct {
544 unsigned short azf_reg;
545 } azf_reg_mapper[] = {
546 /* Especially when taking into consideration
547 * mono/stereo-based sequence of azf vs. AC97 control series,
548 * it's quite obvious that azf simply got rid
549 * of the AC97_HEADPHONE control at its intended offset,
550 * thus shifted _all_ controls by one,
551 * and _then_ simply added it as an FMSYNTH control at the end,
552 * to make up for the offset.
553 * This means we'll have to translate indices here as
554 * needed and then do some tiny AC97 patch action
555 * (snd_ac97_rename_vol_ctl() etc.) - that's it.
556 */
557 { /* AC97_RESET */ IDX_MIXER_RESET
558 | AZF_AC97_REG_REAL_IO_WRITE
559 | AZF_AC97_REG_EMU_IO_READ },
560 { /* AC97_MASTER */ IDX_MIXER_PLAY_MASTER },
561 /* note large shift: AC97_HEADPHONE to IDX_MIXER_FMSYNTH! */
562 { /* AC97_HEADPHONE */ IDX_MIXER_FMSYNTH },
563 { /* AC97_MASTER_MONO */ IDX_MIXER_MODEMOUT },
564 { /* AC97_MASTER_TONE */ IDX_MIXER_BASSTREBLE },
565 { /* AC97_PC_BEEP */ IDX_MIXER_PCBEEP },
566 { /* AC97_PHONE */ IDX_MIXER_MODEMIN },
567 { /* AC97_MIC */ IDX_MIXER_MIC },
568 { /* AC97_LINE */ IDX_MIXER_LINEIN },
569 { /* AC97_CD */ IDX_MIXER_CDAUDIO },
570 { /* AC97_VIDEO */ IDX_MIXER_VIDEO },
571 { /* AC97_AUX */ IDX_MIXER_AUX },
572 { /* AC97_PCM */ IDX_MIXER_WAVEOUT },
573 { /* AC97_REC_SEL */ IDX_MIXER_REC_SELECT },
574 { /* AC97_REC_GAIN */ IDX_MIXER_REC_VOLUME },
575 { /* AC97_REC_GAIN_MIC */ AZF_AC97_REG_EMU_IO_RW },
576 { /* AC97_GENERAL_PURPOSE */ IDX_MIXER_ADVCTL2 },
577 { /* AC97_3D_CONTROL */ IDX_MIXER_ADVCTL1 },
578 };
579
580 unsigned short reg_azf = AZF_AC97_REG_UNSUPPORTED;
581
582 /* azf3328 supports the low-numbered and low-spec:ed range
583 of AC97 regs only */
584 if (reg <= AC97_3D_CONTROL) {
585 unsigned short reg_idx = reg / 2;
586 reg_azf = azf_reg_mapper[reg_idx].azf_reg;
587 /* a translation-only entry means it's real read/write: */
588 if (!(reg_azf & ~AZF_REG_MASK))
589 reg_azf |= AZF_AC97_REG_REAL_IO_RW;
590 } else {
591 switch (reg) {
592 case AC97_POWERDOWN:
593 reg_azf = AZF_AC97_REG_EMU_IO_RW;
594 break;
595 case AC97_EXTENDED_ID:
596 reg_azf = AZF_AC97_REG_EMU_IO_READ;
597 break;
598 case AC97_EXTENDED_STATUS:
599 /* I don't know what the h*ll AC97 layer
600 * would consult this _extended_ register for
601 * given a base-AC97-advertised card,
602 * but let's just emulate it anyway :-P
603 */
604 reg_azf = AZF_AC97_REG_EMU_IO_RW;
605 break;
606 case AC97_VENDOR_ID1:
607 case AC97_VENDOR_ID2:
608 reg_azf = AZF_AC97_REG_EMU_IO_READ;
609 break;
610 }
611 }
612 return reg_azf;
613}
614
615static const unsigned short
616azf_emulated_ac97_caps =
617 AC97_BC_DEDICATED_MIC |
618 AC97_BC_BASS_TREBLE |
619 /* Headphone is an FM Synth control here */
620 AC97_BC_HEADPHONE |
621 /* no AC97_BC_LOUDNESS! */
622 /* mask 0x7c00 is
623 vendor-specific 3D enhancement
624 vendor indicator.
625 Since there actually _is_ an
626 entry for Aztech Labs
627 (13), make damn sure
628 to indicate it. */
629 (13 << 10);
630
631static const unsigned short
632azf_emulated_ac97_powerdown =
633 /* pretend everything to be active */
634 AC97_PD_ADC_STATUS |
635 AC97_PD_DAC_STATUS |
636 AC97_PD_MIXER_STATUS |
637 AC97_PD_VREF_STATUS;
638
639/*
640 * Emulated, _inofficial_ vendor ID
641 * (there might be some devices such as the MR 2800-W
642 * which could reveal the real Aztech AC97 ID).
643 * We choose to use "AZT" prefix, and then use 1 to indicate PCI168
644 * (better don't use 0x68 since there's a PCI368 as well).
645 */
646static const unsigned int
647azf_emulated_ac97_vendor_id = 0x415a5401;
648
649static unsigned short
650snd_azf3328_mixer_ac97_read(struct snd_ac97 *ac97, unsigned short reg_ac97)
651{
652 const struct snd_azf3328 *chip = ac97->private_data;
653 unsigned short reg_azf = snd_azf3328_mixer_ac97_map_reg_idx(reg_ac97);
654 unsigned short reg_val = 0;
Peter Senna Tschudine0f17c72013-09-22 20:44:12 +0200655 bool unsupported = false;
Andreas Mohrb5dc20c2011-02-19 00:49:32 +0100656
Takashi Iwai4a8d9d72014-02-25 14:04:46 +0100657 dev_dbg(chip->card->dev, "snd_azf3328_mixer_ac97_read reg_ac97 %u\n",
658 reg_ac97);
Andreas Mohrb5dc20c2011-02-19 00:49:32 +0100659 if (reg_azf & AZF_AC97_REG_UNSUPPORTED)
Peter Senna Tschudine0f17c72013-09-22 20:44:12 +0200660 unsupported = true;
Andreas Mohrb5dc20c2011-02-19 00:49:32 +0100661 else {
662 if (reg_azf & AZF_AC97_REG_REAL_IO_READ)
663 reg_val = snd_azf3328_mixer_inw(chip,
664 reg_azf & AZF_REG_MASK);
665 else {
666 /*
667 * Proceed with dummy I/O read,
668 * to ensure compatible timing where this may matter.
669 * (ALSA AC97 layer usually doesn't call I/O functions
670 * due to intelligent I/O caching anyway)
671 * Choose a mixer register that's thoroughly unrelated
672 * to common audio (try to minimize distortion).
673 */
674 snd_azf3328_mixer_inw(chip, IDX_MIXER_SOMETHING30H);
675 }
676
677 if (reg_azf & AZF_AC97_REG_EMU_IO_READ) {
678 switch (reg_ac97) {
679 case AC97_RESET:
680 reg_val |= azf_emulated_ac97_caps;
681 break;
682 case AC97_POWERDOWN:
683 reg_val |= azf_emulated_ac97_powerdown;
684 break;
685 case AC97_EXTENDED_ID:
686 case AC97_EXTENDED_STATUS:
687 /* AFAICS we simply can't support anything: */
688 reg_val |= 0;
689 break;
690 case AC97_VENDOR_ID1:
691 reg_val = azf_emulated_ac97_vendor_id >> 16;
692 break;
693 case AC97_VENDOR_ID2:
694 reg_val = azf_emulated_ac97_vendor_id & 0xffff;
695 break;
696 default:
Peter Senna Tschudine0f17c72013-09-22 20:44:12 +0200697 unsupported = true;
Andreas Mohrb5dc20c2011-02-19 00:49:32 +0100698 break;
699 }
700 }
701 }
702 if (unsupported)
Takashi Iwai4a8d9d72014-02-25 14:04:46 +0100703 snd_azf3328_mixer_ac97_map_unsupported(chip, reg_ac97, "read");
Andreas Mohrb5dc20c2011-02-19 00:49:32 +0100704
705 return reg_val;
706}
707
708static void
709snd_azf3328_mixer_ac97_write(struct snd_ac97 *ac97,
710 unsigned short reg_ac97, unsigned short val)
711{
712 const struct snd_azf3328 *chip = ac97->private_data;
713 unsigned short reg_azf = snd_azf3328_mixer_ac97_map_reg_idx(reg_ac97);
Peter Senna Tschudine0f17c72013-09-22 20:44:12 +0200714 bool unsupported = false;
Andreas Mohrb5dc20c2011-02-19 00:49:32 +0100715
Takashi Iwai4a8d9d72014-02-25 14:04:46 +0100716 dev_dbg(chip->card->dev,
Andreas Mohrb5dc20c2011-02-19 00:49:32 +0100717 "snd_azf3328_mixer_ac97_write reg_ac97 %u val %u\n",
Takashi Iwai4a8d9d72014-02-25 14:04:46 +0100718 reg_ac97, val);
Andreas Mohrb5dc20c2011-02-19 00:49:32 +0100719 if (reg_azf & AZF_AC97_REG_UNSUPPORTED)
Peter Senna Tschudine0f17c72013-09-22 20:44:12 +0200720 unsupported = true;
Andreas Mohrb5dc20c2011-02-19 00:49:32 +0100721 else {
722 if (reg_azf & AZF_AC97_REG_REAL_IO_WRITE)
723 snd_azf3328_mixer_outw(
724 chip,
725 reg_azf & AZF_REG_MASK,
726 val
727 );
728 else
729 if (reg_azf & AZF_AC97_REG_EMU_IO_WRITE) {
730 switch (reg_ac97) {
731 case AC97_REC_GAIN_MIC:
732 case AC97_POWERDOWN:
733 case AC97_EXTENDED_STATUS:
734 /*
735 * Silently swallow these writes.
736 * Since for most registers our card doesn't
737 * actually support a comparable feature,
738 * this is exactly what we should do here.
739 * The AC97 layer's I/O caching probably
740 * automatically takes care of all the rest...
741 * (remembers written values etc.)
742 */
743 break;
744 default:
Peter Senna Tschudine0f17c72013-09-22 20:44:12 +0200745 unsupported = true;
Andreas Mohrb5dc20c2011-02-19 00:49:32 +0100746 break;
747 }
748 }
749 }
750 if (unsupported)
Takashi Iwai4a8d9d72014-02-25 14:04:46 +0100751 snd_azf3328_mixer_ac97_map_unsupported(chip, reg_ac97, "write");
Andreas Mohrb5dc20c2011-02-19 00:49:32 +0100752}
753
Bill Pembertone23e7a12012-12-06 12:35:10 -0500754static int
Andreas Mohrb5dc20c2011-02-19 00:49:32 +0100755snd_azf3328_mixer_new(struct snd_azf3328 *chip)
756{
757 struct snd_ac97_bus *bus;
758 struct snd_ac97_template ac97;
Takashi Iwai51055da2020-01-03 09:16:43 +0100759 static const struct snd_ac97_bus_ops ops = {
Andreas Mohrb5dc20c2011-02-19 00:49:32 +0100760 .write = snd_azf3328_mixer_ac97_write,
761 .read = snd_azf3328_mixer_ac97_read,
762 };
763 int rc;
764
765 memset(&ac97, 0, sizeof(ac97));
766 ac97.scaps = AC97_SCAP_SKIP_MODEM
767 | AC97_SCAP_AUDIO /* we support audio! */
768 | AC97_SCAP_NO_SPDIF;
769 ac97.private_data = chip;
770 ac97.pci = chip->pci;
771
772 /*
773 * ALSA's AC97 layer has terrible init crackling issues,
774 * unfortunately, and since it makes use of AC97_RESET,
775 * there's no use trying to mute Master Playback proactively.
776 */
777
778 rc = snd_ac97_bus(chip->card, 0, &ops, NULL, &bus);
779 if (!rc)
780 rc = snd_ac97_mixer(bus, &ac97, &chip->ac97);
781 /*
782 * Make sure to complain loudly in case of AC97 init failure,
783 * since failure may happen quite often,
784 * due to this card being a very quirky AC97 "lookalike".
785 */
786 if (rc)
Takashi Iwai4a8d9d72014-02-25 14:04:46 +0100787 dev_err(chip->card->dev, "AC97 init failed, err %d!\n", rc);
Andreas Mohrb5dc20c2011-02-19 00:49:32 +0100788
789 /* If we return an error here, then snd_card_free() should
790 * free up any ac97 codecs that got created, as well as the bus.
791 */
792 return rc;
793}
794#else /* AZF_USE_AC97_LAYER */
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200795static void
Andreas Mohr02330fba2008-05-16 12:18:29 +0200796snd_azf3328_mixer_write_volume_gradually(const struct snd_azf3328 *chip,
797 unsigned reg,
798 unsigned char dst_vol_left,
799 unsigned char dst_vol_right,
800 int chan_sel, int delay
801)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
Andreas Mohr02330fba2008-05-16 12:18:29 +0200803 unsigned long portbase = chip->mixer_io + reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 unsigned char curr_vol_left = 0, curr_vol_right = 0;
Andreas Mohr02330fba2008-05-16 12:18:29 +0200805 int left_change = 0, right_change = 0;
806
Andreas Mohr02330fba2008-05-16 12:18:29 +0200807 if (chan_sel & SET_CHAN_LEFT) {
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200808 curr_vol_left = inb(portbase + 1);
Andreas Mohr02330fba2008-05-16 12:18:29 +0200809
810 /* take care of muting flag contained in left channel */
811 if (curr_vol_left & AZF_MUTE_BIT)
812 dst_vol_left |= AZF_MUTE_BIT;
813 else
814 dst_vol_left &= ~AZF_MUTE_BIT;
815
816 left_change = (curr_vol_left > dst_vol_left) ? -1 : 1;
817 }
818
819 if (chan_sel & SET_CHAN_RIGHT) {
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200820 curr_vol_right = inb(portbase + 0);
Andreas Mohr02330fba2008-05-16 12:18:29 +0200821
822 right_change = (curr_vol_right > dst_vol_right) ? -1 : 1;
823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Andreas Mohre2f87262006-05-17 11:04:19 +0200825 do {
Andreas Mohr02330fba2008-05-16 12:18:29 +0200826 if (left_change) {
827 if (curr_vol_left != dst_vol_left) {
828 curr_vol_left += left_change;
829 outb(curr_vol_left, portbase + 1);
830 } else
831 left_change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 }
Andreas Mohr02330fba2008-05-16 12:18:29 +0200833 if (right_change) {
834 if (curr_vol_right != dst_vol_right) {
835 curr_vol_right += right_change;
836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 /* during volume change, the right channel is crackling
838 * somewhat more than the left channel, unfortunately.
839 * This seems to be a hardware issue. */
Andreas Mohr02330fba2008-05-16 12:18:29 +0200840 outb(curr_vol_right, portbase + 0);
841 } else
842 right_change = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 }
844 if (delay)
845 mdelay(delay);
Andreas Mohr02330fba2008-05-16 12:18:29 +0200846 } while ((left_change) || (right_change));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847}
848
849/*
850 * general mixer element
851 */
Takashi Iwai95de7762005-11-17 15:02:42 +0100852struct azf3328_mixer_reg {
Andreas Mohr02330fba2008-05-16 12:18:29 +0200853 unsigned reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 unsigned int lchan_shift, rchan_shift;
855 unsigned int mask;
856 unsigned int invert: 1;
857 unsigned int stereo: 1;
858 unsigned int enum_c: 4;
Takashi Iwai95de7762005-11-17 15:02:42 +0100859};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
861#define COMPOSE_MIXER_REG(reg,lchan_shift,rchan_shift,mask,invert,stereo,enum_c) \
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200862 ((reg) | (lchan_shift << 8) | (rchan_shift << 12) | \
863 (mask << 16) | \
864 (invert << 24) | \
865 (stereo << 25) | \
866 (enum_c << 26))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Takashi Iwai95de7762005-11-17 15:02:42 +0100868static void snd_azf3328_mixer_reg_decode(struct azf3328_mixer_reg *r, unsigned long val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
870 r->reg = val & 0xff;
871 r->lchan_shift = (val >> 8) & 0x0f;
872 r->rchan_shift = (val >> 12) & 0x0f;
873 r->mask = (val >> 16) & 0xff;
874 r->invert = (val >> 24) & 1;
875 r->stereo = (val >> 25) & 1;
876 r->enum_c = (val >> 26) & 0x0f;
877}
878
879/*
880 * mixer switches/volumes
881 */
882
883#define AZF3328_MIXER_SWITCH(xname, reg, shift, invert) \
884{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
885 .info = snd_azf3328_info_mixer, \
886 .get = snd_azf3328_get_mixer, .put = snd_azf3328_put_mixer, \
887 .private_value = COMPOSE_MIXER_REG(reg, shift, 0, 0x1, invert, 0, 0), \
888}
889
890#define AZF3328_MIXER_VOL_STEREO(xname, reg, mask, invert) \
891{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
892 .info = snd_azf3328_info_mixer, \
893 .get = snd_azf3328_get_mixer, .put = snd_azf3328_put_mixer, \
894 .private_value = COMPOSE_MIXER_REG(reg, 8, 0, mask, invert, 1, 0), \
895}
896
897#define AZF3328_MIXER_VOL_MONO(xname, reg, mask, is_right_chan) \
898{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
899 .info = snd_azf3328_info_mixer, \
900 .get = snd_azf3328_get_mixer, .put = snd_azf3328_put_mixer, \
901 .private_value = COMPOSE_MIXER_REG(reg, is_right_chan ? 0 : 8, 0, mask, 1, 0, 0), \
902}
903
904#define AZF3328_MIXER_VOL_SPECIAL(xname, reg, mask, shift, invert) \
905{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
906 .info = snd_azf3328_info_mixer, \
907 .get = snd_azf3328_get_mixer, .put = snd_azf3328_put_mixer, \
908 .private_value = COMPOSE_MIXER_REG(reg, shift, 0, mask, invert, 0, 0), \
909}
910
911#define AZF3328_MIXER_ENUM(xname, reg, enum_c, shift) \
912{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
913 .info = snd_azf3328_info_mixer_enum, \
914 .get = snd_azf3328_get_mixer_enum, .put = snd_azf3328_put_mixer_enum, \
915 .private_value = COMPOSE_MIXER_REG(reg, shift, 0, 0, 0, 0, enum_c), \
916}
917
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200918static int
Takashi Iwai95de7762005-11-17 15:02:42 +0100919snd_azf3328_info_mixer(struct snd_kcontrol *kcontrol,
920 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
Takashi Iwai95de7762005-11-17 15:02:42 +0100922 struct azf3328_mixer_reg reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 snd_azf3328_mixer_reg_decode(&reg, kcontrol->private_value);
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200925 uinfo->type = reg.mask == 1 ?
926 SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 uinfo->count = reg.stereo + 1;
928 uinfo->value.integer.min = 0;
929 uinfo->value.integer.max = reg.mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 return 0;
931}
932
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200933static int
Takashi Iwai95de7762005-11-17 15:02:42 +0100934snd_azf3328_get_mixer(struct snd_kcontrol *kcontrol,
935 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
Takashi Iwai95de7762005-11-17 15:02:42 +0100937 struct snd_azf3328 *chip = snd_kcontrol_chip(kcontrol);
938 struct azf3328_mixer_reg reg;
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200939 u16 oreg, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 snd_azf3328_mixer_reg_decode(&reg, kcontrol->private_value);
942
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200943 oreg = snd_azf3328_mixer_inw(chip, reg.reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 val = (oreg >> reg.lchan_shift) & reg.mask;
945 if (reg.invert)
946 val = reg.mask - val;
947 ucontrol->value.integer.value[0] = val;
948 if (reg.stereo) {
949 val = (oreg >> reg.rchan_shift) & reg.mask;
950 if (reg.invert)
951 val = reg.mask - val;
952 ucontrol->value.integer.value[1] = val;
953 }
Takashi Iwai4a8d9d72014-02-25 14:04:46 +0100954 dev_dbg(chip->card->dev,
955 "get: %02x is %04x -> vol %02lx|%02lx (shift %02d|%02d, mask %02x, inv. %d, stereo %d)\n",
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200956 reg.reg, oreg,
957 ucontrol->value.integer.value[0], ucontrol->value.integer.value[1],
958 reg.lchan_shift, reg.rchan_shift, reg.mask, reg.invert, reg.stereo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 return 0;
960}
961
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200962static int
Takashi Iwai95de7762005-11-17 15:02:42 +0100963snd_azf3328_put_mixer(struct snd_kcontrol *kcontrol,
964 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
Takashi Iwai95de7762005-11-17 15:02:42 +0100966 struct snd_azf3328 *chip = snd_kcontrol_chip(kcontrol);
967 struct azf3328_mixer_reg reg;
Andreas Mohrdfbf9512009-07-05 13:55:46 +0200968 u16 oreg, nreg, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 snd_azf3328_mixer_reg_decode(&reg, kcontrol->private_value);
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200971 oreg = snd_azf3328_mixer_inw(chip, reg.reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 val = ucontrol->value.integer.value[0] & reg.mask;
973 if (reg.invert)
974 val = reg.mask - val;
975 nreg = oreg & ~(reg.mask << reg.lchan_shift);
976 nreg |= (val << reg.lchan_shift);
977 if (reg.stereo) {
978 val = ucontrol->value.integer.value[1] & reg.mask;
979 if (reg.invert)
980 val = reg.mask - val;
981 nreg &= ~(reg.mask << reg.rchan_shift);
982 nreg |= (val << reg.rchan_shift);
983 }
984 if (reg.mask >= 0x07) /* it's a volume control, so better take care */
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200985 snd_azf3328_mixer_write_volume_gradually(
986 chip, reg.reg, nreg >> 8, nreg & 0xff,
987 /* just set both channels, doesn't matter */
988 SET_CHAN_LEFT|SET_CHAN_RIGHT,
989 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 else
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200991 snd_azf3328_mixer_outw(chip, reg.reg, nreg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Takashi Iwai4a8d9d72014-02-25 14:04:46 +0100993 dev_dbg(chip->card->dev,
994 "put: %02x to %02lx|%02lx, oreg %04x; shift %02d|%02d -> nreg %04x; after: %04x\n",
Andreas Mohrd91c64c2005-10-25 11:17:45 +0200995 reg.reg, ucontrol->value.integer.value[0], ucontrol->value.integer.value[1],
996 oreg, reg.lchan_shift, reg.rchan_shift,
997 nreg, snd_azf3328_mixer_inw(chip, reg.reg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 return (nreg != oreg);
999}
1000
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001001static int
Takashi Iwai95de7762005-11-17 15:02:42 +01001002snd_azf3328_info_mixer_enum(struct snd_kcontrol *kcontrol,
1003 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001005 static const char * const texts1[] = {
Andreas Mohr13769e32006-05-17 11:03:16 +02001006 "Mic1", "Mic2"
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001007 };
1008 static const char * const texts2[] = {
Andreas Mohr13769e32006-05-17 11:03:16 +02001009 "Mix", "Mic"
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001010 };
1011 static const char * const texts3[] = {
Andreas Mohr02330fba2008-05-16 12:18:29 +02001012 "Mic", "CD", "Video", "Aux",
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001013 "Line", "Mix", "Mix Mono", "Phone"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 };
Andreas Mohr13769e32006-05-17 11:03:16 +02001015 static const char * const texts4[] = {
1016 "pre 3D", "post 3D"
1017 };
Takashi Iwai95de7762005-11-17 15:02:42 +01001018 struct azf3328_mixer_reg reg;
Andreas Mohr627d3e72008-06-23 11:50:47 +02001019 const char * const *p = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
1021 snd_azf3328_mixer_reg_decode(&reg, kcontrol->private_value);
Andreas Mohre2f87262006-05-17 11:04:19 +02001022 if (reg.reg == IDX_MIXER_ADVCTL2) {
Andreas Mohr13769e32006-05-17 11:03:16 +02001023 switch(reg.lchan_shift) {
1024 case 8: /* modem out sel */
Andreas Mohr627d3e72008-06-23 11:50:47 +02001025 p = texts1;
Andreas Mohr13769e32006-05-17 11:03:16 +02001026 break;
1027 case 9: /* mono sel source */
Andreas Mohr627d3e72008-06-23 11:50:47 +02001028 p = texts2;
Andreas Mohr13769e32006-05-17 11:03:16 +02001029 break;
1030 case 15: /* PCM Out Path */
Andreas Mohr627d3e72008-06-23 11:50:47 +02001031 p = texts4;
Andreas Mohr13769e32006-05-17 11:03:16 +02001032 break;
1033 }
Takashi Iwai9b311a02014-10-20 18:16:13 +02001034 } else if (reg.reg == IDX_MIXER_REC_SELECT)
Andreas Mohr627d3e72008-06-23 11:50:47 +02001035 p = texts3;
Andreas Mohr02330fba2008-05-16 12:18:29 +02001036
Takashi Iwai9b311a02014-10-20 18:16:13 +02001037 return snd_ctl_enum_info(uinfo,
1038 (reg.reg == IDX_MIXER_REC_SELECT) ? 2 : 1,
1039 reg.enum_c, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040}
1041
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001042static int
Takashi Iwai95de7762005-11-17 15:02:42 +01001043snd_azf3328_get_mixer_enum(struct snd_kcontrol *kcontrol,
1044 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045{
Takashi Iwai95de7762005-11-17 15:02:42 +01001046 struct snd_azf3328 *chip = snd_kcontrol_chip(kcontrol);
1047 struct azf3328_mixer_reg reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 unsigned short val;
Andreas Mohr02330fba2008-05-16 12:18:29 +02001049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 snd_azf3328_mixer_reg_decode(&reg, kcontrol->private_value);
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001051 val = snd_azf3328_mixer_inw(chip, reg.reg);
Andreas Mohre2f87262006-05-17 11:04:19 +02001052 if (reg.reg == IDX_MIXER_REC_SELECT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 ucontrol->value.enumerated.item[0] = (val >> 8) & (reg.enum_c - 1);
1054 ucontrol->value.enumerated.item[1] = (val >> 0) & (reg.enum_c - 1);
Andreas Mohre2f87262006-05-17 11:04:19 +02001055 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 ucontrol->value.enumerated.item[0] = (val >> reg.lchan_shift) & (reg.enum_c - 1);
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001057
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001058 dev_dbg(chip->card->dev,
1059 "get_enum: %02x is %04x -> %d|%d (shift %02d, enum_c %d)\n",
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001060 reg.reg, val, ucontrol->value.enumerated.item[0], ucontrol->value.enumerated.item[1],
1061 reg.lchan_shift, reg.enum_c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 return 0;
1063}
1064
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001065static int
Takashi Iwai95de7762005-11-17 15:02:42 +01001066snd_azf3328_put_mixer_enum(struct snd_kcontrol *kcontrol,
1067 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
Takashi Iwai95de7762005-11-17 15:02:42 +01001069 struct snd_azf3328 *chip = snd_kcontrol_chip(kcontrol);
1070 struct azf3328_mixer_reg reg;
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001071 u16 oreg, nreg, val;
Andreas Mohr02330fba2008-05-16 12:18:29 +02001072
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 snd_azf3328_mixer_reg_decode(&reg, kcontrol->private_value);
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001074 oreg = snd_azf3328_mixer_inw(chip, reg.reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 val = oreg;
Andreas Mohre2f87262006-05-17 11:04:19 +02001076 if (reg.reg == IDX_MIXER_REC_SELECT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 if (ucontrol->value.enumerated.item[0] > reg.enum_c - 1U ||
1078 ucontrol->value.enumerated.item[1] > reg.enum_c - 1U)
1079 return -EINVAL;
1080 val = (ucontrol->value.enumerated.item[0] << 8) |
1081 (ucontrol->value.enumerated.item[1] << 0);
Andreas Mohre2f87262006-05-17 11:04:19 +02001082 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 if (ucontrol->value.enumerated.item[0] > reg.enum_c - 1U)
1084 return -EINVAL;
1085 val &= ~((reg.enum_c - 1) << reg.lchan_shift);
1086 val |= (ucontrol->value.enumerated.item[0] << reg.lchan_shift);
1087 }
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001088 snd_azf3328_mixer_outw(chip, reg.reg, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 nreg = val;
1090
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001091 dev_dbg(chip->card->dev,
1092 "put_enum: %02x to %04x, oreg %04x\n", reg.reg, val, oreg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 return (nreg != oreg);
1094}
1095
Takashi Iwaib4e5e702020-01-03 09:16:53 +01001096static const struct snd_kcontrol_new snd_azf3328_mixer_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 AZF3328_MIXER_SWITCH("Master Playback Switch", IDX_MIXER_PLAY_MASTER, 15, 1),
1098 AZF3328_MIXER_VOL_STEREO("Master Playback Volume", IDX_MIXER_PLAY_MASTER, 0x1f, 1),
Andreas Mohr627d3e72008-06-23 11:50:47 +02001099 AZF3328_MIXER_SWITCH("PCM Playback Switch", IDX_MIXER_WAVEOUT, 15, 1),
1100 AZF3328_MIXER_VOL_STEREO("PCM Playback Volume",
1101 IDX_MIXER_WAVEOUT, 0x1f, 1),
1102 AZF3328_MIXER_SWITCH("PCM 3D Bypass Playback Switch",
1103 IDX_MIXER_ADVCTL2, 7, 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 AZF3328_MIXER_SWITCH("FM Playback Switch", IDX_MIXER_FMSYNTH, 15, 1),
1105 AZF3328_MIXER_VOL_STEREO("FM Playback Volume", IDX_MIXER_FMSYNTH, 0x1f, 1),
1106 AZF3328_MIXER_SWITCH("CD Playback Switch", IDX_MIXER_CDAUDIO, 15, 1),
1107 AZF3328_MIXER_VOL_STEREO("CD Playback Volume", IDX_MIXER_CDAUDIO, 0x1f, 1),
1108 AZF3328_MIXER_SWITCH("Capture Switch", IDX_MIXER_REC_VOLUME, 15, 1),
1109 AZF3328_MIXER_VOL_STEREO("Capture Volume", IDX_MIXER_REC_VOLUME, 0x0f, 0),
1110 AZF3328_MIXER_ENUM("Capture Source", IDX_MIXER_REC_SELECT, 8, 0),
1111 AZF3328_MIXER_SWITCH("Mic Playback Switch", IDX_MIXER_MIC, 15, 1),
1112 AZF3328_MIXER_VOL_MONO("Mic Playback Volume", IDX_MIXER_MIC, 0x1f, 1),
1113 AZF3328_MIXER_SWITCH("Mic Boost (+20dB)", IDX_MIXER_MIC, 6, 0),
1114 AZF3328_MIXER_SWITCH("Line Playback Switch", IDX_MIXER_LINEIN, 15, 1),
1115 AZF3328_MIXER_VOL_STEREO("Line Playback Volume", IDX_MIXER_LINEIN, 0x1f, 1),
Jaroslav Kyselad355c82a2009-11-03 15:47:25 +01001116 AZF3328_MIXER_SWITCH("Beep Playback Switch", IDX_MIXER_PCBEEP, 15, 1),
1117 AZF3328_MIXER_VOL_SPECIAL("Beep Playback Volume", IDX_MIXER_PCBEEP, 0x0f, 1, 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 AZF3328_MIXER_SWITCH("Video Playback Switch", IDX_MIXER_VIDEO, 15, 1),
1119 AZF3328_MIXER_VOL_STEREO("Video Playback Volume", IDX_MIXER_VIDEO, 0x1f, 1),
1120 AZF3328_MIXER_SWITCH("Aux Playback Switch", IDX_MIXER_AUX, 15, 1),
1121 AZF3328_MIXER_VOL_STEREO("Aux Playback Volume", IDX_MIXER_AUX, 0x1f, 1),
1122 AZF3328_MIXER_SWITCH("Modem Playback Switch", IDX_MIXER_MODEMOUT, 15, 1),
1123 AZF3328_MIXER_VOL_MONO("Modem Playback Volume", IDX_MIXER_MODEMOUT, 0x1f, 1),
1124 AZF3328_MIXER_SWITCH("Modem Capture Switch", IDX_MIXER_MODEMIN, 15, 1),
1125 AZF3328_MIXER_VOL_MONO("Modem Capture Volume", IDX_MIXER_MODEMIN, 0x1f, 1),
Andreas Mohr13769e32006-05-17 11:03:16 +02001126 AZF3328_MIXER_ENUM("Mic Select", IDX_MIXER_ADVCTL2, 2, 8),
1127 AZF3328_MIXER_ENUM("Mono Output Select", IDX_MIXER_ADVCTL2, 2, 9),
Andreas Mohre24a1212007-03-26 12:49:45 +02001128 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 -07001129 AZF3328_MIXER_VOL_SPECIAL("Tone Control - Treble", IDX_MIXER_BASSTREBLE, 0x07, 1, 0),
1130 AZF3328_MIXER_VOL_SPECIAL("Tone Control - Bass", IDX_MIXER_BASSTREBLE, 0x07, 9, 0),
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001131 AZF3328_MIXER_SWITCH("3D Control - Switch", IDX_MIXER_ADVCTL2, 13, 0),
Andreas Mohr13769e32006-05-17 11:03:16 +02001132 AZF3328_MIXER_VOL_SPECIAL("3D Control - Width", IDX_MIXER_ADVCTL1, 0x07, 1, 0), /* "3D Width" */
1133 AZF3328_MIXER_VOL_SPECIAL("3D Control - Depth", IDX_MIXER_ADVCTL1, 0x03, 8, 0), /* "Hifi 3D" */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134#if MIXER_TESTING
1135 AZF3328_MIXER_SWITCH("0", IDX_MIXER_ADVCTL2, 0, 0),
1136 AZF3328_MIXER_SWITCH("1", IDX_MIXER_ADVCTL2, 1, 0),
1137 AZF3328_MIXER_SWITCH("2", IDX_MIXER_ADVCTL2, 2, 0),
1138 AZF3328_MIXER_SWITCH("3", IDX_MIXER_ADVCTL2, 3, 0),
1139 AZF3328_MIXER_SWITCH("4", IDX_MIXER_ADVCTL2, 4, 0),
1140 AZF3328_MIXER_SWITCH("5", IDX_MIXER_ADVCTL2, 5, 0),
1141 AZF3328_MIXER_SWITCH("6", IDX_MIXER_ADVCTL2, 6, 0),
1142 AZF3328_MIXER_SWITCH("7", IDX_MIXER_ADVCTL2, 7, 0),
1143 AZF3328_MIXER_SWITCH("8", IDX_MIXER_ADVCTL2, 8, 0),
1144 AZF3328_MIXER_SWITCH("9", IDX_MIXER_ADVCTL2, 9, 0),
1145 AZF3328_MIXER_SWITCH("10", IDX_MIXER_ADVCTL2, 10, 0),
1146 AZF3328_MIXER_SWITCH("11", IDX_MIXER_ADVCTL2, 11, 0),
1147 AZF3328_MIXER_SWITCH("12", IDX_MIXER_ADVCTL2, 12, 0),
1148 AZF3328_MIXER_SWITCH("13", IDX_MIXER_ADVCTL2, 13, 0),
1149 AZF3328_MIXER_SWITCH("14", IDX_MIXER_ADVCTL2, 14, 0),
1150 AZF3328_MIXER_SWITCH("15", IDX_MIXER_ADVCTL2, 15, 0),
1151#endif
1152};
1153
Takashi Iwai83fdb6f2020-01-05 15:48:15 +01001154static const u16 snd_azf3328_init_values[][2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 { IDX_MIXER_PLAY_MASTER, MIXER_MUTE_MASK|0x1f1f },
1156 { IDX_MIXER_MODEMOUT, MIXER_MUTE_MASK|0x1f1f },
1157 { IDX_MIXER_BASSTREBLE, 0x0000 },
1158 { IDX_MIXER_PCBEEP, MIXER_MUTE_MASK|0x1f1f },
1159 { IDX_MIXER_MODEMIN, MIXER_MUTE_MASK|0x1f1f },
1160 { IDX_MIXER_MIC, MIXER_MUTE_MASK|0x001f },
1161 { IDX_MIXER_LINEIN, MIXER_MUTE_MASK|0x1f1f },
1162 { IDX_MIXER_CDAUDIO, MIXER_MUTE_MASK|0x1f1f },
1163 { IDX_MIXER_VIDEO, MIXER_MUTE_MASK|0x1f1f },
1164 { IDX_MIXER_AUX, MIXER_MUTE_MASK|0x1f1f },
1165 { IDX_MIXER_WAVEOUT, MIXER_MUTE_MASK|0x1f1f },
1166 { IDX_MIXER_FMSYNTH, MIXER_MUTE_MASK|0x1f1f },
1167 { IDX_MIXER_REC_VOLUME, MIXER_MUTE_MASK|0x0707 },
1168};
1169
Bill Pembertone23e7a12012-12-06 12:35:10 -05001170static int
Takashi Iwai95de7762005-11-17 15:02:42 +01001171snd_azf3328_mixer_new(struct snd_azf3328 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172{
Takashi Iwai95de7762005-11-17 15:02:42 +01001173 struct snd_card *card;
1174 const struct snd_kcontrol_new *sw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 unsigned int idx;
1176 int err;
1177
Takashi Iwaida3cec32008-08-08 17:12:14 +02001178 if (snd_BUG_ON(!chip || !chip->card))
1179 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
1181 card = chip->card;
1182
1183 /* mixer reset */
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001184 snd_azf3328_mixer_outw(chip, IDX_MIXER_RESET, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
1186 /* mute and zero volume channels */
Andreas Mohr02330fba2008-05-16 12:18:29 +02001187 for (idx = 0; idx < ARRAY_SIZE(snd_azf3328_init_values); ++idx) {
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001188 snd_azf3328_mixer_outw(chip,
1189 snd_azf3328_init_values[idx][0],
1190 snd_azf3328_init_values[idx][1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 }
Andreas Mohr02330fba2008-05-16 12:18:29 +02001192
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 /* add mixer controls */
1194 sw = snd_azf3328_mixer_controls;
Andreas Mohr02330fba2008-05-16 12:18:29 +02001195 for (idx = 0; idx < ARRAY_SIZE(snd_azf3328_mixer_controls);
1196 ++idx, ++sw) {
Takashi Iwai13c98662021-06-08 16:04:54 +02001197 err = snd_ctl_add(chip->card, snd_ctl_new1(sw, chip));
1198 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 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 void
Andreas Mohrda237f32010-12-27 21:17:26 +01001209snd_azf3328_codec_setfmt(struct snd_azf3328_codec_data *codec,
Andreas Mohr627d3e72008-06-23 11:50:47 +02001210 enum azf_freq_t bitrate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 unsigned int format_width,
1212 unsigned int channels
1213)
1214{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 unsigned long flags;
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001216 u16 val = 0xff00;
Andreas Mohr8d9a1142010-12-27 21:16:49 +01001217 u8 freq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 switch (bitrate) {
Andreas Mohrc9ba3742011-01-25 06:46:31 +01001220 case AZF_FREQ_4000: freq = SOUNDFORMAT_FREQ_SUSPECTED_4000; break;
1221 case AZF_FREQ_4800: freq = SOUNDFORMAT_FREQ_SUSPECTED_4800; break;
1222 case AZF_FREQ_5512:
1223 /* the AZF3328 names it "5510" for some strange reason */
1224 freq = SOUNDFORMAT_FREQ_5510; break;
1225 case AZF_FREQ_6620: freq = SOUNDFORMAT_FREQ_6620; break;
1226 case AZF_FREQ_8000: freq = SOUNDFORMAT_FREQ_8000; break;
1227 case AZF_FREQ_9600: freq = SOUNDFORMAT_FREQ_9600; break;
1228 case AZF_FREQ_11025: freq = SOUNDFORMAT_FREQ_11025; break;
1229 case AZF_FREQ_13240: freq = SOUNDFORMAT_FREQ_SUSPECTED_13240; break;
1230 case AZF_FREQ_16000: freq = SOUNDFORMAT_FREQ_16000; break;
1231 case AZF_FREQ_22050: freq = SOUNDFORMAT_FREQ_22050; break;
1232 case AZF_FREQ_32000: freq = SOUNDFORMAT_FREQ_32000; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 default:
Takashi Iwai99b359b2005-10-20 18:26:44 +02001234 snd_printk(KERN_WARNING "unknown bitrate %d, assuming 44.1kHz!\n", bitrate);
Gustavo A. R. Silvac0dbbda2020-07-08 15:32:36 -05001235 fallthrough;
Andreas Mohrc9ba3742011-01-25 06:46:31 +01001236 case AZF_FREQ_44100: freq = SOUNDFORMAT_FREQ_44100; break;
1237 case AZF_FREQ_48000: freq = SOUNDFORMAT_FREQ_48000; break;
1238 case AZF_FREQ_66200: freq = SOUNDFORMAT_FREQ_SUSPECTED_66200; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 }
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001240 /* val = 0xff07; 3m27.993s (65301Hz; -> 64000Hz???) hmm, 66120, 65967, 66123 */
1241 /* val = 0xff09; 17m15.098s (13123,478Hz; -> 12000Hz???) hmm, 13237.2Hz? */
1242 /* val = 0xff0a; 47m30.599s (4764,891Hz; -> 4800Hz???) yup, 4803Hz */
1243 /* val = 0xff0c; 57m0.510s (4010,263Hz; -> 4000Hz???) yup, 4003Hz */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 /* val = 0xff05; 5m11.556s (... -> 44100Hz) */
1245 /* val = 0xff03; 10m21.529s (21872,463Hz; -> 22050Hz???) */
1246 /* val = 0xff0f; 20m41.883s (10937,993Hz; -> 11025Hz???) */
1247 /* val = 0xff0d; 41m23.135s (5523,600Hz; -> 5512Hz???) */
1248 /* val = 0xff0e; 28m30.777s (8017Hz; -> 8000Hz???) */
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001249
Andreas Mohr8d9a1142010-12-27 21:16:49 +01001250 val |= freq;
1251
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 if (channels == 2)
1253 val |= SOUNDFORMAT_FLAG_2CHANNELS;
1254
1255 if (format_width == 16)
1256 val |= SOUNDFORMAT_FLAG_16BIT;
1257
Andreas Mohrda237f32010-12-27 21:17:26 +01001258 spin_lock_irqsave(codec->lock, flags);
Andreas Mohr02330fba2008-05-16 12:18:29 +02001259
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 /* set bitrate/format */
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001261 snd_azf3328_codec_outw(codec, IDX_IO_CODEC_SOUNDFORMAT, val);
Andreas Mohr02330fba2008-05-16 12:18:29 +02001262
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 /* changing the bitrate/format settings switches off the
1264 * audio output with an annoying click in case of 8/16bit format change
1265 * (maybe shutting down DAC/ADC?), thus immediately
1266 * do some tweaking to reenable it and get rid of the clicking
1267 * (FIXME: yes, it works, but what exactly am I doing here?? :)
1268 * FIXME: does this have some side effects for full-duplex
1269 * or other dramatic side effects? */
Andreas Mohradf59312010-12-27 21:16:43 +01001270 /* do it for non-capture codecs only */
Andreas Mohrda237f32010-12-27 21:17:26 +01001271 if (codec->type != AZF_CODEC_CAPTURE)
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001272 snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS,
1273 snd_azf3328_codec_inw(codec, IDX_IO_CODEC_DMA_FLAGS) |
1274 DMA_RUN_SOMETHING1 |
1275 DMA_RUN_SOMETHING2 |
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001276 SOMETHING_ALMOST_ALWAYS_SET |
1277 DMA_EPILOGUE_SOMETHING |
1278 DMA_SOMETHING_ELSE
1279 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
Andreas Mohrda237f32010-12-27 21:17:26 +01001281 spin_unlock_irqrestore(codec->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282}
1283
Andreas Mohr02330fba2008-05-16 12:18:29 +02001284static inline void
Andreas Mohrda237f32010-12-27 21:17:26 +01001285snd_azf3328_codec_setfmt_lowpower(struct snd_azf3328_codec_data *codec
Andreas Mohr02330fba2008-05-16 12:18:29 +02001286)
1287{
1288 /* choose lowest frequency for low power consumption.
1289 * While this will cause louder noise due to rather coarse frequency,
1290 * it should never matter since output should always
1291 * get disabled properly when idle anyway. */
Andreas Mohrda237f32010-12-27 21:17:26 +01001292 snd_azf3328_codec_setfmt(codec, AZF_FREQ_4000, 8, 1);
Andreas Mohr02330fba2008-05-16 12:18:29 +02001293}
1294
Andreas Mohr627d3e72008-06-23 11:50:47 +02001295static void
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001296snd_azf3328_ctrl_reg_6AH_update(struct snd_azf3328 *chip,
Andreas Mohr627d3e72008-06-23 11:50:47 +02001297 unsigned bitmask,
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001298 bool enable
Andreas Mohr627d3e72008-06-23 11:50:47 +02001299)
1300{
Andreas Mohr78df6172009-07-12 22:17:54 +02001301 bool do_mask = !enable;
1302 if (do_mask)
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001303 chip->shadow_reg_ctrl_6AH |= bitmask;
Andreas Mohr78df6172009-07-12 22:17:54 +02001304 else
1305 chip->shadow_reg_ctrl_6AH &= ~bitmask;
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001306 dev_dbg(chip->card->dev,
1307 "6AH_update mask 0x%04x do_mask %d: val 0x%04x\n",
1308 bitmask, do_mask, chip->shadow_reg_ctrl_6AH);
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001309 snd_azf3328_ctrl_outw(chip, IDX_IO_6AH, chip->shadow_reg_ctrl_6AH);
Andreas Mohr627d3e72008-06-23 11:50:47 +02001310}
1311
Andreas Mohr02330fba2008-05-16 12:18:29 +02001312static inline void
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001313snd_azf3328_ctrl_enable_codecs(struct snd_azf3328 *chip, bool enable)
Andreas Mohr02330fba2008-05-16 12:18:29 +02001314{
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001315 dev_dbg(chip->card->dev, "codec_enable %d\n", enable);
Andreas Mohr02330fba2008-05-16 12:18:29 +02001316 /* no idea what exactly is being done here, but I strongly assume it's
1317 * PM related */
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001318 snd_azf3328_ctrl_reg_6AH_update(
Andreas Mohr627d3e72008-06-23 11:50:47 +02001319 chip, IO_6A_PAUSE_PLAYBACK_BIT8, enable
Andreas Mohr02330fba2008-05-16 12:18:29 +02001320 );
1321}
1322
1323static void
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001324snd_azf3328_ctrl_codec_activity(struct snd_azf3328 *chip,
1325 enum snd_azf3328_codec_type codec_type,
1326 bool enable
Andreas Mohr02330fba2008-05-16 12:18:29 +02001327)
1328{
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001329 struct snd_azf3328_codec_data *codec = &chip->codecs[codec_type];
1330 bool need_change = (codec->running != enable);
Andreas Mohr02330fba2008-05-16 12:18:29 +02001331
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001332 dev_dbg(chip->card->dev,
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001333 "codec_activity: %s codec, enable %d, need_change %d\n",
1334 codec->name, enable, need_change
Andreas Mohr02330fba2008-05-16 12:18:29 +02001335 );
1336 if (need_change) {
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001337 static const struct {
1338 enum snd_azf3328_codec_type other1;
1339 enum snd_azf3328_codec_type other2;
1340 } peer_codecs[3] =
1341 { { AZF_CODEC_CAPTURE, AZF_CODEC_I2S_OUT },
1342 { AZF_CODEC_PLAYBACK, AZF_CODEC_I2S_OUT },
1343 { AZF_CODEC_PLAYBACK, AZF_CODEC_CAPTURE } };
1344 bool call_function;
1345
1346 if (enable)
1347 /* if enable codec, call enable_codecs func
1348 to enable codec supply... */
1349 call_function = 1;
1350 else {
1351 /* ...otherwise call enable_codecs func
1352 (which globally shuts down operation of codecs)
1353 only in case the other codecs are currently
1354 not active either! */
Andreas Mohr78df6172009-07-12 22:17:54 +02001355 call_function =
1356 ((!chip->codecs[peer_codecs[codec_type].other1]
1357 .running)
1358 && (!chip->codecs[peer_codecs[codec_type].other2]
1359 .running));
Dan Carpenter7f788e02015-02-25 16:32:45 +03001360 }
1361 if (call_function)
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001362 snd_azf3328_ctrl_enable_codecs(chip, enable);
Andreas Mohr02330fba2008-05-16 12:18:29 +02001363
1364 /* ...and adjust clock, too
1365 * (reduce noise and power consumption) */
1366 if (!enable)
Andreas Mohrda237f32010-12-27 21:17:26 +01001367 snd_azf3328_codec_setfmt_lowpower(codec);
Andreas Mohr78df6172009-07-12 22:17:54 +02001368 codec->running = enable;
Andreas Mohr02330fba2008-05-16 12:18:29 +02001369 }
Andreas Mohr02330fba2008-05-16 12:18:29 +02001370}
1371
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001372static void
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001373snd_azf3328_codec_setdmaa(struct snd_azf3328 *chip,
1374 struct snd_azf3328_codec_data *codec,
1375 unsigned long addr,
1376 unsigned int period_bytes,
1377 unsigned int buffer_bytes
Andreas Mohr02330fba2008-05-16 12:18:29 +02001378)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379{
Andreas Mohr689c6912010-12-27 21:17:35 +01001380 WARN_ONCE(period_bytes & 1, "odd period length!?\n");
1381 WARN_ONCE(buffer_bytes != 2 * period_bytes,
1382 "missed our input expectations! %u vs. %u\n",
1383 buffer_bytes, period_bytes);
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001384 if (!codec->running) {
1385 /* AZF3328 uses a two buffer pointer DMA transfer approach */
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001386
Andreas Mohr689c6912010-12-27 21:17:35 +01001387 unsigned long flags;
Andreas Mohr02330fba2008-05-16 12:18:29 +02001388
1389 /* width 32bit (prevent overflow): */
Andreas Mohr689c6912010-12-27 21:17:35 +01001390 u32 area_length;
1391 struct codec_setup_io {
1392 u32 dma_start_1;
1393 u32 dma_start_2;
1394 u32 dma_lengths;
1395 } __attribute__((packed)) setup_io;
Andreas Mohr02330fba2008-05-16 12:18:29 +02001396
Andreas Mohr689c6912010-12-27 21:17:35 +01001397 area_length = buffer_bytes/2;
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001398
Andreas Mohr689c6912010-12-27 21:17:35 +01001399 setup_io.dma_start_1 = addr;
1400 setup_io.dma_start_2 = addr+area_length;
1401
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001402 dev_dbg(chip->card->dev,
Andreas Mohr689c6912010-12-27 21:17:35 +01001403 "setdma: buffers %08x[%u] / %08x[%u], %u, %u\n",
1404 setup_io.dma_start_1, area_length,
1405 setup_io.dma_start_2, area_length,
1406 period_bytes, buffer_bytes);
1407
1408 /* Hmm, are we really supposed to decrement this by 1??
1409 Most definitely certainly not: configuring full length does
1410 work properly (i.e. likely better), and BTW we
1411 violated possibly differing frame sizes with this...
1412
1413 area_length--; |* max. index *|
1414 */
Andreas Mohr79741502010-11-21 12:09:32 +01001415
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001416 /* build combined I/O buffer length word */
Andreas Mohr689c6912010-12-27 21:17:35 +01001417 setup_io.dma_lengths = (area_length << 16) | (area_length);
1418
Andreas Mohrda237f32010-12-27 21:17:26 +01001419 spin_lock_irqsave(codec->lock, flags);
Andreas Mohr689c6912010-12-27 21:17:35 +01001420 snd_azf3328_codec_outl_multi(
1421 codec, IDX_IO_CODEC_DMA_START_1, &setup_io, 3
1422 );
Andreas Mohrda237f32010-12-27 21:17:26 +01001423 spin_unlock_irqrestore(codec->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425}
1426
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001427static int
Andreas Mohrda237f32010-12-27 21:17:26 +01001428snd_azf3328_pcm_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429{
Takashi Iwai95de7762005-11-17 15:02:42 +01001430 struct snd_pcm_runtime *runtime = substream->runtime;
Andreas Mohrda237f32010-12-27 21:17:26 +01001431 struct snd_azf3328_codec_data *codec = runtime->private_data;
Andreas Mohr34585592010-12-27 21:17:11 +01001432#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
1434 unsigned int count = snd_pcm_lib_period_bytes(substream);
1435#endif
1436
Andreas Mohr34585592010-12-27 21:17:11 +01001437 codec->dma_base = runtime->dma_addr;
1438
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439#if 0
Andreas Mohrda237f32010-12-27 21:17:26 +01001440 snd_azf3328_codec_setfmt(codec,
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001441 runtime->rate,
1442 snd_pcm_format_width(runtime->format),
1443 runtime->channels);
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001444 snd_azf3328_codec_setdmaa(chip, codec,
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001445 runtime->dma_addr, count, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 return 0;
1448}
1449
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001450static int
Andreas Mohrda237f32010-12-27 21:17:26 +01001451snd_azf3328_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452{
Takashi Iwai95de7762005-11-17 15:02:42 +01001453 struct snd_azf3328 *chip = snd_pcm_substream_chip(substream);
1454 struct snd_pcm_runtime *runtime = substream->runtime;
Andreas Mohrda237f32010-12-27 21:17:26 +01001455 struct snd_azf3328_codec_data *codec = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 int result = 0;
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001457 u16 flags1;
Peter Senna Tschudine0f17c72013-09-22 20:44:12 +02001458 bool previously_muted = false;
Andreas Mohrda237f32010-12-27 21:17:26 +01001459 bool is_main_mixer_playback_codec = (AZF_CODEC_PLAYBACK == codec->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 switch (cmd) {
1462 case SNDRV_PCM_TRIGGER_START:
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001463 dev_dbg(chip->card->dev, "START PCM %s\n", codec->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
Andreas Mohrda237f32010-12-27 21:17:26 +01001465 if (is_main_mixer_playback_codec) {
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001466 /* mute WaveOut (avoid clicking during setup) */
1467 previously_muted =
Andreas Mohrb5dc20c2011-02-19 00:49:32 +01001468 snd_azf3328_mixer_mute_control_pcm(
1469 chip, 1
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001470 );
1471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
Andreas Mohrda237f32010-12-27 21:17:26 +01001473 snd_azf3328_codec_setfmt(codec,
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001474 runtime->rate,
1475 snd_pcm_format_width(runtime->format),
1476 runtime->channels);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Andreas Mohrda237f32010-12-27 21:17:26 +01001478 spin_lock(codec->lock);
Andreas Mohr02330fba2008-05-16 12:18:29 +02001479 /* first, remember current value: */
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001480 flags1 = snd_azf3328_codec_inw(codec, IDX_IO_CODEC_DMA_FLAGS);
Andreas Mohr02330fba2008-05-16 12:18:29 +02001481
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001482 /* stop transfer */
1483 flags1 &= ~DMA_RESUME;
1484 snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS, flags1);
Andreas Mohr02330fba2008-05-16 12:18:29 +02001485
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 /* FIXME: clear interrupts or what??? */
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001487 snd_azf3328_codec_outw(codec, IDX_IO_CODEC_IRQTYPE, 0xffff);
Andreas Mohrda237f32010-12-27 21:17:26 +01001488 spin_unlock(codec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001490 snd_azf3328_codec_setdmaa(chip, codec, runtime->dma_addr,
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001491 snd_pcm_lib_period_bytes(substream),
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001492 snd_pcm_lib_buffer_bytes(substream)
1493 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Andreas Mohrda237f32010-12-27 21:17:26 +01001495 spin_lock(codec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496#ifdef WIN9X
1497 /* FIXME: enable playback/recording??? */
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001498 flags1 |= DMA_RUN_SOMETHING1 | DMA_RUN_SOMETHING2;
1499 snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS, flags1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001501 /* start transfer again */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 /* FIXME: what is this value (0x0010)??? */
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001503 flags1 |= DMA_RESUME | DMA_EPILOGUE_SOMETHING;
1504 snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS, flags1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505#else /* NT4 */
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001506 snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS,
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001507 0x0000);
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001508 snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS,
1509 DMA_RUN_SOMETHING1);
1510 snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS,
1511 DMA_RUN_SOMETHING1 |
1512 DMA_RUN_SOMETHING2);
1513 snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS,
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001514 DMA_RESUME |
1515 SOMETHING_ALMOST_ALWAYS_SET |
1516 DMA_EPILOGUE_SOMETHING |
1517 DMA_SOMETHING_ELSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518#endif
Andreas Mohrda237f32010-12-27 21:17:26 +01001519 spin_unlock(codec->lock);
1520 snd_azf3328_ctrl_codec_activity(chip, codec->type, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Andreas Mohrda237f32010-12-27 21:17:26 +01001522 if (is_main_mixer_playback_codec) {
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001523 /* now unmute WaveOut */
1524 if (!previously_muted)
Andreas Mohrb5dc20c2011-02-19 00:49:32 +01001525 snd_azf3328_mixer_mute_control_pcm(
1526 chip, 0
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001527 );
1528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001530 dev_dbg(chip->card->dev, "PCM STARTED %s\n", codec->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 break;
Andreas Mohrca54bde2006-05-17 11:02:24 +02001532 case SNDRV_PCM_TRIGGER_RESUME:
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001533 dev_dbg(chip->card->dev, "PCM RESUME %s\n", codec->name);
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001534 /* resume codec if we were active */
Andreas Mohrda237f32010-12-27 21:17:26 +01001535 spin_lock(codec->lock);
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001536 if (codec->running)
1537 snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS,
1538 snd_azf3328_codec_inw(
1539 codec, IDX_IO_CODEC_DMA_FLAGS
1540 ) | DMA_RESUME
1541 );
Andreas Mohrda237f32010-12-27 21:17:26 +01001542 spin_unlock(codec->lock);
Andreas Mohrca54bde2006-05-17 11:02:24 +02001543 break;
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001544 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001545 dev_dbg(chip->card->dev, "PCM STOP %s\n", codec->name);
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001546
Andreas Mohrda237f32010-12-27 21:17:26 +01001547 if (is_main_mixer_playback_codec) {
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001548 /* mute WaveOut (avoid clicking during setup) */
1549 previously_muted =
Andreas Mohrb5dc20c2011-02-19 00:49:32 +01001550 snd_azf3328_mixer_mute_control_pcm(
1551 chip, 1
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001552 );
1553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
Andreas Mohrda237f32010-12-27 21:17:26 +01001555 spin_lock(codec->lock);
Andreas Mohr02330fba2008-05-16 12:18:29 +02001556 /* first, remember current value: */
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001557 flags1 = snd_azf3328_codec_inw(codec, IDX_IO_CODEC_DMA_FLAGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001559 /* stop transfer */
1560 flags1 &= ~DMA_RESUME;
1561 snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS, flags1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001563 /* hmm, is this really required? we're resetting the same bit
1564 * immediately thereafter... */
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001565 flags1 |= DMA_RUN_SOMETHING1;
1566 snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS, flags1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001568 flags1 &= ~DMA_RUN_SOMETHING1;
1569 snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS, flags1);
Andreas Mohrda237f32010-12-27 21:17:26 +01001570 spin_unlock(codec->lock);
1571 snd_azf3328_ctrl_codec_activity(chip, codec->type, 0);
Andreas Mohr02330fba2008-05-16 12:18:29 +02001572
Andreas Mohrda237f32010-12-27 21:17:26 +01001573 if (is_main_mixer_playback_codec) {
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001574 /* now unmute WaveOut */
1575 if (!previously_muted)
Andreas Mohrb5dc20c2011-02-19 00:49:32 +01001576 snd_azf3328_mixer_mute_control_pcm(
1577 chip, 0
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001578 );
1579 }
Andreas Mohr02330fba2008-05-16 12:18:29 +02001580
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001581 dev_dbg(chip->card->dev, "PCM STOPPED %s\n", codec->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 break;
Andreas Mohrca54bde2006-05-17 11:02:24 +02001583 case SNDRV_PCM_TRIGGER_SUSPEND:
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001584 dev_dbg(chip->card->dev, "PCM SUSPEND %s\n", codec->name);
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001585 /* make sure codec is stopped */
1586 snd_azf3328_codec_outw(codec, IDX_IO_CODEC_DMA_FLAGS,
1587 snd_azf3328_codec_inw(
1588 codec, IDX_IO_CODEC_DMA_FLAGS
1589 ) & ~DMA_RESUME
1590 );
Andreas Mohrca54bde2006-05-17 11:02:24 +02001591 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001593 WARN(1, "FIXME: SNDRV_PCM_TRIGGER_PAUSE_PUSH NIY!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 break;
1595 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001596 WARN(1, "FIXME: SNDRV_PCM_TRIGGER_PAUSE_RELEASE NIY!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 break;
1598 default:
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001599 WARN(1, "FIXME: unknown trigger mode!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 return -EINVAL;
1601 }
Andreas Mohr02330fba2008-05-16 12:18:29 +02001602
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 return result;
1604}
1605
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001606static snd_pcm_uframes_t
Andreas Mohrda237f32010-12-27 21:17:26 +01001607snd_azf3328_pcm_pointer(struct snd_pcm_substream *substream
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001608)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609{
Andreas Mohrda237f32010-12-27 21:17:26 +01001610 const struct snd_azf3328_codec_data *codec =
1611 substream->runtime->private_data;
Andreas Mohr34585592010-12-27 21:17:11 +01001612 unsigned long result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 snd_pcm_uframes_t frmres;
1614
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001615 result = snd_azf3328_codec_inl(codec, IDX_IO_CODEC_DMA_CURRPOS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001617 /* calculate offset */
Andreas Mohr34585592010-12-27 21:17:11 +01001618#ifdef QUERY_HARDWARE
1619 result -= snd_azf3328_codec_inl(codec, IDX_IO_CODEC_DMA_START_1);
1620#else
1621 result -= codec->dma_base;
1622#endif
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001623 frmres = bytes_to_frames( substream->runtime, result);
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001624 dev_dbg(substream->pcm->card->dev, "%08li %s @ 0x%8lx, frames %8ld\n",
1625 jiffies, codec->name, result, frmres);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 return frmres;
1627}
1628
Andreas Mohr02330fba2008-05-16 12:18:29 +02001629/******************************************************************/
1630
1631#ifdef SUPPORT_GAMEPORT
1632static inline void
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001633snd_azf3328_gameport_irq_enable(struct snd_azf3328 *chip,
1634 bool enable
1635)
Andreas Mohr02330fba2008-05-16 12:18:29 +02001636{
1637 snd_azf3328_io_reg_setb(
1638 chip->game_io+IDX_GAME_HWCONFIG,
1639 GAME_HWCFG_IRQ_ENABLE,
1640 enable
1641 );
1642}
1643
1644static inline void
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001645snd_azf3328_gameport_legacy_address_enable(struct snd_azf3328 *chip,
1646 bool enable
1647)
Andreas Mohr02330fba2008-05-16 12:18:29 +02001648{
1649 snd_azf3328_io_reg_setb(
1650 chip->game_io+IDX_GAME_HWCONFIG,
1651 GAME_HWCFG_LEGACY_ADDRESS_ENABLE,
1652 enable
1653 );
1654}
1655
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001656static void
1657snd_azf3328_gameport_set_counter_frequency(struct snd_azf3328 *chip,
1658 unsigned int freq_cfg
1659)
Andreas Mohr02330fba2008-05-16 12:18:29 +02001660{
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001661 snd_azf3328_io_reg_setb(
1662 chip->game_io+IDX_GAME_HWCONFIG,
1663 0x02,
1664 (freq_cfg & 1) != 0
1665 );
1666 snd_azf3328_io_reg_setb(
1667 chip->game_io+IDX_GAME_HWCONFIG,
1668 0x04,
1669 (freq_cfg & 2) != 0
1670 );
1671}
1672
1673static inline void
1674snd_azf3328_gameport_axis_circuit_enable(struct snd_azf3328 *chip, bool enable)
1675{
1676 snd_azf3328_ctrl_reg_6AH_update(
Andreas Mohr627d3e72008-06-23 11:50:47 +02001677 chip, IO_6A_SOMETHING2_GAMEPORT, enable
Andreas Mohr02330fba2008-05-16 12:18:29 +02001678 );
1679}
1680
1681static inline void
1682snd_azf3328_gameport_interrupt(struct snd_azf3328 *chip)
1683{
1684 /*
1685 * skeleton handler only
1686 * (we do not want axis reading in interrupt handler - too much load!)
1687 */
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001688 dev_dbg(chip->card->dev, "gameport irq\n");
Andreas Mohr02330fba2008-05-16 12:18:29 +02001689
1690 /* this should ACK the gameport IRQ properly, hopefully. */
1691 snd_azf3328_game_inw(chip, IDX_GAME_AXIS_VALUE);
1692}
1693
1694static int
1695snd_azf3328_gameport_open(struct gameport *gameport, int mode)
1696{
1697 struct snd_azf3328 *chip = gameport_get_port_data(gameport);
1698 int res;
1699
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001700 dev_dbg(chip->card->dev, "gameport_open, mode %d\n", mode);
Andreas Mohr02330fba2008-05-16 12:18:29 +02001701 switch (mode) {
1702 case GAMEPORT_MODE_COOKED:
1703 case GAMEPORT_MODE_RAW:
1704 res = 0;
1705 break;
1706 default:
1707 res = -1;
1708 break;
1709 }
1710
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001711 snd_azf3328_gameport_set_counter_frequency(chip,
1712 GAME_HWCFG_ADC_COUNTER_FREQ_STD);
Andreas Mohr02330fba2008-05-16 12:18:29 +02001713 snd_azf3328_gameport_axis_circuit_enable(chip, (res == 0));
1714
1715 return res;
1716}
1717
1718static void
1719snd_azf3328_gameport_close(struct gameport *gameport)
1720{
1721 struct snd_azf3328 *chip = gameport_get_port_data(gameport);
1722
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001723 dev_dbg(chip->card->dev, "gameport_close\n");
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001724 snd_azf3328_gameport_set_counter_frequency(chip,
1725 GAME_HWCFG_ADC_COUNTER_FREQ_1_200);
Andreas Mohr02330fba2008-05-16 12:18:29 +02001726 snd_azf3328_gameport_axis_circuit_enable(chip, 0);
1727}
1728
1729static int
1730snd_azf3328_gameport_cooked_read(struct gameport *gameport,
1731 int *axes,
1732 int *buttons
1733)
1734{
1735 struct snd_azf3328 *chip = gameport_get_port_data(gameport);
1736 int i;
1737 u8 val;
1738 unsigned long flags;
1739
Takashi Iwaida3cec32008-08-08 17:12:14 +02001740 if (snd_BUG_ON(!chip))
1741 return 0;
Andreas Mohr02330fba2008-05-16 12:18:29 +02001742
1743 spin_lock_irqsave(&chip->reg_lock, flags);
1744 val = snd_azf3328_game_inb(chip, IDX_GAME_LEGACY_COMPATIBLE);
1745 *buttons = (~(val) >> 4) & 0xf;
1746
1747 /* ok, this one is a bit dirty: cooked_read is being polled by a timer,
1748 * thus we're atomic and cannot actively wait in here
1749 * (which would be useful for us since it probably would be better
1750 * to trigger a measurement in here, then wait a short amount of
1751 * time until it's finished, then read values of _this_ measurement).
1752 *
1753 * Thus we simply resort to reading values if they're available already
1754 * and trigger the next measurement.
1755 */
1756
1757 val = snd_azf3328_game_inb(chip, IDX_GAME_AXES_CONFIG);
1758 if (val & GAME_AXES_SAMPLING_READY) {
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001759 for (i = 0; i < ARRAY_SIZE(chip->axes); ++i) {
Andreas Mohr02330fba2008-05-16 12:18:29 +02001760 /* configure the axis to read */
1761 val = (i << 4) | 0x0f;
1762 snd_azf3328_game_outb(chip, IDX_GAME_AXES_CONFIG, val);
1763
1764 chip->axes[i] = snd_azf3328_game_inw(
1765 chip, IDX_GAME_AXIS_VALUE
1766 );
1767 }
1768 }
1769
Andreas Mohradf59312010-12-27 21:16:43 +01001770 /* trigger next sampling of axes, to be evaluated the next time we
Andreas Mohr02330fba2008-05-16 12:18:29 +02001771 * enter this function */
1772
1773 /* for some very, very strange reason we cannot enable
1774 * Measurement Ready monitoring for all axes here,
1775 * at least not when only one joystick connected */
1776 val = 0x03; /* we're able to monitor axes 1 and 2 only */
1777 snd_azf3328_game_outb(chip, IDX_GAME_AXES_CONFIG, val);
1778
1779 snd_azf3328_game_outw(chip, IDX_GAME_AXIS_VALUE, 0xffff);
1780 spin_unlock_irqrestore(&chip->reg_lock, flags);
1781
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001782 for (i = 0; i < ARRAY_SIZE(chip->axes); i++) {
Andreas Mohr02330fba2008-05-16 12:18:29 +02001783 axes[i] = chip->axes[i];
1784 if (axes[i] == 0xffff)
1785 axes[i] = -1;
1786 }
1787
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001788 dev_dbg(chip->card->dev, "cooked_read: axes %d %d %d %d buttons %d\n",
1789 axes[0], axes[1], axes[2], axes[3], *buttons);
Andreas Mohr02330fba2008-05-16 12:18:29 +02001790
1791 return 0;
1792}
1793
Bill Pembertone23e7a12012-12-06 12:35:10 -05001794static int
Andreas Mohr02330fba2008-05-16 12:18:29 +02001795snd_azf3328_gameport(struct snd_azf3328 *chip, int dev)
1796{
1797 struct gameport *gp;
1798
Andreas Mohr02330fba2008-05-16 12:18:29 +02001799 chip->gameport = gp = gameport_allocate_port();
1800 if (!gp) {
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001801 dev_err(chip->card->dev, "cannot alloc memory for gameport\n");
Andreas Mohr02330fba2008-05-16 12:18:29 +02001802 return -ENOMEM;
1803 }
1804
1805 gameport_set_name(gp, "AZF3328 Gameport");
1806 gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
1807 gameport_set_dev_parent(gp, &chip->pci->dev);
Andreas Mohr627d3e72008-06-23 11:50:47 +02001808 gp->io = chip->game_io;
Andreas Mohr02330fba2008-05-16 12:18:29 +02001809 gameport_set_port_data(gp, chip);
1810
1811 gp->open = snd_azf3328_gameport_open;
1812 gp->close = snd_azf3328_gameport_close;
1813 gp->fuzz = 16; /* seems ok */
1814 gp->cooked_read = snd_azf3328_gameport_cooked_read;
1815
1816 /* DISABLE legacy address: we don't need it! */
1817 snd_azf3328_gameport_legacy_address_enable(chip, 0);
1818
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001819 snd_azf3328_gameport_set_counter_frequency(chip,
1820 GAME_HWCFG_ADC_COUNTER_FREQ_1_200);
Andreas Mohr02330fba2008-05-16 12:18:29 +02001821 snd_azf3328_gameport_axis_circuit_enable(chip, 0);
1822
1823 gameport_register_port(chip->gameport);
1824
1825 return 0;
1826}
1827
1828static void
1829snd_azf3328_gameport_free(struct snd_azf3328 *chip)
1830{
1831 if (chip->gameport) {
1832 gameport_unregister_port(chip->gameport);
1833 chip->gameport = NULL;
1834 }
1835 snd_azf3328_gameport_irq_enable(chip, 0);
1836}
1837#else
1838static inline int
1839snd_azf3328_gameport(struct snd_azf3328 *chip, int dev) { return -ENOSYS; }
1840static inline void
1841snd_azf3328_gameport_free(struct snd_azf3328 *chip) { }
1842static inline void
1843snd_azf3328_gameport_interrupt(struct snd_azf3328 *chip)
1844{
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001845 dev_warn(chip->card->dev, "huh, game port IRQ occurred!?\n");
Andreas Mohr02330fba2008-05-16 12:18:29 +02001846}
1847#endif /* SUPPORT_GAMEPORT */
1848
1849/******************************************************************/
1850
Andreas Mohr627d3e72008-06-23 11:50:47 +02001851static inline void
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001852snd_azf3328_irq_log_unknown_type(struct snd_azf3328 *chip, u8 which)
Andreas Mohr627d3e72008-06-23 11:50:47 +02001853{
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001854 dev_dbg(chip->card->dev,
1855 "unknown IRQ type (%x) occurred, please report!\n",
1856 which);
Andreas Mohr627d3e72008-06-23 11:50:47 +02001857}
1858
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001859static inline void
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001860snd_azf3328_pcm_interrupt(struct snd_azf3328 *chip,
1861 const struct snd_azf3328_codec_data *first_codec,
Andreas Mohrda237f32010-12-27 21:17:26 +01001862 u8 status
1863)
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001864{
1865 u8 which;
1866 enum snd_azf3328_codec_type codec_type;
Andreas Mohrda237f32010-12-27 21:17:26 +01001867 const struct snd_azf3328_codec_data *codec = first_codec;
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001868
1869 for (codec_type = AZF_CODEC_PLAYBACK;
1870 codec_type <= AZF_CODEC_I2S_OUT;
Andreas Mohrda237f32010-12-27 21:17:26 +01001871 ++codec_type, ++codec) {
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001872
1873 /* skip codec if there's no interrupt for it */
1874 if (!(status & (1 << codec_type)))
1875 continue;
1876
Andreas Mohrda237f32010-12-27 21:17:26 +01001877 spin_lock(codec->lock);
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001878 which = snd_azf3328_codec_inb(codec, IDX_IO_CODEC_IRQTYPE);
1879 /* ack all IRQ types immediately */
1880 snd_azf3328_codec_outb(codec, IDX_IO_CODEC_IRQTYPE, which);
Andreas Mohrda237f32010-12-27 21:17:26 +01001881 spin_unlock(codec->lock);
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001882
Andreas Mohrda237f32010-12-27 21:17:26 +01001883 if (codec->substream) {
Andreas Mohr78df6172009-07-12 22:17:54 +02001884 snd_pcm_period_elapsed(codec->substream);
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001885 dev_dbg(chip->card->dev, "%s period done (#%x), @ %x\n",
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001886 codec->name,
1887 which,
1888 snd_azf3328_codec_inl(
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001889 codec, IDX_IO_CODEC_DMA_CURRPOS));
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001890 } else
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001891 dev_warn(chip->card->dev, "irq handler problem!\n");
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001892 if (which & IRQ_SOMETHING)
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001893 snd_azf3328_irq_log_unknown_type(chip, which);
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001894 }
1895}
1896
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001897static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +01001898snd_azf3328_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899{
Takashi Iwai95de7762005-11-17 15:02:42 +01001900 struct snd_azf3328 *chip = dev_id;
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001901 u8 status;
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001902 static unsigned long irq_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001904 status = snd_azf3328_ctrl_inb(chip, IDX_IO_IRQSTATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
1906 /* fast path out, to ease interrupt sharing */
Andreas Mohr02330fba2008-05-16 12:18:29 +02001907 if (!(status &
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001908 (IRQ_PLAYBACK|IRQ_RECORDING|IRQ_I2S_OUT
1909 |IRQ_GAMEPORT|IRQ_MPU401|IRQ_TIMER)
Andreas Mohr02330fba2008-05-16 12:18:29 +02001910 ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 return IRQ_NONE; /* must be interrupt for another device */
1912
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001913 dev_dbg(chip->card->dev,
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001914 "irq_count %ld! IDX_IO_IRQSTATUS %04x\n",
Andreas Mohr627d3e72008-06-23 11:50:47 +02001915 irq_count++ /* debug-only */,
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001916 status);
Andreas Mohr02330fba2008-05-16 12:18:29 +02001917
Andreas Mohre2f87262006-05-17 11:04:19 +02001918 if (status & IRQ_TIMER) {
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001919 /* dev_dbg(chip->card->dev, "timer %ld\n",
Andreas Mohr02330fba2008-05-16 12:18:29 +02001920 snd_azf3328_codec_inl(chip, IDX_IO_TIMER_VALUE)
1921 & TIMER_VALUE_MASK
1922 ); */
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001923 if (chip->timer)
1924 snd_timer_interrupt(chip->timer, chip->timer->sticks);
1925 /* ACK timer */
1926 spin_lock(&chip->reg_lock);
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001927 snd_azf3328_ctrl_outb(chip, IDX_IO_TIMER_VALUE + 3, 0x07);
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001928 spin_unlock(&chip->reg_lock);
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001929 dev_dbg(chip->card->dev, "timer IRQ\n");
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001930 }
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001931
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001932 if (status & (IRQ_PLAYBACK|IRQ_RECORDING|IRQ_I2S_OUT))
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001933 snd_azf3328_pcm_interrupt(chip, chip->codecs, status);
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001934
Andreas Mohr02330fba2008-05-16 12:18:29 +02001935 if (status & IRQ_GAMEPORT)
1936 snd_azf3328_gameport_interrupt(chip);
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001937
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001938 /* MPU401 has less critical IRQ requirements
1939 * than timer and playback/recording, right? */
Andreas Mohre2f87262006-05-17 11:04:19 +02001940 if (status & IRQ_MPU401) {
David Howells7d12e782006-10-05 14:55:46 +01001941 snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001942
1943 /* hmm, do we have to ack the IRQ here somehow?
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001944 * If so, then I don't know how yet... */
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01001945 dev_dbg(chip->card->dev, "MPU401 IRQ\n");
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001946 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 return IRQ_HANDLED;
1948}
1949
1950/*****************************************************************/
1951
Andreas Mohrdfbf9512009-07-05 13:55:46 +02001952/* as long as we think we have identical snd_pcm_hardware parameters
1953 for playback, capture and i2s out, we can use the same physical struct
1954 since the struct is simply being copied into a member.
1955*/
1956static const struct snd_pcm_hardware snd_azf3328_hardware =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957{
1958 /* FIXME!! Correct? */
Andreas Mohrd91c64c2005-10-25 11:17:45 +02001959 .info = SNDRV_PCM_INFO_MMAP |
1960 SNDRV_PCM_INFO_INTERLEAVED |
1961 SNDRV_PCM_INFO_MMAP_VALID,
1962 .formats = SNDRV_PCM_FMTBIT_S8 |
1963 SNDRV_PCM_FMTBIT_U8 |
1964 SNDRV_PCM_FMTBIT_S16_LE |
1965 SNDRV_PCM_FMTBIT_U16_LE,
1966 .rates = SNDRV_PCM_RATE_5512 |
1967 SNDRV_PCM_RATE_8000_48000 |
1968 SNDRV_PCM_RATE_KNOT,
Andreas Mohr02330fba2008-05-16 12:18:29 +02001969 .rate_min = AZF_FREQ_4000,
1970 .rate_max = AZF_FREQ_66200,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 .channels_min = 1,
1972 .channels_max = 2,
Andreas Mohr79741502010-11-21 12:09:32 +01001973 .buffer_bytes_max = (64*1024),
1974 .period_bytes_min = 1024,
1975 .period_bytes_max = (32*1024),
1976 /* We simply have two DMA areas (instead of a list of descriptors
1977 such as other cards); I believe that this is a fixed hardware
1978 attribute and there isn't much driver magic to be done to expand it.
1979 Thus indicate that we have at least and at most 2 periods. */
1980 .periods_min = 2,
1981 .periods_max = 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 /* FIXME: maybe that card actually has a FIFO?
1983 * Hmm, it seems newer revisions do have one, but we still don't know
1984 * its size... */
1985 .fifo_size = 0,
1986};
1987
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
Takashi Iwai3e2fd042017-06-07 14:18:16 +02001989static const unsigned int snd_azf3328_fixed_rates[] = {
Andreas Mohr02330fba2008-05-16 12:18:29 +02001990 AZF_FREQ_4000,
1991 AZF_FREQ_4800,
1992 AZF_FREQ_5512,
1993 AZF_FREQ_6620,
1994 AZF_FREQ_8000,
1995 AZF_FREQ_9600,
1996 AZF_FREQ_11025,
1997 AZF_FREQ_13240,
1998 AZF_FREQ_16000,
1999 AZF_FREQ_22050,
2000 AZF_FREQ_32000,
2001 AZF_FREQ_44100,
2002 AZF_FREQ_48000,
2003 AZF_FREQ_66200
2004};
2005
Takashi Iwai3e2fd042017-06-07 14:18:16 +02002006static const struct snd_pcm_hw_constraint_list snd_azf3328_hw_constraints_rates = {
Andreas Mohr02330fba2008-05-16 12:18:29 +02002007 .count = ARRAY_SIZE(snd_azf3328_fixed_rates),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 .list = snd_azf3328_fixed_rates,
2009 .mask = 0,
2010};
2011
2012/*****************************************************************/
2013
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002014static int
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002015snd_azf3328_pcm_open(struct snd_pcm_substream *substream,
2016 enum snd_azf3328_codec_type codec_type
2017)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018{
Takashi Iwai95de7762005-11-17 15:02:42 +01002019 struct snd_azf3328 *chip = snd_pcm_substream_chip(substream);
2020 struct snd_pcm_runtime *runtime = substream->runtime;
Andreas Mohrda237f32010-12-27 21:17:26 +01002021 struct snd_azf3328_codec_data *codec = &chip->codecs[codec_type];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
Andreas Mohrda237f32010-12-27 21:17:26 +01002023 codec->substream = substream;
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002024
2025 /* same parameters for all our codecs - at least we think so... */
2026 runtime->hw = snd_azf3328_hardware;
2027
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2029 &snd_azf3328_hw_constraints_rates);
Andreas Mohrda237f32010-12-27 21:17:26 +01002030 runtime->private_data = codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 return 0;
2032}
2033
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002034static int
Andreas Mohrda237f32010-12-27 21:17:26 +01002035snd_azf3328_pcm_playback_open(struct snd_pcm_substream *substream)
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002036{
2037 return snd_azf3328_pcm_open(substream, AZF_CODEC_PLAYBACK);
2038}
2039
2040static int
Andreas Mohrda237f32010-12-27 21:17:26 +01002041snd_azf3328_pcm_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042{
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002043 return snd_azf3328_pcm_open(substream, AZF_CODEC_CAPTURE);
2044}
2045
2046static int
Andreas Mohrda237f32010-12-27 21:17:26 +01002047snd_azf3328_pcm_i2s_out_open(struct snd_pcm_substream *substream)
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002048{
2049 return snd_azf3328_pcm_open(substream, AZF_CODEC_I2S_OUT);
2050}
2051
2052static int
Andreas Mohrda237f32010-12-27 21:17:26 +01002053snd_azf3328_pcm_close(struct snd_pcm_substream *substream
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002054)
2055{
Andreas Mohrda237f32010-12-27 21:17:26 +01002056 struct snd_azf3328_codec_data *codec =
2057 substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
Andreas Mohrda237f32010-12-27 21:17:26 +01002059 codec->substream = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 return 0;
2061}
2062
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063/******************************************************************/
2064
Julia Lawall6769e9882016-09-02 00:13:10 +02002065static const struct snd_pcm_ops snd_azf3328_playback_ops = {
Andreas Mohrda237f32010-12-27 21:17:26 +01002066 .open = snd_azf3328_pcm_playback_open,
2067 .close = snd_azf3328_pcm_close,
Andreas Mohrda237f32010-12-27 21:17:26 +01002068 .prepare = snd_azf3328_pcm_prepare,
2069 .trigger = snd_azf3328_pcm_trigger,
2070 .pointer = snd_azf3328_pcm_pointer
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071};
2072
Julia Lawall6769e9882016-09-02 00:13:10 +02002073static const struct snd_pcm_ops snd_azf3328_capture_ops = {
Andreas Mohrda237f32010-12-27 21:17:26 +01002074 .open = snd_azf3328_pcm_capture_open,
2075 .close = snd_azf3328_pcm_close,
Andreas Mohrda237f32010-12-27 21:17:26 +01002076 .prepare = snd_azf3328_pcm_prepare,
2077 .trigger = snd_azf3328_pcm_trigger,
2078 .pointer = snd_azf3328_pcm_pointer
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002079};
2080
Julia Lawall6769e9882016-09-02 00:13:10 +02002081static const struct snd_pcm_ops snd_azf3328_i2s_out_ops = {
Andreas Mohrda237f32010-12-27 21:17:26 +01002082 .open = snd_azf3328_pcm_i2s_out_open,
2083 .close = snd_azf3328_pcm_close,
Andreas Mohrda237f32010-12-27 21:17:26 +01002084 .prepare = snd_azf3328_pcm_prepare,
2085 .trigger = snd_azf3328_pcm_trigger,
2086 .pointer = snd_azf3328_pcm_pointer
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087};
2088
Bill Pembertone23e7a12012-12-06 12:35:10 -05002089static int
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002090snd_azf3328_pcm(struct snd_azf3328 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091{
Dan Carpenter7f788e02015-02-25 16:32:45 +03002092 /* pcm devices */
2093 enum { AZF_PCMDEV_STD, AZF_PCMDEV_I2S_OUT, NUM_AZF_PCMDEVS };
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002094
Takashi Iwai95de7762005-11-17 15:02:42 +01002095 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 int err;
2097
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002098 err = snd_pcm_new(chip->card, "AZF3328 DSP", AZF_PCMDEV_STD,
2099 1, 1, &pcm);
2100 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 return err;
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002102 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
2103 &snd_azf3328_playback_ops);
2104 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
2105 &snd_azf3328_capture_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106
2107 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 pcm->info_flags = 0;
2109 strcpy(pcm->name, chip->card->shortname);
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002110 /* same pcm object for playback/capture (see snd_pcm_new() above) */
2111 chip->pcm[AZF_CODEC_PLAYBACK] = pcm;
2112 chip->pcm[AZF_CODEC_CAPTURE] = pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113
Takashi Iwai830e7b02019-12-09 10:48:59 +01002114 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, &chip->pci->dev,
2115 64*1024, 64*1024);
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002116
2117 err = snd_pcm_new(chip->card, "AZF3328 I2S OUT", AZF_PCMDEV_I2S_OUT,
2118 1, 0, &pcm);
2119 if (err < 0)
2120 return err;
2121 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
2122 &snd_azf3328_i2s_out_ops);
2123
2124 pcm->private_data = chip;
2125 pcm->info_flags = 0;
2126 strcpy(pcm->name, chip->card->shortname);
2127 chip->pcm[AZF_CODEC_I2S_OUT] = pcm;
2128
Takashi Iwai830e7b02019-12-09 10:48:59 +01002129 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, &chip->pci->dev,
2130 64*1024, 64*1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 return 0;
2133}
2134
2135/******************************************************************/
2136
Andreas Mohr02330fba2008-05-16 12:18:29 +02002137/*** NOTE: the physical timer resolution actually is 1024000 ticks per second
2138 *** (probably derived from main crystal via a divider of 24),
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002139 *** but announcing those attributes to user-space would make programs
2140 *** configure the timer to a 1 tick value, resulting in an absolutely fatal
2141 *** timer IRQ storm.
2142 *** Thus I chose to announce a down-scaled virtual timer to the outside and
2143 *** calculate real timer countdown values internally.
2144 *** (the scale factor can be set via module parameter "seqtimer_scaling").
2145 ***/
2146
2147static int
Takashi Iwai95de7762005-11-17 15:02:42 +01002148snd_azf3328_timer_start(struct snd_timer *timer)
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002149{
Takashi Iwai95de7762005-11-17 15:02:42 +01002150 struct snd_azf3328 *chip;
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002151 unsigned long flags;
2152 unsigned int delay;
2153
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002154 chip = snd_timer_chip(timer);
2155 delay = ((timer->sticks * seqtimer_scaling) - 1) & TIMER_VALUE_MASK;
Andreas Mohre2f87262006-05-17 11:04:19 +02002156 if (delay < 49) {
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002157 /* uhoh, that's not good, since user-space won't know about
2158 * this timing tweak
2159 * (we need to do it to avoid a lockup, though) */
2160
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002161 dev_dbg(chip->card->dev, "delay was too low (%d)!\n", delay);
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002162 delay = 49; /* minimum time is 49 ticks */
2163 }
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002164 dev_dbg(chip->card->dev, "setting timer countdown value %d\n", delay);
Andreas Mohr02330fba2008-05-16 12:18:29 +02002165 delay |= TIMER_COUNTDOWN_ENABLE | TIMER_IRQ_ENABLE;
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002166 spin_lock_irqsave(&chip->reg_lock, flags);
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002167 snd_azf3328_ctrl_outl(chip, IDX_IO_TIMER_VALUE, delay);
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002168 spin_unlock_irqrestore(&chip->reg_lock, flags);
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002169 return 0;
2170}
2171
2172static int
Takashi Iwai95de7762005-11-17 15:02:42 +01002173snd_azf3328_timer_stop(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
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002178 chip = snd_timer_chip(timer);
2179 spin_lock_irqsave(&chip->reg_lock, flags);
2180 /* disable timer countdown and interrupt */
Andreas Mohr79741502010-11-21 12:09:32 +01002181 /* Hmm, should we write TIMER_IRQ_ACK here?
2182 YES indeed, otherwise a rogue timer operation - which prompts
2183 ALSA(?) to call repeated stop() in vain, but NOT start() -
2184 will never end (value 0x03 is kept shown in control byte).
2185 Simply manually poking 0x04 _once_ immediately successfully stops
2186 the hardware/ALSA interrupt activity. */
2187 snd_azf3328_ctrl_outb(chip, IDX_IO_TIMER_VALUE + 3, 0x04);
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002188 spin_unlock_irqrestore(&chip->reg_lock, flags);
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002189 return 0;
2190}
2191
2192
2193static int
Takashi Iwai95de7762005-11-17 15:02:42 +01002194snd_azf3328_timer_precise_resolution(struct snd_timer *timer,
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002195 unsigned long *num, unsigned long *den)
2196{
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002197 *num = 1;
2198 *den = 1024000 / seqtimer_scaling;
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002199 return 0;
2200}
2201
Takashi Iwai95de7762005-11-17 15:02:42 +01002202static struct snd_timer_hardware snd_azf3328_timer_hw = {
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002203 .flags = SNDRV_TIMER_HW_AUTO,
2204 .resolution = 977, /* 1000000/1024000 = 0.9765625us */
2205 .ticks = 1024000, /* max tick count, defined by the value register; actually it's not 1024000, but 1048576, but we don't care */
2206 .start = snd_azf3328_timer_start,
2207 .stop = snd_azf3328_timer_stop,
2208 .precise_resolution = snd_azf3328_timer_precise_resolution,
2209};
2210
Bill Pembertone23e7a12012-12-06 12:35:10 -05002211static int
Takashi Iwai95de7762005-11-17 15:02:42 +01002212snd_azf3328_timer(struct snd_azf3328 *chip, int device)
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002213{
Takashi Iwai95de7762005-11-17 15:02:42 +01002214 struct snd_timer *timer = NULL;
2215 struct snd_timer_id tid;
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002216 int err;
2217
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002218 tid.dev_class = SNDRV_TIMER_CLASS_CARD;
2219 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
2220 tid.card = chip->card->number;
2221 tid.device = device;
2222 tid.subdevice = 0;
2223
2224 snd_azf3328_timer_hw.resolution *= seqtimer_scaling;
2225 snd_azf3328_timer_hw.ticks /= seqtimer_scaling;
Andreas Mohr02330fba2008-05-16 12:18:29 +02002226
2227 err = snd_timer_new(chip->card, "AZF3328", &tid, &timer);
2228 if (err < 0)
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002229 goto out;
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002230
2231 strcpy(timer->name, "AZF3328 timer");
2232 timer->private_data = chip;
2233 timer->hw = snd_azf3328_timer_hw;
2234
2235 chip->timer = timer;
2236
Andreas Mohr02330fba2008-05-16 12:18:29 +02002237 snd_azf3328_timer_stop(timer);
2238
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002239 err = 0;
2240
2241out:
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002242 return err;
2243}
2244
2245/******************************************************************/
2246
Takashi Iwai8c5823e2021-07-15 09:58:33 +02002247static void
2248snd_azf3328_free(struct snd_card *card)
Andreas Mohr02330fba2008-05-16 12:18:29 +02002249{
Takashi Iwai8c5823e2021-07-15 09:58:33 +02002250 struct snd_azf3328 *chip = card->private_data;
Andreas Mohr02330fba2008-05-16 12:18:29 +02002251
Andreas Mohrb5dc20c2011-02-19 00:49:32 +01002252 snd_azf3328_mixer_reset(chip);
Andreas Mohr02330fba2008-05-16 12:18:29 +02002253
2254 snd_azf3328_timer_stop(chip->timer);
2255 snd_azf3328_gameport_free(chip);
Andreas Mohr02330fba2008-05-16 12:18:29 +02002256}
2257
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258#if 0
2259/* check whether a bit can be modified */
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002260static void
Andreas Mohr02330fba2008-05-16 12:18:29 +02002261snd_azf3328_test_bit(unsigned unsigned reg, int bit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262{
2263 unsigned char val, valoff, valon;
2264
2265 val = inb(reg);
2266
2267 outb(val & ~(1 << bit), reg);
2268 valoff = inb(reg);
2269
2270 outb(val|(1 << bit), reg);
2271 valon = inb(reg);
Andreas Mohr02330fba2008-05-16 12:18:29 +02002272
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 outb(val, reg);
2274
Andreas Mohr78df6172009-07-12 22:17:54 +02002275 printk(KERN_DEBUG "reg %04x bit %d: %02x %02x %02x\n",
Andreas Mohr02330fba2008-05-16 12:18:29 +02002276 reg, bit, val, valoff, valon
2277 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278}
2279#endif
2280
Andreas Mohr02330fba2008-05-16 12:18:29 +02002281static inline void
Takashi Iwai95de7762005-11-17 15:02:42 +01002282snd_azf3328_debug_show_ports(const struct snd_azf3328 *chip)
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002283{
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002284 u16 tmp;
2285
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002286 dev_dbg(chip->card->dev,
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002287 "ctrl_io 0x%lx, game_io 0x%lx, mpu_io 0x%lx, "
Andreas Mohr02330fba2008-05-16 12:18:29 +02002288 "opl3_io 0x%lx, mixer_io 0x%lx, irq %d\n",
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002289 chip->ctrl_io, chip->game_io, chip->mpu_io,
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002290 chip->opl3_io, chip->mixer_io, chip->irq);
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002291
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002292 dev_dbg(chip->card->dev,
2293 "game %02x %02x %02x %02x %02x %02x\n",
Andreas Mohr02330fba2008-05-16 12:18:29 +02002294 snd_azf3328_game_inb(chip, 0),
2295 snd_azf3328_game_inb(chip, 1),
2296 snd_azf3328_game_inb(chip, 2),
2297 snd_azf3328_game_inb(chip, 3),
2298 snd_azf3328_game_inb(chip, 4),
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002299 snd_azf3328_game_inb(chip, 5));
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002300
Andreas Mohr02330fba2008-05-16 12:18:29 +02002301 for (tmp = 0; tmp < 0x07; tmp += 1)
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002302 dev_dbg(chip->card->dev,
2303 "mpu_io 0x%04x\n", inb(chip->mpu_io + tmp));
Andreas Mohr02330fba2008-05-16 12:18:29 +02002304
2305 for (tmp = 0; tmp <= 0x07; tmp += 1)
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002306 dev_dbg(chip->card->dev,
2307 "0x%02x: game200 0x%04x, game208 0x%04x\n",
Andreas Mohr02330fba2008-05-16 12:18:29 +02002308 tmp, inb(0x200 + tmp), inb(0x208 + tmp));
2309
2310 for (tmp = 0; tmp <= 0x01; tmp += 1)
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002311 dev_dbg(chip->card->dev,
Andreas Mohr02330fba2008-05-16 12:18:29 +02002312 "0x%02x: mpu300 0x%04x, mpu310 0x%04x, mpu320 0x%04x, "
2313 "mpu330 0x%04x opl388 0x%04x opl38c 0x%04x\n",
2314 tmp,
2315 inb(0x300 + tmp),
2316 inb(0x310 + tmp),
2317 inb(0x320 + tmp),
2318 inb(0x330 + tmp),
2319 inb(0x388 + tmp),
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002320 inb(0x38c + tmp));
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002321
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002322 for (tmp = 0; tmp < AZF_IO_SIZE_CTRL; tmp += 2)
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002323 dev_dbg(chip->card->dev,
2324 "ctrl 0x%02x: 0x%04x\n",
2325 tmp, snd_azf3328_ctrl_inw(chip, tmp));
Andreas Mohre24a1212007-03-26 12:49:45 +02002326
2327 for (tmp = 0; tmp < AZF_IO_SIZE_MIXER; tmp += 2)
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002328 dev_dbg(chip->card->dev,
2329 "mixer 0x%02x: 0x%04x\n",
2330 tmp, snd_azf3328_mixer_inw(chip, tmp));
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002331}
2332
Bill Pembertone23e7a12012-12-06 12:35:10 -05002333static int
Takashi Iwai95de7762005-11-17 15:02:42 +01002334snd_azf3328_create(struct snd_card *card,
Andreas Mohr02330fba2008-05-16 12:18:29 +02002335 struct pci_dev *pci,
Takashi Iwai8c5823e2021-07-15 09:58:33 +02002336 unsigned long device_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337{
Takashi Iwai8c5823e2021-07-15 09:58:33 +02002338 struct snd_azf3328 *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 int err;
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002340 u8 dma_init;
2341 enum snd_azf3328_codec_type codec_type;
Andreas Mohrda237f32010-12-27 21:17:26 +01002342 struct snd_azf3328_codec_data *codec_setup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
Takashi Iwai8c5823e2021-07-15 09:58:33 +02002344 err = pcim_enable_device(pci);
Andreas Mohr02330fba2008-05-16 12:18:29 +02002345 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 return err;
2347
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 spin_lock_init(&chip->reg_lock);
2349 chip->card = card;
2350 chip->pci = pci;
2351 chip->irq = -1;
2352
2353 /* check if we can restrict PCI DMA transfers to 24 bits */
Takashi Iwai669f65e2021-01-14 13:54:11 +01002354 if (dma_set_mask_and_coherent(&pci->dev, DMA_BIT_MASK(24))) {
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002355 dev_err(card->dev,
2356 "architecture does not support 24bit PCI busmaster DMA\n"
Andreas Mohr02330fba2008-05-16 12:18:29 +02002357 );
Takashi Iwai8c5823e2021-07-15 09:58:33 +02002358 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 }
2360
Andreas Mohr02330fba2008-05-16 12:18:29 +02002361 err = pci_request_regions(pci, "Aztech AZF3328");
2362 if (err < 0)
Takashi Iwai8c5823e2021-07-15 09:58:33 +02002363 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002365 chip->ctrl_io = pci_resource_start(pci, 0);
Andreas Mohr02330fba2008-05-16 12:18:29 +02002366 chip->game_io = pci_resource_start(pci, 1);
2367 chip->mpu_io = pci_resource_start(pci, 2);
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002368 chip->opl3_io = pci_resource_start(pci, 3);
Andreas Mohr02330fba2008-05-16 12:18:29 +02002369 chip->mixer_io = pci_resource_start(pci, 4);
2370
Andreas Mohr9fd8d362010-12-27 21:17:00 +01002371 codec_setup = &chip->codecs[AZF_CODEC_PLAYBACK];
2372 codec_setup->io_base = chip->ctrl_io + AZF_IO_OFFS_CODEC_PLAYBACK;
Andreas Mohrda237f32010-12-27 21:17:26 +01002373 codec_setup->lock = &chip->reg_lock;
2374 codec_setup->type = AZF_CODEC_PLAYBACK;
Andreas Mohr9fd8d362010-12-27 21:17:00 +01002375 codec_setup->name = "PLAYBACK";
2376
2377 codec_setup = &chip->codecs[AZF_CODEC_CAPTURE];
2378 codec_setup->io_base = chip->ctrl_io + AZF_IO_OFFS_CODEC_CAPTURE;
Andreas Mohrda237f32010-12-27 21:17:26 +01002379 codec_setup->lock = &chip->reg_lock;
2380 codec_setup->type = AZF_CODEC_CAPTURE;
Andreas Mohr9fd8d362010-12-27 21:17:00 +01002381 codec_setup->name = "CAPTURE";
2382
2383 codec_setup = &chip->codecs[AZF_CODEC_I2S_OUT];
2384 codec_setup->io_base = chip->ctrl_io + AZF_IO_OFFS_CODEC_I2S_OUT;
Andreas Mohrda237f32010-12-27 21:17:26 +01002385 codec_setup->lock = &chip->reg_lock;
2386 codec_setup->type = AZF_CODEC_I2S_OUT;
Andreas Mohr9fd8d362010-12-27 21:17:00 +01002387 codec_setup->name = "I2S_OUT";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388
Takashi Iwai8c5823e2021-07-15 09:58:33 +02002389 if (devm_request_irq(&pci->dev, pci->irq, snd_azf3328_interrupt,
2390 IRQF_SHARED, KBUILD_MODNAME, chip)) {
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002391 dev_err(card->dev, "unable to grab IRQ %d\n", pci->irq);
Takashi Iwai8c5823e2021-07-15 09:58:33 +02002392 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 }
2394 chip->irq = pci->irq;
Takashi Iwaiaefd1862019-12-10 07:34:06 +01002395 card->sync_irq = chip->irq;
Takashi Iwai8c5823e2021-07-15 09:58:33 +02002396 card->private_free = snd_azf3328_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 pci_set_master(pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002399 snd_azf3328_debug_show_ports(chip);
Andreas Mohr02330fba2008-05-16 12:18:29 +02002400
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 /* create mixer interface & switches */
Andreas Mohr02330fba2008-05-16 12:18:29 +02002402 err = snd_azf3328_mixer_new(chip);
2403 if (err < 0)
Takashi Iwai8c5823e2021-07-15 09:58:33 +02002404 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002406 /* standard codec init stuff */
2407 /* default DMA init value */
2408 dma_init = DMA_RUN_SOMETHING2|DMA_EPILOGUE_SOMETHING|DMA_SOMETHING_ELSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002410 for (codec_type = AZF_CODEC_PLAYBACK;
2411 codec_type <= AZF_CODEC_I2S_OUT; ++codec_type) {
2412 struct snd_azf3328_codec_data *codec =
2413 &chip->codecs[codec_type];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414
Andreas Mohradf59312010-12-27 21:16:43 +01002415 /* shutdown codecs to reduce power / noise */
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002416 /* have ...ctrl_codec_activity() act properly */
Jiapeng Chongc2378132021-02-07 15:02:41 +08002417 codec->running = true;
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002418 snd_azf3328_ctrl_codec_activity(chip, codec_type, 0);
2419
Andreas Mohrda237f32010-12-27 21:17:26 +01002420 spin_lock_irq(codec->lock);
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002421 snd_azf3328_codec_outb(codec, IDX_IO_CODEC_DMA_FLAGS,
2422 dma_init);
Andreas Mohrda237f32010-12-27 21:17:26 +01002423 spin_unlock_irq(codec->lock);
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425
Takashi Iwai8c5823e2021-07-15 09:58:33 +02002426 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427}
2428
Bill Pembertone23e7a12012-12-06 12:35:10 -05002429static int
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002430snd_azf3328_probe(struct pci_dev *pci, const struct pci_device_id *pci_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431{
2432 static int dev;
Takashi Iwai95de7762005-11-17 15:02:42 +01002433 struct snd_card *card;
2434 struct snd_azf3328 *chip;
2435 struct snd_opl3 *opl3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 int err;
2437
Takashi Iwai8c5823e2021-07-15 09:58:33 +02002438 if (dev >= SNDRV_CARDS)
2439 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 if (!enable[dev]) {
2441 dev++;
Takashi Iwai8c5823e2021-07-15 09:58:33 +02002442 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 }
2444
Takashi Iwai8c5823e2021-07-15 09:58:33 +02002445 err = snd_devm_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
2446 sizeof(*chip), &card);
Takashi Iwaie58de7b2008-12-28 16:44:30 +01002447 if (err < 0)
Takashi Iwai8c5823e2021-07-15 09:58:33 +02002448 return err;
2449 chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
2451 strcpy(card->driver, "AZF3328");
2452 strcpy(card->shortname, "Aztech AZF3328 (PCI168)");
2453
Takashi Iwai8c5823e2021-07-15 09:58:33 +02002454 err = snd_azf3328_create(card, pci, pci_id->driver_data);
Andreas Mohr02330fba2008-05-16 12:18:29 +02002455 if (err < 0)
Takashi Iwai8c5823e2021-07-15 09:58:33 +02002456 return err;
Andreas Mohrca54bde2006-05-17 11:02:24 +02002457
Andreas Mohr78df6172009-07-12 22:17:54 +02002458 /* chose to use MPU401_HW_AZT2320 ID instead of MPU401_HW_MPU401,
2459 since our hardware ought to be similar, thus use same ID. */
Andreas Mohr02330fba2008-05-16 12:18:29 +02002460 err = snd_mpu401_uart_new(
Andreas Mohr78df6172009-07-12 22:17:54 +02002461 card, 0,
Clemens Ladischdba8b462011-09-13 11:24:41 +02002462 MPU401_HW_AZT2320, chip->mpu_io,
2463 MPU401_INFO_INTEGRATED | MPU401_INFO_IRQ_HOOK,
2464 -1, &chip->rmidi
Andreas Mohr02330fba2008-05-16 12:18:29 +02002465 );
2466 if (err < 0) {
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002467 dev_err(card->dev, "no MPU-401 device at 0x%lx?\n",
Andreas Mohr02330fba2008-05-16 12:18:29 +02002468 chip->mpu_io
2469 );
Takashi Iwai8c5823e2021-07-15 09:58:33 +02002470 return err;
Andreas Mohrd91c64c2005-10-25 11:17:45 +02002471 }
2472
Andreas Mohr02330fba2008-05-16 12:18:29 +02002473 err = snd_azf3328_timer(chip, 0);
2474 if (err < 0)
Takashi Iwai8c5823e2021-07-15 09:58:33 +02002475 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002477 err = snd_azf3328_pcm(chip);
Andreas Mohr02330fba2008-05-16 12:18:29 +02002478 if (err < 0)
Takashi Iwai8c5823e2021-07-15 09:58:33 +02002479 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480
Andreas Mohr02330fba2008-05-16 12:18:29 +02002481 if (snd_opl3_create(card, chip->opl3_io, chip->opl3_io+2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 OPL3_HW_AUTO, 1, &opl3) < 0) {
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002483 dev_err(card->dev, "no OPL3 device at 0x%lx-0x%lx?\n",
Andreas Mohr02330fba2008-05-16 12:18:29 +02002484 chip->opl3_io, chip->opl3_io+2
2485 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 } else {
Andreas Mohr02330fba2008-05-16 12:18:29 +02002487 /* need to use IDs 1, 2 since ID 0 is snd_azf3328_timer above */
2488 err = snd_opl3_timer_new(opl3, 1, 2);
2489 if (err < 0)
Takashi Iwai8c5823e2021-07-15 09:58:33 +02002490 return err;
Andreas Mohr02330fba2008-05-16 12:18:29 +02002491 err = snd_opl3_hwdep_new(opl3, 0, 1, NULL);
2492 if (err < 0)
Takashi Iwai8c5823e2021-07-15 09:58:33 +02002493 return err;
Alban Bedel87c9e7d2012-02-25 16:15:57 +01002494 opl3->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 }
2496
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 sprintf(card->longname, "%s at 0x%lx, irq %i",
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002498 card->shortname, chip->ctrl_io, chip->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499
Andreas Mohr02330fba2008-05-16 12:18:29 +02002500 err = snd_card_register(card);
2501 if (err < 0)
Takashi Iwai8c5823e2021-07-15 09:58:33 +02002502 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503
2504#ifdef MODULE
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002505 dev_info(card->dev,
2506 "Sound driver for Aztech AZF3328-based soundcards such as PCI168.\n");
2507 dev_info(card->dev,
2508 "Hardware was completely undocumented, unfortunately.\n");
2509 dev_info(card->dev,
2510 "Feel free to contact andi AT lisas.de for bug reports etc.!\n");
2511 dev_info(card->dev,
2512 "User-scalable sequencer timer set to %dHz (1024000Hz / %d).\n",
2513 1024000 / seqtimer_scaling, seqtimer_scaling);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514#endif
2515
Andreas Mohr02330fba2008-05-16 12:18:29 +02002516 snd_azf3328_gameport(chip, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517
2518 pci_set_drvdata(pci, card);
2519 dev++;
Takashi Iwai8c5823e2021-07-15 09:58:33 +02002520 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521}
2522
Takashi Iwaic7561cd2012-08-14 18:12:04 +02002523#ifdef CONFIG_PM_SLEEP
Andreas Mohr78df6172009-07-12 22:17:54 +02002524static inline void
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002525snd_azf3328_suspend_regs(const struct snd_azf3328 *chip,
2526 unsigned long io_addr, unsigned count, u32 *saved_regs)
Andreas Mohr78df6172009-07-12 22:17:54 +02002527{
2528 unsigned reg;
2529
2530 for (reg = 0; reg < count; ++reg) {
2531 *saved_regs = inl(io_addr);
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002532 dev_dbg(chip->card->dev, "suspend: io 0x%04lx: 0x%08x\n",
Andreas Mohr78df6172009-07-12 22:17:54 +02002533 io_addr, *saved_regs);
2534 ++saved_regs;
2535 io_addr += sizeof(*saved_regs);
2536 }
2537}
2538
Andreas Mohrb5dc20c2011-02-19 00:49:32 +01002539static inline void
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002540snd_azf3328_resume_regs(const struct snd_azf3328 *chip,
2541 const u32 *saved_regs,
Andreas Mohrb5dc20c2011-02-19 00:49:32 +01002542 unsigned long io_addr,
2543 unsigned count
2544)
2545{
2546 unsigned reg;
2547
2548 for (reg = 0; reg < count; ++reg) {
2549 outl(*saved_regs, io_addr);
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002550 dev_dbg(chip->card->dev,
2551 "resume: io 0x%04lx: 0x%08x --> 0x%08x\n",
Andreas Mohrb5dc20c2011-02-19 00:49:32 +01002552 io_addr, *saved_regs, inl(io_addr));
2553 ++saved_regs;
2554 io_addr += sizeof(*saved_regs);
2555 }
2556}
2557
2558static inline void
2559snd_azf3328_suspend_ac97(struct snd_azf3328 *chip)
2560{
2561#ifdef AZF_USE_AC97_LAYER
2562 snd_ac97_suspend(chip->ac97);
2563#else
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002564 snd_azf3328_suspend_regs(chip, chip->mixer_io,
Andreas Mohrb5dc20c2011-02-19 00:49:32 +01002565 ARRAY_SIZE(chip->saved_regs_mixer), chip->saved_regs_mixer);
2566
2567 /* make sure to disable master volume etc. to prevent looping sound */
2568 snd_azf3328_mixer_mute_control_master(chip, 1);
2569 snd_azf3328_mixer_mute_control_pcm(chip, 1);
2570#endif /* AZF_USE_AC97_LAYER */
2571}
2572
2573static inline void
2574snd_azf3328_resume_ac97(const struct snd_azf3328 *chip)
2575{
2576#ifdef AZF_USE_AC97_LAYER
2577 snd_ac97_resume(chip->ac97);
2578#else
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002579 snd_azf3328_resume_regs(chip, chip->saved_regs_mixer, chip->mixer_io,
Andreas Mohrb5dc20c2011-02-19 00:49:32 +01002580 ARRAY_SIZE(chip->saved_regs_mixer));
2581
2582 /* unfortunately with 32bit transfers, IDX_MIXER_PLAY_MASTER (0x02)
2583 and IDX_MIXER_RESET (offset 0x00) get touched at the same time,
2584 resulting in a mixer reset condition persisting until _after_
2585 master vol was restored. Thus master vol needs an extra restore. */
2586 outw(((u16 *)chip->saved_regs_mixer)[1], chip->mixer_io + 2);
2587#endif /* AZF_USE_AC97_LAYER */
2588}
2589
Andreas Mohrca54bde2006-05-17 11:02:24 +02002590static int
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002591snd_azf3328_suspend(struct device *dev)
Andreas Mohrca54bde2006-05-17 11:02:24 +02002592{
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002593 struct snd_card *card = dev_get_drvdata(dev);
Andreas Mohrca54bde2006-05-17 11:02:24 +02002594 struct snd_azf3328 *chip = card->private_data;
Andreas Mohr78df6172009-07-12 22:17:54 +02002595 u16 *saved_regs_ctrl_u16;
Andreas Mohrca54bde2006-05-17 11:02:24 +02002596
2597 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
Andreas Mohr02330fba2008-05-16 12:18:29 +02002598
Andreas Mohrb5dc20c2011-02-19 00:49:32 +01002599 snd_azf3328_suspend_ac97(chip);
Andreas Mohr02330fba2008-05-16 12:18:29 +02002600
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002601 snd_azf3328_suspend_regs(chip, chip->ctrl_io,
Andreas Mohr78df6172009-07-12 22:17:54 +02002602 ARRAY_SIZE(chip->saved_regs_ctrl), chip->saved_regs_ctrl);
Andreas Mohr627d3e72008-06-23 11:50:47 +02002603
2604 /* manually store the one currently relevant write-only reg, too */
Andreas Mohr78df6172009-07-12 22:17:54 +02002605 saved_regs_ctrl_u16 = (u16 *)chip->saved_regs_ctrl;
2606 saved_regs_ctrl_u16[IDX_IO_6AH / 2] = chip->shadow_reg_ctrl_6AH;
Andreas Mohr627d3e72008-06-23 11:50:47 +02002607
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002608 snd_azf3328_suspend_regs(chip, chip->game_io,
Andreas Mohr78df6172009-07-12 22:17:54 +02002609 ARRAY_SIZE(chip->saved_regs_game), chip->saved_regs_game);
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002610 snd_azf3328_suspend_regs(chip, chip->mpu_io,
Andreas Mohr78df6172009-07-12 22:17:54 +02002611 ARRAY_SIZE(chip->saved_regs_mpu), chip->saved_regs_mpu);
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002612 snd_azf3328_suspend_regs(chip, chip->opl3_io,
Andreas Mohr78df6172009-07-12 22:17:54 +02002613 ARRAY_SIZE(chip->saved_regs_opl3), chip->saved_regs_opl3);
Andreas Mohrca54bde2006-05-17 11:02:24 +02002614 return 0;
2615}
2616
2617static int
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002618snd_azf3328_resume(struct device *dev)
Andreas Mohrca54bde2006-05-17 11:02:24 +02002619{
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002620 struct snd_card *card = dev_get_drvdata(dev);
Andreas Mohrdfbf9512009-07-05 13:55:46 +02002621 const struct snd_azf3328 *chip = card->private_data;
Andreas Mohrca54bde2006-05-17 11:02:24 +02002622
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002623 snd_azf3328_resume_regs(chip, chip->saved_regs_game, chip->game_io,
Andreas Mohr78df6172009-07-12 22:17:54 +02002624 ARRAY_SIZE(chip->saved_regs_game));
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002625 snd_azf3328_resume_regs(chip, chip->saved_regs_mpu, chip->mpu_io,
Andreas Mohr78df6172009-07-12 22:17:54 +02002626 ARRAY_SIZE(chip->saved_regs_mpu));
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002627 snd_azf3328_resume_regs(chip, chip->saved_regs_opl3, chip->opl3_io,
Andreas Mohr78df6172009-07-12 22:17:54 +02002628 ARRAY_SIZE(chip->saved_regs_opl3));
2629
Andreas Mohrb5dc20c2011-02-19 00:49:32 +01002630 snd_azf3328_resume_ac97(chip);
Andreas Mohr78df6172009-07-12 22:17:54 +02002631
Takashi Iwai4a8d9d72014-02-25 14:04:46 +01002632 snd_azf3328_resume_regs(chip, chip->saved_regs_ctrl, chip->ctrl_io,
Andreas Mohr78df6172009-07-12 22:17:54 +02002633 ARRAY_SIZE(chip->saved_regs_ctrl));
Andreas Mohrca54bde2006-05-17 11:02:24 +02002634
2635 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
2636 return 0;
2637}
Andreas Mohrca54bde2006-05-17 11:02:24 +02002638
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002639static SIMPLE_DEV_PM_OPS(snd_azf3328_pm, snd_azf3328_suspend, snd_azf3328_resume);
2640#define SND_AZF3328_PM_OPS &snd_azf3328_pm
2641#else
2642#define SND_AZF3328_PM_OPS NULL
Takashi Iwaic7561cd2012-08-14 18:12:04 +02002643#endif /* CONFIG_PM_SLEEP */
Andreas Mohrca54bde2006-05-17 11:02:24 +02002644
Takashi Iwaie9f66d92012-04-24 12:25:00 +02002645static struct pci_driver azf3328_driver = {
Takashi Iwai3733e422011-06-10 16:20:20 +02002646 .name = KBUILD_MODNAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 .id_table = snd_azf3328_ids,
2648 .probe = snd_azf3328_probe,
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002649 .driver = {
2650 .pm = SND_AZF3328_PM_OPS,
2651 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652};
2653
Takashi Iwaie9f66d92012-04-24 12:25:00 +02002654module_pci_driver(azf3328_driver);