wss_lib.c 65.7 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2
 *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
L
Linus Torvalds 已提交
3 4 5
 *  Routines for control of CS4231(A)/CS4232/InterWave & compatible chips
 *
 *  Bugs:
6
 *     - sometimes record brokes playback with WSS portion of
L
Linus Torvalds 已提交
7 8
 *       Yamaha OPL3-SA3 chip
 *     - CS4231 (GUS MAX) - still trouble with occasional noises
9
 *			  - broken initialization?
L
Linus Torvalds 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 */

#include <linux/delay.h>
#include <linux/pm.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/slab.h>
#include <linux/ioport.h>
#include <sound/core.h>
34
#include <sound/wss.h>
L
Linus Torvalds 已提交
35
#include <sound/pcm_params.h>
36
#include <sound/tlv.h>
L
Linus Torvalds 已提交
37 38 39 40 41

#include <asm/io.h>
#include <asm/dma.h>
#include <asm/irq.h>

42
MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
L
Linus Torvalds 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
MODULE_DESCRIPTION("Routines for control of CS4231(A)/CS4232/InterWave & compatible chips");
MODULE_LICENSE("GPL");

#if 0
#define SNDRV_DEBUG_MCE
#endif

/*
 *  Some variables
 */

static unsigned char freq_bits[14] = {
	/* 5510 */	0x00 | CS4231_XTAL2,
	/* 6620 */	0x0E | CS4231_XTAL2,
	/* 8000 */	0x00 | CS4231_XTAL1,
	/* 9600 */	0x0E | CS4231_XTAL1,
	/* 11025 */	0x02 | CS4231_XTAL2,
	/* 16000 */	0x02 | CS4231_XTAL1,
	/* 18900 */	0x04 | CS4231_XTAL2,
	/* 22050 */	0x06 | CS4231_XTAL2,
	/* 27042 */	0x04 | CS4231_XTAL1,
	/* 32000 */	0x06 | CS4231_XTAL1,
	/* 33075 */	0x0C | CS4231_XTAL2,
	/* 37800 */	0x08 | CS4231_XTAL2,
	/* 44100 */	0x0A | CS4231_XTAL2,
	/* 48000 */	0x0C | CS4231_XTAL1
};

static unsigned int rates[14] = {
	5510, 6620, 8000, 9600, 11025, 16000, 18900, 22050,
	27042, 32000, 33075, 37800, 44100, 48000
};

76
static struct snd_pcm_hw_constraint_list hw_constraints_rates = {
77
	.count = ARRAY_SIZE(rates),
L
Linus Torvalds 已提交
78 79 80 81
	.list = rates,
	.mask = 0,
};

82
static int snd_wss_xrate(struct snd_pcm_runtime *runtime)
L
Linus Torvalds 已提交
83
{
84 85
	return snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
					  &hw_constraints_rates);
L
Linus Torvalds 已提交
86 87
}

88
static unsigned char snd_wss_original_image[32] =
L
Linus Torvalds 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
{
	0x00,			/* 00/00 - lic */
	0x00,			/* 01/01 - ric */
	0x9f,			/* 02/02 - la1ic */
	0x9f,			/* 03/03 - ra1ic */
	0x9f,			/* 04/04 - la2ic */
	0x9f,			/* 05/05 - ra2ic */
	0xbf,			/* 06/06 - loc */
	0xbf,			/* 07/07 - roc */
	0x20,			/* 08/08 - pdfr */
	CS4231_AUTOCALIB,	/* 09/09 - ic */
	0x00,			/* 0a/10 - pc */
	0x00,			/* 0b/11 - ti */
	CS4231_MODE2,		/* 0c/12 - mi */
	0xfc,			/* 0d/13 - lbc */
	0x00,			/* 0e/14 - pbru */
	0x00,			/* 0f/15 - pbrl */
	0x80,			/* 10/16 - afei */
	0x01,			/* 11/17 - afeii */
	0x9f,			/* 12/18 - llic */
	0x9f,			/* 13/19 - rlic */
	0x00,			/* 14/20 - tlb */
	0x00,			/* 15/21 - thb */
	0x00,			/* 16/22 - la3mic/reserved */
	0x00,			/* 17/23 - ra3mic/reserved */
	0x00,			/* 18/24 - afs */
	0x00,			/* 19/25 - lamoc/version */
	0xcf,			/* 1a/26 - mioc */
	0x00,			/* 1b/27 - ramoc/reserved */
	0x20,			/* 1c/28 - cdfr */
	0x00,			/* 1d/29 - res4 */
	0x00,			/* 1e/30 - cbru */
	0x00,			/* 1f/31 - cbrl */
};

124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
static unsigned char snd_opti93x_original_image[32] =
{
	0x00,		/* 00/00 - l_mixout_outctrl */
	0x00,		/* 01/01 - r_mixout_outctrl */
	0x88,		/* 02/02 - l_cd_inctrl */
	0x88,		/* 03/03 - r_cd_inctrl */
	0x88,		/* 04/04 - l_a1/fm_inctrl */
	0x88,		/* 05/05 - r_a1/fm_inctrl */
	0x80,		/* 06/06 - l_dac_inctrl */
	0x80,		/* 07/07 - r_dac_inctrl */
	0x00,		/* 08/08 - ply_dataform_reg */
	0x00,		/* 09/09 - if_conf */
	0x00,		/* 0a/10 - pin_ctrl */
	0x00,		/* 0b/11 - err_init_reg */
	0x0a,		/* 0c/12 - id_reg */
	0x00,		/* 0d/13 - reserved */
	0x00,		/* 0e/14 - ply_upcount_reg */
	0x00,		/* 0f/15 - ply_lowcount_reg */
	0x88,		/* 10/16 - reserved/l_a1_inctrl */
	0x88,		/* 11/17 - reserved/r_a1_inctrl */
	0x88,		/* 12/18 - l_line_inctrl */
	0x88,		/* 13/19 - r_line_inctrl */
	0x88,		/* 14/20 - l_mic_inctrl */
	0x88,		/* 15/21 - r_mic_inctrl */
	0x80,		/* 16/22 - l_out_outctrl */
	0x80,		/* 17/23 - r_out_outctrl */
	0x00,		/* 18/24 - reserved */
	0x00,		/* 19/25 - reserved */
	0x00,		/* 1a/26 - reserved */
	0x00,		/* 1b/27 - reserved */
	0x00,		/* 1c/28 - cap_dataform_reg */
	0x00,		/* 1d/29 - reserved */
	0x00,		/* 1e/30 - cap_upcount_reg */
	0x00		/* 1f/31 - cap_lowcount_reg */
};

L
Linus Torvalds 已提交
160 161 162 163
/*
 *  Basic I/O functions
 */

164
static inline void wss_outb(struct snd_wss *chip, u8 offset, u8 val)
L
Linus Torvalds 已提交
165 166 167 168
{
	outb(val, chip->port + offset);
}

169
static inline u8 wss_inb(struct snd_wss *chip, u8 offset)
L
Linus Torvalds 已提交
170 171 172 173
{
	return inb(chip->port + offset);
}

174
static void snd_wss_wait(struct snd_wss *chip)
L
Linus Torvalds 已提交
175 176 177 178
{
	int timeout;

	for (timeout = 250;
179
	     timeout > 0 && (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT);
L
Linus Torvalds 已提交
180
	     timeout--)
181
		udelay(100);
182 183
}

184
static void snd_wss_outm(struct snd_wss *chip, unsigned char reg,
185 186 187 188
			    unsigned char mask, unsigned char value)
{
	unsigned char tmp = (chip->image[reg] & mask) | value;

189
	snd_wss_wait(chip);
L
Linus Torvalds 已提交
190
#ifdef CONFIG_SND_DEBUG
191
	if (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT)
L
Linus Torvalds 已提交
192 193
		snd_printk("outm: auto calibration time out - reg = 0x%x, value = 0x%x\n", reg, value);
#endif
194 195
	chip->image[reg] = tmp;
	if (!chip->calibrate_mute) {
196
		wss_outb(chip, CS4231P(REGSEL), chip->mce_bit | reg);
197
		wmb();
198
		wss_outb(chip, CS4231P(REG), tmp);
L
Linus Torvalds 已提交
199 200 201 202
		mb();
	}
}

203 204
static void snd_wss_dout(struct snd_wss *chip, unsigned char reg,
			 unsigned char value)
L
Linus Torvalds 已提交
205 206 207 208
{
	int timeout;

	for (timeout = 250;
209
	     timeout > 0 && (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT);
L
Linus Torvalds 已提交
210
	     timeout--)
211
		udelay(10);
212 213
	wss_outb(chip, CS4231P(REGSEL), chip->mce_bit | reg);
	wss_outb(chip, CS4231P(REG), value);
L
Linus Torvalds 已提交
214 215 216
	mb();
}

217
void snd_wss_out(struct snd_wss *chip, unsigned char reg, unsigned char value)
L
Linus Torvalds 已提交
218
{
219
	snd_wss_wait(chip);
L
Linus Torvalds 已提交
220
#ifdef CONFIG_SND_DEBUG
221
	if (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT)
L
Linus Torvalds 已提交
222 223
		snd_printk("out: auto calibration time out - reg = 0x%x, value = 0x%x\n", reg, value);
#endif
224 225
	wss_outb(chip, CS4231P(REGSEL), chip->mce_bit | reg);
	wss_outb(chip, CS4231P(REG), value);
L
Linus Torvalds 已提交
226 227
	chip->image[reg] = value;
	mb();
228 229
	snd_printdd("codec out - reg 0x%x = 0x%x\n",
			chip->mce_bit | reg, value);
L
Linus Torvalds 已提交
230
}
231
EXPORT_SYMBOL(snd_wss_out);
L
Linus Torvalds 已提交
232

233
unsigned char snd_wss_in(struct snd_wss *chip, unsigned char reg)
L
Linus Torvalds 已提交
234
{
235
	snd_wss_wait(chip);
L
Linus Torvalds 已提交
236
#ifdef CONFIG_SND_DEBUG
237
	if (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT)
L
Linus Torvalds 已提交
238 239
		snd_printk("in: auto calibration time out - reg = 0x%x\n", reg);
#endif
240
	wss_outb(chip, CS4231P(REGSEL), chip->mce_bit | reg);
L
Linus Torvalds 已提交
241
	mb();
242
	return wss_inb(chip, CS4231P(REG));
L
Linus Torvalds 已提交
243
}
244
EXPORT_SYMBOL(snd_wss_in);
L
Linus Torvalds 已提交
245

246 247
void snd_cs4236_ext_out(struct snd_wss *chip, unsigned char reg,
			unsigned char val)
L
Linus Torvalds 已提交
248
{
249 250 251 252
	wss_outb(chip, CS4231P(REGSEL), chip->mce_bit | 0x17);
	wss_outb(chip, CS4231P(REG),
		 reg | (chip->image[CS4236_EXT_REG] & 0x01));
	wss_outb(chip, CS4231P(REG), val);
L
Linus Torvalds 已提交
253 254 255 256 257
	chip->eimage[CS4236_REG(reg)] = val;
#if 0
	printk("ext out : reg = 0x%x, val = 0x%x\n", reg, val);
#endif
}
258
EXPORT_SYMBOL(snd_cs4236_ext_out);
L
Linus Torvalds 已提交
259

260
unsigned char snd_cs4236_ext_in(struct snd_wss *chip, unsigned char reg)
L
Linus Torvalds 已提交
261
{
262 263 264
	wss_outb(chip, CS4231P(REGSEL), chip->mce_bit | 0x17);
	wss_outb(chip, CS4231P(REG),
		 reg | (chip->image[CS4236_EXT_REG] & 0x01));
L
Linus Torvalds 已提交
265
#if 1
266
	return wss_inb(chip, CS4231P(REG));
L
Linus Torvalds 已提交
267 268 269
#else
	{
		unsigned char res;
270
		res = wss_inb(chip, CS4231P(REG));
L
Linus Torvalds 已提交
271 272 273 274 275
		printk("ext in : reg = 0x%x, val = 0x%x\n", reg, res);
		return res;
	}
#endif
}
276
EXPORT_SYMBOL(snd_cs4236_ext_in);
L
Linus Torvalds 已提交
277 278 279

#if 0

280 281 282 283 284
static void snd_wss_debug(struct snd_wss *chip)
{
	printk(KERN_DEBUG
		"CS4231 REGS:      INDEX = 0x%02x  "
		"                 STATUS = 0x%02x\n",
285
					wss_inb(chip, CS4231P(REGSEL)),
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
					wss_inb(chip, CS4231P(STATUS)));
	printk(KERN_DEBUG
		"  0x00: left input      = 0x%02x  "
		"  0x10: alt 1 (CFIG 2)  = 0x%02x\n",
					snd_wss_in(chip, 0x00),
					snd_wss_in(chip, 0x10));
	printk(KERN_DEBUG
		"  0x01: right input     = 0x%02x  "
		"  0x11: alt 2 (CFIG 3)  = 0x%02x\n",
					snd_wss_in(chip, 0x01),
					snd_wss_in(chip, 0x11));
	printk(KERN_DEBUG
		"  0x02: GF1 left input  = 0x%02x  "
		"  0x12: left line in    = 0x%02x\n",
					snd_wss_in(chip, 0x02),
					snd_wss_in(chip, 0x12));
	printk(KERN_DEBUG
		"  0x03: GF1 right input = 0x%02x  "
		"  0x13: right line in   = 0x%02x\n",
					snd_wss_in(chip, 0x03),
					snd_wss_in(chip, 0x13));
	printk(KERN_DEBUG
		"  0x04: CD left input   = 0x%02x  "
		"  0x14: timer low       = 0x%02x\n",
					snd_wss_in(chip, 0x04),
					snd_wss_in(chip, 0x14));
	printk(KERN_DEBUG
		"  0x05: CD right input  = 0x%02x  "
		"  0x15: timer high      = 0x%02x\n",
					snd_wss_in(chip, 0x05),
					snd_wss_in(chip, 0x15));
	printk(KERN_DEBUG
		"  0x06: left output     = 0x%02x  "
		"  0x16: left MIC (PnP)  = 0x%02x\n",
					snd_wss_in(chip, 0x06),
					snd_wss_in(chip, 0x16));
	printk(KERN_DEBUG
		"  0x07: right output    = 0x%02x  "
		"  0x17: right MIC (PnP) = 0x%02x\n",
					snd_wss_in(chip, 0x07),
					snd_wss_in(chip, 0x17));
	printk(KERN_DEBUG
		"  0x08: playback format = 0x%02x  "
		"  0x18: IRQ status      = 0x%02x\n",
					snd_wss_in(chip, 0x08),
					snd_wss_in(chip, 0x18));
	printk(KERN_DEBUG
		"  0x09: iface (CFIG 1)  = 0x%02x  "
		"  0x19: left line out   = 0x%02x\n",
					snd_wss_in(chip, 0x09),
					snd_wss_in(chip, 0x19));
	printk(KERN_DEBUG
		"  0x0a: pin control     = 0x%02x  "
		"  0x1a: mono control    = 0x%02x\n",
					snd_wss_in(chip, 0x0a),
					snd_wss_in(chip, 0x1a));
	printk(KERN_DEBUG
		"  0x0b: init & status   = 0x%02x  "
		"  0x1b: right line out  = 0x%02x\n",
					snd_wss_in(chip, 0x0b),
					snd_wss_in(chip, 0x1b));
	printk(KERN_DEBUG
		"  0x0c: revision & mode = 0x%02x  "
		"  0x1c: record format   = 0x%02x\n",
					snd_wss_in(chip, 0x0c),
					snd_wss_in(chip, 0x1c));
	printk(KERN_DEBUG
		"  0x0d: loopback        = 0x%02x  "
		"  0x1d: var freq (PnP)  = 0x%02x\n",
					snd_wss_in(chip, 0x0d),
					snd_wss_in(chip, 0x1d));
	printk(KERN_DEBUG
		"  0x0e: ply upr count   = 0x%02x  "
		"  0x1e: ply lwr count   = 0x%02x\n",
					snd_wss_in(chip, 0x0e),
					snd_wss_in(chip, 0x1e));
	printk(KERN_DEBUG
		"  0x0f: rec upr count   = 0x%02x  "
		"  0x1f: rec lwr count   = 0x%02x\n",
					snd_wss_in(chip, 0x0f),
					snd_wss_in(chip, 0x1f));
L
Linus Torvalds 已提交
367 368 369 370 371 372 373 374
}

#endif

/*
 *  CS4231 detection / MCE routines
 */

375
static void snd_wss_busy_wait(struct snd_wss *chip)
L
Linus Torvalds 已提交
376 377 378 379 380
{
	int timeout;

	/* huh.. looks like this sequence is proper for CS4231A chip (GUS MAX) */
	for (timeout = 5; timeout > 0; timeout--)
381
		wss_inb(chip, CS4231P(REGSEL));
L
Linus Torvalds 已提交
382
	/* end of cleanup sequence */
383
	for (timeout = 25000;
384
	     timeout > 0 && (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT);
L
Linus Torvalds 已提交
385
	     timeout--)
386
		udelay(10);
L
Linus Torvalds 已提交
387 388
}

389
void snd_wss_mce_up(struct snd_wss *chip)
L
Linus Torvalds 已提交
390 391 392 393
{
	unsigned long flags;
	int timeout;

394
	snd_wss_wait(chip);
L
Linus Torvalds 已提交
395
#ifdef CONFIG_SND_DEBUG
396
	if (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT)
L
Linus Torvalds 已提交
397 398 399 400
		snd_printk("mce_up - auto calibration time out (0)\n");
#endif
	spin_lock_irqsave(&chip->reg_lock, flags);
	chip->mce_bit |= CS4231_MCE;
401
	timeout = wss_inb(chip, CS4231P(REGSEL));
L
Linus Torvalds 已提交
402 403 404
	if (timeout == 0x80)
		snd_printk("mce_up [0x%lx]: serious init problem - codec still busy\n", chip->port);
	if (!(timeout & CS4231_MCE))
405 406
		wss_outb(chip, CS4231P(REGSEL),
			 chip->mce_bit | (timeout & 0x1f));
L
Linus Torvalds 已提交
407 408
	spin_unlock_irqrestore(&chip->reg_lock, flags);
}
409
EXPORT_SYMBOL(snd_wss_mce_up);
L
Linus Torvalds 已提交
410

411
void snd_wss_mce_down(struct snd_wss *chip)
L
Linus Torvalds 已提交
412 413
{
	unsigned long flags;
414
	unsigned long end_time;
L
Linus Torvalds 已提交
415
	int timeout;
416
	int hw_mask = WSS_HW_CS4231_MASK | WSS_HW_CS4232_MASK | WSS_HW_AD1848;
L
Linus Torvalds 已提交
417

418
	snd_wss_busy_wait(chip);
419

L
Linus Torvalds 已提交
420
#ifdef CONFIG_SND_DEBUG
421
	if (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT)
L
Linus Torvalds 已提交
422 423 424 425
		snd_printk("mce_down [0x%lx] - auto calibration time out (0)\n", (long)CS4231P(REGSEL));
#endif
	spin_lock_irqsave(&chip->reg_lock, flags);
	chip->mce_bit &= ~CS4231_MCE;
426 427
	timeout = wss_inb(chip, CS4231P(REGSEL));
	wss_outb(chip, CS4231P(REGSEL), chip->mce_bit | (timeout & 0x1f));
L
Linus Torvalds 已提交
428 429 430
	spin_unlock_irqrestore(&chip->reg_lock, flags);
	if (timeout == 0x80)
		snd_printk("mce_down [0x%lx]: serious init problem - codec still busy\n", chip->port);
431
	if ((timeout & CS4231_MCE) == 0 || !(chip->hardware & hw_mask))
L
Linus Torvalds 已提交
432 433
		return;

434 435 436 437 438 439
	/*
	 * Wait for (possible -- during init auto-calibration may not be set)
	 * calibration process to start. Needs upto 5 sample periods on AD1848
	 * which at the slowest possible rate of 5.5125 kHz means 907 us.
	 */
	msleep(1);
440 441 442

	snd_printdd("(1) jiffies = %lu\n", jiffies);

443
	/* check condition up to 250 ms */
444
	end_time = jiffies + msecs_to_jiffies(250);
445
	while (snd_wss_in(chip, CS4231_TEST_INIT) &
446 447
		CS4231_CALIB_IN_PROGRESS) {

448
		if (time_after(jiffies, end_time)) {
449 450
			snd_printk(KERN_ERR "mce_down - "
					"auto calibration time out (2)\n");
L
Linus Torvalds 已提交
451 452
			return;
		}
453
		msleep(1);
L
Linus Torvalds 已提交
454
	}
455 456 457

	snd_printdd("(2) jiffies = %lu\n", jiffies);

458
	/* check condition up to 100 ms */
459
	end_time = jiffies + msecs_to_jiffies(100);
460
	while (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT) {
461
		if (time_after(jiffies, end_time)) {
L
Linus Torvalds 已提交
462 463 464
			snd_printk(KERN_ERR "mce_down - auto calibration time out (3)\n");
			return;
		}
465
		msleep(1);
L
Linus Torvalds 已提交
466
	}
467 468

	snd_printdd("(3) jiffies = %lu\n", jiffies);
469
	snd_printd("mce_down - exit = 0x%x\n", wss_inb(chip, CS4231P(REGSEL)));
L
Linus Torvalds 已提交
470
}
471
EXPORT_SYMBOL(snd_wss_mce_down);
L
Linus Torvalds 已提交
472

473
static unsigned int snd_wss_get_count(unsigned char format, unsigned int size)
L
Linus Torvalds 已提交
474 475 476 477 478 479 480 481 482 483 484 485 486 487
{
	switch (format & 0xe0) {
	case CS4231_LINEAR_16:
	case CS4231_LINEAR_16_BIG:
		size >>= 1;
		break;
	case CS4231_ADPCM_16:
		return size >> 2;
	}
	if (format & CS4231_STEREO)
		size >>= 1;
	return size;
}

488 489
static int snd_wss_trigger(struct snd_pcm_substream *substream,
			   int cmd)
L
Linus Torvalds 已提交
490
{
491
	struct snd_wss *chip = snd_pcm_substream_chip(substream);
L
Linus Torvalds 已提交
492 493
	int result = 0;
	unsigned int what;
494
	struct snd_pcm_substream *s;
L
Linus Torvalds 已提交
495 496 497 498 499 500 501 502 503 504 505 506 507 508
	int do_start;

	switch (cmd) {
	case SNDRV_PCM_TRIGGER_START:
	case SNDRV_PCM_TRIGGER_RESUME:
		do_start = 1; break;
	case SNDRV_PCM_TRIGGER_STOP:
	case SNDRV_PCM_TRIGGER_SUSPEND:
		do_start = 0; break;
	default:
		return -EINVAL;
	}

	what = 0;
509
	snd_pcm_group_for_each_entry(s, substream) {
L
Linus Torvalds 已提交
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
		if (s == chip->playback_substream) {
			what |= CS4231_PLAYBACK_ENABLE;
			snd_pcm_trigger_done(s, substream);
		} else if (s == chip->capture_substream) {
			what |= CS4231_RECORD_ENABLE;
			snd_pcm_trigger_done(s, substream);
		}
	}
	spin_lock(&chip->reg_lock);
	if (do_start) {
		chip->image[CS4231_IFACE_CTRL] |= what;
		if (chip->trigger)
			chip->trigger(chip, what, 1);
	} else {
		chip->image[CS4231_IFACE_CTRL] &= ~what;
		if (chip->trigger)
			chip->trigger(chip, what, 0);
	}
528
	snd_wss_out(chip, CS4231_IFACE_CTRL, chip->image[CS4231_IFACE_CTRL]);
L
Linus Torvalds 已提交
529 530
	spin_unlock(&chip->reg_lock);
#if 0
531
	snd_wss_debug(chip);
L
Linus Torvalds 已提交
532 533 534 535 536 537 538 539
#endif
	return result;
}

/*
 *  CODEC I/O
 */

540
static unsigned char snd_wss_get_rate(unsigned int rate)
L
Linus Torvalds 已提交
541 542 543
{
	int i;

544
	for (i = 0; i < ARRAY_SIZE(rates); i++)
L
Linus Torvalds 已提交
545 546 547
		if (rate == rates[i])
			return freq_bits[i];
	// snd_BUG();
548
	return freq_bits[ARRAY_SIZE(rates) - 1];
L
Linus Torvalds 已提交
549 550
}

551 552 553
static unsigned char snd_wss_get_format(struct snd_wss *chip,
					int format,
					int channels)
L
Linus Torvalds 已提交
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
{
	unsigned char rformat;

	rformat = CS4231_LINEAR_8;
	switch (format) {
	case SNDRV_PCM_FORMAT_MU_LAW:	rformat = CS4231_ULAW_8; break;
	case SNDRV_PCM_FORMAT_A_LAW:	rformat = CS4231_ALAW_8; break;
	case SNDRV_PCM_FORMAT_S16_LE:	rformat = CS4231_LINEAR_16; break;
	case SNDRV_PCM_FORMAT_S16_BE:	rformat = CS4231_LINEAR_16_BIG; break;
	case SNDRV_PCM_FORMAT_IMA_ADPCM:	rformat = CS4231_ADPCM_16; break;
	}
	if (channels > 1)
		rformat |= CS4231_STEREO;
#if 0
	snd_printk("get_format: 0x%x (mode=0x%x)\n", format, mode);
#endif
	return rformat;
}

573
static void snd_wss_calibrate_mute(struct snd_wss *chip, int mute)
L
Linus Torvalds 已提交
574 575 576 577 578 579 580 581 582 583
{
	unsigned long flags;

	mute = mute ? 1 : 0;
	spin_lock_irqsave(&chip->reg_lock, flags);
	if (chip->calibrate_mute == mute) {
		spin_unlock_irqrestore(&chip->reg_lock, flags);
		return;
	}
	if (!mute) {
584 585 586 587 588 589
		snd_wss_dout(chip, CS4231_LEFT_INPUT,
			     chip->image[CS4231_LEFT_INPUT]);
		snd_wss_dout(chip, CS4231_RIGHT_INPUT,
			     chip->image[CS4231_RIGHT_INPUT]);
		snd_wss_dout(chip, CS4231_LOOPBACK,
			     chip->image[CS4231_LOOPBACK]);
L
Linus Torvalds 已提交
590
	}
591 592 593 594 595 596 597 598 599 600 601 602
	snd_wss_dout(chip, CS4231_AUX1_LEFT_INPUT,
		     mute ? 0x80 : chip->image[CS4231_AUX1_LEFT_INPUT]);
	snd_wss_dout(chip, CS4231_AUX1_RIGHT_INPUT,
		     mute ? 0x80 : chip->image[CS4231_AUX1_RIGHT_INPUT]);
	snd_wss_dout(chip, CS4231_AUX2_LEFT_INPUT,
		     mute ? 0x80 : chip->image[CS4231_AUX2_LEFT_INPUT]);
	snd_wss_dout(chip, CS4231_AUX2_RIGHT_INPUT,
		     mute ? 0x80 : chip->image[CS4231_AUX2_RIGHT_INPUT]);
	snd_wss_dout(chip, CS4231_LEFT_OUTPUT,
		     mute ? 0x80 : chip->image[CS4231_LEFT_OUTPUT]);
	snd_wss_dout(chip, CS4231_RIGHT_OUTPUT,
		     mute ? 0x80 : chip->image[CS4231_RIGHT_OUTPUT]);
603 604 605 606 607 608 609 610
	if (!(chip->hardware & WSS_HW_AD1848_MASK)) {
		snd_wss_dout(chip, CS4231_LEFT_LINE_IN,
			     mute ? 0x80 : chip->image[CS4231_LEFT_LINE_IN]);
		snd_wss_dout(chip, CS4231_RIGHT_LINE_IN,
			     mute ? 0x80 : chip->image[CS4231_RIGHT_LINE_IN]);
		snd_wss_dout(chip, CS4231_MONO_CTRL,
			     mute ? 0xc0 : chip->image[CS4231_MONO_CTRL]);
	}
611 612 613 614 615 616 617 618 619
	if (chip->hardware == WSS_HW_INTERWAVE) {
		snd_wss_dout(chip, CS4231_LEFT_MIC_INPUT,
			     mute ? 0x80 : chip->image[CS4231_LEFT_MIC_INPUT]);
		snd_wss_dout(chip, CS4231_RIGHT_MIC_INPUT,
			     mute ? 0x80 : chip->image[CS4231_RIGHT_MIC_INPUT]);
		snd_wss_dout(chip, CS4231_LINE_LEFT_OUTPUT,
			mute ? 0x80 : chip->image[CS4231_LINE_LEFT_OUTPUT]);
		snd_wss_dout(chip, CS4231_LINE_RIGHT_OUTPUT,
			mute ? 0x80 : chip->image[CS4231_LINE_RIGHT_OUTPUT]);
L
Linus Torvalds 已提交
620 621 622 623 624
	}
	chip->calibrate_mute = mute;
	spin_unlock_irqrestore(&chip->reg_lock, flags);
}

625
static void snd_wss_playback_format(struct snd_wss *chip,
626
				       struct snd_pcm_hw_params *params,
L
Linus Torvalds 已提交
627 628 629 630 631
				       unsigned char pdfr)
{
	unsigned long flags;
	int full_calib = 1;

632
	mutex_lock(&chip->mce_mutex);
633 634 635
	snd_wss_calibrate_mute(chip, 1);
	if (chip->hardware == WSS_HW_CS4231A ||
	    (chip->hardware & WSS_HW_CS4232_MASK)) {
L
Linus Torvalds 已提交
636 637
		spin_lock_irqsave(&chip->reg_lock, flags);
		if ((chip->image[CS4231_PLAYBK_FORMAT] & 0x0f) == (pdfr & 0x0f)) {	/* rate is same? */
638 639 640 641 642 643 644
			snd_wss_out(chip, CS4231_ALT_FEATURE_1,
				    chip->image[CS4231_ALT_FEATURE_1] | 0x10);
			chip->image[CS4231_PLAYBK_FORMAT] = pdfr;
			snd_wss_out(chip, CS4231_PLAYBK_FORMAT,
				    chip->image[CS4231_PLAYBK_FORMAT]);
			snd_wss_out(chip, CS4231_ALT_FEATURE_1,
				    chip->image[CS4231_ALT_FEATURE_1] &= ~0x10);
L
Linus Torvalds 已提交
645 646 647 648 649 650
			udelay(100); /* Fixes audible clicks at least on GUS MAX */
			full_calib = 0;
		}
		spin_unlock_irqrestore(&chip->reg_lock, flags);
	}
	if (full_calib) {
651
		snd_wss_mce_up(chip);
L
Linus Torvalds 已提交
652
		spin_lock_irqsave(&chip->reg_lock, flags);
653 654 655 656
		if (chip->hardware != WSS_HW_INTERWAVE && !chip->single_dma) {
			if (chip->image[CS4231_IFACE_CTRL] & CS4231_RECORD_ENABLE)
				pdfr = (pdfr & 0xf0) |
				       (chip->image[CS4231_REC_FORMAT] & 0x0f);
L
Linus Torvalds 已提交
657
		} else {
658
			chip->image[CS4231_PLAYBK_FORMAT] = pdfr;
L
Linus Torvalds 已提交
659
		}
660
		snd_wss_out(chip, CS4231_PLAYBK_FORMAT, pdfr);
L
Linus Torvalds 已提交
661
		spin_unlock_irqrestore(&chip->reg_lock, flags);
662
		if (chip->hardware == WSS_HW_OPL3SA2)
663
			udelay(100);	/* this seems to help */
664
		snd_wss_mce_down(chip);
L
Linus Torvalds 已提交
665
	}
666
	snd_wss_calibrate_mute(chip, 0);
667
	mutex_unlock(&chip->mce_mutex);
L
Linus Torvalds 已提交
668 669
}

670 671 672
static void snd_wss_capture_format(struct snd_wss *chip,
				   struct snd_pcm_hw_params *params,
				   unsigned char cdfr)
L
Linus Torvalds 已提交
673 674 675 676
{
	unsigned long flags;
	int full_calib = 1;

677
	mutex_lock(&chip->mce_mutex);
678 679 680
	snd_wss_calibrate_mute(chip, 1);
	if (chip->hardware == WSS_HW_CS4231A ||
	    (chip->hardware & WSS_HW_CS4232_MASK)) {
L
Linus Torvalds 已提交
681 682 683
		spin_lock_irqsave(&chip->reg_lock, flags);
		if ((chip->image[CS4231_PLAYBK_FORMAT] & 0x0f) == (cdfr & 0x0f) ||	/* rate is same? */
		    (chip->image[CS4231_IFACE_CTRL] & CS4231_PLAYBACK_ENABLE)) {
684 685 686 687 688 689
			snd_wss_out(chip, CS4231_ALT_FEATURE_1,
				chip->image[CS4231_ALT_FEATURE_1] | 0x20);
			snd_wss_out(chip, CS4231_REC_FORMAT,
				chip->image[CS4231_REC_FORMAT] = cdfr);
			snd_wss_out(chip, CS4231_ALT_FEATURE_1,
				chip->image[CS4231_ALT_FEATURE_1] &= ~0x20);
L
Linus Torvalds 已提交
690 691 692 693 694
			full_calib = 0;
		}
		spin_unlock_irqrestore(&chip->reg_lock, flags);
	}
	if (full_calib) {
695
		snd_wss_mce_up(chip);
L
Linus Torvalds 已提交
696
		spin_lock_irqsave(&chip->reg_lock, flags);
697 698 699 700 701 702 703 704 705 706 707 708
		if (chip->hardware != WSS_HW_INTERWAVE &&
		    !(chip->image[CS4231_IFACE_CTRL] & CS4231_PLAYBACK_ENABLE)) {
			if (chip->single_dma)
				snd_wss_out(chip, CS4231_PLAYBK_FORMAT, cdfr);
			else
				snd_wss_out(chip, CS4231_PLAYBK_FORMAT,
				   (chip->image[CS4231_PLAYBK_FORMAT] & 0xf0) |
				   (cdfr & 0x0f));
			spin_unlock_irqrestore(&chip->reg_lock, flags);
			snd_wss_mce_down(chip);
			snd_wss_mce_up(chip);
			spin_lock_irqsave(&chip->reg_lock, flags);
L
Linus Torvalds 已提交
709
		}
710 711 712 713
		if (chip->hardware & WSS_HW_AD1848_MASK)
			snd_wss_out(chip, CS4231_PLAYBK_FORMAT, cdfr);
		else
			snd_wss_out(chip, CS4231_REC_FORMAT, cdfr);
L
Linus Torvalds 已提交
714
		spin_unlock_irqrestore(&chip->reg_lock, flags);
715
		snd_wss_mce_down(chip);
L
Linus Torvalds 已提交
716
	}
717
	snd_wss_calibrate_mute(chip, 0);
718
	mutex_unlock(&chip->mce_mutex);
L
Linus Torvalds 已提交
719 720 721 722 723 724
}

/*
 *  Timer interface
 */

725
static unsigned long snd_wss_timer_resolution(struct snd_timer *timer)
L
Linus Torvalds 已提交
726
{
727 728
	struct snd_wss *chip = snd_timer_chip(timer);
	if (chip->hardware & WSS_HW_CS4236B_MASK)
L
Linus Torvalds 已提交
729 730 731 732 733
		return 14467;
	else
		return chip->image[CS4231_PLAYBK_FORMAT] & 1 ? 9969 : 9920;
}

734
static int snd_wss_timer_start(struct snd_timer *timer)
L
Linus Torvalds 已提交
735 736 737
{
	unsigned long flags;
	unsigned int ticks;
738
	struct snd_wss *chip = snd_timer_chip(timer);
L
Linus Torvalds 已提交
739 740 741 742 743
	spin_lock_irqsave(&chip->reg_lock, flags);
	ticks = timer->sticks;
	if ((chip->image[CS4231_ALT_FEATURE_1] & CS4231_TIMER_ENABLE) == 0 ||
	    (unsigned char)(ticks >> 8) != chip->image[CS4231_TIMER_HIGH] ||
	    (unsigned char)ticks != chip->image[CS4231_TIMER_LOW]) {
744 745 746 747 748 749 750 751 752
		chip->image[CS4231_TIMER_HIGH] = (unsigned char) (ticks >> 8);
		snd_wss_out(chip, CS4231_TIMER_HIGH,
			    chip->image[CS4231_TIMER_HIGH]);
		chip->image[CS4231_TIMER_LOW] = (unsigned char) ticks;
		snd_wss_out(chip, CS4231_TIMER_LOW,
			    chip->image[CS4231_TIMER_LOW]);
		snd_wss_out(chip, CS4231_ALT_FEATURE_1,
			    chip->image[CS4231_ALT_FEATURE_1] |
			    CS4231_TIMER_ENABLE);
L
Linus Torvalds 已提交
753 754 755 756 757
	}
	spin_unlock_irqrestore(&chip->reg_lock, flags);
	return 0;
}

758
static int snd_wss_timer_stop(struct snd_timer *timer)
L
Linus Torvalds 已提交
759 760
{
	unsigned long flags;
761
	struct snd_wss *chip = snd_timer_chip(timer);
L
Linus Torvalds 已提交
762
	spin_lock_irqsave(&chip->reg_lock, flags);
763 764 765
	chip->image[CS4231_ALT_FEATURE_1] &= ~CS4231_TIMER_ENABLE;
	snd_wss_out(chip, CS4231_ALT_FEATURE_1,
		    chip->image[CS4231_ALT_FEATURE_1]);
L
Linus Torvalds 已提交
766 767 768 769
	spin_unlock_irqrestore(&chip->reg_lock, flags);
	return 0;
}

770
static void snd_wss_init(struct snd_wss *chip)
L
Linus Torvalds 已提交
771 772 773
{
	unsigned long flags;

774
	snd_wss_mce_down(chip);
L
Linus Torvalds 已提交
775 776 777 778

#ifdef SNDRV_DEBUG_MCE
	snd_printk("init: (1)\n");
#endif
779
	snd_wss_mce_up(chip);
L
Linus Torvalds 已提交
780
	spin_lock_irqsave(&chip->reg_lock, flags);
781 782 783 784 785
	chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_PLAYBACK_ENABLE |
					    CS4231_PLAYBACK_PIO |
					    CS4231_RECORD_ENABLE |
					    CS4231_RECORD_PIO |
					    CS4231_CALIB_MODE);
L
Linus Torvalds 已提交
786
	chip->image[CS4231_IFACE_CTRL] |= CS4231_AUTOCALIB;
787
	snd_wss_out(chip, CS4231_IFACE_CTRL, chip->image[CS4231_IFACE_CTRL]);
L
Linus Torvalds 已提交
788
	spin_unlock_irqrestore(&chip->reg_lock, flags);
789
	snd_wss_mce_down(chip);
L
Linus Torvalds 已提交
790 791 792 793 794

#ifdef SNDRV_DEBUG_MCE
	snd_printk("init: (2)\n");
#endif

795
	snd_wss_mce_up(chip);
L
Linus Torvalds 已提交
796
	spin_lock_irqsave(&chip->reg_lock, flags);
797 798
	snd_wss_out(chip,
		    CS4231_ALT_FEATURE_1, chip->image[CS4231_ALT_FEATURE_1]);
L
Linus Torvalds 已提交
799
	spin_unlock_irqrestore(&chip->reg_lock, flags);
800
	snd_wss_mce_down(chip);
L
Linus Torvalds 已提交
801 802

#ifdef SNDRV_DEBUG_MCE
803 804
	snd_printk("init: (3) - afei = 0x%x\n",
		   chip->image[CS4231_ALT_FEATURE_1]);
L
Linus Torvalds 已提交
805 806 807
#endif

	spin_lock_irqsave(&chip->reg_lock, flags);
808 809
	snd_wss_out(chip, CS4231_ALT_FEATURE_2,
		    chip->image[CS4231_ALT_FEATURE_2]);
L
Linus Torvalds 已提交
810 811
	spin_unlock_irqrestore(&chip->reg_lock, flags);

812
	snd_wss_mce_up(chip);
L
Linus Torvalds 已提交
813
	spin_lock_irqsave(&chip->reg_lock, flags);
814 815
	snd_wss_out(chip, CS4231_PLAYBK_FORMAT,
		    chip->image[CS4231_PLAYBK_FORMAT]);
L
Linus Torvalds 已提交
816
	spin_unlock_irqrestore(&chip->reg_lock, flags);
817
	snd_wss_mce_down(chip);
L
Linus Torvalds 已提交
818 819 820 821 822

#ifdef SNDRV_DEBUG_MCE
	snd_printk("init: (4)\n");
#endif

823
	snd_wss_mce_up(chip);
L
Linus Torvalds 已提交
824
	spin_lock_irqsave(&chip->reg_lock, flags);
825 826 827
	if (!(chip->hardware & WSS_HW_AD1848_MASK))
		snd_wss_out(chip, CS4231_REC_FORMAT,
			    chip->image[CS4231_REC_FORMAT]);
L
Linus Torvalds 已提交
828
	spin_unlock_irqrestore(&chip->reg_lock, flags);
829
	snd_wss_mce_down(chip);
L
Linus Torvalds 已提交
830 831 832 833 834 835

#ifdef SNDRV_DEBUG_MCE
	snd_printk("init: (5)\n");
#endif
}

836
static int snd_wss_open(struct snd_wss *chip, unsigned int mode)
L
Linus Torvalds 已提交
837 838 839
{
	unsigned long flags;

840
	mutex_lock(&chip->open_mutex);
L
Linus Torvalds 已提交
841
	if ((chip->mode & mode) ||
842
	    ((chip->mode & WSS_MODE_OPEN) && chip->single_dma)) {
843
		mutex_unlock(&chip->open_mutex);
L
Linus Torvalds 已提交
844 845
		return -EAGAIN;
	}
846
	if (chip->mode & WSS_MODE_OPEN) {
L
Linus Torvalds 已提交
847
		chip->mode |= mode;
848
		mutex_unlock(&chip->open_mutex);
L
Linus Torvalds 已提交
849 850 851 852
		return 0;
	}
	/* ok. now enable and ack CODEC IRQ */
	spin_lock_irqsave(&chip->reg_lock, flags);
853 854 855 856 857 858 859
	if (!(chip->hardware & WSS_HW_AD1848_MASK)) {
		snd_wss_out(chip, CS4231_IRQ_STATUS,
			    CS4231_PLAYBACK_IRQ |
			    CS4231_RECORD_IRQ |
			    CS4231_TIMER_IRQ);
		snd_wss_out(chip, CS4231_IRQ_STATUS, 0);
	}
860 861
	wss_outb(chip, CS4231P(STATUS), 0);	/* clear IRQ */
	wss_outb(chip, CS4231P(STATUS), 0);	/* clear IRQ */
L
Linus Torvalds 已提交
862
	chip->image[CS4231_PIN_CTRL] |= CS4231_IRQ_ENABLE;
863
	snd_wss_out(chip, CS4231_PIN_CTRL, chip->image[CS4231_PIN_CTRL]);
864 865 866 867 868 869 870
	if (!(chip->hardware & WSS_HW_AD1848_MASK)) {
		snd_wss_out(chip, CS4231_IRQ_STATUS,
			    CS4231_PLAYBACK_IRQ |
			    CS4231_RECORD_IRQ |
			    CS4231_TIMER_IRQ);
		snd_wss_out(chip, CS4231_IRQ_STATUS, 0);
	}
L
Linus Torvalds 已提交
871 872 873
	spin_unlock_irqrestore(&chip->reg_lock, flags);

	chip->mode = mode;
874
	mutex_unlock(&chip->open_mutex);
L
Linus Torvalds 已提交
875 876 877
	return 0;
}

878
static void snd_wss_close(struct snd_wss *chip, unsigned int mode)
L
Linus Torvalds 已提交
879 880 881
{
	unsigned long flags;

882
	mutex_lock(&chip->open_mutex);
L
Linus Torvalds 已提交
883
	chip->mode &= ~mode;
884
	if (chip->mode & WSS_MODE_OPEN) {
885
		mutex_unlock(&chip->open_mutex);
L
Linus Torvalds 已提交
886 887
		return;
	}
888
	snd_wss_calibrate_mute(chip, 1);
L
Linus Torvalds 已提交
889 890 891

	/* disable IRQ */
	spin_lock_irqsave(&chip->reg_lock, flags);
892 893
	if (!(chip->hardware & WSS_HW_AD1848_MASK))
		snd_wss_out(chip, CS4231_IRQ_STATUS, 0);
894 895
	wss_outb(chip, CS4231P(STATUS), 0);	/* clear IRQ */
	wss_outb(chip, CS4231P(STATUS), 0);	/* clear IRQ */
L
Linus Torvalds 已提交
896
	chip->image[CS4231_PIN_CTRL] &= ~CS4231_IRQ_ENABLE;
897
	snd_wss_out(chip, CS4231_PIN_CTRL, chip->image[CS4231_PIN_CTRL]);
L
Linus Torvalds 已提交
898 899 900 901 902 903

	/* now disable record & playback */

	if (chip->image[CS4231_IFACE_CTRL] & (CS4231_PLAYBACK_ENABLE | CS4231_PLAYBACK_PIO |
					       CS4231_RECORD_ENABLE | CS4231_RECORD_PIO)) {
		spin_unlock_irqrestore(&chip->reg_lock, flags);
904
		snd_wss_mce_up(chip);
L
Linus Torvalds 已提交
905 906 907
		spin_lock_irqsave(&chip->reg_lock, flags);
		chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_PLAYBACK_ENABLE | CS4231_PLAYBACK_PIO |
						     CS4231_RECORD_ENABLE | CS4231_RECORD_PIO);
908 909
		snd_wss_out(chip, CS4231_IFACE_CTRL,
			    chip->image[CS4231_IFACE_CTRL]);
L
Linus Torvalds 已提交
910
		spin_unlock_irqrestore(&chip->reg_lock, flags);
911
		snd_wss_mce_down(chip);
L
Linus Torvalds 已提交
912 913 914 915
		spin_lock_irqsave(&chip->reg_lock, flags);
	}

	/* clear IRQ again */
916 917
	if (!(chip->hardware & WSS_HW_AD1848_MASK))
		snd_wss_out(chip, CS4231_IRQ_STATUS, 0);
918 919
	wss_outb(chip, CS4231P(STATUS), 0);	/* clear IRQ */
	wss_outb(chip, CS4231P(STATUS), 0);	/* clear IRQ */
L
Linus Torvalds 已提交
920 921
	spin_unlock_irqrestore(&chip->reg_lock, flags);

922
	snd_wss_calibrate_mute(chip, 0);
L
Linus Torvalds 已提交
923 924

	chip->mode = 0;
925
	mutex_unlock(&chip->open_mutex);
L
Linus Torvalds 已提交
926 927 928 929 930 931
}

/*
 *  timer open/close
 */

932
static int snd_wss_timer_open(struct snd_timer *timer)
L
Linus Torvalds 已提交
933
{
934 935
	struct snd_wss *chip = snd_timer_chip(timer);
	snd_wss_open(chip, WSS_MODE_TIMER);
L
Linus Torvalds 已提交
936 937 938
	return 0;
}

939
static int snd_wss_timer_close(struct snd_timer *timer)
L
Linus Torvalds 已提交
940
{
941 942
	struct snd_wss *chip = snd_timer_chip(timer);
	snd_wss_close(chip, WSS_MODE_TIMER);
L
Linus Torvalds 已提交
943 944 945
	return 0;
}

946
static struct snd_timer_hardware snd_wss_timer_table =
L
Linus Torvalds 已提交
947 948 949 950
{
	.flags =	SNDRV_TIMER_HW_AUTO,
	.resolution =	9945,
	.ticks =	65535,
951 952 953 954 955
	.open =		snd_wss_timer_open,
	.close =	snd_wss_timer_close,
	.c_resolution = snd_wss_timer_resolution,
	.start =	snd_wss_timer_start,
	.stop =		snd_wss_timer_stop,
L
Linus Torvalds 已提交
956 957 958 959 960 961
};

/*
 *  ok.. exported functions..
 */

962
static int snd_wss_playback_hw_params(struct snd_pcm_substream *substream,
963
					 struct snd_pcm_hw_params *hw_params)
L
Linus Torvalds 已提交
964
{
965
	struct snd_wss *chip = snd_pcm_substream_chip(substream);
L
Linus Torvalds 已提交
966 967 968 969 970
	unsigned char new_pdfr;
	int err;

	if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
		return err;
971 972 973
	new_pdfr = snd_wss_get_format(chip, params_format(hw_params),
				params_channels(hw_params)) |
				snd_wss_get_rate(params_rate(hw_params));
L
Linus Torvalds 已提交
974 975 976 977
	chip->set_playback_format(chip, hw_params, new_pdfr);
	return 0;
}

978
static int snd_wss_playback_hw_free(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
979 980 981 982
{
	return snd_pcm_lib_free_pages(substream);
}

983
static int snd_wss_playback_prepare(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
984
{
985
	struct snd_wss *chip = snd_pcm_substream_chip(substream);
986
	struct snd_pcm_runtime *runtime = substream->runtime;
L
Linus Torvalds 已提交
987 988 989 990 991 992 993 994
	unsigned long flags;
	unsigned int size = snd_pcm_lib_buffer_bytes(substream);
	unsigned int count = snd_pcm_lib_period_bytes(substream);

	spin_lock_irqsave(&chip->reg_lock, flags);
	chip->p_dma_size = size;
	chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_PLAYBACK_ENABLE | CS4231_PLAYBACK_PIO);
	snd_dma_program(chip->dma1, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
995 996 997
	count = snd_wss_get_count(chip->image[CS4231_PLAYBK_FORMAT], count) - 1;
	snd_wss_out(chip, CS4231_PLY_LWR_CNT, (unsigned char) count);
	snd_wss_out(chip, CS4231_PLY_UPR_CNT, (unsigned char) (count >> 8));
L
Linus Torvalds 已提交
998 999
	spin_unlock_irqrestore(&chip->reg_lock, flags);
#if 0
1000
	snd_wss_debug(chip);
L
Linus Torvalds 已提交
1001 1002 1003 1004
#endif
	return 0;
}

1005
static int snd_wss_capture_hw_params(struct snd_pcm_substream *substream,
1006
					struct snd_pcm_hw_params *hw_params)
L
Linus Torvalds 已提交
1007
{
1008
	struct snd_wss *chip = snd_pcm_substream_chip(substream);
L
Linus Torvalds 已提交
1009 1010 1011 1012 1013
	unsigned char new_cdfr;
	int err;

	if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
		return err;
1014 1015 1016
	new_cdfr = snd_wss_get_format(chip, params_format(hw_params),
			   params_channels(hw_params)) |
			   snd_wss_get_rate(params_rate(hw_params));
L
Linus Torvalds 已提交
1017 1018 1019 1020
	chip->set_capture_format(chip, hw_params, new_cdfr);
	return 0;
}

1021
static int snd_wss_capture_hw_free(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
1022 1023 1024 1025
{
	return snd_pcm_lib_free_pages(substream);
}

1026
static int snd_wss_capture_prepare(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
1027
{
1028
	struct snd_wss *chip = snd_pcm_substream_chip(substream);
1029
	struct snd_pcm_runtime *runtime = substream->runtime;
L
Linus Torvalds 已提交
1030 1031 1032 1033 1034 1035 1036 1037
	unsigned long flags;
	unsigned int size = snd_pcm_lib_buffer_bytes(substream);
	unsigned int count = snd_pcm_lib_period_bytes(substream);

	spin_lock_irqsave(&chip->reg_lock, flags);
	chip->c_dma_size = size;
	chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_RECORD_ENABLE | CS4231_RECORD_PIO);
	snd_dma_program(chip->dma2, runtime->dma_addr, size, DMA_MODE_READ | DMA_AUTOINIT);
1038 1039 1040 1041 1042 1043 1044
	if (chip->hardware & WSS_HW_AD1848_MASK)
		count = snd_wss_get_count(chip->image[CS4231_PLAYBK_FORMAT],
					  count);
	else
		count = snd_wss_get_count(chip->image[CS4231_REC_FORMAT],
					  count);
	count--;
1045 1046 1047 1048
	if (chip->single_dma && chip->hardware != WSS_HW_INTERWAVE) {
		snd_wss_out(chip, CS4231_PLY_LWR_CNT, (unsigned char) count);
		snd_wss_out(chip, CS4231_PLY_UPR_CNT,
			    (unsigned char) (count >> 8));
L
Linus Torvalds 已提交
1049
	} else {
1050 1051 1052
		snd_wss_out(chip, CS4231_REC_LWR_CNT, (unsigned char) count);
		snd_wss_out(chip, CS4231_REC_UPR_CNT,
			    (unsigned char) (count >> 8));
L
Linus Torvalds 已提交
1053 1054 1055 1056 1057
	}
	spin_unlock_irqrestore(&chip->reg_lock, flags);
	return 0;
}

1058
void snd_wss_overrange(struct snd_wss *chip)
L
Linus Torvalds 已提交
1059 1060 1061 1062 1063
{
	unsigned long flags;
	unsigned char res;

	spin_lock_irqsave(&chip->reg_lock, flags);
1064
	res = snd_wss_in(chip, CS4231_TEST_INIT);
L
Linus Torvalds 已提交
1065 1066 1067 1068
	spin_unlock_irqrestore(&chip->reg_lock, flags);
	if (res & (0x08 | 0x02))	/* detect overrange only above 0dB; may be user selectable? */
		chip->capture_substream->runtime->overrange++;
}
1069
EXPORT_SYMBOL(snd_wss_overrange);
L
Linus Torvalds 已提交
1070

1071
irqreturn_t snd_wss_interrupt(int irq, void *dev_id)
L
Linus Torvalds 已提交
1072
{
1073
	struct snd_wss *chip = dev_id;
L
Linus Torvalds 已提交
1074 1075
	unsigned char status;

1076 1077 1078 1079 1080
	if (chip->hardware & WSS_HW_AD1848_MASK)
		/* pretend it was the only possible irq for AD1848 */
		status = CS4231_PLAYBACK_IRQ;
	else
		status = snd_wss_in(chip, CS4231_IRQ_STATUS);
L
Linus Torvalds 已提交
1081 1082 1083
	if (status & CS4231_TIMER_IRQ) {
		if (chip->timer)
			snd_timer_interrupt(chip->timer, chip->timer->sticks);
1084
	}
1085
	if (chip->single_dma && chip->hardware != WSS_HW_INTERWAVE) {
L
Linus Torvalds 已提交
1086
		if (status & CS4231_PLAYBACK_IRQ) {
1087
			if (chip->mode & WSS_MODE_PLAY) {
L
Linus Torvalds 已提交
1088 1089 1090
				if (chip->playback_substream)
					snd_pcm_period_elapsed(chip->playback_substream);
			}
1091
			if (chip->mode & WSS_MODE_RECORD) {
L
Linus Torvalds 已提交
1092
				if (chip->capture_substream) {
1093
					snd_wss_overrange(chip);
L
Linus Torvalds 已提交
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
					snd_pcm_period_elapsed(chip->capture_substream);
				}
			}
		}
	} else {
		if (status & CS4231_PLAYBACK_IRQ) {
			if (chip->playback_substream)
				snd_pcm_period_elapsed(chip->playback_substream);
		}
		if (status & CS4231_RECORD_IRQ) {
			if (chip->capture_substream) {
1105
				snd_wss_overrange(chip);
L
Linus Torvalds 已提交
1106 1107 1108 1109 1110 1111
				snd_pcm_period_elapsed(chip->capture_substream);
			}
		}
	}

	spin_lock(&chip->reg_lock);
1112 1113 1114 1115 1116
	status = ~CS4231_ALL_IRQS | ~status;
	if (chip->hardware & WSS_HW_AD1848_MASK)
		wss_outb(chip, CS4231P(STATUS), 0);
	else
		snd_wss_outm(chip, CS4231_IRQ_STATUS, status, 0);
L
Linus Torvalds 已提交
1117 1118 1119
	spin_unlock(&chip->reg_lock);
	return IRQ_HANDLED;
}
1120
EXPORT_SYMBOL(snd_wss_interrupt);
L
Linus Torvalds 已提交
1121

1122
static snd_pcm_uframes_t snd_wss_playback_pointer(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
1123
{
1124
	struct snd_wss *chip = snd_pcm_substream_chip(substream);
L
Linus Torvalds 已提交
1125 1126 1127 1128 1129 1130 1131 1132
	size_t ptr;

	if (!(chip->image[CS4231_IFACE_CTRL] & CS4231_PLAYBACK_ENABLE))
		return 0;
	ptr = snd_dma_pointer(chip->dma1, chip->p_dma_size);
	return bytes_to_frames(substream->runtime, ptr);
}

1133
static snd_pcm_uframes_t snd_wss_capture_pointer(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
1134
{
1135
	struct snd_wss *chip = snd_pcm_substream_chip(substream);
L
Linus Torvalds 已提交
1136
	size_t ptr;
1137

L
Linus Torvalds 已提交
1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
	if (!(chip->image[CS4231_IFACE_CTRL] & CS4231_RECORD_ENABLE))
		return 0;
	ptr = snd_dma_pointer(chip->dma2, chip->c_dma_size);
	return bytes_to_frames(substream->runtime, ptr);
}

/*

 */

1148
static int snd_ad1848_probe(struct snd_wss *chip)
L
Linus Torvalds 已提交
1149
{
1150
	unsigned long timeout = jiffies + msecs_to_jiffies(1000);
L
Linus Torvalds 已提交
1151
	unsigned long flags;
1152 1153 1154 1155
	unsigned char r;
	unsigned short hardware = 0;
	int err = 0;
	int i;
L
Linus Torvalds 已提交
1156

1157 1158 1159 1160
	while (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT) {
		if (time_after(jiffies, timeout))
			return -ENODEV;
		cond_resched();
L
Linus Torvalds 已提交
1161
	}
1162
	spin_lock_irqsave(&chip->reg_lock, flags);
1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182

	/* set CS423x MODE 1 */
	snd_wss_out(chip, CS4231_MISC_INFO, 0);

	snd_wss_out(chip, CS4231_RIGHT_INPUT, 0x45); /* 0x55 & ~0x10 */
	r = snd_wss_in(chip, CS4231_RIGHT_INPUT);
	if (r != 0x45) {
		/* RMGE always high on AD1847 */
		if ((r & ~CS4231_ENABLE_MIC_GAIN) != 0x45) {
			err = -ENODEV;
			goto out;
		}
		hardware = WSS_HW_AD1847;
	} else {
		snd_wss_out(chip, CS4231_LEFT_INPUT,  0xaa);
		r = snd_wss_in(chip, CS4231_LEFT_INPUT);
		/* L/RMGE always low on AT2320 */
		if ((r | CS4231_ENABLE_MIC_GAIN) != 0xaa) {
			err = -ENODEV;
			goto out;
1183 1184
		}
	}
1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196

	/* clear pending IRQ */
	wss_inb(chip, CS4231P(STATUS));
	wss_outb(chip, CS4231P(STATUS), 0);
	mb();

	if ((chip->hardware & WSS_HW_TYPE_MASK) != WSS_HW_DETECT)
		goto out;

	if (hardware) {
		chip->hardware = hardware;
		goto out;
1197 1198
	}

1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
	r = snd_wss_in(chip, CS4231_MISC_INFO);

	/* set CS423x MODE 2 */
	snd_wss_out(chip, CS4231_MISC_INFO, CS4231_MODE2);
	for (i = 0; i < 16; i++) {
		if (snd_wss_in(chip, i) != snd_wss_in(chip, 16 + i)) {
			/* we have more than 16 registers: check ID */
			if ((r & 0xf) != 0xa)
				goto out_mode;
			/*
			 * on CMI8330, CS4231_VERSION is volume control and
			 * can be set to 0
			 */
			snd_wss_dout(chip, CS4231_VERSION, 0);
			r = snd_wss_in(chip, CS4231_VERSION) & 0xe7;
			if (!r)
				chip->hardware = WSS_HW_CMI8330;
			goto out_mode;
		}
	}
	if (r & 0x80)
		chip->hardware = WSS_HW_CS4248;
	else
		chip->hardware = WSS_HW_AD1848;
out_mode:
	snd_wss_out(chip, CS4231_MISC_INFO, 0);
out:
1226
	spin_unlock_irqrestore(&chip->reg_lock, flags);
1227
	return err;
1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239
}

static int snd_wss_probe(struct snd_wss *chip)
{
	unsigned long flags;
	int i, id, rev, regnum;
	unsigned char *ptr;
	unsigned int hw;

	id = snd_ad1848_probe(chip);
	if (id < 0)
		return id;
L
Linus Torvalds 已提交
1240

1241 1242
	hw = chip->hardware;
	if ((hw & WSS_HW_TYPE_MASK) == WSS_HW_DETECT) {
1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
		for (i = 0; i < 50; i++) {
			mb();
			if (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT)
				msleep(2);
			else {
				spin_lock_irqsave(&chip->reg_lock, flags);
				snd_wss_out(chip, CS4231_MISC_INFO,
					    CS4231_MODE2);
				id = snd_wss_in(chip, CS4231_MISC_INFO) & 0x0f;
				spin_unlock_irqrestore(&chip->reg_lock, flags);
				if (id == 0x0a)
					break;	/* this is valid value */
			}
		}
		snd_printdd("wss: port = 0x%lx, id = 0x%x\n", chip->port, id);
		if (id != 0x0a)
			return -ENODEV;	/* no valid device found */

1261
		rev = snd_wss_in(chip, CS4231_VERSION) & 0xe7;
L
Linus Torvalds 已提交
1262 1263
		snd_printdd("CS4231: VERSION (I25) = 0x%x\n", rev);
		if (rev == 0x80) {
1264 1265 1266 1267
			unsigned char tmp = snd_wss_in(chip, 23);
			snd_wss_out(chip, 23, ~tmp);
			if (snd_wss_in(chip, 23) != tmp)
				chip->hardware = WSS_HW_AD1845;
L
Linus Torvalds 已提交
1268
			else
1269
				chip->hardware = WSS_HW_CS4231;
L
Linus Torvalds 已提交
1270
		} else if (rev == 0xa0) {
1271
			chip->hardware = WSS_HW_CS4231A;
L
Linus Torvalds 已提交
1272
		} else if (rev == 0xa2) {
1273
			chip->hardware = WSS_HW_CS4232;
L
Linus Torvalds 已提交
1274
		} else if (rev == 0xb2) {
1275
			chip->hardware = WSS_HW_CS4232A;
L
Linus Torvalds 已提交
1276
		} else if (rev == 0x83) {
1277
			chip->hardware = WSS_HW_CS4236;
L
Linus Torvalds 已提交
1278
		} else if (rev == 0x03) {
1279
			chip->hardware = WSS_HW_CS4236B;
L
Linus Torvalds 已提交
1280 1281 1282 1283 1284 1285
		} else {
			snd_printk("unknown CS chip with version 0x%x\n", rev);
			return -ENODEV;		/* unknown CS4231 chip? */
		}
	}
	spin_lock_irqsave(&chip->reg_lock, flags);
1286 1287
	wss_inb(chip, CS4231P(STATUS));	/* clear any pendings IRQ */
	wss_outb(chip, CS4231P(STATUS), 0);
L
Linus Torvalds 已提交
1288 1289 1290
	mb();
	spin_unlock_irqrestore(&chip->reg_lock, flags);

1291 1292
	if (!(chip->hardware & WSS_HW_AD1848_MASK))
		chip->image[CS4231_MISC_INFO] = CS4231_MODE2;
L
Linus Torvalds 已提交
1293
	switch (chip->hardware) {
1294
	case WSS_HW_INTERWAVE:
L
Linus Torvalds 已提交
1295 1296
		chip->image[CS4231_MISC_INFO] = CS4231_IW_MODE3;
		break;
1297 1298 1299 1300 1301 1302
	case WSS_HW_CS4235:
	case WSS_HW_CS4236B:
	case WSS_HW_CS4237B:
	case WSS_HW_CS4238B:
	case WSS_HW_CS4239:
		if (hw == WSS_HW_DETECT3)
L
Linus Torvalds 已提交
1303 1304
			chip->image[CS4231_MISC_INFO] = CS4231_4236_MODE3;
		else
1305
			chip->hardware = WSS_HW_CS4236;
L
Linus Torvalds 已提交
1306 1307 1308 1309 1310 1311
		break;
	}

	chip->image[CS4231_IFACE_CTRL] =
	    (chip->image[CS4231_IFACE_CTRL] & ~CS4231_SINGLE_DMA) |
	    (chip->single_dma ? CS4231_SINGLE_DMA : 0);
1312
	if (chip->hardware != WSS_HW_OPTI93X) {
1313 1314
		chip->image[CS4231_ALT_FEATURE_1] = 0x80;
		chip->image[CS4231_ALT_FEATURE_2] =
1315
			chip->hardware == WSS_HW_INTERWAVE ? 0xc2 : 0x01;
1316
	}
L
Linus Torvalds 已提交
1317
	ptr = (unsigned char *) &chip->image;
1318
	regnum = (chip->hardware & WSS_HW_AD1848_MASK) ? 16 : 32;
1319
	snd_wss_mce_down(chip);
L
Linus Torvalds 已提交
1320
	spin_lock_irqsave(&chip->reg_lock, flags);
1321
	for (i = 0; i < regnum; i++)	/* ok.. fill all registers */
1322
		snd_wss_out(chip, i, *ptr++);
L
Linus Torvalds 已提交
1323
	spin_unlock_irqrestore(&chip->reg_lock, flags);
1324 1325
	snd_wss_mce_up(chip);
	snd_wss_mce_down(chip);
L
Linus Torvalds 已提交
1326 1327 1328 1329

	mdelay(2);

	/* ok.. try check hardware version for CS4236+ chips */
1330 1331
	if ((hw & WSS_HW_TYPE_MASK) == WSS_HW_DETECT) {
		if (chip->hardware == WSS_HW_CS4236B) {
L
Linus Torvalds 已提交
1332 1333 1334 1335 1336 1337
			rev = snd_cs4236_ext_in(chip, CS4236_VERSION);
			snd_cs4236_ext_out(chip, CS4236_VERSION, 0xff);
			id = snd_cs4236_ext_in(chip, CS4236_VERSION);
			snd_cs4236_ext_out(chip, CS4236_VERSION, rev);
			snd_printdd("CS4231: ext version; rev = 0x%x, id = 0x%x\n", rev, id);
			if ((id & 0x1f) == 0x1d) {	/* CS4235 */
1338
				chip->hardware = WSS_HW_CS4235;
L
Linus Torvalds 已提交
1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352
				switch (id >> 5) {
				case 4:
				case 5:
				case 6:
					break;
				default:
					snd_printk("unknown CS4235 chip (enhanced version = 0x%x)\n", id);
				}
			} else if ((id & 0x1f) == 0x0b) {	/* CS4236/B */
				switch (id >> 5) {
				case 4:
				case 5:
				case 6:
				case 7:
1353
					chip->hardware = WSS_HW_CS4236B;
L
Linus Torvalds 已提交
1354 1355 1356 1357 1358
					break;
				default:
					snd_printk("unknown CS4236 chip (enhanced version = 0x%x)\n", id);
				}
			} else if ((id & 0x1f) == 0x08) {	/* CS4237B */
1359
				chip->hardware = WSS_HW_CS4237B;
L
Linus Torvalds 已提交
1360 1361 1362 1363 1364 1365 1366 1367 1368 1369
				switch (id >> 5) {
				case 4:
				case 5:
				case 6:
				case 7:
					break;
				default:
					snd_printk("unknown CS4237B chip (enhanced version = 0x%x)\n", id);
				}
			} else if ((id & 0x1f) == 0x09) {	/* CS4238B */
1370
				chip->hardware = WSS_HW_CS4238B;
L
Linus Torvalds 已提交
1371 1372 1373 1374 1375 1376 1377 1378 1379
				switch (id >> 5) {
				case 5:
				case 6:
				case 7:
					break;
				default:
					snd_printk("unknown CS4238B chip (enhanced version = 0x%x)\n", id);
				}
			} else if ((id & 0x1f) == 0x1e) {	/* CS4239 */
1380
				chip->hardware = WSS_HW_CS4239;
L
Linus Torvalds 已提交
1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400
				switch (id >> 5) {
				case 4:
				case 5:
				case 6:
					break;
				default:
					snd_printk("unknown CS4239 chip (enhanced version = 0x%x)\n", id);
				}
			} else {
				snd_printk("unknown CS4236/CS423xB chip (enhanced version = 0x%x)\n", id);
			}
		}
	}
	return 0;		/* all things are ok.. */
}

/*

 */

1401
static struct snd_pcm_hardware snd_wss_playback =
L
Linus Torvalds 已提交
1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421
{
	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
				 SNDRV_PCM_INFO_MMAP_VALID |
				 SNDRV_PCM_INFO_RESUME |
				 SNDRV_PCM_INFO_SYNC_START),
	.formats =		(SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW | SNDRV_PCM_FMTBIT_IMA_ADPCM |
				 SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE),
	.rates =		SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
	.rate_min =		5510,
	.rate_max =		48000,
	.channels_min =		1,
	.channels_max =		2,
	.buffer_bytes_max =	(128*1024),
	.period_bytes_min =	64,
	.period_bytes_max =	(128*1024),
	.periods_min =		1,
	.periods_max =		1024,
	.fifo_size =		0,
};

1422
static struct snd_pcm_hardware snd_wss_capture =
L
Linus Torvalds 已提交
1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446
{
	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
				 SNDRV_PCM_INFO_MMAP_VALID |
				 SNDRV_PCM_INFO_RESUME |
				 SNDRV_PCM_INFO_SYNC_START),
	.formats =		(SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW | SNDRV_PCM_FMTBIT_IMA_ADPCM |
				 SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE),
	.rates =		SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
	.rate_min =		5510,
	.rate_max =		48000,
	.channels_min =		1,
	.channels_max =		2,
	.buffer_bytes_max =	(128*1024),
	.period_bytes_min =	64,
	.period_bytes_max =	(128*1024),
	.periods_min =		1,
	.periods_max =		1024,
	.fifo_size =		0,
};

/*

 */

1447
static int snd_wss_playback_open(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
1448
{
1449
	struct snd_wss *chip = snd_pcm_substream_chip(substream);
1450
	struct snd_pcm_runtime *runtime = substream->runtime;
L
Linus Torvalds 已提交
1451 1452
	int err;

1453
	runtime->hw = snd_wss_playback;
L
Linus Torvalds 已提交
1454

1455 1456 1457 1458 1459
	/* hardware limitation of older chipsets */
	if (chip->hardware & WSS_HW_AD1848_MASK)
		runtime->hw.formats &= ~(SNDRV_PCM_FMTBIT_IMA_ADPCM |
					 SNDRV_PCM_FMTBIT_S16_BE);

L
Linus Torvalds 已提交
1460
	/* hardware bug in InterWave chipset */
1461
	if (chip->hardware == WSS_HW_INTERWAVE && chip->dma1 > 3)
1462 1463
		runtime->hw.formats &= ~SNDRV_PCM_FMTBIT_MU_LAW;

L
Linus Torvalds 已提交
1464
	/* hardware limitation of cheap chips */
1465 1466
	if (chip->hardware == WSS_HW_CS4235 ||
	    chip->hardware == WSS_HW_CS4239)
L
Linus Torvalds 已提交
1467 1468 1469 1470 1471 1472 1473 1474 1475 1476
		runtime->hw.formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE;

	snd_pcm_limit_isa_dma_size(chip->dma1, &runtime->hw.buffer_bytes_max);
	snd_pcm_limit_isa_dma_size(chip->dma1, &runtime->hw.period_bytes_max);

	if (chip->claim_dma) {
		if ((err = chip->claim_dma(chip, chip->dma_private_data, chip->dma1)) < 0)
			return err;
	}

1477 1478
	err = snd_wss_open(chip, WSS_MODE_PLAY);
	if (err < 0) {
L
Linus Torvalds 已提交
1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489
		if (chip->release_dma)
			chip->release_dma(chip, chip->dma_private_data, chip->dma1);
		snd_free_pages(runtime->dma_area, runtime->dma_bytes);
		return err;
	}
	chip->playback_substream = substream;
	snd_pcm_set_sync(substream);
	chip->rate_constraint(runtime);
	return 0;
}

1490
static int snd_wss_capture_open(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
1491
{
1492
	struct snd_wss *chip = snd_pcm_substream_chip(substream);
1493
	struct snd_pcm_runtime *runtime = substream->runtime;
L
Linus Torvalds 已提交
1494 1495
	int err;

1496
	runtime->hw = snd_wss_capture;
L
Linus Torvalds 已提交
1497

1498 1499 1500 1501 1502
	/* hardware limitation of older chipsets */
	if (chip->hardware & WSS_HW_AD1848_MASK)
		runtime->hw.formats &= ~(SNDRV_PCM_FMTBIT_IMA_ADPCM |
					 SNDRV_PCM_FMTBIT_S16_BE);

L
Linus Torvalds 已提交
1503
	/* hardware limitation of cheap chips */
1504
	if (chip->hardware == WSS_HW_CS4235 ||
1505 1506 1507 1508
	    chip->hardware == WSS_HW_CS4239 ||
	    chip->hardware == WSS_HW_OPTI93X)
		runtime->hw.formats = SNDRV_PCM_FMTBIT_U8 |
				      SNDRV_PCM_FMTBIT_S16_LE;
L
Linus Torvalds 已提交
1509 1510 1511 1512 1513 1514 1515 1516 1517

	snd_pcm_limit_isa_dma_size(chip->dma2, &runtime->hw.buffer_bytes_max);
	snd_pcm_limit_isa_dma_size(chip->dma2, &runtime->hw.period_bytes_max);

	if (chip->claim_dma) {
		if ((err = chip->claim_dma(chip, chip->dma_private_data, chip->dma2)) < 0)
			return err;
	}

1518 1519
	err = snd_wss_open(chip, WSS_MODE_RECORD);
	if (err < 0) {
L
Linus Torvalds 已提交
1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530
		if (chip->release_dma)
			chip->release_dma(chip, chip->dma_private_data, chip->dma2);
		snd_free_pages(runtime->dma_area, runtime->dma_bytes);
		return err;
	}
	chip->capture_substream = substream;
	snd_pcm_set_sync(substream);
	chip->rate_constraint(runtime);
	return 0;
}

1531
static int snd_wss_playback_close(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
1532
{
1533
	struct snd_wss *chip = snd_pcm_substream_chip(substream);
L
Linus Torvalds 已提交
1534 1535

	chip->playback_substream = NULL;
1536
	snd_wss_close(chip, WSS_MODE_PLAY);
L
Linus Torvalds 已提交
1537 1538 1539
	return 0;
}

1540
static int snd_wss_capture_close(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
1541
{
1542
	struct snd_wss *chip = snd_pcm_substream_chip(substream);
L
Linus Torvalds 已提交
1543 1544

	chip->capture_substream = NULL;
1545
	snd_wss_close(chip, WSS_MODE_RECORD);
L
Linus Torvalds 已提交
1546 1547 1548
	return 0;
}

1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568
static void snd_wss_thinkpad_twiddle(struct snd_wss *chip, int on)
{
	int tmp;

	if (!chip->thinkpad_flag)
		return;

	outb(0x1c, AD1848_THINKPAD_CTL_PORT1);
	tmp = inb(AD1848_THINKPAD_CTL_PORT2);

	if (on)
		/* turn it on */
		tmp |= AD1848_THINKPAD_CS4248_ENABLE_BIT;
	else
		/* turn it off */
		tmp &= ~AD1848_THINKPAD_CS4248_ENABLE_BIT;

	outb(tmp, AD1848_THINKPAD_CTL_PORT2);
}

L
Linus Torvalds 已提交
1569 1570 1571
#ifdef CONFIG_PM

/* lowlevel suspend callback for CS4231 */
1572
static void snd_wss_suspend(struct snd_wss *chip)
L
Linus Torvalds 已提交
1573 1574 1575
{
	int reg;
	unsigned long flags;
1576

T
Takashi Iwai 已提交
1577
	snd_pcm_suspend_all(chip->pcm);
L
Linus Torvalds 已提交
1578 1579
	spin_lock_irqsave(&chip->reg_lock, flags);
	for (reg = 0; reg < 32; reg++)
1580
		chip->image[reg] = snd_wss_in(chip, reg);
L
Linus Torvalds 已提交
1581
	spin_unlock_irqrestore(&chip->reg_lock, flags);
1582 1583
	if (chip->thinkpad_flag)
		snd_wss_thinkpad_twiddle(chip, 0);
L
Linus Torvalds 已提交
1584 1585 1586
}

/* lowlevel resume callback for CS4231 */
1587
static void snd_wss_resume(struct snd_wss *chip)
L
Linus Torvalds 已提交
1588 1589 1590
{
	int reg;
	unsigned long flags;
T
Takashi Iwai 已提交
1591
	/* int timeout; */
1592

1593 1594
	if (chip->thinkpad_flag)
		snd_wss_thinkpad_twiddle(chip, 1);
1595
	snd_wss_mce_up(chip);
L
Linus Torvalds 已提交
1596 1597 1598 1599 1600 1601
	spin_lock_irqsave(&chip->reg_lock, flags);
	for (reg = 0; reg < 32; reg++) {
		switch (reg) {
		case CS4231_VERSION:
			break;
		default:
1602
			snd_wss_out(chip, reg, chip->image[reg]);
L
Linus Torvalds 已提交
1603 1604 1605 1606
			break;
		}
	}
	spin_unlock_irqrestore(&chip->reg_lock, flags);
T
Takashi Iwai 已提交
1607
#if 1
1608
	snd_wss_mce_down(chip);
L
Linus Torvalds 已提交
1609 1610
#else
	/* The following is a workaround to avoid freeze after resume on TP600E.
1611
	   This is the first half of copy of snd_wss_mce_down(), but doesn't
L
Linus Torvalds 已提交
1612 1613
	   include rescheduling.  -- iwai
	   */
1614
	snd_wss_busy_wait(chip);
L
Linus Torvalds 已提交
1615 1616
	spin_lock_irqsave(&chip->reg_lock, flags);
	chip->mce_bit &= ~CS4231_MCE;
1617 1618
	timeout = wss_inb(chip, CS4231P(REGSEL));
	wss_outb(chip, CS4231P(REGSEL), chip->mce_bit | (timeout & 0x1f));
L
Linus Torvalds 已提交
1619 1620 1621 1622
	spin_unlock_irqrestore(&chip->reg_lock, flags);
	if (timeout == 0x80)
		snd_printk("down [0x%lx]: serious init problem - codec still busy\n", chip->port);
	if ((timeout & CS4231_MCE) == 0 ||
1623
	    !(chip->hardware & (WSS_HW_CS4231_MASK | WSS_HW_CS4232_MASK))) {
L
Linus Torvalds 已提交
1624 1625
		return;
	}
1626
	snd_wss_busy_wait(chip);
L
Linus Torvalds 已提交
1627 1628 1629 1630
#endif
}
#endif /* CONFIG_PM */

1631
static int snd_wss_free(struct snd_wss *chip)
L
Linus Torvalds 已提交
1632
{
1633 1634
	release_and_free_resource(chip->res_port);
	release_and_free_resource(chip->res_cport);
L
Linus Torvalds 已提交
1635 1636
	if (chip->irq >= 0) {
		disable_irq(chip->irq);
1637
		if (!(chip->hwshare & WSS_HWSHARE_IRQ))
L
Linus Torvalds 已提交
1638 1639
			free_irq(chip->irq, (void *) chip);
	}
1640
	if (!(chip->hwshare & WSS_HWSHARE_DMA1) && chip->dma1 >= 0) {
L
Linus Torvalds 已提交
1641 1642 1643
		snd_dma_disable(chip->dma1);
		free_dma(chip->dma1);
	}
1644 1645
	if (!(chip->hwshare & WSS_HWSHARE_DMA2) &&
	    chip->dma2 >= 0 && chip->dma2 != chip->dma1) {
L
Linus Torvalds 已提交
1646 1647 1648 1649 1650 1651 1652 1653 1654
		snd_dma_disable(chip->dma2);
		free_dma(chip->dma2);
	}
	if (chip->timer)
		snd_device_free(chip->card, chip->timer);
	kfree(chip);
	return 0;
}

1655
static int snd_wss_dev_free(struct snd_device *device)
L
Linus Torvalds 已提交
1656
{
1657 1658
	struct snd_wss *chip = device->device_data;
	return snd_wss_free(chip);
L
Linus Torvalds 已提交
1659 1660
}

1661
const char *snd_wss_chip_id(struct snd_wss *chip)
L
Linus Torvalds 已提交
1662 1663
{
	switch (chip->hardware) {
1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691
	case WSS_HW_CS4231:
		return "CS4231";
	case WSS_HW_CS4231A:
		return "CS4231A";
	case WSS_HW_CS4232:
		return "CS4232";
	case WSS_HW_CS4232A:
		return "CS4232A";
	case WSS_HW_CS4235:
		return "CS4235";
	case WSS_HW_CS4236:
		return "CS4236";
	case WSS_HW_CS4236B:
		return "CS4236B";
	case WSS_HW_CS4237B:
		return "CS4237B";
	case WSS_HW_CS4238B:
		return "CS4238B";
	case WSS_HW_CS4239:
		return "CS4239";
	case WSS_HW_INTERWAVE:
		return "AMD InterWave";
	case WSS_HW_OPL3SA2:
		return chip->card->shortname;
	case WSS_HW_AD1845:
		return "AD1845";
	case WSS_HW_OPTI93X:
		return "OPTi 93x";
1692 1693 1694 1695 1696 1697 1698 1699
	case WSS_HW_AD1847:
		return "AD1847";
	case WSS_HW_AD1848:
		return "AD1848";
	case WSS_HW_CS4248:
		return "CS4248";
	case WSS_HW_CMI8330:
		return "CMI8330/C3D";
1700 1701
	default:
		return "???";
L
Linus Torvalds 已提交
1702 1703
	}
}
1704
EXPORT_SYMBOL(snd_wss_chip_id);
L
Linus Torvalds 已提交
1705

1706
static int snd_wss_new(struct snd_card *card,
L
Linus Torvalds 已提交
1707 1708
			  unsigned short hardware,
			  unsigned short hwshare,
1709
			  struct snd_wss **rchip)
L
Linus Torvalds 已提交
1710
{
1711
	struct snd_wss *chip;
L
Linus Torvalds 已提交
1712 1713

	*rchip = NULL;
1714
	chip = kzalloc(sizeof(*chip), GFP_KERNEL);
L
Linus Torvalds 已提交
1715 1716 1717 1718 1719 1720
	if (chip == NULL)
		return -ENOMEM;
	chip->hardware = hardware;
	chip->hwshare = hwshare;

	spin_lock_init(&chip->reg_lock);
1721 1722
	mutex_init(&chip->mce_mutex);
	mutex_init(&chip->open_mutex);
L
Linus Torvalds 已提交
1723
	chip->card = card;
1724 1725 1726 1727
	chip->rate_constraint = snd_wss_xrate;
	chip->set_playback_format = snd_wss_playback_format;
	chip->set_capture_format = snd_wss_capture_format;
	if (chip->hardware == WSS_HW_OPTI93X)
1728 1729 1730
		memcpy(&chip->image, &snd_opti93x_original_image,
		       sizeof(snd_opti93x_original_image));
	else
1731 1732
		memcpy(&chip->image, &snd_wss_original_image,
		       sizeof(snd_wss_original_image));
1733 1734 1735 1736
	if (chip->hardware & WSS_HW_AD1848_MASK) {
		chip->image[CS4231_PIN_CTRL] = 0;
		chip->image[CS4231_TEST_INIT] = 0;
	}
1737

1738 1739
	*rchip = chip;
	return 0;
L
Linus Torvalds 已提交
1740 1741
}

1742 1743 1744
int snd_wss_create(struct snd_card *card,
		      unsigned long port,
		      unsigned long cport,
L
Linus Torvalds 已提交
1745 1746 1747
		      int irq, int dma1, int dma2,
		      unsigned short hardware,
		      unsigned short hwshare,
1748
		      struct snd_wss **rchip)
L
Linus Torvalds 已提交
1749
{
1750
	static struct snd_device_ops ops = {
1751
		.dev_free =	snd_wss_dev_free,
L
Linus Torvalds 已提交
1752
	};
1753
	struct snd_wss *chip;
L
Linus Torvalds 已提交
1754 1755
	int err;

1756
	err = snd_wss_new(card, hardware, hwshare, &chip);
L
Linus Torvalds 已提交
1757 1758
	if (err < 0)
		return err;
1759

L
Linus Torvalds 已提交
1760 1761 1762 1763
	chip->irq = -1;
	chip->dma1 = -1;
	chip->dma2 = -1;

1764
	chip->res_port = request_region(port, 4, "WSS");
1765 1766 1767
	if (!chip->res_port) {
		snd_printk(KERN_ERR "wss: can't grab port 0x%lx\n", port);
		snd_wss_free(chip);
L
Linus Torvalds 已提交
1768 1769 1770
		return -EBUSY;
	}
	chip->port = port;
1771 1772 1773 1774 1775 1776 1777 1778
	if ((long)cport >= 0) {
		chip->res_cport = request_region(cport, 8, "CS4232 Control");
		if (!chip->res_cport) {
			snd_printk(KERN_ERR
				"wss: can't grab control port 0x%lx\n", cport);
			snd_wss_free(chip);
			return -ENODEV;
		}
L
Linus Torvalds 已提交
1779 1780
	}
	chip->cport = cport;
1781 1782
	if (!(hwshare & WSS_HWSHARE_IRQ))
		if (request_irq(irq, snd_wss_interrupt, IRQF_DISABLED,
1783
				"WSS", (void *) chip)) {
1784 1785 1786 1787
			snd_printk(KERN_ERR "wss: can't grab IRQ %d\n", irq);
			snd_wss_free(chip);
			return -EBUSY;
		}
L
Linus Torvalds 已提交
1788
	chip->irq = irq;
1789
	if (!(hwshare & WSS_HWSHARE_DMA1) && request_dma(dma1, "WSS - 1")) {
1790 1791
		snd_printk(KERN_ERR "wss: can't grab DMA1 %d\n", dma1);
		snd_wss_free(chip);
L
Linus Torvalds 已提交
1792 1793 1794
		return -EBUSY;
	}
	chip->dma1 = dma1;
1795
	if (!(hwshare & WSS_HWSHARE_DMA2) && dma1 != dma2 &&
1796
	      dma2 >= 0 && request_dma(dma2, "WSS - 2")) {
1797 1798
		snd_printk(KERN_ERR "wss: can't grab DMA2 %d\n", dma2);
		snd_wss_free(chip);
L
Linus Torvalds 已提交
1799 1800 1801 1802 1803 1804 1805 1806
		return -EBUSY;
	}
	if (dma1 == dma2 || dma2 < 0) {
		chip->single_dma = 1;
		chip->dma2 = chip->dma1;
	} else
		chip->dma2 = dma2;

1807 1808 1809 1810 1811 1812
	if (hardware == WSS_HW_THINKPAD) {
		chip->thinkpad_flag = 1;
		chip->hardware = WSS_HW_DETECT; /* reset */
		snd_wss_thinkpad_twiddle(chip, 1);
	}

L
Linus Torvalds 已提交
1813
	/* global setup */
1814 1815
	if (snd_wss_probe(chip) < 0) {
		snd_wss_free(chip);
L
Linus Torvalds 已提交
1816 1817
		return -ENODEV;
	}
1818
	snd_wss_init(chip);
L
Linus Torvalds 已提交
1819

1820
#if 0
1821
	if (chip->hardware & WSS_HW_CS4232_MASK) {
L
Linus Torvalds 已提交
1822 1823 1824
		if (chip->res_cport == NULL)
			snd_printk("CS4232 control port features are not accessible\n");
	}
1825
#endif
L
Linus Torvalds 已提交
1826 1827

	/* Register device */
1828 1829 1830
	err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
	if (err < 0) {
		snd_wss_free(chip);
L
Linus Torvalds 已提交
1831 1832 1833 1834 1835
		return err;
	}

#ifdef CONFIG_PM
	/* Power Management */
1836 1837
	chip->suspend = snd_wss_suspend;
	chip->resume = snd_wss_resume;
L
Linus Torvalds 已提交
1838 1839 1840 1841 1842
#endif

	*rchip = chip;
	return 0;
}
1843
EXPORT_SYMBOL(snd_wss_create);
L
Linus Torvalds 已提交
1844

1845 1846 1847
static struct snd_pcm_ops snd_wss_playback_ops = {
	.open =		snd_wss_playback_open,
	.close =	snd_wss_playback_close,
L
Linus Torvalds 已提交
1848
	.ioctl =	snd_pcm_lib_ioctl,
1849 1850 1851 1852 1853
	.hw_params =	snd_wss_playback_hw_params,
	.hw_free =	snd_wss_playback_hw_free,
	.prepare =	snd_wss_playback_prepare,
	.trigger =	snd_wss_trigger,
	.pointer =	snd_wss_playback_pointer,
L
Linus Torvalds 已提交
1854 1855
};

1856 1857 1858
static struct snd_pcm_ops snd_wss_capture_ops = {
	.open =		snd_wss_capture_open,
	.close =	snd_wss_capture_close,
L
Linus Torvalds 已提交
1859
	.ioctl =	snd_pcm_lib_ioctl,
1860 1861 1862 1863 1864
	.hw_params =	snd_wss_capture_hw_params,
	.hw_free =	snd_wss_capture_hw_free,
	.prepare =	snd_wss_capture_prepare,
	.trigger =	snd_wss_trigger,
	.pointer =	snd_wss_capture_pointer,
L
Linus Torvalds 已提交
1865 1866
};

1867
int snd_wss_pcm(struct snd_wss *chip, int device, struct snd_pcm **rpcm)
L
Linus Torvalds 已提交
1868
{
1869
	struct snd_pcm *pcm;
L
Linus Torvalds 已提交
1870 1871
	int err;

1872 1873
	err = snd_pcm_new(chip->card, "WSS", device, 1, 1, &pcm);
	if (err < 0)
L
Linus Torvalds 已提交
1874 1875
		return err;

1876 1877
	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_wss_playback_ops);
	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_wss_capture_ops);
1878

L
Linus Torvalds 已提交
1879 1880 1881 1882 1883
	/* global setup */
	pcm->private_data = chip;
	pcm->info_flags = 0;
	if (chip->single_dma)
		pcm->info_flags |= SNDRV_PCM_INFO_HALF_DUPLEX;
1884
	if (chip->hardware != WSS_HW_INTERWAVE)
L
Linus Torvalds 已提交
1885
		pcm->info_flags |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1886
	strcpy(pcm->name, snd_wss_chip_id(chip));
L
Linus Torvalds 已提交
1887 1888 1889 1890 1891 1892 1893 1894 1895 1896

	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
					      snd_dma_isa_data(),
					      64*1024, chip->dma1 > 3 || chip->dma2 > 3 ? 128*1024 : 64*1024);

	chip->pcm = pcm;
	if (rpcm)
		*rpcm = pcm;
	return 0;
}
1897
EXPORT_SYMBOL(snd_wss_pcm);
L
Linus Torvalds 已提交
1898

1899
static void snd_wss_timer_free(struct snd_timer *timer)
L
Linus Torvalds 已提交
1900
{
1901
	struct snd_wss *chip = timer->private_data;
L
Linus Torvalds 已提交
1902 1903 1904
	chip->timer = NULL;
}

1905
int snd_wss_timer(struct snd_wss *chip, int device, struct snd_timer **rtimer)
L
Linus Torvalds 已提交
1906
{
1907 1908
	struct snd_timer *timer;
	struct snd_timer_id tid;
L
Linus Torvalds 已提交
1909 1910 1911 1912 1913 1914 1915 1916 1917 1918
	int err;

	/* Timer initialization */
	tid.dev_class = SNDRV_TIMER_CLASS_CARD;
	tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
	tid.card = chip->card->number;
	tid.device = device;
	tid.subdevice = 0;
	if ((err = snd_timer_new(chip->card, "CS4231", &tid, &timer)) < 0)
		return err;
1919
	strcpy(timer->name, snd_wss_chip_id(chip));
L
Linus Torvalds 已提交
1920
	timer->private_data = chip;
1921 1922
	timer->private_free = snd_wss_timer_free;
	timer->hw = snd_wss_timer_table;
L
Linus Torvalds 已提交
1923 1924 1925 1926 1927
	chip->timer = timer;
	if (rtimer)
		*rtimer = timer;
	return 0;
}
1928
EXPORT_SYMBOL(snd_wss_timer);
1929

L
Linus Torvalds 已提交
1930 1931 1932 1933
/*
 *  MIXER part
 */

1934 1935
static int snd_wss_info_mux(struct snd_kcontrol *kcontrol,
			    struct snd_ctl_elem_info *uinfo)
L
Linus Torvalds 已提交
1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946
{
	static char *texts[4] = {
		"Line", "Aux", "Mic", "Mix"
	};
	static char *opl3sa_texts[4] = {
		"Line", "CD", "Mic", "Mix"
	};
	static char *gusmax_texts[4] = {
		"Line", "Synth", "Mic", "Mix"
	};
	char **ptexts = texts;
1947
	struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
1948 1949 1950 1951 1952 1953 1954 1955 1956 1957

	snd_assert(chip->card != NULL, return -EINVAL);
	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
	uinfo->count = 2;
	uinfo->value.enumerated.items = 4;
	if (uinfo->value.enumerated.item > 3)
		uinfo->value.enumerated.item = 3;
	if (!strcmp(chip->card->driver, "GUS MAX"))
		ptexts = gusmax_texts;
	switch (chip->hardware) {
1958 1959 1960 1961 1962 1963
	case WSS_HW_INTERWAVE:
		ptexts = gusmax_texts;
		break;
	case WSS_HW_OPL3SA2:
		ptexts = opl3sa_texts;
		break;
L
Linus Torvalds 已提交
1964 1965 1966 1967 1968
	}
	strcpy(uinfo->value.enumerated.name, ptexts[uinfo->value.enumerated.item]);
	return 0;
}

1969 1970
static int snd_wss_get_mux(struct snd_kcontrol *kcontrol,
			   struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
1971
{
1972
	struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
1973
	unsigned long flags;
1974

L
Linus Torvalds 已提交
1975 1976 1977 1978 1979 1980 1981
	spin_lock_irqsave(&chip->reg_lock, flags);
	ucontrol->value.enumerated.item[0] = (chip->image[CS4231_LEFT_INPUT] & CS4231_MIXS_ALL) >> 6;
	ucontrol->value.enumerated.item[1] = (chip->image[CS4231_RIGHT_INPUT] & CS4231_MIXS_ALL) >> 6;
	spin_unlock_irqrestore(&chip->reg_lock, flags);
	return 0;
}

1982 1983
static int snd_wss_put_mux(struct snd_kcontrol *kcontrol,
			   struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
1984
{
1985
	struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
1986 1987 1988
	unsigned long flags;
	unsigned short left, right;
	int change;
1989

L
Linus Torvalds 已提交
1990 1991 1992 1993 1994 1995 1996 1997 1998
	if (ucontrol->value.enumerated.item[0] > 3 ||
	    ucontrol->value.enumerated.item[1] > 3)
		return -EINVAL;
	left = ucontrol->value.enumerated.item[0] << 6;
	right = ucontrol->value.enumerated.item[1] << 6;
	spin_lock_irqsave(&chip->reg_lock, flags);
	left = (chip->image[CS4231_LEFT_INPUT] & ~CS4231_MIXS_ALL) | left;
	right = (chip->image[CS4231_RIGHT_INPUT] & ~CS4231_MIXS_ALL) | right;
	change = left != chip->image[CS4231_LEFT_INPUT] ||
1999 2000 2001
		 right != chip->image[CS4231_RIGHT_INPUT];
	snd_wss_out(chip, CS4231_LEFT_INPUT, left);
	snd_wss_out(chip, CS4231_RIGHT_INPUT, right);
L
Linus Torvalds 已提交
2002 2003 2004 2005
	spin_unlock_irqrestore(&chip->reg_lock, flags);
	return change;
}

2006 2007
int snd_wss_info_single(struct snd_kcontrol *kcontrol,
			struct snd_ctl_elem_info *uinfo)
L
Linus Torvalds 已提交
2008 2009 2010 2011 2012 2013 2014 2015 2016
{
	int mask = (kcontrol->private_value >> 16) & 0xff;

	uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
	uinfo->count = 1;
	uinfo->value.integer.min = 0;
	uinfo->value.integer.max = mask;
	return 0;
}
2017
EXPORT_SYMBOL(snd_wss_info_single);
L
Linus Torvalds 已提交
2018

2019 2020
int snd_wss_get_single(struct snd_kcontrol *kcontrol,
		       struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
2021
{
2022
	struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
2023 2024 2025 2026 2027
	unsigned long flags;
	int reg = kcontrol->private_value & 0xff;
	int shift = (kcontrol->private_value >> 8) & 0xff;
	int mask = (kcontrol->private_value >> 16) & 0xff;
	int invert = (kcontrol->private_value >> 24) & 0xff;
2028

L
Linus Torvalds 已提交
2029 2030 2031 2032 2033 2034 2035
	spin_lock_irqsave(&chip->reg_lock, flags);
	ucontrol->value.integer.value[0] = (chip->image[reg] >> shift) & mask;
	spin_unlock_irqrestore(&chip->reg_lock, flags);
	if (invert)
		ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
	return 0;
}
2036
EXPORT_SYMBOL(snd_wss_get_single);
L
Linus Torvalds 已提交
2037

2038 2039
int snd_wss_put_single(struct snd_kcontrol *kcontrol,
		       struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
2040
{
2041
	struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
2042 2043 2044 2045 2046 2047 2048
	unsigned long flags;
	int reg = kcontrol->private_value & 0xff;
	int shift = (kcontrol->private_value >> 8) & 0xff;
	int mask = (kcontrol->private_value >> 16) & 0xff;
	int invert = (kcontrol->private_value >> 24) & 0xff;
	int change;
	unsigned short val;
2049

L
Linus Torvalds 已提交
2050 2051 2052 2053 2054 2055 2056
	val = (ucontrol->value.integer.value[0] & mask);
	if (invert)
		val = mask - val;
	val <<= shift;
	spin_lock_irqsave(&chip->reg_lock, flags);
	val = (chip->image[reg] & ~(mask << shift)) | val;
	change = val != chip->image[reg];
2057
	snd_wss_out(chip, reg, val);
L
Linus Torvalds 已提交
2058 2059 2060
	spin_unlock_irqrestore(&chip->reg_lock, flags);
	return change;
}
2061
EXPORT_SYMBOL(snd_wss_put_single);
L
Linus Torvalds 已提交
2062

2063 2064
int snd_wss_info_double(struct snd_kcontrol *kcontrol,
			struct snd_ctl_elem_info *uinfo)
L
Linus Torvalds 已提交
2065 2066 2067 2068 2069 2070 2071 2072 2073
{
	int mask = (kcontrol->private_value >> 24) & 0xff;

	uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
	uinfo->count = 2;
	uinfo->value.integer.min = 0;
	uinfo->value.integer.max = mask;
	return 0;
}
2074
EXPORT_SYMBOL(snd_wss_info_double);
L
Linus Torvalds 已提交
2075

2076 2077
int snd_wss_get_double(struct snd_kcontrol *kcontrol,
		       struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
2078
{
2079
	struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
2080 2081 2082 2083 2084 2085 2086
	unsigned long flags;
	int left_reg = kcontrol->private_value & 0xff;
	int right_reg = (kcontrol->private_value >> 8) & 0xff;
	int shift_left = (kcontrol->private_value >> 16) & 0x07;
	int shift_right = (kcontrol->private_value >> 19) & 0x07;
	int mask = (kcontrol->private_value >> 24) & 0xff;
	int invert = (kcontrol->private_value >> 22) & 1;
2087

L
Linus Torvalds 已提交
2088 2089 2090 2091 2092 2093 2094 2095 2096 2097
	spin_lock_irqsave(&chip->reg_lock, flags);
	ucontrol->value.integer.value[0] = (chip->image[left_reg] >> shift_left) & mask;
	ucontrol->value.integer.value[1] = (chip->image[right_reg] >> shift_right) & mask;
	spin_unlock_irqrestore(&chip->reg_lock, flags);
	if (invert) {
		ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
		ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
	}
	return 0;
}
2098
EXPORT_SYMBOL(snd_wss_get_double);
L
Linus Torvalds 已提交
2099

2100 2101
int snd_wss_put_double(struct snd_kcontrol *kcontrol,
		       struct snd_ctl_elem_value *ucontrol)
L
Linus Torvalds 已提交
2102
{
2103
	struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
L
Linus Torvalds 已提交
2104 2105 2106 2107 2108 2109 2110 2111 2112
	unsigned long flags;
	int left_reg = kcontrol->private_value & 0xff;
	int right_reg = (kcontrol->private_value >> 8) & 0xff;
	int shift_left = (kcontrol->private_value >> 16) & 0x07;
	int shift_right = (kcontrol->private_value >> 19) & 0x07;
	int mask = (kcontrol->private_value >> 24) & 0xff;
	int invert = (kcontrol->private_value >> 22) & 1;
	int change;
	unsigned short val1, val2;
2113

L
Linus Torvalds 已提交
2114 2115 2116 2117 2118 2119 2120 2121 2122
	val1 = ucontrol->value.integer.value[0] & mask;
	val2 = ucontrol->value.integer.value[1] & mask;
	if (invert) {
		val1 = mask - val1;
		val2 = mask - val2;
	}
	val1 <<= shift_left;
	val2 <<= shift_right;
	spin_lock_irqsave(&chip->reg_lock, flags);
2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135
	if (left_reg != right_reg) {
		val1 = (chip->image[left_reg] & ~(mask << shift_left)) | val1;
		val2 = (chip->image[right_reg] & ~(mask << shift_right)) | val2;
		change = val1 != chip->image[left_reg] ||
			 val2 != chip->image[right_reg];
		snd_wss_out(chip, left_reg, val1);
		snd_wss_out(chip, right_reg, val2);
	} else {
		mask = (mask << shift_left) | (mask << shift_right);
		val1 = (chip->image[left_reg] & ~mask) | val1 | val2;
		change = val1 != chip->image[left_reg];
		snd_wss_out(chip, left_reg, val1);
	}
L
Linus Torvalds 已提交
2136 2137 2138
	spin_unlock_irqrestore(&chip->reg_lock, flags);
	return change;
}
2139 2140
EXPORT_SYMBOL(snd_wss_put_double);

2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174
static const DECLARE_TLV_DB_SCALE(db_scale_6bit, -9450, 150, 0);
static const DECLARE_TLV_DB_SCALE(db_scale_5bit_12db_max, -3450, 150, 0);
static const DECLARE_TLV_DB_SCALE(db_scale_rec_gain, 0, 150, 0);

static struct snd_kcontrol_new snd_ad1848_controls[] = {
WSS_DOUBLE("PCM Playback Switch", 0, CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT,
	   7, 7, 1, 1),
WSS_DOUBLE_TLV("PCM Playback Volume", 0,
	       CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 0, 0, 63, 1,
	       db_scale_6bit),
WSS_DOUBLE("Aux Playback Switch", 0,
	   CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 7, 7, 1, 1),
WSS_DOUBLE_TLV("Aux Playback Volume", 0,
	       CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 0, 0, 31, 1,
	       db_scale_5bit_12db_max),
WSS_DOUBLE("Aux Playback Switch", 1,
	   CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 7, 7, 1, 1),
WSS_DOUBLE_TLV("Aux Playback Volume", 1,
	       CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 0, 0, 31, 1,
	       db_scale_5bit_12db_max),
WSS_DOUBLE_TLV("Capture Volume", 0, CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT,
		0, 0, 15, 0, db_scale_rec_gain),
{
	.name = "Capture Source",
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.info = snd_wss_info_mux,
	.get = snd_wss_get_mux,
	.put = snd_wss_put_mux,
},
WSS_SINGLE("Loopback Capture Switch", 0, CS4231_LOOPBACK, 0, 1, 0),
WSS_SINGLE_TLV("Loopback Capture Volume", 0, CS4231_LOOPBACK, 1, 63, 0,
	       db_scale_6bit),
};

2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201
static struct snd_kcontrol_new snd_wss_controls[] = {
WSS_DOUBLE("PCM Playback Switch", 0,
		CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 7, 7, 1, 1),
WSS_DOUBLE("PCM Playback Volume", 0,
		CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 0, 0, 63, 1),
WSS_DOUBLE("Line Playback Switch", 0,
		CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 7, 7, 1, 1),
WSS_DOUBLE("Line Playback Volume", 0,
		CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 0, 0, 31, 1),
WSS_DOUBLE("Aux Playback Switch", 0,
		CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 7, 7, 1, 1),
WSS_DOUBLE("Aux Playback Volume", 0,
		CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 0, 0, 31, 1),
WSS_DOUBLE("Aux Playback Switch", 1,
		CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 7, 7, 1, 1),
WSS_DOUBLE("Aux Playback Volume", 1,
		CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 0, 0, 31, 1),
WSS_SINGLE("Mono Playback Switch", 0,
		CS4231_MONO_CTRL, 7, 1, 1),
WSS_SINGLE("Mono Playback Volume", 0,
		CS4231_MONO_CTRL, 0, 15, 1),
WSS_SINGLE("Mono Output Playback Switch", 0,
		CS4231_MONO_CTRL, 6, 1, 1),
WSS_SINGLE("Mono Output Playback Bypass", 0,
		CS4231_MONO_CTRL, 5, 1, 0),
WSS_DOUBLE("Capture Volume", 0,
		CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT, 0, 0, 15, 0),
L
Linus Torvalds 已提交
2202 2203 2204
{
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.name = "Capture Source",
2205 2206 2207
	.info = snd_wss_info_mux,
	.get = snd_wss_get_mux,
	.put = snd_wss_put_mux,
L
Linus Torvalds 已提交
2208
},
2209 2210 2211 2212 2213 2214
WSS_DOUBLE("Mic Boost", 0,
		CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT, 5, 5, 1, 0),
WSS_SINGLE("Loopback Capture Switch", 0,
		CS4231_LOOPBACK, 0, 1, 0),
WSS_SINGLE("Loopback Capture Volume", 0,
		CS4231_LOOPBACK, 2, 63, 1)
L
Linus Torvalds 已提交
2215
};
2216

2217
static struct snd_kcontrol_new snd_opti93x_controls[] = {
2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249
WSS_DOUBLE("Master Playback Switch", 0,
		OPTi93X_OUT_LEFT, OPTi93X_OUT_RIGHT, 7, 7, 1, 1),
WSS_DOUBLE("Master Playback Volume", 0,
		OPTi93X_OUT_LEFT, OPTi93X_OUT_RIGHT, 1, 1, 31, 1),
WSS_DOUBLE("PCM Playback Switch", 0,
		CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 7, 7, 1, 1),
WSS_DOUBLE("PCM Playback Volume", 0,
		CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 0, 0, 31, 1),
WSS_DOUBLE("FM Playback Switch", 0,
		CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 7, 7, 1, 1),
WSS_DOUBLE("FM Playback Volume", 0,
		CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 1, 1, 15, 1),
WSS_DOUBLE("Line Playback Switch", 0,
		CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 7, 7, 1, 1),
WSS_DOUBLE("Line Playback Volume", 0,
		CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 0, 0, 15, 1),
WSS_DOUBLE("Mic Playback Switch", 0,
		OPTi93X_MIC_LEFT_INPUT, OPTi93X_MIC_RIGHT_INPUT, 7, 7, 1, 1),
WSS_DOUBLE("Mic Playback Volume", 0,
		OPTi93X_MIC_LEFT_INPUT, OPTi93X_MIC_RIGHT_INPUT, 1, 1, 15, 1),
WSS_DOUBLE("Mic Boost", 0,
		CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT, 5, 5, 1, 0),
WSS_DOUBLE("CD Playback Switch", 0,
		CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 7, 7, 1, 1),
WSS_DOUBLE("CD Playback Volume", 0,
		CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 1, 1, 15, 1),
WSS_DOUBLE("Aux Playback Switch", 0,
		OPTi931_AUX_LEFT_INPUT, OPTi931_AUX_RIGHT_INPUT, 7, 7, 1, 1),
WSS_DOUBLE("Aux Playback Volume", 0,
		OPTi931_AUX_LEFT_INPUT, OPTi931_AUX_RIGHT_INPUT, 1, 1, 15, 1),
WSS_DOUBLE("Capture Volume", 0,
		CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT, 0, 0, 15, 0),
2250 2251 2252
{
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.name = "Capture Source",
2253 2254 2255
	.info = snd_wss_info_mux,
	.get = snd_wss_get_mux,
	.put = snd_wss_put_mux,
2256 2257 2258
}
};

2259
int snd_wss_mixer(struct snd_wss *chip)
L
Linus Torvalds 已提交
2260
{
2261
	struct snd_card *card;
L
Linus Torvalds 已提交
2262 2263 2264 2265 2266 2267 2268 2269 2270
	unsigned int idx;
	int err;

	snd_assert(chip != NULL && chip->pcm != NULL, return -EINVAL);

	card = chip->card;

	strcpy(card->mixername, chip->pcm->name);

2271
	if (chip->hardware == WSS_HW_OPTI93X)
2272 2273 2274 2275 2276 2277 2278
		for (idx = 0; idx < ARRAY_SIZE(snd_opti93x_controls); idx++) {
			err = snd_ctl_add(card,
					snd_ctl_new1(&snd_opti93x_controls[idx],
						     chip));
			if (err < 0)
				return err;
		}
2279 2280 2281 2282 2283 2284 2285 2286
	else if (chip->hardware & WSS_HW_AD1848_MASK)
		for (idx = 0; idx < ARRAY_SIZE(snd_ad1848_controls); idx++) {
			err = snd_ctl_add(card,
					snd_ctl_new1(&snd_ad1848_controls[idx],
						     chip));
			if (err < 0)
				return err;
		}
2287
	else
2288
		for (idx = 0; idx < ARRAY_SIZE(snd_wss_controls); idx++) {
2289
			err = snd_ctl_add(card,
2290
					snd_ctl_new1(&snd_wss_controls[idx],
2291 2292 2293 2294
						     chip));
			if (err < 0)
				return err;
		}
L
Linus Torvalds 已提交
2295 2296
	return 0;
}
2297
EXPORT_SYMBOL(snd_wss_mixer);
L
Linus Torvalds 已提交
2298

2299 2300 2301 2302 2303 2304 2305
const struct snd_pcm_ops *snd_wss_get_pcm_ops(int direction)
{
	return direction == SNDRV_PCM_STREAM_PLAYBACK ?
		&snd_wss_playback_ops : &snd_wss_capture_ops;
}
EXPORT_SYMBOL(snd_wss_get_pcm_ops);

L
Linus Torvalds 已提交
2306 2307 2308 2309
/*
 *  INIT part
 */

2310
static int __init alsa_wss_init(void)
L
Linus Torvalds 已提交
2311 2312 2313 2314
{
	return 0;
}

2315
static void __exit alsa_wss_exit(void)
L
Linus Torvalds 已提交
2316 2317 2318
{
}

2319 2320
module_init(alsa_wss_init);
module_exit(alsa_wss_exit);