blob: 3a3ba6f547bc3bd97c2773c5822e25e5cd36f97f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Driver for NeoMagic 256AV and 256ZX chipsets.
3 * Copyright (c) 2000 by Takashi Iwai <tiwai@suse.de>
4 *
5 * Based on nm256_audio.c OSS driver in linux kernel.
6 * The original author of OSS nm256 driver wishes to remain anonymous,
7 * so I just put my acknoledgment to him/her here.
8 * The original author's web page is found at
9 * http://www.uglx.org/sony.html
10 *
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 */
26
27#include <sound/driver.h>
28#include <asm/io.h>
29#include <linux/delay.h>
30#include <linux/interrupt.h>
31#include <linux/init.h>
32#include <linux/pci.h>
33#include <linux/slab.h>
34#include <linux/moduleparam.h>
Ingo Molnar62932df2006-01-16 16:34:20 +010035#include <linux/mutex.h>
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <sound/core.h>
38#include <sound/info.h>
39#include <sound/control.h>
40#include <sound/pcm.h>
41#include <sound/ac97_codec.h>
42#include <sound/initval.h>
43
44#define CARD_NAME "NeoMagic 256AV/ZX"
45#define DRIVER_NAME "NM256"
46
47MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
48MODULE_DESCRIPTION("NeoMagic NM256AV/ZX");
49MODULE_LICENSE("GPL");
50MODULE_SUPPORTED_DEVICE("{{NeoMagic,NM256AV},"
51 "{NeoMagic,NM256ZX}}");
52
53/*
54 * some compile conditions.
55 */
56
Takashi Iwai8a3fb4d2005-10-20 17:10:49 +020057static int index = SNDRV_DEFAULT_IDX1; /* Index */
58static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */
59static int playback_bufsize = 16;
60static int capture_bufsize = 16;
61static int force_ac97; /* disabled as default */
62static int buffer_top; /* not specified */
63static int use_cache; /* disabled */
64static int vaio_hack; /* disabled */
65static int reset_workaround;
66static int reset_workaround_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Takashi Iwai8a3fb4d2005-10-20 17:10:49 +020068module_param(index, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069MODULE_PARM_DESC(index, "Index value for " CARD_NAME " soundcard.");
Takashi Iwai8a3fb4d2005-10-20 17:10:49 +020070module_param(id, charp, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071MODULE_PARM_DESC(id, "ID string for " CARD_NAME " soundcard.");
Takashi Iwai8a3fb4d2005-10-20 17:10:49 +020072module_param(playback_bufsize, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073MODULE_PARM_DESC(playback_bufsize, "DAC frame size in kB for " CARD_NAME " soundcard.");
Takashi Iwai8a3fb4d2005-10-20 17:10:49 +020074module_param(capture_bufsize, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075MODULE_PARM_DESC(capture_bufsize, "ADC frame size in kB for " CARD_NAME " soundcard.");
Takashi Iwai8a3fb4d2005-10-20 17:10:49 +020076module_param(force_ac97, bool, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077MODULE_PARM_DESC(force_ac97, "Force to use AC97 codec for " CARD_NAME " soundcard.");
Takashi Iwai8a3fb4d2005-10-20 17:10:49 +020078module_param(buffer_top, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079MODULE_PARM_DESC(buffer_top, "Set the top address of audio buffer for " CARD_NAME " soundcard.");
Takashi Iwai8a3fb4d2005-10-20 17:10:49 +020080module_param(use_cache, bool, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081MODULE_PARM_DESC(use_cache, "Enable the cache for coefficient table access.");
Takashi Iwai8a3fb4d2005-10-20 17:10:49 +020082module_param(vaio_hack, bool, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083MODULE_PARM_DESC(vaio_hack, "Enable workaround for Sony VAIO notebooks.");
Takashi Iwai8a3fb4d2005-10-20 17:10:49 +020084module_param(reset_workaround, bool, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085MODULE_PARM_DESC(reset_workaround, "Enable AC97 RESET workaround for some laptops.");
Takashi Iwai8a3fb4d2005-10-20 17:10:49 +020086module_param(reset_workaround_2, bool, 0444);
John W. Linville47530cf2005-10-19 16:03:10 +020087MODULE_PARM_DESC(reset_workaround_2, "Enable extended AC97 RESET workaround for some other laptops.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Takashi Iwai8a3fb4d2005-10-20 17:10:49 +020089/* just for backward compatibility */
90static int enable;
91module_param(enable, bool, 0444);
92
93
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095/*
96 * hw definitions
97 */
98
99/* The BIOS signature. */
100#define NM_SIGNATURE 0x4e4d0000
101/* Signature mask. */
102#define NM_SIG_MASK 0xffff0000
103
104/* Size of the second memory area. */
105#define NM_PORT2_SIZE 4096
106
107/* The base offset of the mixer in the second memory area. */
108#define NM_MIXER_OFFSET 0x600
109
110/* The maximum size of a coefficient entry. */
111#define NM_MAX_PLAYBACK_COEF_SIZE 0x5000
112#define NM_MAX_RECORD_COEF_SIZE 0x1260
113
114/* The interrupt register. */
115#define NM_INT_REG 0xa04
116/* And its bits. */
117#define NM_PLAYBACK_INT 0x40
118#define NM_RECORD_INT 0x100
119#define NM_MISC_INT_1 0x4000
120#define NM_MISC_INT_2 0x1
121#define NM_ACK_INT(chip, X) snd_nm256_writew(chip, NM_INT_REG, (X) << 1)
122
123/* The AV's "mixer ready" status bit and location. */
124#define NM_MIXER_STATUS_OFFSET 0xa04
125#define NM_MIXER_READY_MASK 0x0800
126#define NM_MIXER_PRESENCE 0xa06
127#define NM_PRESENCE_MASK 0x0050
128#define NM_PRESENCE_VALUE 0x0040
129
130/*
131 * For the ZX. It uses the same interrupt register, but it holds 32
132 * bits instead of 16.
133 */
134#define NM2_PLAYBACK_INT 0x10000
135#define NM2_RECORD_INT 0x80000
136#define NM2_MISC_INT_1 0x8
137#define NM2_MISC_INT_2 0x2
138#define NM2_ACK_INT(chip, X) snd_nm256_writel(chip, NM_INT_REG, (X))
139
140/* The ZX's "mixer ready" status bit and location. */
141#define NM2_MIXER_STATUS_OFFSET 0xa06
142#define NM2_MIXER_READY_MASK 0x0800
143
144/* The playback registers start from here. */
145#define NM_PLAYBACK_REG_OFFSET 0x0
146/* The record registers start from here. */
147#define NM_RECORD_REG_OFFSET 0x200
148
149/* The rate register is located 2 bytes from the start of the register area. */
150#define NM_RATE_REG_OFFSET 2
151
152/* Mono/stereo flag, number of bits on playback, and rate mask. */
153#define NM_RATE_STEREO 1
154#define NM_RATE_BITS_16 2
155#define NM_RATE_MASK 0xf0
156
157/* Playback enable register. */
158#define NM_PLAYBACK_ENABLE_REG (NM_PLAYBACK_REG_OFFSET + 0x1)
159#define NM_PLAYBACK_ENABLE_FLAG 1
160#define NM_PLAYBACK_ONESHOT 2
161#define NM_PLAYBACK_FREERUN 4
162
163/* Mutes the audio output. */
164#define NM_AUDIO_MUTE_REG (NM_PLAYBACK_REG_OFFSET + 0x18)
165#define NM_AUDIO_MUTE_LEFT 0x8000
166#define NM_AUDIO_MUTE_RIGHT 0x0080
167
168/* Recording enable register. */
169#define NM_RECORD_ENABLE_REG (NM_RECORD_REG_OFFSET + 0)
170#define NM_RECORD_ENABLE_FLAG 1
171#define NM_RECORD_FREERUN 2
172
173/* coefficient buffer pointer */
174#define NM_COEFF_START_OFFSET 0x1c
175#define NM_COEFF_END_OFFSET 0x20
176
177/* DMA buffer offsets */
178#define NM_RBUFFER_START (NM_RECORD_REG_OFFSET + 0x4)
179#define NM_RBUFFER_END (NM_RECORD_REG_OFFSET + 0x10)
180#define NM_RBUFFER_WMARK (NM_RECORD_REG_OFFSET + 0xc)
181#define NM_RBUFFER_CURRP (NM_RECORD_REG_OFFSET + 0x8)
182
183#define NM_PBUFFER_START (NM_PLAYBACK_REG_OFFSET + 0x4)
184#define NM_PBUFFER_END (NM_PLAYBACK_REG_OFFSET + 0x14)
185#define NM_PBUFFER_WMARK (NM_PLAYBACK_REG_OFFSET + 0xc)
186#define NM_PBUFFER_CURRP (NM_PLAYBACK_REG_OFFSET + 0x8)
187
Takashi Iwai10754f532005-11-17 15:01:29 +0100188struct nm256_stream {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Takashi Iwai10754f532005-11-17 15:01:29 +0100190 struct nm256 *chip;
191 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 int running;
Takashi Iwai1204de32005-08-16 16:54:12 +0200193 int suspended;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195 u32 buf; /* offset from chip->buffer */
196 int bufsize; /* buffer size in bytes */
197 void __iomem *bufptr; /* mapped pointer */
198 unsigned long bufptr_addr; /* physical address of the mapped pointer */
199
200 int dma_size; /* buffer size of the substream in bytes */
201 int period_size; /* period size in bytes */
202 int periods; /* # of periods */
203 int shift; /* bit shifts */
204 int cur_period; /* current period # */
205
206};
207
Takashi Iwai10754f532005-11-17 15:01:29 +0100208struct nm256 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Takashi Iwai10754f532005-11-17 15:01:29 +0100210 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212 void __iomem *cport; /* control port */
213 struct resource *res_cport; /* its resource */
214 unsigned long cport_addr; /* physical address */
215
216 void __iomem *buffer; /* buffer */
217 struct resource *res_buffer; /* its resource */
218 unsigned long buffer_addr; /* buffer phyiscal address */
219
220 u32 buffer_start; /* start offset from pci resource 0 */
221 u32 buffer_end; /* end offset */
222 u32 buffer_size; /* total buffer size */
223
224 u32 all_coeff_buf; /* coefficient buffer */
225 u32 coeff_buf[2]; /* coefficient buffer for each stream */
226
227 unsigned int coeffs_current: 1; /* coeff. table is loaded? */
228 unsigned int use_cache: 1; /* use one big coef. table */
229 unsigned int reset_workaround: 1; /* Workaround for some laptops to avoid freeze */
John W. Linville47530cf2005-10-19 16:03:10 +0200230 unsigned int reset_workaround_2: 1; /* Extended workaround for some other laptops to avoid freeze */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 int mixer_base; /* register offset of ac97 mixer */
233 int mixer_status_offset; /* offset of mixer status reg. */
234 int mixer_status_mask; /* bit mask to test the mixer status */
235
236 int irq;
Takashi Iwai1204de32005-08-16 16:54:12 +0200237 int irq_acks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 irqreturn_t (*interrupt)(int, void *, struct pt_regs *);
239 int badintrcount; /* counter to check bogus interrupts */
Ingo Molnar62932df2006-01-16 16:34:20 +0100240 struct mutex irq_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Takashi Iwai10754f532005-11-17 15:01:29 +0100242 struct nm256_stream streams[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Takashi Iwai10754f532005-11-17 15:01:29 +0100244 struct snd_ac97 *ac97;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Takashi Iwai10754f532005-11-17 15:01:29 +0100246 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 struct pci_dev *pci;
249
250 spinlock_t reg_lock;
251
252};
253
254
255/*
256 * include coefficient table
257 */
258#include "nm256_coef.c"
259
260
261/*
262 * PCI ids
263 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264static struct pci_device_id snd_nm256_ids[] = {
265 {PCI_VENDOR_ID_NEOMAGIC, PCI_DEVICE_ID_NEOMAGIC_NM256AV_AUDIO, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
266 {PCI_VENDOR_ID_NEOMAGIC, PCI_DEVICE_ID_NEOMAGIC_NM256ZX_AUDIO, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
267 {PCI_VENDOR_ID_NEOMAGIC, PCI_DEVICE_ID_NEOMAGIC_NM256XL_PLUS_AUDIO, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
268 {0,},
269};
270
271MODULE_DEVICE_TABLE(pci, snd_nm256_ids);
272
273
274/*
275 * lowlvel stuffs
276 */
277
Jesper Juhl77933d72005-07-27 11:46:09 -0700278static inline u8
Takashi Iwai10754f532005-11-17 15:01:29 +0100279snd_nm256_readb(struct nm256 *chip, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
281 return readb(chip->cport + offset);
282}
283
Jesper Juhl77933d72005-07-27 11:46:09 -0700284static inline u16
Takashi Iwai10754f532005-11-17 15:01:29 +0100285snd_nm256_readw(struct nm256 *chip, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
287 return readw(chip->cport + offset);
288}
289
Jesper Juhl77933d72005-07-27 11:46:09 -0700290static inline u32
Takashi Iwai10754f532005-11-17 15:01:29 +0100291snd_nm256_readl(struct nm256 *chip, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
293 return readl(chip->cport + offset);
294}
295
Jesper Juhl77933d72005-07-27 11:46:09 -0700296static inline void
Takashi Iwai10754f532005-11-17 15:01:29 +0100297snd_nm256_writeb(struct nm256 *chip, int offset, u8 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
299 writeb(val, chip->cport + offset);
300}
301
Jesper Juhl77933d72005-07-27 11:46:09 -0700302static inline void
Takashi Iwai10754f532005-11-17 15:01:29 +0100303snd_nm256_writew(struct nm256 *chip, int offset, u16 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
305 writew(val, chip->cport + offset);
306}
307
Jesper Juhl77933d72005-07-27 11:46:09 -0700308static inline void
Takashi Iwai10754f532005-11-17 15:01:29 +0100309snd_nm256_writel(struct nm256 *chip, int offset, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
311 writel(val, chip->cport + offset);
312}
313
Jesper Juhl77933d72005-07-27 11:46:09 -0700314static inline void
Takashi Iwai10754f532005-11-17 15:01:29 +0100315snd_nm256_write_buffer(struct nm256 *chip, void *src, int offset, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
317 offset -= chip->buffer_start;
Takashi Iwai99b359b2005-10-20 18:26:44 +0200318#ifdef CONFIG_SND_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 if (offset < 0 || offset >= chip->buffer_size) {
Takashi Iwai10754f532005-11-17 15:01:29 +0100320 snd_printk(KERN_ERR "write_buffer invalid offset = %d size = %d\n",
321 offset, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 return;
323 }
324#endif
325 memcpy_toio(chip->buffer + offset, src, size);
326}
327
328/*
329 * coefficient handlers -- what a magic!
330 */
331
332static u16
333snd_nm256_get_start_offset(int which)
334{
335 u16 offset = 0;
336 while (which-- > 0)
337 offset += coefficient_sizes[which];
338 return offset;
339}
340
341static void
Takashi Iwai10754f532005-11-17 15:01:29 +0100342snd_nm256_load_one_coefficient(struct nm256 *chip, int stream, u32 port, int which)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
344 u32 coeff_buf = chip->coeff_buf[stream];
345 u16 offset = snd_nm256_get_start_offset(which);
346 u16 size = coefficient_sizes[which];
347
348 snd_nm256_write_buffer(chip, coefficients + offset, coeff_buf, size);
349 snd_nm256_writel(chip, port, coeff_buf);
350 /* ??? Record seems to behave differently than playback. */
351 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
352 size--;
353 snd_nm256_writel(chip, port + 4, coeff_buf + size);
354}
355
356static void
Takashi Iwai10754f532005-11-17 15:01:29 +0100357snd_nm256_load_coefficient(struct nm256 *chip, int stream, int number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
359 /* The enable register for the specified engine. */
Takashi Iwai10754f532005-11-17 15:01:29 +0100360 u32 poffset = (stream == SNDRV_PCM_STREAM_CAPTURE ?
361 NM_RECORD_ENABLE_REG : NM_PLAYBACK_ENABLE_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 u32 addr = NM_COEFF_START_OFFSET;
363
Takashi Iwai10754f532005-11-17 15:01:29 +0100364 addr += (stream == SNDRV_PCM_STREAM_CAPTURE ?
365 NM_RECORD_REG_OFFSET : NM_PLAYBACK_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 if (snd_nm256_readb(chip, poffset) & 1) {
368 snd_printd("NM256: Engine was enabled while loading coefficients!\n");
369 return;
370 }
371
372 /* The recording engine uses coefficient values 8-15. */
373 number &= 7;
374 if (stream == SNDRV_PCM_STREAM_CAPTURE)
375 number += 8;
376
377 if (! chip->use_cache) {
378 snd_nm256_load_one_coefficient(chip, stream, addr, number);
379 return;
380 }
381 if (! chip->coeffs_current) {
382 snd_nm256_write_buffer(chip, coefficients, chip->all_coeff_buf,
383 NM_TOTAL_COEFF_COUNT * 4);
384 chip->coeffs_current = 1;
385 } else {
386 u32 base = chip->all_coeff_buf;
387 u32 offset = snd_nm256_get_start_offset(number);
388 u32 end_offset = offset + coefficient_sizes[number];
389 snd_nm256_writel(chip, addr, base + offset);
390 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
391 end_offset--;
392 snd_nm256_writel(chip, addr + 4, base + end_offset);
393 }
394}
395
396
397/* The actual rates supported by the card. */
398static unsigned int samplerates[8] = {
399 8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000,
400};
Takashi Iwai10754f532005-11-17 15:01:29 +0100401static struct snd_pcm_hw_constraint_list constraints_rates = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 .count = ARRAY_SIZE(samplerates),
403 .list = samplerates,
404 .mask = 0,
405};
406
407/*
408 * return the index of the target rate
409 */
410static int
411snd_nm256_fixed_rate(unsigned int rate)
412{
413 unsigned int i;
414 for (i = 0; i < ARRAY_SIZE(samplerates); i++) {
415 if (rate == samplerates[i])
416 return i;
417 }
418 snd_BUG();
419 return 0;
420}
421
422/*
423 * set sample rate and format
424 */
425static void
Takashi Iwai10754f532005-11-17 15:01:29 +0100426snd_nm256_set_format(struct nm256 *chip, struct nm256_stream *s,
427 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
Takashi Iwai10754f532005-11-17 15:01:29 +0100429 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 int rate_index = snd_nm256_fixed_rate(runtime->rate);
431 unsigned char ratebits = (rate_index << 4) & NM_RATE_MASK;
432
433 s->shift = 0;
434 if (snd_pcm_format_width(runtime->format) == 16) {
435 ratebits |= NM_RATE_BITS_16;
436 s->shift++;
437 }
438 if (runtime->channels > 1) {
439 ratebits |= NM_RATE_STEREO;
440 s->shift++;
441 }
442
443 runtime->rate = samplerates[rate_index];
444
445 switch (substream->stream) {
446 case SNDRV_PCM_STREAM_PLAYBACK:
447 snd_nm256_load_coefficient(chip, 0, rate_index); /* 0 = playback */
448 snd_nm256_writeb(chip,
449 NM_PLAYBACK_REG_OFFSET + NM_RATE_REG_OFFSET,
450 ratebits);
451 break;
452 case SNDRV_PCM_STREAM_CAPTURE:
453 snd_nm256_load_coefficient(chip, 1, rate_index); /* 1 = record */
454 snd_nm256_writeb(chip,
455 NM_RECORD_REG_OFFSET + NM_RATE_REG_OFFSET,
456 ratebits);
457 break;
458 }
459}
460
Takashi Iwai1204de32005-08-16 16:54:12 +0200461/* acquire interrupt */
Takashi Iwai10754f532005-11-17 15:01:29 +0100462static int snd_nm256_acquire_irq(struct nm256 *chip)
Takashi Iwai1204de32005-08-16 16:54:12 +0200463{
Ingo Molnar62932df2006-01-16 16:34:20 +0100464 mutex_lock(&chip->irq_mutex);
Takashi Iwai1204de32005-08-16 16:54:12 +0200465 if (chip->irq < 0) {
466 if (request_irq(chip->pci->irq, chip->interrupt, SA_INTERRUPT|SA_SHIRQ,
Takashi Iwai10754f532005-11-17 15:01:29 +0100467 chip->card->driver, chip)) {
Takashi Iwai99b359b2005-10-20 18:26:44 +0200468 snd_printk(KERN_ERR "unable to grab IRQ %d\n", chip->pci->irq);
Ingo Molnar62932df2006-01-16 16:34:20 +0100469 mutex_unlock(&chip->irq_mutex);
Takashi Iwai1204de32005-08-16 16:54:12 +0200470 return -EBUSY;
471 }
472 chip->irq = chip->pci->irq;
473 }
474 chip->irq_acks++;
Ingo Molnar62932df2006-01-16 16:34:20 +0100475 mutex_unlock(&chip->irq_mutex);
Takashi Iwai1204de32005-08-16 16:54:12 +0200476 return 0;
477}
478
479/* release interrupt */
Takashi Iwai10754f532005-11-17 15:01:29 +0100480static void snd_nm256_release_irq(struct nm256 *chip)
Takashi Iwai1204de32005-08-16 16:54:12 +0200481{
Ingo Molnar62932df2006-01-16 16:34:20 +0100482 mutex_lock(&chip->irq_mutex);
Takashi Iwai1204de32005-08-16 16:54:12 +0200483 if (chip->irq_acks > 0)
484 chip->irq_acks--;
485 if (chip->irq_acks == 0 && chip->irq >= 0) {
Takashi Iwai10754f532005-11-17 15:01:29 +0100486 free_irq(chip->irq, chip);
Takashi Iwai1204de32005-08-16 16:54:12 +0200487 chip->irq = -1;
488 }
Ingo Molnar62932df2006-01-16 16:34:20 +0100489 mutex_unlock(&chip->irq_mutex);
Takashi Iwai1204de32005-08-16 16:54:12 +0200490}
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492/*
493 * start / stop
494 */
495
496/* update the watermark (current period) */
Takashi Iwai10754f532005-11-17 15:01:29 +0100497static void snd_nm256_pcm_mark(struct nm256 *chip, struct nm256_stream *s, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
499 s->cur_period++;
500 s->cur_period %= s->periods;
501 snd_nm256_writel(chip, reg, s->buf + s->cur_period * s->period_size);
502}
503
504#define snd_nm256_playback_mark(chip, s) snd_nm256_pcm_mark(chip, s, NM_PBUFFER_WMARK)
505#define snd_nm256_capture_mark(chip, s) snd_nm256_pcm_mark(chip, s, NM_RBUFFER_WMARK)
506
507static void
Takashi Iwai10754f532005-11-17 15:01:29 +0100508snd_nm256_playback_start(struct nm256 *chip, struct nm256_stream *s,
509 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
511 /* program buffer pointers */
512 snd_nm256_writel(chip, NM_PBUFFER_START, s->buf);
513 snd_nm256_writel(chip, NM_PBUFFER_END, s->buf + s->dma_size - (1 << s->shift));
514 snd_nm256_writel(chip, NM_PBUFFER_CURRP, s->buf);
515 snd_nm256_playback_mark(chip, s);
516
517 /* Enable playback engine and interrupts. */
518 snd_nm256_writeb(chip, NM_PLAYBACK_ENABLE_REG,
519 NM_PLAYBACK_ENABLE_FLAG | NM_PLAYBACK_FREERUN);
520 /* Enable both channels. */
521 snd_nm256_writew(chip, NM_AUDIO_MUTE_REG, 0x0);
522}
523
524static void
Takashi Iwai10754f532005-11-17 15:01:29 +0100525snd_nm256_capture_start(struct nm256 *chip, struct nm256_stream *s,
526 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
528 /* program buffer pointers */
529 snd_nm256_writel(chip, NM_RBUFFER_START, s->buf);
530 snd_nm256_writel(chip, NM_RBUFFER_END, s->buf + s->dma_size);
531 snd_nm256_writel(chip, NM_RBUFFER_CURRP, s->buf);
532 snd_nm256_capture_mark(chip, s);
533
534 /* Enable playback engine and interrupts. */
535 snd_nm256_writeb(chip, NM_RECORD_ENABLE_REG,
536 NM_RECORD_ENABLE_FLAG | NM_RECORD_FREERUN);
537}
538
539/* Stop the play engine. */
540static void
Takashi Iwai10754f532005-11-17 15:01:29 +0100541snd_nm256_playback_stop(struct nm256 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
543 /* Shut off sound from both channels. */
544 snd_nm256_writew(chip, NM_AUDIO_MUTE_REG,
545 NM_AUDIO_MUTE_LEFT | NM_AUDIO_MUTE_RIGHT);
546 /* Disable play engine. */
547 snd_nm256_writeb(chip, NM_PLAYBACK_ENABLE_REG, 0);
548}
549
550static void
Takashi Iwai10754f532005-11-17 15:01:29 +0100551snd_nm256_capture_stop(struct nm256 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
553 /* Disable recording engine. */
554 snd_nm256_writeb(chip, NM_RECORD_ENABLE_REG, 0);
555}
556
557static int
Takashi Iwai10754f532005-11-17 15:01:29 +0100558snd_nm256_playback_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Takashi Iwai10754f532005-11-17 15:01:29 +0100560 struct nm256 *chip = snd_pcm_substream_chip(substream);
561 struct nm256_stream *s = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 int err = 0;
563
564 snd_assert(s != NULL, return -ENXIO);
565
566 spin_lock(&chip->reg_lock);
567 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 case SNDRV_PCM_TRIGGER_RESUME:
Takashi Iwai1204de32005-08-16 16:54:12 +0200569 s->suspended = 0;
570 /* fallthru */
571 case SNDRV_PCM_TRIGGER_START:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (! s->running) {
573 snd_nm256_playback_start(chip, s, substream);
574 s->running = 1;
575 }
576 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 case SNDRV_PCM_TRIGGER_SUSPEND:
Takashi Iwai1204de32005-08-16 16:54:12 +0200578 s->suspended = 1;
579 /* fallthru */
580 case SNDRV_PCM_TRIGGER_STOP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 if (s->running) {
582 snd_nm256_playback_stop(chip);
583 s->running = 0;
584 }
585 break;
586 default:
587 err = -EINVAL;
588 break;
589 }
590 spin_unlock(&chip->reg_lock);
591 return err;
592}
593
594static int
Takashi Iwai10754f532005-11-17 15:01:29 +0100595snd_nm256_capture_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
Takashi Iwai10754f532005-11-17 15:01:29 +0100597 struct nm256 *chip = snd_pcm_substream_chip(substream);
598 struct nm256_stream *s = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 int err = 0;
600
601 snd_assert(s != NULL, return -ENXIO);
602
603 spin_lock(&chip->reg_lock);
604 switch (cmd) {
605 case SNDRV_PCM_TRIGGER_START:
606 case SNDRV_PCM_TRIGGER_RESUME:
607 if (! s->running) {
608 snd_nm256_capture_start(chip, s, substream);
609 s->running = 1;
610 }
611 break;
612 case SNDRV_PCM_TRIGGER_STOP:
613 case SNDRV_PCM_TRIGGER_SUSPEND:
614 if (s->running) {
615 snd_nm256_capture_stop(chip);
616 s->running = 0;
617 }
618 break;
619 default:
620 err = -EINVAL;
621 break;
622 }
623 spin_unlock(&chip->reg_lock);
624 return err;
625}
626
627
628/*
629 * prepare playback/capture channel
630 */
Takashi Iwai10754f532005-11-17 15:01:29 +0100631static int snd_nm256_pcm_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
Takashi Iwai10754f532005-11-17 15:01:29 +0100633 struct nm256 *chip = snd_pcm_substream_chip(substream);
634 struct snd_pcm_runtime *runtime = substream->runtime;
635 struct nm256_stream *s = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637 snd_assert(s, return -ENXIO);
638 s->dma_size = frames_to_bytes(runtime, substream->runtime->buffer_size);
639 s->period_size = frames_to_bytes(runtime, substream->runtime->period_size);
640 s->periods = substream->runtime->periods;
641 s->cur_period = 0;
642
643 spin_lock_irq(&chip->reg_lock);
644 s->running = 0;
645 snd_nm256_set_format(chip, s, substream);
646 spin_unlock_irq(&chip->reg_lock);
647
648 return 0;
649}
650
651
652/*
653 * get the current pointer
654 */
655static snd_pcm_uframes_t
Takashi Iwai10754f532005-11-17 15:01:29 +0100656snd_nm256_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
Takashi Iwai10754f532005-11-17 15:01:29 +0100658 struct nm256 *chip = snd_pcm_substream_chip(substream);
659 struct nm256_stream *s = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 unsigned long curp;
661
662 snd_assert(s, return 0);
663 curp = snd_nm256_readl(chip, NM_PBUFFER_CURRP) - (unsigned long)s->buf;
664 curp %= s->dma_size;
665 return bytes_to_frames(substream->runtime, curp);
666}
667
668static snd_pcm_uframes_t
Takashi Iwai10754f532005-11-17 15:01:29 +0100669snd_nm256_capture_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
Takashi Iwai10754f532005-11-17 15:01:29 +0100671 struct nm256 *chip = snd_pcm_substream_chip(substream);
672 struct nm256_stream *s = substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 unsigned long curp;
674
675 snd_assert(s != NULL, return 0);
676 curp = snd_nm256_readl(chip, NM_RBUFFER_CURRP) - (unsigned long)s->buf;
677 curp %= s->dma_size;
678 return bytes_to_frames(substream->runtime, curp);
679}
680
681/* Remapped I/O space can be accessible as pointer on i386 */
682/* This might be changed in the future */
683#ifndef __i386__
684/*
685 * silence / copy for playback
686 */
687static int
Takashi Iwai10754f532005-11-17 15:01:29 +0100688snd_nm256_playback_silence(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 int channel, /* not used (interleaved data) */
690 snd_pcm_uframes_t pos,
691 snd_pcm_uframes_t count)
692{
Takashi Iwai10754f532005-11-17 15:01:29 +0100693 struct snd_pcm_runtime *runtime = substream->runtime;
694 struct nm256_stream *s = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 count = frames_to_bytes(runtime, count);
696 pos = frames_to_bytes(runtime, pos);
697 memset_io(s->bufptr + pos, 0, count);
698 return 0;
699}
700
701static int
Takashi Iwai10754f532005-11-17 15:01:29 +0100702snd_nm256_playback_copy(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 int channel, /* not used (interleaved data) */
704 snd_pcm_uframes_t pos,
705 void __user *src,
706 snd_pcm_uframes_t count)
707{
Takashi Iwai10754f532005-11-17 15:01:29 +0100708 struct snd_pcm_runtime *runtime = substream->runtime;
709 struct nm256_stream *s = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 count = frames_to_bytes(runtime, count);
711 pos = frames_to_bytes(runtime, pos);
712 if (copy_from_user_toio(s->bufptr + pos, src, count))
713 return -EFAULT;
714 return 0;
715}
716
717/*
718 * copy to user
719 */
720static int
Takashi Iwai10754f532005-11-17 15:01:29 +0100721snd_nm256_capture_copy(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 int channel, /* not used (interleaved data) */
723 snd_pcm_uframes_t pos,
724 void __user *dst,
725 snd_pcm_uframes_t count)
726{
Takashi Iwai10754f532005-11-17 15:01:29 +0100727 struct snd_pcm_runtime *runtime = substream->runtime;
728 struct nm256_stream *s = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 count = frames_to_bytes(runtime, count);
730 pos = frames_to_bytes(runtime, pos);
731 if (copy_to_user_fromio(dst, s->bufptr + pos, count))
732 return -EFAULT;
733 return 0;
734}
735
736#endif /* !__i386__ */
737
738
739/*
740 * update playback/capture watermarks
741 */
742
743/* spinlock held! */
744static void
Takashi Iwai10754f532005-11-17 15:01:29 +0100745snd_nm256_playback_update(struct nm256 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
Takashi Iwai10754f532005-11-17 15:01:29 +0100747 struct nm256_stream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749 s = &chip->streams[SNDRV_PCM_STREAM_PLAYBACK];
750 if (s->running && s->substream) {
751 spin_unlock(&chip->reg_lock);
752 snd_pcm_period_elapsed(s->substream);
753 spin_lock(&chip->reg_lock);
754 snd_nm256_playback_mark(chip, s);
755 }
756}
757
758/* spinlock held! */
759static void
Takashi Iwai10754f532005-11-17 15:01:29 +0100760snd_nm256_capture_update(struct nm256 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
Takashi Iwai10754f532005-11-17 15:01:29 +0100762 struct nm256_stream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764 s = &chip->streams[SNDRV_PCM_STREAM_CAPTURE];
765 if (s->running && s->substream) {
766 spin_unlock(&chip->reg_lock);
767 snd_pcm_period_elapsed(s->substream);
768 spin_lock(&chip->reg_lock);
769 snd_nm256_capture_mark(chip, s);
770 }
771}
772
773/*
774 * hardware info
775 */
Takashi Iwai10754f532005-11-17 15:01:29 +0100776static struct snd_pcm_hardware snd_nm256_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
778 .info = SNDRV_PCM_INFO_MMAP_IOMEM |SNDRV_PCM_INFO_MMAP_VALID |
779 SNDRV_PCM_INFO_INTERLEAVED |
780 /*SNDRV_PCM_INFO_PAUSE |*/
781 SNDRV_PCM_INFO_RESUME,
782 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
783 .rates = SNDRV_PCM_RATE_KNOT/*24k*/ | SNDRV_PCM_RATE_8000_48000,
784 .rate_min = 8000,
785 .rate_max = 48000,
786 .channels_min = 1,
787 .channels_max = 2,
788 .periods_min = 2,
789 .periods_max = 1024,
790 .buffer_bytes_max = 128 * 1024,
791 .period_bytes_min = 256,
792 .period_bytes_max = 128 * 1024,
793};
794
Takashi Iwai10754f532005-11-17 15:01:29 +0100795static struct snd_pcm_hardware snd_nm256_capture =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
797 .info = SNDRV_PCM_INFO_MMAP_IOMEM | SNDRV_PCM_INFO_MMAP_VALID |
798 SNDRV_PCM_INFO_INTERLEAVED |
799 /*SNDRV_PCM_INFO_PAUSE |*/
800 SNDRV_PCM_INFO_RESUME,
801 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
802 .rates = SNDRV_PCM_RATE_KNOT/*24k*/ | SNDRV_PCM_RATE_8000_48000,
803 .rate_min = 8000,
804 .rate_max = 48000,
805 .channels_min = 1,
806 .channels_max = 2,
807 .periods_min = 2,
808 .periods_max = 1024,
809 .buffer_bytes_max = 128 * 1024,
810 .period_bytes_min = 256,
811 .period_bytes_max = 128 * 1024,
812};
813
814
815/* set dma transfer size */
Takashi Iwai10754f532005-11-17 15:01:29 +0100816static int snd_nm256_pcm_hw_params(struct snd_pcm_substream *substream,
817 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
819 /* area and addr are already set and unchanged */
820 substream->runtime->dma_bytes = params_buffer_bytes(hw_params);
821 return 0;
822}
823
824/*
825 * open
826 */
Takashi Iwai10754f532005-11-17 15:01:29 +0100827static void snd_nm256_setup_stream(struct nm256 *chip, struct nm256_stream *s,
828 struct snd_pcm_substream *substream,
829 struct snd_pcm_hardware *hw_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830{
Takashi Iwai10754f532005-11-17 15:01:29 +0100831 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
833 s->running = 0;
834 runtime->hw = *hw_ptr;
835 runtime->hw.buffer_bytes_max = s->bufsize;
836 runtime->hw.period_bytes_max = s->bufsize / 2;
Clemens Ladisch4d233592005-09-05 10:35:20 +0200837 runtime->dma_area = (void __force *) s->bufptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 runtime->dma_addr = s->bufptr_addr;
839 runtime->dma_bytes = s->bufsize;
840 runtime->private_data = s;
841 s->substream = substream;
842
843 snd_pcm_set_sync(substream);
844 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
845 &constraints_rates);
846}
847
848static int
Takashi Iwai10754f532005-11-17 15:01:29 +0100849snd_nm256_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
Takashi Iwai10754f532005-11-17 15:01:29 +0100851 struct nm256 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Takashi Iwai1204de32005-08-16 16:54:12 +0200853 if (snd_nm256_acquire_irq(chip) < 0)
854 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 snd_nm256_setup_stream(chip, &chip->streams[SNDRV_PCM_STREAM_PLAYBACK],
856 substream, &snd_nm256_playback);
857 return 0;
858}
859
860static int
Takashi Iwai10754f532005-11-17 15:01:29 +0100861snd_nm256_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
Takashi Iwai10754f532005-11-17 15:01:29 +0100863 struct nm256 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Takashi Iwai1204de32005-08-16 16:54:12 +0200865 if (snd_nm256_acquire_irq(chip) < 0)
866 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 snd_nm256_setup_stream(chip, &chip->streams[SNDRV_PCM_STREAM_CAPTURE],
868 substream, &snd_nm256_capture);
869 return 0;
870}
871
872/*
873 * close - we don't have to do special..
874 */
875static int
Takashi Iwai10754f532005-11-17 15:01:29 +0100876snd_nm256_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
Takashi Iwai10754f532005-11-17 15:01:29 +0100878 struct nm256 *chip = snd_pcm_substream_chip(substream);
Takashi Iwai1204de32005-08-16 16:54:12 +0200879
880 snd_nm256_release_irq(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 return 0;
882}
883
884
885static int
Takashi Iwai10754f532005-11-17 15:01:29 +0100886snd_nm256_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887{
Takashi Iwai10754f532005-11-17 15:01:29 +0100888 struct nm256 *chip = snd_pcm_substream_chip(substream);
Takashi Iwai1204de32005-08-16 16:54:12 +0200889
890 snd_nm256_release_irq(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 return 0;
892}
893
894/*
895 * create a pcm instance
896 */
Takashi Iwai10754f532005-11-17 15:01:29 +0100897static struct snd_pcm_ops snd_nm256_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 .open = snd_nm256_playback_open,
899 .close = snd_nm256_playback_close,
900 .ioctl = snd_pcm_lib_ioctl,
901 .hw_params = snd_nm256_pcm_hw_params,
902 .prepare = snd_nm256_pcm_prepare,
903 .trigger = snd_nm256_playback_trigger,
904 .pointer = snd_nm256_playback_pointer,
905#ifndef __i386__
906 .copy = snd_nm256_playback_copy,
907 .silence = snd_nm256_playback_silence,
908#endif
909 .mmap = snd_pcm_lib_mmap_iomem,
910};
911
Takashi Iwai10754f532005-11-17 15:01:29 +0100912static struct snd_pcm_ops snd_nm256_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 .open = snd_nm256_capture_open,
914 .close = snd_nm256_capture_close,
915 .ioctl = snd_pcm_lib_ioctl,
916 .hw_params = snd_nm256_pcm_hw_params,
917 .prepare = snd_nm256_pcm_prepare,
918 .trigger = snd_nm256_capture_trigger,
919 .pointer = snd_nm256_capture_pointer,
920#ifndef __i386__
921 .copy = snd_nm256_capture_copy,
922#endif
923 .mmap = snd_pcm_lib_mmap_iomem,
924};
925
926static int __devinit
Takashi Iwai10754f532005-11-17 15:01:29 +0100927snd_nm256_pcm(struct nm256 *chip, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928{
Takashi Iwai10754f532005-11-17 15:01:29 +0100929 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 int i, err;
931
932 for (i = 0; i < 2; i++) {
Takashi Iwai10754f532005-11-17 15:01:29 +0100933 struct nm256_stream *s = &chip->streams[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 s->bufptr = chip->buffer + (s->buf - chip->buffer_start);
935 s->bufptr_addr = chip->buffer_addr + (s->buf - chip->buffer_start);
936 }
937
938 err = snd_pcm_new(chip->card, chip->card->driver, device,
939 1, 1, &pcm);
940 if (err < 0)
941 return err;
942
943 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_nm256_playback_ops);
944 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_nm256_capture_ops);
945
946 pcm->private_data = chip;
947 pcm->info_flags = 0;
948 chip->pcm = pcm;
949
950 return 0;
951}
952
953
954/*
955 * Initialize the hardware.
956 */
957static void
Takashi Iwai10754f532005-11-17 15:01:29 +0100958snd_nm256_init_chip(struct nm256 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 /* Reset everything. */
961 snd_nm256_writeb(chip, 0x0, 0x11);
962 snd_nm256_writew(chip, 0x214, 0);
963 /* stop sounds.. */
964 //snd_nm256_playback_stop(chip);
965 //snd_nm256_capture_stop(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966}
967
968
Takashi Iwai1204de32005-08-16 16:54:12 +0200969static irqreturn_t
Takashi Iwai10754f532005-11-17 15:01:29 +0100970snd_nm256_intr_check(struct nm256 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
972 if (chip->badintrcount++ > 1000) {
973 /*
974 * I'm not sure if the best thing is to stop the card from
975 * playing or just release the interrupt (after all, we're in
976 * a bad situation, so doing fancy stuff may not be such a good
977 * idea).
978 *
979 * I worry about the card engine continuing to play noise
980 * over and over, however--that could become a very
981 * obnoxious problem. And we know that when this usually
982 * happens things are fairly safe, it just means the user's
983 * inserted a PCMCIA card and someone's spamming us with IRQ 9s.
984 */
985 if (chip->streams[SNDRV_PCM_STREAM_PLAYBACK].running)
986 snd_nm256_playback_stop(chip);
987 if (chip->streams[SNDRV_PCM_STREAM_CAPTURE].running)
988 snd_nm256_capture_stop(chip);
989 chip->badintrcount = 0;
Takashi Iwai1204de32005-08-16 16:54:12 +0200990 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 }
Takashi Iwai1204de32005-08-16 16:54:12 +0200992 return IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993}
994
995/*
996 * Handle a potential interrupt for the device referred to by DEV_ID.
997 *
998 * I don't like the cut-n-paste job here either between the two routines,
999 * but there are sufficient differences between the two interrupt handlers
1000 * that parameterizing it isn't all that great either. (Could use a macro,
1001 * I suppose...yucky bleah.)
1002 */
1003
1004static irqreturn_t
1005snd_nm256_interrupt(int irq, void *dev_id, struct pt_regs *dummy)
1006{
Takashi Iwai10754f532005-11-17 15:01:29 +01001007 struct nm256 *chip = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 u16 status;
1009 u8 cbyte;
1010
1011 status = snd_nm256_readw(chip, NM_INT_REG);
1012
1013 /* Not ours. */
Takashi Iwai1204de32005-08-16 16:54:12 +02001014 if (status == 0)
1015 return snd_nm256_intr_check(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
1017 chip->badintrcount = 0;
1018
1019 /* Rather boring; check for individual interrupts and process them. */
1020
1021 spin_lock(&chip->reg_lock);
1022 if (status & NM_PLAYBACK_INT) {
1023 status &= ~NM_PLAYBACK_INT;
1024 NM_ACK_INT(chip, NM_PLAYBACK_INT);
1025 snd_nm256_playback_update(chip);
1026 }
1027
1028 if (status & NM_RECORD_INT) {
1029 status &= ~NM_RECORD_INT;
1030 NM_ACK_INT(chip, NM_RECORD_INT);
1031 snd_nm256_capture_update(chip);
1032 }
1033
1034 if (status & NM_MISC_INT_1) {
1035 status &= ~NM_MISC_INT_1;
1036 NM_ACK_INT(chip, NM_MISC_INT_1);
1037 snd_printd("NM256: Got misc interrupt #1\n");
1038 snd_nm256_writew(chip, NM_INT_REG, 0x8000);
1039 cbyte = snd_nm256_readb(chip, 0x400);
1040 snd_nm256_writeb(chip, 0x400, cbyte | 2);
1041 }
1042
1043 if (status & NM_MISC_INT_2) {
1044 status &= ~NM_MISC_INT_2;
1045 NM_ACK_INT(chip, NM_MISC_INT_2);
1046 snd_printd("NM256: Got misc interrupt #2\n");
1047 cbyte = snd_nm256_readb(chip, 0x400);
1048 snd_nm256_writeb(chip, 0x400, cbyte & ~2);
1049 }
1050
1051 /* Unknown interrupt. */
1052 if (status) {
1053 snd_printd("NM256: Fire in the hole! Unknown status 0x%x\n",
1054 status);
1055 /* Pray. */
1056 NM_ACK_INT(chip, status);
1057 }
1058
1059 spin_unlock(&chip->reg_lock);
1060 return IRQ_HANDLED;
1061}
1062
1063/*
1064 * Handle a potential interrupt for the device referred to by DEV_ID.
1065 * This handler is for the 256ZX, and is very similar to the non-ZX
1066 * routine.
1067 */
1068
1069static irqreturn_t
1070snd_nm256_interrupt_zx(int irq, void *dev_id, struct pt_regs *dummy)
1071{
Takashi Iwai10754f532005-11-17 15:01:29 +01001072 struct nm256 *chip = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 u32 status;
1074 u8 cbyte;
1075
1076 status = snd_nm256_readl(chip, NM_INT_REG);
1077
1078 /* Not ours. */
Takashi Iwai1204de32005-08-16 16:54:12 +02001079 if (status == 0)
1080 return snd_nm256_intr_check(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082 chip->badintrcount = 0;
1083
1084 /* Rather boring; check for individual interrupts and process them. */
1085
1086 spin_lock(&chip->reg_lock);
1087 if (status & NM2_PLAYBACK_INT) {
1088 status &= ~NM2_PLAYBACK_INT;
1089 NM2_ACK_INT(chip, NM2_PLAYBACK_INT);
1090 snd_nm256_playback_update(chip);
1091 }
1092
1093 if (status & NM2_RECORD_INT) {
1094 status &= ~NM2_RECORD_INT;
1095 NM2_ACK_INT(chip, NM2_RECORD_INT);
1096 snd_nm256_capture_update(chip);
1097 }
1098
1099 if (status & NM2_MISC_INT_1) {
1100 status &= ~NM2_MISC_INT_1;
1101 NM2_ACK_INT(chip, NM2_MISC_INT_1);
1102 snd_printd("NM256: Got misc interrupt #1\n");
1103 cbyte = snd_nm256_readb(chip, 0x400);
1104 snd_nm256_writeb(chip, 0x400, cbyte | 2);
1105 }
1106
1107 if (status & NM2_MISC_INT_2) {
1108 status &= ~NM2_MISC_INT_2;
1109 NM2_ACK_INT(chip, NM2_MISC_INT_2);
1110 snd_printd("NM256: Got misc interrupt #2\n");
1111 cbyte = snd_nm256_readb(chip, 0x400);
1112 snd_nm256_writeb(chip, 0x400, cbyte & ~2);
1113 }
1114
1115 /* Unknown interrupt. */
1116 if (status) {
1117 snd_printd("NM256: Fire in the hole! Unknown status 0x%x\n",
1118 status);
1119 /* Pray. */
1120 NM2_ACK_INT(chip, status);
1121 }
1122
1123 spin_unlock(&chip->reg_lock);
1124 return IRQ_HANDLED;
1125}
1126
1127/*
1128 * AC97 interface
1129 */
1130
1131/*
1132 * Waits for the mixer to become ready to be written; returns a zero value
1133 * if it timed out.
1134 */
1135static int
Takashi Iwai10754f532005-11-17 15:01:29 +01001136snd_nm256_ac97_ready(struct nm256 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137{
1138 int timeout = 10;
1139 u32 testaddr;
1140 u16 testb;
1141
1142 testaddr = chip->mixer_status_offset;
1143 testb = chip->mixer_status_mask;
1144
1145 /*
1146 * Loop around waiting for the mixer to become ready.
1147 */
1148 while (timeout-- > 0) {
1149 if ((snd_nm256_readw(chip, testaddr) & testb) == 0)
1150 return 1;
1151 udelay(100);
1152 }
1153 return 0;
1154}
1155
1156/*
1157 */
1158static unsigned short
Takashi Iwai10754f532005-11-17 15:01:29 +01001159snd_nm256_ac97_read(struct snd_ac97 *ac97, unsigned short reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160{
Takashi Iwai10754f532005-11-17 15:01:29 +01001161 struct nm256 *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 int res;
1163
1164 if (reg >= 128)
1165 return 0;
1166
1167 if (! snd_nm256_ac97_ready(chip))
1168 return 0;
1169 res = snd_nm256_readw(chip, chip->mixer_base + reg);
1170 /* Magic delay. Bleah yucky. */
1171 msleep(1);
1172 return res;
1173}
1174
1175/*
1176 */
1177static void
Takashi Iwai10754f532005-11-17 15:01:29 +01001178snd_nm256_ac97_write(struct snd_ac97 *ac97,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 unsigned short reg, unsigned short val)
1180{
Takashi Iwai10754f532005-11-17 15:01:29 +01001181 struct nm256 *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 int tries = 2;
1183 u32 base;
1184
1185 base = chip->mixer_base;
1186
1187 snd_nm256_ac97_ready(chip);
1188
1189 /* Wait for the write to take, too. */
1190 while (tries-- > 0) {
1191 snd_nm256_writew(chip, base + reg, val);
1192 msleep(1); /* a little delay here seems better.. */
1193 if (snd_nm256_ac97_ready(chip))
1194 return;
1195 }
1196 snd_printd("nm256: ac97 codec not ready..\n");
1197}
1198
1199/* initialize the ac97 into a known state */
1200static void
Takashi Iwai10754f532005-11-17 15:01:29 +01001201snd_nm256_ac97_reset(struct snd_ac97 *ac97)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202{
Takashi Iwai10754f532005-11-17 15:01:29 +01001203 struct nm256 *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
1205 /* Reset the mixer. 'Tis magic! */
1206 snd_nm256_writeb(chip, 0x6c0, 1);
1207 if (! chip->reset_workaround) {
1208 /* Dell latitude LS will lock up by this */
1209 snd_nm256_writeb(chip, 0x6cc, 0x87);
1210 }
John W. Linville47530cf2005-10-19 16:03:10 +02001211 if (! chip->reset_workaround_2) {
1212 /* Dell latitude CSx will lock up by this */
1213 snd_nm256_writeb(chip, 0x6cc, 0x80);
1214 snd_nm256_writeb(chip, 0x6cc, 0x0);
1215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216}
1217
1218/* create an ac97 mixer interface */
1219static int __devinit
Takashi Iwai10754f532005-11-17 15:01:29 +01001220snd_nm256_mixer(struct nm256 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221{
Takashi Iwai10754f532005-11-17 15:01:29 +01001222 struct snd_ac97_bus *pbus;
1223 struct snd_ac97_template ac97;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 int i, err;
Takashi Iwai10754f532005-11-17 15:01:29 +01001225 static struct snd_ac97_bus_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 .reset = snd_nm256_ac97_reset,
1227 .write = snd_nm256_ac97_write,
1228 .read = snd_nm256_ac97_read,
1229 };
1230 /* looks like nm256 hangs up when unexpected registers are touched... */
1231 static int mixer_regs[] = {
1232 AC97_MASTER, AC97_HEADPHONE, AC97_MASTER_MONO,
1233 AC97_PC_BEEP, AC97_PHONE, AC97_MIC, AC97_LINE, AC97_CD,
1234 AC97_VIDEO, AC97_AUX, AC97_PCM, AC97_REC_SEL,
1235 AC97_REC_GAIN, AC97_GENERAL_PURPOSE, AC97_3D_CONTROL,
Takashi Iwai1204de32005-08-16 16:54:12 +02001236 /*AC97_EXTENDED_ID,*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 AC97_VENDOR_ID1, AC97_VENDOR_ID2,
1238 -1
1239 };
1240
1241 if ((err = snd_ac97_bus(chip->card, 0, &ops, NULL, &pbus)) < 0)
1242 return err;
1243
1244 memset(&ac97, 0, sizeof(ac97));
1245 ac97.scaps = AC97_SCAP_AUDIO; /* we support audio! */
1246 ac97.limited_regs = 1;
1247 for (i = 0; mixer_regs[i] >= 0; i++)
1248 set_bit(mixer_regs[i], ac97.reg_accessed);
1249 ac97.private_data = chip;
Takashi Iwai1204de32005-08-16 16:54:12 +02001250 pbus->no_vra = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 err = snd_ac97_mixer(pbus, &ac97, &chip->ac97);
1252 if (err < 0)
1253 return err;
1254 if (! (chip->ac97->id & (0xf0000000))) {
1255 /* looks like an invalid id */
1256 sprintf(chip->card->mixername, "%s AC97", chip->card->driver);
1257 }
1258 return 0;
1259}
1260
1261/*
1262 * See if the signature left by the NM256 BIOS is intact; if so, we use
1263 * the associated address as the end of our audio buffer in the video
1264 * RAM.
1265 */
1266
1267static int __devinit
Takashi Iwai10754f532005-11-17 15:01:29 +01001268snd_nm256_peek_for_sig(struct nm256 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
1270 /* The signature is located 1K below the end of video RAM. */
1271 void __iomem *temp;
1272 /* Default buffer end is 5120 bytes below the top of RAM. */
1273 unsigned long pointer_found = chip->buffer_end - 0x1400;
1274 u32 sig;
1275
1276 temp = ioremap_nocache(chip->buffer_addr + chip->buffer_end - 0x400, 16);
1277 if (temp == NULL) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02001278 snd_printk(KERN_ERR "Unable to scan for card signature in video RAM\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 return -EBUSY;
1280 }
1281
1282 sig = readl(temp);
1283 if ((sig & NM_SIG_MASK) == NM_SIGNATURE) {
1284 u32 pointer = readl(temp + 4);
1285
1286 /*
1287 * If it's obviously invalid, don't use it
1288 */
1289 if (pointer == 0xffffffff ||
1290 pointer < chip->buffer_size ||
1291 pointer > chip->buffer_end) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02001292 snd_printk(KERN_ERR "invalid signature found: 0x%x\n", pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 iounmap(temp);
1294 return -ENODEV;
1295 } else {
1296 pointer_found = pointer;
Takashi Iwai10754f532005-11-17 15:01:29 +01001297 printk(KERN_INFO "nm256: found card signature in video RAM: 0x%x\n",
1298 pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 }
1300 }
1301
1302 iounmap(temp);
1303 chip->buffer_end = pointer_found;
1304
1305 return 0;
1306}
1307
1308#ifdef CONFIG_PM
1309/*
1310 * APM event handler, so the card is properly reinitialized after a power
1311 * event.
1312 */
Takashi Iwai3fcf7d22005-11-17 16:11:24 +01001313static int nm256_suspend(struct pci_dev *pci, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314{
Takashi Iwai3fcf7d22005-11-17 16:11:24 +01001315 struct snd_card *card = pci_get_drvdata(pci);
1316 struct nm256 *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
Takashi Iwai3fcf7d22005-11-17 16:11:24 +01001318 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 snd_pcm_suspend_all(chip->pcm);
1320 snd_ac97_suspend(chip->ac97);
1321 chip->coeffs_current = 0;
Takashi Iwai3fcf7d22005-11-17 16:11:24 +01001322 pci_disable_device(pci);
1323 pci_save_state(pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 return 0;
1325}
1326
Takashi Iwai3fcf7d22005-11-17 16:11:24 +01001327static int nm256_resume(struct pci_dev *pci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328{
Takashi Iwai3fcf7d22005-11-17 16:11:24 +01001329 struct snd_card *card = pci_get_drvdata(pci);
1330 struct nm256 *chip = card->private_data;
Takashi Iwai1204de32005-08-16 16:54:12 +02001331 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
1333 /* Perform a full reset on the hardware */
Takashi Iwai3fcf7d22005-11-17 16:11:24 +01001334 pci_restore_state(pci);
1335 pci_enable_device(pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 snd_nm256_init_chip(chip);
1337
1338 /* restore ac97 */
1339 snd_ac97_resume(chip->ac97);
1340
Takashi Iwai1204de32005-08-16 16:54:12 +02001341 for (i = 0; i < 2; i++) {
Takashi Iwai10754f532005-11-17 15:01:29 +01001342 struct nm256_stream *s = &chip->streams[i];
Takashi Iwai1204de32005-08-16 16:54:12 +02001343 if (s->substream && s->suspended) {
1344 spin_lock_irq(&chip->reg_lock);
1345 snd_nm256_set_format(chip, s, s->substream);
1346 spin_unlock_irq(&chip->reg_lock);
1347 }
1348 }
1349
Takashi Iwai3fcf7d22005-11-17 16:11:24 +01001350 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 return 0;
1352}
1353#endif /* CONFIG_PM */
1354
Takashi Iwai10754f532005-11-17 15:01:29 +01001355static int snd_nm256_free(struct nm256 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356{
1357 if (chip->streams[SNDRV_PCM_STREAM_PLAYBACK].running)
1358 snd_nm256_playback_stop(chip);
1359 if (chip->streams[SNDRV_PCM_STREAM_CAPTURE].running)
1360 snd_nm256_capture_stop(chip);
1361
1362 if (chip->irq >= 0)
1363 synchronize_irq(chip->irq);
1364
1365 if (chip->cport)
1366 iounmap(chip->cport);
1367 if (chip->buffer)
1368 iounmap(chip->buffer);
Takashi Iwaib1d57762005-10-10 11:56:31 +02001369 release_and_free_resource(chip->res_cport);
1370 release_and_free_resource(chip->res_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 if (chip->irq >= 0)
Takashi Iwai10754f532005-11-17 15:01:29 +01001372 free_irq(chip->irq, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
1374 pci_disable_device(chip->pci);
1375 kfree(chip);
1376 return 0;
1377}
1378
Takashi Iwai10754f532005-11-17 15:01:29 +01001379static int snd_nm256_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380{
Takashi Iwai10754f532005-11-17 15:01:29 +01001381 struct nm256 *chip = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 return snd_nm256_free(chip);
1383}
1384
1385static int __devinit
Takashi Iwai10754f532005-11-17 15:01:29 +01001386snd_nm256_create(struct snd_card *card, struct pci_dev *pci,
1387 struct nm256 **chip_ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388{
Takashi Iwai10754f532005-11-17 15:01:29 +01001389 struct nm256 *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 int err, pval;
Takashi Iwai10754f532005-11-17 15:01:29 +01001391 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 .dev_free = snd_nm256_dev_free,
1393 };
1394 u32 addr;
1395
1396 *chip_ret = NULL;
1397
1398 if ((err = pci_enable_device(pci)) < 0)
1399 return err;
1400
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001401 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 if (chip == NULL) {
1403 pci_disable_device(pci);
1404 return -ENOMEM;
1405 }
1406
1407 chip->card = card;
1408 chip->pci = pci;
Takashi Iwai3f05f862005-11-17 11:12:06 +01001409 chip->use_cache = use_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 spin_lock_init(&chip->reg_lock);
1411 chip->irq = -1;
Ingo Molnar62932df2006-01-16 16:34:20 +01001412 mutex_init(&chip->irq_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Takashi Iwai3f05f862005-11-17 11:12:06 +01001414 /* store buffer sizes in bytes */
1415 chip->streams[SNDRV_PCM_STREAM_PLAYBACK].bufsize = playback_bufsize * 1024;
1416 chip->streams[SNDRV_PCM_STREAM_CAPTURE].bufsize = capture_bufsize * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
1418 /*
1419 * The NM256 has two memory ports. The first port is nothing
1420 * more than a chunk of video RAM, which is used as the I/O ring
1421 * buffer. The second port has the actual juicy stuff (like the
1422 * mixer and the playback engine control registers).
1423 */
1424
1425 chip->buffer_addr = pci_resource_start(pci, 0);
1426 chip->cport_addr = pci_resource_start(pci, 1);
1427
1428 /* Init the memory port info. */
1429 /* remap control port (#2) */
1430 chip->res_cport = request_mem_region(chip->cport_addr, NM_PORT2_SIZE,
1431 card->driver);
1432 if (chip->res_cport == NULL) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02001433 snd_printk(KERN_ERR "memory region 0x%lx (size 0x%x) busy\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 chip->cport_addr, NM_PORT2_SIZE);
1435 err = -EBUSY;
1436 goto __error;
1437 }
1438 chip->cport = ioremap_nocache(chip->cport_addr, NM_PORT2_SIZE);
1439 if (chip->cport == NULL) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02001440 snd_printk(KERN_ERR "unable to map control port %lx\n", chip->cport_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 err = -ENOMEM;
1442 goto __error;
1443 }
1444
1445 if (!strcmp(card->driver, "NM256AV")) {
1446 /* Ok, try to see if this is a non-AC97 version of the hardware. */
1447 pval = snd_nm256_readw(chip, NM_MIXER_PRESENCE);
1448 if ((pval & NM_PRESENCE_MASK) != NM_PRESENCE_VALUE) {
Takashi Iwai3f05f862005-11-17 11:12:06 +01001449 if (! force_ac97) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 printk(KERN_ERR "nm256: no ac97 is found!\n");
Takashi Iwai10754f532005-11-17 15:01:29 +01001451 printk(KERN_ERR " force the driver to load by "
1452 "passing in the module parameter\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 printk(KERN_ERR " force_ac97=1\n");
1454 printk(KERN_ERR " or try sb16 or cs423x drivers instead.\n");
1455 err = -ENXIO;
1456 goto __error;
1457 }
1458 }
1459 chip->buffer_end = 2560 * 1024;
1460 chip->interrupt = snd_nm256_interrupt;
1461 chip->mixer_status_offset = NM_MIXER_STATUS_OFFSET;
1462 chip->mixer_status_mask = NM_MIXER_READY_MASK;
1463 } else {
1464 /* Not sure if there is any relevant detect for the ZX or not. */
1465 if (snd_nm256_readb(chip, 0xa0b) != 0)
1466 chip->buffer_end = 6144 * 1024;
1467 else
1468 chip->buffer_end = 4096 * 1024;
1469
1470 chip->interrupt = snd_nm256_interrupt_zx;
1471 chip->mixer_status_offset = NM2_MIXER_STATUS_OFFSET;
1472 chip->mixer_status_mask = NM2_MIXER_READY_MASK;
1473 }
1474
Takashi Iwai10754f532005-11-17 15:01:29 +01001475 chip->buffer_size = chip->streams[SNDRV_PCM_STREAM_PLAYBACK].bufsize +
1476 chip->streams[SNDRV_PCM_STREAM_CAPTURE].bufsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 if (chip->use_cache)
1478 chip->buffer_size += NM_TOTAL_COEFF_COUNT * 4;
1479 else
1480 chip->buffer_size += NM_MAX_PLAYBACK_COEF_SIZE + NM_MAX_RECORD_COEF_SIZE;
1481
Takashi Iwai3f05f862005-11-17 11:12:06 +01001482 if (buffer_top >= chip->buffer_size && buffer_top < chip->buffer_end)
1483 chip->buffer_end = buffer_top;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 else {
1485 /* get buffer end pointer from signature */
1486 if ((err = snd_nm256_peek_for_sig(chip)) < 0)
1487 goto __error;
1488 }
1489
1490 chip->buffer_start = chip->buffer_end - chip->buffer_size;
1491 chip->buffer_addr += chip->buffer_start;
1492
1493 printk(KERN_INFO "nm256: Mapping port 1 from 0x%x - 0x%x\n",
1494 chip->buffer_start, chip->buffer_end);
1495
1496 chip->res_buffer = request_mem_region(chip->buffer_addr,
1497 chip->buffer_size,
1498 card->driver);
1499 if (chip->res_buffer == NULL) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02001500 snd_printk(KERN_ERR "nm256: buffer 0x%lx (size 0x%x) busy\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 chip->buffer_addr, chip->buffer_size);
1502 err = -EBUSY;
1503 goto __error;
1504 }
1505 chip->buffer = ioremap_nocache(chip->buffer_addr, chip->buffer_size);
1506 if (chip->buffer == NULL) {
1507 err = -ENOMEM;
Takashi Iwai99b359b2005-10-20 18:26:44 +02001508 snd_printk(KERN_ERR "unable to map ring buffer at %lx\n", chip->buffer_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 goto __error;
1510 }
1511
1512 /* set offsets */
1513 addr = chip->buffer_start;
1514 chip->streams[SNDRV_PCM_STREAM_PLAYBACK].buf = addr;
1515 addr += chip->streams[SNDRV_PCM_STREAM_PLAYBACK].bufsize;
1516 chip->streams[SNDRV_PCM_STREAM_CAPTURE].buf = addr;
1517 addr += chip->streams[SNDRV_PCM_STREAM_CAPTURE].bufsize;
1518 if (chip->use_cache) {
1519 chip->all_coeff_buf = addr;
1520 } else {
1521 chip->coeff_buf[SNDRV_PCM_STREAM_PLAYBACK] = addr;
1522 addr += NM_MAX_PLAYBACK_COEF_SIZE;
1523 chip->coeff_buf[SNDRV_PCM_STREAM_CAPTURE] = addr;
1524 }
1525
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 /* Fixed setting. */
1527 chip->mixer_base = NM_MIXER_OFFSET;
1528
1529 chip->coeffs_current = 0;
1530
1531 snd_nm256_init_chip(chip);
1532
1533 // pci_set_master(pci); /* needed? */
1534
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0)
1536 goto __error;
1537
1538 snd_card_set_dev(card, &pci->dev);
1539
1540 *chip_ret = chip;
1541 return 0;
1542
1543__error:
1544 snd_nm256_free(chip);
1545 return err;
1546}
1547
1548
1549struct nm256_quirk {
1550 unsigned short vendor;
1551 unsigned short device;
1552 int type;
1553};
1554
John W. Linville47530cf2005-10-19 16:03:10 +02001555enum { NM_BLACKLISTED, NM_RESET_WORKAROUND, NM_RESET_WORKAROUND_2 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
1557static struct nm256_quirk nm256_quirks[] __devinitdata = {
1558 /* HP omnibook 4150 has cs4232 codec internally */
1559 { .vendor = 0x103c, .device = 0x0007, .type = NM_BLACKLISTED },
1560 /* Sony PCG-F305 */
1561 { .vendor = 0x104d, .device = 0x8041, .type = NM_RESET_WORKAROUND },
1562 /* Dell Latitude LS */
1563 { .vendor = 0x1028, .device = 0x0080, .type = NM_RESET_WORKAROUND },
John W. Linville47530cf2005-10-19 16:03:10 +02001564 /* Dell Latitude CSx */
1565 { .vendor = 0x1028, .device = 0x0091, .type = NM_RESET_WORKAROUND_2 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 { } /* terminator */
1567};
1568
1569
1570static int __devinit snd_nm256_probe(struct pci_dev *pci,
1571 const struct pci_device_id *pci_id)
1572{
Takashi Iwai10754f532005-11-17 15:01:29 +01001573 struct snd_card *card;
1574 struct nm256 *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 struct nm256_quirk *q;
1577 u16 subsystem_vendor, subsystem_device;
1578
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 pci_read_config_word(pci, PCI_SUBSYSTEM_VENDOR_ID, &subsystem_vendor);
1580 pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &subsystem_device);
1581
1582 for (q = nm256_quirks; q->vendor; q++) {
1583 if (q->vendor == subsystem_vendor && q->device == subsystem_device) {
1584 switch (q->type) {
1585 case NM_BLACKLISTED:
Takashi Iwai10754f532005-11-17 15:01:29 +01001586 printk(KERN_INFO "nm256: The device is blacklisted. "
1587 "Loading stopped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 return -ENODEV;
John W. Linville47530cf2005-10-19 16:03:10 +02001589 case NM_RESET_WORKAROUND_2:
Takashi Iwai8a3fb4d2005-10-20 17:10:49 +02001590 reset_workaround_2 = 1;
John W. Linville47530cf2005-10-19 16:03:10 +02001591 /* Fall-through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 case NM_RESET_WORKAROUND:
Takashi Iwai8a3fb4d2005-10-20 17:10:49 +02001593 reset_workaround = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 break;
1595 }
1596 }
1597 }
1598
Takashi Iwai8a3fb4d2005-10-20 17:10:49 +02001599 card = snd_card_new(index, id, THIS_MODULE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 if (card == NULL)
1601 return -ENOMEM;
1602
1603 switch (pci->device) {
1604 case PCI_DEVICE_ID_NEOMAGIC_NM256AV_AUDIO:
1605 strcpy(card->driver, "NM256AV");
1606 break;
1607 case PCI_DEVICE_ID_NEOMAGIC_NM256ZX_AUDIO:
1608 strcpy(card->driver, "NM256ZX");
1609 break;
1610 case PCI_DEVICE_ID_NEOMAGIC_NM256XL_PLUS_AUDIO:
1611 strcpy(card->driver, "NM256XL+");
1612 break;
1613 default:
Takashi Iwai99b359b2005-10-20 18:26:44 +02001614 snd_printk(KERN_ERR "invalid device id 0x%x\n", pci->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 snd_card_free(card);
1616 return -EINVAL;
1617 }
1618
Takashi Iwai8a3fb4d2005-10-20 17:10:49 +02001619 if (vaio_hack)
Takashi Iwai3f05f862005-11-17 11:12:06 +01001620 buffer_top = 0x25a800; /* this avoids conflicts with XFree86 server */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
Takashi Iwai8a3fb4d2005-10-20 17:10:49 +02001622 if (playback_bufsize < 4)
1623 playback_bufsize = 4;
1624 if (playback_bufsize > 128)
1625 playback_bufsize = 128;
1626 if (capture_bufsize < 4)
1627 capture_bufsize = 4;
1628 if (capture_bufsize > 128)
1629 capture_bufsize = 128;
Takashi Iwai3f05f862005-11-17 11:12:06 +01001630 if ((err = snd_nm256_create(card, pci, &chip)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 snd_card_free(card);
1632 return err;
1633 }
Takashi Iwai3fcf7d22005-11-17 16:11:24 +01001634 card->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
Takashi Iwai8a3fb4d2005-10-20 17:10:49 +02001636 if (reset_workaround) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 snd_printdd(KERN_INFO "nm256: reset_workaround activated\n");
1638 chip->reset_workaround = 1;
1639 }
1640
Takashi Iwai8a3fb4d2005-10-20 17:10:49 +02001641 if (reset_workaround_2) {
John W. Linville47530cf2005-10-19 16:03:10 +02001642 snd_printdd(KERN_INFO "nm256: reset_workaround_2 activated\n");
1643 chip->reset_workaround_2 = 1;
1644 }
1645
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 if ((err = snd_nm256_pcm(chip, 0)) < 0 ||
1647 (err = snd_nm256_mixer(chip)) < 0) {
1648 snd_card_free(card);
1649 return err;
1650 }
1651
1652 sprintf(card->shortname, "NeoMagic %s", card->driver);
1653 sprintf(card->longname, "%s at 0x%lx & 0x%lx, irq %d",
1654 card->shortname,
1655 chip->buffer_addr, chip->cport_addr, chip->irq);
1656
1657 if ((err = snd_card_register(card)) < 0) {
1658 snd_card_free(card);
1659 return err;
1660 }
1661
1662 pci_set_drvdata(pci, card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 return 0;
1664}
1665
1666static void __devexit snd_nm256_remove(struct pci_dev *pci)
1667{
1668 snd_card_free(pci_get_drvdata(pci));
1669 pci_set_drvdata(pci, NULL);
1670}
1671
1672
1673static struct pci_driver driver = {
1674 .name = "NeoMagic 256",
1675 .id_table = snd_nm256_ids,
1676 .probe = snd_nm256_probe,
1677 .remove = __devexit_p(snd_nm256_remove),
Takashi Iwai3fcf7d22005-11-17 16:11:24 +01001678#ifdef CONFIG_PM
1679 .suspend = nm256_suspend,
1680 .resume = nm256_resume,
1681#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682};
1683
1684
1685static int __init alsa_card_nm256_init(void)
1686{
Takashi Iwai01d25d42005-04-11 16:58:24 +02001687 return pci_register_driver(&driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688}
1689
1690static void __exit alsa_card_nm256_exit(void)
1691{
1692 pci_unregister_driver(&driver);
1693}
1694
1695module_init(alsa_card_nm256_init)
1696module_exit(alsa_card_nm256_exit)