18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
48c2ecf20Sopenharmony_ci *  Routines for control of CS4231(A)/CS4232/InterWave & compatible chips
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci *  Bugs:
78c2ecf20Sopenharmony_ci *     - sometimes record brokes playback with WSS portion of
88c2ecf20Sopenharmony_ci *       Yamaha OPL3-SA3 chip
98c2ecf20Sopenharmony_ci *     - CS4231 (GUS MAX) - still trouble with occasional noises
108c2ecf20Sopenharmony_ci *			  - broken initialization?
118c2ecf20Sopenharmony_ci */
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include <linux/delay.h>
148c2ecf20Sopenharmony_ci#include <linux/pm.h>
158c2ecf20Sopenharmony_ci#include <linux/init.h>
168c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
178c2ecf20Sopenharmony_ci#include <linux/slab.h>
188c2ecf20Sopenharmony_ci#include <linux/ioport.h>
198c2ecf20Sopenharmony_ci#include <linux/module.h>
208c2ecf20Sopenharmony_ci#include <linux/io.h>
218c2ecf20Sopenharmony_ci#include <sound/core.h>
228c2ecf20Sopenharmony_ci#include <sound/wss.h>
238c2ecf20Sopenharmony_ci#include <sound/pcm_params.h>
248c2ecf20Sopenharmony_ci#include <sound/tlv.h>
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#include <asm/dma.h>
278c2ecf20Sopenharmony_ci#include <asm/irq.h>
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ciMODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
308c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Routines for control of CS4231(A)/CS4232/InterWave & compatible chips");
318c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci#if 0
348c2ecf20Sopenharmony_ci#define SNDRV_DEBUG_MCE
358c2ecf20Sopenharmony_ci#endif
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci/*
388c2ecf20Sopenharmony_ci *  Some variables
398c2ecf20Sopenharmony_ci */
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_cistatic const unsigned char freq_bits[14] = {
428c2ecf20Sopenharmony_ci	/* 5510 */	0x00 | CS4231_XTAL2,
438c2ecf20Sopenharmony_ci	/* 6620 */	0x0E | CS4231_XTAL2,
448c2ecf20Sopenharmony_ci	/* 8000 */	0x00 | CS4231_XTAL1,
458c2ecf20Sopenharmony_ci	/* 9600 */	0x0E | CS4231_XTAL1,
468c2ecf20Sopenharmony_ci	/* 11025 */	0x02 | CS4231_XTAL2,
478c2ecf20Sopenharmony_ci	/* 16000 */	0x02 | CS4231_XTAL1,
488c2ecf20Sopenharmony_ci	/* 18900 */	0x04 | CS4231_XTAL2,
498c2ecf20Sopenharmony_ci	/* 22050 */	0x06 | CS4231_XTAL2,
508c2ecf20Sopenharmony_ci	/* 27042 */	0x04 | CS4231_XTAL1,
518c2ecf20Sopenharmony_ci	/* 32000 */	0x06 | CS4231_XTAL1,
528c2ecf20Sopenharmony_ci	/* 33075 */	0x0C | CS4231_XTAL2,
538c2ecf20Sopenharmony_ci	/* 37800 */	0x08 | CS4231_XTAL2,
548c2ecf20Sopenharmony_ci	/* 44100 */	0x0A | CS4231_XTAL2,
558c2ecf20Sopenharmony_ci	/* 48000 */	0x0C | CS4231_XTAL1
568c2ecf20Sopenharmony_ci};
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_cistatic const unsigned int rates[14] = {
598c2ecf20Sopenharmony_ci	5510, 6620, 8000, 9600, 11025, 16000, 18900, 22050,
608c2ecf20Sopenharmony_ci	27042, 32000, 33075, 37800, 44100, 48000
618c2ecf20Sopenharmony_ci};
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_cistatic const struct snd_pcm_hw_constraint_list hw_constraints_rates = {
648c2ecf20Sopenharmony_ci	.count = ARRAY_SIZE(rates),
658c2ecf20Sopenharmony_ci	.list = rates,
668c2ecf20Sopenharmony_ci	.mask = 0,
678c2ecf20Sopenharmony_ci};
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_cistatic int snd_wss_xrate(struct snd_pcm_runtime *runtime)
708c2ecf20Sopenharmony_ci{
718c2ecf20Sopenharmony_ci	return snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
728c2ecf20Sopenharmony_ci					  &hw_constraints_rates);
738c2ecf20Sopenharmony_ci}
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_cistatic const unsigned char snd_wss_original_image[32] =
768c2ecf20Sopenharmony_ci{
778c2ecf20Sopenharmony_ci	0x00,			/* 00/00 - lic */
788c2ecf20Sopenharmony_ci	0x00,			/* 01/01 - ric */
798c2ecf20Sopenharmony_ci	0x9f,			/* 02/02 - la1ic */
808c2ecf20Sopenharmony_ci	0x9f,			/* 03/03 - ra1ic */
818c2ecf20Sopenharmony_ci	0x9f,			/* 04/04 - la2ic */
828c2ecf20Sopenharmony_ci	0x9f,			/* 05/05 - ra2ic */
838c2ecf20Sopenharmony_ci	0xbf,			/* 06/06 - loc */
848c2ecf20Sopenharmony_ci	0xbf,			/* 07/07 - roc */
858c2ecf20Sopenharmony_ci	0x20,			/* 08/08 - pdfr */
868c2ecf20Sopenharmony_ci	CS4231_AUTOCALIB,	/* 09/09 - ic */
878c2ecf20Sopenharmony_ci	0x00,			/* 0a/10 - pc */
888c2ecf20Sopenharmony_ci	0x00,			/* 0b/11 - ti */
898c2ecf20Sopenharmony_ci	CS4231_MODE2,		/* 0c/12 - mi */
908c2ecf20Sopenharmony_ci	0xfc,			/* 0d/13 - lbc */
918c2ecf20Sopenharmony_ci	0x00,			/* 0e/14 - pbru */
928c2ecf20Sopenharmony_ci	0x00,			/* 0f/15 - pbrl */
938c2ecf20Sopenharmony_ci	0x80,			/* 10/16 - afei */
948c2ecf20Sopenharmony_ci	0x01,			/* 11/17 - afeii */
958c2ecf20Sopenharmony_ci	0x9f,			/* 12/18 - llic */
968c2ecf20Sopenharmony_ci	0x9f,			/* 13/19 - rlic */
978c2ecf20Sopenharmony_ci	0x00,			/* 14/20 - tlb */
988c2ecf20Sopenharmony_ci	0x00,			/* 15/21 - thb */
998c2ecf20Sopenharmony_ci	0x00,			/* 16/22 - la3mic/reserved */
1008c2ecf20Sopenharmony_ci	0x00,			/* 17/23 - ra3mic/reserved */
1018c2ecf20Sopenharmony_ci	0x00,			/* 18/24 - afs */
1028c2ecf20Sopenharmony_ci	0x00,			/* 19/25 - lamoc/version */
1038c2ecf20Sopenharmony_ci	0xcf,			/* 1a/26 - mioc */
1048c2ecf20Sopenharmony_ci	0x00,			/* 1b/27 - ramoc/reserved */
1058c2ecf20Sopenharmony_ci	0x20,			/* 1c/28 - cdfr */
1068c2ecf20Sopenharmony_ci	0x00,			/* 1d/29 - res4 */
1078c2ecf20Sopenharmony_ci	0x00,			/* 1e/30 - cbru */
1088c2ecf20Sopenharmony_ci	0x00,			/* 1f/31 - cbrl */
1098c2ecf20Sopenharmony_ci};
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_cistatic const unsigned char snd_opti93x_original_image[32] =
1128c2ecf20Sopenharmony_ci{
1138c2ecf20Sopenharmony_ci	0x00,		/* 00/00 - l_mixout_outctrl */
1148c2ecf20Sopenharmony_ci	0x00,		/* 01/01 - r_mixout_outctrl */
1158c2ecf20Sopenharmony_ci	0x88,		/* 02/02 - l_cd_inctrl */
1168c2ecf20Sopenharmony_ci	0x88,		/* 03/03 - r_cd_inctrl */
1178c2ecf20Sopenharmony_ci	0x88,		/* 04/04 - l_a1/fm_inctrl */
1188c2ecf20Sopenharmony_ci	0x88,		/* 05/05 - r_a1/fm_inctrl */
1198c2ecf20Sopenharmony_ci	0x80,		/* 06/06 - l_dac_inctrl */
1208c2ecf20Sopenharmony_ci	0x80,		/* 07/07 - r_dac_inctrl */
1218c2ecf20Sopenharmony_ci	0x00,		/* 08/08 - ply_dataform_reg */
1228c2ecf20Sopenharmony_ci	0x00,		/* 09/09 - if_conf */
1238c2ecf20Sopenharmony_ci	0x00,		/* 0a/10 - pin_ctrl */
1248c2ecf20Sopenharmony_ci	0x00,		/* 0b/11 - err_init_reg */
1258c2ecf20Sopenharmony_ci	0x0a,		/* 0c/12 - id_reg */
1268c2ecf20Sopenharmony_ci	0x00,		/* 0d/13 - reserved */
1278c2ecf20Sopenharmony_ci	0x00,		/* 0e/14 - ply_upcount_reg */
1288c2ecf20Sopenharmony_ci	0x00,		/* 0f/15 - ply_lowcount_reg */
1298c2ecf20Sopenharmony_ci	0x88,		/* 10/16 - reserved/l_a1_inctrl */
1308c2ecf20Sopenharmony_ci	0x88,		/* 11/17 - reserved/r_a1_inctrl */
1318c2ecf20Sopenharmony_ci	0x88,		/* 12/18 - l_line_inctrl */
1328c2ecf20Sopenharmony_ci	0x88,		/* 13/19 - r_line_inctrl */
1338c2ecf20Sopenharmony_ci	0x88,		/* 14/20 - l_mic_inctrl */
1348c2ecf20Sopenharmony_ci	0x88,		/* 15/21 - r_mic_inctrl */
1358c2ecf20Sopenharmony_ci	0x80,		/* 16/22 - l_out_outctrl */
1368c2ecf20Sopenharmony_ci	0x80,		/* 17/23 - r_out_outctrl */
1378c2ecf20Sopenharmony_ci	0x00,		/* 18/24 - reserved */
1388c2ecf20Sopenharmony_ci	0x00,		/* 19/25 - reserved */
1398c2ecf20Sopenharmony_ci	0x00,		/* 1a/26 - reserved */
1408c2ecf20Sopenharmony_ci	0x00,		/* 1b/27 - reserved */
1418c2ecf20Sopenharmony_ci	0x00,		/* 1c/28 - cap_dataform_reg */
1428c2ecf20Sopenharmony_ci	0x00,		/* 1d/29 - reserved */
1438c2ecf20Sopenharmony_ci	0x00,		/* 1e/30 - cap_upcount_reg */
1448c2ecf20Sopenharmony_ci	0x00		/* 1f/31 - cap_lowcount_reg */
1458c2ecf20Sopenharmony_ci};
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci/*
1488c2ecf20Sopenharmony_ci *  Basic I/O functions
1498c2ecf20Sopenharmony_ci */
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_cistatic inline void wss_outb(struct snd_wss *chip, u8 offset, u8 val)
1528c2ecf20Sopenharmony_ci{
1538c2ecf20Sopenharmony_ci	outb(val, chip->port + offset);
1548c2ecf20Sopenharmony_ci}
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_cistatic inline u8 wss_inb(struct snd_wss *chip, u8 offset)
1578c2ecf20Sopenharmony_ci{
1588c2ecf20Sopenharmony_ci	return inb(chip->port + offset);
1598c2ecf20Sopenharmony_ci}
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_cistatic void snd_wss_wait(struct snd_wss *chip)
1628c2ecf20Sopenharmony_ci{
1638c2ecf20Sopenharmony_ci	int timeout;
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	for (timeout = 250;
1668c2ecf20Sopenharmony_ci	     timeout > 0 && (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT);
1678c2ecf20Sopenharmony_ci	     timeout--)
1688c2ecf20Sopenharmony_ci		udelay(100);
1698c2ecf20Sopenharmony_ci}
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_cistatic void snd_wss_dout(struct snd_wss *chip, unsigned char reg,
1728c2ecf20Sopenharmony_ci			 unsigned char value)
1738c2ecf20Sopenharmony_ci{
1748c2ecf20Sopenharmony_ci	int timeout;
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	for (timeout = 250;
1778c2ecf20Sopenharmony_ci	     timeout > 0 && (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT);
1788c2ecf20Sopenharmony_ci	     timeout--)
1798c2ecf20Sopenharmony_ci		udelay(10);
1808c2ecf20Sopenharmony_ci	wss_outb(chip, CS4231P(REGSEL), chip->mce_bit | reg);
1818c2ecf20Sopenharmony_ci	wss_outb(chip, CS4231P(REG), value);
1828c2ecf20Sopenharmony_ci	mb();
1838c2ecf20Sopenharmony_ci}
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_civoid snd_wss_out(struct snd_wss *chip, unsigned char reg, unsigned char value)
1868c2ecf20Sopenharmony_ci{
1878c2ecf20Sopenharmony_ci	snd_wss_wait(chip);
1888c2ecf20Sopenharmony_ci#ifdef CONFIG_SND_DEBUG
1898c2ecf20Sopenharmony_ci	if (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT)
1908c2ecf20Sopenharmony_ci		snd_printk(KERN_DEBUG "out: auto calibration time out "
1918c2ecf20Sopenharmony_ci			   "- reg = 0x%x, value = 0x%x\n", reg, value);
1928c2ecf20Sopenharmony_ci#endif
1938c2ecf20Sopenharmony_ci	wss_outb(chip, CS4231P(REGSEL), chip->mce_bit | reg);
1948c2ecf20Sopenharmony_ci	wss_outb(chip, CS4231P(REG), value);
1958c2ecf20Sopenharmony_ci	chip->image[reg] = value;
1968c2ecf20Sopenharmony_ci	mb();
1978c2ecf20Sopenharmony_ci	snd_printdd("codec out - reg 0x%x = 0x%x\n",
1988c2ecf20Sopenharmony_ci			chip->mce_bit | reg, value);
1998c2ecf20Sopenharmony_ci}
2008c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_wss_out);
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ciunsigned char snd_wss_in(struct snd_wss *chip, unsigned char reg)
2038c2ecf20Sopenharmony_ci{
2048c2ecf20Sopenharmony_ci	snd_wss_wait(chip);
2058c2ecf20Sopenharmony_ci#ifdef CONFIG_SND_DEBUG
2068c2ecf20Sopenharmony_ci	if (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT)
2078c2ecf20Sopenharmony_ci		snd_printk(KERN_DEBUG "in: auto calibration time out "
2088c2ecf20Sopenharmony_ci			   "- reg = 0x%x\n", reg);
2098c2ecf20Sopenharmony_ci#endif
2108c2ecf20Sopenharmony_ci	wss_outb(chip, CS4231P(REGSEL), chip->mce_bit | reg);
2118c2ecf20Sopenharmony_ci	mb();
2128c2ecf20Sopenharmony_ci	return wss_inb(chip, CS4231P(REG));
2138c2ecf20Sopenharmony_ci}
2148c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_wss_in);
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_civoid snd_cs4236_ext_out(struct snd_wss *chip, unsigned char reg,
2178c2ecf20Sopenharmony_ci			unsigned char val)
2188c2ecf20Sopenharmony_ci{
2198c2ecf20Sopenharmony_ci	wss_outb(chip, CS4231P(REGSEL), chip->mce_bit | 0x17);
2208c2ecf20Sopenharmony_ci	wss_outb(chip, CS4231P(REG),
2218c2ecf20Sopenharmony_ci		 reg | (chip->image[CS4236_EXT_REG] & 0x01));
2228c2ecf20Sopenharmony_ci	wss_outb(chip, CS4231P(REG), val);
2238c2ecf20Sopenharmony_ci	chip->eimage[CS4236_REG(reg)] = val;
2248c2ecf20Sopenharmony_ci#if 0
2258c2ecf20Sopenharmony_ci	printk(KERN_DEBUG "ext out : reg = 0x%x, val = 0x%x\n", reg, val);
2268c2ecf20Sopenharmony_ci#endif
2278c2ecf20Sopenharmony_ci}
2288c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_cs4236_ext_out);
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ciunsigned char snd_cs4236_ext_in(struct snd_wss *chip, unsigned char reg)
2318c2ecf20Sopenharmony_ci{
2328c2ecf20Sopenharmony_ci	wss_outb(chip, CS4231P(REGSEL), chip->mce_bit | 0x17);
2338c2ecf20Sopenharmony_ci	wss_outb(chip, CS4231P(REG),
2348c2ecf20Sopenharmony_ci		 reg | (chip->image[CS4236_EXT_REG] & 0x01));
2358c2ecf20Sopenharmony_ci#if 1
2368c2ecf20Sopenharmony_ci	return wss_inb(chip, CS4231P(REG));
2378c2ecf20Sopenharmony_ci#else
2388c2ecf20Sopenharmony_ci	{
2398c2ecf20Sopenharmony_ci		unsigned char res;
2408c2ecf20Sopenharmony_ci		res = wss_inb(chip, CS4231P(REG));
2418c2ecf20Sopenharmony_ci		printk(KERN_DEBUG "ext in : reg = 0x%x, val = 0x%x\n",
2428c2ecf20Sopenharmony_ci		       reg, res);
2438c2ecf20Sopenharmony_ci		return res;
2448c2ecf20Sopenharmony_ci	}
2458c2ecf20Sopenharmony_ci#endif
2468c2ecf20Sopenharmony_ci}
2478c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_cs4236_ext_in);
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci#if 0
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_cistatic void snd_wss_debug(struct snd_wss *chip)
2528c2ecf20Sopenharmony_ci{
2538c2ecf20Sopenharmony_ci	printk(KERN_DEBUG
2548c2ecf20Sopenharmony_ci		"CS4231 REGS:      INDEX = 0x%02x  "
2558c2ecf20Sopenharmony_ci		"                 STATUS = 0x%02x\n",
2568c2ecf20Sopenharmony_ci					wss_inb(chip, CS4231P(REGSEL)),
2578c2ecf20Sopenharmony_ci					wss_inb(chip, CS4231P(STATUS)));
2588c2ecf20Sopenharmony_ci	printk(KERN_DEBUG
2598c2ecf20Sopenharmony_ci		"  0x00: left input      = 0x%02x  "
2608c2ecf20Sopenharmony_ci		"  0x10: alt 1 (CFIG 2)  = 0x%02x\n",
2618c2ecf20Sopenharmony_ci					snd_wss_in(chip, 0x00),
2628c2ecf20Sopenharmony_ci					snd_wss_in(chip, 0x10));
2638c2ecf20Sopenharmony_ci	printk(KERN_DEBUG
2648c2ecf20Sopenharmony_ci		"  0x01: right input     = 0x%02x  "
2658c2ecf20Sopenharmony_ci		"  0x11: alt 2 (CFIG 3)  = 0x%02x\n",
2668c2ecf20Sopenharmony_ci					snd_wss_in(chip, 0x01),
2678c2ecf20Sopenharmony_ci					snd_wss_in(chip, 0x11));
2688c2ecf20Sopenharmony_ci	printk(KERN_DEBUG
2698c2ecf20Sopenharmony_ci		"  0x02: GF1 left input  = 0x%02x  "
2708c2ecf20Sopenharmony_ci		"  0x12: left line in    = 0x%02x\n",
2718c2ecf20Sopenharmony_ci					snd_wss_in(chip, 0x02),
2728c2ecf20Sopenharmony_ci					snd_wss_in(chip, 0x12));
2738c2ecf20Sopenharmony_ci	printk(KERN_DEBUG
2748c2ecf20Sopenharmony_ci		"  0x03: GF1 right input = 0x%02x  "
2758c2ecf20Sopenharmony_ci		"  0x13: right line in   = 0x%02x\n",
2768c2ecf20Sopenharmony_ci					snd_wss_in(chip, 0x03),
2778c2ecf20Sopenharmony_ci					snd_wss_in(chip, 0x13));
2788c2ecf20Sopenharmony_ci	printk(KERN_DEBUG
2798c2ecf20Sopenharmony_ci		"  0x04: CD left input   = 0x%02x  "
2808c2ecf20Sopenharmony_ci		"  0x14: timer low       = 0x%02x\n",
2818c2ecf20Sopenharmony_ci					snd_wss_in(chip, 0x04),
2828c2ecf20Sopenharmony_ci					snd_wss_in(chip, 0x14));
2838c2ecf20Sopenharmony_ci	printk(KERN_DEBUG
2848c2ecf20Sopenharmony_ci		"  0x05: CD right input  = 0x%02x  "
2858c2ecf20Sopenharmony_ci		"  0x15: timer high      = 0x%02x\n",
2868c2ecf20Sopenharmony_ci					snd_wss_in(chip, 0x05),
2878c2ecf20Sopenharmony_ci					snd_wss_in(chip, 0x15));
2888c2ecf20Sopenharmony_ci	printk(KERN_DEBUG
2898c2ecf20Sopenharmony_ci		"  0x06: left output     = 0x%02x  "
2908c2ecf20Sopenharmony_ci		"  0x16: left MIC (PnP)  = 0x%02x\n",
2918c2ecf20Sopenharmony_ci					snd_wss_in(chip, 0x06),
2928c2ecf20Sopenharmony_ci					snd_wss_in(chip, 0x16));
2938c2ecf20Sopenharmony_ci	printk(KERN_DEBUG
2948c2ecf20Sopenharmony_ci		"  0x07: right output    = 0x%02x  "
2958c2ecf20Sopenharmony_ci		"  0x17: right MIC (PnP) = 0x%02x\n",
2968c2ecf20Sopenharmony_ci					snd_wss_in(chip, 0x07),
2978c2ecf20Sopenharmony_ci					snd_wss_in(chip, 0x17));
2988c2ecf20Sopenharmony_ci	printk(KERN_DEBUG
2998c2ecf20Sopenharmony_ci		"  0x08: playback format = 0x%02x  "
3008c2ecf20Sopenharmony_ci		"  0x18: IRQ status      = 0x%02x\n",
3018c2ecf20Sopenharmony_ci					snd_wss_in(chip, 0x08),
3028c2ecf20Sopenharmony_ci					snd_wss_in(chip, 0x18));
3038c2ecf20Sopenharmony_ci	printk(KERN_DEBUG
3048c2ecf20Sopenharmony_ci		"  0x09: iface (CFIG 1)  = 0x%02x  "
3058c2ecf20Sopenharmony_ci		"  0x19: left line out   = 0x%02x\n",
3068c2ecf20Sopenharmony_ci					snd_wss_in(chip, 0x09),
3078c2ecf20Sopenharmony_ci					snd_wss_in(chip, 0x19));
3088c2ecf20Sopenharmony_ci	printk(KERN_DEBUG
3098c2ecf20Sopenharmony_ci		"  0x0a: pin control     = 0x%02x  "
3108c2ecf20Sopenharmony_ci		"  0x1a: mono control    = 0x%02x\n",
3118c2ecf20Sopenharmony_ci					snd_wss_in(chip, 0x0a),
3128c2ecf20Sopenharmony_ci					snd_wss_in(chip, 0x1a));
3138c2ecf20Sopenharmony_ci	printk(KERN_DEBUG
3148c2ecf20Sopenharmony_ci		"  0x0b: init & status   = 0x%02x  "
3158c2ecf20Sopenharmony_ci		"  0x1b: right line out  = 0x%02x\n",
3168c2ecf20Sopenharmony_ci					snd_wss_in(chip, 0x0b),
3178c2ecf20Sopenharmony_ci					snd_wss_in(chip, 0x1b));
3188c2ecf20Sopenharmony_ci	printk(KERN_DEBUG
3198c2ecf20Sopenharmony_ci		"  0x0c: revision & mode = 0x%02x  "
3208c2ecf20Sopenharmony_ci		"  0x1c: record format   = 0x%02x\n",
3218c2ecf20Sopenharmony_ci					snd_wss_in(chip, 0x0c),
3228c2ecf20Sopenharmony_ci					snd_wss_in(chip, 0x1c));
3238c2ecf20Sopenharmony_ci	printk(KERN_DEBUG
3248c2ecf20Sopenharmony_ci		"  0x0d: loopback        = 0x%02x  "
3258c2ecf20Sopenharmony_ci		"  0x1d: var freq (PnP)  = 0x%02x\n",
3268c2ecf20Sopenharmony_ci					snd_wss_in(chip, 0x0d),
3278c2ecf20Sopenharmony_ci					snd_wss_in(chip, 0x1d));
3288c2ecf20Sopenharmony_ci	printk(KERN_DEBUG
3298c2ecf20Sopenharmony_ci		"  0x0e: ply upr count   = 0x%02x  "
3308c2ecf20Sopenharmony_ci		"  0x1e: ply lwr count   = 0x%02x\n",
3318c2ecf20Sopenharmony_ci					snd_wss_in(chip, 0x0e),
3328c2ecf20Sopenharmony_ci					snd_wss_in(chip, 0x1e));
3338c2ecf20Sopenharmony_ci	printk(KERN_DEBUG
3348c2ecf20Sopenharmony_ci		"  0x0f: rec upr count   = 0x%02x  "
3358c2ecf20Sopenharmony_ci		"  0x1f: rec lwr count   = 0x%02x\n",
3368c2ecf20Sopenharmony_ci					snd_wss_in(chip, 0x0f),
3378c2ecf20Sopenharmony_ci					snd_wss_in(chip, 0x1f));
3388c2ecf20Sopenharmony_ci}
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci#endif
3418c2ecf20Sopenharmony_ci
3428c2ecf20Sopenharmony_ci/*
3438c2ecf20Sopenharmony_ci *  CS4231 detection / MCE routines
3448c2ecf20Sopenharmony_ci */
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_cistatic void snd_wss_busy_wait(struct snd_wss *chip)
3478c2ecf20Sopenharmony_ci{
3488c2ecf20Sopenharmony_ci	int timeout;
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci	/* huh.. looks like this sequence is proper for CS4231A chip (GUS MAX) */
3518c2ecf20Sopenharmony_ci	for (timeout = 5; timeout > 0; timeout--)
3528c2ecf20Sopenharmony_ci		wss_inb(chip, CS4231P(REGSEL));
3538c2ecf20Sopenharmony_ci	/* end of cleanup sequence */
3548c2ecf20Sopenharmony_ci	for (timeout = 25000;
3558c2ecf20Sopenharmony_ci	     timeout > 0 && (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT);
3568c2ecf20Sopenharmony_ci	     timeout--)
3578c2ecf20Sopenharmony_ci		udelay(10);
3588c2ecf20Sopenharmony_ci}
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_civoid snd_wss_mce_up(struct snd_wss *chip)
3618c2ecf20Sopenharmony_ci{
3628c2ecf20Sopenharmony_ci	unsigned long flags;
3638c2ecf20Sopenharmony_ci	int timeout;
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_ci	snd_wss_wait(chip);
3668c2ecf20Sopenharmony_ci#ifdef CONFIG_SND_DEBUG
3678c2ecf20Sopenharmony_ci	if (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT)
3688c2ecf20Sopenharmony_ci		snd_printk(KERN_DEBUG
3698c2ecf20Sopenharmony_ci			   "mce_up - auto calibration time out (0)\n");
3708c2ecf20Sopenharmony_ci#endif
3718c2ecf20Sopenharmony_ci	spin_lock_irqsave(&chip->reg_lock, flags);
3728c2ecf20Sopenharmony_ci	chip->mce_bit |= CS4231_MCE;
3738c2ecf20Sopenharmony_ci	timeout = wss_inb(chip, CS4231P(REGSEL));
3748c2ecf20Sopenharmony_ci	if (timeout == 0x80)
3758c2ecf20Sopenharmony_ci		snd_printk(KERN_DEBUG "mce_up [0x%lx]: "
3768c2ecf20Sopenharmony_ci			   "serious init problem - codec still busy\n",
3778c2ecf20Sopenharmony_ci			   chip->port);
3788c2ecf20Sopenharmony_ci	if (!(timeout & CS4231_MCE))
3798c2ecf20Sopenharmony_ci		wss_outb(chip, CS4231P(REGSEL),
3808c2ecf20Sopenharmony_ci			 chip->mce_bit | (timeout & 0x1f));
3818c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&chip->reg_lock, flags);
3828c2ecf20Sopenharmony_ci}
3838c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_wss_mce_up);
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_civoid snd_wss_mce_down(struct snd_wss *chip)
3868c2ecf20Sopenharmony_ci{
3878c2ecf20Sopenharmony_ci	unsigned long flags;
3888c2ecf20Sopenharmony_ci	unsigned long end_time;
3898c2ecf20Sopenharmony_ci	int timeout;
3908c2ecf20Sopenharmony_ci	int hw_mask = WSS_HW_CS4231_MASK | WSS_HW_CS4232_MASK | WSS_HW_AD1848;
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci	snd_wss_busy_wait(chip);
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_ci#ifdef CONFIG_SND_DEBUG
3958c2ecf20Sopenharmony_ci	if (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT)
3968c2ecf20Sopenharmony_ci		snd_printk(KERN_DEBUG "mce_down [0x%lx] - "
3978c2ecf20Sopenharmony_ci			   "auto calibration time out (0)\n",
3988c2ecf20Sopenharmony_ci			   (long)CS4231P(REGSEL));
3998c2ecf20Sopenharmony_ci#endif
4008c2ecf20Sopenharmony_ci	spin_lock_irqsave(&chip->reg_lock, flags);
4018c2ecf20Sopenharmony_ci	chip->mce_bit &= ~CS4231_MCE;
4028c2ecf20Sopenharmony_ci	timeout = wss_inb(chip, CS4231P(REGSEL));
4038c2ecf20Sopenharmony_ci	wss_outb(chip, CS4231P(REGSEL), chip->mce_bit | (timeout & 0x1f));
4048c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&chip->reg_lock, flags);
4058c2ecf20Sopenharmony_ci	if (timeout == 0x80)
4068c2ecf20Sopenharmony_ci		snd_printk(KERN_DEBUG "mce_down [0x%lx]: "
4078c2ecf20Sopenharmony_ci			   "serious init problem - codec still busy\n",
4088c2ecf20Sopenharmony_ci			   chip->port);
4098c2ecf20Sopenharmony_ci	if ((timeout & CS4231_MCE) == 0 || !(chip->hardware & hw_mask))
4108c2ecf20Sopenharmony_ci		return;
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci	/*
4138c2ecf20Sopenharmony_ci	 * Wait for (possible -- during init auto-calibration may not be set)
4148c2ecf20Sopenharmony_ci	 * calibration process to start. Needs up to 5 sample periods on AD1848
4158c2ecf20Sopenharmony_ci	 * which at the slowest possible rate of 5.5125 kHz means 907 us.
4168c2ecf20Sopenharmony_ci	 */
4178c2ecf20Sopenharmony_ci	msleep(1);
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_ci	snd_printdd("(1) jiffies = %lu\n", jiffies);
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_ci	/* check condition up to 250 ms */
4228c2ecf20Sopenharmony_ci	end_time = jiffies + msecs_to_jiffies(250);
4238c2ecf20Sopenharmony_ci	while (snd_wss_in(chip, CS4231_TEST_INIT) &
4248c2ecf20Sopenharmony_ci		CS4231_CALIB_IN_PROGRESS) {
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_ci		if (time_after(jiffies, end_time)) {
4278c2ecf20Sopenharmony_ci			snd_printk(KERN_ERR "mce_down - "
4288c2ecf20Sopenharmony_ci					"auto calibration time out (2)\n");
4298c2ecf20Sopenharmony_ci			return;
4308c2ecf20Sopenharmony_ci		}
4318c2ecf20Sopenharmony_ci		msleep(1);
4328c2ecf20Sopenharmony_ci	}
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_ci	snd_printdd("(2) jiffies = %lu\n", jiffies);
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_ci	/* check condition up to 100 ms */
4378c2ecf20Sopenharmony_ci	end_time = jiffies + msecs_to_jiffies(100);
4388c2ecf20Sopenharmony_ci	while (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT) {
4398c2ecf20Sopenharmony_ci		if (time_after(jiffies, end_time)) {
4408c2ecf20Sopenharmony_ci			snd_printk(KERN_ERR "mce_down - auto calibration time out (3)\n");
4418c2ecf20Sopenharmony_ci			return;
4428c2ecf20Sopenharmony_ci		}
4438c2ecf20Sopenharmony_ci		msleep(1);
4448c2ecf20Sopenharmony_ci	}
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_ci	snd_printdd("(3) jiffies = %lu\n", jiffies);
4478c2ecf20Sopenharmony_ci	snd_printd("mce_down - exit = 0x%x\n", wss_inb(chip, CS4231P(REGSEL)));
4488c2ecf20Sopenharmony_ci}
4498c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_wss_mce_down);
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_cistatic unsigned int snd_wss_get_count(unsigned char format, unsigned int size)
4528c2ecf20Sopenharmony_ci{
4538c2ecf20Sopenharmony_ci	switch (format & 0xe0) {
4548c2ecf20Sopenharmony_ci	case CS4231_LINEAR_16:
4558c2ecf20Sopenharmony_ci	case CS4231_LINEAR_16_BIG:
4568c2ecf20Sopenharmony_ci		size >>= 1;
4578c2ecf20Sopenharmony_ci		break;
4588c2ecf20Sopenharmony_ci	case CS4231_ADPCM_16:
4598c2ecf20Sopenharmony_ci		return size >> 2;
4608c2ecf20Sopenharmony_ci	}
4618c2ecf20Sopenharmony_ci	if (format & CS4231_STEREO)
4628c2ecf20Sopenharmony_ci		size >>= 1;
4638c2ecf20Sopenharmony_ci	return size;
4648c2ecf20Sopenharmony_ci}
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_cistatic int snd_wss_trigger(struct snd_pcm_substream *substream,
4678c2ecf20Sopenharmony_ci			   int cmd)
4688c2ecf20Sopenharmony_ci{
4698c2ecf20Sopenharmony_ci	struct snd_wss *chip = snd_pcm_substream_chip(substream);
4708c2ecf20Sopenharmony_ci	int result = 0;
4718c2ecf20Sopenharmony_ci	unsigned int what;
4728c2ecf20Sopenharmony_ci	struct snd_pcm_substream *s;
4738c2ecf20Sopenharmony_ci	int do_start;
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ci	switch (cmd) {
4768c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_START:
4778c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_RESUME:
4788c2ecf20Sopenharmony_ci		do_start = 1; break;
4798c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_STOP:
4808c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_SUSPEND:
4818c2ecf20Sopenharmony_ci		do_start = 0; break;
4828c2ecf20Sopenharmony_ci	default:
4838c2ecf20Sopenharmony_ci		return -EINVAL;
4848c2ecf20Sopenharmony_ci	}
4858c2ecf20Sopenharmony_ci
4868c2ecf20Sopenharmony_ci	what = 0;
4878c2ecf20Sopenharmony_ci	snd_pcm_group_for_each_entry(s, substream) {
4888c2ecf20Sopenharmony_ci		if (s == chip->playback_substream) {
4898c2ecf20Sopenharmony_ci			what |= CS4231_PLAYBACK_ENABLE;
4908c2ecf20Sopenharmony_ci			snd_pcm_trigger_done(s, substream);
4918c2ecf20Sopenharmony_ci		} else if (s == chip->capture_substream) {
4928c2ecf20Sopenharmony_ci			what |= CS4231_RECORD_ENABLE;
4938c2ecf20Sopenharmony_ci			snd_pcm_trigger_done(s, substream);
4948c2ecf20Sopenharmony_ci		}
4958c2ecf20Sopenharmony_ci	}
4968c2ecf20Sopenharmony_ci	spin_lock(&chip->reg_lock);
4978c2ecf20Sopenharmony_ci	if (do_start) {
4988c2ecf20Sopenharmony_ci		chip->image[CS4231_IFACE_CTRL] |= what;
4998c2ecf20Sopenharmony_ci		if (chip->trigger)
5008c2ecf20Sopenharmony_ci			chip->trigger(chip, what, 1);
5018c2ecf20Sopenharmony_ci	} else {
5028c2ecf20Sopenharmony_ci		chip->image[CS4231_IFACE_CTRL] &= ~what;
5038c2ecf20Sopenharmony_ci		if (chip->trigger)
5048c2ecf20Sopenharmony_ci			chip->trigger(chip, what, 0);
5058c2ecf20Sopenharmony_ci	}
5068c2ecf20Sopenharmony_ci	snd_wss_out(chip, CS4231_IFACE_CTRL, chip->image[CS4231_IFACE_CTRL]);
5078c2ecf20Sopenharmony_ci	spin_unlock(&chip->reg_lock);
5088c2ecf20Sopenharmony_ci#if 0
5098c2ecf20Sopenharmony_ci	snd_wss_debug(chip);
5108c2ecf20Sopenharmony_ci#endif
5118c2ecf20Sopenharmony_ci	return result;
5128c2ecf20Sopenharmony_ci}
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_ci/*
5158c2ecf20Sopenharmony_ci *  CODEC I/O
5168c2ecf20Sopenharmony_ci */
5178c2ecf20Sopenharmony_ci
5188c2ecf20Sopenharmony_cistatic unsigned char snd_wss_get_rate(unsigned int rate)
5198c2ecf20Sopenharmony_ci{
5208c2ecf20Sopenharmony_ci	int i;
5218c2ecf20Sopenharmony_ci
5228c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(rates); i++)
5238c2ecf20Sopenharmony_ci		if (rate == rates[i])
5248c2ecf20Sopenharmony_ci			return freq_bits[i];
5258c2ecf20Sopenharmony_ci	// snd_BUG();
5268c2ecf20Sopenharmony_ci	return freq_bits[ARRAY_SIZE(rates) - 1];
5278c2ecf20Sopenharmony_ci}
5288c2ecf20Sopenharmony_ci
5298c2ecf20Sopenharmony_cistatic unsigned char snd_wss_get_format(struct snd_wss *chip,
5308c2ecf20Sopenharmony_ci					snd_pcm_format_t format,
5318c2ecf20Sopenharmony_ci					int channels)
5328c2ecf20Sopenharmony_ci{
5338c2ecf20Sopenharmony_ci	unsigned char rformat;
5348c2ecf20Sopenharmony_ci
5358c2ecf20Sopenharmony_ci	rformat = CS4231_LINEAR_8;
5368c2ecf20Sopenharmony_ci	switch (format) {
5378c2ecf20Sopenharmony_ci	case SNDRV_PCM_FORMAT_MU_LAW:	rformat = CS4231_ULAW_8; break;
5388c2ecf20Sopenharmony_ci	case SNDRV_PCM_FORMAT_A_LAW:	rformat = CS4231_ALAW_8; break;
5398c2ecf20Sopenharmony_ci	case SNDRV_PCM_FORMAT_S16_LE:	rformat = CS4231_LINEAR_16; break;
5408c2ecf20Sopenharmony_ci	case SNDRV_PCM_FORMAT_S16_BE:	rformat = CS4231_LINEAR_16_BIG; break;
5418c2ecf20Sopenharmony_ci	case SNDRV_PCM_FORMAT_IMA_ADPCM:	rformat = CS4231_ADPCM_16; break;
5428c2ecf20Sopenharmony_ci	}
5438c2ecf20Sopenharmony_ci	if (channels > 1)
5448c2ecf20Sopenharmony_ci		rformat |= CS4231_STEREO;
5458c2ecf20Sopenharmony_ci#if 0
5468c2ecf20Sopenharmony_ci	snd_printk(KERN_DEBUG "get_format: 0x%x (mode=0x%x)\n", format, mode);
5478c2ecf20Sopenharmony_ci#endif
5488c2ecf20Sopenharmony_ci	return rformat;
5498c2ecf20Sopenharmony_ci}
5508c2ecf20Sopenharmony_ci
5518c2ecf20Sopenharmony_cistatic void snd_wss_calibrate_mute(struct snd_wss *chip, int mute)
5528c2ecf20Sopenharmony_ci{
5538c2ecf20Sopenharmony_ci	unsigned long flags;
5548c2ecf20Sopenharmony_ci
5558c2ecf20Sopenharmony_ci	mute = mute ? 0x80 : 0;
5568c2ecf20Sopenharmony_ci	spin_lock_irqsave(&chip->reg_lock, flags);
5578c2ecf20Sopenharmony_ci	if (chip->calibrate_mute == mute) {
5588c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&chip->reg_lock, flags);
5598c2ecf20Sopenharmony_ci		return;
5608c2ecf20Sopenharmony_ci	}
5618c2ecf20Sopenharmony_ci	if (!mute) {
5628c2ecf20Sopenharmony_ci		snd_wss_dout(chip, CS4231_LEFT_INPUT,
5638c2ecf20Sopenharmony_ci			     chip->image[CS4231_LEFT_INPUT]);
5648c2ecf20Sopenharmony_ci		snd_wss_dout(chip, CS4231_RIGHT_INPUT,
5658c2ecf20Sopenharmony_ci			     chip->image[CS4231_RIGHT_INPUT]);
5668c2ecf20Sopenharmony_ci		snd_wss_dout(chip, CS4231_LOOPBACK,
5678c2ecf20Sopenharmony_ci			     chip->image[CS4231_LOOPBACK]);
5688c2ecf20Sopenharmony_ci	} else {
5698c2ecf20Sopenharmony_ci		snd_wss_dout(chip, CS4231_LEFT_INPUT,
5708c2ecf20Sopenharmony_ci			     0);
5718c2ecf20Sopenharmony_ci		snd_wss_dout(chip, CS4231_RIGHT_INPUT,
5728c2ecf20Sopenharmony_ci			     0);
5738c2ecf20Sopenharmony_ci		snd_wss_dout(chip, CS4231_LOOPBACK,
5748c2ecf20Sopenharmony_ci			     0xfd);
5758c2ecf20Sopenharmony_ci	}
5768c2ecf20Sopenharmony_ci
5778c2ecf20Sopenharmony_ci	snd_wss_dout(chip, CS4231_AUX1_LEFT_INPUT,
5788c2ecf20Sopenharmony_ci		     mute | chip->image[CS4231_AUX1_LEFT_INPUT]);
5798c2ecf20Sopenharmony_ci	snd_wss_dout(chip, CS4231_AUX1_RIGHT_INPUT,
5808c2ecf20Sopenharmony_ci		     mute | chip->image[CS4231_AUX1_RIGHT_INPUT]);
5818c2ecf20Sopenharmony_ci	snd_wss_dout(chip, CS4231_AUX2_LEFT_INPUT,
5828c2ecf20Sopenharmony_ci		     mute | chip->image[CS4231_AUX2_LEFT_INPUT]);
5838c2ecf20Sopenharmony_ci	snd_wss_dout(chip, CS4231_AUX2_RIGHT_INPUT,
5848c2ecf20Sopenharmony_ci		     mute | chip->image[CS4231_AUX2_RIGHT_INPUT]);
5858c2ecf20Sopenharmony_ci	snd_wss_dout(chip, CS4231_LEFT_OUTPUT,
5868c2ecf20Sopenharmony_ci		     mute | chip->image[CS4231_LEFT_OUTPUT]);
5878c2ecf20Sopenharmony_ci	snd_wss_dout(chip, CS4231_RIGHT_OUTPUT,
5888c2ecf20Sopenharmony_ci		     mute | chip->image[CS4231_RIGHT_OUTPUT]);
5898c2ecf20Sopenharmony_ci	if (!(chip->hardware & WSS_HW_AD1848_MASK)) {
5908c2ecf20Sopenharmony_ci		snd_wss_dout(chip, CS4231_LEFT_LINE_IN,
5918c2ecf20Sopenharmony_ci			     mute | chip->image[CS4231_LEFT_LINE_IN]);
5928c2ecf20Sopenharmony_ci		snd_wss_dout(chip, CS4231_RIGHT_LINE_IN,
5938c2ecf20Sopenharmony_ci			     mute | chip->image[CS4231_RIGHT_LINE_IN]);
5948c2ecf20Sopenharmony_ci		snd_wss_dout(chip, CS4231_MONO_CTRL,
5958c2ecf20Sopenharmony_ci			     mute ? 0xc0 : chip->image[CS4231_MONO_CTRL]);
5968c2ecf20Sopenharmony_ci	}
5978c2ecf20Sopenharmony_ci	if (chip->hardware == WSS_HW_INTERWAVE) {
5988c2ecf20Sopenharmony_ci		snd_wss_dout(chip, CS4231_LEFT_MIC_INPUT,
5998c2ecf20Sopenharmony_ci			     mute | chip->image[CS4231_LEFT_MIC_INPUT]);
6008c2ecf20Sopenharmony_ci		snd_wss_dout(chip, CS4231_RIGHT_MIC_INPUT,
6018c2ecf20Sopenharmony_ci			     mute | chip->image[CS4231_RIGHT_MIC_INPUT]);
6028c2ecf20Sopenharmony_ci		snd_wss_dout(chip, CS4231_LINE_LEFT_OUTPUT,
6038c2ecf20Sopenharmony_ci			     mute | chip->image[CS4231_LINE_LEFT_OUTPUT]);
6048c2ecf20Sopenharmony_ci		snd_wss_dout(chip, CS4231_LINE_RIGHT_OUTPUT,
6058c2ecf20Sopenharmony_ci			     mute | chip->image[CS4231_LINE_RIGHT_OUTPUT]);
6068c2ecf20Sopenharmony_ci	}
6078c2ecf20Sopenharmony_ci	chip->calibrate_mute = mute;
6088c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&chip->reg_lock, flags);
6098c2ecf20Sopenharmony_ci}
6108c2ecf20Sopenharmony_ci
6118c2ecf20Sopenharmony_cistatic void snd_wss_playback_format(struct snd_wss *chip,
6128c2ecf20Sopenharmony_ci				       struct snd_pcm_hw_params *params,
6138c2ecf20Sopenharmony_ci				       unsigned char pdfr)
6148c2ecf20Sopenharmony_ci{
6158c2ecf20Sopenharmony_ci	unsigned long flags;
6168c2ecf20Sopenharmony_ci	int full_calib = 1;
6178c2ecf20Sopenharmony_ci
6188c2ecf20Sopenharmony_ci	mutex_lock(&chip->mce_mutex);
6198c2ecf20Sopenharmony_ci	if (chip->hardware == WSS_HW_CS4231A ||
6208c2ecf20Sopenharmony_ci	    (chip->hardware & WSS_HW_CS4232_MASK)) {
6218c2ecf20Sopenharmony_ci		spin_lock_irqsave(&chip->reg_lock, flags);
6228c2ecf20Sopenharmony_ci		if ((chip->image[CS4231_PLAYBK_FORMAT] & 0x0f) == (pdfr & 0x0f)) {	/* rate is same? */
6238c2ecf20Sopenharmony_ci			snd_wss_out(chip, CS4231_ALT_FEATURE_1,
6248c2ecf20Sopenharmony_ci				    chip->image[CS4231_ALT_FEATURE_1] | 0x10);
6258c2ecf20Sopenharmony_ci			chip->image[CS4231_PLAYBK_FORMAT] = pdfr;
6268c2ecf20Sopenharmony_ci			snd_wss_out(chip, CS4231_PLAYBK_FORMAT,
6278c2ecf20Sopenharmony_ci				    chip->image[CS4231_PLAYBK_FORMAT]);
6288c2ecf20Sopenharmony_ci			snd_wss_out(chip, CS4231_ALT_FEATURE_1,
6298c2ecf20Sopenharmony_ci				    chip->image[CS4231_ALT_FEATURE_1] &= ~0x10);
6308c2ecf20Sopenharmony_ci			udelay(100); /* Fixes audible clicks at least on GUS MAX */
6318c2ecf20Sopenharmony_ci			full_calib = 0;
6328c2ecf20Sopenharmony_ci		}
6338c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&chip->reg_lock, flags);
6348c2ecf20Sopenharmony_ci	} else if (chip->hardware == WSS_HW_AD1845) {
6358c2ecf20Sopenharmony_ci		unsigned rate = params_rate(params);
6368c2ecf20Sopenharmony_ci
6378c2ecf20Sopenharmony_ci		/*
6388c2ecf20Sopenharmony_ci		 * Program the AD1845 correctly for the playback stream.
6398c2ecf20Sopenharmony_ci		 * Note that we do NOT need to toggle the MCE bit because
6408c2ecf20Sopenharmony_ci		 * the PLAYBACK_ENABLE bit of the Interface Configuration
6418c2ecf20Sopenharmony_ci		 * register is set.
6428c2ecf20Sopenharmony_ci		 *
6438c2ecf20Sopenharmony_ci		 * NOTE: We seem to need to write to the MSB before the LSB
6448c2ecf20Sopenharmony_ci		 *       to get the correct sample frequency.
6458c2ecf20Sopenharmony_ci		 */
6468c2ecf20Sopenharmony_ci		spin_lock_irqsave(&chip->reg_lock, flags);
6478c2ecf20Sopenharmony_ci		snd_wss_out(chip, CS4231_PLAYBK_FORMAT, (pdfr & 0xf0));
6488c2ecf20Sopenharmony_ci		snd_wss_out(chip, AD1845_UPR_FREQ_SEL, (rate >> 8) & 0xff);
6498c2ecf20Sopenharmony_ci		snd_wss_out(chip, AD1845_LWR_FREQ_SEL, rate & 0xff);
6508c2ecf20Sopenharmony_ci		full_calib = 0;
6518c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&chip->reg_lock, flags);
6528c2ecf20Sopenharmony_ci	}
6538c2ecf20Sopenharmony_ci	if (full_calib) {
6548c2ecf20Sopenharmony_ci		snd_wss_mce_up(chip);
6558c2ecf20Sopenharmony_ci		spin_lock_irqsave(&chip->reg_lock, flags);
6568c2ecf20Sopenharmony_ci		if (chip->hardware != WSS_HW_INTERWAVE && !chip->single_dma) {
6578c2ecf20Sopenharmony_ci			if (chip->image[CS4231_IFACE_CTRL] & CS4231_RECORD_ENABLE)
6588c2ecf20Sopenharmony_ci				pdfr = (pdfr & 0xf0) |
6598c2ecf20Sopenharmony_ci				       (chip->image[CS4231_REC_FORMAT] & 0x0f);
6608c2ecf20Sopenharmony_ci		} else {
6618c2ecf20Sopenharmony_ci			chip->image[CS4231_PLAYBK_FORMAT] = pdfr;
6628c2ecf20Sopenharmony_ci		}
6638c2ecf20Sopenharmony_ci		snd_wss_out(chip, CS4231_PLAYBK_FORMAT, pdfr);
6648c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&chip->reg_lock, flags);
6658c2ecf20Sopenharmony_ci		if (chip->hardware == WSS_HW_OPL3SA2)
6668c2ecf20Sopenharmony_ci			udelay(100);	/* this seems to help */
6678c2ecf20Sopenharmony_ci		snd_wss_mce_down(chip);
6688c2ecf20Sopenharmony_ci	}
6698c2ecf20Sopenharmony_ci	mutex_unlock(&chip->mce_mutex);
6708c2ecf20Sopenharmony_ci}
6718c2ecf20Sopenharmony_ci
6728c2ecf20Sopenharmony_cistatic void snd_wss_capture_format(struct snd_wss *chip,
6738c2ecf20Sopenharmony_ci				   struct snd_pcm_hw_params *params,
6748c2ecf20Sopenharmony_ci				   unsigned char cdfr)
6758c2ecf20Sopenharmony_ci{
6768c2ecf20Sopenharmony_ci	unsigned long flags;
6778c2ecf20Sopenharmony_ci	int full_calib = 1;
6788c2ecf20Sopenharmony_ci
6798c2ecf20Sopenharmony_ci	mutex_lock(&chip->mce_mutex);
6808c2ecf20Sopenharmony_ci	if (chip->hardware == WSS_HW_CS4231A ||
6818c2ecf20Sopenharmony_ci	    (chip->hardware & WSS_HW_CS4232_MASK)) {
6828c2ecf20Sopenharmony_ci		spin_lock_irqsave(&chip->reg_lock, flags);
6838c2ecf20Sopenharmony_ci		if ((chip->image[CS4231_PLAYBK_FORMAT] & 0x0f) == (cdfr & 0x0f) ||	/* rate is same? */
6848c2ecf20Sopenharmony_ci		    (chip->image[CS4231_IFACE_CTRL] & CS4231_PLAYBACK_ENABLE)) {
6858c2ecf20Sopenharmony_ci			snd_wss_out(chip, CS4231_ALT_FEATURE_1,
6868c2ecf20Sopenharmony_ci				chip->image[CS4231_ALT_FEATURE_1] | 0x20);
6878c2ecf20Sopenharmony_ci			snd_wss_out(chip, CS4231_REC_FORMAT,
6888c2ecf20Sopenharmony_ci				chip->image[CS4231_REC_FORMAT] = cdfr);
6898c2ecf20Sopenharmony_ci			snd_wss_out(chip, CS4231_ALT_FEATURE_1,
6908c2ecf20Sopenharmony_ci				chip->image[CS4231_ALT_FEATURE_1] &= ~0x20);
6918c2ecf20Sopenharmony_ci			full_calib = 0;
6928c2ecf20Sopenharmony_ci		}
6938c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&chip->reg_lock, flags);
6948c2ecf20Sopenharmony_ci	} else if (chip->hardware == WSS_HW_AD1845) {
6958c2ecf20Sopenharmony_ci		unsigned rate = params_rate(params);
6968c2ecf20Sopenharmony_ci
6978c2ecf20Sopenharmony_ci		/*
6988c2ecf20Sopenharmony_ci		 * Program the AD1845 correctly for the capture stream.
6998c2ecf20Sopenharmony_ci		 * Note that we do NOT need to toggle the MCE bit because
7008c2ecf20Sopenharmony_ci		 * the PLAYBACK_ENABLE bit of the Interface Configuration
7018c2ecf20Sopenharmony_ci		 * register is set.
7028c2ecf20Sopenharmony_ci		 *
7038c2ecf20Sopenharmony_ci		 * NOTE: We seem to need to write to the MSB before the LSB
7048c2ecf20Sopenharmony_ci		 *       to get the correct sample frequency.
7058c2ecf20Sopenharmony_ci		 */
7068c2ecf20Sopenharmony_ci		spin_lock_irqsave(&chip->reg_lock, flags);
7078c2ecf20Sopenharmony_ci		snd_wss_out(chip, CS4231_REC_FORMAT, (cdfr & 0xf0));
7088c2ecf20Sopenharmony_ci		snd_wss_out(chip, AD1845_UPR_FREQ_SEL, (rate >> 8) & 0xff);
7098c2ecf20Sopenharmony_ci		snd_wss_out(chip, AD1845_LWR_FREQ_SEL, rate & 0xff);
7108c2ecf20Sopenharmony_ci		full_calib = 0;
7118c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&chip->reg_lock, flags);
7128c2ecf20Sopenharmony_ci	}
7138c2ecf20Sopenharmony_ci	if (full_calib) {
7148c2ecf20Sopenharmony_ci		snd_wss_mce_up(chip);
7158c2ecf20Sopenharmony_ci		spin_lock_irqsave(&chip->reg_lock, flags);
7168c2ecf20Sopenharmony_ci		if (chip->hardware != WSS_HW_INTERWAVE &&
7178c2ecf20Sopenharmony_ci		    !(chip->image[CS4231_IFACE_CTRL] & CS4231_PLAYBACK_ENABLE)) {
7188c2ecf20Sopenharmony_ci			if (chip->single_dma)
7198c2ecf20Sopenharmony_ci				snd_wss_out(chip, CS4231_PLAYBK_FORMAT, cdfr);
7208c2ecf20Sopenharmony_ci			else
7218c2ecf20Sopenharmony_ci				snd_wss_out(chip, CS4231_PLAYBK_FORMAT,
7228c2ecf20Sopenharmony_ci				   (chip->image[CS4231_PLAYBK_FORMAT] & 0xf0) |
7238c2ecf20Sopenharmony_ci				   (cdfr & 0x0f));
7248c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&chip->reg_lock, flags);
7258c2ecf20Sopenharmony_ci			snd_wss_mce_down(chip);
7268c2ecf20Sopenharmony_ci			snd_wss_mce_up(chip);
7278c2ecf20Sopenharmony_ci			spin_lock_irqsave(&chip->reg_lock, flags);
7288c2ecf20Sopenharmony_ci		}
7298c2ecf20Sopenharmony_ci		if (chip->hardware & WSS_HW_AD1848_MASK)
7308c2ecf20Sopenharmony_ci			snd_wss_out(chip, CS4231_PLAYBK_FORMAT, cdfr);
7318c2ecf20Sopenharmony_ci		else
7328c2ecf20Sopenharmony_ci			snd_wss_out(chip, CS4231_REC_FORMAT, cdfr);
7338c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&chip->reg_lock, flags);
7348c2ecf20Sopenharmony_ci		snd_wss_mce_down(chip);
7358c2ecf20Sopenharmony_ci	}
7368c2ecf20Sopenharmony_ci	mutex_unlock(&chip->mce_mutex);
7378c2ecf20Sopenharmony_ci}
7388c2ecf20Sopenharmony_ci
7398c2ecf20Sopenharmony_ci/*
7408c2ecf20Sopenharmony_ci *  Timer interface
7418c2ecf20Sopenharmony_ci */
7428c2ecf20Sopenharmony_ci
7438c2ecf20Sopenharmony_cistatic unsigned long snd_wss_timer_resolution(struct snd_timer *timer)
7448c2ecf20Sopenharmony_ci{
7458c2ecf20Sopenharmony_ci	struct snd_wss *chip = snd_timer_chip(timer);
7468c2ecf20Sopenharmony_ci	if (chip->hardware & WSS_HW_CS4236B_MASK)
7478c2ecf20Sopenharmony_ci		return 14467;
7488c2ecf20Sopenharmony_ci	else
7498c2ecf20Sopenharmony_ci		return chip->image[CS4231_PLAYBK_FORMAT] & 1 ? 9969 : 9920;
7508c2ecf20Sopenharmony_ci}
7518c2ecf20Sopenharmony_ci
7528c2ecf20Sopenharmony_cistatic int snd_wss_timer_start(struct snd_timer *timer)
7538c2ecf20Sopenharmony_ci{
7548c2ecf20Sopenharmony_ci	unsigned long flags;
7558c2ecf20Sopenharmony_ci	unsigned int ticks;
7568c2ecf20Sopenharmony_ci	struct snd_wss *chip = snd_timer_chip(timer);
7578c2ecf20Sopenharmony_ci	spin_lock_irqsave(&chip->reg_lock, flags);
7588c2ecf20Sopenharmony_ci	ticks = timer->sticks;
7598c2ecf20Sopenharmony_ci	if ((chip->image[CS4231_ALT_FEATURE_1] & CS4231_TIMER_ENABLE) == 0 ||
7608c2ecf20Sopenharmony_ci	    (unsigned char)(ticks >> 8) != chip->image[CS4231_TIMER_HIGH] ||
7618c2ecf20Sopenharmony_ci	    (unsigned char)ticks != chip->image[CS4231_TIMER_LOW]) {
7628c2ecf20Sopenharmony_ci		chip->image[CS4231_TIMER_HIGH] = (unsigned char) (ticks >> 8);
7638c2ecf20Sopenharmony_ci		snd_wss_out(chip, CS4231_TIMER_HIGH,
7648c2ecf20Sopenharmony_ci			    chip->image[CS4231_TIMER_HIGH]);
7658c2ecf20Sopenharmony_ci		chip->image[CS4231_TIMER_LOW] = (unsigned char) ticks;
7668c2ecf20Sopenharmony_ci		snd_wss_out(chip, CS4231_TIMER_LOW,
7678c2ecf20Sopenharmony_ci			    chip->image[CS4231_TIMER_LOW]);
7688c2ecf20Sopenharmony_ci		snd_wss_out(chip, CS4231_ALT_FEATURE_1,
7698c2ecf20Sopenharmony_ci			    chip->image[CS4231_ALT_FEATURE_1] |
7708c2ecf20Sopenharmony_ci			    CS4231_TIMER_ENABLE);
7718c2ecf20Sopenharmony_ci	}
7728c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&chip->reg_lock, flags);
7738c2ecf20Sopenharmony_ci	return 0;
7748c2ecf20Sopenharmony_ci}
7758c2ecf20Sopenharmony_ci
7768c2ecf20Sopenharmony_cistatic int snd_wss_timer_stop(struct snd_timer *timer)
7778c2ecf20Sopenharmony_ci{
7788c2ecf20Sopenharmony_ci	unsigned long flags;
7798c2ecf20Sopenharmony_ci	struct snd_wss *chip = snd_timer_chip(timer);
7808c2ecf20Sopenharmony_ci	spin_lock_irqsave(&chip->reg_lock, flags);
7818c2ecf20Sopenharmony_ci	chip->image[CS4231_ALT_FEATURE_1] &= ~CS4231_TIMER_ENABLE;
7828c2ecf20Sopenharmony_ci	snd_wss_out(chip, CS4231_ALT_FEATURE_1,
7838c2ecf20Sopenharmony_ci		    chip->image[CS4231_ALT_FEATURE_1]);
7848c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&chip->reg_lock, flags);
7858c2ecf20Sopenharmony_ci	return 0;
7868c2ecf20Sopenharmony_ci}
7878c2ecf20Sopenharmony_ci
7888c2ecf20Sopenharmony_cistatic void snd_wss_init(struct snd_wss *chip)
7898c2ecf20Sopenharmony_ci{
7908c2ecf20Sopenharmony_ci	unsigned long flags;
7918c2ecf20Sopenharmony_ci
7928c2ecf20Sopenharmony_ci	snd_wss_calibrate_mute(chip, 1);
7938c2ecf20Sopenharmony_ci	snd_wss_mce_down(chip);
7948c2ecf20Sopenharmony_ci
7958c2ecf20Sopenharmony_ci#ifdef SNDRV_DEBUG_MCE
7968c2ecf20Sopenharmony_ci	snd_printk(KERN_DEBUG "init: (1)\n");
7978c2ecf20Sopenharmony_ci#endif
7988c2ecf20Sopenharmony_ci	snd_wss_mce_up(chip);
7998c2ecf20Sopenharmony_ci	spin_lock_irqsave(&chip->reg_lock, flags);
8008c2ecf20Sopenharmony_ci	chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_PLAYBACK_ENABLE |
8018c2ecf20Sopenharmony_ci					    CS4231_PLAYBACK_PIO |
8028c2ecf20Sopenharmony_ci					    CS4231_RECORD_ENABLE |
8038c2ecf20Sopenharmony_ci					    CS4231_RECORD_PIO |
8048c2ecf20Sopenharmony_ci					    CS4231_CALIB_MODE);
8058c2ecf20Sopenharmony_ci	chip->image[CS4231_IFACE_CTRL] |= CS4231_AUTOCALIB;
8068c2ecf20Sopenharmony_ci	snd_wss_out(chip, CS4231_IFACE_CTRL, chip->image[CS4231_IFACE_CTRL]);
8078c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&chip->reg_lock, flags);
8088c2ecf20Sopenharmony_ci	snd_wss_mce_down(chip);
8098c2ecf20Sopenharmony_ci
8108c2ecf20Sopenharmony_ci#ifdef SNDRV_DEBUG_MCE
8118c2ecf20Sopenharmony_ci	snd_printk(KERN_DEBUG "init: (2)\n");
8128c2ecf20Sopenharmony_ci#endif
8138c2ecf20Sopenharmony_ci
8148c2ecf20Sopenharmony_ci	snd_wss_mce_up(chip);
8158c2ecf20Sopenharmony_ci	spin_lock_irqsave(&chip->reg_lock, flags);
8168c2ecf20Sopenharmony_ci	chip->image[CS4231_IFACE_CTRL] &= ~CS4231_AUTOCALIB;
8178c2ecf20Sopenharmony_ci	snd_wss_out(chip, CS4231_IFACE_CTRL, chip->image[CS4231_IFACE_CTRL]);
8188c2ecf20Sopenharmony_ci	snd_wss_out(chip,
8198c2ecf20Sopenharmony_ci		    CS4231_ALT_FEATURE_1, chip->image[CS4231_ALT_FEATURE_1]);
8208c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&chip->reg_lock, flags);
8218c2ecf20Sopenharmony_ci	snd_wss_mce_down(chip);
8228c2ecf20Sopenharmony_ci
8238c2ecf20Sopenharmony_ci#ifdef SNDRV_DEBUG_MCE
8248c2ecf20Sopenharmony_ci	snd_printk(KERN_DEBUG "init: (3) - afei = 0x%x\n",
8258c2ecf20Sopenharmony_ci		   chip->image[CS4231_ALT_FEATURE_1]);
8268c2ecf20Sopenharmony_ci#endif
8278c2ecf20Sopenharmony_ci
8288c2ecf20Sopenharmony_ci	spin_lock_irqsave(&chip->reg_lock, flags);
8298c2ecf20Sopenharmony_ci	snd_wss_out(chip, CS4231_ALT_FEATURE_2,
8308c2ecf20Sopenharmony_ci		    chip->image[CS4231_ALT_FEATURE_2]);
8318c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&chip->reg_lock, flags);
8328c2ecf20Sopenharmony_ci
8338c2ecf20Sopenharmony_ci	snd_wss_mce_up(chip);
8348c2ecf20Sopenharmony_ci	spin_lock_irqsave(&chip->reg_lock, flags);
8358c2ecf20Sopenharmony_ci	snd_wss_out(chip, CS4231_PLAYBK_FORMAT,
8368c2ecf20Sopenharmony_ci		    chip->image[CS4231_PLAYBK_FORMAT]);
8378c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&chip->reg_lock, flags);
8388c2ecf20Sopenharmony_ci	snd_wss_mce_down(chip);
8398c2ecf20Sopenharmony_ci
8408c2ecf20Sopenharmony_ci#ifdef SNDRV_DEBUG_MCE
8418c2ecf20Sopenharmony_ci	snd_printk(KERN_DEBUG "init: (4)\n");
8428c2ecf20Sopenharmony_ci#endif
8438c2ecf20Sopenharmony_ci
8448c2ecf20Sopenharmony_ci	snd_wss_mce_up(chip);
8458c2ecf20Sopenharmony_ci	spin_lock_irqsave(&chip->reg_lock, flags);
8468c2ecf20Sopenharmony_ci	if (!(chip->hardware & WSS_HW_AD1848_MASK))
8478c2ecf20Sopenharmony_ci		snd_wss_out(chip, CS4231_REC_FORMAT,
8488c2ecf20Sopenharmony_ci			    chip->image[CS4231_REC_FORMAT]);
8498c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&chip->reg_lock, flags);
8508c2ecf20Sopenharmony_ci	snd_wss_mce_down(chip);
8518c2ecf20Sopenharmony_ci	snd_wss_calibrate_mute(chip, 0);
8528c2ecf20Sopenharmony_ci
8538c2ecf20Sopenharmony_ci#ifdef SNDRV_DEBUG_MCE
8548c2ecf20Sopenharmony_ci	snd_printk(KERN_DEBUG "init: (5)\n");
8558c2ecf20Sopenharmony_ci#endif
8568c2ecf20Sopenharmony_ci}
8578c2ecf20Sopenharmony_ci
8588c2ecf20Sopenharmony_cistatic int snd_wss_open(struct snd_wss *chip, unsigned int mode)
8598c2ecf20Sopenharmony_ci{
8608c2ecf20Sopenharmony_ci	unsigned long flags;
8618c2ecf20Sopenharmony_ci
8628c2ecf20Sopenharmony_ci	mutex_lock(&chip->open_mutex);
8638c2ecf20Sopenharmony_ci	if ((chip->mode & mode) ||
8648c2ecf20Sopenharmony_ci	    ((chip->mode & WSS_MODE_OPEN) && chip->single_dma)) {
8658c2ecf20Sopenharmony_ci		mutex_unlock(&chip->open_mutex);
8668c2ecf20Sopenharmony_ci		return -EAGAIN;
8678c2ecf20Sopenharmony_ci	}
8688c2ecf20Sopenharmony_ci	if (chip->mode & WSS_MODE_OPEN) {
8698c2ecf20Sopenharmony_ci		chip->mode |= mode;
8708c2ecf20Sopenharmony_ci		mutex_unlock(&chip->open_mutex);
8718c2ecf20Sopenharmony_ci		return 0;
8728c2ecf20Sopenharmony_ci	}
8738c2ecf20Sopenharmony_ci	/* ok. now enable and ack CODEC IRQ */
8748c2ecf20Sopenharmony_ci	spin_lock_irqsave(&chip->reg_lock, flags);
8758c2ecf20Sopenharmony_ci	if (!(chip->hardware & WSS_HW_AD1848_MASK)) {
8768c2ecf20Sopenharmony_ci		snd_wss_out(chip, CS4231_IRQ_STATUS,
8778c2ecf20Sopenharmony_ci			    CS4231_PLAYBACK_IRQ |
8788c2ecf20Sopenharmony_ci			    CS4231_RECORD_IRQ |
8798c2ecf20Sopenharmony_ci			    CS4231_TIMER_IRQ);
8808c2ecf20Sopenharmony_ci		snd_wss_out(chip, CS4231_IRQ_STATUS, 0);
8818c2ecf20Sopenharmony_ci	}
8828c2ecf20Sopenharmony_ci	wss_outb(chip, CS4231P(STATUS), 0);	/* clear IRQ */
8838c2ecf20Sopenharmony_ci	wss_outb(chip, CS4231P(STATUS), 0);	/* clear IRQ */
8848c2ecf20Sopenharmony_ci	chip->image[CS4231_PIN_CTRL] |= CS4231_IRQ_ENABLE;
8858c2ecf20Sopenharmony_ci	snd_wss_out(chip, CS4231_PIN_CTRL, chip->image[CS4231_PIN_CTRL]);
8868c2ecf20Sopenharmony_ci	if (!(chip->hardware & WSS_HW_AD1848_MASK)) {
8878c2ecf20Sopenharmony_ci		snd_wss_out(chip, CS4231_IRQ_STATUS,
8888c2ecf20Sopenharmony_ci			    CS4231_PLAYBACK_IRQ |
8898c2ecf20Sopenharmony_ci			    CS4231_RECORD_IRQ |
8908c2ecf20Sopenharmony_ci			    CS4231_TIMER_IRQ);
8918c2ecf20Sopenharmony_ci		snd_wss_out(chip, CS4231_IRQ_STATUS, 0);
8928c2ecf20Sopenharmony_ci	}
8938c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&chip->reg_lock, flags);
8948c2ecf20Sopenharmony_ci
8958c2ecf20Sopenharmony_ci	chip->mode = mode;
8968c2ecf20Sopenharmony_ci	mutex_unlock(&chip->open_mutex);
8978c2ecf20Sopenharmony_ci	return 0;
8988c2ecf20Sopenharmony_ci}
8998c2ecf20Sopenharmony_ci
9008c2ecf20Sopenharmony_cistatic void snd_wss_close(struct snd_wss *chip, unsigned int mode)
9018c2ecf20Sopenharmony_ci{
9028c2ecf20Sopenharmony_ci	unsigned long flags;
9038c2ecf20Sopenharmony_ci
9048c2ecf20Sopenharmony_ci	mutex_lock(&chip->open_mutex);
9058c2ecf20Sopenharmony_ci	chip->mode &= ~mode;
9068c2ecf20Sopenharmony_ci	if (chip->mode & WSS_MODE_OPEN) {
9078c2ecf20Sopenharmony_ci		mutex_unlock(&chip->open_mutex);
9088c2ecf20Sopenharmony_ci		return;
9098c2ecf20Sopenharmony_ci	}
9108c2ecf20Sopenharmony_ci	/* disable IRQ */
9118c2ecf20Sopenharmony_ci	spin_lock_irqsave(&chip->reg_lock, flags);
9128c2ecf20Sopenharmony_ci	if (!(chip->hardware & WSS_HW_AD1848_MASK))
9138c2ecf20Sopenharmony_ci		snd_wss_out(chip, CS4231_IRQ_STATUS, 0);
9148c2ecf20Sopenharmony_ci	wss_outb(chip, CS4231P(STATUS), 0);	/* clear IRQ */
9158c2ecf20Sopenharmony_ci	wss_outb(chip, CS4231P(STATUS), 0);	/* clear IRQ */
9168c2ecf20Sopenharmony_ci	chip->image[CS4231_PIN_CTRL] &= ~CS4231_IRQ_ENABLE;
9178c2ecf20Sopenharmony_ci	snd_wss_out(chip, CS4231_PIN_CTRL, chip->image[CS4231_PIN_CTRL]);
9188c2ecf20Sopenharmony_ci
9198c2ecf20Sopenharmony_ci	/* now disable record & playback */
9208c2ecf20Sopenharmony_ci
9218c2ecf20Sopenharmony_ci	if (chip->image[CS4231_IFACE_CTRL] & (CS4231_PLAYBACK_ENABLE | CS4231_PLAYBACK_PIO |
9228c2ecf20Sopenharmony_ci					       CS4231_RECORD_ENABLE | CS4231_RECORD_PIO)) {
9238c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&chip->reg_lock, flags);
9248c2ecf20Sopenharmony_ci		snd_wss_mce_up(chip);
9258c2ecf20Sopenharmony_ci		spin_lock_irqsave(&chip->reg_lock, flags);
9268c2ecf20Sopenharmony_ci		chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_PLAYBACK_ENABLE | CS4231_PLAYBACK_PIO |
9278c2ecf20Sopenharmony_ci						     CS4231_RECORD_ENABLE | CS4231_RECORD_PIO);
9288c2ecf20Sopenharmony_ci		snd_wss_out(chip, CS4231_IFACE_CTRL,
9298c2ecf20Sopenharmony_ci			    chip->image[CS4231_IFACE_CTRL]);
9308c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&chip->reg_lock, flags);
9318c2ecf20Sopenharmony_ci		snd_wss_mce_down(chip);
9328c2ecf20Sopenharmony_ci		spin_lock_irqsave(&chip->reg_lock, flags);
9338c2ecf20Sopenharmony_ci	}
9348c2ecf20Sopenharmony_ci
9358c2ecf20Sopenharmony_ci	/* clear IRQ again */
9368c2ecf20Sopenharmony_ci	if (!(chip->hardware & WSS_HW_AD1848_MASK))
9378c2ecf20Sopenharmony_ci		snd_wss_out(chip, CS4231_IRQ_STATUS, 0);
9388c2ecf20Sopenharmony_ci	wss_outb(chip, CS4231P(STATUS), 0);	/* clear IRQ */
9398c2ecf20Sopenharmony_ci	wss_outb(chip, CS4231P(STATUS), 0);	/* clear IRQ */
9408c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&chip->reg_lock, flags);
9418c2ecf20Sopenharmony_ci
9428c2ecf20Sopenharmony_ci	chip->mode = 0;
9438c2ecf20Sopenharmony_ci	mutex_unlock(&chip->open_mutex);
9448c2ecf20Sopenharmony_ci}
9458c2ecf20Sopenharmony_ci
9468c2ecf20Sopenharmony_ci/*
9478c2ecf20Sopenharmony_ci *  timer open/close
9488c2ecf20Sopenharmony_ci */
9498c2ecf20Sopenharmony_ci
9508c2ecf20Sopenharmony_cistatic int snd_wss_timer_open(struct snd_timer *timer)
9518c2ecf20Sopenharmony_ci{
9528c2ecf20Sopenharmony_ci	struct snd_wss *chip = snd_timer_chip(timer);
9538c2ecf20Sopenharmony_ci	snd_wss_open(chip, WSS_MODE_TIMER);
9548c2ecf20Sopenharmony_ci	return 0;
9558c2ecf20Sopenharmony_ci}
9568c2ecf20Sopenharmony_ci
9578c2ecf20Sopenharmony_cistatic int snd_wss_timer_close(struct snd_timer *timer)
9588c2ecf20Sopenharmony_ci{
9598c2ecf20Sopenharmony_ci	struct snd_wss *chip = snd_timer_chip(timer);
9608c2ecf20Sopenharmony_ci	snd_wss_close(chip, WSS_MODE_TIMER);
9618c2ecf20Sopenharmony_ci	return 0;
9628c2ecf20Sopenharmony_ci}
9638c2ecf20Sopenharmony_ci
9648c2ecf20Sopenharmony_cistatic const struct snd_timer_hardware snd_wss_timer_table =
9658c2ecf20Sopenharmony_ci{
9668c2ecf20Sopenharmony_ci	.flags =	SNDRV_TIMER_HW_AUTO,
9678c2ecf20Sopenharmony_ci	.resolution =	9945,
9688c2ecf20Sopenharmony_ci	.ticks =	65535,
9698c2ecf20Sopenharmony_ci	.open =		snd_wss_timer_open,
9708c2ecf20Sopenharmony_ci	.close =	snd_wss_timer_close,
9718c2ecf20Sopenharmony_ci	.c_resolution = snd_wss_timer_resolution,
9728c2ecf20Sopenharmony_ci	.start =	snd_wss_timer_start,
9738c2ecf20Sopenharmony_ci	.stop =		snd_wss_timer_stop,
9748c2ecf20Sopenharmony_ci};
9758c2ecf20Sopenharmony_ci
9768c2ecf20Sopenharmony_ci/*
9778c2ecf20Sopenharmony_ci *  ok.. exported functions..
9788c2ecf20Sopenharmony_ci */
9798c2ecf20Sopenharmony_ci
9808c2ecf20Sopenharmony_cistatic int snd_wss_playback_hw_params(struct snd_pcm_substream *substream,
9818c2ecf20Sopenharmony_ci					 struct snd_pcm_hw_params *hw_params)
9828c2ecf20Sopenharmony_ci{
9838c2ecf20Sopenharmony_ci	struct snd_wss *chip = snd_pcm_substream_chip(substream);
9848c2ecf20Sopenharmony_ci	unsigned char new_pdfr;
9858c2ecf20Sopenharmony_ci
9868c2ecf20Sopenharmony_ci	new_pdfr = snd_wss_get_format(chip, params_format(hw_params),
9878c2ecf20Sopenharmony_ci				params_channels(hw_params)) |
9888c2ecf20Sopenharmony_ci				snd_wss_get_rate(params_rate(hw_params));
9898c2ecf20Sopenharmony_ci	chip->set_playback_format(chip, hw_params, new_pdfr);
9908c2ecf20Sopenharmony_ci	return 0;
9918c2ecf20Sopenharmony_ci}
9928c2ecf20Sopenharmony_ci
9938c2ecf20Sopenharmony_cistatic int snd_wss_playback_prepare(struct snd_pcm_substream *substream)
9948c2ecf20Sopenharmony_ci{
9958c2ecf20Sopenharmony_ci	struct snd_wss *chip = snd_pcm_substream_chip(substream);
9968c2ecf20Sopenharmony_ci	struct snd_pcm_runtime *runtime = substream->runtime;
9978c2ecf20Sopenharmony_ci	unsigned long flags;
9988c2ecf20Sopenharmony_ci	unsigned int size = snd_pcm_lib_buffer_bytes(substream);
9998c2ecf20Sopenharmony_ci	unsigned int count = snd_pcm_lib_period_bytes(substream);
10008c2ecf20Sopenharmony_ci
10018c2ecf20Sopenharmony_ci	spin_lock_irqsave(&chip->reg_lock, flags);
10028c2ecf20Sopenharmony_ci	chip->p_dma_size = size;
10038c2ecf20Sopenharmony_ci	chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_PLAYBACK_ENABLE | CS4231_PLAYBACK_PIO);
10048c2ecf20Sopenharmony_ci	snd_dma_program(chip->dma1, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
10058c2ecf20Sopenharmony_ci	count = snd_wss_get_count(chip->image[CS4231_PLAYBK_FORMAT], count) - 1;
10068c2ecf20Sopenharmony_ci	snd_wss_out(chip, CS4231_PLY_LWR_CNT, (unsigned char) count);
10078c2ecf20Sopenharmony_ci	snd_wss_out(chip, CS4231_PLY_UPR_CNT, (unsigned char) (count >> 8));
10088c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&chip->reg_lock, flags);
10098c2ecf20Sopenharmony_ci#if 0
10108c2ecf20Sopenharmony_ci	snd_wss_debug(chip);
10118c2ecf20Sopenharmony_ci#endif
10128c2ecf20Sopenharmony_ci	return 0;
10138c2ecf20Sopenharmony_ci}
10148c2ecf20Sopenharmony_ci
10158c2ecf20Sopenharmony_cistatic int snd_wss_capture_hw_params(struct snd_pcm_substream *substream,
10168c2ecf20Sopenharmony_ci					struct snd_pcm_hw_params *hw_params)
10178c2ecf20Sopenharmony_ci{
10188c2ecf20Sopenharmony_ci	struct snd_wss *chip = snd_pcm_substream_chip(substream);
10198c2ecf20Sopenharmony_ci	unsigned char new_cdfr;
10208c2ecf20Sopenharmony_ci
10218c2ecf20Sopenharmony_ci	new_cdfr = snd_wss_get_format(chip, params_format(hw_params),
10228c2ecf20Sopenharmony_ci			   params_channels(hw_params)) |
10238c2ecf20Sopenharmony_ci			   snd_wss_get_rate(params_rate(hw_params));
10248c2ecf20Sopenharmony_ci	chip->set_capture_format(chip, hw_params, new_cdfr);
10258c2ecf20Sopenharmony_ci	return 0;
10268c2ecf20Sopenharmony_ci}
10278c2ecf20Sopenharmony_ci
10288c2ecf20Sopenharmony_cistatic int snd_wss_capture_prepare(struct snd_pcm_substream *substream)
10298c2ecf20Sopenharmony_ci{
10308c2ecf20Sopenharmony_ci	struct snd_wss *chip = snd_pcm_substream_chip(substream);
10318c2ecf20Sopenharmony_ci	struct snd_pcm_runtime *runtime = substream->runtime;
10328c2ecf20Sopenharmony_ci	unsigned long flags;
10338c2ecf20Sopenharmony_ci	unsigned int size = snd_pcm_lib_buffer_bytes(substream);
10348c2ecf20Sopenharmony_ci	unsigned int count = snd_pcm_lib_period_bytes(substream);
10358c2ecf20Sopenharmony_ci
10368c2ecf20Sopenharmony_ci	spin_lock_irqsave(&chip->reg_lock, flags);
10378c2ecf20Sopenharmony_ci	chip->c_dma_size = size;
10388c2ecf20Sopenharmony_ci	chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_RECORD_ENABLE | CS4231_RECORD_PIO);
10398c2ecf20Sopenharmony_ci	snd_dma_program(chip->dma2, runtime->dma_addr, size, DMA_MODE_READ | DMA_AUTOINIT);
10408c2ecf20Sopenharmony_ci	if (chip->hardware & WSS_HW_AD1848_MASK)
10418c2ecf20Sopenharmony_ci		count = snd_wss_get_count(chip->image[CS4231_PLAYBK_FORMAT],
10428c2ecf20Sopenharmony_ci					  count);
10438c2ecf20Sopenharmony_ci	else
10448c2ecf20Sopenharmony_ci		count = snd_wss_get_count(chip->image[CS4231_REC_FORMAT],
10458c2ecf20Sopenharmony_ci					  count);
10468c2ecf20Sopenharmony_ci	count--;
10478c2ecf20Sopenharmony_ci	if (chip->single_dma && chip->hardware != WSS_HW_INTERWAVE) {
10488c2ecf20Sopenharmony_ci		snd_wss_out(chip, CS4231_PLY_LWR_CNT, (unsigned char) count);
10498c2ecf20Sopenharmony_ci		snd_wss_out(chip, CS4231_PLY_UPR_CNT,
10508c2ecf20Sopenharmony_ci			    (unsigned char) (count >> 8));
10518c2ecf20Sopenharmony_ci	} else {
10528c2ecf20Sopenharmony_ci		snd_wss_out(chip, CS4231_REC_LWR_CNT, (unsigned char) count);
10538c2ecf20Sopenharmony_ci		snd_wss_out(chip, CS4231_REC_UPR_CNT,
10548c2ecf20Sopenharmony_ci			    (unsigned char) (count >> 8));
10558c2ecf20Sopenharmony_ci	}
10568c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&chip->reg_lock, flags);
10578c2ecf20Sopenharmony_ci	return 0;
10588c2ecf20Sopenharmony_ci}
10598c2ecf20Sopenharmony_ci
10608c2ecf20Sopenharmony_civoid snd_wss_overrange(struct snd_wss *chip)
10618c2ecf20Sopenharmony_ci{
10628c2ecf20Sopenharmony_ci	unsigned long flags;
10638c2ecf20Sopenharmony_ci	unsigned char res;
10648c2ecf20Sopenharmony_ci
10658c2ecf20Sopenharmony_ci	spin_lock_irqsave(&chip->reg_lock, flags);
10668c2ecf20Sopenharmony_ci	res = snd_wss_in(chip, CS4231_TEST_INIT);
10678c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&chip->reg_lock, flags);
10688c2ecf20Sopenharmony_ci	if (res & (0x08 | 0x02))	/* detect overrange only above 0dB; may be user selectable? */
10698c2ecf20Sopenharmony_ci		chip->capture_substream->runtime->overrange++;
10708c2ecf20Sopenharmony_ci}
10718c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_wss_overrange);
10728c2ecf20Sopenharmony_ci
10738c2ecf20Sopenharmony_ciirqreturn_t snd_wss_interrupt(int irq, void *dev_id)
10748c2ecf20Sopenharmony_ci{
10758c2ecf20Sopenharmony_ci	struct snd_wss *chip = dev_id;
10768c2ecf20Sopenharmony_ci	unsigned char status;
10778c2ecf20Sopenharmony_ci
10788c2ecf20Sopenharmony_ci	if (chip->hardware & WSS_HW_AD1848_MASK)
10798c2ecf20Sopenharmony_ci		/* pretend it was the only possible irq for AD1848 */
10808c2ecf20Sopenharmony_ci		status = CS4231_PLAYBACK_IRQ;
10818c2ecf20Sopenharmony_ci	else
10828c2ecf20Sopenharmony_ci		status = snd_wss_in(chip, CS4231_IRQ_STATUS);
10838c2ecf20Sopenharmony_ci	if (status & CS4231_TIMER_IRQ) {
10848c2ecf20Sopenharmony_ci		if (chip->timer)
10858c2ecf20Sopenharmony_ci			snd_timer_interrupt(chip->timer, chip->timer->sticks);
10868c2ecf20Sopenharmony_ci	}
10878c2ecf20Sopenharmony_ci	if (chip->single_dma && chip->hardware != WSS_HW_INTERWAVE) {
10888c2ecf20Sopenharmony_ci		if (status & CS4231_PLAYBACK_IRQ) {
10898c2ecf20Sopenharmony_ci			if (chip->mode & WSS_MODE_PLAY) {
10908c2ecf20Sopenharmony_ci				if (chip->playback_substream)
10918c2ecf20Sopenharmony_ci					snd_pcm_period_elapsed(chip->playback_substream);
10928c2ecf20Sopenharmony_ci			}
10938c2ecf20Sopenharmony_ci			if (chip->mode & WSS_MODE_RECORD) {
10948c2ecf20Sopenharmony_ci				if (chip->capture_substream) {
10958c2ecf20Sopenharmony_ci					snd_wss_overrange(chip);
10968c2ecf20Sopenharmony_ci					snd_pcm_period_elapsed(chip->capture_substream);
10978c2ecf20Sopenharmony_ci				}
10988c2ecf20Sopenharmony_ci			}
10998c2ecf20Sopenharmony_ci		}
11008c2ecf20Sopenharmony_ci	} else {
11018c2ecf20Sopenharmony_ci		if (status & CS4231_PLAYBACK_IRQ) {
11028c2ecf20Sopenharmony_ci			if (chip->playback_substream)
11038c2ecf20Sopenharmony_ci				snd_pcm_period_elapsed(chip->playback_substream);
11048c2ecf20Sopenharmony_ci		}
11058c2ecf20Sopenharmony_ci		if (status & CS4231_RECORD_IRQ) {
11068c2ecf20Sopenharmony_ci			if (chip->capture_substream) {
11078c2ecf20Sopenharmony_ci				snd_wss_overrange(chip);
11088c2ecf20Sopenharmony_ci				snd_pcm_period_elapsed(chip->capture_substream);
11098c2ecf20Sopenharmony_ci			}
11108c2ecf20Sopenharmony_ci		}
11118c2ecf20Sopenharmony_ci	}
11128c2ecf20Sopenharmony_ci
11138c2ecf20Sopenharmony_ci	spin_lock(&chip->reg_lock);
11148c2ecf20Sopenharmony_ci	status = ~CS4231_ALL_IRQS | ~status;
11158c2ecf20Sopenharmony_ci	if (chip->hardware & WSS_HW_AD1848_MASK)
11168c2ecf20Sopenharmony_ci		wss_outb(chip, CS4231P(STATUS), 0);
11178c2ecf20Sopenharmony_ci	else
11188c2ecf20Sopenharmony_ci		snd_wss_out(chip, CS4231_IRQ_STATUS, status);
11198c2ecf20Sopenharmony_ci	spin_unlock(&chip->reg_lock);
11208c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
11218c2ecf20Sopenharmony_ci}
11228c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_wss_interrupt);
11238c2ecf20Sopenharmony_ci
11248c2ecf20Sopenharmony_cistatic snd_pcm_uframes_t snd_wss_playback_pointer(struct snd_pcm_substream *substream)
11258c2ecf20Sopenharmony_ci{
11268c2ecf20Sopenharmony_ci	struct snd_wss *chip = snd_pcm_substream_chip(substream);
11278c2ecf20Sopenharmony_ci	size_t ptr;
11288c2ecf20Sopenharmony_ci
11298c2ecf20Sopenharmony_ci	if (!(chip->image[CS4231_IFACE_CTRL] & CS4231_PLAYBACK_ENABLE))
11308c2ecf20Sopenharmony_ci		return 0;
11318c2ecf20Sopenharmony_ci	ptr = snd_dma_pointer(chip->dma1, chip->p_dma_size);
11328c2ecf20Sopenharmony_ci	return bytes_to_frames(substream->runtime, ptr);
11338c2ecf20Sopenharmony_ci}
11348c2ecf20Sopenharmony_ci
11358c2ecf20Sopenharmony_cistatic snd_pcm_uframes_t snd_wss_capture_pointer(struct snd_pcm_substream *substream)
11368c2ecf20Sopenharmony_ci{
11378c2ecf20Sopenharmony_ci	struct snd_wss *chip = snd_pcm_substream_chip(substream);
11388c2ecf20Sopenharmony_ci	size_t ptr;
11398c2ecf20Sopenharmony_ci
11408c2ecf20Sopenharmony_ci	if (!(chip->image[CS4231_IFACE_CTRL] & CS4231_RECORD_ENABLE))
11418c2ecf20Sopenharmony_ci		return 0;
11428c2ecf20Sopenharmony_ci	ptr = snd_dma_pointer(chip->dma2, chip->c_dma_size);
11438c2ecf20Sopenharmony_ci	return bytes_to_frames(substream->runtime, ptr);
11448c2ecf20Sopenharmony_ci}
11458c2ecf20Sopenharmony_ci
11468c2ecf20Sopenharmony_ci/*
11478c2ecf20Sopenharmony_ci
11488c2ecf20Sopenharmony_ci */
11498c2ecf20Sopenharmony_ci
11508c2ecf20Sopenharmony_cistatic int snd_ad1848_probe(struct snd_wss *chip)
11518c2ecf20Sopenharmony_ci{
11528c2ecf20Sopenharmony_ci	unsigned long timeout = jiffies + msecs_to_jiffies(1000);
11538c2ecf20Sopenharmony_ci	unsigned long flags;
11548c2ecf20Sopenharmony_ci	unsigned char r;
11558c2ecf20Sopenharmony_ci	unsigned short hardware = 0;
11568c2ecf20Sopenharmony_ci	int err = 0;
11578c2ecf20Sopenharmony_ci	int i;
11588c2ecf20Sopenharmony_ci
11598c2ecf20Sopenharmony_ci	while (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT) {
11608c2ecf20Sopenharmony_ci		if (time_after(jiffies, timeout))
11618c2ecf20Sopenharmony_ci			return -ENODEV;
11628c2ecf20Sopenharmony_ci		cond_resched();
11638c2ecf20Sopenharmony_ci	}
11648c2ecf20Sopenharmony_ci	spin_lock_irqsave(&chip->reg_lock, flags);
11658c2ecf20Sopenharmony_ci
11668c2ecf20Sopenharmony_ci	/* set CS423x MODE 1 */
11678c2ecf20Sopenharmony_ci	snd_wss_dout(chip, CS4231_MISC_INFO, 0);
11688c2ecf20Sopenharmony_ci
11698c2ecf20Sopenharmony_ci	snd_wss_dout(chip, CS4231_RIGHT_INPUT, 0x45); /* 0x55 & ~0x10 */
11708c2ecf20Sopenharmony_ci	r = snd_wss_in(chip, CS4231_RIGHT_INPUT);
11718c2ecf20Sopenharmony_ci	if (r != 0x45) {
11728c2ecf20Sopenharmony_ci		/* RMGE always high on AD1847 */
11738c2ecf20Sopenharmony_ci		if ((r & ~CS4231_ENABLE_MIC_GAIN) != 0x45) {
11748c2ecf20Sopenharmony_ci			err = -ENODEV;
11758c2ecf20Sopenharmony_ci			goto out;
11768c2ecf20Sopenharmony_ci		}
11778c2ecf20Sopenharmony_ci		hardware = WSS_HW_AD1847;
11788c2ecf20Sopenharmony_ci	} else {
11798c2ecf20Sopenharmony_ci		snd_wss_dout(chip, CS4231_LEFT_INPUT,  0xaa);
11808c2ecf20Sopenharmony_ci		r = snd_wss_in(chip, CS4231_LEFT_INPUT);
11818c2ecf20Sopenharmony_ci		/* L/RMGE always low on AT2320 */
11828c2ecf20Sopenharmony_ci		if ((r | CS4231_ENABLE_MIC_GAIN) != 0xaa) {
11838c2ecf20Sopenharmony_ci			err = -ENODEV;
11848c2ecf20Sopenharmony_ci			goto out;
11858c2ecf20Sopenharmony_ci		}
11868c2ecf20Sopenharmony_ci	}
11878c2ecf20Sopenharmony_ci
11888c2ecf20Sopenharmony_ci	/* clear pending IRQ */
11898c2ecf20Sopenharmony_ci	wss_inb(chip, CS4231P(STATUS));
11908c2ecf20Sopenharmony_ci	wss_outb(chip, CS4231P(STATUS), 0);
11918c2ecf20Sopenharmony_ci	mb();
11928c2ecf20Sopenharmony_ci
11938c2ecf20Sopenharmony_ci	if ((chip->hardware & WSS_HW_TYPE_MASK) != WSS_HW_DETECT)
11948c2ecf20Sopenharmony_ci		goto out;
11958c2ecf20Sopenharmony_ci
11968c2ecf20Sopenharmony_ci	if (hardware) {
11978c2ecf20Sopenharmony_ci		chip->hardware = hardware;
11988c2ecf20Sopenharmony_ci		goto out;
11998c2ecf20Sopenharmony_ci	}
12008c2ecf20Sopenharmony_ci
12018c2ecf20Sopenharmony_ci	r = snd_wss_in(chip, CS4231_MISC_INFO);
12028c2ecf20Sopenharmony_ci
12038c2ecf20Sopenharmony_ci	/* set CS423x MODE 2 */
12048c2ecf20Sopenharmony_ci	snd_wss_dout(chip, CS4231_MISC_INFO, CS4231_MODE2);
12058c2ecf20Sopenharmony_ci	for (i = 0; i < 16; i++) {
12068c2ecf20Sopenharmony_ci		if (snd_wss_in(chip, i) != snd_wss_in(chip, 16 + i)) {
12078c2ecf20Sopenharmony_ci			/* we have more than 16 registers: check ID */
12088c2ecf20Sopenharmony_ci			if ((r & 0xf) != 0xa)
12098c2ecf20Sopenharmony_ci				goto out_mode;
12108c2ecf20Sopenharmony_ci			/*
12118c2ecf20Sopenharmony_ci			 * on CMI8330, CS4231_VERSION is volume control and
12128c2ecf20Sopenharmony_ci			 * can be set to 0
12138c2ecf20Sopenharmony_ci			 */
12148c2ecf20Sopenharmony_ci			snd_wss_dout(chip, CS4231_VERSION, 0);
12158c2ecf20Sopenharmony_ci			r = snd_wss_in(chip, CS4231_VERSION) & 0xe7;
12168c2ecf20Sopenharmony_ci			if (!r)
12178c2ecf20Sopenharmony_ci				chip->hardware = WSS_HW_CMI8330;
12188c2ecf20Sopenharmony_ci			goto out_mode;
12198c2ecf20Sopenharmony_ci		}
12208c2ecf20Sopenharmony_ci	}
12218c2ecf20Sopenharmony_ci	if (r & 0x80)
12228c2ecf20Sopenharmony_ci		chip->hardware = WSS_HW_CS4248;
12238c2ecf20Sopenharmony_ci	else
12248c2ecf20Sopenharmony_ci		chip->hardware = WSS_HW_AD1848;
12258c2ecf20Sopenharmony_ciout_mode:
12268c2ecf20Sopenharmony_ci	snd_wss_dout(chip, CS4231_MISC_INFO, 0);
12278c2ecf20Sopenharmony_ciout:
12288c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&chip->reg_lock, flags);
12298c2ecf20Sopenharmony_ci	return err;
12308c2ecf20Sopenharmony_ci}
12318c2ecf20Sopenharmony_ci
12328c2ecf20Sopenharmony_cistatic int snd_wss_probe(struct snd_wss *chip)
12338c2ecf20Sopenharmony_ci{
12348c2ecf20Sopenharmony_ci	unsigned long flags;
12358c2ecf20Sopenharmony_ci	int i, id, rev, regnum;
12368c2ecf20Sopenharmony_ci	unsigned char *ptr;
12378c2ecf20Sopenharmony_ci	unsigned int hw;
12388c2ecf20Sopenharmony_ci
12398c2ecf20Sopenharmony_ci	id = snd_ad1848_probe(chip);
12408c2ecf20Sopenharmony_ci	if (id < 0)
12418c2ecf20Sopenharmony_ci		return id;
12428c2ecf20Sopenharmony_ci
12438c2ecf20Sopenharmony_ci	hw = chip->hardware;
12448c2ecf20Sopenharmony_ci	if ((hw & WSS_HW_TYPE_MASK) == WSS_HW_DETECT) {
12458c2ecf20Sopenharmony_ci		for (i = 0; i < 50; i++) {
12468c2ecf20Sopenharmony_ci			mb();
12478c2ecf20Sopenharmony_ci			if (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT)
12488c2ecf20Sopenharmony_ci				msleep(2);
12498c2ecf20Sopenharmony_ci			else {
12508c2ecf20Sopenharmony_ci				spin_lock_irqsave(&chip->reg_lock, flags);
12518c2ecf20Sopenharmony_ci				snd_wss_out(chip, CS4231_MISC_INFO,
12528c2ecf20Sopenharmony_ci					    CS4231_MODE2);
12538c2ecf20Sopenharmony_ci				id = snd_wss_in(chip, CS4231_MISC_INFO) & 0x0f;
12548c2ecf20Sopenharmony_ci				spin_unlock_irqrestore(&chip->reg_lock, flags);
12558c2ecf20Sopenharmony_ci				if (id == 0x0a)
12568c2ecf20Sopenharmony_ci					break;	/* this is valid value */
12578c2ecf20Sopenharmony_ci			}
12588c2ecf20Sopenharmony_ci		}
12598c2ecf20Sopenharmony_ci		snd_printdd("wss: port = 0x%lx, id = 0x%x\n", chip->port, id);
12608c2ecf20Sopenharmony_ci		if (id != 0x0a)
12618c2ecf20Sopenharmony_ci			return -ENODEV;	/* no valid device found */
12628c2ecf20Sopenharmony_ci
12638c2ecf20Sopenharmony_ci		rev = snd_wss_in(chip, CS4231_VERSION) & 0xe7;
12648c2ecf20Sopenharmony_ci		snd_printdd("CS4231: VERSION (I25) = 0x%x\n", rev);
12658c2ecf20Sopenharmony_ci		if (rev == 0x80) {
12668c2ecf20Sopenharmony_ci			unsigned char tmp = snd_wss_in(chip, 23);
12678c2ecf20Sopenharmony_ci			snd_wss_out(chip, 23, ~tmp);
12688c2ecf20Sopenharmony_ci			if (snd_wss_in(chip, 23) != tmp)
12698c2ecf20Sopenharmony_ci				chip->hardware = WSS_HW_AD1845;
12708c2ecf20Sopenharmony_ci			else
12718c2ecf20Sopenharmony_ci				chip->hardware = WSS_HW_CS4231;
12728c2ecf20Sopenharmony_ci		} else if (rev == 0xa0) {
12738c2ecf20Sopenharmony_ci			chip->hardware = WSS_HW_CS4231A;
12748c2ecf20Sopenharmony_ci		} else if (rev == 0xa2) {
12758c2ecf20Sopenharmony_ci			chip->hardware = WSS_HW_CS4232;
12768c2ecf20Sopenharmony_ci		} else if (rev == 0xb2) {
12778c2ecf20Sopenharmony_ci			chip->hardware = WSS_HW_CS4232A;
12788c2ecf20Sopenharmony_ci		} else if (rev == 0x83) {
12798c2ecf20Sopenharmony_ci			chip->hardware = WSS_HW_CS4236;
12808c2ecf20Sopenharmony_ci		} else if (rev == 0x03) {
12818c2ecf20Sopenharmony_ci			chip->hardware = WSS_HW_CS4236B;
12828c2ecf20Sopenharmony_ci		} else {
12838c2ecf20Sopenharmony_ci			snd_printk(KERN_ERR
12848c2ecf20Sopenharmony_ci				   "unknown CS chip with version 0x%x\n", rev);
12858c2ecf20Sopenharmony_ci			return -ENODEV;		/* unknown CS4231 chip? */
12868c2ecf20Sopenharmony_ci		}
12878c2ecf20Sopenharmony_ci	}
12888c2ecf20Sopenharmony_ci	spin_lock_irqsave(&chip->reg_lock, flags);
12898c2ecf20Sopenharmony_ci	wss_inb(chip, CS4231P(STATUS));	/* clear any pendings IRQ */
12908c2ecf20Sopenharmony_ci	wss_outb(chip, CS4231P(STATUS), 0);
12918c2ecf20Sopenharmony_ci	mb();
12928c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&chip->reg_lock, flags);
12938c2ecf20Sopenharmony_ci
12948c2ecf20Sopenharmony_ci	if (!(chip->hardware & WSS_HW_AD1848_MASK))
12958c2ecf20Sopenharmony_ci		chip->image[CS4231_MISC_INFO] = CS4231_MODE2;
12968c2ecf20Sopenharmony_ci	switch (chip->hardware) {
12978c2ecf20Sopenharmony_ci	case WSS_HW_INTERWAVE:
12988c2ecf20Sopenharmony_ci		chip->image[CS4231_MISC_INFO] = CS4231_IW_MODE3;
12998c2ecf20Sopenharmony_ci		break;
13008c2ecf20Sopenharmony_ci	case WSS_HW_CS4235:
13018c2ecf20Sopenharmony_ci	case WSS_HW_CS4236B:
13028c2ecf20Sopenharmony_ci	case WSS_HW_CS4237B:
13038c2ecf20Sopenharmony_ci	case WSS_HW_CS4238B:
13048c2ecf20Sopenharmony_ci	case WSS_HW_CS4239:
13058c2ecf20Sopenharmony_ci		if (hw == WSS_HW_DETECT3)
13068c2ecf20Sopenharmony_ci			chip->image[CS4231_MISC_INFO] = CS4231_4236_MODE3;
13078c2ecf20Sopenharmony_ci		else
13088c2ecf20Sopenharmony_ci			chip->hardware = WSS_HW_CS4236;
13098c2ecf20Sopenharmony_ci		break;
13108c2ecf20Sopenharmony_ci	}
13118c2ecf20Sopenharmony_ci
13128c2ecf20Sopenharmony_ci	chip->image[CS4231_IFACE_CTRL] =
13138c2ecf20Sopenharmony_ci	    (chip->image[CS4231_IFACE_CTRL] & ~CS4231_SINGLE_DMA) |
13148c2ecf20Sopenharmony_ci	    (chip->single_dma ? CS4231_SINGLE_DMA : 0);
13158c2ecf20Sopenharmony_ci	if (chip->hardware != WSS_HW_OPTI93X) {
13168c2ecf20Sopenharmony_ci		chip->image[CS4231_ALT_FEATURE_1] = 0x80;
13178c2ecf20Sopenharmony_ci		chip->image[CS4231_ALT_FEATURE_2] =
13188c2ecf20Sopenharmony_ci			chip->hardware == WSS_HW_INTERWAVE ? 0xc2 : 0x01;
13198c2ecf20Sopenharmony_ci	}
13208c2ecf20Sopenharmony_ci	/* enable fine grained frequency selection */
13218c2ecf20Sopenharmony_ci	if (chip->hardware == WSS_HW_AD1845)
13228c2ecf20Sopenharmony_ci		chip->image[AD1845_PWR_DOWN] = 8;
13238c2ecf20Sopenharmony_ci
13248c2ecf20Sopenharmony_ci	ptr = (unsigned char *) &chip->image;
13258c2ecf20Sopenharmony_ci	regnum = (chip->hardware & WSS_HW_AD1848_MASK) ? 16 : 32;
13268c2ecf20Sopenharmony_ci	snd_wss_mce_down(chip);
13278c2ecf20Sopenharmony_ci	spin_lock_irqsave(&chip->reg_lock, flags);
13288c2ecf20Sopenharmony_ci	for (i = 0; i < regnum; i++)	/* ok.. fill all registers */
13298c2ecf20Sopenharmony_ci		snd_wss_out(chip, i, *ptr++);
13308c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&chip->reg_lock, flags);
13318c2ecf20Sopenharmony_ci	snd_wss_mce_up(chip);
13328c2ecf20Sopenharmony_ci	snd_wss_mce_down(chip);
13338c2ecf20Sopenharmony_ci
13348c2ecf20Sopenharmony_ci	mdelay(2);
13358c2ecf20Sopenharmony_ci
13368c2ecf20Sopenharmony_ci	/* ok.. try check hardware version for CS4236+ chips */
13378c2ecf20Sopenharmony_ci	if ((hw & WSS_HW_TYPE_MASK) == WSS_HW_DETECT) {
13388c2ecf20Sopenharmony_ci		if (chip->hardware == WSS_HW_CS4236B) {
13398c2ecf20Sopenharmony_ci			rev = snd_cs4236_ext_in(chip, CS4236_VERSION);
13408c2ecf20Sopenharmony_ci			snd_cs4236_ext_out(chip, CS4236_VERSION, 0xff);
13418c2ecf20Sopenharmony_ci			id = snd_cs4236_ext_in(chip, CS4236_VERSION);
13428c2ecf20Sopenharmony_ci			snd_cs4236_ext_out(chip, CS4236_VERSION, rev);
13438c2ecf20Sopenharmony_ci			snd_printdd("CS4231: ext version; rev = 0x%x, id = 0x%x\n", rev, id);
13448c2ecf20Sopenharmony_ci			if ((id & 0x1f) == 0x1d) {	/* CS4235 */
13458c2ecf20Sopenharmony_ci				chip->hardware = WSS_HW_CS4235;
13468c2ecf20Sopenharmony_ci				switch (id >> 5) {
13478c2ecf20Sopenharmony_ci				case 4:
13488c2ecf20Sopenharmony_ci				case 5:
13498c2ecf20Sopenharmony_ci				case 6:
13508c2ecf20Sopenharmony_ci					break;
13518c2ecf20Sopenharmony_ci				default:
13528c2ecf20Sopenharmony_ci					snd_printk(KERN_WARNING
13538c2ecf20Sopenharmony_ci						"unknown CS4235 chip "
13548c2ecf20Sopenharmony_ci						"(enhanced version = 0x%x)\n",
13558c2ecf20Sopenharmony_ci						id);
13568c2ecf20Sopenharmony_ci				}
13578c2ecf20Sopenharmony_ci			} else if ((id & 0x1f) == 0x0b) {	/* CS4236/B */
13588c2ecf20Sopenharmony_ci				switch (id >> 5) {
13598c2ecf20Sopenharmony_ci				case 4:
13608c2ecf20Sopenharmony_ci				case 5:
13618c2ecf20Sopenharmony_ci				case 6:
13628c2ecf20Sopenharmony_ci				case 7:
13638c2ecf20Sopenharmony_ci					chip->hardware = WSS_HW_CS4236B;
13648c2ecf20Sopenharmony_ci					break;
13658c2ecf20Sopenharmony_ci				default:
13668c2ecf20Sopenharmony_ci					snd_printk(KERN_WARNING
13678c2ecf20Sopenharmony_ci						"unknown CS4236 chip "
13688c2ecf20Sopenharmony_ci						"(enhanced version = 0x%x)\n",
13698c2ecf20Sopenharmony_ci						id);
13708c2ecf20Sopenharmony_ci				}
13718c2ecf20Sopenharmony_ci			} else if ((id & 0x1f) == 0x08) {	/* CS4237B */
13728c2ecf20Sopenharmony_ci				chip->hardware = WSS_HW_CS4237B;
13738c2ecf20Sopenharmony_ci				switch (id >> 5) {
13748c2ecf20Sopenharmony_ci				case 4:
13758c2ecf20Sopenharmony_ci				case 5:
13768c2ecf20Sopenharmony_ci				case 6:
13778c2ecf20Sopenharmony_ci				case 7:
13788c2ecf20Sopenharmony_ci					break;
13798c2ecf20Sopenharmony_ci				default:
13808c2ecf20Sopenharmony_ci					snd_printk(KERN_WARNING
13818c2ecf20Sopenharmony_ci						"unknown CS4237B chip "
13828c2ecf20Sopenharmony_ci						"(enhanced version = 0x%x)\n",
13838c2ecf20Sopenharmony_ci						id);
13848c2ecf20Sopenharmony_ci				}
13858c2ecf20Sopenharmony_ci			} else if ((id & 0x1f) == 0x09) {	/* CS4238B */
13868c2ecf20Sopenharmony_ci				chip->hardware = WSS_HW_CS4238B;
13878c2ecf20Sopenharmony_ci				switch (id >> 5) {
13888c2ecf20Sopenharmony_ci				case 5:
13898c2ecf20Sopenharmony_ci				case 6:
13908c2ecf20Sopenharmony_ci				case 7:
13918c2ecf20Sopenharmony_ci					break;
13928c2ecf20Sopenharmony_ci				default:
13938c2ecf20Sopenharmony_ci					snd_printk(KERN_WARNING
13948c2ecf20Sopenharmony_ci						"unknown CS4238B chip "
13958c2ecf20Sopenharmony_ci						"(enhanced version = 0x%x)\n",
13968c2ecf20Sopenharmony_ci						id);
13978c2ecf20Sopenharmony_ci				}
13988c2ecf20Sopenharmony_ci			} else if ((id & 0x1f) == 0x1e) {	/* CS4239 */
13998c2ecf20Sopenharmony_ci				chip->hardware = WSS_HW_CS4239;
14008c2ecf20Sopenharmony_ci				switch (id >> 5) {
14018c2ecf20Sopenharmony_ci				case 4:
14028c2ecf20Sopenharmony_ci				case 5:
14038c2ecf20Sopenharmony_ci				case 6:
14048c2ecf20Sopenharmony_ci					break;
14058c2ecf20Sopenharmony_ci				default:
14068c2ecf20Sopenharmony_ci					snd_printk(KERN_WARNING
14078c2ecf20Sopenharmony_ci						"unknown CS4239 chip "
14088c2ecf20Sopenharmony_ci						"(enhanced version = 0x%x)\n",
14098c2ecf20Sopenharmony_ci						id);
14108c2ecf20Sopenharmony_ci				}
14118c2ecf20Sopenharmony_ci			} else {
14128c2ecf20Sopenharmony_ci				snd_printk(KERN_WARNING
14138c2ecf20Sopenharmony_ci					   "unknown CS4236/CS423xB chip "
14148c2ecf20Sopenharmony_ci					   "(enhanced version = 0x%x)\n", id);
14158c2ecf20Sopenharmony_ci			}
14168c2ecf20Sopenharmony_ci		}
14178c2ecf20Sopenharmony_ci	}
14188c2ecf20Sopenharmony_ci	return 0;		/* all things are ok.. */
14198c2ecf20Sopenharmony_ci}
14208c2ecf20Sopenharmony_ci
14218c2ecf20Sopenharmony_ci/*
14228c2ecf20Sopenharmony_ci
14238c2ecf20Sopenharmony_ci */
14248c2ecf20Sopenharmony_ci
14258c2ecf20Sopenharmony_cistatic const struct snd_pcm_hardware snd_wss_playback =
14268c2ecf20Sopenharmony_ci{
14278c2ecf20Sopenharmony_ci	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
14288c2ecf20Sopenharmony_ci				 SNDRV_PCM_INFO_MMAP_VALID |
14298c2ecf20Sopenharmony_ci				 SNDRV_PCM_INFO_SYNC_START),
14308c2ecf20Sopenharmony_ci	.formats =		(SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW | SNDRV_PCM_FMTBIT_IMA_ADPCM |
14318c2ecf20Sopenharmony_ci				 SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE),
14328c2ecf20Sopenharmony_ci	.rates =		SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
14338c2ecf20Sopenharmony_ci	.rate_min =		5510,
14348c2ecf20Sopenharmony_ci	.rate_max =		48000,
14358c2ecf20Sopenharmony_ci	.channels_min =		1,
14368c2ecf20Sopenharmony_ci	.channels_max =		2,
14378c2ecf20Sopenharmony_ci	.buffer_bytes_max =	(128*1024),
14388c2ecf20Sopenharmony_ci	.period_bytes_min =	64,
14398c2ecf20Sopenharmony_ci	.period_bytes_max =	(128*1024),
14408c2ecf20Sopenharmony_ci	.periods_min =		1,
14418c2ecf20Sopenharmony_ci	.periods_max =		1024,
14428c2ecf20Sopenharmony_ci	.fifo_size =		0,
14438c2ecf20Sopenharmony_ci};
14448c2ecf20Sopenharmony_ci
14458c2ecf20Sopenharmony_cistatic const struct snd_pcm_hardware snd_wss_capture =
14468c2ecf20Sopenharmony_ci{
14478c2ecf20Sopenharmony_ci	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
14488c2ecf20Sopenharmony_ci				 SNDRV_PCM_INFO_MMAP_VALID |
14498c2ecf20Sopenharmony_ci				 SNDRV_PCM_INFO_RESUME |
14508c2ecf20Sopenharmony_ci				 SNDRV_PCM_INFO_SYNC_START),
14518c2ecf20Sopenharmony_ci	.formats =		(SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW | SNDRV_PCM_FMTBIT_IMA_ADPCM |
14528c2ecf20Sopenharmony_ci				 SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE),
14538c2ecf20Sopenharmony_ci	.rates =		SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
14548c2ecf20Sopenharmony_ci	.rate_min =		5510,
14558c2ecf20Sopenharmony_ci	.rate_max =		48000,
14568c2ecf20Sopenharmony_ci	.channels_min =		1,
14578c2ecf20Sopenharmony_ci	.channels_max =		2,
14588c2ecf20Sopenharmony_ci	.buffer_bytes_max =	(128*1024),
14598c2ecf20Sopenharmony_ci	.period_bytes_min =	64,
14608c2ecf20Sopenharmony_ci	.period_bytes_max =	(128*1024),
14618c2ecf20Sopenharmony_ci	.periods_min =		1,
14628c2ecf20Sopenharmony_ci	.periods_max =		1024,
14638c2ecf20Sopenharmony_ci	.fifo_size =		0,
14648c2ecf20Sopenharmony_ci};
14658c2ecf20Sopenharmony_ci
14668c2ecf20Sopenharmony_ci/*
14678c2ecf20Sopenharmony_ci
14688c2ecf20Sopenharmony_ci */
14698c2ecf20Sopenharmony_ci
14708c2ecf20Sopenharmony_cistatic int snd_wss_playback_open(struct snd_pcm_substream *substream)
14718c2ecf20Sopenharmony_ci{
14728c2ecf20Sopenharmony_ci	struct snd_wss *chip = snd_pcm_substream_chip(substream);
14738c2ecf20Sopenharmony_ci	struct snd_pcm_runtime *runtime = substream->runtime;
14748c2ecf20Sopenharmony_ci	int err;
14758c2ecf20Sopenharmony_ci
14768c2ecf20Sopenharmony_ci	runtime->hw = snd_wss_playback;
14778c2ecf20Sopenharmony_ci
14788c2ecf20Sopenharmony_ci	/* hardware limitation of older chipsets */
14798c2ecf20Sopenharmony_ci	if (chip->hardware & WSS_HW_AD1848_MASK)
14808c2ecf20Sopenharmony_ci		runtime->hw.formats &= ~(SNDRV_PCM_FMTBIT_IMA_ADPCM |
14818c2ecf20Sopenharmony_ci					 SNDRV_PCM_FMTBIT_S16_BE);
14828c2ecf20Sopenharmony_ci
14838c2ecf20Sopenharmony_ci	/* hardware bug in InterWave chipset */
14848c2ecf20Sopenharmony_ci	if (chip->hardware == WSS_HW_INTERWAVE && chip->dma1 > 3)
14858c2ecf20Sopenharmony_ci		runtime->hw.formats &= ~SNDRV_PCM_FMTBIT_MU_LAW;
14868c2ecf20Sopenharmony_ci
14878c2ecf20Sopenharmony_ci	/* hardware limitation of cheap chips */
14888c2ecf20Sopenharmony_ci	if (chip->hardware == WSS_HW_CS4235 ||
14898c2ecf20Sopenharmony_ci	    chip->hardware == WSS_HW_CS4239)
14908c2ecf20Sopenharmony_ci		runtime->hw.formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE;
14918c2ecf20Sopenharmony_ci
14928c2ecf20Sopenharmony_ci	snd_pcm_limit_isa_dma_size(chip->dma1, &runtime->hw.buffer_bytes_max);
14938c2ecf20Sopenharmony_ci	snd_pcm_limit_isa_dma_size(chip->dma1, &runtime->hw.period_bytes_max);
14948c2ecf20Sopenharmony_ci
14958c2ecf20Sopenharmony_ci	if (chip->claim_dma) {
14968c2ecf20Sopenharmony_ci		if ((err = chip->claim_dma(chip, chip->dma_private_data, chip->dma1)) < 0)
14978c2ecf20Sopenharmony_ci			return err;
14988c2ecf20Sopenharmony_ci	}
14998c2ecf20Sopenharmony_ci
15008c2ecf20Sopenharmony_ci	err = snd_wss_open(chip, WSS_MODE_PLAY);
15018c2ecf20Sopenharmony_ci	if (err < 0) {
15028c2ecf20Sopenharmony_ci		if (chip->release_dma)
15038c2ecf20Sopenharmony_ci			chip->release_dma(chip, chip->dma_private_data, chip->dma1);
15048c2ecf20Sopenharmony_ci		return err;
15058c2ecf20Sopenharmony_ci	}
15068c2ecf20Sopenharmony_ci	chip->playback_substream = substream;
15078c2ecf20Sopenharmony_ci	snd_pcm_set_sync(substream);
15088c2ecf20Sopenharmony_ci	chip->rate_constraint(runtime);
15098c2ecf20Sopenharmony_ci	return 0;
15108c2ecf20Sopenharmony_ci}
15118c2ecf20Sopenharmony_ci
15128c2ecf20Sopenharmony_cistatic int snd_wss_capture_open(struct snd_pcm_substream *substream)
15138c2ecf20Sopenharmony_ci{
15148c2ecf20Sopenharmony_ci	struct snd_wss *chip = snd_pcm_substream_chip(substream);
15158c2ecf20Sopenharmony_ci	struct snd_pcm_runtime *runtime = substream->runtime;
15168c2ecf20Sopenharmony_ci	int err;
15178c2ecf20Sopenharmony_ci
15188c2ecf20Sopenharmony_ci	runtime->hw = snd_wss_capture;
15198c2ecf20Sopenharmony_ci
15208c2ecf20Sopenharmony_ci	/* hardware limitation of older chipsets */
15218c2ecf20Sopenharmony_ci	if (chip->hardware & WSS_HW_AD1848_MASK)
15228c2ecf20Sopenharmony_ci		runtime->hw.formats &= ~(SNDRV_PCM_FMTBIT_IMA_ADPCM |
15238c2ecf20Sopenharmony_ci					 SNDRV_PCM_FMTBIT_S16_BE);
15248c2ecf20Sopenharmony_ci
15258c2ecf20Sopenharmony_ci	/* hardware limitation of cheap chips */
15268c2ecf20Sopenharmony_ci	if (chip->hardware == WSS_HW_CS4235 ||
15278c2ecf20Sopenharmony_ci	    chip->hardware == WSS_HW_CS4239 ||
15288c2ecf20Sopenharmony_ci	    chip->hardware == WSS_HW_OPTI93X)
15298c2ecf20Sopenharmony_ci		runtime->hw.formats = SNDRV_PCM_FMTBIT_U8 |
15308c2ecf20Sopenharmony_ci				      SNDRV_PCM_FMTBIT_S16_LE;
15318c2ecf20Sopenharmony_ci
15328c2ecf20Sopenharmony_ci	snd_pcm_limit_isa_dma_size(chip->dma2, &runtime->hw.buffer_bytes_max);
15338c2ecf20Sopenharmony_ci	snd_pcm_limit_isa_dma_size(chip->dma2, &runtime->hw.period_bytes_max);
15348c2ecf20Sopenharmony_ci
15358c2ecf20Sopenharmony_ci	if (chip->claim_dma) {
15368c2ecf20Sopenharmony_ci		if ((err = chip->claim_dma(chip, chip->dma_private_data, chip->dma2)) < 0)
15378c2ecf20Sopenharmony_ci			return err;
15388c2ecf20Sopenharmony_ci	}
15398c2ecf20Sopenharmony_ci
15408c2ecf20Sopenharmony_ci	err = snd_wss_open(chip, WSS_MODE_RECORD);
15418c2ecf20Sopenharmony_ci	if (err < 0) {
15428c2ecf20Sopenharmony_ci		if (chip->release_dma)
15438c2ecf20Sopenharmony_ci			chip->release_dma(chip, chip->dma_private_data, chip->dma2);
15448c2ecf20Sopenharmony_ci		return err;
15458c2ecf20Sopenharmony_ci	}
15468c2ecf20Sopenharmony_ci	chip->capture_substream = substream;
15478c2ecf20Sopenharmony_ci	snd_pcm_set_sync(substream);
15488c2ecf20Sopenharmony_ci	chip->rate_constraint(runtime);
15498c2ecf20Sopenharmony_ci	return 0;
15508c2ecf20Sopenharmony_ci}
15518c2ecf20Sopenharmony_ci
15528c2ecf20Sopenharmony_cistatic int snd_wss_playback_close(struct snd_pcm_substream *substream)
15538c2ecf20Sopenharmony_ci{
15548c2ecf20Sopenharmony_ci	struct snd_wss *chip = snd_pcm_substream_chip(substream);
15558c2ecf20Sopenharmony_ci
15568c2ecf20Sopenharmony_ci	chip->playback_substream = NULL;
15578c2ecf20Sopenharmony_ci	snd_wss_close(chip, WSS_MODE_PLAY);
15588c2ecf20Sopenharmony_ci	return 0;
15598c2ecf20Sopenharmony_ci}
15608c2ecf20Sopenharmony_ci
15618c2ecf20Sopenharmony_cistatic int snd_wss_capture_close(struct snd_pcm_substream *substream)
15628c2ecf20Sopenharmony_ci{
15638c2ecf20Sopenharmony_ci	struct snd_wss *chip = snd_pcm_substream_chip(substream);
15648c2ecf20Sopenharmony_ci
15658c2ecf20Sopenharmony_ci	chip->capture_substream = NULL;
15668c2ecf20Sopenharmony_ci	snd_wss_close(chip, WSS_MODE_RECORD);
15678c2ecf20Sopenharmony_ci	return 0;
15688c2ecf20Sopenharmony_ci}
15698c2ecf20Sopenharmony_ci
15708c2ecf20Sopenharmony_cistatic void snd_wss_thinkpad_twiddle(struct snd_wss *chip, int on)
15718c2ecf20Sopenharmony_ci{
15728c2ecf20Sopenharmony_ci	int tmp;
15738c2ecf20Sopenharmony_ci
15748c2ecf20Sopenharmony_ci	if (!chip->thinkpad_flag)
15758c2ecf20Sopenharmony_ci		return;
15768c2ecf20Sopenharmony_ci
15778c2ecf20Sopenharmony_ci	outb(0x1c, AD1848_THINKPAD_CTL_PORT1);
15788c2ecf20Sopenharmony_ci	tmp = inb(AD1848_THINKPAD_CTL_PORT2);
15798c2ecf20Sopenharmony_ci
15808c2ecf20Sopenharmony_ci	if (on)
15818c2ecf20Sopenharmony_ci		/* turn it on */
15828c2ecf20Sopenharmony_ci		tmp |= AD1848_THINKPAD_CS4248_ENABLE_BIT;
15838c2ecf20Sopenharmony_ci	else
15848c2ecf20Sopenharmony_ci		/* turn it off */
15858c2ecf20Sopenharmony_ci		tmp &= ~AD1848_THINKPAD_CS4248_ENABLE_BIT;
15868c2ecf20Sopenharmony_ci
15878c2ecf20Sopenharmony_ci	outb(tmp, AD1848_THINKPAD_CTL_PORT2);
15888c2ecf20Sopenharmony_ci}
15898c2ecf20Sopenharmony_ci
15908c2ecf20Sopenharmony_ci#ifdef CONFIG_PM
15918c2ecf20Sopenharmony_ci
15928c2ecf20Sopenharmony_ci/* lowlevel suspend callback for CS4231 */
15938c2ecf20Sopenharmony_cistatic void snd_wss_suspend(struct snd_wss *chip)
15948c2ecf20Sopenharmony_ci{
15958c2ecf20Sopenharmony_ci	int reg;
15968c2ecf20Sopenharmony_ci	unsigned long flags;
15978c2ecf20Sopenharmony_ci
15988c2ecf20Sopenharmony_ci	spin_lock_irqsave(&chip->reg_lock, flags);
15998c2ecf20Sopenharmony_ci	for (reg = 0; reg < 32; reg++)
16008c2ecf20Sopenharmony_ci		chip->image[reg] = snd_wss_in(chip, reg);
16018c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&chip->reg_lock, flags);
16028c2ecf20Sopenharmony_ci	if (chip->thinkpad_flag)
16038c2ecf20Sopenharmony_ci		snd_wss_thinkpad_twiddle(chip, 0);
16048c2ecf20Sopenharmony_ci}
16058c2ecf20Sopenharmony_ci
16068c2ecf20Sopenharmony_ci/* lowlevel resume callback for CS4231 */
16078c2ecf20Sopenharmony_cistatic void snd_wss_resume(struct snd_wss *chip)
16088c2ecf20Sopenharmony_ci{
16098c2ecf20Sopenharmony_ci	int reg;
16108c2ecf20Sopenharmony_ci	unsigned long flags;
16118c2ecf20Sopenharmony_ci	/* int timeout; */
16128c2ecf20Sopenharmony_ci
16138c2ecf20Sopenharmony_ci	if (chip->thinkpad_flag)
16148c2ecf20Sopenharmony_ci		snd_wss_thinkpad_twiddle(chip, 1);
16158c2ecf20Sopenharmony_ci	snd_wss_mce_up(chip);
16168c2ecf20Sopenharmony_ci	spin_lock_irqsave(&chip->reg_lock, flags);
16178c2ecf20Sopenharmony_ci	for (reg = 0; reg < 32; reg++) {
16188c2ecf20Sopenharmony_ci		switch (reg) {
16198c2ecf20Sopenharmony_ci		case CS4231_VERSION:
16208c2ecf20Sopenharmony_ci			break;
16218c2ecf20Sopenharmony_ci		default:
16228c2ecf20Sopenharmony_ci			snd_wss_out(chip, reg, chip->image[reg]);
16238c2ecf20Sopenharmony_ci			break;
16248c2ecf20Sopenharmony_ci		}
16258c2ecf20Sopenharmony_ci	}
16268c2ecf20Sopenharmony_ci	/* Yamaha needs this to resume properly */
16278c2ecf20Sopenharmony_ci	if (chip->hardware == WSS_HW_OPL3SA2)
16288c2ecf20Sopenharmony_ci		snd_wss_out(chip, CS4231_PLAYBK_FORMAT,
16298c2ecf20Sopenharmony_ci			    chip->image[CS4231_PLAYBK_FORMAT]);
16308c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&chip->reg_lock, flags);
16318c2ecf20Sopenharmony_ci#if 1
16328c2ecf20Sopenharmony_ci	snd_wss_mce_down(chip);
16338c2ecf20Sopenharmony_ci#else
16348c2ecf20Sopenharmony_ci	/* The following is a workaround to avoid freeze after resume on TP600E.
16358c2ecf20Sopenharmony_ci	   This is the first half of copy of snd_wss_mce_down(), but doesn't
16368c2ecf20Sopenharmony_ci	   include rescheduling.  -- iwai
16378c2ecf20Sopenharmony_ci	   */
16388c2ecf20Sopenharmony_ci	snd_wss_busy_wait(chip);
16398c2ecf20Sopenharmony_ci	spin_lock_irqsave(&chip->reg_lock, flags);
16408c2ecf20Sopenharmony_ci	chip->mce_bit &= ~CS4231_MCE;
16418c2ecf20Sopenharmony_ci	timeout = wss_inb(chip, CS4231P(REGSEL));
16428c2ecf20Sopenharmony_ci	wss_outb(chip, CS4231P(REGSEL), chip->mce_bit | (timeout & 0x1f));
16438c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&chip->reg_lock, flags);
16448c2ecf20Sopenharmony_ci	if (timeout == 0x80)
16458c2ecf20Sopenharmony_ci		snd_printk(KERN_ERR "down [0x%lx]: serious init problem "
16468c2ecf20Sopenharmony_ci			   "- codec still busy\n", chip->port);
16478c2ecf20Sopenharmony_ci	if ((timeout & CS4231_MCE) == 0 ||
16488c2ecf20Sopenharmony_ci	    !(chip->hardware & (WSS_HW_CS4231_MASK | WSS_HW_CS4232_MASK))) {
16498c2ecf20Sopenharmony_ci		return;
16508c2ecf20Sopenharmony_ci	}
16518c2ecf20Sopenharmony_ci	snd_wss_busy_wait(chip);
16528c2ecf20Sopenharmony_ci#endif
16538c2ecf20Sopenharmony_ci}
16548c2ecf20Sopenharmony_ci#endif /* CONFIG_PM */
16558c2ecf20Sopenharmony_ci
16568c2ecf20Sopenharmony_cistatic int snd_wss_free(struct snd_wss *chip)
16578c2ecf20Sopenharmony_ci{
16588c2ecf20Sopenharmony_ci	release_and_free_resource(chip->res_port);
16598c2ecf20Sopenharmony_ci	release_and_free_resource(chip->res_cport);
16608c2ecf20Sopenharmony_ci	if (chip->irq >= 0) {
16618c2ecf20Sopenharmony_ci		disable_irq(chip->irq);
16628c2ecf20Sopenharmony_ci		if (!(chip->hwshare & WSS_HWSHARE_IRQ))
16638c2ecf20Sopenharmony_ci			free_irq(chip->irq, (void *) chip);
16648c2ecf20Sopenharmony_ci	}
16658c2ecf20Sopenharmony_ci	if (!(chip->hwshare & WSS_HWSHARE_DMA1) && chip->dma1 >= 0) {
16668c2ecf20Sopenharmony_ci		snd_dma_disable(chip->dma1);
16678c2ecf20Sopenharmony_ci		free_dma(chip->dma1);
16688c2ecf20Sopenharmony_ci	}
16698c2ecf20Sopenharmony_ci	if (!(chip->hwshare & WSS_HWSHARE_DMA2) &&
16708c2ecf20Sopenharmony_ci	    chip->dma2 >= 0 && chip->dma2 != chip->dma1) {
16718c2ecf20Sopenharmony_ci		snd_dma_disable(chip->dma2);
16728c2ecf20Sopenharmony_ci		free_dma(chip->dma2);
16738c2ecf20Sopenharmony_ci	}
16748c2ecf20Sopenharmony_ci	if (chip->timer)
16758c2ecf20Sopenharmony_ci		snd_device_free(chip->card, chip->timer);
16768c2ecf20Sopenharmony_ci	kfree(chip);
16778c2ecf20Sopenharmony_ci	return 0;
16788c2ecf20Sopenharmony_ci}
16798c2ecf20Sopenharmony_ci
16808c2ecf20Sopenharmony_cistatic int snd_wss_dev_free(struct snd_device *device)
16818c2ecf20Sopenharmony_ci{
16828c2ecf20Sopenharmony_ci	struct snd_wss *chip = device->device_data;
16838c2ecf20Sopenharmony_ci	return snd_wss_free(chip);
16848c2ecf20Sopenharmony_ci}
16858c2ecf20Sopenharmony_ci
16868c2ecf20Sopenharmony_ciconst char *snd_wss_chip_id(struct snd_wss *chip)
16878c2ecf20Sopenharmony_ci{
16888c2ecf20Sopenharmony_ci	switch (chip->hardware) {
16898c2ecf20Sopenharmony_ci	case WSS_HW_CS4231:
16908c2ecf20Sopenharmony_ci		return "CS4231";
16918c2ecf20Sopenharmony_ci	case WSS_HW_CS4231A:
16928c2ecf20Sopenharmony_ci		return "CS4231A";
16938c2ecf20Sopenharmony_ci	case WSS_HW_CS4232:
16948c2ecf20Sopenharmony_ci		return "CS4232";
16958c2ecf20Sopenharmony_ci	case WSS_HW_CS4232A:
16968c2ecf20Sopenharmony_ci		return "CS4232A";
16978c2ecf20Sopenharmony_ci	case WSS_HW_CS4235:
16988c2ecf20Sopenharmony_ci		return "CS4235";
16998c2ecf20Sopenharmony_ci	case WSS_HW_CS4236:
17008c2ecf20Sopenharmony_ci		return "CS4236";
17018c2ecf20Sopenharmony_ci	case WSS_HW_CS4236B:
17028c2ecf20Sopenharmony_ci		return "CS4236B";
17038c2ecf20Sopenharmony_ci	case WSS_HW_CS4237B:
17048c2ecf20Sopenharmony_ci		return "CS4237B";
17058c2ecf20Sopenharmony_ci	case WSS_HW_CS4238B:
17068c2ecf20Sopenharmony_ci		return "CS4238B";
17078c2ecf20Sopenharmony_ci	case WSS_HW_CS4239:
17088c2ecf20Sopenharmony_ci		return "CS4239";
17098c2ecf20Sopenharmony_ci	case WSS_HW_INTERWAVE:
17108c2ecf20Sopenharmony_ci		return "AMD InterWave";
17118c2ecf20Sopenharmony_ci	case WSS_HW_OPL3SA2:
17128c2ecf20Sopenharmony_ci		return chip->card->shortname;
17138c2ecf20Sopenharmony_ci	case WSS_HW_AD1845:
17148c2ecf20Sopenharmony_ci		return "AD1845";
17158c2ecf20Sopenharmony_ci	case WSS_HW_OPTI93X:
17168c2ecf20Sopenharmony_ci		return "OPTi 93x";
17178c2ecf20Sopenharmony_ci	case WSS_HW_AD1847:
17188c2ecf20Sopenharmony_ci		return "AD1847";
17198c2ecf20Sopenharmony_ci	case WSS_HW_AD1848:
17208c2ecf20Sopenharmony_ci		return "AD1848";
17218c2ecf20Sopenharmony_ci	case WSS_HW_CS4248:
17228c2ecf20Sopenharmony_ci		return "CS4248";
17238c2ecf20Sopenharmony_ci	case WSS_HW_CMI8330:
17248c2ecf20Sopenharmony_ci		return "CMI8330/C3D";
17258c2ecf20Sopenharmony_ci	default:
17268c2ecf20Sopenharmony_ci		return "???";
17278c2ecf20Sopenharmony_ci	}
17288c2ecf20Sopenharmony_ci}
17298c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_wss_chip_id);
17308c2ecf20Sopenharmony_ci
17318c2ecf20Sopenharmony_cistatic int snd_wss_new(struct snd_card *card,
17328c2ecf20Sopenharmony_ci			  unsigned short hardware,
17338c2ecf20Sopenharmony_ci			  unsigned short hwshare,
17348c2ecf20Sopenharmony_ci			  struct snd_wss **rchip)
17358c2ecf20Sopenharmony_ci{
17368c2ecf20Sopenharmony_ci	struct snd_wss *chip;
17378c2ecf20Sopenharmony_ci
17388c2ecf20Sopenharmony_ci	*rchip = NULL;
17398c2ecf20Sopenharmony_ci	chip = kzalloc(sizeof(*chip), GFP_KERNEL);
17408c2ecf20Sopenharmony_ci	if (chip == NULL)
17418c2ecf20Sopenharmony_ci		return -ENOMEM;
17428c2ecf20Sopenharmony_ci	chip->hardware = hardware;
17438c2ecf20Sopenharmony_ci	chip->hwshare = hwshare;
17448c2ecf20Sopenharmony_ci
17458c2ecf20Sopenharmony_ci	spin_lock_init(&chip->reg_lock);
17468c2ecf20Sopenharmony_ci	mutex_init(&chip->mce_mutex);
17478c2ecf20Sopenharmony_ci	mutex_init(&chip->open_mutex);
17488c2ecf20Sopenharmony_ci	chip->card = card;
17498c2ecf20Sopenharmony_ci	chip->rate_constraint = snd_wss_xrate;
17508c2ecf20Sopenharmony_ci	chip->set_playback_format = snd_wss_playback_format;
17518c2ecf20Sopenharmony_ci	chip->set_capture_format = snd_wss_capture_format;
17528c2ecf20Sopenharmony_ci	if (chip->hardware == WSS_HW_OPTI93X)
17538c2ecf20Sopenharmony_ci		memcpy(&chip->image, &snd_opti93x_original_image,
17548c2ecf20Sopenharmony_ci		       sizeof(snd_opti93x_original_image));
17558c2ecf20Sopenharmony_ci	else
17568c2ecf20Sopenharmony_ci		memcpy(&chip->image, &snd_wss_original_image,
17578c2ecf20Sopenharmony_ci		       sizeof(snd_wss_original_image));
17588c2ecf20Sopenharmony_ci	if (chip->hardware & WSS_HW_AD1848_MASK) {
17598c2ecf20Sopenharmony_ci		chip->image[CS4231_PIN_CTRL] = 0;
17608c2ecf20Sopenharmony_ci		chip->image[CS4231_TEST_INIT] = 0;
17618c2ecf20Sopenharmony_ci	}
17628c2ecf20Sopenharmony_ci
17638c2ecf20Sopenharmony_ci	*rchip = chip;
17648c2ecf20Sopenharmony_ci	return 0;
17658c2ecf20Sopenharmony_ci}
17668c2ecf20Sopenharmony_ci
17678c2ecf20Sopenharmony_ciint snd_wss_create(struct snd_card *card,
17688c2ecf20Sopenharmony_ci		      unsigned long port,
17698c2ecf20Sopenharmony_ci		      unsigned long cport,
17708c2ecf20Sopenharmony_ci		      int irq, int dma1, int dma2,
17718c2ecf20Sopenharmony_ci		      unsigned short hardware,
17728c2ecf20Sopenharmony_ci		      unsigned short hwshare,
17738c2ecf20Sopenharmony_ci		      struct snd_wss **rchip)
17748c2ecf20Sopenharmony_ci{
17758c2ecf20Sopenharmony_ci	static const struct snd_device_ops ops = {
17768c2ecf20Sopenharmony_ci		.dev_free =	snd_wss_dev_free,
17778c2ecf20Sopenharmony_ci	};
17788c2ecf20Sopenharmony_ci	struct snd_wss *chip;
17798c2ecf20Sopenharmony_ci	int err;
17808c2ecf20Sopenharmony_ci
17818c2ecf20Sopenharmony_ci	err = snd_wss_new(card, hardware, hwshare, &chip);
17828c2ecf20Sopenharmony_ci	if (err < 0)
17838c2ecf20Sopenharmony_ci		return err;
17848c2ecf20Sopenharmony_ci
17858c2ecf20Sopenharmony_ci	chip->irq = -1;
17868c2ecf20Sopenharmony_ci	chip->dma1 = -1;
17878c2ecf20Sopenharmony_ci	chip->dma2 = -1;
17888c2ecf20Sopenharmony_ci
17898c2ecf20Sopenharmony_ci	chip->res_port = request_region(port, 4, "WSS");
17908c2ecf20Sopenharmony_ci	if (!chip->res_port) {
17918c2ecf20Sopenharmony_ci		snd_printk(KERN_ERR "wss: can't grab port 0x%lx\n", port);
17928c2ecf20Sopenharmony_ci		snd_wss_free(chip);
17938c2ecf20Sopenharmony_ci		return -EBUSY;
17948c2ecf20Sopenharmony_ci	}
17958c2ecf20Sopenharmony_ci	chip->port = port;
17968c2ecf20Sopenharmony_ci	if ((long)cport >= 0) {
17978c2ecf20Sopenharmony_ci		chip->res_cport = request_region(cport, 8, "CS4232 Control");
17988c2ecf20Sopenharmony_ci		if (!chip->res_cport) {
17998c2ecf20Sopenharmony_ci			snd_printk(KERN_ERR
18008c2ecf20Sopenharmony_ci				"wss: can't grab control port 0x%lx\n", cport);
18018c2ecf20Sopenharmony_ci			snd_wss_free(chip);
18028c2ecf20Sopenharmony_ci			return -ENODEV;
18038c2ecf20Sopenharmony_ci		}
18048c2ecf20Sopenharmony_ci	}
18058c2ecf20Sopenharmony_ci	chip->cport = cport;
18068c2ecf20Sopenharmony_ci	if (!(hwshare & WSS_HWSHARE_IRQ))
18078c2ecf20Sopenharmony_ci		if (request_irq(irq, snd_wss_interrupt, 0,
18088c2ecf20Sopenharmony_ci				"WSS", (void *) chip)) {
18098c2ecf20Sopenharmony_ci			snd_printk(KERN_ERR "wss: can't grab IRQ %d\n", irq);
18108c2ecf20Sopenharmony_ci			snd_wss_free(chip);
18118c2ecf20Sopenharmony_ci			return -EBUSY;
18128c2ecf20Sopenharmony_ci		}
18138c2ecf20Sopenharmony_ci	chip->irq = irq;
18148c2ecf20Sopenharmony_ci	card->sync_irq = chip->irq;
18158c2ecf20Sopenharmony_ci	if (!(hwshare & WSS_HWSHARE_DMA1) && request_dma(dma1, "WSS - 1")) {
18168c2ecf20Sopenharmony_ci		snd_printk(KERN_ERR "wss: can't grab DMA1 %d\n", dma1);
18178c2ecf20Sopenharmony_ci		snd_wss_free(chip);
18188c2ecf20Sopenharmony_ci		return -EBUSY;
18198c2ecf20Sopenharmony_ci	}
18208c2ecf20Sopenharmony_ci	chip->dma1 = dma1;
18218c2ecf20Sopenharmony_ci	if (!(hwshare & WSS_HWSHARE_DMA2) && dma1 != dma2 &&
18228c2ecf20Sopenharmony_ci	      dma2 >= 0 && request_dma(dma2, "WSS - 2")) {
18238c2ecf20Sopenharmony_ci		snd_printk(KERN_ERR "wss: can't grab DMA2 %d\n", dma2);
18248c2ecf20Sopenharmony_ci		snd_wss_free(chip);
18258c2ecf20Sopenharmony_ci		return -EBUSY;
18268c2ecf20Sopenharmony_ci	}
18278c2ecf20Sopenharmony_ci	if (dma1 == dma2 || dma2 < 0) {
18288c2ecf20Sopenharmony_ci		chip->single_dma = 1;
18298c2ecf20Sopenharmony_ci		chip->dma2 = chip->dma1;
18308c2ecf20Sopenharmony_ci	} else
18318c2ecf20Sopenharmony_ci		chip->dma2 = dma2;
18328c2ecf20Sopenharmony_ci
18338c2ecf20Sopenharmony_ci	if (hardware == WSS_HW_THINKPAD) {
18348c2ecf20Sopenharmony_ci		chip->thinkpad_flag = 1;
18358c2ecf20Sopenharmony_ci		chip->hardware = WSS_HW_DETECT; /* reset */
18368c2ecf20Sopenharmony_ci		snd_wss_thinkpad_twiddle(chip, 1);
18378c2ecf20Sopenharmony_ci	}
18388c2ecf20Sopenharmony_ci
18398c2ecf20Sopenharmony_ci	/* global setup */
18408c2ecf20Sopenharmony_ci	if (snd_wss_probe(chip) < 0) {
18418c2ecf20Sopenharmony_ci		snd_wss_free(chip);
18428c2ecf20Sopenharmony_ci		return -ENODEV;
18438c2ecf20Sopenharmony_ci	}
18448c2ecf20Sopenharmony_ci	snd_wss_init(chip);
18458c2ecf20Sopenharmony_ci
18468c2ecf20Sopenharmony_ci#if 0
18478c2ecf20Sopenharmony_ci	if (chip->hardware & WSS_HW_CS4232_MASK) {
18488c2ecf20Sopenharmony_ci		if (chip->res_cport == NULL)
18498c2ecf20Sopenharmony_ci			snd_printk(KERN_ERR "CS4232 control port features are "
18508c2ecf20Sopenharmony_ci				   "not accessible\n");
18518c2ecf20Sopenharmony_ci	}
18528c2ecf20Sopenharmony_ci#endif
18538c2ecf20Sopenharmony_ci
18548c2ecf20Sopenharmony_ci	/* Register device */
18558c2ecf20Sopenharmony_ci	err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
18568c2ecf20Sopenharmony_ci	if (err < 0) {
18578c2ecf20Sopenharmony_ci		snd_wss_free(chip);
18588c2ecf20Sopenharmony_ci		return err;
18598c2ecf20Sopenharmony_ci	}
18608c2ecf20Sopenharmony_ci
18618c2ecf20Sopenharmony_ci#ifdef CONFIG_PM
18628c2ecf20Sopenharmony_ci	/* Power Management */
18638c2ecf20Sopenharmony_ci	chip->suspend = snd_wss_suspend;
18648c2ecf20Sopenharmony_ci	chip->resume = snd_wss_resume;
18658c2ecf20Sopenharmony_ci#endif
18668c2ecf20Sopenharmony_ci
18678c2ecf20Sopenharmony_ci	*rchip = chip;
18688c2ecf20Sopenharmony_ci	return 0;
18698c2ecf20Sopenharmony_ci}
18708c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_wss_create);
18718c2ecf20Sopenharmony_ci
18728c2ecf20Sopenharmony_cistatic const struct snd_pcm_ops snd_wss_playback_ops = {
18738c2ecf20Sopenharmony_ci	.open =		snd_wss_playback_open,
18748c2ecf20Sopenharmony_ci	.close =	snd_wss_playback_close,
18758c2ecf20Sopenharmony_ci	.hw_params =	snd_wss_playback_hw_params,
18768c2ecf20Sopenharmony_ci	.prepare =	snd_wss_playback_prepare,
18778c2ecf20Sopenharmony_ci	.trigger =	snd_wss_trigger,
18788c2ecf20Sopenharmony_ci	.pointer =	snd_wss_playback_pointer,
18798c2ecf20Sopenharmony_ci};
18808c2ecf20Sopenharmony_ci
18818c2ecf20Sopenharmony_cistatic const struct snd_pcm_ops snd_wss_capture_ops = {
18828c2ecf20Sopenharmony_ci	.open =		snd_wss_capture_open,
18838c2ecf20Sopenharmony_ci	.close =	snd_wss_capture_close,
18848c2ecf20Sopenharmony_ci	.hw_params =	snd_wss_capture_hw_params,
18858c2ecf20Sopenharmony_ci	.prepare =	snd_wss_capture_prepare,
18868c2ecf20Sopenharmony_ci	.trigger =	snd_wss_trigger,
18878c2ecf20Sopenharmony_ci	.pointer =	snd_wss_capture_pointer,
18888c2ecf20Sopenharmony_ci};
18898c2ecf20Sopenharmony_ci
18908c2ecf20Sopenharmony_ciint snd_wss_pcm(struct snd_wss *chip, int device)
18918c2ecf20Sopenharmony_ci{
18928c2ecf20Sopenharmony_ci	struct snd_pcm *pcm;
18938c2ecf20Sopenharmony_ci	int err;
18948c2ecf20Sopenharmony_ci
18958c2ecf20Sopenharmony_ci	err = snd_pcm_new(chip->card, "WSS", device, 1, 1, &pcm);
18968c2ecf20Sopenharmony_ci	if (err < 0)
18978c2ecf20Sopenharmony_ci		return err;
18988c2ecf20Sopenharmony_ci
18998c2ecf20Sopenharmony_ci	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_wss_playback_ops);
19008c2ecf20Sopenharmony_ci	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_wss_capture_ops);
19018c2ecf20Sopenharmony_ci
19028c2ecf20Sopenharmony_ci	/* global setup */
19038c2ecf20Sopenharmony_ci	pcm->private_data = chip;
19048c2ecf20Sopenharmony_ci	pcm->info_flags = 0;
19058c2ecf20Sopenharmony_ci	if (chip->single_dma)
19068c2ecf20Sopenharmony_ci		pcm->info_flags |= SNDRV_PCM_INFO_HALF_DUPLEX;
19078c2ecf20Sopenharmony_ci	if (chip->hardware != WSS_HW_INTERWAVE)
19088c2ecf20Sopenharmony_ci		pcm->info_flags |= SNDRV_PCM_INFO_JOINT_DUPLEX;
19098c2ecf20Sopenharmony_ci	strcpy(pcm->name, snd_wss_chip_id(chip));
19108c2ecf20Sopenharmony_ci
19118c2ecf20Sopenharmony_ci	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, chip->card->dev,
19128c2ecf20Sopenharmony_ci				       64*1024, chip->dma1 > 3 || chip->dma2 > 3 ? 128*1024 : 64*1024);
19138c2ecf20Sopenharmony_ci
19148c2ecf20Sopenharmony_ci	chip->pcm = pcm;
19158c2ecf20Sopenharmony_ci	return 0;
19168c2ecf20Sopenharmony_ci}
19178c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_wss_pcm);
19188c2ecf20Sopenharmony_ci
19198c2ecf20Sopenharmony_cistatic void snd_wss_timer_free(struct snd_timer *timer)
19208c2ecf20Sopenharmony_ci{
19218c2ecf20Sopenharmony_ci	struct snd_wss *chip = timer->private_data;
19228c2ecf20Sopenharmony_ci	chip->timer = NULL;
19238c2ecf20Sopenharmony_ci}
19248c2ecf20Sopenharmony_ci
19258c2ecf20Sopenharmony_ciint snd_wss_timer(struct snd_wss *chip, int device)
19268c2ecf20Sopenharmony_ci{
19278c2ecf20Sopenharmony_ci	struct snd_timer *timer;
19288c2ecf20Sopenharmony_ci	struct snd_timer_id tid;
19298c2ecf20Sopenharmony_ci	int err;
19308c2ecf20Sopenharmony_ci
19318c2ecf20Sopenharmony_ci	/* Timer initialization */
19328c2ecf20Sopenharmony_ci	tid.dev_class = SNDRV_TIMER_CLASS_CARD;
19338c2ecf20Sopenharmony_ci	tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
19348c2ecf20Sopenharmony_ci	tid.card = chip->card->number;
19358c2ecf20Sopenharmony_ci	tid.device = device;
19368c2ecf20Sopenharmony_ci	tid.subdevice = 0;
19378c2ecf20Sopenharmony_ci	if ((err = snd_timer_new(chip->card, "CS4231", &tid, &timer)) < 0)
19388c2ecf20Sopenharmony_ci		return err;
19398c2ecf20Sopenharmony_ci	strcpy(timer->name, snd_wss_chip_id(chip));
19408c2ecf20Sopenharmony_ci	timer->private_data = chip;
19418c2ecf20Sopenharmony_ci	timer->private_free = snd_wss_timer_free;
19428c2ecf20Sopenharmony_ci	timer->hw = snd_wss_timer_table;
19438c2ecf20Sopenharmony_ci	chip->timer = timer;
19448c2ecf20Sopenharmony_ci	return 0;
19458c2ecf20Sopenharmony_ci}
19468c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_wss_timer);
19478c2ecf20Sopenharmony_ci
19488c2ecf20Sopenharmony_ci/*
19498c2ecf20Sopenharmony_ci *  MIXER part
19508c2ecf20Sopenharmony_ci */
19518c2ecf20Sopenharmony_ci
19528c2ecf20Sopenharmony_cistatic int snd_wss_info_mux(struct snd_kcontrol *kcontrol,
19538c2ecf20Sopenharmony_ci			    struct snd_ctl_elem_info *uinfo)
19548c2ecf20Sopenharmony_ci{
19558c2ecf20Sopenharmony_ci	static const char * const texts[4] = {
19568c2ecf20Sopenharmony_ci		"Line", "Aux", "Mic", "Mix"
19578c2ecf20Sopenharmony_ci	};
19588c2ecf20Sopenharmony_ci	static const char * const opl3sa_texts[4] = {
19598c2ecf20Sopenharmony_ci		"Line", "CD", "Mic", "Mix"
19608c2ecf20Sopenharmony_ci	};
19618c2ecf20Sopenharmony_ci	static const char * const gusmax_texts[4] = {
19628c2ecf20Sopenharmony_ci		"Line", "Synth", "Mic", "Mix"
19638c2ecf20Sopenharmony_ci	};
19648c2ecf20Sopenharmony_ci	const char * const *ptexts = texts;
19658c2ecf20Sopenharmony_ci	struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
19668c2ecf20Sopenharmony_ci
19678c2ecf20Sopenharmony_ci	if (snd_BUG_ON(!chip->card))
19688c2ecf20Sopenharmony_ci		return -EINVAL;
19698c2ecf20Sopenharmony_ci	if (!strcmp(chip->card->driver, "GUS MAX"))
19708c2ecf20Sopenharmony_ci		ptexts = gusmax_texts;
19718c2ecf20Sopenharmony_ci	switch (chip->hardware) {
19728c2ecf20Sopenharmony_ci	case WSS_HW_INTERWAVE:
19738c2ecf20Sopenharmony_ci		ptexts = gusmax_texts;
19748c2ecf20Sopenharmony_ci		break;
19758c2ecf20Sopenharmony_ci	case WSS_HW_OPTI93X:
19768c2ecf20Sopenharmony_ci	case WSS_HW_OPL3SA2:
19778c2ecf20Sopenharmony_ci		ptexts = opl3sa_texts;
19788c2ecf20Sopenharmony_ci		break;
19798c2ecf20Sopenharmony_ci	}
19808c2ecf20Sopenharmony_ci	return snd_ctl_enum_info(uinfo, 2, 4, ptexts);
19818c2ecf20Sopenharmony_ci}
19828c2ecf20Sopenharmony_ci
19838c2ecf20Sopenharmony_cistatic int snd_wss_get_mux(struct snd_kcontrol *kcontrol,
19848c2ecf20Sopenharmony_ci			   struct snd_ctl_elem_value *ucontrol)
19858c2ecf20Sopenharmony_ci{
19868c2ecf20Sopenharmony_ci	struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
19878c2ecf20Sopenharmony_ci	unsigned long flags;
19888c2ecf20Sopenharmony_ci
19898c2ecf20Sopenharmony_ci	spin_lock_irqsave(&chip->reg_lock, flags);
19908c2ecf20Sopenharmony_ci	ucontrol->value.enumerated.item[0] = (chip->image[CS4231_LEFT_INPUT] & CS4231_MIXS_ALL) >> 6;
19918c2ecf20Sopenharmony_ci	ucontrol->value.enumerated.item[1] = (chip->image[CS4231_RIGHT_INPUT] & CS4231_MIXS_ALL) >> 6;
19928c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&chip->reg_lock, flags);
19938c2ecf20Sopenharmony_ci	return 0;
19948c2ecf20Sopenharmony_ci}
19958c2ecf20Sopenharmony_ci
19968c2ecf20Sopenharmony_cistatic int snd_wss_put_mux(struct snd_kcontrol *kcontrol,
19978c2ecf20Sopenharmony_ci			   struct snd_ctl_elem_value *ucontrol)
19988c2ecf20Sopenharmony_ci{
19998c2ecf20Sopenharmony_ci	struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
20008c2ecf20Sopenharmony_ci	unsigned long flags;
20018c2ecf20Sopenharmony_ci	unsigned short left, right;
20028c2ecf20Sopenharmony_ci	int change;
20038c2ecf20Sopenharmony_ci
20048c2ecf20Sopenharmony_ci	if (ucontrol->value.enumerated.item[0] > 3 ||
20058c2ecf20Sopenharmony_ci	    ucontrol->value.enumerated.item[1] > 3)
20068c2ecf20Sopenharmony_ci		return -EINVAL;
20078c2ecf20Sopenharmony_ci	left = ucontrol->value.enumerated.item[0] << 6;
20088c2ecf20Sopenharmony_ci	right = ucontrol->value.enumerated.item[1] << 6;
20098c2ecf20Sopenharmony_ci	spin_lock_irqsave(&chip->reg_lock, flags);
20108c2ecf20Sopenharmony_ci	left = (chip->image[CS4231_LEFT_INPUT] & ~CS4231_MIXS_ALL) | left;
20118c2ecf20Sopenharmony_ci	right = (chip->image[CS4231_RIGHT_INPUT] & ~CS4231_MIXS_ALL) | right;
20128c2ecf20Sopenharmony_ci	change = left != chip->image[CS4231_LEFT_INPUT] ||
20138c2ecf20Sopenharmony_ci		 right != chip->image[CS4231_RIGHT_INPUT];
20148c2ecf20Sopenharmony_ci	snd_wss_out(chip, CS4231_LEFT_INPUT, left);
20158c2ecf20Sopenharmony_ci	snd_wss_out(chip, CS4231_RIGHT_INPUT, right);
20168c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&chip->reg_lock, flags);
20178c2ecf20Sopenharmony_ci	return change;
20188c2ecf20Sopenharmony_ci}
20198c2ecf20Sopenharmony_ci
20208c2ecf20Sopenharmony_ciint snd_wss_info_single(struct snd_kcontrol *kcontrol,
20218c2ecf20Sopenharmony_ci			struct snd_ctl_elem_info *uinfo)
20228c2ecf20Sopenharmony_ci{
20238c2ecf20Sopenharmony_ci	int mask = (kcontrol->private_value >> 16) & 0xff;
20248c2ecf20Sopenharmony_ci
20258c2ecf20Sopenharmony_ci	uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
20268c2ecf20Sopenharmony_ci	uinfo->count = 1;
20278c2ecf20Sopenharmony_ci	uinfo->value.integer.min = 0;
20288c2ecf20Sopenharmony_ci	uinfo->value.integer.max = mask;
20298c2ecf20Sopenharmony_ci	return 0;
20308c2ecf20Sopenharmony_ci}
20318c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_wss_info_single);
20328c2ecf20Sopenharmony_ci
20338c2ecf20Sopenharmony_ciint snd_wss_get_single(struct snd_kcontrol *kcontrol,
20348c2ecf20Sopenharmony_ci		       struct snd_ctl_elem_value *ucontrol)
20358c2ecf20Sopenharmony_ci{
20368c2ecf20Sopenharmony_ci	struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
20378c2ecf20Sopenharmony_ci	unsigned long flags;
20388c2ecf20Sopenharmony_ci	int reg = kcontrol->private_value & 0xff;
20398c2ecf20Sopenharmony_ci	int shift = (kcontrol->private_value >> 8) & 0xff;
20408c2ecf20Sopenharmony_ci	int mask = (kcontrol->private_value >> 16) & 0xff;
20418c2ecf20Sopenharmony_ci	int invert = (kcontrol->private_value >> 24) & 0xff;
20428c2ecf20Sopenharmony_ci
20438c2ecf20Sopenharmony_ci	spin_lock_irqsave(&chip->reg_lock, flags);
20448c2ecf20Sopenharmony_ci	ucontrol->value.integer.value[0] = (chip->image[reg] >> shift) & mask;
20458c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&chip->reg_lock, flags);
20468c2ecf20Sopenharmony_ci	if (invert)
20478c2ecf20Sopenharmony_ci		ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
20488c2ecf20Sopenharmony_ci	return 0;
20498c2ecf20Sopenharmony_ci}
20508c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_wss_get_single);
20518c2ecf20Sopenharmony_ci
20528c2ecf20Sopenharmony_ciint snd_wss_put_single(struct snd_kcontrol *kcontrol,
20538c2ecf20Sopenharmony_ci		       struct snd_ctl_elem_value *ucontrol)
20548c2ecf20Sopenharmony_ci{
20558c2ecf20Sopenharmony_ci	struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
20568c2ecf20Sopenharmony_ci	unsigned long flags;
20578c2ecf20Sopenharmony_ci	int reg = kcontrol->private_value & 0xff;
20588c2ecf20Sopenharmony_ci	int shift = (kcontrol->private_value >> 8) & 0xff;
20598c2ecf20Sopenharmony_ci	int mask = (kcontrol->private_value >> 16) & 0xff;
20608c2ecf20Sopenharmony_ci	int invert = (kcontrol->private_value >> 24) & 0xff;
20618c2ecf20Sopenharmony_ci	int change;
20628c2ecf20Sopenharmony_ci	unsigned short val;
20638c2ecf20Sopenharmony_ci
20648c2ecf20Sopenharmony_ci	val = (ucontrol->value.integer.value[0] & mask);
20658c2ecf20Sopenharmony_ci	if (invert)
20668c2ecf20Sopenharmony_ci		val = mask - val;
20678c2ecf20Sopenharmony_ci	val <<= shift;
20688c2ecf20Sopenharmony_ci	spin_lock_irqsave(&chip->reg_lock, flags);
20698c2ecf20Sopenharmony_ci	val = (chip->image[reg] & ~(mask << shift)) | val;
20708c2ecf20Sopenharmony_ci	change = val != chip->image[reg];
20718c2ecf20Sopenharmony_ci	snd_wss_out(chip, reg, val);
20728c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&chip->reg_lock, flags);
20738c2ecf20Sopenharmony_ci	return change;
20748c2ecf20Sopenharmony_ci}
20758c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_wss_put_single);
20768c2ecf20Sopenharmony_ci
20778c2ecf20Sopenharmony_ciint snd_wss_info_double(struct snd_kcontrol *kcontrol,
20788c2ecf20Sopenharmony_ci			struct snd_ctl_elem_info *uinfo)
20798c2ecf20Sopenharmony_ci{
20808c2ecf20Sopenharmony_ci	int mask = (kcontrol->private_value >> 24) & 0xff;
20818c2ecf20Sopenharmony_ci
20828c2ecf20Sopenharmony_ci	uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
20838c2ecf20Sopenharmony_ci	uinfo->count = 2;
20848c2ecf20Sopenharmony_ci	uinfo->value.integer.min = 0;
20858c2ecf20Sopenharmony_ci	uinfo->value.integer.max = mask;
20868c2ecf20Sopenharmony_ci	return 0;
20878c2ecf20Sopenharmony_ci}
20888c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_wss_info_double);
20898c2ecf20Sopenharmony_ci
20908c2ecf20Sopenharmony_ciint snd_wss_get_double(struct snd_kcontrol *kcontrol,
20918c2ecf20Sopenharmony_ci		       struct snd_ctl_elem_value *ucontrol)
20928c2ecf20Sopenharmony_ci{
20938c2ecf20Sopenharmony_ci	struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
20948c2ecf20Sopenharmony_ci	unsigned long flags;
20958c2ecf20Sopenharmony_ci	int left_reg = kcontrol->private_value & 0xff;
20968c2ecf20Sopenharmony_ci	int right_reg = (kcontrol->private_value >> 8) & 0xff;
20978c2ecf20Sopenharmony_ci	int shift_left = (kcontrol->private_value >> 16) & 0x07;
20988c2ecf20Sopenharmony_ci	int shift_right = (kcontrol->private_value >> 19) & 0x07;
20998c2ecf20Sopenharmony_ci	int mask = (kcontrol->private_value >> 24) & 0xff;
21008c2ecf20Sopenharmony_ci	int invert = (kcontrol->private_value >> 22) & 1;
21018c2ecf20Sopenharmony_ci
21028c2ecf20Sopenharmony_ci	spin_lock_irqsave(&chip->reg_lock, flags);
21038c2ecf20Sopenharmony_ci	ucontrol->value.integer.value[0] = (chip->image[left_reg] >> shift_left) & mask;
21048c2ecf20Sopenharmony_ci	ucontrol->value.integer.value[1] = (chip->image[right_reg] >> shift_right) & mask;
21058c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&chip->reg_lock, flags);
21068c2ecf20Sopenharmony_ci	if (invert) {
21078c2ecf20Sopenharmony_ci		ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
21088c2ecf20Sopenharmony_ci		ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
21098c2ecf20Sopenharmony_ci	}
21108c2ecf20Sopenharmony_ci	return 0;
21118c2ecf20Sopenharmony_ci}
21128c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_wss_get_double);
21138c2ecf20Sopenharmony_ci
21148c2ecf20Sopenharmony_ciint snd_wss_put_double(struct snd_kcontrol *kcontrol,
21158c2ecf20Sopenharmony_ci		       struct snd_ctl_elem_value *ucontrol)
21168c2ecf20Sopenharmony_ci{
21178c2ecf20Sopenharmony_ci	struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
21188c2ecf20Sopenharmony_ci	unsigned long flags;
21198c2ecf20Sopenharmony_ci	int left_reg = kcontrol->private_value & 0xff;
21208c2ecf20Sopenharmony_ci	int right_reg = (kcontrol->private_value >> 8) & 0xff;
21218c2ecf20Sopenharmony_ci	int shift_left = (kcontrol->private_value >> 16) & 0x07;
21228c2ecf20Sopenharmony_ci	int shift_right = (kcontrol->private_value >> 19) & 0x07;
21238c2ecf20Sopenharmony_ci	int mask = (kcontrol->private_value >> 24) & 0xff;
21248c2ecf20Sopenharmony_ci	int invert = (kcontrol->private_value >> 22) & 1;
21258c2ecf20Sopenharmony_ci	int change;
21268c2ecf20Sopenharmony_ci	unsigned short val1, val2;
21278c2ecf20Sopenharmony_ci
21288c2ecf20Sopenharmony_ci	val1 = ucontrol->value.integer.value[0] & mask;
21298c2ecf20Sopenharmony_ci	val2 = ucontrol->value.integer.value[1] & mask;
21308c2ecf20Sopenharmony_ci	if (invert) {
21318c2ecf20Sopenharmony_ci		val1 = mask - val1;
21328c2ecf20Sopenharmony_ci		val2 = mask - val2;
21338c2ecf20Sopenharmony_ci	}
21348c2ecf20Sopenharmony_ci	val1 <<= shift_left;
21358c2ecf20Sopenharmony_ci	val2 <<= shift_right;
21368c2ecf20Sopenharmony_ci	spin_lock_irqsave(&chip->reg_lock, flags);
21378c2ecf20Sopenharmony_ci	if (left_reg != right_reg) {
21388c2ecf20Sopenharmony_ci		val1 = (chip->image[left_reg] & ~(mask << shift_left)) | val1;
21398c2ecf20Sopenharmony_ci		val2 = (chip->image[right_reg] & ~(mask << shift_right)) | val2;
21408c2ecf20Sopenharmony_ci		change = val1 != chip->image[left_reg] ||
21418c2ecf20Sopenharmony_ci			 val2 != chip->image[right_reg];
21428c2ecf20Sopenharmony_ci		snd_wss_out(chip, left_reg, val1);
21438c2ecf20Sopenharmony_ci		snd_wss_out(chip, right_reg, val2);
21448c2ecf20Sopenharmony_ci	} else {
21458c2ecf20Sopenharmony_ci		mask = (mask << shift_left) | (mask << shift_right);
21468c2ecf20Sopenharmony_ci		val1 = (chip->image[left_reg] & ~mask) | val1 | val2;
21478c2ecf20Sopenharmony_ci		change = val1 != chip->image[left_reg];
21488c2ecf20Sopenharmony_ci		snd_wss_out(chip, left_reg, val1);
21498c2ecf20Sopenharmony_ci	}
21508c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&chip->reg_lock, flags);
21518c2ecf20Sopenharmony_ci	return change;
21528c2ecf20Sopenharmony_ci}
21538c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_wss_put_double);
21548c2ecf20Sopenharmony_ci
21558c2ecf20Sopenharmony_cistatic const DECLARE_TLV_DB_SCALE(db_scale_6bit, -9450, 150, 0);
21568c2ecf20Sopenharmony_cistatic const DECLARE_TLV_DB_SCALE(db_scale_5bit_12db_max, -3450, 150, 0);
21578c2ecf20Sopenharmony_cistatic const DECLARE_TLV_DB_SCALE(db_scale_rec_gain, 0, 150, 0);
21588c2ecf20Sopenharmony_cistatic const DECLARE_TLV_DB_SCALE(db_scale_4bit, -4500, 300, 0);
21598c2ecf20Sopenharmony_ci
21608c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new snd_wss_controls[] = {
21618c2ecf20Sopenharmony_ciWSS_DOUBLE("PCM Playback Switch", 0,
21628c2ecf20Sopenharmony_ci		CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 7, 7, 1, 1),
21638c2ecf20Sopenharmony_ciWSS_DOUBLE_TLV("PCM Playback Volume", 0,
21648c2ecf20Sopenharmony_ci		CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 0, 0, 63, 1,
21658c2ecf20Sopenharmony_ci		db_scale_6bit),
21668c2ecf20Sopenharmony_ciWSS_DOUBLE("Aux Playback Switch", 0,
21678c2ecf20Sopenharmony_ci		CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 7, 7, 1, 1),
21688c2ecf20Sopenharmony_ciWSS_DOUBLE_TLV("Aux Playback Volume", 0,
21698c2ecf20Sopenharmony_ci		CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 0, 0, 31, 1,
21708c2ecf20Sopenharmony_ci		db_scale_5bit_12db_max),
21718c2ecf20Sopenharmony_ciWSS_DOUBLE("Aux Playback Switch", 1,
21728c2ecf20Sopenharmony_ci		CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 7, 7, 1, 1),
21738c2ecf20Sopenharmony_ciWSS_DOUBLE_TLV("Aux Playback Volume", 1,
21748c2ecf20Sopenharmony_ci		CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 0, 0, 31, 1,
21758c2ecf20Sopenharmony_ci		db_scale_5bit_12db_max),
21768c2ecf20Sopenharmony_ciWSS_DOUBLE_TLV("Capture Volume", 0, CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT,
21778c2ecf20Sopenharmony_ci		0, 0, 15, 0, db_scale_rec_gain),
21788c2ecf20Sopenharmony_ci{
21798c2ecf20Sopenharmony_ci	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
21808c2ecf20Sopenharmony_ci	.name = "Capture Source",
21818c2ecf20Sopenharmony_ci	.info = snd_wss_info_mux,
21828c2ecf20Sopenharmony_ci	.get = snd_wss_get_mux,
21838c2ecf20Sopenharmony_ci	.put = snd_wss_put_mux,
21848c2ecf20Sopenharmony_ci},
21858c2ecf20Sopenharmony_ciWSS_DOUBLE("Mic Boost (+20dB)", 0,
21868c2ecf20Sopenharmony_ci		CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT, 5, 5, 1, 0),
21878c2ecf20Sopenharmony_ciWSS_SINGLE("Loopback Capture Switch", 0,
21888c2ecf20Sopenharmony_ci		CS4231_LOOPBACK, 0, 1, 0),
21898c2ecf20Sopenharmony_ciWSS_SINGLE_TLV("Loopback Capture Volume", 0, CS4231_LOOPBACK, 2, 63, 1,
21908c2ecf20Sopenharmony_ci		db_scale_6bit),
21918c2ecf20Sopenharmony_ciWSS_DOUBLE("Line Playback Switch", 0,
21928c2ecf20Sopenharmony_ci		CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 7, 7, 1, 1),
21938c2ecf20Sopenharmony_ciWSS_DOUBLE_TLV("Line Playback Volume", 0,
21948c2ecf20Sopenharmony_ci		CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 0, 0, 31, 1,
21958c2ecf20Sopenharmony_ci		db_scale_5bit_12db_max),
21968c2ecf20Sopenharmony_ciWSS_SINGLE("Beep Playback Switch", 0,
21978c2ecf20Sopenharmony_ci		CS4231_MONO_CTRL, 7, 1, 1),
21988c2ecf20Sopenharmony_ciWSS_SINGLE_TLV("Beep Playback Volume", 0,
21998c2ecf20Sopenharmony_ci		CS4231_MONO_CTRL, 0, 15, 1,
22008c2ecf20Sopenharmony_ci		db_scale_4bit),
22018c2ecf20Sopenharmony_ciWSS_SINGLE("Mono Output Playback Switch", 0,
22028c2ecf20Sopenharmony_ci		CS4231_MONO_CTRL, 6, 1, 1),
22038c2ecf20Sopenharmony_ciWSS_SINGLE("Beep Bypass Playback Switch", 0,
22048c2ecf20Sopenharmony_ci		CS4231_MONO_CTRL, 5, 1, 0),
22058c2ecf20Sopenharmony_ci};
22068c2ecf20Sopenharmony_ci
22078c2ecf20Sopenharmony_ciint snd_wss_mixer(struct snd_wss *chip)
22088c2ecf20Sopenharmony_ci{
22098c2ecf20Sopenharmony_ci	struct snd_card *card;
22108c2ecf20Sopenharmony_ci	unsigned int idx;
22118c2ecf20Sopenharmony_ci	int err;
22128c2ecf20Sopenharmony_ci	int count = ARRAY_SIZE(snd_wss_controls);
22138c2ecf20Sopenharmony_ci
22148c2ecf20Sopenharmony_ci	if (snd_BUG_ON(!chip || !chip->pcm))
22158c2ecf20Sopenharmony_ci		return -EINVAL;
22168c2ecf20Sopenharmony_ci
22178c2ecf20Sopenharmony_ci	card = chip->card;
22188c2ecf20Sopenharmony_ci
22198c2ecf20Sopenharmony_ci	strcpy(card->mixername, chip->pcm->name);
22208c2ecf20Sopenharmony_ci
22218c2ecf20Sopenharmony_ci	/* Use only the first 11 entries on AD1848 */
22228c2ecf20Sopenharmony_ci	if (chip->hardware & WSS_HW_AD1848_MASK)
22238c2ecf20Sopenharmony_ci		count = 11;
22248c2ecf20Sopenharmony_ci	/* There is no loopback on OPTI93X */
22258c2ecf20Sopenharmony_ci	else if (chip->hardware == WSS_HW_OPTI93X)
22268c2ecf20Sopenharmony_ci		count = 9;
22278c2ecf20Sopenharmony_ci
22288c2ecf20Sopenharmony_ci	for (idx = 0; idx < count; idx++) {
22298c2ecf20Sopenharmony_ci		err = snd_ctl_add(card,
22308c2ecf20Sopenharmony_ci				snd_ctl_new1(&snd_wss_controls[idx],
22318c2ecf20Sopenharmony_ci					     chip));
22328c2ecf20Sopenharmony_ci		if (err < 0)
22338c2ecf20Sopenharmony_ci			return err;
22348c2ecf20Sopenharmony_ci	}
22358c2ecf20Sopenharmony_ci	return 0;
22368c2ecf20Sopenharmony_ci}
22378c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_wss_mixer);
22388c2ecf20Sopenharmony_ci
22398c2ecf20Sopenharmony_ciconst struct snd_pcm_ops *snd_wss_get_pcm_ops(int direction)
22408c2ecf20Sopenharmony_ci{
22418c2ecf20Sopenharmony_ci	return direction == SNDRV_PCM_STREAM_PLAYBACK ?
22428c2ecf20Sopenharmony_ci		&snd_wss_playback_ops : &snd_wss_capture_ops;
22438c2ecf20Sopenharmony_ci}
22448c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_wss_get_pcm_ops);
2245