18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  Driver for Ensoniq ES1370/ES1371 AudioPCI soundcard
48c2ecf20Sopenharmony_ci *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>,
58c2ecf20Sopenharmony_ci *		     Thomas Sailer <sailer@ife.ee.ethz.ch>
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci/* Power-Management-Code ( CONFIG_PM )
98c2ecf20Sopenharmony_ci * for ens1371 only ( FIXME )
108c2ecf20Sopenharmony_ci * derived from cs4281.c, atiixp.c and via82xx.c
118c2ecf20Sopenharmony_ci * using http://www.alsa-project.org/~tiwai/writing-an-alsa-driver/
128c2ecf20Sopenharmony_ci * by Kurt J. Bosch
138c2ecf20Sopenharmony_ci */
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include <linux/io.h>
168c2ecf20Sopenharmony_ci#include <linux/delay.h>
178c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
188c2ecf20Sopenharmony_ci#include <linux/init.h>
198c2ecf20Sopenharmony_ci#include <linux/pci.h>
208c2ecf20Sopenharmony_ci#include <linux/slab.h>
218c2ecf20Sopenharmony_ci#include <linux/gameport.h>
228c2ecf20Sopenharmony_ci#include <linux/module.h>
238c2ecf20Sopenharmony_ci#include <linux/mutex.h>
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#include <sound/core.h>
268c2ecf20Sopenharmony_ci#include <sound/control.h>
278c2ecf20Sopenharmony_ci#include <sound/pcm.h>
288c2ecf20Sopenharmony_ci#include <sound/rawmidi.h>
298c2ecf20Sopenharmony_ci#ifdef CHIP1371
308c2ecf20Sopenharmony_ci#include <sound/ac97_codec.h>
318c2ecf20Sopenharmony_ci#else
328c2ecf20Sopenharmony_ci#include <sound/ak4531_codec.h>
338c2ecf20Sopenharmony_ci#endif
348c2ecf20Sopenharmony_ci#include <sound/initval.h>
358c2ecf20Sopenharmony_ci#include <sound/asoundef.h>
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci#ifndef CHIP1371
388c2ecf20Sopenharmony_ci#undef CHIP1370
398c2ecf20Sopenharmony_ci#define CHIP1370
408c2ecf20Sopenharmony_ci#endif
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci#ifdef CHIP1370
438c2ecf20Sopenharmony_ci#define DRIVER_NAME "ENS1370"
448c2ecf20Sopenharmony_ci#define CHIP_NAME "ES1370" /* it can be ENS but just to keep compatibility... */
458c2ecf20Sopenharmony_ci#else
468c2ecf20Sopenharmony_ci#define DRIVER_NAME "ENS1371"
478c2ecf20Sopenharmony_ci#define CHIP_NAME "ES1371"
488c2ecf20Sopenharmony_ci#endif
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ciMODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Thomas Sailer <sailer@ife.ee.ethz.ch>");
528c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
538c2ecf20Sopenharmony_ci#ifdef CHIP1370
548c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Ensoniq AudioPCI ES1370");
558c2ecf20Sopenharmony_ciMODULE_SUPPORTED_DEVICE("{{Ensoniq,AudioPCI-97 ES1370},"
568c2ecf20Sopenharmony_ci	        "{Creative Labs,SB PCI64/128 (ES1370)}}");
578c2ecf20Sopenharmony_ci#endif
588c2ecf20Sopenharmony_ci#ifdef CHIP1371
598c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Ensoniq/Creative AudioPCI ES1371+");
608c2ecf20Sopenharmony_ciMODULE_SUPPORTED_DEVICE("{{Ensoniq,AudioPCI ES1371/73},"
618c2ecf20Sopenharmony_ci		"{Ensoniq,AudioPCI ES1373},"
628c2ecf20Sopenharmony_ci		"{Creative Labs,Ectiva EV1938},"
638c2ecf20Sopenharmony_ci		"{Creative Labs,SB PCI64/128 (ES1371/73)},"
648c2ecf20Sopenharmony_ci		"{Creative Labs,Vibra PCI128},"
658c2ecf20Sopenharmony_ci		"{Ectiva,EV1938}}");
668c2ecf20Sopenharmony_ci#endif
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci#if IS_REACHABLE(CONFIG_GAMEPORT)
698c2ecf20Sopenharmony_ci#define SUPPORT_JOYSTICK
708c2ecf20Sopenharmony_ci#endif
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_cistatic int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	/* Index 0-MAX */
738c2ecf20Sopenharmony_cistatic char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* ID for this card */
748c2ecf20Sopenharmony_cistatic bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;	/* Enable switches */
758c2ecf20Sopenharmony_ci#ifdef SUPPORT_JOYSTICK
768c2ecf20Sopenharmony_ci#ifdef CHIP1371
778c2ecf20Sopenharmony_cistatic int joystick_port[SNDRV_CARDS];
788c2ecf20Sopenharmony_ci#else
798c2ecf20Sopenharmony_cistatic bool joystick[SNDRV_CARDS];
808c2ecf20Sopenharmony_ci#endif
818c2ecf20Sopenharmony_ci#endif
828c2ecf20Sopenharmony_ci#ifdef CHIP1371
838c2ecf20Sopenharmony_cistatic int spdif[SNDRV_CARDS];
848c2ecf20Sopenharmony_cistatic int lineio[SNDRV_CARDS];
858c2ecf20Sopenharmony_ci#endif
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_cimodule_param_array(index, int, NULL, 0444);
888c2ecf20Sopenharmony_ciMODULE_PARM_DESC(index, "Index value for Ensoniq AudioPCI soundcard.");
898c2ecf20Sopenharmony_cimodule_param_array(id, charp, NULL, 0444);
908c2ecf20Sopenharmony_ciMODULE_PARM_DESC(id, "ID string for Ensoniq AudioPCI soundcard.");
918c2ecf20Sopenharmony_cimodule_param_array(enable, bool, NULL, 0444);
928c2ecf20Sopenharmony_ciMODULE_PARM_DESC(enable, "Enable Ensoniq AudioPCI soundcard.");
938c2ecf20Sopenharmony_ci#ifdef SUPPORT_JOYSTICK
948c2ecf20Sopenharmony_ci#ifdef CHIP1371
958c2ecf20Sopenharmony_cimodule_param_hw_array(joystick_port, int, ioport, NULL, 0444);
968c2ecf20Sopenharmony_ciMODULE_PARM_DESC(joystick_port, "Joystick port address.");
978c2ecf20Sopenharmony_ci#else
988c2ecf20Sopenharmony_cimodule_param_array(joystick, bool, NULL, 0444);
998c2ecf20Sopenharmony_ciMODULE_PARM_DESC(joystick, "Enable joystick.");
1008c2ecf20Sopenharmony_ci#endif
1018c2ecf20Sopenharmony_ci#endif /* SUPPORT_JOYSTICK */
1028c2ecf20Sopenharmony_ci#ifdef CHIP1371
1038c2ecf20Sopenharmony_cimodule_param_array(spdif, int, NULL, 0444);
1048c2ecf20Sopenharmony_ciMODULE_PARM_DESC(spdif, "S/PDIF output (-1 = none, 0 = auto, 1 = force).");
1058c2ecf20Sopenharmony_cimodule_param_array(lineio, int, NULL, 0444);
1068c2ecf20Sopenharmony_ciMODULE_PARM_DESC(lineio, "Line In to Rear Out (0 = auto, 1 = force).");
1078c2ecf20Sopenharmony_ci#endif
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci/* ES1371 chip ID */
1108c2ecf20Sopenharmony_ci/* This is a little confusing because all ES1371 compatible chips have the
1118c2ecf20Sopenharmony_ci   same DEVICE_ID, the only thing differentiating them is the REV_ID field.
1128c2ecf20Sopenharmony_ci   This is only significant if you want to enable features on the later parts.
1138c2ecf20Sopenharmony_ci   Yes, I know it's stupid and why didn't we use the sub IDs?
1148c2ecf20Sopenharmony_ci*/
1158c2ecf20Sopenharmony_ci#define ES1371REV_ES1373_A  0x04
1168c2ecf20Sopenharmony_ci#define ES1371REV_ES1373_B  0x06
1178c2ecf20Sopenharmony_ci#define ES1371REV_CT5880_A  0x07
1188c2ecf20Sopenharmony_ci#define CT5880REV_CT5880_C  0x02
1198c2ecf20Sopenharmony_ci#define CT5880REV_CT5880_D  0x03	/* ??? -jk */
1208c2ecf20Sopenharmony_ci#define CT5880REV_CT5880_E  0x04	/* mw */
1218c2ecf20Sopenharmony_ci#define ES1371REV_ES1371_B  0x09
1228c2ecf20Sopenharmony_ci#define EV1938REV_EV1938_A  0x00
1238c2ecf20Sopenharmony_ci#define ES1371REV_ES1373_8  0x08
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci/*
1268c2ecf20Sopenharmony_ci * Direct registers
1278c2ecf20Sopenharmony_ci */
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci#define ES_REG(ensoniq, x) ((ensoniq)->port + ES_REG_##x)
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci#define ES_REG_CONTROL	0x00	/* R/W: Interrupt/Chip select control register */
1328c2ecf20Sopenharmony_ci#define   ES_1370_ADC_STOP	(1<<31)		/* disable capture buffer transfers */
1338c2ecf20Sopenharmony_ci#define   ES_1370_XCTL1 	(1<<30)		/* general purpose output bit */
1348c2ecf20Sopenharmony_ci#define   ES_1373_BYPASS_P1	(1<<31)		/* bypass SRC for PB1 */
1358c2ecf20Sopenharmony_ci#define   ES_1373_BYPASS_P2	(1<<30)		/* bypass SRC for PB2 */
1368c2ecf20Sopenharmony_ci#define   ES_1373_BYPASS_R	(1<<29)		/* bypass SRC for REC */
1378c2ecf20Sopenharmony_ci#define   ES_1373_TEST_BIT	(1<<28)		/* should be set to 0 for normal operation */
1388c2ecf20Sopenharmony_ci#define   ES_1373_RECEN_B	(1<<27)		/* mix record with playback for I2S/SPDIF out */
1398c2ecf20Sopenharmony_ci#define   ES_1373_SPDIF_THRU	(1<<26)		/* 0 = SPDIF thru mode, 1 = SPDIF == dig out */
1408c2ecf20Sopenharmony_ci#define   ES_1371_JOY_ASEL(o)	(((o)&0x03)<<24)/* joystick port mapping */
1418c2ecf20Sopenharmony_ci#define   ES_1371_JOY_ASELM	(0x03<<24)	/* mask for above */
1428c2ecf20Sopenharmony_ci#define   ES_1371_JOY_ASELI(i)  (((i)>>24)&0x03)
1438c2ecf20Sopenharmony_ci#define   ES_1371_GPIO_IN(i)	(((i)>>20)&0x0f)/* GPIO in [3:0] pins - R/O */
1448c2ecf20Sopenharmony_ci#define   ES_1370_PCLKDIVO(o)	(((o)&0x1fff)<<16)/* clock divide ratio for DAC2 */
1458c2ecf20Sopenharmony_ci#define   ES_1370_PCLKDIVM	((0x1fff)<<16)	/* mask for above */
1468c2ecf20Sopenharmony_ci#define   ES_1370_PCLKDIVI(i)	(((i)>>16)&0x1fff)/* clock divide ratio for DAC2 */
1478c2ecf20Sopenharmony_ci#define   ES_1371_GPIO_OUT(o)	(((o)&0x0f)<<16)/* GPIO out [3:0] pins - W/R */
1488c2ecf20Sopenharmony_ci#define   ES_1371_GPIO_OUTM     (0x0f<<16)	/* mask for above */
1498c2ecf20Sopenharmony_ci#define   ES_MSFMTSEL		(1<<15)		/* MPEG serial data format; 0 = SONY, 1 = I2S */
1508c2ecf20Sopenharmony_ci#define   ES_1370_M_SBB		(1<<14)		/* clock source for DAC - 0 = clock generator; 1 = MPEG clocks */
1518c2ecf20Sopenharmony_ci#define   ES_1371_SYNC_RES	(1<<14)		/* Warm AC97 reset */
1528c2ecf20Sopenharmony_ci#define   ES_1370_WTSRSEL(o)	(((o)&0x03)<<12)/* fixed frequency clock for DAC1 */
1538c2ecf20Sopenharmony_ci#define   ES_1370_WTSRSELM	(0x03<<12)	/* mask for above */
1548c2ecf20Sopenharmony_ci#define   ES_1371_ADC_STOP	(1<<13)		/* disable CCB transfer capture information */
1558c2ecf20Sopenharmony_ci#define   ES_1371_PWR_INTRM	(1<<12)		/* power level change interrupts enable */
1568c2ecf20Sopenharmony_ci#define   ES_1370_DAC_SYNC	(1<<11)		/* DAC's are synchronous */
1578c2ecf20Sopenharmony_ci#define   ES_1371_M_CB		(1<<11)		/* capture clock source; 0 = AC'97 ADC; 1 = I2S */
1588c2ecf20Sopenharmony_ci#define   ES_CCB_INTRM		(1<<10)		/* CCB voice interrupts enable */
1598c2ecf20Sopenharmony_ci#define   ES_1370_M_CB		(1<<9)		/* capture clock source; 0 = ADC; 1 = MPEG */
1608c2ecf20Sopenharmony_ci#define   ES_1370_XCTL0		(1<<8)		/* generap purpose output bit */
1618c2ecf20Sopenharmony_ci#define   ES_1371_PDLEV(o)	(((o)&0x03)<<8)	/* current power down level */
1628c2ecf20Sopenharmony_ci#define   ES_1371_PDLEVM	(0x03<<8)	/* mask for above */
1638c2ecf20Sopenharmony_ci#define   ES_BREQ		(1<<7)		/* memory bus request enable */
1648c2ecf20Sopenharmony_ci#define   ES_DAC1_EN		(1<<6)		/* DAC1 playback channel enable */
1658c2ecf20Sopenharmony_ci#define   ES_DAC2_EN		(1<<5)		/* DAC2 playback channel enable */
1668c2ecf20Sopenharmony_ci#define   ES_ADC_EN		(1<<4)		/* ADC capture channel enable */
1678c2ecf20Sopenharmony_ci#define   ES_UART_EN		(1<<3)		/* UART enable */
1688c2ecf20Sopenharmony_ci#define   ES_JYSTK_EN		(1<<2)		/* Joystick module enable */
1698c2ecf20Sopenharmony_ci#define   ES_1370_CDC_EN	(1<<1)		/* Codec interface enable */
1708c2ecf20Sopenharmony_ci#define   ES_1371_XTALCKDIS	(1<<1)		/* Xtal clock disable */
1718c2ecf20Sopenharmony_ci#define   ES_1370_SERR_DISABLE	(1<<0)		/* PCI serr signal disable */
1728c2ecf20Sopenharmony_ci#define   ES_1371_PCICLKDIS     (1<<0)		/* PCI clock disable */
1738c2ecf20Sopenharmony_ci#define ES_REG_STATUS	0x04	/* R/O: Interrupt/Chip select status register */
1748c2ecf20Sopenharmony_ci#define   ES_INTR               (1<<31)		/* Interrupt is pending */
1758c2ecf20Sopenharmony_ci#define   ES_1371_ST_AC97_RST	(1<<29)		/* CT5880 AC'97 Reset bit */
1768c2ecf20Sopenharmony_ci#define   ES_1373_REAR_BIT27	(1<<27)		/* rear bits: 000 - front, 010 - mirror, 101 - separate */
1778c2ecf20Sopenharmony_ci#define   ES_1373_REAR_BIT26	(1<<26)
1788c2ecf20Sopenharmony_ci#define   ES_1373_REAR_BIT24	(1<<24)
1798c2ecf20Sopenharmony_ci#define   ES_1373_GPIO_INT_EN(o)(((o)&0x0f)<<20)/* GPIO [3:0] pins - interrupt enable */
1808c2ecf20Sopenharmony_ci#define   ES_1373_SPDIF_EN	(1<<18)		/* SPDIF enable */
1818c2ecf20Sopenharmony_ci#define   ES_1373_SPDIF_TEST	(1<<17)		/* SPDIF test */
1828c2ecf20Sopenharmony_ci#define   ES_1371_TEST          (1<<16)		/* test ASIC */
1838c2ecf20Sopenharmony_ci#define   ES_1373_GPIO_INT(i)	(((i)&0x0f)>>12)/* GPIO [3:0] pins - interrupt pending */
1848c2ecf20Sopenharmony_ci#define   ES_1370_CSTAT		(1<<10)		/* CODEC is busy or register write in progress */
1858c2ecf20Sopenharmony_ci#define   ES_1370_CBUSY         (1<<9)		/* CODEC is busy */
1868c2ecf20Sopenharmony_ci#define   ES_1370_CWRIP		(1<<8)		/* CODEC register write in progress */
1878c2ecf20Sopenharmony_ci#define   ES_1371_SYNC_ERR	(1<<8)		/* CODEC synchronization error occurred */
1888c2ecf20Sopenharmony_ci#define   ES_1371_VC(i)         (((i)>>6)&0x03)	/* voice code from CCB module */
1898c2ecf20Sopenharmony_ci#define   ES_1370_VC(i)		(((i)>>5)&0x03)	/* voice code from CCB module */
1908c2ecf20Sopenharmony_ci#define   ES_1371_MPWR          (1<<5)		/* power level interrupt pending */
1918c2ecf20Sopenharmony_ci#define   ES_MCCB		(1<<4)		/* CCB interrupt pending */
1928c2ecf20Sopenharmony_ci#define   ES_UART		(1<<3)		/* UART interrupt pending */
1938c2ecf20Sopenharmony_ci#define   ES_DAC1		(1<<2)		/* DAC1 channel interrupt pending */
1948c2ecf20Sopenharmony_ci#define   ES_DAC2		(1<<1)		/* DAC2 channel interrupt pending */
1958c2ecf20Sopenharmony_ci#define   ES_ADC		(1<<0)		/* ADC channel interrupt pending */
1968c2ecf20Sopenharmony_ci#define ES_REG_UART_DATA 0x08	/* R/W: UART data register */
1978c2ecf20Sopenharmony_ci#define ES_REG_UART_STATUS 0x09	/* R/O: UART status register */
1988c2ecf20Sopenharmony_ci#define   ES_RXINT		(1<<7)		/* RX interrupt occurred */
1998c2ecf20Sopenharmony_ci#define   ES_TXINT		(1<<2)		/* TX interrupt occurred */
2008c2ecf20Sopenharmony_ci#define   ES_TXRDY		(1<<1)		/* transmitter ready */
2018c2ecf20Sopenharmony_ci#define   ES_RXRDY		(1<<0)		/* receiver ready */
2028c2ecf20Sopenharmony_ci#define ES_REG_UART_CONTROL 0x09	/* W/O: UART control register */
2038c2ecf20Sopenharmony_ci#define   ES_RXINTEN		(1<<7)		/* RX interrupt enable */
2048c2ecf20Sopenharmony_ci#define   ES_TXINTENO(o)	(((o)&0x03)<<5)	/* TX interrupt enable */
2058c2ecf20Sopenharmony_ci#define   ES_TXINTENM		(0x03<<5)	/* mask for above */
2068c2ecf20Sopenharmony_ci#define   ES_TXINTENI(i)	(((i)>>5)&0x03)
2078c2ecf20Sopenharmony_ci#define   ES_CNTRL(o)		(((o)&0x03)<<0)	/* control */
2088c2ecf20Sopenharmony_ci#define   ES_CNTRLM		(0x03<<0)	/* mask for above */
2098c2ecf20Sopenharmony_ci#define ES_REG_UART_RES	0x0a	/* R/W: UART reserver register */
2108c2ecf20Sopenharmony_ci#define   ES_TEST_MODE		(1<<0)		/* test mode enabled */
2118c2ecf20Sopenharmony_ci#define ES_REG_MEM_PAGE	0x0c	/* R/W: Memory page register */
2128c2ecf20Sopenharmony_ci#define   ES_MEM_PAGEO(o)	(((o)&0x0f)<<0)	/* memory page select - out */
2138c2ecf20Sopenharmony_ci#define   ES_MEM_PAGEM		(0x0f<<0)	/* mask for above */
2148c2ecf20Sopenharmony_ci#define   ES_MEM_PAGEI(i)	(((i)>>0)&0x0f) /* memory page select - in */
2158c2ecf20Sopenharmony_ci#define ES_REG_1370_CODEC 0x10	/* W/O: Codec write register address */
2168c2ecf20Sopenharmony_ci#define   ES_1370_CODEC_WRITE(a,d) ((((a)&0xff)<<8)|(((d)&0xff)<<0))
2178c2ecf20Sopenharmony_ci#define ES_REG_1371_CODEC 0x14	/* W/R: Codec Read/Write register address */
2188c2ecf20Sopenharmony_ci#define   ES_1371_CODEC_RDY	   (1<<31)	/* codec ready */
2198c2ecf20Sopenharmony_ci#define   ES_1371_CODEC_WIP	   (1<<30)	/* codec register access in progress */
2208c2ecf20Sopenharmony_ci#define   EV_1938_CODEC_MAGIC	   (1<<26)
2218c2ecf20Sopenharmony_ci#define   ES_1371_CODEC_PIRD	   (1<<23)	/* codec read/write select register */
2228c2ecf20Sopenharmony_ci#define   ES_1371_CODEC_WRITE(a,d) ((((a)&0x7f)<<16)|(((d)&0xffff)<<0))
2238c2ecf20Sopenharmony_ci#define   ES_1371_CODEC_READS(a)   ((((a)&0x7f)<<16)|ES_1371_CODEC_PIRD)
2248c2ecf20Sopenharmony_ci#define   ES_1371_CODEC_READ(i)    (((i)>>0)&0xffff)
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci#define ES_REG_1371_SMPRATE 0x10	/* W/R: Codec rate converter interface register */
2278c2ecf20Sopenharmony_ci#define   ES_1371_SRC_RAM_ADDRO(o) (((o)&0x7f)<<25)/* address of the sample rate converter */
2288c2ecf20Sopenharmony_ci#define   ES_1371_SRC_RAM_ADDRM	   (0x7f<<25)	/* mask for above */
2298c2ecf20Sopenharmony_ci#define   ES_1371_SRC_RAM_ADDRI(i) (((i)>>25)&0x7f)/* address of the sample rate converter */
2308c2ecf20Sopenharmony_ci#define   ES_1371_SRC_RAM_WE	   (1<<24)	/* R/W: read/write control for sample rate converter */
2318c2ecf20Sopenharmony_ci#define   ES_1371_SRC_RAM_BUSY     (1<<23)	/* R/O: sample rate memory is busy */
2328c2ecf20Sopenharmony_ci#define   ES_1371_SRC_DISABLE      (1<<22)	/* sample rate converter disable */
2338c2ecf20Sopenharmony_ci#define   ES_1371_DIS_P1	   (1<<21)	/* playback channel 1 accumulator update disable */
2348c2ecf20Sopenharmony_ci#define   ES_1371_DIS_P2	   (1<<20)	/* playback channel 1 accumulator update disable */
2358c2ecf20Sopenharmony_ci#define   ES_1371_DIS_R1	   (1<<19)	/* capture channel accumulator update disable */
2368c2ecf20Sopenharmony_ci#define   ES_1371_SRC_RAM_DATAO(o) (((o)&0xffff)<<0)/* current value of the sample rate converter */
2378c2ecf20Sopenharmony_ci#define   ES_1371_SRC_RAM_DATAM	   (0xffff<<0)	/* mask for above */
2388c2ecf20Sopenharmony_ci#define   ES_1371_SRC_RAM_DATAI(i) (((i)>>0)&0xffff)/* current value of the sample rate converter */
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci#define ES_REG_1371_LEGACY 0x18	/* W/R: Legacy control/status register */
2418c2ecf20Sopenharmony_ci#define   ES_1371_JFAST		(1<<31)		/* fast joystick timing */
2428c2ecf20Sopenharmony_ci#define   ES_1371_HIB		(1<<30)		/* host interrupt blocking enable */
2438c2ecf20Sopenharmony_ci#define   ES_1371_VSB		(1<<29)		/* SB; 0 = addr 0x220xH, 1 = 0x22FxH */
2448c2ecf20Sopenharmony_ci#define   ES_1371_VMPUO(o)	(((o)&0x03)<<27)/* base register address; 0 = 0x320xH; 1 = 0x330xH; 2 = 0x340xH; 3 = 0x350xH */
2458c2ecf20Sopenharmony_ci#define   ES_1371_VMPUM		(0x03<<27)	/* mask for above */
2468c2ecf20Sopenharmony_ci#define   ES_1371_VMPUI(i)	(((i)>>27)&0x03)/* base register address */
2478c2ecf20Sopenharmony_ci#define   ES_1371_VCDCO(o)	(((o)&0x03)<<25)/* CODEC; 0 = 0x530xH; 1 = undefined; 2 = 0xe80xH; 3 = 0xF40xH */
2488c2ecf20Sopenharmony_ci#define   ES_1371_VCDCM		(0x03<<25)	/* mask for above */
2498c2ecf20Sopenharmony_ci#define   ES_1371_VCDCI(i)	(((i)>>25)&0x03)/* CODEC address */
2508c2ecf20Sopenharmony_ci#define   ES_1371_FIRQ		(1<<24)		/* force an interrupt */
2518c2ecf20Sopenharmony_ci#define   ES_1371_SDMACAP	(1<<23)		/* enable event capture for slave DMA controller */
2528c2ecf20Sopenharmony_ci#define   ES_1371_SPICAP	(1<<22)		/* enable event capture for slave IRQ controller */
2538c2ecf20Sopenharmony_ci#define   ES_1371_MDMACAP	(1<<21)		/* enable event capture for master DMA controller */
2548c2ecf20Sopenharmony_ci#define   ES_1371_MPICAP	(1<<20)		/* enable event capture for master IRQ controller */
2558c2ecf20Sopenharmony_ci#define   ES_1371_ADCAP		(1<<19)		/* enable event capture for ADLIB register; 0x388xH */
2568c2ecf20Sopenharmony_ci#define   ES_1371_SVCAP		(1<<18)		/* enable event capture for SB registers */
2578c2ecf20Sopenharmony_ci#define   ES_1371_CDCCAP	(1<<17)		/* enable event capture for CODEC registers */
2588c2ecf20Sopenharmony_ci#define   ES_1371_BACAP		(1<<16)		/* enable event capture for SoundScape base address */
2598c2ecf20Sopenharmony_ci#define   ES_1371_EXI(i)	(((i)>>8)&0x07)	/* event number */
2608c2ecf20Sopenharmony_ci#define   ES_1371_AI(i)		(((i)>>3)&0x1f)	/* event significant I/O address */
2618c2ecf20Sopenharmony_ci#define   ES_1371_WR		(1<<2)	/* event capture; 0 = read; 1 = write */
2628c2ecf20Sopenharmony_ci#define   ES_1371_LEGINT	(1<<0)	/* interrupt for legacy events; 0 = interrupt did occur */
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci#define ES_REG_CHANNEL_STATUS 0x1c /* R/W: first 32-bits from S/PDIF channel status block, es1373 */
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ci#define ES_REG_SERIAL	0x20	/* R/W: Serial interface control register */
2678c2ecf20Sopenharmony_ci#define   ES_1371_DAC_TEST	(1<<22)		/* DAC test mode enable */
2688c2ecf20Sopenharmony_ci#define   ES_P2_END_INCO(o)	(((o)&0x07)<<19)/* binary offset value to increment / loop end */
2698c2ecf20Sopenharmony_ci#define   ES_P2_END_INCM	(0x07<<19)	/* mask for above */
2708c2ecf20Sopenharmony_ci#define   ES_P2_END_INCI(i)	(((i)>>16)&0x07)/* binary offset value to increment / loop end */
2718c2ecf20Sopenharmony_ci#define   ES_P2_ST_INCO(o)	(((o)&0x07)<<16)/* binary offset value to increment / start */
2728c2ecf20Sopenharmony_ci#define   ES_P2_ST_INCM		(0x07<<16)	/* mask for above */
2738c2ecf20Sopenharmony_ci#define   ES_P2_ST_INCI(i)	(((i)<<16)&0x07)/* binary offset value to increment / start */
2748c2ecf20Sopenharmony_ci#define   ES_R1_LOOP_SEL	(1<<15)		/* ADC; 0 - loop mode; 1 = stop mode */
2758c2ecf20Sopenharmony_ci#define   ES_P2_LOOP_SEL	(1<<14)		/* DAC2; 0 - loop mode; 1 = stop mode */
2768c2ecf20Sopenharmony_ci#define   ES_P1_LOOP_SEL	(1<<13)		/* DAC1; 0 - loop mode; 1 = stop mode */
2778c2ecf20Sopenharmony_ci#define   ES_P2_PAUSE		(1<<12)		/* DAC2; 0 - play mode; 1 = pause mode */
2788c2ecf20Sopenharmony_ci#define   ES_P1_PAUSE		(1<<11)		/* DAC1; 0 - play mode; 1 = pause mode */
2798c2ecf20Sopenharmony_ci#define   ES_R1_INT_EN		(1<<10)		/* ADC interrupt enable */
2808c2ecf20Sopenharmony_ci#define   ES_P2_INT_EN		(1<<9)		/* DAC2 interrupt enable */
2818c2ecf20Sopenharmony_ci#define   ES_P1_INT_EN		(1<<8)		/* DAC1 interrupt enable */
2828c2ecf20Sopenharmony_ci#define   ES_P1_SCT_RLD		(1<<7)		/* force sample counter reload for DAC1 */
2838c2ecf20Sopenharmony_ci#define   ES_P2_DAC_SEN		(1<<6)		/* when stop mode: 0 - DAC2 play back zeros; 1 = DAC2 play back last sample */
2848c2ecf20Sopenharmony_ci#define   ES_R1_MODEO(o)	(((o)&0x03)<<4)	/* ADC mode; 0 = 8-bit mono; 1 = 8-bit stereo; 2 = 16-bit mono; 3 = 16-bit stereo */
2858c2ecf20Sopenharmony_ci#define   ES_R1_MODEM		(0x03<<4)	/* mask for above */
2868c2ecf20Sopenharmony_ci#define   ES_R1_MODEI(i)	(((i)>>4)&0x03)
2878c2ecf20Sopenharmony_ci#define   ES_P2_MODEO(o)	(((o)&0x03)<<2)	/* DAC2 mode; -- '' -- */
2888c2ecf20Sopenharmony_ci#define   ES_P2_MODEM		(0x03<<2)	/* mask for above */
2898c2ecf20Sopenharmony_ci#define   ES_P2_MODEI(i)	(((i)>>2)&0x03)
2908c2ecf20Sopenharmony_ci#define   ES_P1_MODEO(o)	(((o)&0x03)<<0)	/* DAC1 mode; -- '' -- */
2918c2ecf20Sopenharmony_ci#define   ES_P1_MODEM		(0x03<<0)	/* mask for above */
2928c2ecf20Sopenharmony_ci#define   ES_P1_MODEI(i)	(((i)>>0)&0x03)
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci#define ES_REG_DAC1_COUNT 0x24	/* R/W: DAC1 sample count register */
2958c2ecf20Sopenharmony_ci#define ES_REG_DAC2_COUNT 0x28	/* R/W: DAC2 sample count register */
2968c2ecf20Sopenharmony_ci#define ES_REG_ADC_COUNT  0x2c	/* R/W: ADC sample count register */
2978c2ecf20Sopenharmony_ci#define   ES_REG_CURR_COUNT(i)  (((i)>>16)&0xffff)
2988c2ecf20Sopenharmony_ci#define   ES_REG_COUNTO(o)	(((o)&0xffff)<<0)
2998c2ecf20Sopenharmony_ci#define   ES_REG_COUNTM		(0xffff<<0)
3008c2ecf20Sopenharmony_ci#define   ES_REG_COUNTI(i)	(((i)>>0)&0xffff)
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci#define ES_REG_DAC1_FRAME 0x30	/* R/W: PAGE 0x0c; DAC1 frame address */
3038c2ecf20Sopenharmony_ci#define ES_REG_DAC1_SIZE  0x34	/* R/W: PAGE 0x0c; DAC1 frame size */
3048c2ecf20Sopenharmony_ci#define ES_REG_DAC2_FRAME 0x38	/* R/W: PAGE 0x0c; DAC2 frame address */
3058c2ecf20Sopenharmony_ci#define ES_REG_DAC2_SIZE  0x3c	/* R/W: PAGE 0x0c; DAC2 frame size */
3068c2ecf20Sopenharmony_ci#define ES_REG_ADC_FRAME  0x30	/* R/W: PAGE 0x0d; ADC frame address */
3078c2ecf20Sopenharmony_ci#define ES_REG_ADC_SIZE	  0x34	/* R/W: PAGE 0x0d; ADC frame size */
3088c2ecf20Sopenharmony_ci#define   ES_REG_FCURR_COUNTO(o) (((o)&0xffff)<<16)
3098c2ecf20Sopenharmony_ci#define   ES_REG_FCURR_COUNTM    (0xffff<<16)
3108c2ecf20Sopenharmony_ci#define   ES_REG_FCURR_COUNTI(i) (((i)>>14)&0x3fffc)
3118c2ecf20Sopenharmony_ci#define   ES_REG_FSIZEO(o)	 (((o)&0xffff)<<0)
3128c2ecf20Sopenharmony_ci#define   ES_REG_FSIZEM		 (0xffff<<0)
3138c2ecf20Sopenharmony_ci#define   ES_REG_FSIZEI(i)	 (((i)>>0)&0xffff)
3148c2ecf20Sopenharmony_ci#define ES_REG_PHANTOM_FRAME 0x38 /* R/W: PAGE 0x0d: phantom frame address */
3158c2ecf20Sopenharmony_ci#define ES_REG_PHANTOM_COUNT 0x3c /* R/W: PAGE 0x0d: phantom frame count */
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci#define ES_REG_UART_FIFO  0x30	/* R/W: PAGE 0x0e; UART FIFO register */
3188c2ecf20Sopenharmony_ci#define   ES_REG_UF_VALID	 (1<<8)
3198c2ecf20Sopenharmony_ci#define   ES_REG_UF_BYTEO(o)	 (((o)&0xff)<<0)
3208c2ecf20Sopenharmony_ci#define   ES_REG_UF_BYTEM	 (0xff<<0)
3218c2ecf20Sopenharmony_ci#define   ES_REG_UF_BYTEI(i)	 (((i)>>0)&0xff)
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci/*
3258c2ecf20Sopenharmony_ci *  Pages
3268c2ecf20Sopenharmony_ci */
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_ci#define ES_PAGE_DAC	0x0c
3298c2ecf20Sopenharmony_ci#define ES_PAGE_ADC	0x0d
3308c2ecf20Sopenharmony_ci#define ES_PAGE_UART	0x0e
3318c2ecf20Sopenharmony_ci#define ES_PAGE_UART1	0x0f
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci/*
3348c2ecf20Sopenharmony_ci *  Sample rate converter addresses
3358c2ecf20Sopenharmony_ci */
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci#define ES_SMPREG_DAC1		0x70
3388c2ecf20Sopenharmony_ci#define ES_SMPREG_DAC2		0x74
3398c2ecf20Sopenharmony_ci#define ES_SMPREG_ADC		0x78
3408c2ecf20Sopenharmony_ci#define ES_SMPREG_VOL_ADC	0x6c
3418c2ecf20Sopenharmony_ci#define ES_SMPREG_VOL_DAC1	0x7c
3428c2ecf20Sopenharmony_ci#define ES_SMPREG_VOL_DAC2	0x7e
3438c2ecf20Sopenharmony_ci#define ES_SMPREG_TRUNC_N	0x00
3448c2ecf20Sopenharmony_ci#define ES_SMPREG_INT_REGS	0x01
3458c2ecf20Sopenharmony_ci#define ES_SMPREG_ACCUM_FRAC	0x02
3468c2ecf20Sopenharmony_ci#define ES_SMPREG_VFREQ_FRAC	0x03
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci/*
3498c2ecf20Sopenharmony_ci *  Some contants
3508c2ecf20Sopenharmony_ci */
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_ci#define ES_1370_SRCLOCK	   1411200
3538c2ecf20Sopenharmony_ci#define ES_1370_SRTODIV(x) (ES_1370_SRCLOCK/(x)-2)
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci/*
3568c2ecf20Sopenharmony_ci *  Open modes
3578c2ecf20Sopenharmony_ci */
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_ci#define ES_MODE_PLAY1	0x0001
3608c2ecf20Sopenharmony_ci#define ES_MODE_PLAY2	0x0002
3618c2ecf20Sopenharmony_ci#define ES_MODE_CAPTURE	0x0004
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci#define ES_MODE_OUTPUT	0x0001	/* for MIDI */
3648c2ecf20Sopenharmony_ci#define ES_MODE_INPUT	0x0002	/* for MIDI */
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci/*
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci */
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_cistruct ensoniq {
3718c2ecf20Sopenharmony_ci	spinlock_t reg_lock;
3728c2ecf20Sopenharmony_ci	struct mutex src_mutex;
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci	int irq;
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci	unsigned long playback1size;
3778c2ecf20Sopenharmony_ci	unsigned long playback2size;
3788c2ecf20Sopenharmony_ci	unsigned long capture3size;
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci	unsigned long port;
3818c2ecf20Sopenharmony_ci	unsigned int mode;
3828c2ecf20Sopenharmony_ci	unsigned int uartm;	/* UART mode */
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci	unsigned int ctrl;	/* control register */
3858c2ecf20Sopenharmony_ci	unsigned int sctrl;	/* serial control register */
3868c2ecf20Sopenharmony_ci	unsigned int cssr;	/* control status register */
3878c2ecf20Sopenharmony_ci	unsigned int uartc;	/* uart control register */
3888c2ecf20Sopenharmony_ci	unsigned int rev;	/* chip revision */
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ci	union {
3918c2ecf20Sopenharmony_ci#ifdef CHIP1371
3928c2ecf20Sopenharmony_ci		struct {
3938c2ecf20Sopenharmony_ci			struct snd_ac97 *ac97;
3948c2ecf20Sopenharmony_ci		} es1371;
3958c2ecf20Sopenharmony_ci#else
3968c2ecf20Sopenharmony_ci		struct {
3978c2ecf20Sopenharmony_ci			int pclkdiv_lock;
3988c2ecf20Sopenharmony_ci			struct snd_ak4531 *ak4531;
3998c2ecf20Sopenharmony_ci		} es1370;
4008c2ecf20Sopenharmony_ci#endif
4018c2ecf20Sopenharmony_ci	} u;
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_ci	struct pci_dev *pci;
4048c2ecf20Sopenharmony_ci	struct snd_card *card;
4058c2ecf20Sopenharmony_ci	struct snd_pcm *pcm1;	/* DAC1/ADC PCM */
4068c2ecf20Sopenharmony_ci	struct snd_pcm *pcm2;	/* DAC2 PCM */
4078c2ecf20Sopenharmony_ci	struct snd_pcm_substream *playback1_substream;
4088c2ecf20Sopenharmony_ci	struct snd_pcm_substream *playback2_substream;
4098c2ecf20Sopenharmony_ci	struct snd_pcm_substream *capture_substream;
4108c2ecf20Sopenharmony_ci	unsigned int p1_dma_size;
4118c2ecf20Sopenharmony_ci	unsigned int p2_dma_size;
4128c2ecf20Sopenharmony_ci	unsigned int c_dma_size;
4138c2ecf20Sopenharmony_ci	unsigned int p1_period_size;
4148c2ecf20Sopenharmony_ci	unsigned int p2_period_size;
4158c2ecf20Sopenharmony_ci	unsigned int c_period_size;
4168c2ecf20Sopenharmony_ci	struct snd_rawmidi *rmidi;
4178c2ecf20Sopenharmony_ci	struct snd_rawmidi_substream *midi_input;
4188c2ecf20Sopenharmony_ci	struct snd_rawmidi_substream *midi_output;
4198c2ecf20Sopenharmony_ci
4208c2ecf20Sopenharmony_ci	unsigned int spdif;
4218c2ecf20Sopenharmony_ci	unsigned int spdif_default;
4228c2ecf20Sopenharmony_ci	unsigned int spdif_stream;
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_ci#ifdef CHIP1370
4258c2ecf20Sopenharmony_ci	struct snd_dma_buffer dma_bug;
4268c2ecf20Sopenharmony_ci#endif
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci#ifdef SUPPORT_JOYSTICK
4298c2ecf20Sopenharmony_ci	struct gameport *gameport;
4308c2ecf20Sopenharmony_ci#endif
4318c2ecf20Sopenharmony_ci};
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_cistatic irqreturn_t snd_audiopci_interrupt(int irq, void *dev_id);
4348c2ecf20Sopenharmony_ci
4358c2ecf20Sopenharmony_cistatic const struct pci_device_id snd_audiopci_ids[] = {
4368c2ecf20Sopenharmony_ci#ifdef CHIP1370
4378c2ecf20Sopenharmony_ci	{ PCI_VDEVICE(ENSONIQ, 0x5000), 0, },	/* ES1370 */
4388c2ecf20Sopenharmony_ci#endif
4398c2ecf20Sopenharmony_ci#ifdef CHIP1371
4408c2ecf20Sopenharmony_ci	{ PCI_VDEVICE(ENSONIQ, 0x1371), 0, },	/* ES1371 */
4418c2ecf20Sopenharmony_ci	{ PCI_VDEVICE(ENSONIQ, 0x5880), 0, },	/* ES1373 - CT5880 */
4428c2ecf20Sopenharmony_ci	{ PCI_VDEVICE(ECTIVA, 0x8938), 0, },	/* Ectiva EV1938 */
4438c2ecf20Sopenharmony_ci#endif
4448c2ecf20Sopenharmony_ci	{ 0, }
4458c2ecf20Sopenharmony_ci};
4468c2ecf20Sopenharmony_ci
4478c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(pci, snd_audiopci_ids);
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_ci/*
4508c2ecf20Sopenharmony_ci *  constants
4518c2ecf20Sopenharmony_ci */
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_ci#define POLL_COUNT	0xa000
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_ci#ifdef CHIP1370
4568c2ecf20Sopenharmony_cistatic const unsigned int snd_es1370_fixed_rates[] =
4578c2ecf20Sopenharmony_ci	{5512, 11025, 22050, 44100};
4588c2ecf20Sopenharmony_cistatic const struct snd_pcm_hw_constraint_list snd_es1370_hw_constraints_rates = {
4598c2ecf20Sopenharmony_ci	.count = 4,
4608c2ecf20Sopenharmony_ci	.list = snd_es1370_fixed_rates,
4618c2ecf20Sopenharmony_ci	.mask = 0,
4628c2ecf20Sopenharmony_ci};
4638c2ecf20Sopenharmony_cistatic const struct snd_ratnum es1370_clock = {
4648c2ecf20Sopenharmony_ci	.num = ES_1370_SRCLOCK,
4658c2ecf20Sopenharmony_ci	.den_min = 29,
4668c2ecf20Sopenharmony_ci	.den_max = 353,
4678c2ecf20Sopenharmony_ci	.den_step = 1,
4688c2ecf20Sopenharmony_ci};
4698c2ecf20Sopenharmony_cistatic const struct snd_pcm_hw_constraint_ratnums snd_es1370_hw_constraints_clock = {
4708c2ecf20Sopenharmony_ci	.nrats = 1,
4718c2ecf20Sopenharmony_ci	.rats = &es1370_clock,
4728c2ecf20Sopenharmony_ci};
4738c2ecf20Sopenharmony_ci#else
4748c2ecf20Sopenharmony_cistatic const struct snd_ratden es1371_dac_clock = {
4758c2ecf20Sopenharmony_ci	.num_min = 3000 * (1 << 15),
4768c2ecf20Sopenharmony_ci	.num_max = 48000 * (1 << 15),
4778c2ecf20Sopenharmony_ci	.num_step = 3000,
4788c2ecf20Sopenharmony_ci	.den = 1 << 15,
4798c2ecf20Sopenharmony_ci};
4808c2ecf20Sopenharmony_cistatic const struct snd_pcm_hw_constraint_ratdens snd_es1371_hw_constraints_dac_clock = {
4818c2ecf20Sopenharmony_ci	.nrats = 1,
4828c2ecf20Sopenharmony_ci	.rats = &es1371_dac_clock,
4838c2ecf20Sopenharmony_ci};
4848c2ecf20Sopenharmony_cistatic const struct snd_ratnum es1371_adc_clock = {
4858c2ecf20Sopenharmony_ci	.num = 48000 << 15,
4868c2ecf20Sopenharmony_ci	.den_min = 32768,
4878c2ecf20Sopenharmony_ci	.den_max = 393216,
4888c2ecf20Sopenharmony_ci	.den_step = 1,
4898c2ecf20Sopenharmony_ci};
4908c2ecf20Sopenharmony_cistatic const struct snd_pcm_hw_constraint_ratnums snd_es1371_hw_constraints_adc_clock = {
4918c2ecf20Sopenharmony_ci	.nrats = 1,
4928c2ecf20Sopenharmony_ci	.rats = &es1371_adc_clock,
4938c2ecf20Sopenharmony_ci};
4948c2ecf20Sopenharmony_ci#endif
4958c2ecf20Sopenharmony_cistatic const unsigned int snd_ensoniq_sample_shift[] =
4968c2ecf20Sopenharmony_ci	{0, 1, 1, 2};
4978c2ecf20Sopenharmony_ci
4988c2ecf20Sopenharmony_ci/*
4998c2ecf20Sopenharmony_ci *  common I/O routines
5008c2ecf20Sopenharmony_ci */
5018c2ecf20Sopenharmony_ci
5028c2ecf20Sopenharmony_ci#ifdef CHIP1371
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_cistatic unsigned int snd_es1371_wait_src_ready(struct ensoniq * ensoniq)
5058c2ecf20Sopenharmony_ci{
5068c2ecf20Sopenharmony_ci	unsigned int t, r = 0;
5078c2ecf20Sopenharmony_ci
5088c2ecf20Sopenharmony_ci	for (t = 0; t < POLL_COUNT; t++) {
5098c2ecf20Sopenharmony_ci		r = inl(ES_REG(ensoniq, 1371_SMPRATE));
5108c2ecf20Sopenharmony_ci		if ((r & ES_1371_SRC_RAM_BUSY) == 0)
5118c2ecf20Sopenharmony_ci			return r;
5128c2ecf20Sopenharmony_ci		cond_resched();
5138c2ecf20Sopenharmony_ci	}
5148c2ecf20Sopenharmony_ci	dev_err(ensoniq->card->dev, "wait src ready timeout 0x%lx [0x%x]\n",
5158c2ecf20Sopenharmony_ci		   ES_REG(ensoniq, 1371_SMPRATE), r);
5168c2ecf20Sopenharmony_ci	return 0;
5178c2ecf20Sopenharmony_ci}
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_cistatic unsigned int snd_es1371_src_read(struct ensoniq * ensoniq, unsigned short reg)
5208c2ecf20Sopenharmony_ci{
5218c2ecf20Sopenharmony_ci	unsigned int temp, i, orig, r;
5228c2ecf20Sopenharmony_ci
5238c2ecf20Sopenharmony_ci	/* wait for ready */
5248c2ecf20Sopenharmony_ci	temp = orig = snd_es1371_wait_src_ready(ensoniq);
5258c2ecf20Sopenharmony_ci
5268c2ecf20Sopenharmony_ci	/* expose the SRC state bits */
5278c2ecf20Sopenharmony_ci	r = temp & (ES_1371_SRC_DISABLE | ES_1371_DIS_P1 |
5288c2ecf20Sopenharmony_ci		    ES_1371_DIS_P2 | ES_1371_DIS_R1);
5298c2ecf20Sopenharmony_ci	r |= ES_1371_SRC_RAM_ADDRO(reg) | 0x10000;
5308c2ecf20Sopenharmony_ci	outl(r, ES_REG(ensoniq, 1371_SMPRATE));
5318c2ecf20Sopenharmony_ci
5328c2ecf20Sopenharmony_ci	/* now, wait for busy and the correct time to read */
5338c2ecf20Sopenharmony_ci	temp = snd_es1371_wait_src_ready(ensoniq);
5348c2ecf20Sopenharmony_ci
5358c2ecf20Sopenharmony_ci	if ((temp & 0x00870000) != 0x00010000) {
5368c2ecf20Sopenharmony_ci		/* wait for the right state */
5378c2ecf20Sopenharmony_ci		for (i = 0; i < POLL_COUNT; i++) {
5388c2ecf20Sopenharmony_ci			temp = inl(ES_REG(ensoniq, 1371_SMPRATE));
5398c2ecf20Sopenharmony_ci			if ((temp & 0x00870000) == 0x00010000)
5408c2ecf20Sopenharmony_ci				break;
5418c2ecf20Sopenharmony_ci		}
5428c2ecf20Sopenharmony_ci	}
5438c2ecf20Sopenharmony_ci
5448c2ecf20Sopenharmony_ci	/* hide the state bits */
5458c2ecf20Sopenharmony_ci	r = orig & (ES_1371_SRC_DISABLE | ES_1371_DIS_P1 |
5468c2ecf20Sopenharmony_ci		   ES_1371_DIS_P2 | ES_1371_DIS_R1);
5478c2ecf20Sopenharmony_ci	r |= ES_1371_SRC_RAM_ADDRO(reg);
5488c2ecf20Sopenharmony_ci	outl(r, ES_REG(ensoniq, 1371_SMPRATE));
5498c2ecf20Sopenharmony_ci
5508c2ecf20Sopenharmony_ci	return temp;
5518c2ecf20Sopenharmony_ci}
5528c2ecf20Sopenharmony_ci
5538c2ecf20Sopenharmony_cistatic void snd_es1371_src_write(struct ensoniq * ensoniq,
5548c2ecf20Sopenharmony_ci				 unsigned short reg, unsigned short data)
5558c2ecf20Sopenharmony_ci{
5568c2ecf20Sopenharmony_ci	unsigned int r;
5578c2ecf20Sopenharmony_ci
5588c2ecf20Sopenharmony_ci	r = snd_es1371_wait_src_ready(ensoniq) &
5598c2ecf20Sopenharmony_ci	    (ES_1371_SRC_DISABLE | ES_1371_DIS_P1 |
5608c2ecf20Sopenharmony_ci	     ES_1371_DIS_P2 | ES_1371_DIS_R1);
5618c2ecf20Sopenharmony_ci	r |= ES_1371_SRC_RAM_ADDRO(reg) | ES_1371_SRC_RAM_DATAO(data);
5628c2ecf20Sopenharmony_ci	outl(r | ES_1371_SRC_RAM_WE, ES_REG(ensoniq, 1371_SMPRATE));
5638c2ecf20Sopenharmony_ci}
5648c2ecf20Sopenharmony_ci
5658c2ecf20Sopenharmony_ci#endif /* CHIP1371 */
5668c2ecf20Sopenharmony_ci
5678c2ecf20Sopenharmony_ci#ifdef CHIP1370
5688c2ecf20Sopenharmony_ci
5698c2ecf20Sopenharmony_cistatic void snd_es1370_codec_write(struct snd_ak4531 *ak4531,
5708c2ecf20Sopenharmony_ci				   unsigned short reg, unsigned short val)
5718c2ecf20Sopenharmony_ci{
5728c2ecf20Sopenharmony_ci	struct ensoniq *ensoniq = ak4531->private_data;
5738c2ecf20Sopenharmony_ci	unsigned long end_time = jiffies + HZ / 10;
5748c2ecf20Sopenharmony_ci
5758c2ecf20Sopenharmony_ci#if 0
5768c2ecf20Sopenharmony_ci	dev_dbg(ensoniq->card->dev,
5778c2ecf20Sopenharmony_ci	       "CODEC WRITE: reg = 0x%x, val = 0x%x (0x%x), creg = 0x%x\n",
5788c2ecf20Sopenharmony_ci	       reg, val, ES_1370_CODEC_WRITE(reg, val), ES_REG(ensoniq, 1370_CODEC));
5798c2ecf20Sopenharmony_ci#endif
5808c2ecf20Sopenharmony_ci	do {
5818c2ecf20Sopenharmony_ci		if (!(inl(ES_REG(ensoniq, STATUS)) & ES_1370_CSTAT)) {
5828c2ecf20Sopenharmony_ci			outw(ES_1370_CODEC_WRITE(reg, val), ES_REG(ensoniq, 1370_CODEC));
5838c2ecf20Sopenharmony_ci			return;
5848c2ecf20Sopenharmony_ci		}
5858c2ecf20Sopenharmony_ci		schedule_timeout_uninterruptible(1);
5868c2ecf20Sopenharmony_ci	} while (time_after(end_time, jiffies));
5878c2ecf20Sopenharmony_ci	dev_err(ensoniq->card->dev, "codec write timeout, status = 0x%x\n",
5888c2ecf20Sopenharmony_ci		   inl(ES_REG(ensoniq, STATUS)));
5898c2ecf20Sopenharmony_ci}
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_ci#endif /* CHIP1370 */
5928c2ecf20Sopenharmony_ci
5938c2ecf20Sopenharmony_ci#ifdef CHIP1371
5948c2ecf20Sopenharmony_ci
5958c2ecf20Sopenharmony_cistatic inline bool is_ev1938(struct ensoniq *ensoniq)
5968c2ecf20Sopenharmony_ci{
5978c2ecf20Sopenharmony_ci	return ensoniq->pci->device == 0x8938;
5988c2ecf20Sopenharmony_ci}
5998c2ecf20Sopenharmony_ci
6008c2ecf20Sopenharmony_cistatic void snd_es1371_codec_write(struct snd_ac97 *ac97,
6018c2ecf20Sopenharmony_ci				   unsigned short reg, unsigned short val)
6028c2ecf20Sopenharmony_ci{
6038c2ecf20Sopenharmony_ci	struct ensoniq *ensoniq = ac97->private_data;
6048c2ecf20Sopenharmony_ci	unsigned int t, x, flag;
6058c2ecf20Sopenharmony_ci
6068c2ecf20Sopenharmony_ci	flag = is_ev1938(ensoniq) ? EV_1938_CODEC_MAGIC : 0;
6078c2ecf20Sopenharmony_ci	mutex_lock(&ensoniq->src_mutex);
6088c2ecf20Sopenharmony_ci	for (t = 0; t < POLL_COUNT; t++) {
6098c2ecf20Sopenharmony_ci		if (!(inl(ES_REG(ensoniq, 1371_CODEC)) & ES_1371_CODEC_WIP)) {
6108c2ecf20Sopenharmony_ci			/* save the current state for latter */
6118c2ecf20Sopenharmony_ci			x = snd_es1371_wait_src_ready(ensoniq);
6128c2ecf20Sopenharmony_ci			outl((x & (ES_1371_SRC_DISABLE | ES_1371_DIS_P1 |
6138c2ecf20Sopenharmony_ci			           ES_1371_DIS_P2 | ES_1371_DIS_R1)) | 0x00010000,
6148c2ecf20Sopenharmony_ci			     ES_REG(ensoniq, 1371_SMPRATE));
6158c2ecf20Sopenharmony_ci			/* wait for not busy (state 0) first to avoid
6168c2ecf20Sopenharmony_ci			   transition states */
6178c2ecf20Sopenharmony_ci			for (t = 0; t < POLL_COUNT; t++) {
6188c2ecf20Sopenharmony_ci				if ((inl(ES_REG(ensoniq, 1371_SMPRATE)) & 0x00870000) ==
6198c2ecf20Sopenharmony_ci				    0x00000000)
6208c2ecf20Sopenharmony_ci					break;
6218c2ecf20Sopenharmony_ci			}
6228c2ecf20Sopenharmony_ci			/* wait for a SAFE time to write addr/data and then do it, dammit */
6238c2ecf20Sopenharmony_ci			for (t = 0; t < POLL_COUNT; t++) {
6248c2ecf20Sopenharmony_ci				if ((inl(ES_REG(ensoniq, 1371_SMPRATE)) & 0x00870000) ==
6258c2ecf20Sopenharmony_ci				    0x00010000)
6268c2ecf20Sopenharmony_ci					break;
6278c2ecf20Sopenharmony_ci			}
6288c2ecf20Sopenharmony_ci			outl(ES_1371_CODEC_WRITE(reg, val) | flag,
6298c2ecf20Sopenharmony_ci			     ES_REG(ensoniq, 1371_CODEC));
6308c2ecf20Sopenharmony_ci			/* restore SRC reg */
6318c2ecf20Sopenharmony_ci			snd_es1371_wait_src_ready(ensoniq);
6328c2ecf20Sopenharmony_ci			outl(x, ES_REG(ensoniq, 1371_SMPRATE));
6338c2ecf20Sopenharmony_ci			mutex_unlock(&ensoniq->src_mutex);
6348c2ecf20Sopenharmony_ci			return;
6358c2ecf20Sopenharmony_ci		}
6368c2ecf20Sopenharmony_ci	}
6378c2ecf20Sopenharmony_ci	mutex_unlock(&ensoniq->src_mutex);
6388c2ecf20Sopenharmony_ci	dev_err(ensoniq->card->dev, "codec write timeout at 0x%lx [0x%x]\n",
6398c2ecf20Sopenharmony_ci		   ES_REG(ensoniq, 1371_CODEC), inl(ES_REG(ensoniq, 1371_CODEC)));
6408c2ecf20Sopenharmony_ci}
6418c2ecf20Sopenharmony_ci
6428c2ecf20Sopenharmony_cistatic unsigned short snd_es1371_codec_read(struct snd_ac97 *ac97,
6438c2ecf20Sopenharmony_ci					    unsigned short reg)
6448c2ecf20Sopenharmony_ci{
6458c2ecf20Sopenharmony_ci	struct ensoniq *ensoniq = ac97->private_data;
6468c2ecf20Sopenharmony_ci	unsigned int t, x, flag, fail = 0;
6478c2ecf20Sopenharmony_ci
6488c2ecf20Sopenharmony_ci	flag = is_ev1938(ensoniq) ? EV_1938_CODEC_MAGIC : 0;
6498c2ecf20Sopenharmony_ci      __again:
6508c2ecf20Sopenharmony_ci	mutex_lock(&ensoniq->src_mutex);
6518c2ecf20Sopenharmony_ci	for (t = 0; t < POLL_COUNT; t++) {
6528c2ecf20Sopenharmony_ci		if (!(inl(ES_REG(ensoniq, 1371_CODEC)) & ES_1371_CODEC_WIP)) {
6538c2ecf20Sopenharmony_ci			/* save the current state for latter */
6548c2ecf20Sopenharmony_ci			x = snd_es1371_wait_src_ready(ensoniq);
6558c2ecf20Sopenharmony_ci			outl((x & (ES_1371_SRC_DISABLE | ES_1371_DIS_P1 |
6568c2ecf20Sopenharmony_ci			           ES_1371_DIS_P2 | ES_1371_DIS_R1)) | 0x00010000,
6578c2ecf20Sopenharmony_ci			     ES_REG(ensoniq, 1371_SMPRATE));
6588c2ecf20Sopenharmony_ci			/* wait for not busy (state 0) first to avoid
6598c2ecf20Sopenharmony_ci			   transition states */
6608c2ecf20Sopenharmony_ci			for (t = 0; t < POLL_COUNT; t++) {
6618c2ecf20Sopenharmony_ci				if ((inl(ES_REG(ensoniq, 1371_SMPRATE)) & 0x00870000) ==
6628c2ecf20Sopenharmony_ci				    0x00000000)
6638c2ecf20Sopenharmony_ci					break;
6648c2ecf20Sopenharmony_ci			}
6658c2ecf20Sopenharmony_ci			/* wait for a SAFE time to write addr/data and then do it, dammit */
6668c2ecf20Sopenharmony_ci			for (t = 0; t < POLL_COUNT; t++) {
6678c2ecf20Sopenharmony_ci				if ((inl(ES_REG(ensoniq, 1371_SMPRATE)) & 0x00870000) ==
6688c2ecf20Sopenharmony_ci				    0x00010000)
6698c2ecf20Sopenharmony_ci					break;
6708c2ecf20Sopenharmony_ci			}
6718c2ecf20Sopenharmony_ci			outl(ES_1371_CODEC_READS(reg) | flag,
6728c2ecf20Sopenharmony_ci			     ES_REG(ensoniq, 1371_CODEC));
6738c2ecf20Sopenharmony_ci			/* restore SRC reg */
6748c2ecf20Sopenharmony_ci			snd_es1371_wait_src_ready(ensoniq);
6758c2ecf20Sopenharmony_ci			outl(x, ES_REG(ensoniq, 1371_SMPRATE));
6768c2ecf20Sopenharmony_ci			/* wait for WIP again */
6778c2ecf20Sopenharmony_ci			for (t = 0; t < POLL_COUNT; t++) {
6788c2ecf20Sopenharmony_ci				if (!(inl(ES_REG(ensoniq, 1371_CODEC)) & ES_1371_CODEC_WIP))
6798c2ecf20Sopenharmony_ci					break;
6808c2ecf20Sopenharmony_ci			}
6818c2ecf20Sopenharmony_ci			/* now wait for the stinkin' data (RDY) */
6828c2ecf20Sopenharmony_ci			for (t = 0; t < POLL_COUNT; t++) {
6838c2ecf20Sopenharmony_ci				if ((x = inl(ES_REG(ensoniq, 1371_CODEC))) & ES_1371_CODEC_RDY) {
6848c2ecf20Sopenharmony_ci					if (is_ev1938(ensoniq)) {
6858c2ecf20Sopenharmony_ci						for (t = 0; t < 100; t++)
6868c2ecf20Sopenharmony_ci							inl(ES_REG(ensoniq, CONTROL));
6878c2ecf20Sopenharmony_ci						x = inl(ES_REG(ensoniq, 1371_CODEC));
6888c2ecf20Sopenharmony_ci					}
6898c2ecf20Sopenharmony_ci					mutex_unlock(&ensoniq->src_mutex);
6908c2ecf20Sopenharmony_ci					return ES_1371_CODEC_READ(x);
6918c2ecf20Sopenharmony_ci				}
6928c2ecf20Sopenharmony_ci			}
6938c2ecf20Sopenharmony_ci			mutex_unlock(&ensoniq->src_mutex);
6948c2ecf20Sopenharmony_ci			if (++fail > 10) {
6958c2ecf20Sopenharmony_ci				dev_err(ensoniq->card->dev,
6968c2ecf20Sopenharmony_ci					"codec read timeout (final) at 0x%lx, reg = 0x%x [0x%x]\n",
6978c2ecf20Sopenharmony_ci					   ES_REG(ensoniq, 1371_CODEC), reg,
6988c2ecf20Sopenharmony_ci					   inl(ES_REG(ensoniq, 1371_CODEC)));
6998c2ecf20Sopenharmony_ci				return 0;
7008c2ecf20Sopenharmony_ci			}
7018c2ecf20Sopenharmony_ci			goto __again;
7028c2ecf20Sopenharmony_ci		}
7038c2ecf20Sopenharmony_ci	}
7048c2ecf20Sopenharmony_ci	mutex_unlock(&ensoniq->src_mutex);
7058c2ecf20Sopenharmony_ci	dev_err(ensoniq->card->dev, "codec read timeout at 0x%lx [0x%x]\n",
7068c2ecf20Sopenharmony_ci		   ES_REG(ensoniq, 1371_CODEC), inl(ES_REG(ensoniq, 1371_CODEC)));
7078c2ecf20Sopenharmony_ci	return 0;
7088c2ecf20Sopenharmony_ci}
7098c2ecf20Sopenharmony_ci
7108c2ecf20Sopenharmony_cistatic void snd_es1371_codec_wait(struct snd_ac97 *ac97)
7118c2ecf20Sopenharmony_ci{
7128c2ecf20Sopenharmony_ci	msleep(750);
7138c2ecf20Sopenharmony_ci	snd_es1371_codec_read(ac97, AC97_RESET);
7148c2ecf20Sopenharmony_ci	snd_es1371_codec_read(ac97, AC97_VENDOR_ID1);
7158c2ecf20Sopenharmony_ci	snd_es1371_codec_read(ac97, AC97_VENDOR_ID2);
7168c2ecf20Sopenharmony_ci	msleep(50);
7178c2ecf20Sopenharmony_ci}
7188c2ecf20Sopenharmony_ci
7198c2ecf20Sopenharmony_cistatic void snd_es1371_adc_rate(struct ensoniq * ensoniq, unsigned int rate)
7208c2ecf20Sopenharmony_ci{
7218c2ecf20Sopenharmony_ci	unsigned int n, truncm, freq;
7228c2ecf20Sopenharmony_ci
7238c2ecf20Sopenharmony_ci	mutex_lock(&ensoniq->src_mutex);
7248c2ecf20Sopenharmony_ci	n = rate / 3000;
7258c2ecf20Sopenharmony_ci	if ((1 << n) & ((1 << 15) | (1 << 13) | (1 << 11) | (1 << 9)))
7268c2ecf20Sopenharmony_ci		n--;
7278c2ecf20Sopenharmony_ci	truncm = (21 * n - 1) | 1;
7288c2ecf20Sopenharmony_ci	freq = ((48000UL << 15) / rate) * n;
7298c2ecf20Sopenharmony_ci	if (rate >= 24000) {
7308c2ecf20Sopenharmony_ci		if (truncm > 239)
7318c2ecf20Sopenharmony_ci			truncm = 239;
7328c2ecf20Sopenharmony_ci		snd_es1371_src_write(ensoniq, ES_SMPREG_ADC + ES_SMPREG_TRUNC_N,
7338c2ecf20Sopenharmony_ci				(((239 - truncm) >> 1) << 9) | (n << 4));
7348c2ecf20Sopenharmony_ci	} else {
7358c2ecf20Sopenharmony_ci		if (truncm > 119)
7368c2ecf20Sopenharmony_ci			truncm = 119;
7378c2ecf20Sopenharmony_ci		snd_es1371_src_write(ensoniq, ES_SMPREG_ADC + ES_SMPREG_TRUNC_N,
7388c2ecf20Sopenharmony_ci				0x8000 | (((119 - truncm) >> 1) << 9) | (n << 4));
7398c2ecf20Sopenharmony_ci	}
7408c2ecf20Sopenharmony_ci	snd_es1371_src_write(ensoniq, ES_SMPREG_ADC + ES_SMPREG_INT_REGS,
7418c2ecf20Sopenharmony_ci			     (snd_es1371_src_read(ensoniq, ES_SMPREG_ADC +
7428c2ecf20Sopenharmony_ci						  ES_SMPREG_INT_REGS) & 0x00ff) |
7438c2ecf20Sopenharmony_ci			     ((freq >> 5) & 0xfc00));
7448c2ecf20Sopenharmony_ci	snd_es1371_src_write(ensoniq, ES_SMPREG_ADC + ES_SMPREG_VFREQ_FRAC, freq & 0x7fff);
7458c2ecf20Sopenharmony_ci	snd_es1371_src_write(ensoniq, ES_SMPREG_VOL_ADC, n << 8);
7468c2ecf20Sopenharmony_ci	snd_es1371_src_write(ensoniq, ES_SMPREG_VOL_ADC + 1, n << 8);
7478c2ecf20Sopenharmony_ci	mutex_unlock(&ensoniq->src_mutex);
7488c2ecf20Sopenharmony_ci}
7498c2ecf20Sopenharmony_ci
7508c2ecf20Sopenharmony_cistatic void snd_es1371_dac1_rate(struct ensoniq * ensoniq, unsigned int rate)
7518c2ecf20Sopenharmony_ci{
7528c2ecf20Sopenharmony_ci	unsigned int freq, r;
7538c2ecf20Sopenharmony_ci
7548c2ecf20Sopenharmony_ci	mutex_lock(&ensoniq->src_mutex);
7558c2ecf20Sopenharmony_ci	freq = ((rate << 15) + 1500) / 3000;
7568c2ecf20Sopenharmony_ci	r = (snd_es1371_wait_src_ready(ensoniq) & (ES_1371_SRC_DISABLE |
7578c2ecf20Sopenharmony_ci						   ES_1371_DIS_P2 | ES_1371_DIS_R1)) |
7588c2ecf20Sopenharmony_ci		ES_1371_DIS_P1;
7598c2ecf20Sopenharmony_ci	outl(r, ES_REG(ensoniq, 1371_SMPRATE));
7608c2ecf20Sopenharmony_ci	snd_es1371_src_write(ensoniq, ES_SMPREG_DAC1 + ES_SMPREG_INT_REGS,
7618c2ecf20Sopenharmony_ci			     (snd_es1371_src_read(ensoniq, ES_SMPREG_DAC1 +
7628c2ecf20Sopenharmony_ci						  ES_SMPREG_INT_REGS) & 0x00ff) |
7638c2ecf20Sopenharmony_ci			     ((freq >> 5) & 0xfc00));
7648c2ecf20Sopenharmony_ci	snd_es1371_src_write(ensoniq, ES_SMPREG_DAC1 + ES_SMPREG_VFREQ_FRAC, freq & 0x7fff);
7658c2ecf20Sopenharmony_ci	r = (snd_es1371_wait_src_ready(ensoniq) & (ES_1371_SRC_DISABLE |
7668c2ecf20Sopenharmony_ci						   ES_1371_DIS_P2 | ES_1371_DIS_R1));
7678c2ecf20Sopenharmony_ci	outl(r, ES_REG(ensoniq, 1371_SMPRATE));
7688c2ecf20Sopenharmony_ci	mutex_unlock(&ensoniq->src_mutex);
7698c2ecf20Sopenharmony_ci}
7708c2ecf20Sopenharmony_ci
7718c2ecf20Sopenharmony_cistatic void snd_es1371_dac2_rate(struct ensoniq * ensoniq, unsigned int rate)
7728c2ecf20Sopenharmony_ci{
7738c2ecf20Sopenharmony_ci	unsigned int freq, r;
7748c2ecf20Sopenharmony_ci
7758c2ecf20Sopenharmony_ci	mutex_lock(&ensoniq->src_mutex);
7768c2ecf20Sopenharmony_ci	freq = ((rate << 15) + 1500) / 3000;
7778c2ecf20Sopenharmony_ci	r = (snd_es1371_wait_src_ready(ensoniq) & (ES_1371_SRC_DISABLE |
7788c2ecf20Sopenharmony_ci						   ES_1371_DIS_P1 | ES_1371_DIS_R1)) |
7798c2ecf20Sopenharmony_ci		ES_1371_DIS_P2;
7808c2ecf20Sopenharmony_ci	outl(r, ES_REG(ensoniq, 1371_SMPRATE));
7818c2ecf20Sopenharmony_ci	snd_es1371_src_write(ensoniq, ES_SMPREG_DAC2 + ES_SMPREG_INT_REGS,
7828c2ecf20Sopenharmony_ci			     (snd_es1371_src_read(ensoniq, ES_SMPREG_DAC2 +
7838c2ecf20Sopenharmony_ci						  ES_SMPREG_INT_REGS) & 0x00ff) |
7848c2ecf20Sopenharmony_ci			     ((freq >> 5) & 0xfc00));
7858c2ecf20Sopenharmony_ci	snd_es1371_src_write(ensoniq, ES_SMPREG_DAC2 + ES_SMPREG_VFREQ_FRAC,
7868c2ecf20Sopenharmony_ci			     freq & 0x7fff);
7878c2ecf20Sopenharmony_ci	r = (snd_es1371_wait_src_ready(ensoniq) & (ES_1371_SRC_DISABLE |
7888c2ecf20Sopenharmony_ci						   ES_1371_DIS_P1 | ES_1371_DIS_R1));
7898c2ecf20Sopenharmony_ci	outl(r, ES_REG(ensoniq, 1371_SMPRATE));
7908c2ecf20Sopenharmony_ci	mutex_unlock(&ensoniq->src_mutex);
7918c2ecf20Sopenharmony_ci}
7928c2ecf20Sopenharmony_ci
7938c2ecf20Sopenharmony_ci#endif /* CHIP1371 */
7948c2ecf20Sopenharmony_ci
7958c2ecf20Sopenharmony_cistatic int snd_ensoniq_trigger(struct snd_pcm_substream *substream, int cmd)
7968c2ecf20Sopenharmony_ci{
7978c2ecf20Sopenharmony_ci	struct ensoniq *ensoniq = snd_pcm_substream_chip(substream);
7988c2ecf20Sopenharmony_ci	switch (cmd) {
7998c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
8008c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
8018c2ecf20Sopenharmony_ci	{
8028c2ecf20Sopenharmony_ci		unsigned int what = 0;
8038c2ecf20Sopenharmony_ci		struct snd_pcm_substream *s;
8048c2ecf20Sopenharmony_ci		snd_pcm_group_for_each_entry(s, substream) {
8058c2ecf20Sopenharmony_ci			if (s == ensoniq->playback1_substream) {
8068c2ecf20Sopenharmony_ci				what |= ES_P1_PAUSE;
8078c2ecf20Sopenharmony_ci				snd_pcm_trigger_done(s, substream);
8088c2ecf20Sopenharmony_ci			} else if (s == ensoniq->playback2_substream) {
8098c2ecf20Sopenharmony_ci				what |= ES_P2_PAUSE;
8108c2ecf20Sopenharmony_ci				snd_pcm_trigger_done(s, substream);
8118c2ecf20Sopenharmony_ci			} else if (s == ensoniq->capture_substream)
8128c2ecf20Sopenharmony_ci				return -EINVAL;
8138c2ecf20Sopenharmony_ci		}
8148c2ecf20Sopenharmony_ci		spin_lock(&ensoniq->reg_lock);
8158c2ecf20Sopenharmony_ci		if (cmd == SNDRV_PCM_TRIGGER_PAUSE_PUSH)
8168c2ecf20Sopenharmony_ci			ensoniq->sctrl |= what;
8178c2ecf20Sopenharmony_ci		else
8188c2ecf20Sopenharmony_ci			ensoniq->sctrl &= ~what;
8198c2ecf20Sopenharmony_ci		outl(ensoniq->sctrl, ES_REG(ensoniq, SERIAL));
8208c2ecf20Sopenharmony_ci		spin_unlock(&ensoniq->reg_lock);
8218c2ecf20Sopenharmony_ci		break;
8228c2ecf20Sopenharmony_ci	}
8238c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_START:
8248c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_STOP:
8258c2ecf20Sopenharmony_ci	{
8268c2ecf20Sopenharmony_ci		unsigned int what = 0;
8278c2ecf20Sopenharmony_ci		struct snd_pcm_substream *s;
8288c2ecf20Sopenharmony_ci		snd_pcm_group_for_each_entry(s, substream) {
8298c2ecf20Sopenharmony_ci			if (s == ensoniq->playback1_substream) {
8308c2ecf20Sopenharmony_ci				what |= ES_DAC1_EN;
8318c2ecf20Sopenharmony_ci				snd_pcm_trigger_done(s, substream);
8328c2ecf20Sopenharmony_ci			} else if (s == ensoniq->playback2_substream) {
8338c2ecf20Sopenharmony_ci				what |= ES_DAC2_EN;
8348c2ecf20Sopenharmony_ci				snd_pcm_trigger_done(s, substream);
8358c2ecf20Sopenharmony_ci			} else if (s == ensoniq->capture_substream) {
8368c2ecf20Sopenharmony_ci				what |= ES_ADC_EN;
8378c2ecf20Sopenharmony_ci				snd_pcm_trigger_done(s, substream);
8388c2ecf20Sopenharmony_ci			}
8398c2ecf20Sopenharmony_ci		}
8408c2ecf20Sopenharmony_ci		spin_lock(&ensoniq->reg_lock);
8418c2ecf20Sopenharmony_ci		if (cmd == SNDRV_PCM_TRIGGER_START)
8428c2ecf20Sopenharmony_ci			ensoniq->ctrl |= what;
8438c2ecf20Sopenharmony_ci		else
8448c2ecf20Sopenharmony_ci			ensoniq->ctrl &= ~what;
8458c2ecf20Sopenharmony_ci		outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
8468c2ecf20Sopenharmony_ci		spin_unlock(&ensoniq->reg_lock);
8478c2ecf20Sopenharmony_ci		break;
8488c2ecf20Sopenharmony_ci	}
8498c2ecf20Sopenharmony_ci	default:
8508c2ecf20Sopenharmony_ci		return -EINVAL;
8518c2ecf20Sopenharmony_ci	}
8528c2ecf20Sopenharmony_ci	return 0;
8538c2ecf20Sopenharmony_ci}
8548c2ecf20Sopenharmony_ci
8558c2ecf20Sopenharmony_ci/*
8568c2ecf20Sopenharmony_ci *  PCM part
8578c2ecf20Sopenharmony_ci */
8588c2ecf20Sopenharmony_ci
8598c2ecf20Sopenharmony_cistatic int snd_ensoniq_playback1_prepare(struct snd_pcm_substream *substream)
8608c2ecf20Sopenharmony_ci{
8618c2ecf20Sopenharmony_ci	struct ensoniq *ensoniq = snd_pcm_substream_chip(substream);
8628c2ecf20Sopenharmony_ci	struct snd_pcm_runtime *runtime = substream->runtime;
8638c2ecf20Sopenharmony_ci	unsigned int mode = 0;
8648c2ecf20Sopenharmony_ci
8658c2ecf20Sopenharmony_ci	ensoniq->p1_dma_size = snd_pcm_lib_buffer_bytes(substream);
8668c2ecf20Sopenharmony_ci	ensoniq->p1_period_size = snd_pcm_lib_period_bytes(substream);
8678c2ecf20Sopenharmony_ci	if (snd_pcm_format_width(runtime->format) == 16)
8688c2ecf20Sopenharmony_ci		mode |= 0x02;
8698c2ecf20Sopenharmony_ci	if (runtime->channels > 1)
8708c2ecf20Sopenharmony_ci		mode |= 0x01;
8718c2ecf20Sopenharmony_ci	spin_lock_irq(&ensoniq->reg_lock);
8728c2ecf20Sopenharmony_ci	ensoniq->ctrl &= ~ES_DAC1_EN;
8738c2ecf20Sopenharmony_ci#ifdef CHIP1371
8748c2ecf20Sopenharmony_ci	/* 48k doesn't need SRC (it breaks AC3-passthru) */
8758c2ecf20Sopenharmony_ci	if (runtime->rate == 48000)
8768c2ecf20Sopenharmony_ci		ensoniq->ctrl |= ES_1373_BYPASS_P1;
8778c2ecf20Sopenharmony_ci	else
8788c2ecf20Sopenharmony_ci		ensoniq->ctrl &= ~ES_1373_BYPASS_P1;
8798c2ecf20Sopenharmony_ci#endif
8808c2ecf20Sopenharmony_ci	outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
8818c2ecf20Sopenharmony_ci	outl(ES_MEM_PAGEO(ES_PAGE_DAC), ES_REG(ensoniq, MEM_PAGE));
8828c2ecf20Sopenharmony_ci	outl(runtime->dma_addr, ES_REG(ensoniq, DAC1_FRAME));
8838c2ecf20Sopenharmony_ci	outl((ensoniq->p1_dma_size >> 2) - 1, ES_REG(ensoniq, DAC1_SIZE));
8848c2ecf20Sopenharmony_ci	ensoniq->sctrl &= ~(ES_P1_LOOP_SEL | ES_P1_PAUSE | ES_P1_SCT_RLD | ES_P1_MODEM);
8858c2ecf20Sopenharmony_ci	ensoniq->sctrl |= ES_P1_INT_EN | ES_P1_MODEO(mode);
8868c2ecf20Sopenharmony_ci	outl(ensoniq->sctrl, ES_REG(ensoniq, SERIAL));
8878c2ecf20Sopenharmony_ci	outl((ensoniq->p1_period_size >> snd_ensoniq_sample_shift[mode]) - 1,
8888c2ecf20Sopenharmony_ci	     ES_REG(ensoniq, DAC1_COUNT));
8898c2ecf20Sopenharmony_ci#ifdef CHIP1370
8908c2ecf20Sopenharmony_ci	ensoniq->ctrl &= ~ES_1370_WTSRSELM;
8918c2ecf20Sopenharmony_ci	switch (runtime->rate) {
8928c2ecf20Sopenharmony_ci	case 5512: ensoniq->ctrl |= ES_1370_WTSRSEL(0); break;
8938c2ecf20Sopenharmony_ci	case 11025: ensoniq->ctrl |= ES_1370_WTSRSEL(1); break;
8948c2ecf20Sopenharmony_ci	case 22050: ensoniq->ctrl |= ES_1370_WTSRSEL(2); break;
8958c2ecf20Sopenharmony_ci	case 44100: ensoniq->ctrl |= ES_1370_WTSRSEL(3); break;
8968c2ecf20Sopenharmony_ci	default: snd_BUG();
8978c2ecf20Sopenharmony_ci	}
8988c2ecf20Sopenharmony_ci#endif
8998c2ecf20Sopenharmony_ci	outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
9008c2ecf20Sopenharmony_ci	spin_unlock_irq(&ensoniq->reg_lock);
9018c2ecf20Sopenharmony_ci#ifndef CHIP1370
9028c2ecf20Sopenharmony_ci	snd_es1371_dac1_rate(ensoniq, runtime->rate);
9038c2ecf20Sopenharmony_ci#endif
9048c2ecf20Sopenharmony_ci	return 0;
9058c2ecf20Sopenharmony_ci}
9068c2ecf20Sopenharmony_ci
9078c2ecf20Sopenharmony_cistatic int snd_ensoniq_playback2_prepare(struct snd_pcm_substream *substream)
9088c2ecf20Sopenharmony_ci{
9098c2ecf20Sopenharmony_ci	struct ensoniq *ensoniq = snd_pcm_substream_chip(substream);
9108c2ecf20Sopenharmony_ci	struct snd_pcm_runtime *runtime = substream->runtime;
9118c2ecf20Sopenharmony_ci	unsigned int mode = 0;
9128c2ecf20Sopenharmony_ci
9138c2ecf20Sopenharmony_ci	ensoniq->p2_dma_size = snd_pcm_lib_buffer_bytes(substream);
9148c2ecf20Sopenharmony_ci	ensoniq->p2_period_size = snd_pcm_lib_period_bytes(substream);
9158c2ecf20Sopenharmony_ci	if (snd_pcm_format_width(runtime->format) == 16)
9168c2ecf20Sopenharmony_ci		mode |= 0x02;
9178c2ecf20Sopenharmony_ci	if (runtime->channels > 1)
9188c2ecf20Sopenharmony_ci		mode |= 0x01;
9198c2ecf20Sopenharmony_ci	spin_lock_irq(&ensoniq->reg_lock);
9208c2ecf20Sopenharmony_ci	ensoniq->ctrl &= ~ES_DAC2_EN;
9218c2ecf20Sopenharmony_ci	outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
9228c2ecf20Sopenharmony_ci	outl(ES_MEM_PAGEO(ES_PAGE_DAC), ES_REG(ensoniq, MEM_PAGE));
9238c2ecf20Sopenharmony_ci	outl(runtime->dma_addr, ES_REG(ensoniq, DAC2_FRAME));
9248c2ecf20Sopenharmony_ci	outl((ensoniq->p2_dma_size >> 2) - 1, ES_REG(ensoniq, DAC2_SIZE));
9258c2ecf20Sopenharmony_ci	ensoniq->sctrl &= ~(ES_P2_LOOP_SEL | ES_P2_PAUSE | ES_P2_DAC_SEN |
9268c2ecf20Sopenharmony_ci			    ES_P2_END_INCM | ES_P2_ST_INCM | ES_P2_MODEM);
9278c2ecf20Sopenharmony_ci	ensoniq->sctrl |= ES_P2_INT_EN | ES_P2_MODEO(mode) |
9288c2ecf20Sopenharmony_ci			  ES_P2_END_INCO(mode & 2 ? 2 : 1) | ES_P2_ST_INCO(0);
9298c2ecf20Sopenharmony_ci	outl(ensoniq->sctrl, ES_REG(ensoniq, SERIAL));
9308c2ecf20Sopenharmony_ci	outl((ensoniq->p2_period_size >> snd_ensoniq_sample_shift[mode]) - 1,
9318c2ecf20Sopenharmony_ci	     ES_REG(ensoniq, DAC2_COUNT));
9328c2ecf20Sopenharmony_ci#ifdef CHIP1370
9338c2ecf20Sopenharmony_ci	if (!(ensoniq->u.es1370.pclkdiv_lock & ES_MODE_CAPTURE)) {
9348c2ecf20Sopenharmony_ci		ensoniq->ctrl &= ~ES_1370_PCLKDIVM;
9358c2ecf20Sopenharmony_ci		ensoniq->ctrl |= ES_1370_PCLKDIVO(ES_1370_SRTODIV(runtime->rate));
9368c2ecf20Sopenharmony_ci		ensoniq->u.es1370.pclkdiv_lock |= ES_MODE_PLAY2;
9378c2ecf20Sopenharmony_ci	}
9388c2ecf20Sopenharmony_ci#endif
9398c2ecf20Sopenharmony_ci	outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
9408c2ecf20Sopenharmony_ci	spin_unlock_irq(&ensoniq->reg_lock);
9418c2ecf20Sopenharmony_ci#ifndef CHIP1370
9428c2ecf20Sopenharmony_ci	snd_es1371_dac2_rate(ensoniq, runtime->rate);
9438c2ecf20Sopenharmony_ci#endif
9448c2ecf20Sopenharmony_ci	return 0;
9458c2ecf20Sopenharmony_ci}
9468c2ecf20Sopenharmony_ci
9478c2ecf20Sopenharmony_cistatic int snd_ensoniq_capture_prepare(struct snd_pcm_substream *substream)
9488c2ecf20Sopenharmony_ci{
9498c2ecf20Sopenharmony_ci	struct ensoniq *ensoniq = snd_pcm_substream_chip(substream);
9508c2ecf20Sopenharmony_ci	struct snd_pcm_runtime *runtime = substream->runtime;
9518c2ecf20Sopenharmony_ci	unsigned int mode = 0;
9528c2ecf20Sopenharmony_ci
9538c2ecf20Sopenharmony_ci	ensoniq->c_dma_size = snd_pcm_lib_buffer_bytes(substream);
9548c2ecf20Sopenharmony_ci	ensoniq->c_period_size = snd_pcm_lib_period_bytes(substream);
9558c2ecf20Sopenharmony_ci	if (snd_pcm_format_width(runtime->format) == 16)
9568c2ecf20Sopenharmony_ci		mode |= 0x02;
9578c2ecf20Sopenharmony_ci	if (runtime->channels > 1)
9588c2ecf20Sopenharmony_ci		mode |= 0x01;
9598c2ecf20Sopenharmony_ci	spin_lock_irq(&ensoniq->reg_lock);
9608c2ecf20Sopenharmony_ci	ensoniq->ctrl &= ~ES_ADC_EN;
9618c2ecf20Sopenharmony_ci	outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
9628c2ecf20Sopenharmony_ci	outl(ES_MEM_PAGEO(ES_PAGE_ADC), ES_REG(ensoniq, MEM_PAGE));
9638c2ecf20Sopenharmony_ci	outl(runtime->dma_addr, ES_REG(ensoniq, ADC_FRAME));
9648c2ecf20Sopenharmony_ci	outl((ensoniq->c_dma_size >> 2) - 1, ES_REG(ensoniq, ADC_SIZE));
9658c2ecf20Sopenharmony_ci	ensoniq->sctrl &= ~(ES_R1_LOOP_SEL | ES_R1_MODEM);
9668c2ecf20Sopenharmony_ci	ensoniq->sctrl |= ES_R1_INT_EN | ES_R1_MODEO(mode);
9678c2ecf20Sopenharmony_ci	outl(ensoniq->sctrl, ES_REG(ensoniq, SERIAL));
9688c2ecf20Sopenharmony_ci	outl((ensoniq->c_period_size >> snd_ensoniq_sample_shift[mode]) - 1,
9698c2ecf20Sopenharmony_ci	     ES_REG(ensoniq, ADC_COUNT));
9708c2ecf20Sopenharmony_ci#ifdef CHIP1370
9718c2ecf20Sopenharmony_ci	if (!(ensoniq->u.es1370.pclkdiv_lock & ES_MODE_PLAY2)) {
9728c2ecf20Sopenharmony_ci		ensoniq->ctrl &= ~ES_1370_PCLKDIVM;
9738c2ecf20Sopenharmony_ci		ensoniq->ctrl |= ES_1370_PCLKDIVO(ES_1370_SRTODIV(runtime->rate));
9748c2ecf20Sopenharmony_ci		ensoniq->u.es1370.pclkdiv_lock |= ES_MODE_CAPTURE;
9758c2ecf20Sopenharmony_ci	}
9768c2ecf20Sopenharmony_ci#endif
9778c2ecf20Sopenharmony_ci	outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
9788c2ecf20Sopenharmony_ci	spin_unlock_irq(&ensoniq->reg_lock);
9798c2ecf20Sopenharmony_ci#ifndef CHIP1370
9808c2ecf20Sopenharmony_ci	snd_es1371_adc_rate(ensoniq, runtime->rate);
9818c2ecf20Sopenharmony_ci#endif
9828c2ecf20Sopenharmony_ci	return 0;
9838c2ecf20Sopenharmony_ci}
9848c2ecf20Sopenharmony_ci
9858c2ecf20Sopenharmony_cistatic snd_pcm_uframes_t snd_ensoniq_playback1_pointer(struct snd_pcm_substream *substream)
9868c2ecf20Sopenharmony_ci{
9878c2ecf20Sopenharmony_ci	struct ensoniq *ensoniq = snd_pcm_substream_chip(substream);
9888c2ecf20Sopenharmony_ci	size_t ptr;
9898c2ecf20Sopenharmony_ci
9908c2ecf20Sopenharmony_ci	spin_lock(&ensoniq->reg_lock);
9918c2ecf20Sopenharmony_ci	if (inl(ES_REG(ensoniq, CONTROL)) & ES_DAC1_EN) {
9928c2ecf20Sopenharmony_ci		outl(ES_MEM_PAGEO(ES_PAGE_DAC), ES_REG(ensoniq, MEM_PAGE));
9938c2ecf20Sopenharmony_ci		ptr = ES_REG_FCURR_COUNTI(inl(ES_REG(ensoniq, DAC1_SIZE)));
9948c2ecf20Sopenharmony_ci		ptr = bytes_to_frames(substream->runtime, ptr);
9958c2ecf20Sopenharmony_ci	} else {
9968c2ecf20Sopenharmony_ci		ptr = 0;
9978c2ecf20Sopenharmony_ci	}
9988c2ecf20Sopenharmony_ci	spin_unlock(&ensoniq->reg_lock);
9998c2ecf20Sopenharmony_ci	return ptr;
10008c2ecf20Sopenharmony_ci}
10018c2ecf20Sopenharmony_ci
10028c2ecf20Sopenharmony_cistatic snd_pcm_uframes_t snd_ensoniq_playback2_pointer(struct snd_pcm_substream *substream)
10038c2ecf20Sopenharmony_ci{
10048c2ecf20Sopenharmony_ci	struct ensoniq *ensoniq = snd_pcm_substream_chip(substream);
10058c2ecf20Sopenharmony_ci	size_t ptr;
10068c2ecf20Sopenharmony_ci
10078c2ecf20Sopenharmony_ci	spin_lock(&ensoniq->reg_lock);
10088c2ecf20Sopenharmony_ci	if (inl(ES_REG(ensoniq, CONTROL)) & ES_DAC2_EN) {
10098c2ecf20Sopenharmony_ci		outl(ES_MEM_PAGEO(ES_PAGE_DAC), ES_REG(ensoniq, MEM_PAGE));
10108c2ecf20Sopenharmony_ci		ptr = ES_REG_FCURR_COUNTI(inl(ES_REG(ensoniq, DAC2_SIZE)));
10118c2ecf20Sopenharmony_ci		ptr = bytes_to_frames(substream->runtime, ptr);
10128c2ecf20Sopenharmony_ci	} else {
10138c2ecf20Sopenharmony_ci		ptr = 0;
10148c2ecf20Sopenharmony_ci	}
10158c2ecf20Sopenharmony_ci	spin_unlock(&ensoniq->reg_lock);
10168c2ecf20Sopenharmony_ci	return ptr;
10178c2ecf20Sopenharmony_ci}
10188c2ecf20Sopenharmony_ci
10198c2ecf20Sopenharmony_cistatic snd_pcm_uframes_t snd_ensoniq_capture_pointer(struct snd_pcm_substream *substream)
10208c2ecf20Sopenharmony_ci{
10218c2ecf20Sopenharmony_ci	struct ensoniq *ensoniq = snd_pcm_substream_chip(substream);
10228c2ecf20Sopenharmony_ci	size_t ptr;
10238c2ecf20Sopenharmony_ci
10248c2ecf20Sopenharmony_ci	spin_lock(&ensoniq->reg_lock);
10258c2ecf20Sopenharmony_ci	if (inl(ES_REG(ensoniq, CONTROL)) & ES_ADC_EN) {
10268c2ecf20Sopenharmony_ci		outl(ES_MEM_PAGEO(ES_PAGE_ADC), ES_REG(ensoniq, MEM_PAGE));
10278c2ecf20Sopenharmony_ci		ptr = ES_REG_FCURR_COUNTI(inl(ES_REG(ensoniq, ADC_SIZE)));
10288c2ecf20Sopenharmony_ci		ptr = bytes_to_frames(substream->runtime, ptr);
10298c2ecf20Sopenharmony_ci	} else {
10308c2ecf20Sopenharmony_ci		ptr = 0;
10318c2ecf20Sopenharmony_ci	}
10328c2ecf20Sopenharmony_ci	spin_unlock(&ensoniq->reg_lock);
10338c2ecf20Sopenharmony_ci	return ptr;
10348c2ecf20Sopenharmony_ci}
10358c2ecf20Sopenharmony_ci
10368c2ecf20Sopenharmony_cistatic const struct snd_pcm_hardware snd_ensoniq_playback1 =
10378c2ecf20Sopenharmony_ci{
10388c2ecf20Sopenharmony_ci	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
10398c2ecf20Sopenharmony_ci				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
10408c2ecf20Sopenharmony_ci				 SNDRV_PCM_INFO_MMAP_VALID |
10418c2ecf20Sopenharmony_ci				 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_SYNC_START),
10428c2ecf20Sopenharmony_ci	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
10438c2ecf20Sopenharmony_ci	.rates =
10448c2ecf20Sopenharmony_ci#ifndef CHIP1370
10458c2ecf20Sopenharmony_ci				SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
10468c2ecf20Sopenharmony_ci#else
10478c2ecf20Sopenharmony_ci				(SNDRV_PCM_RATE_KNOT | 	/* 5512Hz rate */
10488c2ecf20Sopenharmony_ci				 SNDRV_PCM_RATE_11025 | SNDRV_PCM_RATE_22050 |
10498c2ecf20Sopenharmony_ci				 SNDRV_PCM_RATE_44100),
10508c2ecf20Sopenharmony_ci#endif
10518c2ecf20Sopenharmony_ci	.rate_min =		4000,
10528c2ecf20Sopenharmony_ci	.rate_max =		48000,
10538c2ecf20Sopenharmony_ci	.channels_min =		1,
10548c2ecf20Sopenharmony_ci	.channels_max =		2,
10558c2ecf20Sopenharmony_ci	.buffer_bytes_max =	(128*1024),
10568c2ecf20Sopenharmony_ci	.period_bytes_min =	64,
10578c2ecf20Sopenharmony_ci	.period_bytes_max =	(128*1024),
10588c2ecf20Sopenharmony_ci	.periods_min =		1,
10598c2ecf20Sopenharmony_ci	.periods_max =		1024,
10608c2ecf20Sopenharmony_ci	.fifo_size =		0,
10618c2ecf20Sopenharmony_ci};
10628c2ecf20Sopenharmony_ci
10638c2ecf20Sopenharmony_cistatic const struct snd_pcm_hardware snd_ensoniq_playback2 =
10648c2ecf20Sopenharmony_ci{
10658c2ecf20Sopenharmony_ci	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
10668c2ecf20Sopenharmony_ci				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
10678c2ecf20Sopenharmony_ci				 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE |
10688c2ecf20Sopenharmony_ci				 SNDRV_PCM_INFO_SYNC_START),
10698c2ecf20Sopenharmony_ci	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
10708c2ecf20Sopenharmony_ci	.rates =		SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
10718c2ecf20Sopenharmony_ci	.rate_min =		4000,
10728c2ecf20Sopenharmony_ci	.rate_max =		48000,
10738c2ecf20Sopenharmony_ci	.channels_min =		1,
10748c2ecf20Sopenharmony_ci	.channels_max =		2,
10758c2ecf20Sopenharmony_ci	.buffer_bytes_max =	(128*1024),
10768c2ecf20Sopenharmony_ci	.period_bytes_min =	64,
10778c2ecf20Sopenharmony_ci	.period_bytes_max =	(128*1024),
10788c2ecf20Sopenharmony_ci	.periods_min =		1,
10798c2ecf20Sopenharmony_ci	.periods_max =		1024,
10808c2ecf20Sopenharmony_ci	.fifo_size =		0,
10818c2ecf20Sopenharmony_ci};
10828c2ecf20Sopenharmony_ci
10838c2ecf20Sopenharmony_cistatic const struct snd_pcm_hardware snd_ensoniq_capture =
10848c2ecf20Sopenharmony_ci{
10858c2ecf20Sopenharmony_ci	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
10868c2ecf20Sopenharmony_ci				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
10878c2ecf20Sopenharmony_ci				 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START),
10888c2ecf20Sopenharmony_ci	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
10898c2ecf20Sopenharmony_ci	.rates =		SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
10908c2ecf20Sopenharmony_ci	.rate_min =		4000,
10918c2ecf20Sopenharmony_ci	.rate_max =		48000,
10928c2ecf20Sopenharmony_ci	.channels_min =		1,
10938c2ecf20Sopenharmony_ci	.channels_max =		2,
10948c2ecf20Sopenharmony_ci	.buffer_bytes_max =	(128*1024),
10958c2ecf20Sopenharmony_ci	.period_bytes_min =	64,
10968c2ecf20Sopenharmony_ci	.period_bytes_max =	(128*1024),
10978c2ecf20Sopenharmony_ci	.periods_min =		1,
10988c2ecf20Sopenharmony_ci	.periods_max =		1024,
10998c2ecf20Sopenharmony_ci	.fifo_size =		0,
11008c2ecf20Sopenharmony_ci};
11018c2ecf20Sopenharmony_ci
11028c2ecf20Sopenharmony_cistatic int snd_ensoniq_playback1_open(struct snd_pcm_substream *substream)
11038c2ecf20Sopenharmony_ci{
11048c2ecf20Sopenharmony_ci	struct ensoniq *ensoniq = snd_pcm_substream_chip(substream);
11058c2ecf20Sopenharmony_ci	struct snd_pcm_runtime *runtime = substream->runtime;
11068c2ecf20Sopenharmony_ci
11078c2ecf20Sopenharmony_ci	ensoniq->mode |= ES_MODE_PLAY1;
11088c2ecf20Sopenharmony_ci	ensoniq->playback1_substream = substream;
11098c2ecf20Sopenharmony_ci	runtime->hw = snd_ensoniq_playback1;
11108c2ecf20Sopenharmony_ci	snd_pcm_set_sync(substream);
11118c2ecf20Sopenharmony_ci	spin_lock_irq(&ensoniq->reg_lock);
11128c2ecf20Sopenharmony_ci	if (ensoniq->spdif && ensoniq->playback2_substream == NULL)
11138c2ecf20Sopenharmony_ci		ensoniq->spdif_stream = ensoniq->spdif_default;
11148c2ecf20Sopenharmony_ci	spin_unlock_irq(&ensoniq->reg_lock);
11158c2ecf20Sopenharmony_ci#ifdef CHIP1370
11168c2ecf20Sopenharmony_ci	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
11178c2ecf20Sopenharmony_ci				   &snd_es1370_hw_constraints_rates);
11188c2ecf20Sopenharmony_ci#else
11198c2ecf20Sopenharmony_ci	snd_pcm_hw_constraint_ratdens(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
11208c2ecf20Sopenharmony_ci				      &snd_es1371_hw_constraints_dac_clock);
11218c2ecf20Sopenharmony_ci#endif
11228c2ecf20Sopenharmony_ci	return 0;
11238c2ecf20Sopenharmony_ci}
11248c2ecf20Sopenharmony_ci
11258c2ecf20Sopenharmony_cistatic int snd_ensoniq_playback2_open(struct snd_pcm_substream *substream)
11268c2ecf20Sopenharmony_ci{
11278c2ecf20Sopenharmony_ci	struct ensoniq *ensoniq = snd_pcm_substream_chip(substream);
11288c2ecf20Sopenharmony_ci	struct snd_pcm_runtime *runtime = substream->runtime;
11298c2ecf20Sopenharmony_ci
11308c2ecf20Sopenharmony_ci	ensoniq->mode |= ES_MODE_PLAY2;
11318c2ecf20Sopenharmony_ci	ensoniq->playback2_substream = substream;
11328c2ecf20Sopenharmony_ci	runtime->hw = snd_ensoniq_playback2;
11338c2ecf20Sopenharmony_ci	snd_pcm_set_sync(substream);
11348c2ecf20Sopenharmony_ci	spin_lock_irq(&ensoniq->reg_lock);
11358c2ecf20Sopenharmony_ci	if (ensoniq->spdif && ensoniq->playback1_substream == NULL)
11368c2ecf20Sopenharmony_ci		ensoniq->spdif_stream = ensoniq->spdif_default;
11378c2ecf20Sopenharmony_ci	spin_unlock_irq(&ensoniq->reg_lock);
11388c2ecf20Sopenharmony_ci#ifdef CHIP1370
11398c2ecf20Sopenharmony_ci	snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
11408c2ecf20Sopenharmony_ci				      &snd_es1370_hw_constraints_clock);
11418c2ecf20Sopenharmony_ci#else
11428c2ecf20Sopenharmony_ci	snd_pcm_hw_constraint_ratdens(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
11438c2ecf20Sopenharmony_ci				      &snd_es1371_hw_constraints_dac_clock);
11448c2ecf20Sopenharmony_ci#endif
11458c2ecf20Sopenharmony_ci	return 0;
11468c2ecf20Sopenharmony_ci}
11478c2ecf20Sopenharmony_ci
11488c2ecf20Sopenharmony_cistatic int snd_ensoniq_capture_open(struct snd_pcm_substream *substream)
11498c2ecf20Sopenharmony_ci{
11508c2ecf20Sopenharmony_ci	struct ensoniq *ensoniq = snd_pcm_substream_chip(substream);
11518c2ecf20Sopenharmony_ci	struct snd_pcm_runtime *runtime = substream->runtime;
11528c2ecf20Sopenharmony_ci
11538c2ecf20Sopenharmony_ci	ensoniq->mode |= ES_MODE_CAPTURE;
11548c2ecf20Sopenharmony_ci	ensoniq->capture_substream = substream;
11558c2ecf20Sopenharmony_ci	runtime->hw = snd_ensoniq_capture;
11568c2ecf20Sopenharmony_ci	snd_pcm_set_sync(substream);
11578c2ecf20Sopenharmony_ci#ifdef CHIP1370
11588c2ecf20Sopenharmony_ci	snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
11598c2ecf20Sopenharmony_ci				      &snd_es1370_hw_constraints_clock);
11608c2ecf20Sopenharmony_ci#else
11618c2ecf20Sopenharmony_ci	snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
11628c2ecf20Sopenharmony_ci				      &snd_es1371_hw_constraints_adc_clock);
11638c2ecf20Sopenharmony_ci#endif
11648c2ecf20Sopenharmony_ci	return 0;
11658c2ecf20Sopenharmony_ci}
11668c2ecf20Sopenharmony_ci
11678c2ecf20Sopenharmony_cistatic int snd_ensoniq_playback1_close(struct snd_pcm_substream *substream)
11688c2ecf20Sopenharmony_ci{
11698c2ecf20Sopenharmony_ci	struct ensoniq *ensoniq = snd_pcm_substream_chip(substream);
11708c2ecf20Sopenharmony_ci
11718c2ecf20Sopenharmony_ci	ensoniq->playback1_substream = NULL;
11728c2ecf20Sopenharmony_ci	ensoniq->mode &= ~ES_MODE_PLAY1;
11738c2ecf20Sopenharmony_ci	return 0;
11748c2ecf20Sopenharmony_ci}
11758c2ecf20Sopenharmony_ci
11768c2ecf20Sopenharmony_cistatic int snd_ensoniq_playback2_close(struct snd_pcm_substream *substream)
11778c2ecf20Sopenharmony_ci{
11788c2ecf20Sopenharmony_ci	struct ensoniq *ensoniq = snd_pcm_substream_chip(substream);
11798c2ecf20Sopenharmony_ci
11808c2ecf20Sopenharmony_ci	ensoniq->playback2_substream = NULL;
11818c2ecf20Sopenharmony_ci	spin_lock_irq(&ensoniq->reg_lock);
11828c2ecf20Sopenharmony_ci#ifdef CHIP1370
11838c2ecf20Sopenharmony_ci	ensoniq->u.es1370.pclkdiv_lock &= ~ES_MODE_PLAY2;
11848c2ecf20Sopenharmony_ci#endif
11858c2ecf20Sopenharmony_ci	ensoniq->mode &= ~ES_MODE_PLAY2;
11868c2ecf20Sopenharmony_ci	spin_unlock_irq(&ensoniq->reg_lock);
11878c2ecf20Sopenharmony_ci	return 0;
11888c2ecf20Sopenharmony_ci}
11898c2ecf20Sopenharmony_ci
11908c2ecf20Sopenharmony_cistatic int snd_ensoniq_capture_close(struct snd_pcm_substream *substream)
11918c2ecf20Sopenharmony_ci{
11928c2ecf20Sopenharmony_ci	struct ensoniq *ensoniq = snd_pcm_substream_chip(substream);
11938c2ecf20Sopenharmony_ci
11948c2ecf20Sopenharmony_ci	ensoniq->capture_substream = NULL;
11958c2ecf20Sopenharmony_ci	spin_lock_irq(&ensoniq->reg_lock);
11968c2ecf20Sopenharmony_ci#ifdef CHIP1370
11978c2ecf20Sopenharmony_ci	ensoniq->u.es1370.pclkdiv_lock &= ~ES_MODE_CAPTURE;
11988c2ecf20Sopenharmony_ci#endif
11998c2ecf20Sopenharmony_ci	ensoniq->mode &= ~ES_MODE_CAPTURE;
12008c2ecf20Sopenharmony_ci	spin_unlock_irq(&ensoniq->reg_lock);
12018c2ecf20Sopenharmony_ci	return 0;
12028c2ecf20Sopenharmony_ci}
12038c2ecf20Sopenharmony_ci
12048c2ecf20Sopenharmony_cistatic const struct snd_pcm_ops snd_ensoniq_playback1_ops = {
12058c2ecf20Sopenharmony_ci	.open =		snd_ensoniq_playback1_open,
12068c2ecf20Sopenharmony_ci	.close =	snd_ensoniq_playback1_close,
12078c2ecf20Sopenharmony_ci	.prepare =	snd_ensoniq_playback1_prepare,
12088c2ecf20Sopenharmony_ci	.trigger =	snd_ensoniq_trigger,
12098c2ecf20Sopenharmony_ci	.pointer =	snd_ensoniq_playback1_pointer,
12108c2ecf20Sopenharmony_ci};
12118c2ecf20Sopenharmony_ci
12128c2ecf20Sopenharmony_cistatic const struct snd_pcm_ops snd_ensoniq_playback2_ops = {
12138c2ecf20Sopenharmony_ci	.open =		snd_ensoniq_playback2_open,
12148c2ecf20Sopenharmony_ci	.close =	snd_ensoniq_playback2_close,
12158c2ecf20Sopenharmony_ci	.prepare =	snd_ensoniq_playback2_prepare,
12168c2ecf20Sopenharmony_ci	.trigger =	snd_ensoniq_trigger,
12178c2ecf20Sopenharmony_ci	.pointer =	snd_ensoniq_playback2_pointer,
12188c2ecf20Sopenharmony_ci};
12198c2ecf20Sopenharmony_ci
12208c2ecf20Sopenharmony_cistatic const struct snd_pcm_ops snd_ensoniq_capture_ops = {
12218c2ecf20Sopenharmony_ci	.open =		snd_ensoniq_capture_open,
12228c2ecf20Sopenharmony_ci	.close =	snd_ensoniq_capture_close,
12238c2ecf20Sopenharmony_ci	.prepare =	snd_ensoniq_capture_prepare,
12248c2ecf20Sopenharmony_ci	.trigger =	snd_ensoniq_trigger,
12258c2ecf20Sopenharmony_ci	.pointer =	snd_ensoniq_capture_pointer,
12268c2ecf20Sopenharmony_ci};
12278c2ecf20Sopenharmony_ci
12288c2ecf20Sopenharmony_cistatic const struct snd_pcm_chmap_elem surround_map[] = {
12298c2ecf20Sopenharmony_ci	{ .channels = 1,
12308c2ecf20Sopenharmony_ci	  .map = { SNDRV_CHMAP_MONO } },
12318c2ecf20Sopenharmony_ci	{ .channels = 2,
12328c2ecf20Sopenharmony_ci	  .map = { SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
12338c2ecf20Sopenharmony_ci	{ }
12348c2ecf20Sopenharmony_ci};
12358c2ecf20Sopenharmony_ci
12368c2ecf20Sopenharmony_cistatic int snd_ensoniq_pcm(struct ensoniq *ensoniq, int device)
12378c2ecf20Sopenharmony_ci{
12388c2ecf20Sopenharmony_ci	struct snd_pcm *pcm;
12398c2ecf20Sopenharmony_ci	int err;
12408c2ecf20Sopenharmony_ci
12418c2ecf20Sopenharmony_ci	err = snd_pcm_new(ensoniq->card, CHIP_NAME "/1", device, 1, 1, &pcm);
12428c2ecf20Sopenharmony_ci	if (err < 0)
12438c2ecf20Sopenharmony_ci		return err;
12448c2ecf20Sopenharmony_ci
12458c2ecf20Sopenharmony_ci#ifdef CHIP1370
12468c2ecf20Sopenharmony_ci	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ensoniq_playback2_ops);
12478c2ecf20Sopenharmony_ci#else
12488c2ecf20Sopenharmony_ci	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ensoniq_playback1_ops);
12498c2ecf20Sopenharmony_ci#endif
12508c2ecf20Sopenharmony_ci	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ensoniq_capture_ops);
12518c2ecf20Sopenharmony_ci
12528c2ecf20Sopenharmony_ci	pcm->private_data = ensoniq;
12538c2ecf20Sopenharmony_ci	pcm->info_flags = 0;
12548c2ecf20Sopenharmony_ci	strcpy(pcm->name, CHIP_NAME " DAC2/ADC");
12558c2ecf20Sopenharmony_ci	ensoniq->pcm1 = pcm;
12568c2ecf20Sopenharmony_ci
12578c2ecf20Sopenharmony_ci	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
12588c2ecf20Sopenharmony_ci				       &ensoniq->pci->dev, 64*1024, 128*1024);
12598c2ecf20Sopenharmony_ci
12608c2ecf20Sopenharmony_ci#ifdef CHIP1370
12618c2ecf20Sopenharmony_ci	err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
12628c2ecf20Sopenharmony_ci				     surround_map, 2, 0, NULL);
12638c2ecf20Sopenharmony_ci#else
12648c2ecf20Sopenharmony_ci	err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
12658c2ecf20Sopenharmony_ci				     snd_pcm_std_chmaps, 2, 0, NULL);
12668c2ecf20Sopenharmony_ci#endif
12678c2ecf20Sopenharmony_ci	return err;
12688c2ecf20Sopenharmony_ci}
12698c2ecf20Sopenharmony_ci
12708c2ecf20Sopenharmony_cistatic int snd_ensoniq_pcm2(struct ensoniq *ensoniq, int device)
12718c2ecf20Sopenharmony_ci{
12728c2ecf20Sopenharmony_ci	struct snd_pcm *pcm;
12738c2ecf20Sopenharmony_ci	int err;
12748c2ecf20Sopenharmony_ci
12758c2ecf20Sopenharmony_ci	err = snd_pcm_new(ensoniq->card, CHIP_NAME "/2", device, 1, 0, &pcm);
12768c2ecf20Sopenharmony_ci	if (err < 0)
12778c2ecf20Sopenharmony_ci		return err;
12788c2ecf20Sopenharmony_ci
12798c2ecf20Sopenharmony_ci#ifdef CHIP1370
12808c2ecf20Sopenharmony_ci	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ensoniq_playback1_ops);
12818c2ecf20Sopenharmony_ci#else
12828c2ecf20Sopenharmony_ci	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ensoniq_playback2_ops);
12838c2ecf20Sopenharmony_ci#endif
12848c2ecf20Sopenharmony_ci	pcm->private_data = ensoniq;
12858c2ecf20Sopenharmony_ci	pcm->info_flags = 0;
12868c2ecf20Sopenharmony_ci	strcpy(pcm->name, CHIP_NAME " DAC1");
12878c2ecf20Sopenharmony_ci	ensoniq->pcm2 = pcm;
12888c2ecf20Sopenharmony_ci
12898c2ecf20Sopenharmony_ci	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
12908c2ecf20Sopenharmony_ci				       &ensoniq->pci->dev, 64*1024, 128*1024);
12918c2ecf20Sopenharmony_ci
12928c2ecf20Sopenharmony_ci#ifdef CHIP1370
12938c2ecf20Sopenharmony_ci	err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
12948c2ecf20Sopenharmony_ci				     snd_pcm_std_chmaps, 2, 0, NULL);
12958c2ecf20Sopenharmony_ci#else
12968c2ecf20Sopenharmony_ci	err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
12978c2ecf20Sopenharmony_ci				     surround_map, 2, 0, NULL);
12988c2ecf20Sopenharmony_ci#endif
12998c2ecf20Sopenharmony_ci	return err;
13008c2ecf20Sopenharmony_ci}
13018c2ecf20Sopenharmony_ci
13028c2ecf20Sopenharmony_ci/*
13038c2ecf20Sopenharmony_ci *  Mixer section
13048c2ecf20Sopenharmony_ci */
13058c2ecf20Sopenharmony_ci
13068c2ecf20Sopenharmony_ci/*
13078c2ecf20Sopenharmony_ci * ENS1371 mixer (including SPDIF interface)
13088c2ecf20Sopenharmony_ci */
13098c2ecf20Sopenharmony_ci#ifdef CHIP1371
13108c2ecf20Sopenharmony_cistatic int snd_ens1373_spdif_info(struct snd_kcontrol *kcontrol,
13118c2ecf20Sopenharmony_ci				  struct snd_ctl_elem_info *uinfo)
13128c2ecf20Sopenharmony_ci{
13138c2ecf20Sopenharmony_ci	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
13148c2ecf20Sopenharmony_ci	uinfo->count = 1;
13158c2ecf20Sopenharmony_ci	return 0;
13168c2ecf20Sopenharmony_ci}
13178c2ecf20Sopenharmony_ci
13188c2ecf20Sopenharmony_cistatic int snd_ens1373_spdif_default_get(struct snd_kcontrol *kcontrol,
13198c2ecf20Sopenharmony_ci                                         struct snd_ctl_elem_value *ucontrol)
13208c2ecf20Sopenharmony_ci{
13218c2ecf20Sopenharmony_ci	struct ensoniq *ensoniq = snd_kcontrol_chip(kcontrol);
13228c2ecf20Sopenharmony_ci	spin_lock_irq(&ensoniq->reg_lock);
13238c2ecf20Sopenharmony_ci	ucontrol->value.iec958.status[0] = (ensoniq->spdif_default >> 0) & 0xff;
13248c2ecf20Sopenharmony_ci	ucontrol->value.iec958.status[1] = (ensoniq->spdif_default >> 8) & 0xff;
13258c2ecf20Sopenharmony_ci	ucontrol->value.iec958.status[2] = (ensoniq->spdif_default >> 16) & 0xff;
13268c2ecf20Sopenharmony_ci	ucontrol->value.iec958.status[3] = (ensoniq->spdif_default >> 24) & 0xff;
13278c2ecf20Sopenharmony_ci	spin_unlock_irq(&ensoniq->reg_lock);
13288c2ecf20Sopenharmony_ci	return 0;
13298c2ecf20Sopenharmony_ci}
13308c2ecf20Sopenharmony_ci
13318c2ecf20Sopenharmony_cistatic int snd_ens1373_spdif_default_put(struct snd_kcontrol *kcontrol,
13328c2ecf20Sopenharmony_ci                                         struct snd_ctl_elem_value *ucontrol)
13338c2ecf20Sopenharmony_ci{
13348c2ecf20Sopenharmony_ci	struct ensoniq *ensoniq = snd_kcontrol_chip(kcontrol);
13358c2ecf20Sopenharmony_ci	unsigned int val;
13368c2ecf20Sopenharmony_ci	int change;
13378c2ecf20Sopenharmony_ci
13388c2ecf20Sopenharmony_ci	val = ((u32)ucontrol->value.iec958.status[0] << 0) |
13398c2ecf20Sopenharmony_ci	      ((u32)ucontrol->value.iec958.status[1] << 8) |
13408c2ecf20Sopenharmony_ci	      ((u32)ucontrol->value.iec958.status[2] << 16) |
13418c2ecf20Sopenharmony_ci	      ((u32)ucontrol->value.iec958.status[3] << 24);
13428c2ecf20Sopenharmony_ci	spin_lock_irq(&ensoniq->reg_lock);
13438c2ecf20Sopenharmony_ci	change = ensoniq->spdif_default != val;
13448c2ecf20Sopenharmony_ci	ensoniq->spdif_default = val;
13458c2ecf20Sopenharmony_ci	if (change && ensoniq->playback1_substream == NULL &&
13468c2ecf20Sopenharmony_ci	    ensoniq->playback2_substream == NULL)
13478c2ecf20Sopenharmony_ci		outl(val, ES_REG(ensoniq, CHANNEL_STATUS));
13488c2ecf20Sopenharmony_ci	spin_unlock_irq(&ensoniq->reg_lock);
13498c2ecf20Sopenharmony_ci	return change;
13508c2ecf20Sopenharmony_ci}
13518c2ecf20Sopenharmony_ci
13528c2ecf20Sopenharmony_cistatic int snd_ens1373_spdif_mask_get(struct snd_kcontrol *kcontrol,
13538c2ecf20Sopenharmony_ci				      struct snd_ctl_elem_value *ucontrol)
13548c2ecf20Sopenharmony_ci{
13558c2ecf20Sopenharmony_ci	ucontrol->value.iec958.status[0] = 0xff;
13568c2ecf20Sopenharmony_ci	ucontrol->value.iec958.status[1] = 0xff;
13578c2ecf20Sopenharmony_ci	ucontrol->value.iec958.status[2] = 0xff;
13588c2ecf20Sopenharmony_ci	ucontrol->value.iec958.status[3] = 0xff;
13598c2ecf20Sopenharmony_ci	return 0;
13608c2ecf20Sopenharmony_ci}
13618c2ecf20Sopenharmony_ci
13628c2ecf20Sopenharmony_cistatic int snd_ens1373_spdif_stream_get(struct snd_kcontrol *kcontrol,
13638c2ecf20Sopenharmony_ci					struct snd_ctl_elem_value *ucontrol)
13648c2ecf20Sopenharmony_ci{
13658c2ecf20Sopenharmony_ci	struct ensoniq *ensoniq = snd_kcontrol_chip(kcontrol);
13668c2ecf20Sopenharmony_ci	spin_lock_irq(&ensoniq->reg_lock);
13678c2ecf20Sopenharmony_ci	ucontrol->value.iec958.status[0] = (ensoniq->spdif_stream >> 0) & 0xff;
13688c2ecf20Sopenharmony_ci	ucontrol->value.iec958.status[1] = (ensoniq->spdif_stream >> 8) & 0xff;
13698c2ecf20Sopenharmony_ci	ucontrol->value.iec958.status[2] = (ensoniq->spdif_stream >> 16) & 0xff;
13708c2ecf20Sopenharmony_ci	ucontrol->value.iec958.status[3] = (ensoniq->spdif_stream >> 24) & 0xff;
13718c2ecf20Sopenharmony_ci	spin_unlock_irq(&ensoniq->reg_lock);
13728c2ecf20Sopenharmony_ci	return 0;
13738c2ecf20Sopenharmony_ci}
13748c2ecf20Sopenharmony_ci
13758c2ecf20Sopenharmony_cistatic int snd_ens1373_spdif_stream_put(struct snd_kcontrol *kcontrol,
13768c2ecf20Sopenharmony_ci                                        struct snd_ctl_elem_value *ucontrol)
13778c2ecf20Sopenharmony_ci{
13788c2ecf20Sopenharmony_ci	struct ensoniq *ensoniq = snd_kcontrol_chip(kcontrol);
13798c2ecf20Sopenharmony_ci	unsigned int val;
13808c2ecf20Sopenharmony_ci	int change;
13818c2ecf20Sopenharmony_ci
13828c2ecf20Sopenharmony_ci	val = ((u32)ucontrol->value.iec958.status[0] << 0) |
13838c2ecf20Sopenharmony_ci	      ((u32)ucontrol->value.iec958.status[1] << 8) |
13848c2ecf20Sopenharmony_ci	      ((u32)ucontrol->value.iec958.status[2] << 16) |
13858c2ecf20Sopenharmony_ci	      ((u32)ucontrol->value.iec958.status[3] << 24);
13868c2ecf20Sopenharmony_ci	spin_lock_irq(&ensoniq->reg_lock);
13878c2ecf20Sopenharmony_ci	change = ensoniq->spdif_stream != val;
13888c2ecf20Sopenharmony_ci	ensoniq->spdif_stream = val;
13898c2ecf20Sopenharmony_ci	if (change && (ensoniq->playback1_substream != NULL ||
13908c2ecf20Sopenharmony_ci		       ensoniq->playback2_substream != NULL))
13918c2ecf20Sopenharmony_ci		outl(val, ES_REG(ensoniq, CHANNEL_STATUS));
13928c2ecf20Sopenharmony_ci	spin_unlock_irq(&ensoniq->reg_lock);
13938c2ecf20Sopenharmony_ci	return change;
13948c2ecf20Sopenharmony_ci}
13958c2ecf20Sopenharmony_ci
13968c2ecf20Sopenharmony_ci#define ES1371_SPDIF(xname) \
13978c2ecf20Sopenharmony_ci{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_es1371_spdif_info, \
13988c2ecf20Sopenharmony_ci  .get = snd_es1371_spdif_get, .put = snd_es1371_spdif_put }
13998c2ecf20Sopenharmony_ci
14008c2ecf20Sopenharmony_ci#define snd_es1371_spdif_info		snd_ctl_boolean_mono_info
14018c2ecf20Sopenharmony_ci
14028c2ecf20Sopenharmony_cistatic int snd_es1371_spdif_get(struct snd_kcontrol *kcontrol,
14038c2ecf20Sopenharmony_ci				struct snd_ctl_elem_value *ucontrol)
14048c2ecf20Sopenharmony_ci{
14058c2ecf20Sopenharmony_ci	struct ensoniq *ensoniq = snd_kcontrol_chip(kcontrol);
14068c2ecf20Sopenharmony_ci
14078c2ecf20Sopenharmony_ci	spin_lock_irq(&ensoniq->reg_lock);
14088c2ecf20Sopenharmony_ci	ucontrol->value.integer.value[0] = ensoniq->ctrl & ES_1373_SPDIF_THRU ? 1 : 0;
14098c2ecf20Sopenharmony_ci	spin_unlock_irq(&ensoniq->reg_lock);
14108c2ecf20Sopenharmony_ci	return 0;
14118c2ecf20Sopenharmony_ci}
14128c2ecf20Sopenharmony_ci
14138c2ecf20Sopenharmony_cistatic int snd_es1371_spdif_put(struct snd_kcontrol *kcontrol,
14148c2ecf20Sopenharmony_ci				struct snd_ctl_elem_value *ucontrol)
14158c2ecf20Sopenharmony_ci{
14168c2ecf20Sopenharmony_ci	struct ensoniq *ensoniq = snd_kcontrol_chip(kcontrol);
14178c2ecf20Sopenharmony_ci	unsigned int nval1, nval2;
14188c2ecf20Sopenharmony_ci	int change;
14198c2ecf20Sopenharmony_ci
14208c2ecf20Sopenharmony_ci	nval1 = ucontrol->value.integer.value[0] ? ES_1373_SPDIF_THRU : 0;
14218c2ecf20Sopenharmony_ci	nval2 = ucontrol->value.integer.value[0] ? ES_1373_SPDIF_EN : 0;
14228c2ecf20Sopenharmony_ci	spin_lock_irq(&ensoniq->reg_lock);
14238c2ecf20Sopenharmony_ci	change = (ensoniq->ctrl & ES_1373_SPDIF_THRU) != nval1;
14248c2ecf20Sopenharmony_ci	ensoniq->ctrl &= ~ES_1373_SPDIF_THRU;
14258c2ecf20Sopenharmony_ci	ensoniq->ctrl |= nval1;
14268c2ecf20Sopenharmony_ci	ensoniq->cssr &= ~ES_1373_SPDIF_EN;
14278c2ecf20Sopenharmony_ci	ensoniq->cssr |= nval2;
14288c2ecf20Sopenharmony_ci	outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
14298c2ecf20Sopenharmony_ci	outl(ensoniq->cssr, ES_REG(ensoniq, STATUS));
14308c2ecf20Sopenharmony_ci	spin_unlock_irq(&ensoniq->reg_lock);
14318c2ecf20Sopenharmony_ci	return change;
14328c2ecf20Sopenharmony_ci}
14338c2ecf20Sopenharmony_ci
14348c2ecf20Sopenharmony_ci
14358c2ecf20Sopenharmony_ci/* spdif controls */
14368c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new snd_es1371_mixer_spdif[] = {
14378c2ecf20Sopenharmony_ci	ES1371_SPDIF(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH)),
14388c2ecf20Sopenharmony_ci	{
14398c2ecf20Sopenharmony_ci		.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
14408c2ecf20Sopenharmony_ci		.name =		SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
14418c2ecf20Sopenharmony_ci		.info =		snd_ens1373_spdif_info,
14428c2ecf20Sopenharmony_ci		.get =		snd_ens1373_spdif_default_get,
14438c2ecf20Sopenharmony_ci		.put =		snd_ens1373_spdif_default_put,
14448c2ecf20Sopenharmony_ci	},
14458c2ecf20Sopenharmony_ci	{
14468c2ecf20Sopenharmony_ci		.access =	SNDRV_CTL_ELEM_ACCESS_READ,
14478c2ecf20Sopenharmony_ci		.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
14488c2ecf20Sopenharmony_ci		.name =		SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
14498c2ecf20Sopenharmony_ci		.info =		snd_ens1373_spdif_info,
14508c2ecf20Sopenharmony_ci		.get =		snd_ens1373_spdif_mask_get
14518c2ecf20Sopenharmony_ci	},
14528c2ecf20Sopenharmony_ci	{
14538c2ecf20Sopenharmony_ci		.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
14548c2ecf20Sopenharmony_ci		.name =		SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
14558c2ecf20Sopenharmony_ci		.info =		snd_ens1373_spdif_info,
14568c2ecf20Sopenharmony_ci		.get =		snd_ens1373_spdif_stream_get,
14578c2ecf20Sopenharmony_ci		.put =		snd_ens1373_spdif_stream_put
14588c2ecf20Sopenharmony_ci	},
14598c2ecf20Sopenharmony_ci};
14608c2ecf20Sopenharmony_ci
14618c2ecf20Sopenharmony_ci
14628c2ecf20Sopenharmony_ci#define snd_es1373_rear_info		snd_ctl_boolean_mono_info
14638c2ecf20Sopenharmony_ci
14648c2ecf20Sopenharmony_cistatic int snd_es1373_rear_get(struct snd_kcontrol *kcontrol,
14658c2ecf20Sopenharmony_ci			       struct snd_ctl_elem_value *ucontrol)
14668c2ecf20Sopenharmony_ci{
14678c2ecf20Sopenharmony_ci	struct ensoniq *ensoniq = snd_kcontrol_chip(kcontrol);
14688c2ecf20Sopenharmony_ci	int val = 0;
14698c2ecf20Sopenharmony_ci
14708c2ecf20Sopenharmony_ci	spin_lock_irq(&ensoniq->reg_lock);
14718c2ecf20Sopenharmony_ci	if ((ensoniq->cssr & (ES_1373_REAR_BIT27|ES_1373_REAR_BIT26|
14728c2ecf20Sopenharmony_ci			      ES_1373_REAR_BIT24)) == ES_1373_REAR_BIT26)
14738c2ecf20Sopenharmony_ci	    	val = 1;
14748c2ecf20Sopenharmony_ci	ucontrol->value.integer.value[0] = val;
14758c2ecf20Sopenharmony_ci	spin_unlock_irq(&ensoniq->reg_lock);
14768c2ecf20Sopenharmony_ci	return 0;
14778c2ecf20Sopenharmony_ci}
14788c2ecf20Sopenharmony_ci
14798c2ecf20Sopenharmony_cistatic int snd_es1373_rear_put(struct snd_kcontrol *kcontrol,
14808c2ecf20Sopenharmony_ci			       struct snd_ctl_elem_value *ucontrol)
14818c2ecf20Sopenharmony_ci{
14828c2ecf20Sopenharmony_ci	struct ensoniq *ensoniq = snd_kcontrol_chip(kcontrol);
14838c2ecf20Sopenharmony_ci	unsigned int nval1;
14848c2ecf20Sopenharmony_ci	int change;
14858c2ecf20Sopenharmony_ci
14868c2ecf20Sopenharmony_ci	nval1 = ucontrol->value.integer.value[0] ?
14878c2ecf20Sopenharmony_ci		ES_1373_REAR_BIT26 : (ES_1373_REAR_BIT27|ES_1373_REAR_BIT24);
14888c2ecf20Sopenharmony_ci	spin_lock_irq(&ensoniq->reg_lock);
14898c2ecf20Sopenharmony_ci	change = (ensoniq->cssr & (ES_1373_REAR_BIT27|
14908c2ecf20Sopenharmony_ci				   ES_1373_REAR_BIT26|ES_1373_REAR_BIT24)) != nval1;
14918c2ecf20Sopenharmony_ci	ensoniq->cssr &= ~(ES_1373_REAR_BIT27|ES_1373_REAR_BIT26|ES_1373_REAR_BIT24);
14928c2ecf20Sopenharmony_ci	ensoniq->cssr |= nval1;
14938c2ecf20Sopenharmony_ci	outl(ensoniq->cssr, ES_REG(ensoniq, STATUS));
14948c2ecf20Sopenharmony_ci	spin_unlock_irq(&ensoniq->reg_lock);
14958c2ecf20Sopenharmony_ci	return change;
14968c2ecf20Sopenharmony_ci}
14978c2ecf20Sopenharmony_ci
14988c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new snd_ens1373_rear =
14998c2ecf20Sopenharmony_ci{
15008c2ecf20Sopenharmony_ci	.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
15018c2ecf20Sopenharmony_ci	.name =		"AC97 2ch->4ch Copy Switch",
15028c2ecf20Sopenharmony_ci	.info =		snd_es1373_rear_info,
15038c2ecf20Sopenharmony_ci	.get =		snd_es1373_rear_get,
15048c2ecf20Sopenharmony_ci	.put =		snd_es1373_rear_put,
15058c2ecf20Sopenharmony_ci};
15068c2ecf20Sopenharmony_ci
15078c2ecf20Sopenharmony_ci#define snd_es1373_line_info		snd_ctl_boolean_mono_info
15088c2ecf20Sopenharmony_ci
15098c2ecf20Sopenharmony_cistatic int snd_es1373_line_get(struct snd_kcontrol *kcontrol,
15108c2ecf20Sopenharmony_ci			       struct snd_ctl_elem_value *ucontrol)
15118c2ecf20Sopenharmony_ci{
15128c2ecf20Sopenharmony_ci	struct ensoniq *ensoniq = snd_kcontrol_chip(kcontrol);
15138c2ecf20Sopenharmony_ci	int val = 0;
15148c2ecf20Sopenharmony_ci
15158c2ecf20Sopenharmony_ci	spin_lock_irq(&ensoniq->reg_lock);
15168c2ecf20Sopenharmony_ci	if (ensoniq->ctrl & ES_1371_GPIO_OUT(4))
15178c2ecf20Sopenharmony_ci	    	val = 1;
15188c2ecf20Sopenharmony_ci	ucontrol->value.integer.value[0] = val;
15198c2ecf20Sopenharmony_ci	spin_unlock_irq(&ensoniq->reg_lock);
15208c2ecf20Sopenharmony_ci	return 0;
15218c2ecf20Sopenharmony_ci}
15228c2ecf20Sopenharmony_ci
15238c2ecf20Sopenharmony_cistatic int snd_es1373_line_put(struct snd_kcontrol *kcontrol,
15248c2ecf20Sopenharmony_ci			       struct snd_ctl_elem_value *ucontrol)
15258c2ecf20Sopenharmony_ci{
15268c2ecf20Sopenharmony_ci	struct ensoniq *ensoniq = snd_kcontrol_chip(kcontrol);
15278c2ecf20Sopenharmony_ci	int changed;
15288c2ecf20Sopenharmony_ci	unsigned int ctrl;
15298c2ecf20Sopenharmony_ci
15308c2ecf20Sopenharmony_ci	spin_lock_irq(&ensoniq->reg_lock);
15318c2ecf20Sopenharmony_ci	ctrl = ensoniq->ctrl;
15328c2ecf20Sopenharmony_ci	if (ucontrol->value.integer.value[0])
15338c2ecf20Sopenharmony_ci		ensoniq->ctrl |= ES_1371_GPIO_OUT(4);	/* switch line-in -> rear out */
15348c2ecf20Sopenharmony_ci	else
15358c2ecf20Sopenharmony_ci		ensoniq->ctrl &= ~ES_1371_GPIO_OUT(4);
15368c2ecf20Sopenharmony_ci	changed = (ctrl != ensoniq->ctrl);
15378c2ecf20Sopenharmony_ci	if (changed)
15388c2ecf20Sopenharmony_ci		outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
15398c2ecf20Sopenharmony_ci	spin_unlock_irq(&ensoniq->reg_lock);
15408c2ecf20Sopenharmony_ci	return changed;
15418c2ecf20Sopenharmony_ci}
15428c2ecf20Sopenharmony_ci
15438c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new snd_ens1373_line =
15448c2ecf20Sopenharmony_ci{
15458c2ecf20Sopenharmony_ci	.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
15468c2ecf20Sopenharmony_ci	.name =		"Line In->Rear Out Switch",
15478c2ecf20Sopenharmony_ci	.info =		snd_es1373_line_info,
15488c2ecf20Sopenharmony_ci	.get =		snd_es1373_line_get,
15498c2ecf20Sopenharmony_ci	.put =		snd_es1373_line_put,
15508c2ecf20Sopenharmony_ci};
15518c2ecf20Sopenharmony_ci
15528c2ecf20Sopenharmony_cistatic void snd_ensoniq_mixer_free_ac97(struct snd_ac97 *ac97)
15538c2ecf20Sopenharmony_ci{
15548c2ecf20Sopenharmony_ci	struct ensoniq *ensoniq = ac97->private_data;
15558c2ecf20Sopenharmony_ci	ensoniq->u.es1371.ac97 = NULL;
15568c2ecf20Sopenharmony_ci}
15578c2ecf20Sopenharmony_ci
15588c2ecf20Sopenharmony_cistruct es1371_quirk {
15598c2ecf20Sopenharmony_ci	unsigned short vid;		/* vendor ID */
15608c2ecf20Sopenharmony_ci	unsigned short did;		/* device ID */
15618c2ecf20Sopenharmony_ci	unsigned char rev;		/* revision */
15628c2ecf20Sopenharmony_ci};
15638c2ecf20Sopenharmony_ci
15648c2ecf20Sopenharmony_cistatic int es1371_quirk_lookup(struct ensoniq *ensoniq,
15658c2ecf20Sopenharmony_ci			       const struct es1371_quirk *list)
15668c2ecf20Sopenharmony_ci{
15678c2ecf20Sopenharmony_ci	while (list->vid != (unsigned short)PCI_ANY_ID) {
15688c2ecf20Sopenharmony_ci		if (ensoniq->pci->vendor == list->vid &&
15698c2ecf20Sopenharmony_ci		    ensoniq->pci->device == list->did &&
15708c2ecf20Sopenharmony_ci		    ensoniq->rev == list->rev)
15718c2ecf20Sopenharmony_ci			return 1;
15728c2ecf20Sopenharmony_ci		list++;
15738c2ecf20Sopenharmony_ci	}
15748c2ecf20Sopenharmony_ci	return 0;
15758c2ecf20Sopenharmony_ci}
15768c2ecf20Sopenharmony_ci
15778c2ecf20Sopenharmony_cistatic const struct es1371_quirk es1371_spdif_present[] = {
15788c2ecf20Sopenharmony_ci	{ .vid = PCI_VENDOR_ID_ENSONIQ, .did = PCI_DEVICE_ID_ENSONIQ_CT5880, .rev = CT5880REV_CT5880_C },
15798c2ecf20Sopenharmony_ci	{ .vid = PCI_VENDOR_ID_ENSONIQ, .did = PCI_DEVICE_ID_ENSONIQ_CT5880, .rev = CT5880REV_CT5880_D },
15808c2ecf20Sopenharmony_ci	{ .vid = PCI_VENDOR_ID_ENSONIQ, .did = PCI_DEVICE_ID_ENSONIQ_CT5880, .rev = CT5880REV_CT5880_E },
15818c2ecf20Sopenharmony_ci	{ .vid = PCI_VENDOR_ID_ENSONIQ, .did = PCI_DEVICE_ID_ENSONIQ_ES1371, .rev = ES1371REV_CT5880_A },
15828c2ecf20Sopenharmony_ci	{ .vid = PCI_VENDOR_ID_ENSONIQ, .did = PCI_DEVICE_ID_ENSONIQ_ES1371, .rev = ES1371REV_ES1373_8 },
15838c2ecf20Sopenharmony_ci	{ .vid = PCI_ANY_ID, .did = PCI_ANY_ID }
15848c2ecf20Sopenharmony_ci};
15858c2ecf20Sopenharmony_ci
15868c2ecf20Sopenharmony_cistatic const struct snd_pci_quirk ens1373_line_quirk[] = {
15878c2ecf20Sopenharmony_ci	SND_PCI_QUIRK_ID(0x1274, 0x2000), /* GA-7DXR */
15888c2ecf20Sopenharmony_ci	SND_PCI_QUIRK_ID(0x1458, 0xa000), /* GA-8IEXP */
15898c2ecf20Sopenharmony_ci	{ } /* end */
15908c2ecf20Sopenharmony_ci};
15918c2ecf20Sopenharmony_ci
15928c2ecf20Sopenharmony_cistatic int snd_ensoniq_1371_mixer(struct ensoniq *ensoniq,
15938c2ecf20Sopenharmony_ci				  int has_spdif, int has_line)
15948c2ecf20Sopenharmony_ci{
15958c2ecf20Sopenharmony_ci	struct snd_card *card = ensoniq->card;
15968c2ecf20Sopenharmony_ci	struct snd_ac97_bus *pbus;
15978c2ecf20Sopenharmony_ci	struct snd_ac97_template ac97;
15988c2ecf20Sopenharmony_ci	int err;
15998c2ecf20Sopenharmony_ci	static const struct snd_ac97_bus_ops ops = {
16008c2ecf20Sopenharmony_ci		.write = snd_es1371_codec_write,
16018c2ecf20Sopenharmony_ci		.read = snd_es1371_codec_read,
16028c2ecf20Sopenharmony_ci		.wait = snd_es1371_codec_wait,
16038c2ecf20Sopenharmony_ci	};
16048c2ecf20Sopenharmony_ci
16058c2ecf20Sopenharmony_ci	if ((err = snd_ac97_bus(card, 0, &ops, NULL, &pbus)) < 0)
16068c2ecf20Sopenharmony_ci		return err;
16078c2ecf20Sopenharmony_ci
16088c2ecf20Sopenharmony_ci	memset(&ac97, 0, sizeof(ac97));
16098c2ecf20Sopenharmony_ci	ac97.private_data = ensoniq;
16108c2ecf20Sopenharmony_ci	ac97.private_free = snd_ensoniq_mixer_free_ac97;
16118c2ecf20Sopenharmony_ci	ac97.pci = ensoniq->pci;
16128c2ecf20Sopenharmony_ci	ac97.scaps = AC97_SCAP_AUDIO;
16138c2ecf20Sopenharmony_ci	if ((err = snd_ac97_mixer(pbus, &ac97, &ensoniq->u.es1371.ac97)) < 0)
16148c2ecf20Sopenharmony_ci		return err;
16158c2ecf20Sopenharmony_ci	if (has_spdif > 0 ||
16168c2ecf20Sopenharmony_ci	    (!has_spdif && es1371_quirk_lookup(ensoniq, es1371_spdif_present))) {
16178c2ecf20Sopenharmony_ci		struct snd_kcontrol *kctl;
16188c2ecf20Sopenharmony_ci		int i, is_spdif = 0;
16198c2ecf20Sopenharmony_ci
16208c2ecf20Sopenharmony_ci		ensoniq->spdif_default = ensoniq->spdif_stream =
16218c2ecf20Sopenharmony_ci			SNDRV_PCM_DEFAULT_CON_SPDIF;
16228c2ecf20Sopenharmony_ci		outl(ensoniq->spdif_default, ES_REG(ensoniq, CHANNEL_STATUS));
16238c2ecf20Sopenharmony_ci
16248c2ecf20Sopenharmony_ci		if (ensoniq->u.es1371.ac97->ext_id & AC97_EI_SPDIF)
16258c2ecf20Sopenharmony_ci			is_spdif++;
16268c2ecf20Sopenharmony_ci
16278c2ecf20Sopenharmony_ci		for (i = 0; i < ARRAY_SIZE(snd_es1371_mixer_spdif); i++) {
16288c2ecf20Sopenharmony_ci			kctl = snd_ctl_new1(&snd_es1371_mixer_spdif[i], ensoniq);
16298c2ecf20Sopenharmony_ci			if (!kctl)
16308c2ecf20Sopenharmony_ci				return -ENOMEM;
16318c2ecf20Sopenharmony_ci			kctl->id.index = is_spdif;
16328c2ecf20Sopenharmony_ci			err = snd_ctl_add(card, kctl);
16338c2ecf20Sopenharmony_ci			if (err < 0)
16348c2ecf20Sopenharmony_ci				return err;
16358c2ecf20Sopenharmony_ci		}
16368c2ecf20Sopenharmony_ci	}
16378c2ecf20Sopenharmony_ci	if (ensoniq->u.es1371.ac97->ext_id & AC97_EI_SDAC) {
16388c2ecf20Sopenharmony_ci		/* mirror rear to front speakers */
16398c2ecf20Sopenharmony_ci		ensoniq->cssr &= ~(ES_1373_REAR_BIT27|ES_1373_REAR_BIT24);
16408c2ecf20Sopenharmony_ci		ensoniq->cssr |= ES_1373_REAR_BIT26;
16418c2ecf20Sopenharmony_ci		err = snd_ctl_add(card, snd_ctl_new1(&snd_ens1373_rear, ensoniq));
16428c2ecf20Sopenharmony_ci		if (err < 0)
16438c2ecf20Sopenharmony_ci			return err;
16448c2ecf20Sopenharmony_ci	}
16458c2ecf20Sopenharmony_ci	if (has_line > 0 ||
16468c2ecf20Sopenharmony_ci	    snd_pci_quirk_lookup(ensoniq->pci, ens1373_line_quirk)) {
16478c2ecf20Sopenharmony_ci		 err = snd_ctl_add(card, snd_ctl_new1(&snd_ens1373_line,
16488c2ecf20Sopenharmony_ci						      ensoniq));
16498c2ecf20Sopenharmony_ci		 if (err < 0)
16508c2ecf20Sopenharmony_ci			 return err;
16518c2ecf20Sopenharmony_ci	}
16528c2ecf20Sopenharmony_ci
16538c2ecf20Sopenharmony_ci	return 0;
16548c2ecf20Sopenharmony_ci}
16558c2ecf20Sopenharmony_ci
16568c2ecf20Sopenharmony_ci#endif /* CHIP1371 */
16578c2ecf20Sopenharmony_ci
16588c2ecf20Sopenharmony_ci/* generic control callbacks for ens1370 */
16598c2ecf20Sopenharmony_ci#ifdef CHIP1370
16608c2ecf20Sopenharmony_ci#define ENSONIQ_CONTROL(xname, mask) \
16618c2ecf20Sopenharmony_ci{ .iface = SNDRV_CTL_ELEM_IFACE_CARD, .name = xname, .info = snd_ensoniq_control_info, \
16628c2ecf20Sopenharmony_ci  .get = snd_ensoniq_control_get, .put = snd_ensoniq_control_put, \
16638c2ecf20Sopenharmony_ci  .private_value = mask }
16648c2ecf20Sopenharmony_ci
16658c2ecf20Sopenharmony_ci#define snd_ensoniq_control_info	snd_ctl_boolean_mono_info
16668c2ecf20Sopenharmony_ci
16678c2ecf20Sopenharmony_cistatic int snd_ensoniq_control_get(struct snd_kcontrol *kcontrol,
16688c2ecf20Sopenharmony_ci				   struct snd_ctl_elem_value *ucontrol)
16698c2ecf20Sopenharmony_ci{
16708c2ecf20Sopenharmony_ci	struct ensoniq *ensoniq = snd_kcontrol_chip(kcontrol);
16718c2ecf20Sopenharmony_ci	int mask = kcontrol->private_value;
16728c2ecf20Sopenharmony_ci
16738c2ecf20Sopenharmony_ci	spin_lock_irq(&ensoniq->reg_lock);
16748c2ecf20Sopenharmony_ci	ucontrol->value.integer.value[0] = ensoniq->ctrl & mask ? 1 : 0;
16758c2ecf20Sopenharmony_ci	spin_unlock_irq(&ensoniq->reg_lock);
16768c2ecf20Sopenharmony_ci	return 0;
16778c2ecf20Sopenharmony_ci}
16788c2ecf20Sopenharmony_ci
16798c2ecf20Sopenharmony_cistatic int snd_ensoniq_control_put(struct snd_kcontrol *kcontrol,
16808c2ecf20Sopenharmony_ci				   struct snd_ctl_elem_value *ucontrol)
16818c2ecf20Sopenharmony_ci{
16828c2ecf20Sopenharmony_ci	struct ensoniq *ensoniq = snd_kcontrol_chip(kcontrol);
16838c2ecf20Sopenharmony_ci	int mask = kcontrol->private_value;
16848c2ecf20Sopenharmony_ci	unsigned int nval;
16858c2ecf20Sopenharmony_ci	int change;
16868c2ecf20Sopenharmony_ci
16878c2ecf20Sopenharmony_ci	nval = ucontrol->value.integer.value[0] ? mask : 0;
16888c2ecf20Sopenharmony_ci	spin_lock_irq(&ensoniq->reg_lock);
16898c2ecf20Sopenharmony_ci	change = (ensoniq->ctrl & mask) != nval;
16908c2ecf20Sopenharmony_ci	ensoniq->ctrl &= ~mask;
16918c2ecf20Sopenharmony_ci	ensoniq->ctrl |= nval;
16928c2ecf20Sopenharmony_ci	outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
16938c2ecf20Sopenharmony_ci	spin_unlock_irq(&ensoniq->reg_lock);
16948c2ecf20Sopenharmony_ci	return change;
16958c2ecf20Sopenharmony_ci}
16968c2ecf20Sopenharmony_ci
16978c2ecf20Sopenharmony_ci/*
16988c2ecf20Sopenharmony_ci * ENS1370 mixer
16998c2ecf20Sopenharmony_ci */
17008c2ecf20Sopenharmony_ci
17018c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new snd_es1370_controls[2] = {
17028c2ecf20Sopenharmony_ciENSONIQ_CONTROL("PCM 0 Output also on Line-In Jack", ES_1370_XCTL0),
17038c2ecf20Sopenharmony_ciENSONIQ_CONTROL("Mic +5V bias", ES_1370_XCTL1)
17048c2ecf20Sopenharmony_ci};
17058c2ecf20Sopenharmony_ci
17068c2ecf20Sopenharmony_ci#define ES1370_CONTROLS ARRAY_SIZE(snd_es1370_controls)
17078c2ecf20Sopenharmony_ci
17088c2ecf20Sopenharmony_cistatic void snd_ensoniq_mixer_free_ak4531(struct snd_ak4531 *ak4531)
17098c2ecf20Sopenharmony_ci{
17108c2ecf20Sopenharmony_ci	struct ensoniq *ensoniq = ak4531->private_data;
17118c2ecf20Sopenharmony_ci	ensoniq->u.es1370.ak4531 = NULL;
17128c2ecf20Sopenharmony_ci}
17138c2ecf20Sopenharmony_ci
17148c2ecf20Sopenharmony_cistatic int snd_ensoniq_1370_mixer(struct ensoniq *ensoniq)
17158c2ecf20Sopenharmony_ci{
17168c2ecf20Sopenharmony_ci	struct snd_card *card = ensoniq->card;
17178c2ecf20Sopenharmony_ci	struct snd_ak4531 ak4531;
17188c2ecf20Sopenharmony_ci	unsigned int idx;
17198c2ecf20Sopenharmony_ci	int err;
17208c2ecf20Sopenharmony_ci
17218c2ecf20Sopenharmony_ci	/* try reset AK4531 */
17228c2ecf20Sopenharmony_ci	outw(ES_1370_CODEC_WRITE(AK4531_RESET, 0x02), ES_REG(ensoniq, 1370_CODEC));
17238c2ecf20Sopenharmony_ci	inw(ES_REG(ensoniq, 1370_CODEC));
17248c2ecf20Sopenharmony_ci	udelay(100);
17258c2ecf20Sopenharmony_ci	outw(ES_1370_CODEC_WRITE(AK4531_RESET, 0x03), ES_REG(ensoniq, 1370_CODEC));
17268c2ecf20Sopenharmony_ci	inw(ES_REG(ensoniq, 1370_CODEC));
17278c2ecf20Sopenharmony_ci	udelay(100);
17288c2ecf20Sopenharmony_ci
17298c2ecf20Sopenharmony_ci	memset(&ak4531, 0, sizeof(ak4531));
17308c2ecf20Sopenharmony_ci	ak4531.write = snd_es1370_codec_write;
17318c2ecf20Sopenharmony_ci	ak4531.private_data = ensoniq;
17328c2ecf20Sopenharmony_ci	ak4531.private_free = snd_ensoniq_mixer_free_ak4531;
17338c2ecf20Sopenharmony_ci	if ((err = snd_ak4531_mixer(card, &ak4531, &ensoniq->u.es1370.ak4531)) < 0)
17348c2ecf20Sopenharmony_ci		return err;
17358c2ecf20Sopenharmony_ci	for (idx = 0; idx < ES1370_CONTROLS; idx++) {
17368c2ecf20Sopenharmony_ci		err = snd_ctl_add(card, snd_ctl_new1(&snd_es1370_controls[idx], ensoniq));
17378c2ecf20Sopenharmony_ci		if (err < 0)
17388c2ecf20Sopenharmony_ci			return err;
17398c2ecf20Sopenharmony_ci	}
17408c2ecf20Sopenharmony_ci	return 0;
17418c2ecf20Sopenharmony_ci}
17428c2ecf20Sopenharmony_ci
17438c2ecf20Sopenharmony_ci#endif /* CHIP1370 */
17448c2ecf20Sopenharmony_ci
17458c2ecf20Sopenharmony_ci#ifdef SUPPORT_JOYSTICK
17468c2ecf20Sopenharmony_ci
17478c2ecf20Sopenharmony_ci#ifdef CHIP1371
17488c2ecf20Sopenharmony_cistatic int snd_ensoniq_get_joystick_port(struct ensoniq *ensoniq, int dev)
17498c2ecf20Sopenharmony_ci{
17508c2ecf20Sopenharmony_ci	switch (joystick_port[dev]) {
17518c2ecf20Sopenharmony_ci	case 0: /* disabled */
17528c2ecf20Sopenharmony_ci	case 1: /* auto-detect */
17538c2ecf20Sopenharmony_ci	case 0x200:
17548c2ecf20Sopenharmony_ci	case 0x208:
17558c2ecf20Sopenharmony_ci	case 0x210:
17568c2ecf20Sopenharmony_ci	case 0x218:
17578c2ecf20Sopenharmony_ci		return joystick_port[dev];
17588c2ecf20Sopenharmony_ci
17598c2ecf20Sopenharmony_ci	default:
17608c2ecf20Sopenharmony_ci		dev_err(ensoniq->card->dev,
17618c2ecf20Sopenharmony_ci			"invalid joystick port %#x", joystick_port[dev]);
17628c2ecf20Sopenharmony_ci		return 0;
17638c2ecf20Sopenharmony_ci	}
17648c2ecf20Sopenharmony_ci}
17658c2ecf20Sopenharmony_ci#else
17668c2ecf20Sopenharmony_cistatic int snd_ensoniq_get_joystick_port(struct ensoniq *ensoniq, int dev)
17678c2ecf20Sopenharmony_ci{
17688c2ecf20Sopenharmony_ci	return joystick[dev] ? 0x200 : 0;
17698c2ecf20Sopenharmony_ci}
17708c2ecf20Sopenharmony_ci#endif
17718c2ecf20Sopenharmony_ci
17728c2ecf20Sopenharmony_cistatic int snd_ensoniq_create_gameport(struct ensoniq *ensoniq, int dev)
17738c2ecf20Sopenharmony_ci{
17748c2ecf20Sopenharmony_ci	struct gameport *gp;
17758c2ecf20Sopenharmony_ci	int io_port;
17768c2ecf20Sopenharmony_ci
17778c2ecf20Sopenharmony_ci	io_port = snd_ensoniq_get_joystick_port(ensoniq, dev);
17788c2ecf20Sopenharmony_ci
17798c2ecf20Sopenharmony_ci	switch (io_port) {
17808c2ecf20Sopenharmony_ci	case 0:
17818c2ecf20Sopenharmony_ci		return -ENOSYS;
17828c2ecf20Sopenharmony_ci
17838c2ecf20Sopenharmony_ci	case 1: /* auto_detect */
17848c2ecf20Sopenharmony_ci		for (io_port = 0x200; io_port <= 0x218; io_port += 8)
17858c2ecf20Sopenharmony_ci			if (request_region(io_port, 8, "ens137x: gameport"))
17868c2ecf20Sopenharmony_ci				break;
17878c2ecf20Sopenharmony_ci		if (io_port > 0x218) {
17888c2ecf20Sopenharmony_ci			dev_warn(ensoniq->card->dev,
17898c2ecf20Sopenharmony_ci				 "no gameport ports available\n");
17908c2ecf20Sopenharmony_ci			return -EBUSY;
17918c2ecf20Sopenharmony_ci		}
17928c2ecf20Sopenharmony_ci		break;
17938c2ecf20Sopenharmony_ci
17948c2ecf20Sopenharmony_ci	default:
17958c2ecf20Sopenharmony_ci		if (!request_region(io_port, 8, "ens137x: gameport")) {
17968c2ecf20Sopenharmony_ci			dev_warn(ensoniq->card->dev,
17978c2ecf20Sopenharmony_ci				 "gameport io port %#x in use\n",
17988c2ecf20Sopenharmony_ci			       io_port);
17998c2ecf20Sopenharmony_ci			return -EBUSY;
18008c2ecf20Sopenharmony_ci		}
18018c2ecf20Sopenharmony_ci		break;
18028c2ecf20Sopenharmony_ci	}
18038c2ecf20Sopenharmony_ci
18048c2ecf20Sopenharmony_ci	ensoniq->gameport = gp = gameport_allocate_port();
18058c2ecf20Sopenharmony_ci	if (!gp) {
18068c2ecf20Sopenharmony_ci		dev_err(ensoniq->card->dev,
18078c2ecf20Sopenharmony_ci			"cannot allocate memory for gameport\n");
18088c2ecf20Sopenharmony_ci		release_region(io_port, 8);
18098c2ecf20Sopenharmony_ci		return -ENOMEM;
18108c2ecf20Sopenharmony_ci	}
18118c2ecf20Sopenharmony_ci
18128c2ecf20Sopenharmony_ci	gameport_set_name(gp, "ES137x");
18138c2ecf20Sopenharmony_ci	gameport_set_phys(gp, "pci%s/gameport0", pci_name(ensoniq->pci));
18148c2ecf20Sopenharmony_ci	gameport_set_dev_parent(gp, &ensoniq->pci->dev);
18158c2ecf20Sopenharmony_ci	gp->io = io_port;
18168c2ecf20Sopenharmony_ci
18178c2ecf20Sopenharmony_ci	ensoniq->ctrl |= ES_JYSTK_EN;
18188c2ecf20Sopenharmony_ci#ifdef CHIP1371
18198c2ecf20Sopenharmony_ci	ensoniq->ctrl &= ~ES_1371_JOY_ASELM;
18208c2ecf20Sopenharmony_ci	ensoniq->ctrl |= ES_1371_JOY_ASEL((io_port - 0x200) / 8);
18218c2ecf20Sopenharmony_ci#endif
18228c2ecf20Sopenharmony_ci	outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
18238c2ecf20Sopenharmony_ci
18248c2ecf20Sopenharmony_ci	gameport_register_port(ensoniq->gameport);
18258c2ecf20Sopenharmony_ci
18268c2ecf20Sopenharmony_ci	return 0;
18278c2ecf20Sopenharmony_ci}
18288c2ecf20Sopenharmony_ci
18298c2ecf20Sopenharmony_cistatic void snd_ensoniq_free_gameport(struct ensoniq *ensoniq)
18308c2ecf20Sopenharmony_ci{
18318c2ecf20Sopenharmony_ci	if (ensoniq->gameport) {
18328c2ecf20Sopenharmony_ci		int port = ensoniq->gameport->io;
18338c2ecf20Sopenharmony_ci
18348c2ecf20Sopenharmony_ci		gameport_unregister_port(ensoniq->gameport);
18358c2ecf20Sopenharmony_ci		ensoniq->gameport = NULL;
18368c2ecf20Sopenharmony_ci		ensoniq->ctrl &= ~ES_JYSTK_EN;
18378c2ecf20Sopenharmony_ci		outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
18388c2ecf20Sopenharmony_ci		release_region(port, 8);
18398c2ecf20Sopenharmony_ci	}
18408c2ecf20Sopenharmony_ci}
18418c2ecf20Sopenharmony_ci#else
18428c2ecf20Sopenharmony_cistatic inline int snd_ensoniq_create_gameport(struct ensoniq *ensoniq, long port) { return -ENOSYS; }
18438c2ecf20Sopenharmony_cistatic inline void snd_ensoniq_free_gameport(struct ensoniq *ensoniq) { }
18448c2ecf20Sopenharmony_ci#endif /* SUPPORT_JOYSTICK */
18458c2ecf20Sopenharmony_ci
18468c2ecf20Sopenharmony_ci/*
18478c2ecf20Sopenharmony_ci
18488c2ecf20Sopenharmony_ci */
18498c2ecf20Sopenharmony_ci
18508c2ecf20Sopenharmony_cistatic void snd_ensoniq_proc_read(struct snd_info_entry *entry,
18518c2ecf20Sopenharmony_ci				  struct snd_info_buffer *buffer)
18528c2ecf20Sopenharmony_ci{
18538c2ecf20Sopenharmony_ci	struct ensoniq *ensoniq = entry->private_data;
18548c2ecf20Sopenharmony_ci
18558c2ecf20Sopenharmony_ci	snd_iprintf(buffer, "Ensoniq AudioPCI " CHIP_NAME "\n\n");
18568c2ecf20Sopenharmony_ci	snd_iprintf(buffer, "Joystick enable  : %s\n",
18578c2ecf20Sopenharmony_ci		    ensoniq->ctrl & ES_JYSTK_EN ? "on" : "off");
18588c2ecf20Sopenharmony_ci#ifdef CHIP1370
18598c2ecf20Sopenharmony_ci	snd_iprintf(buffer, "MIC +5V bias     : %s\n",
18608c2ecf20Sopenharmony_ci		    ensoniq->ctrl & ES_1370_XCTL1 ? "on" : "off");
18618c2ecf20Sopenharmony_ci	snd_iprintf(buffer, "Line In to AOUT  : %s\n",
18628c2ecf20Sopenharmony_ci		    ensoniq->ctrl & ES_1370_XCTL0 ? "on" : "off");
18638c2ecf20Sopenharmony_ci#else
18648c2ecf20Sopenharmony_ci	snd_iprintf(buffer, "Joystick port    : 0x%x\n",
18658c2ecf20Sopenharmony_ci		    (ES_1371_JOY_ASELI(ensoniq->ctrl) * 8) + 0x200);
18668c2ecf20Sopenharmony_ci#endif
18678c2ecf20Sopenharmony_ci}
18688c2ecf20Sopenharmony_ci
18698c2ecf20Sopenharmony_cistatic void snd_ensoniq_proc_init(struct ensoniq *ensoniq)
18708c2ecf20Sopenharmony_ci{
18718c2ecf20Sopenharmony_ci	snd_card_ro_proc_new(ensoniq->card, "audiopci", ensoniq,
18728c2ecf20Sopenharmony_ci			     snd_ensoniq_proc_read);
18738c2ecf20Sopenharmony_ci}
18748c2ecf20Sopenharmony_ci
18758c2ecf20Sopenharmony_ci/*
18768c2ecf20Sopenharmony_ci
18778c2ecf20Sopenharmony_ci */
18788c2ecf20Sopenharmony_ci
18798c2ecf20Sopenharmony_cistatic int snd_ensoniq_free(struct ensoniq *ensoniq)
18808c2ecf20Sopenharmony_ci{
18818c2ecf20Sopenharmony_ci	snd_ensoniq_free_gameport(ensoniq);
18828c2ecf20Sopenharmony_ci	if (ensoniq->irq < 0)
18838c2ecf20Sopenharmony_ci		goto __hw_end;
18848c2ecf20Sopenharmony_ci#ifdef CHIP1370
18858c2ecf20Sopenharmony_ci	outl(ES_1370_SERR_DISABLE, ES_REG(ensoniq, CONTROL));	/* switch everything off */
18868c2ecf20Sopenharmony_ci	outl(0, ES_REG(ensoniq, SERIAL));	/* clear serial interface */
18878c2ecf20Sopenharmony_ci#else
18888c2ecf20Sopenharmony_ci	outl(0, ES_REG(ensoniq, CONTROL));	/* switch everything off */
18898c2ecf20Sopenharmony_ci	outl(0, ES_REG(ensoniq, SERIAL));	/* clear serial interface */
18908c2ecf20Sopenharmony_ci#endif
18918c2ecf20Sopenharmony_ci	pci_set_power_state(ensoniq->pci, PCI_D3hot);
18928c2ecf20Sopenharmony_ci      __hw_end:
18938c2ecf20Sopenharmony_ci#ifdef CHIP1370
18948c2ecf20Sopenharmony_ci	if (ensoniq->dma_bug.area)
18958c2ecf20Sopenharmony_ci		snd_dma_free_pages(&ensoniq->dma_bug);
18968c2ecf20Sopenharmony_ci#endif
18978c2ecf20Sopenharmony_ci	if (ensoniq->irq >= 0)
18988c2ecf20Sopenharmony_ci		free_irq(ensoniq->irq, ensoniq);
18998c2ecf20Sopenharmony_ci	pci_release_regions(ensoniq->pci);
19008c2ecf20Sopenharmony_ci	pci_disable_device(ensoniq->pci);
19018c2ecf20Sopenharmony_ci	kfree(ensoniq);
19028c2ecf20Sopenharmony_ci	return 0;
19038c2ecf20Sopenharmony_ci}
19048c2ecf20Sopenharmony_ci
19058c2ecf20Sopenharmony_cistatic int snd_ensoniq_dev_free(struct snd_device *device)
19068c2ecf20Sopenharmony_ci{
19078c2ecf20Sopenharmony_ci	struct ensoniq *ensoniq = device->device_data;
19088c2ecf20Sopenharmony_ci	return snd_ensoniq_free(ensoniq);
19098c2ecf20Sopenharmony_ci}
19108c2ecf20Sopenharmony_ci
19118c2ecf20Sopenharmony_ci#ifdef CHIP1371
19128c2ecf20Sopenharmony_cistatic const struct snd_pci_quirk es1371_amplifier_hack[] = {
19138c2ecf20Sopenharmony_ci	SND_PCI_QUIRK_ID(0x107b, 0x2150),	/* Gateway Solo 2150 */
19148c2ecf20Sopenharmony_ci	SND_PCI_QUIRK_ID(0x13bd, 0x100c),	/* EV1938 on Mebius PC-MJ100V */
19158c2ecf20Sopenharmony_ci	SND_PCI_QUIRK_ID(0x1102, 0x5938),	/* Targa Xtender300 */
19168c2ecf20Sopenharmony_ci	SND_PCI_QUIRK_ID(0x1102, 0x8938),	/* IPC Topnote G notebook */
19178c2ecf20Sopenharmony_ci	{ } /* end */
19188c2ecf20Sopenharmony_ci};
19198c2ecf20Sopenharmony_ci
19208c2ecf20Sopenharmony_cistatic const struct es1371_quirk es1371_ac97_reset_hack[] = {
19218c2ecf20Sopenharmony_ci	{ .vid = PCI_VENDOR_ID_ENSONIQ, .did = PCI_DEVICE_ID_ENSONIQ_CT5880, .rev = CT5880REV_CT5880_C },
19228c2ecf20Sopenharmony_ci	{ .vid = PCI_VENDOR_ID_ENSONIQ, .did = PCI_DEVICE_ID_ENSONIQ_CT5880, .rev = CT5880REV_CT5880_D },
19238c2ecf20Sopenharmony_ci	{ .vid = PCI_VENDOR_ID_ENSONIQ, .did = PCI_DEVICE_ID_ENSONIQ_CT5880, .rev = CT5880REV_CT5880_E },
19248c2ecf20Sopenharmony_ci	{ .vid = PCI_VENDOR_ID_ENSONIQ, .did = PCI_DEVICE_ID_ENSONIQ_ES1371, .rev = ES1371REV_CT5880_A },
19258c2ecf20Sopenharmony_ci	{ .vid = PCI_VENDOR_ID_ENSONIQ, .did = PCI_DEVICE_ID_ENSONIQ_ES1371, .rev = ES1371REV_ES1373_8 },
19268c2ecf20Sopenharmony_ci	{ .vid = PCI_ANY_ID, .did = PCI_ANY_ID }
19278c2ecf20Sopenharmony_ci};
19288c2ecf20Sopenharmony_ci#endif
19298c2ecf20Sopenharmony_ci
19308c2ecf20Sopenharmony_cistatic void snd_ensoniq_chip_init(struct ensoniq *ensoniq)
19318c2ecf20Sopenharmony_ci{
19328c2ecf20Sopenharmony_ci#ifdef CHIP1371
19338c2ecf20Sopenharmony_ci	int idx;
19348c2ecf20Sopenharmony_ci#endif
19358c2ecf20Sopenharmony_ci	/* this code was part of snd_ensoniq_create before intruduction
19368c2ecf20Sopenharmony_ci	  * of suspend/resume
19378c2ecf20Sopenharmony_ci	  */
19388c2ecf20Sopenharmony_ci#ifdef CHIP1370
19398c2ecf20Sopenharmony_ci	outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
19408c2ecf20Sopenharmony_ci	outl(ensoniq->sctrl, ES_REG(ensoniq, SERIAL));
19418c2ecf20Sopenharmony_ci	outl(ES_MEM_PAGEO(ES_PAGE_ADC), ES_REG(ensoniq, MEM_PAGE));
19428c2ecf20Sopenharmony_ci	outl(ensoniq->dma_bug.addr, ES_REG(ensoniq, PHANTOM_FRAME));
19438c2ecf20Sopenharmony_ci	outl(0, ES_REG(ensoniq, PHANTOM_COUNT));
19448c2ecf20Sopenharmony_ci#else
19458c2ecf20Sopenharmony_ci	outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
19468c2ecf20Sopenharmony_ci	outl(ensoniq->sctrl, ES_REG(ensoniq, SERIAL));
19478c2ecf20Sopenharmony_ci	outl(0, ES_REG(ensoniq, 1371_LEGACY));
19488c2ecf20Sopenharmony_ci	if (es1371_quirk_lookup(ensoniq, es1371_ac97_reset_hack)) {
19498c2ecf20Sopenharmony_ci	    outl(ensoniq->cssr, ES_REG(ensoniq, STATUS));
19508c2ecf20Sopenharmony_ci	    /* need to delay around 20ms(bleech) to give
19518c2ecf20Sopenharmony_ci	       some CODECs enough time to wakeup */
19528c2ecf20Sopenharmony_ci	    msleep(20);
19538c2ecf20Sopenharmony_ci	}
19548c2ecf20Sopenharmony_ci	/* AC'97 warm reset to start the bitclk */
19558c2ecf20Sopenharmony_ci	outl(ensoniq->ctrl | ES_1371_SYNC_RES, ES_REG(ensoniq, CONTROL));
19568c2ecf20Sopenharmony_ci	inl(ES_REG(ensoniq, CONTROL));
19578c2ecf20Sopenharmony_ci	udelay(20);
19588c2ecf20Sopenharmony_ci	outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
19598c2ecf20Sopenharmony_ci	/* Init the sample rate converter */
19608c2ecf20Sopenharmony_ci	snd_es1371_wait_src_ready(ensoniq);
19618c2ecf20Sopenharmony_ci	outl(ES_1371_SRC_DISABLE, ES_REG(ensoniq, 1371_SMPRATE));
19628c2ecf20Sopenharmony_ci	for (idx = 0; idx < 0x80; idx++)
19638c2ecf20Sopenharmony_ci		snd_es1371_src_write(ensoniq, idx, 0);
19648c2ecf20Sopenharmony_ci	snd_es1371_src_write(ensoniq, ES_SMPREG_DAC1 + ES_SMPREG_TRUNC_N, 16 << 4);
19658c2ecf20Sopenharmony_ci	snd_es1371_src_write(ensoniq, ES_SMPREG_DAC1 + ES_SMPREG_INT_REGS, 16 << 10);
19668c2ecf20Sopenharmony_ci	snd_es1371_src_write(ensoniq, ES_SMPREG_DAC2 + ES_SMPREG_TRUNC_N, 16 << 4);
19678c2ecf20Sopenharmony_ci	snd_es1371_src_write(ensoniq, ES_SMPREG_DAC2 + ES_SMPREG_INT_REGS, 16 << 10);
19688c2ecf20Sopenharmony_ci	snd_es1371_src_write(ensoniq, ES_SMPREG_VOL_ADC, 1 << 12);
19698c2ecf20Sopenharmony_ci	snd_es1371_src_write(ensoniq, ES_SMPREG_VOL_ADC + 1, 1 << 12);
19708c2ecf20Sopenharmony_ci	snd_es1371_src_write(ensoniq, ES_SMPREG_VOL_DAC1, 1 << 12);
19718c2ecf20Sopenharmony_ci	snd_es1371_src_write(ensoniq, ES_SMPREG_VOL_DAC1 + 1, 1 << 12);
19728c2ecf20Sopenharmony_ci	snd_es1371_src_write(ensoniq, ES_SMPREG_VOL_DAC2, 1 << 12);
19738c2ecf20Sopenharmony_ci	snd_es1371_src_write(ensoniq, ES_SMPREG_VOL_DAC2 + 1, 1 << 12);
19748c2ecf20Sopenharmony_ci	snd_es1371_adc_rate(ensoniq, 22050);
19758c2ecf20Sopenharmony_ci	snd_es1371_dac1_rate(ensoniq, 22050);
19768c2ecf20Sopenharmony_ci	snd_es1371_dac2_rate(ensoniq, 22050);
19778c2ecf20Sopenharmony_ci	/* WARNING:
19788c2ecf20Sopenharmony_ci	 * enabling the sample rate converter without properly programming
19798c2ecf20Sopenharmony_ci	 * its parameters causes the chip to lock up (the SRC busy bit will
19808c2ecf20Sopenharmony_ci	 * be stuck high, and I've found no way to rectify this other than
19818c2ecf20Sopenharmony_ci	 * power cycle) - Thomas Sailer
19828c2ecf20Sopenharmony_ci	 */
19838c2ecf20Sopenharmony_ci	snd_es1371_wait_src_ready(ensoniq);
19848c2ecf20Sopenharmony_ci	outl(0, ES_REG(ensoniq, 1371_SMPRATE));
19858c2ecf20Sopenharmony_ci	/* try reset codec directly */
19868c2ecf20Sopenharmony_ci	outl(ES_1371_CODEC_WRITE(0, 0), ES_REG(ensoniq, 1371_CODEC));
19878c2ecf20Sopenharmony_ci#endif
19888c2ecf20Sopenharmony_ci	outb(ensoniq->uartc = 0x00, ES_REG(ensoniq, UART_CONTROL));
19898c2ecf20Sopenharmony_ci	outb(0x00, ES_REG(ensoniq, UART_RES));
19908c2ecf20Sopenharmony_ci	outl(ensoniq->cssr, ES_REG(ensoniq, STATUS));
19918c2ecf20Sopenharmony_ci}
19928c2ecf20Sopenharmony_ci
19938c2ecf20Sopenharmony_ci#ifdef CONFIG_PM_SLEEP
19948c2ecf20Sopenharmony_cistatic int snd_ensoniq_suspend(struct device *dev)
19958c2ecf20Sopenharmony_ci{
19968c2ecf20Sopenharmony_ci	struct snd_card *card = dev_get_drvdata(dev);
19978c2ecf20Sopenharmony_ci	struct ensoniq *ensoniq = card->private_data;
19988c2ecf20Sopenharmony_ci
19998c2ecf20Sopenharmony_ci	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
20008c2ecf20Sopenharmony_ci
20018c2ecf20Sopenharmony_ci#ifdef CHIP1371
20028c2ecf20Sopenharmony_ci	snd_ac97_suspend(ensoniq->u.es1371.ac97);
20038c2ecf20Sopenharmony_ci#else
20048c2ecf20Sopenharmony_ci	/* try to reset AK4531 */
20058c2ecf20Sopenharmony_ci	outw(ES_1370_CODEC_WRITE(AK4531_RESET, 0x02), ES_REG(ensoniq, 1370_CODEC));
20068c2ecf20Sopenharmony_ci	inw(ES_REG(ensoniq, 1370_CODEC));
20078c2ecf20Sopenharmony_ci	udelay(100);
20088c2ecf20Sopenharmony_ci	outw(ES_1370_CODEC_WRITE(AK4531_RESET, 0x03), ES_REG(ensoniq, 1370_CODEC));
20098c2ecf20Sopenharmony_ci	inw(ES_REG(ensoniq, 1370_CODEC));
20108c2ecf20Sopenharmony_ci	udelay(100);
20118c2ecf20Sopenharmony_ci	snd_ak4531_suspend(ensoniq->u.es1370.ak4531);
20128c2ecf20Sopenharmony_ci#endif
20138c2ecf20Sopenharmony_ci	return 0;
20148c2ecf20Sopenharmony_ci}
20158c2ecf20Sopenharmony_ci
20168c2ecf20Sopenharmony_cistatic int snd_ensoniq_resume(struct device *dev)
20178c2ecf20Sopenharmony_ci{
20188c2ecf20Sopenharmony_ci	struct snd_card *card = dev_get_drvdata(dev);
20198c2ecf20Sopenharmony_ci	struct ensoniq *ensoniq = card->private_data;
20208c2ecf20Sopenharmony_ci
20218c2ecf20Sopenharmony_ci	snd_ensoniq_chip_init(ensoniq);
20228c2ecf20Sopenharmony_ci
20238c2ecf20Sopenharmony_ci#ifdef CHIP1371
20248c2ecf20Sopenharmony_ci	snd_ac97_resume(ensoniq->u.es1371.ac97);
20258c2ecf20Sopenharmony_ci#else
20268c2ecf20Sopenharmony_ci	snd_ak4531_resume(ensoniq->u.es1370.ak4531);
20278c2ecf20Sopenharmony_ci#endif
20288c2ecf20Sopenharmony_ci	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
20298c2ecf20Sopenharmony_ci	return 0;
20308c2ecf20Sopenharmony_ci}
20318c2ecf20Sopenharmony_ci
20328c2ecf20Sopenharmony_cistatic SIMPLE_DEV_PM_OPS(snd_ensoniq_pm, snd_ensoniq_suspend, snd_ensoniq_resume);
20338c2ecf20Sopenharmony_ci#define SND_ENSONIQ_PM_OPS	&snd_ensoniq_pm
20348c2ecf20Sopenharmony_ci#else
20358c2ecf20Sopenharmony_ci#define SND_ENSONIQ_PM_OPS	NULL
20368c2ecf20Sopenharmony_ci#endif /* CONFIG_PM_SLEEP */
20378c2ecf20Sopenharmony_ci
20388c2ecf20Sopenharmony_cistatic int snd_ensoniq_create(struct snd_card *card,
20398c2ecf20Sopenharmony_ci			      struct pci_dev *pci,
20408c2ecf20Sopenharmony_ci			      struct ensoniq **rensoniq)
20418c2ecf20Sopenharmony_ci{
20428c2ecf20Sopenharmony_ci	struct ensoniq *ensoniq;
20438c2ecf20Sopenharmony_ci	int err;
20448c2ecf20Sopenharmony_ci	static const struct snd_device_ops ops = {
20458c2ecf20Sopenharmony_ci		.dev_free =	snd_ensoniq_dev_free,
20468c2ecf20Sopenharmony_ci	};
20478c2ecf20Sopenharmony_ci
20488c2ecf20Sopenharmony_ci	*rensoniq = NULL;
20498c2ecf20Sopenharmony_ci	if ((err = pci_enable_device(pci)) < 0)
20508c2ecf20Sopenharmony_ci		return err;
20518c2ecf20Sopenharmony_ci	ensoniq = kzalloc(sizeof(*ensoniq), GFP_KERNEL);
20528c2ecf20Sopenharmony_ci	if (ensoniq == NULL) {
20538c2ecf20Sopenharmony_ci		pci_disable_device(pci);
20548c2ecf20Sopenharmony_ci		return -ENOMEM;
20558c2ecf20Sopenharmony_ci	}
20568c2ecf20Sopenharmony_ci	spin_lock_init(&ensoniq->reg_lock);
20578c2ecf20Sopenharmony_ci	mutex_init(&ensoniq->src_mutex);
20588c2ecf20Sopenharmony_ci	ensoniq->card = card;
20598c2ecf20Sopenharmony_ci	ensoniq->pci = pci;
20608c2ecf20Sopenharmony_ci	ensoniq->irq = -1;
20618c2ecf20Sopenharmony_ci	if ((err = pci_request_regions(pci, "Ensoniq AudioPCI")) < 0) {
20628c2ecf20Sopenharmony_ci		kfree(ensoniq);
20638c2ecf20Sopenharmony_ci		pci_disable_device(pci);
20648c2ecf20Sopenharmony_ci		return err;
20658c2ecf20Sopenharmony_ci	}
20668c2ecf20Sopenharmony_ci	ensoniq->port = pci_resource_start(pci, 0);
20678c2ecf20Sopenharmony_ci	if (request_irq(pci->irq, snd_audiopci_interrupt, IRQF_SHARED,
20688c2ecf20Sopenharmony_ci			KBUILD_MODNAME, ensoniq)) {
20698c2ecf20Sopenharmony_ci		dev_err(card->dev, "unable to grab IRQ %d\n", pci->irq);
20708c2ecf20Sopenharmony_ci		snd_ensoniq_free(ensoniq);
20718c2ecf20Sopenharmony_ci		return -EBUSY;
20728c2ecf20Sopenharmony_ci	}
20738c2ecf20Sopenharmony_ci	ensoniq->irq = pci->irq;
20748c2ecf20Sopenharmony_ci	card->sync_irq = ensoniq->irq;
20758c2ecf20Sopenharmony_ci#ifdef CHIP1370
20768c2ecf20Sopenharmony_ci	if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, &pci->dev,
20778c2ecf20Sopenharmony_ci				16, &ensoniq->dma_bug) < 0) {
20788c2ecf20Sopenharmony_ci		dev_err(card->dev, "unable to allocate space for phantom area - dma_bug\n");
20798c2ecf20Sopenharmony_ci		snd_ensoniq_free(ensoniq);
20808c2ecf20Sopenharmony_ci		return -EBUSY;
20818c2ecf20Sopenharmony_ci	}
20828c2ecf20Sopenharmony_ci#endif
20838c2ecf20Sopenharmony_ci	pci_set_master(pci);
20848c2ecf20Sopenharmony_ci	ensoniq->rev = pci->revision;
20858c2ecf20Sopenharmony_ci#ifdef CHIP1370
20868c2ecf20Sopenharmony_ci#if 0
20878c2ecf20Sopenharmony_ci	ensoniq->ctrl = ES_1370_CDC_EN | ES_1370_SERR_DISABLE |
20888c2ecf20Sopenharmony_ci		ES_1370_PCLKDIVO(ES_1370_SRTODIV(8000));
20898c2ecf20Sopenharmony_ci#else	/* get microphone working */
20908c2ecf20Sopenharmony_ci	ensoniq->ctrl = ES_1370_CDC_EN | ES_1370_PCLKDIVO(ES_1370_SRTODIV(8000));
20918c2ecf20Sopenharmony_ci#endif
20928c2ecf20Sopenharmony_ci	ensoniq->sctrl = 0;
20938c2ecf20Sopenharmony_ci#else
20948c2ecf20Sopenharmony_ci	ensoniq->ctrl = 0;
20958c2ecf20Sopenharmony_ci	ensoniq->sctrl = 0;
20968c2ecf20Sopenharmony_ci	ensoniq->cssr = 0;
20978c2ecf20Sopenharmony_ci	if (snd_pci_quirk_lookup(pci, es1371_amplifier_hack))
20988c2ecf20Sopenharmony_ci		ensoniq->ctrl |= ES_1371_GPIO_OUT(1);	/* turn amplifier on */
20998c2ecf20Sopenharmony_ci
21008c2ecf20Sopenharmony_ci	if (es1371_quirk_lookup(ensoniq, es1371_ac97_reset_hack))
21018c2ecf20Sopenharmony_ci		ensoniq->cssr |= ES_1371_ST_AC97_RST;
21028c2ecf20Sopenharmony_ci#endif
21038c2ecf20Sopenharmony_ci
21048c2ecf20Sopenharmony_ci	snd_ensoniq_chip_init(ensoniq);
21058c2ecf20Sopenharmony_ci
21068c2ecf20Sopenharmony_ci	if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, ensoniq, &ops)) < 0) {
21078c2ecf20Sopenharmony_ci		snd_ensoniq_free(ensoniq);
21088c2ecf20Sopenharmony_ci		return err;
21098c2ecf20Sopenharmony_ci	}
21108c2ecf20Sopenharmony_ci
21118c2ecf20Sopenharmony_ci	snd_ensoniq_proc_init(ensoniq);
21128c2ecf20Sopenharmony_ci
21138c2ecf20Sopenharmony_ci	*rensoniq = ensoniq;
21148c2ecf20Sopenharmony_ci	return 0;
21158c2ecf20Sopenharmony_ci}
21168c2ecf20Sopenharmony_ci
21178c2ecf20Sopenharmony_ci/*
21188c2ecf20Sopenharmony_ci *  MIDI section
21198c2ecf20Sopenharmony_ci */
21208c2ecf20Sopenharmony_ci
21218c2ecf20Sopenharmony_cistatic void snd_ensoniq_midi_interrupt(struct ensoniq * ensoniq)
21228c2ecf20Sopenharmony_ci{
21238c2ecf20Sopenharmony_ci	struct snd_rawmidi *rmidi = ensoniq->rmidi;
21248c2ecf20Sopenharmony_ci	unsigned char status, mask, byte;
21258c2ecf20Sopenharmony_ci
21268c2ecf20Sopenharmony_ci	if (rmidi == NULL)
21278c2ecf20Sopenharmony_ci		return;
21288c2ecf20Sopenharmony_ci	/* do Rx at first */
21298c2ecf20Sopenharmony_ci	spin_lock(&ensoniq->reg_lock);
21308c2ecf20Sopenharmony_ci	mask = ensoniq->uartm & ES_MODE_INPUT ? ES_RXRDY : 0;
21318c2ecf20Sopenharmony_ci	while (mask) {
21328c2ecf20Sopenharmony_ci		status = inb(ES_REG(ensoniq, UART_STATUS));
21338c2ecf20Sopenharmony_ci		if ((status & mask) == 0)
21348c2ecf20Sopenharmony_ci			break;
21358c2ecf20Sopenharmony_ci		byte = inb(ES_REG(ensoniq, UART_DATA));
21368c2ecf20Sopenharmony_ci		snd_rawmidi_receive(ensoniq->midi_input, &byte, 1);
21378c2ecf20Sopenharmony_ci	}
21388c2ecf20Sopenharmony_ci	spin_unlock(&ensoniq->reg_lock);
21398c2ecf20Sopenharmony_ci
21408c2ecf20Sopenharmony_ci	/* do Tx at second */
21418c2ecf20Sopenharmony_ci	spin_lock(&ensoniq->reg_lock);
21428c2ecf20Sopenharmony_ci	mask = ensoniq->uartm & ES_MODE_OUTPUT ? ES_TXRDY : 0;
21438c2ecf20Sopenharmony_ci	while (mask) {
21448c2ecf20Sopenharmony_ci		status = inb(ES_REG(ensoniq, UART_STATUS));
21458c2ecf20Sopenharmony_ci		if ((status & mask) == 0)
21468c2ecf20Sopenharmony_ci			break;
21478c2ecf20Sopenharmony_ci		if (snd_rawmidi_transmit(ensoniq->midi_output, &byte, 1) != 1) {
21488c2ecf20Sopenharmony_ci			ensoniq->uartc &= ~ES_TXINTENM;
21498c2ecf20Sopenharmony_ci			outb(ensoniq->uartc, ES_REG(ensoniq, UART_CONTROL));
21508c2ecf20Sopenharmony_ci			mask &= ~ES_TXRDY;
21518c2ecf20Sopenharmony_ci		} else {
21528c2ecf20Sopenharmony_ci			outb(byte, ES_REG(ensoniq, UART_DATA));
21538c2ecf20Sopenharmony_ci		}
21548c2ecf20Sopenharmony_ci	}
21558c2ecf20Sopenharmony_ci	spin_unlock(&ensoniq->reg_lock);
21568c2ecf20Sopenharmony_ci}
21578c2ecf20Sopenharmony_ci
21588c2ecf20Sopenharmony_cistatic int snd_ensoniq_midi_input_open(struct snd_rawmidi_substream *substream)
21598c2ecf20Sopenharmony_ci{
21608c2ecf20Sopenharmony_ci	struct ensoniq *ensoniq = substream->rmidi->private_data;
21618c2ecf20Sopenharmony_ci
21628c2ecf20Sopenharmony_ci	spin_lock_irq(&ensoniq->reg_lock);
21638c2ecf20Sopenharmony_ci	ensoniq->uartm |= ES_MODE_INPUT;
21648c2ecf20Sopenharmony_ci	ensoniq->midi_input = substream;
21658c2ecf20Sopenharmony_ci	if (!(ensoniq->uartm & ES_MODE_OUTPUT)) {
21668c2ecf20Sopenharmony_ci		outb(ES_CNTRL(3), ES_REG(ensoniq, UART_CONTROL));
21678c2ecf20Sopenharmony_ci		outb(ensoniq->uartc = 0, ES_REG(ensoniq, UART_CONTROL));
21688c2ecf20Sopenharmony_ci		outl(ensoniq->ctrl |= ES_UART_EN, ES_REG(ensoniq, CONTROL));
21698c2ecf20Sopenharmony_ci	}
21708c2ecf20Sopenharmony_ci	spin_unlock_irq(&ensoniq->reg_lock);
21718c2ecf20Sopenharmony_ci	return 0;
21728c2ecf20Sopenharmony_ci}
21738c2ecf20Sopenharmony_ci
21748c2ecf20Sopenharmony_cistatic int snd_ensoniq_midi_input_close(struct snd_rawmidi_substream *substream)
21758c2ecf20Sopenharmony_ci{
21768c2ecf20Sopenharmony_ci	struct ensoniq *ensoniq = substream->rmidi->private_data;
21778c2ecf20Sopenharmony_ci
21788c2ecf20Sopenharmony_ci	spin_lock_irq(&ensoniq->reg_lock);
21798c2ecf20Sopenharmony_ci	if (!(ensoniq->uartm & ES_MODE_OUTPUT)) {
21808c2ecf20Sopenharmony_ci		outb(ensoniq->uartc = 0, ES_REG(ensoniq, UART_CONTROL));
21818c2ecf20Sopenharmony_ci		outl(ensoniq->ctrl &= ~ES_UART_EN, ES_REG(ensoniq, CONTROL));
21828c2ecf20Sopenharmony_ci	} else {
21838c2ecf20Sopenharmony_ci		outb(ensoniq->uartc &= ~ES_RXINTEN, ES_REG(ensoniq, UART_CONTROL));
21848c2ecf20Sopenharmony_ci	}
21858c2ecf20Sopenharmony_ci	ensoniq->midi_input = NULL;
21868c2ecf20Sopenharmony_ci	ensoniq->uartm &= ~ES_MODE_INPUT;
21878c2ecf20Sopenharmony_ci	spin_unlock_irq(&ensoniq->reg_lock);
21888c2ecf20Sopenharmony_ci	return 0;
21898c2ecf20Sopenharmony_ci}
21908c2ecf20Sopenharmony_ci
21918c2ecf20Sopenharmony_cistatic int snd_ensoniq_midi_output_open(struct snd_rawmidi_substream *substream)
21928c2ecf20Sopenharmony_ci{
21938c2ecf20Sopenharmony_ci	struct ensoniq *ensoniq = substream->rmidi->private_data;
21948c2ecf20Sopenharmony_ci
21958c2ecf20Sopenharmony_ci	spin_lock_irq(&ensoniq->reg_lock);
21968c2ecf20Sopenharmony_ci	ensoniq->uartm |= ES_MODE_OUTPUT;
21978c2ecf20Sopenharmony_ci	ensoniq->midi_output = substream;
21988c2ecf20Sopenharmony_ci	if (!(ensoniq->uartm & ES_MODE_INPUT)) {
21998c2ecf20Sopenharmony_ci		outb(ES_CNTRL(3), ES_REG(ensoniq, UART_CONTROL));
22008c2ecf20Sopenharmony_ci		outb(ensoniq->uartc = 0, ES_REG(ensoniq, UART_CONTROL));
22018c2ecf20Sopenharmony_ci		outl(ensoniq->ctrl |= ES_UART_EN, ES_REG(ensoniq, CONTROL));
22028c2ecf20Sopenharmony_ci	}
22038c2ecf20Sopenharmony_ci	spin_unlock_irq(&ensoniq->reg_lock);
22048c2ecf20Sopenharmony_ci	return 0;
22058c2ecf20Sopenharmony_ci}
22068c2ecf20Sopenharmony_ci
22078c2ecf20Sopenharmony_cistatic int snd_ensoniq_midi_output_close(struct snd_rawmidi_substream *substream)
22088c2ecf20Sopenharmony_ci{
22098c2ecf20Sopenharmony_ci	struct ensoniq *ensoniq = substream->rmidi->private_data;
22108c2ecf20Sopenharmony_ci
22118c2ecf20Sopenharmony_ci	spin_lock_irq(&ensoniq->reg_lock);
22128c2ecf20Sopenharmony_ci	if (!(ensoniq->uartm & ES_MODE_INPUT)) {
22138c2ecf20Sopenharmony_ci		outb(ensoniq->uartc = 0, ES_REG(ensoniq, UART_CONTROL));
22148c2ecf20Sopenharmony_ci		outl(ensoniq->ctrl &= ~ES_UART_EN, ES_REG(ensoniq, CONTROL));
22158c2ecf20Sopenharmony_ci	} else {
22168c2ecf20Sopenharmony_ci		outb(ensoniq->uartc &= ~ES_TXINTENM, ES_REG(ensoniq, UART_CONTROL));
22178c2ecf20Sopenharmony_ci	}
22188c2ecf20Sopenharmony_ci	ensoniq->midi_output = NULL;
22198c2ecf20Sopenharmony_ci	ensoniq->uartm &= ~ES_MODE_OUTPUT;
22208c2ecf20Sopenharmony_ci	spin_unlock_irq(&ensoniq->reg_lock);
22218c2ecf20Sopenharmony_ci	return 0;
22228c2ecf20Sopenharmony_ci}
22238c2ecf20Sopenharmony_ci
22248c2ecf20Sopenharmony_cistatic void snd_ensoniq_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
22258c2ecf20Sopenharmony_ci{
22268c2ecf20Sopenharmony_ci	unsigned long flags;
22278c2ecf20Sopenharmony_ci	struct ensoniq *ensoniq = substream->rmidi->private_data;
22288c2ecf20Sopenharmony_ci	int idx;
22298c2ecf20Sopenharmony_ci
22308c2ecf20Sopenharmony_ci	spin_lock_irqsave(&ensoniq->reg_lock, flags);
22318c2ecf20Sopenharmony_ci	if (up) {
22328c2ecf20Sopenharmony_ci		if ((ensoniq->uartc & ES_RXINTEN) == 0) {
22338c2ecf20Sopenharmony_ci			/* empty input FIFO */
22348c2ecf20Sopenharmony_ci			for (idx = 0; idx < 32; idx++)
22358c2ecf20Sopenharmony_ci				inb(ES_REG(ensoniq, UART_DATA));
22368c2ecf20Sopenharmony_ci			ensoniq->uartc |= ES_RXINTEN;
22378c2ecf20Sopenharmony_ci			outb(ensoniq->uartc, ES_REG(ensoniq, UART_CONTROL));
22388c2ecf20Sopenharmony_ci		}
22398c2ecf20Sopenharmony_ci	} else {
22408c2ecf20Sopenharmony_ci		if (ensoniq->uartc & ES_RXINTEN) {
22418c2ecf20Sopenharmony_ci			ensoniq->uartc &= ~ES_RXINTEN;
22428c2ecf20Sopenharmony_ci			outb(ensoniq->uartc, ES_REG(ensoniq, UART_CONTROL));
22438c2ecf20Sopenharmony_ci		}
22448c2ecf20Sopenharmony_ci	}
22458c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&ensoniq->reg_lock, flags);
22468c2ecf20Sopenharmony_ci}
22478c2ecf20Sopenharmony_ci
22488c2ecf20Sopenharmony_cistatic void snd_ensoniq_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
22498c2ecf20Sopenharmony_ci{
22508c2ecf20Sopenharmony_ci	unsigned long flags;
22518c2ecf20Sopenharmony_ci	struct ensoniq *ensoniq = substream->rmidi->private_data;
22528c2ecf20Sopenharmony_ci	unsigned char byte;
22538c2ecf20Sopenharmony_ci
22548c2ecf20Sopenharmony_ci	spin_lock_irqsave(&ensoniq->reg_lock, flags);
22558c2ecf20Sopenharmony_ci	if (up) {
22568c2ecf20Sopenharmony_ci		if (ES_TXINTENI(ensoniq->uartc) == 0) {
22578c2ecf20Sopenharmony_ci			ensoniq->uartc |= ES_TXINTENO(1);
22588c2ecf20Sopenharmony_ci			/* fill UART FIFO buffer at first, and turn Tx interrupts only if necessary */
22598c2ecf20Sopenharmony_ci			while (ES_TXINTENI(ensoniq->uartc) == 1 &&
22608c2ecf20Sopenharmony_ci			       (inb(ES_REG(ensoniq, UART_STATUS)) & ES_TXRDY)) {
22618c2ecf20Sopenharmony_ci				if (snd_rawmidi_transmit(substream, &byte, 1) != 1) {
22628c2ecf20Sopenharmony_ci					ensoniq->uartc &= ~ES_TXINTENM;
22638c2ecf20Sopenharmony_ci				} else {
22648c2ecf20Sopenharmony_ci					outb(byte, ES_REG(ensoniq, UART_DATA));
22658c2ecf20Sopenharmony_ci				}
22668c2ecf20Sopenharmony_ci			}
22678c2ecf20Sopenharmony_ci			outb(ensoniq->uartc, ES_REG(ensoniq, UART_CONTROL));
22688c2ecf20Sopenharmony_ci		}
22698c2ecf20Sopenharmony_ci	} else {
22708c2ecf20Sopenharmony_ci		if (ES_TXINTENI(ensoniq->uartc) == 1) {
22718c2ecf20Sopenharmony_ci			ensoniq->uartc &= ~ES_TXINTENM;
22728c2ecf20Sopenharmony_ci			outb(ensoniq->uartc, ES_REG(ensoniq, UART_CONTROL));
22738c2ecf20Sopenharmony_ci		}
22748c2ecf20Sopenharmony_ci	}
22758c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&ensoniq->reg_lock, flags);
22768c2ecf20Sopenharmony_ci}
22778c2ecf20Sopenharmony_ci
22788c2ecf20Sopenharmony_cistatic const struct snd_rawmidi_ops snd_ensoniq_midi_output =
22798c2ecf20Sopenharmony_ci{
22808c2ecf20Sopenharmony_ci	.open =		snd_ensoniq_midi_output_open,
22818c2ecf20Sopenharmony_ci	.close =	snd_ensoniq_midi_output_close,
22828c2ecf20Sopenharmony_ci	.trigger =	snd_ensoniq_midi_output_trigger,
22838c2ecf20Sopenharmony_ci};
22848c2ecf20Sopenharmony_ci
22858c2ecf20Sopenharmony_cistatic const struct snd_rawmidi_ops snd_ensoniq_midi_input =
22868c2ecf20Sopenharmony_ci{
22878c2ecf20Sopenharmony_ci	.open =		snd_ensoniq_midi_input_open,
22888c2ecf20Sopenharmony_ci	.close =	snd_ensoniq_midi_input_close,
22898c2ecf20Sopenharmony_ci	.trigger =	snd_ensoniq_midi_input_trigger,
22908c2ecf20Sopenharmony_ci};
22918c2ecf20Sopenharmony_ci
22928c2ecf20Sopenharmony_cistatic int snd_ensoniq_midi(struct ensoniq *ensoniq, int device)
22938c2ecf20Sopenharmony_ci{
22948c2ecf20Sopenharmony_ci	struct snd_rawmidi *rmidi;
22958c2ecf20Sopenharmony_ci	int err;
22968c2ecf20Sopenharmony_ci
22978c2ecf20Sopenharmony_ci	if ((err = snd_rawmidi_new(ensoniq->card, "ES1370/1", device, 1, 1, &rmidi)) < 0)
22988c2ecf20Sopenharmony_ci		return err;
22998c2ecf20Sopenharmony_ci	strcpy(rmidi->name, CHIP_NAME);
23008c2ecf20Sopenharmony_ci	snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_ensoniq_midi_output);
23018c2ecf20Sopenharmony_ci	snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_ensoniq_midi_input);
23028c2ecf20Sopenharmony_ci	rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT | SNDRV_RAWMIDI_INFO_INPUT |
23038c2ecf20Sopenharmony_ci		SNDRV_RAWMIDI_INFO_DUPLEX;
23048c2ecf20Sopenharmony_ci	rmidi->private_data = ensoniq;
23058c2ecf20Sopenharmony_ci	ensoniq->rmidi = rmidi;
23068c2ecf20Sopenharmony_ci	return 0;
23078c2ecf20Sopenharmony_ci}
23088c2ecf20Sopenharmony_ci
23098c2ecf20Sopenharmony_ci/*
23108c2ecf20Sopenharmony_ci *  Interrupt handler
23118c2ecf20Sopenharmony_ci */
23128c2ecf20Sopenharmony_ci
23138c2ecf20Sopenharmony_cistatic irqreturn_t snd_audiopci_interrupt(int irq, void *dev_id)
23148c2ecf20Sopenharmony_ci{
23158c2ecf20Sopenharmony_ci	struct ensoniq *ensoniq = dev_id;
23168c2ecf20Sopenharmony_ci	unsigned int status, sctrl;
23178c2ecf20Sopenharmony_ci
23188c2ecf20Sopenharmony_ci	if (ensoniq == NULL)
23198c2ecf20Sopenharmony_ci		return IRQ_NONE;
23208c2ecf20Sopenharmony_ci
23218c2ecf20Sopenharmony_ci	status = inl(ES_REG(ensoniq, STATUS));
23228c2ecf20Sopenharmony_ci	if (!(status & ES_INTR))
23238c2ecf20Sopenharmony_ci		return IRQ_NONE;
23248c2ecf20Sopenharmony_ci
23258c2ecf20Sopenharmony_ci	spin_lock(&ensoniq->reg_lock);
23268c2ecf20Sopenharmony_ci	sctrl = ensoniq->sctrl;
23278c2ecf20Sopenharmony_ci	if (status & ES_DAC1)
23288c2ecf20Sopenharmony_ci		sctrl &= ~ES_P1_INT_EN;
23298c2ecf20Sopenharmony_ci	if (status & ES_DAC2)
23308c2ecf20Sopenharmony_ci		sctrl &= ~ES_P2_INT_EN;
23318c2ecf20Sopenharmony_ci	if (status & ES_ADC)
23328c2ecf20Sopenharmony_ci		sctrl &= ~ES_R1_INT_EN;
23338c2ecf20Sopenharmony_ci	outl(sctrl, ES_REG(ensoniq, SERIAL));
23348c2ecf20Sopenharmony_ci	outl(ensoniq->sctrl, ES_REG(ensoniq, SERIAL));
23358c2ecf20Sopenharmony_ci	spin_unlock(&ensoniq->reg_lock);
23368c2ecf20Sopenharmony_ci
23378c2ecf20Sopenharmony_ci	if (status & ES_UART)
23388c2ecf20Sopenharmony_ci		snd_ensoniq_midi_interrupt(ensoniq);
23398c2ecf20Sopenharmony_ci	if ((status & ES_DAC2) && ensoniq->playback2_substream)
23408c2ecf20Sopenharmony_ci		snd_pcm_period_elapsed(ensoniq->playback2_substream);
23418c2ecf20Sopenharmony_ci	if ((status & ES_ADC) && ensoniq->capture_substream)
23428c2ecf20Sopenharmony_ci		snd_pcm_period_elapsed(ensoniq->capture_substream);
23438c2ecf20Sopenharmony_ci	if ((status & ES_DAC1) && ensoniq->playback1_substream)
23448c2ecf20Sopenharmony_ci		snd_pcm_period_elapsed(ensoniq->playback1_substream);
23458c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
23468c2ecf20Sopenharmony_ci}
23478c2ecf20Sopenharmony_ci
23488c2ecf20Sopenharmony_cistatic int snd_audiopci_probe(struct pci_dev *pci,
23498c2ecf20Sopenharmony_ci			      const struct pci_device_id *pci_id)
23508c2ecf20Sopenharmony_ci{
23518c2ecf20Sopenharmony_ci	static int dev;
23528c2ecf20Sopenharmony_ci	struct snd_card *card;
23538c2ecf20Sopenharmony_ci	struct ensoniq *ensoniq;
23548c2ecf20Sopenharmony_ci	int err;
23558c2ecf20Sopenharmony_ci
23568c2ecf20Sopenharmony_ci	if (dev >= SNDRV_CARDS)
23578c2ecf20Sopenharmony_ci		return -ENODEV;
23588c2ecf20Sopenharmony_ci	if (!enable[dev]) {
23598c2ecf20Sopenharmony_ci		dev++;
23608c2ecf20Sopenharmony_ci		return -ENOENT;
23618c2ecf20Sopenharmony_ci	}
23628c2ecf20Sopenharmony_ci
23638c2ecf20Sopenharmony_ci	err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
23648c2ecf20Sopenharmony_ci			   0, &card);
23658c2ecf20Sopenharmony_ci	if (err < 0)
23668c2ecf20Sopenharmony_ci		return err;
23678c2ecf20Sopenharmony_ci
23688c2ecf20Sopenharmony_ci	if ((err = snd_ensoniq_create(card, pci, &ensoniq)) < 0) {
23698c2ecf20Sopenharmony_ci		snd_card_free(card);
23708c2ecf20Sopenharmony_ci		return err;
23718c2ecf20Sopenharmony_ci	}
23728c2ecf20Sopenharmony_ci	card->private_data = ensoniq;
23738c2ecf20Sopenharmony_ci
23748c2ecf20Sopenharmony_ci#ifdef CHIP1370
23758c2ecf20Sopenharmony_ci	if ((err = snd_ensoniq_1370_mixer(ensoniq)) < 0) {
23768c2ecf20Sopenharmony_ci		snd_card_free(card);
23778c2ecf20Sopenharmony_ci		return err;
23788c2ecf20Sopenharmony_ci	}
23798c2ecf20Sopenharmony_ci#endif
23808c2ecf20Sopenharmony_ci#ifdef CHIP1371
23818c2ecf20Sopenharmony_ci	if ((err = snd_ensoniq_1371_mixer(ensoniq, spdif[dev], lineio[dev])) < 0) {
23828c2ecf20Sopenharmony_ci		snd_card_free(card);
23838c2ecf20Sopenharmony_ci		return err;
23848c2ecf20Sopenharmony_ci	}
23858c2ecf20Sopenharmony_ci#endif
23868c2ecf20Sopenharmony_ci	if ((err = snd_ensoniq_pcm(ensoniq, 0)) < 0) {
23878c2ecf20Sopenharmony_ci		snd_card_free(card);
23888c2ecf20Sopenharmony_ci		return err;
23898c2ecf20Sopenharmony_ci	}
23908c2ecf20Sopenharmony_ci	if ((err = snd_ensoniq_pcm2(ensoniq, 1)) < 0) {
23918c2ecf20Sopenharmony_ci		snd_card_free(card);
23928c2ecf20Sopenharmony_ci		return err;
23938c2ecf20Sopenharmony_ci	}
23948c2ecf20Sopenharmony_ci	if ((err = snd_ensoniq_midi(ensoniq, 0)) < 0) {
23958c2ecf20Sopenharmony_ci		snd_card_free(card);
23968c2ecf20Sopenharmony_ci		return err;
23978c2ecf20Sopenharmony_ci	}
23988c2ecf20Sopenharmony_ci
23998c2ecf20Sopenharmony_ci	snd_ensoniq_create_gameport(ensoniq, dev);
24008c2ecf20Sopenharmony_ci
24018c2ecf20Sopenharmony_ci	strcpy(card->driver, DRIVER_NAME);
24028c2ecf20Sopenharmony_ci
24038c2ecf20Sopenharmony_ci	strcpy(card->shortname, "Ensoniq AudioPCI");
24048c2ecf20Sopenharmony_ci	sprintf(card->longname, "%s %s at 0x%lx, irq %i",
24058c2ecf20Sopenharmony_ci		card->shortname,
24068c2ecf20Sopenharmony_ci		card->driver,
24078c2ecf20Sopenharmony_ci		ensoniq->port,
24088c2ecf20Sopenharmony_ci		ensoniq->irq);
24098c2ecf20Sopenharmony_ci
24108c2ecf20Sopenharmony_ci	if ((err = snd_card_register(card)) < 0) {
24118c2ecf20Sopenharmony_ci		snd_card_free(card);
24128c2ecf20Sopenharmony_ci		return err;
24138c2ecf20Sopenharmony_ci	}
24148c2ecf20Sopenharmony_ci
24158c2ecf20Sopenharmony_ci	pci_set_drvdata(pci, card);
24168c2ecf20Sopenharmony_ci	dev++;
24178c2ecf20Sopenharmony_ci	return 0;
24188c2ecf20Sopenharmony_ci}
24198c2ecf20Sopenharmony_ci
24208c2ecf20Sopenharmony_cistatic void snd_audiopci_remove(struct pci_dev *pci)
24218c2ecf20Sopenharmony_ci{
24228c2ecf20Sopenharmony_ci	snd_card_free(pci_get_drvdata(pci));
24238c2ecf20Sopenharmony_ci}
24248c2ecf20Sopenharmony_ci
24258c2ecf20Sopenharmony_cistatic struct pci_driver ens137x_driver = {
24268c2ecf20Sopenharmony_ci	.name = KBUILD_MODNAME,
24278c2ecf20Sopenharmony_ci	.id_table = snd_audiopci_ids,
24288c2ecf20Sopenharmony_ci	.probe = snd_audiopci_probe,
24298c2ecf20Sopenharmony_ci	.remove = snd_audiopci_remove,
24308c2ecf20Sopenharmony_ci	.driver = {
24318c2ecf20Sopenharmony_ci		.pm = SND_ENSONIQ_PM_OPS,
24328c2ecf20Sopenharmony_ci	},
24338c2ecf20Sopenharmony_ci};
24348c2ecf20Sopenharmony_ci
24358c2ecf20Sopenharmony_cimodule_pci_driver(ens137x_driver);
2436