📄 nm256.c
字号:
/* * Driver for NeoMagic 256AV and 256ZX chipsets. * Copyright (c) 2000 by Takashi Iwai <tiwai@suse.de> * * Based on nm256_audio.c OSS driver in linux kernel. * The original author of OSS nm256 driver wishes to remain anonymous, * so I just put my acknoledgment to him/her here. * The original author's web page is found at * http://www.uglx.org/sony.html * * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include <sound/driver.h>#include <asm/io.h>#include <linux/delay.h>#include <linux/interrupt.h>#include <linux/init.h>#include <linux/pci.h>#include <linux/slab.h>#include <linux/moduleparam.h>#include <sound/core.h>#include <sound/info.h>#include <sound/control.h>#include <sound/pcm.h>#include <sound/ac97_codec.h>#include <sound/initval.h>#define CARD_NAME "NeoMagic 256AV/ZX"#define DRIVER_NAME "NM256"MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");MODULE_DESCRIPTION("NeoMagic NM256AV/ZX");MODULE_LICENSE("GPL");MODULE_SUPPORTED_DEVICE("{{NeoMagic,NM256AV}," "{NeoMagic,NM256ZX}}");/* * some compile conditions. */static int index = SNDRV_DEFAULT_IDX1; /* Index */static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */static int playback_bufsize = 16;static int capture_bufsize = 16;static int force_ac97; /* disabled as default */static int buffer_top; /* not specified */static int use_cache; /* disabled */static int vaio_hack; /* disabled */static int reset_workaround;static int reset_workaround_2;module_param(index, int, 0444);MODULE_PARM_DESC(index, "Index value for " CARD_NAME " soundcard.");module_param(id, charp, 0444);MODULE_PARM_DESC(id, "ID string for " CARD_NAME " soundcard.");module_param(playback_bufsize, int, 0444);MODULE_PARM_DESC(playback_bufsize, "DAC frame size in kB for " CARD_NAME " soundcard.");module_param(capture_bufsize, int, 0444);MODULE_PARM_DESC(capture_bufsize, "ADC frame size in kB for " CARD_NAME " soundcard.");module_param(force_ac97, bool, 0444);MODULE_PARM_DESC(force_ac97, "Force to use AC97 codec for " CARD_NAME " soundcard.");module_param(buffer_top, int, 0444);MODULE_PARM_DESC(buffer_top, "Set the top address of audio buffer for " CARD_NAME " soundcard.");module_param(use_cache, bool, 0444);MODULE_PARM_DESC(use_cache, "Enable the cache for coefficient table access.");module_param(vaio_hack, bool, 0444);MODULE_PARM_DESC(vaio_hack, "Enable workaround for Sony VAIO notebooks.");module_param(reset_workaround, bool, 0444);MODULE_PARM_DESC(reset_workaround, "Enable AC97 RESET workaround for some laptops.");module_param(reset_workaround_2, bool, 0444);MODULE_PARM_DESC(reset_workaround_2, "Enable extended AC97 RESET workaround for some other laptops.");/* just for backward compatibility */static int enable;module_param(enable, bool, 0444);/* * hw definitions *//* The BIOS signature. */#define NM_SIGNATURE 0x4e4d0000/* Signature mask. */#define NM_SIG_MASK 0xffff0000/* Size of the second memory area. */#define NM_PORT2_SIZE 4096/* The base offset of the mixer in the second memory area. */#define NM_MIXER_OFFSET 0x600/* The maximum size of a coefficient entry. */#define NM_MAX_PLAYBACK_COEF_SIZE 0x5000#define NM_MAX_RECORD_COEF_SIZE 0x1260/* The interrupt register. */#define NM_INT_REG 0xa04/* And its bits. */#define NM_PLAYBACK_INT 0x40#define NM_RECORD_INT 0x100#define NM_MISC_INT_1 0x4000#define NM_MISC_INT_2 0x1#define NM_ACK_INT(chip, X) snd_nm256_writew(chip, NM_INT_REG, (X) << 1)/* The AV's "mixer ready" status bit and location. */#define NM_MIXER_STATUS_OFFSET 0xa04#define NM_MIXER_READY_MASK 0x0800#define NM_MIXER_PRESENCE 0xa06#define NM_PRESENCE_MASK 0x0050#define NM_PRESENCE_VALUE 0x0040/* * For the ZX. It uses the same interrupt register, but it holds 32 * bits instead of 16. */#define NM2_PLAYBACK_INT 0x10000#define NM2_RECORD_INT 0x80000#define NM2_MISC_INT_1 0x8#define NM2_MISC_INT_2 0x2#define NM2_ACK_INT(chip, X) snd_nm256_writel(chip, NM_INT_REG, (X))/* The ZX's "mixer ready" status bit and location. */#define NM2_MIXER_STATUS_OFFSET 0xa06#define NM2_MIXER_READY_MASK 0x0800/* The playback registers start from here. */#define NM_PLAYBACK_REG_OFFSET 0x0/* The record registers start from here. */#define NM_RECORD_REG_OFFSET 0x200/* The rate register is located 2 bytes from the start of the register area. */#define NM_RATE_REG_OFFSET 2/* Mono/stereo flag, number of bits on playback, and rate mask. */#define NM_RATE_STEREO 1#define NM_RATE_BITS_16 2#define NM_RATE_MASK 0xf0/* Playback enable register. */#define NM_PLAYBACK_ENABLE_REG (NM_PLAYBACK_REG_OFFSET + 0x1)#define NM_PLAYBACK_ENABLE_FLAG 1#define NM_PLAYBACK_ONESHOT 2#define NM_PLAYBACK_FREERUN 4/* Mutes the audio output. */#define NM_AUDIO_MUTE_REG (NM_PLAYBACK_REG_OFFSET + 0x18)#define NM_AUDIO_MUTE_LEFT 0x8000#define NM_AUDIO_MUTE_RIGHT 0x0080/* Recording enable register. */#define NM_RECORD_ENABLE_REG (NM_RECORD_REG_OFFSET + 0)#define NM_RECORD_ENABLE_FLAG 1#define NM_RECORD_FREERUN 2/* coefficient buffer pointer */#define NM_COEFF_START_OFFSET 0x1c#define NM_COEFF_END_OFFSET 0x20/* DMA buffer offsets */#define NM_RBUFFER_START (NM_RECORD_REG_OFFSET + 0x4)#define NM_RBUFFER_END (NM_RECORD_REG_OFFSET + 0x10)#define NM_RBUFFER_WMARK (NM_RECORD_REG_OFFSET + 0xc)#define NM_RBUFFER_CURRP (NM_RECORD_REG_OFFSET + 0x8)#define NM_PBUFFER_START (NM_PLAYBACK_REG_OFFSET + 0x4)#define NM_PBUFFER_END (NM_PLAYBACK_REG_OFFSET + 0x14)#define NM_PBUFFER_WMARK (NM_PLAYBACK_REG_OFFSET + 0xc)#define NM_PBUFFER_CURRP (NM_PLAYBACK_REG_OFFSET + 0x8)/* * type definitions */typedef struct snd_nm256 nm256_t;typedef struct snd_nm256_stream nm256_stream_t;struct snd_nm256_stream { nm256_t *chip; snd_pcm_substream_t *substream; int running; int suspended; u32 buf; /* offset from chip->buffer */ int bufsize; /* buffer size in bytes */ void __iomem *bufptr; /* mapped pointer */ unsigned long bufptr_addr; /* physical address of the mapped pointer */ int dma_size; /* buffer size of the substream in bytes */ int period_size; /* period size in bytes */ int periods; /* # of periods */ int shift; /* bit shifts */ int cur_period; /* current period # */};struct snd_nm256 { snd_card_t *card; void __iomem *cport; /* control port */ struct resource *res_cport; /* its resource */ unsigned long cport_addr; /* physical address */ void __iomem *buffer; /* buffer */ struct resource *res_buffer; /* its resource */ unsigned long buffer_addr; /* buffer phyiscal address */ u32 buffer_start; /* start offset from pci resource 0 */ u32 buffer_end; /* end offset */ u32 buffer_size; /* total buffer size */ u32 all_coeff_buf; /* coefficient buffer */ u32 coeff_buf[2]; /* coefficient buffer for each stream */ unsigned int coeffs_current: 1; /* coeff. table is loaded? */ unsigned int use_cache: 1; /* use one big coef. table */ unsigned int reset_workaround: 1; /* Workaround for some laptops to avoid freeze */ unsigned int reset_workaround_2: 1; /* Extended workaround for some other laptops to avoid freeze */ int mixer_base; /* register offset of ac97 mixer */ int mixer_status_offset; /* offset of mixer status reg. */ int mixer_status_mask; /* bit mask to test the mixer status */ int irq; int irq_acks; irqreturn_t (*interrupt)(int, void *, struct pt_regs *); int badintrcount; /* counter to check bogus interrupts */ struct semaphore irq_mutex; nm256_stream_t streams[2]; ac97_t *ac97; snd_pcm_t *pcm; struct pci_dev *pci; spinlock_t reg_lock;};/* * include coefficient table */#include "nm256_coef.c"/* * PCI ids */static struct pci_device_id snd_nm256_ids[] = { {PCI_VENDOR_ID_NEOMAGIC, PCI_DEVICE_ID_NEOMAGIC_NM256AV_AUDIO, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, {PCI_VENDOR_ID_NEOMAGIC, PCI_DEVICE_ID_NEOMAGIC_NM256ZX_AUDIO, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, {PCI_VENDOR_ID_NEOMAGIC, PCI_DEVICE_ID_NEOMAGIC_NM256XL_PLUS_AUDIO, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, {0,},};MODULE_DEVICE_TABLE(pci, snd_nm256_ids);/* * lowlvel stuffs */static inline u8snd_nm256_readb(nm256_t *chip, int offset){ return readb(chip->cport + offset);}static inline u16snd_nm256_readw(nm256_t *chip, int offset){ return readw(chip->cport + offset);}static inline u32snd_nm256_readl(nm256_t *chip, int offset){ return readl(chip->cport + offset);}static inline voidsnd_nm256_writeb(nm256_t *chip, int offset, u8 val){ writeb(val, chip->cport + offset);}static inline voidsnd_nm256_writew(nm256_t *chip, int offset, u16 val){ writew(val, chip->cport + offset);}static inline voidsnd_nm256_writel(nm256_t *chip, int offset, u32 val){ writel(val, chip->cport + offset);}static inline voidsnd_nm256_write_buffer(nm256_t *chip, void *src, int offset, int size){ offset -= chip->buffer_start;#ifdef CONFIG_SND_DEBUG if (offset < 0 || offset >= chip->buffer_size) { snd_printk(KERN_ERR "write_buffer invalid offset = %d size = %d\n", offset, size); return; }#endif memcpy_toio(chip->buffer + offset, src, size);}/* * coefficient handlers -- what a magic! */static u16snd_nm256_get_start_offset(int which){ u16 offset = 0; while (which-- > 0) offset += coefficient_sizes[which]; return offset;}static voidsnd_nm256_load_one_coefficient(nm256_t *chip, int stream, u32 port, int which){ u32 coeff_buf = chip->coeff_buf[stream]; u16 offset = snd_nm256_get_start_offset(which); u16 size = coefficient_sizes[which]; snd_nm256_write_buffer(chip, coefficients + offset, coeff_buf, size); snd_nm256_writel(chip, port, coeff_buf); /* ??? Record seems to behave differently than playback. */ if (stream == SNDRV_PCM_STREAM_PLAYBACK) size--; snd_nm256_writel(chip, port + 4, coeff_buf + size);}static voidsnd_nm256_load_coefficient(nm256_t *chip, int stream, int number){ /* The enable register for the specified engine. */ u32 poffset = (stream == SNDRV_PCM_STREAM_CAPTURE ? NM_RECORD_ENABLE_REG : NM_PLAYBACK_ENABLE_REG); u32 addr = NM_COEFF_START_OFFSET; addr += (stream == SNDRV_PCM_STREAM_CAPTURE ? NM_RECORD_REG_OFFSET : NM_PLAYBACK_REG_OFFSET); if (snd_nm256_readb(chip, poffset) & 1) { snd_printd("NM256: Engine was enabled while loading coefficients!\n"); return; } /* The recording engine uses coefficient values 8-15. */ number &= 7; if (stream == SNDRV_PCM_STREAM_CAPTURE) number += 8; if (! chip->use_cache) { snd_nm256_load_one_coefficient(chip, stream, addr, number); return; } if (! chip->coeffs_current) { snd_nm256_write_buffer(chip, coefficients, chip->all_coeff_buf, NM_TOTAL_COEFF_COUNT * 4); chip->coeffs_current = 1; } else { u32 base = chip->all_coeff_buf; u32 offset = snd_nm256_get_start_offset(number); u32 end_offset = offset + coefficient_sizes[number]; snd_nm256_writel(chip, addr, base + offset); if (stream == SNDRV_PCM_STREAM_PLAYBACK) end_offset--; snd_nm256_writel(chip, addr + 4, base + end_offset); }}/* The actual rates supported by the card. */static unsigned int samplerates[8] = { 8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000,};static snd_pcm_hw_constraint_list_t constraints_rates = { .count = ARRAY_SIZE(samplerates), .list = samplerates, .mask = 0,};/* * return the index of the target rate */static intsnd_nm256_fixed_rate(unsigned int rate){ unsigned int i; for (i = 0; i < ARRAY_SIZE(samplerates); i++) { if (rate == samplerates[i]) return i; } snd_BUG(); return 0;}/* * set sample rate and format */static voidsnd_nm256_set_format(nm256_t *chip, nm256_stream_t *s, snd_pcm_substream_t *substream){ snd_pcm_runtime_t *runtime = substream->runtime; int rate_index = snd_nm256_fixed_rate(runtime->rate); unsigned char ratebits = (rate_index << 4) & NM_RATE_MASK; s->shift = 0; if (snd_pcm_format_width(runtime->format) == 16) { ratebits |= NM_RATE_BITS_16; s->shift++; } if (runtime->channels > 1) { ratebits |= NM_RATE_STEREO; s->shift++; } runtime->rate = samplerates[rate_index]; switch (substream->stream) { case SNDRV_PCM_STREAM_PLAYBACK: snd_nm256_load_coefficient(chip, 0, rate_index); /* 0 = playback */ snd_nm256_writeb(chip, NM_PLAYBACK_REG_OFFSET + NM_RATE_REG_OFFSET, ratebits); break; case SNDRV_PCM_STREAM_CAPTURE: snd_nm256_load_coefficient(chip, 1, rate_index); /* 1 = record */ snd_nm256_writeb(chip, NM_RECORD_REG_OFFSET + NM_RATE_REG_OFFSET, ratebits); break; }}/* acquire interrupt */static int snd_nm256_acquire_irq(nm256_t *chip){ down(&chip->irq_mutex); if (chip->irq < 0) { if (request_irq(chip->pci->irq, chip->interrupt, SA_INTERRUPT|SA_SHIRQ, chip->card->driver, (void*)chip)) { snd_printk(KERN_ERR "unable to grab IRQ %d\n", chip->pci->irq); up(&chip->irq_mutex); return -EBUSY; } chip->irq = chip->pci->irq; } chip->irq_acks++; up(&chip->irq_mutex); return 0;}/* release interrupt */static void snd_nm256_release_irq(nm256_t *chip){ down(&chip->irq_mutex); if (chip->irq_acks > 0) chip->irq_acks--; if (chip->irq_acks == 0 && chip->irq >= 0) { free_irq(chip->irq, (void*)chip); chip->irq = -1; } up(&chip->irq_mutex);}/* * start / stop *//* update the watermark (current period) */static void snd_nm256_pcm_mark(nm256_t *chip, nm256_stream_t *s, int reg){ s->cur_period++; s->cur_period %= s->periods; snd_nm256_writel(chip, reg, s->buf + s->cur_period * s->period_size);}#define snd_nm256_playback_mark(chip, s) snd_nm256_pcm_mark(chip, s, NM_PBUFFER_WMARK)#define snd_nm256_capture_mark(chip, s) snd_nm256_pcm_mark(chip, s, NM_RBUFFER_WMARK)static voidsnd_nm256_playback_start(nm256_t *chip, nm256_stream_t *s, snd_pcm_substream_t *substream){ /* program buffer pointers */ snd_nm256_writel(chip, NM_PBUFFER_START, s->buf); snd_nm256_writel(chip, NM_PBUFFER_END, s->buf + s->dma_size - (1 << s->shift)); snd_nm256_writel(chip, NM_PBUFFER_CURRP, s->buf); snd_nm256_playback_mark(chip, s); /* Enable playback engine and interrupts. */ snd_nm256_writeb(chip, NM_PLAYBACK_ENABLE_REG, NM_PLAYBACK_ENABLE_FLAG | NM_PLAYBACK_FREERUN); /* Enable both channels. */ snd_nm256_writew(chip, NM_AUDIO_MUTE_REG, 0x0);}static voidsnd_nm256_capture_start(nm256_t *chip, nm256_stream_t *s, snd_pcm_substream_t *substream){ /* program buffer pointers */ snd_nm256_writel(chip, NM_RBUFFER_START, s->buf); snd_nm256_writel(chip, NM_RBUFFER_END, s->buf + s->dma_size); snd_nm256_writel(chip, NM_RBUFFER_CURRP, s->buf); snd_nm256_capture_mark(chip, s); /* Enable playback engine and interrupts. */ snd_nm256_writeb(chip, NM_RECORD_ENABLE_REG, NM_RECORD_ENABLE_FLAG | NM_RECORD_FREERUN);}/* Stop the play engine. */static voidsnd_nm256_playback_stop(nm256_t *chip){ /* Shut off sound from both channels. */ snd_nm256_writew(chip, NM_AUDIO_MUTE_REG, NM_AUDIO_MUTE_LEFT | NM_AUDIO_MUTE_RIGHT); /* Disable play engine. */ snd_nm256_writeb(chip, NM_PLAYBACK_ENABLE_REG, 0);}static voidsnd_nm256_capture_stop(nm256_t *chip){ /* Disable recording engine. */ snd_nm256_writeb(chip, NM_RECORD_ENABLE_REG, 0);}static intsnd_nm256_playback_trigger(snd_pcm_substream_t *substream, int cmd){ nm256_t *chip = snd_pcm_substream_chip(substream); nm256_stream_t *s = (nm256_stream_t*)substream->runtime->private_data; int err = 0; snd_assert(s != NULL, return -ENXIO); spin_lock(&chip->reg_lock);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -