initial commit with v2.6.9
[linux-2.6.9-moxart.git] / sound / pci / nm256 / nm256.c
blobe65e7de31f263008f73eeadc20bdd974dc8d03e3
1 /*
2 * Driver for NeoMagic 256AV and 256ZX chipsets.
3 * Copyright (c) 2000 by Takashi Iwai <tiwai@suse.de>
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
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.
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.
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
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>
35 #include <sound/core.h>
36 #include <sound/info.h>
37 #include <sound/control.h>
38 #include <sound/pcm.h>
39 #include <sound/ac97_codec.h>
40 #include <sound/initval.h>
42 #define CARD_NAME "NeoMagic 256AV/ZX"
43 #define DRIVER_NAME "NM256"
45 MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
46 MODULE_DESCRIPTION("NeoMagic NM256AV/ZX");
47 MODULE_LICENSE("GPL");
48 MODULE_SUPPORTED_DEVICE("{{NeoMagic,NM256AV},"
49 "{NeoMagic,NM256ZX}}");
52 * some compile conditions.
55 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
56 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
57 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
58 static int playback_bufsize[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 16};
59 static int capture_bufsize[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 16};
60 static int force_ac97[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 0}; /* disabled as default */
61 static int buffer_top[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 0}; /* not specified */
62 static int use_cache[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 0}; /* disabled */
63 static int vaio_hack[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 0}; /* disabled */
64 static int boot_devs;
66 module_param_array(index, int, boot_devs, 0444);
67 MODULE_PARM_DESC(index, "Index value for " CARD_NAME " soundcard.");
68 module_param_array(id, charp, boot_devs, 0444);
69 MODULE_PARM_DESC(id, "ID string for " CARD_NAME " soundcard.");
70 module_param_array(enable, bool, boot_devs, 0444);
71 MODULE_PARM_DESC(enable, "Enable this soundcard.");
72 module_param_array(playback_bufsize, int, boot_devs, 0444);
73 MODULE_PARM_DESC(playback_bufsize, "DAC frame size in kB for " CARD_NAME " soundcard.");
74 module_param_array(capture_bufsize, int, boot_devs, 0444);
75 MODULE_PARM_DESC(capture_bufsize, "ADC frame size in kB for " CARD_NAME " soundcard.");
76 module_param_array(force_ac97, bool, boot_devs, 0444);
77 MODULE_PARM_DESC(force_ac97, "Force to use AC97 codec for " CARD_NAME " soundcard.");
78 module_param_array(buffer_top, int, boot_devs, 0444);
79 MODULE_PARM_DESC(buffer_top, "Set the top address of audio buffer for " CARD_NAME " soundcard.");
80 module_param_array(use_cache, bool, boot_devs, 0444);
81 MODULE_PARM_DESC(use_cache, "Enable the cache for coefficient table access.");
82 module_param_array(vaio_hack, bool, boot_devs, 0444);
83 MODULE_PARM_DESC(vaio_hack, "Enable workaround for Sony VAIO notebooks.");
86 * hw definitions
89 /* The BIOS signature. */
90 #define NM_SIGNATURE 0x4e4d0000
91 /* Signature mask. */
92 #define NM_SIG_MASK 0xffff0000
94 /* Size of the second memory area. */
95 #define NM_PORT2_SIZE 4096
97 /* The base offset of the mixer in the second memory area. */
98 #define NM_MIXER_OFFSET 0x600
100 /* The maximum size of a coefficient entry. */
101 #define NM_MAX_PLAYBACK_COEF_SIZE 0x5000
102 #define NM_MAX_RECORD_COEF_SIZE 0x1260
104 /* The interrupt register. */
105 #define NM_INT_REG 0xa04
106 /* And its bits. */
107 #define NM_PLAYBACK_INT 0x40
108 #define NM_RECORD_INT 0x100
109 #define NM_MISC_INT_1 0x4000
110 #define NM_MISC_INT_2 0x1
111 #define NM_ACK_INT(chip, X) snd_nm256_writew(chip, NM_INT_REG, (X) << 1)
113 /* The AV's "mixer ready" status bit and location. */
114 #define NM_MIXER_STATUS_OFFSET 0xa04
115 #define NM_MIXER_READY_MASK 0x0800
116 #define NM_MIXER_PRESENCE 0xa06
117 #define NM_PRESENCE_MASK 0x0050
118 #define NM_PRESENCE_VALUE 0x0040
121 * For the ZX. It uses the same interrupt register, but it holds 32
122 * bits instead of 16.
124 #define NM2_PLAYBACK_INT 0x10000
125 #define NM2_RECORD_INT 0x80000
126 #define NM2_MISC_INT_1 0x8
127 #define NM2_MISC_INT_2 0x2
128 #define NM2_ACK_INT(chip, X) snd_nm256_writel(chip, NM_INT_REG, (X))
130 /* The ZX's "mixer ready" status bit and location. */
131 #define NM2_MIXER_STATUS_OFFSET 0xa06
132 #define NM2_MIXER_READY_MASK 0x0800
134 /* The playback registers start from here. */
135 #define NM_PLAYBACK_REG_OFFSET 0x0
136 /* The record registers start from here. */
137 #define NM_RECORD_REG_OFFSET 0x200
139 /* The rate register is located 2 bytes from the start of the register area. */
140 #define NM_RATE_REG_OFFSET 2
142 /* Mono/stereo flag, number of bits on playback, and rate mask. */
143 #define NM_RATE_STEREO 1
144 #define NM_RATE_BITS_16 2
145 #define NM_RATE_MASK 0xf0
147 /* Playback enable register. */
148 #define NM_PLAYBACK_ENABLE_REG (NM_PLAYBACK_REG_OFFSET + 0x1)
149 #define NM_PLAYBACK_ENABLE_FLAG 1
150 #define NM_PLAYBACK_ONESHOT 2
151 #define NM_PLAYBACK_FREERUN 4
153 /* Mutes the audio output. */
154 #define NM_AUDIO_MUTE_REG (NM_PLAYBACK_REG_OFFSET + 0x18)
155 #define NM_AUDIO_MUTE_LEFT 0x8000
156 #define NM_AUDIO_MUTE_RIGHT 0x0080
158 /* Recording enable register. */
159 #define NM_RECORD_ENABLE_REG (NM_RECORD_REG_OFFSET + 0)
160 #define NM_RECORD_ENABLE_FLAG 1
161 #define NM_RECORD_FREERUN 2
163 /* coefficient buffer pointer */
164 #define NM_COEFF_START_OFFSET 0x1c
165 #define NM_COEFF_END_OFFSET 0x20
167 /* DMA buffer offsets */
168 #define NM_RBUFFER_START (NM_RECORD_REG_OFFSET + 0x4)
169 #define NM_RBUFFER_END (NM_RECORD_REG_OFFSET + 0x10)
170 #define NM_RBUFFER_WMARK (NM_RECORD_REG_OFFSET + 0xc)
171 #define NM_RBUFFER_CURRP (NM_RECORD_REG_OFFSET + 0x8)
173 #define NM_PBUFFER_START (NM_PLAYBACK_REG_OFFSET + 0x4)
174 #define NM_PBUFFER_END (NM_PLAYBACK_REG_OFFSET + 0x14)
175 #define NM_PBUFFER_WMARK (NM_PLAYBACK_REG_OFFSET + 0xc)
176 #define NM_PBUFFER_CURRP (NM_PLAYBACK_REG_OFFSET + 0x8)
179 * type definitions
182 typedef struct snd_nm256 nm256_t;
183 typedef struct snd_nm256_stream nm256_stream_t;
185 struct snd_nm256_stream {
187 nm256_t *chip;
188 snd_pcm_substream_t *substream;
189 int running;
191 u32 buf; /* offset from chip->buffer */
192 int bufsize; /* buffer size in bytes */
193 unsigned long bufptr; /* mapped pointer */
194 unsigned long bufptr_addr; /* physical address of the mapped pointer */
196 int dma_size; /* buffer size of the substream in bytes */
197 int period_size; /* period size in bytes */
198 int periods; /* # of periods */
199 int shift; /* bit shifts */
200 int cur_period; /* current period # */
204 struct snd_nm256 {
206 snd_card_t *card;
208 unsigned long cport; /* control port */
209 struct resource *res_cport; /* its resource */
210 unsigned long cport_addr; /* physical address */
212 unsigned long buffer; /* buffer */
213 struct resource *res_buffer; /* its resource */
214 unsigned long buffer_addr; /* buffer phyiscal address */
216 u32 buffer_start; /* start offset from pci resource 0 */
217 u32 buffer_end; /* end offset */
218 u32 buffer_size; /* total buffer size */
220 u32 all_coeff_buf; /* coefficient buffer */
221 u32 coeff_buf[2]; /* coefficient buffer for each stream */
223 unsigned int coeffs_current: 1; /* coeff. table is loaded? */
224 unsigned int use_cache: 1; /* use one big coef. table */
225 unsigned int latitude_workaround: 1; /* Dell Latitude LS workaround needed */
227 int mixer_base; /* register offset of ac97 mixer */
228 int mixer_status_offset; /* offset of mixer status reg. */
229 int mixer_status_mask; /* bit mask to test the mixer status */
231 int irq;
232 irqreturn_t (*interrupt)(int, void *, struct pt_regs *);
233 int badintrcount; /* counter to check bogus interrupts */
235 nm256_stream_t streams[2];
237 ac97_t *ac97;
239 snd_pcm_t *pcm;
241 struct pci_dev *pci;
243 spinlock_t reg_lock;
249 * include coefficient table
251 #include "nm256_coef.c"
255 * PCI ids
258 #ifndef PCI_VENDOR_ID_NEOMAGIC
259 #define PCI_VENDOR_ID_NEOMEGIC 0x10c8
260 #endif
261 #ifndef PCI_DEVICE_ID_NEOMAGIC_NM256AV_AUDIO
262 #define PCI_DEVICE_ID_NEOMAGIC_NM256AV_AUDIO 0x8005
263 #endif
264 #ifndef PCI_DEVICE_ID_NEOMAGIC_NM256ZX_AUDIO
265 #define PCI_DEVICE_ID_NEOMAGIC_NM256ZX_AUDIO 0x8006
266 #endif
267 #ifndef PCI_DEVICE_ID_NEOMAGIC_NM256XL_PLUS_AUDIO
268 #define PCI_DEVICE_ID_NEOMAGIC_NM256XL_PLUS_AUDIO 0x8016
269 #endif
272 static struct pci_device_id snd_nm256_ids[] = {
273 {PCI_VENDOR_ID_NEOMAGIC, PCI_DEVICE_ID_NEOMAGIC_NM256AV_AUDIO, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
274 {PCI_VENDOR_ID_NEOMAGIC, PCI_DEVICE_ID_NEOMAGIC_NM256ZX_AUDIO, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
275 {PCI_VENDOR_ID_NEOMAGIC, PCI_DEVICE_ID_NEOMAGIC_NM256XL_PLUS_AUDIO, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
276 {0,},
279 MODULE_DEVICE_TABLE(pci, snd_nm256_ids);
283 * lowlvel stuffs
286 inline static u8
287 snd_nm256_readb(nm256_t *chip, int offset)
289 return readb(chip->cport + offset);
292 inline static u16
293 snd_nm256_readw(nm256_t *chip, int offset)
295 return readw(chip->cport + offset);
298 inline static u32
299 snd_nm256_readl(nm256_t *chip, int offset)
301 return readl(chip->cport + offset);
304 inline static void
305 snd_nm256_writeb(nm256_t *chip, int offset, u8 val)
307 writeb(val, chip->cport + offset);
310 inline static void
311 snd_nm256_writew(nm256_t *chip, int offset, u16 val)
313 writew(val, chip->cport + offset);
316 inline static void
317 snd_nm256_writel(nm256_t *chip, int offset, u32 val)
319 writel(val, chip->cport + offset);
322 inline static void
323 snd_nm256_write_buffer(nm256_t *chip, void *src, int offset, int size)
325 offset -= chip->buffer_start;
326 #ifdef SNDRV_CONFIG_DEBUG
327 if (offset < 0 || offset >= chip->buffer_size) {
328 snd_printk("write_buffer invalid offset = %d size = %d\n", offset, size);
329 return;
331 #endif
332 memcpy_toio((void *)chip->buffer + offset, src, size);
336 * coefficient handlers -- what a magic!
339 static u16
340 snd_nm256_get_start_offset(int which)
342 u16 offset = 0;
343 while (which-- > 0)
344 offset += coefficient_sizes[which];
345 return offset;
348 static void
349 snd_nm256_load_one_coefficient(nm256_t *chip, int stream, u32 port, int which)
351 u32 coeff_buf = chip->coeff_buf[stream];
352 u16 offset = snd_nm256_get_start_offset(which);
353 u16 size = coefficient_sizes[which];
355 snd_nm256_write_buffer(chip, coefficients + offset, coeff_buf, size);
356 snd_nm256_writel(chip, port, coeff_buf);
357 /* ??? Record seems to behave differently than playback. */
358 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
359 size--;
360 snd_nm256_writel(chip, port + 4, coeff_buf + size);
363 static void
364 snd_nm256_load_coefficient(nm256_t *chip, int stream, int number)
366 /* The enable register for the specified engine. */
367 u32 poffset = (stream == SNDRV_PCM_STREAM_CAPTURE ? NM_RECORD_ENABLE_REG : NM_PLAYBACK_ENABLE_REG);
368 u32 addr = NM_COEFF_START_OFFSET;
370 addr += (stream == SNDRV_PCM_STREAM_CAPTURE ? NM_RECORD_REG_OFFSET : NM_PLAYBACK_REG_OFFSET);
372 if (snd_nm256_readb(chip, poffset) & 1) {
373 snd_printd("NM256: Engine was enabled while loading coefficients!\n");
374 return;
377 /* The recording engine uses coefficient values 8-15. */
378 number &= 7;
379 if (stream == SNDRV_PCM_STREAM_CAPTURE)
380 number += 8;
382 if (! chip->use_cache) {
383 snd_nm256_load_one_coefficient(chip, stream, addr, number);
384 return;
386 if (! chip->coeffs_current) {
387 snd_nm256_write_buffer(chip, coefficients, chip->all_coeff_buf,
388 NM_TOTAL_COEFF_COUNT * 4);
389 chip->coeffs_current = 1;
390 } else {
391 u32 base = chip->all_coeff_buf;
392 u32 offset = snd_nm256_get_start_offset(number);
393 u32 end_offset = offset + coefficient_sizes[number];
394 snd_nm256_writel(chip, addr, base + offset);
395 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
396 end_offset--;
397 snd_nm256_writel(chip, addr + 4, base + end_offset);
402 /* The actual rates supported by the card. */
403 static unsigned int samplerates[8] = {
404 8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000,
406 static snd_pcm_hw_constraint_list_t constraints_rates = {
407 .count = ARRAY_SIZE(samplerates),
408 .list = samplerates,
409 .mask = 0,
413 * return the index of the target rate
415 static int
416 snd_nm256_fixed_rate(unsigned int rate)
418 unsigned int i;
419 for (i = 0; i < ARRAY_SIZE(samplerates); i++) {
420 if (rate == samplerates[i])
421 return i;
423 snd_BUG();
424 return 0;
428 * set sample rate and format
430 static void
431 snd_nm256_set_format(nm256_t *chip, nm256_stream_t *s, snd_pcm_substream_t *substream)
433 snd_pcm_runtime_t *runtime = substream->runtime;
434 int rate_index = snd_nm256_fixed_rate(runtime->rate);
435 unsigned char ratebits = (rate_index << 4) & NM_RATE_MASK;
437 s->shift = 0;
438 if (snd_pcm_format_width(runtime->format) == 16) {
439 ratebits |= NM_RATE_BITS_16;
440 s->shift++;
442 if (runtime->channels > 1) {
443 ratebits |= NM_RATE_STEREO;
444 s->shift++;
447 runtime->rate = samplerates[rate_index];
449 switch (substream->stream) {
450 case SNDRV_PCM_STREAM_PLAYBACK:
451 snd_nm256_load_coefficient(chip, 0, rate_index); /* 0 = playback */
452 snd_nm256_writeb(chip,
453 NM_PLAYBACK_REG_OFFSET + NM_RATE_REG_OFFSET,
454 ratebits);
455 break;
456 case SNDRV_PCM_STREAM_CAPTURE:
457 snd_nm256_load_coefficient(chip, 1, rate_index); /* 1 = record */
458 snd_nm256_writeb(chip,
459 NM_RECORD_REG_OFFSET + NM_RATE_REG_OFFSET,
460 ratebits);
461 break;
466 * start / stop
469 /* update the watermark (current period) */
470 static void snd_nm256_pcm_mark(nm256_t *chip, nm256_stream_t *s, int reg)
472 s->cur_period++;
473 s->cur_period %= s->periods;
474 snd_nm256_writel(chip, reg, s->buf + s->cur_period * s->period_size);
477 #define snd_nm256_playback_mark(chip, s) snd_nm256_pcm_mark(chip, s, NM_PBUFFER_WMARK)
478 #define snd_nm256_capture_mark(chip, s) snd_nm256_pcm_mark(chip, s, NM_RBUFFER_WMARK)
480 static void
481 snd_nm256_playback_start(nm256_t *chip, nm256_stream_t *s, snd_pcm_substream_t *substream)
483 /* program buffer pointers */
484 snd_nm256_writel(chip, NM_PBUFFER_START, s->buf);
485 snd_nm256_writel(chip, NM_PBUFFER_END, s->buf + s->dma_size - (1 << s->shift));
486 snd_nm256_writel(chip, NM_PBUFFER_CURRP, s->buf);
487 snd_nm256_playback_mark(chip, s);
489 /* Enable playback engine and interrupts. */
490 snd_nm256_writeb(chip, NM_PLAYBACK_ENABLE_REG,
491 NM_PLAYBACK_ENABLE_FLAG | NM_PLAYBACK_FREERUN);
492 /* Enable both channels. */
493 snd_nm256_writew(chip, NM_AUDIO_MUTE_REG, 0x0);
496 static void
497 snd_nm256_capture_start(nm256_t *chip, nm256_stream_t *s, snd_pcm_substream_t *substream)
499 /* program buffer pointers */
500 snd_nm256_writel(chip, NM_RBUFFER_START, s->buf);
501 snd_nm256_writel(chip, NM_RBUFFER_END, s->buf + s->dma_size);
502 snd_nm256_writel(chip, NM_RBUFFER_CURRP, s->buf);
503 snd_nm256_capture_mark(chip, s);
505 /* Enable playback engine and interrupts. */
506 snd_nm256_writeb(chip, NM_RECORD_ENABLE_REG,
507 NM_RECORD_ENABLE_FLAG | NM_RECORD_FREERUN);
510 /* Stop the play engine. */
511 static void
512 snd_nm256_playback_stop(nm256_t *chip)
514 /* Shut off sound from both channels. */
515 snd_nm256_writew(chip, NM_AUDIO_MUTE_REG,
516 NM_AUDIO_MUTE_LEFT | NM_AUDIO_MUTE_RIGHT);
517 /* Disable play engine. */
518 snd_nm256_writeb(chip, NM_PLAYBACK_ENABLE_REG, 0);
521 static void
522 snd_nm256_capture_stop(nm256_t *chip)
524 /* Disable recording engine. */
525 snd_nm256_writeb(chip, NM_RECORD_ENABLE_REG, 0);
528 static int
529 snd_nm256_playback_trigger(snd_pcm_substream_t *substream, int cmd)
531 nm256_t *chip = snd_pcm_substream_chip(substream);
532 nm256_stream_t *s = (nm256_stream_t*)substream->runtime->private_data;
533 int err = 0;
535 snd_assert(s != NULL, return -ENXIO);
537 spin_lock(&chip->reg_lock);
538 switch (cmd) {
539 case SNDRV_PCM_TRIGGER_START:
540 case SNDRV_PCM_TRIGGER_RESUME:
541 if (! s->running) {
542 snd_nm256_playback_start(chip, s, substream);
543 s->running = 1;
545 break;
546 case SNDRV_PCM_TRIGGER_STOP:
547 case SNDRV_PCM_TRIGGER_SUSPEND:
548 if (s->running) {
549 snd_nm256_playback_stop(chip);
550 s->running = 0;
552 break;
553 default:
554 err = -EINVAL;
555 break;
557 spin_unlock(&chip->reg_lock);
558 return err;
561 static int
562 snd_nm256_capture_trigger(snd_pcm_substream_t *substream, int cmd)
564 nm256_t *chip = snd_pcm_substream_chip(substream);
565 nm256_stream_t *s = (nm256_stream_t*)substream->runtime->private_data;
566 int err = 0;
568 snd_assert(s != NULL, return -ENXIO);
570 spin_lock(&chip->reg_lock);
571 switch (cmd) {
572 case SNDRV_PCM_TRIGGER_START:
573 case SNDRV_PCM_TRIGGER_RESUME:
574 if (! s->running) {
575 snd_nm256_capture_start(chip, s, substream);
576 s->running = 1;
578 break;
579 case SNDRV_PCM_TRIGGER_STOP:
580 case SNDRV_PCM_TRIGGER_SUSPEND:
581 if (s->running) {
582 snd_nm256_capture_stop(chip);
583 s->running = 0;
585 break;
586 default:
587 err = -EINVAL;
588 break;
590 spin_unlock(&chip->reg_lock);
591 return err;
596 * prepare playback/capture channel
598 static int snd_nm256_pcm_prepare(snd_pcm_substream_t *substream)
600 nm256_t *chip = snd_pcm_substream_chip(substream);
601 snd_pcm_runtime_t *runtime = substream->runtime;
602 nm256_stream_t *s = (nm256_stream_t*)runtime->private_data;
604 snd_assert(s, return -ENXIO);
605 s->dma_size = frames_to_bytes(runtime, substream->runtime->buffer_size);
606 s->period_size = frames_to_bytes(runtime, substream->runtime->period_size);
607 s->periods = substream->runtime->periods;
608 s->cur_period = 0;
610 spin_lock_irq(&chip->reg_lock);
611 s->running = 0;
612 snd_nm256_set_format(chip, s, substream);
613 spin_unlock_irq(&chip->reg_lock);
615 return 0;
620 * get the current pointer
622 static snd_pcm_uframes_t
623 snd_nm256_playback_pointer(snd_pcm_substream_t * substream)
625 nm256_t *chip = snd_pcm_substream_chip(substream);
626 nm256_stream_t *s = (nm256_stream_t*)substream->runtime->private_data;
627 unsigned long curp;
629 snd_assert(s, return 0);
630 curp = snd_nm256_readl(chip, NM_PBUFFER_CURRP) - (unsigned long)s->buf;
631 curp %= s->dma_size;
632 return bytes_to_frames(substream->runtime, curp);
635 static snd_pcm_uframes_t
636 snd_nm256_capture_pointer(snd_pcm_substream_t * substream)
638 nm256_t *chip = snd_pcm_substream_chip(substream);
639 nm256_stream_t *s = (nm256_stream_t*)substream->runtime->private_data;
640 unsigned long curp;
642 snd_assert(s != NULL, return 0);
643 curp = snd_nm256_readl(chip, NM_RBUFFER_CURRP) - (unsigned long)s->buf;
644 curp %= s->dma_size;
645 return bytes_to_frames(substream->runtime, curp);
648 /* Remapped I/O space can be accessible as pointer on i386 */
649 /* This might be changed in the future */
650 #ifndef __i386__
652 * silence / copy for playback
654 static int
655 snd_nm256_playback_silence(snd_pcm_substream_t *substream,
656 int channel, /* not used (interleaved data) */
657 snd_pcm_uframes_t pos,
658 snd_pcm_uframes_t count)
660 snd_pcm_runtime_t *runtime = substream->runtime;
661 nm256_stream_t *s = (nm256_stream_t*)runtime->private_data;
662 count = frames_to_bytes(runtime, count);
663 pos = frames_to_bytes(runtime, pos);
664 memset_io(s->bufptr + pos, 0, count);
665 return 0;
668 static int
669 snd_nm256_playback_copy(snd_pcm_substream_t *substream,
670 int channel, /* not used (interleaved data) */
671 snd_pcm_uframes_t pos,
672 void __user *src,
673 snd_pcm_uframes_t count)
675 snd_pcm_runtime_t *runtime = substream->runtime;
676 nm256_stream_t *s = (nm256_stream_t*)runtime->private_data;
677 count = frames_to_bytes(runtime, count);
678 pos = frames_to_bytes(runtime, pos);
679 if (copy_from_user_toio(s->bufptr + pos, src, count))
680 return -EFAULT;
681 return 0;
685 * copy to user
687 static int
688 snd_nm256_capture_copy(snd_pcm_substream_t *substream,
689 int channel, /* not used (interleaved data) */
690 snd_pcm_uframes_t pos,
691 void __user *dst,
692 snd_pcm_uframes_t count)
694 snd_pcm_runtime_t *runtime = substream->runtime;
695 nm256_stream_t *s = (nm256_stream_t*)runtime->private_data;
696 count = frames_to_bytes(runtime, count);
697 pos = frames_to_bytes(runtime, pos);
698 if (copy_to_user_fromio(dst, s->bufptr + pos, count))
699 return -EFAULT;
700 return 0;
703 #endif /* !__i386__ */
707 * update playback/capture watermarks
710 /* spinlock held! */
711 static void
712 snd_nm256_playback_update(nm256_t *chip)
714 nm256_stream_t *s;
716 s = &chip->streams[SNDRV_PCM_STREAM_PLAYBACK];
717 if (s->running && s->substream) {
718 spin_unlock(&chip->reg_lock);
719 snd_pcm_period_elapsed(s->substream);
720 spin_lock(&chip->reg_lock);
721 snd_nm256_playback_mark(chip, s);
725 /* spinlock held! */
726 static void
727 snd_nm256_capture_update(nm256_t *chip)
729 nm256_stream_t *s;
731 s = &chip->streams[SNDRV_PCM_STREAM_CAPTURE];
732 if (s->running && s->substream) {
733 spin_unlock(&chip->reg_lock);
734 snd_pcm_period_elapsed(s->substream);
735 spin_lock(&chip->reg_lock);
736 snd_nm256_capture_mark(chip, s);
741 * hardware info
743 static snd_pcm_hardware_t snd_nm256_playback =
745 .info = SNDRV_PCM_INFO_MMAP_IOMEM |SNDRV_PCM_INFO_MMAP_VALID |
746 SNDRV_PCM_INFO_INTERLEAVED |
747 /*SNDRV_PCM_INFO_PAUSE |*/
748 SNDRV_PCM_INFO_RESUME,
749 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
750 .rates = SNDRV_PCM_RATE_KNOT/*24k*/ | SNDRV_PCM_RATE_8000_48000,
751 .rate_min = 8000,
752 .rate_max = 48000,
753 .channels_min = 1,
754 .channels_max = 2,
755 .periods_min = 2,
756 .periods_max = 1024,
757 .buffer_bytes_max = 128 * 1024,
758 .period_bytes_min = 256,
759 .period_bytes_max = 128 * 1024,
762 static snd_pcm_hardware_t snd_nm256_capture =
764 .info = SNDRV_PCM_INFO_MMAP_IOMEM | SNDRV_PCM_INFO_MMAP_VALID |
765 SNDRV_PCM_INFO_INTERLEAVED |
766 /*SNDRV_PCM_INFO_PAUSE |*/
767 SNDRV_PCM_INFO_RESUME,
768 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
769 .rates = SNDRV_PCM_RATE_KNOT/*24k*/ | SNDRV_PCM_RATE_8000_48000,
770 .rate_min = 8000,
771 .rate_max = 48000,
772 .channels_min = 1,
773 .channels_max = 2,
774 .periods_min = 2,
775 .periods_max = 1024,
776 .buffer_bytes_max = 128 * 1024,
777 .period_bytes_min = 256,
778 .period_bytes_max = 128 * 1024,
782 /* set dma transfer size */
783 static int snd_nm256_pcm_hw_params(snd_pcm_substream_t *substream, snd_pcm_hw_params_t *hw_params)
785 /* area and addr are already set and unchanged */
786 substream->runtime->dma_bytes = params_buffer_bytes(hw_params);
787 return 0;
791 * open
793 static void snd_nm256_setup_stream(nm256_t *chip, nm256_stream_t *s,
794 snd_pcm_substream_t *substream,
795 snd_pcm_hardware_t *hw_ptr)
797 snd_pcm_runtime_t *runtime = substream->runtime;
799 s->running = 0;
800 runtime->hw = *hw_ptr;
801 runtime->hw.buffer_bytes_max = s->bufsize;
802 runtime->hw.period_bytes_max = s->bufsize / 2;
803 runtime->dma_area = (void*) s->bufptr;
804 runtime->dma_addr = s->bufptr_addr;
805 runtime->dma_bytes = s->bufsize;
806 runtime->private_data = s;
807 s->substream = substream;
809 snd_pcm_set_sync(substream);
810 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
811 &constraints_rates);
814 static int
815 snd_nm256_playback_open(snd_pcm_substream_t *substream)
817 nm256_t *chip = snd_pcm_substream_chip(substream);
819 snd_nm256_setup_stream(chip, &chip->streams[SNDRV_PCM_STREAM_PLAYBACK],
820 substream, &snd_nm256_playback);
821 return 0;
824 static int
825 snd_nm256_capture_open(snd_pcm_substream_t *substream)
827 nm256_t *chip = snd_pcm_substream_chip(substream);
829 snd_nm256_setup_stream(chip, &chip->streams[SNDRV_PCM_STREAM_CAPTURE],
830 substream, &snd_nm256_capture);
831 return 0;
835 * close - we don't have to do special..
837 static int
838 snd_nm256_playback_close(snd_pcm_substream_t *substream)
840 return 0;
844 static int
845 snd_nm256_capture_close(snd_pcm_substream_t *substream)
847 return 0;
851 * create a pcm instance
853 static snd_pcm_ops_t snd_nm256_playback_ops = {
854 .open = snd_nm256_playback_open,
855 .close = snd_nm256_playback_close,
856 .ioctl = snd_pcm_lib_ioctl,
857 .hw_params = snd_nm256_pcm_hw_params,
858 .prepare = snd_nm256_pcm_prepare,
859 .trigger = snd_nm256_playback_trigger,
860 .pointer = snd_nm256_playback_pointer,
861 #ifndef __i386__
862 .copy = snd_nm256_playback_copy,
863 .silence = snd_nm256_playback_silence,
864 #endif
865 .mmap = snd_pcm_lib_mmap_iomem,
868 static snd_pcm_ops_t snd_nm256_capture_ops = {
869 .open = snd_nm256_capture_open,
870 .close = snd_nm256_capture_close,
871 .ioctl = snd_pcm_lib_ioctl,
872 .hw_params = snd_nm256_pcm_hw_params,
873 .prepare = snd_nm256_pcm_prepare,
874 .trigger = snd_nm256_capture_trigger,
875 .pointer = snd_nm256_capture_pointer,
876 #ifndef __i386__
877 .copy = snd_nm256_capture_copy,
878 #endif
879 .mmap = snd_pcm_lib_mmap_iomem,
882 static int __devinit
883 snd_nm256_pcm(nm256_t *chip, int device)
885 snd_pcm_t *pcm;
886 int i, err;
888 for (i = 0; i < 2; i++) {
889 nm256_stream_t *s = &chip->streams[i];
890 s->bufptr = chip->buffer + s->buf - chip->buffer_start;
891 s->bufptr_addr = chip->buffer_addr + s->buf - chip->buffer_start;
894 err = snd_pcm_new(chip->card, chip->card->driver, device,
895 1, 1, &pcm);
896 if (err < 0)
897 return err;
899 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_nm256_playback_ops);
900 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_nm256_capture_ops);
902 pcm->private_data = chip;
903 pcm->info_flags = 0;
904 chip->pcm = pcm;
906 return 0;
911 * Initialize the hardware.
913 static void
914 snd_nm256_init_chip(nm256_t *chip)
916 spin_lock_irq(&chip->reg_lock);
917 /* Reset everything. */
918 snd_nm256_writeb(chip, 0x0, 0x11);
919 snd_nm256_writew(chip, 0x214, 0);
920 /* stop sounds.. */
921 //snd_nm256_playback_stop(chip);
922 //snd_nm256_capture_stop(chip);
923 spin_unlock_irq(&chip->reg_lock);
927 inline static void
928 snd_nm256_intr_check(nm256_t *chip)
930 if (chip->badintrcount++ > 1000) {
932 * I'm not sure if the best thing is to stop the card from
933 * playing or just release the interrupt (after all, we're in
934 * a bad situation, so doing fancy stuff may not be such a good
935 * idea).
937 * I worry about the card engine continuing to play noise
938 * over and over, however--that could become a very
939 * obnoxious problem. And we know that when this usually
940 * happens things are fairly safe, it just means the user's
941 * inserted a PCMCIA card and someone's spamming us with IRQ 9s.
943 if (chip->streams[SNDRV_PCM_STREAM_PLAYBACK].running)
944 snd_nm256_playback_stop(chip);
945 if (chip->streams[SNDRV_PCM_STREAM_CAPTURE].running)
946 snd_nm256_capture_stop(chip);
947 chip->badintrcount = 0;
952 * Handle a potential interrupt for the device referred to by DEV_ID.
954 * I don't like the cut-n-paste job here either between the two routines,
955 * but there are sufficient differences between the two interrupt handlers
956 * that parameterizing it isn't all that great either. (Could use a macro,
957 * I suppose...yucky bleah.)
960 static irqreturn_t
961 snd_nm256_interrupt(int irq, void *dev_id, struct pt_regs *dummy)
963 nm256_t *chip = dev_id;
964 u16 status;
965 u8 cbyte;
967 status = snd_nm256_readw(chip, NM_INT_REG);
969 /* Not ours. */
970 if (status == 0) {
971 snd_nm256_intr_check(chip);
972 return IRQ_NONE;
975 chip->badintrcount = 0;
977 /* Rather boring; check for individual interrupts and process them. */
979 spin_lock(&chip->reg_lock);
980 if (status & NM_PLAYBACK_INT) {
981 status &= ~NM_PLAYBACK_INT;
982 NM_ACK_INT(chip, NM_PLAYBACK_INT);
983 snd_nm256_playback_update(chip);
986 if (status & NM_RECORD_INT) {
987 status &= ~NM_RECORD_INT;
988 NM_ACK_INT(chip, NM_RECORD_INT);
989 snd_nm256_capture_update(chip);
992 if (status & NM_MISC_INT_1) {
993 status &= ~NM_MISC_INT_1;
994 NM_ACK_INT(chip, NM_MISC_INT_1);
995 snd_printd("NM256: Got misc interrupt #1\n");
996 snd_nm256_writew(chip, NM_INT_REG, 0x8000);
997 cbyte = snd_nm256_readb(chip, 0x400);
998 snd_nm256_writeb(chip, 0x400, cbyte | 2);
1001 if (status & NM_MISC_INT_2) {
1002 status &= ~NM_MISC_INT_2;
1003 NM_ACK_INT(chip, NM_MISC_INT_2);
1004 snd_printd("NM256: Got misc interrupt #2\n");
1005 cbyte = snd_nm256_readb(chip, 0x400);
1006 snd_nm256_writeb(chip, 0x400, cbyte & ~2);
1009 /* Unknown interrupt. */
1010 if (status) {
1011 snd_printd("NM256: Fire in the hole! Unknown status 0x%x\n",
1012 status);
1013 /* Pray. */
1014 NM_ACK_INT(chip, status);
1017 spin_unlock(&chip->reg_lock);
1018 return IRQ_HANDLED;
1022 * Handle a potential interrupt for the device referred to by DEV_ID.
1023 * This handler is for the 256ZX, and is very similar to the non-ZX
1024 * routine.
1027 static irqreturn_t
1028 snd_nm256_interrupt_zx(int irq, void *dev_id, struct pt_regs *dummy)
1030 nm256_t *chip = dev_id;
1031 u32 status;
1032 u8 cbyte;
1034 status = snd_nm256_readl(chip, NM_INT_REG);
1036 /* Not ours. */
1037 if (status == 0) {
1038 snd_nm256_intr_check(chip);
1039 return IRQ_NONE;
1042 chip->badintrcount = 0;
1044 /* Rather boring; check for individual interrupts and process them. */
1046 spin_lock(&chip->reg_lock);
1047 if (status & NM2_PLAYBACK_INT) {
1048 status &= ~NM2_PLAYBACK_INT;
1049 NM2_ACK_INT(chip, NM2_PLAYBACK_INT);
1050 snd_nm256_playback_update(chip);
1053 if (status & NM2_RECORD_INT) {
1054 status &= ~NM2_RECORD_INT;
1055 NM2_ACK_INT(chip, NM2_RECORD_INT);
1056 snd_nm256_capture_update(chip);
1059 if (status & NM2_MISC_INT_1) {
1060 status &= ~NM2_MISC_INT_1;
1061 NM2_ACK_INT(chip, NM2_MISC_INT_1);
1062 snd_printd("NM256: Got misc interrupt #1\n");
1063 cbyte = snd_nm256_readb(chip, 0x400);
1064 snd_nm256_writeb(chip, 0x400, cbyte | 2);
1067 if (status & NM2_MISC_INT_2) {
1068 status &= ~NM2_MISC_INT_2;
1069 NM2_ACK_INT(chip, NM2_MISC_INT_2);
1070 snd_printd("NM256: Got misc interrupt #2\n");
1071 cbyte = snd_nm256_readb(chip, 0x400);
1072 snd_nm256_writeb(chip, 0x400, cbyte & ~2);
1075 /* Unknown interrupt. */
1076 if (status) {
1077 snd_printd("NM256: Fire in the hole! Unknown status 0x%x\n",
1078 status);
1079 /* Pray. */
1080 NM2_ACK_INT(chip, status);
1083 spin_unlock(&chip->reg_lock);
1084 return IRQ_HANDLED;
1088 * AC97 interface
1092 * Waits for the mixer to become ready to be written; returns a zero value
1093 * if it timed out.
1095 static int
1096 snd_nm256_ac97_ready(nm256_t *chip)
1098 int timeout = 10;
1099 u32 testaddr;
1100 u16 testb;
1102 testaddr = chip->mixer_status_offset;
1103 testb = chip->mixer_status_mask;
1106 * Loop around waiting for the mixer to become ready.
1108 while (timeout-- > 0) {
1109 if ((snd_nm256_readw(chip, testaddr) & testb) == 0)
1110 return 1;
1111 udelay(100);
1113 return 0;
1118 static unsigned short
1119 snd_nm256_ac97_read(ac97_t *ac97, unsigned short reg)
1121 nm256_t *chip = ac97->private_data;
1122 int res;
1124 if (reg >= 128)
1125 return 0;
1127 if (! snd_nm256_ac97_ready(chip))
1128 return 0;
1129 res = snd_nm256_readw(chip, chip->mixer_base + reg);
1130 /* Magic delay. Bleah yucky. */
1131 udelay(1000);
1132 return res;
1137 static void
1138 snd_nm256_ac97_write(ac97_t *ac97,
1139 unsigned short reg, unsigned short val)
1141 nm256_t *chip = ac97->private_data;
1142 int tries = 2;
1143 u32 base;
1145 base = chip->mixer_base;
1147 snd_nm256_ac97_ready(chip);
1149 /* Wait for the write to take, too. */
1150 while (tries-- > 0) {
1151 snd_nm256_writew(chip, base + reg, val);
1152 udelay(1000); /* a little delay here seems better.. */
1153 if (snd_nm256_ac97_ready(chip))
1154 return;
1156 snd_printd("nm256: ac97 codec not ready..\n");
1159 /* initialize the ac97 into a known state */
1160 static void
1161 snd_nm256_ac97_reset(ac97_t *ac97)
1163 nm256_t *chip = ac97->private_data;
1165 spin_lock(&chip->reg_lock);
1166 /* Reset the mixer. 'Tis magic! */
1167 snd_nm256_writeb(chip, 0x6c0, 1);
1168 if (chip->latitude_workaround) {
1169 /* Dell latitude LS will lock up by this */
1170 snd_nm256_writeb(chip, 0x6cc, 0x87);
1172 snd_nm256_writeb(chip, 0x6cc, 0x80);
1173 snd_nm256_writeb(chip, 0x6cc, 0x0);
1174 spin_unlock(&chip->reg_lock);
1177 /* create an ac97 mixer interface */
1178 static int __devinit
1179 snd_nm256_mixer(nm256_t *chip)
1181 ac97_bus_t *pbus;
1182 ac97_template_t ac97;
1183 int i, err;
1184 static ac97_bus_ops_t ops = {
1185 .reset = snd_nm256_ac97_reset,
1186 .write = snd_nm256_ac97_write,
1187 .read = snd_nm256_ac97_read,
1189 /* looks like nm256 hangs up when unexpected registers are touched... */
1190 static int mixer_regs[] = {
1191 AC97_MASTER, AC97_HEADPHONE, AC97_MASTER_MONO,
1192 AC97_PC_BEEP, AC97_PHONE, AC97_MIC, AC97_LINE, AC97_CD,
1193 AC97_VIDEO, AC97_AUX, AC97_PCM, AC97_REC_SEL,
1194 AC97_REC_GAIN, AC97_GENERAL_PURPOSE, AC97_3D_CONTROL,
1195 AC97_EXTENDED_ID,
1196 AC97_VENDOR_ID1, AC97_VENDOR_ID2,
1200 if ((err = snd_ac97_bus(chip->card, 0, &ops, NULL, &pbus)) < 0)
1201 return err;
1203 memset(&ac97, 0, sizeof(ac97));
1204 ac97.scaps = AC97_SCAP_AUDIO; /* we support audio! */
1205 ac97.limited_regs = 1;
1206 for (i = 0; mixer_regs[i] >= 0; i++)
1207 set_bit(mixer_regs[i], ac97.reg_accessed);
1208 ac97.private_data = chip;
1209 err = snd_ac97_mixer(pbus, &ac97, &chip->ac97);
1210 if (err < 0)
1211 return err;
1212 if (! (chip->ac97->id & (0xf0000000))) {
1213 /* looks like an invalid id */
1214 sprintf(chip->card->mixername, "%s AC97", chip->card->driver);
1216 return 0;
1220 * See if the signature left by the NM256 BIOS is intact; if so, we use
1221 * the associated address as the end of our audio buffer in the video
1222 * RAM.
1225 static int __devinit
1226 snd_nm256_peek_for_sig(nm256_t *chip)
1228 /* The signature is located 1K below the end of video RAM. */
1229 unsigned long temp;
1230 /* Default buffer end is 5120 bytes below the top of RAM. */
1231 unsigned long pointer_found = chip->buffer_end - 0x1400;
1232 u32 sig;
1234 temp = (unsigned long) ioremap_nocache(chip->buffer_addr + chip->buffer_end - 0x400, 16);
1235 if (temp == 0) {
1236 snd_printk("Unable to scan for card signature in video RAM\n");
1237 return -EBUSY;
1240 sig = readl(temp);
1241 if ((sig & NM_SIG_MASK) == NM_SIGNATURE) {
1242 u32 pointer = readl(temp + 4);
1245 * If it's obviously invalid, don't use it
1247 if (pointer == 0xffffffff ||
1248 pointer < chip->buffer_size ||
1249 pointer > chip->buffer_end) {
1250 snd_printk("invalid signature found: 0x%x\n", pointer);
1251 iounmap((void *)temp);
1252 return -ENODEV;
1253 } else {
1254 pointer_found = pointer;
1255 printk(KERN_INFO "nm256: found card signature in video RAM: 0x%x\n", pointer);
1259 iounmap((void *)temp);
1260 chip->buffer_end = pointer_found;
1262 return 0;
1265 #ifdef CONFIG_PM
1267 * APM event handler, so the card is properly reinitialized after a power
1268 * event.
1270 static int nm256_suspend(snd_card_t *card, unsigned int state)
1272 nm256_t *chip = card->pm_private_data;
1274 snd_pcm_suspend_all(chip->pcm);
1275 snd_ac97_suspend(chip->ac97);
1276 chip->coeffs_current = 0;
1277 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1278 return 0;
1281 static int nm256_resume(snd_card_t *card, unsigned int state)
1283 nm256_t *chip = card->pm_private_data;
1285 /* Perform a full reset on the hardware */
1286 pci_enable_device(chip->pci);
1287 snd_nm256_init_chip(chip);
1289 /* restore ac97 */
1290 snd_ac97_resume(chip->ac97);
1292 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1293 return 0;
1295 #endif /* CONFIG_PM */
1297 static int snd_nm256_free(nm256_t *chip)
1299 if (chip->streams[SNDRV_PCM_STREAM_PLAYBACK].running)
1300 snd_nm256_playback_stop(chip);
1301 if (chip->streams[SNDRV_PCM_STREAM_CAPTURE].running)
1302 snd_nm256_capture_stop(chip);
1304 if (chip->irq >= 0)
1305 synchronize_irq(chip->irq);
1307 if (chip->cport)
1308 iounmap((void *) chip->cport);
1309 if (chip->buffer)
1310 iounmap((void *) chip->buffer);
1311 if (chip->res_cport) {
1312 release_resource(chip->res_cport);
1313 kfree_nocheck(chip->res_cport);
1315 if (chip->res_buffer) {
1316 release_resource(chip->res_buffer);
1317 kfree_nocheck(chip->res_buffer);
1319 if (chip->irq >= 0)
1320 free_irq(chip->irq, (void*)chip);
1322 kfree(chip);
1323 return 0;
1326 static int snd_nm256_dev_free(snd_device_t *device)
1328 nm256_t *chip = device->device_data;
1329 return snd_nm256_free(chip);
1332 static int __devinit
1333 snd_nm256_create(snd_card_t *card, struct pci_dev *pci,
1334 int play_bufsize, int capt_bufsize,
1335 int force_load,
1336 u32 buffertop,
1337 int usecache,
1338 nm256_t **chip_ret)
1340 nm256_t *chip;
1341 int err, pval;
1342 static snd_device_ops_t ops = {
1343 .dev_free = snd_nm256_dev_free,
1345 u32 addr;
1346 u16 subsystem_vendor, subsystem_device;
1348 *chip_ret = NULL;
1350 chip = kcalloc(1, sizeof(*chip), GFP_KERNEL);
1351 if (chip == NULL)
1352 return -ENOMEM;
1354 chip->card = card;
1355 chip->pci = pci;
1356 chip->use_cache = usecache;
1357 spin_lock_init(&chip->reg_lock);
1358 chip->irq = -1;
1360 chip->streams[SNDRV_PCM_STREAM_PLAYBACK].bufsize = play_bufsize;
1361 chip->streams[SNDRV_PCM_STREAM_CAPTURE].bufsize = capt_bufsize;
1364 * The NM256 has two memory ports. The first port is nothing
1365 * more than a chunk of video RAM, which is used as the I/O ring
1366 * buffer. The second port has the actual juicy stuff (like the
1367 * mixer and the playback engine control registers).
1370 chip->buffer_addr = pci_resource_start(pci, 0);
1371 chip->cport_addr = pci_resource_start(pci, 1);
1373 /* Init the memory port info. */
1374 /* remap control port (#2) */
1375 chip->res_cport = request_mem_region(chip->cport_addr, NM_PORT2_SIZE,
1376 card->driver);
1377 if (chip->res_cport == NULL) {
1378 snd_printk("memory region 0x%lx (size 0x%x) busy\n",
1379 chip->cport_addr, NM_PORT2_SIZE);
1380 err = -EBUSY;
1381 goto __error;
1383 chip->cport = (unsigned long) ioremap_nocache(chip->cport_addr, NM_PORT2_SIZE);
1384 if (chip->cport == 0) {
1385 snd_printk("unable to map control port %lx\n", chip->cport_addr);
1386 err = -ENOMEM;
1387 goto __error;
1390 if (!strcmp(card->driver, "NM256AV")) {
1391 /* Ok, try to see if this is a non-AC97 version of the hardware. */
1392 pval = snd_nm256_readw(chip, NM_MIXER_PRESENCE);
1393 if ((pval & NM_PRESENCE_MASK) != NM_PRESENCE_VALUE) {
1394 if (! force_load) {
1395 printk(KERN_ERR "nm256: no ac97 is found!\n");
1396 printk(KERN_ERR " force the driver to load by passing in the module parameter\n");
1397 printk(KERN_ERR " force_ac97=1\n");
1398 printk(KERN_ERR " or try sb16 or cs423x drivers instead.\n");
1399 err = -ENXIO;
1400 goto __error;
1403 chip->buffer_end = 2560 * 1024;
1404 chip->interrupt = snd_nm256_interrupt;
1405 chip->mixer_status_offset = NM_MIXER_STATUS_OFFSET;
1406 chip->mixer_status_mask = NM_MIXER_READY_MASK;
1407 } else {
1408 /* Not sure if there is any relevant detect for the ZX or not. */
1409 if (snd_nm256_readb(chip, 0xa0b) != 0)
1410 chip->buffer_end = 6144 * 1024;
1411 else
1412 chip->buffer_end = 4096 * 1024;
1414 chip->interrupt = snd_nm256_interrupt_zx;
1415 chip->mixer_status_offset = NM2_MIXER_STATUS_OFFSET;
1416 chip->mixer_status_mask = NM2_MIXER_READY_MASK;
1419 chip->buffer_size = chip->streams[SNDRV_PCM_STREAM_PLAYBACK].bufsize + chip->streams[SNDRV_PCM_STREAM_CAPTURE].bufsize;
1420 if (chip->use_cache)
1421 chip->buffer_size += NM_TOTAL_COEFF_COUNT * 4;
1422 else
1423 chip->buffer_size += NM_MAX_PLAYBACK_COEF_SIZE + NM_MAX_RECORD_COEF_SIZE;
1425 if (buffertop >= chip->buffer_size && buffertop < chip->buffer_end)
1426 chip->buffer_end = buffertop;
1427 else {
1428 /* get buffer end pointer from signature */
1429 if ((err = snd_nm256_peek_for_sig(chip)) < 0)
1430 goto __error;
1433 chip->buffer_start = chip->buffer_end - chip->buffer_size;
1434 chip->buffer_addr += chip->buffer_start;
1436 printk(KERN_INFO "nm256: Mapping port 1 from 0x%x - 0x%x\n",
1437 chip->buffer_start, chip->buffer_end);
1439 chip->res_buffer = request_mem_region(chip->buffer_addr,
1440 chip->buffer_size,
1441 card->driver);
1442 if (chip->res_buffer == NULL) {
1443 snd_printk("nm256: buffer 0x%lx (size 0x%x) busy\n",
1444 chip->buffer_addr, chip->buffer_size);
1445 err = -EBUSY;
1446 goto __error;
1448 chip->buffer = (unsigned long) ioremap_nocache(chip->buffer_addr, chip->buffer_size);
1449 if (chip->buffer == 0) {
1450 err = -ENOMEM;
1451 snd_printk("unable to map ring buffer at %lx\n", chip->buffer_addr);
1452 goto __error;
1455 /* set offsets */
1456 addr = chip->buffer_start;
1457 chip->streams[SNDRV_PCM_STREAM_PLAYBACK].buf = addr;
1458 addr += chip->streams[SNDRV_PCM_STREAM_PLAYBACK].bufsize;
1459 chip->streams[SNDRV_PCM_STREAM_CAPTURE].buf = addr;
1460 addr += chip->streams[SNDRV_PCM_STREAM_CAPTURE].bufsize;
1461 if (chip->use_cache) {
1462 chip->all_coeff_buf = addr;
1463 } else {
1464 chip->coeff_buf[SNDRV_PCM_STREAM_PLAYBACK] = addr;
1465 addr += NM_MAX_PLAYBACK_COEF_SIZE;
1466 chip->coeff_buf[SNDRV_PCM_STREAM_CAPTURE] = addr;
1469 /* acquire interrupt */
1470 if (request_irq(pci->irq, chip->interrupt, SA_INTERRUPT|SA_SHIRQ,
1471 card->driver, (void*)chip)) {
1472 err = -EBUSY;
1473 snd_printk("unable to grab IRQ %d\n", pci->irq);
1474 goto __error;
1476 chip->irq = pci->irq;
1478 /* Fixed setting. */
1479 chip->mixer_base = NM_MIXER_OFFSET;
1481 chip->coeffs_current = 0;
1483 /* check workarounds */
1484 chip->latitude_workaround = 1;
1485 pci_read_config_word(pci, PCI_SUBSYSTEM_VENDOR_ID, &subsystem_vendor);
1486 pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &subsystem_device);
1487 if (subsystem_vendor == 0x104d && subsystem_device == 0x8041) {
1488 /* this workaround will cause lock-up after suspend/resume on Sony PCG-F305 */
1489 chip->latitude_workaround = 0;
1491 if (subsystem_vendor == 0x1028 && subsystem_device == 0x0080) {
1492 /* this workaround will cause lock-up after suspend/resume on a Dell laptop */
1493 chip->latitude_workaround = 0;
1496 snd_nm256_init_chip(chip);
1498 if ((err = snd_nm256_pcm(chip, 0)) < 0)
1499 goto __error;
1501 if ((err = snd_nm256_mixer(chip)) < 0)
1502 goto __error;
1504 // pci_set_master(pci); /* needed? */
1506 snd_card_set_pm_callback(card, nm256_suspend, nm256_resume, chip);
1508 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0)
1509 goto __error;
1511 snd_card_set_dev(card, &pci->dev);
1513 *chip_ret = chip;
1514 return 0;
1516 __error:
1517 snd_nm256_free(chip);
1518 return err;
1522 struct nm256_quirk {
1523 unsigned short vendor;
1524 unsigned short device;
1525 int type;
1528 #define NM_BLACKLISTED 1
1530 static struct nm256_quirk nm256_quirks[] __devinitdata = {
1531 /* HP omnibook 4150 has cs4232 codec internally */
1532 { .vendor = 0x103c, .device = 0x0007, .type = NM_BLACKLISTED },
1533 { } /* terminator */
1537 static int __devinit snd_nm256_probe(struct pci_dev *pci,
1538 const struct pci_device_id *pci_id)
1540 static int dev;
1541 snd_card_t *card;
1542 nm256_t *chip;
1543 int err;
1544 unsigned int xbuffer_top;
1545 struct nm256_quirk *q;
1546 u16 subsystem_vendor, subsystem_device;
1548 if ((err = pci_enable_device(pci)) < 0)
1549 return err;
1551 if (dev >= SNDRV_CARDS)
1552 return -ENODEV;
1553 if (!enable[dev]) {
1554 dev++;
1555 return -ENOENT;
1558 pci_read_config_word(pci, PCI_SUBSYSTEM_VENDOR_ID, &subsystem_vendor);
1559 pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &subsystem_device);
1561 for (q = nm256_quirks; q->vendor; q++) {
1562 if (q->vendor == subsystem_vendor && q->device == subsystem_device) {
1563 if (q->type == NM_BLACKLISTED) {
1564 printk(KERN_INFO "nm256: The device is blacklisted. Loading stopped\n");
1565 return -ENODEV;
1570 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
1571 if (card == NULL)
1572 return -ENOMEM;
1574 switch (pci->device) {
1575 case PCI_DEVICE_ID_NEOMAGIC_NM256AV_AUDIO:
1576 strcpy(card->driver, "NM256AV");
1577 break;
1578 case PCI_DEVICE_ID_NEOMAGIC_NM256ZX_AUDIO:
1579 strcpy(card->driver, "NM256ZX");
1580 break;
1581 case PCI_DEVICE_ID_NEOMAGIC_NM256XL_PLUS_AUDIO:
1582 strcpy(card->driver, "NM256XL+");
1583 break;
1584 default:
1585 snd_printk("invalid device id 0x%x\n", pci->device);
1586 snd_card_free(card);
1587 return -EINVAL;
1590 if (vaio_hack[dev])
1591 xbuffer_top = 0x25a800; /* this avoids conflicts with XFree86 server */
1592 else
1593 xbuffer_top = buffer_top[dev];
1595 if (playback_bufsize[dev] < 4)
1596 playback_bufsize[dev] = 4;
1597 if (playback_bufsize[dev] > 128)
1598 playback_bufsize[dev] = 128;
1599 if (capture_bufsize[dev] < 4)
1600 capture_bufsize[dev] = 4;
1601 if (capture_bufsize[dev] > 128)
1602 capture_bufsize[dev] = 128;
1603 if ((err = snd_nm256_create(card, pci,
1604 playback_bufsize[dev] * 1024, /* in bytes */
1605 capture_bufsize[dev] * 1024, /* in bytes */
1606 force_ac97[dev],
1607 xbuffer_top,
1608 use_cache[dev],
1609 &chip)) < 0) {
1610 snd_card_free(card);
1611 return err;
1614 sprintf(card->shortname, "NeoMagic %s", card->driver);
1615 sprintf(card->longname, "%s at 0x%lx & 0x%lx, irq %d",
1616 card->shortname,
1617 chip->buffer_addr, chip->cport_addr, chip->irq);
1619 if ((err = snd_card_register(card)) < 0) {
1620 snd_card_free(card);
1621 return err;
1624 pci_set_drvdata(pci, card);
1625 dev++;
1626 return 0;
1629 static void __devexit snd_nm256_remove(struct pci_dev *pci)
1631 snd_card_free(pci_get_drvdata(pci));
1632 pci_set_drvdata(pci, NULL);
1636 static struct pci_driver driver = {
1637 .name = "NeoMagic 256",
1638 .id_table = snd_nm256_ids,
1639 .probe = snd_nm256_probe,
1640 .remove = __devexit_p(snd_nm256_remove),
1641 SND_PCI_PM_CALLBACKS
1645 static int __init alsa_card_nm256_init(void)
1647 return pci_module_init(&driver);
1650 static void __exit alsa_card_nm256_exit(void)
1652 pci_unregister_driver(&driver);
1655 module_init(alsa_card_nm256_init)
1656 module_exit(alsa_card_nm256_exit)