[ALSA] Remove xxx_t typedefs: ISA AD1848
[linux-2.6-block.git] / sound / isa / cs423x / cs4231_lib.c
CommitLineData
1da177e4
LT
1/*
2 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
3 * Routines for control of CS4231(A)/CS4232/InterWave & compatible chips
4 *
5 * Bugs:
6 * - sometimes record brokes playback with WSS portion of
7 * Yamaha OPL3-SA3 chip
8 * - CS4231 (GUS MAX) - still trouble with occasional noises
9 * - broken initialization?
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 *
25 */
26
27#include <sound/driver.h>
28#include <linux/delay.h>
29#include <linux/pm.h>
30#include <linux/init.h>
31#include <linux/interrupt.h>
32#include <linux/slab.h>
33#include <linux/ioport.h>
34#include <sound/core.h>
35#include <sound/cs4231.h>
36#include <sound/pcm_params.h>
37
38#include <asm/io.h>
39#include <asm/dma.h>
40#include <asm/irq.h>
41
42MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
43MODULE_DESCRIPTION("Routines for control of CS4231(A)/CS4232/InterWave & compatible chips");
44MODULE_LICENSE("GPL");
45
46#if 0
47#define SNDRV_DEBUG_MCE
48#endif
49
50/*
51 * Some variables
52 */
53
54static unsigned char freq_bits[14] = {
55 /* 5510 */ 0x00 | CS4231_XTAL2,
56 /* 6620 */ 0x0E | CS4231_XTAL2,
57 /* 8000 */ 0x00 | CS4231_XTAL1,
58 /* 9600 */ 0x0E | CS4231_XTAL1,
59 /* 11025 */ 0x02 | CS4231_XTAL2,
60 /* 16000 */ 0x02 | CS4231_XTAL1,
61 /* 18900 */ 0x04 | CS4231_XTAL2,
62 /* 22050 */ 0x06 | CS4231_XTAL2,
63 /* 27042 */ 0x04 | CS4231_XTAL1,
64 /* 32000 */ 0x06 | CS4231_XTAL1,
65 /* 33075 */ 0x0C | CS4231_XTAL2,
66 /* 37800 */ 0x08 | CS4231_XTAL2,
67 /* 44100 */ 0x0A | CS4231_XTAL2,
68 /* 48000 */ 0x0C | CS4231_XTAL1
69};
70
71static unsigned int rates[14] = {
72 5510, 6620, 8000, 9600, 11025, 16000, 18900, 22050,
73 27042, 32000, 33075, 37800, 44100, 48000
74};
75
76static snd_pcm_hw_constraint_list_t hw_constraints_rates = {
77 .count = 14,
78 .list = rates,
79 .mask = 0,
80};
81
82static int snd_cs4231_xrate(snd_pcm_runtime_t *runtime)
83{
84 return snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
85}
86
87static unsigned char snd_cs4231_original_image[32] =
88{
89 0x00, /* 00/00 - lic */
90 0x00, /* 01/01 - ric */
91 0x9f, /* 02/02 - la1ic */
92 0x9f, /* 03/03 - ra1ic */
93 0x9f, /* 04/04 - la2ic */
94 0x9f, /* 05/05 - ra2ic */
95 0xbf, /* 06/06 - loc */
96 0xbf, /* 07/07 - roc */
97 0x20, /* 08/08 - pdfr */
98 CS4231_AUTOCALIB, /* 09/09 - ic */
99 0x00, /* 0a/10 - pc */
100 0x00, /* 0b/11 - ti */
101 CS4231_MODE2, /* 0c/12 - mi */
102 0xfc, /* 0d/13 - lbc */
103 0x00, /* 0e/14 - pbru */
104 0x00, /* 0f/15 - pbrl */
105 0x80, /* 10/16 - afei */
106 0x01, /* 11/17 - afeii */
107 0x9f, /* 12/18 - llic */
108 0x9f, /* 13/19 - rlic */
109 0x00, /* 14/20 - tlb */
110 0x00, /* 15/21 - thb */
111 0x00, /* 16/22 - la3mic/reserved */
112 0x00, /* 17/23 - ra3mic/reserved */
113 0x00, /* 18/24 - afs */
114 0x00, /* 19/25 - lamoc/version */
115 0xcf, /* 1a/26 - mioc */
116 0x00, /* 1b/27 - ramoc/reserved */
117 0x20, /* 1c/28 - cdfr */
118 0x00, /* 1d/29 - res4 */
119 0x00, /* 1e/30 - cbru */
120 0x00, /* 1f/31 - cbrl */
121};
122
123/*
124 * Basic I/O functions
125 */
126
f739aecc 127static inline void cs4231_outb(cs4231_t *chip, u8 offset, u8 val)
1da177e4 128{
1da177e4 129 outb(val, chip->port + offset);
1da177e4
LT
130}
131
f739aecc 132static inline u8 cs4231_inb(cs4231_t *chip, u8 offset)
1da177e4 133{
1da177e4 134 return inb(chip->port + offset);
1da177e4
LT
135}
136
137static void snd_cs4231_outm(cs4231_t *chip, unsigned char reg,
138 unsigned char mask, unsigned char value)
139{
140 int timeout;
141 unsigned char tmp;
142
143 for (timeout = 250;
144 timeout > 0 && (cs4231_inb(chip, CS4231P(REGSEL)) & CS4231_INIT);
145 timeout--)
146 udelay(100);
147#ifdef CONFIG_SND_DEBUG
148 if (cs4231_inb(chip, CS4231P(REGSEL)) & CS4231_INIT)
149 snd_printk("outm: auto calibration time out - reg = 0x%x, value = 0x%x\n", reg, value);
150#endif
151 if (chip->calibrate_mute) {
152 chip->image[reg] &= mask;
153 chip->image[reg] |= value;
154 } else {
155 cs4231_outb(chip, CS4231P(REGSEL), chip->mce_bit | reg);
156 mb();
157 tmp = (chip->image[reg] & mask) | value;
158 cs4231_outb(chip, CS4231P(REG), tmp);
159 chip->image[reg] = tmp;
160 mb();
161 }
162}
163
164static void snd_cs4231_dout(cs4231_t *chip, unsigned char reg, unsigned char value)
165{
166 int timeout;
167
168 for (timeout = 250;
169 timeout > 0 && (cs4231_inb(chip, CS4231P(REGSEL)) & CS4231_INIT);
170 timeout--)
171 udelay(10);
172 cs4231_outb(chip, CS4231P(REGSEL), chip->mce_bit | reg);
173 cs4231_outb(chip, CS4231P(REG), value);
174 mb();
175}
176
177void snd_cs4231_out(cs4231_t *chip, unsigned char reg, unsigned char value)
178{
179 int timeout;
180
181 for (timeout = 250;
182 timeout > 0 && (cs4231_inb(chip, CS4231P(REGSEL)) & CS4231_INIT);
183 timeout--)
184 udelay(100);
185#ifdef CONFIG_SND_DEBUG
186 if (cs4231_inb(chip, CS4231P(REGSEL)) & CS4231_INIT)
187 snd_printk("out: auto calibration time out - reg = 0x%x, value = 0x%x\n", reg, value);
188#endif
189 cs4231_outb(chip, CS4231P(REGSEL), chip->mce_bit | reg);
190 cs4231_outb(chip, CS4231P(REG), value);
191 chip->image[reg] = value;
192 mb();
193#if 0
194 printk("codec out - reg 0x%x = 0x%x\n", chip->mce_bit | reg, value);
195#endif
196}
197
198unsigned char snd_cs4231_in(cs4231_t *chip, unsigned char reg)
199{
200 int timeout;
201
202 for (timeout = 250;
203 timeout > 0 && (cs4231_inb(chip, CS4231P(REGSEL)) & CS4231_INIT);
204 timeout--)
205 udelay(100);
206#ifdef CONFIG_SND_DEBUG
207 if (cs4231_inb(chip, CS4231P(REGSEL)) & CS4231_INIT)
208 snd_printk("in: auto calibration time out - reg = 0x%x\n", reg);
209#endif
210 cs4231_outb(chip, CS4231P(REGSEL), chip->mce_bit | reg);
211 mb();
212 return cs4231_inb(chip, CS4231P(REG));
213}
214
215void snd_cs4236_ext_out(cs4231_t *chip, unsigned char reg, unsigned char val)
216{
217 cs4231_outb(chip, CS4231P(REGSEL), chip->mce_bit | 0x17);
218 cs4231_outb(chip, CS4231P(REG), reg | (chip->image[CS4236_EXT_REG] & 0x01));
219 cs4231_outb(chip, CS4231P(REG), val);
220 chip->eimage[CS4236_REG(reg)] = val;
221#if 0
222 printk("ext out : reg = 0x%x, val = 0x%x\n", reg, val);
223#endif
224}
225
226unsigned char snd_cs4236_ext_in(cs4231_t *chip, unsigned char reg)
227{
228 cs4231_outb(chip, CS4231P(REGSEL), chip->mce_bit | 0x17);
229 cs4231_outb(chip, CS4231P(REG), reg | (chip->image[CS4236_EXT_REG] & 0x01));
230#if 1
231 return cs4231_inb(chip, CS4231P(REG));
232#else
233 {
234 unsigned char res;
235 res = cs4231_inb(chip, CS4231P(REG));
236 printk("ext in : reg = 0x%x, val = 0x%x\n", reg, res);
237 return res;
238 }
239#endif
240}
241
242#if 0
243
244static void snd_cs4231_debug(cs4231_t *chip)
245{
246 printk("CS4231 REGS: INDEX = 0x%02x ", cs4231_inb(chip, CS4231P(REGSEL)));
247 printk(" STATUS = 0x%02x\n", cs4231_inb(chip, CS4231P(STATUS)));
248 printk(" 0x00: left input = 0x%02x ", snd_cs4231_in(chip, 0x00));
249 printk(" 0x10: alt 1 (CFIG 2) = 0x%02x\n", snd_cs4231_in(chip, 0x10));
250 printk(" 0x01: right input = 0x%02x ", snd_cs4231_in(chip, 0x01));
251 printk(" 0x11: alt 2 (CFIG 3) = 0x%02x\n", snd_cs4231_in(chip, 0x11));
252 printk(" 0x02: GF1 left input = 0x%02x ", snd_cs4231_in(chip, 0x02));
253 printk(" 0x12: left line in = 0x%02x\n", snd_cs4231_in(chip, 0x12));
254 printk(" 0x03: GF1 right input = 0x%02x ", snd_cs4231_in(chip, 0x03));
255 printk(" 0x13: right line in = 0x%02x\n", snd_cs4231_in(chip, 0x13));
256 printk(" 0x04: CD left input = 0x%02x ", snd_cs4231_in(chip, 0x04));
257 printk(" 0x14: timer low = 0x%02x\n", snd_cs4231_in(chip, 0x14));
258 printk(" 0x05: CD right input = 0x%02x ", snd_cs4231_in(chip, 0x05));
259 printk(" 0x15: timer high = 0x%02x\n", snd_cs4231_in(chip, 0x15));
260 printk(" 0x06: left output = 0x%02x ", snd_cs4231_in(chip, 0x06));
261 printk(" 0x16: left MIC (PnP) = 0x%02x\n", snd_cs4231_in(chip, 0x16));
262 printk(" 0x07: right output = 0x%02x ", snd_cs4231_in(chip, 0x07));
263 printk(" 0x17: right MIC (PnP) = 0x%02x\n", snd_cs4231_in(chip, 0x17));
264 printk(" 0x08: playback format = 0x%02x ", snd_cs4231_in(chip, 0x08));
265 printk(" 0x18: IRQ status = 0x%02x\n", snd_cs4231_in(chip, 0x18));
266 printk(" 0x09: iface (CFIG 1) = 0x%02x ", snd_cs4231_in(chip, 0x09));
267 printk(" 0x19: left line out = 0x%02x\n", snd_cs4231_in(chip, 0x19));
268 printk(" 0x0a: pin control = 0x%02x ", snd_cs4231_in(chip, 0x0a));
269 printk(" 0x1a: mono control = 0x%02x\n", snd_cs4231_in(chip, 0x1a));
270 printk(" 0x0b: init & status = 0x%02x ", snd_cs4231_in(chip, 0x0b));
271 printk(" 0x1b: right line out = 0x%02x\n", snd_cs4231_in(chip, 0x1b));
272 printk(" 0x0c: revision & mode = 0x%02x ", snd_cs4231_in(chip, 0x0c));
273 printk(" 0x1c: record format = 0x%02x\n", snd_cs4231_in(chip, 0x1c));
274 printk(" 0x0d: loopback = 0x%02x ", snd_cs4231_in(chip, 0x0d));
275 printk(" 0x1d: var freq (PnP) = 0x%02x\n", snd_cs4231_in(chip, 0x1d));
276 printk(" 0x0e: ply upr count = 0x%02x ", snd_cs4231_in(chip, 0x0e));
277 printk(" 0x1e: ply lwr count = 0x%02x\n", snd_cs4231_in(chip, 0x1e));
278 printk(" 0x0f: rec upr count = 0x%02x ", snd_cs4231_in(chip, 0x0f));
279 printk(" 0x1f: rec lwr count = 0x%02x\n", snd_cs4231_in(chip, 0x1f));
280}
281
282#endif
283
284/*
285 * CS4231 detection / MCE routines
286 */
287
288static void snd_cs4231_busy_wait(cs4231_t *chip)
289{
290 int timeout;
291
292 /* huh.. looks like this sequence is proper for CS4231A chip (GUS MAX) */
293 for (timeout = 5; timeout > 0; timeout--)
294 cs4231_inb(chip, CS4231P(REGSEL));
295 /* end of cleanup sequence */
296 for (timeout = 250;
297 timeout > 0 && (cs4231_inb(chip, CS4231P(REGSEL)) & CS4231_INIT);
298 timeout--)
299 udelay(10);
300}
301
302void snd_cs4231_mce_up(cs4231_t *chip)
303{
304 unsigned long flags;
305 int timeout;
306
307 for (timeout = 250; timeout > 0 && (cs4231_inb(chip, CS4231P(REGSEL)) & CS4231_INIT); timeout--)
308 udelay(100);
309#ifdef CONFIG_SND_DEBUG
310 if (cs4231_inb(chip, CS4231P(REGSEL)) & CS4231_INIT)
311 snd_printk("mce_up - auto calibration time out (0)\n");
312#endif
313 spin_lock_irqsave(&chip->reg_lock, flags);
314 chip->mce_bit |= CS4231_MCE;
315 timeout = cs4231_inb(chip, CS4231P(REGSEL));
316 if (timeout == 0x80)
317 snd_printk("mce_up [0x%lx]: serious init problem - codec still busy\n", chip->port);
318 if (!(timeout & CS4231_MCE))
319 cs4231_outb(chip, CS4231P(REGSEL), chip->mce_bit | (timeout & 0x1f));
320 spin_unlock_irqrestore(&chip->reg_lock, flags);
321}
322
323void snd_cs4231_mce_down(cs4231_t *chip)
324{
325 unsigned long flags;
326 int timeout;
327
328 snd_cs4231_busy_wait(chip);
329#if 0
330 printk("(1) timeout = %i\n", timeout);
331#endif
332#ifdef CONFIG_SND_DEBUG
333 if (cs4231_inb(chip, CS4231P(REGSEL)) & CS4231_INIT)
334 snd_printk("mce_down [0x%lx] - auto calibration time out (0)\n", (long)CS4231P(REGSEL));
335#endif
336 spin_lock_irqsave(&chip->reg_lock, flags);
337 chip->mce_bit &= ~CS4231_MCE;
338 timeout = cs4231_inb(chip, CS4231P(REGSEL));
339 cs4231_outb(chip, CS4231P(REGSEL), chip->mce_bit | (timeout & 0x1f));
340 spin_unlock_irqrestore(&chip->reg_lock, flags);
341 if (timeout == 0x80)
342 snd_printk("mce_down [0x%lx]: serious init problem - codec still busy\n", chip->port);
343 if ((timeout & CS4231_MCE) == 0 ||
344 !(chip->hardware & (CS4231_HW_CS4231_MASK | CS4231_HW_CS4232_MASK))) {
345 return;
346 }
347 snd_cs4231_busy_wait(chip);
348
349 /* calibration process */
350
351 for (timeout = 500; timeout > 0 && (snd_cs4231_in(chip, CS4231_TEST_INIT) & CS4231_CALIB_IN_PROGRESS) == 0; timeout--)
352 udelay(10);
353 if ((snd_cs4231_in(chip, CS4231_TEST_INIT) & CS4231_CALIB_IN_PROGRESS) == 0) {
354 snd_printd("cs4231_mce_down - auto calibration time out (1)\n");
355 return;
356 }
357#if 0
358 printk("(2) timeout = %i, jiffies = %li\n", timeout, jiffies);
359#endif
360 /* in 10 ms increments, check condition, up to 250 ms */
361 timeout = 25;
362 while (snd_cs4231_in(chip, CS4231_TEST_INIT) & CS4231_CALIB_IN_PROGRESS) {
363 if (--timeout < 0) {
364 snd_printk("mce_down - auto calibration time out (2)\n");
365 return;
366 }
367 msleep(10);
368 }
369#if 0
370 printk("(3) jiffies = %li\n", jiffies);
371#endif
372 /* in 10 ms increments, check condition, up to 100 ms */
373 timeout = 10;
374 while (cs4231_inb(chip, CS4231P(REGSEL)) & CS4231_INIT) {
375 if (--timeout < 0) {
376 snd_printk(KERN_ERR "mce_down - auto calibration time out (3)\n");
377 return;
378 }
379 msleep(10);
380 }
381#if 0
382 printk("(4) jiffies = %li\n", jiffies);
383 snd_printk("mce_down - exit = 0x%x\n", cs4231_inb(chip, CS4231P(REGSEL)));
384#endif
385}
386
387static unsigned int snd_cs4231_get_count(unsigned char format, unsigned int size)
388{
389 switch (format & 0xe0) {
390 case CS4231_LINEAR_16:
391 case CS4231_LINEAR_16_BIG:
392 size >>= 1;
393 break;
394 case CS4231_ADPCM_16:
395 return size >> 2;
396 }
397 if (format & CS4231_STEREO)
398 size >>= 1;
399 return size;
400}
401
402static int snd_cs4231_trigger(snd_pcm_substream_t *substream,
403 int cmd)
404{
405 cs4231_t *chip = snd_pcm_substream_chip(substream);
406 int result = 0;
407 unsigned int what;
408 struct list_head *pos;
409 snd_pcm_substream_t *s;
410 int do_start;
411
412#if 0
413 printk("codec trigger!!! - what = %i, enable = %i, status = 0x%x\n", what, enable, cs4231_inb(chip, CS4231P(STATUS)));
414#endif
415
416 switch (cmd) {
417 case SNDRV_PCM_TRIGGER_START:
418 case SNDRV_PCM_TRIGGER_RESUME:
419 do_start = 1; break;
420 case SNDRV_PCM_TRIGGER_STOP:
421 case SNDRV_PCM_TRIGGER_SUSPEND:
422 do_start = 0; break;
423 default:
424 return -EINVAL;
425 }
426
427 what = 0;
428 snd_pcm_group_for_each(pos, substream) {
429 s = snd_pcm_group_substream_entry(pos);
430 if (s == chip->playback_substream) {
431 what |= CS4231_PLAYBACK_ENABLE;
432 snd_pcm_trigger_done(s, substream);
433 } else if (s == chip->capture_substream) {
434 what |= CS4231_RECORD_ENABLE;
435 snd_pcm_trigger_done(s, substream);
436 }
437 }
438 spin_lock(&chip->reg_lock);
439 if (do_start) {
440 chip->image[CS4231_IFACE_CTRL] |= what;
441 if (chip->trigger)
442 chip->trigger(chip, what, 1);
443 } else {
444 chip->image[CS4231_IFACE_CTRL] &= ~what;
445 if (chip->trigger)
446 chip->trigger(chip, what, 0);
447 }
448 snd_cs4231_out(chip, CS4231_IFACE_CTRL, chip->image[CS4231_IFACE_CTRL]);
449 spin_unlock(&chip->reg_lock);
450#if 0
451 snd_cs4231_debug(chip);
452#endif
453 return result;
454}
455
456/*
457 * CODEC I/O
458 */
459
460static unsigned char snd_cs4231_get_rate(unsigned int rate)
461{
462 int i;
463
464 for (i = 0; i < 14; i++)
465 if (rate == rates[i])
466 return freq_bits[i];
467 // snd_BUG();
468 return freq_bits[13];
469}
470
471static unsigned char snd_cs4231_get_format(cs4231_t *chip,
472 int format,
473 int channels)
474{
475 unsigned char rformat;
476
477 rformat = CS4231_LINEAR_8;
478 switch (format) {
479 case SNDRV_PCM_FORMAT_MU_LAW: rformat = CS4231_ULAW_8; break;
480 case SNDRV_PCM_FORMAT_A_LAW: rformat = CS4231_ALAW_8; break;
481 case SNDRV_PCM_FORMAT_S16_LE: rformat = CS4231_LINEAR_16; break;
482 case SNDRV_PCM_FORMAT_S16_BE: rformat = CS4231_LINEAR_16_BIG; break;
483 case SNDRV_PCM_FORMAT_IMA_ADPCM: rformat = CS4231_ADPCM_16; break;
484 }
485 if (channels > 1)
486 rformat |= CS4231_STEREO;
487#if 0
488 snd_printk("get_format: 0x%x (mode=0x%x)\n", format, mode);
489#endif
490 return rformat;
491}
492
493static void snd_cs4231_calibrate_mute(cs4231_t *chip, int mute)
494{
495 unsigned long flags;
496
497 mute = mute ? 1 : 0;
498 spin_lock_irqsave(&chip->reg_lock, flags);
499 if (chip->calibrate_mute == mute) {
500 spin_unlock_irqrestore(&chip->reg_lock, flags);
501 return;
502 }
503 if (!mute) {
504 snd_cs4231_dout(chip, CS4231_LEFT_INPUT, chip->image[CS4231_LEFT_INPUT]);
505 snd_cs4231_dout(chip, CS4231_RIGHT_INPUT, chip->image[CS4231_RIGHT_INPUT]);
506 snd_cs4231_dout(chip, CS4231_LOOPBACK, chip->image[CS4231_LOOPBACK]);
507 }
508 snd_cs4231_dout(chip, CS4231_AUX1_LEFT_INPUT, mute ? 0x80 : chip->image[CS4231_AUX1_LEFT_INPUT]);
509 snd_cs4231_dout(chip, CS4231_AUX1_RIGHT_INPUT, mute ? 0x80 : chip->image[CS4231_AUX1_RIGHT_INPUT]);
510 snd_cs4231_dout(chip, CS4231_AUX2_LEFT_INPUT, mute ? 0x80 : chip->image[CS4231_AUX2_LEFT_INPUT]);
511 snd_cs4231_dout(chip, CS4231_AUX2_RIGHT_INPUT, mute ? 0x80 : chip->image[CS4231_AUX2_RIGHT_INPUT]);
512 snd_cs4231_dout(chip, CS4231_LEFT_OUTPUT, mute ? 0x80 : chip->image[CS4231_LEFT_OUTPUT]);
513 snd_cs4231_dout(chip, CS4231_RIGHT_OUTPUT, mute ? 0x80 : chip->image[CS4231_RIGHT_OUTPUT]);
514 snd_cs4231_dout(chip, CS4231_LEFT_LINE_IN, mute ? 0x80 : chip->image[CS4231_LEFT_LINE_IN]);
515 snd_cs4231_dout(chip, CS4231_RIGHT_LINE_IN, mute ? 0x80 : chip->image[CS4231_RIGHT_LINE_IN]);
516 snd_cs4231_dout(chip, CS4231_MONO_CTRL, mute ? 0xc0 : chip->image[CS4231_MONO_CTRL]);
517 if (chip->hardware == CS4231_HW_INTERWAVE) {
518 snd_cs4231_dout(chip, CS4231_LEFT_MIC_INPUT, mute ? 0x80 : chip->image[CS4231_LEFT_MIC_INPUT]);
519 snd_cs4231_dout(chip, CS4231_RIGHT_MIC_INPUT, mute ? 0x80 : chip->image[CS4231_RIGHT_MIC_INPUT]);
520 snd_cs4231_dout(chip, CS4231_LINE_LEFT_OUTPUT, mute ? 0x80 : chip->image[CS4231_LINE_LEFT_OUTPUT]);
521 snd_cs4231_dout(chip, CS4231_LINE_RIGHT_OUTPUT, mute ? 0x80 : chip->image[CS4231_LINE_RIGHT_OUTPUT]);
522 }
523 chip->calibrate_mute = mute;
524 spin_unlock_irqrestore(&chip->reg_lock, flags);
525}
526
527static void snd_cs4231_playback_format(cs4231_t *chip,
528 snd_pcm_hw_params_t *params,
529 unsigned char pdfr)
530{
531 unsigned long flags;
532 int full_calib = 1;
533
534 down(&chip->mce_mutex);
535 snd_cs4231_calibrate_mute(chip, 1);
536 if (chip->hardware == CS4231_HW_CS4231A ||
537 (chip->hardware & CS4231_HW_CS4232_MASK)) {
538 spin_lock_irqsave(&chip->reg_lock, flags);
539 if ((chip->image[CS4231_PLAYBK_FORMAT] & 0x0f) == (pdfr & 0x0f)) { /* rate is same? */
540 snd_cs4231_out(chip, CS4231_ALT_FEATURE_1, chip->image[CS4231_ALT_FEATURE_1] | 0x10);
541 snd_cs4231_out(chip, CS4231_PLAYBK_FORMAT, chip->image[CS4231_PLAYBK_FORMAT] = pdfr);
542 snd_cs4231_out(chip, CS4231_ALT_FEATURE_1, chip->image[CS4231_ALT_FEATURE_1] &= ~0x10);
543 udelay(100); /* Fixes audible clicks at least on GUS MAX */
544 full_calib = 0;
545 }
546 spin_unlock_irqrestore(&chip->reg_lock, flags);
547 }
548 if (full_calib) {
549 snd_cs4231_mce_up(chip);
550 spin_lock_irqsave(&chip->reg_lock, flags);
551 if (chip->hardware != CS4231_HW_INTERWAVE && !chip->single_dma) {
552 snd_cs4231_out(chip, CS4231_PLAYBK_FORMAT,
553 (chip->image[CS4231_IFACE_CTRL] & CS4231_RECORD_ENABLE) ?
554 (pdfr & 0xf0) | (chip->image[CS4231_REC_FORMAT] & 0x0f) :
555 pdfr);
556 } else {
557 snd_cs4231_out(chip, CS4231_PLAYBK_FORMAT, chip->image[CS4231_PLAYBK_FORMAT] = pdfr);
558 }
559 spin_unlock_irqrestore(&chip->reg_lock, flags);
560 snd_cs4231_mce_down(chip);
561 }
562 snd_cs4231_calibrate_mute(chip, 0);
563 up(&chip->mce_mutex);
564}
565
566static void snd_cs4231_capture_format(cs4231_t *chip,
567 snd_pcm_hw_params_t *params,
568 unsigned char cdfr)
569{
570 unsigned long flags;
571 int full_calib = 1;
572
573 down(&chip->mce_mutex);
574 snd_cs4231_calibrate_mute(chip, 1);
575 if (chip->hardware == CS4231_HW_CS4231A ||
576 (chip->hardware & CS4231_HW_CS4232_MASK)) {
577 spin_lock_irqsave(&chip->reg_lock, flags);
578 if ((chip->image[CS4231_PLAYBK_FORMAT] & 0x0f) == (cdfr & 0x0f) || /* rate is same? */
579 (chip->image[CS4231_IFACE_CTRL] & CS4231_PLAYBACK_ENABLE)) {
580 snd_cs4231_out(chip, CS4231_ALT_FEATURE_1, chip->image[CS4231_ALT_FEATURE_1] | 0x20);
581 snd_cs4231_out(chip, CS4231_REC_FORMAT, chip->image[CS4231_REC_FORMAT] = cdfr);
582 snd_cs4231_out(chip, CS4231_ALT_FEATURE_1, chip->image[CS4231_ALT_FEATURE_1] &= ~0x20);
583 full_calib = 0;
584 }
585 spin_unlock_irqrestore(&chip->reg_lock, flags);
586 }
587 if (full_calib) {
588 snd_cs4231_mce_up(chip);
589 spin_lock_irqsave(&chip->reg_lock, flags);
590 if (chip->hardware != CS4231_HW_INTERWAVE) {
591 if (!(chip->image[CS4231_IFACE_CTRL] & CS4231_PLAYBACK_ENABLE)) {
592 snd_cs4231_out(chip, CS4231_PLAYBK_FORMAT,
593 ((chip->single_dma ? cdfr : chip->image[CS4231_PLAYBK_FORMAT]) & 0xf0) |
594 (cdfr & 0x0f));
595 spin_unlock_irqrestore(&chip->reg_lock, flags);
596 snd_cs4231_mce_down(chip);
597 snd_cs4231_mce_up(chip);
598 spin_lock_irqsave(&chip->reg_lock, flags);
599 }
600 }
601 snd_cs4231_out(chip, CS4231_REC_FORMAT, cdfr);
602 spin_unlock_irqrestore(&chip->reg_lock, flags);
603 snd_cs4231_mce_down(chip);
604 }
605 snd_cs4231_calibrate_mute(chip, 0);
606 up(&chip->mce_mutex);
607}
608
609/*
610 * Timer interface
611 */
612
613static unsigned long snd_cs4231_timer_resolution(snd_timer_t * timer)
614{
615 cs4231_t *chip = snd_timer_chip(timer);
616 if (chip->hardware & CS4231_HW_CS4236B_MASK)
617 return 14467;
618 else
619 return chip->image[CS4231_PLAYBK_FORMAT] & 1 ? 9969 : 9920;
620}
621
622static int snd_cs4231_timer_start(snd_timer_t * timer)
623{
624 unsigned long flags;
625 unsigned int ticks;
626 cs4231_t *chip = snd_timer_chip(timer);
627 spin_lock_irqsave(&chip->reg_lock, flags);
628 ticks = timer->sticks;
629 if ((chip->image[CS4231_ALT_FEATURE_1] & CS4231_TIMER_ENABLE) == 0 ||
630 (unsigned char)(ticks >> 8) != chip->image[CS4231_TIMER_HIGH] ||
631 (unsigned char)ticks != chip->image[CS4231_TIMER_LOW]) {
632 snd_cs4231_out(chip, CS4231_TIMER_HIGH, chip->image[CS4231_TIMER_HIGH] = (unsigned char) (ticks >> 8));
633 snd_cs4231_out(chip, CS4231_TIMER_LOW, chip->image[CS4231_TIMER_LOW] = (unsigned char) ticks);
634 snd_cs4231_out(chip, CS4231_ALT_FEATURE_1, chip->image[CS4231_ALT_FEATURE_1] | CS4231_TIMER_ENABLE);
635 }
636 spin_unlock_irqrestore(&chip->reg_lock, flags);
637 return 0;
638}
639
640static int snd_cs4231_timer_stop(snd_timer_t * timer)
641{
642 unsigned long flags;
643 cs4231_t *chip = snd_timer_chip(timer);
644 spin_lock_irqsave(&chip->reg_lock, flags);
645 snd_cs4231_out(chip, CS4231_ALT_FEATURE_1, chip->image[CS4231_ALT_FEATURE_1] &= ~CS4231_TIMER_ENABLE);
646 spin_unlock_irqrestore(&chip->reg_lock, flags);
647 return 0;
648}
649
650static void snd_cs4231_init(cs4231_t *chip)
651{
652 unsigned long flags;
653
654 snd_cs4231_mce_down(chip);
655
656#ifdef SNDRV_DEBUG_MCE
657 snd_printk("init: (1)\n");
658#endif
659 snd_cs4231_mce_up(chip);
660 spin_lock_irqsave(&chip->reg_lock, flags);
661 chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_PLAYBACK_ENABLE | CS4231_PLAYBACK_PIO |
662 CS4231_RECORD_ENABLE | CS4231_RECORD_PIO |
663 CS4231_CALIB_MODE);
664 chip->image[CS4231_IFACE_CTRL] |= CS4231_AUTOCALIB;
665 snd_cs4231_out(chip, CS4231_IFACE_CTRL, chip->image[CS4231_IFACE_CTRL]);
666 spin_unlock_irqrestore(&chip->reg_lock, flags);
667 snd_cs4231_mce_down(chip);
668
669#ifdef SNDRV_DEBUG_MCE
670 snd_printk("init: (2)\n");
671#endif
672
673 snd_cs4231_mce_up(chip);
674 spin_lock_irqsave(&chip->reg_lock, flags);
675 snd_cs4231_out(chip, CS4231_ALT_FEATURE_1, chip->image[CS4231_ALT_FEATURE_1]);
676 spin_unlock_irqrestore(&chip->reg_lock, flags);
677 snd_cs4231_mce_down(chip);
678
679#ifdef SNDRV_DEBUG_MCE
680 snd_printk("init: (3) - afei = 0x%x\n", chip->image[CS4231_ALT_FEATURE_1]);
681#endif
682
683 spin_lock_irqsave(&chip->reg_lock, flags);
684 snd_cs4231_out(chip, CS4231_ALT_FEATURE_2, chip->image[CS4231_ALT_FEATURE_2]);
685 spin_unlock_irqrestore(&chip->reg_lock, flags);
686
687 snd_cs4231_mce_up(chip);
688 spin_lock_irqsave(&chip->reg_lock, flags);
689 snd_cs4231_out(chip, CS4231_PLAYBK_FORMAT, chip->image[CS4231_PLAYBK_FORMAT]);
690 spin_unlock_irqrestore(&chip->reg_lock, flags);
691 snd_cs4231_mce_down(chip);
692
693#ifdef SNDRV_DEBUG_MCE
694 snd_printk("init: (4)\n");
695#endif
696
697 snd_cs4231_mce_up(chip);
698 spin_lock_irqsave(&chip->reg_lock, flags);
699 snd_cs4231_out(chip, CS4231_REC_FORMAT, chip->image[CS4231_REC_FORMAT]);
700 spin_unlock_irqrestore(&chip->reg_lock, flags);
701 snd_cs4231_mce_down(chip);
702
703#ifdef SNDRV_DEBUG_MCE
704 snd_printk("init: (5)\n");
705#endif
706}
707
708static int snd_cs4231_open(cs4231_t *chip, unsigned int mode)
709{
710 unsigned long flags;
711
712 down(&chip->open_mutex);
713 if ((chip->mode & mode) ||
714 ((chip->mode & CS4231_MODE_OPEN) && chip->single_dma)) {
715 up(&chip->open_mutex);
716 return -EAGAIN;
717 }
718 if (chip->mode & CS4231_MODE_OPEN) {
719 chip->mode |= mode;
720 up(&chip->open_mutex);
721 return 0;
722 }
723 /* ok. now enable and ack CODEC IRQ */
724 spin_lock_irqsave(&chip->reg_lock, flags);
725 snd_cs4231_out(chip, CS4231_IRQ_STATUS, CS4231_PLAYBACK_IRQ |
726 CS4231_RECORD_IRQ |
727 CS4231_TIMER_IRQ);
728 snd_cs4231_out(chip, CS4231_IRQ_STATUS, 0);
729 cs4231_outb(chip, CS4231P(STATUS), 0); /* clear IRQ */
730 cs4231_outb(chip, CS4231P(STATUS), 0); /* clear IRQ */
731 chip->image[CS4231_PIN_CTRL] |= CS4231_IRQ_ENABLE;
732 snd_cs4231_out(chip, CS4231_PIN_CTRL, chip->image[CS4231_PIN_CTRL]);
733 snd_cs4231_out(chip, CS4231_IRQ_STATUS, CS4231_PLAYBACK_IRQ |
734 CS4231_RECORD_IRQ |
735 CS4231_TIMER_IRQ);
736 snd_cs4231_out(chip, CS4231_IRQ_STATUS, 0);
737 spin_unlock_irqrestore(&chip->reg_lock, flags);
738
739 chip->mode = mode;
740 up(&chip->open_mutex);
741 return 0;
742}
743
744static void snd_cs4231_close(cs4231_t *chip, unsigned int mode)
745{
746 unsigned long flags;
747
748 down(&chip->open_mutex);
749 chip->mode &= ~mode;
750 if (chip->mode & CS4231_MODE_OPEN) {
751 up(&chip->open_mutex);
752 return;
753 }
754 snd_cs4231_calibrate_mute(chip, 1);
755
756 /* disable IRQ */
757 spin_lock_irqsave(&chip->reg_lock, flags);
758 snd_cs4231_out(chip, CS4231_IRQ_STATUS, 0);
759 cs4231_outb(chip, CS4231P(STATUS), 0); /* clear IRQ */
760 cs4231_outb(chip, CS4231P(STATUS), 0); /* clear IRQ */
761 chip->image[CS4231_PIN_CTRL] &= ~CS4231_IRQ_ENABLE;
762 snd_cs4231_out(chip, CS4231_PIN_CTRL, chip->image[CS4231_PIN_CTRL]);
763
764 /* now disable record & playback */
765
766 if (chip->image[CS4231_IFACE_CTRL] & (CS4231_PLAYBACK_ENABLE | CS4231_PLAYBACK_PIO |
767 CS4231_RECORD_ENABLE | CS4231_RECORD_PIO)) {
768 spin_unlock_irqrestore(&chip->reg_lock, flags);
769 snd_cs4231_mce_up(chip);
770 spin_lock_irqsave(&chip->reg_lock, flags);
771 chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_PLAYBACK_ENABLE | CS4231_PLAYBACK_PIO |
772 CS4231_RECORD_ENABLE | CS4231_RECORD_PIO);
773 snd_cs4231_out(chip, CS4231_IFACE_CTRL, chip->image[CS4231_IFACE_CTRL]);
774 spin_unlock_irqrestore(&chip->reg_lock, flags);
775 snd_cs4231_mce_down(chip);
776 spin_lock_irqsave(&chip->reg_lock, flags);
777 }
778
779 /* clear IRQ again */
780 snd_cs4231_out(chip, CS4231_IRQ_STATUS, 0);
781 cs4231_outb(chip, CS4231P(STATUS), 0); /* clear IRQ */
782 cs4231_outb(chip, CS4231P(STATUS), 0); /* clear IRQ */
783 spin_unlock_irqrestore(&chip->reg_lock, flags);
784
785 snd_cs4231_calibrate_mute(chip, 0);
786
787 chip->mode = 0;
788 up(&chip->open_mutex);
789}
790
791/*
792 * timer open/close
793 */
794
795static int snd_cs4231_timer_open(snd_timer_t * timer)
796{
797 cs4231_t *chip = snd_timer_chip(timer);
798 snd_cs4231_open(chip, CS4231_MODE_TIMER);
799 return 0;
800}
801
802static int snd_cs4231_timer_close(snd_timer_t * timer)
803{
804 cs4231_t *chip = snd_timer_chip(timer);
805 snd_cs4231_close(chip, CS4231_MODE_TIMER);
806 return 0;
807}
808
809static struct _snd_timer_hardware snd_cs4231_timer_table =
810{
811 .flags = SNDRV_TIMER_HW_AUTO,
812 .resolution = 9945,
813 .ticks = 65535,
814 .open = snd_cs4231_timer_open,
815 .close = snd_cs4231_timer_close,
816 .c_resolution = snd_cs4231_timer_resolution,
817 .start = snd_cs4231_timer_start,
818 .stop = snd_cs4231_timer_stop,
819};
820
821/*
822 * ok.. exported functions..
823 */
824
825static int snd_cs4231_playback_hw_params(snd_pcm_substream_t * substream,
826 snd_pcm_hw_params_t * hw_params)
827{
828 cs4231_t *chip = snd_pcm_substream_chip(substream);
829 unsigned char new_pdfr;
830 int err;
831
832 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
833 return err;
834 new_pdfr = snd_cs4231_get_format(chip, params_format(hw_params), params_channels(hw_params)) |
835 snd_cs4231_get_rate(params_rate(hw_params));
836 chip->set_playback_format(chip, hw_params, new_pdfr);
837 return 0;
838}
839
840static int snd_cs4231_playback_hw_free(snd_pcm_substream_t * substream)
841{
842 return snd_pcm_lib_free_pages(substream);
843}
844
1da177e4
LT
845static int snd_cs4231_playback_prepare(snd_pcm_substream_t * substream)
846{
847 cs4231_t *chip = snd_pcm_substream_chip(substream);
848 snd_pcm_runtime_t *runtime = substream->runtime;
849 unsigned long flags;
850 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
851 unsigned int count = snd_pcm_lib_period_bytes(substream);
852
853 spin_lock_irqsave(&chip->reg_lock, flags);
854 chip->p_dma_size = size;
855 chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_PLAYBACK_ENABLE | CS4231_PLAYBACK_PIO);
856 snd_dma_program(chip->dma1, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
857 count = snd_cs4231_get_count(chip->image[CS4231_PLAYBK_FORMAT], count) - 1;
858 snd_cs4231_out(chip, CS4231_PLY_LWR_CNT, (unsigned char) count);
859 snd_cs4231_out(chip, CS4231_PLY_UPR_CNT, (unsigned char) (count >> 8));
860 spin_unlock_irqrestore(&chip->reg_lock, flags);
861#if 0
862 snd_cs4231_debug(chip);
863#endif
864 return 0;
865}
1da177e4
LT
866
867static int snd_cs4231_capture_hw_params(snd_pcm_substream_t * substream,
868 snd_pcm_hw_params_t * hw_params)
869{
870 cs4231_t *chip = snd_pcm_substream_chip(substream);
871 unsigned char new_cdfr;
872 int err;
873
874 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
875 return err;
876 new_cdfr = snd_cs4231_get_format(chip, params_format(hw_params), params_channels(hw_params)) |
877 snd_cs4231_get_rate(params_rate(hw_params));
878 chip->set_capture_format(chip, hw_params, new_cdfr);
879 return 0;
880}
881
882static int snd_cs4231_capture_hw_free(snd_pcm_substream_t * substream)
883{
884 return snd_pcm_lib_free_pages(substream);
885}
886
1da177e4
LT
887static int snd_cs4231_capture_prepare(snd_pcm_substream_t * substream)
888{
889 cs4231_t *chip = snd_pcm_substream_chip(substream);
890 snd_pcm_runtime_t *runtime = substream->runtime;
891 unsigned long flags;
892 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
893 unsigned int count = snd_pcm_lib_period_bytes(substream);
894
895 spin_lock_irqsave(&chip->reg_lock, flags);
896 chip->c_dma_size = size;
897 chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_RECORD_ENABLE | CS4231_RECORD_PIO);
898 snd_dma_program(chip->dma2, runtime->dma_addr, size, DMA_MODE_READ | DMA_AUTOINIT);
899 count = snd_cs4231_get_count(chip->image[CS4231_REC_FORMAT], count) - 1;
900 if (chip->single_dma && chip->hardware != CS4231_HW_INTERWAVE) {
901 snd_cs4231_out(chip, CS4231_PLY_LWR_CNT, (unsigned char) count);
902 snd_cs4231_out(chip, CS4231_PLY_UPR_CNT, (unsigned char) (count >> 8));
903 } else {
904 snd_cs4231_out(chip, CS4231_REC_LWR_CNT, (unsigned char) count);
905 snd_cs4231_out(chip, CS4231_REC_UPR_CNT, (unsigned char) (count >> 8));
906 }
907 spin_unlock_irqrestore(&chip->reg_lock, flags);
908 return 0;
909}
1da177e4
LT
910
911static void snd_cs4231_overrange(cs4231_t *chip)
912{
913 unsigned long flags;
914 unsigned char res;
915
916 spin_lock_irqsave(&chip->reg_lock, flags);
917 res = snd_cs4231_in(chip, CS4231_TEST_INIT);
918 spin_unlock_irqrestore(&chip->reg_lock, flags);
919 if (res & (0x08 | 0x02)) /* detect overrange only above 0dB; may be user selectable? */
920 chip->capture_substream->runtime->overrange++;
921}
922
923irqreturn_t snd_cs4231_interrupt(int irq, void *dev_id, struct pt_regs *regs)
924{
925 cs4231_t *chip = dev_id;
926 unsigned char status;
927
928 status = snd_cs4231_in(chip, CS4231_IRQ_STATUS);
929 if (status & CS4231_TIMER_IRQ) {
930 if (chip->timer)
931 snd_timer_interrupt(chip->timer, chip->timer->sticks);
932 }
933 if (chip->single_dma && chip->hardware != CS4231_HW_INTERWAVE) {
934 if (status & CS4231_PLAYBACK_IRQ) {
935 if (chip->mode & CS4231_MODE_PLAY) {
936 if (chip->playback_substream)
937 snd_pcm_period_elapsed(chip->playback_substream);
938 }
939 if (chip->mode & CS4231_MODE_RECORD) {
940 if (chip->capture_substream) {
941 snd_cs4231_overrange(chip);
942 snd_pcm_period_elapsed(chip->capture_substream);
943 }
944 }
945 }
946 } else {
947 if (status & CS4231_PLAYBACK_IRQ) {
948 if (chip->playback_substream)
949 snd_pcm_period_elapsed(chip->playback_substream);
950 }
951 if (status & CS4231_RECORD_IRQ) {
952 if (chip->capture_substream) {
953 snd_cs4231_overrange(chip);
954 snd_pcm_period_elapsed(chip->capture_substream);
955 }
956 }
957 }
958
959 spin_lock(&chip->reg_lock);
960 snd_cs4231_outm(chip, CS4231_IRQ_STATUS, ~CS4231_ALL_IRQS | ~status, 0);
961 spin_unlock(&chip->reg_lock);
962 return IRQ_HANDLED;
963}
964
1da177e4
LT
965static snd_pcm_uframes_t snd_cs4231_playback_pointer(snd_pcm_substream_t * substream)
966{
967 cs4231_t *chip = snd_pcm_substream_chip(substream);
968 size_t ptr;
969
970 if (!(chip->image[CS4231_IFACE_CTRL] & CS4231_PLAYBACK_ENABLE))
971 return 0;
972 ptr = snd_dma_pointer(chip->dma1, chip->p_dma_size);
973 return bytes_to_frames(substream->runtime, ptr);
974}
975
976static snd_pcm_uframes_t snd_cs4231_capture_pointer(snd_pcm_substream_t * substream)
977{
978 cs4231_t *chip = snd_pcm_substream_chip(substream);
979 size_t ptr;
980
981 if (!(chip->image[CS4231_IFACE_CTRL] & CS4231_RECORD_ENABLE))
982 return 0;
983 ptr = snd_dma_pointer(chip->dma2, chip->c_dma_size);
984 return bytes_to_frames(substream->runtime, ptr);
985}
1da177e4
LT
986
987/*
988
989 */
990
991static int snd_cs4231_probe(cs4231_t *chip)
992{
993 unsigned long flags;
994 int i, id, rev;
995 unsigned char *ptr;
996 unsigned int hw;
997
998#if 0
999 snd_cs4231_debug(chip);
1000#endif
1001 id = 0;
1002 for (i = 0; i < 50; i++) {
1003 mb();
1004 if (cs4231_inb(chip, CS4231P(REGSEL)) & CS4231_INIT)
1005 udelay(2000);
1006 else {
1007 spin_lock_irqsave(&chip->reg_lock, flags);
1008 snd_cs4231_out(chip, CS4231_MISC_INFO, CS4231_MODE2);
1009 id = snd_cs4231_in(chip, CS4231_MISC_INFO) & 0x0f;
1010 spin_unlock_irqrestore(&chip->reg_lock, flags);
1011 if (id == 0x0a)
1012 break; /* this is valid value */
1013 }
1014 }
1015 snd_printdd("cs4231: port = 0x%lx, id = 0x%x\n", chip->port, id);
1016 if (id != 0x0a)
1017 return -ENODEV; /* no valid device found */
1018
1019 if (((hw = chip->hardware) & CS4231_HW_TYPE_MASK) == CS4231_HW_DETECT) {
1020 rev = snd_cs4231_in(chip, CS4231_VERSION) & 0xe7;
1021 snd_printdd("CS4231: VERSION (I25) = 0x%x\n", rev);
1022 if (rev == 0x80) {
1023 unsigned char tmp = snd_cs4231_in(chip, 23);
1024 snd_cs4231_out(chip, 23, ~tmp);
1025 if (snd_cs4231_in(chip, 23) != tmp)
1026 chip->hardware = CS4231_HW_AD1845;
1027 else
1028 chip->hardware = CS4231_HW_CS4231;
1029 } else if (rev == 0xa0) {
1030 chip->hardware = CS4231_HW_CS4231A;
1031 } else if (rev == 0xa2) {
1032 chip->hardware = CS4231_HW_CS4232;
1033 } else if (rev == 0xb2) {
1034 chip->hardware = CS4231_HW_CS4232A;
1035 } else if (rev == 0x83) {
1036 chip->hardware = CS4231_HW_CS4236;
1037 } else if (rev == 0x03) {
1038 chip->hardware = CS4231_HW_CS4236B;
1039 } else {
1040 snd_printk("unknown CS chip with version 0x%x\n", rev);
1041 return -ENODEV; /* unknown CS4231 chip? */
1042 }
1043 }
1044 spin_lock_irqsave(&chip->reg_lock, flags);
1045 cs4231_inb(chip, CS4231P(STATUS)); /* clear any pendings IRQ */
1046 cs4231_outb(chip, CS4231P(STATUS), 0);
1047 mb();
1048 spin_unlock_irqrestore(&chip->reg_lock, flags);
1049
1050 chip->image[CS4231_MISC_INFO] = CS4231_MODE2;
1051 switch (chip->hardware) {
1052 case CS4231_HW_INTERWAVE:
1053 chip->image[CS4231_MISC_INFO] = CS4231_IW_MODE3;
1054 break;
1055 case CS4231_HW_CS4235:
1056 case CS4231_HW_CS4236B:
1057 case CS4231_HW_CS4237B:
1058 case CS4231_HW_CS4238B:
1059 case CS4231_HW_CS4239:
1060 if (hw == CS4231_HW_DETECT3)
1061 chip->image[CS4231_MISC_INFO] = CS4231_4236_MODE3;
1062 else
1063 chip->hardware = CS4231_HW_CS4236;
1064 break;
1065 }
1066
1067 chip->image[CS4231_IFACE_CTRL] =
1068 (chip->image[CS4231_IFACE_CTRL] & ~CS4231_SINGLE_DMA) |
1069 (chip->single_dma ? CS4231_SINGLE_DMA : 0);
1070 chip->image[CS4231_ALT_FEATURE_1] = 0x80;
1071 chip->image[CS4231_ALT_FEATURE_2] = chip->hardware == CS4231_HW_INTERWAVE ? 0xc2 : 0x01;
1072 ptr = (unsigned char *) &chip->image;
1073 snd_cs4231_mce_down(chip);
1074 spin_lock_irqsave(&chip->reg_lock, flags);
1075 for (i = 0; i < 32; i++) /* ok.. fill all CS4231 registers */
1076 snd_cs4231_out(chip, i, *ptr++);
1077 spin_unlock_irqrestore(&chip->reg_lock, flags);
1078 snd_cs4231_mce_up(chip);
1079 snd_cs4231_mce_down(chip);
1080
1081 mdelay(2);
1082
1083 /* ok.. try check hardware version for CS4236+ chips */
1084 if ((hw & CS4231_HW_TYPE_MASK) == CS4231_HW_DETECT) {
1085 if (chip->hardware == CS4231_HW_CS4236B) {
1086 rev = snd_cs4236_ext_in(chip, CS4236_VERSION);
1087 snd_cs4236_ext_out(chip, CS4236_VERSION, 0xff);
1088 id = snd_cs4236_ext_in(chip, CS4236_VERSION);
1089 snd_cs4236_ext_out(chip, CS4236_VERSION, rev);
1090 snd_printdd("CS4231: ext version; rev = 0x%x, id = 0x%x\n", rev, id);
1091 if ((id & 0x1f) == 0x1d) { /* CS4235 */
1092 chip->hardware = CS4231_HW_CS4235;
1093 switch (id >> 5) {
1094 case 4:
1095 case 5:
1096 case 6:
1097 break;
1098 default:
1099 snd_printk("unknown CS4235 chip (enhanced version = 0x%x)\n", id);
1100 }
1101 } else if ((id & 0x1f) == 0x0b) { /* CS4236/B */
1102 switch (id >> 5) {
1103 case 4:
1104 case 5:
1105 case 6:
1106 case 7:
1107 chip->hardware = CS4231_HW_CS4236B;
1108 break;
1109 default:
1110 snd_printk("unknown CS4236 chip (enhanced version = 0x%x)\n", id);
1111 }
1112 } else if ((id & 0x1f) == 0x08) { /* CS4237B */
1113 chip->hardware = CS4231_HW_CS4237B;
1114 switch (id >> 5) {
1115 case 4:
1116 case 5:
1117 case 6:
1118 case 7:
1119 break;
1120 default:
1121 snd_printk("unknown CS4237B chip (enhanced version = 0x%x)\n", id);
1122 }
1123 } else if ((id & 0x1f) == 0x09) { /* CS4238B */
1124 chip->hardware = CS4231_HW_CS4238B;
1125 switch (id >> 5) {
1126 case 5:
1127 case 6:
1128 case 7:
1129 break;
1130 default:
1131 snd_printk("unknown CS4238B chip (enhanced version = 0x%x)\n", id);
1132 }
1133 } else if ((id & 0x1f) == 0x1e) { /* CS4239 */
1134 chip->hardware = CS4231_HW_CS4239;
1135 switch (id >> 5) {
1136 case 4:
1137 case 5:
1138 case 6:
1139 break;
1140 default:
1141 snd_printk("unknown CS4239 chip (enhanced version = 0x%x)\n", id);
1142 }
1143 } else {
1144 snd_printk("unknown CS4236/CS423xB chip (enhanced version = 0x%x)\n", id);
1145 }
1146 }
1147 }
1148 return 0; /* all things are ok.. */
1149}
1150
1151/*
1152
1153 */
1154
1155static snd_pcm_hardware_t snd_cs4231_playback =
1156{
1157 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1158 SNDRV_PCM_INFO_MMAP_VALID |
1159 SNDRV_PCM_INFO_RESUME |
1160 SNDRV_PCM_INFO_SYNC_START),
1161 .formats = (SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW | SNDRV_PCM_FMTBIT_IMA_ADPCM |
1162 SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE),
1163 .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
1164 .rate_min = 5510,
1165 .rate_max = 48000,
1166 .channels_min = 1,
1167 .channels_max = 2,
1168 .buffer_bytes_max = (128*1024),
1169 .period_bytes_min = 64,
1170 .period_bytes_max = (128*1024),
1171 .periods_min = 1,
1172 .periods_max = 1024,
1173 .fifo_size = 0,
1174};
1175
1176static snd_pcm_hardware_t snd_cs4231_capture =
1177{
1178 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1179 SNDRV_PCM_INFO_MMAP_VALID |
1180 SNDRV_PCM_INFO_RESUME |
1181 SNDRV_PCM_INFO_SYNC_START),
1182 .formats = (SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW | SNDRV_PCM_FMTBIT_IMA_ADPCM |
1183 SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE),
1184 .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
1185 .rate_min = 5510,
1186 .rate_max = 48000,
1187 .channels_min = 1,
1188 .channels_max = 2,
1189 .buffer_bytes_max = (128*1024),
1190 .period_bytes_min = 64,
1191 .period_bytes_max = (128*1024),
1192 .periods_min = 1,
1193 .periods_max = 1024,
1194 .fifo_size = 0,
1195};
1196
1197/*
1198
1199 */
1200
1201static int snd_cs4231_playback_open(snd_pcm_substream_t * substream)
1202{
1203 cs4231_t *chip = snd_pcm_substream_chip(substream);
1204 snd_pcm_runtime_t *runtime = substream->runtime;
1205 int err;
1206
1207 runtime->hw = snd_cs4231_playback;
1208
1209 /* hardware bug in InterWave chipset */
1210 if (chip->hardware == CS4231_HW_INTERWAVE && chip->dma1 > 3)
1211 runtime->hw.formats &= ~SNDRV_PCM_FMTBIT_MU_LAW;
1212
1213 /* hardware limitation of cheap chips */
1214 if (chip->hardware == CS4231_HW_CS4235 ||
1215 chip->hardware == CS4231_HW_CS4239)
1216 runtime->hw.formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE;
1217
1da177e4
LT
1218 snd_pcm_limit_isa_dma_size(chip->dma1, &runtime->hw.buffer_bytes_max);
1219 snd_pcm_limit_isa_dma_size(chip->dma1, &runtime->hw.period_bytes_max);
1220
1221 if (chip->claim_dma) {
1222 if ((err = chip->claim_dma(chip, chip->dma_private_data, chip->dma1)) < 0)
1223 return err;
1224 }
1da177e4
LT
1225
1226 if ((err = snd_cs4231_open(chip, CS4231_MODE_PLAY)) < 0) {
1da177e4
LT
1227 if (chip->release_dma)
1228 chip->release_dma(chip, chip->dma_private_data, chip->dma1);
1da177e4
LT
1229 snd_free_pages(runtime->dma_area, runtime->dma_bytes);
1230 return err;
1231 }
1232 chip->playback_substream = substream;
1da177e4
LT
1233 snd_pcm_set_sync(substream);
1234 chip->rate_constraint(runtime);
1235 return 0;
1236}
1237
1238static int snd_cs4231_capture_open(snd_pcm_substream_t * substream)
1239{
1240 cs4231_t *chip = snd_pcm_substream_chip(substream);
1241 snd_pcm_runtime_t *runtime = substream->runtime;
1242 int err;
1243
1244 runtime->hw = snd_cs4231_capture;
1245
1246 /* hardware limitation of cheap chips */
1247 if (chip->hardware == CS4231_HW_CS4235 ||
1248 chip->hardware == CS4231_HW_CS4239)
1249 runtime->hw.formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE;
1250
1da177e4
LT
1251 snd_pcm_limit_isa_dma_size(chip->dma2, &runtime->hw.buffer_bytes_max);
1252 snd_pcm_limit_isa_dma_size(chip->dma2, &runtime->hw.period_bytes_max);
1253
1254 if (chip->claim_dma) {
1255 if ((err = chip->claim_dma(chip, chip->dma_private_data, chip->dma2)) < 0)
1256 return err;
1257 }
1da177e4
LT
1258
1259 if ((err = snd_cs4231_open(chip, CS4231_MODE_RECORD)) < 0) {
1da177e4
LT
1260 if (chip->release_dma)
1261 chip->release_dma(chip, chip->dma_private_data, chip->dma2);
1da177e4
LT
1262 snd_free_pages(runtime->dma_area, runtime->dma_bytes);
1263 return err;
1264 }
1265 chip->capture_substream = substream;
1da177e4
LT
1266 snd_pcm_set_sync(substream);
1267 chip->rate_constraint(runtime);
1268 return 0;
1269}
1270
1271static int snd_cs4231_playback_close(snd_pcm_substream_t * substream)
1272{
1273 cs4231_t *chip = snd_pcm_substream_chip(substream);
1274
1275 chip->playback_substream = NULL;
1276 snd_cs4231_close(chip, CS4231_MODE_PLAY);
1277 return 0;
1278}
1279
1280static int snd_cs4231_capture_close(snd_pcm_substream_t * substream)
1281{
1282 cs4231_t *chip = snd_pcm_substream_chip(substream);
1283
1284 chip->capture_substream = NULL;
1285 snd_cs4231_close(chip, CS4231_MODE_RECORD);
1286 return 0;
1287}
1288
1289#ifdef CONFIG_PM
1290
1291/* lowlevel suspend callback for CS4231 */
1292static void snd_cs4231_suspend(cs4231_t *chip)
1293{
1294 int reg;
1295 unsigned long flags;
1296
41e4845c
JK
1297 if (chip->pcm)
1298 snd_pcm_suspend_all(chip->pcm);
1da177e4
LT
1299 spin_lock_irqsave(&chip->reg_lock, flags);
1300 for (reg = 0; reg < 32; reg++)
1301 chip->image[reg] = snd_cs4231_in(chip, reg);
1302 spin_unlock_irqrestore(&chip->reg_lock, flags);
1303}
1304
1305/* lowlevel resume callback for CS4231 */
1306static void snd_cs4231_resume(cs4231_t *chip)
1307{
1308 int reg;
1309 unsigned long flags;
1310 int timeout;
1311
1312 snd_cs4231_mce_up(chip);
1313 spin_lock_irqsave(&chip->reg_lock, flags);
1314 for (reg = 0; reg < 32; reg++) {
1315 switch (reg) {
1316 case CS4231_VERSION:
1317 break;
1318 default:
1319 snd_cs4231_out(chip, reg, chip->image[reg]);
1320 break;
1321 }
1322 }
1323 spin_unlock_irqrestore(&chip->reg_lock, flags);
1324#if 0
1325 snd_cs4231_mce_down(chip);
1326#else
1327 /* The following is a workaround to avoid freeze after resume on TP600E.
1328 This is the first half of copy of snd_cs4231_mce_down(), but doesn't
1329 include rescheduling. -- iwai
1330 */
1331 snd_cs4231_busy_wait(chip);
1332 spin_lock_irqsave(&chip->reg_lock, flags);
1333 chip->mce_bit &= ~CS4231_MCE;
1334 timeout = cs4231_inb(chip, CS4231P(REGSEL));
1335 cs4231_outb(chip, CS4231P(REGSEL), chip->mce_bit | (timeout & 0x1f));
1336 spin_unlock_irqrestore(&chip->reg_lock, flags);
1337 if (timeout == 0x80)
1338 snd_printk("down [0x%lx]: serious init problem - codec still busy\n", chip->port);
1339 if ((timeout & CS4231_MCE) == 0 ||
1340 !(chip->hardware & (CS4231_HW_CS4231_MASK | CS4231_HW_CS4232_MASK))) {
1341 return;
1342 }
1343 snd_cs4231_busy_wait(chip);
1344#endif
1345}
1346
1347static int snd_cs4231_pm_suspend(snd_card_t *card, pm_message_t state)
1348{
1349 cs4231_t *chip = card->pm_private_data;
1350 if (chip->suspend)
1351 chip->suspend(chip);
1352 return 0;
1353}
1354
1355static int snd_cs4231_pm_resume(snd_card_t *card)
1356{
1357 cs4231_t *chip = card->pm_private_data;
1358 if (chip->resume)
1359 chip->resume(chip);
1360 return 0;
1361}
1362#endif /* CONFIG_PM */
1363
1da177e4
LT
1364static int snd_cs4231_free(cs4231_t *chip)
1365{
b1d5776d
TI
1366 release_and_free_resource(chip->res_port);
1367 release_and_free_resource(chip->res_cport);
1da177e4
LT
1368 if (chip->irq >= 0) {
1369 disable_irq(chip->irq);
1370 if (!(chip->hwshare & CS4231_HWSHARE_IRQ))
1371 free_irq(chip->irq, (void *) chip);
1372 }
1373 if (!(chip->hwshare & CS4231_HWSHARE_DMA1) && chip->dma1 >= 0) {
1374 snd_dma_disable(chip->dma1);
1375 free_dma(chip->dma1);
1376 }
1377 if (!(chip->hwshare & CS4231_HWSHARE_DMA2) && chip->dma2 >= 0 && chip->dma2 != chip->dma1) {
1378 snd_dma_disable(chip->dma2);
1379 free_dma(chip->dma2);
1380 }
1381 if (chip->timer)
1382 snd_device_free(chip->card, chip->timer);
1383 kfree(chip);
1384 return 0;
1385}
1386
1387static int snd_cs4231_dev_free(snd_device_t *device)
1388{
1389 cs4231_t *chip = device->device_data;
1390 return snd_cs4231_free(chip);
1391}
1392
1da177e4
LT
1393const char *snd_cs4231_chip_id(cs4231_t *chip)
1394{
1395 switch (chip->hardware) {
1396 case CS4231_HW_CS4231: return "CS4231";
1397 case CS4231_HW_CS4231A: return "CS4231A";
1398 case CS4231_HW_CS4232: return "CS4232";
1399 case CS4231_HW_CS4232A: return "CS4232A";
1400 case CS4231_HW_CS4235: return "CS4235";
1401 case CS4231_HW_CS4236: return "CS4236";
1402 case CS4231_HW_CS4236B: return "CS4236B";
1403 case CS4231_HW_CS4237B: return "CS4237B";
1404 case CS4231_HW_CS4238B: return "CS4238B";
1405 case CS4231_HW_CS4239: return "CS4239";
1406 case CS4231_HW_INTERWAVE: return "AMD InterWave";
1407 case CS4231_HW_OPL3SA2: return chip->card->shortname;
1408 case CS4231_HW_AD1845: return "AD1845";
1409 default: return "???";
1410 }
1411}
1412
1413static int snd_cs4231_new(snd_card_t * card,
1414 unsigned short hardware,
1415 unsigned short hwshare,
1416 cs4231_t ** rchip)
1417{
1418 cs4231_t *chip;
1419
1420 *rchip = NULL;
9e76a76e 1421 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1da177e4
LT
1422 if (chip == NULL)
1423 return -ENOMEM;
1424 chip->hardware = hardware;
1425 chip->hwshare = hwshare;
1426
1427 spin_lock_init(&chip->reg_lock);
1428 init_MUTEX(&chip->mce_mutex);
1429 init_MUTEX(&chip->open_mutex);
1430 chip->card = card;
1431 chip->rate_constraint = snd_cs4231_xrate;
1432 chip->set_playback_format = snd_cs4231_playback_format;
1433 chip->set_capture_format = snd_cs4231_capture_format;
1434 memcpy(&chip->image, &snd_cs4231_original_image, sizeof(snd_cs4231_original_image));
1435
1436 *rchip = chip;
1437 return 0;
1438}
1439
1da177e4
LT
1440int snd_cs4231_create(snd_card_t * card,
1441 unsigned long port,
1442 unsigned long cport,
1443 int irq, int dma1, int dma2,
1444 unsigned short hardware,
1445 unsigned short hwshare,
1446 cs4231_t ** rchip)
1447{
1448 static snd_device_ops_t ops = {
1449 .dev_free = snd_cs4231_dev_free,
1450 };
1451 cs4231_t *chip;
1452 int err;
1453
1454 err = snd_cs4231_new(card, hardware, hwshare, &chip);
1455 if (err < 0)
1456 return err;
1457
1458 chip->irq = -1;
1459 chip->dma1 = -1;
1460 chip->dma2 = -1;
1461
1462 if ((chip->res_port = request_region(port, 4, "CS4231")) == NULL) {
1463 snd_printk(KERN_ERR "cs4231: can't grab port 0x%lx\n", port);
1464 snd_cs4231_free(chip);
1465 return -EBUSY;
1466 }
1467 chip->port = port;
1468 if ((long)cport >= 0 && (chip->res_cport = request_region(cport, 8, "CS4232 Control")) == NULL) {
1469 snd_printk(KERN_ERR "cs4231: can't grab control port 0x%lx\n", cport);
1470 snd_cs4231_free(chip);
1471 return -ENODEV;
1472 }
1473 chip->cport = cport;
1474 if (!(hwshare & CS4231_HWSHARE_IRQ) && request_irq(irq, snd_cs4231_interrupt, SA_INTERRUPT, "CS4231", (void *) chip)) {
1475 snd_printk(KERN_ERR "cs4231: can't grab IRQ %d\n", irq);
1476 snd_cs4231_free(chip);
1477 return -EBUSY;
1478 }
1479 chip->irq = irq;
1480 if (!(hwshare & CS4231_HWSHARE_DMA1) && request_dma(dma1, "CS4231 - 1")) {
1481 snd_printk(KERN_ERR "cs4231: can't grab DMA1 %d\n", dma1);
1482 snd_cs4231_free(chip);
1483 return -EBUSY;
1484 }
1485 chip->dma1 = dma1;
1486 if (!(hwshare & CS4231_HWSHARE_DMA2) && dma1 != dma2 && dma2 >= 0 && request_dma(dma2, "CS4231 - 2")) {
1487 snd_printk(KERN_ERR "cs4231: can't grab DMA2 %d\n", dma2);
1488 snd_cs4231_free(chip);
1489 return -EBUSY;
1490 }
1491 if (dma1 == dma2 || dma2 < 0) {
1492 chip->single_dma = 1;
1493 chip->dma2 = chip->dma1;
1494 } else
1495 chip->dma2 = dma2;
1496
1497 /* global setup */
1498 if (snd_cs4231_probe(chip) < 0) {
1499 snd_cs4231_free(chip);
1500 return -ENODEV;
1501 }
1502 snd_cs4231_init(chip);
1503
1504 if (chip->hardware & CS4231_HW_CS4232_MASK) {
1505 if (chip->res_cport == NULL)
1506 snd_printk("CS4232 control port features are not accessible\n");
1507 }
1508
1509 /* Register device */
1510 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
1511 snd_cs4231_free(chip);
1512 return err;
1513 }
1514
1515#ifdef CONFIG_PM
1516 /* Power Management */
1517 chip->suspend = snd_cs4231_suspend;
1518 chip->resume = snd_cs4231_resume;
1519 snd_card_set_isa_pm_callback(card, snd_cs4231_pm_suspend, snd_cs4231_pm_resume, chip);
1520#endif
1521
1522 *rchip = chip;
1523 return 0;
1524}
1525
1da177e4
LT
1526static snd_pcm_ops_t snd_cs4231_playback_ops = {
1527 .open = snd_cs4231_playback_open,
1528 .close = snd_cs4231_playback_close,
1529 .ioctl = snd_pcm_lib_ioctl,
1530 .hw_params = snd_cs4231_playback_hw_params,
1531 .hw_free = snd_cs4231_playback_hw_free,
1532 .prepare = snd_cs4231_playback_prepare,
1533 .trigger = snd_cs4231_trigger,
1534 .pointer = snd_cs4231_playback_pointer,
1535};
1536
1537static snd_pcm_ops_t snd_cs4231_capture_ops = {
1538 .open = snd_cs4231_capture_open,
1539 .close = snd_cs4231_capture_close,
1540 .ioctl = snd_pcm_lib_ioctl,
1541 .hw_params = snd_cs4231_capture_hw_params,
1542 .hw_free = snd_cs4231_capture_hw_free,
1543 .prepare = snd_cs4231_capture_prepare,
1544 .trigger = snd_cs4231_trigger,
1545 .pointer = snd_cs4231_capture_pointer,
1546};
1547
1da177e4
LT
1548int snd_cs4231_pcm(cs4231_t *chip, int device, snd_pcm_t **rpcm)
1549{
1550 snd_pcm_t *pcm;
1551 int err;
1552
1553 if ((err = snd_pcm_new(chip->card, "CS4231", device, 1, 1, &pcm)) < 0)
1554 return err;
1555
1556 spin_lock_init(&chip->reg_lock);
1557 init_MUTEX(&chip->mce_mutex);
1558 init_MUTEX(&chip->open_mutex);
1559
1560 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cs4231_playback_ops);
1561 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cs4231_capture_ops);
1562
1563 /* global setup */
1564 pcm->private_data = chip;
1da177e4
LT
1565 pcm->info_flags = 0;
1566 if (chip->single_dma)
1567 pcm->info_flags |= SNDRV_PCM_INFO_HALF_DUPLEX;
1568 if (chip->hardware != CS4231_HW_INTERWAVE)
1569 pcm->info_flags |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1570 strcpy(pcm->name, snd_cs4231_chip_id(chip));
1571
1da177e4
LT
1572 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1573 snd_dma_isa_data(),
1574 64*1024, chip->dma1 > 3 || chip->dma2 > 3 ? 128*1024 : 64*1024);
1da177e4
LT
1575
1576 chip->pcm = pcm;
1577 if (rpcm)
1578 *rpcm = pcm;
1579 return 0;
1580}
1581
1582static void snd_cs4231_timer_free(snd_timer_t *timer)
1583{
1584 cs4231_t *chip = timer->private_data;
1585 chip->timer = NULL;
1586}
1587
1588int snd_cs4231_timer(cs4231_t *chip, int device, snd_timer_t **rtimer)
1589{
1590 snd_timer_t *timer;
1591 snd_timer_id_t tid;
1592 int err;
1593
1594 /* Timer initialization */
1595 tid.dev_class = SNDRV_TIMER_CLASS_CARD;
1596 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1597 tid.card = chip->card->number;
1598 tid.device = device;
1599 tid.subdevice = 0;
1600 if ((err = snd_timer_new(chip->card, "CS4231", &tid, &timer)) < 0)
1601 return err;
1602 strcpy(timer->name, snd_cs4231_chip_id(chip));
1603 timer->private_data = chip;
1604 timer->private_free = snd_cs4231_timer_free;
1605 timer->hw = snd_cs4231_timer_table;
1606 chip->timer = timer;
1607 if (rtimer)
1608 *rtimer = timer;
1609 return 0;
1610}
1611
1612/*
1613 * MIXER part
1614 */
1615
1616static int snd_cs4231_info_mux(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1617{
1618 static char *texts[4] = {
1619 "Line", "Aux", "Mic", "Mix"
1620 };
1621 static char *opl3sa_texts[4] = {
1622 "Line", "CD", "Mic", "Mix"
1623 };
1624 static char *gusmax_texts[4] = {
1625 "Line", "Synth", "Mic", "Mix"
1626 };
1627 char **ptexts = texts;
1628 cs4231_t *chip = snd_kcontrol_chip(kcontrol);
1629
1630 snd_assert(chip->card != NULL, return -EINVAL);
1631 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1632 uinfo->count = 2;
1633 uinfo->value.enumerated.items = 4;
1634 if (uinfo->value.enumerated.item > 3)
1635 uinfo->value.enumerated.item = 3;
1636 if (!strcmp(chip->card->driver, "GUS MAX"))
1637 ptexts = gusmax_texts;
1638 switch (chip->hardware) {
1639 case CS4231_HW_INTERWAVE: ptexts = gusmax_texts; break;
1640 case CS4231_HW_OPL3SA2: ptexts = opl3sa_texts; break;
1641 }
1642 strcpy(uinfo->value.enumerated.name, ptexts[uinfo->value.enumerated.item]);
1643 return 0;
1644}
1645
1646static int snd_cs4231_get_mux(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1647{
1648 cs4231_t *chip = snd_kcontrol_chip(kcontrol);
1649 unsigned long flags;
1650
1651 spin_lock_irqsave(&chip->reg_lock, flags);
1652 ucontrol->value.enumerated.item[0] = (chip->image[CS4231_LEFT_INPUT] & CS4231_MIXS_ALL) >> 6;
1653 ucontrol->value.enumerated.item[1] = (chip->image[CS4231_RIGHT_INPUT] & CS4231_MIXS_ALL) >> 6;
1654 spin_unlock_irqrestore(&chip->reg_lock, flags);
1655 return 0;
1656}
1657
1658static int snd_cs4231_put_mux(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1659{
1660 cs4231_t *chip = snd_kcontrol_chip(kcontrol);
1661 unsigned long flags;
1662 unsigned short left, right;
1663 int change;
1664
1665 if (ucontrol->value.enumerated.item[0] > 3 ||
1666 ucontrol->value.enumerated.item[1] > 3)
1667 return -EINVAL;
1668 left = ucontrol->value.enumerated.item[0] << 6;
1669 right = ucontrol->value.enumerated.item[1] << 6;
1670 spin_lock_irqsave(&chip->reg_lock, flags);
1671 left = (chip->image[CS4231_LEFT_INPUT] & ~CS4231_MIXS_ALL) | left;
1672 right = (chip->image[CS4231_RIGHT_INPUT] & ~CS4231_MIXS_ALL) | right;
1673 change = left != chip->image[CS4231_LEFT_INPUT] ||
1674 right != chip->image[CS4231_RIGHT_INPUT];
1675 snd_cs4231_out(chip, CS4231_LEFT_INPUT, left);
1676 snd_cs4231_out(chip, CS4231_RIGHT_INPUT, right);
1677 spin_unlock_irqrestore(&chip->reg_lock, flags);
1678 return change;
1679}
1680
1681int snd_cs4231_info_single(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1682{
1683 int mask = (kcontrol->private_value >> 16) & 0xff;
1684
1685 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1686 uinfo->count = 1;
1687 uinfo->value.integer.min = 0;
1688 uinfo->value.integer.max = mask;
1689 return 0;
1690}
1691
1692int snd_cs4231_get_single(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1693{
1694 cs4231_t *chip = snd_kcontrol_chip(kcontrol);
1695 unsigned long flags;
1696 int reg = kcontrol->private_value & 0xff;
1697 int shift = (kcontrol->private_value >> 8) & 0xff;
1698 int mask = (kcontrol->private_value >> 16) & 0xff;
1699 int invert = (kcontrol->private_value >> 24) & 0xff;
1700
1701 spin_lock_irqsave(&chip->reg_lock, flags);
1702 ucontrol->value.integer.value[0] = (chip->image[reg] >> shift) & mask;
1703 spin_unlock_irqrestore(&chip->reg_lock, flags);
1704 if (invert)
1705 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
1706 return 0;
1707}
1708
1709int snd_cs4231_put_single(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1710{
1711 cs4231_t *chip = snd_kcontrol_chip(kcontrol);
1712 unsigned long flags;
1713 int reg = kcontrol->private_value & 0xff;
1714 int shift = (kcontrol->private_value >> 8) & 0xff;
1715 int mask = (kcontrol->private_value >> 16) & 0xff;
1716 int invert = (kcontrol->private_value >> 24) & 0xff;
1717 int change;
1718 unsigned short val;
1719
1720 val = (ucontrol->value.integer.value[0] & mask);
1721 if (invert)
1722 val = mask - val;
1723 val <<= shift;
1724 spin_lock_irqsave(&chip->reg_lock, flags);
1725 val = (chip->image[reg] & ~(mask << shift)) | val;
1726 change = val != chip->image[reg];
1727 snd_cs4231_out(chip, reg, val);
1728 spin_unlock_irqrestore(&chip->reg_lock, flags);
1729 return change;
1730}
1731
1732int snd_cs4231_info_double(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1733{
1734 int mask = (kcontrol->private_value >> 24) & 0xff;
1735
1736 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1737 uinfo->count = 2;
1738 uinfo->value.integer.min = 0;
1739 uinfo->value.integer.max = mask;
1740 return 0;
1741}
1742
1743int snd_cs4231_get_double(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1744{
1745 cs4231_t *chip = snd_kcontrol_chip(kcontrol);
1746 unsigned long flags;
1747 int left_reg = kcontrol->private_value & 0xff;
1748 int right_reg = (kcontrol->private_value >> 8) & 0xff;
1749 int shift_left = (kcontrol->private_value >> 16) & 0x07;
1750 int shift_right = (kcontrol->private_value >> 19) & 0x07;
1751 int mask = (kcontrol->private_value >> 24) & 0xff;
1752 int invert = (kcontrol->private_value >> 22) & 1;
1753
1754 spin_lock_irqsave(&chip->reg_lock, flags);
1755 ucontrol->value.integer.value[0] = (chip->image[left_reg] >> shift_left) & mask;
1756 ucontrol->value.integer.value[1] = (chip->image[right_reg] >> shift_right) & mask;
1757 spin_unlock_irqrestore(&chip->reg_lock, flags);
1758 if (invert) {
1759 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
1760 ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
1761 }
1762 return 0;
1763}
1764
1765int snd_cs4231_put_double(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1766{
1767 cs4231_t *chip = snd_kcontrol_chip(kcontrol);
1768 unsigned long flags;
1769 int left_reg = kcontrol->private_value & 0xff;
1770 int right_reg = (kcontrol->private_value >> 8) & 0xff;
1771 int shift_left = (kcontrol->private_value >> 16) & 0x07;
1772 int shift_right = (kcontrol->private_value >> 19) & 0x07;
1773 int mask = (kcontrol->private_value >> 24) & 0xff;
1774 int invert = (kcontrol->private_value >> 22) & 1;
1775 int change;
1776 unsigned short val1, val2;
1777
1778 val1 = ucontrol->value.integer.value[0] & mask;
1779 val2 = ucontrol->value.integer.value[1] & mask;
1780 if (invert) {
1781 val1 = mask - val1;
1782 val2 = mask - val2;
1783 }
1784 val1 <<= shift_left;
1785 val2 <<= shift_right;
1786 spin_lock_irqsave(&chip->reg_lock, flags);
1787 val1 = (chip->image[left_reg] & ~(mask << shift_left)) | val1;
1788 val2 = (chip->image[right_reg] & ~(mask << shift_right)) | val2;
1789 change = val1 != chip->image[left_reg] || val2 != chip->image[right_reg];
1790 snd_cs4231_out(chip, left_reg, val1);
1791 snd_cs4231_out(chip, right_reg, val2);
1792 spin_unlock_irqrestore(&chip->reg_lock, flags);
1793 return change;
1794}
1795
1796static snd_kcontrol_new_t snd_cs4231_controls[] = {
1797CS4231_DOUBLE("PCM Playback Switch", 0, CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 7, 7, 1, 1),
1798CS4231_DOUBLE("PCM Playback Volume", 0, CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 0, 0, 63, 1),
1799CS4231_DOUBLE("Line Playback Switch", 0, CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 7, 7, 1, 1),
1800CS4231_DOUBLE("Line Playback Volume", 0, CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 0, 0, 31, 1),
1801CS4231_DOUBLE("Aux Playback Switch", 0, CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 7, 7, 1, 1),
1802CS4231_DOUBLE("Aux Playback Volume", 0, CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 0, 0, 31, 1),
1803CS4231_DOUBLE("Aux Playback Switch", 1, CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 7, 7, 1, 1),
1804CS4231_DOUBLE("Aux Playback Volume", 1, CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 0, 0, 31, 1),
1805CS4231_SINGLE("Mono Playback Switch", 0, CS4231_MONO_CTRL, 7, 1, 1),
1806CS4231_SINGLE("Mono Playback Volume", 0, CS4231_MONO_CTRL, 0, 15, 1),
1807CS4231_SINGLE("Mono Output Playback Switch", 0, CS4231_MONO_CTRL, 6, 1, 1),
1808CS4231_SINGLE("Mono Output Playback Bypass", 0, CS4231_MONO_CTRL, 5, 1, 0),
1809CS4231_DOUBLE("Capture Volume", 0, CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT, 0, 0, 15, 0),
1810{
1811 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1812 .name = "Capture Source",
1813 .info = snd_cs4231_info_mux,
1814 .get = snd_cs4231_get_mux,
1815 .put = snd_cs4231_put_mux,
1816},
1817CS4231_DOUBLE("Mic Boost", 0, CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT, 5, 5, 1, 0),
1818CS4231_SINGLE("Loopback Capture Switch", 0, CS4231_LOOPBACK, 0, 1, 0),
1819CS4231_SINGLE("Loopback Capture Volume", 0, CS4231_LOOPBACK, 2, 63, 1)
1820};
1821
1822int snd_cs4231_mixer(cs4231_t *chip)
1823{
1824 snd_card_t *card;
1825 unsigned int idx;
1826 int err;
1827
1828 snd_assert(chip != NULL && chip->pcm != NULL, return -EINVAL);
1829
1830 card = chip->card;
1831
1832 strcpy(card->mixername, chip->pcm->name);
1833
1834 for (idx = 0; idx < ARRAY_SIZE(snd_cs4231_controls); idx++) {
1835 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_cs4231_controls[idx], chip))) < 0)
1836 return err;
1837 }
1838 return 0;
1839}
1840
1841EXPORT_SYMBOL(snd_cs4231_out);
1842EXPORT_SYMBOL(snd_cs4231_in);
1843EXPORT_SYMBOL(snd_cs4236_ext_out);
1844EXPORT_SYMBOL(snd_cs4236_ext_in);
1845EXPORT_SYMBOL(snd_cs4231_mce_up);
1846EXPORT_SYMBOL(snd_cs4231_mce_down);
1847EXPORT_SYMBOL(snd_cs4231_interrupt);
1848EXPORT_SYMBOL(snd_cs4231_chip_id);
1849EXPORT_SYMBOL(snd_cs4231_create);
1850EXPORT_SYMBOL(snd_cs4231_pcm);
1851EXPORT_SYMBOL(snd_cs4231_mixer);
1852EXPORT_SYMBOL(snd_cs4231_timer);
1853EXPORT_SYMBOL(snd_cs4231_info_single);
1854EXPORT_SYMBOL(snd_cs4231_get_single);
1855EXPORT_SYMBOL(snd_cs4231_put_single);
1856EXPORT_SYMBOL(snd_cs4231_info_double);
1857EXPORT_SYMBOL(snd_cs4231_get_double);
1858EXPORT_SYMBOL(snd_cs4231_put_double);
1859
1860/*
1861 * INIT part
1862 */
1863
1864static int __init alsa_cs4231_init(void)
1865{
1866 return 0;
1867}
1868
1869static void __exit alsa_cs4231_exit(void)
1870{
1871}
1872
1873module_init(alsa_cs4231_init)
1874module_exit(alsa_cs4231_exit)