18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * bt87x.c - Brooktree Bt878/Bt879 driver for ALSA
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * based on btaudio.c by Gerd Knorr <kraxel@bytesex.org>
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/init.h>
118c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
128c2ecf20Sopenharmony_ci#include <linux/pci.h>
138c2ecf20Sopenharmony_ci#include <linux/slab.h>
148c2ecf20Sopenharmony_ci#include <linux/module.h>
158c2ecf20Sopenharmony_ci#include <linux/bitops.h>
168c2ecf20Sopenharmony_ci#include <linux/io.h>
178c2ecf20Sopenharmony_ci#include <sound/core.h>
188c2ecf20Sopenharmony_ci#include <sound/pcm.h>
198c2ecf20Sopenharmony_ci#include <sound/pcm_params.h>
208c2ecf20Sopenharmony_ci#include <sound/control.h>
218c2ecf20Sopenharmony_ci#include <sound/initval.h>
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ciMODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
248c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Brooktree Bt87x audio driver");
258c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
268c2ecf20Sopenharmony_ciMODULE_SUPPORTED_DEVICE("{{Brooktree,Bt878},"
278c2ecf20Sopenharmony_ci		"{Brooktree,Bt879}}");
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_cistatic int index[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = -2}; /* Exclude the first card */
308c2ecf20Sopenharmony_cistatic char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* ID for this card */
318c2ecf20Sopenharmony_cistatic bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;	/* Enable this card */
328c2ecf20Sopenharmony_cistatic int digital_rate[SNDRV_CARDS];	/* digital input rate */
338c2ecf20Sopenharmony_cistatic bool load_all;	/* allow to load cards not the allowlist */
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_cimodule_param_array(index, int, NULL, 0444);
368c2ecf20Sopenharmony_ciMODULE_PARM_DESC(index, "Index value for Bt87x soundcard");
378c2ecf20Sopenharmony_cimodule_param_array(id, charp, NULL, 0444);
388c2ecf20Sopenharmony_ciMODULE_PARM_DESC(id, "ID string for Bt87x soundcard");
398c2ecf20Sopenharmony_cimodule_param_array(enable, bool, NULL, 0444);
408c2ecf20Sopenharmony_ciMODULE_PARM_DESC(enable, "Enable Bt87x soundcard");
418c2ecf20Sopenharmony_cimodule_param_array(digital_rate, int, NULL, 0444);
428c2ecf20Sopenharmony_ciMODULE_PARM_DESC(digital_rate, "Digital input rate for Bt87x soundcard");
438c2ecf20Sopenharmony_cimodule_param(load_all, bool, 0444);
448c2ecf20Sopenharmony_ciMODULE_PARM_DESC(load_all, "Allow to load cards not on the allowlist");
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci/* register offsets */
488c2ecf20Sopenharmony_ci#define REG_INT_STAT		0x100	/* interrupt status */
498c2ecf20Sopenharmony_ci#define REG_INT_MASK		0x104	/* interrupt mask */
508c2ecf20Sopenharmony_ci#define REG_GPIO_DMA_CTL	0x10c	/* audio control */
518c2ecf20Sopenharmony_ci#define REG_PACKET_LEN		0x110	/* audio packet lengths */
528c2ecf20Sopenharmony_ci#define REG_RISC_STRT_ADD	0x114	/* RISC program start address */
538c2ecf20Sopenharmony_ci#define REG_RISC_COUNT		0x120	/* RISC program counter */
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci/* interrupt bits */
568c2ecf20Sopenharmony_ci#define INT_OFLOW	(1 <<  3)	/* audio A/D overflow */
578c2ecf20Sopenharmony_ci#define INT_RISCI	(1 << 11)	/* RISC instruction IRQ bit set */
588c2ecf20Sopenharmony_ci#define INT_FBUS	(1 << 12)	/* FIFO overrun due to bus access latency */
598c2ecf20Sopenharmony_ci#define INT_FTRGT	(1 << 13)	/* FIFO overrun due to target latency */
608c2ecf20Sopenharmony_ci#define INT_FDSR	(1 << 14)	/* FIFO data stream resynchronization */
618c2ecf20Sopenharmony_ci#define INT_PPERR	(1 << 15)	/* PCI parity error */
628c2ecf20Sopenharmony_ci#define INT_RIPERR	(1 << 16)	/* RISC instruction parity error */
638c2ecf20Sopenharmony_ci#define INT_PABORT	(1 << 17)	/* PCI master or target abort */
648c2ecf20Sopenharmony_ci#define INT_OCERR	(1 << 18)	/* invalid opcode */
658c2ecf20Sopenharmony_ci#define INT_SCERR	(1 << 19)	/* sync counter overflow */
668c2ecf20Sopenharmony_ci#define INT_RISC_EN	(1 << 27)	/* DMA controller running */
678c2ecf20Sopenharmony_ci#define INT_RISCS_SHIFT	      28	/* RISC status bits */
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci/* audio control bits */
708c2ecf20Sopenharmony_ci#define CTL_FIFO_ENABLE		(1 <<  0)	/* enable audio data FIFO */
718c2ecf20Sopenharmony_ci#define CTL_RISC_ENABLE		(1 <<  1)	/* enable audio DMA controller */
728c2ecf20Sopenharmony_ci#define CTL_PKTP_4		(0 <<  2)	/* packet mode FIFO trigger point - 4 DWORDs */
738c2ecf20Sopenharmony_ci#define CTL_PKTP_8		(1 <<  2)	/* 8 DWORDs */
748c2ecf20Sopenharmony_ci#define CTL_PKTP_16		(2 <<  2)	/* 16 DWORDs */
758c2ecf20Sopenharmony_ci#define CTL_ACAP_EN		(1 <<  4)	/* enable audio capture */
768c2ecf20Sopenharmony_ci#define CTL_DA_APP		(1 <<  5)	/* GPIO input */
778c2ecf20Sopenharmony_ci#define CTL_DA_IOM_AFE		(0 <<  6)	/* audio A/D input */
788c2ecf20Sopenharmony_ci#define CTL_DA_IOM_DA		(1 <<  6)	/* digital audio input */
798c2ecf20Sopenharmony_ci#define CTL_DA_SDR_SHIFT	       8	/* DDF first stage decimation rate */
808c2ecf20Sopenharmony_ci#define CTL_DA_SDR_MASK		(0xf<< 8)
818c2ecf20Sopenharmony_ci#define CTL_DA_LMT		(1 << 12)	/* limit audio data values */
828c2ecf20Sopenharmony_ci#define CTL_DA_ES2		(1 << 13)	/* enable DDF stage 2 */
838c2ecf20Sopenharmony_ci#define CTL_DA_SBR		(1 << 14)	/* samples rounded to 8 bits */
848c2ecf20Sopenharmony_ci#define CTL_DA_DPM		(1 << 15)	/* data packet mode */
858c2ecf20Sopenharmony_ci#define CTL_DA_LRD_SHIFT	      16	/* ALRCK delay */
868c2ecf20Sopenharmony_ci#define CTL_DA_MLB		(1 << 21)	/* MSB/LSB format */
878c2ecf20Sopenharmony_ci#define CTL_DA_LRI		(1 << 22)	/* left/right indication */
888c2ecf20Sopenharmony_ci#define CTL_DA_SCE		(1 << 23)	/* sample clock edge */
898c2ecf20Sopenharmony_ci#define CTL_A_SEL_STV		(0 << 24)	/* TV tuner audio input */
908c2ecf20Sopenharmony_ci#define CTL_A_SEL_SFM		(1 << 24)	/* FM audio input */
918c2ecf20Sopenharmony_ci#define CTL_A_SEL_SML		(2 << 24)	/* mic/line audio input */
928c2ecf20Sopenharmony_ci#define CTL_A_SEL_SMXC		(3 << 24)	/* MUX bypass */
938c2ecf20Sopenharmony_ci#define CTL_A_SEL_SHIFT		      24
948c2ecf20Sopenharmony_ci#define CTL_A_SEL_MASK		(3 << 24)
958c2ecf20Sopenharmony_ci#define CTL_A_PWRDN		(1 << 26)	/* analog audio power-down */
968c2ecf20Sopenharmony_ci#define CTL_A_G2X		(1 << 27)	/* audio gain boost */
978c2ecf20Sopenharmony_ci#define CTL_A_GAIN_SHIFT	      28	/* audio input gain */
988c2ecf20Sopenharmony_ci#define CTL_A_GAIN_MASK		(0xf<<28)
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci/* RISC instruction opcodes */
1018c2ecf20Sopenharmony_ci#define RISC_WRITE	(0x1 << 28)	/* write FIFO data to memory at address */
1028c2ecf20Sopenharmony_ci#define RISC_WRITEC	(0x5 << 28)	/* write FIFO data to memory at current address */
1038c2ecf20Sopenharmony_ci#define RISC_SKIP	(0x2 << 28)	/* skip FIFO data */
1048c2ecf20Sopenharmony_ci#define RISC_JUMP	(0x7 << 28)	/* jump to address */
1058c2ecf20Sopenharmony_ci#define RISC_SYNC	(0x8 << 28)	/* synchronize with FIFO */
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci/* RISC instruction bits */
1088c2ecf20Sopenharmony_ci#define RISC_BYTES_ENABLE	(0xf << 12)	/* byte enable bits */
1098c2ecf20Sopenharmony_ci#define RISC_RESYNC		(  1 << 15)	/* disable FDSR errors */
1108c2ecf20Sopenharmony_ci#define RISC_SET_STATUS_SHIFT	        16	/* set status bits */
1118c2ecf20Sopenharmony_ci#define RISC_RESET_STATUS_SHIFT	        20	/* clear status bits */
1128c2ecf20Sopenharmony_ci#define RISC_IRQ		(  1 << 24)	/* interrupt */
1138c2ecf20Sopenharmony_ci#define RISC_EOL		(  1 << 26)	/* end of line */
1148c2ecf20Sopenharmony_ci#define RISC_SOL		(  1 << 27)	/* start of line */
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci/* SYNC status bits values */
1178c2ecf20Sopenharmony_ci#define RISC_SYNC_FM1	0x6
1188c2ecf20Sopenharmony_ci#define RISC_SYNC_VRO	0xc
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci#define ANALOG_CLOCK 1792000
1218c2ecf20Sopenharmony_ci#ifdef CONFIG_SND_BT87X_OVERCLOCK
1228c2ecf20Sopenharmony_ci#define CLOCK_DIV_MIN 1
1238c2ecf20Sopenharmony_ci#else
1248c2ecf20Sopenharmony_ci#define CLOCK_DIV_MIN 4
1258c2ecf20Sopenharmony_ci#endif
1268c2ecf20Sopenharmony_ci#define CLOCK_DIV_MAX 15
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci#define ERROR_INTERRUPTS (INT_FBUS | INT_FTRGT | INT_PPERR | \
1298c2ecf20Sopenharmony_ci			  INT_RIPERR | INT_PABORT | INT_OCERR)
1308c2ecf20Sopenharmony_ci#define MY_INTERRUPTS (INT_RISCI | ERROR_INTERRUPTS)
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci/* SYNC, one WRITE per line, one extra WRITE per page boundary, SYNC, JUMP */
1338c2ecf20Sopenharmony_ci#define MAX_RISC_SIZE ((1 + 255 + (PAGE_ALIGN(255 * 4092) / PAGE_SIZE - 1) + 1 + 1) * 8)
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci/* Cards with configuration information */
1368c2ecf20Sopenharmony_cienum snd_bt87x_boardid {
1378c2ecf20Sopenharmony_ci	SND_BT87X_BOARD_UNKNOWN,
1388c2ecf20Sopenharmony_ci	SND_BT87X_BOARD_GENERIC,	/* both an & dig interfaces, 32kHz */
1398c2ecf20Sopenharmony_ci	SND_BT87X_BOARD_ANALOG,		/* board with no external A/D */
1408c2ecf20Sopenharmony_ci	SND_BT87X_BOARD_OSPREY2x0,
1418c2ecf20Sopenharmony_ci	SND_BT87X_BOARD_OSPREY440,
1428c2ecf20Sopenharmony_ci	SND_BT87X_BOARD_AVPHONE98,
1438c2ecf20Sopenharmony_ci};
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci/* Card configuration */
1468c2ecf20Sopenharmony_cistruct snd_bt87x_board {
1478c2ecf20Sopenharmony_ci	int dig_rate;		/* Digital input sampling rate */
1488c2ecf20Sopenharmony_ci	u32 digital_fmt;	/* Register settings for digital input */
1498c2ecf20Sopenharmony_ci	unsigned no_analog:1;	/* No analog input */
1508c2ecf20Sopenharmony_ci	unsigned no_digital:1;	/* No digital input */
1518c2ecf20Sopenharmony_ci};
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_cistatic const struct snd_bt87x_board snd_bt87x_boards[] = {
1548c2ecf20Sopenharmony_ci	[SND_BT87X_BOARD_UNKNOWN] = {
1558c2ecf20Sopenharmony_ci		.dig_rate = 32000, /* just a guess */
1568c2ecf20Sopenharmony_ci	},
1578c2ecf20Sopenharmony_ci	[SND_BT87X_BOARD_GENERIC] = {
1588c2ecf20Sopenharmony_ci		.dig_rate = 32000,
1598c2ecf20Sopenharmony_ci	},
1608c2ecf20Sopenharmony_ci	[SND_BT87X_BOARD_ANALOG] = {
1618c2ecf20Sopenharmony_ci		.no_digital = 1,
1628c2ecf20Sopenharmony_ci	},
1638c2ecf20Sopenharmony_ci	[SND_BT87X_BOARD_OSPREY2x0] = {
1648c2ecf20Sopenharmony_ci		.dig_rate = 44100,
1658c2ecf20Sopenharmony_ci		.digital_fmt = CTL_DA_LRI | (1 << CTL_DA_LRD_SHIFT),
1668c2ecf20Sopenharmony_ci	},
1678c2ecf20Sopenharmony_ci	[SND_BT87X_BOARD_OSPREY440] = {
1688c2ecf20Sopenharmony_ci		.dig_rate = 32000,
1698c2ecf20Sopenharmony_ci		.digital_fmt = CTL_DA_LRI | (1 << CTL_DA_LRD_SHIFT),
1708c2ecf20Sopenharmony_ci		.no_analog = 1,
1718c2ecf20Sopenharmony_ci	},
1728c2ecf20Sopenharmony_ci	[SND_BT87X_BOARD_AVPHONE98] = {
1738c2ecf20Sopenharmony_ci		.dig_rate = 48000,
1748c2ecf20Sopenharmony_ci	},
1758c2ecf20Sopenharmony_ci};
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_cistruct snd_bt87x {
1788c2ecf20Sopenharmony_ci	struct snd_card *card;
1798c2ecf20Sopenharmony_ci	struct pci_dev *pci;
1808c2ecf20Sopenharmony_ci	struct snd_bt87x_board board;
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	void __iomem *mmio;
1838c2ecf20Sopenharmony_ci	int irq;
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	spinlock_t reg_lock;
1868c2ecf20Sopenharmony_ci	unsigned long opened;
1878c2ecf20Sopenharmony_ci	struct snd_pcm_substream *substream;
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci	struct snd_dma_buffer dma_risc;
1908c2ecf20Sopenharmony_ci	unsigned int line_bytes;
1918c2ecf20Sopenharmony_ci	unsigned int lines;
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	u32 reg_control;
1948c2ecf20Sopenharmony_ci	u32 interrupt_mask;
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	int current_line;
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci	int pci_parity_errors;
1998c2ecf20Sopenharmony_ci};
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_cienum { DEVICE_DIGITAL, DEVICE_ANALOG };
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_cistatic inline u32 snd_bt87x_readl(struct snd_bt87x *chip, u32 reg)
2048c2ecf20Sopenharmony_ci{
2058c2ecf20Sopenharmony_ci	return readl(chip->mmio + reg);
2068c2ecf20Sopenharmony_ci}
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_cistatic inline void snd_bt87x_writel(struct snd_bt87x *chip, u32 reg, u32 value)
2098c2ecf20Sopenharmony_ci{
2108c2ecf20Sopenharmony_ci	writel(value, chip->mmio + reg);
2118c2ecf20Sopenharmony_ci}
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_cistatic int snd_bt87x_create_risc(struct snd_bt87x *chip, struct snd_pcm_substream *substream,
2148c2ecf20Sopenharmony_ci			       	 unsigned int periods, unsigned int period_bytes)
2158c2ecf20Sopenharmony_ci{
2168c2ecf20Sopenharmony_ci	unsigned int i, offset;
2178c2ecf20Sopenharmony_ci	__le32 *risc;
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	if (chip->dma_risc.area == NULL) {
2208c2ecf20Sopenharmony_ci		if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, &chip->pci->dev,
2218c2ecf20Sopenharmony_ci					PAGE_ALIGN(MAX_RISC_SIZE), &chip->dma_risc) < 0)
2228c2ecf20Sopenharmony_ci			return -ENOMEM;
2238c2ecf20Sopenharmony_ci	}
2248c2ecf20Sopenharmony_ci	risc = (__le32 *)chip->dma_risc.area;
2258c2ecf20Sopenharmony_ci	offset = 0;
2268c2ecf20Sopenharmony_ci	*risc++ = cpu_to_le32(RISC_SYNC | RISC_SYNC_FM1);
2278c2ecf20Sopenharmony_ci	*risc++ = cpu_to_le32(0);
2288c2ecf20Sopenharmony_ci	for (i = 0; i < periods; ++i) {
2298c2ecf20Sopenharmony_ci		u32 rest;
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci		rest = period_bytes;
2328c2ecf20Sopenharmony_ci		do {
2338c2ecf20Sopenharmony_ci			u32 cmd, len;
2348c2ecf20Sopenharmony_ci			unsigned int addr;
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci			len = PAGE_SIZE - (offset % PAGE_SIZE);
2378c2ecf20Sopenharmony_ci			if (len > rest)
2388c2ecf20Sopenharmony_ci				len = rest;
2398c2ecf20Sopenharmony_ci			cmd = RISC_WRITE | len;
2408c2ecf20Sopenharmony_ci			if (rest == period_bytes) {
2418c2ecf20Sopenharmony_ci				u32 block = i * 16 / periods;
2428c2ecf20Sopenharmony_ci				cmd |= RISC_SOL;
2438c2ecf20Sopenharmony_ci				cmd |= block << RISC_SET_STATUS_SHIFT;
2448c2ecf20Sopenharmony_ci				cmd |= (~block & 0xf) << RISC_RESET_STATUS_SHIFT;
2458c2ecf20Sopenharmony_ci			}
2468c2ecf20Sopenharmony_ci			if (len == rest)
2478c2ecf20Sopenharmony_ci				cmd |= RISC_EOL | RISC_IRQ;
2488c2ecf20Sopenharmony_ci			*risc++ = cpu_to_le32(cmd);
2498c2ecf20Sopenharmony_ci			addr = snd_pcm_sgbuf_get_addr(substream, offset);
2508c2ecf20Sopenharmony_ci			*risc++ = cpu_to_le32(addr);
2518c2ecf20Sopenharmony_ci			offset += len;
2528c2ecf20Sopenharmony_ci			rest -= len;
2538c2ecf20Sopenharmony_ci		} while (rest > 0);
2548c2ecf20Sopenharmony_ci	}
2558c2ecf20Sopenharmony_ci	*risc++ = cpu_to_le32(RISC_SYNC | RISC_SYNC_VRO);
2568c2ecf20Sopenharmony_ci	*risc++ = cpu_to_le32(0);
2578c2ecf20Sopenharmony_ci	*risc++ = cpu_to_le32(RISC_JUMP);
2588c2ecf20Sopenharmony_ci	*risc++ = cpu_to_le32(chip->dma_risc.addr);
2598c2ecf20Sopenharmony_ci	chip->line_bytes = period_bytes;
2608c2ecf20Sopenharmony_ci	chip->lines = periods;
2618c2ecf20Sopenharmony_ci	return 0;
2628c2ecf20Sopenharmony_ci}
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_cistatic void snd_bt87x_free_risc(struct snd_bt87x *chip)
2658c2ecf20Sopenharmony_ci{
2668c2ecf20Sopenharmony_ci	if (chip->dma_risc.area) {
2678c2ecf20Sopenharmony_ci		snd_dma_free_pages(&chip->dma_risc);
2688c2ecf20Sopenharmony_ci		chip->dma_risc.area = NULL;
2698c2ecf20Sopenharmony_ci	}
2708c2ecf20Sopenharmony_ci}
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_cistatic void snd_bt87x_pci_error(struct snd_bt87x *chip, unsigned int status)
2738c2ecf20Sopenharmony_ci{
2748c2ecf20Sopenharmony_ci	int pci_status = pci_status_get_and_clear_errors(chip->pci);
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	if (pci_status != PCI_STATUS_DETECTED_PARITY)
2778c2ecf20Sopenharmony_ci		dev_err(chip->card->dev,
2788c2ecf20Sopenharmony_ci			"Aieee - PCI error! status %#08x, PCI status %#04x\n",
2798c2ecf20Sopenharmony_ci			   status & ERROR_INTERRUPTS, pci_status);
2808c2ecf20Sopenharmony_ci	else {
2818c2ecf20Sopenharmony_ci		dev_err(chip->card->dev,
2828c2ecf20Sopenharmony_ci			"Aieee - PCI parity error detected!\n");
2838c2ecf20Sopenharmony_ci		/* error 'handling' similar to aic7xxx_pci.c: */
2848c2ecf20Sopenharmony_ci		chip->pci_parity_errors++;
2858c2ecf20Sopenharmony_ci		if (chip->pci_parity_errors > 20) {
2868c2ecf20Sopenharmony_ci			dev_err(chip->card->dev,
2878c2ecf20Sopenharmony_ci				"Too many PCI parity errors observed.\n");
2888c2ecf20Sopenharmony_ci			dev_err(chip->card->dev,
2898c2ecf20Sopenharmony_ci				"Some device on this bus is generating bad parity.\n");
2908c2ecf20Sopenharmony_ci			dev_err(chip->card->dev,
2918c2ecf20Sopenharmony_ci				"This is an error *observed by*, not *generated by*, this card.\n");
2928c2ecf20Sopenharmony_ci			dev_err(chip->card->dev,
2938c2ecf20Sopenharmony_ci				"PCI parity error checking has been disabled.\n");
2948c2ecf20Sopenharmony_ci			chip->interrupt_mask &= ~(INT_PPERR | INT_RIPERR);
2958c2ecf20Sopenharmony_ci			snd_bt87x_writel(chip, REG_INT_MASK, chip->interrupt_mask);
2968c2ecf20Sopenharmony_ci		}
2978c2ecf20Sopenharmony_ci	}
2988c2ecf20Sopenharmony_ci}
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_cistatic irqreturn_t snd_bt87x_interrupt(int irq, void *dev_id)
3018c2ecf20Sopenharmony_ci{
3028c2ecf20Sopenharmony_ci	struct snd_bt87x *chip = dev_id;
3038c2ecf20Sopenharmony_ci	unsigned int status, irq_status;
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci	status = snd_bt87x_readl(chip, REG_INT_STAT);
3068c2ecf20Sopenharmony_ci	irq_status = status & chip->interrupt_mask;
3078c2ecf20Sopenharmony_ci	if (!irq_status)
3088c2ecf20Sopenharmony_ci		return IRQ_NONE;
3098c2ecf20Sopenharmony_ci	snd_bt87x_writel(chip, REG_INT_STAT, irq_status);
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci	if (irq_status & ERROR_INTERRUPTS) {
3128c2ecf20Sopenharmony_ci		if (irq_status & (INT_FBUS | INT_FTRGT))
3138c2ecf20Sopenharmony_ci			dev_warn(chip->card->dev,
3148c2ecf20Sopenharmony_ci				 "FIFO overrun, status %#08x\n", status);
3158c2ecf20Sopenharmony_ci		if (irq_status & INT_OCERR)
3168c2ecf20Sopenharmony_ci			dev_err(chip->card->dev,
3178c2ecf20Sopenharmony_ci				"internal RISC error, status %#08x\n", status);
3188c2ecf20Sopenharmony_ci		if (irq_status & (INT_PPERR | INT_RIPERR | INT_PABORT))
3198c2ecf20Sopenharmony_ci			snd_bt87x_pci_error(chip, irq_status);
3208c2ecf20Sopenharmony_ci	}
3218c2ecf20Sopenharmony_ci	if ((irq_status & INT_RISCI) && (chip->reg_control & CTL_ACAP_EN)) {
3228c2ecf20Sopenharmony_ci		int current_block, irq_block;
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci		/* assume that exactly one line has been recorded */
3258c2ecf20Sopenharmony_ci		chip->current_line = (chip->current_line + 1) % chip->lines;
3268c2ecf20Sopenharmony_ci		/* but check if some interrupts have been skipped */
3278c2ecf20Sopenharmony_ci		current_block = chip->current_line * 16 / chip->lines;
3288c2ecf20Sopenharmony_ci		irq_block = status >> INT_RISCS_SHIFT;
3298c2ecf20Sopenharmony_ci		if (current_block != irq_block)
3308c2ecf20Sopenharmony_ci			chip->current_line = (irq_block * chip->lines + 15) / 16;
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci		snd_pcm_period_elapsed(chip->substream);
3338c2ecf20Sopenharmony_ci	}
3348c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
3358c2ecf20Sopenharmony_ci}
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_cistatic const struct snd_pcm_hardware snd_bt87x_digital_hw = {
3388c2ecf20Sopenharmony_ci	.info = SNDRV_PCM_INFO_MMAP |
3398c2ecf20Sopenharmony_ci		SNDRV_PCM_INFO_INTERLEAVED |
3408c2ecf20Sopenharmony_ci		SNDRV_PCM_INFO_BLOCK_TRANSFER |
3418c2ecf20Sopenharmony_ci		SNDRV_PCM_INFO_MMAP_VALID |
3428c2ecf20Sopenharmony_ci		SNDRV_PCM_INFO_BATCH,
3438c2ecf20Sopenharmony_ci	.formats = SNDRV_PCM_FMTBIT_S16_LE,
3448c2ecf20Sopenharmony_ci	.rates = 0, /* set at runtime */
3458c2ecf20Sopenharmony_ci	.channels_min = 2,
3468c2ecf20Sopenharmony_ci	.channels_max = 2,
3478c2ecf20Sopenharmony_ci	.buffer_bytes_max = 255 * 4092,
3488c2ecf20Sopenharmony_ci	.period_bytes_min = 32,
3498c2ecf20Sopenharmony_ci	.period_bytes_max = 4092,
3508c2ecf20Sopenharmony_ci	.periods_min = 2,
3518c2ecf20Sopenharmony_ci	.periods_max = 255,
3528c2ecf20Sopenharmony_ci};
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_cistatic const struct snd_pcm_hardware snd_bt87x_analog_hw = {
3558c2ecf20Sopenharmony_ci	.info = SNDRV_PCM_INFO_MMAP |
3568c2ecf20Sopenharmony_ci		SNDRV_PCM_INFO_INTERLEAVED |
3578c2ecf20Sopenharmony_ci		SNDRV_PCM_INFO_BLOCK_TRANSFER |
3588c2ecf20Sopenharmony_ci		SNDRV_PCM_INFO_MMAP_VALID |
3598c2ecf20Sopenharmony_ci		SNDRV_PCM_INFO_BATCH,
3608c2ecf20Sopenharmony_ci	.formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S8,
3618c2ecf20Sopenharmony_ci	.rates = SNDRV_PCM_RATE_KNOT,
3628c2ecf20Sopenharmony_ci	.rate_min = ANALOG_CLOCK / CLOCK_DIV_MAX,
3638c2ecf20Sopenharmony_ci	.rate_max = ANALOG_CLOCK / CLOCK_DIV_MIN,
3648c2ecf20Sopenharmony_ci	.channels_min = 1,
3658c2ecf20Sopenharmony_ci	.channels_max = 1,
3668c2ecf20Sopenharmony_ci	.buffer_bytes_max = 255 * 4092,
3678c2ecf20Sopenharmony_ci	.period_bytes_min = 32,
3688c2ecf20Sopenharmony_ci	.period_bytes_max = 4092,
3698c2ecf20Sopenharmony_ci	.periods_min = 2,
3708c2ecf20Sopenharmony_ci	.periods_max = 255,
3718c2ecf20Sopenharmony_ci};
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_cistatic int snd_bt87x_set_digital_hw(struct snd_bt87x *chip, struct snd_pcm_runtime *runtime)
3748c2ecf20Sopenharmony_ci{
3758c2ecf20Sopenharmony_ci	chip->reg_control |= CTL_DA_IOM_DA | CTL_A_PWRDN;
3768c2ecf20Sopenharmony_ci	runtime->hw = snd_bt87x_digital_hw;
3778c2ecf20Sopenharmony_ci	runtime->hw.rates = snd_pcm_rate_to_rate_bit(chip->board.dig_rate);
3788c2ecf20Sopenharmony_ci	runtime->hw.rate_min = chip->board.dig_rate;
3798c2ecf20Sopenharmony_ci	runtime->hw.rate_max = chip->board.dig_rate;
3808c2ecf20Sopenharmony_ci	return 0;
3818c2ecf20Sopenharmony_ci}
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_cistatic int snd_bt87x_set_analog_hw(struct snd_bt87x *chip, struct snd_pcm_runtime *runtime)
3848c2ecf20Sopenharmony_ci{
3858c2ecf20Sopenharmony_ci	static const struct snd_ratnum analog_clock = {
3868c2ecf20Sopenharmony_ci		.num = ANALOG_CLOCK,
3878c2ecf20Sopenharmony_ci		.den_min = CLOCK_DIV_MIN,
3888c2ecf20Sopenharmony_ci		.den_max = CLOCK_DIV_MAX,
3898c2ecf20Sopenharmony_ci		.den_step = 1
3908c2ecf20Sopenharmony_ci	};
3918c2ecf20Sopenharmony_ci	static const struct snd_pcm_hw_constraint_ratnums constraint_rates = {
3928c2ecf20Sopenharmony_ci		.nrats = 1,
3938c2ecf20Sopenharmony_ci		.rats = &analog_clock
3948c2ecf20Sopenharmony_ci	};
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci	chip->reg_control &= ~(CTL_DA_IOM_DA | CTL_A_PWRDN);
3978c2ecf20Sopenharmony_ci	runtime->hw = snd_bt87x_analog_hw;
3988c2ecf20Sopenharmony_ci	return snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
3998c2ecf20Sopenharmony_ci					     &constraint_rates);
4008c2ecf20Sopenharmony_ci}
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_cistatic int snd_bt87x_pcm_open(struct snd_pcm_substream *substream)
4038c2ecf20Sopenharmony_ci{
4048c2ecf20Sopenharmony_ci	struct snd_bt87x *chip = snd_pcm_substream_chip(substream);
4058c2ecf20Sopenharmony_ci	struct snd_pcm_runtime *runtime = substream->runtime;
4068c2ecf20Sopenharmony_ci	int err;
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_ci	if (test_and_set_bit(0, &chip->opened))
4098c2ecf20Sopenharmony_ci		return -EBUSY;
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_ci	if (substream->pcm->device == DEVICE_DIGITAL)
4128c2ecf20Sopenharmony_ci		err = snd_bt87x_set_digital_hw(chip, runtime);
4138c2ecf20Sopenharmony_ci	else
4148c2ecf20Sopenharmony_ci		err = snd_bt87x_set_analog_hw(chip, runtime);
4158c2ecf20Sopenharmony_ci	if (err < 0)
4168c2ecf20Sopenharmony_ci		goto _error;
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_ci	err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
4198c2ecf20Sopenharmony_ci	if (err < 0)
4208c2ecf20Sopenharmony_ci		goto _error;
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_ci	chip->substream = substream;
4238c2ecf20Sopenharmony_ci	return 0;
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_ci_error:
4268c2ecf20Sopenharmony_ci	clear_bit(0, &chip->opened);
4278c2ecf20Sopenharmony_ci	smp_mb__after_atomic();
4288c2ecf20Sopenharmony_ci	return err;
4298c2ecf20Sopenharmony_ci}
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_cistatic int snd_bt87x_close(struct snd_pcm_substream *substream)
4328c2ecf20Sopenharmony_ci{
4338c2ecf20Sopenharmony_ci	struct snd_bt87x *chip = snd_pcm_substream_chip(substream);
4348c2ecf20Sopenharmony_ci
4358c2ecf20Sopenharmony_ci	spin_lock_irq(&chip->reg_lock);
4368c2ecf20Sopenharmony_ci	chip->reg_control |= CTL_A_PWRDN;
4378c2ecf20Sopenharmony_ci	snd_bt87x_writel(chip, REG_GPIO_DMA_CTL, chip->reg_control);
4388c2ecf20Sopenharmony_ci	spin_unlock_irq(&chip->reg_lock);
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_ci	chip->substream = NULL;
4418c2ecf20Sopenharmony_ci	clear_bit(0, &chip->opened);
4428c2ecf20Sopenharmony_ci	smp_mb__after_atomic();
4438c2ecf20Sopenharmony_ci	return 0;
4448c2ecf20Sopenharmony_ci}
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_cistatic int snd_bt87x_hw_params(struct snd_pcm_substream *substream,
4478c2ecf20Sopenharmony_ci			       struct snd_pcm_hw_params *hw_params)
4488c2ecf20Sopenharmony_ci{
4498c2ecf20Sopenharmony_ci	struct snd_bt87x *chip = snd_pcm_substream_chip(substream);
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_ci	return snd_bt87x_create_risc(chip, substream,
4528c2ecf20Sopenharmony_ci				     params_periods(hw_params),
4538c2ecf20Sopenharmony_ci				     params_period_bytes(hw_params));
4548c2ecf20Sopenharmony_ci}
4558c2ecf20Sopenharmony_ci
4568c2ecf20Sopenharmony_cistatic int snd_bt87x_hw_free(struct snd_pcm_substream *substream)
4578c2ecf20Sopenharmony_ci{
4588c2ecf20Sopenharmony_ci	struct snd_bt87x *chip = snd_pcm_substream_chip(substream);
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_ci	snd_bt87x_free_risc(chip);
4618c2ecf20Sopenharmony_ci	return 0;
4628c2ecf20Sopenharmony_ci}
4638c2ecf20Sopenharmony_ci
4648c2ecf20Sopenharmony_cistatic int snd_bt87x_prepare(struct snd_pcm_substream *substream)
4658c2ecf20Sopenharmony_ci{
4668c2ecf20Sopenharmony_ci	struct snd_bt87x *chip = snd_pcm_substream_chip(substream);
4678c2ecf20Sopenharmony_ci	struct snd_pcm_runtime *runtime = substream->runtime;
4688c2ecf20Sopenharmony_ci	int decimation;
4698c2ecf20Sopenharmony_ci
4708c2ecf20Sopenharmony_ci	spin_lock_irq(&chip->reg_lock);
4718c2ecf20Sopenharmony_ci	chip->reg_control &= ~(CTL_DA_SDR_MASK | CTL_DA_SBR);
4728c2ecf20Sopenharmony_ci	decimation = (ANALOG_CLOCK + runtime->rate / 4) / runtime->rate;
4738c2ecf20Sopenharmony_ci	chip->reg_control |= decimation << CTL_DA_SDR_SHIFT;
4748c2ecf20Sopenharmony_ci	if (runtime->format == SNDRV_PCM_FORMAT_S8)
4758c2ecf20Sopenharmony_ci		chip->reg_control |= CTL_DA_SBR;
4768c2ecf20Sopenharmony_ci	snd_bt87x_writel(chip, REG_GPIO_DMA_CTL, chip->reg_control);
4778c2ecf20Sopenharmony_ci	spin_unlock_irq(&chip->reg_lock);
4788c2ecf20Sopenharmony_ci	return 0;
4798c2ecf20Sopenharmony_ci}
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_cistatic int snd_bt87x_start(struct snd_bt87x *chip)
4828c2ecf20Sopenharmony_ci{
4838c2ecf20Sopenharmony_ci	spin_lock(&chip->reg_lock);
4848c2ecf20Sopenharmony_ci	chip->current_line = 0;
4858c2ecf20Sopenharmony_ci	chip->reg_control |= CTL_FIFO_ENABLE | CTL_RISC_ENABLE | CTL_ACAP_EN;
4868c2ecf20Sopenharmony_ci	snd_bt87x_writel(chip, REG_RISC_STRT_ADD, chip->dma_risc.addr);
4878c2ecf20Sopenharmony_ci	snd_bt87x_writel(chip, REG_PACKET_LEN,
4888c2ecf20Sopenharmony_ci			 chip->line_bytes | (chip->lines << 16));
4898c2ecf20Sopenharmony_ci	snd_bt87x_writel(chip, REG_INT_MASK, chip->interrupt_mask);
4908c2ecf20Sopenharmony_ci	snd_bt87x_writel(chip, REG_GPIO_DMA_CTL, chip->reg_control);
4918c2ecf20Sopenharmony_ci	spin_unlock(&chip->reg_lock);
4928c2ecf20Sopenharmony_ci	return 0;
4938c2ecf20Sopenharmony_ci}
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_cistatic int snd_bt87x_stop(struct snd_bt87x *chip)
4968c2ecf20Sopenharmony_ci{
4978c2ecf20Sopenharmony_ci	spin_lock(&chip->reg_lock);
4988c2ecf20Sopenharmony_ci	chip->reg_control &= ~(CTL_FIFO_ENABLE | CTL_RISC_ENABLE | CTL_ACAP_EN);
4998c2ecf20Sopenharmony_ci	snd_bt87x_writel(chip, REG_GPIO_DMA_CTL, chip->reg_control);
5008c2ecf20Sopenharmony_ci	snd_bt87x_writel(chip, REG_INT_MASK, 0);
5018c2ecf20Sopenharmony_ci	snd_bt87x_writel(chip, REG_INT_STAT, MY_INTERRUPTS);
5028c2ecf20Sopenharmony_ci	spin_unlock(&chip->reg_lock);
5038c2ecf20Sopenharmony_ci	return 0;
5048c2ecf20Sopenharmony_ci}
5058c2ecf20Sopenharmony_ci
5068c2ecf20Sopenharmony_cistatic int snd_bt87x_trigger(struct snd_pcm_substream *substream, int cmd)
5078c2ecf20Sopenharmony_ci{
5088c2ecf20Sopenharmony_ci	struct snd_bt87x *chip = snd_pcm_substream_chip(substream);
5098c2ecf20Sopenharmony_ci
5108c2ecf20Sopenharmony_ci	switch (cmd) {
5118c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_START:
5128c2ecf20Sopenharmony_ci		return snd_bt87x_start(chip);
5138c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_STOP:
5148c2ecf20Sopenharmony_ci		return snd_bt87x_stop(chip);
5158c2ecf20Sopenharmony_ci	default:
5168c2ecf20Sopenharmony_ci		return -EINVAL;
5178c2ecf20Sopenharmony_ci	}
5188c2ecf20Sopenharmony_ci}
5198c2ecf20Sopenharmony_ci
5208c2ecf20Sopenharmony_cistatic snd_pcm_uframes_t snd_bt87x_pointer(struct snd_pcm_substream *substream)
5218c2ecf20Sopenharmony_ci{
5228c2ecf20Sopenharmony_ci	struct snd_bt87x *chip = snd_pcm_substream_chip(substream);
5238c2ecf20Sopenharmony_ci	struct snd_pcm_runtime *runtime = substream->runtime;
5248c2ecf20Sopenharmony_ci
5258c2ecf20Sopenharmony_ci	return (snd_pcm_uframes_t)bytes_to_frames(runtime, chip->current_line * chip->line_bytes);
5268c2ecf20Sopenharmony_ci}
5278c2ecf20Sopenharmony_ci
5288c2ecf20Sopenharmony_cistatic const struct snd_pcm_ops snd_bt87x_pcm_ops = {
5298c2ecf20Sopenharmony_ci	.open = snd_bt87x_pcm_open,
5308c2ecf20Sopenharmony_ci	.close = snd_bt87x_close,
5318c2ecf20Sopenharmony_ci	.hw_params = snd_bt87x_hw_params,
5328c2ecf20Sopenharmony_ci	.hw_free = snd_bt87x_hw_free,
5338c2ecf20Sopenharmony_ci	.prepare = snd_bt87x_prepare,
5348c2ecf20Sopenharmony_ci	.trigger = snd_bt87x_trigger,
5358c2ecf20Sopenharmony_ci	.pointer = snd_bt87x_pointer,
5368c2ecf20Sopenharmony_ci};
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_cistatic int snd_bt87x_capture_volume_info(struct snd_kcontrol *kcontrol,
5398c2ecf20Sopenharmony_ci					 struct snd_ctl_elem_info *info)
5408c2ecf20Sopenharmony_ci{
5418c2ecf20Sopenharmony_ci	info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
5428c2ecf20Sopenharmony_ci	info->count = 1;
5438c2ecf20Sopenharmony_ci	info->value.integer.min = 0;
5448c2ecf20Sopenharmony_ci	info->value.integer.max = 15;
5458c2ecf20Sopenharmony_ci	return 0;
5468c2ecf20Sopenharmony_ci}
5478c2ecf20Sopenharmony_ci
5488c2ecf20Sopenharmony_cistatic int snd_bt87x_capture_volume_get(struct snd_kcontrol *kcontrol,
5498c2ecf20Sopenharmony_ci					struct snd_ctl_elem_value *value)
5508c2ecf20Sopenharmony_ci{
5518c2ecf20Sopenharmony_ci	struct snd_bt87x *chip = snd_kcontrol_chip(kcontrol);
5528c2ecf20Sopenharmony_ci
5538c2ecf20Sopenharmony_ci	value->value.integer.value[0] = (chip->reg_control & CTL_A_GAIN_MASK) >> CTL_A_GAIN_SHIFT;
5548c2ecf20Sopenharmony_ci	return 0;
5558c2ecf20Sopenharmony_ci}
5568c2ecf20Sopenharmony_ci
5578c2ecf20Sopenharmony_cistatic int snd_bt87x_capture_volume_put(struct snd_kcontrol *kcontrol,
5588c2ecf20Sopenharmony_ci					struct snd_ctl_elem_value *value)
5598c2ecf20Sopenharmony_ci{
5608c2ecf20Sopenharmony_ci	struct snd_bt87x *chip = snd_kcontrol_chip(kcontrol);
5618c2ecf20Sopenharmony_ci	u32 old_control;
5628c2ecf20Sopenharmony_ci	int changed;
5638c2ecf20Sopenharmony_ci
5648c2ecf20Sopenharmony_ci	spin_lock_irq(&chip->reg_lock);
5658c2ecf20Sopenharmony_ci	old_control = chip->reg_control;
5668c2ecf20Sopenharmony_ci	chip->reg_control = (chip->reg_control & ~CTL_A_GAIN_MASK)
5678c2ecf20Sopenharmony_ci		| (value->value.integer.value[0] << CTL_A_GAIN_SHIFT);
5688c2ecf20Sopenharmony_ci	snd_bt87x_writel(chip, REG_GPIO_DMA_CTL, chip->reg_control);
5698c2ecf20Sopenharmony_ci	changed = old_control != chip->reg_control;
5708c2ecf20Sopenharmony_ci	spin_unlock_irq(&chip->reg_lock);
5718c2ecf20Sopenharmony_ci	return changed;
5728c2ecf20Sopenharmony_ci}
5738c2ecf20Sopenharmony_ci
5748c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new snd_bt87x_capture_volume = {
5758c2ecf20Sopenharmony_ci	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
5768c2ecf20Sopenharmony_ci	.name = "Capture Volume",
5778c2ecf20Sopenharmony_ci	.info = snd_bt87x_capture_volume_info,
5788c2ecf20Sopenharmony_ci	.get = snd_bt87x_capture_volume_get,
5798c2ecf20Sopenharmony_ci	.put = snd_bt87x_capture_volume_put,
5808c2ecf20Sopenharmony_ci};
5818c2ecf20Sopenharmony_ci
5828c2ecf20Sopenharmony_ci#define snd_bt87x_capture_boost_info	snd_ctl_boolean_mono_info
5838c2ecf20Sopenharmony_ci
5848c2ecf20Sopenharmony_cistatic int snd_bt87x_capture_boost_get(struct snd_kcontrol *kcontrol,
5858c2ecf20Sopenharmony_ci				       struct snd_ctl_elem_value *value)
5868c2ecf20Sopenharmony_ci{
5878c2ecf20Sopenharmony_ci	struct snd_bt87x *chip = snd_kcontrol_chip(kcontrol);
5888c2ecf20Sopenharmony_ci
5898c2ecf20Sopenharmony_ci	value->value.integer.value[0] = !! (chip->reg_control & CTL_A_G2X);
5908c2ecf20Sopenharmony_ci	return 0;
5918c2ecf20Sopenharmony_ci}
5928c2ecf20Sopenharmony_ci
5938c2ecf20Sopenharmony_cistatic int snd_bt87x_capture_boost_put(struct snd_kcontrol *kcontrol,
5948c2ecf20Sopenharmony_ci				       struct snd_ctl_elem_value *value)
5958c2ecf20Sopenharmony_ci{
5968c2ecf20Sopenharmony_ci	struct snd_bt87x *chip = snd_kcontrol_chip(kcontrol);
5978c2ecf20Sopenharmony_ci	u32 old_control;
5988c2ecf20Sopenharmony_ci	int changed;
5998c2ecf20Sopenharmony_ci
6008c2ecf20Sopenharmony_ci	spin_lock_irq(&chip->reg_lock);
6018c2ecf20Sopenharmony_ci	old_control = chip->reg_control;
6028c2ecf20Sopenharmony_ci	chip->reg_control = (chip->reg_control & ~CTL_A_G2X)
6038c2ecf20Sopenharmony_ci		| (value->value.integer.value[0] ? CTL_A_G2X : 0);
6048c2ecf20Sopenharmony_ci	snd_bt87x_writel(chip, REG_GPIO_DMA_CTL, chip->reg_control);
6058c2ecf20Sopenharmony_ci	changed = chip->reg_control != old_control;
6068c2ecf20Sopenharmony_ci	spin_unlock_irq(&chip->reg_lock);
6078c2ecf20Sopenharmony_ci	return changed;
6088c2ecf20Sopenharmony_ci}
6098c2ecf20Sopenharmony_ci
6108c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new snd_bt87x_capture_boost = {
6118c2ecf20Sopenharmony_ci	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6128c2ecf20Sopenharmony_ci	.name = "Capture Boost",
6138c2ecf20Sopenharmony_ci	.info = snd_bt87x_capture_boost_info,
6148c2ecf20Sopenharmony_ci	.get = snd_bt87x_capture_boost_get,
6158c2ecf20Sopenharmony_ci	.put = snd_bt87x_capture_boost_put,
6168c2ecf20Sopenharmony_ci};
6178c2ecf20Sopenharmony_ci
6188c2ecf20Sopenharmony_cistatic int snd_bt87x_capture_source_info(struct snd_kcontrol *kcontrol,
6198c2ecf20Sopenharmony_ci					 struct snd_ctl_elem_info *info)
6208c2ecf20Sopenharmony_ci{
6218c2ecf20Sopenharmony_ci	static const char *const texts[3] = {"TV Tuner", "FM", "Mic/Line"};
6228c2ecf20Sopenharmony_ci
6238c2ecf20Sopenharmony_ci	return snd_ctl_enum_info(info, 1, 3, texts);
6248c2ecf20Sopenharmony_ci}
6258c2ecf20Sopenharmony_ci
6268c2ecf20Sopenharmony_cistatic int snd_bt87x_capture_source_get(struct snd_kcontrol *kcontrol,
6278c2ecf20Sopenharmony_ci					struct snd_ctl_elem_value *value)
6288c2ecf20Sopenharmony_ci{
6298c2ecf20Sopenharmony_ci	struct snd_bt87x *chip = snd_kcontrol_chip(kcontrol);
6308c2ecf20Sopenharmony_ci
6318c2ecf20Sopenharmony_ci	value->value.enumerated.item[0] = (chip->reg_control & CTL_A_SEL_MASK) >> CTL_A_SEL_SHIFT;
6328c2ecf20Sopenharmony_ci	return 0;
6338c2ecf20Sopenharmony_ci}
6348c2ecf20Sopenharmony_ci
6358c2ecf20Sopenharmony_cistatic int snd_bt87x_capture_source_put(struct snd_kcontrol *kcontrol,
6368c2ecf20Sopenharmony_ci					struct snd_ctl_elem_value *value)
6378c2ecf20Sopenharmony_ci{
6388c2ecf20Sopenharmony_ci	struct snd_bt87x *chip = snd_kcontrol_chip(kcontrol);
6398c2ecf20Sopenharmony_ci	u32 old_control;
6408c2ecf20Sopenharmony_ci	int changed;
6418c2ecf20Sopenharmony_ci
6428c2ecf20Sopenharmony_ci	spin_lock_irq(&chip->reg_lock);
6438c2ecf20Sopenharmony_ci	old_control = chip->reg_control;
6448c2ecf20Sopenharmony_ci	chip->reg_control = (chip->reg_control & ~CTL_A_SEL_MASK)
6458c2ecf20Sopenharmony_ci		| (value->value.enumerated.item[0] << CTL_A_SEL_SHIFT);
6468c2ecf20Sopenharmony_ci	snd_bt87x_writel(chip, REG_GPIO_DMA_CTL, chip->reg_control);
6478c2ecf20Sopenharmony_ci	changed = chip->reg_control != old_control;
6488c2ecf20Sopenharmony_ci	spin_unlock_irq(&chip->reg_lock);
6498c2ecf20Sopenharmony_ci	return changed;
6508c2ecf20Sopenharmony_ci}
6518c2ecf20Sopenharmony_ci
6528c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new snd_bt87x_capture_source = {
6538c2ecf20Sopenharmony_ci	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
6548c2ecf20Sopenharmony_ci	.name = "Capture Source",
6558c2ecf20Sopenharmony_ci	.info = snd_bt87x_capture_source_info,
6568c2ecf20Sopenharmony_ci	.get = snd_bt87x_capture_source_get,
6578c2ecf20Sopenharmony_ci	.put = snd_bt87x_capture_source_put,
6588c2ecf20Sopenharmony_ci};
6598c2ecf20Sopenharmony_ci
6608c2ecf20Sopenharmony_cistatic int snd_bt87x_free(struct snd_bt87x *chip)
6618c2ecf20Sopenharmony_ci{
6628c2ecf20Sopenharmony_ci	if (chip->mmio)
6638c2ecf20Sopenharmony_ci		snd_bt87x_stop(chip);
6648c2ecf20Sopenharmony_ci	if (chip->irq >= 0)
6658c2ecf20Sopenharmony_ci		free_irq(chip->irq, chip);
6668c2ecf20Sopenharmony_ci	iounmap(chip->mmio);
6678c2ecf20Sopenharmony_ci	pci_release_regions(chip->pci);
6688c2ecf20Sopenharmony_ci	pci_disable_device(chip->pci);
6698c2ecf20Sopenharmony_ci	kfree(chip);
6708c2ecf20Sopenharmony_ci	return 0;
6718c2ecf20Sopenharmony_ci}
6728c2ecf20Sopenharmony_ci
6738c2ecf20Sopenharmony_cistatic int snd_bt87x_dev_free(struct snd_device *device)
6748c2ecf20Sopenharmony_ci{
6758c2ecf20Sopenharmony_ci	struct snd_bt87x *chip = device->device_data;
6768c2ecf20Sopenharmony_ci	return snd_bt87x_free(chip);
6778c2ecf20Sopenharmony_ci}
6788c2ecf20Sopenharmony_ci
6798c2ecf20Sopenharmony_cistatic int snd_bt87x_pcm(struct snd_bt87x *chip, int device, char *name)
6808c2ecf20Sopenharmony_ci{
6818c2ecf20Sopenharmony_ci	int err;
6828c2ecf20Sopenharmony_ci	struct snd_pcm *pcm;
6838c2ecf20Sopenharmony_ci
6848c2ecf20Sopenharmony_ci	err = snd_pcm_new(chip->card, name, device, 0, 1, &pcm);
6858c2ecf20Sopenharmony_ci	if (err < 0)
6868c2ecf20Sopenharmony_ci		return err;
6878c2ecf20Sopenharmony_ci	pcm->private_data = chip;
6888c2ecf20Sopenharmony_ci	strcpy(pcm->name, name);
6898c2ecf20Sopenharmony_ci	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_bt87x_pcm_ops);
6908c2ecf20Sopenharmony_ci	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
6918c2ecf20Sopenharmony_ci				       &chip->pci->dev,
6928c2ecf20Sopenharmony_ci				       128 * 1024,
6938c2ecf20Sopenharmony_ci				       ALIGN(255 * 4092, 1024));
6948c2ecf20Sopenharmony_ci	return 0;
6958c2ecf20Sopenharmony_ci}
6968c2ecf20Sopenharmony_ci
6978c2ecf20Sopenharmony_cistatic int snd_bt87x_create(struct snd_card *card,
6988c2ecf20Sopenharmony_ci			    struct pci_dev *pci,
6998c2ecf20Sopenharmony_ci			    struct snd_bt87x **rchip)
7008c2ecf20Sopenharmony_ci{
7018c2ecf20Sopenharmony_ci	struct snd_bt87x *chip;
7028c2ecf20Sopenharmony_ci	int err;
7038c2ecf20Sopenharmony_ci	static const struct snd_device_ops ops = {
7048c2ecf20Sopenharmony_ci		.dev_free = snd_bt87x_dev_free
7058c2ecf20Sopenharmony_ci	};
7068c2ecf20Sopenharmony_ci
7078c2ecf20Sopenharmony_ci	*rchip = NULL;
7088c2ecf20Sopenharmony_ci
7098c2ecf20Sopenharmony_ci	err = pci_enable_device(pci);
7108c2ecf20Sopenharmony_ci	if (err < 0)
7118c2ecf20Sopenharmony_ci		return err;
7128c2ecf20Sopenharmony_ci
7138c2ecf20Sopenharmony_ci	chip = kzalloc(sizeof(*chip), GFP_KERNEL);
7148c2ecf20Sopenharmony_ci	if (!chip) {
7158c2ecf20Sopenharmony_ci		pci_disable_device(pci);
7168c2ecf20Sopenharmony_ci		return -ENOMEM;
7178c2ecf20Sopenharmony_ci	}
7188c2ecf20Sopenharmony_ci	chip->card = card;
7198c2ecf20Sopenharmony_ci	chip->pci = pci;
7208c2ecf20Sopenharmony_ci	chip->irq = -1;
7218c2ecf20Sopenharmony_ci	spin_lock_init(&chip->reg_lock);
7228c2ecf20Sopenharmony_ci
7238c2ecf20Sopenharmony_ci	if ((err = pci_request_regions(pci, "Bt87x audio")) < 0) {
7248c2ecf20Sopenharmony_ci		kfree(chip);
7258c2ecf20Sopenharmony_ci		pci_disable_device(pci);
7268c2ecf20Sopenharmony_ci		return err;
7278c2ecf20Sopenharmony_ci	}
7288c2ecf20Sopenharmony_ci	chip->mmio = pci_ioremap_bar(pci, 0);
7298c2ecf20Sopenharmony_ci	if (!chip->mmio) {
7308c2ecf20Sopenharmony_ci		dev_err(card->dev, "cannot remap io memory\n");
7318c2ecf20Sopenharmony_ci		err = -ENOMEM;
7328c2ecf20Sopenharmony_ci		goto fail;
7338c2ecf20Sopenharmony_ci	}
7348c2ecf20Sopenharmony_ci
7358c2ecf20Sopenharmony_ci	chip->reg_control = CTL_A_PWRDN | CTL_DA_ES2 |
7368c2ecf20Sopenharmony_ci			    CTL_PKTP_16 | (15 << CTL_DA_SDR_SHIFT);
7378c2ecf20Sopenharmony_ci	chip->interrupt_mask = MY_INTERRUPTS;
7388c2ecf20Sopenharmony_ci	snd_bt87x_writel(chip, REG_GPIO_DMA_CTL, chip->reg_control);
7398c2ecf20Sopenharmony_ci	snd_bt87x_writel(chip, REG_INT_MASK, 0);
7408c2ecf20Sopenharmony_ci	snd_bt87x_writel(chip, REG_INT_STAT, MY_INTERRUPTS);
7418c2ecf20Sopenharmony_ci
7428c2ecf20Sopenharmony_ci	err = request_irq(pci->irq, snd_bt87x_interrupt, IRQF_SHARED,
7438c2ecf20Sopenharmony_ci			  KBUILD_MODNAME, chip);
7448c2ecf20Sopenharmony_ci	if (err < 0) {
7458c2ecf20Sopenharmony_ci		dev_err(card->dev, "cannot grab irq %d\n", pci->irq);
7468c2ecf20Sopenharmony_ci		goto fail;
7478c2ecf20Sopenharmony_ci	}
7488c2ecf20Sopenharmony_ci	chip->irq = pci->irq;
7498c2ecf20Sopenharmony_ci	card->sync_irq = chip->irq;
7508c2ecf20Sopenharmony_ci	pci_set_master(pci);
7518c2ecf20Sopenharmony_ci
7528c2ecf20Sopenharmony_ci	err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
7538c2ecf20Sopenharmony_ci	if (err < 0)
7548c2ecf20Sopenharmony_ci		goto fail;
7558c2ecf20Sopenharmony_ci
7568c2ecf20Sopenharmony_ci	*rchip = chip;
7578c2ecf20Sopenharmony_ci	return 0;
7588c2ecf20Sopenharmony_ci
7598c2ecf20Sopenharmony_cifail:
7608c2ecf20Sopenharmony_ci	snd_bt87x_free(chip);
7618c2ecf20Sopenharmony_ci	return err;
7628c2ecf20Sopenharmony_ci}
7638c2ecf20Sopenharmony_ci
7648c2ecf20Sopenharmony_ci#define BT_DEVICE(chip, subvend, subdev, id) \
7658c2ecf20Sopenharmony_ci	{ .vendor = PCI_VENDOR_ID_BROOKTREE, \
7668c2ecf20Sopenharmony_ci	  .device = chip, \
7678c2ecf20Sopenharmony_ci	  .subvendor = subvend, .subdevice = subdev, \
7688c2ecf20Sopenharmony_ci	  .driver_data = SND_BT87X_BOARD_ ## id }
7698c2ecf20Sopenharmony_ci/* driver_data is the card id for that device */
7708c2ecf20Sopenharmony_ci
7718c2ecf20Sopenharmony_cistatic const struct pci_device_id snd_bt87x_ids[] = {
7728c2ecf20Sopenharmony_ci	/* Hauppauge WinTV series */
7738c2ecf20Sopenharmony_ci	BT_DEVICE(PCI_DEVICE_ID_BROOKTREE_878, 0x0070, 0x13eb, GENERIC),
7748c2ecf20Sopenharmony_ci	/* Hauppauge WinTV series */
7758c2ecf20Sopenharmony_ci	BT_DEVICE(PCI_DEVICE_ID_BROOKTREE_879, 0x0070, 0x13eb, GENERIC),
7768c2ecf20Sopenharmony_ci	/* Viewcast Osprey 200 */
7778c2ecf20Sopenharmony_ci	BT_DEVICE(PCI_DEVICE_ID_BROOKTREE_878, 0x0070, 0xff01, OSPREY2x0),
7788c2ecf20Sopenharmony_ci	/* Viewcast Osprey 440 (rate is configurable via gpio) */
7798c2ecf20Sopenharmony_ci	BT_DEVICE(PCI_DEVICE_ID_BROOKTREE_878, 0x0070, 0xff07, OSPREY440),
7808c2ecf20Sopenharmony_ci	/* ATI TV-Wonder */
7818c2ecf20Sopenharmony_ci	BT_DEVICE(PCI_DEVICE_ID_BROOKTREE_878, 0x1002, 0x0001, GENERIC),
7828c2ecf20Sopenharmony_ci	/* Leadtek Winfast tv 2000xp delux */
7838c2ecf20Sopenharmony_ci	BT_DEVICE(PCI_DEVICE_ID_BROOKTREE_878, 0x107d, 0x6606, GENERIC),
7848c2ecf20Sopenharmony_ci	/* Pinnacle PCTV */
7858c2ecf20Sopenharmony_ci	BT_DEVICE(PCI_DEVICE_ID_BROOKTREE_878, 0x11bd, 0x0012, GENERIC),
7868c2ecf20Sopenharmony_ci	/* Voodoo TV 200 */
7878c2ecf20Sopenharmony_ci	BT_DEVICE(PCI_DEVICE_ID_BROOKTREE_878, 0x121a, 0x3000, GENERIC),
7888c2ecf20Sopenharmony_ci	/* Askey Computer Corp. MagicTView'99 */
7898c2ecf20Sopenharmony_ci	BT_DEVICE(PCI_DEVICE_ID_BROOKTREE_878, 0x144f, 0x3000, GENERIC),
7908c2ecf20Sopenharmony_ci	/* AVerMedia Studio No. 103, 203, ...? */
7918c2ecf20Sopenharmony_ci	BT_DEVICE(PCI_DEVICE_ID_BROOKTREE_878, 0x1461, 0x0003, AVPHONE98),
7928c2ecf20Sopenharmony_ci	/* Prolink PixelView PV-M4900 */
7938c2ecf20Sopenharmony_ci	BT_DEVICE(PCI_DEVICE_ID_BROOKTREE_878, 0x1554, 0x4011, GENERIC),
7948c2ecf20Sopenharmony_ci	/* Pinnacle  Studio PCTV rave */
7958c2ecf20Sopenharmony_ci	BT_DEVICE(PCI_DEVICE_ID_BROOKTREE_878, 0xbd11, 0x1200, GENERIC),
7968c2ecf20Sopenharmony_ci	{ }
7978c2ecf20Sopenharmony_ci};
7988c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(pci, snd_bt87x_ids);
7998c2ecf20Sopenharmony_ci
8008c2ecf20Sopenharmony_ci/* cards known not to have audio
8018c2ecf20Sopenharmony_ci * (DVB cards use the audio function to transfer MPEG data) */
8028c2ecf20Sopenharmony_cistatic struct {
8038c2ecf20Sopenharmony_ci	unsigned short subvendor, subdevice;
8048c2ecf20Sopenharmony_ci} denylist[] = {
8058c2ecf20Sopenharmony_ci	{0x0071, 0x0101}, /* Nebula Electronics DigiTV */
8068c2ecf20Sopenharmony_ci	{0x11bd, 0x001c}, /* Pinnacle PCTV Sat */
8078c2ecf20Sopenharmony_ci	{0x11bd, 0x0026}, /* Pinnacle PCTV SAT CI */
8088c2ecf20Sopenharmony_ci	{0x1461, 0x0761}, /* AVermedia AverTV DVB-T */
8098c2ecf20Sopenharmony_ci	{0x1461, 0x0771}, /* AVermedia DVB-T 771 */
8108c2ecf20Sopenharmony_ci	{0x1822, 0x0001}, /* Twinhan VisionPlus DVB-T */
8118c2ecf20Sopenharmony_ci	{0x18ac, 0xd500}, /* DVICO FusionHDTV 5 Lite */
8128c2ecf20Sopenharmony_ci	{0x18ac, 0xdb10}, /* DVICO FusionHDTV DVB-T Lite */
8138c2ecf20Sopenharmony_ci	{0x18ac, 0xdb11}, /* Ultraview DVB-T Lite */
8148c2ecf20Sopenharmony_ci	{0x270f, 0xfc00}, /* Chaintech Digitop DST-1000 DVB-S */
8158c2ecf20Sopenharmony_ci	{0x7063, 0x2000}, /* pcHDTV HD-2000 TV */
8168c2ecf20Sopenharmony_ci};
8178c2ecf20Sopenharmony_ci
8188c2ecf20Sopenharmony_cistatic struct pci_driver driver;
8198c2ecf20Sopenharmony_ci
8208c2ecf20Sopenharmony_ci/* return the id of the card, or a negative value if it's on the denylist */
8218c2ecf20Sopenharmony_cistatic int snd_bt87x_detect_card(struct pci_dev *pci)
8228c2ecf20Sopenharmony_ci{
8238c2ecf20Sopenharmony_ci	int i;
8248c2ecf20Sopenharmony_ci	const struct pci_device_id *supported;
8258c2ecf20Sopenharmony_ci
8268c2ecf20Sopenharmony_ci	supported = pci_match_id(snd_bt87x_ids, pci);
8278c2ecf20Sopenharmony_ci	if (supported && supported->driver_data > 0)
8288c2ecf20Sopenharmony_ci		return supported->driver_data;
8298c2ecf20Sopenharmony_ci
8308c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(denylist); ++i)
8318c2ecf20Sopenharmony_ci		if (denylist[i].subvendor == pci->subsystem_vendor &&
8328c2ecf20Sopenharmony_ci		    denylist[i].subdevice == pci->subsystem_device) {
8338c2ecf20Sopenharmony_ci			dev_dbg(&pci->dev,
8348c2ecf20Sopenharmony_ci				"card %#04x-%#04x:%#04x has no audio\n",
8358c2ecf20Sopenharmony_ci				    pci->device, pci->subsystem_vendor, pci->subsystem_device);
8368c2ecf20Sopenharmony_ci			return -EBUSY;
8378c2ecf20Sopenharmony_ci		}
8388c2ecf20Sopenharmony_ci
8398c2ecf20Sopenharmony_ci	dev_info(&pci->dev, "unknown card %#04x-%#04x:%#04x\n",
8408c2ecf20Sopenharmony_ci		   pci->device, pci->subsystem_vendor, pci->subsystem_device);
8418c2ecf20Sopenharmony_ci	dev_info(&pci->dev, "please mail id, board name, and, "
8428c2ecf20Sopenharmony_ci		   "if it works, the correct digital_rate option to "
8438c2ecf20Sopenharmony_ci		   "<alsa-devel@alsa-project.org>\n");
8448c2ecf20Sopenharmony_ci	return SND_BT87X_BOARD_UNKNOWN;
8458c2ecf20Sopenharmony_ci}
8468c2ecf20Sopenharmony_ci
8478c2ecf20Sopenharmony_cistatic int snd_bt87x_probe(struct pci_dev *pci,
8488c2ecf20Sopenharmony_ci			   const struct pci_device_id *pci_id)
8498c2ecf20Sopenharmony_ci{
8508c2ecf20Sopenharmony_ci	static int dev;
8518c2ecf20Sopenharmony_ci	struct snd_card *card;
8528c2ecf20Sopenharmony_ci	struct snd_bt87x *chip;
8538c2ecf20Sopenharmony_ci	int err;
8548c2ecf20Sopenharmony_ci	enum snd_bt87x_boardid boardid;
8558c2ecf20Sopenharmony_ci
8568c2ecf20Sopenharmony_ci	if (!pci_id->driver_data) {
8578c2ecf20Sopenharmony_ci		err = snd_bt87x_detect_card(pci);
8588c2ecf20Sopenharmony_ci		if (err < 0)
8598c2ecf20Sopenharmony_ci			return -ENODEV;
8608c2ecf20Sopenharmony_ci		boardid = err;
8618c2ecf20Sopenharmony_ci	} else
8628c2ecf20Sopenharmony_ci		boardid = pci_id->driver_data;
8638c2ecf20Sopenharmony_ci
8648c2ecf20Sopenharmony_ci	if (dev >= SNDRV_CARDS)
8658c2ecf20Sopenharmony_ci		return -ENODEV;
8668c2ecf20Sopenharmony_ci	if (!enable[dev]) {
8678c2ecf20Sopenharmony_ci		++dev;
8688c2ecf20Sopenharmony_ci		return -ENOENT;
8698c2ecf20Sopenharmony_ci	}
8708c2ecf20Sopenharmony_ci
8718c2ecf20Sopenharmony_ci	err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
8728c2ecf20Sopenharmony_ci			   0, &card);
8738c2ecf20Sopenharmony_ci	if (err < 0)
8748c2ecf20Sopenharmony_ci		return err;
8758c2ecf20Sopenharmony_ci
8768c2ecf20Sopenharmony_ci	err = snd_bt87x_create(card, pci, &chip);
8778c2ecf20Sopenharmony_ci	if (err < 0)
8788c2ecf20Sopenharmony_ci		goto _error;
8798c2ecf20Sopenharmony_ci
8808c2ecf20Sopenharmony_ci	memcpy(&chip->board, &snd_bt87x_boards[boardid], sizeof(chip->board));
8818c2ecf20Sopenharmony_ci
8828c2ecf20Sopenharmony_ci	if (!chip->board.no_digital) {
8838c2ecf20Sopenharmony_ci		if (digital_rate[dev] > 0)
8848c2ecf20Sopenharmony_ci			chip->board.dig_rate = digital_rate[dev];
8858c2ecf20Sopenharmony_ci
8868c2ecf20Sopenharmony_ci		chip->reg_control |= chip->board.digital_fmt;
8878c2ecf20Sopenharmony_ci
8888c2ecf20Sopenharmony_ci		err = snd_bt87x_pcm(chip, DEVICE_DIGITAL, "Bt87x Digital");
8898c2ecf20Sopenharmony_ci		if (err < 0)
8908c2ecf20Sopenharmony_ci			goto _error;
8918c2ecf20Sopenharmony_ci	}
8928c2ecf20Sopenharmony_ci	if (!chip->board.no_analog) {
8938c2ecf20Sopenharmony_ci		err = snd_bt87x_pcm(chip, DEVICE_ANALOG, "Bt87x Analog");
8948c2ecf20Sopenharmony_ci		if (err < 0)
8958c2ecf20Sopenharmony_ci			goto _error;
8968c2ecf20Sopenharmony_ci		err = snd_ctl_add(card, snd_ctl_new1(
8978c2ecf20Sopenharmony_ci				  &snd_bt87x_capture_volume, chip));
8988c2ecf20Sopenharmony_ci		if (err < 0)
8998c2ecf20Sopenharmony_ci			goto _error;
9008c2ecf20Sopenharmony_ci		err = snd_ctl_add(card, snd_ctl_new1(
9018c2ecf20Sopenharmony_ci				  &snd_bt87x_capture_boost, chip));
9028c2ecf20Sopenharmony_ci		if (err < 0)
9038c2ecf20Sopenharmony_ci			goto _error;
9048c2ecf20Sopenharmony_ci		err = snd_ctl_add(card, snd_ctl_new1(
9058c2ecf20Sopenharmony_ci				  &snd_bt87x_capture_source, chip));
9068c2ecf20Sopenharmony_ci		if (err < 0)
9078c2ecf20Sopenharmony_ci			goto _error;
9088c2ecf20Sopenharmony_ci	}
9098c2ecf20Sopenharmony_ci	dev_info(card->dev, "bt87x%d: Using board %d, %sanalog, %sdigital "
9108c2ecf20Sopenharmony_ci		   "(rate %d Hz)\n", dev, boardid,
9118c2ecf20Sopenharmony_ci		   chip->board.no_analog ? "no " : "",
9128c2ecf20Sopenharmony_ci		   chip->board.no_digital ? "no " : "", chip->board.dig_rate);
9138c2ecf20Sopenharmony_ci
9148c2ecf20Sopenharmony_ci	strcpy(card->driver, "Bt87x");
9158c2ecf20Sopenharmony_ci	sprintf(card->shortname, "Brooktree Bt%x", pci->device);
9168c2ecf20Sopenharmony_ci	sprintf(card->longname, "%s at %#llx, irq %i",
9178c2ecf20Sopenharmony_ci		card->shortname, (unsigned long long)pci_resource_start(pci, 0),
9188c2ecf20Sopenharmony_ci		chip->irq);
9198c2ecf20Sopenharmony_ci	strcpy(card->mixername, "Bt87x");
9208c2ecf20Sopenharmony_ci
9218c2ecf20Sopenharmony_ci	err = snd_card_register(card);
9228c2ecf20Sopenharmony_ci	if (err < 0)
9238c2ecf20Sopenharmony_ci		goto _error;
9248c2ecf20Sopenharmony_ci
9258c2ecf20Sopenharmony_ci	pci_set_drvdata(pci, card);
9268c2ecf20Sopenharmony_ci	++dev;
9278c2ecf20Sopenharmony_ci	return 0;
9288c2ecf20Sopenharmony_ci
9298c2ecf20Sopenharmony_ci_error:
9308c2ecf20Sopenharmony_ci	snd_card_free(card);
9318c2ecf20Sopenharmony_ci	return err;
9328c2ecf20Sopenharmony_ci}
9338c2ecf20Sopenharmony_ci
9348c2ecf20Sopenharmony_cistatic void snd_bt87x_remove(struct pci_dev *pci)
9358c2ecf20Sopenharmony_ci{
9368c2ecf20Sopenharmony_ci	snd_card_free(pci_get_drvdata(pci));
9378c2ecf20Sopenharmony_ci}
9388c2ecf20Sopenharmony_ci
9398c2ecf20Sopenharmony_ci/* default entries for all Bt87x cards - it's not exported */
9408c2ecf20Sopenharmony_ci/* driver_data is set to 0 to call detection */
9418c2ecf20Sopenharmony_cistatic const struct pci_device_id snd_bt87x_default_ids[] = {
9428c2ecf20Sopenharmony_ci	BT_DEVICE(PCI_DEVICE_ID_BROOKTREE_878, PCI_ANY_ID, PCI_ANY_ID, UNKNOWN),
9438c2ecf20Sopenharmony_ci	BT_DEVICE(PCI_DEVICE_ID_BROOKTREE_879, PCI_ANY_ID, PCI_ANY_ID, UNKNOWN),
9448c2ecf20Sopenharmony_ci	{ }
9458c2ecf20Sopenharmony_ci};
9468c2ecf20Sopenharmony_ci
9478c2ecf20Sopenharmony_cistatic struct pci_driver driver = {
9488c2ecf20Sopenharmony_ci	.name = KBUILD_MODNAME,
9498c2ecf20Sopenharmony_ci	.id_table = snd_bt87x_ids,
9508c2ecf20Sopenharmony_ci	.probe = snd_bt87x_probe,
9518c2ecf20Sopenharmony_ci	.remove = snd_bt87x_remove,
9528c2ecf20Sopenharmony_ci};
9538c2ecf20Sopenharmony_ci
9548c2ecf20Sopenharmony_cistatic int __init alsa_card_bt87x_init(void)
9558c2ecf20Sopenharmony_ci{
9568c2ecf20Sopenharmony_ci	if (load_all)
9578c2ecf20Sopenharmony_ci		driver.id_table = snd_bt87x_default_ids;
9588c2ecf20Sopenharmony_ci	return pci_register_driver(&driver);
9598c2ecf20Sopenharmony_ci}
9608c2ecf20Sopenharmony_ci
9618c2ecf20Sopenharmony_cistatic void __exit alsa_card_bt87x_exit(void)
9628c2ecf20Sopenharmony_ci{
9638c2ecf20Sopenharmony_ci	pci_unregister_driver(&driver);
9648c2ecf20Sopenharmony_ci}
9658c2ecf20Sopenharmony_ci
9668c2ecf20Sopenharmony_cimodule_init(alsa_card_bt87x_init)
9678c2ecf20Sopenharmony_cimodule_exit(alsa_card_bt87x_exit)
968