⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 es1688_lib.c

📁 是关于linux2.5.1的完全源码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* *  Copyright (c) by Jaroslav Kysela <perex@suse.cz> *  Routines for control of ESS ES1688/688/488 chip * * *   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 <asm/dma.h>#include <linux/init.h>#include <linux/delay.h>#include <linux/slab.h>#include <linux/ioport.h>#include <sound/core.h>#include <sound/es1688.h>#include <sound/initval.h>MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");MODULE_DESCRIPTION("ESS ESx688 lowlevel module");MODULE_CLASSES("{sound}");MODULE_LICENSE("GPL");#define chip_t es1688_tstatic int snd_es1688_dsp_command(es1688_t *chip, unsigned char val){	int i;	for (i = 10000; i; i--)		if ((inb(ES1688P(chip, STATUS)) & 0x80) == 0) {			outb(val, ES1688P(chip, COMMAND));			return 1;		}#ifdef CONFIG_SND_DEBUG	printk("snd_es1688_dsp_command: timeout (0x%x)\n", val);#endif	return 0;}static int snd_es1688_dsp_get_byte(es1688_t *chip){	int i;	for (i = 1000; i; i--)		if (inb(ES1688P(chip, DATA_AVAIL)) & 0x80)			return inb(ES1688P(chip, READ));	snd_printd("es1688 get byte failed: 0x%lx = 0x%x!!!\n", ES1688P(chip, DATA_AVAIL), inb(ES1688P(chip, DATA_AVAIL)));	return -ENODEV;}static int snd_es1688_write(es1688_t *chip,			    unsigned char reg, unsigned char data){	if (!snd_es1688_dsp_command(chip, reg))		return 0;	return snd_es1688_dsp_command(chip, data);}int snd_es1688_read(es1688_t *chip, unsigned char reg){	/* Read a byte from an extended mode register of ES1688 */	if (!snd_es1688_dsp_command(chip, 0xc0))		return -1;	if (!snd_es1688_dsp_command(chip, reg))		return -1;	return snd_es1688_dsp_get_byte(chip);}void snd_es1688_mixer_write(es1688_t *chip,			    unsigned char reg, unsigned char data){	outb(reg, ES1688P(chip, MIXER_ADDR));	udelay(10);	outb(data, ES1688P(chip, MIXER_DATA));	udelay(10);}unsigned char snd_es1688_mixer_read(es1688_t *chip, unsigned char reg){	unsigned char result;	outb(reg, ES1688P(chip, MIXER_ADDR));	udelay(10);	result = inb(ES1688P(chip, MIXER_DATA));	udelay(10);	return result;}static int snd_es1688_reset(es1688_t *chip){	int i;	outb(3, ES1688P(chip, RESET));		/* valid only for ESS chips, SB -> 1 */	udelay(10);	outb(0, ES1688P(chip, RESET));	udelay(30);	for (i = 0; i < 1000 && !(inb(ES1688P(chip, DATA_AVAIL)) & 0x80); i++);	if (inb(ES1688P(chip, READ)) != 0xaa) {		snd_printd("ess_reset at 0x%lx: failed!!!\n", chip->port);		return -ENODEV;	}	snd_es1688_dsp_command(chip, 0xc6);	/* enable extended mode */	return 0;}static int snd_es1688_probe(es1688_t *chip){	unsigned long flags;	unsigned short major, minor, hw;	int i;	/*	 *  initialization sequence	 */	spin_lock_irqsave(&chip->reg_lock, flags);	/* Some ESS1688 cards need this */	inb(ES1688P(chip, ENABLE1));	/* ENABLE1 */	inb(ES1688P(chip, ENABLE1));	/* ENABLE1 */	inb(ES1688P(chip, ENABLE1));	/* ENABLE1 */	inb(ES1688P(chip, ENABLE2));	/* ENABLE2 */	inb(ES1688P(chip, ENABLE1));	/* ENABLE1 */	inb(ES1688P(chip, ENABLE2));	/* ENABLE2 */	inb(ES1688P(chip, ENABLE1));	/* ENABLE1 */	inb(ES1688P(chip, ENABLE1));	/* ENABLE1 */	inb(ES1688P(chip, ENABLE2));	/* ENABLE2 */	inb(ES1688P(chip, ENABLE1));	/* ENABLE1 */	inb(ES1688P(chip, ENABLE0));	/* ENABLE0 */	if (snd_es1688_reset(chip) < 0) {		snd_printdd("ESS: [0x%lx] reset failed... 0x%x\n", chip->port, inb(ES1688P(chip, READ)));		spin_unlock_irqrestore(&chip->reg_lock, flags);		return -ENODEV;	}	snd_es1688_dsp_command(chip, 0xe7);	/* return identification */	for (i = 1000, major = minor = 0; i; i--) {		if (inb(ES1688P(chip, DATA_AVAIL)) & 0x80) {			if (major == 0) {				major = inb(ES1688P(chip, READ));			} else {				minor = inb(ES1688P(chip, READ));			}		}	}	spin_unlock_irqrestore(&chip->reg_lock, flags);	snd_printdd("ESS: [0x%lx] found.. major = 0x%x, minor = 0x%x\n", chip->port, major, minor);	chip->version = (major << 8) | minor;	if (!chip->version)		return -ENODEV;	/* probably SB */	hw = ES1688_HW_AUTO;	switch (chip->version & 0xfff0) {	case 0x4880:		snd_printk("[0x%lx] ESS: AudioDrive ES488 detected, but driver is in another place\n", chip->port);		return -ENODEV;	case 0x6880:		hw = (chip->version & 0x0f) >= 8 ? ES1688_HW_1688 : ES1688_HW_688;		break;	default:		snd_printk("[0x%lx] ESS: unknown AudioDrive chip with version 0x%x (Jazz16 soundcard?)\n", chip->port, chip->version);		return -ENODEV;	}	spin_lock_irqsave(&chip->reg_lock, flags);	snd_es1688_write(chip, 0xb1, 0x10);	/* disable IRQ */	snd_es1688_write(chip, 0xb2, 0x00);	/* disable DMA */	spin_unlock_irqrestore(&chip->reg_lock, flags);	/* enable joystick, but disable OPL3 */	spin_lock_irqsave(&chip->mixer_lock, flags);	snd_es1688_mixer_write(chip, 0x40, 0x01);	spin_unlock_irqrestore(&chip->mixer_lock, flags);	return 0;}static int snd_es1688_init(es1688_t * chip, int enable){	static int irqs[16] = {-1, -1, 0, -1, -1, 1, -1, 2, -1, 0, 3, -1, -1, -1, -1, -1};	unsigned long flags;	int cfg, irq_bits, dma, dma_bits, tmp, tmp1;	/* ok.. setup MPU-401 port and joystick and OPL3 */	cfg = 0x01;		/* enable joystick, but disable OPL3 */	if (enable && chip->mpu_port >= 0x300 && chip->mpu_irq > 0 && chip->hardware != ES1688_HW_688) {		tmp = (chip->mpu_port & 0x0f0) >> 4;		if (tmp <= 3) {			switch (chip->mpu_irq) {			case 9:				tmp1 = 4;				break;			case 5:				tmp1 = 5;				break;			case 7:				tmp1 = 6;				break;			case 10:				tmp1 = 7;				break;			default:				tmp1 = 0;			}			if (tmp1) {				cfg |= (tmp << 3) | (tmp1 << 5);			}		}	}#if 0	snd_printk("mpu cfg = 0x%x\n", cfg);#endif	spin_lock_irqsave(&chip->reg_lock, flags);	snd_es1688_mixer_write(chip, 0x40, cfg);	spin_unlock_irqrestore(&chip->reg_lock, flags);	/* --- */	spin_lock_irqsave(&chip->reg_lock, flags);	snd_es1688_read(chip, 0xb1);	snd_es1688_read(chip, 0xb2);	spin_unlock_irqrestore(&chip->reg_lock, flags);	if (enable) {		cfg = 0xf0;	/* enable only DMA counter interrupt */		irq_bits = irqs[chip->irq & 0x0f];		if (irq_bits < 0) {			snd_printk("[0x%lx] ESS: bad IRQ %d for ES1688 chip!!\n", chip->port, chip->irq);#if 0			irq_bits = 0;			cfg = 0x10;#endif			return -EINVAL;		}		spin_lock_irqsave(&chip->reg_lock, flags);		snd_es1688_write(chip, 0xb1, cfg | (irq_bits << 2));		spin_unlock_irqrestore(&chip->reg_lock, flags);		cfg = 0xf0;	/* extended mode DMA enable */		dma = chip->dma8;		if (dma > 3 || dma == 2) {			snd_printk("[0x%lx] ESS: bad DMA channel %d for ES1688 chip!!\n", chip->port, dma);#if 0			dma_bits = 0;			cfg = 0x00;	/* disable all DMA */#endif			return -EINVAL;		} else {			dma_bits = dma;			if (dma != 3)				dma_bits++;		}		spin_lock_irqsave(&chip->reg_lock, flags);		snd_es1688_write(chip, 0xb2, cfg | (dma_bits << 2));		spin_unlock_irqrestore(&chip->reg_lock, flags);	} else {		spin_lock_irqsave(&chip->reg_lock, flags);		snd_es1688_write(chip, 0xb1, 0x10);	/* disable IRQ */		snd_es1688_write(chip, 0xb2, 0x00);	/* disable DMA */		spin_unlock_irqrestore(&chip->reg_lock, flags);	}	spin_lock_irqsave(&chip->reg_lock, flags);	snd_es1688_read(chip, 0xb1);	snd_es1688_read(chip, 0xb2);	snd_es1688_reset(chip);	spin_unlock_irqrestore(&chip->reg_lock, flags);	return 0;}/* */static ratnum_t clocks[2] = {	{		num: 795444,		den_min: 1,		den_max: 128,		den_step: 1,	},	{		num: 397722,		den_min: 1,		den_max: 128,		den_step: 1,	}};static snd_pcm_hw_constraint_ratnums_t hw_constraints_clocks  = {	nrats: 2,	rats: clocks,};static void snd_es1688_set_rate(es1688_t *chip, snd_pcm_substream_t *substream){	snd_pcm_runtime_t *runtime = substream->runtime;	unsigned int bits, divider;	if (runtime->rate_num == clocks[0].num)		bits = 256 - runtime->rate_den;	else		bits = 128 - runtime->rate_den;	/* set filter register */	divider = 256 - 7160000*20/(8*82*runtime->rate);	/* write result to hardware */	snd_es1688_write(chip, 0xa1, bits);	snd_es1688_write(chip, 0xa2, divider);}static int snd_es1688_ioctl(snd_pcm_substream_t * substream,			    unsigned int cmd, void *arg){	return snd_pcm_lib_ioctl(substream, cmd, arg);}static int snd_es1688_trigger(es1688_t *chip, int cmd, unsigned char value){	int val;	if (cmd == SNDRV_PCM_TRIGGER_STOP) {		value = 0x00;	} else if (cmd != SNDRV_PCM_TRIGGER_START) {		return -EINVAL;	}	spin_lock(&chip->reg_lock);	chip->trigger_value = value;	val = snd_es1688_read(chip, 0xb8);	if ((val < 0) || (val & 0x0f) == value) {		spin_unlock(&chip->reg_lock);		return -EINVAL;	/* something is wrong */	}#if 0	printk("trigger: val = 0x%x, value = 0x%x\n", val, value);	printk("trigger: residue = 0x%x\n", get_dma_residue(chip->dma8));#endif	snd_es1688_write(chip, 0xb8, (val & 0xf0) | value);	spin_unlock(&chip->reg_lock);	return 0;}static int snd_es1688_hw_params(snd_pcm_substream_t * substream,				snd_pcm_hw_params_t * hw_params){	return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));}static int snd_es1688_hw_free(snd_pcm_substream_t * substream){	return snd_pcm_lib_free_pages(substream);}static int snd_es1688_playback_prepare(snd_pcm_substream_t * substream){	unsigned long flags;	es1688_t *chip = snd_pcm_substream_chip(substream);	snd_pcm_runtime_t *runtime = substream->runtime;	unsigned int size = snd_pcm_lib_buffer_bytes(substream);	unsigned int count = snd_pcm_lib_period_bytes(substream);	chip->dma_size = size;	spin_lock_irqsave(&chip->reg_lock, flags);	snd_es1688_reset(chip);	snd_es1688_set_rate(chip, substream);	snd_es1688_write(chip, 0xb8, 4);	/* auto init DMA mode */	snd_es1688_write(chip, 0xa8, (snd_es1688_read(chip, 0xa8) & ~0x03) | (3 - runtime->channels));	snd_es1688_write(chip, 0xb9, 2);	/* demand mode (4 bytes/request) */	if (runtime->channels == 1) {		if (snd_pcm_format_width(runtime->format) == 8) {			/* 8. bit mono */			snd_es1688_write(chip, 0xb6, 0x80);			snd_es1688_write(chip, 0xb7, 0x51);			snd_es1688_write(chip, 0xb7, 0xd0);		} else {			/* 16. bit mono */			snd_es1688_write(chip, 0xb6, 0x00);			snd_es1688_write(chip, 0xb7, 0x71);			snd_es1688_write(chip, 0xb7, 0xf4);		}	} else {		if (snd_pcm_format_width(runtime->format) == 8) {			/* 8. bit stereo */			snd_es1688_write(chip, 0xb6, 0x80);			snd_es1688_write(chip, 0xb7, 0x51);			snd_es1688_write(chip, 0xb7, 0x98);		} else {			/* 16. bit stereo */			snd_es1688_write(chip, 0xb6, 0x00);			snd_es1688_write(chip, 0xb7, 0x71);			snd_es1688_write(chip, 0xb7, 0xbc);		}	}	snd_es1688_write(chip, 0xb1, (snd_es1688_read(chip, 0xb1) & 0x0f) | 0x50);	snd_es1688_write(chip, 0xb2, (snd_es1688_read(chip, 0xb2) & 0x0f) | 0x50);	snd_es1688_dsp_command(chip, ES1688_DSP_CMD_SPKON);	spin_unlock_irqrestore(&chip->reg_lock, flags);	/* --- */	count = -count;	snd_dma_program(chip->dma8, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);	spin_lock_irqsave(&chip->reg_lock, flags);	snd_es1688_write(chip, 0xa4, (unsigned char) count);	snd_es1688_write(chip, 0xa5, (unsigned char) (count >> 8));	spin_unlock_irqrestore(&chip->reg_lock, flags);	return 0;}static int snd_es1688_playback_trigger(snd_pcm_substream_t * substream,				       int cmd){	es1688_t *chip = snd_pcm_substream_chip(substream);	return snd_es1688_trigger(chip, cmd, 0x05);}static int snd_es1688_capture_prepare(snd_pcm_substream_t * substream){	unsigned long flags;	es1688_t *chip = snd_pcm_substream_chip(substream);	snd_pcm_runtime_t *runtime = substream->runtime;	unsigned int size = snd_pcm_lib_buffer_bytes(substream);	unsigned int count = snd_pcm_lib_period_bytes(substream);	chip->dma_size = size;	spin_lock_irqsave(&chip->reg_lock, flags);	snd_es1688_reset(chip);	snd_es1688_set_rate(chip, substream);	snd_es1688_dsp_command(chip, ES1688_DSP_CMD_SPKOFF);	snd_es1688_write(chip, 0xb8, 0x0e);	/* auto init DMA mode */	snd_es1688_write(chip, 0xa8, (snd_es1688_read(chip, 0xa8) & ~0x03) | (3 - runtime->channels));	snd_es1688_write(chip, 0xb9, 2);	/* demand mode (4 bytes/request) */	if (runtime->channels == 1) {		if (snd_pcm_format_width(runtime->format) == 8) {			/* 8. bit mono */			snd_es1688_write(chip, 0xb7, 0x51);			snd_es1688_write(chip, 0xb7, 0xd0);		} else {			/* 16. bit mono */			snd_es1688_write(chip, 0xb7, 0x71);			snd_es1688_write(chip, 0xb7, 0xf4);		}	} else {		if (snd_pcm_format_width(runtime->format) == 8) {			/* 8. bit stereo */			snd_es1688_write(chip, 0xb7, 0x51);			snd_es1688_write(chip, 0xb7, 0x98);		} else {			/* 16. bit stereo */			snd_es1688_write(chip, 0xb7, 0x71);			snd_es1688_write(chip, 0xb7, 0xbc);		}	}	snd_es1688_write(chip, 0xb1, (snd_es1688_read(chip, 0xb1) & 0x0f) | 0x50);	snd_es1688_write(chip, 0xb2, (snd_es1688_read(chip, 0xb2) & 0x0f) | 0x50);	spin_unlock_irqrestore(&chip->reg_lock, flags);	/* --- */	count = -count;	snd_dma_program(chip->dma8, runtime->dma_addr, size, DMA_MODE_READ | DMA_AUTOINIT);	spin_lock_irqsave(&chip->reg_lock, flags);	snd_es1688_write(chip, 0xa4, (unsigned char) count);	snd_es1688_write(chip, 0xa5, (unsigned char) (count >> 8));	spin_unlock_irqrestore(&chip->reg_lock, flags);	return 0;}static int snd_es1688_capture_trigger(snd_pcm_substream_t * substream,				      int cmd){	es1688_t *chip = snd_pcm_substream_chip(substream);	return snd_es1688_trigger(chip, cmd, 0x0f);}void snd_es1688_interrupt(int irq, void *dev_id, struct pt_regs *regs){	es1688_t *chip = snd_magic_cast(es1688_t, dev_id, return);	if (chip->trigger_value == 0x05)	/* ok.. playback is active */		snd_pcm_period_elapsed(chip->playback_substream);	if (chip->trigger_value == 0x0f)	/* ok.. capture is active */		snd_pcm_period_elapsed(chip->capture_substream);	inb(ES1688P(chip, DATA_AVAIL));	/* ack interrupt */}static snd_pcm_uframes_t snd_es1688_playback_pointer(snd_pcm_substream_t * substream){	es1688_t *chip = snd_pcm_substream_chip(substream);	size_t ptr;		if (chip->trigger_value != 0x05)		return 0;	ptr = chip->dma_size - snd_dma_residue(chip->dma8);	return bytes_to_frames(substream->runtime, ptr);}static snd_pcm_uframes_t snd_es1688_capture_pointer(snd_pcm_substream_t * substream){	es1688_t *chip = snd_pcm_substream_chip(substream);	size_t ptr;		if (chip->trigger_value != 0x0f)		return 0;	ptr = chip->dma_size - snd_dma_residue(chip->dma8);	return bytes_to_frames(substream->runtime, ptr);}/* */static snd_pcm_hardware_t snd_es1688_playback ={	info:			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |				 SNDRV_PCM_INFO_MMAP_VALID),	formats:		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,	rates:			SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,	rate_min:		4000,	rate_max:		48000,	channels_min:		1,	channels_max:		2,	buffer_bytes_max:	65536,

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -