18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *   ALSA modem driver for VIA VT82xx (South Bridge)
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *   VT82C686A/B/C, VT8233A/C, VT8235
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci *	Copyright (c) 2000 Jaroslav Kysela <perex@perex.cz>
88c2ecf20Sopenharmony_ci *	                   Tjeerd.Mulder <Tjeerd.Mulder@fujitsu-siemens.com>
98c2ecf20Sopenharmony_ci *                    2002 Takashi Iwai <tiwai@suse.de>
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci/*
138c2ecf20Sopenharmony_ci * Changes:
148c2ecf20Sopenharmony_ci *
158c2ecf20Sopenharmony_ci * Sep. 2,  2004  Sasha Khapyorsky <sashak@alsa-project.org>
168c2ecf20Sopenharmony_ci *      Modified from original audio driver 'via82xx.c' to support AC97
178c2ecf20Sopenharmony_ci *      modems.
188c2ecf20Sopenharmony_ci */
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#include <linux/io.h>
218c2ecf20Sopenharmony_ci#include <linux/delay.h>
228c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
238c2ecf20Sopenharmony_ci#include <linux/init.h>
248c2ecf20Sopenharmony_ci#include <linux/pci.h>
258c2ecf20Sopenharmony_ci#include <linux/slab.h>
268c2ecf20Sopenharmony_ci#include <linux/module.h>
278c2ecf20Sopenharmony_ci#include <sound/core.h>
288c2ecf20Sopenharmony_ci#include <sound/pcm.h>
298c2ecf20Sopenharmony_ci#include <sound/pcm_params.h>
308c2ecf20Sopenharmony_ci#include <sound/info.h>
318c2ecf20Sopenharmony_ci#include <sound/ac97_codec.h>
328c2ecf20Sopenharmony_ci#include <sound/initval.h>
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci#if 0
358c2ecf20Sopenharmony_ci#define POINTER_DEBUG
368c2ecf20Sopenharmony_ci#endif
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ciMODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
398c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("VIA VT82xx modem");
408c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
418c2ecf20Sopenharmony_ciMODULE_SUPPORTED_DEVICE("{{VIA,VT82C686A/B/C modem,pci}}");
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cistatic int index = -2; /* Exclude the first card */
448c2ecf20Sopenharmony_cistatic char *id = SNDRV_DEFAULT_STR1;	/* ID for this card */
458c2ecf20Sopenharmony_cistatic int ac97_clock = 48000;
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_cimodule_param(index, int, 0444);
488c2ecf20Sopenharmony_ciMODULE_PARM_DESC(index, "Index value for VIA 82xx bridge.");
498c2ecf20Sopenharmony_cimodule_param(id, charp, 0444);
508c2ecf20Sopenharmony_ciMODULE_PARM_DESC(id, "ID string for VIA 82xx bridge.");
518c2ecf20Sopenharmony_cimodule_param(ac97_clock, int, 0444);
528c2ecf20Sopenharmony_ciMODULE_PARM_DESC(ac97_clock, "AC'97 codec clock (default 48000Hz).");
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci/* just for backward compatibility */
558c2ecf20Sopenharmony_cistatic bool enable;
568c2ecf20Sopenharmony_cimodule_param(enable, bool, 0444);
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci/*
608c2ecf20Sopenharmony_ci *  Direct registers
618c2ecf20Sopenharmony_ci */
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci#define VIAREG(via, x) ((via)->port + VIA_REG_##x)
648c2ecf20Sopenharmony_ci#define VIADEV_REG(viadev, x) ((viadev)->port + VIA_REG_##x)
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci/* common offsets */
678c2ecf20Sopenharmony_ci#define VIA_REG_OFFSET_STATUS		0x00	/* byte - channel status */
688c2ecf20Sopenharmony_ci#define   VIA_REG_STAT_ACTIVE		0x80	/* RO */
698c2ecf20Sopenharmony_ci#define   VIA_REG_STAT_PAUSED		0x40	/* RO */
708c2ecf20Sopenharmony_ci#define   VIA_REG_STAT_TRIGGER_QUEUED	0x08	/* RO */
718c2ecf20Sopenharmony_ci#define   VIA_REG_STAT_STOPPED		0x04	/* RWC */
728c2ecf20Sopenharmony_ci#define   VIA_REG_STAT_EOL		0x02	/* RWC */
738c2ecf20Sopenharmony_ci#define   VIA_REG_STAT_FLAG		0x01	/* RWC */
748c2ecf20Sopenharmony_ci#define VIA_REG_OFFSET_CONTROL		0x01	/* byte - channel control */
758c2ecf20Sopenharmony_ci#define   VIA_REG_CTRL_START		0x80	/* WO */
768c2ecf20Sopenharmony_ci#define   VIA_REG_CTRL_TERMINATE	0x40	/* WO */
778c2ecf20Sopenharmony_ci#define   VIA_REG_CTRL_AUTOSTART	0x20
788c2ecf20Sopenharmony_ci#define   VIA_REG_CTRL_PAUSE		0x08	/* RW */
798c2ecf20Sopenharmony_ci#define   VIA_REG_CTRL_INT_STOP		0x04
808c2ecf20Sopenharmony_ci#define   VIA_REG_CTRL_INT_EOL		0x02
818c2ecf20Sopenharmony_ci#define   VIA_REG_CTRL_INT_FLAG		0x01
828c2ecf20Sopenharmony_ci#define   VIA_REG_CTRL_RESET		0x01	/* RW - probably reset? undocumented */
838c2ecf20Sopenharmony_ci#define   VIA_REG_CTRL_INT (VIA_REG_CTRL_INT_FLAG | VIA_REG_CTRL_INT_EOL | VIA_REG_CTRL_AUTOSTART)
848c2ecf20Sopenharmony_ci#define VIA_REG_OFFSET_TYPE		0x02	/* byte - channel type (686 only) */
858c2ecf20Sopenharmony_ci#define   VIA_REG_TYPE_AUTOSTART	0x80	/* RW - autostart at EOL */
868c2ecf20Sopenharmony_ci#define   VIA_REG_TYPE_16BIT		0x20	/* RW */
878c2ecf20Sopenharmony_ci#define   VIA_REG_TYPE_STEREO		0x10	/* RW */
888c2ecf20Sopenharmony_ci#define   VIA_REG_TYPE_INT_LLINE	0x00
898c2ecf20Sopenharmony_ci#define   VIA_REG_TYPE_INT_LSAMPLE	0x04
908c2ecf20Sopenharmony_ci#define   VIA_REG_TYPE_INT_LESSONE	0x08
918c2ecf20Sopenharmony_ci#define   VIA_REG_TYPE_INT_MASK		0x0c
928c2ecf20Sopenharmony_ci#define   VIA_REG_TYPE_INT_EOL		0x02
938c2ecf20Sopenharmony_ci#define   VIA_REG_TYPE_INT_FLAG		0x01
948c2ecf20Sopenharmony_ci#define VIA_REG_OFFSET_TABLE_PTR	0x04	/* dword - channel table pointer */
958c2ecf20Sopenharmony_ci#define VIA_REG_OFFSET_CURR_PTR		0x04	/* dword - channel current pointer */
968c2ecf20Sopenharmony_ci#define VIA_REG_OFFSET_STOP_IDX		0x08	/* dword - stop index, channel type, sample rate */
978c2ecf20Sopenharmony_ci#define VIA_REG_OFFSET_CURR_COUNT	0x0c	/* dword - channel current count (24 bit) */
988c2ecf20Sopenharmony_ci#define VIA_REG_OFFSET_CURR_INDEX	0x0f	/* byte - channel current index (for via8233 only) */
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci#define DEFINE_VIA_REGSET(name,val) \
1018c2ecf20Sopenharmony_cienum {\
1028c2ecf20Sopenharmony_ci	VIA_REG_##name##_STATUS		= (val),\
1038c2ecf20Sopenharmony_ci	VIA_REG_##name##_CONTROL	= (val) + 0x01,\
1048c2ecf20Sopenharmony_ci	VIA_REG_##name##_TYPE		= (val) + 0x02,\
1058c2ecf20Sopenharmony_ci	VIA_REG_##name##_TABLE_PTR	= (val) + 0x04,\
1068c2ecf20Sopenharmony_ci	VIA_REG_##name##_CURR_PTR	= (val) + 0x04,\
1078c2ecf20Sopenharmony_ci	VIA_REG_##name##_STOP_IDX	= (val) + 0x08,\
1088c2ecf20Sopenharmony_ci	VIA_REG_##name##_CURR_COUNT	= (val) + 0x0c,\
1098c2ecf20Sopenharmony_ci}
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci/* modem block */
1128c2ecf20Sopenharmony_ciDEFINE_VIA_REGSET(MO, 0x40);
1138c2ecf20Sopenharmony_ciDEFINE_VIA_REGSET(MI, 0x50);
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci/* AC'97 */
1168c2ecf20Sopenharmony_ci#define VIA_REG_AC97			0x80	/* dword */
1178c2ecf20Sopenharmony_ci#define   VIA_REG_AC97_CODEC_ID_MASK	(3<<30)
1188c2ecf20Sopenharmony_ci#define   VIA_REG_AC97_CODEC_ID_SHIFT	30
1198c2ecf20Sopenharmony_ci#define   VIA_REG_AC97_CODEC_ID_PRIMARY	0x00
1208c2ecf20Sopenharmony_ci#define   VIA_REG_AC97_CODEC_ID_SECONDARY 0x01
1218c2ecf20Sopenharmony_ci#define   VIA_REG_AC97_SECONDARY_VALID	(1<<27)
1228c2ecf20Sopenharmony_ci#define   VIA_REG_AC97_PRIMARY_VALID	(1<<25)
1238c2ecf20Sopenharmony_ci#define   VIA_REG_AC97_BUSY		(1<<24)
1248c2ecf20Sopenharmony_ci#define   VIA_REG_AC97_READ		(1<<23)
1258c2ecf20Sopenharmony_ci#define   VIA_REG_AC97_CMD_SHIFT	16
1268c2ecf20Sopenharmony_ci#define   VIA_REG_AC97_CMD_MASK		0x7e
1278c2ecf20Sopenharmony_ci#define   VIA_REG_AC97_DATA_SHIFT	0
1288c2ecf20Sopenharmony_ci#define   VIA_REG_AC97_DATA_MASK	0xffff
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci#define VIA_REG_SGD_SHADOW		0x84	/* dword */
1318c2ecf20Sopenharmony_ci#define   VIA_REG_SGD_STAT_PB_FLAG	(1<<0)
1328c2ecf20Sopenharmony_ci#define   VIA_REG_SGD_STAT_CP_FLAG	(1<<1)
1338c2ecf20Sopenharmony_ci#define   VIA_REG_SGD_STAT_FM_FLAG	(1<<2)
1348c2ecf20Sopenharmony_ci#define   VIA_REG_SGD_STAT_PB_EOL	(1<<4)
1358c2ecf20Sopenharmony_ci#define   VIA_REG_SGD_STAT_CP_EOL	(1<<5)
1368c2ecf20Sopenharmony_ci#define   VIA_REG_SGD_STAT_FM_EOL	(1<<6)
1378c2ecf20Sopenharmony_ci#define   VIA_REG_SGD_STAT_PB_STOP	(1<<8)
1388c2ecf20Sopenharmony_ci#define   VIA_REG_SGD_STAT_CP_STOP	(1<<9)
1398c2ecf20Sopenharmony_ci#define   VIA_REG_SGD_STAT_FM_STOP	(1<<10)
1408c2ecf20Sopenharmony_ci#define   VIA_REG_SGD_STAT_PB_ACTIVE	(1<<12)
1418c2ecf20Sopenharmony_ci#define   VIA_REG_SGD_STAT_CP_ACTIVE	(1<<13)
1428c2ecf20Sopenharmony_ci#define   VIA_REG_SGD_STAT_FM_ACTIVE	(1<<14)
1438c2ecf20Sopenharmony_ci#define   VIA_REG_SGD_STAT_MR_FLAG      (1<<16)
1448c2ecf20Sopenharmony_ci#define   VIA_REG_SGD_STAT_MW_FLAG      (1<<17)
1458c2ecf20Sopenharmony_ci#define   VIA_REG_SGD_STAT_MR_EOL       (1<<20)
1468c2ecf20Sopenharmony_ci#define   VIA_REG_SGD_STAT_MW_EOL       (1<<21)
1478c2ecf20Sopenharmony_ci#define   VIA_REG_SGD_STAT_MR_STOP      (1<<24)
1488c2ecf20Sopenharmony_ci#define   VIA_REG_SGD_STAT_MW_STOP      (1<<25)
1498c2ecf20Sopenharmony_ci#define   VIA_REG_SGD_STAT_MR_ACTIVE    (1<<28)
1508c2ecf20Sopenharmony_ci#define   VIA_REG_SGD_STAT_MW_ACTIVE    (1<<29)
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci#define VIA_REG_GPI_STATUS		0x88
1538c2ecf20Sopenharmony_ci#define VIA_REG_GPI_INTR		0x8c
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci#define VIA_TBL_BIT_FLAG	0x40000000
1568c2ecf20Sopenharmony_ci#define VIA_TBL_BIT_EOL		0x80000000
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci/* pci space */
1598c2ecf20Sopenharmony_ci#define VIA_ACLINK_STAT		0x40
1608c2ecf20Sopenharmony_ci#define  VIA_ACLINK_C11_READY	0x20
1618c2ecf20Sopenharmony_ci#define  VIA_ACLINK_C10_READY	0x10
1628c2ecf20Sopenharmony_ci#define  VIA_ACLINK_C01_READY	0x04 /* secondary codec ready */
1638c2ecf20Sopenharmony_ci#define  VIA_ACLINK_LOWPOWER	0x02 /* low-power state */
1648c2ecf20Sopenharmony_ci#define  VIA_ACLINK_C00_READY	0x01 /* primary codec ready */
1658c2ecf20Sopenharmony_ci#define VIA_ACLINK_CTRL		0x41
1668c2ecf20Sopenharmony_ci#define  VIA_ACLINK_CTRL_ENABLE	0x80 /* 0: disable, 1: enable */
1678c2ecf20Sopenharmony_ci#define  VIA_ACLINK_CTRL_RESET	0x40 /* 0: assert, 1: de-assert */
1688c2ecf20Sopenharmony_ci#define  VIA_ACLINK_CTRL_SYNC	0x20 /* 0: release SYNC, 1: force SYNC hi */
1698c2ecf20Sopenharmony_ci#define  VIA_ACLINK_CTRL_SDO	0x10 /* 0: release SDO, 1: force SDO hi */
1708c2ecf20Sopenharmony_ci#define  VIA_ACLINK_CTRL_VRA	0x08 /* 0: disable VRA, 1: enable VRA */
1718c2ecf20Sopenharmony_ci#define  VIA_ACLINK_CTRL_PCM	0x04 /* 0: disable PCM, 1: enable PCM */
1728c2ecf20Sopenharmony_ci#define  VIA_ACLINK_CTRL_FM	0x02 /* via686 only */
1738c2ecf20Sopenharmony_ci#define  VIA_ACLINK_CTRL_SB	0x01 /* via686 only */
1748c2ecf20Sopenharmony_ci#define  VIA_ACLINK_CTRL_INIT	(VIA_ACLINK_CTRL_ENABLE|\
1758c2ecf20Sopenharmony_ci				 VIA_ACLINK_CTRL_RESET|\
1768c2ecf20Sopenharmony_ci				 VIA_ACLINK_CTRL_PCM)
1778c2ecf20Sopenharmony_ci#define VIA_FUNC_ENABLE		0x42
1788c2ecf20Sopenharmony_ci#define  VIA_FUNC_MIDI_PNP	0x80 /* FIXME: it's 0x40 in the datasheet! */
1798c2ecf20Sopenharmony_ci#define  VIA_FUNC_MIDI_IRQMASK	0x40 /* FIXME: not documented! */
1808c2ecf20Sopenharmony_ci#define  VIA_FUNC_RX2C_WRITE	0x20
1818c2ecf20Sopenharmony_ci#define  VIA_FUNC_SB_FIFO_EMPTY	0x10
1828c2ecf20Sopenharmony_ci#define  VIA_FUNC_ENABLE_GAME	0x08
1838c2ecf20Sopenharmony_ci#define  VIA_FUNC_ENABLE_FM	0x04
1848c2ecf20Sopenharmony_ci#define  VIA_FUNC_ENABLE_MIDI	0x02
1858c2ecf20Sopenharmony_ci#define  VIA_FUNC_ENABLE_SB	0x01
1868c2ecf20Sopenharmony_ci#define VIA_PNP_CONTROL		0x43
1878c2ecf20Sopenharmony_ci#define VIA_MC97_CTRL		0x44
1888c2ecf20Sopenharmony_ci#define  VIA_MC97_CTRL_ENABLE   0x80
1898c2ecf20Sopenharmony_ci#define  VIA_MC97_CTRL_SECONDARY 0x40
1908c2ecf20Sopenharmony_ci#define  VIA_MC97_CTRL_INIT     (VIA_MC97_CTRL_ENABLE|\
1918c2ecf20Sopenharmony_ci                                 VIA_MC97_CTRL_SECONDARY)
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci/*
1958c2ecf20Sopenharmony_ci * pcm stream
1968c2ecf20Sopenharmony_ci */
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_cistruct snd_via_sg_table {
1998c2ecf20Sopenharmony_ci	unsigned int offset;
2008c2ecf20Sopenharmony_ci	unsigned int size;
2018c2ecf20Sopenharmony_ci} ;
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci#define VIA_TABLE_SIZE	255
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_cistruct viadev {
2068c2ecf20Sopenharmony_ci	unsigned int reg_offset;
2078c2ecf20Sopenharmony_ci	unsigned long port;
2088c2ecf20Sopenharmony_ci	int direction;	/* playback = 0, capture = 1 */
2098c2ecf20Sopenharmony_ci        struct snd_pcm_substream *substream;
2108c2ecf20Sopenharmony_ci	int running;
2118c2ecf20Sopenharmony_ci	unsigned int tbl_entries; /* # descriptors */
2128c2ecf20Sopenharmony_ci	struct snd_dma_buffer table;
2138c2ecf20Sopenharmony_ci	struct snd_via_sg_table *idx_table;
2148c2ecf20Sopenharmony_ci	/* for recovery from the unexpected pointer */
2158c2ecf20Sopenharmony_ci	unsigned int lastpos;
2168c2ecf20Sopenharmony_ci	unsigned int bufsize;
2178c2ecf20Sopenharmony_ci	unsigned int bufsize2;
2188c2ecf20Sopenharmony_ci};
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_cienum { TYPE_CARD_VIA82XX_MODEM = 1 };
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci#define VIA_MAX_MODEM_DEVS	2
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_cistruct via82xx_modem {
2258c2ecf20Sopenharmony_ci	int irq;
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci	unsigned long port;
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	unsigned int intr_mask; /* SGD_SHADOW mask to check interrupts */
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci	struct pci_dev *pci;
2328c2ecf20Sopenharmony_ci	struct snd_card *card;
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci	unsigned int num_devs;
2358c2ecf20Sopenharmony_ci	unsigned int playback_devno, capture_devno;
2368c2ecf20Sopenharmony_ci	struct viadev devs[VIA_MAX_MODEM_DEVS];
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci	struct snd_pcm *pcms[2];
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	struct snd_ac97_bus *ac97_bus;
2418c2ecf20Sopenharmony_ci	struct snd_ac97 *ac97;
2428c2ecf20Sopenharmony_ci	unsigned int ac97_clock;
2438c2ecf20Sopenharmony_ci	unsigned int ac97_secondary;	/* secondary AC'97 codec is present */
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci	spinlock_t reg_lock;
2468c2ecf20Sopenharmony_ci	struct snd_info_entry *proc_entry;
2478c2ecf20Sopenharmony_ci};
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_cistatic const struct pci_device_id snd_via82xx_modem_ids[] = {
2508c2ecf20Sopenharmony_ci	{ PCI_VDEVICE(VIA, 0x3068), TYPE_CARD_VIA82XX_MODEM, },
2518c2ecf20Sopenharmony_ci	{ 0, }
2528c2ecf20Sopenharmony_ci};
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(pci, snd_via82xx_modem_ids);
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci/*
2578c2ecf20Sopenharmony_ci */
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci/*
2608c2ecf20Sopenharmony_ci * allocate and initialize the descriptor buffers
2618c2ecf20Sopenharmony_ci * periods = number of periods
2628c2ecf20Sopenharmony_ci * fragsize = period size in bytes
2638c2ecf20Sopenharmony_ci */
2648c2ecf20Sopenharmony_cistatic int build_via_table(struct viadev *dev, struct snd_pcm_substream *substream,
2658c2ecf20Sopenharmony_ci			   struct pci_dev *pci,
2668c2ecf20Sopenharmony_ci			   unsigned int periods, unsigned int fragsize)
2678c2ecf20Sopenharmony_ci{
2688c2ecf20Sopenharmony_ci	unsigned int i, idx, ofs, rest;
2698c2ecf20Sopenharmony_ci	struct via82xx_modem *chip = snd_pcm_substream_chip(substream);
2708c2ecf20Sopenharmony_ci	__le32 *pgtbl;
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci	if (dev->table.area == NULL) {
2738c2ecf20Sopenharmony_ci		/* the start of each lists must be aligned to 8 bytes,
2748c2ecf20Sopenharmony_ci		 * but the kernel pages are much bigger, so we don't care
2758c2ecf20Sopenharmony_ci		 */
2768c2ecf20Sopenharmony_ci		if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, &chip->pci->dev,
2778c2ecf20Sopenharmony_ci					PAGE_ALIGN(VIA_TABLE_SIZE * 2 * 8),
2788c2ecf20Sopenharmony_ci					&dev->table) < 0)
2798c2ecf20Sopenharmony_ci			return -ENOMEM;
2808c2ecf20Sopenharmony_ci	}
2818c2ecf20Sopenharmony_ci	if (! dev->idx_table) {
2828c2ecf20Sopenharmony_ci		dev->idx_table = kmalloc_array(VIA_TABLE_SIZE,
2838c2ecf20Sopenharmony_ci					       sizeof(*dev->idx_table),
2848c2ecf20Sopenharmony_ci					       GFP_KERNEL);
2858c2ecf20Sopenharmony_ci		if (! dev->idx_table)
2868c2ecf20Sopenharmony_ci			return -ENOMEM;
2878c2ecf20Sopenharmony_ci	}
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci	/* fill the entries */
2908c2ecf20Sopenharmony_ci	idx = 0;
2918c2ecf20Sopenharmony_ci	ofs = 0;
2928c2ecf20Sopenharmony_ci	pgtbl = (__le32 *)dev->table.area;
2938c2ecf20Sopenharmony_ci	for (i = 0; i < periods; i++) {
2948c2ecf20Sopenharmony_ci		rest = fragsize;
2958c2ecf20Sopenharmony_ci		/* fill descriptors for a period.
2968c2ecf20Sopenharmony_ci		 * a period can be split to several descriptors if it's
2978c2ecf20Sopenharmony_ci		 * over page boundary.
2988c2ecf20Sopenharmony_ci		 */
2998c2ecf20Sopenharmony_ci		do {
3008c2ecf20Sopenharmony_ci			unsigned int r;
3018c2ecf20Sopenharmony_ci			unsigned int flag;
3028c2ecf20Sopenharmony_ci			unsigned int addr;
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci			if (idx >= VIA_TABLE_SIZE) {
3058c2ecf20Sopenharmony_ci				dev_err(&pci->dev, "too much table size!\n");
3068c2ecf20Sopenharmony_ci				return -EINVAL;
3078c2ecf20Sopenharmony_ci			}
3088c2ecf20Sopenharmony_ci			addr = snd_pcm_sgbuf_get_addr(substream, ofs);
3098c2ecf20Sopenharmony_ci			pgtbl[idx << 1] = cpu_to_le32(addr);
3108c2ecf20Sopenharmony_ci			r = PAGE_SIZE - (ofs % PAGE_SIZE);
3118c2ecf20Sopenharmony_ci			if (rest < r)
3128c2ecf20Sopenharmony_ci				r = rest;
3138c2ecf20Sopenharmony_ci			rest -= r;
3148c2ecf20Sopenharmony_ci			if (! rest) {
3158c2ecf20Sopenharmony_ci				if (i == periods - 1)
3168c2ecf20Sopenharmony_ci					flag = VIA_TBL_BIT_EOL; /* buffer boundary */
3178c2ecf20Sopenharmony_ci				else
3188c2ecf20Sopenharmony_ci					flag = VIA_TBL_BIT_FLAG; /* period boundary */
3198c2ecf20Sopenharmony_ci			} else
3208c2ecf20Sopenharmony_ci				flag = 0; /* period continues to the next */
3218c2ecf20Sopenharmony_ci			/*
3228c2ecf20Sopenharmony_ci			dev_dbg(&pci->dev,
3238c2ecf20Sopenharmony_ci				"tbl %d: at %d  size %d (rest %d)\n",
3248c2ecf20Sopenharmony_ci				idx, ofs, r, rest);
3258c2ecf20Sopenharmony_ci			*/
3268c2ecf20Sopenharmony_ci			pgtbl[(idx<<1) + 1] = cpu_to_le32(r | flag);
3278c2ecf20Sopenharmony_ci			dev->idx_table[idx].offset = ofs;
3288c2ecf20Sopenharmony_ci			dev->idx_table[idx].size = r;
3298c2ecf20Sopenharmony_ci			ofs += r;
3308c2ecf20Sopenharmony_ci			idx++;
3318c2ecf20Sopenharmony_ci		} while (rest > 0);
3328c2ecf20Sopenharmony_ci	}
3338c2ecf20Sopenharmony_ci	dev->tbl_entries = idx;
3348c2ecf20Sopenharmony_ci	dev->bufsize = periods * fragsize;
3358c2ecf20Sopenharmony_ci	dev->bufsize2 = dev->bufsize / 2;
3368c2ecf20Sopenharmony_ci	return 0;
3378c2ecf20Sopenharmony_ci}
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_cistatic int clean_via_table(struct viadev *dev, struct snd_pcm_substream *substream,
3418c2ecf20Sopenharmony_ci			   struct pci_dev *pci)
3428c2ecf20Sopenharmony_ci{
3438c2ecf20Sopenharmony_ci	if (dev->table.area) {
3448c2ecf20Sopenharmony_ci		snd_dma_free_pages(&dev->table);
3458c2ecf20Sopenharmony_ci		dev->table.area = NULL;
3468c2ecf20Sopenharmony_ci	}
3478c2ecf20Sopenharmony_ci	kfree(dev->idx_table);
3488c2ecf20Sopenharmony_ci	dev->idx_table = NULL;
3498c2ecf20Sopenharmony_ci	return 0;
3508c2ecf20Sopenharmony_ci}
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_ci/*
3538c2ecf20Sopenharmony_ci *  Basic I/O
3548c2ecf20Sopenharmony_ci */
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_cistatic inline unsigned int snd_via82xx_codec_xread(struct via82xx_modem *chip)
3578c2ecf20Sopenharmony_ci{
3588c2ecf20Sopenharmony_ci	return inl(VIAREG(chip, AC97));
3598c2ecf20Sopenharmony_ci}
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_cistatic inline void snd_via82xx_codec_xwrite(struct via82xx_modem *chip, unsigned int val)
3628c2ecf20Sopenharmony_ci{
3638c2ecf20Sopenharmony_ci	outl(val, VIAREG(chip, AC97));
3648c2ecf20Sopenharmony_ci}
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_cistatic int snd_via82xx_codec_ready(struct via82xx_modem *chip, int secondary)
3678c2ecf20Sopenharmony_ci{
3688c2ecf20Sopenharmony_ci	unsigned int timeout = 1000;	/* 1ms */
3698c2ecf20Sopenharmony_ci	unsigned int val;
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_ci	while (timeout-- > 0) {
3728c2ecf20Sopenharmony_ci		udelay(1);
3738c2ecf20Sopenharmony_ci		if (!((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_BUSY))
3748c2ecf20Sopenharmony_ci			return val & 0xffff;
3758c2ecf20Sopenharmony_ci	}
3768c2ecf20Sopenharmony_ci	dev_err(chip->card->dev, "codec_ready: codec %i is not ready [0x%x]\n",
3778c2ecf20Sopenharmony_ci		   secondary, snd_via82xx_codec_xread(chip));
3788c2ecf20Sopenharmony_ci	return -EIO;
3798c2ecf20Sopenharmony_ci}
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_cistatic int snd_via82xx_codec_valid(struct via82xx_modem *chip, int secondary)
3828c2ecf20Sopenharmony_ci{
3838c2ecf20Sopenharmony_ci	unsigned int timeout = 1000;	/* 1ms */
3848c2ecf20Sopenharmony_ci	unsigned int val, val1;
3858c2ecf20Sopenharmony_ci	unsigned int stat = !secondary ? VIA_REG_AC97_PRIMARY_VALID :
3868c2ecf20Sopenharmony_ci					 VIA_REG_AC97_SECONDARY_VALID;
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci	while (timeout-- > 0) {
3898c2ecf20Sopenharmony_ci		val = snd_via82xx_codec_xread(chip);
3908c2ecf20Sopenharmony_ci		val1 = val & (VIA_REG_AC97_BUSY | stat);
3918c2ecf20Sopenharmony_ci		if (val1 == stat)
3928c2ecf20Sopenharmony_ci			return val & 0xffff;
3938c2ecf20Sopenharmony_ci		udelay(1);
3948c2ecf20Sopenharmony_ci	}
3958c2ecf20Sopenharmony_ci	return -EIO;
3968c2ecf20Sopenharmony_ci}
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_cistatic void snd_via82xx_codec_wait(struct snd_ac97 *ac97)
3998c2ecf20Sopenharmony_ci{
4008c2ecf20Sopenharmony_ci	struct via82xx_modem *chip = ac97->private_data;
4018c2ecf20Sopenharmony_ci	__always_unused int err;
4028c2ecf20Sopenharmony_ci	err = snd_via82xx_codec_ready(chip, ac97->num);
4038c2ecf20Sopenharmony_ci	/* here we need to wait fairly for long time.. */
4048c2ecf20Sopenharmony_ci	msleep(500);
4058c2ecf20Sopenharmony_ci}
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_cistatic void snd_via82xx_codec_write(struct snd_ac97 *ac97,
4088c2ecf20Sopenharmony_ci				    unsigned short reg,
4098c2ecf20Sopenharmony_ci				    unsigned short val)
4108c2ecf20Sopenharmony_ci{
4118c2ecf20Sopenharmony_ci	struct via82xx_modem *chip = ac97->private_data;
4128c2ecf20Sopenharmony_ci	unsigned int xval;
4138c2ecf20Sopenharmony_ci	if(reg == AC97_GPIO_STATUS) {
4148c2ecf20Sopenharmony_ci		outl(val, VIAREG(chip, GPI_STATUS));
4158c2ecf20Sopenharmony_ci		return;
4168c2ecf20Sopenharmony_ci	}
4178c2ecf20Sopenharmony_ci	xval = !ac97->num ? VIA_REG_AC97_CODEC_ID_PRIMARY : VIA_REG_AC97_CODEC_ID_SECONDARY;
4188c2ecf20Sopenharmony_ci	xval <<= VIA_REG_AC97_CODEC_ID_SHIFT;
4198c2ecf20Sopenharmony_ci	xval |= reg << VIA_REG_AC97_CMD_SHIFT;
4208c2ecf20Sopenharmony_ci	xval |= val << VIA_REG_AC97_DATA_SHIFT;
4218c2ecf20Sopenharmony_ci	snd_via82xx_codec_xwrite(chip, xval);
4228c2ecf20Sopenharmony_ci	snd_via82xx_codec_ready(chip, ac97->num);
4238c2ecf20Sopenharmony_ci}
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_cistatic unsigned short snd_via82xx_codec_read(struct snd_ac97 *ac97, unsigned short reg)
4268c2ecf20Sopenharmony_ci{
4278c2ecf20Sopenharmony_ci	struct via82xx_modem *chip = ac97->private_data;
4288c2ecf20Sopenharmony_ci	unsigned int xval, val = 0xffff;
4298c2ecf20Sopenharmony_ci	int again = 0;
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_ci	xval = ac97->num << VIA_REG_AC97_CODEC_ID_SHIFT;
4328c2ecf20Sopenharmony_ci	xval |= ac97->num ? VIA_REG_AC97_SECONDARY_VALID : VIA_REG_AC97_PRIMARY_VALID;
4338c2ecf20Sopenharmony_ci	xval |= VIA_REG_AC97_READ;
4348c2ecf20Sopenharmony_ci	xval |= (reg & 0x7f) << VIA_REG_AC97_CMD_SHIFT;
4358c2ecf20Sopenharmony_ci      	while (1) {
4368c2ecf20Sopenharmony_ci      		if (again++ > 3) {
4378c2ecf20Sopenharmony_ci			dev_err(chip->card->dev,
4388c2ecf20Sopenharmony_ci				"codec_read: codec %i is not valid [0x%x]\n",
4398c2ecf20Sopenharmony_ci				   ac97->num, snd_via82xx_codec_xread(chip));
4408c2ecf20Sopenharmony_ci		      	return 0xffff;
4418c2ecf20Sopenharmony_ci		}
4428c2ecf20Sopenharmony_ci		snd_via82xx_codec_xwrite(chip, xval);
4438c2ecf20Sopenharmony_ci		udelay (20);
4448c2ecf20Sopenharmony_ci		if (snd_via82xx_codec_valid(chip, ac97->num) >= 0) {
4458c2ecf20Sopenharmony_ci			udelay(25);
4468c2ecf20Sopenharmony_ci			val = snd_via82xx_codec_xread(chip);
4478c2ecf20Sopenharmony_ci			break;
4488c2ecf20Sopenharmony_ci		}
4498c2ecf20Sopenharmony_ci	}
4508c2ecf20Sopenharmony_ci	return val & 0xffff;
4518c2ecf20Sopenharmony_ci}
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_cistatic void snd_via82xx_channel_reset(struct via82xx_modem *chip, struct viadev *viadev)
4548c2ecf20Sopenharmony_ci{
4558c2ecf20Sopenharmony_ci	outb(VIA_REG_CTRL_PAUSE | VIA_REG_CTRL_TERMINATE | VIA_REG_CTRL_RESET,
4568c2ecf20Sopenharmony_ci	     VIADEV_REG(viadev, OFFSET_CONTROL));
4578c2ecf20Sopenharmony_ci	inb(VIADEV_REG(viadev, OFFSET_CONTROL));
4588c2ecf20Sopenharmony_ci	udelay(50);
4598c2ecf20Sopenharmony_ci	/* disable interrupts */
4608c2ecf20Sopenharmony_ci	outb(0x00, VIADEV_REG(viadev, OFFSET_CONTROL));
4618c2ecf20Sopenharmony_ci	/* clear interrupts */
4628c2ecf20Sopenharmony_ci	outb(0x03, VIADEV_REG(viadev, OFFSET_STATUS));
4638c2ecf20Sopenharmony_ci	outb(0x00, VIADEV_REG(viadev, OFFSET_TYPE)); /* for via686 */
4648c2ecf20Sopenharmony_ci	// outl(0, VIADEV_REG(viadev, OFFSET_CURR_PTR));
4658c2ecf20Sopenharmony_ci	viadev->lastpos = 0;
4668c2ecf20Sopenharmony_ci}
4678c2ecf20Sopenharmony_ci
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_ci/*
4708c2ecf20Sopenharmony_ci *  Interrupt handler
4718c2ecf20Sopenharmony_ci */
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_cistatic irqreturn_t snd_via82xx_interrupt(int irq, void *dev_id)
4748c2ecf20Sopenharmony_ci{
4758c2ecf20Sopenharmony_ci	struct via82xx_modem *chip = dev_id;
4768c2ecf20Sopenharmony_ci	unsigned int status;
4778c2ecf20Sopenharmony_ci	unsigned int i;
4788c2ecf20Sopenharmony_ci
4798c2ecf20Sopenharmony_ci	status = inl(VIAREG(chip, SGD_SHADOW));
4808c2ecf20Sopenharmony_ci	if (! (status & chip->intr_mask)) {
4818c2ecf20Sopenharmony_ci		return IRQ_NONE;
4828c2ecf20Sopenharmony_ci	}
4838c2ecf20Sopenharmony_ci// _skip_sgd:
4848c2ecf20Sopenharmony_ci
4858c2ecf20Sopenharmony_ci	/* check status for each stream */
4868c2ecf20Sopenharmony_ci	spin_lock(&chip->reg_lock);
4878c2ecf20Sopenharmony_ci	for (i = 0; i < chip->num_devs; i++) {
4888c2ecf20Sopenharmony_ci		struct viadev *viadev = &chip->devs[i];
4898c2ecf20Sopenharmony_ci		unsigned char c_status = inb(VIADEV_REG(viadev, OFFSET_STATUS));
4908c2ecf20Sopenharmony_ci		c_status &= (VIA_REG_STAT_EOL|VIA_REG_STAT_FLAG|VIA_REG_STAT_STOPPED);
4918c2ecf20Sopenharmony_ci		if (! c_status)
4928c2ecf20Sopenharmony_ci			continue;
4938c2ecf20Sopenharmony_ci		if (viadev->substream && viadev->running) {
4948c2ecf20Sopenharmony_ci			spin_unlock(&chip->reg_lock);
4958c2ecf20Sopenharmony_ci			snd_pcm_period_elapsed(viadev->substream);
4968c2ecf20Sopenharmony_ci			spin_lock(&chip->reg_lock);
4978c2ecf20Sopenharmony_ci		}
4988c2ecf20Sopenharmony_ci		outb(c_status, VIADEV_REG(viadev, OFFSET_STATUS)); /* ack */
4998c2ecf20Sopenharmony_ci	}
5008c2ecf20Sopenharmony_ci	spin_unlock(&chip->reg_lock);
5018c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
5028c2ecf20Sopenharmony_ci}
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_ci/*
5058c2ecf20Sopenharmony_ci *  PCM callbacks
5068c2ecf20Sopenharmony_ci */
5078c2ecf20Sopenharmony_ci
5088c2ecf20Sopenharmony_ci/*
5098c2ecf20Sopenharmony_ci * trigger callback
5108c2ecf20Sopenharmony_ci */
5118c2ecf20Sopenharmony_cistatic int snd_via82xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
5128c2ecf20Sopenharmony_ci{
5138c2ecf20Sopenharmony_ci	struct via82xx_modem *chip = snd_pcm_substream_chip(substream);
5148c2ecf20Sopenharmony_ci	struct viadev *viadev = substream->runtime->private_data;
5158c2ecf20Sopenharmony_ci	unsigned char val = 0;
5168c2ecf20Sopenharmony_ci
5178c2ecf20Sopenharmony_ci	switch (cmd) {
5188c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_START:
5198c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_SUSPEND:
5208c2ecf20Sopenharmony_ci		val |= VIA_REG_CTRL_START;
5218c2ecf20Sopenharmony_ci		viadev->running = 1;
5228c2ecf20Sopenharmony_ci		break;
5238c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_STOP:
5248c2ecf20Sopenharmony_ci		val = VIA_REG_CTRL_TERMINATE;
5258c2ecf20Sopenharmony_ci		viadev->running = 0;
5268c2ecf20Sopenharmony_ci		break;
5278c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
5288c2ecf20Sopenharmony_ci		val |= VIA_REG_CTRL_PAUSE;
5298c2ecf20Sopenharmony_ci		viadev->running = 0;
5308c2ecf20Sopenharmony_ci		break;
5318c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
5328c2ecf20Sopenharmony_ci		viadev->running = 1;
5338c2ecf20Sopenharmony_ci		break;
5348c2ecf20Sopenharmony_ci	default:
5358c2ecf20Sopenharmony_ci		return -EINVAL;
5368c2ecf20Sopenharmony_ci	}
5378c2ecf20Sopenharmony_ci	outb(val, VIADEV_REG(viadev, OFFSET_CONTROL));
5388c2ecf20Sopenharmony_ci	if (cmd == SNDRV_PCM_TRIGGER_STOP)
5398c2ecf20Sopenharmony_ci		snd_via82xx_channel_reset(chip, viadev);
5408c2ecf20Sopenharmony_ci	return 0;
5418c2ecf20Sopenharmony_ci}
5428c2ecf20Sopenharmony_ci
5438c2ecf20Sopenharmony_ci/*
5448c2ecf20Sopenharmony_ci * pointer callbacks
5458c2ecf20Sopenharmony_ci */
5468c2ecf20Sopenharmony_ci
5478c2ecf20Sopenharmony_ci/*
5488c2ecf20Sopenharmony_ci * calculate the linear position at the given sg-buffer index and the rest count
5498c2ecf20Sopenharmony_ci */
5508c2ecf20Sopenharmony_ci
5518c2ecf20Sopenharmony_ci#define check_invalid_pos(viadev,pos) \
5528c2ecf20Sopenharmony_ci	((pos) < viadev->lastpos && ((pos) >= viadev->bufsize2 ||\
5538c2ecf20Sopenharmony_ci				     viadev->lastpos < viadev->bufsize2))
5548c2ecf20Sopenharmony_ci
5558c2ecf20Sopenharmony_cistatic inline unsigned int calc_linear_pos(struct via82xx_modem *chip,
5568c2ecf20Sopenharmony_ci					   struct viadev *viadev,
5578c2ecf20Sopenharmony_ci					   unsigned int idx,
5588c2ecf20Sopenharmony_ci					   unsigned int count)
5598c2ecf20Sopenharmony_ci{
5608c2ecf20Sopenharmony_ci	unsigned int size, res;
5618c2ecf20Sopenharmony_ci
5628c2ecf20Sopenharmony_ci	size = viadev->idx_table[idx].size;
5638c2ecf20Sopenharmony_ci	res = viadev->idx_table[idx].offset + size - count;
5648c2ecf20Sopenharmony_ci
5658c2ecf20Sopenharmony_ci	/* check the validity of the calculated position */
5668c2ecf20Sopenharmony_ci	if (size < count) {
5678c2ecf20Sopenharmony_ci		dev_err(chip->card->dev,
5688c2ecf20Sopenharmony_ci			"invalid via82xx_cur_ptr (size = %d, count = %d)\n",
5698c2ecf20Sopenharmony_ci			   (int)size, (int)count);
5708c2ecf20Sopenharmony_ci		res = viadev->lastpos;
5718c2ecf20Sopenharmony_ci	} else if (check_invalid_pos(viadev, res)) {
5728c2ecf20Sopenharmony_ci#ifdef POINTER_DEBUG
5738c2ecf20Sopenharmony_ci		dev_dbg(chip->card->dev,
5748c2ecf20Sopenharmony_ci			"fail: idx = %i/%i, lastpos = 0x%x, bufsize2 = 0x%x, offsize = 0x%x, size = 0x%x, count = 0x%x\n",
5758c2ecf20Sopenharmony_ci			idx, viadev->tbl_entries, viadev->lastpos,
5768c2ecf20Sopenharmony_ci		       viadev->bufsize2, viadev->idx_table[idx].offset,
5778c2ecf20Sopenharmony_ci		       viadev->idx_table[idx].size, count);
5788c2ecf20Sopenharmony_ci#endif
5798c2ecf20Sopenharmony_ci		if (count && size < count) {
5808c2ecf20Sopenharmony_ci			dev_dbg(chip->card->dev,
5818c2ecf20Sopenharmony_ci				"invalid via82xx_cur_ptr, using last valid pointer\n");
5828c2ecf20Sopenharmony_ci			res = viadev->lastpos;
5838c2ecf20Sopenharmony_ci		} else {
5848c2ecf20Sopenharmony_ci			if (! count)
5858c2ecf20Sopenharmony_ci				/* bogus count 0 on the DMA boundary? */
5868c2ecf20Sopenharmony_ci				res = viadev->idx_table[idx].offset;
5878c2ecf20Sopenharmony_ci			else
5888c2ecf20Sopenharmony_ci				/* count register returns full size
5898c2ecf20Sopenharmony_ci				 * when end of buffer is reached
5908c2ecf20Sopenharmony_ci				 */
5918c2ecf20Sopenharmony_ci				res = viadev->idx_table[idx].offset + size;
5928c2ecf20Sopenharmony_ci			if (check_invalid_pos(viadev, res)) {
5938c2ecf20Sopenharmony_ci				dev_dbg(chip->card->dev,
5948c2ecf20Sopenharmony_ci					"invalid via82xx_cur_ptr (2), using last valid pointer\n");
5958c2ecf20Sopenharmony_ci				res = viadev->lastpos;
5968c2ecf20Sopenharmony_ci			}
5978c2ecf20Sopenharmony_ci		}
5988c2ecf20Sopenharmony_ci	}
5998c2ecf20Sopenharmony_ci	viadev->lastpos = res; /* remember the last position */
6008c2ecf20Sopenharmony_ci	if (res >= viadev->bufsize)
6018c2ecf20Sopenharmony_ci		res -= viadev->bufsize;
6028c2ecf20Sopenharmony_ci	return res;
6038c2ecf20Sopenharmony_ci}
6048c2ecf20Sopenharmony_ci
6058c2ecf20Sopenharmony_ci/*
6068c2ecf20Sopenharmony_ci * get the current pointer on via686
6078c2ecf20Sopenharmony_ci */
6088c2ecf20Sopenharmony_cistatic snd_pcm_uframes_t snd_via686_pcm_pointer(struct snd_pcm_substream *substream)
6098c2ecf20Sopenharmony_ci{
6108c2ecf20Sopenharmony_ci	struct via82xx_modem *chip = snd_pcm_substream_chip(substream);
6118c2ecf20Sopenharmony_ci	struct viadev *viadev = substream->runtime->private_data;
6128c2ecf20Sopenharmony_ci	unsigned int idx, ptr, count, res;
6138c2ecf20Sopenharmony_ci
6148c2ecf20Sopenharmony_ci	if (snd_BUG_ON(!viadev->tbl_entries))
6158c2ecf20Sopenharmony_ci		return 0;
6168c2ecf20Sopenharmony_ci	if (!(inb(VIADEV_REG(viadev, OFFSET_STATUS)) & VIA_REG_STAT_ACTIVE))
6178c2ecf20Sopenharmony_ci		return 0;
6188c2ecf20Sopenharmony_ci
6198c2ecf20Sopenharmony_ci	spin_lock(&chip->reg_lock);
6208c2ecf20Sopenharmony_ci	count = inl(VIADEV_REG(viadev, OFFSET_CURR_COUNT)) & 0xffffff;
6218c2ecf20Sopenharmony_ci	/* The via686a does not have the current index register,
6228c2ecf20Sopenharmony_ci	 * so we need to calculate the index from CURR_PTR.
6238c2ecf20Sopenharmony_ci	 */
6248c2ecf20Sopenharmony_ci	ptr = inl(VIADEV_REG(viadev, OFFSET_CURR_PTR));
6258c2ecf20Sopenharmony_ci	if (ptr <= (unsigned int)viadev->table.addr)
6268c2ecf20Sopenharmony_ci		idx = 0;
6278c2ecf20Sopenharmony_ci	else /* CURR_PTR holds the address + 8 */
6288c2ecf20Sopenharmony_ci		idx = ((ptr - (unsigned int)viadev->table.addr) / 8 - 1) %
6298c2ecf20Sopenharmony_ci			viadev->tbl_entries;
6308c2ecf20Sopenharmony_ci	res = calc_linear_pos(chip, viadev, idx, count);
6318c2ecf20Sopenharmony_ci	spin_unlock(&chip->reg_lock);
6328c2ecf20Sopenharmony_ci
6338c2ecf20Sopenharmony_ci	return bytes_to_frames(substream->runtime, res);
6348c2ecf20Sopenharmony_ci}
6358c2ecf20Sopenharmony_ci
6368c2ecf20Sopenharmony_ci/*
6378c2ecf20Sopenharmony_ci * hw_params callback:
6388c2ecf20Sopenharmony_ci * allocate the buffer and build up the buffer description table
6398c2ecf20Sopenharmony_ci */
6408c2ecf20Sopenharmony_cistatic int snd_via82xx_hw_params(struct snd_pcm_substream *substream,
6418c2ecf20Sopenharmony_ci				 struct snd_pcm_hw_params *hw_params)
6428c2ecf20Sopenharmony_ci{
6438c2ecf20Sopenharmony_ci	struct via82xx_modem *chip = snd_pcm_substream_chip(substream);
6448c2ecf20Sopenharmony_ci	struct viadev *viadev = substream->runtime->private_data;
6458c2ecf20Sopenharmony_ci	int err;
6468c2ecf20Sopenharmony_ci
6478c2ecf20Sopenharmony_ci	err = build_via_table(viadev, substream, chip->pci,
6488c2ecf20Sopenharmony_ci			      params_periods(hw_params),
6498c2ecf20Sopenharmony_ci			      params_period_bytes(hw_params));
6508c2ecf20Sopenharmony_ci	if (err < 0)
6518c2ecf20Sopenharmony_ci		return err;
6528c2ecf20Sopenharmony_ci
6538c2ecf20Sopenharmony_ci	snd_ac97_write(chip->ac97, AC97_LINE1_RATE, params_rate(hw_params));
6548c2ecf20Sopenharmony_ci	snd_ac97_write(chip->ac97, AC97_LINE1_LEVEL, 0);
6558c2ecf20Sopenharmony_ci
6568c2ecf20Sopenharmony_ci	return 0;
6578c2ecf20Sopenharmony_ci}
6588c2ecf20Sopenharmony_ci
6598c2ecf20Sopenharmony_ci/*
6608c2ecf20Sopenharmony_ci * hw_free callback:
6618c2ecf20Sopenharmony_ci * clean up the buffer description table and release the buffer
6628c2ecf20Sopenharmony_ci */
6638c2ecf20Sopenharmony_cistatic int snd_via82xx_hw_free(struct snd_pcm_substream *substream)
6648c2ecf20Sopenharmony_ci{
6658c2ecf20Sopenharmony_ci	struct via82xx_modem *chip = snd_pcm_substream_chip(substream);
6668c2ecf20Sopenharmony_ci	struct viadev *viadev = substream->runtime->private_data;
6678c2ecf20Sopenharmony_ci
6688c2ecf20Sopenharmony_ci	clean_via_table(viadev, substream, chip->pci);
6698c2ecf20Sopenharmony_ci	return 0;
6708c2ecf20Sopenharmony_ci}
6718c2ecf20Sopenharmony_ci
6728c2ecf20Sopenharmony_ci
6738c2ecf20Sopenharmony_ci/*
6748c2ecf20Sopenharmony_ci * set up the table pointer
6758c2ecf20Sopenharmony_ci */
6768c2ecf20Sopenharmony_cistatic void snd_via82xx_set_table_ptr(struct via82xx_modem *chip, struct viadev *viadev)
6778c2ecf20Sopenharmony_ci{
6788c2ecf20Sopenharmony_ci	snd_via82xx_codec_ready(chip, chip->ac97_secondary);
6798c2ecf20Sopenharmony_ci	outl((u32)viadev->table.addr, VIADEV_REG(viadev, OFFSET_TABLE_PTR));
6808c2ecf20Sopenharmony_ci	udelay(20);
6818c2ecf20Sopenharmony_ci	snd_via82xx_codec_ready(chip, chip->ac97_secondary);
6828c2ecf20Sopenharmony_ci}
6838c2ecf20Sopenharmony_ci
6848c2ecf20Sopenharmony_ci/*
6858c2ecf20Sopenharmony_ci * prepare callback for playback and capture
6868c2ecf20Sopenharmony_ci */
6878c2ecf20Sopenharmony_cistatic int snd_via82xx_pcm_prepare(struct snd_pcm_substream *substream)
6888c2ecf20Sopenharmony_ci{
6898c2ecf20Sopenharmony_ci	struct via82xx_modem *chip = snd_pcm_substream_chip(substream);
6908c2ecf20Sopenharmony_ci	struct viadev *viadev = substream->runtime->private_data;
6918c2ecf20Sopenharmony_ci
6928c2ecf20Sopenharmony_ci	snd_via82xx_channel_reset(chip, viadev);
6938c2ecf20Sopenharmony_ci	/* this must be set after channel_reset */
6948c2ecf20Sopenharmony_ci	snd_via82xx_set_table_ptr(chip, viadev);
6958c2ecf20Sopenharmony_ci	outb(VIA_REG_TYPE_AUTOSTART|VIA_REG_TYPE_INT_EOL|VIA_REG_TYPE_INT_FLAG,
6968c2ecf20Sopenharmony_ci	     VIADEV_REG(viadev, OFFSET_TYPE));
6978c2ecf20Sopenharmony_ci	return 0;
6988c2ecf20Sopenharmony_ci}
6998c2ecf20Sopenharmony_ci
7008c2ecf20Sopenharmony_ci/*
7018c2ecf20Sopenharmony_ci * pcm hardware definition, identical for both playback and capture
7028c2ecf20Sopenharmony_ci */
7038c2ecf20Sopenharmony_cistatic const struct snd_pcm_hardware snd_via82xx_hw =
7048c2ecf20Sopenharmony_ci{
7058c2ecf20Sopenharmony_ci	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
7068c2ecf20Sopenharmony_ci				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
7078c2ecf20Sopenharmony_ci				 SNDRV_PCM_INFO_MMAP_VALID |
7088c2ecf20Sopenharmony_ci				 /* SNDRV_PCM_INFO_RESUME | */
7098c2ecf20Sopenharmony_ci				 SNDRV_PCM_INFO_PAUSE),
7108c2ecf20Sopenharmony_ci	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
7118c2ecf20Sopenharmony_ci	.rates =		SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_KNOT,
7128c2ecf20Sopenharmony_ci	.rate_min =		8000,
7138c2ecf20Sopenharmony_ci	.rate_max =		16000,
7148c2ecf20Sopenharmony_ci	.channels_min =		1,
7158c2ecf20Sopenharmony_ci	.channels_max =		1,
7168c2ecf20Sopenharmony_ci	.buffer_bytes_max =	128 * 1024,
7178c2ecf20Sopenharmony_ci	.period_bytes_min =	32,
7188c2ecf20Sopenharmony_ci	.period_bytes_max =	128 * 1024,
7198c2ecf20Sopenharmony_ci	.periods_min =		2,
7208c2ecf20Sopenharmony_ci	.periods_max =		VIA_TABLE_SIZE / 2,
7218c2ecf20Sopenharmony_ci	.fifo_size =		0,
7228c2ecf20Sopenharmony_ci};
7238c2ecf20Sopenharmony_ci
7248c2ecf20Sopenharmony_ci
7258c2ecf20Sopenharmony_ci/*
7268c2ecf20Sopenharmony_ci * open callback skeleton
7278c2ecf20Sopenharmony_ci */
7288c2ecf20Sopenharmony_cistatic int snd_via82xx_modem_pcm_open(struct via82xx_modem *chip, struct viadev *viadev,
7298c2ecf20Sopenharmony_ci				      struct snd_pcm_substream *substream)
7308c2ecf20Sopenharmony_ci{
7318c2ecf20Sopenharmony_ci	struct snd_pcm_runtime *runtime = substream->runtime;
7328c2ecf20Sopenharmony_ci	int err;
7338c2ecf20Sopenharmony_ci	static const unsigned int rates[] = { 8000,  9600, 12000, 16000 };
7348c2ecf20Sopenharmony_ci	static const struct snd_pcm_hw_constraint_list hw_constraints_rates = {
7358c2ecf20Sopenharmony_ci                .count = ARRAY_SIZE(rates),
7368c2ecf20Sopenharmony_ci                .list = rates,
7378c2ecf20Sopenharmony_ci                .mask = 0,
7388c2ecf20Sopenharmony_ci        };
7398c2ecf20Sopenharmony_ci
7408c2ecf20Sopenharmony_ci	runtime->hw = snd_via82xx_hw;
7418c2ecf20Sopenharmony_ci
7428c2ecf20Sopenharmony_ci        if ((err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
7438c2ecf20Sopenharmony_ci					      &hw_constraints_rates)) < 0)
7448c2ecf20Sopenharmony_ci                return err;
7458c2ecf20Sopenharmony_ci
7468c2ecf20Sopenharmony_ci	/* we may remove following constaint when we modify table entries
7478c2ecf20Sopenharmony_ci	   in interrupt */
7488c2ecf20Sopenharmony_ci	if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
7498c2ecf20Sopenharmony_ci		return err;
7508c2ecf20Sopenharmony_ci
7518c2ecf20Sopenharmony_ci	runtime->private_data = viadev;
7528c2ecf20Sopenharmony_ci	viadev->substream = substream;
7538c2ecf20Sopenharmony_ci
7548c2ecf20Sopenharmony_ci	return 0;
7558c2ecf20Sopenharmony_ci}
7568c2ecf20Sopenharmony_ci
7578c2ecf20Sopenharmony_ci
7588c2ecf20Sopenharmony_ci/*
7598c2ecf20Sopenharmony_ci * open callback for playback
7608c2ecf20Sopenharmony_ci */
7618c2ecf20Sopenharmony_cistatic int snd_via82xx_playback_open(struct snd_pcm_substream *substream)
7628c2ecf20Sopenharmony_ci{
7638c2ecf20Sopenharmony_ci	struct via82xx_modem *chip = snd_pcm_substream_chip(substream);
7648c2ecf20Sopenharmony_ci	struct viadev *viadev = &chip->devs[chip->playback_devno + substream->number];
7658c2ecf20Sopenharmony_ci
7668c2ecf20Sopenharmony_ci	return snd_via82xx_modem_pcm_open(chip, viadev, substream);
7678c2ecf20Sopenharmony_ci}
7688c2ecf20Sopenharmony_ci
7698c2ecf20Sopenharmony_ci/*
7708c2ecf20Sopenharmony_ci * open callback for capture
7718c2ecf20Sopenharmony_ci */
7728c2ecf20Sopenharmony_cistatic int snd_via82xx_capture_open(struct snd_pcm_substream *substream)
7738c2ecf20Sopenharmony_ci{
7748c2ecf20Sopenharmony_ci	struct via82xx_modem *chip = snd_pcm_substream_chip(substream);
7758c2ecf20Sopenharmony_ci	struct viadev *viadev = &chip->devs[chip->capture_devno + substream->pcm->device];
7768c2ecf20Sopenharmony_ci
7778c2ecf20Sopenharmony_ci	return snd_via82xx_modem_pcm_open(chip, viadev, substream);
7788c2ecf20Sopenharmony_ci}
7798c2ecf20Sopenharmony_ci
7808c2ecf20Sopenharmony_ci/*
7818c2ecf20Sopenharmony_ci * close callback
7828c2ecf20Sopenharmony_ci */
7838c2ecf20Sopenharmony_cistatic int snd_via82xx_pcm_close(struct snd_pcm_substream *substream)
7848c2ecf20Sopenharmony_ci{
7858c2ecf20Sopenharmony_ci	struct viadev *viadev = substream->runtime->private_data;
7868c2ecf20Sopenharmony_ci
7878c2ecf20Sopenharmony_ci	viadev->substream = NULL;
7888c2ecf20Sopenharmony_ci	return 0;
7898c2ecf20Sopenharmony_ci}
7908c2ecf20Sopenharmony_ci
7918c2ecf20Sopenharmony_ci
7928c2ecf20Sopenharmony_ci/* via686 playback callbacks */
7938c2ecf20Sopenharmony_cistatic const struct snd_pcm_ops snd_via686_playback_ops = {
7948c2ecf20Sopenharmony_ci	.open =		snd_via82xx_playback_open,
7958c2ecf20Sopenharmony_ci	.close =	snd_via82xx_pcm_close,
7968c2ecf20Sopenharmony_ci	.hw_params =	snd_via82xx_hw_params,
7978c2ecf20Sopenharmony_ci	.hw_free =	snd_via82xx_hw_free,
7988c2ecf20Sopenharmony_ci	.prepare =	snd_via82xx_pcm_prepare,
7998c2ecf20Sopenharmony_ci	.trigger =	snd_via82xx_pcm_trigger,
8008c2ecf20Sopenharmony_ci	.pointer =	snd_via686_pcm_pointer,
8018c2ecf20Sopenharmony_ci};
8028c2ecf20Sopenharmony_ci
8038c2ecf20Sopenharmony_ci/* via686 capture callbacks */
8048c2ecf20Sopenharmony_cistatic const struct snd_pcm_ops snd_via686_capture_ops = {
8058c2ecf20Sopenharmony_ci	.open =		snd_via82xx_capture_open,
8068c2ecf20Sopenharmony_ci	.close =	snd_via82xx_pcm_close,
8078c2ecf20Sopenharmony_ci	.hw_params =	snd_via82xx_hw_params,
8088c2ecf20Sopenharmony_ci	.hw_free =	snd_via82xx_hw_free,
8098c2ecf20Sopenharmony_ci	.prepare =	snd_via82xx_pcm_prepare,
8108c2ecf20Sopenharmony_ci	.trigger =	snd_via82xx_pcm_trigger,
8118c2ecf20Sopenharmony_ci	.pointer =	snd_via686_pcm_pointer,
8128c2ecf20Sopenharmony_ci};
8138c2ecf20Sopenharmony_ci
8148c2ecf20Sopenharmony_ci
8158c2ecf20Sopenharmony_cistatic void init_viadev(struct via82xx_modem *chip, int idx, unsigned int reg_offset,
8168c2ecf20Sopenharmony_ci			int direction)
8178c2ecf20Sopenharmony_ci{
8188c2ecf20Sopenharmony_ci	chip->devs[idx].reg_offset = reg_offset;
8198c2ecf20Sopenharmony_ci	chip->devs[idx].direction = direction;
8208c2ecf20Sopenharmony_ci	chip->devs[idx].port = chip->port + reg_offset;
8218c2ecf20Sopenharmony_ci}
8228c2ecf20Sopenharmony_ci
8238c2ecf20Sopenharmony_ci/*
8248c2ecf20Sopenharmony_ci * create a pcm instance for via686a/b
8258c2ecf20Sopenharmony_ci */
8268c2ecf20Sopenharmony_cistatic int snd_via686_pcm_new(struct via82xx_modem *chip)
8278c2ecf20Sopenharmony_ci{
8288c2ecf20Sopenharmony_ci	struct snd_pcm *pcm;
8298c2ecf20Sopenharmony_ci	int err;
8308c2ecf20Sopenharmony_ci
8318c2ecf20Sopenharmony_ci	chip->playback_devno = 0;
8328c2ecf20Sopenharmony_ci	chip->capture_devno = 1;
8338c2ecf20Sopenharmony_ci	chip->num_devs = 2;
8348c2ecf20Sopenharmony_ci	chip->intr_mask = 0x330000; /* FLAGS | EOL for MR, MW */
8358c2ecf20Sopenharmony_ci
8368c2ecf20Sopenharmony_ci	err = snd_pcm_new(chip->card, chip->card->shortname, 0, 1, 1, &pcm);
8378c2ecf20Sopenharmony_ci	if (err < 0)
8388c2ecf20Sopenharmony_ci		return err;
8398c2ecf20Sopenharmony_ci	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_via686_playback_ops);
8408c2ecf20Sopenharmony_ci	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_via686_capture_ops);
8418c2ecf20Sopenharmony_ci	pcm->dev_class = SNDRV_PCM_CLASS_MODEM;
8428c2ecf20Sopenharmony_ci	pcm->private_data = chip;
8438c2ecf20Sopenharmony_ci	strcpy(pcm->name, chip->card->shortname);
8448c2ecf20Sopenharmony_ci	chip->pcms[0] = pcm;
8458c2ecf20Sopenharmony_ci	init_viadev(chip, 0, VIA_REG_MO_STATUS, 0);
8468c2ecf20Sopenharmony_ci	init_viadev(chip, 1, VIA_REG_MI_STATUS, 1);
8478c2ecf20Sopenharmony_ci
8488c2ecf20Sopenharmony_ci	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
8498c2ecf20Sopenharmony_ci				       &chip->pci->dev, 64*1024, 128*1024);
8508c2ecf20Sopenharmony_ci	return 0;
8518c2ecf20Sopenharmony_ci}
8528c2ecf20Sopenharmony_ci
8538c2ecf20Sopenharmony_ci
8548c2ecf20Sopenharmony_ci/*
8558c2ecf20Sopenharmony_ci *  Mixer part
8568c2ecf20Sopenharmony_ci */
8578c2ecf20Sopenharmony_ci
8588c2ecf20Sopenharmony_ci
8598c2ecf20Sopenharmony_cistatic void snd_via82xx_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
8608c2ecf20Sopenharmony_ci{
8618c2ecf20Sopenharmony_ci	struct via82xx_modem *chip = bus->private_data;
8628c2ecf20Sopenharmony_ci	chip->ac97_bus = NULL;
8638c2ecf20Sopenharmony_ci}
8648c2ecf20Sopenharmony_ci
8658c2ecf20Sopenharmony_cistatic void snd_via82xx_mixer_free_ac97(struct snd_ac97 *ac97)
8668c2ecf20Sopenharmony_ci{
8678c2ecf20Sopenharmony_ci	struct via82xx_modem *chip = ac97->private_data;
8688c2ecf20Sopenharmony_ci	chip->ac97 = NULL;
8698c2ecf20Sopenharmony_ci}
8708c2ecf20Sopenharmony_ci
8718c2ecf20Sopenharmony_ci
8728c2ecf20Sopenharmony_cistatic int snd_via82xx_mixer_new(struct via82xx_modem *chip)
8738c2ecf20Sopenharmony_ci{
8748c2ecf20Sopenharmony_ci	struct snd_ac97_template ac97;
8758c2ecf20Sopenharmony_ci	int err;
8768c2ecf20Sopenharmony_ci	static const struct snd_ac97_bus_ops ops = {
8778c2ecf20Sopenharmony_ci		.write = snd_via82xx_codec_write,
8788c2ecf20Sopenharmony_ci		.read = snd_via82xx_codec_read,
8798c2ecf20Sopenharmony_ci		.wait = snd_via82xx_codec_wait,
8808c2ecf20Sopenharmony_ci	};
8818c2ecf20Sopenharmony_ci
8828c2ecf20Sopenharmony_ci	if ((err = snd_ac97_bus(chip->card, 0, &ops, chip, &chip->ac97_bus)) < 0)
8838c2ecf20Sopenharmony_ci		return err;
8848c2ecf20Sopenharmony_ci	chip->ac97_bus->private_free = snd_via82xx_mixer_free_ac97_bus;
8858c2ecf20Sopenharmony_ci	chip->ac97_bus->clock = chip->ac97_clock;
8868c2ecf20Sopenharmony_ci
8878c2ecf20Sopenharmony_ci	memset(&ac97, 0, sizeof(ac97));
8888c2ecf20Sopenharmony_ci	ac97.private_data = chip;
8898c2ecf20Sopenharmony_ci	ac97.private_free = snd_via82xx_mixer_free_ac97;
8908c2ecf20Sopenharmony_ci	ac97.pci = chip->pci;
8918c2ecf20Sopenharmony_ci	ac97.scaps = AC97_SCAP_SKIP_AUDIO | AC97_SCAP_POWER_SAVE;
8928c2ecf20Sopenharmony_ci	ac97.num = chip->ac97_secondary;
8938c2ecf20Sopenharmony_ci
8948c2ecf20Sopenharmony_ci	if ((err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97)) < 0)
8958c2ecf20Sopenharmony_ci		return err;
8968c2ecf20Sopenharmony_ci
8978c2ecf20Sopenharmony_ci	return 0;
8988c2ecf20Sopenharmony_ci}
8998c2ecf20Sopenharmony_ci
9008c2ecf20Sopenharmony_ci
9018c2ecf20Sopenharmony_ci/*
9028c2ecf20Sopenharmony_ci * proc interface
9038c2ecf20Sopenharmony_ci */
9048c2ecf20Sopenharmony_cistatic void snd_via82xx_proc_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
9058c2ecf20Sopenharmony_ci{
9068c2ecf20Sopenharmony_ci	struct via82xx_modem *chip = entry->private_data;
9078c2ecf20Sopenharmony_ci	int i;
9088c2ecf20Sopenharmony_ci
9098c2ecf20Sopenharmony_ci	snd_iprintf(buffer, "%s\n\n", chip->card->longname);
9108c2ecf20Sopenharmony_ci	for (i = 0; i < 0xa0; i += 4) {
9118c2ecf20Sopenharmony_ci		snd_iprintf(buffer, "%02x: %08x\n", i, inl(chip->port + i));
9128c2ecf20Sopenharmony_ci	}
9138c2ecf20Sopenharmony_ci}
9148c2ecf20Sopenharmony_ci
9158c2ecf20Sopenharmony_cistatic void snd_via82xx_proc_init(struct via82xx_modem *chip)
9168c2ecf20Sopenharmony_ci{
9178c2ecf20Sopenharmony_ci	snd_card_ro_proc_new(chip->card, "via82xx", chip,
9188c2ecf20Sopenharmony_ci			     snd_via82xx_proc_read);
9198c2ecf20Sopenharmony_ci}
9208c2ecf20Sopenharmony_ci
9218c2ecf20Sopenharmony_ci/*
9228c2ecf20Sopenharmony_ci *
9238c2ecf20Sopenharmony_ci */
9248c2ecf20Sopenharmony_ci
9258c2ecf20Sopenharmony_cistatic int snd_via82xx_chip_init(struct via82xx_modem *chip)
9268c2ecf20Sopenharmony_ci{
9278c2ecf20Sopenharmony_ci	unsigned int val;
9288c2ecf20Sopenharmony_ci	unsigned long end_time;
9298c2ecf20Sopenharmony_ci	unsigned char pval;
9308c2ecf20Sopenharmony_ci
9318c2ecf20Sopenharmony_ci	pci_read_config_byte(chip->pci, VIA_MC97_CTRL, &pval);
9328c2ecf20Sopenharmony_ci	if((pval & VIA_MC97_CTRL_INIT) != VIA_MC97_CTRL_INIT) {
9338c2ecf20Sopenharmony_ci		pci_write_config_byte(chip->pci, 0x44, pval|VIA_MC97_CTRL_INIT);
9348c2ecf20Sopenharmony_ci		udelay(100);
9358c2ecf20Sopenharmony_ci	}
9368c2ecf20Sopenharmony_ci
9378c2ecf20Sopenharmony_ci	pci_read_config_byte(chip->pci, VIA_ACLINK_STAT, &pval);
9388c2ecf20Sopenharmony_ci	if (! (pval & VIA_ACLINK_C00_READY)) { /* codec not ready? */
9398c2ecf20Sopenharmony_ci		/* deassert ACLink reset, force SYNC */
9408c2ecf20Sopenharmony_ci		pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL,
9418c2ecf20Sopenharmony_ci				      VIA_ACLINK_CTRL_ENABLE |
9428c2ecf20Sopenharmony_ci				      VIA_ACLINK_CTRL_RESET |
9438c2ecf20Sopenharmony_ci				      VIA_ACLINK_CTRL_SYNC);
9448c2ecf20Sopenharmony_ci		udelay(100);
9458c2ecf20Sopenharmony_ci#if 1 /* FIXME: should we do full reset here for all chip models? */
9468c2ecf20Sopenharmony_ci		pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, 0x00);
9478c2ecf20Sopenharmony_ci		udelay(100);
9488c2ecf20Sopenharmony_ci#else
9498c2ecf20Sopenharmony_ci		/* deassert ACLink reset, force SYNC (warm AC'97 reset) */
9508c2ecf20Sopenharmony_ci		pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL,
9518c2ecf20Sopenharmony_ci				      VIA_ACLINK_CTRL_RESET|VIA_ACLINK_CTRL_SYNC);
9528c2ecf20Sopenharmony_ci		udelay(2);
9538c2ecf20Sopenharmony_ci#endif
9548c2ecf20Sopenharmony_ci		/* ACLink on, deassert ACLink reset, VSR, SGD data out */
9558c2ecf20Sopenharmony_ci		pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, VIA_ACLINK_CTRL_INIT);
9568c2ecf20Sopenharmony_ci		udelay(100);
9578c2ecf20Sopenharmony_ci	}
9588c2ecf20Sopenharmony_ci
9598c2ecf20Sopenharmony_ci	pci_read_config_byte(chip->pci, VIA_ACLINK_CTRL, &pval);
9608c2ecf20Sopenharmony_ci	if ((pval & VIA_ACLINK_CTRL_INIT) != VIA_ACLINK_CTRL_INIT) {
9618c2ecf20Sopenharmony_ci		/* ACLink on, deassert ACLink reset, VSR, SGD data out */
9628c2ecf20Sopenharmony_ci		pci_write_config_byte(chip->pci, VIA_ACLINK_CTRL, VIA_ACLINK_CTRL_INIT);
9638c2ecf20Sopenharmony_ci		udelay(100);
9648c2ecf20Sopenharmony_ci	}
9658c2ecf20Sopenharmony_ci
9668c2ecf20Sopenharmony_ci	/* wait until codec ready */
9678c2ecf20Sopenharmony_ci	end_time = jiffies + msecs_to_jiffies(750);
9688c2ecf20Sopenharmony_ci	do {
9698c2ecf20Sopenharmony_ci		pci_read_config_byte(chip->pci, VIA_ACLINK_STAT, &pval);
9708c2ecf20Sopenharmony_ci		if (pval & VIA_ACLINK_C00_READY) /* primary codec ready */
9718c2ecf20Sopenharmony_ci			break;
9728c2ecf20Sopenharmony_ci		schedule_timeout_uninterruptible(1);
9738c2ecf20Sopenharmony_ci	} while (time_before(jiffies, end_time));
9748c2ecf20Sopenharmony_ci
9758c2ecf20Sopenharmony_ci	if ((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_BUSY)
9768c2ecf20Sopenharmony_ci		dev_err(chip->card->dev,
9778c2ecf20Sopenharmony_ci			"AC'97 codec is not ready [0x%x]\n", val);
9788c2ecf20Sopenharmony_ci
9798c2ecf20Sopenharmony_ci	snd_via82xx_codec_xwrite(chip, VIA_REG_AC97_READ |
9808c2ecf20Sopenharmony_ci				 VIA_REG_AC97_SECONDARY_VALID |
9818c2ecf20Sopenharmony_ci				 (VIA_REG_AC97_CODEC_ID_SECONDARY << VIA_REG_AC97_CODEC_ID_SHIFT));
9828c2ecf20Sopenharmony_ci	end_time = jiffies + msecs_to_jiffies(750);
9838c2ecf20Sopenharmony_ci	snd_via82xx_codec_xwrite(chip, VIA_REG_AC97_READ |
9848c2ecf20Sopenharmony_ci				 VIA_REG_AC97_SECONDARY_VALID |
9858c2ecf20Sopenharmony_ci				 (VIA_REG_AC97_CODEC_ID_SECONDARY << VIA_REG_AC97_CODEC_ID_SHIFT));
9868c2ecf20Sopenharmony_ci	do {
9878c2ecf20Sopenharmony_ci		if ((val = snd_via82xx_codec_xread(chip)) & VIA_REG_AC97_SECONDARY_VALID) {
9888c2ecf20Sopenharmony_ci			chip->ac97_secondary = 1;
9898c2ecf20Sopenharmony_ci			goto __ac97_ok2;
9908c2ecf20Sopenharmony_ci		}
9918c2ecf20Sopenharmony_ci		schedule_timeout_uninterruptible(1);
9928c2ecf20Sopenharmony_ci	} while (time_before(jiffies, end_time));
9938c2ecf20Sopenharmony_ci	/* This is ok, the most of motherboards have only one codec */
9948c2ecf20Sopenharmony_ci
9958c2ecf20Sopenharmony_ci      __ac97_ok2:
9968c2ecf20Sopenharmony_ci
9978c2ecf20Sopenharmony_ci	/* route FM trap to IRQ, disable FM trap */
9988c2ecf20Sopenharmony_ci	// pci_write_config_byte(chip->pci, VIA_FM_NMI_CTRL, 0);
9998c2ecf20Sopenharmony_ci	/* disable all GPI interrupts */
10008c2ecf20Sopenharmony_ci	outl(0, VIAREG(chip, GPI_INTR));
10018c2ecf20Sopenharmony_ci
10028c2ecf20Sopenharmony_ci	return 0;
10038c2ecf20Sopenharmony_ci}
10048c2ecf20Sopenharmony_ci
10058c2ecf20Sopenharmony_ci#ifdef CONFIG_PM_SLEEP
10068c2ecf20Sopenharmony_ci/*
10078c2ecf20Sopenharmony_ci * power management
10088c2ecf20Sopenharmony_ci */
10098c2ecf20Sopenharmony_cistatic int snd_via82xx_suspend(struct device *dev)
10108c2ecf20Sopenharmony_ci{
10118c2ecf20Sopenharmony_ci	struct snd_card *card = dev_get_drvdata(dev);
10128c2ecf20Sopenharmony_ci	struct via82xx_modem *chip = card->private_data;
10138c2ecf20Sopenharmony_ci	int i;
10148c2ecf20Sopenharmony_ci
10158c2ecf20Sopenharmony_ci	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
10168c2ecf20Sopenharmony_ci	for (i = 0; i < chip->num_devs; i++)
10178c2ecf20Sopenharmony_ci		snd_via82xx_channel_reset(chip, &chip->devs[i]);
10188c2ecf20Sopenharmony_ci	snd_ac97_suspend(chip->ac97);
10198c2ecf20Sopenharmony_ci	return 0;
10208c2ecf20Sopenharmony_ci}
10218c2ecf20Sopenharmony_ci
10228c2ecf20Sopenharmony_cistatic int snd_via82xx_resume(struct device *dev)
10238c2ecf20Sopenharmony_ci{
10248c2ecf20Sopenharmony_ci	struct snd_card *card = dev_get_drvdata(dev);
10258c2ecf20Sopenharmony_ci	struct via82xx_modem *chip = card->private_data;
10268c2ecf20Sopenharmony_ci	int i;
10278c2ecf20Sopenharmony_ci
10288c2ecf20Sopenharmony_ci	snd_via82xx_chip_init(chip);
10298c2ecf20Sopenharmony_ci
10308c2ecf20Sopenharmony_ci	snd_ac97_resume(chip->ac97);
10318c2ecf20Sopenharmony_ci
10328c2ecf20Sopenharmony_ci	for (i = 0; i < chip->num_devs; i++)
10338c2ecf20Sopenharmony_ci		snd_via82xx_channel_reset(chip, &chip->devs[i]);
10348c2ecf20Sopenharmony_ci
10358c2ecf20Sopenharmony_ci	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
10368c2ecf20Sopenharmony_ci	return 0;
10378c2ecf20Sopenharmony_ci}
10388c2ecf20Sopenharmony_ci
10398c2ecf20Sopenharmony_cistatic SIMPLE_DEV_PM_OPS(snd_via82xx_pm, snd_via82xx_suspend, snd_via82xx_resume);
10408c2ecf20Sopenharmony_ci#define SND_VIA82XX_PM_OPS	&snd_via82xx_pm
10418c2ecf20Sopenharmony_ci#else
10428c2ecf20Sopenharmony_ci#define SND_VIA82XX_PM_OPS	NULL
10438c2ecf20Sopenharmony_ci#endif /* CONFIG_PM_SLEEP */
10448c2ecf20Sopenharmony_ci
10458c2ecf20Sopenharmony_cistatic int snd_via82xx_free(struct via82xx_modem *chip)
10468c2ecf20Sopenharmony_ci{
10478c2ecf20Sopenharmony_ci	unsigned int i;
10488c2ecf20Sopenharmony_ci
10498c2ecf20Sopenharmony_ci	if (chip->irq < 0)
10508c2ecf20Sopenharmony_ci		goto __end_hw;
10518c2ecf20Sopenharmony_ci	/* disable interrupts */
10528c2ecf20Sopenharmony_ci	for (i = 0; i < chip->num_devs; i++)
10538c2ecf20Sopenharmony_ci		snd_via82xx_channel_reset(chip, &chip->devs[i]);
10548c2ecf20Sopenharmony_ci
10558c2ecf20Sopenharmony_ci      __end_hw:
10568c2ecf20Sopenharmony_ci	if (chip->irq >= 0)
10578c2ecf20Sopenharmony_ci		free_irq(chip->irq, chip);
10588c2ecf20Sopenharmony_ci	pci_release_regions(chip->pci);
10598c2ecf20Sopenharmony_ci	pci_disable_device(chip->pci);
10608c2ecf20Sopenharmony_ci	kfree(chip);
10618c2ecf20Sopenharmony_ci	return 0;
10628c2ecf20Sopenharmony_ci}
10638c2ecf20Sopenharmony_ci
10648c2ecf20Sopenharmony_cistatic int snd_via82xx_dev_free(struct snd_device *device)
10658c2ecf20Sopenharmony_ci{
10668c2ecf20Sopenharmony_ci	struct via82xx_modem *chip = device->device_data;
10678c2ecf20Sopenharmony_ci	return snd_via82xx_free(chip);
10688c2ecf20Sopenharmony_ci}
10698c2ecf20Sopenharmony_ci
10708c2ecf20Sopenharmony_cistatic int snd_via82xx_create(struct snd_card *card,
10718c2ecf20Sopenharmony_ci			      struct pci_dev *pci,
10728c2ecf20Sopenharmony_ci			      int chip_type,
10738c2ecf20Sopenharmony_ci			      int revision,
10748c2ecf20Sopenharmony_ci			      unsigned int ac97_clock,
10758c2ecf20Sopenharmony_ci			      struct via82xx_modem **r_via)
10768c2ecf20Sopenharmony_ci{
10778c2ecf20Sopenharmony_ci	struct via82xx_modem *chip;
10788c2ecf20Sopenharmony_ci	int err;
10798c2ecf20Sopenharmony_ci	static const struct snd_device_ops ops = {
10808c2ecf20Sopenharmony_ci		.dev_free =	snd_via82xx_dev_free,
10818c2ecf20Sopenharmony_ci        };
10828c2ecf20Sopenharmony_ci
10838c2ecf20Sopenharmony_ci	if ((err = pci_enable_device(pci)) < 0)
10848c2ecf20Sopenharmony_ci		return err;
10858c2ecf20Sopenharmony_ci
10868c2ecf20Sopenharmony_ci	if ((chip = kzalloc(sizeof(*chip), GFP_KERNEL)) == NULL) {
10878c2ecf20Sopenharmony_ci		pci_disable_device(pci);
10888c2ecf20Sopenharmony_ci		return -ENOMEM;
10898c2ecf20Sopenharmony_ci	}
10908c2ecf20Sopenharmony_ci
10918c2ecf20Sopenharmony_ci	spin_lock_init(&chip->reg_lock);
10928c2ecf20Sopenharmony_ci	chip->card = card;
10938c2ecf20Sopenharmony_ci	chip->pci = pci;
10948c2ecf20Sopenharmony_ci	chip->irq = -1;
10958c2ecf20Sopenharmony_ci
10968c2ecf20Sopenharmony_ci	if ((err = pci_request_regions(pci, card->driver)) < 0) {
10978c2ecf20Sopenharmony_ci		kfree(chip);
10988c2ecf20Sopenharmony_ci		pci_disable_device(pci);
10998c2ecf20Sopenharmony_ci		return err;
11008c2ecf20Sopenharmony_ci	}
11018c2ecf20Sopenharmony_ci	chip->port = pci_resource_start(pci, 0);
11028c2ecf20Sopenharmony_ci	if (request_irq(pci->irq, snd_via82xx_interrupt, IRQF_SHARED,
11038c2ecf20Sopenharmony_ci			KBUILD_MODNAME, chip)) {
11048c2ecf20Sopenharmony_ci		dev_err(card->dev, "unable to grab IRQ %d\n", pci->irq);
11058c2ecf20Sopenharmony_ci		snd_via82xx_free(chip);
11068c2ecf20Sopenharmony_ci		return -EBUSY;
11078c2ecf20Sopenharmony_ci	}
11088c2ecf20Sopenharmony_ci	chip->irq = pci->irq;
11098c2ecf20Sopenharmony_ci	card->sync_irq = chip->irq;
11108c2ecf20Sopenharmony_ci	if (ac97_clock >= 8000 && ac97_clock <= 48000)
11118c2ecf20Sopenharmony_ci		chip->ac97_clock = ac97_clock;
11128c2ecf20Sopenharmony_ci
11138c2ecf20Sopenharmony_ci	if ((err = snd_via82xx_chip_init(chip)) < 0) {
11148c2ecf20Sopenharmony_ci		snd_via82xx_free(chip);
11158c2ecf20Sopenharmony_ci		return err;
11168c2ecf20Sopenharmony_ci	}
11178c2ecf20Sopenharmony_ci
11188c2ecf20Sopenharmony_ci	if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
11198c2ecf20Sopenharmony_ci		snd_via82xx_free(chip);
11208c2ecf20Sopenharmony_ci		return err;
11218c2ecf20Sopenharmony_ci	}
11228c2ecf20Sopenharmony_ci
11238c2ecf20Sopenharmony_ci	/* The 8233 ac97 controller does not implement the master bit
11248c2ecf20Sopenharmony_ci	 * in the pci command register. IMHO this is a violation of the PCI spec.
11258c2ecf20Sopenharmony_ci	 * We call pci_set_master here because it does not hurt. */
11268c2ecf20Sopenharmony_ci	pci_set_master(pci);
11278c2ecf20Sopenharmony_ci
11288c2ecf20Sopenharmony_ci	*r_via = chip;
11298c2ecf20Sopenharmony_ci	return 0;
11308c2ecf20Sopenharmony_ci}
11318c2ecf20Sopenharmony_ci
11328c2ecf20Sopenharmony_ci
11338c2ecf20Sopenharmony_cistatic int snd_via82xx_probe(struct pci_dev *pci,
11348c2ecf20Sopenharmony_ci			     const struct pci_device_id *pci_id)
11358c2ecf20Sopenharmony_ci{
11368c2ecf20Sopenharmony_ci	struct snd_card *card;
11378c2ecf20Sopenharmony_ci	struct via82xx_modem *chip;
11388c2ecf20Sopenharmony_ci	int chip_type = 0, card_type;
11398c2ecf20Sopenharmony_ci	unsigned int i;
11408c2ecf20Sopenharmony_ci	int err;
11418c2ecf20Sopenharmony_ci
11428c2ecf20Sopenharmony_ci	err = snd_card_new(&pci->dev, index, id, THIS_MODULE, 0, &card);
11438c2ecf20Sopenharmony_ci	if (err < 0)
11448c2ecf20Sopenharmony_ci		return err;
11458c2ecf20Sopenharmony_ci
11468c2ecf20Sopenharmony_ci	card_type = pci_id->driver_data;
11478c2ecf20Sopenharmony_ci	switch (card_type) {
11488c2ecf20Sopenharmony_ci	case TYPE_CARD_VIA82XX_MODEM:
11498c2ecf20Sopenharmony_ci		strcpy(card->driver, "VIA82XX-MODEM");
11508c2ecf20Sopenharmony_ci		sprintf(card->shortname, "VIA 82XX modem");
11518c2ecf20Sopenharmony_ci		break;
11528c2ecf20Sopenharmony_ci	default:
11538c2ecf20Sopenharmony_ci		dev_err(card->dev, "invalid card type %d\n", card_type);
11548c2ecf20Sopenharmony_ci		err = -EINVAL;
11558c2ecf20Sopenharmony_ci		goto __error;
11568c2ecf20Sopenharmony_ci	}
11578c2ecf20Sopenharmony_ci
11588c2ecf20Sopenharmony_ci	if ((err = snd_via82xx_create(card, pci, chip_type, pci->revision,
11598c2ecf20Sopenharmony_ci				      ac97_clock, &chip)) < 0)
11608c2ecf20Sopenharmony_ci		goto __error;
11618c2ecf20Sopenharmony_ci	card->private_data = chip;
11628c2ecf20Sopenharmony_ci	if ((err = snd_via82xx_mixer_new(chip)) < 0)
11638c2ecf20Sopenharmony_ci		goto __error;
11648c2ecf20Sopenharmony_ci
11658c2ecf20Sopenharmony_ci	if ((err = snd_via686_pcm_new(chip)) < 0 )
11668c2ecf20Sopenharmony_ci		goto __error;
11678c2ecf20Sopenharmony_ci
11688c2ecf20Sopenharmony_ci	/* disable interrupts */
11698c2ecf20Sopenharmony_ci	for (i = 0; i < chip->num_devs; i++)
11708c2ecf20Sopenharmony_ci		snd_via82xx_channel_reset(chip, &chip->devs[i]);
11718c2ecf20Sopenharmony_ci
11728c2ecf20Sopenharmony_ci	sprintf(card->longname, "%s at 0x%lx, irq %d",
11738c2ecf20Sopenharmony_ci		card->shortname, chip->port, chip->irq);
11748c2ecf20Sopenharmony_ci
11758c2ecf20Sopenharmony_ci	snd_via82xx_proc_init(chip);
11768c2ecf20Sopenharmony_ci
11778c2ecf20Sopenharmony_ci	if ((err = snd_card_register(card)) < 0) {
11788c2ecf20Sopenharmony_ci		snd_card_free(card);
11798c2ecf20Sopenharmony_ci		return err;
11808c2ecf20Sopenharmony_ci	}
11818c2ecf20Sopenharmony_ci	pci_set_drvdata(pci, card);
11828c2ecf20Sopenharmony_ci	return 0;
11838c2ecf20Sopenharmony_ci
11848c2ecf20Sopenharmony_ci __error:
11858c2ecf20Sopenharmony_ci	snd_card_free(card);
11868c2ecf20Sopenharmony_ci	return err;
11878c2ecf20Sopenharmony_ci}
11888c2ecf20Sopenharmony_ci
11898c2ecf20Sopenharmony_cistatic void snd_via82xx_remove(struct pci_dev *pci)
11908c2ecf20Sopenharmony_ci{
11918c2ecf20Sopenharmony_ci	snd_card_free(pci_get_drvdata(pci));
11928c2ecf20Sopenharmony_ci}
11938c2ecf20Sopenharmony_ci
11948c2ecf20Sopenharmony_cistatic struct pci_driver via82xx_modem_driver = {
11958c2ecf20Sopenharmony_ci	.name = KBUILD_MODNAME,
11968c2ecf20Sopenharmony_ci	.id_table = snd_via82xx_modem_ids,
11978c2ecf20Sopenharmony_ci	.probe = snd_via82xx_probe,
11988c2ecf20Sopenharmony_ci	.remove = snd_via82xx_remove,
11998c2ecf20Sopenharmony_ci	.driver = {
12008c2ecf20Sopenharmony_ci		.pm = SND_VIA82XX_PM_OPS,
12018c2ecf20Sopenharmony_ci	},
12028c2ecf20Sopenharmony_ci};
12038c2ecf20Sopenharmony_ci
12048c2ecf20Sopenharmony_cimodule_pci_driver(via82xx_modem_driver);
1205