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