/*
 *  Copyright (c) by Jaroslav Kysela <perex@jcu.cz>
 *  Routines for control of AD1848/AD1847/CS4248
 *
 *
 *   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., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include "driver.h"
#include "dma.h"
#include "ad1848.h"
#include "ulaw.h"

#if 0
#define SND_DEBUG_MCE
#endif

/*
 *  Some variables
 */

static struct snd_stru_ad1848_freq snd_ad1848_freq[14] =
{
	{5, 5510, 0x00 | AD1848_XTAL2},
	{6, 6620, 0x0E | AD1848_XTAL2},
	{8, 8000, 0x00 | AD1848_XTAL1},
	{9, 9600, 0x0E | AD1848_XTAL1},
	{11, 11025, 0x02 | AD1848_XTAL2},
	{16, 16000, 0x02 | AD1848_XTAL1},
	{18, 18900, 0x04 | AD1848_XTAL2},
	{22, 22050, 0x06 | AD1848_XTAL2},
	{27, 27042, 0x04 | AD1848_XTAL1},
	{32, 32000, 0x06 | AD1848_XTAL1},
	{33, 33075, 0x0C | AD1848_XTAL2},
	{37, 37800, 0x08 | AD1848_XTAL2},
	{44, 44100, 0x0A | AD1848_XTAL2},
	{48, 48000, 0x0C | AD1848_XTAL1}
};

static struct snd_stru_ad1848_image snd_ad1848_original_image =
{
	0x00,			/* 00 - lic */
	0x00,			/* 01 - ric */
	0x80,			/* 02 - la1ic */
	0x80,			/* 03 - ra1ic */
	0x80,			/* 04 - la2ic */
	0x80,			/* 05 - ra2ic */
	0x80,			/* 06 - loc */
	0x80,			/* 07 - roc */
	0x20,			/* 08 - dfr */
	AD1848_AUTOCALIB,	/* 09 - ic */
	0x00,			/* 0a - pc */
	0x00,			/* 0b - ti */
	0x00,			/* 0c - mi */
	0x00,			/* 0d - lbc */
	0x00,			/* 0e - dru */
	0x00,			/* 0f - drl */
};

/*
 *  Basic I/O functions
 */

static void snd_ad1848_outm(ad1848_t * codec, unsigned char reg,
			    unsigned char mask, unsigned char value)
{
	int timeout;
	unsigned char tmp;

	for (timeout = 250; timeout > 0 && (inb(AD1848P(codec, REGSEL)) & AD1848_INIT); timeout--)
		snd_delay(1);
#ifdef SNDCFG_DEBUG
	if (inb(AD1848P(codec, REGSEL)) & AD1848_INIT)
		snd_printk("snd_ad1848_outm: auto calibration time out - reg = 0x%x, value = 0x%x\n", reg, value);
#endif
	outb(codec->mce_bit | reg, AD1848P(codec, REGSEL));
	tmp = (inb(AD1848P(codec, REG)) & mask) | value;
	outb(tmp, AD1848P(codec, REG));
}

static void snd_ad1848_out(ad1848_t * codec,
			   unsigned char reg,
			   unsigned char value)
{
	int timeout;

	for (timeout = 250; timeout > 0 && (inb(AD1848P(codec, REGSEL)) & AD1848_INIT); timeout--)
		snd_delay(1);
#ifdef SNDCFG_DEBUG
	if (inb(AD1848P(codec, REGSEL)) & AD1848_INIT)
		snd_printk("snd_ad1848_out: auto calibration time out - reg = 0x%x, value = 0x%x\n", reg, value);
#endif
	outb(codec->mce_bit | reg, AD1848P(codec, REGSEL));
	outb(value, AD1848P(codec, REG));
#if 0
	snd_printk("codec out - reg 0x%x = 0x%x\n", codec->mce_bit | reg, value);
#endif
}

static unsigned char snd_ad1848_in(ad1848_t * codec, unsigned char reg)
{
	int timeout;

	for (timeout = 250; timeout > 0 && (inb(AD1848P(codec, REGSEL)) & AD1848_INIT); timeout--)
		snd_delay(1);
#ifdef SNDCFG_DEBUG
	if (inb(AD1848P(codec, REGSEL)) & AD1848_INIT)
		snd_printk("snd_ad1848_in: auto calibration time out - reg = 0x%x\n", reg);
#endif
	outb(codec->mce_bit | reg, AD1848P(codec, REGSEL));
	return inb(AD1848P(codec, REG));
}

#ifdef SNDCFG_DEBUG

void snd_ad1848_debug(ad1848_t * codec)
{
	snd_printk("AD1848 REGS:      INDEX = 0x%02x  ", inb(AD1848P(codec, REGSEL)));
	snd_printk("                 STATUS = 0x%02x\n", inb(AD1848P(codec, STATUS)));
	snd_printk("  0x00: left input      = 0x%02x  ", snd_ad1848_in(codec, 0x00));
	snd_printk("  0x08: playback format = 0x%02x\n", snd_ad1848_in(codec, 0x08));
	snd_printk("  0x01: right input     = 0x%02x  ", snd_ad1848_in(codec, 0x01));
	snd_printk("  0x09: iface (CFIG 1)  = 0x%02x\n", snd_ad1848_in(codec, 0x09));
	snd_printk("  0x02: AUXA left       = 0x%02x  ", snd_ad1848_in(codec, 0x02));
	snd_printk("  0x0a: pin control     = 0x%02x\n", snd_ad1848_in(codec, 0x0a));
	snd_printk("  0x03: AUXA right      = 0x%02x  ", snd_ad1848_in(codec, 0x03));
	snd_printk("  0x0b: init & status   = 0x%02x\n", snd_ad1848_in(codec, 0x0b));
	snd_printk("  0x04: AUXB left       = 0x%02x  ", snd_ad1848_in(codec, 0x04));
	snd_printk("  0x0c: revision & mode = 0x%02x\n", snd_ad1848_in(codec, 0x0c));
	snd_printk("  0x05: AUXB right      = 0x%02x  ", snd_ad1848_in(codec, 0x05));
	snd_printk("  0x0d: loopback        = 0x%02x\n", snd_ad1848_in(codec, 0x0d));
	snd_printk("  0x06: left output     = 0x%02x  ", snd_ad1848_in(codec, 0x06));
	snd_printk("  0x0e: data upr count  = 0x%02x\n", snd_ad1848_in(codec, 0x0e));
	snd_printk("  0x07: right output    = 0x%02x  ", snd_ad1848_in(codec, 0x07));
	snd_printk("  0x0f: data lwr count  = 0x%02x\n", snd_ad1848_in(codec, 0x0f));
}

#endif

/*
 *  AD1848 detection / MCE routines
 */

static void snd_ad1848_mce_up(ad1848_t * codec)
{
	int timeout;

	for (timeout = 250; timeout > 0 && (inb(AD1848P(codec, REGSEL)) & AD1848_INIT); timeout--)
		snd_delay(1);
#ifdef SNDCFG_DEBUG
	if (inb(AD1848P(codec, REGSEL)) & AD1848_INIT)
		snd_printk("ad1848_mce_up - auto calibration time out (0)\n");
#endif
	snd_mutex_lock(codec, reg);
	codec->mce_bit |= AD1848_MCE;
	timeout = inb(AD1848P(codec, REGSEL));
	if (timeout == 0x80)
		snd_printk("snd_ad1848_up [0x%x]: serious init problem - codec still busy\n", codec->port);
	if (!(timeout & AD1848_MCE))
		outb(codec->mce_bit | (timeout & 0x1f), AD1848P(codec, REGSEL));
	snd_mutex_unlock(codec, reg);
}

static void snd_ad1848_mce_down(ad1848_t * codec)
{
	int timeout;
	signed long time;

	snd_mutex_lock(codec, reg);
	for (timeout = 5; timeout > 0; timeout--)
		inb(AD1848P(codec, REGSEL));
	/* end of cleanup sequence */
	for (timeout = 12000; timeout > 0 && (inb(AD1848P(codec, REGSEL)) & AD1848_INIT); timeout--)
		snd_delay(10);
#if 0
	snd_printk("(1) timeout = %i\n", timeout);
#endif
#ifdef SNDCFG_DEBUG
	if (inb(AD1848P(codec, REGSEL)) & AD1848_INIT)
		snd_printk("ad1848_mce_down [0x%x] - auto calibration time out (0)\n", AD1848P(codec, REGSEL));
#endif
	codec->mce_bit &= ~AD1848_MCE;
	timeout = inb(AD1848P(codec, REGSEL));
	outb(codec->mce_bit | (timeout & 0x1f), AD1848P(codec, REGSEL));
	if (timeout == 0x80)
		snd_printk("snd_ad1848_down [0x%x]: serious init problem - codec still busy\n", codec->port);
	if ((timeout & AD1848_MCE) == 0) {
		snd_mutex_unlock(codec, reg);
		return;
	}
	/* calibration process */

	for (timeout = 500; timeout > 0 && (snd_ad1848_in(codec, AD1848_TEST_INIT) & AD1848_CALIB_IN_PROGRESS) == 0; timeout--)
		snd_delay(10);

	if ((snd_ad1848_in(codec, AD1848_TEST_INIT) & AD1848_CALIB_IN_PROGRESS) == 0) {
		snd_printd("ad1848_mce_down - auto calibration time out (1)\n");
		snd_mutex_unlock(codec, reg);
		return;
	}
#if 0
	snd_printk("(2) timeout = %i, jiffies = %li\n", timeout, jiffies);
#endif
	time = 100;
	while (snd_ad1848_in(codec, AD1848_TEST_INIT) & AD1848_CALIB_IN_PROGRESS) {
		snd_mutex_unlock(codec, reg);
		if (time <= 0) {
			snd_printk("ad1848_mce_down - auto calibration time out (2)\n");
			return;
		}
		snd_delay(10);
		time--;
		snd_mutex_lock(codec, reg);
	}
#if 0
	snd_printk("(3) jiffies = %li\n", jiffies);
#endif
	time = 100;
	while (inb(AD1848P(codec, REGSEL)) & AD1848_INIT) {
		snd_mutex_unlock(codec, reg);
		if (time <= 0) {
			snd_printk("ad1848_mce_down - auto calibration time out (3)\n");
			return;
		}
		snd_delay(10);
		time--;
		snd_mutex_lock(codec, reg);
	}
	snd_mutex_unlock(codec, reg);
#if 0
	snd_printk("(4) jiffies = %li\n", jiffies);
	snd_printk("ad1848_mce_down - exit = 0x%x\n", inb(AD1848P(codec, REGSEL)));
#endif
}

static unsigned int snd_ad1848_get_count(unsigned char format,
				         unsigned int size)
{
	switch (format & 0xe0) {
	case AD1848_LINEAR_16:
		size >>= 1;
		break;
	}
	if (format & AD1848_STEREO)
		size >>= 1;
	return size;
}

static void snd_ad1848_trigger(ad1848_t * codec,
			       unsigned char what, int enable)
{
#if 0
	snd_printk("codec trigger!!! - what = %i, enable = %i, status = 0x%x\n", what, enable, inb(AD1848P(card, STATUS)));
#endif
	snd_mutex_lock(codec, reg);
	if (enable) {
		if (codec->image.ic & what) {
			snd_mutex_unlock(codec, reg);
			return;
		}
		snd_ad1848_out(codec, AD1848_IFACE_CTRL, codec->image.ic |= what);
	} else {
		if (!(codec->image.ic & what)) {
			snd_mutex_unlock(codec, reg);
			return;
		}
		snd_ad1848_out(codec, AD1848_IFACE_CTRL, codec->image.ic &= ~what);
	}
	snd_mutex_unlock(codec, reg);
#if 0
	snd_ad1848_debug(codec);
	snd_delay(100);
	snd_printk("residue = 0x%x\n", get_dma_residue(codec->dma));
#endif
}

/*
 *  CODEC I/O
 */

static unsigned char snd_ad1848_get_freq(unsigned int freq)
{				/* freq in Hz */
	int i;

	freq /= 1000;
#if 0
	snd_printk("pcm_rate: %d\n", freq);
#endif
	if (freq > 48)
		freq = 48;
	for (i = 0; i < 14; i++)
		if (freq <= snd_ad1848_freq[i].hertz)
			return snd_ad1848_freq[i].bits;
	return snd_ad1848_freq[13].bits;
}

static unsigned int snd_ad1848_get_real_freq(unsigned int freq)
{				/* freq in Hz */
	int i;

	freq /= 1000;
#if 0
	snd_printk("pcm_rate: %d\n", freq);
#endif
	if (freq > 48)
		freq = 48;
	for (i = 0; i < 14; i++)
		if (freq <= snd_ad1848_freq[i].hertz)
			return snd_ad1848_freq[i].rate;
	return snd_ad1848_freq[13].rate;
}

static int snd_ad1848_playback_ioctl(snd_pcm1_t * pcm1, unsigned int cmd, unsigned long *arg)
{
	switch (cmd) {
	case SND_PCM1_IOCTL_RATE:
		pcm1->playback.real_rate = snd_ad1848_get_real_freq(pcm1->playback.rate);
		return 0;
	}
	return -ENXIO;
}

static int snd_ad1848_record_ioctl(snd_pcm1_t * pcm1, unsigned int cmd, unsigned long *arg)
{
	switch (cmd) {
	case SND_PCM1_IOCTL_RATE:
		pcm1->record.real_rate = snd_ad1848_get_real_freq(pcm1->record.rate);
		return 0;
	}
	return -ENXIO;
}

static unsigned char snd_ad1848_get_format(unsigned int mode, int voices)
{
	unsigned char format;

	format = 0;
	if (mode & SND_PCM1_MODE_16) {
		format |= AD1848_LINEAR_16;
	} else {
		if (mode & SND_PCM1_MODE_ALAW)
			format |= AD1848_ALAW_8;
		else if (mode & SND_PCM1_MODE_ULAW)
			format |= AD1848_ULAW_8;
#if 0
		else
			format |= AD1848_LINEAR_8;	/* I know, OR with 0 */
#endif
	}
	if (voices > 1)
		format |= AD1848_STEREO;
#if 0
	snd_printk("snd_ad1848_get_format: 0x%x (mode=0x%x)\n", format, mode);
#endif
	return format;
}

static void snd_ad1848_set_data_format(ad1848_t * codec, int direction)
{
	snd_pcm1_t *pcm1;
	snd_pcm1_channel_t *pchn;

	pcm1 = (snd_pcm1_t *) codec->pcm->private_data;
	pchn = direction == SND_PCM1_PLAYBACK ? &pcm1->playback : &pcm1->record;
	codec->image.dfr =
	    snd_ad1848_get_format(pchn->mode, pchn->voices) |
	    snd_ad1848_get_freq(pchn->real_rate);
#if 0
	snd_printk(">>> pmode = 0x%x, dfr = 0x%x\n", pchn->mode, codec->image.dfr);
#endif
}

static int snd_ad1848_open(ad1848_t * codec, unsigned int mode)
{
	snd_mutex_lock(codec, open);
	if (codec->mode & AD1848_MODE_OPEN) {
		snd_mutex_unlock(codec, open);
		return -1;
	}
	snd_ad1848_mce_down(codec);

#ifdef SND_DEBUG_MCE
	snd_printk("snd_ad1848_open: (1)\n");
#endif
	snd_ad1848_mce_up(codec);
	snd_mutex_lock(codec, reg);
	codec->image.ic &= ~(AD1848_PLAYBACK_ENABLE | AD1848_PLAYBACK_PIO |
			     AD1848_RECORD_ENABLE | AD1848_RECORD_PIO |
			     AD1848_CALIB_MODE);
	codec->image.ic |= AD1848_AUTOCALIB;
	snd_ad1848_out(codec, AD1848_IFACE_CTRL, codec->image.ic);
	snd_mutex_unlock(codec, reg);
	snd_ad1848_mce_down(codec);

#ifdef SND_DEBUG_MCE
	snd_printk("snd_ad1848_open: (2)\n");
#endif

	snd_ad1848_set_data_format(codec, SND_PCM1_PLAYBACK);

	snd_ad1848_mce_up(codec);
	snd_mutex_lock(codec, reg);
	snd_ad1848_out(codec, AD1848_DATA_FORMAT, codec->image.dfr);
	snd_mutex_unlock(codec, reg);
	snd_ad1848_mce_down(codec);

#ifdef SND_DEBUG_MCE
	snd_printk("snd_ad1848_open: (3)\n");
#endif

	/* ok. now enable and ack CODEC IRQ */
	snd_mutex_lock(codec, reg);
	outb(0, AD1848P(codec, STATUS));	/* clear IRQ */
	outb(0, AD1848P(codec, STATUS));	/* clear IRQ */
	codec->image.pc |= AD1848_IRQ_ENABLE;
	snd_ad1848_out(codec, AD1848_PIN_CTRL, codec->image.pc);
	snd_mutex_unlock(codec, reg);

	codec->mode = mode;
	snd_mutex_unlock(codec, open);

	return 0;
}

static void snd_ad1848_close(ad1848_t * codec)
{
	snd_mutex_lock(codec, open);
	if (!codec->mode) {
		snd_mutex_unlock(codec, open);
		return;
	}
	/* disable IRQ */
	snd_mutex_lock(codec, reg);
	outb(0, AD1848P(codec, STATUS));	/* clear IRQ */
	outb(0, AD1848P(codec, STATUS));	/* clear IRQ */
	codec->image.pc &= ~AD1848_IRQ_ENABLE;
	snd_ad1848_out(codec, AD1848_PIN_CTRL, codec->image.pc);
	snd_mutex_unlock(codec, reg);

	/* now disable record & playback */

	snd_ad1848_mce_up(codec);
	snd_mutex_lock(codec, reg);
	codec->image.ic &= ~(AD1848_PLAYBACK_ENABLE | AD1848_PLAYBACK_PIO |
			     AD1848_RECORD_ENABLE | AD1848_RECORD_PIO);
	snd_ad1848_out(codec, AD1848_IFACE_CTRL, codec->image.ic);
	snd_mutex_unlock(codec, reg);
	snd_ad1848_mce_down(codec);

	/* clear IRQ again */
	snd_mutex_lock(codec, reg);
	outb(0, AD1848P(codec, STATUS));	/* clear IRQ */
	outb(0, AD1848P(codec, STATUS));	/* clear IRQ */
	snd_mutex_unlock(codec, reg);

	codec->mode = 0;
	snd_mutex_unlock(codec, open);
}

/*
 *  ok.. exported functions..
 */

static void snd_ad1848_playback_trigger(snd_pcm1_t * pcm1, int up)
{
	ad1848_t *codec;

	codec = (ad1848_t *) pcm1->private_data;

	if (codec->mixer && !up)
		snd_mixer_set_kernel_mute(codec->mixer, SND_MIXER_PRI_PCM, SND_MIX_MUTE, 0);

	snd_ad1848_trigger(codec, AD1848_PLAYBACK_ENABLE, up);

	if (codec->mixer && up)
		snd_mixer_set_kernel_mute(codec->mixer, SND_MIXER_PRI_PCM, 0, 0);

#if 0
	if (up)
		snd_ad1848_debug(codec);
#endif
}

static void snd_ad1848_record_trigger(snd_pcm1_t * pcm1, int up)
{
	ad1848_t *codec;

	codec = (ad1848_t *) pcm1->private_data;
	snd_ad1848_trigger(codec, AD1848_RECORD_ENABLE, up);
}

static void snd_ad1848_playback_prepare(snd_pcm1_t * pcm1,
					unsigned char *buffer,
					unsigned int size,
					unsigned int offset,
					unsigned int count)
{
	ad1848_t *codec;

	codec = (ad1848_t *) pcm1->private_data;
	/* Note: offset is always 0 if AUTO DMA */
	snd_ad1848_set_data_format(codec, SND_PCM1_PLAYBACK);
#if 0
	snd_printk("ad1848_start_playback - dma = %i, buffer = 0x%lx, size = 0x%x, offset = 0x%x, count = 0x%x\n", codec->dma1, (long) buffer, size, offset, count);
#endif
	codec->image.ic &= ~(AD1848_PLAYBACK_ENABLE | AD1848_PLAYBACK_PIO);
	snd_dma_program(codec->dma, buffer, size, DMA_MODE_WRITE | DMA_MODE_AUTOINIT);
	count = snd_ad1848_get_count(codec->image.dfr, count) - 1;
	snd_ad1848_mce_up(codec);
	snd_mutex_lock(codec, reg);
	snd_ad1848_out(codec, AD1848_DATA_FORMAT, codec->image.dfr);
#if 0
	snd_printk("ic = 0x%x, REC_FORMAT = 0x%x (0x%x), PLAYBK_FORMAT = 0x%x (0x%x)\n", codec->image.ic, snd_ad1848_in(codec, AD1848_REC_FORMAT), codec->image.cdfr, snd_ad1848_in(codec, AD1848_PLAYBK_FORMAT), codec->image.pdfr);
#endif
	snd_mutex_unlock(codec, reg);
	snd_ad1848_mce_down(codec);
	snd_mutex_lock(codec, reg);
	snd_ad1848_out(codec, AD1848_DATA_LWR_CNT, (unsigned char) count);
	snd_ad1848_out(codec, AD1848_DATA_UPR_CNT, (unsigned char) (count >> 8));
	snd_mutex_unlock(codec, reg);
#if 0
	snd_ad1848_debug(codec);
#endif
}

static void snd_ad1848_record_prepare(snd_pcm1_t * pcm1,
				      unsigned char *buffer,
				      unsigned int size,
				      unsigned int offset,
				      unsigned int count)
{
	ad1848_t *codec;

	codec = (ad1848_t *) pcm1->private_data;
	snd_ad1848_set_data_format(codec, SND_PCM1_RECORD);
#if 0
	snd_printk("ad1848_start_record: start - buffer = 0x%lx, offset = 0x%x, size = 0x%x, count = 0x%x\n", (long) buffer, offset, size, count);
#endif
	codec->image.ic &= ~(AD1848_RECORD_ENABLE | AD1848_RECORD_PIO);
	snd_dma_program(codec->dma, buffer, size, DMA_MODE_READ | DMA_MODE_AUTOINIT);
	count = snd_ad1848_get_count(codec->image.dfr, count) - 1;
	snd_ad1848_mce_up(codec);
	snd_mutex_lock(codec, reg);
	snd_ad1848_out(codec, AD1848_DATA_FORMAT, codec->image.dfr);
#if 0
	snd_printk("ic = 0x%x, REC_FORMAT = 0x%x (0x%x), PLAYBK_FORMAT = 0x%x (0x%x)\n", codec->image.ic, snd_ad1848_in(codec, AD1848_REC_FORMAT), codec->image.cdfr, snd_ad1848_in(codec, AD1848_PLAYBK_FORMAT), codec->image.pdfr);
#endif
	snd_mutex_unlock(codec, reg);
	snd_ad1848_mce_down(codec);
#if 0
	snd_printk("cdfr = 0x%x, rport = 0x%x, pport = 0x%x, status = 0x%x\n", codec->image.cdfr, snd_ad1848_in(codec, AD1848_REC_FORMAT), snd_ad1848_in(codec, AD1848_PLAYBK_FORMAT), inb(AD1848P(codec, REGSEL)));
#endif
	snd_mutex_lock(codec, reg);
	snd_ad1848_out(codec, AD1848_DATA_LWR_CNT, (unsigned char) count);
	snd_ad1848_out(codec, AD1848_DATA_UPR_CNT, (unsigned char) (count >> 8));
	snd_mutex_unlock(codec, reg);
}

void snd_ad1848_interrupt(snd_pcm_t * pcm, unsigned char status)
{
	ad1848_t *codec;
	snd_pcm1_t *pcm1;

#if 0
	snd_printk("snd_ad1848_interrupt: status=0x%x\n", status);
#endif
	pcm1 = (snd_pcm1_t *) pcm->private_data;
	codec = pcm1->private_data;
	if (!codec)
		return;

	if ((codec->mode & AD1848_MODE_PLAY) && pcm1->playback.ack)
		pcm1->playback.ack(pcm1);
	if ((codec->mode & AD1848_MODE_RECORD) && pcm1->record.ack)
		pcm1->record.ack(pcm1);

	snd_mutex_lock(codec, reg);
	outb(0, AD1848P(codec, STATUS));	/* clear global interrupt bit */
	snd_mutex_unlock(codec, reg);
}

/*

 */

static int snd_ad1848_playback_open(snd_pcm1_t * pcm1)
{
	snd_card_t *card;
	ad1848_t *codec;
	int err;

	card = pcm1->card;
	codec = (ad1848_t *) pcm1->private_data;
	if ((err = snd_pcm1_dma_alloc(pcm1, SND_PCM1_PLAYBACK, codec->dmaptr, "AD1848 (playback)")) < 0)
		return err;
	if (snd_ad1848_open(codec, AD1848_MODE_PLAY) < 0) {
		snd_pcm1_dma_free(pcm1, SND_PCM1_PLAYBACK, codec->dmaptr);
		return -EBUSY;
	}
	return 0;
}

static int snd_ad1848_record_open(snd_pcm1_t * pcm1)
{
	snd_card_t *card;
	ad1848_t *codec;
	int err;

	card = pcm1->card;
	codec = (ad1848_t *) pcm1->private_data;
	if ((err = snd_pcm1_dma_alloc(pcm1, SND_PCM1_RECORD, codec->dmaptr, "AD1848 (record)")) < 0)
		return err;
	if (snd_ad1848_open(codec, AD1848_MODE_RECORD) < 0) {
		snd_pcm1_dma_free(pcm1, SND_PCM1_RECORD, codec->dmaptr);
		return -EBUSY;
	}
	return 0;
}

static void snd_ad1848_playback_close(snd_pcm1_t * pcm1)
{
	snd_card_t *card;
	ad1848_t *codec;

	card = pcm1->card;
	codec = (ad1848_t *) pcm1->private_data;
	codec->mode &= ~AD1848_MODE_PLAY;
	snd_ad1848_close(codec);
	snd_pcm1_dma_free(pcm1, SND_PCM1_PLAYBACK, codec->dmaptr);
}

static void snd_ad1848_record_close(snd_pcm1_t * pcm1)
{
	snd_card_t *card;
	ad1848_t *codec;

	card = pcm1->card;
	codec = (ad1848_t *) pcm1->private_data;
	codec->mode &= ~AD1848_MODE_RECORD;
	snd_ad1848_close(codec);
	snd_pcm1_dma_free(pcm1, SND_PCM1_RECORD, codec->dmaptr);
}

static unsigned int snd_ad1848_playback_pointer(snd_pcm1_t * pcm1,
					        unsigned int used_size)
{
	ad1848_t *codec;

	codec = (ad1848_t *) pcm1->private_data;
	if (!(codec->image.ic & AD1848_PLAYBACK_ENABLE))
		return 0;
	return used_size - snd_dma_residue(codec->dma);
}

static unsigned int snd_ad1848_record_pointer(snd_pcm1_t * pcm1,
					      unsigned int used_size)
{
	ad1848_t *codec;

	codec = (ad1848_t *) pcm1->private_data;
	if (!(codec->image.ic & AD1848_RECORD_ENABLE))
		return 0;
	return used_size - snd_dma_residue(codec->dma);
}

/*

 */

static int snd_ad1848_probe(snd_pcm1_t * pcm1)
{
	snd_pcm_t *pcm;
	ad1848_t *codec;
	int i, id, rev, ad1847;
	unsigned char *ptr;

	pcm = pcm1->pcm;
	codec = (ad1848_t *) pcm1->private_data;
	if (!codec)
		return -EINVAL;

#if 0
	snd_ad1848_debug(codec);
#endif
	id = ad1847 = 0;
	for (i = 0; i < 1000; i++) {
		if (inb(AD1848P(codec, REGSEL)) & AD1848_INIT)
			snd_delay(50 * 8);
		else {
			snd_mutex_lock(codec, reg);
			snd_ad1848_out(codec, AD1848_MISC_INFO, 0x00);
			snd_ad1848_out(codec, AD1848_LEFT_INPUT, 0xaa);
			snd_ad1848_out(codec, AD1848_RIGHT_INPUT, 0x45);
			rev = snd_ad1848_in(codec, AD1848_RIGHT_INPUT);
			if (rev == 0x65) {
				id = 1;
				ad1847 = 1;
				break;
			}
			if (snd_ad1848_in(codec, AD1848_LEFT_INPUT) == 0xaa && rev == 0x45) {
				id = 1;
				break;
			}
		}
	}
	if (id != 1)
		return -ENODEV;	/* no valid device found */
	if (codec->hardware == AD1848_HW_DETECT) {
		if (ad1847) {
			codec->hardware = AD1848_HW_AD1847;
			strcpy(pcm->name, "AD1847");
		} else {
			codec->hardware = AD1848_HW_AD1848;
			strcpy(pcm->name, "AD1848");
			rev = snd_ad1848_in(codec, AD1848_MISC_INFO);
			if (rev & 0x80) {
				codec->hardware = AD1848_HW_CS4248;
				strcpy(pcm->name, "CS4248");
			}
		}
	}
	snd_mutex_lock(codec, reg);
	inb(AD1848P(codec, STATUS));	/* clear any pendings IRQ */
	outb(0, AD1848P(codec, STATUS));
	snd_mutex_unlock(codec, reg);

	codec->image.mi = 0x00;
	codec->image.ic =
	    (codec->image.ic & ~AD1848_SINGLE_DMA) | AD1848_SINGLE_DMA;
	ptr = (unsigned char *) &codec->image;
	snd_ad1848_mce_down(codec);
	snd_mutex_lock(codec, reg);
	for (i = 0; i < 16; i++)	/* ok.. fill all AD1848 registers */
		snd_ad1848_out(codec, i, *ptr++);
	snd_mutex_unlock(codec, reg);
	snd_ad1848_mce_up(codec);
	snd_ad1848_mce_down(codec);
	return 0;		/* all things are ok.. */
}

/*

 */

static struct snd_stru_pcm1_hardware snd_ad1848_playback =
{
	NULL,			/* private data */
	NULL,			/* private_free */
	SND_PCM1_HW_AUTODMA,	/* flags */
	SND_PCM_FMT_MU_LAW | SND_PCM_FMT_A_LAW |
	SND_PCM_FMT_U8 | SND_PCM_FMT_S16_LE,	/* formats */
	SND_PCM_FMT_MU_LAW | SND_PCM_FMT_A_LAW |
	SND_PCM_FMT_U8 | SND_PCM_FMT_S16_LE,	/* hardware formats */
	0,			/* align value */
	6,			/* minimal fragment */
	5510,			/* min. rate */
	48000,			/* max. rate */
	2,			/* max. voices */
	snd_ad1848_playback_open,
	snd_ad1848_playback_close,
	snd_ad1848_playback_ioctl,
	snd_ad1848_playback_prepare,
	snd_ad1848_playback_trigger,
	snd_ad1848_playback_pointer,
	snd_pcm1_playback_dma,
	snd_pcm1_dma_move,
	snd_pcm1_playback_dma_neutral
};

static struct snd_stru_pcm1_hardware snd_ad1848_record =
{
	NULL,			/* private data */
	NULL,			/* private free */
	SND_PCM1_HW_AUTODMA,	/* flags */
	SND_PCM_FMT_MU_LAW | SND_PCM_FMT_A_LAW |
	SND_PCM_FMT_U8 | SND_PCM_FMT_S16_LE,	/* formats */
	SND_PCM_FMT_MU_LAW | SND_PCM_FMT_A_LAW |
	SND_PCM_FMT_U8 | SND_PCM_FMT_S16_LE,	/* hardware formats */
	0,			/* align value */
	6,			/* minimal fragment */
	5510,			/* min. rate */
	48000,			/* max. rate */
	2,			/* max. voices */
	snd_ad1848_record_open,
	snd_ad1848_record_close,
	snd_ad1848_record_ioctl,
	snd_ad1848_record_prepare,
	snd_ad1848_record_trigger,
	snd_ad1848_record_pointer,
	snd_pcm1_record_dma,
	snd_pcm1_dma_move,
	NULL
};

static void snd_ad1848_free(void *private_data)
{
	snd_free(private_data, sizeof(ad1848_t));
}

snd_pcm_t *snd_ad1848_new_device(snd_card_t * card,
				 unsigned short port,
				 snd_irq_t * irqptr,
				 snd_dma_t * dmaptr,
				 unsigned short hardware)
{
	snd_pcm_t *pcm;
	snd_pcm1_t *pcm1;
	ad1848_t *codec;
	char *str;

	pcm = snd_pcm1_new_device(card, "AD1848");
	if (!pcm)
		return NULL;
	pcm1 = (snd_pcm1_t *) pcm->private_data;
	codec = (ad1848_t *) snd_calloc(sizeof(ad1848_t));
	if (!codec)
		return NULL;
	snd_mutex_prepare(codec, reg);
	snd_mutex_prepare(codec, open);
	codec->pcm = pcm;
	codec->card = pcm1->card;
	codec->port = port;
	codec->irq = irqptr->irq;
	codec->irqptr = irqptr;
	codec->dmaptr = dmaptr;
	codec->dma = dmaptr->dma;
	codec->hardware = hardware;
	memcpy(&codec->image, &snd_ad1848_original_image, sizeof(snd_ad1848_original_image));
	memcpy(&pcm1->playback.hw, &snd_ad1848_playback, sizeof(snd_ad1848_playback));
	memcpy(&pcm1->record.hw, &snd_ad1848_record, sizeof(snd_ad1848_record));
	pcm1->private_data = codec;
	pcm1->private_free = snd_ad1848_free;
	pcm->info_flags = SND_PCM_INFO_CODEC | SND_PCM_INFO_MMAP |
	    SND_PCM_INFO_PLAYBACK | SND_PCM_INFO_RECORD;
	switch (codec->hardware) {
	case AD1848_HW_AD1847:
		str = "AD1847";
		break;
	case AD1848_HW_AD1848:
		str = "AD1848";
		break;
	case AD1848_HW_CS4248:
		str = "CS4248";
		break;
	default:
		str = "???";
	}
	strcpy(pcm->name, str);
	if (snd_ad1848_probe(pcm1) < 0) {
		snd_pcm_free(pcm);
		return NULL;
	}
	return pcm;
}

/*
 *  MIXER part
 */

/*
 *    private_value
 *              0x000000ff - mute mask
 *              0xff000000 - left register (or mono register)
 *              0x00ff0000 - right register
 *              0x00000f00 - shift (from right)
 *              0x00001000 - invert mute
 *              0x00002000 - invert value
 */

static int snd_ad1848_input_mute(snd_kmixer_t * mixer,
				 snd_kmixer_channel_t * channel,
				 unsigned int mute)
{
	unsigned char mixs = AD1848_MIXS_LINE, mask = 0;
	ad1848_t *codec;

	switch (channel->hw.priority) {
	case SND_MIXER_PRI_AUXA:
		mask = 1;
		break;
	case SND_MIXER_PRI_MIC:
		mask = 2;
		break;
	case SND_MIXER_PRI_LINE:
		mask = 4;
		break;
	default:
		mask = 8;
		break;		/* master */
	}
	if ((mute & SND_MIX_MUTE) != SND_MIX_MUTE)
		mixer->private_value |= mask;
	else
		mixer->private_value &= ~mask;

	if (mixer->private_value == 0)
		mixs = AD1848_MIXS_LINE;
	else if (mixer->private_value & 8)
		mixs = AD1848_MIXS_ALL;
	else if (mixer->private_value & 2)
		mixs = AD1848_MIXS_MIC;
	else if (mixer->private_value & 4)
		mixs = AD1848_MIXS_LINE;
	else
		mixs = AD1848_MIXS_AUX1;

	codec = (ad1848_t *) mixer->private_data;
	snd_mutex_lock(codec, reg);
	snd_ad1848_outm(codec, AD1848_LEFT_INPUT, 0x3f, mixs);
	snd_ad1848_outm(codec, AD1848_RIGHT_INPUT, 0x3f, mixs);
	snd_mutex_unlock(codec, reg);
	return 0;
}

static int snd_ad1848_output_mute(snd_kmixer_t * mixer,
				  snd_kmixer_channel_t * channel,
				  unsigned int mute)
{
	unsigned char regl, regr, mask;
	ad1848_t *codec;

	regl = (unsigned char) (channel->hw.private_value >> 24);
	regr = (unsigned char) (channel->hw.private_value >> 16);
	mask = (unsigned char) (channel->hw.private_value);
	if (channel->hw.private_value & 0x1000)
		mute ^= SND_MIX_MUTE;
#if 0
	snd_printk("mute: regl = 0x%x(0x%x), regr = 0x%x(0x%x), mask = 0x%x, mute = 0x%x\n", regl, snd_ad1848_in(mixer->private_data, regl), regr, snd_ad1848_in(mixer->private_data, regr), mask, mute);
#endif
	codec = (ad1848_t *) mixer->private_data;
	snd_mutex_lock(codec, reg);
	snd_ad1848_outm(codec, regl, ~mask, (mute & SND_MIX_MUTE_LEFT) ? mask : 0);
	if (channel->hw.ocaps & SND_MIXER_CINFO_DCAP_STEREO)
		snd_ad1848_outm(codec, regr, ~mask, (mute & SND_MIX_MUTE_RIGHT) ? mask : 0);
	snd_mutex_unlock(codec, reg);
	return 0;
}

static int snd_ad1848_control(snd_kmixer_t * mixer,
			      snd_kmixer_channel_t * channel,
			      unsigned int ctrl, long val)
{
	switch (ctrl) {
	case SND_MIX_CTRL_MUTE_OUT:
		return snd_ad1848_output_mute(mixer, channel, val);
	case SND_MIX_CTRL_MUTE_IN:
		return snd_ad1848_input_mute(mixer, channel, val);
	}
	return -EINVAL;
}


static int snd_ad1848_volume_level(snd_kmixer_t * mixer,
				   snd_kmixer_channel_t * channel,
				   int left, int right)
{
	unsigned char regl, regr, shift, mask;
	ad1848_t *codec;

	regl = (unsigned char) (channel->hw.private_value >> 24);
	regr = (unsigned char) (channel->hw.private_value >> 16);
	shift = (channel->hw.private_value >> 8) & 0x0f;
	mask = ~(unsigned char) (channel->hw.omax << shift);
#if 0
	snd_printk("volume: regl = 0x%x(0x%x), regr = 0x%x(0x%x), mask = 0x%x, shift = %i, left = %i, right = %i\n", regl, snd_ad1848_in(mixer->private_data, regl), regr, snd_ad1848_in(mixer->private_data, regr), mask, shift, left, right);
#endif
	if (!(channel->hw.private_value & 0x2000)) {
		left = channel->hw.omax - left;
		right = channel->hw.omax - right;
	}
	codec = (ad1848_t *) mixer->private_data;
	snd_mutex_lock(codec, reg);
	snd_ad1848_outm(codec, regl, mask, left << shift);
	if (channel->hw.ocaps & SND_MIXER_CINFO_DCAP_STEREO)
		snd_ad1848_outm(codec, regr, mask, right << shift);
	snd_mutex_unlock(codec, reg);
	return 0;
}

static int snd_ad1848_volume(snd_kmixer_t * mixer,
			     snd_kmixer_channel_t * channel,
			     unsigned int type, int left, int right)
{
	switch (type) {
	case SND_MIX_VOL_OUTPUT:
		return snd_ad1848_volume_level(mixer, channel, left, right);
	}
	return -EINVAL;
}


#define AD1848_MIXS \
	  (sizeof(snd_ad1848_mixs)/sizeof(struct snd_stru_mixer_channel_hw))
#define AD1848_PRIVATE( left, right, shift, mute ) \
	  ((left << 24)|(right << 16)|(shift<<8)|mute)

static struct snd_stru_mixer_channel_hw snd_ad1848_mixs[] =
{
	{
		SND_MIXER_PRI_GAIN,	/* priority */
		SND_MIXER_PRI_PARENT,	/* parent priority */
		SND_MIXER_ID_GAIN,	/* device name */
		SND_MIXER_OSS_IMIX,	/* OSS device # */
		/* General capabilities */
		SND_MIXER_CINFO_CAP_OUTPUT,
		/* Output capabilities */
		SND_MIXER_CINFO_DCAP_STEREO | SND_MIXER_CINFO_DCAP_VOLUME,
		/* Input capabilities */
		0,
		0, 15,			/* omin, omax value */
		0, 2250,		/* omin, omax dB */
                0, 0,			/* imin, imax value */
                0, 0,			/* imin, imax dB */
		0,			/* set control */
		snd_ad1848_volume,	/* set volume level*/
		AD1848_PRIVATE(AD1848_LEFT_INPUT, AD1848_RIGHT_INPUT, 0, 0x00) | 0x2000
	},
	{
		SND_MIXER_PRI_AUXA,	/* priority */
		SND_MIXER_PRI_PARENT,	/* parent priority */
		SND_MIXER_ID_AUXA,	/* device name */
		SND_MIXER_OSS_LINE1,	/* OSS device # */
		/* General capabilities */
		SND_MIXER_CINFO_CAP_OUTPUT | SND_MIXER_CINFO_CAP_INPUT |
		SND_MIXER_CINFO_CAP_EXTINPUT,
		/* Output capabilities */
		SND_MIXER_CINFO_DCAP_STEREO | SND_MIXER_CINFO_DCAP_VOLUME |
		SND_MIXER_CINFO_DCAP_HWMUTE,
		/* Input capabilities */
		SND_MIXER_CINFO_DCAP_STEREO | SND_MIXER_CINFO_DCAP_EXCLUSIVE |
		SND_MIXER_CINFO_DCAP_HWMUTE | SND_MIXER_CINFO_DCAP_JOINMUTE,
		0, 31,			/* omin, omax value */
		-3450, 1200,		/* omin, omax dB */
                0, 0,			/* imin, imax value */
                0, 0,			/* imin, imax dB */
		snd_ad1848_control,	/* set control */
		snd_ad1848_volume,	/* set volume level*/
		AD1848_PRIVATE(AD1848_AUX1_LEFT_INPUT, AD1848_AUX1_RIGHT_INPUT, 0, 0x80)
	},
	{
		SND_MIXER_PRI_AUXB,	/* priority */
		SND_MIXER_PRI_PARENT,	/* parent priority */
		SND_MIXER_ID_AUXB,	/* device name */
		SND_MIXER_OSS_LINE2,	/* OSS device # */
		/* General capabilities */
		SND_MIXER_CINFO_CAP_OUTPUT | SND_MIXER_CINFO_CAP_INPUT |
		SND_MIXER_CINFO_CAP_EXTINPUT,
		/* Output capabilities */
		SND_MIXER_CINFO_DCAP_STEREO | SND_MIXER_CINFO_DCAP_VOLUME |
		SND_MIXER_CINFO_DCAP_HWMUTE,
		/* Input capabilities */
		SND_MIXER_CINFO_DCAP_STEREO | SND_MIXER_CINFO_DCAP_EXCLUSIVE |
		SND_MIXER_CINFO_DCAP_HWMUTE | SND_MIXER_CINFO_DCAP_JOINMUTE,
		0, 31,			/* omin, omax value */
		-3450, 1200,		/* omin, omax dB */
                0, 0,			/* imin, imax value */
                0, 0,			/* imin, imax dB */
		snd_ad1848_control,	/* set control */
		snd_ad1848_volume,	/* set volume level*/
		AD1848_PRIVATE(AD1848_AUX2_LEFT_INPUT, AD1848_AUX2_RIGHT_INPUT, 0, 0x80)
	},
	{
		SND_MIXER_PRI_PCM,	/* priority */
		SND_MIXER_PRI_PARENT,	/* parent priority */
		SND_MIXER_ID_PCM,	/* device name */
		SND_MIXER_OSS_PCM,	/* OSS device # */
		/* General capabilities */
		SND_MIXER_CINFO_CAP_OUTPUT,
		/* Output capabilities */
		SND_MIXER_CINFO_DCAP_STEREO | SND_MIXER_CINFO_DCAP_VOLUME |
		SND_MIXER_CINFO_DCAP_HWMUTE,
		/* Input capabilities */
		0,
		0, 63,			/* omin, omax value */
		-9450, 0,		/* omin, omax dB */
                0, 0,			/* imin, imax value */
                0, 0,			/* imin, imax dB */
		snd_ad1848_control,	/* set control */
		snd_ad1848_volume,	/* set volume level*/
		AD1848_PRIVATE(AD1848_LEFT_OUTPUT, AD1848_RIGHT_OUTPUT, 0, 0x80)
	},
	{
		SND_MIXER_PRI_LOOPBACK,	/* priority */
		SND_MIXER_PRI_PARENT,	/* parent priority */
		SND_MIXER_ID_LOOPBACK,	/* device name */
		SND_MIXER_OSS_MONITOR,	/* OSS device # */
		/* General capabilities */
		SND_MIXER_CINFO_CAP_OUTPUT,
		/* Output capabilities */
		SND_MIXER_CINFO_DCAP_VOLUME | SND_MIXER_CINFO_DCAP_HWMUTE,
		/* Input capabilities */
		0,
		0, 63,			/* omin, omax value */
		-9450, 0,		/* omin, omax dB */
                0, 0,			/* imin, imax value */
                0, 0,			/* imin, imax dB */
		snd_ad1848_control,	/* set control */
		snd_ad1848_volume,	/* set volume level*/
		AD1848_PRIVATE(AD1848_LOOPBACK, 0, 0x12, 0x01)
	},
	{
		SND_MIXER_PRI_MIC,	/* priority */
		SND_MIXER_PRI_PARENT,	/* parent priority */
		SND_MIXER_ID_MIC,	/* device name */
		SND_MIXER_OSS_MIC,	/* OSS device # */
		/* General capabilities */
		SND_MIXER_CINFO_CAP_OUTPUT | SND_MIXER_CINFO_CAP_INPUT |
		SND_MIXER_CINFO_CAP_EXTINPUT,
		/* Output capabilities */
		SND_MIXER_CINFO_DCAP_STEREO | SND_MIXER_CINFO_DCAP_VOLUME,
		/* Input capabilities */
		SND_MIXER_CINFO_DCAP_STEREO | SND_MIXER_CINFO_DCAP_EXCLUSIVE |
		SND_MIXER_CINFO_DCAP_HWMUTE | SND_MIXER_CINFO_DCAP_JOINMUTE,
		0, 1,			/* omin, omax value */
		-4500, 0,		/* omin, omax dB */
                0, 0,			/* imin, imax value */
                0, 0,			/* imin, imax dB */
		snd_ad1848_control,	/* set control */
		snd_ad1848_volume,	/* set volume level*/
    		AD1848_PRIVATE(AD1848_LEFT_INPUT, AD1848_RIGHT_INPUT, 5, 0) | 0x2000
	},
};

snd_kmixer_t *snd_ad1848_new_mixer(snd_pcm_t * pcm)
{
	int idx;
	ad1848_t *codec;
	snd_pcm1_t *pcm1;
	snd_kmixer_t *mixer;
	snd_kmixer_channel_t *channel;

	if (!pcm || !pcm->card)
		return NULL;
	pcm1 = (snd_pcm1_t *) pcm->private_data;
	if (!pcm1)
		return NULL;
	codec = (ad1848_t *) pcm1->private_data;
	if (!codec)
		return NULL;
	mixer = snd_mixer_new(pcm->card, pcm->id);
	if (!mixer)
		return NULL;
	strcpy(mixer->name, pcm->name);
	for (idx = 0; idx < AD1848_MIXS; idx++) {
		channel = snd_mixer_new_channel(mixer, &snd_ad1848_mixs[idx]);
		if (!channel) {
			snd_mixer_free(mixer);
			return NULL;
		}
	}
	mixer->hw.caps = 0;
	mixer->private_data = codec;
	codec->mixer = mixer;
	return mixer;
}

