blob: ff39a0c7277b6d42f646ca0790fffa4ca8fd9c5c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * card-als4000.c - driver for Avance Logic ALS4000 based soundcards.
3 * Copyright (C) 2000 by Bart Hartgers <bart@etpmod.phys.tue.nl>,
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02004 * Jaroslav Kysela <perex@perex.cz>
Andreas Mohrcb780cd2008-08-21 21:55:57 +02005 * Copyright (C) 2002, 2008 by Andreas Mohr <hw7oshyuv3001@sneakemail.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * Framework borrowed from Massimo Piccioni's card-als100.c.
8 *
Andreas Mohrba7301c2005-11-17 11:03:31 +01009 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * NOTES
25 *
26 * Since Avance does not provide any meaningful documentation, and I
27 * bought an ALS4000 based soundcard, I was forced to base this driver
28 * on reverse engineering.
29 *
Andreas Mohrcb780cd2008-08-21 21:55:57 +020030 * Note: this is no longer true (thank you!):
31 * pretty verbose chip docu (ALS4000a.PDF) can be found on the ALSA web site.
32 * Page numbers stated anywhere below with the "SPECS_PAGE:" tag
33 * refer to: ALS4000a.PDF specs Ver 1.0, May 28th, 1998.
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 *
35 * The ALS4000 seems to be the PCI-cousin of the ALS100. It contains an
36 * ALS100-like SB DSP/mixer, an OPL3 synth, a MPU401 and a gameport
37 * interface. These subsystems can be mapped into ISA io-port space,
38 * using the PCI-interface. In addition, the PCI-bit provides DMA and IRQ
39 * services to the subsystems.
40 *
41 * While ALS4000 is very similar to a SoundBlaster, the differences in
42 * DMA and capturing require more changes to the SoundBlaster than
43 * desirable, so I made this separate driver.
44 *
45 * The ALS4000 can do real full duplex playback/capture.
46 *
47 * FMDAC:
48 * - 0x4f -> port 0x14
49 * - port 0x15 |= 1
50 *
51 * Enable/disable 3D sound:
52 * - 0x50 -> port 0x14
53 * - change bit 6 (0x40) of port 0x15
54 *
55 * Set QSound:
56 * - 0xdb -> port 0x14
57 * - set port 0x15:
58 * 0x3e (mode 3), 0x3c (mode 2), 0x3a (mode 1), 0x38 (mode 0)
59 *
60 * Set KSound:
61 * - value -> some port 0x0c0d
62 *
Andreas Mohrba7301c2005-11-17 11:03:31 +010063 * ToDo:
Andreas Mohrc0874442008-08-20 10:04:56 +020064 * - by default, don't enable legacy game and use PCI game I/O
Andreas Mohrba7301c2005-11-17 11:03:31 +010065 * - power management? (card can do voice wakeup according to datasheet!!)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 */
67
Takashi Iwai6cbbfe12015-01-28 16:49:33 +010068#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#include <linux/init.h>
70#include <linux/pci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#include <linux/gameport.h>
Paul Gortmaker65a77212011-07-15 13:13:37 -040072#include <linux/module.h>
Matthias Gehre910638a2006-03-28 01:56:48 -080073#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#include <sound/core.h>
75#include <sound/pcm.h>
76#include <sound/rawmidi.h>
77#include <sound/mpu401.h>
78#include <sound/opl3.h>
79#include <sound/sb.h>
80#include <sound/initval.h>
81
Andreas Mohrcb780cd2008-08-21 21:55:57 +020082MODULE_AUTHOR("Bart Hartgers <bart@etpmod.phys.tue.nl>, Andreas Mohr");
Linus Torvalds1da177e2005-04-16 15:20:36 -070083MODULE_DESCRIPTION("Avance Logic ALS4000");
84MODULE_LICENSE("GPL");
85MODULE_SUPPORTED_DEVICE("{{Avance Logic,ALS4000}}");
86
87#if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
88#define SUPPORT_JOYSTICK 1
89#endif
90
91static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
92static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
Rusty Russella67ff6a2011-12-15 13:49:36 +103093static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
Linus Torvalds1da177e2005-04-16 15:20:36 -070094#ifdef SUPPORT_JOYSTICK
95static int joystick_port[SNDRV_CARDS];
96#endif
97
98module_param_array(index, int, NULL, 0444);
99MODULE_PARM_DESC(index, "Index value for ALS4000 soundcard.");
100module_param_array(id, charp, NULL, 0444);
101MODULE_PARM_DESC(id, "ID string for ALS4000 soundcard.");
102module_param_array(enable, bool, NULL, 0444);
103MODULE_PARM_DESC(enable, "Enable ALS4000 soundcard.");
104#ifdef SUPPORT_JOYSTICK
105module_param_array(joystick_port, int, NULL, 0444);
106MODULE_PARM_DESC(joystick_port, "Joystick port address for ALS4000 soundcard. (0 = disabled)");
107#endif
108
Takashi Iwai17c39d92005-11-17 15:02:01 +0100109struct snd_card_als4000 {
Andreas Mohrba7301c2005-11-17 11:03:31 +0100110 /* most frequent access first */
Andreas Mohrc0874442008-08-20 10:04:56 +0200111 unsigned long iobase;
Andreas Mohrba7301c2005-11-17 11:03:31 +0100112 struct pci_dev *pci;
Takashi Iwai70352912005-11-17 16:16:36 +0100113 struct snd_sb *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114#ifdef SUPPORT_JOYSTICK
115 struct gameport *gameport;
116#endif
Takashi Iwai17c39d92005-11-17 15:02:01 +0100117};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Benoit Taine9baa3c32014-08-08 15:56:03 +0200119static const struct pci_device_id snd_als4000_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 { 0x4005, 0x4000, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, }, /* ALS4000 */
121 { 0, }
122};
123
124MODULE_DEVICE_TABLE(pci, snd_als4000_ids);
125
Andreas Mohrc0874442008-08-20 10:04:56 +0200126enum als4k_iobase_t {
Andreas Mohrcb780cd2008-08-21 21:55:57 +0200127 /* IOx: B == Byte, W = Word, D = DWord; SPECS_PAGE: 37 */
Andreas Mohrc0874442008-08-20 10:04:56 +0200128 ALS4K_IOD_00_AC97_ACCESS = 0x00,
129 ALS4K_IOW_04_AC97_READ = 0x04,
130 ALS4K_IOB_06_AC97_STATUS = 0x06,
131 ALS4K_IOB_07_IRQSTATUS = 0x07,
132 ALS4K_IOD_08_GCR_DATA = 0x08,
133 ALS4K_IOB_0C_GCR_INDEX = 0x0c,
Andreas Mohrcb780cd2008-08-21 21:55:57 +0200134 ALS4K_IOB_0E_IRQTYPE_SB_CR1E_MPU = 0x0e,
Andreas Mohrc0874442008-08-20 10:04:56 +0200135 ALS4K_IOB_10_ADLIB_ADDR0 = 0x10,
136 ALS4K_IOB_11_ADLIB_ADDR1 = 0x11,
137 ALS4K_IOB_12_ADLIB_ADDR2 = 0x12,
138 ALS4K_IOB_13_ADLIB_ADDR3 = 0x13,
139 ALS4K_IOB_14_MIXER_INDEX = 0x14,
140 ALS4K_IOB_15_MIXER_DATA = 0x15,
Andreas Mohrcb780cd2008-08-21 21:55:57 +0200141 ALS4K_IOB_16_ESP_RESET = 0x16,
142 ALS4K_IOB_16_ACK_FOR_CR1E = 0x16, /* 2nd function */
Andreas Mohrc0874442008-08-20 10:04:56 +0200143 ALS4K_IOB_18_OPL_ADDR0 = 0x18,
144 ALS4K_IOB_19_OPL_ADDR1 = 0x19,
145 ALS4K_IOB_1A_ESP_RD_DATA = 0x1a,
146 ALS4K_IOB_1C_ESP_CMD_DATA = 0x1c,
147 ALS4K_IOB_1C_ESP_WR_STATUS = 0x1c, /* 2nd function */
148 ALS4K_IOB_1E_ESP_RD_STATUS8 = 0x1e,
149 ALS4K_IOB_1F_ESP_RD_STATUS16 = 0x1f,
150 ALS4K_IOB_20_ESP_GAMEPORT_200 = 0x20,
151 ALS4K_IOB_21_ESP_GAMEPORT_201 = 0x21,
152 ALS4K_IOB_30_MIDI_DATA = 0x30,
153 ALS4K_IOB_31_MIDI_STATUS = 0x31,
154 ALS4K_IOB_31_MIDI_COMMAND = 0x31, /* 2nd function */
155};
156
Andreas Mohrcb780cd2008-08-21 21:55:57 +0200157enum als4k_iobase_0e_t {
158 ALS4K_IOB_0E_MPU_IRQ = 0x10,
159 ALS4K_IOB_0E_CR1E_IRQ = 0x40,
160 ALS4K_IOB_0E_SB_DMA_IRQ = 0x80,
Andreas Mohrc0874442008-08-20 10:04:56 +0200161};
162
Andreas Mohrcb780cd2008-08-21 21:55:57 +0200163enum als4k_gcr_t { /* all registers 32bit wide; SPECS_PAGE: 38 to 42 */
164 ALS4K_GCR8C_MISC_CTRL = 0x8c,
165 ALS4K_GCR90_TEST_MODE_REG = 0x90,
166 ALS4K_GCR91_DMA0_ADDR = 0x91,
167 ALS4K_GCR92_DMA0_MODE_COUNT = 0x92,
168 ALS4K_GCR93_DMA1_ADDR = 0x93,
169 ALS4K_GCR94_DMA1_MODE_COUNT = 0x94,
170 ALS4K_GCR95_DMA3_ADDR = 0x95,
171 ALS4K_GCR96_DMA3_MODE_COUNT = 0x96,
172 ALS4K_GCR99_DMA_EMULATION_CTRL = 0x99,
173 ALS4K_GCRA0_FIFO1_CURRENT_ADDR = 0xa0,
174 ALS4K_GCRA1_FIFO1_STATUS_BYTECOUNT = 0xa1,
175 ALS4K_GCRA2_FIFO2_PCIADDR = 0xa2,
176 ALS4K_GCRA3_FIFO2_COUNT = 0xa3,
177 ALS4K_GCRA4_FIFO2_CURRENT_ADDR = 0xa4,
178 ALS4K_GCRA5_FIFO1_STATUS_BYTECOUNT = 0xa5,
179 ALS4K_GCRA6_PM_CTRL = 0xa6,
180 ALS4K_GCRA7_PCI_ACCESS_STORAGE = 0xa7,
181 ALS4K_GCRA8_LEGACY_CFG1 = 0xa8,
182 ALS4K_GCRA9_LEGACY_CFG2 = 0xa9,
183 ALS4K_GCRFF_DUMMY_SCRATCH = 0xff,
Andreas Mohrc0874442008-08-20 10:04:56 +0200184};
185
Andreas Mohrcb780cd2008-08-21 21:55:57 +0200186enum als4k_gcr8c_t {
187 ALS4K_GCR8C_IRQ_MASK_CTRL_ENABLE = 0x8000,
188 ALS4K_GCR8C_CHIP_REV_MASK = 0xf0000
189};
190
191static inline void snd_als4k_iobase_writeb(unsigned long iobase,
192 enum als4k_iobase_t reg,
193 u8 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
Andreas Mohrcb780cd2008-08-21 21:55:57 +0200195 outb(val, iobase + reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
Andreas Mohrcb780cd2008-08-21 21:55:57 +0200198static inline void snd_als4k_iobase_writel(unsigned long iobase,
199 enum als4k_iobase_t reg,
200 u32 val)
201{
202 outl(val, iobase + reg);
203}
204
205static inline u8 snd_als4k_iobase_readb(unsigned long iobase,
206 enum als4k_iobase_t reg)
207{
208 return inb(iobase + reg);
209}
210
211static inline u32 snd_als4k_iobase_readl(unsigned long iobase,
212 enum als4k_iobase_t reg)
213{
214 return inl(iobase + reg);
215}
216
217static inline void snd_als4k_gcr_write_addr(unsigned long iobase,
Andreas Mohrc0874442008-08-20 10:04:56 +0200218 enum als4k_gcr_t reg,
219 u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
Andreas Mohrcb780cd2008-08-21 21:55:57 +0200221 snd_als4k_iobase_writeb(iobase, ALS4K_IOB_0C_GCR_INDEX, reg);
222 snd_als4k_iobase_writel(iobase, ALS4K_IOD_08_GCR_DATA, val);
223}
224
225static inline void snd_als4k_gcr_write(struct snd_sb *sb,
226 enum als4k_gcr_t reg,
227 u32 val)
228{
229 snd_als4k_gcr_write_addr(sb->alt_port, reg, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
Andreas Mohrcb780cd2008-08-21 21:55:57 +0200232static inline u32 snd_als4k_gcr_read_addr(unsigned long iobase,
Andreas Mohrc0874442008-08-20 10:04:56 +0200233 enum als4k_gcr_t reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
Andreas Mohrcb780cd2008-08-21 21:55:57 +0200235 /* SPECS_PAGE: 37/38 */
236 snd_als4k_iobase_writeb(iobase, ALS4K_IOB_0C_GCR_INDEX, reg);
237 return snd_als4k_iobase_readl(iobase, ALS4K_IOD_08_GCR_DATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
239
Andreas Mohrcb780cd2008-08-21 21:55:57 +0200240static inline u32 snd_als4k_gcr_read(struct snd_sb *sb, enum als4k_gcr_t reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
Andreas Mohrcb780cd2008-08-21 21:55:57 +0200242 return snd_als4k_gcr_read_addr(sb->alt_port, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
Andreas Mohrcb780cd2008-08-21 21:55:57 +0200245enum als4k_cr_t { /* all registers 8bit wide; SPECS_PAGE: 20 to 23 */
246 ALS4K_CR0_SB_CONFIG = 0x00,
247 ALS4K_CR2_MISC_CONTROL = 0x02,
248 ALS4K_CR3_CONFIGURATION = 0x03,
249 ALS4K_CR17_FIFO_STATUS = 0x17,
250 ALS4K_CR18_ESP_MAJOR_VERSION = 0x18,
251 ALS4K_CR19_ESP_MINOR_VERSION = 0x19,
252 ALS4K_CR1A_MPU401_UART_MODE_CONTROL = 0x1a,
253 ALS4K_CR1C_FIFO2_BLOCK_LENGTH_LO = 0x1c,
254 ALS4K_CR1D_FIFO2_BLOCK_LENGTH_HI = 0x1d,
255 ALS4K_CR1E_FIFO2_CONTROL = 0x1e, /* secondary PCM FIFO (recording) */
256 ALS4K_CR3A_MISC_CONTROL = 0x3a,
257 ALS4K_CR3B_CRC32_BYTE0 = 0x3b, /* for testing, activate via CR3A */
258 ALS4K_CR3C_CRC32_BYTE1 = 0x3c,
259 ALS4K_CR3D_CRC32_BYTE2 = 0x3d,
260 ALS4K_CR3E_CRC32_BYTE3 = 0x3e,
261};
262
263enum als4k_cr0_t {
264 ALS4K_CR0_DMA_CONTIN_MODE_CTRL = 0x02, /* IRQ/FIFO controlled for 0/1 */
265 ALS4K_CR0_DMA_90H_MODE_CTRL = 0x04, /* IRQ/FIFO controlled for 0/1 */
266 ALS4K_CR0_MX80_81_REG_WRITE_ENABLE = 0x80,
267};
268
269static inline void snd_als4_cr_write(struct snd_sb *chip,
270 enum als4k_cr_t reg,
271 u8 data)
272{
273 /* Control Register is reg | 0xc0 (bit 7, 6 set) on sbmixer_index
274 * NOTE: assumes chip->mixer_lock to be locked externally already!
275 * SPECS_PAGE: 6 */
276 snd_sbmixer_write(chip, reg | 0xc0, data);
277}
278
279static inline u8 snd_als4_cr_read(struct snd_sb *chip,
280 enum als4k_cr_t reg)
281{
282 /* NOTE: assumes chip->mixer_lock to be locked externally already! */
283 return snd_sbmixer_read(chip, reg | 0xc0);
284}
285
286
287
Takashi Iwai17c39d92005-11-17 15:02:01 +0100288static void snd_als4000_set_rate(struct snd_sb *chip, unsigned int rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
290 if (!(chip->mode & SB_RATE_LOCK)) {
291 snd_sbdsp_command(chip, SB_DSP_SAMPLE_RATE_OUT);
292 snd_sbdsp_command(chip, rate>>8);
293 snd_sbdsp_command(chip, rate);
294 }
295}
296
Takashi Iwai17c39d92005-11-17 15:02:01 +0100297static inline void snd_als4000_set_capture_dma(struct snd_sb *chip,
298 dma_addr_t addr, unsigned size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
Andreas Mohrcb780cd2008-08-21 21:55:57 +0200300 /* SPECS_PAGE: 40 */
301 snd_als4k_gcr_write(chip, ALS4K_GCRA2_FIFO2_PCIADDR, addr);
302 snd_als4k_gcr_write(chip, ALS4K_GCRA3_FIFO2_COUNT, (size-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
Takashi Iwai17c39d92005-11-17 15:02:01 +0100305static inline void snd_als4000_set_playback_dma(struct snd_sb *chip,
Andreas Mohrc0874442008-08-20 10:04:56 +0200306 dma_addr_t addr,
307 unsigned size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
Andreas Mohrcb780cd2008-08-21 21:55:57 +0200309 /* SPECS_PAGE: 38 */
310 snd_als4k_gcr_write(chip, ALS4K_GCR91_DMA0_ADDR, addr);
311 snd_als4k_gcr_write(chip, ALS4K_GCR92_DMA0_MODE_COUNT,
Andreas Mohrc0874442008-08-20 10:04:56 +0200312 (size-1)|0x180000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
315#define ALS4000_FORMAT_SIGNED (1<<0)
316#define ALS4000_FORMAT_16BIT (1<<1)
317#define ALS4000_FORMAT_STEREO (1<<2)
318
Takashi Iwai17c39d92005-11-17 15:02:01 +0100319static int snd_als4000_get_format(struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
321 int result;
322
323 result = 0;
324 if (snd_pcm_format_signed(runtime->format))
325 result |= ALS4000_FORMAT_SIGNED;
326 if (snd_pcm_format_physical_width(runtime->format) == 16)
327 result |= ALS4000_FORMAT_16BIT;
328 if (runtime->channels > 1)
329 result |= ALS4000_FORMAT_STEREO;
330 return result;
331}
332
333/* structure for setting up playback */
Andreas Mohrba7301c2005-11-17 11:03:31 +0100334static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 unsigned char dsp_cmd, dma_on, dma_off, format;
336} playback_cmd_vals[]={
337/* ALS4000_FORMAT_U8_MONO */
338{ SB_DSP4_OUT8_AI, SB_DSP_DMA8_ON, SB_DSP_DMA8_OFF, SB_DSP4_MODE_UNS_MONO },
339/* ALS4000_FORMAT_S8_MONO */
340{ SB_DSP4_OUT8_AI, SB_DSP_DMA8_ON, SB_DSP_DMA8_OFF, SB_DSP4_MODE_SIGN_MONO },
341/* ALS4000_FORMAT_U16L_MONO */
342{ SB_DSP4_OUT16_AI, SB_DSP_DMA16_ON, SB_DSP_DMA16_OFF, SB_DSP4_MODE_UNS_MONO },
343/* ALS4000_FORMAT_S16L_MONO */
344{ SB_DSP4_OUT16_AI, SB_DSP_DMA16_ON, SB_DSP_DMA16_OFF, SB_DSP4_MODE_SIGN_MONO },
345/* ALS4000_FORMAT_U8_STEREO */
346{ SB_DSP4_OUT8_AI, SB_DSP_DMA8_ON, SB_DSP_DMA8_OFF, SB_DSP4_MODE_UNS_STEREO },
347/* ALS4000_FORMAT_S8_STEREO */
348{ SB_DSP4_OUT8_AI, SB_DSP_DMA8_ON, SB_DSP_DMA8_OFF, SB_DSP4_MODE_SIGN_STEREO },
349/* ALS4000_FORMAT_U16L_STEREO */
350{ SB_DSP4_OUT16_AI, SB_DSP_DMA16_ON, SB_DSP_DMA16_OFF, SB_DSP4_MODE_UNS_STEREO },
351/* ALS4000_FORMAT_S16L_STEREO */
352{ SB_DSP4_OUT16_AI, SB_DSP_DMA16_ON, SB_DSP_DMA16_OFF, SB_DSP4_MODE_SIGN_STEREO },
353};
354#define playback_cmd(chip) (playback_cmd_vals[(chip)->playback_format])
355
356/* structure for setting up capture */
357enum { CMD_WIDTH8=0x04, CMD_SIGNED=0x10, CMD_MONO=0x80, CMD_STEREO=0xA0 };
Andreas Mohrba7301c2005-11-17 11:03:31 +0100358static const unsigned char capture_cmd_vals[]=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
360CMD_WIDTH8|CMD_MONO, /* ALS4000_FORMAT_U8_MONO */
361CMD_WIDTH8|CMD_SIGNED|CMD_MONO, /* ALS4000_FORMAT_S8_MONO */
362CMD_MONO, /* ALS4000_FORMAT_U16L_MONO */
363CMD_SIGNED|CMD_MONO, /* ALS4000_FORMAT_S16L_MONO */
364CMD_WIDTH8|CMD_STEREO, /* ALS4000_FORMAT_U8_STEREO */
365CMD_WIDTH8|CMD_SIGNED|CMD_STEREO, /* ALS4000_FORMAT_S8_STEREO */
366CMD_STEREO, /* ALS4000_FORMAT_U16L_STEREO */
367CMD_SIGNED|CMD_STEREO, /* ALS4000_FORMAT_S16L_STEREO */
368};
369#define capture_cmd(chip) (capture_cmd_vals[(chip)->capture_format])
370
Takashi Iwai17c39d92005-11-17 15:02:01 +0100371static int snd_als4000_hw_params(struct snd_pcm_substream *substream,
372 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
374 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
375}
376
Takashi Iwai17c39d92005-11-17 15:02:01 +0100377static int snd_als4000_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
379 snd_pcm_lib_free_pages(substream);
380 return 0;
381}
382
Takashi Iwai17c39d92005-11-17 15:02:01 +0100383static int snd_als4000_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
Takashi Iwai17c39d92005-11-17 15:02:01 +0100385 struct snd_sb *chip = snd_pcm_substream_chip(substream);
386 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 unsigned long size;
388 unsigned count;
389
390 chip->capture_format = snd_als4000_get_format(runtime);
391
392 size = snd_pcm_lib_buffer_bytes(substream);
393 count = snd_pcm_lib_period_bytes(substream);
394
395 if (chip->capture_format & ALS4000_FORMAT_16BIT)
Andreas Mohrcb780cd2008-08-21 21:55:57 +0200396 count >>= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 count--;
398
Takashi Iwai17c39d92005-11-17 15:02:01 +0100399 spin_lock_irq(&chip->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 snd_als4000_set_rate(chip, runtime->rate);
401 snd_als4000_set_capture_dma(chip, runtime->dma_addr, size);
Takashi Iwai17c39d92005-11-17 15:02:01 +0100402 spin_unlock_irq(&chip->reg_lock);
403 spin_lock_irq(&chip->mixer_lock);
Andreas Mohrcb780cd2008-08-21 21:55:57 +0200404 snd_als4_cr_write(chip, ALS4K_CR1C_FIFO2_BLOCK_LENGTH_LO, count & 0xff);
405 snd_als4_cr_write(chip, ALS4K_CR1D_FIFO2_BLOCK_LENGTH_HI, count >> 8);
Takashi Iwai17c39d92005-11-17 15:02:01 +0100406 spin_unlock_irq(&chip->mixer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 return 0;
408}
409
Takashi Iwai17c39d92005-11-17 15:02:01 +0100410static int snd_als4000_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
Takashi Iwai17c39d92005-11-17 15:02:01 +0100412 struct snd_sb *chip = snd_pcm_substream_chip(substream);
413 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 unsigned long size;
415 unsigned count;
416
417 chip->playback_format = snd_als4000_get_format(runtime);
418
419 size = snd_pcm_lib_buffer_bytes(substream);
420 count = snd_pcm_lib_period_bytes(substream);
421
422 if (chip->playback_format & ALS4000_FORMAT_16BIT)
Andreas Mohrcb780cd2008-08-21 21:55:57 +0200423 count >>= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 count--;
425
426 /* FIXME: from second playback on, there's a lot more clicks and pops
427 * involved here than on first playback. Fiddling with
428 * tons of different settings didn't help (DMA, speaker on/off,
429 * reordering, ...). Something seems to get enabled on playback
430 * that I haven't found out how to disable again, which then causes
431 * the switching pops to reach the speakers the next time here. */
Takashi Iwai17c39d92005-11-17 15:02:01 +0100432 spin_lock_irq(&chip->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 snd_als4000_set_rate(chip, runtime->rate);
434 snd_als4000_set_playback_dma(chip, runtime->dma_addr, size);
435
436 /* SPEAKER_ON not needed, since dma_on seems to also enable speaker */
437 /* snd_sbdsp_command(chip, SB_DSP_SPEAKER_ON); */
438 snd_sbdsp_command(chip, playback_cmd(chip).dsp_cmd);
439 snd_sbdsp_command(chip, playback_cmd(chip).format);
Andreas Mohrcb780cd2008-08-21 21:55:57 +0200440 snd_sbdsp_command(chip, count & 0xff);
441 snd_sbdsp_command(chip, count >> 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 snd_sbdsp_command(chip, playback_cmd(chip).dma_off);
Takashi Iwai17c39d92005-11-17 15:02:01 +0100443 spin_unlock_irq(&chip->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 return 0;
446}
447
Takashi Iwai17c39d92005-11-17 15:02:01 +0100448static int snd_als4000_capture_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
Takashi Iwai17c39d92005-11-17 15:02:01 +0100450 struct snd_sb *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 int result = 0;
452
Andreas Mohrc0874442008-08-20 10:04:56 +0200453 /* FIXME race condition in here!!!
454 chip->mode non-atomic update gets consistently protected
455 by reg_lock always, _except_ for this place!!
456 Probably need to take reg_lock as outer (or inner??) lock, too.
457 (or serialize both lock operations? probably not, though... - racy?)
458 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 spin_lock(&chip->mixer_lock);
Takashi Iwai70352912005-11-17 16:16:36 +0100460 switch (cmd) {
461 case SNDRV_PCM_TRIGGER_START:
462 case SNDRV_PCM_TRIGGER_RESUME:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 chip->mode |= SB_RATE_LOCK_CAPTURE;
Andreas Mohrcb780cd2008-08-21 21:55:57 +0200464 snd_als4_cr_write(chip, ALS4K_CR1E_FIFO2_CONTROL,
465 capture_cmd(chip));
Takashi Iwai70352912005-11-17 16:16:36 +0100466 break;
467 case SNDRV_PCM_TRIGGER_STOP:
468 case SNDRV_PCM_TRIGGER_SUSPEND:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 chip->mode &= ~SB_RATE_LOCK_CAPTURE;
Andreas Mohrcb780cd2008-08-21 21:55:57 +0200470 snd_als4_cr_write(chip, ALS4K_CR1E_FIFO2_CONTROL,
471 capture_cmd(chip));
Takashi Iwai70352912005-11-17 16:16:36 +0100472 break;
473 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 result = -EINVAL;
Takashi Iwai70352912005-11-17 16:16:36 +0100475 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 }
477 spin_unlock(&chip->mixer_lock);
478 return result;
479}
480
Takashi Iwai17c39d92005-11-17 15:02:01 +0100481static int snd_als4000_playback_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
Takashi Iwai17c39d92005-11-17 15:02:01 +0100483 struct snd_sb *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 int result = 0;
485
486 spin_lock(&chip->reg_lock);
Takashi Iwai70352912005-11-17 16:16:36 +0100487 switch (cmd) {
488 case SNDRV_PCM_TRIGGER_START:
489 case SNDRV_PCM_TRIGGER_RESUME:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 chip->mode |= SB_RATE_LOCK_PLAYBACK;
491 snd_sbdsp_command(chip, playback_cmd(chip).dma_on);
Takashi Iwai70352912005-11-17 16:16:36 +0100492 break;
493 case SNDRV_PCM_TRIGGER_STOP:
494 case SNDRV_PCM_TRIGGER_SUSPEND:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 snd_sbdsp_command(chip, playback_cmd(chip).dma_off);
496 chip->mode &= ~SB_RATE_LOCK_PLAYBACK;
Takashi Iwai70352912005-11-17 16:16:36 +0100497 break;
498 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 result = -EINVAL;
Takashi Iwai70352912005-11-17 16:16:36 +0100500 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 }
502 spin_unlock(&chip->reg_lock);
503 return result;
504}
505
Takashi Iwai17c39d92005-11-17 15:02:01 +0100506static snd_pcm_uframes_t snd_als4000_capture_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
Takashi Iwai17c39d92005-11-17 15:02:01 +0100508 struct snd_sb *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 unsigned int result;
510
511 spin_lock(&chip->reg_lock);
Andreas Mohrcb780cd2008-08-21 21:55:57 +0200512 result = snd_als4k_gcr_read(chip, ALS4K_GCRA4_FIFO2_CURRENT_ADDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 spin_unlock(&chip->reg_lock);
Andreas Mohrcb780cd2008-08-21 21:55:57 +0200514 result &= 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return bytes_to_frames( substream->runtime, result );
516}
517
Takashi Iwai17c39d92005-11-17 15:02:01 +0100518static snd_pcm_uframes_t snd_als4000_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
Takashi Iwai17c39d92005-11-17 15:02:01 +0100520 struct snd_sb *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 unsigned result;
522
523 spin_lock(&chip->reg_lock);
Andreas Mohrcb780cd2008-08-21 21:55:57 +0200524 result = snd_als4k_gcr_read(chip, ALS4K_GCRA0_FIFO1_CURRENT_ADDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 spin_unlock(&chip->reg_lock);
Andreas Mohrcb780cd2008-08-21 21:55:57 +0200526 result &= 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 return bytes_to_frames( substream->runtime, result );
528}
529
Andreas Mohrba7301c2005-11-17 11:03:31 +0100530/* FIXME: this IRQ routine doesn't really support IRQ sharing (we always
531 * return IRQ_HANDLED no matter whether we actually had an IRQ flag or not).
532 * ALS4000a.PDF writes that while ACKing IRQ in PCI block will *not* ACK
533 * the IRQ in the SB core, ACKing IRQ in SB block *will* ACK the PCI IRQ
Andreas Mohrcb780cd2008-08-21 21:55:57 +0200534 * register (alt_port + ALS4K_IOB_0E_IRQTYPE_SB_CR1E_MPU). Probably something
Andreas Mohrc0874442008-08-20 10:04:56 +0200535 * could be optimized here to query/write one register only...
Andreas Mohrba7301c2005-11-17 11:03:31 +0100536 * And even if both registers need to be queried, then there's still the
537 * question of whether it's actually correct to ACK PCI IRQ before reading
538 * SB IRQ like we do now, since ALS4000a.PDF mentions that PCI IRQ will *clear*
539 * SB IRQ status.
Andreas Mohrcb780cd2008-08-21 21:55:57 +0200540 * (hmm, SPECS_PAGE: 38 mentions it the other way around!)
Andreas Mohrba7301c2005-11-17 11:03:31 +0100541 * And do we *really* need the lock here for *reading* SB_DSP4_IRQSTATUS??
542 * */
David Howells7d12e782006-10-05 14:55:46 +0100543static irqreturn_t snd_als4000_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
Takashi Iwai17c39d92005-11-17 15:02:01 +0100545 struct snd_sb *chip = dev_id;
Andreas Mohrcb780cd2008-08-21 21:55:57 +0200546 unsigned pci_irqstatus;
547 unsigned sb_irqstatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Andreas Mohrcb780cd2008-08-21 21:55:57 +0200549 /* find out which bit of the ALS4000 PCI block produced the interrupt,
550 SPECS_PAGE: 38, 5 */
551 pci_irqstatus = snd_als4k_iobase_readb(chip->alt_port,
552 ALS4K_IOB_0E_IRQTYPE_SB_CR1E_MPU);
553 if ((pci_irqstatus & ALS4K_IOB_0E_SB_DMA_IRQ)
554 && (chip->playback_substream)) /* playback */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 snd_pcm_period_elapsed(chip->playback_substream);
Andreas Mohrcb780cd2008-08-21 21:55:57 +0200556 if ((pci_irqstatus & ALS4K_IOB_0E_CR1E_IRQ)
557 && (chip->capture_substream)) /* capturing */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 snd_pcm_period_elapsed(chip->capture_substream);
Andreas Mohrcb780cd2008-08-21 21:55:57 +0200559 if ((pci_irqstatus & ALS4K_IOB_0E_MPU_IRQ)
560 && (chip->rmidi)) /* MPU401 interrupt */
David Howells7d12e782006-10-05 14:55:46 +0100561 snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
Andreas Mohrcb780cd2008-08-21 21:55:57 +0200562 /* ACK the PCI block IRQ */
563 snd_als4k_iobase_writeb(chip->alt_port,
564 ALS4K_IOB_0E_IRQTYPE_SB_CR1E_MPU, pci_irqstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 spin_lock(&chip->mixer_lock);
Andreas Mohrcb780cd2008-08-21 21:55:57 +0200567 /* SPECS_PAGE: 20 */
568 sb_irqstatus = snd_sbmixer_read(chip, SB_DSP4_IRQSTATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 spin_unlock(&chip->mixer_lock);
570
Andreas Mohrcb780cd2008-08-21 21:55:57 +0200571 if (sb_irqstatus & SB_IRQTYPE_8BIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 snd_sb_ack_8bit(chip);
Andreas Mohrcb780cd2008-08-21 21:55:57 +0200573 if (sb_irqstatus & SB_IRQTYPE_16BIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 snd_sb_ack_16bit(chip);
Andreas Mohrcb780cd2008-08-21 21:55:57 +0200575 if (sb_irqstatus & SB_IRQTYPE_MPUIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 inb(chip->mpu_port);
Andreas Mohrcb780cd2008-08-21 21:55:57 +0200577 if (sb_irqstatus & ALS4K_IRQTYPE_CR1E_DMA)
578 snd_als4k_iobase_readb(chip->alt_port,
579 ALS4K_IOB_16_ACK_FOR_CR1E);
580
Takashi Iwaid85d8782014-02-25 12:51:05 +0100581 /* dev_dbg(chip->card->dev, "als4000: irq 0x%04x 0x%04x\n",
Andreas Mohrcb780cd2008-08-21 21:55:57 +0200582 pci_irqstatus, sb_irqstatus); */
583
584 /* only ack the things we actually handled above */
585 return IRQ_RETVAL(
586 (pci_irqstatus & (ALS4K_IOB_0E_SB_DMA_IRQ|ALS4K_IOB_0E_CR1E_IRQ|
587 ALS4K_IOB_0E_MPU_IRQ))
588 || (sb_irqstatus & (SB_IRQTYPE_8BIT|SB_IRQTYPE_16BIT|
589 SB_IRQTYPE_MPUIN|ALS4K_IRQTYPE_CR1E_DMA))
590 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592
593/*****************************************************************/
594
Takashi Iwai17c39d92005-11-17 15:02:01 +0100595static struct snd_pcm_hardware snd_als4000_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
597 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
598 SNDRV_PCM_INFO_MMAP_VALID),
599 .formats = SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
600 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE, /* formats */
601 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
602 .rate_min = 4000,
603 .rate_max = 48000,
604 .channels_min = 1,
605 .channels_max = 2,
606 .buffer_bytes_max = 65536,
607 .period_bytes_min = 64,
608 .period_bytes_max = 65536,
609 .periods_min = 1,
610 .periods_max = 1024,
611 .fifo_size = 0
612};
613
Takashi Iwai17c39d92005-11-17 15:02:01 +0100614static struct snd_pcm_hardware snd_als4000_capture =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
616 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
617 SNDRV_PCM_INFO_MMAP_VALID),
618 .formats = SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
619 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE, /* formats */
620 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
621 .rate_min = 4000,
622 .rate_max = 48000,
623 .channels_min = 1,
624 .channels_max = 2,
625 .buffer_bytes_max = 65536,
626 .period_bytes_min = 64,
627 .period_bytes_max = 65536,
628 .periods_min = 1,
629 .periods_max = 1024,
630 .fifo_size = 0
631};
632
633/*****************************************************************/
634
Takashi Iwai17c39d92005-11-17 15:02:01 +0100635static int snd_als4000_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
Takashi Iwai17c39d92005-11-17 15:02:01 +0100637 struct snd_sb *chip = snd_pcm_substream_chip(substream);
638 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640 chip->playback_substream = substream;
641 runtime->hw = snd_als4000_playback;
642 return 0;
643}
644
Takashi Iwai17c39d92005-11-17 15:02:01 +0100645static int snd_als4000_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
Takashi Iwai17c39d92005-11-17 15:02:01 +0100647 struct snd_sb *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
649 chip->playback_substream = NULL;
650 snd_pcm_lib_free_pages(substream);
651 return 0;
652}
653
Takashi Iwai17c39d92005-11-17 15:02:01 +0100654static int snd_als4000_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
Takashi Iwai17c39d92005-11-17 15:02:01 +0100656 struct snd_sb *chip = snd_pcm_substream_chip(substream);
657 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659 chip->capture_substream = substream;
660 runtime->hw = snd_als4000_capture;
661 return 0;
662}
663
Takashi Iwai17c39d92005-11-17 15:02:01 +0100664static int snd_als4000_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
Takashi Iwai17c39d92005-11-17 15:02:01 +0100666 struct snd_sb *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668 chip->capture_substream = NULL;
669 snd_pcm_lib_free_pages(substream);
670 return 0;
671}
672
673/******************************************************************/
674
Takashi Iwai17c39d92005-11-17 15:02:01 +0100675static struct snd_pcm_ops snd_als4000_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 .open = snd_als4000_playback_open,
677 .close = snd_als4000_playback_close,
678 .ioctl = snd_pcm_lib_ioctl,
679 .hw_params = snd_als4000_hw_params,
680 .hw_free = snd_als4000_hw_free,
681 .prepare = snd_als4000_playback_prepare,
682 .trigger = snd_als4000_playback_trigger,
683 .pointer = snd_als4000_playback_pointer
684};
685
Takashi Iwai17c39d92005-11-17 15:02:01 +0100686static struct snd_pcm_ops snd_als4000_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 .open = snd_als4000_capture_open,
688 .close = snd_als4000_capture_close,
689 .ioctl = snd_pcm_lib_ioctl,
690 .hw_params = snd_als4000_hw_params,
691 .hw_free = snd_als4000_hw_free,
692 .prepare = snd_als4000_capture_prepare,
693 .trigger = snd_als4000_capture_trigger,
694 .pointer = snd_als4000_capture_pointer
695};
696
Bill Pembertone23e7a12012-12-06 12:35:10 -0500697static int snd_als4000_pcm(struct snd_sb *chip, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
Takashi Iwai17c39d92005-11-17 15:02:01 +0100699 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 int err;
701
Andreas Mohrcb780cd2008-08-21 21:55:57 +0200702 err = snd_pcm_new(chip->card, "ALS4000 DSP", device, 1, 1, &pcm);
703 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 pcm->private_data = chip;
706 pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
707 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_als4000_playback_ops);
708 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_als4000_capture_ops);
709
710 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
711 64*1024, 64*1024);
712
713 chip->pcm = pcm;
714
715 return 0;
716}
717
718/******************************************************************/
719
Andreas Mohrc0874442008-08-20 10:04:56 +0200720static void snd_als4000_set_addr(unsigned long iobase,
721 unsigned int sb_io,
722 unsigned int mpu_io,
723 unsigned int opl_io,
724 unsigned int game_io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
Andreas Mohrc0874442008-08-20 10:04:56 +0200726 u32 cfg1 = 0;
727 u32 cfg2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Andreas Mohrc0874442008-08-20 10:04:56 +0200729 if (mpu_io > 0)
730 cfg2 |= (mpu_io | 1) << 16;
731 if (sb_io > 0)
732 cfg2 |= (sb_io | 1);
733 if (game_io > 0)
734 cfg1 |= (game_io | 1) << 16;
735 if (opl_io > 0)
736 cfg1 |= (opl_io | 1);
Andreas Mohrcb780cd2008-08-21 21:55:57 +0200737 snd_als4k_gcr_write_addr(iobase, ALS4K_GCRA8_LEGACY_CFG1, cfg1);
738 snd_als4k_gcr_write_addr(iobase, ALS4K_GCRA9_LEGACY_CFG2, cfg2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739}
740
Takashi Iwai70352912005-11-17 16:16:36 +0100741static void snd_als4000_configure(struct snd_sb *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
Andreas Mohrcb780cd2008-08-21 21:55:57 +0200743 u8 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 int i;
745
746 /* do some more configuration */
747 spin_lock_irq(&chip->mixer_lock);
Andreas Mohrcb780cd2008-08-21 21:55:57 +0200748 tmp = snd_als4_cr_read(chip, ALS4K_CR0_SB_CONFIG);
749 snd_als4_cr_write(chip, ALS4K_CR0_SB_CONFIG,
750 tmp|ALS4K_CR0_MX80_81_REG_WRITE_ENABLE);
751 /* always select DMA channel 0, since we do not actually use DMA
752 * SPECS_PAGE: 19/20 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 snd_sbmixer_write(chip, SB_DSP4_DMASETUP, SB_DMASETUP_DMA0);
Andreas Mohrcb780cd2008-08-21 21:55:57 +0200754 snd_als4_cr_write(chip, ALS4K_CR0_SB_CONFIG,
755 tmp & ~ALS4K_CR0_MX80_81_REG_WRITE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 spin_unlock_irq(&chip->mixer_lock);
757
758 spin_lock_irq(&chip->reg_lock);
Andreas Mohrc0874442008-08-20 10:04:56 +0200759 /* enable interrupts */
Andreas Mohrcb780cd2008-08-21 21:55:57 +0200760 snd_als4k_gcr_write(chip, ALS4K_GCR8C_MISC_CTRL,
761 ALS4K_GCR8C_IRQ_MASK_CTRL_ENABLE);
Andreas Mohrc0874442008-08-20 10:04:56 +0200762
Andreas Mohrcb780cd2008-08-21 21:55:57 +0200763 /* SPECS_PAGE: 39 */
764 for (i = ALS4K_GCR91_DMA0_ADDR; i <= ALS4K_GCR96_DMA3_MODE_COUNT; ++i)
765 snd_als4k_gcr_write(chip, i, 0);
Ondrej Zaryb9619232010-08-03 23:57:05 +0200766 /* enable burst mode to prevent dropouts during high PCI bus usage */
Andreas Mohrcb780cd2008-08-21 21:55:57 +0200767 snd_als4k_gcr_write(chip, ALS4K_GCR99_DMA_EMULATION_CTRL,
Ondrej Zaryc4685842010-08-04 21:56:44 +0200768 (snd_als4k_gcr_read(chip, ALS4K_GCR99_DMA_EMULATION_CTRL) & ~0x07) | 0x04);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 spin_unlock_irq(&chip->reg_lock);
770}
771
772#ifdef SUPPORT_JOYSTICK
Bill Pembertone23e7a12012-12-06 12:35:10 -0500773static int snd_als4000_create_gameport(struct snd_card_als4000 *acard, int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
775 struct gameport *gp;
776 struct resource *r;
777 int io_port;
778
779 if (joystick_port[dev] == 0)
780 return -ENODEV;
781
782 if (joystick_port[dev] == 1) { /* auto-detect */
783 for (io_port = 0x200; io_port <= 0x218; io_port += 8) {
784 r = request_region(io_port, 8, "ALS4000 gameport");
785 if (r)
786 break;
787 }
788 } else {
789 io_port = joystick_port[dev];
790 r = request_region(io_port, 8, "ALS4000 gameport");
791 }
792
793 if (!r) {
Takashi Iwaid85d8782014-02-25 12:51:05 +0100794 dev_warn(&acard->pci->dev, "cannot reserve joystick ports\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 return -EBUSY;
796 }
797
798 acard->gameport = gp = gameport_allocate_port();
799 if (!gp) {
Takashi Iwaid85d8782014-02-25 12:51:05 +0100800 dev_err(&acard->pci->dev, "cannot allocate memory for gameport\n");
Takashi Iwaib1d57762005-10-10 11:56:31 +0200801 release_and_free_resource(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 return -ENOMEM;
803 }
804
805 gameport_set_name(gp, "ALS4000 Gameport");
806 gameport_set_phys(gp, "pci%s/gameport0", pci_name(acard->pci));
807 gameport_set_dev_parent(gp, &acard->pci->dev);
808 gp->io = io_port;
809 gameport_set_port_data(gp, r);
810
811 /* Enable legacy joystick port */
Andreas Mohrc0874442008-08-20 10:04:56 +0200812 snd_als4000_set_addr(acard->iobase, 0, 0, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
814 gameport_register_port(acard->gameport);
815
816 return 0;
817}
818
Takashi Iwai17c39d92005-11-17 15:02:01 +0100819static void snd_als4000_free_gameport(struct snd_card_als4000 *acard)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820{
821 if (acard->gameport) {
822 struct resource *r = gameport_get_port_data(acard->gameport);
823
824 gameport_unregister_port(acard->gameport);
825 acard->gameport = NULL;
826
Andreas Mohrc0874442008-08-20 10:04:56 +0200827 /* disable joystick */
828 snd_als4000_set_addr(acard->iobase, 0, 0, 0, 0);
829
Takashi Iwaib1d57762005-10-10 11:56:31 +0200830 release_and_free_resource(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 }
832}
833#else
Takashi Iwai17c39d92005-11-17 15:02:01 +0100834static inline int snd_als4000_create_gameport(struct snd_card_als4000 *acard, int dev) { return -ENOSYS; }
835static inline void snd_als4000_free_gameport(struct snd_card_als4000 *acard) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836#endif
837
Takashi Iwai17c39d92005-11-17 15:02:01 +0100838static void snd_card_als4000_free( struct snd_card *card )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
Andreas Mohrc0874442008-08-20 10:04:56 +0200840 struct snd_card_als4000 *acard = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842 /* make sure that interrupts are disabled */
Andreas Mohrcb780cd2008-08-21 21:55:57 +0200843 snd_als4k_gcr_write_addr(acard->iobase, ALS4K_GCR8C_MISC_CTRL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 /* free resources */
845 snd_als4000_free_gameport(acard);
846 pci_release_regions(acard->pci);
847 pci_disable_device(acard->pci);
848}
849
Bill Pembertone23e7a12012-12-06 12:35:10 -0500850static int snd_card_als4000_probe(struct pci_dev *pci,
851 const struct pci_device_id *pci_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
853 static int dev;
Takashi Iwai17c39d92005-11-17 15:02:01 +0100854 struct snd_card *card;
855 struct snd_card_als4000 *acard;
Andreas Mohrc0874442008-08-20 10:04:56 +0200856 unsigned long iobase;
Takashi Iwai17c39d92005-11-17 15:02:01 +0100857 struct snd_sb *chip;
858 struct snd_opl3 *opl3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 unsigned short word;
860 int err;
861
862 if (dev >= SNDRV_CARDS)
863 return -ENODEV;
864 if (!enable[dev]) {
865 dev++;
866 return -ENOENT;
867 }
868
869 /* enable PCI device */
870 if ((err = pci_enable_device(pci)) < 0) {
871 return err;
872 }
873 /* check, if we can restrict PCI DMA transfers to 24 bits */
Quentin Lambert412b9792015-04-15 16:10:17 +0200874 if (dma_set_mask(&pci->dev, DMA_BIT_MASK(24)) < 0 ||
875 dma_set_coherent_mask(&pci->dev, DMA_BIT_MASK(24)) < 0) {
Takashi Iwaid85d8782014-02-25 12:51:05 +0100876 dev_err(&pci->dev, "architecture does not support 24bit PCI busmaster DMA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 pci_disable_device(pci);
878 return -ENXIO;
879 }
880
881 if ((err = pci_request_regions(pci, "ALS4000")) < 0) {
882 pci_disable_device(pci);
883 return err;
884 }
Andreas Mohrc0874442008-08-20 10:04:56 +0200885 iobase = pci_resource_start(pci, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
887 pci_read_config_word(pci, PCI_COMMAND, &word);
888 pci_write_config_word(pci, PCI_COMMAND, word | PCI_COMMAND_IO);
889 pci_set_master(pci);
890
Takashi Iwai60c57722014-01-29 14:20:19 +0100891 err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
892 sizeof(*acard) /* private_data: acard */,
893 &card);
Takashi Iwaie58de7b2008-12-28 16:44:30 +0100894 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 pci_release_regions(pci);
896 pci_disable_device(pci);
Takashi Iwaie58de7b2008-12-28 16:44:30 +0100897 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 }
899
Andreas Mohrc0874442008-08-20 10:04:56 +0200900 acard = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 acard->pci = pci;
Andreas Mohrc0874442008-08-20 10:04:56 +0200902 acard->iobase = iobase;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 card->private_free = snd_card_als4000_free;
904
905 /* disable all legacy ISA stuff */
Andreas Mohrc0874442008-08-20 10:04:56 +0200906 snd_als4000_set_addr(acard->iobase, 0, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
908 if ((err = snd_sbdsp_create(card,
Andreas Mohrc0874442008-08-20 10:04:56 +0200909 iobase + ALS4K_IOB_10_ADLIB_ADDR0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 pci->irq,
Andreas Mohrcb780cd2008-08-21 21:55:57 +0200911 /* internally registered as IRQF_SHARED in case of ALS4000 SB */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 snd_als4000_interrupt,
913 -1,
914 -1,
915 SB_HW_ALS4000,
916 &chip)) < 0) {
Andreas Mohrba7301c2005-11-17 11:03:31 +0100917 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 }
Takashi Iwai70352912005-11-17 16:16:36 +0100919 acard->chip = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 chip->pci = pci;
Andreas Mohrc0874442008-08-20 10:04:56 +0200922 chip->alt_port = iobase;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
924 snd_als4000_configure(chip);
925
926 strcpy(card->driver, "ALS4000");
927 strcpy(card->shortname, "Avance Logic ALS4000");
928 sprintf(card->longname, "%s at 0x%lx, irq %i",
929 card->shortname, chip->alt_port, chip->irq);
930
931 if ((err = snd_mpu401_uart_new( card, 0, MPU401_HW_ALS4000,
Andreas Mohrc0874442008-08-20 10:04:56 +0200932 iobase + ALS4K_IOB_30_MIDI_DATA,
Clemens Ladischdba8b462011-09-13 11:24:41 +0200933 MPU401_INFO_INTEGRATED |
934 MPU401_INFO_IRQ_HOOK,
935 -1, &chip->rmidi)) < 0) {
Takashi Iwaid85d8782014-02-25 12:51:05 +0100936 dev_err(&pci->dev, "no MPU-401 device at 0x%lx?\n",
Andreas Mohrc0874442008-08-20 10:04:56 +0200937 iobase + ALS4K_IOB_30_MIDI_DATA);
Andreas Mohrba7301c2005-11-17 11:03:31 +0100938 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 }
Andreas Mohrc0874442008-08-20 10:04:56 +0200940 /* FIXME: ALS4000 has interesting MPU401 configuration features
Andreas Mohrcb780cd2008-08-21 21:55:57 +0200941 * at ALS4K_CR1A_MPU401_UART_MODE_CONTROL
942 * (pass-thru / UART switching, fast MIDI clock, etc.),
943 * however there doesn't seem to be an ALSA API for this...
944 * SPECS_PAGE: 21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
946 if ((err = snd_als4000_pcm(chip, 0)) < 0) {
Andreas Mohrba7301c2005-11-17 11:03:31 +0100947 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 }
949 if ((err = snd_sbmixer_new(chip)) < 0) {
Andreas Mohrba7301c2005-11-17 11:03:31 +0100950 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 }
952
Andreas Mohrc0874442008-08-20 10:04:56 +0200953 if (snd_opl3_create(card,
954 iobase + ALS4K_IOB_10_ADLIB_ADDR0,
955 iobase + ALS4K_IOB_12_ADLIB_ADDR2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 OPL3_HW_AUTO, 1, &opl3) < 0) {
Takashi Iwaid85d8782014-02-25 12:51:05 +0100957 dev_err(&pci->dev, "no OPL device at 0x%lx-0x%lx?\n",
Andreas Mohrc0874442008-08-20 10:04:56 +0200958 iobase + ALS4K_IOB_10_ADLIB_ADDR0,
959 iobase + ALS4K_IOB_12_ADLIB_ADDR2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 } else {
961 if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
Andreas Mohrba7301c2005-11-17 11:03:31 +0100962 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 }
964 }
965
966 snd_als4000_create_gameport(acard, dev);
967
968 if ((err = snd_card_register(card)) < 0) {
Andreas Mohrba7301c2005-11-17 11:03:31 +0100969 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 }
971 pci_set_drvdata(pci, card);
972 dev++;
Andreas Mohrba7301c2005-11-17 11:03:31 +0100973 err = 0;
974 goto out;
975
976out_err:
977 snd_card_free(card);
978
979out:
980 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981}
982
Bill Pembertone23e7a12012-12-06 12:35:10 -0500983static void snd_card_als4000_remove(struct pci_dev *pci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984{
985 snd_card_free(pci_get_drvdata(pci));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986}
987
Takashi Iwaic7561cd2012-08-14 18:12:04 +0200988#ifdef CONFIG_PM_SLEEP
Takashi Iwai68cb2b52012-07-02 15:20:37 +0200989static int snd_als4000_suspend(struct device *dev)
Takashi Iwai70352912005-11-17 16:16:36 +0100990{
Takashi Iwai68cb2b52012-07-02 15:20:37 +0200991 struct snd_card *card = dev_get_drvdata(dev);
Takashi Iwai70352912005-11-17 16:16:36 +0100992 struct snd_card_als4000 *acard = card->private_data;
993 struct snd_sb *chip = acard->chip;
994
995 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
996
997 snd_pcm_suspend_all(chip->pcm);
998 snd_sbmixer_suspend(chip);
Takashi Iwai70352912005-11-17 16:16:36 +0100999 return 0;
1000}
1001
Takashi Iwai68cb2b52012-07-02 15:20:37 +02001002static int snd_als4000_resume(struct device *dev)
Takashi Iwai70352912005-11-17 16:16:36 +01001003{
Takashi Iwai68cb2b52012-07-02 15:20:37 +02001004 struct snd_card *card = dev_get_drvdata(dev);
Takashi Iwai70352912005-11-17 16:16:36 +01001005 struct snd_card_als4000 *acard = card->private_data;
1006 struct snd_sb *chip = acard->chip;
1007
Takashi Iwai70352912005-11-17 16:16:36 +01001008 snd_als4000_configure(chip);
1009 snd_sbdsp_reset(chip);
1010 snd_sbmixer_resume(chip);
1011
1012#ifdef SUPPORT_JOYSTICK
1013 if (acard->gameport)
Andreas Mohrc0874442008-08-20 10:04:56 +02001014 snd_als4000_set_addr(acard->iobase, 0, 0, 0, 1);
Takashi Iwai70352912005-11-17 16:16:36 +01001015#endif
1016
1017 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1018 return 0;
1019}
Takashi Iwai70352912005-11-17 16:16:36 +01001020
Takashi Iwai68cb2b52012-07-02 15:20:37 +02001021static SIMPLE_DEV_PM_OPS(snd_als4000_pm, snd_als4000_suspend, snd_als4000_resume);
1022#define SND_ALS4000_PM_OPS &snd_als4000_pm
1023#else
1024#define SND_ALS4000_PM_OPS NULL
Takashi Iwaic7561cd2012-08-14 18:12:04 +02001025#endif /* CONFIG_PM_SLEEP */
Takashi Iwai70352912005-11-17 16:16:36 +01001026
Takashi Iwaie9f66d92012-04-24 12:25:00 +02001027static struct pci_driver als4000_driver = {
Takashi Iwai3733e422011-06-10 16:20:20 +02001028 .name = KBUILD_MODNAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 .id_table = snd_als4000_ids,
1030 .probe = snd_card_als4000_probe,
Bill Pembertone23e7a12012-12-06 12:35:10 -05001031 .remove = snd_card_als4000_remove,
Takashi Iwai68cb2b52012-07-02 15:20:37 +02001032 .driver = {
1033 .pm = SND_ALS4000_PM_OPS,
1034 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035};
1036
Takashi Iwaie9f66d92012-04-24 12:25:00 +02001037module_pci_driver(als4000_driver);