18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  als300.c - driver for Avance Logic ALS300/ALS300+ soundcards.
48c2ecf20Sopenharmony_ci *  Copyright (C) 2005 by Ash Willis <ashwillis@programmer.net>
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci *  TODO
78c2ecf20Sopenharmony_ci *  4 channel playback for ALS300+
88c2ecf20Sopenharmony_ci *  gameport
98c2ecf20Sopenharmony_ci *  mpu401
108c2ecf20Sopenharmony_ci *  opl3
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci *  NOTES
138c2ecf20Sopenharmony_ci *  The BLOCK_COUNTER registers for the ALS300(+) return a figure related to
148c2ecf20Sopenharmony_ci *  the position in the current period, NOT the whole buffer. It is important
158c2ecf20Sopenharmony_ci *  to know which period we are in so we can calculate the correct pointer.
168c2ecf20Sopenharmony_ci *  This is why we always use 2 periods. We can then use a flip-flop variable
178c2ecf20Sopenharmony_ci *  to keep track of what period we are in.
188c2ecf20Sopenharmony_ci */
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#include <linux/delay.h>
218c2ecf20Sopenharmony_ci#include <linux/init.h>
228c2ecf20Sopenharmony_ci#include <linux/module.h>
238c2ecf20Sopenharmony_ci#include <linux/pci.h>
248c2ecf20Sopenharmony_ci#include <linux/dma-mapping.h>
258c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
268c2ecf20Sopenharmony_ci#include <linux/slab.h>
278c2ecf20Sopenharmony_ci#include <linux/io.h>
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci#include <sound/core.h>
308c2ecf20Sopenharmony_ci#include <sound/control.h>
318c2ecf20Sopenharmony_ci#include <sound/initval.h>
328c2ecf20Sopenharmony_ci#include <sound/pcm.h>
338c2ecf20Sopenharmony_ci#include <sound/pcm_params.h>
348c2ecf20Sopenharmony_ci#include <sound/ac97_codec.h>
358c2ecf20Sopenharmony_ci#include <sound/opl3.h>
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci/* snd_als300_set_irq_flag */
388c2ecf20Sopenharmony_ci#define IRQ_DISABLE		0
398c2ecf20Sopenharmony_ci#define IRQ_ENABLE		1
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci/* I/O port layout */
428c2ecf20Sopenharmony_ci#define AC97_ACCESS		0x00
438c2ecf20Sopenharmony_ci#define AC97_READ		0x04
448c2ecf20Sopenharmony_ci#define AC97_STATUS		0x06
458c2ecf20Sopenharmony_ci#define   AC97_DATA_AVAIL		(1<<6)
468c2ecf20Sopenharmony_ci#define   AC97_BUSY			(1<<7)
478c2ecf20Sopenharmony_ci#define ALS300_IRQ_STATUS	0x07		/* ALS300 Only */
488c2ecf20Sopenharmony_ci#define   IRQ_PLAYBACK			(1<<3)
498c2ecf20Sopenharmony_ci#define   IRQ_CAPTURE			(1<<2)
508c2ecf20Sopenharmony_ci#define GCR_DATA		0x08
518c2ecf20Sopenharmony_ci#define GCR_INDEX		0x0C
528c2ecf20Sopenharmony_ci#define ALS300P_DRAM_IRQ_STATUS	0x0D		/* ALS300+ Only */
538c2ecf20Sopenharmony_ci#define MPU_IRQ_STATUS		0x0E		/* ALS300 Rev. E+, ALS300+ */
548c2ecf20Sopenharmony_ci#define ALS300P_IRQ_STATUS	0x0F		/* ALS300+ Only */
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci/* General Control Registers */
578c2ecf20Sopenharmony_ci#define PLAYBACK_START		0x80
588c2ecf20Sopenharmony_ci#define PLAYBACK_END		0x81
598c2ecf20Sopenharmony_ci#define PLAYBACK_CONTROL	0x82
608c2ecf20Sopenharmony_ci#define   TRANSFER_START		(1<<16)
618c2ecf20Sopenharmony_ci#define   FIFO_PAUSE			(1<<17)
628c2ecf20Sopenharmony_ci#define RECORD_START		0x83
638c2ecf20Sopenharmony_ci#define RECORD_END		0x84
648c2ecf20Sopenharmony_ci#define RECORD_CONTROL		0x85
658c2ecf20Sopenharmony_ci#define DRAM_WRITE_CONTROL	0x8B
668c2ecf20Sopenharmony_ci#define   WRITE_TRANS_START		(1<<16)
678c2ecf20Sopenharmony_ci#define   DRAM_MODE_2			(1<<17)
688c2ecf20Sopenharmony_ci#define MISC_CONTROL		0x8C
698c2ecf20Sopenharmony_ci#define   IRQ_SET_BIT			(1<<15)
708c2ecf20Sopenharmony_ci#define   VMUTE_NORMAL			(1<<20)
718c2ecf20Sopenharmony_ci#define   MMUTE_NORMAL			(1<<21)
728c2ecf20Sopenharmony_ci#define MUS_VOC_VOL		0x8E
738c2ecf20Sopenharmony_ci#define PLAYBACK_BLOCK_COUNTER	0x9A
748c2ecf20Sopenharmony_ci#define RECORD_BLOCK_COUNTER	0x9B
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci#define DEBUG_PLAY_REC	0
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci#if DEBUG_PLAY_REC
798c2ecf20Sopenharmony_ci#define snd_als300_dbgplay(format, args...) printk(KERN_ERR format, ##args)
808c2ecf20Sopenharmony_ci#else
818c2ecf20Sopenharmony_ci#define snd_als300_dbgplay(format, args...)
828c2ecf20Sopenharmony_ci#endif
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_cienum {DEVICE_ALS300, DEVICE_ALS300_PLUS};
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ciMODULE_AUTHOR("Ash Willis <ashwillis@programmer.net>");
878c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Avance Logic ALS300");
888c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
898c2ecf20Sopenharmony_ciMODULE_SUPPORTED_DEVICE("{{Avance Logic,ALS300},{Avance Logic,ALS300+}}");
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_cistatic int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
928c2ecf20Sopenharmony_cistatic char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
938c2ecf20Sopenharmony_cistatic bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_cimodule_param_array(index, int, NULL, 0444);
968c2ecf20Sopenharmony_ciMODULE_PARM_DESC(index, "Index value for ALS300 sound card.");
978c2ecf20Sopenharmony_cimodule_param_array(id, charp, NULL, 0444);
988c2ecf20Sopenharmony_ciMODULE_PARM_DESC(id, "ID string for ALS300 sound card.");
998c2ecf20Sopenharmony_cimodule_param_array(enable, bool, NULL, 0444);
1008c2ecf20Sopenharmony_ciMODULE_PARM_DESC(enable, "Enable ALS300 sound card.");
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_cistruct snd_als300 {
1038c2ecf20Sopenharmony_ci	unsigned long port;
1048c2ecf20Sopenharmony_ci	spinlock_t reg_lock;
1058c2ecf20Sopenharmony_ci	struct snd_card *card;
1068c2ecf20Sopenharmony_ci	struct pci_dev *pci;
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	struct snd_pcm *pcm;
1098c2ecf20Sopenharmony_ci	struct snd_pcm_substream *playback_substream;
1108c2ecf20Sopenharmony_ci	struct snd_pcm_substream *capture_substream;
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	struct snd_ac97 *ac97;
1138c2ecf20Sopenharmony_ci	struct snd_opl3 *opl3;
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	struct resource *res_port;
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	int irq;
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	int chip_type; /* ALS300 or ALS300+ */
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	char revision;
1228c2ecf20Sopenharmony_ci};
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_cistruct snd_als300_substream_data {
1258c2ecf20Sopenharmony_ci	int period_flipflop;
1268c2ecf20Sopenharmony_ci	int control_register;
1278c2ecf20Sopenharmony_ci	int block_counter_register;
1288c2ecf20Sopenharmony_ci};
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_cistatic const struct pci_device_id snd_als300_ids[] = {
1318c2ecf20Sopenharmony_ci	{ 0x4005, 0x0300, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_ALS300 },
1328c2ecf20Sopenharmony_ci	{ 0x4005, 0x0308, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DEVICE_ALS300_PLUS },
1338c2ecf20Sopenharmony_ci	{ 0, }
1348c2ecf20Sopenharmony_ci};
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(pci, snd_als300_ids);
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_cistatic inline u32 snd_als300_gcr_read(unsigned long port, unsigned short reg)
1398c2ecf20Sopenharmony_ci{
1408c2ecf20Sopenharmony_ci	outb(reg, port+GCR_INDEX);
1418c2ecf20Sopenharmony_ci	return inl(port+GCR_DATA);
1428c2ecf20Sopenharmony_ci}
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_cistatic inline void snd_als300_gcr_write(unsigned long port,
1458c2ecf20Sopenharmony_ci						unsigned short reg, u32 val)
1468c2ecf20Sopenharmony_ci{
1478c2ecf20Sopenharmony_ci	outb(reg, port+GCR_INDEX);
1488c2ecf20Sopenharmony_ci	outl(val, port+GCR_DATA);
1498c2ecf20Sopenharmony_ci}
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci/* Enable/Disable Interrupts */
1528c2ecf20Sopenharmony_cistatic void snd_als300_set_irq_flag(struct snd_als300 *chip, int cmd)
1538c2ecf20Sopenharmony_ci{
1548c2ecf20Sopenharmony_ci	u32 tmp = snd_als300_gcr_read(chip->port, MISC_CONTROL);
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	/* boolean XOR check, since old vs. new hardware have
1578c2ecf20Sopenharmony_ci	   directly reversed bit setting for ENABLE and DISABLE.
1588c2ecf20Sopenharmony_ci	   ALS300+ acts like newer versions of ALS300 */
1598c2ecf20Sopenharmony_ci	if (((chip->revision > 5 || chip->chip_type == DEVICE_ALS300_PLUS) ^
1608c2ecf20Sopenharmony_ci						(cmd == IRQ_ENABLE)) == 0)
1618c2ecf20Sopenharmony_ci		tmp |= IRQ_SET_BIT;
1628c2ecf20Sopenharmony_ci	else
1638c2ecf20Sopenharmony_ci		tmp &= ~IRQ_SET_BIT;
1648c2ecf20Sopenharmony_ci	snd_als300_gcr_write(chip->port, MISC_CONTROL, tmp);
1658c2ecf20Sopenharmony_ci}
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_cistatic int snd_als300_free(struct snd_als300 *chip)
1688c2ecf20Sopenharmony_ci{
1698c2ecf20Sopenharmony_ci	snd_als300_set_irq_flag(chip, IRQ_DISABLE);
1708c2ecf20Sopenharmony_ci	if (chip->irq >= 0)
1718c2ecf20Sopenharmony_ci		free_irq(chip->irq, chip);
1728c2ecf20Sopenharmony_ci	pci_release_regions(chip->pci);
1738c2ecf20Sopenharmony_ci	pci_disable_device(chip->pci);
1748c2ecf20Sopenharmony_ci	kfree(chip);
1758c2ecf20Sopenharmony_ci	return 0;
1768c2ecf20Sopenharmony_ci}
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_cistatic int snd_als300_dev_free(struct snd_device *device)
1798c2ecf20Sopenharmony_ci{
1808c2ecf20Sopenharmony_ci	struct snd_als300 *chip = device->device_data;
1818c2ecf20Sopenharmony_ci	return snd_als300_free(chip);
1828c2ecf20Sopenharmony_ci}
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_cistatic irqreturn_t snd_als300_interrupt(int irq, void *dev_id)
1858c2ecf20Sopenharmony_ci{
1868c2ecf20Sopenharmony_ci	u8 status;
1878c2ecf20Sopenharmony_ci	struct snd_als300 *chip = dev_id;
1888c2ecf20Sopenharmony_ci	struct snd_als300_substream_data *data;
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci	status = inb(chip->port+ALS300_IRQ_STATUS);
1918c2ecf20Sopenharmony_ci	if (!status) /* shared IRQ, for different device?? Exit ASAP! */
1928c2ecf20Sopenharmony_ci		return IRQ_NONE;
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	/* ACK everything ASAP */
1958c2ecf20Sopenharmony_ci	outb(status, chip->port+ALS300_IRQ_STATUS);
1968c2ecf20Sopenharmony_ci	if (status & IRQ_PLAYBACK) {
1978c2ecf20Sopenharmony_ci		if (chip->pcm && chip->playback_substream) {
1988c2ecf20Sopenharmony_ci			data = chip->playback_substream->runtime->private_data;
1998c2ecf20Sopenharmony_ci			data->period_flipflop ^= 1;
2008c2ecf20Sopenharmony_ci			snd_pcm_period_elapsed(chip->playback_substream);
2018c2ecf20Sopenharmony_ci			snd_als300_dbgplay("IRQ_PLAYBACK\n");
2028c2ecf20Sopenharmony_ci		}
2038c2ecf20Sopenharmony_ci	}
2048c2ecf20Sopenharmony_ci	if (status & IRQ_CAPTURE) {
2058c2ecf20Sopenharmony_ci		if (chip->pcm && chip->capture_substream) {
2068c2ecf20Sopenharmony_ci			data = chip->capture_substream->runtime->private_data;
2078c2ecf20Sopenharmony_ci			data->period_flipflop ^= 1;
2088c2ecf20Sopenharmony_ci			snd_pcm_period_elapsed(chip->capture_substream);
2098c2ecf20Sopenharmony_ci			snd_als300_dbgplay("IRQ_CAPTURE\n");
2108c2ecf20Sopenharmony_ci		}
2118c2ecf20Sopenharmony_ci	}
2128c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
2138c2ecf20Sopenharmony_ci}
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_cistatic irqreturn_t snd_als300plus_interrupt(int irq, void *dev_id)
2168c2ecf20Sopenharmony_ci{
2178c2ecf20Sopenharmony_ci	u8 general, mpu, dram;
2188c2ecf20Sopenharmony_ci	struct snd_als300 *chip = dev_id;
2198c2ecf20Sopenharmony_ci	struct snd_als300_substream_data *data;
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	general = inb(chip->port+ALS300P_IRQ_STATUS);
2228c2ecf20Sopenharmony_ci	mpu = inb(chip->port+MPU_IRQ_STATUS);
2238c2ecf20Sopenharmony_ci	dram = inb(chip->port+ALS300P_DRAM_IRQ_STATUS);
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci	/* shared IRQ, for different device?? Exit ASAP! */
2268c2ecf20Sopenharmony_ci	if ((general == 0) && ((mpu & 0x80) == 0) && ((dram & 0x01) == 0))
2278c2ecf20Sopenharmony_ci		return IRQ_NONE;
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	if (general & IRQ_PLAYBACK) {
2308c2ecf20Sopenharmony_ci		if (chip->pcm && chip->playback_substream) {
2318c2ecf20Sopenharmony_ci			outb(IRQ_PLAYBACK, chip->port+ALS300P_IRQ_STATUS);
2328c2ecf20Sopenharmony_ci			data = chip->playback_substream->runtime->private_data;
2338c2ecf20Sopenharmony_ci			data->period_flipflop ^= 1;
2348c2ecf20Sopenharmony_ci			snd_pcm_period_elapsed(chip->playback_substream);
2358c2ecf20Sopenharmony_ci			snd_als300_dbgplay("IRQ_PLAYBACK\n");
2368c2ecf20Sopenharmony_ci		}
2378c2ecf20Sopenharmony_ci	}
2388c2ecf20Sopenharmony_ci	if (general & IRQ_CAPTURE) {
2398c2ecf20Sopenharmony_ci		if (chip->pcm && chip->capture_substream) {
2408c2ecf20Sopenharmony_ci			outb(IRQ_CAPTURE, chip->port+ALS300P_IRQ_STATUS);
2418c2ecf20Sopenharmony_ci			data = chip->capture_substream->runtime->private_data;
2428c2ecf20Sopenharmony_ci			data->period_flipflop ^= 1;
2438c2ecf20Sopenharmony_ci			snd_pcm_period_elapsed(chip->capture_substream);
2448c2ecf20Sopenharmony_ci			snd_als300_dbgplay("IRQ_CAPTURE\n");
2458c2ecf20Sopenharmony_ci		}
2468c2ecf20Sopenharmony_ci	}
2478c2ecf20Sopenharmony_ci	/* FIXME: Ack other interrupt types. Not important right now as
2488c2ecf20Sopenharmony_ci	 * those other devices aren't enabled. */
2498c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
2508c2ecf20Sopenharmony_ci}
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_cistatic void snd_als300_remove(struct pci_dev *pci)
2538c2ecf20Sopenharmony_ci{
2548c2ecf20Sopenharmony_ci	snd_card_free(pci_get_drvdata(pci));
2558c2ecf20Sopenharmony_ci}
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_cistatic unsigned short snd_als300_ac97_read(struct snd_ac97 *ac97,
2588c2ecf20Sopenharmony_ci							unsigned short reg)
2598c2ecf20Sopenharmony_ci{
2608c2ecf20Sopenharmony_ci	int i;
2618c2ecf20Sopenharmony_ci	struct snd_als300 *chip = ac97->private_data;
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci	for (i = 0; i < 1000; i++) {
2648c2ecf20Sopenharmony_ci		if ((inb(chip->port+AC97_STATUS) & (AC97_BUSY)) == 0)
2658c2ecf20Sopenharmony_ci			break;
2668c2ecf20Sopenharmony_ci		udelay(10);
2678c2ecf20Sopenharmony_ci	}
2688c2ecf20Sopenharmony_ci	outl((reg << 24) | (1 << 31), chip->port+AC97_ACCESS);
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci	for (i = 0; i < 1000; i++) {
2718c2ecf20Sopenharmony_ci		if ((inb(chip->port+AC97_STATUS) & (AC97_DATA_AVAIL)) != 0)
2728c2ecf20Sopenharmony_ci			break;
2738c2ecf20Sopenharmony_ci		udelay(10);
2748c2ecf20Sopenharmony_ci	}
2758c2ecf20Sopenharmony_ci	return inw(chip->port+AC97_READ);
2768c2ecf20Sopenharmony_ci}
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_cistatic void snd_als300_ac97_write(struct snd_ac97 *ac97,
2798c2ecf20Sopenharmony_ci				unsigned short reg, unsigned short val)
2808c2ecf20Sopenharmony_ci{
2818c2ecf20Sopenharmony_ci	int i;
2828c2ecf20Sopenharmony_ci	struct snd_als300 *chip = ac97->private_data;
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci	for (i = 0; i < 1000; i++) {
2858c2ecf20Sopenharmony_ci		if ((inb(chip->port+AC97_STATUS) & (AC97_BUSY)) == 0)
2868c2ecf20Sopenharmony_ci			break;
2878c2ecf20Sopenharmony_ci		udelay(10);
2888c2ecf20Sopenharmony_ci	}
2898c2ecf20Sopenharmony_ci	outl((reg << 24) | val, chip->port+AC97_ACCESS);
2908c2ecf20Sopenharmony_ci}
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_cistatic int snd_als300_ac97(struct snd_als300 *chip)
2938c2ecf20Sopenharmony_ci{
2948c2ecf20Sopenharmony_ci	struct snd_ac97_bus *bus;
2958c2ecf20Sopenharmony_ci	struct snd_ac97_template ac97;
2968c2ecf20Sopenharmony_ci	int err;
2978c2ecf20Sopenharmony_ci	static const struct snd_ac97_bus_ops ops = {
2988c2ecf20Sopenharmony_ci		.write = snd_als300_ac97_write,
2998c2ecf20Sopenharmony_ci		.read = snd_als300_ac97_read,
3008c2ecf20Sopenharmony_ci	};
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci	if ((err = snd_ac97_bus(chip->card, 0, &ops, NULL, &bus)) < 0)
3038c2ecf20Sopenharmony_ci		return err;
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci	memset(&ac97, 0, sizeof(ac97));
3068c2ecf20Sopenharmony_ci	ac97.private_data = chip;
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci	return snd_ac97_mixer(bus, &ac97, &chip->ac97);
3098c2ecf20Sopenharmony_ci}
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci/* hardware definition
3128c2ecf20Sopenharmony_ci *
3138c2ecf20Sopenharmony_ci * In AC97 mode, we always use 48k/16bit/stereo.
3148c2ecf20Sopenharmony_ci * Any request to change data type is ignored by
3158c2ecf20Sopenharmony_ci * the card when it is running outside of legacy
3168c2ecf20Sopenharmony_ci * mode.
3178c2ecf20Sopenharmony_ci */
3188c2ecf20Sopenharmony_cistatic const struct snd_pcm_hardware snd_als300_playback_hw =
3198c2ecf20Sopenharmony_ci{
3208c2ecf20Sopenharmony_ci	.info =			(SNDRV_PCM_INFO_MMAP |
3218c2ecf20Sopenharmony_ci				SNDRV_PCM_INFO_INTERLEAVED |
3228c2ecf20Sopenharmony_ci				SNDRV_PCM_INFO_PAUSE |
3238c2ecf20Sopenharmony_ci				SNDRV_PCM_INFO_MMAP_VALID),
3248c2ecf20Sopenharmony_ci	.formats =		SNDRV_PCM_FMTBIT_S16,
3258c2ecf20Sopenharmony_ci	.rates =		SNDRV_PCM_RATE_48000,
3268c2ecf20Sopenharmony_ci	.rate_min =		48000,
3278c2ecf20Sopenharmony_ci	.rate_max =		48000,
3288c2ecf20Sopenharmony_ci	.channels_min =		2,
3298c2ecf20Sopenharmony_ci	.channels_max =		2,
3308c2ecf20Sopenharmony_ci	.buffer_bytes_max =	64 * 1024,
3318c2ecf20Sopenharmony_ci	.period_bytes_min =	64,
3328c2ecf20Sopenharmony_ci	.period_bytes_max =	32 * 1024,
3338c2ecf20Sopenharmony_ci	.periods_min =		2,
3348c2ecf20Sopenharmony_ci	.periods_max =		2,
3358c2ecf20Sopenharmony_ci};
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_cistatic const struct snd_pcm_hardware snd_als300_capture_hw =
3388c2ecf20Sopenharmony_ci{
3398c2ecf20Sopenharmony_ci	.info =			(SNDRV_PCM_INFO_MMAP |
3408c2ecf20Sopenharmony_ci				SNDRV_PCM_INFO_INTERLEAVED |
3418c2ecf20Sopenharmony_ci				SNDRV_PCM_INFO_PAUSE |
3428c2ecf20Sopenharmony_ci				SNDRV_PCM_INFO_MMAP_VALID),
3438c2ecf20Sopenharmony_ci	.formats =		SNDRV_PCM_FMTBIT_S16,
3448c2ecf20Sopenharmony_ci	.rates =		SNDRV_PCM_RATE_48000,
3458c2ecf20Sopenharmony_ci	.rate_min =		48000,
3468c2ecf20Sopenharmony_ci	.rate_max =		48000,
3478c2ecf20Sopenharmony_ci	.channels_min =		2,
3488c2ecf20Sopenharmony_ci	.channels_max =		2,
3498c2ecf20Sopenharmony_ci	.buffer_bytes_max =	64 * 1024,
3508c2ecf20Sopenharmony_ci	.period_bytes_min =	64,
3518c2ecf20Sopenharmony_ci	.period_bytes_max =	32 * 1024,
3528c2ecf20Sopenharmony_ci	.periods_min =		2,
3538c2ecf20Sopenharmony_ci	.periods_max =		2,
3548c2ecf20Sopenharmony_ci};
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_cistatic int snd_als300_playback_open(struct snd_pcm_substream *substream)
3578c2ecf20Sopenharmony_ci{
3588c2ecf20Sopenharmony_ci	struct snd_als300 *chip = snd_pcm_substream_chip(substream);
3598c2ecf20Sopenharmony_ci	struct snd_pcm_runtime *runtime = substream->runtime;
3608c2ecf20Sopenharmony_ci	struct snd_als300_substream_data *data = kzalloc(sizeof(*data),
3618c2ecf20Sopenharmony_ci								GFP_KERNEL);
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci	if (!data)
3648c2ecf20Sopenharmony_ci		return -ENOMEM;
3658c2ecf20Sopenharmony_ci	chip->playback_substream = substream;
3668c2ecf20Sopenharmony_ci	runtime->hw = snd_als300_playback_hw;
3678c2ecf20Sopenharmony_ci	runtime->private_data = data;
3688c2ecf20Sopenharmony_ci	data->control_register = PLAYBACK_CONTROL;
3698c2ecf20Sopenharmony_ci	data->block_counter_register = PLAYBACK_BLOCK_COUNTER;
3708c2ecf20Sopenharmony_ci	return 0;
3718c2ecf20Sopenharmony_ci}
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_cistatic int snd_als300_playback_close(struct snd_pcm_substream *substream)
3748c2ecf20Sopenharmony_ci{
3758c2ecf20Sopenharmony_ci	struct snd_als300 *chip = snd_pcm_substream_chip(substream);
3768c2ecf20Sopenharmony_ci	struct snd_als300_substream_data *data;
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_ci	data = substream->runtime->private_data;
3798c2ecf20Sopenharmony_ci	kfree(data);
3808c2ecf20Sopenharmony_ci	chip->playback_substream = NULL;
3818c2ecf20Sopenharmony_ci	return 0;
3828c2ecf20Sopenharmony_ci}
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_cistatic int snd_als300_capture_open(struct snd_pcm_substream *substream)
3858c2ecf20Sopenharmony_ci{
3868c2ecf20Sopenharmony_ci	struct snd_als300 *chip = snd_pcm_substream_chip(substream);
3878c2ecf20Sopenharmony_ci	struct snd_pcm_runtime *runtime = substream->runtime;
3888c2ecf20Sopenharmony_ci	struct snd_als300_substream_data *data = kzalloc(sizeof(*data),
3898c2ecf20Sopenharmony_ci								GFP_KERNEL);
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci	if (!data)
3928c2ecf20Sopenharmony_ci		return -ENOMEM;
3938c2ecf20Sopenharmony_ci	chip->capture_substream = substream;
3948c2ecf20Sopenharmony_ci	runtime->hw = snd_als300_capture_hw;
3958c2ecf20Sopenharmony_ci	runtime->private_data = data;
3968c2ecf20Sopenharmony_ci	data->control_register = RECORD_CONTROL;
3978c2ecf20Sopenharmony_ci	data->block_counter_register = RECORD_BLOCK_COUNTER;
3988c2ecf20Sopenharmony_ci	return 0;
3998c2ecf20Sopenharmony_ci}
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_cistatic int snd_als300_capture_close(struct snd_pcm_substream *substream)
4028c2ecf20Sopenharmony_ci{
4038c2ecf20Sopenharmony_ci	struct snd_als300 *chip = snd_pcm_substream_chip(substream);
4048c2ecf20Sopenharmony_ci	struct snd_als300_substream_data *data;
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_ci	data = substream->runtime->private_data;
4078c2ecf20Sopenharmony_ci	kfree(data);
4088c2ecf20Sopenharmony_ci	chip->capture_substream = NULL;
4098c2ecf20Sopenharmony_ci	return 0;
4108c2ecf20Sopenharmony_ci}
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_cistatic int snd_als300_playback_prepare(struct snd_pcm_substream *substream)
4138c2ecf20Sopenharmony_ci{
4148c2ecf20Sopenharmony_ci	u32 tmp;
4158c2ecf20Sopenharmony_ci	struct snd_als300 *chip = snd_pcm_substream_chip(substream);
4168c2ecf20Sopenharmony_ci	struct snd_pcm_runtime *runtime = substream->runtime;
4178c2ecf20Sopenharmony_ci	unsigned short period_bytes = snd_pcm_lib_period_bytes(substream);
4188c2ecf20Sopenharmony_ci	unsigned short buffer_bytes = snd_pcm_lib_buffer_bytes(substream);
4198c2ecf20Sopenharmony_ci
4208c2ecf20Sopenharmony_ci	spin_lock_irq(&chip->reg_lock);
4218c2ecf20Sopenharmony_ci	tmp = snd_als300_gcr_read(chip->port, PLAYBACK_CONTROL);
4228c2ecf20Sopenharmony_ci	tmp &= ~TRANSFER_START;
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_ci	snd_als300_dbgplay("Period bytes: %d Buffer bytes %d\n",
4258c2ecf20Sopenharmony_ci						period_bytes, buffer_bytes);
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_ci	/* set block size */
4288c2ecf20Sopenharmony_ci	tmp &= 0xffff0000;
4298c2ecf20Sopenharmony_ci	tmp |= period_bytes - 1;
4308c2ecf20Sopenharmony_ci	snd_als300_gcr_write(chip->port, PLAYBACK_CONTROL, tmp);
4318c2ecf20Sopenharmony_ci
4328c2ecf20Sopenharmony_ci	/* set dma area */
4338c2ecf20Sopenharmony_ci	snd_als300_gcr_write(chip->port, PLAYBACK_START,
4348c2ecf20Sopenharmony_ci					runtime->dma_addr);
4358c2ecf20Sopenharmony_ci	snd_als300_gcr_write(chip->port, PLAYBACK_END,
4368c2ecf20Sopenharmony_ci					runtime->dma_addr + buffer_bytes - 1);
4378c2ecf20Sopenharmony_ci	spin_unlock_irq(&chip->reg_lock);
4388c2ecf20Sopenharmony_ci	return 0;
4398c2ecf20Sopenharmony_ci}
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_cistatic int snd_als300_capture_prepare(struct snd_pcm_substream *substream)
4428c2ecf20Sopenharmony_ci{
4438c2ecf20Sopenharmony_ci	u32 tmp;
4448c2ecf20Sopenharmony_ci	struct snd_als300 *chip = snd_pcm_substream_chip(substream);
4458c2ecf20Sopenharmony_ci	struct snd_pcm_runtime *runtime = substream->runtime;
4468c2ecf20Sopenharmony_ci	unsigned short period_bytes = snd_pcm_lib_period_bytes(substream);
4478c2ecf20Sopenharmony_ci	unsigned short buffer_bytes = snd_pcm_lib_buffer_bytes(substream);
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_ci	spin_lock_irq(&chip->reg_lock);
4508c2ecf20Sopenharmony_ci	tmp = snd_als300_gcr_read(chip->port, RECORD_CONTROL);
4518c2ecf20Sopenharmony_ci	tmp &= ~TRANSFER_START;
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_ci	snd_als300_dbgplay("Period bytes: %d Buffer bytes %d\n", period_bytes,
4548c2ecf20Sopenharmony_ci							buffer_bytes);
4558c2ecf20Sopenharmony_ci
4568c2ecf20Sopenharmony_ci	/* set block size */
4578c2ecf20Sopenharmony_ci	tmp &= 0xffff0000;
4588c2ecf20Sopenharmony_ci	tmp |= period_bytes - 1;
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_ci	/* set dma area */
4618c2ecf20Sopenharmony_ci	snd_als300_gcr_write(chip->port, RECORD_CONTROL, tmp);
4628c2ecf20Sopenharmony_ci	snd_als300_gcr_write(chip->port, RECORD_START,
4638c2ecf20Sopenharmony_ci					runtime->dma_addr);
4648c2ecf20Sopenharmony_ci	snd_als300_gcr_write(chip->port, RECORD_END,
4658c2ecf20Sopenharmony_ci					runtime->dma_addr + buffer_bytes - 1);
4668c2ecf20Sopenharmony_ci	spin_unlock_irq(&chip->reg_lock);
4678c2ecf20Sopenharmony_ci	return 0;
4688c2ecf20Sopenharmony_ci}
4698c2ecf20Sopenharmony_ci
4708c2ecf20Sopenharmony_cistatic int snd_als300_trigger(struct snd_pcm_substream *substream, int cmd)
4718c2ecf20Sopenharmony_ci{
4728c2ecf20Sopenharmony_ci	struct snd_als300 *chip = snd_pcm_substream_chip(substream);
4738c2ecf20Sopenharmony_ci	u32 tmp;
4748c2ecf20Sopenharmony_ci	struct snd_als300_substream_data *data;
4758c2ecf20Sopenharmony_ci	unsigned short reg;
4768c2ecf20Sopenharmony_ci	int ret = 0;
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_ci	data = substream->runtime->private_data;
4798c2ecf20Sopenharmony_ci	reg = data->control_register;
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_ci	spin_lock(&chip->reg_lock);
4828c2ecf20Sopenharmony_ci	switch (cmd) {
4838c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_START:
4848c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_RESUME:
4858c2ecf20Sopenharmony_ci		tmp = snd_als300_gcr_read(chip->port, reg);
4868c2ecf20Sopenharmony_ci		data->period_flipflop = 1;
4878c2ecf20Sopenharmony_ci		snd_als300_gcr_write(chip->port, reg, tmp | TRANSFER_START);
4888c2ecf20Sopenharmony_ci		snd_als300_dbgplay("TRIGGER START\n");
4898c2ecf20Sopenharmony_ci		break;
4908c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_STOP:
4918c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_SUSPEND:
4928c2ecf20Sopenharmony_ci		tmp = snd_als300_gcr_read(chip->port, reg);
4938c2ecf20Sopenharmony_ci		snd_als300_gcr_write(chip->port, reg, tmp & ~TRANSFER_START);
4948c2ecf20Sopenharmony_ci		snd_als300_dbgplay("TRIGGER STOP\n");
4958c2ecf20Sopenharmony_ci		break;
4968c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
4978c2ecf20Sopenharmony_ci		tmp = snd_als300_gcr_read(chip->port, reg);
4988c2ecf20Sopenharmony_ci		snd_als300_gcr_write(chip->port, reg, tmp | FIFO_PAUSE);
4998c2ecf20Sopenharmony_ci		snd_als300_dbgplay("TRIGGER PAUSE\n");
5008c2ecf20Sopenharmony_ci		break;
5018c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
5028c2ecf20Sopenharmony_ci		tmp = snd_als300_gcr_read(chip->port, reg);
5038c2ecf20Sopenharmony_ci		snd_als300_gcr_write(chip->port, reg, tmp & ~FIFO_PAUSE);
5048c2ecf20Sopenharmony_ci		snd_als300_dbgplay("TRIGGER RELEASE\n");
5058c2ecf20Sopenharmony_ci		break;
5068c2ecf20Sopenharmony_ci	default:
5078c2ecf20Sopenharmony_ci		snd_als300_dbgplay("TRIGGER INVALID\n");
5088c2ecf20Sopenharmony_ci		ret = -EINVAL;
5098c2ecf20Sopenharmony_ci	}
5108c2ecf20Sopenharmony_ci	spin_unlock(&chip->reg_lock);
5118c2ecf20Sopenharmony_ci	return ret;
5128c2ecf20Sopenharmony_ci}
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_cistatic snd_pcm_uframes_t snd_als300_pointer(struct snd_pcm_substream *substream)
5158c2ecf20Sopenharmony_ci{
5168c2ecf20Sopenharmony_ci	u16 current_ptr;
5178c2ecf20Sopenharmony_ci	struct snd_als300 *chip = snd_pcm_substream_chip(substream);
5188c2ecf20Sopenharmony_ci	struct snd_als300_substream_data *data;
5198c2ecf20Sopenharmony_ci	unsigned short period_bytes;
5208c2ecf20Sopenharmony_ci
5218c2ecf20Sopenharmony_ci	data = substream->runtime->private_data;
5228c2ecf20Sopenharmony_ci	period_bytes = snd_pcm_lib_period_bytes(substream);
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_ci	spin_lock(&chip->reg_lock);
5258c2ecf20Sopenharmony_ci	current_ptr = (u16) snd_als300_gcr_read(chip->port,
5268c2ecf20Sopenharmony_ci					data->block_counter_register) + 4;
5278c2ecf20Sopenharmony_ci	spin_unlock(&chip->reg_lock);
5288c2ecf20Sopenharmony_ci	if (current_ptr > period_bytes)
5298c2ecf20Sopenharmony_ci		current_ptr = 0;
5308c2ecf20Sopenharmony_ci	else
5318c2ecf20Sopenharmony_ci		current_ptr = period_bytes - current_ptr;
5328c2ecf20Sopenharmony_ci
5338c2ecf20Sopenharmony_ci	if (data->period_flipflop == 0)
5348c2ecf20Sopenharmony_ci		current_ptr += period_bytes;
5358c2ecf20Sopenharmony_ci	snd_als300_dbgplay("Pointer (bytes): %d\n", current_ptr);
5368c2ecf20Sopenharmony_ci	return bytes_to_frames(substream->runtime, current_ptr);
5378c2ecf20Sopenharmony_ci}
5388c2ecf20Sopenharmony_ci
5398c2ecf20Sopenharmony_cistatic const struct snd_pcm_ops snd_als300_playback_ops = {
5408c2ecf20Sopenharmony_ci	.open =		snd_als300_playback_open,
5418c2ecf20Sopenharmony_ci	.close =	snd_als300_playback_close,
5428c2ecf20Sopenharmony_ci	.prepare =	snd_als300_playback_prepare,
5438c2ecf20Sopenharmony_ci	.trigger =	snd_als300_trigger,
5448c2ecf20Sopenharmony_ci	.pointer =	snd_als300_pointer,
5458c2ecf20Sopenharmony_ci};
5468c2ecf20Sopenharmony_ci
5478c2ecf20Sopenharmony_cistatic const struct snd_pcm_ops snd_als300_capture_ops = {
5488c2ecf20Sopenharmony_ci	.open =		snd_als300_capture_open,
5498c2ecf20Sopenharmony_ci	.close =	snd_als300_capture_close,
5508c2ecf20Sopenharmony_ci	.prepare =	snd_als300_capture_prepare,
5518c2ecf20Sopenharmony_ci	.trigger =	snd_als300_trigger,
5528c2ecf20Sopenharmony_ci	.pointer =	snd_als300_pointer,
5538c2ecf20Sopenharmony_ci};
5548c2ecf20Sopenharmony_ci
5558c2ecf20Sopenharmony_cistatic int snd_als300_new_pcm(struct snd_als300 *chip)
5568c2ecf20Sopenharmony_ci{
5578c2ecf20Sopenharmony_ci	struct snd_pcm *pcm;
5588c2ecf20Sopenharmony_ci	int err;
5598c2ecf20Sopenharmony_ci
5608c2ecf20Sopenharmony_ci	err = snd_pcm_new(chip->card, "ALS300", 0, 1, 1, &pcm);
5618c2ecf20Sopenharmony_ci	if (err < 0)
5628c2ecf20Sopenharmony_ci		return err;
5638c2ecf20Sopenharmony_ci	pcm->private_data = chip;
5648c2ecf20Sopenharmony_ci	strcpy(pcm->name, "ALS300");
5658c2ecf20Sopenharmony_ci	chip->pcm = pcm;
5668c2ecf20Sopenharmony_ci
5678c2ecf20Sopenharmony_ci	/* set operators */
5688c2ecf20Sopenharmony_ci	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
5698c2ecf20Sopenharmony_ci				&snd_als300_playback_ops);
5708c2ecf20Sopenharmony_ci	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
5718c2ecf20Sopenharmony_ci				&snd_als300_capture_ops);
5728c2ecf20Sopenharmony_ci
5738c2ecf20Sopenharmony_ci	/* pre-allocation of buffers */
5748c2ecf20Sopenharmony_ci	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, &chip->pci->dev,
5758c2ecf20Sopenharmony_ci				       64*1024, 64*1024);
5768c2ecf20Sopenharmony_ci	return 0;
5778c2ecf20Sopenharmony_ci}
5788c2ecf20Sopenharmony_ci
5798c2ecf20Sopenharmony_cistatic void snd_als300_init(struct snd_als300 *chip)
5808c2ecf20Sopenharmony_ci{
5818c2ecf20Sopenharmony_ci	unsigned long flags;
5828c2ecf20Sopenharmony_ci	u32 tmp;
5838c2ecf20Sopenharmony_ci
5848c2ecf20Sopenharmony_ci	spin_lock_irqsave(&chip->reg_lock, flags);
5858c2ecf20Sopenharmony_ci	chip->revision = (snd_als300_gcr_read(chip->port, MISC_CONTROL) >> 16)
5868c2ecf20Sopenharmony_ci								& 0x0000000F;
5878c2ecf20Sopenharmony_ci	/* Setup DRAM */
5888c2ecf20Sopenharmony_ci	tmp = snd_als300_gcr_read(chip->port, DRAM_WRITE_CONTROL);
5898c2ecf20Sopenharmony_ci	snd_als300_gcr_write(chip->port, DRAM_WRITE_CONTROL,
5908c2ecf20Sopenharmony_ci						(tmp | DRAM_MODE_2)
5918c2ecf20Sopenharmony_ci						& ~WRITE_TRANS_START);
5928c2ecf20Sopenharmony_ci
5938c2ecf20Sopenharmony_ci	/* Enable IRQ output */
5948c2ecf20Sopenharmony_ci	snd_als300_set_irq_flag(chip, IRQ_ENABLE);
5958c2ecf20Sopenharmony_ci
5968c2ecf20Sopenharmony_ci	/* Unmute hardware devices so their outputs get routed to
5978c2ecf20Sopenharmony_ci	 * the onboard mixer */
5988c2ecf20Sopenharmony_ci	tmp = snd_als300_gcr_read(chip->port, MISC_CONTROL);
5998c2ecf20Sopenharmony_ci	snd_als300_gcr_write(chip->port, MISC_CONTROL,
6008c2ecf20Sopenharmony_ci			tmp | VMUTE_NORMAL | MMUTE_NORMAL);
6018c2ecf20Sopenharmony_ci
6028c2ecf20Sopenharmony_ci	/* Reset volumes */
6038c2ecf20Sopenharmony_ci	snd_als300_gcr_write(chip->port, MUS_VOC_VOL, 0);
6048c2ecf20Sopenharmony_ci
6058c2ecf20Sopenharmony_ci	/* Make sure playback transfer is stopped */
6068c2ecf20Sopenharmony_ci	tmp = snd_als300_gcr_read(chip->port, PLAYBACK_CONTROL);
6078c2ecf20Sopenharmony_ci	snd_als300_gcr_write(chip->port, PLAYBACK_CONTROL,
6088c2ecf20Sopenharmony_ci			tmp & ~TRANSFER_START);
6098c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&chip->reg_lock, flags);
6108c2ecf20Sopenharmony_ci}
6118c2ecf20Sopenharmony_ci
6128c2ecf20Sopenharmony_cistatic int snd_als300_create(struct snd_card *card,
6138c2ecf20Sopenharmony_ci			     struct pci_dev *pci, int chip_type,
6148c2ecf20Sopenharmony_ci			     struct snd_als300 **rchip)
6158c2ecf20Sopenharmony_ci{
6168c2ecf20Sopenharmony_ci	struct snd_als300 *chip;
6178c2ecf20Sopenharmony_ci	void *irq_handler;
6188c2ecf20Sopenharmony_ci	int err;
6198c2ecf20Sopenharmony_ci
6208c2ecf20Sopenharmony_ci	static const struct snd_device_ops ops = {
6218c2ecf20Sopenharmony_ci		.dev_free = snd_als300_dev_free,
6228c2ecf20Sopenharmony_ci	};
6238c2ecf20Sopenharmony_ci	*rchip = NULL;
6248c2ecf20Sopenharmony_ci
6258c2ecf20Sopenharmony_ci	if ((err = pci_enable_device(pci)) < 0)
6268c2ecf20Sopenharmony_ci		return err;
6278c2ecf20Sopenharmony_ci
6288c2ecf20Sopenharmony_ci	if (dma_set_mask(&pci->dev, DMA_BIT_MASK(28)) < 0 ||
6298c2ecf20Sopenharmony_ci		dma_set_coherent_mask(&pci->dev, DMA_BIT_MASK(28)) < 0) {
6308c2ecf20Sopenharmony_ci		dev_err(card->dev, "error setting 28bit DMA mask\n");
6318c2ecf20Sopenharmony_ci		pci_disable_device(pci);
6328c2ecf20Sopenharmony_ci		return -ENXIO;
6338c2ecf20Sopenharmony_ci	}
6348c2ecf20Sopenharmony_ci	pci_set_master(pci);
6358c2ecf20Sopenharmony_ci
6368c2ecf20Sopenharmony_ci	chip = kzalloc(sizeof(*chip), GFP_KERNEL);
6378c2ecf20Sopenharmony_ci	if (chip == NULL) {
6388c2ecf20Sopenharmony_ci		pci_disable_device(pci);
6398c2ecf20Sopenharmony_ci		return -ENOMEM;
6408c2ecf20Sopenharmony_ci	}
6418c2ecf20Sopenharmony_ci
6428c2ecf20Sopenharmony_ci	chip->card = card;
6438c2ecf20Sopenharmony_ci	chip->pci = pci;
6448c2ecf20Sopenharmony_ci	chip->irq = -1;
6458c2ecf20Sopenharmony_ci	chip->chip_type = chip_type;
6468c2ecf20Sopenharmony_ci	spin_lock_init(&chip->reg_lock);
6478c2ecf20Sopenharmony_ci
6488c2ecf20Sopenharmony_ci	if ((err = pci_request_regions(pci, "ALS300")) < 0) {
6498c2ecf20Sopenharmony_ci		kfree(chip);
6508c2ecf20Sopenharmony_ci		pci_disable_device(pci);
6518c2ecf20Sopenharmony_ci		return err;
6528c2ecf20Sopenharmony_ci	}
6538c2ecf20Sopenharmony_ci	chip->port = pci_resource_start(pci, 0);
6548c2ecf20Sopenharmony_ci
6558c2ecf20Sopenharmony_ci	if (chip->chip_type == DEVICE_ALS300_PLUS)
6568c2ecf20Sopenharmony_ci		irq_handler = snd_als300plus_interrupt;
6578c2ecf20Sopenharmony_ci	else
6588c2ecf20Sopenharmony_ci		irq_handler = snd_als300_interrupt;
6598c2ecf20Sopenharmony_ci
6608c2ecf20Sopenharmony_ci	if (request_irq(pci->irq, irq_handler, IRQF_SHARED,
6618c2ecf20Sopenharmony_ci			KBUILD_MODNAME, chip)) {
6628c2ecf20Sopenharmony_ci		dev_err(card->dev, "unable to grab IRQ %d\n", pci->irq);
6638c2ecf20Sopenharmony_ci		snd_als300_free(chip);
6648c2ecf20Sopenharmony_ci		return -EBUSY;
6658c2ecf20Sopenharmony_ci	}
6668c2ecf20Sopenharmony_ci	chip->irq = pci->irq;
6678c2ecf20Sopenharmony_ci	card->sync_irq = chip->irq;
6688c2ecf20Sopenharmony_ci
6698c2ecf20Sopenharmony_ci	snd_als300_init(chip);
6708c2ecf20Sopenharmony_ci
6718c2ecf20Sopenharmony_ci	err = snd_als300_ac97(chip);
6728c2ecf20Sopenharmony_ci	if (err < 0) {
6738c2ecf20Sopenharmony_ci		dev_err(card->dev, "Could not create ac97\n");
6748c2ecf20Sopenharmony_ci		snd_als300_free(chip);
6758c2ecf20Sopenharmony_ci		return err;
6768c2ecf20Sopenharmony_ci	}
6778c2ecf20Sopenharmony_ci
6788c2ecf20Sopenharmony_ci	if ((err = snd_als300_new_pcm(chip)) < 0) {
6798c2ecf20Sopenharmony_ci		dev_err(card->dev, "Could not create PCM\n");
6808c2ecf20Sopenharmony_ci		snd_als300_free(chip);
6818c2ecf20Sopenharmony_ci		return err;
6828c2ecf20Sopenharmony_ci	}
6838c2ecf20Sopenharmony_ci
6848c2ecf20Sopenharmony_ci	if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,
6858c2ecf20Sopenharmony_ci						chip, &ops)) < 0) {
6868c2ecf20Sopenharmony_ci		snd_als300_free(chip);
6878c2ecf20Sopenharmony_ci		return err;
6888c2ecf20Sopenharmony_ci	}
6898c2ecf20Sopenharmony_ci
6908c2ecf20Sopenharmony_ci	*rchip = chip;
6918c2ecf20Sopenharmony_ci	return 0;
6928c2ecf20Sopenharmony_ci}
6938c2ecf20Sopenharmony_ci
6948c2ecf20Sopenharmony_ci#ifdef CONFIG_PM_SLEEP
6958c2ecf20Sopenharmony_cistatic int snd_als300_suspend(struct device *dev)
6968c2ecf20Sopenharmony_ci{
6978c2ecf20Sopenharmony_ci	struct snd_card *card = dev_get_drvdata(dev);
6988c2ecf20Sopenharmony_ci	struct snd_als300 *chip = card->private_data;
6998c2ecf20Sopenharmony_ci
7008c2ecf20Sopenharmony_ci	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
7018c2ecf20Sopenharmony_ci	snd_ac97_suspend(chip->ac97);
7028c2ecf20Sopenharmony_ci	return 0;
7038c2ecf20Sopenharmony_ci}
7048c2ecf20Sopenharmony_ci
7058c2ecf20Sopenharmony_cistatic int snd_als300_resume(struct device *dev)
7068c2ecf20Sopenharmony_ci{
7078c2ecf20Sopenharmony_ci	struct snd_card *card = dev_get_drvdata(dev);
7088c2ecf20Sopenharmony_ci	struct snd_als300 *chip = card->private_data;
7098c2ecf20Sopenharmony_ci
7108c2ecf20Sopenharmony_ci	snd_als300_init(chip);
7118c2ecf20Sopenharmony_ci	snd_ac97_resume(chip->ac97);
7128c2ecf20Sopenharmony_ci
7138c2ecf20Sopenharmony_ci	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
7148c2ecf20Sopenharmony_ci	return 0;
7158c2ecf20Sopenharmony_ci}
7168c2ecf20Sopenharmony_ci
7178c2ecf20Sopenharmony_cistatic SIMPLE_DEV_PM_OPS(snd_als300_pm, snd_als300_suspend, snd_als300_resume);
7188c2ecf20Sopenharmony_ci#define SND_ALS300_PM_OPS	&snd_als300_pm
7198c2ecf20Sopenharmony_ci#else
7208c2ecf20Sopenharmony_ci#define SND_ALS300_PM_OPS	NULL
7218c2ecf20Sopenharmony_ci#endif
7228c2ecf20Sopenharmony_ci
7238c2ecf20Sopenharmony_cistatic int snd_als300_probe(struct pci_dev *pci,
7248c2ecf20Sopenharmony_ci                             const struct pci_device_id *pci_id)
7258c2ecf20Sopenharmony_ci{
7268c2ecf20Sopenharmony_ci	static int dev;
7278c2ecf20Sopenharmony_ci	struct snd_card *card;
7288c2ecf20Sopenharmony_ci	struct snd_als300 *chip;
7298c2ecf20Sopenharmony_ci	int err, chip_type;
7308c2ecf20Sopenharmony_ci
7318c2ecf20Sopenharmony_ci	if (dev >= SNDRV_CARDS)
7328c2ecf20Sopenharmony_ci		return -ENODEV;
7338c2ecf20Sopenharmony_ci	if (!enable[dev]) {
7348c2ecf20Sopenharmony_ci		dev++;
7358c2ecf20Sopenharmony_ci		return -ENOENT;
7368c2ecf20Sopenharmony_ci	}
7378c2ecf20Sopenharmony_ci
7388c2ecf20Sopenharmony_ci	err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
7398c2ecf20Sopenharmony_ci			   0, &card);
7408c2ecf20Sopenharmony_ci
7418c2ecf20Sopenharmony_ci	if (err < 0)
7428c2ecf20Sopenharmony_ci		return err;
7438c2ecf20Sopenharmony_ci
7448c2ecf20Sopenharmony_ci	chip_type = pci_id->driver_data;
7458c2ecf20Sopenharmony_ci
7468c2ecf20Sopenharmony_ci	if ((err = snd_als300_create(card, pci, chip_type, &chip)) < 0) {
7478c2ecf20Sopenharmony_ci		snd_card_free(card);
7488c2ecf20Sopenharmony_ci		return err;
7498c2ecf20Sopenharmony_ci	}
7508c2ecf20Sopenharmony_ci	card->private_data = chip;
7518c2ecf20Sopenharmony_ci
7528c2ecf20Sopenharmony_ci	strcpy(card->driver, "ALS300");
7538c2ecf20Sopenharmony_ci	if (chip->chip_type == DEVICE_ALS300_PLUS)
7548c2ecf20Sopenharmony_ci		/* don't know much about ALS300+ yet
7558c2ecf20Sopenharmony_ci		 * print revision number for now */
7568c2ecf20Sopenharmony_ci		sprintf(card->shortname, "ALS300+ (Rev. %d)", chip->revision);
7578c2ecf20Sopenharmony_ci	else
7588c2ecf20Sopenharmony_ci		sprintf(card->shortname, "ALS300 (Rev. %c)", 'A' +
7598c2ecf20Sopenharmony_ci							chip->revision - 1);
7608c2ecf20Sopenharmony_ci	sprintf(card->longname, "%s at 0x%lx irq %i",
7618c2ecf20Sopenharmony_ci				card->shortname, chip->port, chip->irq);
7628c2ecf20Sopenharmony_ci
7638c2ecf20Sopenharmony_ci	if ((err = snd_card_register(card)) < 0) {
7648c2ecf20Sopenharmony_ci		snd_card_free(card);
7658c2ecf20Sopenharmony_ci		return err;
7668c2ecf20Sopenharmony_ci	}
7678c2ecf20Sopenharmony_ci	pci_set_drvdata(pci, card);
7688c2ecf20Sopenharmony_ci	dev++;
7698c2ecf20Sopenharmony_ci	return 0;
7708c2ecf20Sopenharmony_ci}
7718c2ecf20Sopenharmony_ci
7728c2ecf20Sopenharmony_cistatic struct pci_driver als300_driver = {
7738c2ecf20Sopenharmony_ci	.name = KBUILD_MODNAME,
7748c2ecf20Sopenharmony_ci	.id_table = snd_als300_ids,
7758c2ecf20Sopenharmony_ci	.probe = snd_als300_probe,
7768c2ecf20Sopenharmony_ci	.remove = snd_als300_remove,
7778c2ecf20Sopenharmony_ci	.driver = {
7788c2ecf20Sopenharmony_ci		.pm = SND_ALS300_PM_OPS,
7798c2ecf20Sopenharmony_ci	},
7808c2ecf20Sopenharmony_ci};
7818c2ecf20Sopenharmony_ci
7828c2ecf20Sopenharmony_cimodule_pci_driver(als300_driver);
783